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 "dex/hidden_api_access_flags.h"
21 #include "dex/method_reference.h"
22 
23 #include <ostream>
24 #include <set>
25 #include <string>
26 
27 namespace art {
28 
29 class DexFile;
30 
31 /**
32  * Helper class for logging if a method/field is in a hidden API list.
33  */
34 class HiddenApi {
35  public:
HiddenApi(const char * blacklist,const char * dark_greylist,const char * light_greylist)36   HiddenApi(const char* blacklist, const char* dark_greylist, const char* light_greylist) {
37     FillList(light_greylist, light_greylist_);
38     FillList(dark_greylist, dark_greylist_);
39     FillList(blacklist, blacklist_);
40   }
41 
GetApiList(const std::string & name)42   HiddenApiAccessFlags::ApiList GetApiList(const std::string& name) const {
43     if (IsInList(name, blacklist_)) {
44       return HiddenApiAccessFlags::kBlacklist;
45     } else if (IsInList(name, dark_greylist_)) {
46       return HiddenApiAccessFlags::kDarkGreylist;
47     } else if (IsInList(name, light_greylist_)) {
48       return HiddenApiAccessFlags::kLightGreylist;
49     } else {
50       return HiddenApiAccessFlags::kWhitelist;
51     }
52   }
53 
IsInRestrictionList(const std::string & name)54   bool IsInRestrictionList(const std::string& name) const {
55     return GetApiList(name) != HiddenApiAccessFlags::kWhitelist;
56   }
57 
58   static std::string GetApiMethodName(const DexFile& dex_file, uint32_t method_index);
59 
60   static std::string GetApiFieldName(const DexFile& dex_file, uint32_t field_index);
61 
GetApiMethodName(MethodReference ref)62   static std::string GetApiMethodName(MethodReference ref) {
63     return HiddenApi::GetApiMethodName(*ref.dex_file, ref.index);
64   }
65 
ToInternalName(const std::string & str)66   static std::string ToInternalName(const std::string& str) {
67     std::string val = str;
68     std::replace(val.begin(), val.end(), '.', '/');
69     return "L" + val + ";";
70   }
71 
72  private:
IsInList(const std::string & name,const std::set<std::string> & list)73   static bool IsInList(const std::string& name, const std::set<std::string>& list) {
74     return list.find(name) != list.end();
75   }
76 
77   static void FillList(const char* filename, std::set<std::string>& entries);
78 
79   std::set<std::string> blacklist_;
80   std::set<std::string> light_greylist_;
81   std::set<std::string> dark_greylist_;
82 };
83 
84 struct HiddenApiStats {
85   uint32_t count = 0;
86   uint32_t reflection_count = 0;
87   uint32_t linking_count = 0;
88   uint32_t api_counts[4] = { 0, 0, 0, 0 };
89 };
90 
91 }  // namespace art
92 
93 #endif  // ART_TOOLS_VERIDEX_HIDDEN_API_H_
94