blob: dac381974e05b9fafa47049d43cd7e97097ff4cb [file] [log] [blame]
Steven Morelanddea3cf92019-07-16 18:06:55 -07001/*
2 * Copyright (C) 2019 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
Steven Moreland89ddfc52020-11-13 02:39:26 +000016#define LOG_TAG "Stability"
17
Steven Morelanddea3cf92019-07-16 18:06:55 -070018#include <binder/Stability.h>
19
Steven Morelanda7fb0182020-02-26 16:02:08 -080020#include <binder/BpBinder.h>
21#include <binder/Binder.h>
22
Steven Morelanddea3cf92019-07-16 18:06:55 -070023namespace android {
24namespace internal {
25
Steven Moreland89ddfc52020-11-13 02:39:26 +000026// the libbinder parcel format is currently unstable
27
28// oldest version which is supported
29constexpr uint8_t kBinderWireFormatOldest = 1;
30// current version
31constexpr uint8_t kBinderWireFormatVersion = 1;
32
33Stability::Category Stability::Category::currentFromLevel(Level level) {
34 return {
35 .version = kBinderWireFormatVersion,
Steven Moreland89ddfc52020-11-13 02:39:26 +000036 .level = level,
37 };
38}
39
Kalesh Singh8b802912021-03-31 12:13:56 -040040void Stability::forceDowngradeToStability(const sp<IBinder>& binder, Level level) {
Steven Morelande35fef32021-03-23 01:38:24 +000041 // Downgrading a remote binder would require also copying the version from
42 // the binder sent here. In practice though, we don't need to downgrade the
43 // stability of a remote binder, since this would as an effect only restrict
44 // what we can do to it.
45 LOG_ALWAYS_FATAL_IF(!binder || !binder->localBinder(), "Can only downgrade local binder");
46
Kalesh Singh8b802912021-03-31 12:13:56 -040047 auto stability = Category::currentFromLevel(level);
Steven Morelande35fef32021-03-23 01:38:24 +000048 status_t result = setRepr(binder.get(), stability.repr(), REPR_LOG | REPR_ALLOW_DOWNGRADE);
49 LOG_ALWAYS_FATAL_IF(result != OK, "Should only mark known object.");
50}
51
Kalesh Singh8b802912021-03-31 12:13:56 -040052void Stability::forceDowngradeToLocalStability(const sp<IBinder>& binder) {
53 forceDowngradeToStability(binder, getLocalLevel());
54}
55
56void Stability::forceDowngradeToSystemStability(const sp<IBinder>& binder) {
57 forceDowngradeToStability(binder, Level::SYSTEM);
58}
59
60void Stability::forceDowngradeToVendorStability(const sp<IBinder>& binder) {
61 forceDowngradeToStability(binder, Level::VENDOR);
62}
63
Steven Moreland89ddfc52020-11-13 02:39:26 +000064std::string Stability::Category::debugString() {
65 return levelString(level) + " wire protocol version "
66 + std::to_string(version);
67}
68
Steven Morelanddea3cf92019-07-16 18:06:55 -070069void Stability::markCompilationUnit(IBinder* binder) {
Steven Moreland89ddfc52020-11-13 02:39:26 +000070 auto stability = Category::currentFromLevel(getLocalLevel());
Steven Morelande35fef32021-03-23 01:38:24 +000071 status_t result = setRepr(binder, stability.repr(), REPR_LOG);
Steven Morelanddea3cf92019-07-16 18:06:55 -070072 LOG_ALWAYS_FATAL_IF(result != OK, "Should only mark known object.");
73}
74
75void Stability::markVintf(IBinder* binder) {
Steven Moreland89ddfc52020-11-13 02:39:26 +000076 auto stability = Category::currentFromLevel(Level::VINTF);
Steven Morelande35fef32021-03-23 01:38:24 +000077 status_t result = setRepr(binder, stability.repr(), REPR_LOG);
Steven Morelanddea3cf92019-07-16 18:06:55 -070078 LOG_ALWAYS_FATAL_IF(result != OK, "Should only mark known object.");
79}
80
Steven Morelande5a6a872021-05-19 22:11:37 +000081std::string Stability::debugToString(const sp<IBinder>& binder) {
Steven Moreland89ddfc52020-11-13 02:39:26 +000082 auto stability = getCategory(binder.get());
Steven Morelande5a6a872021-05-19 22:11:37 +000083 return stability.debugString();
Steven Moreland64ae9172019-08-02 20:45:15 -070084}
85
Steven Morelandc709dd82019-08-05 20:30:14 -070086void Stability::markVndk(IBinder* binder) {
Steven Moreland89ddfc52020-11-13 02:39:26 +000087 auto stability = Category::currentFromLevel(Level::VENDOR);
Steven Morelande35fef32021-03-23 01:38:24 +000088 status_t result = setRepr(binder, stability.repr(), REPR_LOG);
Steven Morelandc709dd82019-08-05 20:30:14 -070089 LOG_ALWAYS_FATAL_IF(result != OK, "Should only mark known object.");
90}
91
Steven Moreland86a17f82019-09-10 10:18:00 -070092bool Stability::requiresVintfDeclaration(const sp<IBinder>& binder) {
Steven Moreland89ddfc52020-11-13 02:39:26 +000093 return check(getCategory(binder.get()), Level::VINTF);
Steven Moreland86a17f82019-09-10 10:18:00 -070094}
95
Steven Moreland2a9f32f2019-07-31 17:51:25 -070096void Stability::tryMarkCompilationUnit(IBinder* binder) {
Steven Moreland89ddfc52020-11-13 02:39:26 +000097 auto stability = Category::currentFromLevel(getLocalLevel());
Steven Morelande35fef32021-03-23 01:38:24 +000098 (void) setRepr(binder, stability.repr(), REPR_NONE);
Steven Moreland2f2c4e02020-07-28 18:11:21 +000099}
100
Steven Moreland89ddfc52020-11-13 02:39:26 +0000101Stability::Level Stability::getLocalLevel() {
Steven Moreland2f2c4e02020-07-28 18:11:21 +0000102#ifdef __ANDROID_VNDK__
103 #ifdef __ANDROID_APEX__
104 // TODO(b/142684679) avoid use_vendor on system APEXes
105 #if !defined(__ANDROID_APEX_COM_ANDROID_MEDIA_SWCODEC__) \
106 && !defined(__ANDROID_APEX_TEST_COM_ANDROID_MEDIA_SWCODEC__)
107 #error VNDK + APEX only defined for com.android.media.swcodec
108 #endif
109 // TODO(b/142684679) avoid use_vendor on system APEXes
110 return Level::SYSTEM;
111 #else
112 return Level::VENDOR;
113 #endif
114#else
115 // TODO(b/139325195): split up stability levels for system/APEX.
116 return Level::SYSTEM;
117#endif
Steven Moreland2a9f32f2019-07-31 17:51:25 -0700118}
119
Steven Morelande35fef32021-03-23 01:38:24 +0000120status_t Stability::setRepr(IBinder* binder, int32_t representation, uint32_t flags) {
121 bool log = flags & REPR_LOG;
122 bool allowDowngrade = flags & REPR_ALLOW_DOWNGRADE;
123
Steven Moreland89ddfc52020-11-13 02:39:26 +0000124 auto current = getCategory(binder);
125 auto setting = Category::fromRepr(representation);
126
127 // If we have ahold of a binder with a newer declared version, then it
128 // should support older versions, and we will simply write our parcels with
129 // the current wire parcel format.
130 if (setting.version < kBinderWireFormatOldest) {
131 // always log, because this shouldn't happen
132 ALOGE("Cannot accept binder with older binder wire protocol version "
133 "%u. Versions less than %u are unsupported.", setting.version,
134 kBinderWireFormatOldest);
135 return BAD_TYPE;
136 }
Steven Morelanddea3cf92019-07-16 18:06:55 -0700137
138 // null binder is always written w/ 'UNDECLARED' stability
139 if (binder == nullptr) {
Steven Moreland89ddfc52020-11-13 02:39:26 +0000140 if (setting.level == UNDECLARED) {
Steven Morelanddea3cf92019-07-16 18:06:55 -0700141 return OK;
142 } else {
Steven Moreland2a9f32f2019-07-31 17:51:25 -0700143 if (log) {
144 ALOGE("Null binder written with stability %s.",
Steven Moreland89ddfc52020-11-13 02:39:26 +0000145 levelString(setting.level).c_str());
Steven Moreland2a9f32f2019-07-31 17:51:25 -0700146 }
Steven Morelanddea3cf92019-07-16 18:06:55 -0700147 return BAD_TYPE;
148 }
149 }
150
Steven Moreland89ddfc52020-11-13 02:39:26 +0000151 if (!isDeclaredLevel(setting.level)) {
Steven Moreland2a9f32f2019-07-31 17:51:25 -0700152 if (log) {
Steven Moreland89ddfc52020-11-13 02:39:26 +0000153 ALOGE("Can only set known stability, not %u.", setting.level);
Steven Morelanddea3cf92019-07-16 18:06:55 -0700154 }
Steven Moreland2a9f32f2019-07-31 17:51:25 -0700155 return BAD_TYPE;
Steven Morelanddea3cf92019-07-16 18:06:55 -0700156 }
157
Steven Morelande35fef32021-03-23 01:38:24 +0000158 if (current == setting) return OK;
159
160 bool hasAlreadyBeenSet = current.repr() != 0;
161 bool isAllowedDowngrade = allowDowngrade && check(current, setting.level);
162 if (hasAlreadyBeenSet && !isAllowedDowngrade) {
Steven Moreland2a9f32f2019-07-31 17:51:25 -0700163 if (log) {
Steven Moreland89ddfc52020-11-13 02:39:26 +0000164 ALOGE("Interface being set with %s but it is already marked as %s",
165 setting.debugString().c_str(),
166 current.debugString().c_str());
Steven Moreland2a9f32f2019-07-31 17:51:25 -0700167 }
Steven Morelanddea3cf92019-07-16 18:06:55 -0700168 return BAD_TYPE;
169 }
170
Steven Morelande35fef32021-03-23 01:38:24 +0000171 if (isAllowedDowngrade) {
172 ALOGI("Interface set with %s downgraded to %s stability",
173 current.debugString().c_str(),
174 setting.debugString().c_str());
175 }
Steven Morelanddea3cf92019-07-16 18:06:55 -0700176
Steven Morelanda7fb0182020-02-26 16:02:08 -0800177 BBinder* local = binder->localBinder();
178 if (local != nullptr) {
Steven Moreland89ddfc52020-11-13 02:39:26 +0000179 local->mStability = setting.repr();
Steven Morelanda7fb0182020-02-26 16:02:08 -0800180 } else {
Steven Moreland89ddfc52020-11-13 02:39:26 +0000181 binder->remoteBinder()->mStability = setting.repr();
Steven Morelanda7fb0182020-02-26 16:02:08 -0800182 }
Steven Morelanddea3cf92019-07-16 18:06:55 -0700183
184 return OK;
185}
186
Steven Moreland89ddfc52020-11-13 02:39:26 +0000187Stability::Category Stability::getCategory(IBinder* binder) {
188 if (binder == nullptr) {
189 return Category::currentFromLevel(Level::UNDECLARED);
190 }
Steven Morelanddea3cf92019-07-16 18:06:55 -0700191
Steven Morelanda7fb0182020-02-26 16:02:08 -0800192 BBinder* local = binder->localBinder();
193 if (local != nullptr) {
Steven Moreland89ddfc52020-11-13 02:39:26 +0000194 return Category::fromRepr(local->mStability);
Steven Morelanda7fb0182020-02-26 16:02:08 -0800195 }
196
Steven Moreland89ddfc52020-11-13 02:39:26 +0000197 return Category::fromRepr(binder->remoteBinder()->mStability);
Steven Morelanddea3cf92019-07-16 18:06:55 -0700198}
199
Steven Moreland89ddfc52020-11-13 02:39:26 +0000200bool Stability::check(Category provided, Level required) {
201 bool stable = (provided.level & required) == required;
Steven Morelanddea3cf92019-07-16 18:06:55 -0700202
Steven Moreland89ddfc52020-11-13 02:39:26 +0000203 if (provided.level != UNDECLARED && !isDeclaredLevel(provided.level)) {
204 ALOGE("Unknown stability when checking interface stability %d.",
205 provided.level);
Steven Morelanddea3cf92019-07-16 18:06:55 -0700206
207 stable = false;
208 }
209
Steven Morelanddea3cf92019-07-16 18:06:55 -0700210 return stable;
211}
212
Steven Moreland89ddfc52020-11-13 02:39:26 +0000213bool Stability::isDeclaredLevel(Level stability) {
Steven Morelanddea3cf92019-07-16 18:06:55 -0700214 return stability == VENDOR || stability == SYSTEM || stability == VINTF;
215}
216
Steven Moreland89ddfc52020-11-13 02:39:26 +0000217std::string Stability::levelString(Level level) {
218 switch (level) {
Steven Morelanddea3cf92019-07-16 18:06:55 -0700219 case Level::UNDECLARED: return "undeclared stability";
220 case Level::VENDOR: return "vendor stability";
221 case Level::SYSTEM: return "system stability";
222 case Level::VINTF: return "vintf stability";
223 }
Steven Moreland89ddfc52020-11-13 02:39:26 +0000224 return "unknown stability " + std::to_string(level);
Steven Morelanddea3cf92019-07-16 18:06:55 -0700225}
226
227} // namespace internal
Steven Moreland86a17f82019-09-10 10:18:00 -0700228} // namespace stability