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,bool sdk_uses_only)27 HiddenApi::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     const std::string& signature = values[0];
34 
35     hiddenapi::ApiList membership;
36     bool success = hiddenapi::ApiList::FromNames(values.begin() + 1, values.end(), &membership);
37     CHECK(success) << "Unknown ApiList flag: " << str;
38     CHECK(membership.IsValid()) << "Invalid ApiList: " << membership;
39 
40     if (sdk_uses_only != membership.Contains(hiddenapi::ApiList::Whitelist())) {
41       // Either we want only SDK uses and this is not a whitelist entry,
42       // or we want only non-SDK uses and this is a whitelist entry.
43       continue;
44     }
45 
46     AddSignatureToApiList(signature, membership);
47     size_t pos = signature.find("->");
48     if (pos != std::string::npos) {
49       // Add the class name.
50       AddSignatureToApiList(signature.substr(0, pos), membership);
51       pos = signature.find('(');
52       if (pos != std::string::npos) {
53         // Add the class->method name (so stripping the signature).
54         AddSignatureToApiList(signature.substr(0, pos), membership);
55       }
56       pos = signature.find(':');
57       if (pos != std::string::npos) {
58         // Add the class->field name (so stripping the type).
59         AddSignatureToApiList(signature.substr(0, pos), membership);
60       }
61     }
62   }
63 }
64 
AddSignatureToApiList(const std::string & signature,hiddenapi::ApiList membership)65 void HiddenApi::AddSignatureToApiList(const std::string& signature, hiddenapi::ApiList membership) {
66   auto it = api_list_.find(signature);
67   if (it == api_list_.end()) {
68     // Does not exist yet. Add it to list.
69     api_list_.emplace(signature, membership);
70   } else if (membership.GetMaxAllowedSdkVersion() < it->second.GetMaxAllowedSdkVersion()) {
71     // Already exist but `membership` is more restrictive.
72     it->second = membership;
73   } else {
74     // Already exists and `membership` is equally or less restrictive.
75   }
76 }
77 
GetApiMethodName(const DexFile & dex_file,uint32_t method_index)78 std::string HiddenApi::GetApiMethodName(const DexFile& dex_file, uint32_t method_index) {
79   std::stringstream ss;
80   const dex::MethodId& method_id = dex_file.GetMethodId(method_index);
81   ss << dex_file.StringByTypeIdx(method_id.class_idx_)
82      << "->"
83      << dex_file.GetMethodName(method_id)
84      << dex_file.GetMethodSignature(method_id).ToString();
85   return ss.str();
86 }
87 
GetApiFieldName(const DexFile & dex_file,uint32_t field_index)88 std::string HiddenApi::GetApiFieldName(const DexFile& dex_file, uint32_t field_index) {
89   std::stringstream ss;
90   const dex::FieldId& field_id = dex_file.GetFieldId(field_index);
91   ss << dex_file.StringByTypeIdx(field_id.class_idx_)
92      << "->"
93      << dex_file.GetFieldName(field_id)
94      << ":"
95      << dex_file.GetFieldTypeDescriptor(field_id);
96   return ss.str();
97 }
98 
99 }  // namespace art
100