blob: 4fb8fa08bc68f0c6258e7ea75cccc04ed047b98b [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
Kalesh Singh8b802912021-03-31 12:13:56 -040026void Stability::forceDowngradeToStability(const sp<IBinder>& binder, Level level) {
Steven Morelande35fef32021-03-23 01:38:24 +000027 // Downgrading a remote binder would require also copying the version from
28 // the binder sent here. In practice though, we don't need to downgrade the
29 // stability of a remote binder, since this would as an effect only restrict
30 // what we can do to it.
31 LOG_ALWAYS_FATAL_IF(!binder || !binder->localBinder(), "Can only downgrade local binder");
32
Steven Moreland16a41062021-07-23 13:35:25 -070033 status_t result = setRepr(binder.get(), level, REPR_LOG | REPR_ALLOW_DOWNGRADE);
Steven Morelande35fef32021-03-23 01:38:24 +000034 LOG_ALWAYS_FATAL_IF(result != OK, "Should only mark known object.");
35}
36
Kalesh Singh8b802912021-03-31 12:13:56 -040037void Stability::forceDowngradeToLocalStability(const sp<IBinder>& binder) {
38 forceDowngradeToStability(binder, getLocalLevel());
39}
40
41void Stability::forceDowngradeToSystemStability(const sp<IBinder>& binder) {
42 forceDowngradeToStability(binder, Level::SYSTEM);
43}
44
45void Stability::forceDowngradeToVendorStability(const sp<IBinder>& binder) {
46 forceDowngradeToStability(binder, Level::VENDOR);
47}
48
Steven Morelanddea3cf92019-07-16 18:06:55 -070049void Stability::markCompilationUnit(IBinder* binder) {
Steven Moreland16a41062021-07-23 13:35:25 -070050 status_t result = setRepr(binder, getLocalLevel(), REPR_LOG);
Steven Morelanddea3cf92019-07-16 18:06:55 -070051 LOG_ALWAYS_FATAL_IF(result != OK, "Should only mark known object.");
52}
53
54void Stability::markVintf(IBinder* binder) {
Steven Moreland16a41062021-07-23 13:35:25 -070055 status_t result = setRepr(binder, Level::VINTF, REPR_LOG);
Steven Morelanddea3cf92019-07-16 18:06:55 -070056 LOG_ALWAYS_FATAL_IF(result != OK, "Should only mark known object.");
57}
58
Steven Morelande5a6a872021-05-19 22:11:37 +000059std::string Stability::debugToString(const sp<IBinder>& binder) {
Steven Moreland16a41062021-07-23 13:35:25 -070060 return levelString(getRepr(binder.get()));
Steven Moreland64ae9172019-08-02 20:45:15 -070061}
62
Steven Morelandc709dd82019-08-05 20:30:14 -070063void Stability::markVndk(IBinder* binder) {
Steven Moreland16a41062021-07-23 13:35:25 -070064 status_t result = setRepr(binder, Level::VENDOR, REPR_LOG);
Steven Morelandc709dd82019-08-05 20:30:14 -070065 LOG_ALWAYS_FATAL_IF(result != OK, "Should only mark known object.");
66}
67
Steven Moreland86a17f82019-09-10 10:18:00 -070068bool Stability::requiresVintfDeclaration(const sp<IBinder>& binder) {
Steven Moreland16a41062021-07-23 13:35:25 -070069 return check(getRepr(binder.get()), Level::VINTF);
Steven Moreland86a17f82019-09-10 10:18:00 -070070}
71
Steven Moreland2a9f32f2019-07-31 17:51:25 -070072void Stability::tryMarkCompilationUnit(IBinder* binder) {
Tomasz Wasilczyke97f3a82024-04-30 10:37:32 -070073 std::ignore = setRepr(binder, getLocalLevel(), REPR_NONE);
Steven Moreland2f2c4e02020-07-28 18:11:21 +000074}
75
Steven Morelandd5beffc2023-12-15 21:58:38 +000076// after deprecation of the VNDK, these should be aliases. At some point
77// all references to __ANDROID_VNDK__ should be replaced by __ANDROID_VENDOR__
78// but for right now, check that this condition holds because some
79// places check __ANDROID_VNDK__ and some places check __ANDROID_VENDOR__
80#if defined(__ANDROID_VNDK__) != defined(__ANDROID_VENDOR__)
81#error "__ANDROID_VNDK__ and __ANDROID_VENDOR__ should be aliases"
82#endif
83
Steven Moreland89ddfc52020-11-13 02:39:26 +000084Stability::Level Stability::getLocalLevel() {
Steven Morelandb6c7e222021-02-18 19:20:14 +000085#ifdef __ANDROID_APEX__
Tomasz Wasilczyk5d7b4ca2023-10-05 18:15:40 +000086#error "APEX can't use libbinder (must use libbinder_ndk)"
Steven Morelandb6c7e222021-02-18 19:20:14 +000087#endif
88
Steven Moreland2f2c4e02020-07-28 18:11:21 +000089#ifdef __ANDROID_VNDK__
Steven Morelandb6c7e222021-02-18 19:20:14 +000090 return Level::VENDOR;
Steven Moreland2f2c4e02020-07-28 18:11:21 +000091#else
92 // TODO(b/139325195): split up stability levels for system/APEX.
93 return Level::SYSTEM;
94#endif
Steven Moreland2a9f32f2019-07-31 17:51:25 -070095}
96
Steven Moreland16a41062021-07-23 13:35:25 -070097status_t Stability::setRepr(IBinder* binder, int32_t setting, uint32_t flags) {
Steven Morelande35fef32021-03-23 01:38:24 +000098 bool log = flags & REPR_LOG;
99 bool allowDowngrade = flags & REPR_ALLOW_DOWNGRADE;
100
Steven Moreland16a41062021-07-23 13:35:25 -0700101 int16_t current = getRepr(binder);
Steven Morelanddea3cf92019-07-16 18:06:55 -0700102
103 // null binder is always written w/ 'UNDECLARED' stability
104 if (binder == nullptr) {
Steven Moreland16a41062021-07-23 13:35:25 -0700105 if (setting == UNDECLARED) {
Steven Morelanddea3cf92019-07-16 18:06:55 -0700106 return OK;
107 } else {
Steven Moreland2a9f32f2019-07-31 17:51:25 -0700108 if (log) {
Steven Moreland16a41062021-07-23 13:35:25 -0700109 ALOGE("Null binder written with stability %s.", levelString(setting).c_str());
Steven Moreland2a9f32f2019-07-31 17:51:25 -0700110 }
Steven Morelanddea3cf92019-07-16 18:06:55 -0700111 return BAD_TYPE;
112 }
113 }
114
Steven Moreland16a41062021-07-23 13:35:25 -0700115 if (!isDeclaredLevel(setting)) {
Steven Moreland2a9f32f2019-07-31 17:51:25 -0700116 if (log) {
Steven Moreland16a41062021-07-23 13:35:25 -0700117 ALOGE("Can only set known stability, not %d.", setting);
Steven Morelanddea3cf92019-07-16 18:06:55 -0700118 }
Steven Moreland2a9f32f2019-07-31 17:51:25 -0700119 return BAD_TYPE;
Steven Morelanddea3cf92019-07-16 18:06:55 -0700120 }
Steven Moreland16a41062021-07-23 13:35:25 -0700121 Level levelSetting = static_cast<Level>(setting);
Steven Morelanddea3cf92019-07-16 18:06:55 -0700122
Steven Morelande35fef32021-03-23 01:38:24 +0000123 if (current == setting) return OK;
124
Steven Moreland16a41062021-07-23 13:35:25 -0700125 bool hasAlreadyBeenSet = current != Level::UNDECLARED;
126 bool isAllowedDowngrade = allowDowngrade && check(current, levelSetting);
Steven Morelande35fef32021-03-23 01:38:24 +0000127 if (hasAlreadyBeenSet && !isAllowedDowngrade) {
Steven Moreland2a9f32f2019-07-31 17:51:25 -0700128 if (log) {
Steven Moreland89ddfc52020-11-13 02:39:26 +0000129 ALOGE("Interface being set with %s but it is already marked as %s",
Steven Moreland16a41062021-07-23 13:35:25 -0700130 levelString(setting).c_str(), levelString(current).c_str());
Steven Moreland2a9f32f2019-07-31 17:51:25 -0700131 }
Steven Morelanddea3cf92019-07-16 18:06:55 -0700132 return BAD_TYPE;
133 }
134
Steven Morelande35fef32021-03-23 01:38:24 +0000135 if (isAllowedDowngrade) {
Steven Moreland16a41062021-07-23 13:35:25 -0700136 ALOGI("Interface set with %s downgraded to %s stability", levelString(current).c_str(),
137 levelString(setting).c_str());
Steven Morelande35fef32021-03-23 01:38:24 +0000138 }
Steven Morelanddea3cf92019-07-16 18:06:55 -0700139
Steven Morelanda7fb0182020-02-26 16:02:08 -0800140 BBinder* local = binder->localBinder();
141 if (local != nullptr) {
Steven Moreland16a41062021-07-23 13:35:25 -0700142 local->mStability = setting;
Steven Morelanda7fb0182020-02-26 16:02:08 -0800143 } else {
Steven Moreland16a41062021-07-23 13:35:25 -0700144 binder->remoteBinder()->mStability = setting;
Steven Morelanda7fb0182020-02-26 16:02:08 -0800145 }
Steven Morelanddea3cf92019-07-16 18:06:55 -0700146
147 return OK;
148}
149
Steven Moreland16a41062021-07-23 13:35:25 -0700150int16_t Stability::getRepr(IBinder* binder) {
Steven Moreland89ddfc52020-11-13 02:39:26 +0000151 if (binder == nullptr) {
Steven Moreland16a41062021-07-23 13:35:25 -0700152 return Level::UNDECLARED;
Steven Moreland89ddfc52020-11-13 02:39:26 +0000153 }
Steven Morelanddea3cf92019-07-16 18:06:55 -0700154
Steven Morelanda7fb0182020-02-26 16:02:08 -0800155 BBinder* local = binder->localBinder();
156 if (local != nullptr) {
Steven Moreland16a41062021-07-23 13:35:25 -0700157 return local->mStability;
Steven Morelanda7fb0182020-02-26 16:02:08 -0800158 }
159
Steven Moreland16a41062021-07-23 13:35:25 -0700160 return binder->remoteBinder()->mStability;
Steven Morelanddea3cf92019-07-16 18:06:55 -0700161}
162
Steven Moreland16a41062021-07-23 13:35:25 -0700163bool Stability::check(int16_t provided, Level required) {
164 bool stable = (provided & required) == required;
Steven Morelanddea3cf92019-07-16 18:06:55 -0700165
Steven Moreland16a41062021-07-23 13:35:25 -0700166 if (provided != UNDECLARED && !isDeclaredLevel(provided)) {
167 ALOGE("Unknown stability when checking interface stability %d.", provided);
Steven Morelanddea3cf92019-07-16 18:06:55 -0700168
169 stable = false;
170 }
171
Steven Morelanddea3cf92019-07-16 18:06:55 -0700172 return stable;
173}
174
Steven Moreland16a41062021-07-23 13:35:25 -0700175bool Stability::isDeclaredLevel(int32_t stability) {
Steven Morelanddea3cf92019-07-16 18:06:55 -0700176 return stability == VENDOR || stability == SYSTEM || stability == VINTF;
177}
178
Steven Moreland16a41062021-07-23 13:35:25 -0700179std::string Stability::levelString(int32_t level) {
Steven Moreland89ddfc52020-11-13 02:39:26 +0000180 switch (level) {
Steven Morelanddea3cf92019-07-16 18:06:55 -0700181 case Level::UNDECLARED: return "undeclared stability";
182 case Level::VENDOR: return "vendor stability";
183 case Level::SYSTEM: return "system stability";
184 case Level::VINTF: return "vintf stability";
185 }
Steven Moreland89ddfc52020-11-13 02:39:26 +0000186 return "unknown stability " + std::to_string(level);
Steven Morelanddea3cf92019-07-16 18:06:55 -0700187}
188
189} // namespace internal
Steven Moreland86a17f82019-09-10 10:18:00 -0700190} // namespace stability