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 | 2f2c4e0 | 2020-07-28 18:11:21 +0000 | [diff] [blame] | 79 | #ifdef __ANDROID_VNDK__ |
Steven Moreland | 15b7029 | 2021-02-18 19:10:15 +0000 | [diff] [blame] | 80 | #ifdef __ANDROID_APEX__ |
| 81 | // TODO(b/142684679) avoid use_vendor on system APEXes |
| 82 | #if !defined(__ANDROID_APEX_COM_ANDROID_MEDIA_SWCODEC__) \ |
| 83 | && !defined(__ANDROID_APEX_TEST_COM_ANDROID_MEDIA_SWCODEC__) |
| 84 | #error VNDK + APEX only defined for com.android.media.swcodec |
| 85 | #endif |
| 86 | // TODO(b/142684679) avoid use_vendor on system APEXes |
| 87 | return Level::SYSTEM; |
| 88 | #else |
| 89 | return Level::VENDOR; |
| 90 | #endif |
Steven Moreland | 2f2c4e0 | 2020-07-28 18:11:21 +0000 | [diff] [blame] | 91 | #else |
| 92 | // TODO(b/139325195): split up stability levels for system/APEX. |
| 93 | return Level::SYSTEM; |
| 94 | #endif |
Steven Moreland | 2a9f32f | 2019-07-31 17:51:25 -0700 | [diff] [blame] | 95 | } |
| 96 | |
Steven Moreland | 89ddfc5 | 2020-11-13 02:39:26 +0000 | [diff] [blame] | 97 | status_t Stability::setRepr(IBinder* binder, int32_t representation, bool log) { |
| 98 | auto current = getCategory(binder); |
| 99 | auto setting = Category::fromRepr(representation); |
| 100 | |
| 101 | // If we have ahold of a binder with a newer declared version, then it |
| 102 | // should support older versions, and we will simply write our parcels with |
| 103 | // the current wire parcel format. |
| 104 | if (setting.version < kBinderWireFormatOldest) { |
| 105 | // always log, because this shouldn't happen |
| 106 | ALOGE("Cannot accept binder with older binder wire protocol version " |
| 107 | "%u. Versions less than %u are unsupported.", setting.version, |
| 108 | kBinderWireFormatOldest); |
| 109 | return BAD_TYPE; |
| 110 | } |
Steven Moreland | dea3cf9 | 2019-07-16 18:06:55 -0700 | [diff] [blame] | 111 | |
| 112 | // null binder is always written w/ 'UNDECLARED' stability |
| 113 | if (binder == nullptr) { |
Steven Moreland | 89ddfc5 | 2020-11-13 02:39:26 +0000 | [diff] [blame] | 114 | if (setting.level == UNDECLARED) { |
Steven Moreland | dea3cf9 | 2019-07-16 18:06:55 -0700 | [diff] [blame] | 115 | return OK; |
| 116 | } else { |
Steven Moreland | 2a9f32f | 2019-07-31 17:51:25 -0700 | [diff] [blame] | 117 | if (log) { |
| 118 | ALOGE("Null binder written with stability %s.", |
Steven Moreland | 89ddfc5 | 2020-11-13 02:39:26 +0000 | [diff] [blame] | 119 | levelString(setting.level).c_str()); |
Steven Moreland | 2a9f32f | 2019-07-31 17:51:25 -0700 | [diff] [blame] | 120 | } |
Steven Moreland | dea3cf9 | 2019-07-16 18:06:55 -0700 | [diff] [blame] | 121 | return BAD_TYPE; |
| 122 | } |
| 123 | } |
| 124 | |
Steven Moreland | 89ddfc5 | 2020-11-13 02:39:26 +0000 | [diff] [blame] | 125 | if (!isDeclaredLevel(setting.level)) { |
Steven Moreland | 2a9f32f | 2019-07-31 17:51:25 -0700 | [diff] [blame] | 126 | if (log) { |
Steven Moreland | 89ddfc5 | 2020-11-13 02:39:26 +0000 | [diff] [blame] | 127 | ALOGE("Can only set known stability, not %u.", setting.level); |
Steven Moreland | dea3cf9 | 2019-07-16 18:06:55 -0700 | [diff] [blame] | 128 | } |
Steven Moreland | 2a9f32f | 2019-07-31 17:51:25 -0700 | [diff] [blame] | 129 | return BAD_TYPE; |
Steven Moreland | dea3cf9 | 2019-07-16 18:06:55 -0700 | [diff] [blame] | 130 | } |
| 131 | |
Steven Moreland | 89ddfc5 | 2020-11-13 02:39:26 +0000 | [diff] [blame] | 132 | if (current.repr() != 0 && current != setting) { |
Steven Moreland | 2a9f32f | 2019-07-31 17:51:25 -0700 | [diff] [blame] | 133 | if (log) { |
Steven Moreland | 89ddfc5 | 2020-11-13 02:39:26 +0000 | [diff] [blame] | 134 | ALOGE("Interface being set with %s but it is already marked as %s", |
| 135 | setting.debugString().c_str(), |
| 136 | current.debugString().c_str()); |
Steven Moreland | 2a9f32f | 2019-07-31 17:51:25 -0700 | [diff] [blame] | 137 | } |
Steven Moreland | dea3cf9 | 2019-07-16 18:06:55 -0700 | [diff] [blame] | 138 | return BAD_TYPE; |
| 139 | } |
| 140 | |
Steven Moreland | 89ddfc5 | 2020-11-13 02:39:26 +0000 | [diff] [blame] | 141 | if (current == setting) return OK; |
Steven Moreland | dea3cf9 | 2019-07-16 18:06:55 -0700 | [diff] [blame] | 142 | |
Steven Moreland | a7fb018 | 2020-02-26 16:02:08 -0800 | [diff] [blame] | 143 | BBinder* local = binder->localBinder(); |
| 144 | if (local != nullptr) { |
Steven Moreland | 89ddfc5 | 2020-11-13 02:39:26 +0000 | [diff] [blame] | 145 | local->mStability = setting.repr(); |
Steven Moreland | a7fb018 | 2020-02-26 16:02:08 -0800 | [diff] [blame] | 146 | } else { |
Steven Moreland | 89ddfc5 | 2020-11-13 02:39:26 +0000 | [diff] [blame] | 147 | binder->remoteBinder()->mStability = setting.repr(); |
Steven Moreland | a7fb018 | 2020-02-26 16:02:08 -0800 | [diff] [blame] | 148 | } |
Steven Moreland | dea3cf9 | 2019-07-16 18:06:55 -0700 | [diff] [blame] | 149 | |
| 150 | return OK; |
| 151 | } |
| 152 | |
Steven Moreland | 89ddfc5 | 2020-11-13 02:39:26 +0000 | [diff] [blame] | 153 | Stability::Category Stability::getCategory(IBinder* binder) { |
| 154 | if (binder == nullptr) { |
| 155 | return Category::currentFromLevel(Level::UNDECLARED); |
| 156 | } |
Steven Moreland | dea3cf9 | 2019-07-16 18:06:55 -0700 | [diff] [blame] | 157 | |
Steven Moreland | a7fb018 | 2020-02-26 16:02:08 -0800 | [diff] [blame] | 158 | BBinder* local = binder->localBinder(); |
| 159 | if (local != nullptr) { |
Steven Moreland | 89ddfc5 | 2020-11-13 02:39:26 +0000 | [diff] [blame] | 160 | return Category::fromRepr(local->mStability); |
Steven Moreland | a7fb018 | 2020-02-26 16:02:08 -0800 | [diff] [blame] | 161 | } |
| 162 | |
Steven Moreland | 89ddfc5 | 2020-11-13 02:39:26 +0000 | [diff] [blame] | 163 | return Category::fromRepr(binder->remoteBinder()->mStability); |
Steven Moreland | dea3cf9 | 2019-07-16 18:06:55 -0700 | [diff] [blame] | 164 | } |
| 165 | |
Steven Moreland | 89ddfc5 | 2020-11-13 02:39:26 +0000 | [diff] [blame] | 166 | bool Stability::check(Category provided, Level required) { |
| 167 | bool stable = (provided.level & required) == required; |
Steven Moreland | dea3cf9 | 2019-07-16 18:06:55 -0700 | [diff] [blame] | 168 | |
Steven Moreland | 89ddfc5 | 2020-11-13 02:39:26 +0000 | [diff] [blame] | 169 | if (provided.level != UNDECLARED && !isDeclaredLevel(provided.level)) { |
| 170 | ALOGE("Unknown stability when checking interface stability %d.", |
| 171 | provided.level); |
Steven Moreland | dea3cf9 | 2019-07-16 18:06:55 -0700 | [diff] [blame] | 172 | |
| 173 | stable = false; |
| 174 | } |
| 175 | |
Steven Moreland | dea3cf9 | 2019-07-16 18:06:55 -0700 | [diff] [blame] | 176 | return stable; |
| 177 | } |
| 178 | |
Steven Moreland | 89ddfc5 | 2020-11-13 02:39:26 +0000 | [diff] [blame] | 179 | bool Stability::isDeclaredLevel(Level stability) { |
Steven Moreland | dea3cf9 | 2019-07-16 18:06:55 -0700 | [diff] [blame] | 180 | return stability == VENDOR || stability == SYSTEM || stability == VINTF; |
| 181 | } |
| 182 | |
Steven Moreland | 89ddfc5 | 2020-11-13 02:39:26 +0000 | [diff] [blame] | 183 | std::string Stability::levelString(Level level) { |
| 184 | switch (level) { |
Steven Moreland | dea3cf9 | 2019-07-16 18:06:55 -0700 | [diff] [blame] | 185 | case Level::UNDECLARED: return "undeclared stability"; |
| 186 | case Level::VENDOR: return "vendor stability"; |
| 187 | case Level::SYSTEM: return "system stability"; |
| 188 | case Level::VINTF: return "vintf stability"; |
| 189 | } |
Steven Moreland | 89ddfc5 | 2020-11-13 02:39:26 +0000 | [diff] [blame] | 190 | return "unknown stability " + std::to_string(level); |
Steven Moreland | dea3cf9 | 2019-07-16 18:06:55 -0700 | [diff] [blame] | 191 | } |
| 192 | |
| 193 | } // namespace internal |
Steven Moreland | 86a17f8 | 2019-09-10 10:18:00 -0700 | [diff] [blame] | 194 | } // namespace stability |