blob: c4058d63ee132839a24e393c52600492f1c423b0 [file] [log] [blame]
David Brazdilca3c8c32016-09-06 14:04:48 +01001/*
2 * Copyright (C) 2016 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 "verifier_deps.h"
18
Mathieu Chartier32b50302016-11-17 13:08:35 -080019#include <cstring>
20
Nicolas Geoffray340dafa2016-11-18 16:03:10 +000021#include "base/stl_util.h"
David Brazdilca3c8c32016-09-06 14:04:48 +010022#include "compiler_callbacks.h"
David Brazdil6f82fbd2016-09-14 11:55:26 +010023#include "leb128.h"
David Brazdilca3c8c32016-09-06 14:04:48 +010024#include "mirror/class-inl.h"
Mathieu Chartier3398c782016-09-30 10:27:43 -070025#include "obj_ptr-inl.h"
David Brazdilca3c8c32016-09-06 14:04:48 +010026#include "runtime.h"
27
28namespace art {
29namespace verifier {
30
31VerifierDeps::VerifierDeps(const std::vector<const DexFile*>& dex_files) {
David Brazdilca3c8c32016-09-06 14:04:48 +010032 for (const DexFile* dex_file : dex_files) {
33 DCHECK(GetDexFileDeps(*dex_file) == nullptr);
34 std::unique_ptr<DexFileDeps> deps(new DexFileDeps());
35 dex_deps_.emplace(dex_file, std::move(deps));
36 }
37}
38
Nicolas Geoffray340dafa2016-11-18 16:03:10 +000039void VerifierDeps::MergeWith(const VerifierDeps& other,
40 const std::vector<const DexFile*>& dex_files) {
41 DCHECK(dex_deps_.size() == other.dex_deps_.size());
42 for (const DexFile* dex_file : dex_files) {
43 DexFileDeps* my_deps = GetDexFileDeps(*dex_file);
44 const DexFileDeps& other_deps = *other.GetDexFileDeps(*dex_file);
45 // We currently collect extra strings only on the main `VerifierDeps`,
46 // which should be the one passed as `this` in this method.
47 DCHECK(other_deps.strings_.empty());
48 MergeSets(my_deps->assignable_types_, other_deps.assignable_types_);
49 MergeSets(my_deps->unassignable_types_, other_deps.unassignable_types_);
50 MergeSets(my_deps->classes_, other_deps.classes_);
51 MergeSets(my_deps->fields_, other_deps.fields_);
52 MergeSets(my_deps->direct_methods_, other_deps.direct_methods_);
53 MergeSets(my_deps->virtual_methods_, other_deps.virtual_methods_);
54 MergeSets(my_deps->interface_methods_, other_deps.interface_methods_);
55 for (dex::TypeIndex entry : other_deps.unverified_classes_) {
56 my_deps->unverified_classes_.push_back(entry);
57 }
58 }
59}
60
David Brazdilca3c8c32016-09-06 14:04:48 +010061VerifierDeps::DexFileDeps* VerifierDeps::GetDexFileDeps(const DexFile& dex_file) {
62 auto it = dex_deps_.find(&dex_file);
63 return (it == dex_deps_.end()) ? nullptr : it->second.get();
64}
65
Nicolas Geoffrayd01f60c2016-10-28 14:45:48 +010066const VerifierDeps::DexFileDeps* VerifierDeps::GetDexFileDeps(const DexFile& dex_file) const {
67 auto it = dex_deps_.find(&dex_file);
68 return (it == dex_deps_.end()) ? nullptr : it->second.get();
69}
70
David Brazdilca3c8c32016-09-06 14:04:48 +010071template <typename T>
72uint16_t VerifierDeps::GetAccessFlags(T* element) {
73 static_assert(kAccJavaFlagsMask == 0xFFFF, "Unexpected value of a constant");
74 if (element == nullptr) {
75 return VerifierDeps::kUnresolvedMarker;
76 } else {
77 uint16_t access_flags = Low16Bits(element->GetAccessFlags());
78 CHECK_NE(access_flags, VerifierDeps::kUnresolvedMarker);
79 return access_flags;
80 }
81}
82
Andreas Gampe8a0128a2016-11-28 07:38:35 -080083dex::StringIndex VerifierDeps::GetClassDescriptorStringId(const DexFile& dex_file,
84 ObjPtr<mirror::Class> klass) {
Mathieu Chartier32b50302016-11-17 13:08:35 -080085 DCHECK(klass != nullptr);
86 ObjPtr<mirror::DexCache> dex_cache = klass->GetDexCache();
Mathieu Chartierfc2dd612016-11-21 15:05:23 -080087 // Array and proxy classes do not have a dex cache.
Mathieu Chartier32b50302016-11-17 13:08:35 -080088 if (!klass->IsArrayClass() && !klass->IsProxyClass()) {
89 DCHECK(dex_cache != nullptr) << klass->PrettyClass();
90 if (dex_cache->GetDexFile() == &dex_file) {
91 // FindStringId is slow, try to go through the class def if we have one.
92 const DexFile::ClassDef* class_def = klass->GetClassDef();
93 DCHECK(class_def != nullptr) << klass->PrettyClass();
Mathieu Chartier32b50302016-11-17 13:08:35 -080094 const DexFile::TypeId& type_id = dex_file.GetTypeId(class_def->class_idx_);
Mathieu Chartierfc2dd612016-11-21 15:05:23 -080095 if (kIsDebugBuild) {
96 std::string temp;
97 CHECK_EQ(GetIdFromString(dex_file, klass->GetDescriptor(&temp)), type_id.descriptor_idx_);
98 }
Mathieu Chartier32b50302016-11-17 13:08:35 -080099 return type_id.descriptor_idx_;
100 }
101 }
102 std::string temp;
103 return GetIdFromString(dex_file, klass->GetDescriptor(&temp));
104}
105
106// Try to find the string descriptor of the class. type_idx is a best guess of a matching string id.
Andreas Gampe8a0128a2016-11-28 07:38:35 -0800107static dex::StringIndex TryGetClassDescriptorStringId(const DexFile& dex_file,
108 dex::TypeIndex type_idx,
109 ObjPtr<mirror::Class> klass)
Mathieu Chartier32b50302016-11-17 13:08:35 -0800110 REQUIRES_SHARED(Locks::mutator_lock_) {
111 if (!klass->IsArrayClass()) {
112 const DexFile::TypeId& type_id = dex_file.GetTypeId(type_idx);
113 const DexFile& klass_dex = klass->GetDexFile();
114 const DexFile::TypeId& klass_type_id = klass_dex.GetTypeId(klass->GetClassDef()->class_idx_);
115 if (strcmp(dex_file.GetTypeDescriptor(type_id),
116 klass_dex.GetTypeDescriptor(klass_type_id)) == 0) {
117 return type_id.descriptor_idx_;
118 }
119 }
Andreas Gampe8a0128a2016-11-28 07:38:35 -0800120 return dex::StringIndex::Invalid();
Mathieu Chartier32b50302016-11-17 13:08:35 -0800121}
122
Andreas Gampe8a0128a2016-11-28 07:38:35 -0800123dex::StringIndex VerifierDeps::GetMethodDeclaringClassStringId(const DexFile& dex_file,
124 uint32_t dex_method_index,
125 ArtMethod* method) {
David Brazdilca3c8c32016-09-06 14:04:48 +0100126 static_assert(kAccJavaFlagsMask == 0xFFFF, "Unexpected value of a constant");
Mathieu Chartier32b50302016-11-17 13:08:35 -0800127 if (method == nullptr) {
Andreas Gampe8a0128a2016-11-28 07:38:35 -0800128 return dex::StringIndex(VerifierDeps::kUnresolvedMarker);
Mathieu Chartier32b50302016-11-17 13:08:35 -0800129 }
Andreas Gampe8a0128a2016-11-28 07:38:35 -0800130 const dex::StringIndex string_id = TryGetClassDescriptorStringId(
Mathieu Chartier32b50302016-11-17 13:08:35 -0800131 dex_file,
132 dex_file.GetMethodId(dex_method_index).class_idx_,
133 method->GetDeclaringClass());
Andreas Gampe8a0128a2016-11-28 07:38:35 -0800134 if (string_id.IsValid()) {
Mathieu Chartier32b50302016-11-17 13:08:35 -0800135 // Got lucky using the original dex file, return based on the input dex file.
136 DCHECK_EQ(GetClassDescriptorStringId(dex_file, method->GetDeclaringClass()), string_id);
David Brazdilca3c8c32016-09-06 14:04:48 +0100137 return string_id;
138 }
Mathieu Chartier32b50302016-11-17 13:08:35 -0800139 return GetClassDescriptorStringId(dex_file, method->GetDeclaringClass());
140}
141
Andreas Gampe8a0128a2016-11-28 07:38:35 -0800142dex::StringIndex VerifierDeps::GetFieldDeclaringClassStringId(const DexFile& dex_file,
143 uint32_t dex_field_idx,
144 ArtField* field) {
Mathieu Chartier32b50302016-11-17 13:08:35 -0800145 static_assert(kAccJavaFlagsMask == 0xFFFF, "Unexpected value of a constant");
146 if (field == nullptr) {
Andreas Gampe8a0128a2016-11-28 07:38:35 -0800147 return dex::StringIndex(VerifierDeps::kUnresolvedMarker);
Mathieu Chartier32b50302016-11-17 13:08:35 -0800148 }
Andreas Gampe8a0128a2016-11-28 07:38:35 -0800149 const dex::StringIndex string_id = TryGetClassDescriptorStringId(
Mathieu Chartier32b50302016-11-17 13:08:35 -0800150 dex_file,
151 dex_file.GetFieldId(dex_field_idx).class_idx_,
152 field->GetDeclaringClass());
Andreas Gampe8a0128a2016-11-28 07:38:35 -0800153 if (string_id.IsValid()) {
Mathieu Chartier32b50302016-11-17 13:08:35 -0800154 // Got lucky using the original dex file, return based on the input dex file.
155 DCHECK_EQ(GetClassDescriptorStringId(dex_file, field->GetDeclaringClass()), string_id);
156 return string_id;
157 }
158 return GetClassDescriptorStringId(dex_file, field->GetDeclaringClass());
David Brazdilca3c8c32016-09-06 14:04:48 +0100159}
160
Nicolas Geoffray340dafa2016-11-18 16:03:10 +0000161static inline VerifierDeps* GetMainVerifierDeps() {
162 // The main VerifierDeps is the one set in the compiler callbacks, which at the
163 // end of verification will have all the per-thread VerifierDeps merged into it.
164 CompilerCallbacks* callbacks = Runtime::Current()->GetCompilerCallbacks();
165 if (callbacks == nullptr) {
166 return nullptr;
167 }
168 return callbacks->GetVerifierDeps();
169}
170
171static inline VerifierDeps* GetThreadLocalVerifierDeps() {
172 // During AOT, each thread has its own VerifierDeps, to avoid lock contention. At the end
173 // of full verification, these VerifierDeps will be merged into the main one.
174 if (!Runtime::Current()->IsAotCompiler()) {
175 return nullptr;
176 }
177 return Thread::Current()->GetVerifierDeps();
178}
179
180static bool FindExistingStringId(const std::vector<std::string>& strings,
181 const std::string& str,
182 uint32_t* found_id) {
183 uint32_t num_extra_ids = strings.size();
184 for (size_t i = 0; i < num_extra_ids; ++i) {
185 if (strings[i] == str) {
186 *found_id = i;
187 return true;
188 }
189 }
190 return false;
191}
192
Andreas Gampe8a0128a2016-11-28 07:38:35 -0800193dex::StringIndex VerifierDeps::GetIdFromString(const DexFile& dex_file, const std::string& str) {
David Brazdilca3c8c32016-09-06 14:04:48 +0100194 const DexFile::StringId* string_id = dex_file.FindStringId(str.c_str());
195 if (string_id != nullptr) {
196 // String is in the DEX file. Return its ID.
197 return dex_file.GetIndexForStringId(*string_id);
198 }
199
200 // String is not in the DEX file. Assign a new ID to it which is higher than
201 // the number of strings in the DEX file.
202
Nicolas Geoffray340dafa2016-11-18 16:03:10 +0000203 // We use the main `VerifierDeps` for adding new strings to simplify
204 // synchronization/merging of these entries between threads.
205 VerifierDeps* singleton = GetMainVerifierDeps();
206 DexFileDeps* deps = singleton->GetDexFileDeps(dex_file);
David Brazdilca3c8c32016-09-06 14:04:48 +0100207 DCHECK(deps != nullptr);
208
209 uint32_t num_ids_in_dex = dex_file.NumStringIds();
Nicolas Geoffray340dafa2016-11-18 16:03:10 +0000210 uint32_t found_id;
David Brazdilca3c8c32016-09-06 14:04:48 +0100211
Nicolas Geoffray340dafa2016-11-18 16:03:10 +0000212 {
213 ReaderMutexLock mu(Thread::Current(), *Locks::verifier_deps_lock_);
214 if (FindExistingStringId(deps->strings_, str, &found_id)) {
Andreas Gampe8a0128a2016-11-28 07:38:35 -0800215 return dex::StringIndex(num_ids_in_dex + found_id);
David Brazdilca3c8c32016-09-06 14:04:48 +0100216 }
217 }
Nicolas Geoffray340dafa2016-11-18 16:03:10 +0000218 {
219 WriterMutexLock mu(Thread::Current(), *Locks::verifier_deps_lock_);
220 if (FindExistingStringId(deps->strings_, str, &found_id)) {
Andreas Gampe8a0128a2016-11-28 07:38:35 -0800221 return dex::StringIndex(num_ids_in_dex + found_id);
Nicolas Geoffray340dafa2016-11-18 16:03:10 +0000222 }
223 deps->strings_.push_back(str);
Andreas Gampe8a0128a2016-11-28 07:38:35 -0800224 dex::StringIndex new_id(num_ids_in_dex + deps->strings_.size() - 1);
225 CHECK_GE(new_id.index_, num_ids_in_dex); // check for overflows
Nicolas Geoffray340dafa2016-11-18 16:03:10 +0000226 DCHECK_EQ(str, singleton->GetStringFromId(dex_file, new_id));
227 return new_id;
228 }
David Brazdilca3c8c32016-09-06 14:04:48 +0100229}
230
Andreas Gampe8a0128a2016-11-28 07:38:35 -0800231std::string VerifierDeps::GetStringFromId(const DexFile& dex_file, dex::StringIndex string_id)
232 const {
David Brazdilca3c8c32016-09-06 14:04:48 +0100233 uint32_t num_ids_in_dex = dex_file.NumStringIds();
Andreas Gampe8a0128a2016-11-28 07:38:35 -0800234 if (string_id.index_ < num_ids_in_dex) {
David Brazdilca3c8c32016-09-06 14:04:48 +0100235 return std::string(dex_file.StringDataByIdx(string_id));
236 } else {
Nicolas Geoffrayd01f60c2016-10-28 14:45:48 +0100237 const DexFileDeps* deps = GetDexFileDeps(dex_file);
David Brazdilca3c8c32016-09-06 14:04:48 +0100238 DCHECK(deps != nullptr);
Andreas Gampe8a0128a2016-11-28 07:38:35 -0800239 string_id.index_ -= num_ids_in_dex;
240 CHECK_LT(string_id.index_, deps->strings_.size());
241 return deps->strings_[string_id.index_];
David Brazdilca3c8c32016-09-06 14:04:48 +0100242 }
243}
244
Nicolas Geoffrayd01f60c2016-10-28 14:45:48 +0100245bool VerifierDeps::IsInClassPath(ObjPtr<mirror::Class> klass) const {
David Brazdilca3c8c32016-09-06 14:04:48 +0100246 DCHECK(klass != nullptr);
247
Nicolas Geoffray0f1cb172017-01-05 15:23:19 +0000248 // For array types, we return whether the non-array component type
249 // is in the classpath.
250 while (klass->IsArrayClass()) {
251 klass = klass->GetComponentType();
David Brazdilca3c8c32016-09-06 14:04:48 +0100252 }
253
Nicolas Geoffray0f1cb172017-01-05 15:23:19 +0000254 if (klass->IsPrimitive()) {
255 return true;
256 }
257
258 ObjPtr<mirror::DexCache> dex_cache = klass->GetDexCache();
259 DCHECK(dex_cache != nullptr);
David Brazdilca3c8c32016-09-06 14:04:48 +0100260 const DexFile* dex_file = dex_cache->GetDexFile();
261 DCHECK(dex_file != nullptr);
262
263 // Test if the `dex_deps_` contains an entry for `dex_file`. If not, the dex
264 // file was not registered as being compiled and we assume `klass` is in the
265 // classpath.
266 return (GetDexFileDeps(*dex_file) == nullptr);
267}
268
269void VerifierDeps::AddClassResolution(const DexFile& dex_file,
Andreas Gampea5b09a62016-11-17 15:21:22 -0800270 dex::TypeIndex type_idx,
David Brazdilca3c8c32016-09-06 14:04:48 +0100271 mirror::Class* klass) {
272 DexFileDeps* dex_deps = GetDexFileDeps(dex_file);
273 if (dex_deps == nullptr) {
274 // This invocation is from verification of a dex file which is not being compiled.
275 return;
276 }
277
278 if (klass != nullptr && !IsInClassPath(klass)) {
279 // Class resolved into one of the DEX files which are being compiled.
280 // This is not a classpath dependency.
281 return;
282 }
283
David Brazdilca3c8c32016-09-06 14:04:48 +0100284 dex_deps->classes_.emplace(ClassResolution(type_idx, GetAccessFlags(klass)));
285}
286
287void VerifierDeps::AddFieldResolution(const DexFile& dex_file,
288 uint32_t field_idx,
289 ArtField* field) {
290 DexFileDeps* dex_deps = GetDexFileDeps(dex_file);
291 if (dex_deps == nullptr) {
292 // This invocation is from verification of a dex file which is not being compiled.
293 return;
294 }
295
296 if (field != nullptr && !IsInClassPath(field->GetDeclaringClass())) {
297 // Field resolved into one of the DEX files which are being compiled.
298 // This is not a classpath dependency.
299 return;
300 }
301
Mathieu Chartier32b50302016-11-17 13:08:35 -0800302 dex_deps->fields_.emplace(FieldResolution(field_idx,
303 GetAccessFlags(field),
304 GetFieldDeclaringClassStringId(dex_file,
305 field_idx,
306 field)));
David Brazdilca3c8c32016-09-06 14:04:48 +0100307}
308
309void VerifierDeps::AddMethodResolution(const DexFile& dex_file,
310 uint32_t method_idx,
311 MethodResolutionKind resolution_kind,
312 ArtMethod* method) {
313 DexFileDeps* dex_deps = GetDexFileDeps(dex_file);
314 if (dex_deps == nullptr) {
315 // This invocation is from verification of a dex file which is not being compiled.
316 return;
317 }
318
319 if (method != nullptr && !IsInClassPath(method->GetDeclaringClass())) {
320 // Method resolved into one of the DEX files which are being compiled.
321 // This is not a classpath dependency.
322 return;
323 }
324
David Brazdilca3c8c32016-09-06 14:04:48 +0100325 MethodResolution method_tuple(method_idx,
326 GetAccessFlags(method),
Mathieu Chartier32b50302016-11-17 13:08:35 -0800327 GetMethodDeclaringClassStringId(dex_file, method_idx, method));
David Brazdilca3c8c32016-09-06 14:04:48 +0100328 if (resolution_kind == kDirectMethodResolution) {
329 dex_deps->direct_methods_.emplace(method_tuple);
330 } else if (resolution_kind == kVirtualMethodResolution) {
331 dex_deps->virtual_methods_.emplace(method_tuple);
332 } else {
333 DCHECK_EQ(resolution_kind, kInterfaceMethodResolution);
334 dex_deps->interface_methods_.emplace(method_tuple);
335 }
336}
337
338void VerifierDeps::AddAssignability(const DexFile& dex_file,
339 mirror::Class* destination,
340 mirror::Class* source,
341 bool is_strict,
342 bool is_assignable) {
343 // Test that the method is only called on reference types.
344 // Note that concurrent verification of `destination` and `source` may have
345 // set their status to erroneous. However, the tests performed below rely
346 // merely on no issues with linking (valid access flags, superclass and
347 // implemented interfaces). If the class at any point reached the IsResolved
348 // status, the requirement holds. This is guaranteed by RegTypeCache::ResolveClass.
Nicolas Geoffrayd1665a02016-12-12 13:07:07 +0000349 DCHECK(destination != nullptr);
350 DCHECK(source != nullptr);
351
352 if (destination->IsPrimitive() || source->IsPrimitive()) {
353 // Primitive types are trivially non-assignable to anything else.
354 // We do not need to record trivial assignability, as it will
355 // not change across releases.
356 return;
357 }
David Brazdilca3c8c32016-09-06 14:04:48 +0100358
Nicolas Geoffray119e8462016-12-21 10:29:43 +0000359 if (source->IsObjectClass() && !is_assignable) {
360 // j.l.Object is trivially non-assignable to other types, don't
361 // record it.
362 return;
363 }
364
David Brazdilca3c8c32016-09-06 14:04:48 +0100365 if (destination == source ||
366 destination->IsObjectClass() ||
367 (!is_strict && destination->IsInterface())) {
368 // Cases when `destination` is trivially assignable from `source`.
369 DCHECK(is_assignable);
370 return;
371 }
372
373 DCHECK_EQ(is_assignable, destination->IsAssignableFrom(source));
374
375 if (destination->IsArrayClass() && source->IsArrayClass()) {
376 // Both types are arrays. Break down to component types and add recursively.
377 // This helps filter out destinations from compiled DEX files (see below)
378 // and deduplicate entries with the same canonical component type.
379 mirror::Class* destination_component = destination->GetComponentType();
380 mirror::Class* source_component = source->GetComponentType();
381
382 // Only perform the optimization if both types are resolved which guarantees
383 // that they linked successfully, as required at the top of this method.
384 if (destination_component->IsResolved() && source_component->IsResolved()) {
385 AddAssignability(dex_file,
386 destination_component,
387 source_component,
388 /* is_strict */ true,
389 is_assignable);
390 return;
391 }
392 }
393
394 DexFileDeps* dex_deps = GetDexFileDeps(dex_file);
395 if (dex_deps == nullptr) {
396 // This invocation is from verification of a DEX file which is not being compiled.
397 return;
398 }
399
400 if (!IsInClassPath(destination) && !IsInClassPath(source)) {
401 // Both `destination` and `source` are defined in the compiled DEX files.
402 // No need to record a dependency.
403 return;
404 }
405
Nicolas Geoffray119e8462016-12-21 10:29:43 +0000406 if (!IsInClassPath(source) && !source->IsInterface() && !destination->IsInterface()) {
407 // Find the super class at the classpath boundary. Only that class
408 // can change the assignability.
409 // TODO: also chase the boundary for interfaces.
410 do {
411 source = source->GetSuperClass();
412 } while (!IsInClassPath(source));
413
414 // If that class is the actual destination, no need to record it.
415 if (source == destination) {
416 return;
417 }
418 }
419
420
David Brazdilca3c8c32016-09-06 14:04:48 +0100421 // Get string IDs for both descriptors and store in the appropriate set.
Andreas Gampe8a0128a2016-11-28 07:38:35 -0800422 dex::StringIndex destination_id = GetClassDescriptorStringId(dex_file, destination);
423 dex::StringIndex source_id = GetClassDescriptorStringId(dex_file, source);
David Brazdilca3c8c32016-09-06 14:04:48 +0100424
425 if (is_assignable) {
426 dex_deps->assignable_types_.emplace(TypeAssignability(destination_id, source_id));
427 } else {
428 dex_deps->unassignable_types_.emplace(TypeAssignability(destination_id, source_id));
429 }
430}
431
Nicolas Geoffray08025182016-10-25 17:20:18 +0100432void VerifierDeps::MaybeRecordVerificationStatus(const DexFile& dex_file,
Andreas Gampea5b09a62016-11-17 15:21:22 -0800433 dex::TypeIndex type_idx,
Nicolas Geoffray08025182016-10-25 17:20:18 +0100434 MethodVerifier::FailureKind failure_kind) {
435 if (failure_kind == MethodVerifier::kNoFailure) {
436 // We only record classes that did not fully verify at compile time.
437 return;
438 }
439
Nicolas Geoffray340dafa2016-11-18 16:03:10 +0000440 VerifierDeps* thread_deps = GetThreadLocalVerifierDeps();
441 if (thread_deps != nullptr) {
442 DexFileDeps* dex_deps = thread_deps->GetDexFileDeps(dex_file);
Nicolas Geoffray08025182016-10-25 17:20:18 +0100443 dex_deps->unverified_classes_.push_back(type_idx);
444 }
445}
446
David Brazdilca3c8c32016-09-06 14:04:48 +0100447void VerifierDeps::MaybeRecordClassResolution(const DexFile& dex_file,
Andreas Gampea5b09a62016-11-17 15:21:22 -0800448 dex::TypeIndex type_idx,
David Brazdilca3c8c32016-09-06 14:04:48 +0100449 mirror::Class* klass) {
Nicolas Geoffray340dafa2016-11-18 16:03:10 +0000450 VerifierDeps* thread_deps = GetThreadLocalVerifierDeps();
451 if (thread_deps != nullptr) {
452 thread_deps->AddClassResolution(dex_file, type_idx, klass);
David Brazdilca3c8c32016-09-06 14:04:48 +0100453 }
454}
455
456void VerifierDeps::MaybeRecordFieldResolution(const DexFile& dex_file,
457 uint32_t field_idx,
458 ArtField* field) {
Nicolas Geoffray340dafa2016-11-18 16:03:10 +0000459 VerifierDeps* thread_deps = GetThreadLocalVerifierDeps();
460 if (thread_deps != nullptr) {
461 thread_deps->AddFieldResolution(dex_file, field_idx, field);
David Brazdilca3c8c32016-09-06 14:04:48 +0100462 }
463}
464
465void VerifierDeps::MaybeRecordMethodResolution(const DexFile& dex_file,
466 uint32_t method_idx,
467 MethodResolutionKind resolution_kind,
468 ArtMethod* method) {
Nicolas Geoffray340dafa2016-11-18 16:03:10 +0000469 VerifierDeps* thread_deps = GetThreadLocalVerifierDeps();
470 if (thread_deps != nullptr) {
471 thread_deps->AddMethodResolution(dex_file, method_idx, resolution_kind, method);
David Brazdilca3c8c32016-09-06 14:04:48 +0100472 }
473}
474
475void VerifierDeps::MaybeRecordAssignability(const DexFile& dex_file,
476 mirror::Class* destination,
477 mirror::Class* source,
478 bool is_strict,
479 bool is_assignable) {
Nicolas Geoffray340dafa2016-11-18 16:03:10 +0000480 VerifierDeps* thread_deps = GetThreadLocalVerifierDeps();
481 if (thread_deps != nullptr) {
482 thread_deps->AddAssignability(dex_file, destination, source, is_strict, is_assignable);
David Brazdilca3c8c32016-09-06 14:04:48 +0100483 }
484}
485
Andreas Gampea5b09a62016-11-17 15:21:22 -0800486namespace {
487
David Brazdil6f82fbd2016-09-14 11:55:26 +0100488static inline uint32_t DecodeUint32WithOverflowCheck(const uint8_t** in, const uint8_t* end) {
489 CHECK_LT(*in, end);
490 return DecodeUnsignedLeb128(in);
491}
492
Andreas Gampea5b09a62016-11-17 15:21:22 -0800493template<typename T> inline uint32_t Encode(T in);
494
495template<> inline uint32_t Encode<uint16_t>(uint16_t in) {
496 return in;
497}
498template<> inline uint32_t Encode<uint32_t>(uint32_t in) {
499 return in;
500}
501template<> inline uint32_t Encode<dex::TypeIndex>(dex::TypeIndex in) {
502 return in.index_;
503}
Andreas Gampe8a0128a2016-11-28 07:38:35 -0800504template<> inline uint32_t Encode<dex::StringIndex>(dex::StringIndex in) {
505 return in.index_;
506}
Andreas Gampea5b09a62016-11-17 15:21:22 -0800507
508template<typename T> inline T Decode(uint32_t in);
509
510template<> inline uint16_t Decode<uint16_t>(uint32_t in) {
511 return dchecked_integral_cast<uint16_t>(in);
512}
513template<> inline uint32_t Decode<uint32_t>(uint32_t in) {
514 return in;
515}
516template<> inline dex::TypeIndex Decode<dex::TypeIndex>(uint32_t in) {
517 return dex::TypeIndex(in);
518}
Andreas Gampe8a0128a2016-11-28 07:38:35 -0800519template<> inline dex::StringIndex Decode<dex::StringIndex>(uint32_t in) {
520 return dex::StringIndex(in);
521}
Andreas Gampea5b09a62016-11-17 15:21:22 -0800522
David Brazdil6f82fbd2016-09-14 11:55:26 +0100523template<typename T1, typename T2>
524static inline void EncodeTuple(std::vector<uint8_t>* out, const std::tuple<T1, T2>& t) {
Andreas Gampea5b09a62016-11-17 15:21:22 -0800525 EncodeUnsignedLeb128(out, Encode(std::get<0>(t)));
526 EncodeUnsignedLeb128(out, Encode(std::get<1>(t)));
David Brazdil6f82fbd2016-09-14 11:55:26 +0100527}
528
529template<typename T1, typename T2>
530static inline void DecodeTuple(const uint8_t** in, const uint8_t* end, std::tuple<T1, T2>* t) {
Andreas Gampea5b09a62016-11-17 15:21:22 -0800531 T1 v1 = Decode<T1>(DecodeUint32WithOverflowCheck(in, end));
532 T2 v2 = Decode<T2>(DecodeUint32WithOverflowCheck(in, end));
David Brazdil6f82fbd2016-09-14 11:55:26 +0100533 *t = std::make_tuple(v1, v2);
534}
535
536template<typename T1, typename T2, typename T3>
537static inline void EncodeTuple(std::vector<uint8_t>* out, const std::tuple<T1, T2, T3>& t) {
Andreas Gampea5b09a62016-11-17 15:21:22 -0800538 EncodeUnsignedLeb128(out, Encode(std::get<0>(t)));
539 EncodeUnsignedLeb128(out, Encode(std::get<1>(t)));
540 EncodeUnsignedLeb128(out, Encode(std::get<2>(t)));
David Brazdil6f82fbd2016-09-14 11:55:26 +0100541}
542
543template<typename T1, typename T2, typename T3>
544static inline void DecodeTuple(const uint8_t** in, const uint8_t* end, std::tuple<T1, T2, T3>* t) {
Andreas Gampea5b09a62016-11-17 15:21:22 -0800545 T1 v1 = Decode<T1>(DecodeUint32WithOverflowCheck(in, end));
546 T2 v2 = Decode<T2>(DecodeUint32WithOverflowCheck(in, end));
Andreas Gampe8a0128a2016-11-28 07:38:35 -0800547 T3 v3 = Decode<T3>(DecodeUint32WithOverflowCheck(in, end));
David Brazdil6f82fbd2016-09-14 11:55:26 +0100548 *t = std::make_tuple(v1, v2, v3);
549}
550
551template<typename T>
552static inline void EncodeSet(std::vector<uint8_t>* out, const std::set<T>& set) {
553 EncodeUnsignedLeb128(out, set.size());
554 for (const T& entry : set) {
555 EncodeTuple(out, entry);
556 }
557}
558
Andreas Gampea5b09a62016-11-17 15:21:22 -0800559template <typename T>
Nicolas Geoffray08025182016-10-25 17:20:18 +0100560static inline void EncodeUint16Vector(std::vector<uint8_t>* out,
Andreas Gampea5b09a62016-11-17 15:21:22 -0800561 const std::vector<T>& vector) {
Nicolas Geoffray08025182016-10-25 17:20:18 +0100562 EncodeUnsignedLeb128(out, vector.size());
Andreas Gampea5b09a62016-11-17 15:21:22 -0800563 for (const T& entry : vector) {
564 EncodeUnsignedLeb128(out, Encode(entry));
Nicolas Geoffray08025182016-10-25 17:20:18 +0100565 }
566}
567
David Brazdil6f82fbd2016-09-14 11:55:26 +0100568template<typename T>
569static inline void DecodeSet(const uint8_t** in, const uint8_t* end, std::set<T>* set) {
570 DCHECK(set->empty());
571 size_t num_entries = DecodeUint32WithOverflowCheck(in, end);
572 for (size_t i = 0; i < num_entries; ++i) {
573 T tuple;
574 DecodeTuple(in, end, &tuple);
575 set->emplace(tuple);
576 }
577}
578
Andreas Gampea5b09a62016-11-17 15:21:22 -0800579template<typename T>
Nicolas Geoffray08025182016-10-25 17:20:18 +0100580static inline void DecodeUint16Vector(const uint8_t** in,
581 const uint8_t* end,
Andreas Gampea5b09a62016-11-17 15:21:22 -0800582 std::vector<T>* vector) {
Nicolas Geoffray08025182016-10-25 17:20:18 +0100583 DCHECK(vector->empty());
584 size_t num_entries = DecodeUint32WithOverflowCheck(in, end);
585 vector->reserve(num_entries);
586 for (size_t i = 0; i < num_entries; ++i) {
Andreas Gampea5b09a62016-11-17 15:21:22 -0800587 vector->push_back(
588 Decode<T>(dchecked_integral_cast<uint16_t>(DecodeUint32WithOverflowCheck(in, end))));
Nicolas Geoffray08025182016-10-25 17:20:18 +0100589 }
590}
591
David Brazdil6f82fbd2016-09-14 11:55:26 +0100592static inline void EncodeStringVector(std::vector<uint8_t>* out,
593 const std::vector<std::string>& strings) {
594 EncodeUnsignedLeb128(out, strings.size());
595 for (const std::string& str : strings) {
596 const uint8_t* data = reinterpret_cast<const uint8_t*>(str.c_str());
597 size_t length = str.length() + 1;
598 out->insert(out->end(), data, data + length);
599 DCHECK_EQ(0u, out->back());
600 }
601}
602
603static inline void DecodeStringVector(const uint8_t** in,
604 const uint8_t* end,
605 std::vector<std::string>* strings) {
606 DCHECK(strings->empty());
607 size_t num_strings = DecodeUint32WithOverflowCheck(in, end);
608 strings->reserve(num_strings);
609 for (size_t i = 0; i < num_strings; ++i) {
610 CHECK_LT(*in, end);
611 const char* string_start = reinterpret_cast<const char*>(*in);
612 strings->emplace_back(std::string(string_start));
613 *in += strings->back().length() + 1;
614 }
615}
616
Andreas Gampea5b09a62016-11-17 15:21:22 -0800617} // namespace
618
Nicolas Geoffrayd01f60c2016-10-28 14:45:48 +0100619void VerifierDeps::Encode(const std::vector<const DexFile*>& dex_files,
620 std::vector<uint8_t>* buffer) const {
Nicolas Geoffrayd01f60c2016-10-28 14:45:48 +0100621 for (const DexFile* dex_file : dex_files) {
622 const DexFileDeps& deps = *GetDexFileDeps(*dex_file);
623 EncodeStringVector(buffer, deps.strings_);
624 EncodeSet(buffer, deps.assignable_types_);
625 EncodeSet(buffer, deps.unassignable_types_);
626 EncodeSet(buffer, deps.classes_);
627 EncodeSet(buffer, deps.fields_);
628 EncodeSet(buffer, deps.direct_methods_);
629 EncodeSet(buffer, deps.virtual_methods_);
630 EncodeSet(buffer, deps.interface_methods_);
631 EncodeUint16Vector(buffer, deps.unverified_classes_);
David Brazdil6f82fbd2016-09-14 11:55:26 +0100632 }
633}
634
Nicolas Geoffraye70dd562016-10-30 21:03:35 +0000635VerifierDeps::VerifierDeps(const std::vector<const DexFile*>& dex_files,
636 ArrayRef<const uint8_t> data)
David Brazdil6f82fbd2016-09-14 11:55:26 +0100637 : VerifierDeps(dex_files) {
Nicolas Geoffraye70dd562016-10-30 21:03:35 +0000638 if (data.empty()) {
639 // Return eagerly, as the first thing we expect from VerifierDeps data is
640 // the number of created strings, even if there is no dependency.
641 // Currently, only the boot image does not have any VerifierDeps data.
642 return;
643 }
David Brazdil6f82fbd2016-09-14 11:55:26 +0100644 const uint8_t* data_start = data.data();
645 const uint8_t* data_end = data_start + data.size();
Nicolas Geoffrayd01f60c2016-10-28 14:45:48 +0100646 for (const DexFile* dex_file : dex_files) {
647 DexFileDeps* deps = GetDexFileDeps(*dex_file);
648 DecodeStringVector(&data_start, data_end, &deps->strings_);
649 DecodeSet(&data_start, data_end, &deps->assignable_types_);
650 DecodeSet(&data_start, data_end, &deps->unassignable_types_);
651 DecodeSet(&data_start, data_end, &deps->classes_);
652 DecodeSet(&data_start, data_end, &deps->fields_);
653 DecodeSet(&data_start, data_end, &deps->direct_methods_);
654 DecodeSet(&data_start, data_end, &deps->virtual_methods_);
655 DecodeSet(&data_start, data_end, &deps->interface_methods_);
656 DecodeUint16Vector(&data_start, data_end, &deps->unverified_classes_);
David Brazdil6f82fbd2016-09-14 11:55:26 +0100657 }
658 CHECK_LE(data_start, data_end);
659}
660
661bool VerifierDeps::Equals(const VerifierDeps& rhs) const {
David Brazdil6f82fbd2016-09-14 11:55:26 +0100662 if (dex_deps_.size() != rhs.dex_deps_.size()) {
663 return false;
664 }
665
666 auto lhs_it = dex_deps_.begin();
667 auto rhs_it = rhs.dex_deps_.begin();
668
669 for (; (lhs_it != dex_deps_.end()) && (rhs_it != rhs.dex_deps_.end()); lhs_it++, rhs_it++) {
670 const DexFile* lhs_dex_file = lhs_it->first;
671 const DexFile* rhs_dex_file = rhs_it->first;
672 if (lhs_dex_file != rhs_dex_file) {
673 return false;
674 }
675
676 DexFileDeps* lhs_deps = lhs_it->second.get();
677 DexFileDeps* rhs_deps = rhs_it->second.get();
678 if (!lhs_deps->Equals(*rhs_deps)) {
679 return false;
680 }
681 }
682
683 DCHECK((lhs_it == dex_deps_.end()) && (rhs_it == rhs.dex_deps_.end()));
684 return true;
685}
686
687bool VerifierDeps::DexFileDeps::Equals(const VerifierDeps::DexFileDeps& rhs) const {
688 return (strings_ == rhs.strings_) &&
689 (assignable_types_ == rhs.assignable_types_) &&
690 (unassignable_types_ == rhs.unassignable_types_) &&
691 (classes_ == rhs.classes_) &&
692 (fields_ == rhs.fields_) &&
693 (direct_methods_ == rhs.direct_methods_) &&
694 (virtual_methods_ == rhs.virtual_methods_) &&
Nicolas Geoffray08025182016-10-25 17:20:18 +0100695 (interface_methods_ == rhs.interface_methods_) &&
696 (unverified_classes_ == rhs.unverified_classes_);
David Brazdil6f82fbd2016-09-14 11:55:26 +0100697}
698
Nicolas Geoffrayd01f60c2016-10-28 14:45:48 +0100699void VerifierDeps::Dump(VariableIndentationOutputStream* vios) const {
700 for (const auto& dep : dex_deps_) {
701 const DexFile& dex_file = *dep.first;
702 vios->Stream()
703 << "Dependencies of "
704 << dex_file.GetLocation()
705 << ":\n";
706
707 ScopedIndentation indent(vios);
708
709 for (const std::string& str : dep.second->strings_) {
710 vios->Stream() << "Extra string: " << str << "\n";
711 }
712
713 for (const TypeAssignability& entry : dep.second->assignable_types_) {
714 vios->Stream()
715 << GetStringFromId(dex_file, entry.GetSource())
716 << " must be assignable to "
717 << GetStringFromId(dex_file, entry.GetDestination())
718 << "\n";
719 }
720
721 for (const TypeAssignability& entry : dep.second->unassignable_types_) {
722 vios->Stream()
723 << GetStringFromId(dex_file, entry.GetSource())
724 << " must not be assignable to "
725 << GetStringFromId(dex_file, entry.GetDestination())
726 << "\n";
727 }
728
729 for (const ClassResolution& entry : dep.second->classes_) {
730 vios->Stream()
731 << dex_file.StringByTypeIdx(entry.GetDexTypeIndex())
732 << (entry.IsResolved() ? " must be resolved " : "must not be resolved ")
733 << " with access flags " << std::hex << entry.GetAccessFlags() << std::dec
734 << "\n";
735 }
736
737 for (const FieldResolution& entry : dep.second->fields_) {
738 const DexFile::FieldId& field_id = dex_file.GetFieldId(entry.GetDexFieldIndex());
739 vios->Stream()
740 << dex_file.GetFieldDeclaringClassDescriptor(field_id) << "->"
741 << dex_file.GetFieldName(field_id) << ":"
742 << dex_file.GetFieldTypeDescriptor(field_id)
743 << " is expected to be ";
744 if (!entry.IsResolved()) {
745 vios->Stream() << "unresolved\n";
746 } else {
747 vios->Stream()
748 << "in class "
749 << GetStringFromId(dex_file, entry.GetDeclaringClassIndex())
750 << ", and have the access flags " << std::hex << entry.GetAccessFlags() << std::dec
751 << "\n";
752 }
753 }
754
755 for (const auto& entry :
756 { std::make_pair(kDirectMethodResolution, dep.second->direct_methods_),
757 std::make_pair(kVirtualMethodResolution, dep.second->virtual_methods_),
758 std::make_pair(kInterfaceMethodResolution, dep.second->interface_methods_) }) {
759 for (const MethodResolution& method : entry.second) {
760 const DexFile::MethodId& method_id = dex_file.GetMethodId(method.GetDexMethodIndex());
761 vios->Stream()
762 << dex_file.GetMethodDeclaringClassDescriptor(method_id) << "->"
763 << dex_file.GetMethodName(method_id)
764 << dex_file.GetMethodSignature(method_id).ToString()
765 << " is expected to be ";
766 if (!method.IsResolved()) {
767 vios->Stream() << "unresolved\n";
768 } else {
769 vios->Stream()
770 << "in class "
771 << GetStringFromId(dex_file, method.GetDeclaringClassIndex())
772 << ", have the access flags " << std::hex << method.GetAccessFlags() << std::dec
773 << ", and be of kind " << entry.first
774 << "\n";
775 }
776 }
777 }
778
Andreas Gampea5b09a62016-11-17 15:21:22 -0800779 for (dex::TypeIndex type_index : dep.second->unverified_classes_) {
Nicolas Geoffrayd01f60c2016-10-28 14:45:48 +0100780 vios->Stream()
781 << dex_file.StringByTypeIdx(type_index)
782 << " is expected to be verified at runtime\n";
783 }
784 }
785}
786
Nicolas Geoffray6bb7f1b2016-11-03 10:52:49 +0000787bool VerifierDeps::ValidateDependencies(Handle<mirror::ClassLoader> class_loader,
788 Thread* self) const {
Nicolas Geoffray8904b6f2016-10-28 19:50:34 +0100789 for (const auto& entry : dex_deps_) {
790 if (!VerifyDexFile(class_loader, *entry.first, *entry.second, self)) {
791 return false;
792 }
793 }
794 return true;
795}
796
797// TODO: share that helper with other parts of the compiler that have
798// the same lookup pattern.
799static mirror::Class* FindClassAndClearException(ClassLinker* class_linker,
800 Thread* self,
801 const char* name,
802 Handle<mirror::ClassLoader> class_loader)
803 REQUIRES_SHARED(Locks::mutator_lock_) {
804 mirror::Class* result = class_linker->FindClass(self, name, class_loader);
805 if (result == nullptr) {
806 DCHECK(self->IsExceptionPending());
807 self->ClearException();
808 }
809 return result;
810}
811
812bool VerifierDeps::VerifyAssignability(Handle<mirror::ClassLoader> class_loader,
813 const DexFile& dex_file,
814 const std::set<TypeAssignability>& assignables,
815 bool expected_assignability,
816 Thread* self) const {
817 StackHandleScope<2> hs(self);
818 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
819 MutableHandle<mirror::Class> source(hs.NewHandle<mirror::Class>(nullptr));
820 MutableHandle<mirror::Class> destination(hs.NewHandle<mirror::Class>(nullptr));
821
822 for (const auto& entry : assignables) {
823 const std::string& destination_desc = GetStringFromId(dex_file, entry.GetDestination());
824 destination.Assign(
825 FindClassAndClearException(class_linker, self, destination_desc.c_str(), class_loader));
826 const std::string& source_desc = GetStringFromId(dex_file, entry.GetSource());
827 source.Assign(
828 FindClassAndClearException(class_linker, self, source_desc.c_str(), class_loader));
829
830 if (destination.Get() == nullptr) {
831 LOG(INFO) << "VerifiersDeps: Could not resolve class " << destination_desc;
832 return false;
833 }
834
835 if (source.Get() == nullptr) {
836 LOG(INFO) << "VerifierDeps: Could not resolve class " << source_desc;
837 return false;
838 }
839
840 DCHECK(destination->IsResolved() && source->IsResolved());
841 if (destination->IsAssignableFrom(source.Get()) != expected_assignability) {
842 LOG(INFO) << "VerifierDeps: Class "
843 << destination_desc
844 << (expected_assignability ? " not " : " ")
845 << "assignable from "
846 << source_desc;
847 return false;
848 }
849 }
850 return true;
851}
852
853bool VerifierDeps::VerifyClasses(Handle<mirror::ClassLoader> class_loader,
854 const DexFile& dex_file,
855 const std::set<ClassResolution>& classes,
856 Thread* self) const {
857 StackHandleScope<1> hs(self);
858 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
859 MutableHandle<mirror::Class> cls(hs.NewHandle<mirror::Class>(nullptr));
860 for (const auto& entry : classes) {
861 const char* descriptor = dex_file.StringByTypeIdx(entry.GetDexTypeIndex());
862 cls.Assign(FindClassAndClearException(class_linker, self, descriptor, class_loader));
863
864 if (entry.IsResolved()) {
865 if (cls.Get() == nullptr) {
866 LOG(INFO) << "VerifierDeps: Could not resolve class " << descriptor;
867 return false;
868 } else if (entry.GetAccessFlags() != GetAccessFlags(cls.Get())) {
869 LOG(INFO) << "VerifierDeps: Unexpected access flags on class "
870 << descriptor
871 << std::hex
872 << " (expected="
873 << entry.GetAccessFlags()
874 << ", actual="
875 << GetAccessFlags(cls.Get()) << ")"
876 << std::dec;
877 return false;
878 }
879 } else if (cls.Get() != nullptr) {
880 LOG(INFO) << "VerifierDeps: Unexpected successful resolution of class " << descriptor;
881 return false;
882 }
883 }
884 return true;
885}
886
887static std::string GetFieldDescription(const DexFile& dex_file, uint32_t index) {
888 const DexFile::FieldId& field_id = dex_file.GetFieldId(index);
889 return std::string(dex_file.GetFieldDeclaringClassDescriptor(field_id))
890 + "->"
891 + dex_file.GetFieldName(field_id)
892 + ":"
893 + dex_file.GetFieldTypeDescriptor(field_id);
894}
895
896bool VerifierDeps::VerifyFields(Handle<mirror::ClassLoader> class_loader,
897 const DexFile& dex_file,
898 const std::set<FieldResolution>& fields,
899 Thread* self) const {
900 // Check recorded fields are resolved the same way, have the same recorded class,
901 // and have the same recorded flags.
902 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
903 StackHandleScope<1> hs(self);
904 Handle<mirror::DexCache> dex_cache(
905 hs.NewHandle(class_linker->FindDexCache(self, dex_file, /* allow_failure */ false)));
906 for (const auto& entry : fields) {
907 ArtField* field = class_linker->ResolveFieldJLS(
908 dex_file, entry.GetDexFieldIndex(), dex_cache, class_loader);
909
910 if (field == nullptr) {
911 DCHECK(self->IsExceptionPending());
912 self->ClearException();
913 }
914
915 if (entry.IsResolved()) {
916 std::string expected_decl_klass = GetStringFromId(dex_file, entry.GetDeclaringClassIndex());
917 std::string temp;
918 if (field == nullptr) {
919 LOG(INFO) << "VerifierDeps: Could not resolve field "
920 << GetFieldDescription(dex_file, entry.GetDexFieldIndex());
921 return false;
922 } else if (expected_decl_klass != field->GetDeclaringClass()->GetDescriptor(&temp)) {
923 LOG(INFO) << "VerifierDeps: Unexpected declaring class for field resolution "
924 << GetFieldDescription(dex_file, entry.GetDexFieldIndex())
925 << " (expected=" << expected_decl_klass
926 << ", actual=" << field->GetDeclaringClass()->GetDescriptor(&temp) << ")";
927 return false;
928 } else if (entry.GetAccessFlags() != GetAccessFlags(field)) {
929 LOG(INFO) << "VerifierDeps: Unexpected access flags for resolved field "
930 << GetFieldDescription(dex_file, entry.GetDexFieldIndex())
931 << std::hex << " (expected=" << entry.GetAccessFlags()
932 << ", actual=" << GetAccessFlags(field) << ")" << std::dec;
933 return false;
934 }
935 } else if (field != nullptr) {
936 LOG(INFO) << "VerifierDeps: Unexpected successful resolution of field "
937 << GetFieldDescription(dex_file, entry.GetDexFieldIndex());
938 return false;
939 }
940 }
941 return true;
942}
943
944static std::string GetMethodDescription(const DexFile& dex_file, uint32_t index) {
945 const DexFile::MethodId& method_id = dex_file.GetMethodId(index);
946 return std::string(dex_file.GetMethodDeclaringClassDescriptor(method_id))
947 + "->"
948 + dex_file.GetMethodName(method_id)
949 + dex_file.GetMethodSignature(method_id).ToString();
950}
951
952bool VerifierDeps::VerifyMethods(Handle<mirror::ClassLoader> class_loader,
953 const DexFile& dex_file,
954 const std::set<MethodResolution>& methods,
955 MethodResolutionKind kind,
956 Thread* self) const {
957 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
958 PointerSize pointer_size = class_linker->GetImagePointerSize();
959
960 for (const auto& entry : methods) {
961 const DexFile::MethodId& method_id = dex_file.GetMethodId(entry.GetDexMethodIndex());
962
963 const char* name = dex_file.GetMethodName(method_id);
964 const Signature signature = dex_file.GetMethodSignature(method_id);
965 const char* descriptor = dex_file.GetMethodDeclaringClassDescriptor(method_id);
966
967 mirror::Class* cls = FindClassAndClearException(class_linker, self, descriptor, class_loader);
968 if (cls == nullptr) {
969 LOG(INFO) << "VerifierDeps: Could not resolve class " << descriptor;
970 return false;
971 }
972 DCHECK(cls->IsResolved());
973 ArtMethod* method = nullptr;
974 if (kind == kDirectMethodResolution) {
975 method = cls->FindDirectMethod(name, signature, pointer_size);
976 } else if (kind == kVirtualMethodResolution) {
977 method = cls->FindVirtualMethod(name, signature, pointer_size);
978 } else {
979 DCHECK_EQ(kind, kInterfaceMethodResolution);
980 method = cls->FindInterfaceMethod(name, signature, pointer_size);
981 }
982
983 if (entry.IsResolved()) {
984 std::string temp;
985 std::string expected_decl_klass = GetStringFromId(dex_file, entry.GetDeclaringClassIndex());
986 if (method == nullptr) {
987 LOG(INFO) << "VerifierDeps: Could not resolve "
988 << kind
989 << " method "
990 << GetMethodDescription(dex_file, entry.GetDexMethodIndex());
991 return false;
992 } else if (expected_decl_klass != method->GetDeclaringClass()->GetDescriptor(&temp)) {
993 LOG(INFO) << "VerifierDeps: Unexpected declaring class for "
994 << kind
995 << " method resolution "
996 << GetMethodDescription(dex_file, entry.GetDexMethodIndex())
997 << " (expected="
998 << expected_decl_klass
999 << ", actual="
1000 << method->GetDeclaringClass()->GetDescriptor(&temp)
1001 << ")";
1002 return false;
1003 } else if (entry.GetAccessFlags() != GetAccessFlags(method)) {
1004 LOG(INFO) << "VerifierDeps: Unexpected access flags for resolved "
1005 << kind
1006 << " method resolution "
1007 << GetMethodDescription(dex_file, entry.GetDexMethodIndex())
1008 << std::hex
1009 << " (expected="
1010 << entry.GetAccessFlags()
1011 << ", actual="
1012 << GetAccessFlags(method) << ")"
1013 << std::dec;
1014 return false;
1015 }
1016 } else if (method != nullptr) {
1017 LOG(INFO) << "VerifierDeps: Unexpected successful resolution of "
1018 << kind
1019 << " method "
1020 << GetMethodDescription(dex_file, entry.GetDexMethodIndex());
1021 return false;
1022 }
1023 }
1024 return true;
1025}
1026
1027bool VerifierDeps::VerifyDexFile(Handle<mirror::ClassLoader> class_loader,
1028 const DexFile& dex_file,
1029 const DexFileDeps& deps,
1030 Thread* self) const {
1031 bool result = VerifyAssignability(
1032 class_loader, dex_file, deps.assignable_types_, /* expected_assignability */ true, self);
1033 result = result && VerifyAssignability(
1034 class_loader, dex_file, deps.unassignable_types_, /* expected_assignability */ false, self);
1035
1036 result = result && VerifyClasses(class_loader, dex_file, deps.classes_, self);
1037 result = result && VerifyFields(class_loader, dex_file, deps.fields_, self);
1038
1039 result = result && VerifyMethods(
1040 class_loader, dex_file, deps.direct_methods_, kDirectMethodResolution, self);
1041 result = result && VerifyMethods(
1042 class_loader, dex_file, deps.virtual_methods_, kVirtualMethodResolution, self);
1043 result = result && VerifyMethods(
1044 class_loader, dex_file, deps.interface_methods_, kInterfaceMethodResolution, self);
1045
1046 return result;
1047}
1048
David Brazdilca3c8c32016-09-06 14:04:48 +01001049} // namespace verifier
1050} // namespace art