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