blob: 2af7b50a73887b6bec8a29fa073451de77025b25 [file] [log] [blame]
Nicolas Geoffray534a0a12018-03-24 20:02:25 +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#include "hidden_api.h"
18
19#include <fstream>
20#include <sstream>
21
David Brazdil91690d32018-11-04 18:07:23 +000022#include "android-base/strings.h"
Nicolas Geoffray534a0a12018-03-24 20:02:25 +000023#include "dex/dex_file-inl.h"
24
25namespace art {
26
David Brazdil91690d32018-11-04 18:07:23 +000027HiddenApi::HiddenApi(const char* filename, bool sdk_uses_only) {
28 CHECK(filename != nullptr);
29
30 std::ifstream in(filename);
31 for (std::string str; std::getline(in, str);) {
32 std::vector<std::string> values = android::base::Split(str, ",");
33 CHECK_EQ(values.size(), 2u) << "Currently only signature and one flag are supported";
34
35 const std::string& signature = values[0];
36 const std::string& flag_str = values[1];
37
38 hiddenapi::ApiList membership = hiddenapi::ApiList::FromName(flag_str);
39 CHECK(membership.IsValid()) << "Unknown ApiList name: " << flag_str;
40
41 if (sdk_uses_only != (membership == hiddenapi::ApiList::Whitelist())) {
42 // Either we want only SDK uses and this is not a whitelist entry,
43 // or we want only non-SDK uses and this is a whitelist entry.
44 continue;
45 }
46
47 AddSignatureToApiList(signature, membership);
48 size_t pos = signature.find("->");
49 if (pos != std::string::npos) {
50 // Add the class name.
51 AddSignatureToApiList(signature.substr(0, pos), membership);
52 pos = signature.find('(');
53 if (pos != std::string::npos) {
54 // Add the class->method name (so stripping the signature).
55 AddSignatureToApiList(signature.substr(0, pos), membership);
56 }
57 pos = signature.find(':');
58 if (pos != std::string::npos) {
59 // Add the class->field name (so stripping the type).
60 AddSignatureToApiList(signature.substr(0, pos), membership);
61 }
62 }
63 }
64}
65
66void HiddenApi::AddSignatureToApiList(const std::string& signature, hiddenapi::ApiList membership) {
67 auto it = api_list_.find(signature);
68 if (it == api_list_.end()) {
69 // Does not exist yet. Add it to list.
70 api_list_.emplace(signature, membership);
71 } else if (membership.GetMaxAllowedSdkVersion() < it->second.GetMaxAllowedSdkVersion()) {
72 // Already exist but `membership` is more restrictive.
73 it->second = membership;
74 } else {
75 // Already exists and `membership` is equally or less restrictive.
76 }
77}
78
Nicolas Geoffray534a0a12018-03-24 20:02:25 +000079std::string HiddenApi::GetApiMethodName(const DexFile& dex_file, uint32_t method_index) {
80 std::stringstream ss;
Andreas Gampe3f1dcd32018-12-28 09:39:56 -080081 const dex::MethodId& method_id = dex_file.GetMethodId(method_index);
Nicolas Geoffray534a0a12018-03-24 20:02:25 +000082 ss << dex_file.StringByTypeIdx(method_id.class_idx_)
83 << "->"
84 << dex_file.GetMethodName(method_id)
85 << dex_file.GetMethodSignature(method_id).ToString();
86 return ss.str();
87}
88
89std::string HiddenApi::GetApiFieldName(const DexFile& dex_file, uint32_t field_index) {
90 std::stringstream ss;
Andreas Gampe3f1dcd32018-12-28 09:39:56 -080091 const dex::FieldId& field_id = dex_file.GetFieldId(field_index);
Nicolas Geoffray534a0a12018-03-24 20:02:25 +000092 ss << dex_file.StringByTypeIdx(field_id.class_idx_)
93 << "->"
94 << dex_file.GetFieldName(field_id)
95 << ":"
96 << dex_file.GetFieldTypeDescriptor(field_id);
97 return ss.str();
98}
99
Nicolas Geoffray534a0a12018-03-24 20:02:25 +0000100} // namespace art