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 
22 #include "android-base/strings.h"
23 #include "dex/dex_file-inl.h"
24 
25 namespace art {
26 
HiddenApi(const char * filename,const ApiListFilter & api_list_filter)27 HiddenApi::HiddenApi(const char* filename, const ApiListFilter& api_list_filter)
28     : api_list_filter_(api_list_filter) {
29   CHECK(filename != nullptr);
30 
31   std::ifstream in(filename);
32   bool errors = false;
33   for (std::string str; std::getline(in, str);) {
34     std::vector<std::string> values = android::base::Split(str, ",");
35     const std::string& signature = values[0];
36 
37     hiddenapi::ApiList membership;
38     bool success = hiddenapi::ApiList::FromNames(values.begin() + 1, values.end(), &membership);
39     if (!success) {
40       LOG(ERROR) << "Unknown ApiList flag: " << str;
41       errors = true;
42       continue;
43     } else if (!membership.IsValid()) {
44       LOG(ERROR) << "Invalid ApiList: " << membership;
45       errors = true;
46       continue;
47     }
48 
49     AddSignatureToApiList(signature, membership);
50     size_t pos = signature.find("->");
51     if (pos != std::string::npos) {
52       // Add the class name.
53       AddSignatureToApiList(signature.substr(0, pos), membership);
54       pos = signature.find('(');
55       if (pos != std::string::npos) {
56         // Add the class->method name (so stripping the signature).
57         AddSignatureToApiList(signature.substr(0, pos), membership);
58       }
59       pos = signature.find(':');
60       if (pos != std::string::npos) {
61         // Add the class->field name (so stripping the type).
62         AddSignatureToApiList(signature.substr(0, pos), membership);
63       }
64     }
65   }
66   CHECK(!errors) << "Errors encountered while parsing file " << filename;
67 }
68 
AddSignatureToApiList(const std::string & signature,hiddenapi::ApiList membership)69 void HiddenApi::AddSignatureToApiList(const std::string& signature, hiddenapi::ApiList membership) {
70   auto it = api_list_.find(signature);
71   if (it == api_list_.end()) {
72     // Does not exist yet. Add it to list.
73     api_list_.emplace(signature, membership);
74   } else if (membership.GetMaxAllowedSdkVersion() < it->second.GetMaxAllowedSdkVersion()) {
75     // Already exist but `membership` is more restrictive.
76     it->second = membership;
77   } else {
78     // Already exists and `membership` is equally or less restrictive.
79   }
80 }
81 
GetApiMethodName(const DexFile & dex_file,uint32_t method_index)82 std::string HiddenApi::GetApiMethodName(const DexFile& dex_file, uint32_t method_index) {
83   std::stringstream ss;
84   const dex::MethodId& method_id = dex_file.GetMethodId(method_index);
85   ss << dex_file.GetTypeDescriptorView(method_id.class_idx_)
86      << "->"
87      << dex_file.GetMethodName(method_id)
88      << dex_file.GetMethodSignature(method_id).ToString();
89   return ss.str();
90 }
91 
GetApiFieldName(const DexFile & dex_file,uint32_t field_index)92 std::string HiddenApi::GetApiFieldName(const DexFile& dex_file, uint32_t field_index) {
93   std::stringstream ss;
94   const dex::FieldId& field_id = dex_file.GetFieldId(field_index);
95   ss << dex_file.GetTypeDescriptorView(field_id.class_idx_)
96      << "->"
97      << dex_file.GetFieldName(field_id)
98      << ":"
99      << dex_file.GetFieldTypeDescriptor(field_id);
100   return ss.str();
101 }
102 
103 }  // namespace art
104