blob: b56e09fa6e01081b6456e442ccdb99a3b95a22a4 [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 Moreland89ddfc52020-11-13 02:39:26 +000016#define LOG_TAG "Stability"
17
Steven Morelanddea3cf92019-07-16 18:06:55 -070018#include <binder/Stability.h>
19
Steven Morelanda7fb0182020-02-26 16:02:08 -080020#include <binder/BpBinder.h>
21#include <binder/Binder.h>
22
Steven Morelanddea3cf92019-07-16 18:06:55 -070023namespace android {
24namespace internal {
25
Steven Moreland89ddfc52020-11-13 02:39:26 +000026// the libbinder parcel format is currently unstable
27
28// oldest version which is supported
29constexpr uint8_t kBinderWireFormatOldest = 1;
30// current version
31constexpr uint8_t kBinderWireFormatVersion = 1;
32
33Stability::Category Stability::Category::currentFromLevel(Level level) {
34 return {
35 .version = kBinderWireFormatVersion,
36 .reserved = {0},
37 .level = level,
38 };
39}
40
41std::string Stability::Category::debugString() {
42 return levelString(level) + " wire protocol version "
43 + std::to_string(version);
44}
45
Steven Morelanddea3cf92019-07-16 18:06:55 -070046void Stability::markCompilationUnit(IBinder* binder) {
Steven Moreland89ddfc52020-11-13 02:39:26 +000047 auto stability = Category::currentFromLevel(getLocalLevel());
48 status_t result = setRepr(binder, stability.repr(), true /*log*/);
Steven Morelanddea3cf92019-07-16 18:06:55 -070049 LOG_ALWAYS_FATAL_IF(result != OK, "Should only mark known object.");
50}
51
52void Stability::markVintf(IBinder* binder) {
Steven Moreland89ddfc52020-11-13 02:39:26 +000053 auto stability = Category::currentFromLevel(Level::VINTF);
54 status_t result = setRepr(binder, stability.repr(), true /*log*/);
Steven Morelanddea3cf92019-07-16 18:06:55 -070055 LOG_ALWAYS_FATAL_IF(result != OK, "Should only mark known object.");
56}
57
Steven Moreland64ae9172019-08-02 20:45:15 -070058void Stability::debugLogStability(const std::string& tag, const sp<IBinder>& binder) {
Steven Moreland89ddfc52020-11-13 02:39:26 +000059 auto stability = getCategory(binder.get());
60 ALOGE("%s: stability is %s", tag.c_str(), stability.debugString().c_str());
Steven Moreland64ae9172019-08-02 20:45:15 -070061}
62
Steven Morelandc709dd82019-08-05 20:30:14 -070063void Stability::markVndk(IBinder* binder) {
Steven Moreland89ddfc52020-11-13 02:39:26 +000064 auto stability = Category::currentFromLevel(Level::VENDOR);
65 status_t result = setRepr(binder, stability.repr(), true /*log*/);
Steven Morelandc709dd82019-08-05 20:30:14 -070066 LOG_ALWAYS_FATAL_IF(result != OK, "Should only mark known object.");
67}
68
Steven Moreland86a17f82019-09-10 10:18:00 -070069bool Stability::requiresVintfDeclaration(const sp<IBinder>& binder) {
Steven Moreland89ddfc52020-11-13 02:39:26 +000070 return check(getCategory(binder.get()), Level::VINTF);
Steven Moreland86a17f82019-09-10 10:18:00 -070071}
72
Steven Moreland2a9f32f2019-07-31 17:51:25 -070073void Stability::tryMarkCompilationUnit(IBinder* binder) {
Steven Moreland89ddfc52020-11-13 02:39:26 +000074 auto stability = Category::currentFromLevel(getLocalLevel());
75 (void) setRepr(binder, stability.repr(), false /*log*/);
Steven Moreland2f2c4e02020-07-28 18:11:21 +000076}
77
Steven Moreland89ddfc52020-11-13 02:39:26 +000078Stability::Level Stability::getLocalLevel() {
Steven Morelandb6c7e222021-02-18 19:20:14 +000079#ifdef __ANDROID_APEX__
80#error APEX can't use libbinder (must use libbinder_ndk)
81#endif
82
Steven Moreland2f2c4e02020-07-28 18:11:21 +000083#ifdef __ANDROID_VNDK__
Steven Morelandb6c7e222021-02-18 19:20:14 +000084 return Level::VENDOR;
Steven Moreland2f2c4e02020-07-28 18:11:21 +000085#else
86 // TODO(b/139325195): split up stability levels for system/APEX.
87 return Level::SYSTEM;
88#endif
Steven Moreland2a9f32f2019-07-31 17:51:25 -070089}
90
Steven Moreland89ddfc52020-11-13 02:39:26 +000091status_t Stability::setRepr(IBinder* binder, int32_t representation, bool log) {
92 auto current = getCategory(binder);
93 auto setting = Category::fromRepr(representation);
94
95 // If we have ahold of a binder with a newer declared version, then it
96 // should support older versions, and we will simply write our parcels with
97 // the current wire parcel format.
98 if (setting.version < kBinderWireFormatOldest) {
99 // always log, because this shouldn't happen
100 ALOGE("Cannot accept binder with older binder wire protocol version "
101 "%u. Versions less than %u are unsupported.", setting.version,
102 kBinderWireFormatOldest);
103 return BAD_TYPE;
104 }
Steven Morelanddea3cf92019-07-16 18:06:55 -0700105
106 // null binder is always written w/ 'UNDECLARED' stability
107 if (binder == nullptr) {
Steven Moreland89ddfc52020-11-13 02:39:26 +0000108 if (setting.level == UNDECLARED) {
Steven Morelanddea3cf92019-07-16 18:06:55 -0700109 return OK;
110 } else {
Steven Moreland2a9f32f2019-07-31 17:51:25 -0700111 if (log) {
112 ALOGE("Null binder written with stability %s.",
Steven Moreland89ddfc52020-11-13 02:39:26 +0000113 levelString(setting.level).c_str());
Steven Moreland2a9f32f2019-07-31 17:51:25 -0700114 }
Steven Morelanddea3cf92019-07-16 18:06:55 -0700115 return BAD_TYPE;
116 }
117 }
118
Steven Moreland89ddfc52020-11-13 02:39:26 +0000119 if (!isDeclaredLevel(setting.level)) {
Steven Moreland2a9f32f2019-07-31 17:51:25 -0700120 if (log) {
Steven Moreland89ddfc52020-11-13 02:39:26 +0000121 ALOGE("Can only set known stability, not %u.", setting.level);
Steven Morelanddea3cf92019-07-16 18:06:55 -0700122 }
Steven Moreland2a9f32f2019-07-31 17:51:25 -0700123 return BAD_TYPE;
Steven Morelanddea3cf92019-07-16 18:06:55 -0700124 }
125
Steven Moreland89ddfc52020-11-13 02:39:26 +0000126 if (current.repr() != 0 && current != setting) {
Steven Moreland2a9f32f2019-07-31 17:51:25 -0700127 if (log) {
Steven Moreland89ddfc52020-11-13 02:39:26 +0000128 ALOGE("Interface being set with %s but it is already marked as %s",
129 setting.debugString().c_str(),
130 current.debugString().c_str());
Steven Moreland2a9f32f2019-07-31 17:51:25 -0700131 }
Steven Morelanddea3cf92019-07-16 18:06:55 -0700132 return BAD_TYPE;
133 }
134
Steven Moreland89ddfc52020-11-13 02:39:26 +0000135 if (current == setting) return OK;
Steven Morelanddea3cf92019-07-16 18:06:55 -0700136
Steven Morelanda7fb0182020-02-26 16:02:08 -0800137 BBinder* local = binder->localBinder();
138 if (local != nullptr) {
Steven Moreland89ddfc52020-11-13 02:39:26 +0000139 local->mStability = setting.repr();
Steven Morelanda7fb0182020-02-26 16:02:08 -0800140 } else {
Steven Moreland89ddfc52020-11-13 02:39:26 +0000141 binder->remoteBinder()->mStability = setting.repr();
Steven Morelanda7fb0182020-02-26 16:02:08 -0800142 }
Steven Morelanddea3cf92019-07-16 18:06:55 -0700143
144 return OK;
145}
146
Steven Moreland89ddfc52020-11-13 02:39:26 +0000147Stability::Category Stability::getCategory(IBinder* binder) {
148 if (binder == nullptr) {
149 return Category::currentFromLevel(Level::UNDECLARED);
150 }
Steven Morelanddea3cf92019-07-16 18:06:55 -0700151
Steven Morelanda7fb0182020-02-26 16:02:08 -0800152 BBinder* local = binder->localBinder();
153 if (local != nullptr) {
Steven Moreland89ddfc52020-11-13 02:39:26 +0000154 return Category::fromRepr(local->mStability);
Steven Morelanda7fb0182020-02-26 16:02:08 -0800155 }
156
Steven Moreland89ddfc52020-11-13 02:39:26 +0000157 return Category::fromRepr(binder->remoteBinder()->mStability);
Steven Morelanddea3cf92019-07-16 18:06:55 -0700158}
159
Steven Moreland89ddfc52020-11-13 02:39:26 +0000160bool Stability::check(Category provided, Level required) {
161 bool stable = (provided.level & required) == required;
Steven Morelanddea3cf92019-07-16 18:06:55 -0700162
Steven Moreland89ddfc52020-11-13 02:39:26 +0000163 if (provided.level != UNDECLARED && !isDeclaredLevel(provided.level)) {
164 ALOGE("Unknown stability when checking interface stability %d.",
165 provided.level);
Steven Morelanddea3cf92019-07-16 18:06:55 -0700166
167 stable = false;
168 }
169
Steven Morelanddea3cf92019-07-16 18:06:55 -0700170 return stable;
171}
172
Steven Moreland89ddfc52020-11-13 02:39:26 +0000173bool Stability::isDeclaredLevel(Level stability) {
Steven Morelanddea3cf92019-07-16 18:06:55 -0700174 return stability == VENDOR || stability == SYSTEM || stability == VINTF;
175}
176
Steven Moreland89ddfc52020-11-13 02:39:26 +0000177std::string Stability::levelString(Level level) {
178 switch (level) {
Steven Morelanddea3cf92019-07-16 18:06:55 -0700179 case Level::UNDECLARED: return "undeclared stability";
180 case Level::VENDOR: return "vendor stability";
181 case Level::SYSTEM: return "system stability";
182 case Level::VINTF: return "vintf stability";
183 }
Steven Moreland89ddfc52020-11-13 02:39:26 +0000184 return "unknown stability " + std::to_string(level);
Steven Morelanddea3cf92019-07-16 18:06:55 -0700185}
186
187} // namespace internal
Steven Moreland86a17f82019-09-10 10:18:00 -0700188} // namespace stability