blob: cdd142665177c4227ce6603eca109c7d95f10a51 [file] [log] [blame]
Steven Morelanddea3cf92019-07-16 18:06:55 -07001/*
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 Morelanddea3cf92019-07-16 18:06:55 -070016#include <binder/Stability.h>
17
Steven Morelanda7fb0182020-02-26 16:02:08 -080018#include <binder/BpBinder.h>
19#include <binder/Binder.h>
20
Steven Morelanddea3cf92019-07-16 18:06:55 -070021namespace android {
22namespace internal {
23
24void Stability::markCompilationUnit(IBinder* binder) {
Steven Moreland2f2c4e02020-07-28 18:11:21 +000025 status_t result = set(binder, getLocalStability(), true /*log*/);
Steven Morelanddea3cf92019-07-16 18:06:55 -070026 LOG_ALWAYS_FATAL_IF(result != OK, "Should only mark known object.");
27}
28
29void Stability::markVintf(IBinder* binder) {
Steven Moreland2a9f32f2019-07-31 17:51:25 -070030 status_t result = set(binder, Level::VINTF, true /*log*/);
Steven Morelanddea3cf92019-07-16 18:06:55 -070031 LOG_ALWAYS_FATAL_IF(result != OK, "Should only mark known object.");
32}
33
Steven Moreland64ae9172019-08-02 20:45:15 -070034void 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 Morelandc709dd82019-08-05 20:30:14 -070038void 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 Moreland86a17f82019-09-10 10:18:00 -070043bool Stability::requiresVintfDeclaration(const sp<IBinder>& binder) {
44 return check(get(binder.get()), Level::VINTF);
45}
46
Steven Moreland2a9f32f2019-07-31 17:51:25 -070047void Stability::tryMarkCompilationUnit(IBinder* binder) {
Steven Moreland2f2c4e02020-07-28 18:11:21 +000048 (void) set(binder, getLocalStability(), false /*log*/);
49}
50
51Stability::Level Stability::getLocalStability() {
52#ifdef __ANDROID_VNDK__
Steven Moreland8852cc72020-08-18 18:15:03 +000053 return Level::VENDOR;
Steven Moreland2f2c4e02020-07-28 18:11:21 +000054#else
55 // TODO(b/139325195): split up stability levels for system/APEX.
56 return Level::SYSTEM;
57#endif
Steven Moreland2a9f32f2019-07-31 17:51:25 -070058}
59
60status_t Stability::set(IBinder* binder, int32_t stability, bool log) {
Steven Morelanddea3cf92019-07-16 18:06:55 -070061 Level currentStability = get(binder);
62
63 // null binder is always written w/ 'UNDECLARED' stability
64 if (binder == nullptr) {
65 if (stability == UNDECLARED) {
66 return OK;
67 } else {
Steven Moreland2a9f32f2019-07-31 17:51:25 -070068 if (log) {
69 ALOGE("Null binder written with stability %s.",
70 stabilityString(stability).c_str());
71 }
Steven Morelanddea3cf92019-07-16 18:06:55 -070072 return BAD_TYPE;
73 }
74 }
75
76 if (!isDeclaredStability(stability)) {
Steven Moreland2a9f32f2019-07-31 17:51:25 -070077 if (log) {
Steven Morelanddea3cf92019-07-16 18:06:55 -070078 ALOGE("Can only set known stability, not %d.", stability);
Steven Morelanddea3cf92019-07-16 18:06:55 -070079 }
Steven Moreland2a9f32f2019-07-31 17:51:25 -070080 return BAD_TYPE;
Steven Morelanddea3cf92019-07-16 18:06:55 -070081 }
82
83 if (currentStability != Level::UNDECLARED && currentStability != stability) {
Steven Moreland2a9f32f2019-07-31 17:51:25 -070084 if (log) {
85 ALOGE("Interface being set with %s but it is already marked as %s.",
Steven Moreland732de212019-08-02 20:41:10 -070086 stabilityString(stability).c_str(), stabilityString(currentStability).c_str());
Steven Moreland2a9f32f2019-07-31 17:51:25 -070087 }
Steven Morelanddea3cf92019-07-16 18:06:55 -070088 return BAD_TYPE;
89 }
90
91 if (currentStability == stability) return OK;
92
Steven Morelanda7fb0182020-02-26 16:02:08 -080093 BBinder* local = binder->localBinder();
94 if (local != nullptr) {
95 local->mStability = static_cast<int32_t>(stability);
96 } else {
97 binder->remoteBinder()->mStability = static_cast<int32_t>(stability);
98 }
Steven Morelanddea3cf92019-07-16 18:06:55 -070099
100 return OK;
101}
102
103Stability::Level Stability::get(IBinder* binder) {
104 if (binder == nullptr) return UNDECLARED;
105
Steven Morelanda7fb0182020-02-26 16:02:08 -0800106 BBinder* local = binder->localBinder();
107 if (local != nullptr) {
108 return static_cast<Stability::Level>(local->mStability);
109 }
110
111 return static_cast<Stability::Level>(binder->remoteBinder()->mStability);
Steven Morelanddea3cf92019-07-16 18:06:55 -0700112}
113
114bool Stability::check(int32_t provided, Level required) {
115 bool stable = (provided & required) == required;
116
117 if (!isDeclaredStability(provided) && provided != UNDECLARED) {
118 ALOGE("Unknown stability when checking interface stability %d.", provided);
119
120 stable = false;
121 }
122
Steven Morelanddea3cf92019-07-16 18:06:55 -0700123 return stable;
124}
125
126bool Stability::isDeclaredStability(int32_t stability) {
127 return stability == VENDOR || stability == SYSTEM || stability == VINTF;
128}
129
130std::string Stability::stabilityString(int32_t stability) {
131 switch (stability) {
132 case Level::UNDECLARED: return "undeclared stability";
133 case Level::VENDOR: return "vendor stability";
134 case Level::SYSTEM: return "system stability";
135 case Level::VINTF: return "vintf stability";
136 }
137 return "unknown stability " + std::to_string(stability);
138}
139
140} // namespace internal
Steven Moreland86a17f82019-09-10 10:18:00 -0700141} // namespace stability