Steven Moreland | dea3cf9 | 2019-07-16 18:06:55 -0700 | [diff] [blame] | 1 | /* |
| 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 Moreland | 89ddfc5 | 2020-11-13 02:39:26 +0000 | [diff] [blame] | 16 | #define LOG_TAG "Stability" |
| 17 | |
Steven Moreland | dea3cf9 | 2019-07-16 18:06:55 -0700 | [diff] [blame] | 18 | #include <binder/Stability.h> |
| 19 | |
Steven Moreland | a7fb018 | 2020-02-26 16:02:08 -0800 | [diff] [blame] | 20 | #include <binder/BpBinder.h> |
| 21 | #include <binder/Binder.h> |
| 22 | |
Steven Moreland | dea3cf9 | 2019-07-16 18:06:55 -0700 | [diff] [blame] | 23 | namespace android { |
| 24 | namespace internal { |
| 25 | |
Steven Moreland | 89ddfc5 | 2020-11-13 02:39:26 +0000 | [diff] [blame] | 26 | // the libbinder parcel format is currently unstable |
| 27 | |
| 28 | // oldest version which is supported |
| 29 | constexpr uint8_t kBinderWireFormatOldest = 1; |
| 30 | // current version |
| 31 | constexpr uint8_t kBinderWireFormatVersion = 1; |
| 32 | |
| 33 | Stability::Category Stability::Category::currentFromLevel(Level level) { |
| 34 | return { |
| 35 | .version = kBinderWireFormatVersion, |
Steven Moreland | 89ddfc5 | 2020-11-13 02:39:26 +0000 | [diff] [blame] | 36 | .level = level, |
| 37 | }; |
| 38 | } |
| 39 | |
Kalesh Singh | 8b80291 | 2021-03-31 12:13:56 -0400 | [diff] [blame] | 40 | void Stability::forceDowngradeToStability(const sp<IBinder>& binder, Level level) { |
Steven Moreland | e35fef3 | 2021-03-23 01:38:24 +0000 | [diff] [blame] | 41 | // 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 Singh | 8b80291 | 2021-03-31 12:13:56 -0400 | [diff] [blame] | 47 | auto stability = Category::currentFromLevel(level); |
Steven Moreland | e35fef3 | 2021-03-23 01:38:24 +0000 | [diff] [blame] | 48 | 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 Singh | 8b80291 | 2021-03-31 12:13:56 -0400 | [diff] [blame] | 52 | void Stability::forceDowngradeToLocalStability(const sp<IBinder>& binder) { |
| 53 | forceDowngradeToStability(binder, getLocalLevel()); |
| 54 | } |
| 55 | |
| 56 | void Stability::forceDowngradeToSystemStability(const sp<IBinder>& binder) { |
| 57 | forceDowngradeToStability(binder, Level::SYSTEM); |
| 58 | } |
| 59 | |
| 60 | void Stability::forceDowngradeToVendorStability(const sp<IBinder>& binder) { |
| 61 | forceDowngradeToStability(binder, Level::VENDOR); |
| 62 | } |
| 63 | |
Steven Moreland | 89ddfc5 | 2020-11-13 02:39:26 +0000 | [diff] [blame] | 64 | std::string Stability::Category::debugString() { |
| 65 | return levelString(level) + " wire protocol version " |
| 66 | + std::to_string(version); |
| 67 | } |
| 68 | |
Steven Moreland | dea3cf9 | 2019-07-16 18:06:55 -0700 | [diff] [blame] | 69 | void Stability::markCompilationUnit(IBinder* binder) { |
Steven Moreland | 89ddfc5 | 2020-11-13 02:39:26 +0000 | [diff] [blame] | 70 | auto stability = Category::currentFromLevel(getLocalLevel()); |
Steven Moreland | e35fef3 | 2021-03-23 01:38:24 +0000 | [diff] [blame] | 71 | status_t result = setRepr(binder, stability.repr(), REPR_LOG); |
Steven Moreland | dea3cf9 | 2019-07-16 18:06:55 -0700 | [diff] [blame] | 72 | LOG_ALWAYS_FATAL_IF(result != OK, "Should only mark known object."); |
| 73 | } |
| 74 | |
| 75 | void Stability::markVintf(IBinder* binder) { |
Steven Moreland | 89ddfc5 | 2020-11-13 02:39:26 +0000 | [diff] [blame] | 76 | auto stability = Category::currentFromLevel(Level::VINTF); |
Steven Moreland | e35fef3 | 2021-03-23 01:38:24 +0000 | [diff] [blame] | 77 | status_t result = setRepr(binder, stability.repr(), REPR_LOG); |
Steven Moreland | dea3cf9 | 2019-07-16 18:06:55 -0700 | [diff] [blame] | 78 | LOG_ALWAYS_FATAL_IF(result != OK, "Should only mark known object."); |
| 79 | } |
| 80 | |
Steven Moreland | e5a6a87 | 2021-05-19 22:11:37 +0000 | [diff] [blame] | 81 | std::string Stability::debugToString(const sp<IBinder>& binder) { |
Steven Moreland | 89ddfc5 | 2020-11-13 02:39:26 +0000 | [diff] [blame] | 82 | auto stability = getCategory(binder.get()); |
Steven Moreland | e5a6a87 | 2021-05-19 22:11:37 +0000 | [diff] [blame] | 83 | return stability.debugString(); |
Steven Moreland | 64ae917 | 2019-08-02 20:45:15 -0700 | [diff] [blame] | 84 | } |
| 85 | |
Steven Moreland | c709dd8 | 2019-08-05 20:30:14 -0700 | [diff] [blame] | 86 | void Stability::markVndk(IBinder* binder) { |
Steven Moreland | 89ddfc5 | 2020-11-13 02:39:26 +0000 | [diff] [blame] | 87 | auto stability = Category::currentFromLevel(Level::VENDOR); |
Steven Moreland | e35fef3 | 2021-03-23 01:38:24 +0000 | [diff] [blame] | 88 | status_t result = setRepr(binder, stability.repr(), REPR_LOG); |
Steven Moreland | c709dd8 | 2019-08-05 20:30:14 -0700 | [diff] [blame] | 89 | LOG_ALWAYS_FATAL_IF(result != OK, "Should only mark known object."); |
| 90 | } |
| 91 | |
Steven Moreland | 86a17f8 | 2019-09-10 10:18:00 -0700 | [diff] [blame] | 92 | bool Stability::requiresVintfDeclaration(const sp<IBinder>& binder) { |
Steven Moreland | 89ddfc5 | 2020-11-13 02:39:26 +0000 | [diff] [blame] | 93 | return check(getCategory(binder.get()), Level::VINTF); |
Steven Moreland | 86a17f8 | 2019-09-10 10:18:00 -0700 | [diff] [blame] | 94 | } |
| 95 | |
Steven Moreland | 2a9f32f | 2019-07-31 17:51:25 -0700 | [diff] [blame] | 96 | void Stability::tryMarkCompilationUnit(IBinder* binder) { |
Steven Moreland | 89ddfc5 | 2020-11-13 02:39:26 +0000 | [diff] [blame] | 97 | auto stability = Category::currentFromLevel(getLocalLevel()); |
Steven Moreland | e35fef3 | 2021-03-23 01:38:24 +0000 | [diff] [blame] | 98 | (void) setRepr(binder, stability.repr(), REPR_NONE); |
Steven Moreland | 2f2c4e0 | 2020-07-28 18:11:21 +0000 | [diff] [blame] | 99 | } |
| 100 | |
Steven Moreland | 89ddfc5 | 2020-11-13 02:39:26 +0000 | [diff] [blame] | 101 | Stability::Level Stability::getLocalLevel() { |
Steven Moreland | 2f2c4e0 | 2020-07-28 18:11:21 +0000 | [diff] [blame] | 102 | #ifdef __ANDROID_VNDK__ |
| 103 | #ifdef __ANDROID_APEX__ |
| 104 | // TODO(b/142684679) avoid use_vendor on system APEXes |
| 105 | #if !defined(__ANDROID_APEX_COM_ANDROID_MEDIA_SWCODEC__) \ |
| 106 | && !defined(__ANDROID_APEX_TEST_COM_ANDROID_MEDIA_SWCODEC__) |
| 107 | #error VNDK + APEX only defined for com.android.media.swcodec |
| 108 | #endif |
| 109 | // TODO(b/142684679) avoid use_vendor on system APEXes |
| 110 | return Level::SYSTEM; |
| 111 | #else |
| 112 | return Level::VENDOR; |
| 113 | #endif |
| 114 | #else |
| 115 | // TODO(b/139325195): split up stability levels for system/APEX. |
| 116 | return Level::SYSTEM; |
| 117 | #endif |
Steven Moreland | 2a9f32f | 2019-07-31 17:51:25 -0700 | [diff] [blame] | 118 | } |
| 119 | |
Steven Moreland | e35fef3 | 2021-03-23 01:38:24 +0000 | [diff] [blame] | 120 | status_t Stability::setRepr(IBinder* binder, int32_t representation, uint32_t flags) { |
| 121 | bool log = flags & REPR_LOG; |
| 122 | bool allowDowngrade = flags & REPR_ALLOW_DOWNGRADE; |
| 123 | |
Steven Moreland | 89ddfc5 | 2020-11-13 02:39:26 +0000 | [diff] [blame] | 124 | auto current = getCategory(binder); |
| 125 | auto setting = Category::fromRepr(representation); |
| 126 | |
| 127 | // If we have ahold of a binder with a newer declared version, then it |
| 128 | // should support older versions, and we will simply write our parcels with |
| 129 | // the current wire parcel format. |
| 130 | if (setting.version < kBinderWireFormatOldest) { |
| 131 | // always log, because this shouldn't happen |
| 132 | ALOGE("Cannot accept binder with older binder wire protocol version " |
| 133 | "%u. Versions less than %u are unsupported.", setting.version, |
| 134 | kBinderWireFormatOldest); |
| 135 | return BAD_TYPE; |
| 136 | } |
Steven Moreland | dea3cf9 | 2019-07-16 18:06:55 -0700 | [diff] [blame] | 137 | |
| 138 | // null binder is always written w/ 'UNDECLARED' stability |
| 139 | if (binder == nullptr) { |
Steven Moreland | 89ddfc5 | 2020-11-13 02:39:26 +0000 | [diff] [blame] | 140 | if (setting.level == UNDECLARED) { |
Steven Moreland | dea3cf9 | 2019-07-16 18:06:55 -0700 | [diff] [blame] | 141 | return OK; |
| 142 | } else { |
Steven Moreland | 2a9f32f | 2019-07-31 17:51:25 -0700 | [diff] [blame] | 143 | if (log) { |
| 144 | ALOGE("Null binder written with stability %s.", |
Steven Moreland | 89ddfc5 | 2020-11-13 02:39:26 +0000 | [diff] [blame] | 145 | levelString(setting.level).c_str()); |
Steven Moreland | 2a9f32f | 2019-07-31 17:51:25 -0700 | [diff] [blame] | 146 | } |
Steven Moreland | dea3cf9 | 2019-07-16 18:06:55 -0700 | [diff] [blame] | 147 | return BAD_TYPE; |
| 148 | } |
| 149 | } |
| 150 | |
Steven Moreland | 89ddfc5 | 2020-11-13 02:39:26 +0000 | [diff] [blame] | 151 | if (!isDeclaredLevel(setting.level)) { |
Steven Moreland | 2a9f32f | 2019-07-31 17:51:25 -0700 | [diff] [blame] | 152 | if (log) { |
Steven Moreland | 89ddfc5 | 2020-11-13 02:39:26 +0000 | [diff] [blame] | 153 | ALOGE("Can only set known stability, not %u.", setting.level); |
Steven Moreland | dea3cf9 | 2019-07-16 18:06:55 -0700 | [diff] [blame] | 154 | } |
Steven Moreland | 2a9f32f | 2019-07-31 17:51:25 -0700 | [diff] [blame] | 155 | return BAD_TYPE; |
Steven Moreland | dea3cf9 | 2019-07-16 18:06:55 -0700 | [diff] [blame] | 156 | } |
| 157 | |
Steven Moreland | e35fef3 | 2021-03-23 01:38:24 +0000 | [diff] [blame] | 158 | if (current == setting) return OK; |
| 159 | |
| 160 | bool hasAlreadyBeenSet = current.repr() != 0; |
| 161 | bool isAllowedDowngrade = allowDowngrade && check(current, setting.level); |
| 162 | if (hasAlreadyBeenSet && !isAllowedDowngrade) { |
Steven Moreland | 2a9f32f | 2019-07-31 17:51:25 -0700 | [diff] [blame] | 163 | if (log) { |
Steven Moreland | 89ddfc5 | 2020-11-13 02:39:26 +0000 | [diff] [blame] | 164 | ALOGE("Interface being set with %s but it is already marked as %s", |
| 165 | setting.debugString().c_str(), |
| 166 | current.debugString().c_str()); |
Steven Moreland | 2a9f32f | 2019-07-31 17:51:25 -0700 | [diff] [blame] | 167 | } |
Steven Moreland | dea3cf9 | 2019-07-16 18:06:55 -0700 | [diff] [blame] | 168 | return BAD_TYPE; |
| 169 | } |
| 170 | |
Steven Moreland | e35fef3 | 2021-03-23 01:38:24 +0000 | [diff] [blame] | 171 | if (isAllowedDowngrade) { |
| 172 | ALOGI("Interface set with %s downgraded to %s stability", |
| 173 | current.debugString().c_str(), |
| 174 | setting.debugString().c_str()); |
| 175 | } |
Steven Moreland | dea3cf9 | 2019-07-16 18:06:55 -0700 | [diff] [blame] | 176 | |
Steven Moreland | a7fb018 | 2020-02-26 16:02:08 -0800 | [diff] [blame] | 177 | BBinder* local = binder->localBinder(); |
| 178 | if (local != nullptr) { |
Steven Moreland | 89ddfc5 | 2020-11-13 02:39:26 +0000 | [diff] [blame] | 179 | local->mStability = setting.repr(); |
Steven Moreland | a7fb018 | 2020-02-26 16:02:08 -0800 | [diff] [blame] | 180 | } else { |
Steven Moreland | 89ddfc5 | 2020-11-13 02:39:26 +0000 | [diff] [blame] | 181 | binder->remoteBinder()->mStability = setting.repr(); |
Steven Moreland | a7fb018 | 2020-02-26 16:02:08 -0800 | [diff] [blame] | 182 | } |
Steven Moreland | dea3cf9 | 2019-07-16 18:06:55 -0700 | [diff] [blame] | 183 | |
| 184 | return OK; |
| 185 | } |
| 186 | |
Steven Moreland | 89ddfc5 | 2020-11-13 02:39:26 +0000 | [diff] [blame] | 187 | Stability::Category Stability::getCategory(IBinder* binder) { |
| 188 | if (binder == nullptr) { |
| 189 | return Category::currentFromLevel(Level::UNDECLARED); |
| 190 | } |
Steven Moreland | dea3cf9 | 2019-07-16 18:06:55 -0700 | [diff] [blame] | 191 | |
Steven Moreland | a7fb018 | 2020-02-26 16:02:08 -0800 | [diff] [blame] | 192 | BBinder* local = binder->localBinder(); |
| 193 | if (local != nullptr) { |
Steven Moreland | 89ddfc5 | 2020-11-13 02:39:26 +0000 | [diff] [blame] | 194 | return Category::fromRepr(local->mStability); |
Steven Moreland | a7fb018 | 2020-02-26 16:02:08 -0800 | [diff] [blame] | 195 | } |
| 196 | |
Steven Moreland | 89ddfc5 | 2020-11-13 02:39:26 +0000 | [diff] [blame] | 197 | return Category::fromRepr(binder->remoteBinder()->mStability); |
Steven Moreland | dea3cf9 | 2019-07-16 18:06:55 -0700 | [diff] [blame] | 198 | } |
| 199 | |
Steven Moreland | 89ddfc5 | 2020-11-13 02:39:26 +0000 | [diff] [blame] | 200 | bool Stability::check(Category provided, Level required) { |
| 201 | bool stable = (provided.level & required) == required; |
Steven Moreland | dea3cf9 | 2019-07-16 18:06:55 -0700 | [diff] [blame] | 202 | |
Steven Moreland | 89ddfc5 | 2020-11-13 02:39:26 +0000 | [diff] [blame] | 203 | if (provided.level != UNDECLARED && !isDeclaredLevel(provided.level)) { |
| 204 | ALOGE("Unknown stability when checking interface stability %d.", |
| 205 | provided.level); |
Steven Moreland | dea3cf9 | 2019-07-16 18:06:55 -0700 | [diff] [blame] | 206 | |
| 207 | stable = false; |
| 208 | } |
| 209 | |
Steven Moreland | dea3cf9 | 2019-07-16 18:06:55 -0700 | [diff] [blame] | 210 | return stable; |
| 211 | } |
| 212 | |
Steven Moreland | 89ddfc5 | 2020-11-13 02:39:26 +0000 | [diff] [blame] | 213 | bool Stability::isDeclaredLevel(Level stability) { |
Steven Moreland | dea3cf9 | 2019-07-16 18:06:55 -0700 | [diff] [blame] | 214 | return stability == VENDOR || stability == SYSTEM || stability == VINTF; |
| 215 | } |
| 216 | |
Steven Moreland | 89ddfc5 | 2020-11-13 02:39:26 +0000 | [diff] [blame] | 217 | std::string Stability::levelString(Level level) { |
| 218 | switch (level) { |
Steven Moreland | dea3cf9 | 2019-07-16 18:06:55 -0700 | [diff] [blame] | 219 | case Level::UNDECLARED: return "undeclared stability"; |
| 220 | case Level::VENDOR: return "vendor stability"; |
| 221 | case Level::SYSTEM: return "system stability"; |
| 222 | case Level::VINTF: return "vintf stability"; |
| 223 | } |
Steven Moreland | 89ddfc5 | 2020-11-13 02:39:26 +0000 | [diff] [blame] | 224 | return "unknown stability " + std::to_string(level); |
Steven Moreland | dea3cf9 | 2019-07-16 18:06:55 -0700 | [diff] [blame] | 225 | } |
| 226 | |
| 227 | } // namespace internal |
Steven Moreland | 86a17f8 | 2019-09-10 10:18:00 -0700 | [diff] [blame] | 228 | } // namespace stability |