David Brazdil | dcfa89b | 2018-10-31 11:04:10 +0000 | [diff] [blame] | 1 | /* |
| 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 Brazdil | 90faceb | 2018-12-14 14:36:15 +0000 | [diff] [blame] | 22 | #include <vector> |
| 23 | |
David Brazdil | dcfa89b | 2018-10-31 11:04:10 +0000 | [diff] [blame] | 24 | #include "android-base/logging.h" |
David Brazdil | 90faceb | 2018-12-14 14:36:15 +0000 | [diff] [blame] | 25 | #include "base/bit_utils.h" |
| 26 | #include "base/dumpable.h" |
| 27 | #include "base/macros.h" |
Andrei Onea | 370a064 | 2019-03-01 17:48:27 +0000 | [diff] [blame] | 28 | #include "base/hiddenapi_stubs.h" |
David Brazdil | dcfa89b | 2018-10-31 11:04:10 +0000 | [diff] [blame] | 29 | |
| 30 | namespace art { |
| 31 | namespace hiddenapi { |
| 32 | |
David Brazdil | 90faceb | 2018-12-14 14:36:15 +0000 | [diff] [blame] | 33 | // 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. |
| 36 | namespace 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 Feofanova | 4945b29 | 2019-09-04 17:49:42 +0100 | [diff] [blame] | 67 | |
| 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 Brazdil | 90faceb | 2018-12-14 14:36:15 +0000 | [diff] [blame] | 74 | } // namespace helper |
| 75 | |
David Brazdil | dcfa89b | 2018-10-31 11:04:10 +0000 | [diff] [blame] | 76 | /* |
| 77 | * This class represents the information whether a field/method is in |
Orion Hodson | 611d71c | 2021-08-27 17:00:10 +0100 | [diff] [blame] | 78 | * public API (SDK) or if it isn't, apps targeting which SDK |
David Brazdil | dcfa89b | 2018-10-31 11:04:10 +0000 | [diff] [blame] | 79 | * versions are allowed to access it. |
| 80 | */ |
| 81 | class ApiList { |
| 82 | private: |
David Brazdil | 90faceb | 2018-12-14 14:36:15 +0000 | [diff] [blame] | 83 | // Number of bits reserved for Value in dex flags, and the corresponding bit mask. |
Nicolas Geoffray | 56f4c81 | 2021-04-01 14:06:23 +0100 | [diff] [blame] | 84 | static constexpr uint32_t kValueBitSize = 4; |
David Brazdil | 90faceb | 2018-12-14 14:36:15 +0000 | [diff] [blame] | 85 | static constexpr uint32_t kValueBitMask = helper::BitMask(kValueBitSize); |
David Brazdil | dcfa89b | 2018-10-31 11:04:10 +0000 | [diff] [blame] | 86 | |
David Brazdil | 90faceb | 2018-12-14 14:36:15 +0000 | [diff] [blame] | 87 | enum class Value : uint32_t { |
David Brazdil | dcfa89b | 2018-10-31 11:04:10 +0000 | [diff] [blame] | 88 | // Values independent of target SDK version of app |
Andrei Onea | fc12a6c | 2020-07-29 19:52:34 +0100 | [diff] [blame] | 89 | kSdk = 0, |
| 90 | kUnsupported = 1, |
| 91 | kBlocked = 2, |
David Brazdil | dcfa89b | 2018-10-31 11:04:10 +0000 | [diff] [blame] | 92 | |
| 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, |
Andrei Onea | fc12a6c | 2020-07-29 19:52:34 +0100 | [diff] [blame] | 96 | // e.g. MaxTargetO is accessible to targetSdkVersion <= 27 (O_MR1). |
| 97 | kMaxTargetO = 3, |
| 98 | kMaxTargetP = 4, |
| 99 | kMaxTargetQ = 5, |
| 100 | kMaxTargetR = 6, |
Artur Satayev | 36c0cb0 | 2022-05-04 12:28:59 +0000 | [diff] [blame] | 101 | kMaxTargetS = 7, |
David Brazdil | dcfa89b | 2018-10-31 11:04:10 +0000 | [diff] [blame] | 102 | |
| 103 | // Special values |
David Brazdil | 90faceb | 2018-12-14 14:36:15 +0000 | [diff] [blame] | 104 | kInvalid = (static_cast<uint32_t>(-1) & kValueBitMask), |
Andrei Onea | fc12a6c | 2020-07-29 19:52:34 +0100 | [diff] [blame] | 105 | kMin = kSdk, |
Artur Satayev | 36c0cb0 | 2022-05-04 12:28:59 +0000 | [diff] [blame] | 106 | kMax = kMaxTargetS, |
David Brazdil | dcfa89b | 2018-10-31 11:04:10 +0000 | [diff] [blame] | 107 | }; |
| 108 | |
David Brazdil | 90faceb | 2018-12-14 14:36:15 +0000 | [diff] [blame] | 109 | // Additional bit flags after the first kValueBitSize bits in dex flags. |
| 110 | // These are used for domain-specific API. |
| 111 | enum class DomainApi : uint32_t { |
| 112 | kCorePlatformApi = kValueBitSize, |
Mariia Feofanova | 4945b29 | 2019-09-04 17:49:42 +0100 | [diff] [blame] | 113 | kTestApi = kValueBitSize + 1, |
David Brazdil | 90faceb | 2018-12-14 14:36:15 +0000 | [diff] [blame] | 114 | |
| 115 | // Special values |
| 116 | kMin = kCorePlatformApi, |
Mariia Feofanova | 4945b29 | 2019-09-04 17:49:42 +0100 | [diff] [blame] | 117 | kMax = kTestApi, |
David Brazdil | 90faceb | 2018-12-14 14:36:15 +0000 | [diff] [blame] | 118 | }; |
| 119 | |
| 120 | // Bit mask of all domain API flags. |
| 121 | static constexpr uint32_t kDomainApiBitMask = helper::BitMask<DomainApi>(); |
| 122 | |
| 123 | // Check that Values fit in the designated number of bits. |
| 124 | static_assert(kValueBitSize >= MinimumBitsToStore(helper::ToUint(Value::kMax)), |
| 125 | "Not enough bits to store all ApiList values"); |
| 126 | |
Ian Pedowitz | 2d53643 | 2020-07-22 14:33:00 -0700 | [diff] [blame] | 127 | // Checks that all Values are covered by kValueBitMask. |
David Brazdil | 90faceb | 2018-12-14 14:36:15 +0000 | [diff] [blame] | 128 | static_assert(helper::MatchesBitMask(Value::kMin, kValueBitMask)); |
| 129 | static_assert(helper::MatchesBitMask(Value::kMax, kValueBitMask)); |
| 130 | |
| 131 | // Assert that Value::kInvalid is larger than the maximum Value. |
| 132 | static_assert(helper::ToUint(Value::kMax) < helper::ToUint(Value::kInvalid)); |
| 133 | |
| 134 | // Names corresponding to Values. |
| 135 | static constexpr const char* kValueNames[] = { |
Andrei Onea | 02db072 | 2020-08-07 14:56:59 +0100 | [diff] [blame] | 136 | "sdk", |
| 137 | "unsupported", |
| 138 | "blocked", |
| 139 | "max-target-o", |
| 140 | "max-target-p", |
| 141 | "max-target-q", |
| 142 | "max-target-r", |
Artur Satayev | 36c0cb0 | 2022-05-04 12:28:59 +0000 | [diff] [blame] | 143 | "max-target-s", |
David Brazdil | dcfa89b | 2018-10-31 11:04:10 +0000 | [diff] [blame] | 144 | }; |
| 145 | |
Nicolas Geoffray | 56f4c81 | 2021-04-01 14:06:23 +0100 | [diff] [blame] | 146 | // A magic marker used by tests to mimic a hiddenapi list which doesn't exist |
| 147 | // yet. |
| 148 | static constexpr const char* kFutureValueName = "max-target-future"; |
| 149 | |
David Brazdil | 90faceb | 2018-12-14 14:36:15 +0000 | [diff] [blame] | 150 | // Names corresponding to DomainApis. |
| 151 | static constexpr const char* kDomainApiNames[] { |
| 152 | "core-platform-api", |
Mariia Feofanova | 4945b29 | 2019-09-04 17:49:42 +0100 | [diff] [blame] | 153 | "test-api", |
David Brazdil | 90faceb | 2018-12-14 14:36:15 +0000 | [diff] [blame] | 154 | }; |
David Brazdil | 91690d3 | 2018-11-04 18:07:23 +0000 | [diff] [blame] | 155 | |
David Brazdil | 90faceb | 2018-12-14 14:36:15 +0000 | [diff] [blame] | 156 | // Maximum SDK versions allowed to access ApiList of given Value. |
David Brazdil | dcfa89b | 2018-10-31 11:04:10 +0000 | [diff] [blame] | 157 | static constexpr SdkVersion kMaxSdkVersions[] { |
Andrei Onea | fc12a6c | 2020-07-29 19:52:34 +0100 | [diff] [blame] | 158 | /* sdk */ SdkVersion::kMax, |
| 159 | /* unsupported */ SdkVersion::kMax, |
| 160 | /* blocklist */ SdkVersion::kMin, |
| 161 | /* max-target-o */ SdkVersion::kO_MR1, |
| 162 | /* max-target-p */ SdkVersion::kP, |
| 163 | /* max-target-q */ SdkVersion::kQ, |
| 164 | /* max-target-r */ SdkVersion::kR, |
Artur Satayev | 36c0cb0 | 2022-05-04 12:28:59 +0000 | [diff] [blame] | 165 | /* max-target-s */ SdkVersion::kS, |
David Brazdil | dcfa89b | 2018-10-31 11:04:10 +0000 | [diff] [blame] | 166 | }; |
| 167 | |
David Brazdil | 90faceb | 2018-12-14 14:36:15 +0000 | [diff] [blame] | 168 | explicit ApiList(Value val, uint32_t domain_apis = 0u) |
| 169 | : dex_flags_(helper::ToUint(val) | domain_apis) { |
| 170 | DCHECK(GetValue() == val); |
| 171 | DCHECK_EQ(GetDomainApis(), domain_apis); |
| 172 | } |
David Brazdil | dcfa89b | 2018-10-31 11:04:10 +0000 | [diff] [blame] | 173 | |
David Brazdil | 90faceb | 2018-12-14 14:36:15 +0000 | [diff] [blame] | 174 | explicit ApiList(DomainApi val) : ApiList(Value::kInvalid, helper::ToBit(val)) {} |
David Brazdil | dcfa89b | 2018-10-31 11:04:10 +0000 | [diff] [blame] | 175 | |
David Brazdil | 90faceb | 2018-12-14 14:36:15 +0000 | [diff] [blame] | 176 | Value GetValue() const { |
| 177 | uint32_t value = (dex_flags_ & kValueBitMask); |
| 178 | |
| 179 | // Treat all ones as invalid value |
| 180 | if (value == helper::ToUint(Value::kInvalid)) { |
| 181 | return Value::kInvalid; |
Nicolas Geoffray | 56f4c81 | 2021-04-01 14:06:23 +0100 | [diff] [blame] | 182 | } else if (value > helper::ToUint(Value::kMax)) { |
| 183 | // For future unknown flag values, return unsupported. |
| 184 | return Value::kUnsupported; |
David Brazdil | 90faceb | 2018-12-14 14:36:15 +0000 | [diff] [blame] | 185 | } else { |
| 186 | DCHECK_GE(value, helper::ToUint(Value::kMin)); |
David Brazdil | 90faceb | 2018-12-14 14:36:15 +0000 | [diff] [blame] | 187 | return static_cast<Value>(value); |
| 188 | } |
| 189 | } |
| 190 | |
| 191 | uint32_t GetDomainApis() const { return (dex_flags_ & kDomainApiBitMask); } |
| 192 | |
| 193 | uint32_t dex_flags_; |
David Brazdil | dcfa89b | 2018-10-31 11:04:10 +0000 | [diff] [blame] | 194 | |
| 195 | public: |
David Brazdil | 62a4bcf | 2018-12-13 17:00:06 +0000 | [diff] [blame] | 196 | ApiList() : ApiList(Value::kInvalid) {} |
| 197 | |
David Brazdil | 90faceb | 2018-12-14 14:36:15 +0000 | [diff] [blame] | 198 | explicit ApiList(uint32_t dex_flags) : dex_flags_(dex_flags) { |
| 199 | DCHECK_EQ(dex_flags_, (dex_flags_ & kValueBitMask) | (dex_flags_ & kDomainApiBitMask)); |
| 200 | } |
| 201 | |
| 202 | // Helpers for conveniently constructing ApiList instances. |
Andrei Onea | fc12a6c | 2020-07-29 19:52:34 +0100 | [diff] [blame] | 203 | static ApiList Sdk() { return ApiList(Value::kSdk); } |
| 204 | static ApiList Unsupported() { return ApiList(Value::kUnsupported); } |
| 205 | static ApiList Blocked() { return ApiList(Value::kBlocked); } |
| 206 | static ApiList MaxTargetO() { return ApiList(Value::kMaxTargetO); } |
| 207 | static ApiList MaxTargetP() { return ApiList(Value::kMaxTargetP); } |
| 208 | static ApiList MaxTargetQ() { return ApiList(Value::kMaxTargetQ); } |
| 209 | static ApiList MaxTargetR() { return ApiList(Value::kMaxTargetR); } |
Artur Satayev | 36c0cb0 | 2022-05-04 12:28:59 +0000 | [diff] [blame] | 210 | static ApiList MaxTargetS() { return ApiList(Value::kMaxTargetS); } |
David Brazdil | 90faceb | 2018-12-14 14:36:15 +0000 | [diff] [blame] | 211 | static ApiList CorePlatformApi() { return ApiList(DomainApi::kCorePlatformApi); } |
Mariia Feofanova | 4945b29 | 2019-09-04 17:49:42 +0100 | [diff] [blame] | 212 | static ApiList TestApi() { return ApiList(DomainApi::kTestApi); } |
David Brazdil | dcfa89b | 2018-10-31 11:04:10 +0000 | [diff] [blame] | 213 | |
David Brazdil | 90faceb | 2018-12-14 14:36:15 +0000 | [diff] [blame] | 214 | uint32_t GetDexFlags() const { return dex_flags_; } |
| 215 | uint32_t GetIntValue() const { return helper::ToUint(GetValue()) - helper::ToUint(Value::kMin); } |
David Brazdil | dcfa89b | 2018-10-31 11:04:10 +0000 | [diff] [blame] | 216 | |
David Brazdil | 90faceb | 2018-12-14 14:36:15 +0000 | [diff] [blame] | 217 | // Returns the ApiList with a flag of a given name, or an empty ApiList if not matched. |
David Brazdil | dcfa89b | 2018-10-31 11:04:10 +0000 | [diff] [blame] | 218 | static ApiList FromName(const std::string& str) { |
David Brazdil | 90faceb | 2018-12-14 14:36:15 +0000 | [diff] [blame] | 219 | for (uint32_t i = 0; i < kValueCount; ++i) { |
| 220 | if (str == kValueNames[i]) { |
Mariia Feofanova | 4945b29 | 2019-09-04 17:49:42 +0100 | [diff] [blame] | 221 | return ApiList(helper::GetEnumAt<Value>(i)); |
David Brazdil | dcfa89b | 2018-10-31 11:04:10 +0000 | [diff] [blame] | 222 | } |
| 223 | } |
David Brazdil | 90faceb | 2018-12-14 14:36:15 +0000 | [diff] [blame] | 224 | for (uint32_t i = 0; i < kDomainApiCount; ++i) { |
| 225 | if (str == kDomainApiNames[i]) { |
Mariia Feofanova | 4945b29 | 2019-09-04 17:49:42 +0100 | [diff] [blame] | 226 | return ApiList(helper::GetEnumAt<DomainApi>(i)); |
David Brazdil | 90faceb | 2018-12-14 14:36:15 +0000 | [diff] [blame] | 227 | } |
| 228 | } |
Nicolas Geoffray | 56f4c81 | 2021-04-01 14:06:23 +0100 | [diff] [blame] | 229 | if (str == kFutureValueName) { |
| 230 | static_assert(helper::ToUint(Value::kMax) + 1 < helper::ToUint(Value::kInvalid)); |
| 231 | return ApiList(helper::ToUint(Value::kMax) + 1); |
| 232 | } |
David Brazdil | 90faceb | 2018-12-14 14:36:15 +0000 | [diff] [blame] | 233 | return ApiList(); |
David Brazdil | dcfa89b | 2018-10-31 11:04:10 +0000 | [diff] [blame] | 234 | } |
| 235 | |
David Brazdil | 90faceb | 2018-12-14 14:36:15 +0000 | [diff] [blame] | 236 | // Parses a vector of flag names into a single ApiList value. If successful, |
| 237 | // returns true and assigns the new ApiList to `out_api_list`. |
| 238 | static bool FromNames(std::vector<std::string>::iterator begin, |
| 239 | std::vector<std::string>::iterator end, |
| 240 | /* out */ ApiList* out_api_list) { |
| 241 | ApiList api_list; |
| 242 | for (std::vector<std::string>::iterator it = begin; it != end; it++) { |
| 243 | ApiList current = FromName(*it); |
| 244 | if (current.IsEmpty() || !api_list.CanCombineWith(current)) { |
Andrei Onea | 370a064 | 2019-03-01 17:48:27 +0000 | [diff] [blame] | 245 | if (ApiStubs::IsStubsFlag(*it)) { |
| 246 | // Ignore flags which correspond to the stubs from where the api |
| 247 | // originates (i.e. system-api, test-api, public-api), as they are not |
| 248 | // relevant at runtime |
| 249 | continue; |
| 250 | } |
David Brazdil | 90faceb | 2018-12-14 14:36:15 +0000 | [diff] [blame] | 251 | return false; |
| 252 | } |
| 253 | api_list |= current; |
| 254 | } |
| 255 | if (out_api_list != nullptr) { |
| 256 | *out_api_list = api_list; |
| 257 | } |
| 258 | return true; |
David Brazdil | dcfa89b | 2018-10-31 11:04:10 +0000 | [diff] [blame] | 259 | } |
| 260 | |
David Brazdil | 90faceb | 2018-12-14 14:36:15 +0000 | [diff] [blame] | 261 | bool operator==(const ApiList& other) const { return dex_flags_ == other.dex_flags_; } |
| 262 | bool operator!=(const ApiList& other) const { return !(*this == other); } |
Artur Satayev | 7acb846 | 2019-09-11 12:16:12 +0100 | [diff] [blame] | 263 | bool operator<(const ApiList& other) const { return dex_flags_ < other.dex_flags_; } |
Andrei Onea | a6b3b29 | 2021-05-19 16:22:46 +0100 | [diff] [blame] | 264 | bool operator>(const ApiList& other) const { return dex_flags_ > other.dex_flags_; } |
David Brazdil | dcfa89b | 2018-10-31 11:04:10 +0000 | [diff] [blame] | 265 | |
Paul Duffin | c03eafe | 2022-12-21 14:03:57 +0000 | [diff] [blame] | 266 | // Returns true if combining this ApiList (using | operator) with `other` will succeed. |
| 267 | // |
| 268 | // b/253335210 - This returns true as the | operator will combine any two ApiLists. |
David Brazdil | 90faceb | 2018-12-14 14:36:15 +0000 | [diff] [blame] | 269 | bool CanCombineWith(const ApiList& other) const { |
Paul Duffin | c03eafe | 2022-12-21 14:03:57 +0000 | [diff] [blame] | 270 | UNUSED(other); |
| 271 | return true; |
David Brazdil | 90faceb | 2018-12-14 14:36:15 +0000 | [diff] [blame] | 272 | } |
| 273 | |
| 274 | // Combine two ApiList instances. |
| 275 | ApiList operator|(const ApiList& other) { |
| 276 | // DomainApis are not mutually exclusive. Simply OR them. |
| 277 | const uint32_t domain_apis = GetDomainApis() | other.GetDomainApis(); |
| 278 | |
Paul Duffin | c03eafe | 2022-12-21 14:03:57 +0000 | [diff] [blame] | 279 | // Values are mutually exclusive but the most accessible value is chosen. |
| 280 | // Check if `this` and `other` have the same Value, or if at most one is set and |
| 281 | // if so use the set value. Otherwise, use the value that is most accessible. |
David Brazdil | 90faceb | 2018-12-14 14:36:15 +0000 | [diff] [blame] | 282 | const Value val1 = GetValue(); |
| 283 | const Value val2 = other.GetValue(); |
| 284 | if (val1 == val2) { |
| 285 | return ApiList(val1, domain_apis); |
| 286 | } else if (val1 == Value::kInvalid) { |
| 287 | return ApiList(val2, domain_apis); |
| 288 | } else if (val2 == Value::kInvalid) { |
| 289 | return ApiList(val1, domain_apis); |
Paul Duffin | c03eafe | 2022-12-21 14:03:57 +0000 | [diff] [blame] | 290 | } else if (val1 < val2) { |
| 291 | return ApiList(val1, domain_apis); |
| 292 | } else if (val1 > val2) { |
| 293 | return ApiList(val2, domain_apis); |
David Brazdil | 90faceb | 2018-12-14 14:36:15 +0000 | [diff] [blame] | 294 | } else { |
| 295 | LOG(FATAL) << "Invalid combination of values " << Dumpable(ApiList(val1)) |
| 296 | << " and " << Dumpable(ApiList(val2)); |
| 297 | UNREACHABLE(); |
| 298 | } |
| 299 | } |
| 300 | |
| 301 | const ApiList& operator|=(const ApiList& other) { |
| 302 | (*this) = (*this) | other; |
| 303 | return *this; |
| 304 | } |
| 305 | |
| 306 | // Returns true if all flags set in `other` are also set in `this`. |
| 307 | bool Contains(const ApiList& other) const { |
| 308 | return ((other.GetValue() == Value::kInvalid) || (GetValue() == other.GetValue())) && |
| 309 | helper::MatchesBitMask(other.GetDomainApis(), GetDomainApis()); |
| 310 | } |
| 311 | |
| 312 | // Returns true whether the configuration is valid for runtime use. |
| 313 | bool IsValid() const { return GetValue() != Value::kInvalid; } |
| 314 | |
| 315 | // Returns true when no ApiList is specified and no domain_api flags either. |
| 316 | bool IsEmpty() const { return (GetValue() == Value::kInvalid) && (GetDomainApis() == 0); } |
| 317 | |
Andrei Onea | fc12a6c | 2020-07-29 19:52:34 +0100 | [diff] [blame] | 318 | // Returns true if the ApiList is on blocklist. |
| 319 | bool IsBlocked() const { |
| 320 | return GetValue() == Value::kBlocked; |
Artur Satayev | 267366c | 2019-10-31 14:59:26 +0000 | [diff] [blame] | 321 | } |
| 322 | |
Nicolas Geoffray | 2464218 | 2022-02-14 14:40:51 +0000 | [diff] [blame] | 323 | bool IsSdkApi() const { |
| 324 | return GetValue() == Value::kSdk; |
| 325 | } |
| 326 | |
Artur Satayev | 267366c | 2019-10-31 14:59:26 +0000 | [diff] [blame] | 327 | // Returns true if the ApiList is a test API. |
| 328 | bool IsTestApi() const { |
| 329 | return helper::MatchesBitMask(helper::ToBit(DomainApi::kTestApi), dex_flags_); |
| 330 | } |
| 331 | |
David Brazdil | 90faceb | 2018-12-14 14:36:15 +0000 | [diff] [blame] | 332 | // Returns the maximum target SDK version allowed to access this ApiList. |
David Brazdil | dcfa89b | 2018-10-31 11:04:10 +0000 | [diff] [blame] | 333 | SdkVersion GetMaxAllowedSdkVersion() const { return kMaxSdkVersions[GetIntValue()]; } |
| 334 | |
David Brazdil | 90faceb | 2018-12-14 14:36:15 +0000 | [diff] [blame] | 335 | void Dump(std::ostream& os) const { |
| 336 | bool is_first = true; |
| 337 | |
Artur Satayev | 39c399a | 2019-09-13 16:09:09 +0100 | [diff] [blame] | 338 | if (IsEmpty()) { |
| 339 | os << "invalid"; |
| 340 | return; |
| 341 | } |
| 342 | |
David Brazdil | 90faceb | 2018-12-14 14:36:15 +0000 | [diff] [blame] | 343 | if (GetValue() != Value::kInvalid) { |
| 344 | os << kValueNames[GetIntValue()]; |
| 345 | is_first = false; |
| 346 | } |
| 347 | |
| 348 | const uint32_t domain_apis = GetDomainApis(); |
Mariia Feofanova | 4945b29 | 2019-09-04 17:49:42 +0100 | [diff] [blame] | 349 | for (uint32_t i = 0; i < kDomainApiCount; i++) { |
| 350 | if (helper::MatchesBitMask(helper::ToBit(helper::GetEnumAt<DomainApi>(i)), domain_apis)) { |
David Brazdil | 90faceb | 2018-12-14 14:36:15 +0000 | [diff] [blame] | 351 | if (is_first) { |
| 352 | is_first = false; |
| 353 | } else { |
| 354 | os << ","; |
| 355 | } |
Mariia Feofanova | 4945b29 | 2019-09-04 17:49:42 +0100 | [diff] [blame] | 356 | os << kDomainApiNames[i]; |
David Brazdil | 90faceb | 2018-12-14 14:36:15 +0000 | [diff] [blame] | 357 | } |
| 358 | } |
| 359 | |
| 360 | DCHECK_EQ(IsEmpty(), is_first); |
| 361 | } |
| 362 | |
Artur Satayev | 39c399a | 2019-09-13 16:09:09 +0100 | [diff] [blame] | 363 | // Number of valid enum values in Value. |
David Brazdil | 90faceb | 2018-12-14 14:36:15 +0000 | [diff] [blame] | 364 | static constexpr uint32_t kValueCount = helper::NumValues<Value>(); |
Artur Satayev | 39c399a | 2019-09-13 16:09:09 +0100 | [diff] [blame] | 365 | // Number of valid enum values in DomainApi. |
David Brazdil | 90faceb | 2018-12-14 14:36:15 +0000 | [diff] [blame] | 366 | static constexpr uint32_t kDomainApiCount = helper::NumValues<DomainApi>(); |
Artur Satayev | 39c399a | 2019-09-13 16:09:09 +0100 | [diff] [blame] | 367 | // Total number of possible enum values, including invalid, in Value. |
| 368 | static constexpr uint32_t kValueSize = (1u << kValueBitSize) + 1; |
Mariia Feofanova | 4945b29 | 2019-09-04 17:49:42 +0100 | [diff] [blame] | 369 | |
| 370 | // Check min and max values are calculated correctly. |
| 371 | static_assert(Value::kMin == helper::GetEnumAt<Value>(0)); |
| 372 | static_assert(Value::kMax == helper::GetEnumAt<Value>(kValueCount - 1)); |
| 373 | |
| 374 | static_assert(DomainApi::kMin == helper::GetEnumAt<DomainApi>(0)); |
| 375 | static_assert(DomainApi::kMax == helper::GetEnumAt<DomainApi>(kDomainApiCount - 1)); |
David Brazdil | dcfa89b | 2018-10-31 11:04:10 +0000 | [diff] [blame] | 376 | }; |
| 377 | |
| 378 | inline std::ostream& operator<<(std::ostream& os, ApiList value) { |
David Brazdil | 90faceb | 2018-12-14 14:36:15 +0000 | [diff] [blame] | 379 | value.Dump(os); |
David Brazdil | dcfa89b | 2018-10-31 11:04:10 +0000 | [diff] [blame] | 380 | return os; |
| 381 | } |
| 382 | |
David Brazdil | dcfa89b | 2018-10-31 11:04:10 +0000 | [diff] [blame] | 383 | } // namespace hiddenapi |
| 384 | } // namespace art |
| 385 | |
| 386 | |
| 387 | #endif // ART_LIBARTBASE_BASE_HIDDENAPI_FLAGS_H_ |