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 | #include "hidden_api.h" |
| 18 | |
| 19 | #include <fstream> |
| 20 | #include <sstream> |
| 21 | |
David Brazdil | 91690d3 | 2018-11-04 18:07:23 +0000 | [diff] [blame] | 22 | #include "android-base/strings.h" |
Nicolas Geoffray | 534a0a1 | 2018-03-24 20:02:25 +0000 | [diff] [blame] | 23 | #include "dex/dex_file-inl.h" |
| 24 | |
| 25 | namespace art { |
| 26 | |
Artur Satayev | 39c399a | 2019-09-13 16:09:09 +0100 | [diff] [blame] | 27 | HiddenApi::HiddenApi(const char* filename, const ApiListFilter& api_list_filter) |
| 28 | : api_list_filter_(api_list_filter) { |
David Brazdil | 91690d3 | 2018-11-04 18:07:23 +0000 | [diff] [blame] | 29 | CHECK(filename != nullptr); |
| 30 | |
| 31 | std::ifstream in(filename); |
| 32 | for (std::string str; std::getline(in, str);) { |
| 33 | std::vector<std::string> values = android::base::Split(str, ","); |
David Brazdil | 91690d3 | 2018-11-04 18:07:23 +0000 | [diff] [blame] | 34 | const std::string& signature = values[0]; |
David Brazdil | 91690d3 | 2018-11-04 18:07:23 +0000 | [diff] [blame] | 35 | |
David Brazdil | 90faceb | 2018-12-14 14:36:15 +0000 | [diff] [blame] | 36 | hiddenapi::ApiList membership; |
| 37 | bool success = hiddenapi::ApiList::FromNames(values.begin() + 1, values.end(), &membership); |
Paul Duffin | c03eafe | 2022-12-21 14:03:57 +0000 | [diff] [blame^] | 38 | CHECK(success) << "Unknown ApiList flag: " << str << " from file " |
| 39 | << filename; |
David Brazdil | 90faceb | 2018-12-14 14:36:15 +0000 | [diff] [blame] | 40 | CHECK(membership.IsValid()) << "Invalid ApiList: " << membership; |
David Brazdil | 91690d3 | 2018-11-04 18:07:23 +0000 | [diff] [blame] | 41 | |
David Brazdil | 91690d3 | 2018-11-04 18:07:23 +0000 | [diff] [blame] | 42 | AddSignatureToApiList(signature, membership); |
| 43 | size_t pos = signature.find("->"); |
| 44 | if (pos != std::string::npos) { |
| 45 | // Add the class name. |
| 46 | AddSignatureToApiList(signature.substr(0, pos), membership); |
| 47 | pos = signature.find('('); |
| 48 | if (pos != std::string::npos) { |
| 49 | // Add the class->method name (so stripping the signature). |
| 50 | AddSignatureToApiList(signature.substr(0, pos), membership); |
| 51 | } |
| 52 | pos = signature.find(':'); |
| 53 | if (pos != std::string::npos) { |
| 54 | // Add the class->field name (so stripping the type). |
| 55 | AddSignatureToApiList(signature.substr(0, pos), membership); |
| 56 | } |
| 57 | } |
| 58 | } |
| 59 | } |
| 60 | |
| 61 | void HiddenApi::AddSignatureToApiList(const std::string& signature, hiddenapi::ApiList membership) { |
| 62 | auto it = api_list_.find(signature); |
| 63 | if (it == api_list_.end()) { |
| 64 | // Does not exist yet. Add it to list. |
| 65 | api_list_.emplace(signature, membership); |
| 66 | } else if (membership.GetMaxAllowedSdkVersion() < it->second.GetMaxAllowedSdkVersion()) { |
| 67 | // Already exist but `membership` is more restrictive. |
| 68 | it->second = membership; |
| 69 | } else { |
| 70 | // Already exists and `membership` is equally or less restrictive. |
| 71 | } |
| 72 | } |
| 73 | |
Nicolas Geoffray | 534a0a1 | 2018-03-24 20:02:25 +0000 | [diff] [blame] | 74 | std::string HiddenApi::GetApiMethodName(const DexFile& dex_file, uint32_t method_index) { |
| 75 | std::stringstream ss; |
Andreas Gampe | 3f1dcd3 | 2018-12-28 09:39:56 -0800 | [diff] [blame] | 76 | const dex::MethodId& method_id = dex_file.GetMethodId(method_index); |
Nicolas Geoffray | 534a0a1 | 2018-03-24 20:02:25 +0000 | [diff] [blame] | 77 | ss << dex_file.StringByTypeIdx(method_id.class_idx_) |
| 78 | << "->" |
| 79 | << dex_file.GetMethodName(method_id) |
| 80 | << dex_file.GetMethodSignature(method_id).ToString(); |
| 81 | return ss.str(); |
| 82 | } |
| 83 | |
| 84 | std::string HiddenApi::GetApiFieldName(const DexFile& dex_file, uint32_t field_index) { |
| 85 | std::stringstream ss; |
Andreas Gampe | 3f1dcd3 | 2018-12-28 09:39:56 -0800 | [diff] [blame] | 86 | const dex::FieldId& field_id = dex_file.GetFieldId(field_index); |
Nicolas Geoffray | 534a0a1 | 2018-03-24 20:02:25 +0000 | [diff] [blame] | 87 | ss << dex_file.StringByTypeIdx(field_id.class_idx_) |
| 88 | << "->" |
| 89 | << dex_file.GetFieldName(field_id) |
| 90 | << ":" |
| 91 | << dex_file.GetFieldTypeDescriptor(field_id); |
| 92 | return ss.str(); |
| 93 | } |
| 94 | |
Nicolas Geoffray | 534a0a1 | 2018-03-24 20:02:25 +0000 | [diff] [blame] | 95 | } // namespace art |