blob: 00b69d527a5a223d02646a30b13737811a787a66 [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 Morelandb6c7e222021-02-18 19:20:14 +0000102#ifdef __ANDROID_APEX__
103#error APEX can't use libbinder (must use libbinder_ndk)
104#endif
105
Steven Moreland2f2c4e02020-07-28 18:11:21 +0000106#ifdef __ANDROID_VNDK__
Steven Morelandb6c7e222021-02-18 19:20:14 +0000107 return Level::VENDOR;
Steven Moreland2f2c4e02020-07-28 18:11:21 +0000108#else
109 // TODO(b/139325195): split up stability levels for system/APEX.
110 return Level::SYSTEM;
111#endif
Steven Moreland2a9f32f2019-07-31 17:51:25 -0700112}
113
Steven Morelande35fef32021-03-23 01:38:24 +0000114status_t Stability::setRepr(IBinder* binder, int32_t representation, uint32_t flags) {
115 bool log = flags & REPR_LOG;
116 bool allowDowngrade = flags & REPR_ALLOW_DOWNGRADE;
117
Steven Moreland89ddfc52020-11-13 02:39:26 +0000118 auto current = getCategory(binder);
119 auto setting = Category::fromRepr(representation);
120
121 // If we have ahold of a binder with a newer declared version, then it
122 // should support older versions, and we will simply write our parcels with
123 // the current wire parcel format.
124 if (setting.version < kBinderWireFormatOldest) {
125 // always log, because this shouldn't happen
126 ALOGE("Cannot accept binder with older binder wire protocol version "
127 "%u. Versions less than %u are unsupported.", setting.version,
128 kBinderWireFormatOldest);
129 return BAD_TYPE;
130 }
Steven Morelanddea3cf92019-07-16 18:06:55 -0700131
132 // null binder is always written w/ 'UNDECLARED' stability
133 if (binder == nullptr) {
Steven Moreland89ddfc52020-11-13 02:39:26 +0000134 if (setting.level == UNDECLARED) {
Steven Morelanddea3cf92019-07-16 18:06:55 -0700135 return OK;
136 } else {
Steven Moreland2a9f32f2019-07-31 17:51:25 -0700137 if (log) {
138 ALOGE("Null binder written with stability %s.",
Steven Moreland89ddfc52020-11-13 02:39:26 +0000139 levelString(setting.level).c_str());
Steven Moreland2a9f32f2019-07-31 17:51:25 -0700140 }
Steven Morelanddea3cf92019-07-16 18:06:55 -0700141 return BAD_TYPE;
142 }
143 }
144
Steven Moreland89ddfc52020-11-13 02:39:26 +0000145 if (!isDeclaredLevel(setting.level)) {
Steven Moreland2a9f32f2019-07-31 17:51:25 -0700146 if (log) {
Steven Moreland89ddfc52020-11-13 02:39:26 +0000147 ALOGE("Can only set known stability, not %u.", setting.level);
Steven Morelanddea3cf92019-07-16 18:06:55 -0700148 }
Steven Moreland2a9f32f2019-07-31 17:51:25 -0700149 return BAD_TYPE;
Steven Morelanddea3cf92019-07-16 18:06:55 -0700150 }
151
Steven Morelande35fef32021-03-23 01:38:24 +0000152 if (current == setting) return OK;
153
154 bool hasAlreadyBeenSet = current.repr() != 0;
155 bool isAllowedDowngrade = allowDowngrade && check(current, setting.level);
156 if (hasAlreadyBeenSet && !isAllowedDowngrade) {
Steven Moreland2a9f32f2019-07-31 17:51:25 -0700157 if (log) {
Steven Moreland89ddfc52020-11-13 02:39:26 +0000158 ALOGE("Interface being set with %s but it is already marked as %s",
159 setting.debugString().c_str(),
160 current.debugString().c_str());
Steven Moreland2a9f32f2019-07-31 17:51:25 -0700161 }
Steven Morelanddea3cf92019-07-16 18:06:55 -0700162 return BAD_TYPE;
163 }
164
Steven Morelande35fef32021-03-23 01:38:24 +0000165 if (isAllowedDowngrade) {
166 ALOGI("Interface set with %s downgraded to %s stability",
167 current.debugString().c_str(),
168 setting.debugString().c_str());
169 }
Steven Morelanddea3cf92019-07-16 18:06:55 -0700170
Steven Morelanda7fb0182020-02-26 16:02:08 -0800171 BBinder* local = binder->localBinder();
172 if (local != nullptr) {
Steven Moreland89ddfc52020-11-13 02:39:26 +0000173 local->mStability = setting.repr();
Steven Morelanda7fb0182020-02-26 16:02:08 -0800174 } else {
Steven Moreland89ddfc52020-11-13 02:39:26 +0000175 binder->remoteBinder()->mStability = setting.repr();
Steven Morelanda7fb0182020-02-26 16:02:08 -0800176 }
Steven Morelanddea3cf92019-07-16 18:06:55 -0700177
178 return OK;
179}
180
Steven Moreland89ddfc52020-11-13 02:39:26 +0000181Stability::Category Stability::getCategory(IBinder* binder) {
182 if (binder == nullptr) {
183 return Category::currentFromLevel(Level::UNDECLARED);
184 }
Steven Morelanddea3cf92019-07-16 18:06:55 -0700185
Steven Morelanda7fb0182020-02-26 16:02:08 -0800186 BBinder* local = binder->localBinder();
187 if (local != nullptr) {
Steven Moreland89ddfc52020-11-13 02:39:26 +0000188 return Category::fromRepr(local->mStability);
Steven Morelanda7fb0182020-02-26 16:02:08 -0800189 }
190
Steven Moreland89ddfc52020-11-13 02:39:26 +0000191 return Category::fromRepr(binder->remoteBinder()->mStability);
Steven Morelanddea3cf92019-07-16 18:06:55 -0700192}
193
Steven Moreland89ddfc52020-11-13 02:39:26 +0000194bool Stability::check(Category provided, Level required) {
195 bool stable = (provided.level & required) == required;
Steven Morelanddea3cf92019-07-16 18:06:55 -0700196
Steven Moreland89ddfc52020-11-13 02:39:26 +0000197 if (provided.level != UNDECLARED && !isDeclaredLevel(provided.level)) {
198 ALOGE("Unknown stability when checking interface stability %d.",
199 provided.level);
Steven Morelanddea3cf92019-07-16 18:06:55 -0700200
201 stable = false;
202 }
203
Steven Morelanddea3cf92019-07-16 18:06:55 -0700204 return stable;
205}
206
Steven Moreland89ddfc52020-11-13 02:39:26 +0000207bool Stability::isDeclaredLevel(Level stability) {
Steven Morelanddea3cf92019-07-16 18:06:55 -0700208 return stability == VENDOR || stability == SYSTEM || stability == VINTF;
209}
210
Steven Moreland89ddfc52020-11-13 02:39:26 +0000211std::string Stability::levelString(Level level) {
212 switch (level) {
Steven Morelanddea3cf92019-07-16 18:06:55 -0700213 case Level::UNDECLARED: return "undeclared stability";
214 case Level::VENDOR: return "vendor stability";
215 case Level::SYSTEM: return "system stability";
216 case Level::VINTF: return "vintf stability";
217 }
Steven Moreland89ddfc52020-11-13 02:39:26 +0000218 return "unknown stability " + std::to_string(level);
Steven Morelanddea3cf92019-07-16 18:06:55 -0700219}
220
221} // namespace internal
Steven Moreland86a17f82019-09-10 10:18:00 -0700222} // namespace stability