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