blob: a9a903b71f18723bdb6083e9ef20bd68bf00d0a6 [file] [log] [blame]
David Brazdildcfa89b2018-10-31 11:04:10 +00001/*
2 * Copyright (C) 2018 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
17#ifndef ART_LIBARTBASE_BASE_HIDDENAPI_FLAGS_H_
18#define ART_LIBARTBASE_BASE_HIDDENAPI_FLAGS_H_
19
20#include "sdk_version.h"
21
David Brazdil90faceb2018-12-14 14:36:15 +000022#include <vector>
23
David Brazdildcfa89b2018-10-31 11:04:10 +000024#include "android-base/logging.h"
David Brazdil90faceb2018-12-14 14:36:15 +000025#include "base/bit_utils.h"
26#include "base/dumpable.h"
27#include "base/macros.h"
Andrei Onea370a0642019-03-01 17:48:27 +000028#include "base/hiddenapi_stubs.h"
David Brazdildcfa89b2018-10-31 11:04:10 +000029
30namespace art {
31namespace hiddenapi {
32
David Brazdil90faceb2018-12-14 14:36:15 +000033// Helper methods used inside ApiList. These were moved outside of the ApiList
34// class so that they can be used in static_asserts. If they were inside, they
35// would be part of an unfinished type.
36namespace helper {
37 // Casts enum value to uint32_t.
38 template<typename T>
39 constexpr uint32_t ToUint(T val) { return static_cast<uint32_t>(val); }
40
41 // Returns uint32_t with one bit set at an index given by an enum value.
42 template<typename T>
43 constexpr uint32_t ToBit(T val) { return 1u << ToUint(val); }
44
45 // Returns a bit mask with `size` least significant bits set.
46 constexpr uint32_t BitMask(uint32_t size) { return (1u << size) - 1; }
47
48 // Returns a bit mask formed from an enum defining kMin and kMax. The values
49 // are assumed to be indices of min/max bits and the resulting bitmask has
50 // bits [kMin, kMax] set.
51 template<typename T>
52 constexpr uint32_t BitMask() {
53 return BitMask(ToUint(T::kMax) + 1) & (~BitMask(ToUint(T::kMin)));
54 }
55
56 // Returns true if `val` is a bitwise subset of `mask`.
57 constexpr bool MatchesBitMask(uint32_t val, uint32_t mask) { return (val & mask) == val; }
58
59 // Returns true if the uint32_t value of `val` is a bitwise subset of `mask`.
60 template<typename T>
61 constexpr bool MatchesBitMask(T val, uint32_t mask) { return MatchesBitMask(ToUint(val), mask); }
62
63 // Returns the number of values defined in an enum, assuming the enum defines
64 // kMin and kMax and no integer values are skipped between them.
65 template<typename T>
66 constexpr uint32_t NumValues() { return ToUint(T::kMax) - ToUint(T::kMin) + 1; }
Mariia Feofanova4945b292019-09-04 17:49:42 +010067
68 // Returns enum value at position i from enum list.
69 template <typename T>
70 constexpr T GetEnumAt(uint32_t i) {
71 return static_cast<T>(ToUint(T::kMin) + i);
72 }
73
David Brazdil90faceb2018-12-14 14:36:15 +000074} // namespace helper
75
David Brazdildcfa89b2018-10-31 11:04:10 +000076/*
77 * This class represents the information whether a field/method is in
78 * public API (whitelist) or if it isn't, apps targeting which SDK
79 * versions are allowed to access it.
80 */
81class ApiList {
82 private:
David Brazdil90faceb2018-12-14 14:36:15 +000083 // Number of bits reserved for Value in dex flags, and the corresponding bit mask.
84 static constexpr uint32_t kValueBitSize = 3;
85 static constexpr uint32_t kValueBitMask = helper::BitMask(kValueBitSize);
David Brazdildcfa89b2018-10-31 11:04:10 +000086
David Brazdil90faceb2018-12-14 14:36:15 +000087 enum class Value : uint32_t {
David Brazdildcfa89b2018-10-31 11:04:10 +000088 // Values independent of target SDK version of app
David Brazdil90faceb2018-12-14 14:36:15 +000089 kWhitelist = 0,
90 kGreylist = 1,
91 kBlacklist = 2,
David Brazdildcfa89b2018-10-31 11:04:10 +000092
93 // Values dependent on target SDK version of app. Put these last as
94 // their list will be extended in future releases.
95 // The max release code implicitly includes all maintenance releases,
96 // e.g. GreylistMaxO is accessible to targetSdkVersion <= 27 (O_MR1).
David Brazdil90faceb2018-12-14 14:36:15 +000097 kGreylistMaxO = 3,
98 kGreylistMaxP = 4,
David Brazdil6ae463f2019-05-01 11:55:01 +010099 kGreylistMaxQ = 5,
David Brazdildcfa89b2018-10-31 11:04:10 +0000100
101 // Special values
David Brazdil90faceb2018-12-14 14:36:15 +0000102 kInvalid = (static_cast<uint32_t>(-1) & kValueBitMask),
103 kMin = kWhitelist,
David Brazdil6ae463f2019-05-01 11:55:01 +0100104 kMax = kGreylistMaxQ,
David Brazdildcfa89b2018-10-31 11:04:10 +0000105 };
106
David Brazdil90faceb2018-12-14 14:36:15 +0000107 // Additional bit flags after the first kValueBitSize bits in dex flags.
108 // These are used for domain-specific API.
109 enum class DomainApi : uint32_t {
110 kCorePlatformApi = kValueBitSize,
Mariia Feofanova4945b292019-09-04 17:49:42 +0100111 kTestApi = kValueBitSize + 1,
David Brazdil90faceb2018-12-14 14:36:15 +0000112
113 // Special values
114 kMin = kCorePlatformApi,
Mariia Feofanova4945b292019-09-04 17:49:42 +0100115 kMax = kTestApi,
David Brazdil90faceb2018-12-14 14:36:15 +0000116 };
117
118 // Bit mask of all domain API flags.
119 static constexpr uint32_t kDomainApiBitMask = helper::BitMask<DomainApi>();
120
121 // Check that Values fit in the designated number of bits.
122 static_assert(kValueBitSize >= MinimumBitsToStore(helper::ToUint(Value::kMax)),
123 "Not enough bits to store all ApiList values");
124
125 // Sanity checks that all Values are covered by kValueBitMask.
126 static_assert(helper::MatchesBitMask(Value::kMin, kValueBitMask));
127 static_assert(helper::MatchesBitMask(Value::kMax, kValueBitMask));
128
129 // Assert that Value::kInvalid is larger than the maximum Value.
130 static_assert(helper::ToUint(Value::kMax) < helper::ToUint(Value::kInvalid));
131
132 // Names corresponding to Values.
133 static constexpr const char* kValueNames[] = {
David Brazdildcfa89b2018-10-31 11:04:10 +0000134 "whitelist",
135 "greylist",
136 "blacklist",
137 "greylist-max-o",
David Brazdil80d16282018-11-01 09:55:09 +0000138 "greylist-max-p",
David Brazdil6ae463f2019-05-01 11:55:01 +0100139 "greylist-max-q",
David Brazdildcfa89b2018-10-31 11:04:10 +0000140 };
141
David Brazdil90faceb2018-12-14 14:36:15 +0000142 // Names corresponding to DomainApis.
143 static constexpr const char* kDomainApiNames[] {
144 "core-platform-api",
Mariia Feofanova4945b292019-09-04 17:49:42 +0100145 "test-api",
David Brazdil90faceb2018-12-14 14:36:15 +0000146 };
David Brazdil91690d32018-11-04 18:07:23 +0000147
David Brazdil90faceb2018-12-14 14:36:15 +0000148 // Maximum SDK versions allowed to access ApiList of given Value.
David Brazdildcfa89b2018-10-31 11:04:10 +0000149 static constexpr SdkVersion kMaxSdkVersions[] {
150 /* whitelist */ SdkVersion::kMax,
151 /* greylist */ SdkVersion::kMax,
152 /* blacklist */ SdkVersion::kMin,
153 /* greylist-max-o */ SdkVersion::kO_MR1,
David Brazdil80d16282018-11-01 09:55:09 +0000154 /* greylist-max-p */ SdkVersion::kP,
David Brazdil6ae463f2019-05-01 11:55:01 +0100155 /* greylist-max-q */ SdkVersion::kQ,
David Brazdildcfa89b2018-10-31 11:04:10 +0000156 };
157
David Brazdil90faceb2018-12-14 14:36:15 +0000158 explicit ApiList(Value val, uint32_t domain_apis = 0u)
159 : dex_flags_(helper::ToUint(val) | domain_apis) {
160 DCHECK(GetValue() == val);
161 DCHECK_EQ(GetDomainApis(), domain_apis);
162 }
David Brazdildcfa89b2018-10-31 11:04:10 +0000163
David Brazdil90faceb2018-12-14 14:36:15 +0000164 explicit ApiList(DomainApi val) : ApiList(Value::kInvalid, helper::ToBit(val)) {}
David Brazdildcfa89b2018-10-31 11:04:10 +0000165
David Brazdil90faceb2018-12-14 14:36:15 +0000166 Value GetValue() const {
167 uint32_t value = (dex_flags_ & kValueBitMask);
168
169 // Treat all ones as invalid value
170 if (value == helper::ToUint(Value::kInvalid)) {
171 return Value::kInvalid;
172 } else {
173 DCHECK_GE(value, helper::ToUint(Value::kMin));
174 DCHECK_LE(value, helper::ToUint(Value::kMax));
175 return static_cast<Value>(value);
176 }
177 }
178
179 uint32_t GetDomainApis() const { return (dex_flags_ & kDomainApiBitMask); }
180
181 uint32_t dex_flags_;
David Brazdildcfa89b2018-10-31 11:04:10 +0000182
183 public:
David Brazdil62a4bcf2018-12-13 17:00:06 +0000184 ApiList() : ApiList(Value::kInvalid) {}
185
David Brazdil90faceb2018-12-14 14:36:15 +0000186 explicit ApiList(uint32_t dex_flags) : dex_flags_(dex_flags) {
187 DCHECK_EQ(dex_flags_, (dex_flags_ & kValueBitMask) | (dex_flags_ & kDomainApiBitMask));
188 }
189
190 // Helpers for conveniently constructing ApiList instances.
David Brazdildcfa89b2018-10-31 11:04:10 +0000191 static ApiList Whitelist() { return ApiList(Value::kWhitelist); }
192 static ApiList Greylist() { return ApiList(Value::kGreylist); }
193 static ApiList Blacklist() { return ApiList(Value::kBlacklist); }
194 static ApiList GreylistMaxO() { return ApiList(Value::kGreylistMaxO); }
David Brazdil80d16282018-11-01 09:55:09 +0000195 static ApiList GreylistMaxP() { return ApiList(Value::kGreylistMaxP); }
Artur Satayev201ffea2019-10-31 14:58:03 +0000196 static ApiList GreylistMaxQ() { return ApiList(Value::kGreylistMaxQ); }
David Brazdil90faceb2018-12-14 14:36:15 +0000197 static ApiList CorePlatformApi() { return ApiList(DomainApi::kCorePlatformApi); }
Mariia Feofanova4945b292019-09-04 17:49:42 +0100198 static ApiList TestApi() { return ApiList(DomainApi::kTestApi); }
David Brazdildcfa89b2018-10-31 11:04:10 +0000199
David Brazdil90faceb2018-12-14 14:36:15 +0000200 uint32_t GetDexFlags() const { return dex_flags_; }
201 uint32_t GetIntValue() const { return helper::ToUint(GetValue()) - helper::ToUint(Value::kMin); }
David Brazdildcfa89b2018-10-31 11:04:10 +0000202
David Brazdil90faceb2018-12-14 14:36:15 +0000203 // Returns the ApiList with a flag of a given name, or an empty ApiList if not matched.
David Brazdildcfa89b2018-10-31 11:04:10 +0000204 static ApiList FromName(const std::string& str) {
David Brazdil90faceb2018-12-14 14:36:15 +0000205 for (uint32_t i = 0; i < kValueCount; ++i) {
206 if (str == kValueNames[i]) {
Mariia Feofanova4945b292019-09-04 17:49:42 +0100207 return ApiList(helper::GetEnumAt<Value>(i));
David Brazdildcfa89b2018-10-31 11:04:10 +0000208 }
209 }
David Brazdil90faceb2018-12-14 14:36:15 +0000210 for (uint32_t i = 0; i < kDomainApiCount; ++i) {
211 if (str == kDomainApiNames[i]) {
Mariia Feofanova4945b292019-09-04 17:49:42 +0100212 return ApiList(helper::GetEnumAt<DomainApi>(i));
David Brazdil90faceb2018-12-14 14:36:15 +0000213 }
214 }
215 return ApiList();
David Brazdildcfa89b2018-10-31 11:04:10 +0000216 }
217
David Brazdil90faceb2018-12-14 14:36:15 +0000218 // Parses a vector of flag names into a single ApiList value. If successful,
219 // returns true and assigns the new ApiList to `out_api_list`.
220 static bool FromNames(std::vector<std::string>::iterator begin,
221 std::vector<std::string>::iterator end,
222 /* out */ ApiList* out_api_list) {
223 ApiList api_list;
224 for (std::vector<std::string>::iterator it = begin; it != end; it++) {
225 ApiList current = FromName(*it);
226 if (current.IsEmpty() || !api_list.CanCombineWith(current)) {
Andrei Onea370a0642019-03-01 17:48:27 +0000227 if (ApiStubs::IsStubsFlag(*it)) {
228 // Ignore flags which correspond to the stubs from where the api
229 // originates (i.e. system-api, test-api, public-api), as they are not
230 // relevant at runtime
231 continue;
232 }
David Brazdil90faceb2018-12-14 14:36:15 +0000233 return false;
234 }
235 api_list |= current;
236 }
237 if (out_api_list != nullptr) {
238 *out_api_list = api_list;
239 }
240 return true;
David Brazdildcfa89b2018-10-31 11:04:10 +0000241 }
242
David Brazdil90faceb2018-12-14 14:36:15 +0000243 bool operator==(const ApiList& other) const { return dex_flags_ == other.dex_flags_; }
244 bool operator!=(const ApiList& other) const { return !(*this == other); }
Artur Satayev7acb8462019-09-11 12:16:12 +0100245 bool operator<(const ApiList& other) const { return dex_flags_ < other.dex_flags_; }
David Brazdildcfa89b2018-10-31 11:04:10 +0000246
David Brazdil90faceb2018-12-14 14:36:15 +0000247 // Returns true if combining this ApiList with `other` will succeed.
248 bool CanCombineWith(const ApiList& other) const {
249 const Value val1 = GetValue();
250 const Value val2 = other.GetValue();
251 return (val1 == val2) || (val1 == Value::kInvalid) || (val2 == Value::kInvalid);
252 }
253
254 // Combine two ApiList instances.
255 ApiList operator|(const ApiList& other) {
256 // DomainApis are not mutually exclusive. Simply OR them.
257 const uint32_t domain_apis = GetDomainApis() | other.GetDomainApis();
258
259 // Values are mutually exclusive. Check if `this` and `other` have the same Value
260 // or if at most one is set.
261 const Value val1 = GetValue();
262 const Value val2 = other.GetValue();
263 if (val1 == val2) {
264 return ApiList(val1, domain_apis);
265 } else if (val1 == Value::kInvalid) {
266 return ApiList(val2, domain_apis);
267 } else if (val2 == Value::kInvalid) {
268 return ApiList(val1, domain_apis);
269 } else {
270 LOG(FATAL) << "Invalid combination of values " << Dumpable(ApiList(val1))
271 << " and " << Dumpable(ApiList(val2));
272 UNREACHABLE();
273 }
274 }
275
276 const ApiList& operator|=(const ApiList& other) {
277 (*this) = (*this) | other;
278 return *this;
279 }
280
281 // Returns true if all flags set in `other` are also set in `this`.
282 bool Contains(const ApiList& other) const {
283 return ((other.GetValue() == Value::kInvalid) || (GetValue() == other.GetValue())) &&
284 helper::MatchesBitMask(other.GetDomainApis(), GetDomainApis());
285 }
286
287 // Returns true whether the configuration is valid for runtime use.
288 bool IsValid() const { return GetValue() != Value::kInvalid; }
289
290 // Returns true when no ApiList is specified and no domain_api flags either.
291 bool IsEmpty() const { return (GetValue() == Value::kInvalid) && (GetDomainApis() == 0); }
292
Artur Satayev267366c2019-10-31 14:59:26 +0000293 // Returns true if the ApiList is on blacklist.
294 bool IsBlacklisted() const {
295 return GetValue() == Value::kBlacklist;
296 }
297
298 // Returns true if the ApiList is a test API.
299 bool IsTestApi() const {
300 return helper::MatchesBitMask(helper::ToBit(DomainApi::kTestApi), dex_flags_);
301 }
302
David Brazdil90faceb2018-12-14 14:36:15 +0000303 // Returns the maximum target SDK version allowed to access this ApiList.
David Brazdildcfa89b2018-10-31 11:04:10 +0000304 SdkVersion GetMaxAllowedSdkVersion() const { return kMaxSdkVersions[GetIntValue()]; }
305
David Brazdil90faceb2018-12-14 14:36:15 +0000306 void Dump(std::ostream& os) const {
307 bool is_first = true;
308
Artur Satayev39c399a2019-09-13 16:09:09 +0100309 if (IsEmpty()) {
310 os << "invalid";
311 return;
312 }
313
David Brazdil90faceb2018-12-14 14:36:15 +0000314 if (GetValue() != Value::kInvalid) {
315 os << kValueNames[GetIntValue()];
316 is_first = false;
317 }
318
319 const uint32_t domain_apis = GetDomainApis();
Mariia Feofanova4945b292019-09-04 17:49:42 +0100320 for (uint32_t i = 0; i < kDomainApiCount; i++) {
321 if (helper::MatchesBitMask(helper::ToBit(helper::GetEnumAt<DomainApi>(i)), domain_apis)) {
David Brazdil90faceb2018-12-14 14:36:15 +0000322 if (is_first) {
323 is_first = false;
324 } else {
325 os << ",";
326 }
Mariia Feofanova4945b292019-09-04 17:49:42 +0100327 os << kDomainApiNames[i];
David Brazdil90faceb2018-12-14 14:36:15 +0000328 }
329 }
330
331 DCHECK_EQ(IsEmpty(), is_first);
332 }
333
Artur Satayev39c399a2019-09-13 16:09:09 +0100334 // Number of valid enum values in Value.
David Brazdil90faceb2018-12-14 14:36:15 +0000335 static constexpr uint32_t kValueCount = helper::NumValues<Value>();
Artur Satayev39c399a2019-09-13 16:09:09 +0100336 // Number of valid enum values in DomainApi.
David Brazdil90faceb2018-12-14 14:36:15 +0000337 static constexpr uint32_t kDomainApiCount = helper::NumValues<DomainApi>();
Artur Satayev39c399a2019-09-13 16:09:09 +0100338 // Total number of possible enum values, including invalid, in Value.
339 static constexpr uint32_t kValueSize = (1u << kValueBitSize) + 1;
Mariia Feofanova4945b292019-09-04 17:49:42 +0100340
341 // Check min and max values are calculated correctly.
342 static_assert(Value::kMin == helper::GetEnumAt<Value>(0));
343 static_assert(Value::kMax == helper::GetEnumAt<Value>(kValueCount - 1));
344
345 static_assert(DomainApi::kMin == helper::GetEnumAt<DomainApi>(0));
346 static_assert(DomainApi::kMax == helper::GetEnumAt<DomainApi>(kDomainApiCount - 1));
David Brazdildcfa89b2018-10-31 11:04:10 +0000347};
348
349inline std::ostream& operator<<(std::ostream& os, ApiList value) {
David Brazdil90faceb2018-12-14 14:36:15 +0000350 value.Dump(os);
David Brazdildcfa89b2018-10-31 11:04:10 +0000351 return os;
352}
353
David Brazdildcfa89b2018-10-31 11:04:10 +0000354} // namespace hiddenapi
355} // namespace art
356
357
358#endif // ART_LIBARTBASE_BASE_HIDDENAPI_FLAGS_H_