blob: bdf63cb45b0c0cf8a30169d70a3d7eeb82cf8368 [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
19#include "compiler_callbacks.h"
David Brazdil6f82fbd2016-09-14 11:55:26 +010020#include "leb128.h"
David Brazdilca3c8c32016-09-06 14:04:48 +010021#include "mirror/class-inl.h"
Mathieu Chartier3398c782016-09-30 10:27:43 -070022#include "obj_ptr-inl.h"
David Brazdilca3c8c32016-09-06 14:04:48 +010023#include "runtime.h"
24
25namespace art {
26namespace verifier {
27
28VerifierDeps::VerifierDeps(const std::vector<const DexFile*>& dex_files) {
29 MutexLock mu(Thread::Current(), *Locks::verifier_deps_lock_);
30 for (const DexFile* dex_file : dex_files) {
31 DCHECK(GetDexFileDeps(*dex_file) == nullptr);
32 std::unique_ptr<DexFileDeps> deps(new DexFileDeps());
33 dex_deps_.emplace(dex_file, std::move(deps));
34 }
35}
36
37VerifierDeps::DexFileDeps* VerifierDeps::GetDexFileDeps(const DexFile& dex_file) {
38 auto it = dex_deps_.find(&dex_file);
39 return (it == dex_deps_.end()) ? nullptr : it->second.get();
40}
41
Nicolas Geoffrayd01f60c2016-10-28 14:45:48 +010042const VerifierDeps::DexFileDeps* VerifierDeps::GetDexFileDeps(const DexFile& dex_file) const {
43 auto it = dex_deps_.find(&dex_file);
44 return (it == dex_deps_.end()) ? nullptr : it->second.get();
45}
46
David Brazdilca3c8c32016-09-06 14:04:48 +010047template <typename T>
48uint16_t VerifierDeps::GetAccessFlags(T* element) {
49 static_assert(kAccJavaFlagsMask == 0xFFFF, "Unexpected value of a constant");
50 if (element == nullptr) {
51 return VerifierDeps::kUnresolvedMarker;
52 } else {
53 uint16_t access_flags = Low16Bits(element->GetAccessFlags());
54 CHECK_NE(access_flags, VerifierDeps::kUnresolvedMarker);
55 return access_flags;
56 }
57}
58
59template <typename T>
60uint32_t VerifierDeps::GetDeclaringClassStringId(const DexFile& dex_file, T* element) {
61 static_assert(kAccJavaFlagsMask == 0xFFFF, "Unexpected value of a constant");
62 if (element == nullptr) {
63 return VerifierDeps::kUnresolvedMarker;
64 } else {
65 std::string temp;
66 uint32_t string_id = GetIdFromString(
67 dex_file, element->GetDeclaringClass()->GetDescriptor(&temp));
68 return string_id;
69 }
70}
71
72uint32_t VerifierDeps::GetIdFromString(const DexFile& dex_file, const std::string& str) {
73 const DexFile::StringId* string_id = dex_file.FindStringId(str.c_str());
74 if (string_id != nullptr) {
75 // String is in the DEX file. Return its ID.
76 return dex_file.GetIndexForStringId(*string_id);
77 }
78
79 // String is not in the DEX file. Assign a new ID to it which is higher than
80 // the number of strings in the DEX file.
81
82 DexFileDeps* deps = GetDexFileDeps(dex_file);
83 DCHECK(deps != nullptr);
84
85 uint32_t num_ids_in_dex = dex_file.NumStringIds();
86 uint32_t num_extra_ids = deps->strings_.size();
87
88 for (size_t i = 0; i < num_extra_ids; ++i) {
89 if (deps->strings_[i] == str) {
90 return num_ids_in_dex + i;
91 }
92 }
93
94 deps->strings_.push_back(str);
95
96 uint32_t new_id = num_ids_in_dex + num_extra_ids;
97 CHECK_GE(new_id, num_ids_in_dex); // check for overflows
98 DCHECK_EQ(str, GetStringFromId(dex_file, new_id));
99
100 return new_id;
101}
102
Nicolas Geoffrayd01f60c2016-10-28 14:45:48 +0100103std::string VerifierDeps::GetStringFromId(const DexFile& dex_file, uint32_t string_id) const {
David Brazdilca3c8c32016-09-06 14:04:48 +0100104 uint32_t num_ids_in_dex = dex_file.NumStringIds();
105 if (string_id < num_ids_in_dex) {
106 return std::string(dex_file.StringDataByIdx(string_id));
107 } else {
Nicolas Geoffrayd01f60c2016-10-28 14:45:48 +0100108 const DexFileDeps* deps = GetDexFileDeps(dex_file);
David Brazdilca3c8c32016-09-06 14:04:48 +0100109 DCHECK(deps != nullptr);
110 string_id -= num_ids_in_dex;
111 CHECK_LT(string_id, deps->strings_.size());
112 return deps->strings_[string_id];
113 }
114}
115
Nicolas Geoffrayd01f60c2016-10-28 14:45:48 +0100116bool VerifierDeps::IsInClassPath(ObjPtr<mirror::Class> klass) const {
David Brazdilca3c8c32016-09-06 14:04:48 +0100117 DCHECK(klass != nullptr);
118
Mathieu Chartier3398c782016-09-30 10:27:43 -0700119 ObjPtr<mirror::DexCache> dex_cache = klass->GetDexCache();
David Brazdilca3c8c32016-09-06 14:04:48 +0100120 if (dex_cache == nullptr) {
121 // This is a synthesized class, in this case always an array. They are not
122 // defined in the compiled DEX files and therefore are part of the classpath.
123 // We could avoid recording dependencies on arrays with component types in
124 // the compiled DEX files but we choose to record them anyway so as to
125 // record the access flags VM sets for array classes.
David Sehr709b0702016-10-13 09:12:37 -0700126 DCHECK(klass->IsArrayClass()) << klass->PrettyDescriptor();
David Brazdilca3c8c32016-09-06 14:04:48 +0100127 return true;
128 }
129
130 const DexFile* dex_file = dex_cache->GetDexFile();
131 DCHECK(dex_file != nullptr);
132
133 // Test if the `dex_deps_` contains an entry for `dex_file`. If not, the dex
134 // file was not registered as being compiled and we assume `klass` is in the
135 // classpath.
136 return (GetDexFileDeps(*dex_file) == nullptr);
137}
138
139void VerifierDeps::AddClassResolution(const DexFile& dex_file,
140 uint16_t type_idx,
141 mirror::Class* klass) {
142 DexFileDeps* dex_deps = GetDexFileDeps(dex_file);
143 if (dex_deps == nullptr) {
144 // This invocation is from verification of a dex file which is not being compiled.
145 return;
146 }
147
148 if (klass != nullptr && !IsInClassPath(klass)) {
149 // Class resolved into one of the DEX files which are being compiled.
150 // This is not a classpath dependency.
151 return;
152 }
153
154 MutexLock mu(Thread::Current(), *Locks::verifier_deps_lock_);
155 dex_deps->classes_.emplace(ClassResolution(type_idx, GetAccessFlags(klass)));
156}
157
158void VerifierDeps::AddFieldResolution(const DexFile& dex_file,
159 uint32_t field_idx,
160 ArtField* field) {
161 DexFileDeps* dex_deps = GetDexFileDeps(dex_file);
162 if (dex_deps == nullptr) {
163 // This invocation is from verification of a dex file which is not being compiled.
164 return;
165 }
166
167 if (field != nullptr && !IsInClassPath(field->GetDeclaringClass())) {
168 // Field resolved into one of the DEX files which are being compiled.
169 // This is not a classpath dependency.
170 return;
171 }
172
173 MutexLock mu(Thread::Current(), *Locks::verifier_deps_lock_);
174 dex_deps->fields_.emplace(FieldResolution(
175 field_idx, GetAccessFlags(field), GetDeclaringClassStringId(dex_file, field)));
176}
177
178void VerifierDeps::AddMethodResolution(const DexFile& dex_file,
179 uint32_t method_idx,
180 MethodResolutionKind resolution_kind,
181 ArtMethod* method) {
182 DexFileDeps* dex_deps = GetDexFileDeps(dex_file);
183 if (dex_deps == nullptr) {
184 // This invocation is from verification of a dex file which is not being compiled.
185 return;
186 }
187
188 if (method != nullptr && !IsInClassPath(method->GetDeclaringClass())) {
189 // Method resolved into one of the DEX files which are being compiled.
190 // This is not a classpath dependency.
191 return;
192 }
193
194 MutexLock mu(Thread::Current(), *Locks::verifier_deps_lock_);
195 MethodResolution method_tuple(method_idx,
196 GetAccessFlags(method),
197 GetDeclaringClassStringId(dex_file, method));
198 if (resolution_kind == kDirectMethodResolution) {
199 dex_deps->direct_methods_.emplace(method_tuple);
200 } else if (resolution_kind == kVirtualMethodResolution) {
201 dex_deps->virtual_methods_.emplace(method_tuple);
202 } else {
203 DCHECK_EQ(resolution_kind, kInterfaceMethodResolution);
204 dex_deps->interface_methods_.emplace(method_tuple);
205 }
206}
207
208void VerifierDeps::AddAssignability(const DexFile& dex_file,
209 mirror::Class* destination,
210 mirror::Class* source,
211 bool is_strict,
212 bool is_assignable) {
213 // Test that the method is only called on reference types.
214 // Note that concurrent verification of `destination` and `source` may have
215 // set their status to erroneous. However, the tests performed below rely
216 // merely on no issues with linking (valid access flags, superclass and
217 // implemented interfaces). If the class at any point reached the IsResolved
218 // status, the requirement holds. This is guaranteed by RegTypeCache::ResolveClass.
219 DCHECK(destination != nullptr && !destination->IsPrimitive());
220 DCHECK(source != nullptr && !source->IsPrimitive());
221
222 if (destination == source ||
223 destination->IsObjectClass() ||
224 (!is_strict && destination->IsInterface())) {
225 // Cases when `destination` is trivially assignable from `source`.
226 DCHECK(is_assignable);
227 return;
228 }
229
230 DCHECK_EQ(is_assignable, destination->IsAssignableFrom(source));
231
232 if (destination->IsArrayClass() && source->IsArrayClass()) {
233 // Both types are arrays. Break down to component types and add recursively.
234 // This helps filter out destinations from compiled DEX files (see below)
235 // and deduplicate entries with the same canonical component type.
236 mirror::Class* destination_component = destination->GetComponentType();
237 mirror::Class* source_component = source->GetComponentType();
238
239 // Only perform the optimization if both types are resolved which guarantees
240 // that they linked successfully, as required at the top of this method.
241 if (destination_component->IsResolved() && source_component->IsResolved()) {
242 AddAssignability(dex_file,
243 destination_component,
244 source_component,
245 /* is_strict */ true,
246 is_assignable);
247 return;
248 }
249 }
250
251 DexFileDeps* dex_deps = GetDexFileDeps(dex_file);
252 if (dex_deps == nullptr) {
253 // This invocation is from verification of a DEX file which is not being compiled.
254 return;
255 }
256
257 if (!IsInClassPath(destination) && !IsInClassPath(source)) {
258 // Both `destination` and `source` are defined in the compiled DEX files.
259 // No need to record a dependency.
260 return;
261 }
262
263 MutexLock mu(Thread::Current(), *Locks::verifier_deps_lock_);
264
265 // Get string IDs for both descriptors and store in the appropriate set.
266
267 std::string temp1, temp2;
268 std::string destination_desc(destination->GetDescriptor(&temp1));
269 std::string source_desc(source->GetDescriptor(&temp2));
270 uint32_t destination_id = GetIdFromString(dex_file, destination_desc);
271 uint32_t source_id = GetIdFromString(dex_file, source_desc);
272
273 if (is_assignable) {
274 dex_deps->assignable_types_.emplace(TypeAssignability(destination_id, source_id));
275 } else {
276 dex_deps->unassignable_types_.emplace(TypeAssignability(destination_id, source_id));
277 }
278}
279
280static inline VerifierDeps* GetVerifierDepsSingleton() {
281 CompilerCallbacks* callbacks = Runtime::Current()->GetCompilerCallbacks();
282 if (callbacks == nullptr) {
283 return nullptr;
284 }
285 return callbacks->GetVerifierDeps();
286}
287
Nicolas Geoffray08025182016-10-25 17:20:18 +0100288void VerifierDeps::MaybeRecordVerificationStatus(const DexFile& dex_file,
289 uint16_t type_idx,
290 MethodVerifier::FailureKind failure_kind) {
291 if (failure_kind == MethodVerifier::kNoFailure) {
292 // We only record classes that did not fully verify at compile time.
293 return;
294 }
295
296 VerifierDeps* singleton = GetVerifierDepsSingleton();
297 if (singleton != nullptr) {
298 DexFileDeps* dex_deps = singleton->GetDexFileDeps(dex_file);
299 MutexLock mu(Thread::Current(), *Locks::verifier_deps_lock_);
300 dex_deps->unverified_classes_.push_back(type_idx);
301 }
302}
303
David Brazdilca3c8c32016-09-06 14:04:48 +0100304void VerifierDeps::MaybeRecordClassResolution(const DexFile& dex_file,
305 uint16_t type_idx,
306 mirror::Class* klass) {
307 VerifierDeps* singleton = GetVerifierDepsSingleton();
308 if (singleton != nullptr) {
309 singleton->AddClassResolution(dex_file, type_idx, klass);
310 }
311}
312
313void VerifierDeps::MaybeRecordFieldResolution(const DexFile& dex_file,
314 uint32_t field_idx,
315 ArtField* field) {
316 VerifierDeps* singleton = GetVerifierDepsSingleton();
317 if (singleton != nullptr) {
318 singleton->AddFieldResolution(dex_file, field_idx, field);
319 }
320}
321
322void VerifierDeps::MaybeRecordMethodResolution(const DexFile& dex_file,
323 uint32_t method_idx,
324 MethodResolutionKind resolution_kind,
325 ArtMethod* method) {
326 VerifierDeps* singleton = GetVerifierDepsSingleton();
327 if (singleton != nullptr) {
328 singleton->AddMethodResolution(dex_file, method_idx, resolution_kind, method);
329 }
330}
331
332void VerifierDeps::MaybeRecordAssignability(const DexFile& dex_file,
333 mirror::Class* destination,
334 mirror::Class* source,
335 bool is_strict,
336 bool is_assignable) {
337 VerifierDeps* singleton = GetVerifierDepsSingleton();
338 if (singleton != nullptr) {
339 singleton->AddAssignability(dex_file, destination, source, is_strict, is_assignable);
340 }
341}
342
David Brazdil6f82fbd2016-09-14 11:55:26 +0100343static inline uint32_t DecodeUint32WithOverflowCheck(const uint8_t** in, const uint8_t* end) {
344 CHECK_LT(*in, end);
345 return DecodeUnsignedLeb128(in);
346}
347
348template<typename T1, typename T2>
349static inline void EncodeTuple(std::vector<uint8_t>* out, const std::tuple<T1, T2>& t) {
350 EncodeUnsignedLeb128(out, std::get<0>(t));
351 EncodeUnsignedLeb128(out, std::get<1>(t));
352}
353
354template<typename T1, typename T2>
355static inline void DecodeTuple(const uint8_t** in, const uint8_t* end, std::tuple<T1, T2>* t) {
356 T1 v1 = static_cast<T1>(DecodeUint32WithOverflowCheck(in, end));
357 T2 v2 = static_cast<T2>(DecodeUint32WithOverflowCheck(in, end));
358 *t = std::make_tuple(v1, v2);
359}
360
361template<typename T1, typename T2, typename T3>
362static inline void EncodeTuple(std::vector<uint8_t>* out, const std::tuple<T1, T2, T3>& t) {
363 EncodeUnsignedLeb128(out, std::get<0>(t));
364 EncodeUnsignedLeb128(out, std::get<1>(t));
365 EncodeUnsignedLeb128(out, std::get<2>(t));
366}
367
368template<typename T1, typename T2, typename T3>
369static inline void DecodeTuple(const uint8_t** in, const uint8_t* end, std::tuple<T1, T2, T3>* t) {
370 T1 v1 = static_cast<T1>(DecodeUint32WithOverflowCheck(in, end));
371 T2 v2 = static_cast<T2>(DecodeUint32WithOverflowCheck(in, end));
372 T3 v3 = static_cast<T2>(DecodeUint32WithOverflowCheck(in, end));
373 *t = std::make_tuple(v1, v2, v3);
374}
375
376template<typename T>
377static inline void EncodeSet(std::vector<uint8_t>* out, const std::set<T>& set) {
378 EncodeUnsignedLeb128(out, set.size());
379 for (const T& entry : set) {
380 EncodeTuple(out, entry);
381 }
382}
383
Nicolas Geoffray08025182016-10-25 17:20:18 +0100384static inline void EncodeUint16Vector(std::vector<uint8_t>* out,
385 const std::vector<uint16_t>& vector) {
386 EncodeUnsignedLeb128(out, vector.size());
387 for (uint16_t entry : vector) {
388 EncodeUnsignedLeb128(out, entry);
389 }
390}
391
David Brazdil6f82fbd2016-09-14 11:55:26 +0100392template<typename T>
393static inline void DecodeSet(const uint8_t** in, const uint8_t* end, std::set<T>* set) {
394 DCHECK(set->empty());
395 size_t num_entries = DecodeUint32WithOverflowCheck(in, end);
396 for (size_t i = 0; i < num_entries; ++i) {
397 T tuple;
398 DecodeTuple(in, end, &tuple);
399 set->emplace(tuple);
400 }
401}
402
Nicolas Geoffray08025182016-10-25 17:20:18 +0100403static inline void DecodeUint16Vector(const uint8_t** in,
404 const uint8_t* end,
405 std::vector<uint16_t>* vector) {
406 DCHECK(vector->empty());
407 size_t num_entries = DecodeUint32WithOverflowCheck(in, end);
408 vector->reserve(num_entries);
409 for (size_t i = 0; i < num_entries; ++i) {
410 vector->push_back(dchecked_integral_cast<uint16_t>(DecodeUint32WithOverflowCheck(in, end)));
411 }
412}
413
David Brazdil6f82fbd2016-09-14 11:55:26 +0100414static inline void EncodeStringVector(std::vector<uint8_t>* out,
415 const std::vector<std::string>& strings) {
416 EncodeUnsignedLeb128(out, strings.size());
417 for (const std::string& str : strings) {
418 const uint8_t* data = reinterpret_cast<const uint8_t*>(str.c_str());
419 size_t length = str.length() + 1;
420 out->insert(out->end(), data, data + length);
421 DCHECK_EQ(0u, out->back());
422 }
423}
424
425static inline void DecodeStringVector(const uint8_t** in,
426 const uint8_t* end,
427 std::vector<std::string>* strings) {
428 DCHECK(strings->empty());
429 size_t num_strings = DecodeUint32WithOverflowCheck(in, end);
430 strings->reserve(num_strings);
431 for (size_t i = 0; i < num_strings; ++i) {
432 CHECK_LT(*in, end);
433 const char* string_start = reinterpret_cast<const char*>(*in);
434 strings->emplace_back(std::string(string_start));
435 *in += strings->back().length() + 1;
436 }
437}
438
Nicolas Geoffrayd01f60c2016-10-28 14:45:48 +0100439void VerifierDeps::Encode(const std::vector<const DexFile*>& dex_files,
440 std::vector<uint8_t>* buffer) const {
David Brazdil6f82fbd2016-09-14 11:55:26 +0100441 MutexLock mu(Thread::Current(), *Locks::verifier_deps_lock_);
Nicolas Geoffrayd01f60c2016-10-28 14:45:48 +0100442 for (const DexFile* dex_file : dex_files) {
443 const DexFileDeps& deps = *GetDexFileDeps(*dex_file);
444 EncodeStringVector(buffer, deps.strings_);
445 EncodeSet(buffer, deps.assignable_types_);
446 EncodeSet(buffer, deps.unassignable_types_);
447 EncodeSet(buffer, deps.classes_);
448 EncodeSet(buffer, deps.fields_);
449 EncodeSet(buffer, deps.direct_methods_);
450 EncodeSet(buffer, deps.virtual_methods_);
451 EncodeSet(buffer, deps.interface_methods_);
452 EncodeUint16Vector(buffer, deps.unverified_classes_);
David Brazdil6f82fbd2016-09-14 11:55:26 +0100453 }
454}
455
Nicolas Geoffraye70dd562016-10-30 21:03:35 +0000456VerifierDeps::VerifierDeps(const std::vector<const DexFile*>& dex_files,
457 ArrayRef<const uint8_t> data)
David Brazdil6f82fbd2016-09-14 11:55:26 +0100458 : VerifierDeps(dex_files) {
Nicolas Geoffraye70dd562016-10-30 21:03:35 +0000459 if (data.empty()) {
460 // Return eagerly, as the first thing we expect from VerifierDeps data is
461 // the number of created strings, even if there is no dependency.
462 // Currently, only the boot image does not have any VerifierDeps data.
463 return;
464 }
David Brazdil6f82fbd2016-09-14 11:55:26 +0100465 const uint8_t* data_start = data.data();
466 const uint8_t* data_end = data_start + data.size();
Nicolas Geoffrayd01f60c2016-10-28 14:45:48 +0100467 for (const DexFile* dex_file : dex_files) {
468 DexFileDeps* deps = GetDexFileDeps(*dex_file);
469 DecodeStringVector(&data_start, data_end, &deps->strings_);
470 DecodeSet(&data_start, data_end, &deps->assignable_types_);
471 DecodeSet(&data_start, data_end, &deps->unassignable_types_);
472 DecodeSet(&data_start, data_end, &deps->classes_);
473 DecodeSet(&data_start, data_end, &deps->fields_);
474 DecodeSet(&data_start, data_end, &deps->direct_methods_);
475 DecodeSet(&data_start, data_end, &deps->virtual_methods_);
476 DecodeSet(&data_start, data_end, &deps->interface_methods_);
477 DecodeUint16Vector(&data_start, data_end, &deps->unverified_classes_);
David Brazdil6f82fbd2016-09-14 11:55:26 +0100478 }
479 CHECK_LE(data_start, data_end);
480}
481
482bool VerifierDeps::Equals(const VerifierDeps& rhs) const {
483 MutexLock mu(Thread::Current(), *Locks::verifier_deps_lock_);
484
485 if (dex_deps_.size() != rhs.dex_deps_.size()) {
486 return false;
487 }
488
489 auto lhs_it = dex_deps_.begin();
490 auto rhs_it = rhs.dex_deps_.begin();
491
492 for (; (lhs_it != dex_deps_.end()) && (rhs_it != rhs.dex_deps_.end()); lhs_it++, rhs_it++) {
493 const DexFile* lhs_dex_file = lhs_it->first;
494 const DexFile* rhs_dex_file = rhs_it->first;
495 if (lhs_dex_file != rhs_dex_file) {
496 return false;
497 }
498
499 DexFileDeps* lhs_deps = lhs_it->second.get();
500 DexFileDeps* rhs_deps = rhs_it->second.get();
501 if (!lhs_deps->Equals(*rhs_deps)) {
502 return false;
503 }
504 }
505
506 DCHECK((lhs_it == dex_deps_.end()) && (rhs_it == rhs.dex_deps_.end()));
507 return true;
508}
509
510bool VerifierDeps::DexFileDeps::Equals(const VerifierDeps::DexFileDeps& rhs) const {
511 return (strings_ == rhs.strings_) &&
512 (assignable_types_ == rhs.assignable_types_) &&
513 (unassignable_types_ == rhs.unassignable_types_) &&
514 (classes_ == rhs.classes_) &&
515 (fields_ == rhs.fields_) &&
516 (direct_methods_ == rhs.direct_methods_) &&
517 (virtual_methods_ == rhs.virtual_methods_) &&
Nicolas Geoffray08025182016-10-25 17:20:18 +0100518 (interface_methods_ == rhs.interface_methods_) &&
519 (unverified_classes_ == rhs.unverified_classes_);
David Brazdil6f82fbd2016-09-14 11:55:26 +0100520}
521
Nicolas Geoffrayd01f60c2016-10-28 14:45:48 +0100522void VerifierDeps::Dump(VariableIndentationOutputStream* vios) const {
523 for (const auto& dep : dex_deps_) {
524 const DexFile& dex_file = *dep.first;
525 vios->Stream()
526 << "Dependencies of "
527 << dex_file.GetLocation()
528 << ":\n";
529
530 ScopedIndentation indent(vios);
531
532 for (const std::string& str : dep.second->strings_) {
533 vios->Stream() << "Extra string: " << str << "\n";
534 }
535
536 for (const TypeAssignability& entry : dep.second->assignable_types_) {
537 vios->Stream()
538 << GetStringFromId(dex_file, entry.GetSource())
539 << " must be assignable to "
540 << GetStringFromId(dex_file, entry.GetDestination())
541 << "\n";
542 }
543
544 for (const TypeAssignability& entry : dep.second->unassignable_types_) {
545 vios->Stream()
546 << GetStringFromId(dex_file, entry.GetSource())
547 << " must not be assignable to "
548 << GetStringFromId(dex_file, entry.GetDestination())
549 << "\n";
550 }
551
552 for (const ClassResolution& entry : dep.second->classes_) {
553 vios->Stream()
554 << dex_file.StringByTypeIdx(entry.GetDexTypeIndex())
555 << (entry.IsResolved() ? " must be resolved " : "must not be resolved ")
556 << " with access flags " << std::hex << entry.GetAccessFlags() << std::dec
557 << "\n";
558 }
559
560 for (const FieldResolution& entry : dep.second->fields_) {
561 const DexFile::FieldId& field_id = dex_file.GetFieldId(entry.GetDexFieldIndex());
562 vios->Stream()
563 << dex_file.GetFieldDeclaringClassDescriptor(field_id) << "->"
564 << dex_file.GetFieldName(field_id) << ":"
565 << dex_file.GetFieldTypeDescriptor(field_id)
566 << " is expected to be ";
567 if (!entry.IsResolved()) {
568 vios->Stream() << "unresolved\n";
569 } else {
570 vios->Stream()
571 << "in class "
572 << GetStringFromId(dex_file, entry.GetDeclaringClassIndex())
573 << ", and have the access flags " << std::hex << entry.GetAccessFlags() << std::dec
574 << "\n";
575 }
576 }
577
578 for (const auto& entry :
579 { std::make_pair(kDirectMethodResolution, dep.second->direct_methods_),
580 std::make_pair(kVirtualMethodResolution, dep.second->virtual_methods_),
581 std::make_pair(kInterfaceMethodResolution, dep.second->interface_methods_) }) {
582 for (const MethodResolution& method : entry.second) {
583 const DexFile::MethodId& method_id = dex_file.GetMethodId(method.GetDexMethodIndex());
584 vios->Stream()
585 << dex_file.GetMethodDeclaringClassDescriptor(method_id) << "->"
586 << dex_file.GetMethodName(method_id)
587 << dex_file.GetMethodSignature(method_id).ToString()
588 << " is expected to be ";
589 if (!method.IsResolved()) {
590 vios->Stream() << "unresolved\n";
591 } else {
592 vios->Stream()
593 << "in class "
594 << GetStringFromId(dex_file, method.GetDeclaringClassIndex())
595 << ", have the access flags " << std::hex << method.GetAccessFlags() << std::dec
596 << ", and be of kind " << entry.first
597 << "\n";
598 }
599 }
600 }
601
602 for (uint16_t type_index : dep.second->unverified_classes_) {
603 vios->Stream()
604 << dex_file.StringByTypeIdx(type_index)
605 << " is expected to be verified at runtime\n";
606 }
607 }
608}
609
Nicolas Geoffray8904b6f2016-10-28 19:50:34 +0100610bool VerifierDeps::Verify(Handle<mirror::ClassLoader> class_loader, Thread* self) const {
611 for (const auto& entry : dex_deps_) {
612 if (!VerifyDexFile(class_loader, *entry.first, *entry.second, self)) {
613 return false;
614 }
615 }
616 return true;
617}
618
619// TODO: share that helper with other parts of the compiler that have
620// the same lookup pattern.
621static mirror::Class* FindClassAndClearException(ClassLinker* class_linker,
622 Thread* self,
623 const char* name,
624 Handle<mirror::ClassLoader> class_loader)
625 REQUIRES_SHARED(Locks::mutator_lock_) {
626 mirror::Class* result = class_linker->FindClass(self, name, class_loader);
627 if (result == nullptr) {
628 DCHECK(self->IsExceptionPending());
629 self->ClearException();
630 }
631 return result;
632}
633
634bool VerifierDeps::VerifyAssignability(Handle<mirror::ClassLoader> class_loader,
635 const DexFile& dex_file,
636 const std::set<TypeAssignability>& assignables,
637 bool expected_assignability,
638 Thread* self) const {
639 StackHandleScope<2> hs(self);
640 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
641 MutableHandle<mirror::Class> source(hs.NewHandle<mirror::Class>(nullptr));
642 MutableHandle<mirror::Class> destination(hs.NewHandle<mirror::Class>(nullptr));
643
644 for (const auto& entry : assignables) {
645 const std::string& destination_desc = GetStringFromId(dex_file, entry.GetDestination());
646 destination.Assign(
647 FindClassAndClearException(class_linker, self, destination_desc.c_str(), class_loader));
648 const std::string& source_desc = GetStringFromId(dex_file, entry.GetSource());
649 source.Assign(
650 FindClassAndClearException(class_linker, self, source_desc.c_str(), class_loader));
651
652 if (destination.Get() == nullptr) {
653 LOG(INFO) << "VerifiersDeps: Could not resolve class " << destination_desc;
654 return false;
655 }
656
657 if (source.Get() == nullptr) {
658 LOG(INFO) << "VerifierDeps: Could not resolve class " << source_desc;
659 return false;
660 }
661
662 DCHECK(destination->IsResolved() && source->IsResolved());
663 if (destination->IsAssignableFrom(source.Get()) != expected_assignability) {
664 LOG(INFO) << "VerifierDeps: Class "
665 << destination_desc
666 << (expected_assignability ? " not " : " ")
667 << "assignable from "
668 << source_desc;
669 return false;
670 }
671 }
672 return true;
673}
674
675bool VerifierDeps::VerifyClasses(Handle<mirror::ClassLoader> class_loader,
676 const DexFile& dex_file,
677 const std::set<ClassResolution>& classes,
678 Thread* self) const {
679 StackHandleScope<1> hs(self);
680 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
681 MutableHandle<mirror::Class> cls(hs.NewHandle<mirror::Class>(nullptr));
682 for (const auto& entry : classes) {
683 const char* descriptor = dex_file.StringByTypeIdx(entry.GetDexTypeIndex());
684 cls.Assign(FindClassAndClearException(class_linker, self, descriptor, class_loader));
685
686 if (entry.IsResolved()) {
687 if (cls.Get() == nullptr) {
688 LOG(INFO) << "VerifierDeps: Could not resolve class " << descriptor;
689 return false;
690 } else if (entry.GetAccessFlags() != GetAccessFlags(cls.Get())) {
691 LOG(INFO) << "VerifierDeps: Unexpected access flags on class "
692 << descriptor
693 << std::hex
694 << " (expected="
695 << entry.GetAccessFlags()
696 << ", actual="
697 << GetAccessFlags(cls.Get()) << ")"
698 << std::dec;
699 return false;
700 }
701 } else if (cls.Get() != nullptr) {
702 LOG(INFO) << "VerifierDeps: Unexpected successful resolution of class " << descriptor;
703 return false;
704 }
705 }
706 return true;
707}
708
709static std::string GetFieldDescription(const DexFile& dex_file, uint32_t index) {
710 const DexFile::FieldId& field_id = dex_file.GetFieldId(index);
711 return std::string(dex_file.GetFieldDeclaringClassDescriptor(field_id))
712 + "->"
713 + dex_file.GetFieldName(field_id)
714 + ":"
715 + dex_file.GetFieldTypeDescriptor(field_id);
716}
717
718bool VerifierDeps::VerifyFields(Handle<mirror::ClassLoader> class_loader,
719 const DexFile& dex_file,
720 const std::set<FieldResolution>& fields,
721 Thread* self) const {
722 // Check recorded fields are resolved the same way, have the same recorded class,
723 // and have the same recorded flags.
724 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
725 StackHandleScope<1> hs(self);
726 Handle<mirror::DexCache> dex_cache(
727 hs.NewHandle(class_linker->FindDexCache(self, dex_file, /* allow_failure */ false)));
728 for (const auto& entry : fields) {
729 ArtField* field = class_linker->ResolveFieldJLS(
730 dex_file, entry.GetDexFieldIndex(), dex_cache, class_loader);
731
732 if (field == nullptr) {
733 DCHECK(self->IsExceptionPending());
734 self->ClearException();
735 }
736
737 if (entry.IsResolved()) {
738 std::string expected_decl_klass = GetStringFromId(dex_file, entry.GetDeclaringClassIndex());
739 std::string temp;
740 if (field == nullptr) {
741 LOG(INFO) << "VerifierDeps: Could not resolve field "
742 << GetFieldDescription(dex_file, entry.GetDexFieldIndex());
743 return false;
744 } else if (expected_decl_klass != field->GetDeclaringClass()->GetDescriptor(&temp)) {
745 LOG(INFO) << "VerifierDeps: Unexpected declaring class for field resolution "
746 << GetFieldDescription(dex_file, entry.GetDexFieldIndex())
747 << " (expected=" << expected_decl_klass
748 << ", actual=" << field->GetDeclaringClass()->GetDescriptor(&temp) << ")";
749 return false;
750 } else if (entry.GetAccessFlags() != GetAccessFlags(field)) {
751 LOG(INFO) << "VerifierDeps: Unexpected access flags for resolved field "
752 << GetFieldDescription(dex_file, entry.GetDexFieldIndex())
753 << std::hex << " (expected=" << entry.GetAccessFlags()
754 << ", actual=" << GetAccessFlags(field) << ")" << std::dec;
755 return false;
756 }
757 } else if (field != nullptr) {
758 LOG(INFO) << "VerifierDeps: Unexpected successful resolution of field "
759 << GetFieldDescription(dex_file, entry.GetDexFieldIndex());
760 return false;
761 }
762 }
763 return true;
764}
765
766static std::string GetMethodDescription(const DexFile& dex_file, uint32_t index) {
767 const DexFile::MethodId& method_id = dex_file.GetMethodId(index);
768 return std::string(dex_file.GetMethodDeclaringClassDescriptor(method_id))
769 + "->"
770 + dex_file.GetMethodName(method_id)
771 + dex_file.GetMethodSignature(method_id).ToString();
772}
773
774bool VerifierDeps::VerifyMethods(Handle<mirror::ClassLoader> class_loader,
775 const DexFile& dex_file,
776 const std::set<MethodResolution>& methods,
777 MethodResolutionKind kind,
778 Thread* self) const {
779 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
780 PointerSize pointer_size = class_linker->GetImagePointerSize();
781
782 for (const auto& entry : methods) {
783 const DexFile::MethodId& method_id = dex_file.GetMethodId(entry.GetDexMethodIndex());
784
785 const char* name = dex_file.GetMethodName(method_id);
786 const Signature signature = dex_file.GetMethodSignature(method_id);
787 const char* descriptor = dex_file.GetMethodDeclaringClassDescriptor(method_id);
788
789 mirror::Class* cls = FindClassAndClearException(class_linker, self, descriptor, class_loader);
790 if (cls == nullptr) {
791 LOG(INFO) << "VerifierDeps: Could not resolve class " << descriptor;
792 return false;
793 }
794 DCHECK(cls->IsResolved());
795 ArtMethod* method = nullptr;
796 if (kind == kDirectMethodResolution) {
797 method = cls->FindDirectMethod(name, signature, pointer_size);
798 } else if (kind == kVirtualMethodResolution) {
799 method = cls->FindVirtualMethod(name, signature, pointer_size);
800 } else {
801 DCHECK_EQ(kind, kInterfaceMethodResolution);
802 method = cls->FindInterfaceMethod(name, signature, pointer_size);
803 }
804
805 if (entry.IsResolved()) {
806 std::string temp;
807 std::string expected_decl_klass = GetStringFromId(dex_file, entry.GetDeclaringClassIndex());
808 if (method == nullptr) {
809 LOG(INFO) << "VerifierDeps: Could not resolve "
810 << kind
811 << " method "
812 << GetMethodDescription(dex_file, entry.GetDexMethodIndex());
813 return false;
814 } else if (expected_decl_klass != method->GetDeclaringClass()->GetDescriptor(&temp)) {
815 LOG(INFO) << "VerifierDeps: Unexpected declaring class for "
816 << kind
817 << " method resolution "
818 << GetMethodDescription(dex_file, entry.GetDexMethodIndex())
819 << " (expected="
820 << expected_decl_klass
821 << ", actual="
822 << method->GetDeclaringClass()->GetDescriptor(&temp)
823 << ")";
824 return false;
825 } else if (entry.GetAccessFlags() != GetAccessFlags(method)) {
826 LOG(INFO) << "VerifierDeps: Unexpected access flags for resolved "
827 << kind
828 << " method resolution "
829 << GetMethodDescription(dex_file, entry.GetDexMethodIndex())
830 << std::hex
831 << " (expected="
832 << entry.GetAccessFlags()
833 << ", actual="
834 << GetAccessFlags(method) << ")"
835 << std::dec;
836 return false;
837 }
838 } else if (method != nullptr) {
839 LOG(INFO) << "VerifierDeps: Unexpected successful resolution of "
840 << kind
841 << " method "
842 << GetMethodDescription(dex_file, entry.GetDexMethodIndex());
843 return false;
844 }
845 }
846 return true;
847}
848
849bool VerifierDeps::VerifyDexFile(Handle<mirror::ClassLoader> class_loader,
850 const DexFile& dex_file,
851 const DexFileDeps& deps,
852 Thread* self) const {
853 bool result = VerifyAssignability(
854 class_loader, dex_file, deps.assignable_types_, /* expected_assignability */ true, self);
855 result = result && VerifyAssignability(
856 class_loader, dex_file, deps.unassignable_types_, /* expected_assignability */ false, self);
857
858 result = result && VerifyClasses(class_loader, dex_file, deps.classes_, self);
859 result = result && VerifyFields(class_loader, dex_file, deps.fields_, self);
860
861 result = result && VerifyMethods(
862 class_loader, dex_file, deps.direct_methods_, kDirectMethodResolution, self);
863 result = result && VerifyMethods(
864 class_loader, dex_file, deps.virtual_methods_, kVirtualMethodResolution, self);
865 result = result && VerifyMethods(
866 class_loader, dex_file, deps.interface_methods_, kInterfaceMethodResolution, self);
867
868 return result;
869}
870
David Brazdilca3c8c32016-09-06 14:04:48 +0100871} // namespace verifier
872} // namespace art