blob: 3af7c013fda4170e9856ee271a6d68fd64e04783 [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
Mathieu Chartier3398c782016-09-30 10:27:43 -0700248 ObjPtr<mirror::DexCache> dex_cache = klass->GetDexCache();
David Brazdilca3c8c32016-09-06 14:04:48 +0100249 if (dex_cache == nullptr) {
250 // This is a synthesized class, in this case always an array. They are not
251 // defined in the compiled DEX files and therefore are part of the classpath.
252 // We could avoid recording dependencies on arrays with component types in
253 // the compiled DEX files but we choose to record them anyway so as to
254 // record the access flags VM sets for array classes.
David Sehr709b0702016-10-13 09:12:37 -0700255 DCHECK(klass->IsArrayClass()) << klass->PrettyDescriptor();
David Brazdilca3c8c32016-09-06 14:04:48 +0100256 return true;
257 }
258
259 const DexFile* dex_file = dex_cache->GetDexFile();
260 DCHECK(dex_file != nullptr);
261
262 // Test if the `dex_deps_` contains an entry for `dex_file`. If not, the dex
263 // file was not registered as being compiled and we assume `klass` is in the
264 // classpath.
265 return (GetDexFileDeps(*dex_file) == nullptr);
266}
267
268void VerifierDeps::AddClassResolution(const DexFile& dex_file,
Andreas Gampea5b09a62016-11-17 15:21:22 -0800269 dex::TypeIndex type_idx,
David Brazdilca3c8c32016-09-06 14:04:48 +0100270 mirror::Class* klass) {
271 DexFileDeps* dex_deps = GetDexFileDeps(dex_file);
272 if (dex_deps == nullptr) {
273 // This invocation is from verification of a dex file which is not being compiled.
274 return;
275 }
276
277 if (klass != nullptr && !IsInClassPath(klass)) {
278 // Class resolved into one of the DEX files which are being compiled.
279 // This is not a classpath dependency.
280 return;
281 }
282
David Brazdilca3c8c32016-09-06 14:04:48 +0100283 dex_deps->classes_.emplace(ClassResolution(type_idx, GetAccessFlags(klass)));
284}
285
286void VerifierDeps::AddFieldResolution(const DexFile& dex_file,
287 uint32_t field_idx,
288 ArtField* field) {
289 DexFileDeps* dex_deps = GetDexFileDeps(dex_file);
290 if (dex_deps == nullptr) {
291 // This invocation is from verification of a dex file which is not being compiled.
292 return;
293 }
294
295 if (field != nullptr && !IsInClassPath(field->GetDeclaringClass())) {
296 // Field resolved into one of the DEX files which are being compiled.
297 // This is not a classpath dependency.
298 return;
299 }
300
Mathieu Chartier32b50302016-11-17 13:08:35 -0800301 dex_deps->fields_.emplace(FieldResolution(field_idx,
302 GetAccessFlags(field),
303 GetFieldDeclaringClassStringId(dex_file,
304 field_idx,
305 field)));
David Brazdilca3c8c32016-09-06 14:04:48 +0100306}
307
308void VerifierDeps::AddMethodResolution(const DexFile& dex_file,
309 uint32_t method_idx,
310 MethodResolutionKind resolution_kind,
311 ArtMethod* method) {
312 DexFileDeps* dex_deps = GetDexFileDeps(dex_file);
313 if (dex_deps == nullptr) {
314 // This invocation is from verification of a dex file which is not being compiled.
315 return;
316 }
317
318 if (method != nullptr && !IsInClassPath(method->GetDeclaringClass())) {
319 // Method resolved into one of the DEX files which are being compiled.
320 // This is not a classpath dependency.
321 return;
322 }
323
David Brazdilca3c8c32016-09-06 14:04:48 +0100324 MethodResolution method_tuple(method_idx,
325 GetAccessFlags(method),
Mathieu Chartier32b50302016-11-17 13:08:35 -0800326 GetMethodDeclaringClassStringId(dex_file, method_idx, method));
David Brazdilca3c8c32016-09-06 14:04:48 +0100327 if (resolution_kind == kDirectMethodResolution) {
328 dex_deps->direct_methods_.emplace(method_tuple);
329 } else if (resolution_kind == kVirtualMethodResolution) {
330 dex_deps->virtual_methods_.emplace(method_tuple);
331 } else {
332 DCHECK_EQ(resolution_kind, kInterfaceMethodResolution);
333 dex_deps->interface_methods_.emplace(method_tuple);
334 }
335}
336
337void VerifierDeps::AddAssignability(const DexFile& dex_file,
338 mirror::Class* destination,
339 mirror::Class* source,
340 bool is_strict,
341 bool is_assignable) {
342 // Test that the method is only called on reference types.
343 // Note that concurrent verification of `destination` and `source` may have
344 // set their status to erroneous. However, the tests performed below rely
345 // merely on no issues with linking (valid access flags, superclass and
346 // implemented interfaces). If the class at any point reached the IsResolved
347 // status, the requirement holds. This is guaranteed by RegTypeCache::ResolveClass.
Nicolas Geoffrayd1665a02016-12-12 13:07:07 +0000348 DCHECK(destination != nullptr);
349 DCHECK(source != nullptr);
350
351 if (destination->IsPrimitive() || source->IsPrimitive()) {
352 // Primitive types are trivially non-assignable to anything else.
353 // We do not need to record trivial assignability, as it will
354 // not change across releases.
355 return;
356 }
David Brazdilca3c8c32016-09-06 14:04:48 +0100357
358 if (destination == source ||
359 destination->IsObjectClass() ||
360 (!is_strict && destination->IsInterface())) {
361 // Cases when `destination` is trivially assignable from `source`.
362 DCHECK(is_assignable);
363 return;
364 }
365
366 DCHECK_EQ(is_assignable, destination->IsAssignableFrom(source));
367
368 if (destination->IsArrayClass() && source->IsArrayClass()) {
369 // Both types are arrays. Break down to component types and add recursively.
370 // This helps filter out destinations from compiled DEX files (see below)
371 // and deduplicate entries with the same canonical component type.
372 mirror::Class* destination_component = destination->GetComponentType();
373 mirror::Class* source_component = source->GetComponentType();
374
375 // Only perform the optimization if both types are resolved which guarantees
376 // that they linked successfully, as required at the top of this method.
377 if (destination_component->IsResolved() && source_component->IsResolved()) {
378 AddAssignability(dex_file,
379 destination_component,
380 source_component,
381 /* is_strict */ true,
382 is_assignable);
383 return;
384 }
385 }
386
387 DexFileDeps* dex_deps = GetDexFileDeps(dex_file);
388 if (dex_deps == nullptr) {
389 // This invocation is from verification of a DEX file which is not being compiled.
390 return;
391 }
392
393 if (!IsInClassPath(destination) && !IsInClassPath(source)) {
394 // Both `destination` and `source` are defined in the compiled DEX files.
395 // No need to record a dependency.
396 return;
397 }
398
David Brazdilca3c8c32016-09-06 14:04:48 +0100399 // Get string IDs for both descriptors and store in the appropriate set.
Andreas Gampe8a0128a2016-11-28 07:38:35 -0800400 dex::StringIndex destination_id = GetClassDescriptorStringId(dex_file, destination);
401 dex::StringIndex source_id = GetClassDescriptorStringId(dex_file, source);
David Brazdilca3c8c32016-09-06 14:04:48 +0100402
403 if (is_assignable) {
404 dex_deps->assignable_types_.emplace(TypeAssignability(destination_id, source_id));
405 } else {
406 dex_deps->unassignable_types_.emplace(TypeAssignability(destination_id, source_id));
407 }
408}
409
Nicolas Geoffray08025182016-10-25 17:20:18 +0100410void VerifierDeps::MaybeRecordVerificationStatus(const DexFile& dex_file,
Andreas Gampea5b09a62016-11-17 15:21:22 -0800411 dex::TypeIndex type_idx,
Nicolas Geoffray08025182016-10-25 17:20:18 +0100412 MethodVerifier::FailureKind failure_kind) {
413 if (failure_kind == MethodVerifier::kNoFailure) {
414 // We only record classes that did not fully verify at compile time.
415 return;
416 }
417
Nicolas Geoffray340dafa2016-11-18 16:03:10 +0000418 VerifierDeps* thread_deps = GetThreadLocalVerifierDeps();
419 if (thread_deps != nullptr) {
420 DexFileDeps* dex_deps = thread_deps->GetDexFileDeps(dex_file);
Nicolas Geoffray08025182016-10-25 17:20:18 +0100421 dex_deps->unverified_classes_.push_back(type_idx);
422 }
423}
424
David Brazdilca3c8c32016-09-06 14:04:48 +0100425void VerifierDeps::MaybeRecordClassResolution(const DexFile& dex_file,
Andreas Gampea5b09a62016-11-17 15:21:22 -0800426 dex::TypeIndex type_idx,
David Brazdilca3c8c32016-09-06 14:04:48 +0100427 mirror::Class* klass) {
Nicolas Geoffray340dafa2016-11-18 16:03:10 +0000428 VerifierDeps* thread_deps = GetThreadLocalVerifierDeps();
429 if (thread_deps != nullptr) {
430 thread_deps->AddClassResolution(dex_file, type_idx, klass);
David Brazdilca3c8c32016-09-06 14:04:48 +0100431 }
432}
433
434void VerifierDeps::MaybeRecordFieldResolution(const DexFile& dex_file,
435 uint32_t field_idx,
436 ArtField* field) {
Nicolas Geoffray340dafa2016-11-18 16:03:10 +0000437 VerifierDeps* thread_deps = GetThreadLocalVerifierDeps();
438 if (thread_deps != nullptr) {
439 thread_deps->AddFieldResolution(dex_file, field_idx, field);
David Brazdilca3c8c32016-09-06 14:04:48 +0100440 }
441}
442
443void VerifierDeps::MaybeRecordMethodResolution(const DexFile& dex_file,
444 uint32_t method_idx,
445 MethodResolutionKind resolution_kind,
446 ArtMethod* method) {
Nicolas Geoffray340dafa2016-11-18 16:03:10 +0000447 VerifierDeps* thread_deps = GetThreadLocalVerifierDeps();
448 if (thread_deps != nullptr) {
449 thread_deps->AddMethodResolution(dex_file, method_idx, resolution_kind, method);
David Brazdilca3c8c32016-09-06 14:04:48 +0100450 }
451}
452
453void VerifierDeps::MaybeRecordAssignability(const DexFile& dex_file,
454 mirror::Class* destination,
455 mirror::Class* source,
456 bool is_strict,
457 bool is_assignable) {
Nicolas Geoffray340dafa2016-11-18 16:03:10 +0000458 VerifierDeps* thread_deps = GetThreadLocalVerifierDeps();
459 if (thread_deps != nullptr) {
460 thread_deps->AddAssignability(dex_file, destination, source, is_strict, is_assignable);
David Brazdilca3c8c32016-09-06 14:04:48 +0100461 }
462}
463
Andreas Gampea5b09a62016-11-17 15:21:22 -0800464namespace {
465
David Brazdil6f82fbd2016-09-14 11:55:26 +0100466static inline uint32_t DecodeUint32WithOverflowCheck(const uint8_t** in, const uint8_t* end) {
467 CHECK_LT(*in, end);
468 return DecodeUnsignedLeb128(in);
469}
470
Andreas Gampea5b09a62016-11-17 15:21:22 -0800471template<typename T> inline uint32_t Encode(T in);
472
473template<> inline uint32_t Encode<uint16_t>(uint16_t in) {
474 return in;
475}
476template<> inline uint32_t Encode<uint32_t>(uint32_t in) {
477 return in;
478}
479template<> inline uint32_t Encode<dex::TypeIndex>(dex::TypeIndex in) {
480 return in.index_;
481}
Andreas Gampe8a0128a2016-11-28 07:38:35 -0800482template<> inline uint32_t Encode<dex::StringIndex>(dex::StringIndex in) {
483 return in.index_;
484}
Andreas Gampea5b09a62016-11-17 15:21:22 -0800485
486template<typename T> inline T Decode(uint32_t in);
487
488template<> inline uint16_t Decode<uint16_t>(uint32_t in) {
489 return dchecked_integral_cast<uint16_t>(in);
490}
491template<> inline uint32_t Decode<uint32_t>(uint32_t in) {
492 return in;
493}
494template<> inline dex::TypeIndex Decode<dex::TypeIndex>(uint32_t in) {
495 return dex::TypeIndex(in);
496}
Andreas Gampe8a0128a2016-11-28 07:38:35 -0800497template<> inline dex::StringIndex Decode<dex::StringIndex>(uint32_t in) {
498 return dex::StringIndex(in);
499}
Andreas Gampea5b09a62016-11-17 15:21:22 -0800500
David Brazdil6f82fbd2016-09-14 11:55:26 +0100501template<typename T1, typename T2>
502static inline void EncodeTuple(std::vector<uint8_t>* out, const std::tuple<T1, T2>& t) {
Andreas Gampea5b09a62016-11-17 15:21:22 -0800503 EncodeUnsignedLeb128(out, Encode(std::get<0>(t)));
504 EncodeUnsignedLeb128(out, Encode(std::get<1>(t)));
David Brazdil6f82fbd2016-09-14 11:55:26 +0100505}
506
507template<typename T1, typename T2>
508static inline void DecodeTuple(const uint8_t** in, const uint8_t* end, std::tuple<T1, T2>* t) {
Andreas Gampea5b09a62016-11-17 15:21:22 -0800509 T1 v1 = Decode<T1>(DecodeUint32WithOverflowCheck(in, end));
510 T2 v2 = Decode<T2>(DecodeUint32WithOverflowCheck(in, end));
David Brazdil6f82fbd2016-09-14 11:55:26 +0100511 *t = std::make_tuple(v1, v2);
512}
513
514template<typename T1, typename T2, typename T3>
515static inline void EncodeTuple(std::vector<uint8_t>* out, const std::tuple<T1, T2, T3>& t) {
Andreas Gampea5b09a62016-11-17 15:21:22 -0800516 EncodeUnsignedLeb128(out, Encode(std::get<0>(t)));
517 EncodeUnsignedLeb128(out, Encode(std::get<1>(t)));
518 EncodeUnsignedLeb128(out, Encode(std::get<2>(t)));
David Brazdil6f82fbd2016-09-14 11:55:26 +0100519}
520
521template<typename T1, typename T2, typename T3>
522static inline void DecodeTuple(const uint8_t** in, const uint8_t* end, std::tuple<T1, T2, T3>* t) {
Andreas Gampea5b09a62016-11-17 15:21:22 -0800523 T1 v1 = Decode<T1>(DecodeUint32WithOverflowCheck(in, end));
524 T2 v2 = Decode<T2>(DecodeUint32WithOverflowCheck(in, end));
Andreas Gampe8a0128a2016-11-28 07:38:35 -0800525 T3 v3 = Decode<T3>(DecodeUint32WithOverflowCheck(in, end));
David Brazdil6f82fbd2016-09-14 11:55:26 +0100526 *t = std::make_tuple(v1, v2, v3);
527}
528
529template<typename T>
530static inline void EncodeSet(std::vector<uint8_t>* out, const std::set<T>& set) {
531 EncodeUnsignedLeb128(out, set.size());
532 for (const T& entry : set) {
533 EncodeTuple(out, entry);
534 }
535}
536
Andreas Gampea5b09a62016-11-17 15:21:22 -0800537template <typename T>
Nicolas Geoffray08025182016-10-25 17:20:18 +0100538static inline void EncodeUint16Vector(std::vector<uint8_t>* out,
Andreas Gampea5b09a62016-11-17 15:21:22 -0800539 const std::vector<T>& vector) {
Nicolas Geoffray08025182016-10-25 17:20:18 +0100540 EncodeUnsignedLeb128(out, vector.size());
Andreas Gampea5b09a62016-11-17 15:21:22 -0800541 for (const T& entry : vector) {
542 EncodeUnsignedLeb128(out, Encode(entry));
Nicolas Geoffray08025182016-10-25 17:20:18 +0100543 }
544}
545
David Brazdil6f82fbd2016-09-14 11:55:26 +0100546template<typename T>
547static inline void DecodeSet(const uint8_t** in, const uint8_t* end, std::set<T>* set) {
548 DCHECK(set->empty());
549 size_t num_entries = DecodeUint32WithOverflowCheck(in, end);
550 for (size_t i = 0; i < num_entries; ++i) {
551 T tuple;
552 DecodeTuple(in, end, &tuple);
553 set->emplace(tuple);
554 }
555}
556
Andreas Gampea5b09a62016-11-17 15:21:22 -0800557template<typename T>
Nicolas Geoffray08025182016-10-25 17:20:18 +0100558static inline void DecodeUint16Vector(const uint8_t** in,
559 const uint8_t* end,
Andreas Gampea5b09a62016-11-17 15:21:22 -0800560 std::vector<T>* vector) {
Nicolas Geoffray08025182016-10-25 17:20:18 +0100561 DCHECK(vector->empty());
562 size_t num_entries = DecodeUint32WithOverflowCheck(in, end);
563 vector->reserve(num_entries);
564 for (size_t i = 0; i < num_entries; ++i) {
Andreas Gampea5b09a62016-11-17 15:21:22 -0800565 vector->push_back(
566 Decode<T>(dchecked_integral_cast<uint16_t>(DecodeUint32WithOverflowCheck(in, end))));
Nicolas Geoffray08025182016-10-25 17:20:18 +0100567 }
568}
569
David Brazdil6f82fbd2016-09-14 11:55:26 +0100570static inline void EncodeStringVector(std::vector<uint8_t>* out,
571 const std::vector<std::string>& strings) {
572 EncodeUnsignedLeb128(out, strings.size());
573 for (const std::string& str : strings) {
574 const uint8_t* data = reinterpret_cast<const uint8_t*>(str.c_str());
575 size_t length = str.length() + 1;
576 out->insert(out->end(), data, data + length);
577 DCHECK_EQ(0u, out->back());
578 }
579}
580
581static inline void DecodeStringVector(const uint8_t** in,
582 const uint8_t* end,
583 std::vector<std::string>* strings) {
584 DCHECK(strings->empty());
585 size_t num_strings = DecodeUint32WithOverflowCheck(in, end);
586 strings->reserve(num_strings);
587 for (size_t i = 0; i < num_strings; ++i) {
588 CHECK_LT(*in, end);
589 const char* string_start = reinterpret_cast<const char*>(*in);
590 strings->emplace_back(std::string(string_start));
591 *in += strings->back().length() + 1;
592 }
593}
594
Andreas Gampea5b09a62016-11-17 15:21:22 -0800595} // namespace
596
Nicolas Geoffrayd01f60c2016-10-28 14:45:48 +0100597void VerifierDeps::Encode(const std::vector<const DexFile*>& dex_files,
598 std::vector<uint8_t>* buffer) const {
Nicolas Geoffrayd01f60c2016-10-28 14:45:48 +0100599 for (const DexFile* dex_file : dex_files) {
600 const DexFileDeps& deps = *GetDexFileDeps(*dex_file);
601 EncodeStringVector(buffer, deps.strings_);
602 EncodeSet(buffer, deps.assignable_types_);
603 EncodeSet(buffer, deps.unassignable_types_);
604 EncodeSet(buffer, deps.classes_);
605 EncodeSet(buffer, deps.fields_);
606 EncodeSet(buffer, deps.direct_methods_);
607 EncodeSet(buffer, deps.virtual_methods_);
608 EncodeSet(buffer, deps.interface_methods_);
609 EncodeUint16Vector(buffer, deps.unverified_classes_);
David Brazdil6f82fbd2016-09-14 11:55:26 +0100610 }
611}
612
Nicolas Geoffraye70dd562016-10-30 21:03:35 +0000613VerifierDeps::VerifierDeps(const std::vector<const DexFile*>& dex_files,
614 ArrayRef<const uint8_t> data)
David Brazdil6f82fbd2016-09-14 11:55:26 +0100615 : VerifierDeps(dex_files) {
Nicolas Geoffraye70dd562016-10-30 21:03:35 +0000616 if (data.empty()) {
617 // Return eagerly, as the first thing we expect from VerifierDeps data is
618 // the number of created strings, even if there is no dependency.
619 // Currently, only the boot image does not have any VerifierDeps data.
620 return;
621 }
David Brazdil6f82fbd2016-09-14 11:55:26 +0100622 const uint8_t* data_start = data.data();
623 const uint8_t* data_end = data_start + data.size();
Nicolas Geoffrayd01f60c2016-10-28 14:45:48 +0100624 for (const DexFile* dex_file : dex_files) {
625 DexFileDeps* deps = GetDexFileDeps(*dex_file);
626 DecodeStringVector(&data_start, data_end, &deps->strings_);
627 DecodeSet(&data_start, data_end, &deps->assignable_types_);
628 DecodeSet(&data_start, data_end, &deps->unassignable_types_);
629 DecodeSet(&data_start, data_end, &deps->classes_);
630 DecodeSet(&data_start, data_end, &deps->fields_);
631 DecodeSet(&data_start, data_end, &deps->direct_methods_);
632 DecodeSet(&data_start, data_end, &deps->virtual_methods_);
633 DecodeSet(&data_start, data_end, &deps->interface_methods_);
634 DecodeUint16Vector(&data_start, data_end, &deps->unverified_classes_);
David Brazdil6f82fbd2016-09-14 11:55:26 +0100635 }
636 CHECK_LE(data_start, data_end);
637}
638
639bool VerifierDeps::Equals(const VerifierDeps& rhs) const {
David Brazdil6f82fbd2016-09-14 11:55:26 +0100640 if (dex_deps_.size() != rhs.dex_deps_.size()) {
641 return false;
642 }
643
644 auto lhs_it = dex_deps_.begin();
645 auto rhs_it = rhs.dex_deps_.begin();
646
647 for (; (lhs_it != dex_deps_.end()) && (rhs_it != rhs.dex_deps_.end()); lhs_it++, rhs_it++) {
648 const DexFile* lhs_dex_file = lhs_it->first;
649 const DexFile* rhs_dex_file = rhs_it->first;
650 if (lhs_dex_file != rhs_dex_file) {
651 return false;
652 }
653
654 DexFileDeps* lhs_deps = lhs_it->second.get();
655 DexFileDeps* rhs_deps = rhs_it->second.get();
656 if (!lhs_deps->Equals(*rhs_deps)) {
657 return false;
658 }
659 }
660
661 DCHECK((lhs_it == dex_deps_.end()) && (rhs_it == rhs.dex_deps_.end()));
662 return true;
663}
664
665bool VerifierDeps::DexFileDeps::Equals(const VerifierDeps::DexFileDeps& rhs) const {
666 return (strings_ == rhs.strings_) &&
667 (assignable_types_ == rhs.assignable_types_) &&
668 (unassignable_types_ == rhs.unassignable_types_) &&
669 (classes_ == rhs.classes_) &&
670 (fields_ == rhs.fields_) &&
671 (direct_methods_ == rhs.direct_methods_) &&
672 (virtual_methods_ == rhs.virtual_methods_) &&
Nicolas Geoffray08025182016-10-25 17:20:18 +0100673 (interface_methods_ == rhs.interface_methods_) &&
674 (unverified_classes_ == rhs.unverified_classes_);
David Brazdil6f82fbd2016-09-14 11:55:26 +0100675}
676
Nicolas Geoffrayd01f60c2016-10-28 14:45:48 +0100677void VerifierDeps::Dump(VariableIndentationOutputStream* vios) const {
678 for (const auto& dep : dex_deps_) {
679 const DexFile& dex_file = *dep.first;
680 vios->Stream()
681 << "Dependencies of "
682 << dex_file.GetLocation()
683 << ":\n";
684
685 ScopedIndentation indent(vios);
686
687 for (const std::string& str : dep.second->strings_) {
688 vios->Stream() << "Extra string: " << str << "\n";
689 }
690
691 for (const TypeAssignability& entry : dep.second->assignable_types_) {
692 vios->Stream()
693 << GetStringFromId(dex_file, entry.GetSource())
694 << " must be assignable to "
695 << GetStringFromId(dex_file, entry.GetDestination())
696 << "\n";
697 }
698
699 for (const TypeAssignability& entry : dep.second->unassignable_types_) {
700 vios->Stream()
701 << GetStringFromId(dex_file, entry.GetSource())
702 << " must not be assignable to "
703 << GetStringFromId(dex_file, entry.GetDestination())
704 << "\n";
705 }
706
707 for (const ClassResolution& entry : dep.second->classes_) {
708 vios->Stream()
709 << dex_file.StringByTypeIdx(entry.GetDexTypeIndex())
710 << (entry.IsResolved() ? " must be resolved " : "must not be resolved ")
711 << " with access flags " << std::hex << entry.GetAccessFlags() << std::dec
712 << "\n";
713 }
714
715 for (const FieldResolution& entry : dep.second->fields_) {
716 const DexFile::FieldId& field_id = dex_file.GetFieldId(entry.GetDexFieldIndex());
717 vios->Stream()
718 << dex_file.GetFieldDeclaringClassDescriptor(field_id) << "->"
719 << dex_file.GetFieldName(field_id) << ":"
720 << dex_file.GetFieldTypeDescriptor(field_id)
721 << " is expected to be ";
722 if (!entry.IsResolved()) {
723 vios->Stream() << "unresolved\n";
724 } else {
725 vios->Stream()
726 << "in class "
727 << GetStringFromId(dex_file, entry.GetDeclaringClassIndex())
728 << ", and have the access flags " << std::hex << entry.GetAccessFlags() << std::dec
729 << "\n";
730 }
731 }
732
733 for (const auto& entry :
734 { std::make_pair(kDirectMethodResolution, dep.second->direct_methods_),
735 std::make_pair(kVirtualMethodResolution, dep.second->virtual_methods_),
736 std::make_pair(kInterfaceMethodResolution, dep.second->interface_methods_) }) {
737 for (const MethodResolution& method : entry.second) {
738 const DexFile::MethodId& method_id = dex_file.GetMethodId(method.GetDexMethodIndex());
739 vios->Stream()
740 << dex_file.GetMethodDeclaringClassDescriptor(method_id) << "->"
741 << dex_file.GetMethodName(method_id)
742 << dex_file.GetMethodSignature(method_id).ToString()
743 << " is expected to be ";
744 if (!method.IsResolved()) {
745 vios->Stream() << "unresolved\n";
746 } else {
747 vios->Stream()
748 << "in class "
749 << GetStringFromId(dex_file, method.GetDeclaringClassIndex())
750 << ", have the access flags " << std::hex << method.GetAccessFlags() << std::dec
751 << ", and be of kind " << entry.first
752 << "\n";
753 }
754 }
755 }
756
Andreas Gampea5b09a62016-11-17 15:21:22 -0800757 for (dex::TypeIndex type_index : dep.second->unverified_classes_) {
Nicolas Geoffrayd01f60c2016-10-28 14:45:48 +0100758 vios->Stream()
759 << dex_file.StringByTypeIdx(type_index)
760 << " is expected to be verified at runtime\n";
761 }
762 }
763}
764
Nicolas Geoffray6bb7f1b2016-11-03 10:52:49 +0000765bool VerifierDeps::ValidateDependencies(Handle<mirror::ClassLoader> class_loader,
766 Thread* self) const {
Nicolas Geoffray8904b6f2016-10-28 19:50:34 +0100767 for (const auto& entry : dex_deps_) {
768 if (!VerifyDexFile(class_loader, *entry.first, *entry.second, self)) {
769 return false;
770 }
771 }
772 return true;
773}
774
775// TODO: share that helper with other parts of the compiler that have
776// the same lookup pattern.
777static mirror::Class* FindClassAndClearException(ClassLinker* class_linker,
778 Thread* self,
779 const char* name,
780 Handle<mirror::ClassLoader> class_loader)
781 REQUIRES_SHARED(Locks::mutator_lock_) {
782 mirror::Class* result = class_linker->FindClass(self, name, class_loader);
783 if (result == nullptr) {
784 DCHECK(self->IsExceptionPending());
785 self->ClearException();
786 }
787 return result;
788}
789
790bool VerifierDeps::VerifyAssignability(Handle<mirror::ClassLoader> class_loader,
791 const DexFile& dex_file,
792 const std::set<TypeAssignability>& assignables,
793 bool expected_assignability,
794 Thread* self) const {
795 StackHandleScope<2> hs(self);
796 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
797 MutableHandle<mirror::Class> source(hs.NewHandle<mirror::Class>(nullptr));
798 MutableHandle<mirror::Class> destination(hs.NewHandle<mirror::Class>(nullptr));
799
800 for (const auto& entry : assignables) {
801 const std::string& destination_desc = GetStringFromId(dex_file, entry.GetDestination());
802 destination.Assign(
803 FindClassAndClearException(class_linker, self, destination_desc.c_str(), class_loader));
804 const std::string& source_desc = GetStringFromId(dex_file, entry.GetSource());
805 source.Assign(
806 FindClassAndClearException(class_linker, self, source_desc.c_str(), class_loader));
807
808 if (destination.Get() == nullptr) {
809 LOG(INFO) << "VerifiersDeps: Could not resolve class " << destination_desc;
810 return false;
811 }
812
813 if (source.Get() == nullptr) {
814 LOG(INFO) << "VerifierDeps: Could not resolve class " << source_desc;
815 return false;
816 }
817
818 DCHECK(destination->IsResolved() && source->IsResolved());
819 if (destination->IsAssignableFrom(source.Get()) != expected_assignability) {
820 LOG(INFO) << "VerifierDeps: Class "
821 << destination_desc
822 << (expected_assignability ? " not " : " ")
823 << "assignable from "
824 << source_desc;
825 return false;
826 }
827 }
828 return true;
829}
830
831bool VerifierDeps::VerifyClasses(Handle<mirror::ClassLoader> class_loader,
832 const DexFile& dex_file,
833 const std::set<ClassResolution>& classes,
834 Thread* self) const {
835 StackHandleScope<1> hs(self);
836 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
837 MutableHandle<mirror::Class> cls(hs.NewHandle<mirror::Class>(nullptr));
838 for (const auto& entry : classes) {
839 const char* descriptor = dex_file.StringByTypeIdx(entry.GetDexTypeIndex());
840 cls.Assign(FindClassAndClearException(class_linker, self, descriptor, class_loader));
841
842 if (entry.IsResolved()) {
843 if (cls.Get() == nullptr) {
844 LOG(INFO) << "VerifierDeps: Could not resolve class " << descriptor;
845 return false;
846 } else if (entry.GetAccessFlags() != GetAccessFlags(cls.Get())) {
847 LOG(INFO) << "VerifierDeps: Unexpected access flags on class "
848 << descriptor
849 << std::hex
850 << " (expected="
851 << entry.GetAccessFlags()
852 << ", actual="
853 << GetAccessFlags(cls.Get()) << ")"
854 << std::dec;
855 return false;
856 }
857 } else if (cls.Get() != nullptr) {
858 LOG(INFO) << "VerifierDeps: Unexpected successful resolution of class " << descriptor;
859 return false;
860 }
861 }
862 return true;
863}
864
865static std::string GetFieldDescription(const DexFile& dex_file, uint32_t index) {
866 const DexFile::FieldId& field_id = dex_file.GetFieldId(index);
867 return std::string(dex_file.GetFieldDeclaringClassDescriptor(field_id))
868 + "->"
869 + dex_file.GetFieldName(field_id)
870 + ":"
871 + dex_file.GetFieldTypeDescriptor(field_id);
872}
873
874bool VerifierDeps::VerifyFields(Handle<mirror::ClassLoader> class_loader,
875 const DexFile& dex_file,
876 const std::set<FieldResolution>& fields,
877 Thread* self) const {
878 // Check recorded fields are resolved the same way, have the same recorded class,
879 // and have the same recorded flags.
880 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
881 StackHandleScope<1> hs(self);
882 Handle<mirror::DexCache> dex_cache(
883 hs.NewHandle(class_linker->FindDexCache(self, dex_file, /* allow_failure */ false)));
884 for (const auto& entry : fields) {
885 ArtField* field = class_linker->ResolveFieldJLS(
886 dex_file, entry.GetDexFieldIndex(), dex_cache, class_loader);
887
888 if (field == nullptr) {
889 DCHECK(self->IsExceptionPending());
890 self->ClearException();
891 }
892
893 if (entry.IsResolved()) {
894 std::string expected_decl_klass = GetStringFromId(dex_file, entry.GetDeclaringClassIndex());
895 std::string temp;
896 if (field == nullptr) {
897 LOG(INFO) << "VerifierDeps: Could not resolve field "
898 << GetFieldDescription(dex_file, entry.GetDexFieldIndex());
899 return false;
900 } else if (expected_decl_klass != field->GetDeclaringClass()->GetDescriptor(&temp)) {
901 LOG(INFO) << "VerifierDeps: Unexpected declaring class for field resolution "
902 << GetFieldDescription(dex_file, entry.GetDexFieldIndex())
903 << " (expected=" << expected_decl_klass
904 << ", actual=" << field->GetDeclaringClass()->GetDescriptor(&temp) << ")";
905 return false;
906 } else if (entry.GetAccessFlags() != GetAccessFlags(field)) {
907 LOG(INFO) << "VerifierDeps: Unexpected access flags for resolved field "
908 << GetFieldDescription(dex_file, entry.GetDexFieldIndex())
909 << std::hex << " (expected=" << entry.GetAccessFlags()
910 << ", actual=" << GetAccessFlags(field) << ")" << std::dec;
911 return false;
912 }
913 } else if (field != nullptr) {
914 LOG(INFO) << "VerifierDeps: Unexpected successful resolution of field "
915 << GetFieldDescription(dex_file, entry.GetDexFieldIndex());
916 return false;
917 }
918 }
919 return true;
920}
921
922static std::string GetMethodDescription(const DexFile& dex_file, uint32_t index) {
923 const DexFile::MethodId& method_id = dex_file.GetMethodId(index);
924 return std::string(dex_file.GetMethodDeclaringClassDescriptor(method_id))
925 + "->"
926 + dex_file.GetMethodName(method_id)
927 + dex_file.GetMethodSignature(method_id).ToString();
928}
929
930bool VerifierDeps::VerifyMethods(Handle<mirror::ClassLoader> class_loader,
931 const DexFile& dex_file,
932 const std::set<MethodResolution>& methods,
933 MethodResolutionKind kind,
934 Thread* self) const {
935 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
936 PointerSize pointer_size = class_linker->GetImagePointerSize();
937
938 for (const auto& entry : methods) {
939 const DexFile::MethodId& method_id = dex_file.GetMethodId(entry.GetDexMethodIndex());
940
941 const char* name = dex_file.GetMethodName(method_id);
942 const Signature signature = dex_file.GetMethodSignature(method_id);
943 const char* descriptor = dex_file.GetMethodDeclaringClassDescriptor(method_id);
944
945 mirror::Class* cls = FindClassAndClearException(class_linker, self, descriptor, class_loader);
946 if (cls == nullptr) {
947 LOG(INFO) << "VerifierDeps: Could not resolve class " << descriptor;
948 return false;
949 }
950 DCHECK(cls->IsResolved());
951 ArtMethod* method = nullptr;
952 if (kind == kDirectMethodResolution) {
953 method = cls->FindDirectMethod(name, signature, pointer_size);
954 } else if (kind == kVirtualMethodResolution) {
955 method = cls->FindVirtualMethod(name, signature, pointer_size);
956 } else {
957 DCHECK_EQ(kind, kInterfaceMethodResolution);
958 method = cls->FindInterfaceMethod(name, signature, pointer_size);
959 }
960
961 if (entry.IsResolved()) {
962 std::string temp;
963 std::string expected_decl_klass = GetStringFromId(dex_file, entry.GetDeclaringClassIndex());
964 if (method == nullptr) {
965 LOG(INFO) << "VerifierDeps: Could not resolve "
966 << kind
967 << " method "
968 << GetMethodDescription(dex_file, entry.GetDexMethodIndex());
969 return false;
970 } else if (expected_decl_klass != method->GetDeclaringClass()->GetDescriptor(&temp)) {
971 LOG(INFO) << "VerifierDeps: Unexpected declaring class for "
972 << kind
973 << " method resolution "
974 << GetMethodDescription(dex_file, entry.GetDexMethodIndex())
975 << " (expected="
976 << expected_decl_klass
977 << ", actual="
978 << method->GetDeclaringClass()->GetDescriptor(&temp)
979 << ")";
980 return false;
981 } else if (entry.GetAccessFlags() != GetAccessFlags(method)) {
982 LOG(INFO) << "VerifierDeps: Unexpected access flags for resolved "
983 << kind
984 << " method resolution "
985 << GetMethodDescription(dex_file, entry.GetDexMethodIndex())
986 << std::hex
987 << " (expected="
988 << entry.GetAccessFlags()
989 << ", actual="
990 << GetAccessFlags(method) << ")"
991 << std::dec;
992 return false;
993 }
994 } else if (method != nullptr) {
995 LOG(INFO) << "VerifierDeps: Unexpected successful resolution of "
996 << kind
997 << " method "
998 << GetMethodDescription(dex_file, entry.GetDexMethodIndex());
999 return false;
1000 }
1001 }
1002 return true;
1003}
1004
1005bool VerifierDeps::VerifyDexFile(Handle<mirror::ClassLoader> class_loader,
1006 const DexFile& dex_file,
1007 const DexFileDeps& deps,
1008 Thread* self) const {
1009 bool result = VerifyAssignability(
1010 class_loader, dex_file, deps.assignable_types_, /* expected_assignability */ true, self);
1011 result = result && VerifyAssignability(
1012 class_loader, dex_file, deps.unassignable_types_, /* expected_assignability */ false, self);
1013
1014 result = result && VerifyClasses(class_loader, dex_file, deps.classes_, self);
1015 result = result && VerifyFields(class_loader, dex_file, deps.fields_, self);
1016
1017 result = result && VerifyMethods(
1018 class_loader, dex_file, deps.direct_methods_, kDirectMethodResolution, self);
1019 result = result && VerifyMethods(
1020 class_loader, dex_file, deps.virtual_methods_, kVirtualMethodResolution, self);
1021 result = result && VerifyMethods(
1022 class_loader, dex_file, deps.interface_methods_, kInterfaceMethodResolution, self);
1023
1024 return result;
1025}
1026
David Brazdilca3c8c32016-09-06 14:04:48 +01001027} // namespace verifier
1028} // namespace art