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