blob: 59e17a8d90e3e918adf349b408814bc2c823ffe6 [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; }
67} // namespace helper
68
David Brazdildcfa89b2018-10-31 11:04:10 +000069/*
70 * This class represents the information whether a field/method is in
71 * public API (whitelist) or if it isn't, apps targeting which SDK
72 * versions are allowed to access it.
73 */
74class ApiList {
75 private:
David Brazdil90faceb2018-12-14 14:36:15 +000076 // Number of bits reserved for Value in dex flags, and the corresponding bit mask.
77 static constexpr uint32_t kValueBitSize = 3;
78 static constexpr uint32_t kValueBitMask = helper::BitMask(kValueBitSize);
David Brazdildcfa89b2018-10-31 11:04:10 +000079
David Brazdil90faceb2018-12-14 14:36:15 +000080 enum class Value : uint32_t {
David Brazdildcfa89b2018-10-31 11:04:10 +000081 // Values independent of target SDK version of app
David Brazdil90faceb2018-12-14 14:36:15 +000082 kWhitelist = 0,
83 kGreylist = 1,
84 kBlacklist = 2,
David Brazdildcfa89b2018-10-31 11:04:10 +000085
86 // Values dependent on target SDK version of app. Put these last as
87 // their list will be extended in future releases.
88 // The max release code implicitly includes all maintenance releases,
89 // e.g. GreylistMaxO is accessible to targetSdkVersion <= 27 (O_MR1).
David Brazdil90faceb2018-12-14 14:36:15 +000090 kGreylistMaxO = 3,
91 kGreylistMaxP = 4,
David Brazdil6ae463f2019-05-01 11:55:01 +010092 kGreylistMaxQ = 5,
David Brazdildcfa89b2018-10-31 11:04:10 +000093
94 // Special values
David Brazdil90faceb2018-12-14 14:36:15 +000095 kInvalid = (static_cast<uint32_t>(-1) & kValueBitMask),
96 kMin = kWhitelist,
David Brazdil6ae463f2019-05-01 11:55:01 +010097 kMax = kGreylistMaxQ,
David Brazdildcfa89b2018-10-31 11:04:10 +000098 };
99
David Brazdil90faceb2018-12-14 14:36:15 +0000100 // Additional bit flags after the first kValueBitSize bits in dex flags.
101 // These are used for domain-specific API.
102 enum class DomainApi : uint32_t {
103 kCorePlatformApi = kValueBitSize,
104
105 // Special values
106 kMin = kCorePlatformApi,
107 kMax = kCorePlatformApi,
108 };
109
110 // Bit mask of all domain API flags.
111 static constexpr uint32_t kDomainApiBitMask = helper::BitMask<DomainApi>();
112
113 // Check that Values fit in the designated number of bits.
114 static_assert(kValueBitSize >= MinimumBitsToStore(helper::ToUint(Value::kMax)),
115 "Not enough bits to store all ApiList values");
116
117 // Sanity checks that all Values are covered by kValueBitMask.
118 static_assert(helper::MatchesBitMask(Value::kMin, kValueBitMask));
119 static_assert(helper::MatchesBitMask(Value::kMax, kValueBitMask));
120
121 // Assert that Value::kInvalid is larger than the maximum Value.
122 static_assert(helper::ToUint(Value::kMax) < helper::ToUint(Value::kInvalid));
123
124 // Names corresponding to Values.
125 static constexpr const char* kValueNames[] = {
David Brazdildcfa89b2018-10-31 11:04:10 +0000126 "whitelist",
127 "greylist",
128 "blacklist",
129 "greylist-max-o",
David Brazdil80d16282018-11-01 09:55:09 +0000130 "greylist-max-p",
David Brazdil6ae463f2019-05-01 11:55:01 +0100131 "greylist-max-q",
David Brazdildcfa89b2018-10-31 11:04:10 +0000132 };
133
David Brazdil90faceb2018-12-14 14:36:15 +0000134 // Names corresponding to DomainApis.
135 static constexpr const char* kDomainApiNames[] {
136 "core-platform-api",
137 };
David Brazdil91690d32018-11-04 18:07:23 +0000138
David Brazdil90faceb2018-12-14 14:36:15 +0000139 // Maximum SDK versions allowed to access ApiList of given Value.
David Brazdildcfa89b2018-10-31 11:04:10 +0000140 static constexpr SdkVersion kMaxSdkVersions[] {
141 /* whitelist */ SdkVersion::kMax,
142 /* greylist */ SdkVersion::kMax,
143 /* blacklist */ SdkVersion::kMin,
144 /* greylist-max-o */ SdkVersion::kO_MR1,
David Brazdil80d16282018-11-01 09:55:09 +0000145 /* greylist-max-p */ SdkVersion::kP,
David Brazdil6ae463f2019-05-01 11:55:01 +0100146 /* greylist-max-q */ SdkVersion::kQ,
David Brazdildcfa89b2018-10-31 11:04:10 +0000147 };
148
David Brazdil90faceb2018-12-14 14:36:15 +0000149 explicit ApiList(Value val, uint32_t domain_apis = 0u)
150 : dex_flags_(helper::ToUint(val) | domain_apis) {
151 DCHECK(GetValue() == val);
152 DCHECK_EQ(GetDomainApis(), domain_apis);
153 }
David Brazdildcfa89b2018-10-31 11:04:10 +0000154
David Brazdil90faceb2018-12-14 14:36:15 +0000155 explicit ApiList(DomainApi val) : ApiList(Value::kInvalid, helper::ToBit(val)) {}
David Brazdildcfa89b2018-10-31 11:04:10 +0000156
David Brazdil90faceb2018-12-14 14:36:15 +0000157 Value GetValue() const {
158 uint32_t value = (dex_flags_ & kValueBitMask);
159
160 // Treat all ones as invalid value
161 if (value == helper::ToUint(Value::kInvalid)) {
162 return Value::kInvalid;
163 } else {
164 DCHECK_GE(value, helper::ToUint(Value::kMin));
165 DCHECK_LE(value, helper::ToUint(Value::kMax));
166 return static_cast<Value>(value);
167 }
168 }
169
170 uint32_t GetDomainApis() const { return (dex_flags_ & kDomainApiBitMask); }
171
172 uint32_t dex_flags_;
David Brazdildcfa89b2018-10-31 11:04:10 +0000173
174 public:
David Brazdil62a4bcf2018-12-13 17:00:06 +0000175 ApiList() : ApiList(Value::kInvalid) {}
176
David Brazdil90faceb2018-12-14 14:36:15 +0000177 explicit ApiList(uint32_t dex_flags) : dex_flags_(dex_flags) {
178 DCHECK_EQ(dex_flags_, (dex_flags_ & kValueBitMask) | (dex_flags_ & kDomainApiBitMask));
179 }
180
181 // Helpers for conveniently constructing ApiList instances.
David Brazdildcfa89b2018-10-31 11:04:10 +0000182 static ApiList Whitelist() { return ApiList(Value::kWhitelist); }
183 static ApiList Greylist() { return ApiList(Value::kGreylist); }
184 static ApiList Blacklist() { return ApiList(Value::kBlacklist); }
185 static ApiList GreylistMaxO() { return ApiList(Value::kGreylistMaxO); }
David Brazdil80d16282018-11-01 09:55:09 +0000186 static ApiList GreylistMaxP() { return ApiList(Value::kGreylistMaxP); }
David Brazdil90faceb2018-12-14 14:36:15 +0000187 static ApiList CorePlatformApi() { return ApiList(DomainApi::kCorePlatformApi); }
David Brazdildcfa89b2018-10-31 11:04:10 +0000188
David Brazdil90faceb2018-12-14 14:36:15 +0000189 uint32_t GetDexFlags() const { return dex_flags_; }
190 uint32_t GetIntValue() const { return helper::ToUint(GetValue()) - helper::ToUint(Value::kMin); }
David Brazdildcfa89b2018-10-31 11:04:10 +0000191
David Brazdil90faceb2018-12-14 14:36:15 +0000192 // Returns the ApiList with a flag of a given name, or an empty ApiList if not matched.
David Brazdildcfa89b2018-10-31 11:04:10 +0000193 static ApiList FromName(const std::string& str) {
David Brazdil90faceb2018-12-14 14:36:15 +0000194 for (uint32_t i = 0; i < kValueCount; ++i) {
195 if (str == kValueNames[i]) {
196 return ApiList(static_cast<Value>(i + helper::ToUint(Value::kMin)));
David Brazdildcfa89b2018-10-31 11:04:10 +0000197 }
198 }
David Brazdil90faceb2018-12-14 14:36:15 +0000199 for (uint32_t i = 0; i < kDomainApiCount; ++i) {
200 if (str == kDomainApiNames[i]) {
201 return ApiList(static_cast<DomainApi>(i + helper::ToUint(DomainApi::kMin)));
202 }
203 }
204 return ApiList();
David Brazdildcfa89b2018-10-31 11:04:10 +0000205 }
206
David Brazdil90faceb2018-12-14 14:36:15 +0000207 // Parses a vector of flag names into a single ApiList value. If successful,
208 // returns true and assigns the new ApiList to `out_api_list`.
209 static bool FromNames(std::vector<std::string>::iterator begin,
210 std::vector<std::string>::iterator end,
211 /* out */ ApiList* out_api_list) {
212 ApiList api_list;
213 for (std::vector<std::string>::iterator it = begin; it != end; it++) {
214 ApiList current = FromName(*it);
215 if (current.IsEmpty() || !api_list.CanCombineWith(current)) {
Andrei Onea370a0642019-03-01 17:48:27 +0000216 if (ApiStubs::IsStubsFlag(*it)) {
217 // Ignore flags which correspond to the stubs from where the api
218 // originates (i.e. system-api, test-api, public-api), as they are not
219 // relevant at runtime
220 continue;
221 }
David Brazdil90faceb2018-12-14 14:36:15 +0000222 return false;
223 }
224 api_list |= current;
225 }
226 if (out_api_list != nullptr) {
227 *out_api_list = api_list;
228 }
229 return true;
David Brazdildcfa89b2018-10-31 11:04:10 +0000230 }
231
David Brazdil90faceb2018-12-14 14:36:15 +0000232 bool operator==(const ApiList& other) const { return dex_flags_ == other.dex_flags_; }
233 bool operator!=(const ApiList& other) const { return !(*this == other); }
David Brazdildcfa89b2018-10-31 11:04:10 +0000234
David Brazdil90faceb2018-12-14 14:36:15 +0000235 // Returns true if combining this ApiList with `other` will succeed.
236 bool CanCombineWith(const ApiList& other) const {
237 const Value val1 = GetValue();
238 const Value val2 = other.GetValue();
239 return (val1 == val2) || (val1 == Value::kInvalid) || (val2 == Value::kInvalid);
240 }
241
242 // Combine two ApiList instances.
243 ApiList operator|(const ApiList& other) {
244 // DomainApis are not mutually exclusive. Simply OR them.
245 const uint32_t domain_apis = GetDomainApis() | other.GetDomainApis();
246
247 // Values are mutually exclusive. Check if `this` and `other` have the same Value
248 // or if at most one is set.
249 const Value val1 = GetValue();
250 const Value val2 = other.GetValue();
251 if (val1 == val2) {
252 return ApiList(val1, domain_apis);
253 } else if (val1 == Value::kInvalid) {
254 return ApiList(val2, domain_apis);
255 } else if (val2 == Value::kInvalid) {
256 return ApiList(val1, domain_apis);
257 } else {
258 LOG(FATAL) << "Invalid combination of values " << Dumpable(ApiList(val1))
259 << " and " << Dumpable(ApiList(val2));
260 UNREACHABLE();
261 }
262 }
263
264 const ApiList& operator|=(const ApiList& other) {
265 (*this) = (*this) | other;
266 return *this;
267 }
268
269 // Returns true if all flags set in `other` are also set in `this`.
270 bool Contains(const ApiList& other) const {
271 return ((other.GetValue() == Value::kInvalid) || (GetValue() == other.GetValue())) &&
272 helper::MatchesBitMask(other.GetDomainApis(), GetDomainApis());
273 }
274
275 // Returns true whether the configuration is valid for runtime use.
276 bool IsValid() const { return GetValue() != Value::kInvalid; }
277
278 // Returns true when no ApiList is specified and no domain_api flags either.
279 bool IsEmpty() const { return (GetValue() == Value::kInvalid) && (GetDomainApis() == 0); }
280
281 // Returns the maximum target SDK version allowed to access this ApiList.
David Brazdildcfa89b2018-10-31 11:04:10 +0000282 SdkVersion GetMaxAllowedSdkVersion() const { return kMaxSdkVersions[GetIntValue()]; }
283
David Brazdil90faceb2018-12-14 14:36:15 +0000284 void Dump(std::ostream& os) const {
285 bool is_first = true;
286
287 if (GetValue() != Value::kInvalid) {
288 os << kValueNames[GetIntValue()];
289 is_first = false;
290 }
291
292 const uint32_t domain_apis = GetDomainApis();
293 for (uint32_t i = helper::ToUint(DomainApi::kMin); i <= helper::ToUint(DomainApi::kMax); i++) {
294 if (helper::MatchesBitMask(helper::ToBit(i), domain_apis)) {
295 if (is_first) {
296 is_first = false;
297 } else {
298 os << ",";
299 }
Peter Collingbourne323e8c62019-08-20 11:13:58 -0700300 os << kDomainApiNames[i - helper::ToUint(DomainApi::kMin)];
David Brazdil90faceb2018-12-14 14:36:15 +0000301 }
302 }
303
304 DCHECK_EQ(IsEmpty(), is_first);
305 }
306
307 static constexpr uint32_t kValueCount = helper::NumValues<Value>();
308 static constexpr uint32_t kDomainApiCount = helper::NumValues<DomainApi>();
David Brazdildcfa89b2018-10-31 11:04:10 +0000309};
310
311inline std::ostream& operator<<(std::ostream& os, ApiList value) {
David Brazdil90faceb2018-12-14 14:36:15 +0000312 value.Dump(os);
David Brazdildcfa89b2018-10-31 11:04:10 +0000313 return os;
314}
315
David Brazdildcfa89b2018-10-31 11:04:10 +0000316} // namespace hiddenapi
317} // namespace art
318
319
320#endif // ART_LIBARTBASE_BASE_HIDDENAPI_FLAGS_H_