Nicolas Geoffray | 534a0a1 | 2018-03-24 20:02:25 +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_TOOLS_VERIDEX_HIDDEN_API_H_ |
| 18 | #define ART_TOOLS_VERIDEX_HIDDEN_API_H_ |
| 19 | |
Artur Satayev | 7acb846 | 2019-09-11 12:16:12 +0100 | [diff] [blame] | 20 | #include "api_list_filter.h" |
| 21 | |
David Brazdil | dcfa89b | 2018-10-31 11:04:10 +0000 | [diff] [blame] | 22 | #include "base/hiddenapi_flags.h" |
Nicolas Geoffray | 2ebff05 | 2018-04-04 22:32:03 +0100 | [diff] [blame] | 23 | #include "dex/method_reference.h" |
Nicolas Geoffray | 11ed027 | 2018-03-28 18:18:48 +0100 | [diff] [blame] | 24 | |
David Brazdil | 91690d3 | 2018-11-04 18:07:23 +0000 | [diff] [blame] | 25 | #include <map> |
Nicolas Geoffray | 11ed027 | 2018-03-28 18:18:48 +0100 | [diff] [blame] | 26 | #include <ostream> |
Nicolas Geoffray | 534a0a1 | 2018-03-24 20:02:25 +0000 | [diff] [blame] | 27 | #include <string> |
| 28 | |
| 29 | namespace art { |
| 30 | |
| 31 | class DexFile; |
| 32 | |
Artur Satayev | 39c399a | 2019-09-13 16:09:09 +0100 | [diff] [blame] | 33 | enum class SignatureSource { |
| 34 | UNKNOWN, |
| 35 | BOOT, |
| 36 | APP, |
| 37 | }; |
| 38 | |
Nicolas Geoffray | 534a0a1 | 2018-03-24 20:02:25 +0000 | [diff] [blame] | 39 | /** |
| 40 | * Helper class for logging if a method/field is in a hidden API list. |
| 41 | */ |
| 42 | class HiddenApi { |
| 43 | public: |
Artur Satayev | 7acb846 | 2019-09-11 12:16:12 +0100 | [diff] [blame] | 44 | HiddenApi(const char* flags_file, const ApiListFilter& api_list_filter); |
Nicolas Geoffray | 534a0a1 | 2018-03-24 20:02:25 +0000 | [diff] [blame] | 45 | |
David Brazdil | 47cd272 | 2018-10-23 12:50:02 +0100 | [diff] [blame] | 46 | hiddenapi::ApiList GetApiList(const std::string& name) const { |
David Brazdil | 91690d3 | 2018-11-04 18:07:23 +0000 | [diff] [blame] | 47 | auto it = api_list_.find(name); |
David Brazdil | 90faceb | 2018-12-14 14:36:15 +0000 | [diff] [blame] | 48 | return (it == api_list_.end()) ? hiddenapi::ApiList() : it->second; |
Nicolas Geoffray | 11ed027 | 2018-03-28 18:18:48 +0100 | [diff] [blame] | 49 | } |
| 50 | |
Artur Satayev | 39c399a | 2019-09-13 16:09:09 +0100 | [diff] [blame] | 51 | bool ShouldReport(const std::string& signature) const { |
| 52 | return api_list_filter_.Matches(GetApiList(signature)); |
| 53 | } |
| 54 | |
| 55 | void AddSignatureSource(const std::string &signature, SignatureSource source) { |
| 56 | const auto type = GetApiClassName(signature); |
| 57 | auto it = source_.find(type); |
| 58 | if (it == source_.end() || it->second == SignatureSource::UNKNOWN) { |
| 59 | source_[type] = source; |
| 60 | } else if (it->second != source) { |
| 61 | LOG(WARNING) << type << "is present both in boot and in app."; |
| 62 | if (source == SignatureSource::BOOT) { |
| 63 | // Runtime resolves to boot type, so it takes precedence. |
| 64 | it->second = source; |
| 65 | } |
| 66 | } else { |
| 67 | // Already exists with the same source. |
| 68 | } |
| 69 | } |
| 70 | |
| 71 | SignatureSource GetSignatureSource(const std::string& signature) const { |
| 72 | auto it = source_.find(GetApiClassName(signature)); |
| 73 | return (it == source_.end()) ? SignatureSource::UNKNOWN : it->second; |
| 74 | } |
| 75 | |
| 76 | bool IsInBoot(const std::string& signature) const { |
| 77 | return SignatureSource::BOOT == GetSignatureSource(signature); |
Nicolas Geoffray | 534a0a1 | 2018-03-24 20:02:25 +0000 | [diff] [blame] | 78 | } |
| 79 | |
| 80 | static std::string GetApiMethodName(const DexFile& dex_file, uint32_t method_index); |
| 81 | |
| 82 | static std::string GetApiFieldName(const DexFile& dex_file, uint32_t field_index); |
| 83 | |
Nicolas Geoffray | 2ebff05 | 2018-04-04 22:32:03 +0100 | [diff] [blame] | 84 | static std::string GetApiMethodName(MethodReference ref) { |
| 85 | return HiddenApi::GetApiMethodName(*ref.dex_file, ref.index); |
| 86 | } |
| 87 | |
Nicolas Geoffray | 4f6e523 | 2018-04-18 12:54:04 +0100 | [diff] [blame] | 88 | static std::string ToInternalName(const std::string& str) { |
| 89 | std::string val = str; |
| 90 | std::replace(val.begin(), val.end(), '.', '/'); |
| 91 | return "L" + val + ";"; |
| 92 | } |
| 93 | |
Nicolas Geoffray | 534a0a1 | 2018-03-24 20:02:25 +0000 | [diff] [blame] | 94 | private: |
David Brazdil | 91690d3 | 2018-11-04 18:07:23 +0000 | [diff] [blame] | 95 | void AddSignatureToApiList(const std::string& signature, hiddenapi::ApiList membership); |
Nicolas Geoffray | 534a0a1 | 2018-03-24 20:02:25 +0000 | [diff] [blame] | 96 | |
Artur Satayev | 39c399a | 2019-09-13 16:09:09 +0100 | [diff] [blame] | 97 | static std::string GetApiClassName(const std::string& signature) { |
| 98 | size_t pos = signature.find("->"); |
| 99 | if (pos != std::string::npos) { |
| 100 | return signature.substr(0, pos); |
| 101 | } |
| 102 | return signature; |
| 103 | } |
| 104 | |
| 105 | const ApiListFilter& api_list_filter_; |
David Brazdil | 91690d3 | 2018-11-04 18:07:23 +0000 | [diff] [blame] | 106 | std::map<std::string, hiddenapi::ApiList> api_list_; |
Artur Satayev | 39c399a | 2019-09-13 16:09:09 +0100 | [diff] [blame] | 107 | std::map<std::string, SignatureSource> source_; |
Nicolas Geoffray | 534a0a1 | 2018-03-24 20:02:25 +0000 | [diff] [blame] | 108 | }; |
| 109 | |
Nicolas Geoffray | 2ebff05 | 2018-04-04 22:32:03 +0100 | [diff] [blame] | 110 | struct HiddenApiStats { |
| 111 | uint32_t count = 0; |
| 112 | uint32_t reflection_count = 0; |
| 113 | uint32_t linking_count = 0; |
Artur Satayev | 39c399a | 2019-09-13 16:09:09 +0100 | [diff] [blame] | 114 | // Ensure enough space for kInvalid as well, and initialize all to zero |
| 115 | uint32_t api_counts[hiddenapi::ApiList::kValueSize] = {}; |
Nicolas Geoffray | 2ebff05 | 2018-04-04 22:32:03 +0100 | [diff] [blame] | 116 | }; |
| 117 | |
Artur Satayev | 39c399a | 2019-09-13 16:09:09 +0100 | [diff] [blame] | 118 | |
Nicolas Geoffray | 534a0a1 | 2018-03-24 20:02:25 +0000 | [diff] [blame] | 119 | } // namespace art |
| 120 | |
| 121 | #endif // ART_TOOLS_VERIDEX_HIDDEN_API_H_ |