blob: 4cebb7b91583708fa2c32eee508dd64db1d2378f [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
Nicolas Geoffray119e8462016-12-21 10:29:43 +0000358 if (source->IsObjectClass() && !is_assignable) {
359 // j.l.Object is trivially non-assignable to other types, don't
360 // record it.
361 return;
362 }
363
David Brazdilca3c8c32016-09-06 14:04:48 +0100364 if (destination == source ||
365 destination->IsObjectClass() ||
366 (!is_strict && destination->IsInterface())) {
367 // Cases when `destination` is trivially assignable from `source`.
368 DCHECK(is_assignable);
369 return;
370 }
371
372 DCHECK_EQ(is_assignable, destination->IsAssignableFrom(source));
373
374 if (destination->IsArrayClass() && source->IsArrayClass()) {
375 // Both types are arrays. Break down to component types and add recursively.
376 // This helps filter out destinations from compiled DEX files (see below)
377 // and deduplicate entries with the same canonical component type.
378 mirror::Class* destination_component = destination->GetComponentType();
379 mirror::Class* source_component = source->GetComponentType();
380
381 // Only perform the optimization if both types are resolved which guarantees
382 // that they linked successfully, as required at the top of this method.
383 if (destination_component->IsResolved() && source_component->IsResolved()) {
384 AddAssignability(dex_file,
385 destination_component,
386 source_component,
387 /* is_strict */ true,
388 is_assignable);
389 return;
390 }
391 }
392
393 DexFileDeps* dex_deps = GetDexFileDeps(dex_file);
394 if (dex_deps == nullptr) {
395 // This invocation is from verification of a DEX file which is not being compiled.
396 return;
397 }
398
399 if (!IsInClassPath(destination) && !IsInClassPath(source)) {
400 // Both `destination` and `source` are defined in the compiled DEX files.
401 // No need to record a dependency.
402 return;
403 }
404
Nicolas Geoffray119e8462016-12-21 10:29:43 +0000405 if (!IsInClassPath(source) && !source->IsInterface() && !destination->IsInterface()) {
406 // Find the super class at the classpath boundary. Only that class
407 // can change the assignability.
408 // TODO: also chase the boundary for interfaces.
409 do {
410 source = source->GetSuperClass();
411 } while (!IsInClassPath(source));
412
413 // If that class is the actual destination, no need to record it.
414 if (source == destination) {
415 return;
416 }
417 }
418
419
David Brazdilca3c8c32016-09-06 14:04:48 +0100420 // Get string IDs for both descriptors and store in the appropriate set.
Andreas Gampe8a0128a2016-11-28 07:38:35 -0800421 dex::StringIndex destination_id = GetClassDescriptorStringId(dex_file, destination);
422 dex::StringIndex source_id = GetClassDescriptorStringId(dex_file, source);
David Brazdilca3c8c32016-09-06 14:04:48 +0100423
424 if (is_assignable) {
425 dex_deps->assignable_types_.emplace(TypeAssignability(destination_id, source_id));
426 } else {
427 dex_deps->unassignable_types_.emplace(TypeAssignability(destination_id, source_id));
428 }
429}
430
Nicolas Geoffray08025182016-10-25 17:20:18 +0100431void VerifierDeps::MaybeRecordVerificationStatus(const DexFile& dex_file,
Andreas Gampea5b09a62016-11-17 15:21:22 -0800432 dex::TypeIndex type_idx,
Nicolas Geoffray08025182016-10-25 17:20:18 +0100433 MethodVerifier::FailureKind failure_kind) {
434 if (failure_kind == MethodVerifier::kNoFailure) {
435 // We only record classes that did not fully verify at compile time.
436 return;
437 }
438
Nicolas Geoffray340dafa2016-11-18 16:03:10 +0000439 VerifierDeps* thread_deps = GetThreadLocalVerifierDeps();
440 if (thread_deps != nullptr) {
441 DexFileDeps* dex_deps = thread_deps->GetDexFileDeps(dex_file);
Nicolas Geoffray08025182016-10-25 17:20:18 +0100442 dex_deps->unverified_classes_.push_back(type_idx);
443 }
444}
445
David Brazdilca3c8c32016-09-06 14:04:48 +0100446void VerifierDeps::MaybeRecordClassResolution(const DexFile& dex_file,
Andreas Gampea5b09a62016-11-17 15:21:22 -0800447 dex::TypeIndex type_idx,
David Brazdilca3c8c32016-09-06 14:04:48 +0100448 mirror::Class* klass) {
Nicolas Geoffray340dafa2016-11-18 16:03:10 +0000449 VerifierDeps* thread_deps = GetThreadLocalVerifierDeps();
450 if (thread_deps != nullptr) {
451 thread_deps->AddClassResolution(dex_file, type_idx, klass);
David Brazdilca3c8c32016-09-06 14:04:48 +0100452 }
453}
454
455void VerifierDeps::MaybeRecordFieldResolution(const DexFile& dex_file,
456 uint32_t field_idx,
457 ArtField* field) {
Nicolas Geoffray340dafa2016-11-18 16:03:10 +0000458 VerifierDeps* thread_deps = GetThreadLocalVerifierDeps();
459 if (thread_deps != nullptr) {
460 thread_deps->AddFieldResolution(dex_file, field_idx, field);
David Brazdilca3c8c32016-09-06 14:04:48 +0100461 }
462}
463
464void VerifierDeps::MaybeRecordMethodResolution(const DexFile& dex_file,
465 uint32_t method_idx,
466 MethodResolutionKind resolution_kind,
467 ArtMethod* method) {
Nicolas Geoffray340dafa2016-11-18 16:03:10 +0000468 VerifierDeps* thread_deps = GetThreadLocalVerifierDeps();
469 if (thread_deps != nullptr) {
470 thread_deps->AddMethodResolution(dex_file, method_idx, resolution_kind, method);
David Brazdilca3c8c32016-09-06 14:04:48 +0100471 }
472}
473
474void VerifierDeps::MaybeRecordAssignability(const DexFile& dex_file,
475 mirror::Class* destination,
476 mirror::Class* source,
477 bool is_strict,
478 bool is_assignable) {
Nicolas Geoffray340dafa2016-11-18 16:03:10 +0000479 VerifierDeps* thread_deps = GetThreadLocalVerifierDeps();
480 if (thread_deps != nullptr) {
481 thread_deps->AddAssignability(dex_file, destination, source, is_strict, is_assignable);
David Brazdilca3c8c32016-09-06 14:04:48 +0100482 }
483}
484
Andreas Gampea5b09a62016-11-17 15:21:22 -0800485namespace {
486
David Brazdil6f82fbd2016-09-14 11:55:26 +0100487static inline uint32_t DecodeUint32WithOverflowCheck(const uint8_t** in, const uint8_t* end) {
488 CHECK_LT(*in, end);
489 return DecodeUnsignedLeb128(in);
490}
491
Andreas Gampea5b09a62016-11-17 15:21:22 -0800492template<typename T> inline uint32_t Encode(T in);
493
494template<> inline uint32_t Encode<uint16_t>(uint16_t in) {
495 return in;
496}
497template<> inline uint32_t Encode<uint32_t>(uint32_t in) {
498 return in;
499}
500template<> inline uint32_t Encode<dex::TypeIndex>(dex::TypeIndex in) {
501 return in.index_;
502}
Andreas Gampe8a0128a2016-11-28 07:38:35 -0800503template<> inline uint32_t Encode<dex::StringIndex>(dex::StringIndex in) {
504 return in.index_;
505}
Andreas Gampea5b09a62016-11-17 15:21:22 -0800506
507template<typename T> inline T Decode(uint32_t in);
508
509template<> inline uint16_t Decode<uint16_t>(uint32_t in) {
510 return dchecked_integral_cast<uint16_t>(in);
511}
512template<> inline uint32_t Decode<uint32_t>(uint32_t in) {
513 return in;
514}
515template<> inline dex::TypeIndex Decode<dex::TypeIndex>(uint32_t in) {
516 return dex::TypeIndex(in);
517}
Andreas Gampe8a0128a2016-11-28 07:38:35 -0800518template<> inline dex::StringIndex Decode<dex::StringIndex>(uint32_t in) {
519 return dex::StringIndex(in);
520}
Andreas Gampea5b09a62016-11-17 15:21:22 -0800521
David Brazdil6f82fbd2016-09-14 11:55:26 +0100522template<typename T1, typename T2>
523static inline void EncodeTuple(std::vector<uint8_t>* out, const std::tuple<T1, T2>& t) {
Andreas Gampea5b09a62016-11-17 15:21:22 -0800524 EncodeUnsignedLeb128(out, Encode(std::get<0>(t)));
525 EncodeUnsignedLeb128(out, Encode(std::get<1>(t)));
David Brazdil6f82fbd2016-09-14 11:55:26 +0100526}
527
528template<typename T1, typename T2>
529static inline void DecodeTuple(const uint8_t** in, const uint8_t* end, std::tuple<T1, T2>* t) {
Andreas Gampea5b09a62016-11-17 15:21:22 -0800530 T1 v1 = Decode<T1>(DecodeUint32WithOverflowCheck(in, end));
531 T2 v2 = Decode<T2>(DecodeUint32WithOverflowCheck(in, end));
David Brazdil6f82fbd2016-09-14 11:55:26 +0100532 *t = std::make_tuple(v1, v2);
533}
534
535template<typename T1, typename T2, typename T3>
536static inline void EncodeTuple(std::vector<uint8_t>* out, const std::tuple<T1, T2, T3>& t) {
Andreas Gampea5b09a62016-11-17 15:21:22 -0800537 EncodeUnsignedLeb128(out, Encode(std::get<0>(t)));
538 EncodeUnsignedLeb128(out, Encode(std::get<1>(t)));
539 EncodeUnsignedLeb128(out, Encode(std::get<2>(t)));
David Brazdil6f82fbd2016-09-14 11:55:26 +0100540}
541
542template<typename T1, typename T2, typename T3>
543static inline void DecodeTuple(const uint8_t** in, const uint8_t* end, std::tuple<T1, T2, T3>* t) {
Andreas Gampea5b09a62016-11-17 15:21:22 -0800544 T1 v1 = Decode<T1>(DecodeUint32WithOverflowCheck(in, end));
545 T2 v2 = Decode<T2>(DecodeUint32WithOverflowCheck(in, end));
Andreas Gampe8a0128a2016-11-28 07:38:35 -0800546 T3 v3 = Decode<T3>(DecodeUint32WithOverflowCheck(in, end));
David Brazdil6f82fbd2016-09-14 11:55:26 +0100547 *t = std::make_tuple(v1, v2, v3);
548}
549
550template<typename T>
551static inline void EncodeSet(std::vector<uint8_t>* out, const std::set<T>& set) {
552 EncodeUnsignedLeb128(out, set.size());
553 for (const T& entry : set) {
554 EncodeTuple(out, entry);
555 }
556}
557
Andreas Gampea5b09a62016-11-17 15:21:22 -0800558template <typename T>
Nicolas Geoffray08025182016-10-25 17:20:18 +0100559static inline void EncodeUint16Vector(std::vector<uint8_t>* out,
Andreas Gampea5b09a62016-11-17 15:21:22 -0800560 const std::vector<T>& vector) {
Nicolas Geoffray08025182016-10-25 17:20:18 +0100561 EncodeUnsignedLeb128(out, vector.size());
Andreas Gampea5b09a62016-11-17 15:21:22 -0800562 for (const T& entry : vector) {
563 EncodeUnsignedLeb128(out, Encode(entry));
Nicolas Geoffray08025182016-10-25 17:20:18 +0100564 }
565}
566
David Brazdil6f82fbd2016-09-14 11:55:26 +0100567template<typename T>
568static inline void DecodeSet(const uint8_t** in, const uint8_t* end, std::set<T>* set) {
569 DCHECK(set->empty());
570 size_t num_entries = DecodeUint32WithOverflowCheck(in, end);
571 for (size_t i = 0; i < num_entries; ++i) {
572 T tuple;
573 DecodeTuple(in, end, &tuple);
574 set->emplace(tuple);
575 }
576}
577
Andreas Gampea5b09a62016-11-17 15:21:22 -0800578template<typename T>
Nicolas Geoffray08025182016-10-25 17:20:18 +0100579static inline void DecodeUint16Vector(const uint8_t** in,
580 const uint8_t* end,
Andreas Gampea5b09a62016-11-17 15:21:22 -0800581 std::vector<T>* vector) {
Nicolas Geoffray08025182016-10-25 17:20:18 +0100582 DCHECK(vector->empty());
583 size_t num_entries = DecodeUint32WithOverflowCheck(in, end);
584 vector->reserve(num_entries);
585 for (size_t i = 0; i < num_entries; ++i) {
Andreas Gampea5b09a62016-11-17 15:21:22 -0800586 vector->push_back(
587 Decode<T>(dchecked_integral_cast<uint16_t>(DecodeUint32WithOverflowCheck(in, end))));
Nicolas Geoffray08025182016-10-25 17:20:18 +0100588 }
589}
590
David Brazdil6f82fbd2016-09-14 11:55:26 +0100591static inline void EncodeStringVector(std::vector<uint8_t>* out,
592 const std::vector<std::string>& strings) {
593 EncodeUnsignedLeb128(out, strings.size());
594 for (const std::string& str : strings) {
595 const uint8_t* data = reinterpret_cast<const uint8_t*>(str.c_str());
596 size_t length = str.length() + 1;
597 out->insert(out->end(), data, data + length);
598 DCHECK_EQ(0u, out->back());
599 }
600}
601
602static inline void DecodeStringVector(const uint8_t** in,
603 const uint8_t* end,
604 std::vector<std::string>* strings) {
605 DCHECK(strings->empty());
606 size_t num_strings = DecodeUint32WithOverflowCheck(in, end);
607 strings->reserve(num_strings);
608 for (size_t i = 0; i < num_strings; ++i) {
609 CHECK_LT(*in, end);
610 const char* string_start = reinterpret_cast<const char*>(*in);
611 strings->emplace_back(std::string(string_start));
612 *in += strings->back().length() + 1;
613 }
614}
615
Andreas Gampea5b09a62016-11-17 15:21:22 -0800616} // namespace
617
Nicolas Geoffrayd01f60c2016-10-28 14:45:48 +0100618void VerifierDeps::Encode(const std::vector<const DexFile*>& dex_files,
619 std::vector<uint8_t>* buffer) const {
Nicolas Geoffrayd01f60c2016-10-28 14:45:48 +0100620 for (const DexFile* dex_file : dex_files) {
621 const DexFileDeps& deps = *GetDexFileDeps(*dex_file);
622 EncodeStringVector(buffer, deps.strings_);
623 EncodeSet(buffer, deps.assignable_types_);
624 EncodeSet(buffer, deps.unassignable_types_);
625 EncodeSet(buffer, deps.classes_);
626 EncodeSet(buffer, deps.fields_);
627 EncodeSet(buffer, deps.direct_methods_);
628 EncodeSet(buffer, deps.virtual_methods_);
629 EncodeSet(buffer, deps.interface_methods_);
630 EncodeUint16Vector(buffer, deps.unverified_classes_);
David Brazdil6f82fbd2016-09-14 11:55:26 +0100631 }
632}
633
Nicolas Geoffraye70dd562016-10-30 21:03:35 +0000634VerifierDeps::VerifierDeps(const std::vector<const DexFile*>& dex_files,
635 ArrayRef<const uint8_t> data)
David Brazdil6f82fbd2016-09-14 11:55:26 +0100636 : VerifierDeps(dex_files) {
Nicolas Geoffraye70dd562016-10-30 21:03:35 +0000637 if (data.empty()) {
638 // Return eagerly, as the first thing we expect from VerifierDeps data is
639 // the number of created strings, even if there is no dependency.
640 // Currently, only the boot image does not have any VerifierDeps data.
641 return;
642 }
David Brazdil6f82fbd2016-09-14 11:55:26 +0100643 const uint8_t* data_start = data.data();
644 const uint8_t* data_end = data_start + data.size();
Nicolas Geoffrayd01f60c2016-10-28 14:45:48 +0100645 for (const DexFile* dex_file : dex_files) {
646 DexFileDeps* deps = GetDexFileDeps(*dex_file);
647 DecodeStringVector(&data_start, data_end, &deps->strings_);
648 DecodeSet(&data_start, data_end, &deps->assignable_types_);
649 DecodeSet(&data_start, data_end, &deps->unassignable_types_);
650 DecodeSet(&data_start, data_end, &deps->classes_);
651 DecodeSet(&data_start, data_end, &deps->fields_);
652 DecodeSet(&data_start, data_end, &deps->direct_methods_);
653 DecodeSet(&data_start, data_end, &deps->virtual_methods_);
654 DecodeSet(&data_start, data_end, &deps->interface_methods_);
655 DecodeUint16Vector(&data_start, data_end, &deps->unverified_classes_);
David Brazdil6f82fbd2016-09-14 11:55:26 +0100656 }
657 CHECK_LE(data_start, data_end);
658}
659
660bool VerifierDeps::Equals(const VerifierDeps& rhs) const {
David Brazdil6f82fbd2016-09-14 11:55:26 +0100661 if (dex_deps_.size() != rhs.dex_deps_.size()) {
662 return false;
663 }
664
665 auto lhs_it = dex_deps_.begin();
666 auto rhs_it = rhs.dex_deps_.begin();
667
668 for (; (lhs_it != dex_deps_.end()) && (rhs_it != rhs.dex_deps_.end()); lhs_it++, rhs_it++) {
669 const DexFile* lhs_dex_file = lhs_it->first;
670 const DexFile* rhs_dex_file = rhs_it->first;
671 if (lhs_dex_file != rhs_dex_file) {
672 return false;
673 }
674
675 DexFileDeps* lhs_deps = lhs_it->second.get();
676 DexFileDeps* rhs_deps = rhs_it->second.get();
677 if (!lhs_deps->Equals(*rhs_deps)) {
678 return false;
679 }
680 }
681
682 DCHECK((lhs_it == dex_deps_.end()) && (rhs_it == rhs.dex_deps_.end()));
683 return true;
684}
685
686bool VerifierDeps::DexFileDeps::Equals(const VerifierDeps::DexFileDeps& rhs) const {
687 return (strings_ == rhs.strings_) &&
688 (assignable_types_ == rhs.assignable_types_) &&
689 (unassignable_types_ == rhs.unassignable_types_) &&
690 (classes_ == rhs.classes_) &&
691 (fields_ == rhs.fields_) &&
692 (direct_methods_ == rhs.direct_methods_) &&
693 (virtual_methods_ == rhs.virtual_methods_) &&
Nicolas Geoffray08025182016-10-25 17:20:18 +0100694 (interface_methods_ == rhs.interface_methods_) &&
695 (unverified_classes_ == rhs.unverified_classes_);
David Brazdil6f82fbd2016-09-14 11:55:26 +0100696}
697
Nicolas Geoffrayd01f60c2016-10-28 14:45:48 +0100698void VerifierDeps::Dump(VariableIndentationOutputStream* vios) const {
699 for (const auto& dep : dex_deps_) {
700 const DexFile& dex_file = *dep.first;
701 vios->Stream()
702 << "Dependencies of "
703 << dex_file.GetLocation()
704 << ":\n";
705
706 ScopedIndentation indent(vios);
707
708 for (const std::string& str : dep.second->strings_) {
709 vios->Stream() << "Extra string: " << str << "\n";
710 }
711
712 for (const TypeAssignability& entry : dep.second->assignable_types_) {
713 vios->Stream()
714 << GetStringFromId(dex_file, entry.GetSource())
715 << " must be assignable to "
716 << GetStringFromId(dex_file, entry.GetDestination())
717 << "\n";
718 }
719
720 for (const TypeAssignability& entry : dep.second->unassignable_types_) {
721 vios->Stream()
722 << GetStringFromId(dex_file, entry.GetSource())
723 << " must not be assignable to "
724 << GetStringFromId(dex_file, entry.GetDestination())
725 << "\n";
726 }
727
728 for (const ClassResolution& entry : dep.second->classes_) {
729 vios->Stream()
730 << dex_file.StringByTypeIdx(entry.GetDexTypeIndex())
731 << (entry.IsResolved() ? " must be resolved " : "must not be resolved ")
732 << " with access flags " << std::hex << entry.GetAccessFlags() << std::dec
733 << "\n";
734 }
735
736 for (const FieldResolution& entry : dep.second->fields_) {
737 const DexFile::FieldId& field_id = dex_file.GetFieldId(entry.GetDexFieldIndex());
738 vios->Stream()
739 << dex_file.GetFieldDeclaringClassDescriptor(field_id) << "->"
740 << dex_file.GetFieldName(field_id) << ":"
741 << dex_file.GetFieldTypeDescriptor(field_id)
742 << " is expected to be ";
743 if (!entry.IsResolved()) {
744 vios->Stream() << "unresolved\n";
745 } else {
746 vios->Stream()
747 << "in class "
748 << GetStringFromId(dex_file, entry.GetDeclaringClassIndex())
749 << ", and have the access flags " << std::hex << entry.GetAccessFlags() << std::dec
750 << "\n";
751 }
752 }
753
754 for (const auto& entry :
755 { std::make_pair(kDirectMethodResolution, dep.second->direct_methods_),
756 std::make_pair(kVirtualMethodResolution, dep.second->virtual_methods_),
757 std::make_pair(kInterfaceMethodResolution, dep.second->interface_methods_) }) {
758 for (const MethodResolution& method : entry.second) {
759 const DexFile::MethodId& method_id = dex_file.GetMethodId(method.GetDexMethodIndex());
760 vios->Stream()
761 << dex_file.GetMethodDeclaringClassDescriptor(method_id) << "->"
762 << dex_file.GetMethodName(method_id)
763 << dex_file.GetMethodSignature(method_id).ToString()
764 << " is expected to be ";
765 if (!method.IsResolved()) {
766 vios->Stream() << "unresolved\n";
767 } else {
768 vios->Stream()
769 << "in class "
770 << GetStringFromId(dex_file, method.GetDeclaringClassIndex())
771 << ", have the access flags " << std::hex << method.GetAccessFlags() << std::dec
772 << ", and be of kind " << entry.first
773 << "\n";
774 }
775 }
776 }
777
Andreas Gampea5b09a62016-11-17 15:21:22 -0800778 for (dex::TypeIndex type_index : dep.second->unverified_classes_) {
Nicolas Geoffrayd01f60c2016-10-28 14:45:48 +0100779 vios->Stream()
780 << dex_file.StringByTypeIdx(type_index)
781 << " is expected to be verified at runtime\n";
782 }
783 }
784}
785
Nicolas Geoffray6bb7f1b2016-11-03 10:52:49 +0000786bool VerifierDeps::ValidateDependencies(Handle<mirror::ClassLoader> class_loader,
787 Thread* self) const {
Nicolas Geoffray8904b6f2016-10-28 19:50:34 +0100788 for (const auto& entry : dex_deps_) {
789 if (!VerifyDexFile(class_loader, *entry.first, *entry.second, self)) {
790 return false;
791 }
792 }
793 return true;
794}
795
796// TODO: share that helper with other parts of the compiler that have
797// the same lookup pattern.
798static mirror::Class* FindClassAndClearException(ClassLinker* class_linker,
799 Thread* self,
800 const char* name,
801 Handle<mirror::ClassLoader> class_loader)
802 REQUIRES_SHARED(Locks::mutator_lock_) {
803 mirror::Class* result = class_linker->FindClass(self, name, class_loader);
804 if (result == nullptr) {
805 DCHECK(self->IsExceptionPending());
806 self->ClearException();
807 }
808 return result;
809}
810
811bool VerifierDeps::VerifyAssignability(Handle<mirror::ClassLoader> class_loader,
812 const DexFile& dex_file,
813 const std::set<TypeAssignability>& assignables,
814 bool expected_assignability,
815 Thread* self) const {
816 StackHandleScope<2> hs(self);
817 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
818 MutableHandle<mirror::Class> source(hs.NewHandle<mirror::Class>(nullptr));
819 MutableHandle<mirror::Class> destination(hs.NewHandle<mirror::Class>(nullptr));
820
821 for (const auto& entry : assignables) {
822 const std::string& destination_desc = GetStringFromId(dex_file, entry.GetDestination());
823 destination.Assign(
824 FindClassAndClearException(class_linker, self, destination_desc.c_str(), class_loader));
825 const std::string& source_desc = GetStringFromId(dex_file, entry.GetSource());
826 source.Assign(
827 FindClassAndClearException(class_linker, self, source_desc.c_str(), class_loader));
828
829 if (destination.Get() == nullptr) {
830 LOG(INFO) << "VerifiersDeps: Could not resolve class " << destination_desc;
831 return false;
832 }
833
834 if (source.Get() == nullptr) {
835 LOG(INFO) << "VerifierDeps: Could not resolve class " << source_desc;
836 return false;
837 }
838
839 DCHECK(destination->IsResolved() && source->IsResolved());
840 if (destination->IsAssignableFrom(source.Get()) != expected_assignability) {
841 LOG(INFO) << "VerifierDeps: Class "
842 << destination_desc
843 << (expected_assignability ? " not " : " ")
844 << "assignable from "
845 << source_desc;
846 return false;
847 }
848 }
849 return true;
850}
851
852bool VerifierDeps::VerifyClasses(Handle<mirror::ClassLoader> class_loader,
853 const DexFile& dex_file,
854 const std::set<ClassResolution>& classes,
855 Thread* self) const {
856 StackHandleScope<1> hs(self);
857 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
858 MutableHandle<mirror::Class> cls(hs.NewHandle<mirror::Class>(nullptr));
859 for (const auto& entry : classes) {
860 const char* descriptor = dex_file.StringByTypeIdx(entry.GetDexTypeIndex());
861 cls.Assign(FindClassAndClearException(class_linker, self, descriptor, class_loader));
862
863 if (entry.IsResolved()) {
864 if (cls.Get() == nullptr) {
865 LOG(INFO) << "VerifierDeps: Could not resolve class " << descriptor;
866 return false;
867 } else if (entry.GetAccessFlags() != GetAccessFlags(cls.Get())) {
868 LOG(INFO) << "VerifierDeps: Unexpected access flags on class "
869 << descriptor
870 << std::hex
871 << " (expected="
872 << entry.GetAccessFlags()
873 << ", actual="
874 << GetAccessFlags(cls.Get()) << ")"
875 << std::dec;
876 return false;
877 }
878 } else if (cls.Get() != nullptr) {
879 LOG(INFO) << "VerifierDeps: Unexpected successful resolution of class " << descriptor;
880 return false;
881 }
882 }
883 return true;
884}
885
886static std::string GetFieldDescription(const DexFile& dex_file, uint32_t index) {
887 const DexFile::FieldId& field_id = dex_file.GetFieldId(index);
888 return std::string(dex_file.GetFieldDeclaringClassDescriptor(field_id))
889 + "->"
890 + dex_file.GetFieldName(field_id)
891 + ":"
892 + dex_file.GetFieldTypeDescriptor(field_id);
893}
894
895bool VerifierDeps::VerifyFields(Handle<mirror::ClassLoader> class_loader,
896 const DexFile& dex_file,
897 const std::set<FieldResolution>& fields,
898 Thread* self) const {
899 // Check recorded fields are resolved the same way, have the same recorded class,
900 // and have the same recorded flags.
901 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
902 StackHandleScope<1> hs(self);
903 Handle<mirror::DexCache> dex_cache(
904 hs.NewHandle(class_linker->FindDexCache(self, dex_file, /* allow_failure */ false)));
905 for (const auto& entry : fields) {
906 ArtField* field = class_linker->ResolveFieldJLS(
907 dex_file, entry.GetDexFieldIndex(), dex_cache, class_loader);
908
909 if (field == nullptr) {
910 DCHECK(self->IsExceptionPending());
911 self->ClearException();
912 }
913
914 if (entry.IsResolved()) {
915 std::string expected_decl_klass = GetStringFromId(dex_file, entry.GetDeclaringClassIndex());
916 std::string temp;
917 if (field == nullptr) {
918 LOG(INFO) << "VerifierDeps: Could not resolve field "
919 << GetFieldDescription(dex_file, entry.GetDexFieldIndex());
920 return false;
921 } else if (expected_decl_klass != field->GetDeclaringClass()->GetDescriptor(&temp)) {
922 LOG(INFO) << "VerifierDeps: Unexpected declaring class for field resolution "
923 << GetFieldDescription(dex_file, entry.GetDexFieldIndex())
924 << " (expected=" << expected_decl_klass
925 << ", actual=" << field->GetDeclaringClass()->GetDescriptor(&temp) << ")";
926 return false;
927 } else if (entry.GetAccessFlags() != GetAccessFlags(field)) {
928 LOG(INFO) << "VerifierDeps: Unexpected access flags for resolved field "
929 << GetFieldDescription(dex_file, entry.GetDexFieldIndex())
930 << std::hex << " (expected=" << entry.GetAccessFlags()
931 << ", actual=" << GetAccessFlags(field) << ")" << std::dec;
932 return false;
933 }
934 } else if (field != nullptr) {
935 LOG(INFO) << "VerifierDeps: Unexpected successful resolution of field "
936 << GetFieldDescription(dex_file, entry.GetDexFieldIndex());
937 return false;
938 }
939 }
940 return true;
941}
942
943static std::string GetMethodDescription(const DexFile& dex_file, uint32_t index) {
944 const DexFile::MethodId& method_id = dex_file.GetMethodId(index);
945 return std::string(dex_file.GetMethodDeclaringClassDescriptor(method_id))
946 + "->"
947 + dex_file.GetMethodName(method_id)
948 + dex_file.GetMethodSignature(method_id).ToString();
949}
950
951bool VerifierDeps::VerifyMethods(Handle<mirror::ClassLoader> class_loader,
952 const DexFile& dex_file,
953 const std::set<MethodResolution>& methods,
954 MethodResolutionKind kind,
955 Thread* self) const {
956 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
957 PointerSize pointer_size = class_linker->GetImagePointerSize();
958
959 for (const auto& entry : methods) {
960 const DexFile::MethodId& method_id = dex_file.GetMethodId(entry.GetDexMethodIndex());
961
962 const char* name = dex_file.GetMethodName(method_id);
963 const Signature signature = dex_file.GetMethodSignature(method_id);
964 const char* descriptor = dex_file.GetMethodDeclaringClassDescriptor(method_id);
965
966 mirror::Class* cls = FindClassAndClearException(class_linker, self, descriptor, class_loader);
967 if (cls == nullptr) {
968 LOG(INFO) << "VerifierDeps: Could not resolve class " << descriptor;
969 return false;
970 }
971 DCHECK(cls->IsResolved());
972 ArtMethod* method = nullptr;
973 if (kind == kDirectMethodResolution) {
974 method = cls->FindDirectMethod(name, signature, pointer_size);
975 } else if (kind == kVirtualMethodResolution) {
976 method = cls->FindVirtualMethod(name, signature, pointer_size);
977 } else {
978 DCHECK_EQ(kind, kInterfaceMethodResolution);
979 method = cls->FindInterfaceMethod(name, signature, pointer_size);
980 }
981
982 if (entry.IsResolved()) {
983 std::string temp;
984 std::string expected_decl_klass = GetStringFromId(dex_file, entry.GetDeclaringClassIndex());
985 if (method == nullptr) {
986 LOG(INFO) << "VerifierDeps: Could not resolve "
987 << kind
988 << " method "
989 << GetMethodDescription(dex_file, entry.GetDexMethodIndex());
990 return false;
991 } else if (expected_decl_klass != method->GetDeclaringClass()->GetDescriptor(&temp)) {
992 LOG(INFO) << "VerifierDeps: Unexpected declaring class for "
993 << kind
994 << " method resolution "
995 << GetMethodDescription(dex_file, entry.GetDexMethodIndex())
996 << " (expected="
997 << expected_decl_klass
998 << ", actual="
999 << method->GetDeclaringClass()->GetDescriptor(&temp)
1000 << ")";
1001 return false;
1002 } else if (entry.GetAccessFlags() != GetAccessFlags(method)) {
1003 LOG(INFO) << "VerifierDeps: Unexpected access flags for resolved "
1004 << kind
1005 << " method resolution "
1006 << GetMethodDescription(dex_file, entry.GetDexMethodIndex())
1007 << std::hex
1008 << " (expected="
1009 << entry.GetAccessFlags()
1010 << ", actual="
1011 << GetAccessFlags(method) << ")"
1012 << std::dec;
1013 return false;
1014 }
1015 } else if (method != nullptr) {
1016 LOG(INFO) << "VerifierDeps: Unexpected successful resolution of "
1017 << kind
1018 << " method "
1019 << GetMethodDescription(dex_file, entry.GetDexMethodIndex());
1020 return false;
1021 }
1022 }
1023 return true;
1024}
1025
1026bool VerifierDeps::VerifyDexFile(Handle<mirror::ClassLoader> class_loader,
1027 const DexFile& dex_file,
1028 const DexFileDeps& deps,
1029 Thread* self) const {
1030 bool result = VerifyAssignability(
1031 class_loader, dex_file, deps.assignable_types_, /* expected_assignability */ true, self);
1032 result = result && VerifyAssignability(
1033 class_loader, dex_file, deps.unassignable_types_, /* expected_assignability */ false, self);
1034
1035 result = result && VerifyClasses(class_loader, dex_file, deps.classes_, self);
1036 result = result && VerifyFields(class_loader, dex_file, deps.fields_, self);
1037
1038 result = result && VerifyMethods(
1039 class_loader, dex_file, deps.direct_methods_, kDirectMethodResolution, self);
1040 result = result && VerifyMethods(
1041 class_loader, dex_file, deps.virtual_methods_, kVirtualMethodResolution, self);
1042 result = result && VerifyMethods(
1043 class_loader, dex_file, deps.interface_methods_, kInterfaceMethodResolution, self);
1044
1045 return result;
1046}
1047
David Brazdilca3c8c32016-09-06 14:04:48 +01001048} // namespace verifier
1049} // namespace art