Nicolas Geoffray | dd96ed3 | 2018-03-21 11:00:14 +0000 | [diff] [blame] | 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 | |
Nicolas Geoffray | 534a0a1 | 2018-03-24 20:02:25 +0000 | [diff] [blame] | 25 | class HiddenApi; |
Nicolas Geoffray | e826477 | 2018-03-22 22:16:41 +0000 | [diff] [blame] | 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 | |
Nicolas Geoffray | dd96ed3 | 2018-03-21 11:00:14 +0000 | [diff] [blame] | 34 | class VeridexResolver { |
| 35 | public: |
Nicolas Geoffray | e826477 | 2018-03-22 22:16:41 +0000 | [diff] [blame] | 36 | VeridexResolver(const DexFile& dex_file, |
| 37 | const DexResolverMap& dex_resolvers, |
| 38 | TypeMap& type_map) |
Nicolas Geoffray | dd96ed3 | 2018-03-21 11:00:14 +0000 | [diff] [blame] | 39 | : dex_file_(dex_file), |
| 40 | type_map_(type_map), |
Nicolas Geoffray | e826477 | 2018-03-22 22:16:41 +0000 | [diff] [blame] | 41 | dex_resolvers_(dex_resolvers), |
Nicolas Geoffray | dd96ed3 | 2018-03-21 11:00:14 +0000 | [diff] [blame] | 42 | type_infos_(dex_file.NumTypeIds(), VeriClass()), |
| 43 | method_infos_(dex_file.NumMethodIds(), nullptr), |
| 44 | field_infos_(dex_file.NumFieldIds(), nullptr) {} |
| 45 | |
Nicolas Geoffray | e826477 | 2018-03-22 22:16:41 +0000 | [diff] [blame] | 46 | // Run on the defined classes of that dex file and populate our |
| 47 | // local type cache. |
Nicolas Geoffray | dd96ed3 | 2018-03-21 11:00:14 +0000 | [diff] [blame] | 48 | void Run(); |
| 49 | |
Nicolas Geoffray | e826477 | 2018-03-22 22:16:41 +0000 | [diff] [blame] | 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 | |
Nicolas Geoffray | 2ebff05 | 2018-04-04 22:32:03 +0100 | [diff] [blame] | 69 | // Lookup a method declared in `kls`. |
| 70 | VeriMethod LookupDeclaredMethodIn(const VeriClass& kls, |
| 71 | const char* method_name, |
| 72 | const char* signature) const; |
| 73 | |
Nicolas Geoffray | 11ed027 | 2018-03-28 18:18:48 +0100 | [diff] [blame] | 74 | // Resolve all type_id/method_id/field_id. |
| 75 | void ResolveAll(); |
| 76 | |
| 77 | // The dex file this resolver is associated to. |
| 78 | const DexFile& GetDexFile() const { |
| 79 | return dex_file_; |
| 80 | } |
Nicolas Geoffray | e826477 | 2018-03-22 22:16:41 +0000 | [diff] [blame] | 81 | |
Nicolas Geoffray | 909b127 | 2018-04-23 13:43:31 +0100 | [diff] [blame] | 82 | const DexFile& GetDexFileOf(const VeriClass& kls) { |
| 83 | return GetResolverOf(kls)->dex_file_; |
| 84 | } |
| 85 | |
Nicolas Geoffray | dd96ed3 | 2018-03-21 11:00:14 +0000 | [diff] [blame] | 86 | private: |
Nicolas Geoffray | e826477 | 2018-03-22 22:16:41 +0000 | [diff] [blame] | 87 | // Return the resolver where `kls` is from. |
| 88 | VeridexResolver* GetResolverOf(const VeriClass& kls) const; |
| 89 | |
Nicolas Geoffray | dd96ed3 | 2018-03-21 11:00:14 +0000 | [diff] [blame] | 90 | const DexFile& dex_file_; |
| 91 | TypeMap& type_map_; |
Nicolas Geoffray | e826477 | 2018-03-22 22:16:41 +0000 | [diff] [blame] | 92 | const DexResolverMap& dex_resolvers_; |
Nicolas Geoffray | dd96ed3 | 2018-03-21 11:00:14 +0000 | [diff] [blame] | 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_ |