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 | |
| 41 | std::string Stability::Category::debugString() { |
| 42 | return levelString(level) + " wire protocol version " |
| 43 | + std::to_string(version); |
| 44 | } |
| 45 | |
Steven Moreland | dea3cf9 | 2019-07-16 18:06:55 -0700 | [diff] [blame] | 46 | void Stability::markCompilationUnit(IBinder* binder) { |
Steven Moreland | 89ddfc5 | 2020-11-13 02:39:26 +0000 | [diff] [blame] | 47 | auto stability = Category::currentFromLevel(getLocalLevel()); |
| 48 | status_t result = setRepr(binder, stability.repr(), true /*log*/); |
Steven Moreland | dea3cf9 | 2019-07-16 18:06:55 -0700 | [diff] [blame] | 49 | LOG_ALWAYS_FATAL_IF(result != OK, "Should only mark known object."); |
| 50 | } |
| 51 | |
| 52 | void Stability::markVintf(IBinder* binder) { |
Steven Moreland | 89ddfc5 | 2020-11-13 02:39:26 +0000 | [diff] [blame] | 53 | auto stability = Category::currentFromLevel(Level::VINTF); |
| 54 | status_t result = setRepr(binder, stability.repr(), true /*log*/); |
Steven Moreland | dea3cf9 | 2019-07-16 18:06:55 -0700 | [diff] [blame] | 55 | LOG_ALWAYS_FATAL_IF(result != OK, "Should only mark known object."); |
| 56 | } |
| 57 | |
Steven Moreland | 64ae917 | 2019-08-02 20:45:15 -0700 | [diff] [blame] | 58 | void Stability::debugLogStability(const std::string& tag, const sp<IBinder>& binder) { |
Steven Moreland | 89ddfc5 | 2020-11-13 02:39:26 +0000 | [diff] [blame] | 59 | auto stability = getCategory(binder.get()); |
| 60 | ALOGE("%s: stability is %s", tag.c_str(), stability.debugString().c_str()); |
Steven Moreland | 64ae917 | 2019-08-02 20:45:15 -0700 | [diff] [blame] | 61 | } |
| 62 | |
Steven Moreland | c709dd8 | 2019-08-05 20:30:14 -0700 | [diff] [blame] | 63 | void Stability::markVndk(IBinder* binder) { |
Steven Moreland | 89ddfc5 | 2020-11-13 02:39:26 +0000 | [diff] [blame] | 64 | auto stability = Category::currentFromLevel(Level::VENDOR); |
| 65 | status_t result = setRepr(binder, stability.repr(), true /*log*/); |
Steven Moreland | c709dd8 | 2019-08-05 20:30:14 -0700 | [diff] [blame] | 66 | LOG_ALWAYS_FATAL_IF(result != OK, "Should only mark known object."); |
| 67 | } |
| 68 | |
Steven Moreland | 86a17f8 | 2019-09-10 10:18:00 -0700 | [diff] [blame] | 69 | bool Stability::requiresVintfDeclaration(const sp<IBinder>& binder) { |
Steven Moreland | 89ddfc5 | 2020-11-13 02:39:26 +0000 | [diff] [blame] | 70 | return check(getCategory(binder.get()), Level::VINTF); |
Steven Moreland | 86a17f8 | 2019-09-10 10:18:00 -0700 | [diff] [blame] | 71 | } |
| 72 | |
Steven Moreland | 2a9f32f | 2019-07-31 17:51:25 -0700 | [diff] [blame] | 73 | void Stability::tryMarkCompilationUnit(IBinder* binder) { |
Steven Moreland | 89ddfc5 | 2020-11-13 02:39:26 +0000 | [diff] [blame] | 74 | auto stability = Category::currentFromLevel(getLocalLevel()); |
| 75 | (void) setRepr(binder, stability.repr(), false /*log*/); |
Steven Moreland | 2f2c4e0 | 2020-07-28 18:11:21 +0000 | [diff] [blame] | 76 | } |
| 77 | |
Steven Moreland | 89ddfc5 | 2020-11-13 02:39:26 +0000 | [diff] [blame] | 78 | Stability::Level Stability::getLocalLevel() { |
Steven Moreland | b6c7e22 | 2021-02-18 19:20:14 +0000 | [diff] [blame^] | 79 | #ifdef __ANDROID_APEX__ |
| 80 | #error APEX can't use libbinder (must use libbinder_ndk) |
| 81 | #endif |
| 82 | |
Steven Moreland | 2f2c4e0 | 2020-07-28 18:11:21 +0000 | [diff] [blame] | 83 | #ifdef __ANDROID_VNDK__ |
Steven Moreland | b6c7e22 | 2021-02-18 19:20:14 +0000 | [diff] [blame^] | 84 | return Level::VENDOR; |
Steven Moreland | 2f2c4e0 | 2020-07-28 18:11:21 +0000 | [diff] [blame] | 85 | #else |
| 86 | // TODO(b/139325195): split up stability levels for system/APEX. |
| 87 | return Level::SYSTEM; |
| 88 | #endif |
Steven Moreland | 2a9f32f | 2019-07-31 17:51:25 -0700 | [diff] [blame] | 89 | } |
| 90 | |
Steven Moreland | 89ddfc5 | 2020-11-13 02:39:26 +0000 | [diff] [blame] | 91 | status_t Stability::setRepr(IBinder* binder, int32_t representation, bool log) { |
| 92 | auto current = getCategory(binder); |
| 93 | auto setting = Category::fromRepr(representation); |
| 94 | |
| 95 | // If we have ahold of a binder with a newer declared version, then it |
| 96 | // should support older versions, and we will simply write our parcels with |
| 97 | // the current wire parcel format. |
| 98 | if (setting.version < kBinderWireFormatOldest) { |
| 99 | // always log, because this shouldn't happen |
| 100 | ALOGE("Cannot accept binder with older binder wire protocol version " |
| 101 | "%u. Versions less than %u are unsupported.", setting.version, |
| 102 | kBinderWireFormatOldest); |
| 103 | return BAD_TYPE; |
| 104 | } |
Steven Moreland | dea3cf9 | 2019-07-16 18:06:55 -0700 | [diff] [blame] | 105 | |
| 106 | // null binder is always written w/ 'UNDECLARED' stability |
| 107 | if (binder == nullptr) { |
Steven Moreland | 89ddfc5 | 2020-11-13 02:39:26 +0000 | [diff] [blame] | 108 | if (setting.level == UNDECLARED) { |
Steven Moreland | dea3cf9 | 2019-07-16 18:06:55 -0700 | [diff] [blame] | 109 | return OK; |
| 110 | } else { |
Steven Moreland | 2a9f32f | 2019-07-31 17:51:25 -0700 | [diff] [blame] | 111 | if (log) { |
| 112 | ALOGE("Null binder written with stability %s.", |
Steven Moreland | 89ddfc5 | 2020-11-13 02:39:26 +0000 | [diff] [blame] | 113 | levelString(setting.level).c_str()); |
Steven Moreland | 2a9f32f | 2019-07-31 17:51:25 -0700 | [diff] [blame] | 114 | } |
Steven Moreland | dea3cf9 | 2019-07-16 18:06:55 -0700 | [diff] [blame] | 115 | return BAD_TYPE; |
| 116 | } |
| 117 | } |
| 118 | |
Steven Moreland | 89ddfc5 | 2020-11-13 02:39:26 +0000 | [diff] [blame] | 119 | if (!isDeclaredLevel(setting.level)) { |
Steven Moreland | 2a9f32f | 2019-07-31 17:51:25 -0700 | [diff] [blame] | 120 | if (log) { |
Steven Moreland | 89ddfc5 | 2020-11-13 02:39:26 +0000 | [diff] [blame] | 121 | ALOGE("Can only set known stability, not %u.", setting.level); |
Steven Moreland | dea3cf9 | 2019-07-16 18:06:55 -0700 | [diff] [blame] | 122 | } |
Steven Moreland | 2a9f32f | 2019-07-31 17:51:25 -0700 | [diff] [blame] | 123 | return BAD_TYPE; |
Steven Moreland | dea3cf9 | 2019-07-16 18:06:55 -0700 | [diff] [blame] | 124 | } |
| 125 | |
Steven Moreland | 89ddfc5 | 2020-11-13 02:39:26 +0000 | [diff] [blame] | 126 | if (current.repr() != 0 && current != setting) { |
Steven Moreland | 2a9f32f | 2019-07-31 17:51:25 -0700 | [diff] [blame] | 127 | if (log) { |
Steven Moreland | 89ddfc5 | 2020-11-13 02:39:26 +0000 | [diff] [blame] | 128 | ALOGE("Interface being set with %s but it is already marked as %s", |
| 129 | setting.debugString().c_str(), |
| 130 | current.debugString().c_str()); |
Steven Moreland | 2a9f32f | 2019-07-31 17:51:25 -0700 | [diff] [blame] | 131 | } |
Steven Moreland | dea3cf9 | 2019-07-16 18:06:55 -0700 | [diff] [blame] | 132 | return BAD_TYPE; |
| 133 | } |
| 134 | |
Steven Moreland | 89ddfc5 | 2020-11-13 02:39:26 +0000 | [diff] [blame] | 135 | if (current == setting) return OK; |
Steven Moreland | dea3cf9 | 2019-07-16 18:06:55 -0700 | [diff] [blame] | 136 | |
Steven Moreland | a7fb018 | 2020-02-26 16:02:08 -0800 | [diff] [blame] | 137 | BBinder* local = binder->localBinder(); |
| 138 | if (local != nullptr) { |
Steven Moreland | 89ddfc5 | 2020-11-13 02:39:26 +0000 | [diff] [blame] | 139 | local->mStability = setting.repr(); |
Steven Moreland | a7fb018 | 2020-02-26 16:02:08 -0800 | [diff] [blame] | 140 | } else { |
Steven Moreland | 89ddfc5 | 2020-11-13 02:39:26 +0000 | [diff] [blame] | 141 | binder->remoteBinder()->mStability = setting.repr(); |
Steven Moreland | a7fb018 | 2020-02-26 16:02:08 -0800 | [diff] [blame] | 142 | } |
Steven Moreland | dea3cf9 | 2019-07-16 18:06:55 -0700 | [diff] [blame] | 143 | |
| 144 | return OK; |
| 145 | } |
| 146 | |
Steven Moreland | 89ddfc5 | 2020-11-13 02:39:26 +0000 | [diff] [blame] | 147 | Stability::Category Stability::getCategory(IBinder* binder) { |
| 148 | if (binder == nullptr) { |
| 149 | return Category::currentFromLevel(Level::UNDECLARED); |
| 150 | } |
Steven Moreland | dea3cf9 | 2019-07-16 18:06:55 -0700 | [diff] [blame] | 151 | |
Steven Moreland | a7fb018 | 2020-02-26 16:02:08 -0800 | [diff] [blame] | 152 | BBinder* local = binder->localBinder(); |
| 153 | if (local != nullptr) { |
Steven Moreland | 89ddfc5 | 2020-11-13 02:39:26 +0000 | [diff] [blame] | 154 | return Category::fromRepr(local->mStability); |
Steven Moreland | a7fb018 | 2020-02-26 16:02:08 -0800 | [diff] [blame] | 155 | } |
| 156 | |
Steven Moreland | 89ddfc5 | 2020-11-13 02:39:26 +0000 | [diff] [blame] | 157 | return Category::fromRepr(binder->remoteBinder()->mStability); |
Steven Moreland | dea3cf9 | 2019-07-16 18:06:55 -0700 | [diff] [blame] | 158 | } |
| 159 | |
Steven Moreland | 89ddfc5 | 2020-11-13 02:39:26 +0000 | [diff] [blame] | 160 | bool Stability::check(Category provided, Level required) { |
| 161 | bool stable = (provided.level & required) == required; |
Steven Moreland | dea3cf9 | 2019-07-16 18:06:55 -0700 | [diff] [blame] | 162 | |
Steven Moreland | 89ddfc5 | 2020-11-13 02:39:26 +0000 | [diff] [blame] | 163 | if (provided.level != UNDECLARED && !isDeclaredLevel(provided.level)) { |
| 164 | ALOGE("Unknown stability when checking interface stability %d.", |
| 165 | provided.level); |
Steven Moreland | dea3cf9 | 2019-07-16 18:06:55 -0700 | [diff] [blame] | 166 | |
| 167 | stable = false; |
| 168 | } |
| 169 | |
Steven Moreland | dea3cf9 | 2019-07-16 18:06:55 -0700 | [diff] [blame] | 170 | return stable; |
| 171 | } |
| 172 | |
Steven Moreland | 89ddfc5 | 2020-11-13 02:39:26 +0000 | [diff] [blame] | 173 | bool Stability::isDeclaredLevel(Level stability) { |
Steven Moreland | dea3cf9 | 2019-07-16 18:06:55 -0700 | [diff] [blame] | 174 | return stability == VENDOR || stability == SYSTEM || stability == VINTF; |
| 175 | } |
| 176 | |
Steven Moreland | 89ddfc5 | 2020-11-13 02:39:26 +0000 | [diff] [blame] | 177 | std::string Stability::levelString(Level level) { |
| 178 | switch (level) { |
Steven Moreland | dea3cf9 | 2019-07-16 18:06:55 -0700 | [diff] [blame] | 179 | 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 Moreland | 89ddfc5 | 2020-11-13 02:39:26 +0000 | [diff] [blame] | 184 | return "unknown stability " + std::to_string(level); |
Steven Moreland | dea3cf9 | 2019-07-16 18:06:55 -0700 | [diff] [blame] | 185 | } |
| 186 | |
| 187 | } // namespace internal |
Steven Moreland | 86a17f8 | 2019-09-10 10:18:00 -0700 | [diff] [blame] | 188 | } // namespace stability |