blob: 7ce5e3629282662b4131c3d85fb4796958dc7e77 [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
18namespace android {
19namespace internal {
20
21void Stability::markCompilationUnit(IBinder* binder) {
Steven Moreland2a9f32f2019-07-31 17:51:25 -070022 status_t result = set(binder, kLocalStability, true /*log*/);
Steven Morelanddea3cf92019-07-16 18:06:55 -070023 LOG_ALWAYS_FATAL_IF(result != OK, "Should only mark known object.");
24}
25
26void Stability::markVintf(IBinder* binder) {
Steven Moreland2a9f32f2019-07-31 17:51:25 -070027 status_t result = set(binder, Level::VINTF, true /*log*/);
Steven Morelanddea3cf92019-07-16 18:06:55 -070028 LOG_ALWAYS_FATAL_IF(result != OK, "Should only mark known object.");
29}
30
Steven Moreland64ae9172019-08-02 20:45:15 -070031void 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 Morelandc709dd82019-08-05 20:30:14 -070035void 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 Moreland86a17f82019-09-10 10:18:00 -070040bool Stability::requiresVintfDeclaration(const sp<IBinder>& binder) {
41 return check(get(binder.get()), Level::VINTF);
42}
43
Steven Moreland2a9f32f2019-07-31 17:51:25 -070044void Stability::tryMarkCompilationUnit(IBinder* binder) {
45 (void) set(binder, kLocalStability, false /*log*/);
46}
47
48status_t Stability::set(IBinder* binder, int32_t stability, bool log) {
Steven Morelanddea3cf92019-07-16 18:06:55 -070049 Level currentStability = get(binder);
50
51 // null binder is always written w/ 'UNDECLARED' stability
52 if (binder == nullptr) {
53 if (stability == UNDECLARED) {
54 return OK;
55 } else {
Steven Moreland2a9f32f2019-07-31 17:51:25 -070056 if (log) {
57 ALOGE("Null binder written with stability %s.",
58 stabilityString(stability).c_str());
59 }
Steven Morelanddea3cf92019-07-16 18:06:55 -070060 return BAD_TYPE;
61 }
62 }
63
64 if (!isDeclaredStability(stability)) {
Steven Moreland2a9f32f2019-07-31 17:51:25 -070065 if (log) {
Steven Morelanddea3cf92019-07-16 18:06:55 -070066 ALOGE("Can only set known stability, not %d.", stability);
Steven Morelanddea3cf92019-07-16 18:06:55 -070067 }
Steven Moreland2a9f32f2019-07-31 17:51:25 -070068 return BAD_TYPE;
Steven Morelanddea3cf92019-07-16 18:06:55 -070069 }
70
71 if (currentStability != Level::UNDECLARED && currentStability != stability) {
Steven Moreland2a9f32f2019-07-31 17:51:25 -070072 if (log) {
73 ALOGE("Interface being set with %s but it is already marked as %s.",
Steven Moreland732de212019-08-02 20:41:10 -070074 stabilityString(stability).c_str(), stabilityString(currentStability).c_str());
Steven Moreland2a9f32f2019-07-31 17:51:25 -070075 }
Steven Morelanddea3cf92019-07-16 18:06:55 -070076 return BAD_TYPE;
77 }
78
79 if (currentStability == stability) return OK;
80
81 binder->attachObject(
82 reinterpret_cast<void*>(&Stability::get),
83 reinterpret_cast<void*>(stability),
84 nullptr /*cleanupCookie*/,
85 nullptr /*cleanup function*/);
86
87 return OK;
88}
89
90Stability::Level Stability::get(IBinder* binder) {
91 if (binder == nullptr) return UNDECLARED;
92
93 return static_cast<Level>(reinterpret_cast<intptr_t>(
94 binder->findObject(reinterpret_cast<void*>(&Stability::get))));
95}
96
97bool Stability::check(int32_t provided, Level required) {
98 bool stable = (provided & required) == required;
99
100 if (!isDeclaredStability(provided) && provided != UNDECLARED) {
101 ALOGE("Unknown stability when checking interface stability %d.", provided);
102
103 stable = false;
104 }
105
Steven Morelanddea3cf92019-07-16 18:06:55 -0700106 return stable;
107}
108
109bool Stability::isDeclaredStability(int32_t stability) {
110 return stability == VENDOR || stability == SYSTEM || stability == VINTF;
111}
112
113std::string Stability::stabilityString(int32_t stability) {
114 switch (stability) {
115 case Level::UNDECLARED: return "undeclared stability";
116 case Level::VENDOR: return "vendor stability";
117 case Level::SYSTEM: return "system stability";
118 case Level::VINTF: return "vintf stability";
119 }
120 return "unknown stability " + std::to_string(stability);
121}
122
123} // namespace internal
Steven Moreland86a17f82019-09-10 10:18:00 -0700124} // namespace stability