blob: d25c038df50b550bb6da5fa91d481e8fb4ec40b0 [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) {
Steven Moreland16a41062021-07-23 13:35:25 -070073 (void)setRepr(binder, getLocalLevel(), REPR_NONE);
Steven Moreland2f2c4e02020-07-28 18:11:21 +000074}
75
Steven Moreland89ddfc52020-11-13 02:39:26 +000076Stability::Level Stability::getLocalLevel() {
Steven Moreland2f2c4e02020-07-28 18:11:21 +000077#ifdef __ANDROID_VNDK__
78 #ifdef __ANDROID_APEX__
79 // TODO(b/142684679) avoid use_vendor on system APEXes
80 #if !defined(__ANDROID_APEX_COM_ANDROID_MEDIA_SWCODEC__) \
81 && !defined(__ANDROID_APEX_TEST_COM_ANDROID_MEDIA_SWCODEC__)
82 #error VNDK + APEX only defined for com.android.media.swcodec
83 #endif
84 // TODO(b/142684679) avoid use_vendor on system APEXes
85 return Level::SYSTEM;
86 #else
87 return Level::VENDOR;
88 #endif
89#else
90 // TODO(b/139325195): split up stability levels for system/APEX.
91 return Level::SYSTEM;
92#endif
Steven Moreland2a9f32f2019-07-31 17:51:25 -070093}
94
Steven Moreland16a41062021-07-23 13:35:25 -070095status_t Stability::setRepr(IBinder* binder, int32_t setting, uint32_t flags) {
Steven Morelande35fef32021-03-23 01:38:24 +000096 bool log = flags & REPR_LOG;
97 bool allowDowngrade = flags & REPR_ALLOW_DOWNGRADE;
98
Steven Moreland16a41062021-07-23 13:35:25 -070099 int16_t current = getRepr(binder);
Steven Morelanddea3cf92019-07-16 18:06:55 -0700100
101 // null binder is always written w/ 'UNDECLARED' stability
102 if (binder == nullptr) {
Steven Moreland16a41062021-07-23 13:35:25 -0700103 if (setting == UNDECLARED) {
Steven Morelanddea3cf92019-07-16 18:06:55 -0700104 return OK;
105 } else {
Steven Moreland2a9f32f2019-07-31 17:51:25 -0700106 if (log) {
Steven Moreland16a41062021-07-23 13:35:25 -0700107 ALOGE("Null binder written with stability %s.", levelString(setting).c_str());
Steven Moreland2a9f32f2019-07-31 17:51:25 -0700108 }
Steven Morelanddea3cf92019-07-16 18:06:55 -0700109 return BAD_TYPE;
110 }
111 }
112
Steven Moreland16a41062021-07-23 13:35:25 -0700113 if (!isDeclaredLevel(setting)) {
Steven Moreland2a9f32f2019-07-31 17:51:25 -0700114 if (log) {
Steven Moreland16a41062021-07-23 13:35:25 -0700115 ALOGE("Can only set known stability, not %d.", setting);
Steven Morelanddea3cf92019-07-16 18:06:55 -0700116 }
Steven Moreland2a9f32f2019-07-31 17:51:25 -0700117 return BAD_TYPE;
Steven Morelanddea3cf92019-07-16 18:06:55 -0700118 }
Steven Moreland16a41062021-07-23 13:35:25 -0700119 Level levelSetting = static_cast<Level>(setting);
Steven Morelanddea3cf92019-07-16 18:06:55 -0700120
Steven Morelande35fef32021-03-23 01:38:24 +0000121 if (current == setting) return OK;
122
Steven Moreland16a41062021-07-23 13:35:25 -0700123 bool hasAlreadyBeenSet = current != Level::UNDECLARED;
124 bool isAllowedDowngrade = allowDowngrade && check(current, levelSetting);
Steven Morelande35fef32021-03-23 01:38:24 +0000125 if (hasAlreadyBeenSet && !isAllowedDowngrade) {
Steven Moreland2a9f32f2019-07-31 17:51:25 -0700126 if (log) {
Steven Moreland89ddfc52020-11-13 02:39:26 +0000127 ALOGE("Interface being set with %s but it is already marked as %s",
Steven Moreland16a41062021-07-23 13:35:25 -0700128 levelString(setting).c_str(), levelString(current).c_str());
Steven Moreland2a9f32f2019-07-31 17:51:25 -0700129 }
Steven Morelanddea3cf92019-07-16 18:06:55 -0700130 return BAD_TYPE;
131 }
132
Steven Morelande35fef32021-03-23 01:38:24 +0000133 if (isAllowedDowngrade) {
Steven Moreland16a41062021-07-23 13:35:25 -0700134 ALOGI("Interface set with %s downgraded to %s stability", levelString(current).c_str(),
135 levelString(setting).c_str());
Steven Morelande35fef32021-03-23 01:38:24 +0000136 }
Steven Morelanddea3cf92019-07-16 18:06:55 -0700137
Steven Morelanda7fb0182020-02-26 16:02:08 -0800138 BBinder* local = binder->localBinder();
139 if (local != nullptr) {
Steven Moreland16a41062021-07-23 13:35:25 -0700140 local->mStability = setting;
Steven Morelanda7fb0182020-02-26 16:02:08 -0800141 } else {
Steven Moreland16a41062021-07-23 13:35:25 -0700142 binder->remoteBinder()->mStability = setting;
Steven Morelanda7fb0182020-02-26 16:02:08 -0800143 }
Steven Morelanddea3cf92019-07-16 18:06:55 -0700144
145 return OK;
146}
147
Steven Moreland16a41062021-07-23 13:35:25 -0700148int16_t Stability::getRepr(IBinder* binder) {
Steven Moreland89ddfc52020-11-13 02:39:26 +0000149 if (binder == nullptr) {
Steven Moreland16a41062021-07-23 13:35:25 -0700150 return Level::UNDECLARED;
Steven Moreland89ddfc52020-11-13 02:39:26 +0000151 }
Steven Morelanddea3cf92019-07-16 18:06:55 -0700152
Steven Morelanda7fb0182020-02-26 16:02:08 -0800153 BBinder* local = binder->localBinder();
154 if (local != nullptr) {
Steven Moreland16a41062021-07-23 13:35:25 -0700155 return local->mStability;
Steven Morelanda7fb0182020-02-26 16:02:08 -0800156 }
157
Steven Moreland16a41062021-07-23 13:35:25 -0700158 return binder->remoteBinder()->mStability;
Steven Morelanddea3cf92019-07-16 18:06:55 -0700159}
160
Steven Moreland16a41062021-07-23 13:35:25 -0700161bool Stability::check(int16_t provided, Level required) {
162 bool stable = (provided & required) == required;
Steven Morelanddea3cf92019-07-16 18:06:55 -0700163
Steven Moreland16a41062021-07-23 13:35:25 -0700164 if (provided != UNDECLARED && !isDeclaredLevel(provided)) {
165 ALOGE("Unknown stability when checking interface stability %d.", provided);
Steven Morelanddea3cf92019-07-16 18:06:55 -0700166
167 stable = false;
168 }
169
Steven Morelanddea3cf92019-07-16 18:06:55 -0700170 return stable;
171}
172
Steven Moreland16a41062021-07-23 13:35:25 -0700173bool Stability::isDeclaredLevel(int32_t stability) {
Steven Morelanddea3cf92019-07-16 18:06:55 -0700174 return stability == VENDOR || stability == SYSTEM || stability == VINTF;
175}
176
Steven Moreland16a41062021-07-23 13:35:25 -0700177std::string Stability::levelString(int32_t level) {
Steven Moreland89ddfc52020-11-13 02:39:26 +0000178 switch (level) {
Steven Morelanddea3cf92019-07-16 18:06:55 -0700179 case Level::UNDECLARED: return "undeclared stability";
180 case Level::VENDOR: return "vendor stability";
181 case Level::SYSTEM: return "system stability";
182 case Level::VINTF: return "vintf stability";
183 }
Steven Moreland89ddfc52020-11-13 02:39:26 +0000184 return "unknown stability " + std::to_string(level);
Steven Morelanddea3cf92019-07-16 18:06:55 -0700185}
186
187} // namespace internal
Steven Moreland86a17f82019-09-10 10:18:00 -0700188} // namespace stability