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 
20 #include "api_list_filter.h"
21 
22 #include "base/hiddenapi_flags.h"
23 #include "dex/method_reference.h"
24 
25 #include <map>
26 #include <ostream>
27 #include <string>
28 
29 namespace art {
30 
31 class DexFile;
32 
33 enum class SignatureSource {
34   UNKNOWN,
35   BOOT,
36   APP,
37 };
38 
39 /**
40  * Helper class for logging if a method/field is in a hidden API list.
41  */
42 class HiddenApi {
43  public:
44   HiddenApi(const char* flags_file, const ApiListFilter& api_list_filter);
45 
GetApiList(const std::string & name)46   hiddenapi::ApiList GetApiList(const std::string& name) const {
47     auto it = api_list_.find(name);
48     return (it == api_list_.end()) ? hiddenapi::ApiList() : it->second;
49   }
50 
ShouldReport(const std::string & signature)51   bool ShouldReport(const std::string& signature) const {
52     return api_list_filter_.Matches(GetApiList(signature));
53   }
54 
AddSignatureSource(const std::string & signature,SignatureSource source)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 
GetSignatureSource(const std::string & signature)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 
IsInBoot(const std::string & signature)76   bool IsInBoot(const std::string& signature) const {
77     return SignatureSource::BOOT == GetSignatureSource(signature);
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 
GetApiMethodName(MethodReference ref)84   static std::string GetApiMethodName(MethodReference ref) {
85     return HiddenApi::GetApiMethodName(*ref.dex_file, ref.index);
86   }
87 
ToInternalName(const std::string & str)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 
94  private:
95   void AddSignatureToApiList(const std::string& signature, hiddenapi::ApiList membership);
96 
GetApiClassName(const std::string & signature)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_;
106   std::map<std::string, hiddenapi::ApiList> api_list_;
107   std::map<std::string, SignatureSource> source_;
108 };
109 
110 struct HiddenApiStats {
111   uint32_t count = 0;
112   uint32_t reflection_count = 0;
113   uint32_t linking_count = 0;
114   // Ensure enough space for kInvalid as well, and initialize all to zero
115   uint32_t api_counts[hiddenapi::ApiList::kValueSize] = {};
116 };
117 
118 
119 }  // namespace art
120 
121 #endif  // ART_TOOLS_VERIDEX_HIDDEN_API_H_
122