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_VERIDEX_H_
18 #define ART_TOOLS_VERIDEX_VERIDEX_H_
19 
20 #include <map>
21 
22 #include "dex/dex_file.h"
23 #include "dex/primitive.h"
24 
25 namespace art {
26 
27 static int gTargetSdkVersion = 1000;  // Will be initialized after parsing options.
28 
29 /**
30  * Abstraction for fields defined in dex files. Currently, that's a pointer into their
31  * `encoded_field` description.
32  */
33 using VeriField = const uint8_t*;
34 
35 /**
36  * Abstraction for methods defined in dex files. Currently, that's a pointer into their
37  * `encoded_method` description.
38  */
39 using VeriMethod = const uint8_t*;
40 
41 /**
42  * Abstraction for classes defined, or implicitly defined (for arrays and primitives)
43  * in dex files.
44  */
45 class VeriClass {
46  public:
47   VeriClass(const VeriClass& other) = default;
48   VeriClass() = default;
VeriClass(Primitive::Type k,uint8_t dims,const DexFile::ClassDef * cl)49   VeriClass(Primitive::Type k, uint8_t dims, const DexFile::ClassDef* cl)
50       : kind_(k), dimensions_(dims), class_def_(cl) {}
51 
IsUninitialized()52   bool IsUninitialized() const {
53     return kind_ == Primitive::Type::kPrimNot && dimensions_ == 0 && class_def_ == nullptr;
54   }
55 
IsPrimitive()56   bool IsPrimitive() const {
57     return kind_ != Primitive::Type::kPrimNot && dimensions_ == 0;
58   }
59 
IsArray()60   bool IsArray() const {
61     return dimensions_ != 0;
62   }
63 
GetKind()64   Primitive::Type GetKind() const { return kind_; }
GetDimensions()65   uint8_t GetDimensions() const { return dimensions_; }
GetClassDef()66   const DexFile::ClassDef* GetClassDef() const { return class_def_; }
67 
68   static VeriClass* object_;
69   static VeriClass* class_;
70   static VeriClass* class_loader_;
71   static VeriClass* string_;
72   static VeriClass* throwable_;
73   static VeriClass* boolean_;
74   static VeriClass* byte_;
75   static VeriClass* char_;
76   static VeriClass* short_;
77   static VeriClass* integer_;
78   static VeriClass* float_;
79   static VeriClass* double_;
80   static VeriClass* long_;
81   static VeriClass* void_;
82 
83   static VeriMethod forName_;
84   static VeriMethod getField_;
85   static VeriMethod getDeclaredField_;
86   static VeriMethod getMethod_;
87   static VeriMethod getDeclaredMethod_;
88   static VeriMethod getClass_;
89   static VeriMethod loadClass_;
90 
91   static VeriField sdkInt_;
92 
93  private:
94   Primitive::Type kind_;
95   uint8_t dimensions_;
96   const DexFile::ClassDef* class_def_;
97 };
98 
IsGetMethod(VeriMethod method)99 inline bool IsGetMethod(VeriMethod method) {
100   return method == VeriClass::getMethod_ || method == VeriClass::getDeclaredMethod_;
101 }
102 
IsGetField(VeriMethod method)103 inline bool IsGetField(VeriMethod method) {
104   return method == VeriClass::getField_ || method == VeriClass::getDeclaredField_;
105 }
106 
107 /**
108  * Map from name to VeriClass to quickly lookup classes.
109  */
110 using TypeMap = std::map<std::string, VeriClass*>;
111 
112 }  // namespace art
113 
114 #endif  // ART_TOOLS_VERIDEX_VERIDEX_H_
115