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_RESOLVER_H_
18 #define ART_TOOLS_VERIDEX_RESOLVER_H_
19 
20 #include "dex/dex_file.h"
21 #include "veridex.h"
22 
23 namespace art {
24 
25 class HiddenApi;
26 class VeridexResolver;
27 
28 /**
29  * Map from the start of a dex file (ie DexFile::Begin()), to
30  * its corresponding resolver.
31  */
32 using DexResolverMap = std::map<uintptr_t, VeridexResolver*>;
33 
34 class VeridexResolver {
35  public:
VeridexResolver(const DexFile & dex_file,const DexResolverMap & dex_resolvers,TypeMap & type_map)36   VeridexResolver(const DexFile& dex_file,
37                   const DexResolverMap& dex_resolvers,
38                   TypeMap& type_map)
39       : dex_file_(dex_file),
40         type_map_(type_map),
41         dex_resolvers_(dex_resolvers),
42         type_infos_(dex_file.NumTypeIds(), VeriClass()),
43         method_infos_(dex_file.NumMethodIds(), nullptr),
44         field_infos_(dex_file.NumFieldIds(), nullptr) {}
45 
46   // Run on the defined classes of that dex file and populate our
47   // local type cache.
48   void Run();
49 
50   // Return the class declared at `index`.
51   VeriClass* GetVeriClass(dex::TypeIndex index);
52 
53   // Return the method declared at `method_index`.
54   VeriMethod GetMethod(uint32_t method_index);
55 
56   // Return the field declared at `field_index`.
57   VeriField GetField(uint32_t field_index);
58 
59   // Do a JLS lookup in `kls` to find a method.
60   VeriMethod LookupMethodIn(const VeriClass& kls,
61                             const char* method_name,
62                             const Signature& method_signature);
63 
64   // Do a JLS lookup in `kls` to find a field.
65   VeriField LookupFieldIn(const VeriClass& kls,
66                           const char* field_name,
67                           const char* field_type);
68 
69   // Lookup a method declared in `kls`.
70   VeriMethod LookupDeclaredMethodIn(const VeriClass& kls,
71                                     const char* method_name,
72                                     const char* signature) const;
73 
74   // Resolve all type_id/method_id/field_id.
75   void ResolveAll();
76 
77   // The dex file this resolver is associated to.
GetDexFile()78   const DexFile& GetDexFile() const {
79     return dex_file_;
80   }
81 
GetDexFileOf(const VeriClass & kls)82   const DexFile& GetDexFileOf(const VeriClass& kls) {
83     return GetResolverOf(kls)->dex_file_;
84   }
85 
86  private:
87   // Return the resolver where `kls` is from.
88   VeridexResolver* GetResolverOf(const VeriClass& kls) const;
89 
90   const DexFile& dex_file_;
91   TypeMap& type_map_;
92   const DexResolverMap& dex_resolvers_;
93   std::vector<VeriClass> type_infos_;
94   std::vector<VeriMethod> method_infos_;
95   std::vector<VeriField> field_infos_;
96 };
97 
98 }  // namespace art
99 
100 #endif  // ART_TOOLS_VERIDEX_RESOLVER_H_
101