| 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 | dea3cf9 | 2019-07-16 18:06:55 -0700 | [diff] [blame] | 16 | #include <binder/Stability.h> | 
|  | 17 |  | 
| Steven Moreland | a7fb018 | 2020-02-26 16:02:08 -0800 | [diff] [blame] | 18 | #include <binder/BpBinder.h> | 
|  | 19 | #include <binder/Binder.h> | 
|  | 20 |  | 
| Steven Moreland | dea3cf9 | 2019-07-16 18:06:55 -0700 | [diff] [blame] | 21 | namespace android { | 
|  | 22 | namespace internal { | 
|  | 23 |  | 
|  | 24 | void Stability::markCompilationUnit(IBinder* binder) { | 
| Steven Moreland | 2f2c4e0 | 2020-07-28 18:11:21 +0000 | [diff] [blame] | 25 | status_t result = set(binder, getLocalStability(), true /*log*/); | 
| Steven Moreland | dea3cf9 | 2019-07-16 18:06:55 -0700 | [diff] [blame] | 26 | LOG_ALWAYS_FATAL_IF(result != OK, "Should only mark known object."); | 
|  | 27 | } | 
|  | 28 |  | 
|  | 29 | void Stability::markVintf(IBinder* binder) { | 
| Steven Moreland | 2a9f32f | 2019-07-31 17:51:25 -0700 | [diff] [blame] | 30 | status_t result = set(binder, Level::VINTF, true /*log*/); | 
| Steven Moreland | dea3cf9 | 2019-07-16 18:06:55 -0700 | [diff] [blame] | 31 | LOG_ALWAYS_FATAL_IF(result != OK, "Should only mark known object."); | 
|  | 32 | } | 
|  | 33 |  | 
| Steven Moreland | 64ae917 | 2019-08-02 20:45:15 -0700 | [diff] [blame] | 34 | void Stability::debugLogStability(const std::string& tag, const sp<IBinder>& binder) { | 
|  | 35 | ALOGE("%s: stability is %s", tag.c_str(), stabilityString(get(binder.get())).c_str()); | 
|  | 36 | } | 
|  | 37 |  | 
| Steven Moreland | c709dd8 | 2019-08-05 20:30:14 -0700 | [diff] [blame] | 38 | void Stability::markVndk(IBinder* binder) { | 
|  | 39 | status_t result = set(binder, Level::VENDOR, true /*log*/); | 
|  | 40 | LOG_ALWAYS_FATAL_IF(result != OK, "Should only mark known object."); | 
|  | 41 | } | 
|  | 42 |  | 
| Steven Moreland | 86a17f8 | 2019-09-10 10:18:00 -0700 | [diff] [blame] | 43 | bool Stability::requiresVintfDeclaration(const sp<IBinder>& binder) { | 
|  | 44 | return check(get(binder.get()), Level::VINTF); | 
|  | 45 | } | 
|  | 46 |  | 
| Steven Moreland | 2a9f32f | 2019-07-31 17:51:25 -0700 | [diff] [blame] | 47 | void Stability::tryMarkCompilationUnit(IBinder* binder) { | 
| Steven Moreland | 2f2c4e0 | 2020-07-28 18:11:21 +0000 | [diff] [blame] | 48 | (void) set(binder, getLocalStability(), false /*log*/); | 
|  | 49 | } | 
|  | 50 |  | 
|  | 51 | Stability::Level Stability::getLocalStability() { | 
|  | 52 | #ifdef __ANDROID_VNDK__ | 
| Steven Moreland | e70d8fb | 2020-08-20 00:15:14 +0000 | [diff] [blame] | 53 | #ifdef __ANDROID_APEX__ | 
|  | 54 | // TODO(b/142684679) avoid use_vendor on system APEXes | 
|  | 55 | #if !defined(__ANDROID_APEX_COM_ANDROID_MEDIA_SWCODEC__) \ | 
|  | 56 | && !defined(__ANDROID_APEX_TEST_COM_ANDROID_MEDIA_SWCODEC__) | 
|  | 57 | #error VNDK + APEX only defined for com.android.media.swcodec | 
|  | 58 | #endif | 
|  | 59 | // TODO(b/142684679) avoid use_vendor on system APEXes | 
|  | 60 | return Level::SYSTEM; | 
|  | 61 | #else | 
|  | 62 | return Level::VENDOR; | 
|  | 63 | #endif | 
| Steven Moreland | 2f2c4e0 | 2020-07-28 18:11:21 +0000 | [diff] [blame] | 64 | #else | 
|  | 65 | // TODO(b/139325195): split up stability levels for system/APEX. | 
|  | 66 | return Level::SYSTEM; | 
|  | 67 | #endif | 
| Steven Moreland | 2a9f32f | 2019-07-31 17:51:25 -0700 | [diff] [blame] | 68 | } | 
|  | 69 |  | 
|  | 70 | status_t Stability::set(IBinder* binder, int32_t stability, bool log) { | 
| Steven Moreland | dea3cf9 | 2019-07-16 18:06:55 -0700 | [diff] [blame] | 71 | Level currentStability = get(binder); | 
|  | 72 |  | 
|  | 73 | // null binder is always written w/ 'UNDECLARED' stability | 
|  | 74 | if (binder == nullptr) { | 
|  | 75 | if (stability == UNDECLARED) { | 
|  | 76 | return OK; | 
|  | 77 | } else { | 
| Steven Moreland | 2a9f32f | 2019-07-31 17:51:25 -0700 | [diff] [blame] | 78 | if (log) { | 
|  | 79 | ALOGE("Null binder written with stability %s.", | 
|  | 80 | stabilityString(stability).c_str()); | 
|  | 81 | } | 
| Steven Moreland | dea3cf9 | 2019-07-16 18:06:55 -0700 | [diff] [blame] | 82 | return BAD_TYPE; | 
|  | 83 | } | 
|  | 84 | } | 
|  | 85 |  | 
|  | 86 | if (!isDeclaredStability(stability)) { | 
| Steven Moreland | 2a9f32f | 2019-07-31 17:51:25 -0700 | [diff] [blame] | 87 | if (log) { | 
| Steven Moreland | dea3cf9 | 2019-07-16 18:06:55 -0700 | [diff] [blame] | 88 | ALOGE("Can only set known stability, not %d.", stability); | 
| Steven Moreland | dea3cf9 | 2019-07-16 18:06:55 -0700 | [diff] [blame] | 89 | } | 
| Steven Moreland | 2a9f32f | 2019-07-31 17:51:25 -0700 | [diff] [blame] | 90 | return BAD_TYPE; | 
| Steven Moreland | dea3cf9 | 2019-07-16 18:06:55 -0700 | [diff] [blame] | 91 | } | 
|  | 92 |  | 
|  | 93 | if (currentStability != Level::UNDECLARED && currentStability != stability) { | 
| Steven Moreland | 2a9f32f | 2019-07-31 17:51:25 -0700 | [diff] [blame] | 94 | if (log) { | 
|  | 95 | ALOGE("Interface being set with %s but it is already marked as %s.", | 
| Steven Moreland | 732de21 | 2019-08-02 20:41:10 -0700 | [diff] [blame] | 96 | stabilityString(stability).c_str(), stabilityString(currentStability).c_str()); | 
| Steven Moreland | 2a9f32f | 2019-07-31 17:51:25 -0700 | [diff] [blame] | 97 | } | 
| Steven Moreland | dea3cf9 | 2019-07-16 18:06:55 -0700 | [diff] [blame] | 98 | return BAD_TYPE; | 
|  | 99 | } | 
|  | 100 |  | 
|  | 101 | if (currentStability == stability) return OK; | 
|  | 102 |  | 
| Steven Moreland | a7fb018 | 2020-02-26 16:02:08 -0800 | [diff] [blame] | 103 | BBinder* local = binder->localBinder(); | 
|  | 104 | if (local != nullptr) { | 
|  | 105 | local->mStability = static_cast<int32_t>(stability); | 
|  | 106 | } else { | 
|  | 107 | binder->remoteBinder()->mStability = static_cast<int32_t>(stability); | 
|  | 108 | } | 
| Steven Moreland | dea3cf9 | 2019-07-16 18:06:55 -0700 | [diff] [blame] | 109 |  | 
|  | 110 | return OK; | 
|  | 111 | } | 
|  | 112 |  | 
|  | 113 | Stability::Level Stability::get(IBinder* binder) { | 
|  | 114 | if (binder == nullptr) return UNDECLARED; | 
|  | 115 |  | 
| Steven Moreland | a7fb018 | 2020-02-26 16:02:08 -0800 | [diff] [blame] | 116 | BBinder* local = binder->localBinder(); | 
|  | 117 | if (local != nullptr) { | 
|  | 118 | return static_cast<Stability::Level>(local->mStability); | 
|  | 119 | } | 
|  | 120 |  | 
|  | 121 | return static_cast<Stability::Level>(binder->remoteBinder()->mStability); | 
| Steven Moreland | dea3cf9 | 2019-07-16 18:06:55 -0700 | [diff] [blame] | 122 | } | 
|  | 123 |  | 
|  | 124 | bool Stability::check(int32_t provided, Level required) { | 
|  | 125 | bool stable = (provided & required) == required; | 
|  | 126 |  | 
|  | 127 | if (!isDeclaredStability(provided) && provided != UNDECLARED) { | 
|  | 128 | ALOGE("Unknown stability when checking interface stability %d.", provided); | 
|  | 129 |  | 
|  | 130 | stable = false; | 
|  | 131 | } | 
|  | 132 |  | 
| Steven Moreland | dea3cf9 | 2019-07-16 18:06:55 -0700 | [diff] [blame] | 133 | return stable; | 
|  | 134 | } | 
|  | 135 |  | 
|  | 136 | bool Stability::isDeclaredStability(int32_t stability) { | 
|  | 137 | return stability == VENDOR || stability == SYSTEM || stability == VINTF; | 
|  | 138 | } | 
|  | 139 |  | 
|  | 140 | std::string Stability::stabilityString(int32_t stability) { | 
|  | 141 | switch (stability) { | 
|  | 142 | case Level::UNDECLARED: return "undeclared stability"; | 
|  | 143 | case Level::VENDOR: return "vendor stability"; | 
|  | 144 | case Level::SYSTEM: return "system stability"; | 
|  | 145 | case Level::VINTF: return "vintf stability"; | 
|  | 146 | } | 
|  | 147 | return "unknown stability " + std::to_string(stability); | 
|  | 148 | } | 
|  | 149 |  | 
|  | 150 | }  // namespace internal | 
| Steven Moreland | 86a17f8 | 2019-09-10 10:18:00 -0700 | [diff] [blame] | 151 | }  // namespace stability |