blob: 709cf67127952aaea8c9e6310624a45df9a86579 [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,
36 .reserved = {0},
37 .level = level,
38 };
39}
40
Kalesh Singh8b802912021-03-31 12:13:56 -040041void Stability::forceDowngradeToStability(const sp<IBinder>& binder, Level level) {
Steven Morelande35fef32021-03-23 01:38:24 +000042 // Downgrading a remote binder would require also copying the version from
43 // the binder sent here. In practice though, we don't need to downgrade the
44 // stability of a remote binder, since this would as an effect only restrict
45 // what we can do to it.
46 LOG_ALWAYS_FATAL_IF(!binder || !binder->localBinder(), "Can only downgrade local binder");
47
Kalesh Singh8b802912021-03-31 12:13:56 -040048 auto stability = Category::currentFromLevel(level);
Steven Morelande35fef32021-03-23 01:38:24 +000049 status_t result = setRepr(binder.get(), stability.repr(), REPR_LOG | REPR_ALLOW_DOWNGRADE);
50 LOG_ALWAYS_FATAL_IF(result != OK, "Should only mark known object.");
51}
52
Kalesh Singh8b802912021-03-31 12:13:56 -040053void Stability::forceDowngradeToLocalStability(const sp<IBinder>& binder) {
54 forceDowngradeToStability(binder, getLocalLevel());
55}
56
57void Stability::forceDowngradeToSystemStability(const sp<IBinder>& binder) {
58 forceDowngradeToStability(binder, Level::SYSTEM);
59}
60
61void Stability::forceDowngradeToVendorStability(const sp<IBinder>& binder) {
62 forceDowngradeToStability(binder, Level::VENDOR);
63}
64
Steven Moreland89ddfc52020-11-13 02:39:26 +000065std::string Stability::Category::debugString() {
66 return levelString(level) + " wire protocol version "
67 + std::to_string(version);
68}
69
Steven Morelanddea3cf92019-07-16 18:06:55 -070070void Stability::markCompilationUnit(IBinder* binder) {
Steven Moreland89ddfc52020-11-13 02:39:26 +000071 auto stability = Category::currentFromLevel(getLocalLevel());
Steven Morelande35fef32021-03-23 01:38:24 +000072 status_t result = setRepr(binder, stability.repr(), REPR_LOG);
Steven Morelanddea3cf92019-07-16 18:06:55 -070073 LOG_ALWAYS_FATAL_IF(result != OK, "Should only mark known object.");
74}
75
76void Stability::markVintf(IBinder* binder) {
Steven Moreland89ddfc52020-11-13 02:39:26 +000077 auto stability = Category::currentFromLevel(Level::VINTF);
Steven Morelande35fef32021-03-23 01:38:24 +000078 status_t result = setRepr(binder, stability.repr(), REPR_LOG);
Steven Morelanddea3cf92019-07-16 18:06:55 -070079 LOG_ALWAYS_FATAL_IF(result != OK, "Should only mark known object.");
80}
81
Steven Moreland64ae9172019-08-02 20:45:15 -070082void Stability::debugLogStability(const std::string& tag, const sp<IBinder>& binder) {
Steven Moreland89ddfc52020-11-13 02:39:26 +000083 auto stability = getCategory(binder.get());
84 ALOGE("%s: stability is %s", tag.c_str(), stability.debugString().c_str());
Steven Moreland64ae9172019-08-02 20:45:15 -070085}
86
Steven Morelandc709dd82019-08-05 20:30:14 -070087void Stability::markVndk(IBinder* binder) {
Steven Moreland89ddfc52020-11-13 02:39:26 +000088 auto stability = Category::currentFromLevel(Level::VENDOR);
Steven Morelande35fef32021-03-23 01:38:24 +000089 status_t result = setRepr(binder, stability.repr(), REPR_LOG);
Steven Morelandc709dd82019-08-05 20:30:14 -070090 LOG_ALWAYS_FATAL_IF(result != OK, "Should only mark known object.");
91}
92
Steven Moreland86a17f82019-09-10 10:18:00 -070093bool Stability::requiresVintfDeclaration(const sp<IBinder>& binder) {
Steven Moreland89ddfc52020-11-13 02:39:26 +000094 return check(getCategory(binder.get()), Level::VINTF);
Steven Moreland86a17f82019-09-10 10:18:00 -070095}
96
Steven Moreland2a9f32f2019-07-31 17:51:25 -070097void Stability::tryMarkCompilationUnit(IBinder* binder) {
Steven Moreland89ddfc52020-11-13 02:39:26 +000098 auto stability = Category::currentFromLevel(getLocalLevel());
Steven Morelande35fef32021-03-23 01:38:24 +000099 (void) setRepr(binder, stability.repr(), REPR_NONE);
Steven Moreland2f2c4e02020-07-28 18:11:21 +0000100}
101
Steven Moreland89ddfc52020-11-13 02:39:26 +0000102Stability::Level Stability::getLocalLevel() {
Steven Moreland2f2c4e02020-07-28 18:11:21 +0000103#ifdef __ANDROID_VNDK__
104 #ifdef __ANDROID_APEX__
105 // TODO(b/142684679) avoid use_vendor on system APEXes
106 #if !defined(__ANDROID_APEX_COM_ANDROID_MEDIA_SWCODEC__) \
107 && !defined(__ANDROID_APEX_TEST_COM_ANDROID_MEDIA_SWCODEC__)
108 #error VNDK + APEX only defined for com.android.media.swcodec
109 #endif
110 // TODO(b/142684679) avoid use_vendor on system APEXes
111 return Level::SYSTEM;
112 #else
113 return Level::VENDOR;
114 #endif
115#else
116 // TODO(b/139325195): split up stability levels for system/APEX.
117 return Level::SYSTEM;
118#endif
Steven Moreland2a9f32f2019-07-31 17:51:25 -0700119}
120
Steven Morelande35fef32021-03-23 01:38:24 +0000121status_t Stability::setRepr(IBinder* binder, int32_t representation, uint32_t flags) {
122 bool log = flags & REPR_LOG;
123 bool allowDowngrade = flags & REPR_ALLOW_DOWNGRADE;
124
Steven Moreland89ddfc52020-11-13 02:39:26 +0000125 auto current = getCategory(binder);
126 auto setting = Category::fromRepr(representation);
127
128 // If we have ahold of a binder with a newer declared version, then it
129 // should support older versions, and we will simply write our parcels with
130 // the current wire parcel format.
131 if (setting.version < kBinderWireFormatOldest) {
132 // always log, because this shouldn't happen
133 ALOGE("Cannot accept binder with older binder wire protocol version "
134 "%u. Versions less than %u are unsupported.", setting.version,
135 kBinderWireFormatOldest);
136 return BAD_TYPE;
137 }
Steven Morelanddea3cf92019-07-16 18:06:55 -0700138
139 // null binder is always written w/ 'UNDECLARED' stability
140 if (binder == nullptr) {
Steven Moreland89ddfc52020-11-13 02:39:26 +0000141 if (setting.level == UNDECLARED) {
Steven Morelanddea3cf92019-07-16 18:06:55 -0700142 return OK;
143 } else {
Steven Moreland2a9f32f2019-07-31 17:51:25 -0700144 if (log) {
145 ALOGE("Null binder written with stability %s.",
Steven Moreland89ddfc52020-11-13 02:39:26 +0000146 levelString(setting.level).c_str());
Steven Moreland2a9f32f2019-07-31 17:51:25 -0700147 }
Steven Morelanddea3cf92019-07-16 18:06:55 -0700148 return BAD_TYPE;
149 }
150 }
151
Steven Moreland89ddfc52020-11-13 02:39:26 +0000152 if (!isDeclaredLevel(setting.level)) {
Steven Moreland2a9f32f2019-07-31 17:51:25 -0700153 if (log) {
Steven Moreland89ddfc52020-11-13 02:39:26 +0000154 ALOGE("Can only set known stability, not %u.", setting.level);
Steven Morelanddea3cf92019-07-16 18:06:55 -0700155 }
Steven Moreland2a9f32f2019-07-31 17:51:25 -0700156 return BAD_TYPE;
Steven Morelanddea3cf92019-07-16 18:06:55 -0700157 }
158
Steven Morelande35fef32021-03-23 01:38:24 +0000159 if (current == setting) return OK;
160
161 bool hasAlreadyBeenSet = current.repr() != 0;
162 bool isAllowedDowngrade = allowDowngrade && check(current, setting.level);
163 if (hasAlreadyBeenSet && !isAllowedDowngrade) {
Steven Moreland2a9f32f2019-07-31 17:51:25 -0700164 if (log) {
Steven Moreland89ddfc52020-11-13 02:39:26 +0000165 ALOGE("Interface being set with %s but it is already marked as %s",
166 setting.debugString().c_str(),
167 current.debugString().c_str());
Steven Moreland2a9f32f2019-07-31 17:51:25 -0700168 }
Steven Morelanddea3cf92019-07-16 18:06:55 -0700169 return BAD_TYPE;
170 }
171
Steven Morelande35fef32021-03-23 01:38:24 +0000172 if (isAllowedDowngrade) {
173 ALOGI("Interface set with %s downgraded to %s stability",
174 current.debugString().c_str(),
175 setting.debugString().c_str());
176 }
Steven Morelanddea3cf92019-07-16 18:06:55 -0700177
Steven Morelanda7fb0182020-02-26 16:02:08 -0800178 BBinder* local = binder->localBinder();
179 if (local != nullptr) {
Steven Moreland89ddfc52020-11-13 02:39:26 +0000180 local->mStability = setting.repr();
Steven Morelanda7fb0182020-02-26 16:02:08 -0800181 } else {
Steven Moreland89ddfc52020-11-13 02:39:26 +0000182 binder->remoteBinder()->mStability = setting.repr();
Steven Morelanda7fb0182020-02-26 16:02:08 -0800183 }
Steven Morelanddea3cf92019-07-16 18:06:55 -0700184
185 return OK;
186}
187
Steven Moreland89ddfc52020-11-13 02:39:26 +0000188Stability::Category Stability::getCategory(IBinder* binder) {
189 if (binder == nullptr) {
190 return Category::currentFromLevel(Level::UNDECLARED);
191 }
Steven Morelanddea3cf92019-07-16 18:06:55 -0700192
Steven Morelanda7fb0182020-02-26 16:02:08 -0800193 BBinder* local = binder->localBinder();
194 if (local != nullptr) {
Steven Moreland89ddfc52020-11-13 02:39:26 +0000195 return Category::fromRepr(local->mStability);
Steven Morelanda7fb0182020-02-26 16:02:08 -0800196 }
197
Steven Moreland89ddfc52020-11-13 02:39:26 +0000198 return Category::fromRepr(binder->remoteBinder()->mStability);
Steven Morelanddea3cf92019-07-16 18:06:55 -0700199}
200
Steven Moreland89ddfc52020-11-13 02:39:26 +0000201bool Stability::check(Category provided, Level required) {
202 bool stable = (provided.level & required) == required;
Steven Morelanddea3cf92019-07-16 18:06:55 -0700203
Steven Moreland89ddfc52020-11-13 02:39:26 +0000204 if (provided.level != UNDECLARED && !isDeclaredLevel(provided.level)) {
205 ALOGE("Unknown stability when checking interface stability %d.",
206 provided.level);
Steven Morelanddea3cf92019-07-16 18:06:55 -0700207
208 stable = false;
209 }
210
Steven Morelanddea3cf92019-07-16 18:06:55 -0700211 return stable;
212}
213
Steven Moreland89ddfc52020-11-13 02:39:26 +0000214bool Stability::isDeclaredLevel(Level stability) {
Steven Morelanddea3cf92019-07-16 18:06:55 -0700215 return stability == VENDOR || stability == SYSTEM || stability == VINTF;
216}
217
Steven Moreland89ddfc52020-11-13 02:39:26 +0000218std::string Stability::levelString(Level level) {
219 switch (level) {
Steven Morelanddea3cf92019-07-16 18:06:55 -0700220 case Level::UNDECLARED: return "undeclared stability";
221 case Level::VENDOR: return "vendor stability";
222 case Level::SYSTEM: return "system stability";
223 case Level::VINTF: return "vintf stability";
224 }
Steven Moreland89ddfc52020-11-13 02:39:26 +0000225 return "unknown stability " + std::to_string(level);
Steven Morelanddea3cf92019-07-16 18:06:55 -0700226}
227
228} // namespace internal
Steven Moreland86a17f82019-09-10 10:18:00 -0700229} // namespace stability