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