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 */
16 #include <binder/Stability.h>
17
18 #include <binder/BpBinder.h>
19 #include <binder/Binder.h>
20
21 namespace android {
22 namespace internal {
23
markCompilationUnit(IBinder * binder)24 void Stability::markCompilationUnit(IBinder* binder) {
25 status_t result = set(binder, kLocalStability, true /*log*/);
26 LOG_ALWAYS_FATAL_IF(result != OK, "Should only mark known object.");
27 }
28
markVintf(IBinder * binder)29 void Stability::markVintf(IBinder* binder) {
30 status_t result = set(binder, Level::VINTF, true /*log*/);
31 LOG_ALWAYS_FATAL_IF(result != OK, "Should only mark known object.");
32 }
33
debugLogStability(const std::string & tag,const sp<IBinder> & binder)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
markVndk(IBinder * binder)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
requiresVintfDeclaration(const sp<IBinder> & binder)43 bool Stability::requiresVintfDeclaration(const sp<IBinder>& binder) {
44 return check(get(binder.get()), Level::VINTF);
45 }
46
tryMarkCompilationUnit(IBinder * binder)47 void Stability::tryMarkCompilationUnit(IBinder* binder) {
48 (void) set(binder, kLocalStability, false /*log*/);
49 }
50
set(IBinder * binder,int32_t stability,bool log)51 status_t Stability::set(IBinder* binder, int32_t stability, bool log) {
52 Level currentStability = get(binder);
53
54 // null binder is always written w/ 'UNDECLARED' stability
55 if (binder == nullptr) {
56 if (stability == UNDECLARED) {
57 return OK;
58 } else {
59 if (log) {
60 ALOGE("Null binder written with stability %s.",
61 stabilityString(stability).c_str());
62 }
63 return BAD_TYPE;
64 }
65 }
66
67 if (!isDeclaredStability(stability)) {
68 if (log) {
69 ALOGE("Can only set known stability, not %d.", stability);
70 }
71 return BAD_TYPE;
72 }
73
74 if (currentStability != Level::UNDECLARED && currentStability != stability) {
75 if (log) {
76 ALOGE("Interface being set with %s but it is already marked as %s.",
77 stabilityString(stability).c_str(), stabilityString(currentStability).c_str());
78 }
79 return BAD_TYPE;
80 }
81
82 if (currentStability == stability) return OK;
83
84 BBinder* local = binder->localBinder();
85 if (local != nullptr) {
86 local->mStability = static_cast<int32_t>(stability);
87 } else {
88 binder->remoteBinder()->mStability = static_cast<int32_t>(stability);
89 }
90
91 return OK;
92 }
93
get(IBinder * binder)94 Stability::Level Stability::get(IBinder* binder) {
95 if (binder == nullptr) return UNDECLARED;
96
97 BBinder* local = binder->localBinder();
98 if (local != nullptr) {
99 return static_cast<Stability::Level>(local->mStability);
100 }
101
102 return static_cast<Stability::Level>(binder->remoteBinder()->mStability);
103 }
104
check(int32_t provided,Level required)105 bool Stability::check(int32_t provided, Level required) {
106 bool stable = (provided & required) == required;
107
108 if (!isDeclaredStability(provided) && provided != UNDECLARED) {
109 ALOGE("Unknown stability when checking interface stability %d.", provided);
110
111 stable = false;
112 }
113
114 return stable;
115 }
116
isDeclaredStability(int32_t stability)117 bool Stability::isDeclaredStability(int32_t stability) {
118 return stability == VENDOR || stability == SYSTEM || stability == VINTF;
119 }
120
stabilityString(int32_t stability)121 std::string Stability::stabilityString(int32_t stability) {
122 switch (stability) {
123 case Level::UNDECLARED: return "undeclared stability";
124 case Level::VENDOR: return "vendor stability";
125 case Level::SYSTEM: return "system stability";
126 case Level::VINTF: return "vintf stability";
127 }
128 return "unknown stability " + std::to_string(stability);
129 }
130
131 } // namespace internal
132 } // namespace stability
133