blob: f12ef4e35b76dab175210a7ce851677b24d14ad5 [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 Morelandb6c7e222021-02-18 19:20:14 +0000103#ifdef __ANDROID_APEX__
104#error APEX can't use libbinder (must use libbinder_ndk)
105#endif
106
Steven Moreland2f2c4e02020-07-28 18:11:21 +0000107#ifdef __ANDROID_VNDK__
Steven Morelandb6c7e222021-02-18 19:20:14 +0000108 return Level::VENDOR;
Steven Moreland2f2c4e02020-07-28 18:11:21 +0000109#else
110 // TODO(b/139325195): split up stability levels for system/APEX.
111 return Level::SYSTEM;
112#endif
Steven Moreland2a9f32f2019-07-31 17:51:25 -0700113}
114
Steven Morelande35fef32021-03-23 01:38:24 +0000115status_t Stability::setRepr(IBinder* binder, int32_t representation, uint32_t flags) {
116 bool log = flags & REPR_LOG;
117 bool allowDowngrade = flags & REPR_ALLOW_DOWNGRADE;
118
Steven Moreland89ddfc52020-11-13 02:39:26 +0000119 auto current = getCategory(binder);
120 auto setting = Category::fromRepr(representation);
121
122 // If we have ahold of a binder with a newer declared version, then it
123 // should support older versions, and we will simply write our parcels with
124 // the current wire parcel format.
125 if (setting.version < kBinderWireFormatOldest) {
126 // always log, because this shouldn't happen
127 ALOGE("Cannot accept binder with older binder wire protocol version "
128 "%u. Versions less than %u are unsupported.", setting.version,
129 kBinderWireFormatOldest);
130 return BAD_TYPE;
131 }
Steven Morelanddea3cf92019-07-16 18:06:55 -0700132
133 // null binder is always written w/ 'UNDECLARED' stability
134 if (binder == nullptr) {
Steven Moreland89ddfc52020-11-13 02:39:26 +0000135 if (setting.level == UNDECLARED) {
Steven Morelanddea3cf92019-07-16 18:06:55 -0700136 return OK;
137 } else {
Steven Moreland2a9f32f2019-07-31 17:51:25 -0700138 if (log) {
139 ALOGE("Null binder written with stability %s.",
Steven Moreland89ddfc52020-11-13 02:39:26 +0000140 levelString(setting.level).c_str());
Steven Moreland2a9f32f2019-07-31 17:51:25 -0700141 }
Steven Morelanddea3cf92019-07-16 18:06:55 -0700142 return BAD_TYPE;
143 }
144 }
145
Steven Moreland89ddfc52020-11-13 02:39:26 +0000146 if (!isDeclaredLevel(setting.level)) {
Steven Moreland2a9f32f2019-07-31 17:51:25 -0700147 if (log) {
Steven Moreland89ddfc52020-11-13 02:39:26 +0000148 ALOGE("Can only set known stability, not %u.", setting.level);
Steven Morelanddea3cf92019-07-16 18:06:55 -0700149 }
Steven Moreland2a9f32f2019-07-31 17:51:25 -0700150 return BAD_TYPE;
Steven Morelanddea3cf92019-07-16 18:06:55 -0700151 }
152
Steven Morelande35fef32021-03-23 01:38:24 +0000153 if (current == setting) return OK;
154
155 bool hasAlreadyBeenSet = current.repr() != 0;
156 bool isAllowedDowngrade = allowDowngrade && check(current, setting.level);
157 if (hasAlreadyBeenSet && !isAllowedDowngrade) {
Steven Moreland2a9f32f2019-07-31 17:51:25 -0700158 if (log) {
Steven Moreland89ddfc52020-11-13 02:39:26 +0000159 ALOGE("Interface being set with %s but it is already marked as %s",
160 setting.debugString().c_str(),
161 current.debugString().c_str());
Steven Moreland2a9f32f2019-07-31 17:51:25 -0700162 }
Steven Morelanddea3cf92019-07-16 18:06:55 -0700163 return BAD_TYPE;
164 }
165
Steven Morelande35fef32021-03-23 01:38:24 +0000166 if (isAllowedDowngrade) {
167 ALOGI("Interface set with %s downgraded to %s stability",
168 current.debugString().c_str(),
169 setting.debugString().c_str());
170 }
Steven Morelanddea3cf92019-07-16 18:06:55 -0700171
Steven Morelanda7fb0182020-02-26 16:02:08 -0800172 BBinder* local = binder->localBinder();
173 if (local != nullptr) {
Steven Moreland89ddfc52020-11-13 02:39:26 +0000174 local->mStability = setting.repr();
Steven Morelanda7fb0182020-02-26 16:02:08 -0800175 } else {
Steven Moreland89ddfc52020-11-13 02:39:26 +0000176 binder->remoteBinder()->mStability = setting.repr();
Steven Morelanda7fb0182020-02-26 16:02:08 -0800177 }
Steven Morelanddea3cf92019-07-16 18:06:55 -0700178
179 return OK;
180}
181
Steven Moreland89ddfc52020-11-13 02:39:26 +0000182Stability::Category Stability::getCategory(IBinder* binder) {
183 if (binder == nullptr) {
184 return Category::currentFromLevel(Level::UNDECLARED);
185 }
Steven Morelanddea3cf92019-07-16 18:06:55 -0700186
Steven Morelanda7fb0182020-02-26 16:02:08 -0800187 BBinder* local = binder->localBinder();
188 if (local != nullptr) {
Steven Moreland89ddfc52020-11-13 02:39:26 +0000189 return Category::fromRepr(local->mStability);
Steven Morelanda7fb0182020-02-26 16:02:08 -0800190 }
191
Steven Moreland89ddfc52020-11-13 02:39:26 +0000192 return Category::fromRepr(binder->remoteBinder()->mStability);
Steven Morelanddea3cf92019-07-16 18:06:55 -0700193}
194
Steven Moreland89ddfc52020-11-13 02:39:26 +0000195bool Stability::check(Category provided, Level required) {
196 bool stable = (provided.level & required) == required;
Steven Morelanddea3cf92019-07-16 18:06:55 -0700197
Steven Moreland89ddfc52020-11-13 02:39:26 +0000198 if (provided.level != UNDECLARED && !isDeclaredLevel(provided.level)) {
199 ALOGE("Unknown stability when checking interface stability %d.",
200 provided.level);
Steven Morelanddea3cf92019-07-16 18:06:55 -0700201
202 stable = false;
203 }
204
Steven Morelanddea3cf92019-07-16 18:06:55 -0700205 return stable;
206}
207
Steven Moreland89ddfc52020-11-13 02:39:26 +0000208bool Stability::isDeclaredLevel(Level stability) {
Steven Morelanddea3cf92019-07-16 18:06:55 -0700209 return stability == VENDOR || stability == SYSTEM || stability == VINTF;
210}
211
Steven Moreland89ddfc52020-11-13 02:39:26 +0000212std::string Stability::levelString(Level level) {
213 switch (level) {
Steven Morelanddea3cf92019-07-16 18:06:55 -0700214 case Level::UNDECLARED: return "undeclared stability";
215 case Level::VENDOR: return "vendor stability";
216 case Level::SYSTEM: return "system stability";
217 case Level::VINTF: return "vintf stability";
218 }
Steven Moreland89ddfc52020-11-13 02:39:26 +0000219 return "unknown stability " + std::to_string(level);
Steven Morelanddea3cf92019-07-16 18:06:55 -0700220}
221
222} // namespace internal
Steven Moreland86a17f82019-09-10 10:18:00 -0700223} // namespace stability