blob: 06830c027b949cc505989cd7db5deb75db2519e9 [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
Steven Morelande35fef32021-03-23 01:38:24 +000041void Stability::forceDowngradeCompilationUnit(const sp<IBinder>& binder) {
42 // Downgrading a remote binder would require also copying the version from
43 // the binder sent here. In practice though, we don't need to downgrade the
44 // stability of a remote binder, since this would as an effect only restrict
45 // what we can do to it.
46 LOG_ALWAYS_FATAL_IF(!binder || !binder->localBinder(), "Can only downgrade local binder");
47
48 auto stability = Category::currentFromLevel(getLocalLevel());
49 status_t result = setRepr(binder.get(), stability.repr(), REPR_LOG | REPR_ALLOW_DOWNGRADE);
50 LOG_ALWAYS_FATAL_IF(result != OK, "Should only mark known object.");
51}
52
Steven Moreland89ddfc52020-11-13 02:39:26 +000053std::string Stability::Category::debugString() {
54 return levelString(level) + " wire protocol version "
55 + std::to_string(version);
56}
57
Steven Morelanddea3cf92019-07-16 18:06:55 -070058void Stability::markCompilationUnit(IBinder* binder) {
Steven Moreland89ddfc52020-11-13 02:39:26 +000059 auto stability = Category::currentFromLevel(getLocalLevel());
Steven Morelande35fef32021-03-23 01:38:24 +000060 status_t result = setRepr(binder, stability.repr(), REPR_LOG);
Steven Morelanddea3cf92019-07-16 18:06:55 -070061 LOG_ALWAYS_FATAL_IF(result != OK, "Should only mark known object.");
62}
63
64void Stability::markVintf(IBinder* binder) {
Steven Moreland89ddfc52020-11-13 02:39:26 +000065 auto stability = Category::currentFromLevel(Level::VINTF);
Steven Morelande35fef32021-03-23 01:38:24 +000066 status_t result = setRepr(binder, stability.repr(), REPR_LOG);
Steven Morelanddea3cf92019-07-16 18:06:55 -070067 LOG_ALWAYS_FATAL_IF(result != OK, "Should only mark known object.");
68}
69
Steven Moreland64ae9172019-08-02 20:45:15 -070070void Stability::debugLogStability(const std::string& tag, const sp<IBinder>& binder) {
Steven Moreland89ddfc52020-11-13 02:39:26 +000071 auto stability = getCategory(binder.get());
72 ALOGE("%s: stability is %s", tag.c_str(), stability.debugString().c_str());
Steven Moreland64ae9172019-08-02 20:45:15 -070073}
74
Steven Morelandc709dd82019-08-05 20:30:14 -070075void Stability::markVndk(IBinder* binder) {
Steven Moreland89ddfc52020-11-13 02:39:26 +000076 auto stability = Category::currentFromLevel(Level::VENDOR);
Steven Morelande35fef32021-03-23 01:38:24 +000077 status_t result = setRepr(binder, stability.repr(), REPR_LOG);
Steven Morelandc709dd82019-08-05 20:30:14 -070078 LOG_ALWAYS_FATAL_IF(result != OK, "Should only mark known object.");
79}
80
Steven Moreland86a17f82019-09-10 10:18:00 -070081bool Stability::requiresVintfDeclaration(const sp<IBinder>& binder) {
Steven Moreland89ddfc52020-11-13 02:39:26 +000082 return check(getCategory(binder.get()), Level::VINTF);
Steven Moreland86a17f82019-09-10 10:18:00 -070083}
84
Steven Moreland2a9f32f2019-07-31 17:51:25 -070085void Stability::tryMarkCompilationUnit(IBinder* binder) {
Steven Moreland89ddfc52020-11-13 02:39:26 +000086 auto stability = Category::currentFromLevel(getLocalLevel());
Steven Morelande35fef32021-03-23 01:38:24 +000087 (void) setRepr(binder, stability.repr(), REPR_NONE);
Steven Moreland2f2c4e02020-07-28 18:11:21 +000088}
89
Steven Moreland89ddfc52020-11-13 02:39:26 +000090Stability::Level Stability::getLocalLevel() {
Steven Moreland2f2c4e02020-07-28 18:11:21 +000091#ifdef __ANDROID_VNDK__
92 #ifdef __ANDROID_APEX__
93 // TODO(b/142684679) avoid use_vendor on system APEXes
94 #if !defined(__ANDROID_APEX_COM_ANDROID_MEDIA_SWCODEC__) \
95 && !defined(__ANDROID_APEX_TEST_COM_ANDROID_MEDIA_SWCODEC__)
96 #error VNDK + APEX only defined for com.android.media.swcodec
97 #endif
98 // TODO(b/142684679) avoid use_vendor on system APEXes
99 return Level::SYSTEM;
100 #else
101 return Level::VENDOR;
102 #endif
103#else
104 // TODO(b/139325195): split up stability levels for system/APEX.
105 return Level::SYSTEM;
106#endif
Steven Moreland2a9f32f2019-07-31 17:51:25 -0700107}
108
Steven Morelande35fef32021-03-23 01:38:24 +0000109status_t Stability::setRepr(IBinder* binder, int32_t representation, uint32_t flags) {
110 bool log = flags & REPR_LOG;
111 bool allowDowngrade = flags & REPR_ALLOW_DOWNGRADE;
112
Steven Moreland89ddfc52020-11-13 02:39:26 +0000113 auto current = getCategory(binder);
114 auto setting = Category::fromRepr(representation);
115
116 // If we have ahold of a binder with a newer declared version, then it
117 // should support older versions, and we will simply write our parcels with
118 // the current wire parcel format.
119 if (setting.version < kBinderWireFormatOldest) {
120 // always log, because this shouldn't happen
121 ALOGE("Cannot accept binder with older binder wire protocol version "
122 "%u. Versions less than %u are unsupported.", setting.version,
123 kBinderWireFormatOldest);
124 return BAD_TYPE;
125 }
Steven Morelanddea3cf92019-07-16 18:06:55 -0700126
127 // null binder is always written w/ 'UNDECLARED' stability
128 if (binder == nullptr) {
Steven Moreland89ddfc52020-11-13 02:39:26 +0000129 if (setting.level == UNDECLARED) {
Steven Morelanddea3cf92019-07-16 18:06:55 -0700130 return OK;
131 } else {
Steven Moreland2a9f32f2019-07-31 17:51:25 -0700132 if (log) {
133 ALOGE("Null binder written with stability %s.",
Steven Moreland89ddfc52020-11-13 02:39:26 +0000134 levelString(setting.level).c_str());
Steven Moreland2a9f32f2019-07-31 17:51:25 -0700135 }
Steven Morelanddea3cf92019-07-16 18:06:55 -0700136 return BAD_TYPE;
137 }
138 }
139
Steven Moreland89ddfc52020-11-13 02:39:26 +0000140 if (!isDeclaredLevel(setting.level)) {
Steven Moreland2a9f32f2019-07-31 17:51:25 -0700141 if (log) {
Steven Moreland89ddfc52020-11-13 02:39:26 +0000142 ALOGE("Can only set known stability, not %u.", setting.level);
Steven Morelanddea3cf92019-07-16 18:06:55 -0700143 }
Steven Moreland2a9f32f2019-07-31 17:51:25 -0700144 return BAD_TYPE;
Steven Morelanddea3cf92019-07-16 18:06:55 -0700145 }
146
Steven Morelande35fef32021-03-23 01:38:24 +0000147 if (current == setting) return OK;
148
149 bool hasAlreadyBeenSet = current.repr() != 0;
150 bool isAllowedDowngrade = allowDowngrade && check(current, setting.level);
151 if (hasAlreadyBeenSet && !isAllowedDowngrade) {
Steven Moreland2a9f32f2019-07-31 17:51:25 -0700152 if (log) {
Steven Moreland89ddfc52020-11-13 02:39:26 +0000153 ALOGE("Interface being set with %s but it is already marked as %s",
154 setting.debugString().c_str(),
155 current.debugString().c_str());
Steven Moreland2a9f32f2019-07-31 17:51:25 -0700156 }
Steven Morelanddea3cf92019-07-16 18:06:55 -0700157 return BAD_TYPE;
158 }
159
Steven Morelande35fef32021-03-23 01:38:24 +0000160 if (isAllowedDowngrade) {
161 ALOGI("Interface set with %s downgraded to %s stability",
162 current.debugString().c_str(),
163 setting.debugString().c_str());
164 }
Steven Morelanddea3cf92019-07-16 18:06:55 -0700165
Steven Morelanda7fb0182020-02-26 16:02:08 -0800166 BBinder* local = binder->localBinder();
167 if (local != nullptr) {
Steven Moreland89ddfc52020-11-13 02:39:26 +0000168 local->mStability = setting.repr();
Steven Morelanda7fb0182020-02-26 16:02:08 -0800169 } else {
Steven Moreland89ddfc52020-11-13 02:39:26 +0000170 binder->remoteBinder()->mStability = setting.repr();
Steven Morelanda7fb0182020-02-26 16:02:08 -0800171 }
Steven Morelanddea3cf92019-07-16 18:06:55 -0700172
173 return OK;
174}
175
Steven Moreland89ddfc52020-11-13 02:39:26 +0000176Stability::Category Stability::getCategory(IBinder* binder) {
177 if (binder == nullptr) {
178 return Category::currentFromLevel(Level::UNDECLARED);
179 }
Steven Morelanddea3cf92019-07-16 18:06:55 -0700180
Steven Morelanda7fb0182020-02-26 16:02:08 -0800181 BBinder* local = binder->localBinder();
182 if (local != nullptr) {
Steven Moreland89ddfc52020-11-13 02:39:26 +0000183 return Category::fromRepr(local->mStability);
Steven Morelanda7fb0182020-02-26 16:02:08 -0800184 }
185
Steven Moreland89ddfc52020-11-13 02:39:26 +0000186 return Category::fromRepr(binder->remoteBinder()->mStability);
Steven Morelanddea3cf92019-07-16 18:06:55 -0700187}
188
Steven Moreland89ddfc52020-11-13 02:39:26 +0000189bool Stability::check(Category provided, Level required) {
190 bool stable = (provided.level & required) == required;
Steven Morelanddea3cf92019-07-16 18:06:55 -0700191
Steven Moreland89ddfc52020-11-13 02:39:26 +0000192 if (provided.level != UNDECLARED && !isDeclaredLevel(provided.level)) {
193 ALOGE("Unknown stability when checking interface stability %d.",
194 provided.level);
Steven Morelanddea3cf92019-07-16 18:06:55 -0700195
196 stable = false;
197 }
198
Steven Morelanddea3cf92019-07-16 18:06:55 -0700199 return stable;
200}
201
Steven Moreland89ddfc52020-11-13 02:39:26 +0000202bool Stability::isDeclaredLevel(Level stability) {
Steven Morelanddea3cf92019-07-16 18:06:55 -0700203 return stability == VENDOR || stability == SYSTEM || stability == VINTF;
204}
205
Steven Moreland89ddfc52020-11-13 02:39:26 +0000206std::string Stability::levelString(Level level) {
207 switch (level) {
Steven Morelanddea3cf92019-07-16 18:06:55 -0700208 case Level::UNDECLARED: return "undeclared stability";
209 case Level::VENDOR: return "vendor stability";
210 case Level::SYSTEM: return "system stability";
211 case Level::VINTF: return "vintf stability";
212 }
Steven Moreland89ddfc52020-11-13 02:39:26 +0000213 return "unknown stability " + std::to_string(level);
Steven Morelanddea3cf92019-07-16 18:06:55 -0700214}
215
216} // namespace internal
Steven Moreland86a17f82019-09-10 10:18:00 -0700217} // namespace stability