| 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 |  | 
|  | 18 | namespace android { | 
|  | 19 | namespace internal { | 
|  | 20 |  | 
|  | 21 | void Stability::markCompilationUnit(IBinder* binder) { | 
| Steven Moreland | 2a9f32f | 2019-07-31 17:51:25 -0700 | [diff] [blame] | 22 | status_t result = set(binder, kLocalStability, true /*log*/); | 
| Steven Moreland | dea3cf9 | 2019-07-16 18:06:55 -0700 | [diff] [blame] | 23 | LOG_ALWAYS_FATAL_IF(result != OK, "Should only mark known object."); | 
|  | 24 | } | 
|  | 25 |  | 
|  | 26 | void Stability::markVintf(IBinder* binder) { | 
| Steven Moreland | 2a9f32f | 2019-07-31 17:51:25 -0700 | [diff] [blame] | 27 | status_t result = set(binder, Level::VINTF, true /*log*/); | 
| Steven Moreland | dea3cf9 | 2019-07-16 18:06:55 -0700 | [diff] [blame] | 28 | LOG_ALWAYS_FATAL_IF(result != OK, "Should only mark known object."); | 
|  | 29 | } | 
|  | 30 |  | 
| Steven Moreland | 64ae917 | 2019-08-02 20:45:15 -0700 | [diff] [blame] | 31 | void Stability::debugLogStability(const std::string& tag, const sp<IBinder>& binder) { | 
|  | 32 | ALOGE("%s: stability is %s", tag.c_str(), stabilityString(get(binder.get())).c_str()); | 
|  | 33 | } | 
|  | 34 |  | 
| Steven Moreland | 2a9f32f | 2019-07-31 17:51:25 -0700 | [diff] [blame] | 35 | void Stability::tryMarkCompilationUnit(IBinder* binder) { | 
|  | 36 | (void) set(binder, kLocalStability, false /*log*/); | 
|  | 37 | } | 
|  | 38 |  | 
|  | 39 | status_t Stability::set(IBinder* binder, int32_t stability, bool log) { | 
| Steven Moreland | dea3cf9 | 2019-07-16 18:06:55 -0700 | [diff] [blame] | 40 | Level currentStability = get(binder); | 
|  | 41 |  | 
|  | 42 | // null binder is always written w/ 'UNDECLARED' stability | 
|  | 43 | if (binder == nullptr) { | 
|  | 44 | if (stability == UNDECLARED) { | 
|  | 45 | return OK; | 
|  | 46 | } else { | 
| Steven Moreland | 2a9f32f | 2019-07-31 17:51:25 -0700 | [diff] [blame] | 47 | if (log) { | 
|  | 48 | ALOGE("Null binder written with stability %s.", | 
|  | 49 | stabilityString(stability).c_str()); | 
|  | 50 | } | 
| Steven Moreland | dea3cf9 | 2019-07-16 18:06:55 -0700 | [diff] [blame] | 51 | return BAD_TYPE; | 
|  | 52 | } | 
|  | 53 | } | 
|  | 54 |  | 
|  | 55 | if (!isDeclaredStability(stability)) { | 
| Steven Moreland | 2a9f32f | 2019-07-31 17:51:25 -0700 | [diff] [blame] | 56 | if (log) { | 
| Steven Moreland | dea3cf9 | 2019-07-16 18:06:55 -0700 | [diff] [blame] | 57 | ALOGE("Can only set known stability, not %d.", stability); | 
| Steven Moreland | dea3cf9 | 2019-07-16 18:06:55 -0700 | [diff] [blame] | 58 | } | 
| Steven Moreland | 2a9f32f | 2019-07-31 17:51:25 -0700 | [diff] [blame] | 59 | return BAD_TYPE; | 
| Steven Moreland | dea3cf9 | 2019-07-16 18:06:55 -0700 | [diff] [blame] | 60 | } | 
|  | 61 |  | 
|  | 62 | if (currentStability != Level::UNDECLARED && currentStability != stability) { | 
| Steven Moreland | 2a9f32f | 2019-07-31 17:51:25 -0700 | [diff] [blame] | 63 | if (log) { | 
|  | 64 | 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] | 65 | stabilityString(stability).c_str(), stabilityString(currentStability).c_str()); | 
| Steven Moreland | 2a9f32f | 2019-07-31 17:51:25 -0700 | [diff] [blame] | 66 | } | 
| Steven Moreland | dea3cf9 | 2019-07-16 18:06:55 -0700 | [diff] [blame] | 67 | return BAD_TYPE; | 
|  | 68 | } | 
|  | 69 |  | 
|  | 70 | if (currentStability == stability) return OK; | 
|  | 71 |  | 
|  | 72 | binder->attachObject( | 
|  | 73 | reinterpret_cast<void*>(&Stability::get), | 
|  | 74 | reinterpret_cast<void*>(stability), | 
|  | 75 | nullptr /*cleanupCookie*/, | 
|  | 76 | nullptr /*cleanup function*/); | 
|  | 77 |  | 
|  | 78 | return OK; | 
|  | 79 | } | 
|  | 80 |  | 
|  | 81 | Stability::Level Stability::get(IBinder* binder) { | 
|  | 82 | if (binder == nullptr) return UNDECLARED; | 
|  | 83 |  | 
|  | 84 | return static_cast<Level>(reinterpret_cast<intptr_t>( | 
|  | 85 | binder->findObject(reinterpret_cast<void*>(&Stability::get)))); | 
|  | 86 | } | 
|  | 87 |  | 
|  | 88 | bool Stability::check(int32_t provided, Level required) { | 
|  | 89 | bool stable = (provided & required) == required; | 
|  | 90 |  | 
|  | 91 | if (!isDeclaredStability(provided) && provided != UNDECLARED) { | 
|  | 92 | ALOGE("Unknown stability when checking interface stability %d.", provided); | 
|  | 93 |  | 
|  | 94 | stable = false; | 
|  | 95 | } | 
|  | 96 |  | 
|  | 97 | if (!stable) { | 
|  | 98 | ALOGE("Interface with %s cannot accept interface with %s.", | 
|  | 99 | stabilityString(required).c_str(), | 
|  | 100 | stabilityString(provided).c_str()); | 
|  | 101 | } | 
|  | 102 |  | 
|  | 103 | return stable; | 
|  | 104 | } | 
|  | 105 |  | 
|  | 106 | bool Stability::isDeclaredStability(int32_t stability) { | 
|  | 107 | return stability == VENDOR || stability == SYSTEM || stability == VINTF; | 
|  | 108 | } | 
|  | 109 |  | 
|  | 110 | std::string Stability::stabilityString(int32_t stability) { | 
|  | 111 | switch (stability) { | 
|  | 112 | case Level::UNDECLARED: return "undeclared stability"; | 
|  | 113 | case Level::VENDOR: return "vendor stability"; | 
|  | 114 | case Level::SYSTEM: return "system stability"; | 
|  | 115 | case Level::VINTF: return "vintf stability"; | 
|  | 116 | } | 
|  | 117 | return "unknown stability " + std::to_string(stability); | 
|  | 118 | } | 
|  | 119 |  | 
|  | 120 | }  // namespace internal | 
|  | 121 | }  // namespace stability |