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 | |
| 22 | #include "android-base/logging.h" |
| 23 | |
| 24 | namespace art { |
| 25 | namespace hiddenapi { |
| 26 | |
| 27 | /* |
| 28 | * This class represents the information whether a field/method is in |
| 29 | * public API (whitelist) or if it isn't, apps targeting which SDK |
| 30 | * versions are allowed to access it. |
| 31 | */ |
| 32 | class ApiList { |
| 33 | private: |
| 34 | using IntValueType = uint32_t; |
| 35 | |
| 36 | enum class Value : IntValueType { |
| 37 | // Values independent of target SDK version of app |
| 38 | kWhitelist = 0, |
| 39 | kGreylist = 1, |
| 40 | kBlacklist = 2, |
| 41 | |
| 42 | // Values dependent on target SDK version of app. Put these last as |
| 43 | // their list will be extended in future releases. |
| 44 | // The max release code implicitly includes all maintenance releases, |
| 45 | // e.g. GreylistMaxO is accessible to targetSdkVersion <= 27 (O_MR1). |
| 46 | kGreylistMaxO = 3, |
David Brazdil | 80d1628 | 2018-11-01 09:55:09 +0000 | [diff] [blame^] | 47 | kGreylistMaxP = 4, |
David Brazdil | dcfa89b | 2018-10-31 11:04:10 +0000 | [diff] [blame] | 48 | |
| 49 | // Special values |
| 50 | kInvalid = static_cast<uint32_t>(-1), |
| 51 | kMinValue = kWhitelist, |
David Brazdil | 80d1628 | 2018-11-01 09:55:09 +0000 | [diff] [blame^] | 52 | kMaxValue = kGreylistMaxP, |
David Brazdil | dcfa89b | 2018-10-31 11:04:10 +0000 | [diff] [blame] | 53 | }; |
| 54 | |
| 55 | static constexpr const char* kNames[] = { |
| 56 | "whitelist", |
| 57 | "greylist", |
| 58 | "blacklist", |
| 59 | "greylist-max-o", |
David Brazdil | 80d1628 | 2018-11-01 09:55:09 +0000 | [diff] [blame^] | 60 | "greylist-max-p", |
David Brazdil | dcfa89b | 2018-10-31 11:04:10 +0000 | [diff] [blame] | 61 | }; |
| 62 | |
David Brazdil | 91690d3 | 2018-11-04 18:07:23 +0000 | [diff] [blame] | 63 | static constexpr const char* kInvalidName = "invalid"; |
| 64 | |
David Brazdil | dcfa89b | 2018-10-31 11:04:10 +0000 | [diff] [blame] | 65 | static constexpr SdkVersion kMaxSdkVersions[] { |
| 66 | /* whitelist */ SdkVersion::kMax, |
| 67 | /* greylist */ SdkVersion::kMax, |
| 68 | /* blacklist */ SdkVersion::kMin, |
| 69 | /* greylist-max-o */ SdkVersion::kO_MR1, |
David Brazdil | 80d1628 | 2018-11-01 09:55:09 +0000 | [diff] [blame^] | 70 | /* greylist-max-p */ SdkVersion::kP, |
David Brazdil | dcfa89b | 2018-10-31 11:04:10 +0000 | [diff] [blame] | 71 | }; |
| 72 | |
| 73 | static ApiList MinValue() { return ApiList(Value::kMinValue); } |
| 74 | static ApiList MaxValue() { return ApiList(Value::kMaxValue); } |
| 75 | |
| 76 | explicit ApiList(Value value) : value_(value) {} |
| 77 | |
David Brazdil | 91690d3 | 2018-11-04 18:07:23 +0000 | [diff] [blame] | 78 | Value value_; |
David Brazdil | dcfa89b | 2018-10-31 11:04:10 +0000 | [diff] [blame] | 79 | |
| 80 | public: |
| 81 | static ApiList Whitelist() { return ApiList(Value::kWhitelist); } |
| 82 | static ApiList Greylist() { return ApiList(Value::kGreylist); } |
| 83 | static ApiList Blacklist() { return ApiList(Value::kBlacklist); } |
| 84 | static ApiList GreylistMaxO() { return ApiList(Value::kGreylistMaxO); } |
David Brazdil | 80d1628 | 2018-11-01 09:55:09 +0000 | [diff] [blame^] | 85 | static ApiList GreylistMaxP() { return ApiList(Value::kGreylistMaxP); } |
David Brazdil | dcfa89b | 2018-10-31 11:04:10 +0000 | [diff] [blame] | 86 | static ApiList Invalid() { return ApiList(Value::kInvalid); } |
| 87 | |
| 88 | // Decodes ApiList from dex hiddenapi flags. |
| 89 | static ApiList FromDexFlags(uint32_t dex_flags) { |
| 90 | if (MinValue().GetIntValue() <= dex_flags && dex_flags <= MaxValue().GetIntValue()) { |
| 91 | return ApiList(static_cast<Value>(dex_flags)); |
| 92 | } |
| 93 | return Invalid(); |
| 94 | } |
| 95 | |
David Brazdil | 91690d3 | 2018-11-04 18:07:23 +0000 | [diff] [blame] | 96 | // Decodes ApiList from its integer value. |
| 97 | static ApiList FromIntValue(IntValueType int_value) { |
| 98 | if (MinValue().GetIntValue() <= int_value && int_value <= MaxValue().GetIntValue()) { |
| 99 | return ApiList(static_cast<Value>(int_value)); |
| 100 | } |
| 101 | return Invalid(); |
| 102 | } |
| 103 | |
David Brazdil | dcfa89b | 2018-10-31 11:04:10 +0000 | [diff] [blame] | 104 | // Returns the ApiList with a given name. |
| 105 | static ApiList FromName(const std::string& str) { |
| 106 | for (IntValueType i = MinValue().GetIntValue(); i <= MaxValue().GetIntValue(); i++) { |
| 107 | ApiList current = ApiList(static_cast<Value>(i)); |
| 108 | if (str == current.GetName()) { |
| 109 | return current; |
| 110 | } |
| 111 | } |
| 112 | return Invalid(); |
| 113 | } |
| 114 | |
| 115 | bool operator==(const ApiList other) const { return value_ == other.value_; } |
| 116 | bool operator!=(const ApiList other) const { return !(*this == other); } |
| 117 | |
| 118 | bool IsValid() const { return *this != Invalid(); } |
| 119 | |
| 120 | IntValueType GetIntValue() const { |
| 121 | DCHECK(IsValid()); |
| 122 | return static_cast<IntValueType>(value_); |
| 123 | } |
| 124 | |
David Brazdil | 91690d3 | 2018-11-04 18:07:23 +0000 | [diff] [blame] | 125 | const char* GetName() const { return IsValid() ? kNames[GetIntValue()]: kInvalidName; } |
David Brazdil | dcfa89b | 2018-10-31 11:04:10 +0000 | [diff] [blame] | 126 | |
| 127 | SdkVersion GetMaxAllowedSdkVersion() const { return kMaxSdkVersions[GetIntValue()]; } |
| 128 | |
| 129 | static constexpr size_t kValueCount = static_cast<size_t>(Value::kMaxValue) + 1; |
| 130 | }; |
| 131 | |
| 132 | inline std::ostream& operator<<(std::ostream& os, ApiList value) { |
| 133 | os << value.GetName(); |
| 134 | return os; |
| 135 | } |
| 136 | |
| 137 | inline bool AreValidDexFlags(uint32_t dex_flags) { |
| 138 | return ApiList::FromDexFlags(dex_flags).IsValid(); |
| 139 | } |
| 140 | |
| 141 | } // namespace hiddenapi |
| 142 | } // namespace art |
| 143 | |
| 144 | |
| 145 | #endif // ART_LIBARTBASE_BASE_HIDDENAPI_FLAGS_H_ |