• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2017 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 #include "slicer/dex_ir.h"
18 
19 #include "slicer/chronometer.h"
20 #include "slicer/dex_utf8.h"
21 #include "slicer/dex_format.h"
22 
23 #include <cstdint>
24 #include <algorithm>
25 #include <memory>
26 #include <sstream>
27 #include <vector>
28 
29 namespace ir {
30 
31 // DBJ2a string hash
HashString(const char * cstr)32 static uint32_t HashString(const char* cstr) {
33   uint32_t hash = 5381;  // DBJ2 magic prime value
34   while (*cstr) {
35     hash = ((hash << 5) + hash) ^ *cstr++;
36   }
37   return hash;
38 }
39 
Hash(const char * string_key) const40 uint32_t StringsHasher::Hash(const char* string_key) const {
41   return HashString(string_key);
42 }
43 
Compare(const char * string_key,const String * string) const44 bool StringsHasher::Compare(const char* string_key, const String* string) const {
45   return dex::Utf8Cmp(string_key, string->c_str()) == 0;
46 }
47 
Hash(const std::string & proto_key) const48 uint32_t ProtosHasher::Hash(const std::string& proto_key) const {
49   return HashString(proto_key.c_str());
50 }
51 
Compare(const std::string & proto_key,const Proto * proto) const52 bool ProtosHasher::Compare(const std::string& proto_key, const Proto* proto) const {
53   return proto_key == proto->Signature();
54 }
55 
GetKey(const EncodedMethod * method) const56 MethodKey MethodsHasher::GetKey(const EncodedMethod* method) const {
57   MethodKey method_key;
58   method_key.class_descriptor = method->decl->parent->descriptor;
59   method_key.method_name = method->decl->name;
60   method_key.prototype = method->decl->prototype;
61   return method_key;
62 }
63 
Hash(const MethodKey & method_key) const64 uint32_t MethodsHasher::Hash(const MethodKey& method_key) const {
65   return static_cast<uint32_t>(std::hash<void*>{}(method_key.class_descriptor) ^
66                                std::hash<void*>{}(method_key.method_name) ^
67                                std::hash<void*>{}(method_key.prototype));
68 }
69 
Compare(const MethodKey & method_key,const EncodedMethod * method) const70 bool MethodsHasher::Compare(const MethodKey& method_key, const EncodedMethod* method) const {
71   return method_key.class_descriptor == method->decl->parent->descriptor &&
72          method_key.method_name == method->decl->name &&
73          method_key.prototype == method->decl->prototype;
74 }
75 
76 // Human-readable type declaration
Decl() const77 std::string Type::Decl() const {
78   return dex::DescriptorToDecl(descriptor->c_str());
79 }
80 
GetCategory() const81 Type::Category Type::GetCategory() const {
82   switch (*descriptor->c_str()) {
83     case 'L':
84     case '[':
85       return Category::Reference;
86     case 'V':
87       return Category::Void;
88     case 'D':
89     case 'J':
90       return Category::WideScalar;
91     default:
92       return Category::Scalar;
93   }
94 }
95 
96 // Create the corresponding JNI signature:
97 //  https://docs.oracle.com/javase/8/docs/technotes/guides/jni/spec/types.html#type_signatures
Signature() const98 std::string Proto::Signature() const {
99   std::stringstream ss;
100   ss << "(";
101   if (param_types != nullptr) {
102     for (const auto& type : param_types->types) {
103       ss << type->descriptor->c_str();
104     }
105   }
106   ss << ")";
107   ss << return_type->descriptor->c_str();
108   return ss.str();
109 }
110 
111 // Helper for IR normalization
112 // (it sorts items and update the numeric idexes to match)
113 template <class T, class C>
IndexItems(std::vector<T> & items,C comp)114 static void IndexItems(std::vector<T>& items, C comp) {
115   std::sort(items.begin(), items.end(), comp);
116   for (size_t i = 0; i < items.size(); ++i) {
117     items[i]->index = i;
118   }
119 }
120 
121 // Helper for IR normalization (DFS for topological sort)
122 //
123 // NOTE: this recursive version is clean and simple and we know
124 //  that the max depth is bounded (exactly 1 for JVMTI and a small
125 //  max for general case - the largest .dex file in AOSP has 5000 classes
126 //  total)
127 //
TopSortClassIndex(Class * irClass,dex::u4 * nextIndex)128 void DexFile::TopSortClassIndex(Class* irClass, dex::u4* nextIndex) {
129   if (irClass->index == dex::u4(-1)) {
130     if (irClass->super_class && irClass->super_class->class_def) {
131       TopSortClassIndex(irClass->super_class->class_def, nextIndex);
132     }
133 
134     if (irClass->interfaces) {
135       for (Type* interfaceType : irClass->interfaces->types) {
136         if (interfaceType->class_def) {
137           TopSortClassIndex(interfaceType->class_def, nextIndex);
138         }
139       }
140     }
141 
142     SLICER_CHECK_LT(*nextIndex, classes.size());
143     irClass->index = (*nextIndex)++;
144   }
145 }
146 
147 // Helper for IR normalization
148 // (topological sort the classes)
SortClassIndexes()149 void DexFile::SortClassIndexes() {
150   for (auto& irClass : classes) {
151     irClass->index = dex::u4(-1);
152   }
153 
154   dex::u4 nextIndex = 0;
155   for (auto& irClass : classes) {
156     TopSortClassIndex(irClass.get(), &nextIndex);
157   }
158 }
159 
160 // Helper for NormalizeClass()
SortEncodedFields(std::vector<EncodedField * > * fields)161 static void SortEncodedFields(std::vector<EncodedField*>* fields) {
162   std::sort(fields->begin(), fields->end(),
163             [](const EncodedField* a, const EncodedField* b) {
164               SLICER_CHECK(a->decl->index != b->decl->index || a == b);
165               return a->decl->index < b->decl->index;
166             });
167 }
168 
169 // Helper for NormalizeClass()
SortEncodedMethods(std::vector<EncodedMethod * > * methods)170 static void SortEncodedMethods(std::vector<EncodedMethod*>* methods) {
171   std::sort(methods->begin(), methods->end(),
172             [](const EncodedMethod* a, const EncodedMethod* b) {
173               SLICER_CHECK(a->decl->index != b->decl->index || a == b);
174               return a->decl->index < b->decl->index;
175             });
176 }
177 
178 // Helper for IR normalization
179 // (sort the field & method arrays)
NormalizeClass(Class * irClass)180 static void NormalizeClass(Class* irClass) {
181   SortEncodedFields(&irClass->static_fields);
182   SortEncodedFields(&irClass->instance_fields);
183   SortEncodedMethods(&irClass->direct_methods);
184   SortEncodedMethods(&irClass->virtual_methods);
185 }
186 
187 // Prepare the IR for generating a .dex image
188 // (the .dex format requires a specific sort order for some of the arrays, etc...)
189 //
190 // TODO: not a great solution - move this logic to the writer!
191 //
192 // TODO: the comparison predicate can be better expressed by using std::tie()
193 //  Ex. FieldDecl has a method comp() returning tie(parent->index, name->index, type->index)
194 //
Normalize()195 void DexFile::Normalize() {
196   // sort build the .dex indexes
197   IndexItems(strings, [](const own<String>& a, const own<String>& b) {
198     // this list must be sorted by std::string contents, using UTF-16 code point values
199     // (not in a locale-sensitive manner)
200     return dex::Utf8Cmp(a->c_str(), b->c_str()) < 0;
201   });
202 
203   IndexItems(types, [](const own<Type>& a, const own<Type>& b) {
204     // this list must be sorted by string_id index
205     return a->descriptor->index < b->descriptor->index;
206   });
207 
208   IndexItems(protos, [](const own<Proto>& a, const own<Proto>& b) {
209     // this list must be sorted in return-type (by type_id index) major order,
210     // and then by argument list (lexicographic ordering, individual arguments
211     // ordered by type_id index)
212     if (a->return_type->index != b->return_type->index) {
213       return a->return_type->index < b->return_type->index;
214     } else {
215       std::vector<Type*> empty;
216       const auto& aParamTypes = a->param_types ? a->param_types->types : empty;
217       const auto& bParamTypes = b->param_types ? b->param_types->types : empty;
218       return std::lexicographical_compare(
219           aParamTypes.begin(), aParamTypes.end(), bParamTypes.begin(),
220           bParamTypes.end(),
221           [](const Type* t1, const Type* t2) { return t1->index < t2->index; });
222     }
223   });
224 
225   IndexItems(fields, [](const own<FieldDecl>& a, const own<FieldDecl>& b) {
226     // this list must be sorted, where the defining type (by type_id index) is
227     // the major order, field name (by string_id index) is the intermediate
228     // order, and type (by type_id index) is the minor order
229     return (a->parent->index != b->parent->index)
230                ? a->parent->index < b->parent->index
231                : (a->name->index != b->name->index)
232                      ? a->name->index < b->name->index
233                      : a->type->index < b->type->index;
234   });
235 
236   IndexItems(methods, [](const own<MethodDecl>& a, const own<MethodDecl>& b) {
237     // this list must be sorted, where the defining type (by type_id index) is
238     // the major order, method name (by string_id index) is the intermediate
239     // order, and method prototype (by proto_id index) is the minor order
240     return (a->parent->index != b->parent->index)
241                ? a->parent->index < b->parent->index
242                : (a->name->index != b->name->index)
243                      ? a->name->index < b->name->index
244                      : a->prototype->index < b->prototype->index;
245   });
246 
247   // reverse topological sort
248   //
249   // the classes must be ordered such that a given class's superclass and
250   // implemented interfaces appear in the list earlier than the referring
251   // class
252   //
253   // CONSIDER: for the BCI-only scenario we can avoid this
254   //
255   SortClassIndexes();
256 
257   IndexItems(classes, [&](const own<Class>& a, const own<Class>& b) {
258     SLICER_CHECK_LT(a->index, classes.size());
259     SLICER_CHECK_LT(b->index, classes.size());
260     SLICER_CHECK(a->index != b->index || a == b);
261     return a->index < b->index;
262   });
263 
264   // normalize class data
265   for (const auto& irClass : classes) {
266     NormalizeClass(irClass.get());
267   }
268 
269   // normalize annotations
270   for (const auto& irAnnotation : annotations) {
271     // elements must be sorted in increasing order by string_id index
272     auto& elements = irAnnotation->elements;
273     std::sort(elements.begin(), elements.end(),
274               [](const AnnotationElement* a, const AnnotationElement* b) {
275                 return a->name->index < b->name->index;
276               });
277   }
278 
279   // normalize "annotation_set_item"
280   for (const auto& irAnnotationSet : annotation_sets) {
281     // The elements must be sorted in increasing order, by type_idx
282     auto& annotations = irAnnotationSet->annotations;
283     std::sort(annotations.begin(), annotations.end(),
284               [](const Annotation* a, const Annotation* b) {
285                 return a->type->index < b->type->index;
286               });
287   }
288 
289   // normalize "annotations_directory_item"
290   for (const auto& irAnnotationDirectory : annotations_directories) {
291     // field_annotations: The elements of the list must be
292     // sorted in increasing order, by field_idx
293     auto& field_annotations = irAnnotationDirectory->field_annotations;
294     std::sort(field_annotations.begin(), field_annotations.end(),
295               [](const FieldAnnotation* a, const FieldAnnotation* b) {
296                 return a->field_decl->index < b->field_decl->index;
297               });
298 
299     // method_annotations: The elements of the list must be
300     // sorted in increasing order, by method_idx
301     auto& method_annotations = irAnnotationDirectory->method_annotations;
302     std::sort(method_annotations.begin(), method_annotations.end(),
303               [](const MethodAnnotation* a, const MethodAnnotation* b) {
304                 return a->method_decl->index < b->method_decl->index;
305               });
306 
307     // parameter_annotations: The elements of the list must be
308     // sorted in increasing order, by method_idx
309     auto& param_annotations = irAnnotationDirectory->param_annotations;
310     std::sort(param_annotations.begin(), param_annotations.end(),
311               [](const ParamAnnotation* a, const ParamAnnotation* b) {
312                 return a->method_decl->index < b->method_decl->index;
313               });
314   }
315 }
316 
317 } // namespace ir
318 
319