blob: e716cdbed85e99e34c4e5745c158c60dda96696f [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
Nicolas Geoffray08025182016-10-25 17:20:18 +010017// Test is in compiler, as it uses compiler related code.
18#include "verifier/verifier_deps.h"
David Brazdilca3c8c32016-09-06 14:04:48 +010019
20#include "class_linker.h"
Nicolas Geoffray08025182016-10-25 17:20:18 +010021#include "compiler/common_compiler_test.h"
Nicolas Geoffray1d0ae3f2016-12-06 13:40:16 +000022#include "compiler/dex/verification_results.h"
23#include "compiler/dex/verified_method.h"
Nicolas Geoffray08025182016-10-25 17:20:18 +010024#include "compiler/driver/compiler_options.h"
25#include "compiler/driver/compiler_driver.h"
Nicolas Geoffray1d0ae3f2016-12-06 13:40:16 +000026#include "compiler/utils/atomic_method_ref_map-inl.h"
David Brazdilca3c8c32016-09-06 14:04:48 +010027#include "compiler_callbacks.h"
28#include "dex_file.h"
Andreas Gampea5b09a62016-11-17 15:21:22 -080029#include "dex_file_types.h"
David Brazdilca3c8c32016-09-06 14:04:48 +010030#include "handle_scope-inl.h"
Nicolas Geoffray08025182016-10-25 17:20:18 +010031#include "verifier/method_verifier-inl.h"
David Brazdilca3c8c32016-09-06 14:04:48 +010032#include "mirror/class_loader.h"
33#include "runtime.h"
34#include "thread.h"
Mathieu Chartier0795f232016-09-27 18:43:30 -070035#include "scoped_thread_state_change-inl.h"
David Brazdilca3c8c32016-09-06 14:04:48 +010036
37namespace art {
38namespace verifier {
39
40class VerifierDepsCompilerCallbacks : public CompilerCallbacks {
41 public:
42 explicit VerifierDepsCompilerCallbacks()
43 : CompilerCallbacks(CompilerCallbacks::CallbackMode::kCompileApp),
44 deps_(nullptr) {}
45
46 void MethodVerified(verifier::MethodVerifier* verifier ATTRIBUTE_UNUSED) OVERRIDE {}
47 void ClassRejected(ClassReference ref ATTRIBUTE_UNUSED) OVERRIDE {}
48 bool IsRelocationPossible() OVERRIDE { return false; }
49
50 verifier::VerifierDeps* GetVerifierDeps() const OVERRIDE { return deps_; }
51 void SetVerifierDeps(verifier::VerifierDeps* deps) { deps_ = deps; }
52
53 private:
54 verifier::VerifierDeps* deps_;
55};
56
Nicolas Geoffray08025182016-10-25 17:20:18 +010057class VerifierDepsTest : public CommonCompilerTest {
David Brazdilca3c8c32016-09-06 14:04:48 +010058 public:
59 void SetUpRuntimeOptions(RuntimeOptions* options) {
Nicolas Geoffray08025182016-10-25 17:20:18 +010060 CommonCompilerTest::SetUpRuntimeOptions(options);
David Brazdilca3c8c32016-09-06 14:04:48 +010061 callbacks_.reset(new VerifierDepsCompilerCallbacks());
62 }
63
64 mirror::Class* FindClassByName(const std::string& name, ScopedObjectAccess* soa)
65 REQUIRES_SHARED(Locks::mutator_lock_) {
66 StackHandleScope<1> hs(Thread::Current());
67 Handle<mirror::ClassLoader> class_loader_handle(
Mathieu Chartier0795f232016-09-27 18:43:30 -070068 hs.NewHandle(soa->Decode<mirror::ClassLoader>(class_loader_)));
David Brazdil6f82fbd2016-09-14 11:55:26 +010069 mirror::Class* klass = class_linker_->FindClass(Thread::Current(),
70 name.c_str(),
71 class_loader_handle);
72 if (klass == nullptr) {
73 DCHECK(Thread::Current()->IsExceptionPending());
74 Thread::Current()->ClearException();
75 }
76 return klass;
77 }
78
Nicolas Geoffray6bb7f1b2016-11-03 10:52:49 +000079 void SetupCompilerDriver() {
80 compiler_options_->boot_image_ = false;
81 compiler_driver_->InitializeThreadPools();
82 }
83
84 void VerifyWithCompilerDriver(verifier::VerifierDeps* deps) {
85 TimingLogger timings("Verify", false, false);
86 // The compiler driver handles the verifier deps in the callbacks, so
87 // remove what this class did for unit testing.
88 verifier_deps_.reset(nullptr);
Nicolas Geoffrayb0bbe8e2016-11-19 10:42:37 +000089 callbacks_->SetVerifierDeps(deps);
90 compiler_driver_->Verify(class_loader_, dex_files_, &timings);
Nicolas Geoffray6bb7f1b2016-11-03 10:52:49 +000091 // The compiler driver may have updated the VerifierDeps in the callback object.
Nicolas Geoffrayb0bbe8e2016-11-19 10:42:37 +000092 if (callbacks_->GetVerifierDeps() != deps) {
93 verifier_deps_.reset(callbacks_->GetVerifierDeps());
94 }
95 callbacks_->SetVerifierDeps(nullptr);
Nicolas Geoffray1d0ae3f2016-12-06 13:40:16 +000096 // Clear entries in the verification results to avoid hitting a DCHECK that
97 // we always succeed inserting a new entry after verifying.
98 AtomicMethodRefMap<const VerifiedMethod*>* map =
99 &compiler_driver_->GetVerificationResults()->atomic_verified_methods_;
100 map->Visit([](const MethodReference& ref ATTRIBUTE_UNUSED, const VerifiedMethod* method) {
101 delete method;
102 });
103 map->ClearEntries();
Nicolas Geoffray6bb7f1b2016-11-03 10:52:49 +0000104 }
105
David Brazdil6f82fbd2016-09-14 11:55:26 +0100106 void SetVerifierDeps(const std::vector<const DexFile*>& dex_files) {
107 verifier_deps_.reset(new verifier::VerifierDeps(dex_files));
108 VerifierDepsCompilerCallbacks* callbacks =
109 reinterpret_cast<VerifierDepsCompilerCallbacks*>(callbacks_.get());
110 callbacks->SetVerifierDeps(verifier_deps_.get());
David Brazdilca3c8c32016-09-06 14:04:48 +0100111 }
112
Nicolas Geoffrayd01f60c2016-10-28 14:45:48 +0100113 void LoadDexFile(ScopedObjectAccess* soa, const char* name1, const char* name2 = nullptr)
114 REQUIRES_SHARED(Locks::mutator_lock_) {
115 class_loader_ = (name2 == nullptr) ? LoadDex(name1) : LoadMultiDex(name1, name2);
116 dex_files_ = GetDexFiles(class_loader_);
117 primary_dex_file_ = dex_files_.front();
118
119 SetVerifierDeps(dex_files_);
120 StackHandleScope<1> hs(soa->Self());
121 Handle<mirror::ClassLoader> loader =
122 hs.NewHandle(soa->Decode<mirror::ClassLoader>(class_loader_));
123 for (const DexFile* dex_file : dex_files_) {
124 class_linker_->RegisterDexFile(*dex_file, loader.Get());
125 }
Nicolas Geoffray1d0ae3f2016-12-06 13:40:16 +0000126 for (const DexFile* dex_file : dex_files_) {
127 compiler_driver_->GetVerificationResults()->AddDexFile(dex_file);
128 }
Nicolas Geoffrayd01f60c2016-10-28 14:45:48 +0100129 }
130
David Brazdilca3c8c32016-09-06 14:04:48 +0100131 void LoadDexFile(ScopedObjectAccess* soa) REQUIRES_SHARED(Locks::mutator_lock_) {
Nicolas Geoffrayd01f60c2016-10-28 14:45:48 +0100132 LoadDexFile(soa, "VerifierDeps");
133 CHECK_EQ(dex_files_.size(), 1u);
David Brazdilca3c8c32016-09-06 14:04:48 +0100134 klass_Main_ = FindClassByName("LMain;", soa);
135 CHECK(klass_Main_ != nullptr);
David Brazdilca3c8c32016-09-06 14:04:48 +0100136 }
137
138 bool VerifyMethod(const std::string& method_name) {
139 ScopedObjectAccess soa(Thread::Current());
140 LoadDexFile(&soa);
141
Nicolas Geoffrayd01f60c2016-10-28 14:45:48 +0100142 StackHandleScope<2> hs(soa.Self());
David Brazdilca3c8c32016-09-06 14:04:48 +0100143 Handle<mirror::ClassLoader> class_loader_handle(
Mathieu Chartier0795f232016-09-27 18:43:30 -0700144 hs.NewHandle(soa.Decode<mirror::ClassLoader>(class_loader_)));
David Brazdilca3c8c32016-09-06 14:04:48 +0100145 Handle<mirror::DexCache> dex_cache_handle(hs.NewHandle(klass_Main_->GetDexCache()));
146
147 const DexFile::ClassDef* class_def = klass_Main_->GetClassDef();
Nicolas Geoffrayd01f60c2016-10-28 14:45:48 +0100148 const uint8_t* class_data = primary_dex_file_->GetClassData(*class_def);
David Brazdilca3c8c32016-09-06 14:04:48 +0100149 CHECK(class_data != nullptr);
150
Nicolas Geoffrayd01f60c2016-10-28 14:45:48 +0100151 ClassDataItemIterator it(*primary_dex_file_, class_data);
David Brazdilca3c8c32016-09-06 14:04:48 +0100152 while (it.HasNextStaticField() || it.HasNextInstanceField()) {
153 it.Next();
154 }
155
156 ArtMethod* method = nullptr;
157 while (it.HasNextDirectMethod()) {
158 ArtMethod* resolved_method = class_linker_->ResolveMethod<ClassLinker::kNoICCECheckForCache>(
Nicolas Geoffrayd01f60c2016-10-28 14:45:48 +0100159 *primary_dex_file_,
David Brazdilca3c8c32016-09-06 14:04:48 +0100160 it.GetMemberIndex(),
161 dex_cache_handle,
162 class_loader_handle,
163 nullptr,
164 it.GetMethodInvokeType(*class_def));
165 CHECK(resolved_method != nullptr);
166 if (method_name == resolved_method->GetName()) {
167 method = resolved_method;
168 break;
169 }
170 it.Next();
171 }
172 CHECK(method != nullptr);
173
Nicolas Geoffray340dafa2016-11-18 16:03:10 +0000174 Thread::Current()->SetVerifierDeps(callbacks_->GetVerifierDeps());
David Brazdilca3c8c32016-09-06 14:04:48 +0100175 MethodVerifier verifier(Thread::Current(),
Nicolas Geoffrayd01f60c2016-10-28 14:45:48 +0100176 primary_dex_file_,
David Brazdilca3c8c32016-09-06 14:04:48 +0100177 dex_cache_handle,
178 class_loader_handle,
179 *class_def,
180 it.GetMethodCodeItem(),
181 it.GetMemberIndex(),
182 method,
183 it.GetMethodAccessFlags(),
184 true /* can_load_classes */,
185 true /* allow_soft_failures */,
186 true /* need_precise_constants */,
187 false /* verify to dump */,
188 true /* allow_thread_suspension */);
189 verifier.Verify();
Nicolas Geoffray340dafa2016-11-18 16:03:10 +0000190 Thread::Current()->SetVerifierDeps(nullptr);
David Brazdilca3c8c32016-09-06 14:04:48 +0100191 return !verifier.HasFailures();
192 }
193
Nicolas Geoffrayd01f60c2016-10-28 14:45:48 +0100194 void VerifyDexFile(const char* multidex = nullptr) {
Nicolas Geoffray08025182016-10-25 17:20:18 +0100195 {
196 ScopedObjectAccess soa(Thread::Current());
Nicolas Geoffrayd01f60c2016-10-28 14:45:48 +0100197 LoadDexFile(&soa, "VerifierDeps", multidex);
David Brazdil6f82fbd2016-09-14 11:55:26 +0100198 }
Nicolas Geoffray6bb7f1b2016-11-03 10:52:49 +0000199 SetupCompilerDriver();
200 VerifyWithCompilerDriver(/* verifier_deps */ nullptr);
David Brazdil6f82fbd2016-09-14 11:55:26 +0100201 }
202
David Brazdilca3c8c32016-09-06 14:04:48 +0100203 bool TestAssignabilityRecording(const std::string& dst,
204 const std::string& src,
205 bool is_strict,
206 bool is_assignable) {
207 ScopedObjectAccess soa(Thread::Current());
208 LoadDexFile(&soa);
David Brazdil6f82fbd2016-09-14 11:55:26 +0100209 mirror::Class* klass_dst = FindClassByName(dst, &soa);
210 DCHECK(klass_dst != nullptr);
211 mirror::Class* klass_src = FindClassByName(src, &soa);
212 DCHECK(klass_src != nullptr);
Nicolas Geoffrayd01f60c2016-10-28 14:45:48 +0100213 verifier_deps_->AddAssignability(*primary_dex_file_,
David Brazdil6f82fbd2016-09-14 11:55:26 +0100214 klass_dst,
215 klass_src,
David Brazdilca3c8c32016-09-06 14:04:48 +0100216 is_strict,
217 is_assignable);
218 return true;
219 }
220
Nicolas Geoffray6bb7f1b2016-11-03 10:52:49 +0000221 // Check that the status of classes in `class_loader_` match the
222 // expected status in `deps`.
223 void VerifyClassStatus(const verifier::VerifierDeps& deps) {
224 ScopedObjectAccess soa(Thread::Current());
225 StackHandleScope<2> hs(soa.Self());
226 Handle<mirror::ClassLoader> class_loader_handle(
227 hs.NewHandle(soa.Decode<mirror::ClassLoader>(class_loader_)));
228 MutableHandle<mirror::Class> cls(hs.NewHandle<mirror::Class>(nullptr));
229 for (const DexFile* dex_file : dex_files_) {
Andreas Gampea5b09a62016-11-17 15:21:22 -0800230 const std::vector<dex::TypeIndex>& unverified_classes = deps.GetUnverifiedClasses(*dex_file);
231 std::set<dex::TypeIndex> set(unverified_classes.begin(), unverified_classes.end());
Nicolas Geoffray6bb7f1b2016-11-03 10:52:49 +0000232 for (uint32_t i = 0; i < dex_file->NumClassDefs(); ++i) {
233 const DexFile::ClassDef& class_def = dex_file->GetClassDef(i);
234 const char* descriptor = dex_file->GetClassDescriptor(class_def);
235 cls.Assign(class_linker_->FindClass(soa.Self(), descriptor, class_loader_handle));
236 if (cls.Get() == nullptr) {
237 CHECK(soa.Self()->IsExceptionPending());
238 soa.Self()->ClearException();
239 } else if (set.find(class_def.class_idx_) == set.end()) {
240 ASSERT_EQ(cls->GetStatus(), mirror::Class::kStatusVerified);
241 } else {
242 ASSERT_LT(cls->GetStatus(), mirror::Class::kStatusVerified);
243 }
244 }
245 }
246 }
247
Nicolas Geoffray08025182016-10-25 17:20:18 +0100248 bool HasUnverifiedClass(const std::string& cls) {
Nicolas Geoffrayd01f60c2016-10-28 14:45:48 +0100249 const DexFile::TypeId* type_id = primary_dex_file_->FindTypeId(cls.c_str());
Nicolas Geoffray08025182016-10-25 17:20:18 +0100250 DCHECK(type_id != nullptr);
Andreas Gampea5b09a62016-11-17 15:21:22 -0800251 dex::TypeIndex index = primary_dex_file_->GetIndexForTypeId(*type_id);
Nicolas Geoffray08025182016-10-25 17:20:18 +0100252 for (const auto& dex_dep : verifier_deps_->dex_deps_) {
Andreas Gampea5b09a62016-11-17 15:21:22 -0800253 for (dex::TypeIndex entry : dex_dep.second->unverified_classes_) {
Nicolas Geoffray08025182016-10-25 17:20:18 +0100254 if (index == entry) {
255 return true;
256 }
257 }
258 }
259 return false;
260 }
261
David Brazdilca3c8c32016-09-06 14:04:48 +0100262 // Iterates over all assignability records and tries to find an entry which
263 // matches the expected destination/source pair.
264 bool HasAssignable(const std::string& expected_destination,
265 const std::string& expected_source,
266 bool expected_is_assignable) {
David Brazdilca3c8c32016-09-06 14:04:48 +0100267 for (auto& dex_dep : verifier_deps_->dex_deps_) {
268 const DexFile& dex_file = *dex_dep.first;
269 auto& storage = expected_is_assignable ? dex_dep.second->assignable_types_
270 : dex_dep.second->unassignable_types_;
271 for (auto& entry : storage) {
272 std::string actual_destination =
273 verifier_deps_->GetStringFromId(dex_file, entry.GetDestination());
274 std::string actual_source = verifier_deps_->GetStringFromId(dex_file, entry.GetSource());
275 if ((expected_destination == actual_destination) && (expected_source == actual_source)) {
276 return true;
277 }
278 }
279 }
280 return false;
281 }
282
283 // Iterates over all class resolution records, finds an entry which matches
284 // the given class descriptor and tests its properties.
285 bool HasClass(const std::string& expected_klass,
286 bool expected_resolved,
287 const std::string& expected_access_flags = "") {
David Brazdilca3c8c32016-09-06 14:04:48 +0100288 for (auto& dex_dep : verifier_deps_->dex_deps_) {
289 for (auto& entry : dex_dep.second->classes_) {
290 if (expected_resolved != entry.IsResolved()) {
291 continue;
292 }
293
294 std::string actual_klass = dex_dep.first->StringByTypeIdx(entry.GetDexTypeIndex());
295 if (expected_klass != actual_klass) {
296 continue;
297 }
298
299 if (expected_resolved) {
300 // Test access flags. Note that PrettyJavaAccessFlags always appends
301 // a space after the modifiers. Add it to the expected access flags.
302 std::string actual_access_flags = PrettyJavaAccessFlags(entry.GetAccessFlags());
303 if (expected_access_flags + " " != actual_access_flags) {
304 continue;
305 }
306 }
307
308 return true;
309 }
310 }
311 return false;
312 }
313
314 // Iterates over all field resolution records, finds an entry which matches
315 // the given field class+name+type and tests its properties.
316 bool HasField(const std::string& expected_klass,
317 const std::string& expected_name,
318 const std::string& expected_type,
319 bool expected_resolved,
320 const std::string& expected_access_flags = "",
321 const std::string& expected_decl_klass = "") {
David Brazdilca3c8c32016-09-06 14:04:48 +0100322 for (auto& dex_dep : verifier_deps_->dex_deps_) {
323 for (auto& entry : dex_dep.second->fields_) {
324 if (expected_resolved != entry.IsResolved()) {
325 continue;
326 }
327
328 const DexFile::FieldId& field_id = dex_dep.first->GetFieldId(entry.GetDexFieldIndex());
329
330 std::string actual_klass = dex_dep.first->StringByTypeIdx(field_id.class_idx_);
331 if (expected_klass != actual_klass) {
332 continue;
333 }
334
335 std::string actual_name = dex_dep.first->StringDataByIdx(field_id.name_idx_);
336 if (expected_name != actual_name) {
337 continue;
338 }
339
340 std::string actual_type = dex_dep.first->StringByTypeIdx(field_id.type_idx_);
341 if (expected_type != actual_type) {
342 continue;
343 }
344
345 if (expected_resolved) {
346 // Test access flags. Note that PrettyJavaAccessFlags always appends
347 // a space after the modifiers. Add it to the expected access flags.
348 std::string actual_access_flags = PrettyJavaAccessFlags(entry.GetAccessFlags());
349 if (expected_access_flags + " " != actual_access_flags) {
350 continue;
351 }
352
353 std::string actual_decl_klass = verifier_deps_->GetStringFromId(
354 *dex_dep.first, entry.GetDeclaringClassIndex());
355 if (expected_decl_klass != actual_decl_klass) {
356 continue;
357 }
358 }
359
360 return true;
361 }
362 }
363 return false;
364 }
365
366 // Iterates over all method resolution records, finds an entry which matches
367 // the given field kind+class+name+signature and tests its properties.
368 bool HasMethod(const std::string& expected_kind,
369 const std::string& expected_klass,
370 const std::string& expected_name,
371 const std::string& expected_signature,
372 bool expected_resolved,
373 const std::string& expected_access_flags = "",
374 const std::string& expected_decl_klass = "") {
David Brazdilca3c8c32016-09-06 14:04:48 +0100375 for (auto& dex_dep : verifier_deps_->dex_deps_) {
376 auto& storage = (expected_kind == "direct") ? dex_dep.second->direct_methods_
377 : (expected_kind == "virtual") ? dex_dep.second->virtual_methods_
378 : dex_dep.second->interface_methods_;
379 for (auto& entry : storage) {
380 if (expected_resolved != entry.IsResolved()) {
381 continue;
382 }
383
384 const DexFile::MethodId& method_id = dex_dep.first->GetMethodId(entry.GetDexMethodIndex());
385
386 std::string actual_klass = dex_dep.first->StringByTypeIdx(method_id.class_idx_);
387 if (expected_klass != actual_klass) {
388 continue;
389 }
390
391 std::string actual_name = dex_dep.first->StringDataByIdx(method_id.name_idx_);
392 if (expected_name != actual_name) {
393 continue;
394 }
395
396 std::string actual_signature = dex_dep.first->GetMethodSignature(method_id).ToString();
397 if (expected_signature != actual_signature) {
398 continue;
399 }
400
401 if (expected_resolved) {
402 // Test access flags. Note that PrettyJavaAccessFlags always appends
403 // a space after the modifiers. Add it to the expected access flags.
404 std::string actual_access_flags = PrettyJavaAccessFlags(entry.GetAccessFlags());
405 if (expected_access_flags + " " != actual_access_flags) {
406 continue;
407 }
408
409 std::string actual_decl_klass = verifier_deps_->GetStringFromId(
410 *dex_dep.first, entry.GetDeclaringClassIndex());
411 if (expected_decl_klass != actual_decl_klass) {
412 continue;
413 }
414 }
415
416 return true;
417 }
418 }
419 return false;
420 }
421
David Brazdil6f82fbd2016-09-14 11:55:26 +0100422 size_t NumberOfCompiledDexFiles() {
David Brazdil6f82fbd2016-09-14 11:55:26 +0100423 return verifier_deps_->dex_deps_.size();
424 }
425
426 size_t HasEachKindOfRecord() {
David Brazdil6f82fbd2016-09-14 11:55:26 +0100427 bool has_strings = false;
428 bool has_assignability = false;
429 bool has_classes = false;
430 bool has_fields = false;
431 bool has_methods = false;
Nicolas Geoffray08025182016-10-25 17:20:18 +0100432 bool has_unverified_classes = false;
David Brazdil6f82fbd2016-09-14 11:55:26 +0100433
434 for (auto& entry : verifier_deps_->dex_deps_) {
435 has_strings |= !entry.second->strings_.empty();
436 has_assignability |= !entry.second->assignable_types_.empty();
437 has_assignability |= !entry.second->unassignable_types_.empty();
438 has_classes |= !entry.second->classes_.empty();
439 has_fields |= !entry.second->fields_.empty();
440 has_methods |= !entry.second->direct_methods_.empty();
441 has_methods |= !entry.second->virtual_methods_.empty();
442 has_methods |= !entry.second->interface_methods_.empty();
Nicolas Geoffray08025182016-10-25 17:20:18 +0100443 has_unverified_classes |= !entry.second->unverified_classes_.empty();
David Brazdil6f82fbd2016-09-14 11:55:26 +0100444 }
445
Nicolas Geoffray08025182016-10-25 17:20:18 +0100446 return has_strings &&
447 has_assignability &&
448 has_classes &&
449 has_fields &&
450 has_methods &&
451 has_unverified_classes;
David Brazdil6f82fbd2016-09-14 11:55:26 +0100452 }
453
Nicolas Geoffray8904b6f2016-10-28 19:50:34 +0100454 static std::set<VerifierDeps::MethodResolution>* GetMethods(
455 VerifierDeps::DexFileDeps* deps, MethodResolutionKind resolution_kind) {
456 if (resolution_kind == kDirectMethodResolution) {
457 return &deps->direct_methods_;
458 } else if (resolution_kind == kVirtualMethodResolution) {
459 return &deps->virtual_methods_;
460 } else {
461 DCHECK_EQ(resolution_kind, kInterfaceMethodResolution);
462 return &deps->interface_methods_;
463 }
464 }
465
David Brazdilca3c8c32016-09-06 14:04:48 +0100466 std::unique_ptr<verifier::VerifierDeps> verifier_deps_;
Nicolas Geoffrayd01f60c2016-10-28 14:45:48 +0100467 std::vector<const DexFile*> dex_files_;
468 const DexFile* primary_dex_file_;
David Brazdilca3c8c32016-09-06 14:04:48 +0100469 jobject class_loader_;
470 mirror::Class* klass_Main_;
471};
472
473TEST_F(VerifierDepsTest, StringToId) {
474 ScopedObjectAccess soa(Thread::Current());
475 LoadDexFile(&soa);
476
Andreas Gampe8a0128a2016-11-28 07:38:35 -0800477 dex::StringIndex id_Main1 = verifier_deps_->GetIdFromString(*primary_dex_file_, "LMain;");
478 ASSERT_LT(id_Main1.index_, primary_dex_file_->NumStringIds());
Nicolas Geoffrayd01f60c2016-10-28 14:45:48 +0100479 ASSERT_EQ("LMain;", verifier_deps_->GetStringFromId(*primary_dex_file_, id_Main1));
David Brazdilca3c8c32016-09-06 14:04:48 +0100480
Andreas Gampe8a0128a2016-11-28 07:38:35 -0800481 dex::StringIndex id_Main2 = verifier_deps_->GetIdFromString(*primary_dex_file_, "LMain;");
482 ASSERT_LT(id_Main2.index_, primary_dex_file_->NumStringIds());
Nicolas Geoffrayd01f60c2016-10-28 14:45:48 +0100483 ASSERT_EQ("LMain;", verifier_deps_->GetStringFromId(*primary_dex_file_, id_Main2));
David Brazdilca3c8c32016-09-06 14:04:48 +0100484
Andreas Gampe8a0128a2016-11-28 07:38:35 -0800485 dex::StringIndex id_Lorem1 = verifier_deps_->GetIdFromString(*primary_dex_file_, "Lorem ipsum");
486 ASSERT_GE(id_Lorem1.index_, primary_dex_file_->NumStringIds());
Nicolas Geoffrayd01f60c2016-10-28 14:45:48 +0100487 ASSERT_EQ("Lorem ipsum", verifier_deps_->GetStringFromId(*primary_dex_file_, id_Lorem1));
David Brazdilca3c8c32016-09-06 14:04:48 +0100488
Andreas Gampe8a0128a2016-11-28 07:38:35 -0800489 dex::StringIndex id_Lorem2 = verifier_deps_->GetIdFromString(*primary_dex_file_, "Lorem ipsum");
490 ASSERT_GE(id_Lorem2.index_, primary_dex_file_->NumStringIds());
Nicolas Geoffrayd01f60c2016-10-28 14:45:48 +0100491 ASSERT_EQ("Lorem ipsum", verifier_deps_->GetStringFromId(*primary_dex_file_, id_Lorem2));
David Brazdilca3c8c32016-09-06 14:04:48 +0100492
493 ASSERT_EQ(id_Main1, id_Main2);
494 ASSERT_EQ(id_Lorem1, id_Lorem2);
495 ASSERT_NE(id_Main1, id_Lorem1);
496}
497
498TEST_F(VerifierDepsTest, Assignable_BothInBoot) {
499 ASSERT_TRUE(TestAssignabilityRecording(/* dst */ "Ljava/util/TimeZone;",
500 /* src */ "Ljava/util/SimpleTimeZone;",
501 /* is_strict */ true,
502 /* is_assignable */ true));
503 ASSERT_TRUE(HasAssignable("Ljava/util/TimeZone;", "Ljava/util/SimpleTimeZone;", true));
504}
505
506TEST_F(VerifierDepsTest, Assignable_DestinationInBoot1) {
507 ASSERT_TRUE(TestAssignabilityRecording(/* dst */ "Ljava/net/Socket;",
508 /* src */ "LMySSLSocket;",
509 /* is_strict */ true,
510 /* is_assignable */ true));
Nicolas Geoffray119e8462016-12-21 10:29:43 +0000511 ASSERT_TRUE(HasAssignable("Ljava/net/Socket;", "Ljavax/net/ssl/SSLSocket;", true));
David Brazdilca3c8c32016-09-06 14:04:48 +0100512}
513
514TEST_F(VerifierDepsTest, Assignable_DestinationInBoot2) {
515 ASSERT_TRUE(TestAssignabilityRecording(/* dst */ "Ljava/util/TimeZone;",
516 /* src */ "LMySimpleTimeZone;",
517 /* is_strict */ true,
518 /* is_assignable */ true));
Nicolas Geoffray119e8462016-12-21 10:29:43 +0000519 ASSERT_TRUE(HasAssignable("Ljava/util/TimeZone;", "Ljava/util/SimpleTimeZone;", true));
David Brazdilca3c8c32016-09-06 14:04:48 +0100520}
521
522TEST_F(VerifierDepsTest, Assignable_DestinationInBoot3) {
523 ASSERT_TRUE(TestAssignabilityRecording(/* dst */ "Ljava/util/Collection;",
524 /* src */ "LMyThreadSet;",
525 /* is_strict */ true,
526 /* is_assignable */ true));
527 ASSERT_TRUE(HasAssignable("Ljava/util/Collection;", "LMyThreadSet;", true));
528}
529
530TEST_F(VerifierDepsTest, Assignable_BothArrays_Resolved) {
531 ASSERT_TRUE(TestAssignabilityRecording(/* dst */ "[[Ljava/util/TimeZone;",
532 /* src */ "[[Ljava/util/SimpleTimeZone;",
533 /* is_strict */ true,
534 /* is_assignable */ true));
535 // If the component types of both arrays are resolved, we optimize the list of
536 // dependencies by recording a dependency on the component types.
537 ASSERT_FALSE(HasAssignable("[[Ljava/util/TimeZone;", "[[Ljava/util/SimpleTimeZone;", true));
538 ASSERT_FALSE(HasAssignable("[Ljava/util/TimeZone;", "[Ljava/util/SimpleTimeZone;", true));
539 ASSERT_TRUE(HasAssignable("Ljava/util/TimeZone;", "Ljava/util/SimpleTimeZone;", true));
540}
541
Nicolas Geoffray2709b5f2016-12-21 15:06:46 +0000542// We test that VerifierDeps does not try to optimize by storing assignability
543// of the component types. This is due to the fact that the component type may
544// be an erroneous class, even though the array type has resolved status.
David Brazdilca3c8c32016-09-06 14:04:48 +0100545
546TEST_F(VerifierDepsTest, Assignable_ArrayToInterface1) {
547 ASSERT_TRUE(TestAssignabilityRecording(/* dst */ "Ljava/io/Serializable;",
548 /* src */ "[Ljava/util/TimeZone;",
549 /* is_strict */ true,
550 /* is_assignable */ true));
551 ASSERT_TRUE(HasAssignable("Ljava/io/Serializable;", "[Ljava/util/TimeZone;", true));
552}
553
554TEST_F(VerifierDepsTest, Assignable_ArrayToInterface2) {
555 ASSERT_TRUE(TestAssignabilityRecording(/* dst */ "Ljava/io/Serializable;",
556 /* src */ "[LMyThreadSet;",
557 /* is_strict */ true,
558 /* is_assignable */ true));
559 ASSERT_TRUE(HasAssignable("Ljava/io/Serializable;", "[LMyThreadSet;", true));
560}
561
562TEST_F(VerifierDepsTest, NotAssignable_BothInBoot) {
563 ASSERT_TRUE(TestAssignabilityRecording(/* dst */ "Ljava/lang/Exception;",
564 /* src */ "Ljava/util/SimpleTimeZone;",
565 /* is_strict */ true,
566 /* is_assignable */ false));
567 ASSERT_TRUE(HasAssignable("Ljava/lang/Exception;", "Ljava/util/SimpleTimeZone;", false));
568}
569
570TEST_F(VerifierDepsTest, NotAssignable_DestinationInBoot1) {
571 ASSERT_TRUE(TestAssignabilityRecording(/* dst */ "Ljava/lang/Exception;",
572 /* src */ "LMySSLSocket;",
573 /* is_strict */ true,
574 /* is_assignable */ false));
Nicolas Geoffray119e8462016-12-21 10:29:43 +0000575 ASSERT_TRUE(HasAssignable("Ljava/lang/Exception;", "Ljavax/net/ssl/SSLSocket;", false));
David Brazdilca3c8c32016-09-06 14:04:48 +0100576}
577
578TEST_F(VerifierDepsTest, NotAssignable_DestinationInBoot2) {
579 ASSERT_TRUE(TestAssignabilityRecording(/* dst */ "Ljava/lang/Exception;",
580 /* src */ "LMySimpleTimeZone;",
581 /* is_strict */ true,
582 /* is_assignable */ false));
Nicolas Geoffray119e8462016-12-21 10:29:43 +0000583 ASSERT_TRUE(HasAssignable("Ljava/lang/Exception;", "Ljava/util/SimpleTimeZone;", false));
David Brazdilca3c8c32016-09-06 14:04:48 +0100584}
585
586TEST_F(VerifierDepsTest, NotAssignable_BothArrays) {
587 ASSERT_TRUE(TestAssignabilityRecording(/* dst */ "[Ljava/lang/Exception;",
588 /* src */ "[Ljava/util/SimpleTimeZone;",
589 /* is_strict */ true,
590 /* is_assignable */ false));
591 ASSERT_TRUE(HasAssignable("Ljava/lang/Exception;", "Ljava/util/SimpleTimeZone;", false));
592}
593
594TEST_F(VerifierDepsTest, ArgumentType_ResolvedClass) {
595 ASSERT_TRUE(VerifyMethod("ArgumentType_ResolvedClass"));
596 ASSERT_TRUE(HasClass("Ljava/lang/Thread;", true, "public"));
597}
598
David Brazdilca3c8c32016-09-06 14:04:48 +0100599TEST_F(VerifierDepsTest, ArgumentType_UnresolvedClass) {
600 ASSERT_TRUE(VerifyMethod("ArgumentType_UnresolvedClass"));
601 ASSERT_TRUE(HasClass("LUnresolvedClass;", false));
602}
603
604TEST_F(VerifierDepsTest, ArgumentType_UnresolvedSuper) {
605 ASSERT_TRUE(VerifyMethod("ArgumentType_UnresolvedSuper"));
606 ASSERT_TRUE(HasClass("LMySetWithUnresolvedSuper;", false));
607}
608
609TEST_F(VerifierDepsTest, ReturnType_Reference) {
610 ASSERT_TRUE(VerifyMethod("ReturnType_Reference"));
611 ASSERT_TRUE(HasAssignable("Ljava/lang/Throwable;", "Ljava/lang/IllegalStateException;", true));
612}
613
614TEST_F(VerifierDepsTest, ReturnType_Array) {
615 ASSERT_FALSE(VerifyMethod("ReturnType_Array"));
616 ASSERT_TRUE(HasAssignable("Ljava/lang/Integer;", "Ljava/lang/IllegalStateException;", false));
617}
618
619TEST_F(VerifierDepsTest, InvokeArgumentType) {
620 ASSERT_TRUE(VerifyMethod("InvokeArgumentType"));
621 ASSERT_TRUE(HasClass("Ljava/text/SimpleDateFormat;", true, "public"));
622 ASSERT_TRUE(HasClass("Ljava/util/SimpleTimeZone;", true, "public"));
623 ASSERT_TRUE(HasMethod("virtual",
624 "Ljava/text/SimpleDateFormat;",
625 "setTimeZone",
626 "(Ljava/util/TimeZone;)V",
627 true,
628 "public",
629 "Ljava/text/DateFormat;"));
630 ASSERT_TRUE(HasAssignable("Ljava/util/TimeZone;", "Ljava/util/SimpleTimeZone;", true));
631}
632
633TEST_F(VerifierDepsTest, MergeTypes_RegisterLines) {
634 ASSERT_TRUE(VerifyMethod("MergeTypes_RegisterLines"));
Nicolas Geoffray119e8462016-12-21 10:29:43 +0000635 ASSERT_TRUE(HasAssignable("Ljava/lang/Exception;", "Ljava/net/SocketTimeoutException;", true));
David Brazdilca3c8c32016-09-06 14:04:48 +0100636 ASSERT_TRUE(HasAssignable(
637 "Ljava/lang/Exception;", "Ljava/util/concurrent/TimeoutException;", true));
638}
639
640TEST_F(VerifierDepsTest, MergeTypes_IfInstanceOf) {
641 ASSERT_TRUE(VerifyMethod("MergeTypes_IfInstanceOf"));
642 ASSERT_TRUE(HasAssignable("Ljava/lang/Exception;", "Ljava/net/SocketTimeoutException;", true));
643 ASSERT_TRUE(HasAssignable(
644 "Ljava/lang/Exception;", "Ljava/util/concurrent/TimeoutException;", true));
645 ASSERT_TRUE(HasAssignable("Ljava/net/SocketTimeoutException;", "Ljava/lang/Exception;", false));
646}
647
648TEST_F(VerifierDepsTest, MergeTypes_Unresolved) {
649 ASSERT_TRUE(VerifyMethod("MergeTypes_Unresolved"));
650 ASSERT_TRUE(HasAssignable("Ljava/lang/Exception;", "Ljava/net/SocketTimeoutException;", true));
651 ASSERT_TRUE(HasAssignable(
652 "Ljava/lang/Exception;", "Ljava/util/concurrent/TimeoutException;", true));
653}
654
655TEST_F(VerifierDepsTest, ConstClass_Resolved) {
656 ASSERT_TRUE(VerifyMethod("ConstClass_Resolved"));
657 ASSERT_TRUE(HasClass("Ljava/lang/IllegalStateException;", true, "public"));
658}
659
660TEST_F(VerifierDepsTest, ConstClass_Unresolved) {
661 ASSERT_TRUE(VerifyMethod("ConstClass_Unresolved"));
662 ASSERT_TRUE(HasClass("LUnresolvedClass;", false));
663}
664
665TEST_F(VerifierDepsTest, CheckCast_Resolved) {
666 ASSERT_TRUE(VerifyMethod("CheckCast_Resolved"));
667 ASSERT_TRUE(HasClass("Ljava/lang/IllegalStateException;", true, "public"));
668}
669
670TEST_F(VerifierDepsTest, CheckCast_Unresolved) {
671 ASSERT_TRUE(VerifyMethod("CheckCast_Unresolved"));
672 ASSERT_TRUE(HasClass("LUnresolvedClass;", false));
673}
674
675TEST_F(VerifierDepsTest, InstanceOf_Resolved) {
676 ASSERT_TRUE(VerifyMethod("InstanceOf_Resolved"));
677 ASSERT_TRUE(HasClass("Ljava/lang/IllegalStateException;", true, "public"));
678}
679
680TEST_F(VerifierDepsTest, InstanceOf_Unresolved) {
681 ASSERT_TRUE(VerifyMethod("InstanceOf_Unresolved"));
682 ASSERT_TRUE(HasClass("LUnresolvedClass;", false));
683}
684
685TEST_F(VerifierDepsTest, NewInstance_Resolved) {
686 ASSERT_TRUE(VerifyMethod("NewInstance_Resolved"));
687 ASSERT_TRUE(HasClass("Ljava/lang/IllegalStateException;", true, "public"));
688}
689
690TEST_F(VerifierDepsTest, NewInstance_Unresolved) {
691 ASSERT_TRUE(VerifyMethod("NewInstance_Unresolved"));
692 ASSERT_TRUE(HasClass("LUnresolvedClass;", false));
693}
694
David Brazdilca3c8c32016-09-06 14:04:48 +0100695TEST_F(VerifierDepsTest, NewArray_Unresolved) {
696 ASSERT_TRUE(VerifyMethod("NewArray_Unresolved"));
697 ASSERT_TRUE(HasClass("[LUnresolvedClass;", false));
698}
699
700TEST_F(VerifierDepsTest, Throw) {
701 ASSERT_TRUE(VerifyMethod("Throw"));
702 ASSERT_TRUE(HasAssignable("Ljava/lang/Throwable;", "Ljava/lang/IllegalStateException;", true));
703}
704
705TEST_F(VerifierDepsTest, MoveException_Resolved) {
706 ASSERT_TRUE(VerifyMethod("MoveException_Resolved"));
707 ASSERT_TRUE(HasClass("Ljava/io/InterruptedIOException;", true, "public"));
708 ASSERT_TRUE(HasClass("Ljava/net/SocketTimeoutException;", true, "public"));
709 ASSERT_TRUE(HasClass("Ljava/util/zip/ZipException;", true, "public"));
710
711 // Testing that all exception types are assignable to Throwable.
712 ASSERT_TRUE(HasAssignable("Ljava/lang/Throwable;", "Ljava/io/InterruptedIOException;", true));
713 ASSERT_TRUE(HasAssignable("Ljava/lang/Throwable;", "Ljava/net/SocketTimeoutException;", true));
714 ASSERT_TRUE(HasAssignable("Ljava/lang/Throwable;", "Ljava/util/zip/ZipException;", true));
715
716 // Testing that the merge type is assignable to Throwable.
717 ASSERT_TRUE(HasAssignable("Ljava/lang/Throwable;", "Ljava/io/IOException;", true));
718
719 // Merging of exception types.
720 ASSERT_TRUE(HasAssignable("Ljava/io/IOException;", "Ljava/io/InterruptedIOException;", true));
721 ASSERT_TRUE(HasAssignable("Ljava/io/IOException;", "Ljava/util/zip/ZipException;", true));
722 ASSERT_TRUE(HasAssignable(
723 "Ljava/io/InterruptedIOException;", "Ljava/net/SocketTimeoutException;", true));
724}
725
726TEST_F(VerifierDepsTest, MoveException_Unresolved) {
727 ASSERT_FALSE(VerifyMethod("MoveException_Unresolved"));
728 ASSERT_TRUE(HasClass("LUnresolvedException;", false));
729}
730
731TEST_F(VerifierDepsTest, StaticField_Resolved_DeclaredInReferenced) {
732 ASSERT_TRUE(VerifyMethod("StaticField_Resolved_DeclaredInReferenced"));
733 ASSERT_TRUE(HasClass("Ljava/lang/System;", true, "public final"));
734 ASSERT_TRUE(HasField("Ljava/lang/System;",
735 "out",
736 "Ljava/io/PrintStream;",
737 true,
738 "public final static",
739 "Ljava/lang/System;"));
740}
741
742TEST_F(VerifierDepsTest, StaticField_Resolved_DeclaredInSuperclass1) {
743 ASSERT_TRUE(VerifyMethod("StaticField_Resolved_DeclaredInSuperclass1"));
744 ASSERT_TRUE(HasClass("Ljava/util/SimpleTimeZone;", true, "public"));
745 ASSERT_TRUE(HasField(
746 "Ljava/util/SimpleTimeZone;", "LONG", "I", true, "public final static", "Ljava/util/TimeZone;"));
747}
748
749TEST_F(VerifierDepsTest, StaticField_Resolved_DeclaredInSuperclass2) {
750 ASSERT_TRUE(VerifyMethod("StaticField_Resolved_DeclaredInSuperclass2"));
751 ASSERT_TRUE(HasField(
752 "LMySimpleTimeZone;", "SHORT", "I", true, "public final static", "Ljava/util/TimeZone;"));
753}
754
755TEST_F(VerifierDepsTest, StaticField_Resolved_DeclaredInInterface1) {
756 ASSERT_TRUE(VerifyMethod("StaticField_Resolved_DeclaredInInterface1"));
757 ASSERT_TRUE(HasClass("Ljavax/xml/transform/dom/DOMResult;", true, "public"));
758 ASSERT_TRUE(HasField("Ljavax/xml/transform/dom/DOMResult;",
759 "PI_ENABLE_OUTPUT_ESCAPING",
760 "Ljava/lang/String;",
761 true,
762 "public final static",
763 "Ljavax/xml/transform/Result;"));
764}
765
766TEST_F(VerifierDepsTest, StaticField_Resolved_DeclaredInInterface2) {
767 ASSERT_TRUE(VerifyMethod("StaticField_Resolved_DeclaredInInterface2"));
768 ASSERT_TRUE(HasField("LMyDOMResult;",
769 "PI_ENABLE_OUTPUT_ESCAPING",
770 "Ljava/lang/String;",
771 true,
772 "public final static",
773 "Ljavax/xml/transform/Result;"));
774}
775
776TEST_F(VerifierDepsTest, StaticField_Resolved_DeclaredInInterface3) {
777 ASSERT_TRUE(VerifyMethod("StaticField_Resolved_DeclaredInInterface3"));
778 ASSERT_TRUE(HasField("LMyResult;",
779 "PI_ENABLE_OUTPUT_ESCAPING",
780 "Ljava/lang/String;",
781 true,
782 "public final static",
783 "Ljavax/xml/transform/Result;"));
784}
785
786TEST_F(VerifierDepsTest, StaticField_Resolved_DeclaredInInterface4) {
787 ASSERT_TRUE(VerifyMethod("StaticField_Resolved_DeclaredInInterface4"));
788 ASSERT_TRUE(HasField("LMyDocument;",
789 "ELEMENT_NODE",
790 "S",
791 true,
792 "public final static",
793 "Lorg/w3c/dom/Node;"));
794}
795
796TEST_F(VerifierDepsTest, StaticField_Unresolved_ReferrerInBoot) {
797 ASSERT_TRUE(VerifyMethod("StaticField_Unresolved_ReferrerInBoot"));
798 ASSERT_TRUE(HasClass("Ljava/util/TimeZone;", true, "public abstract"));
799 ASSERT_TRUE(HasField("Ljava/util/TimeZone;", "x", "I", false));
800}
801
802TEST_F(VerifierDepsTest, StaticField_Unresolved_ReferrerInDex) {
803 ASSERT_TRUE(VerifyMethod("StaticField_Unresolved_ReferrerInDex"));
804 ASSERT_TRUE(HasField("LMyThreadSet;", "x", "I", false));
805}
806
807TEST_F(VerifierDepsTest, InstanceField_Resolved_DeclaredInReferenced) {
808 ASSERT_TRUE(VerifyMethod("InstanceField_Resolved_DeclaredInReferenced"));
809 ASSERT_TRUE(HasClass("Ljava/io/InterruptedIOException;", true, "public"));
810 ASSERT_TRUE(HasField("Ljava/io/InterruptedIOException;",
811 "bytesTransferred",
812 "I",
813 true,
814 "public",
815 "Ljava/io/InterruptedIOException;"));
816 ASSERT_TRUE(HasAssignable(
Nicolas Geoffray119e8462016-12-21 10:29:43 +0000817 "Ljava/io/InterruptedIOException;", "Ljava/net/SocketTimeoutException;", true));
David Brazdilca3c8c32016-09-06 14:04:48 +0100818}
819
820TEST_F(VerifierDepsTest, InstanceField_Resolved_DeclaredInSuperclass1) {
821 ASSERT_TRUE(VerifyMethod("InstanceField_Resolved_DeclaredInSuperclass1"));
822 ASSERT_TRUE(HasClass("Ljava/net/SocketTimeoutException;", true, "public"));
823 ASSERT_TRUE(HasField("Ljava/net/SocketTimeoutException;",
824 "bytesTransferred",
825 "I",
826 true,
827 "public",
828 "Ljava/io/InterruptedIOException;"));
829 ASSERT_TRUE(HasAssignable(
Nicolas Geoffray119e8462016-12-21 10:29:43 +0000830 "Ljava/io/InterruptedIOException;", "Ljava/net/SocketTimeoutException;", true));
David Brazdilca3c8c32016-09-06 14:04:48 +0100831}
832
833TEST_F(VerifierDepsTest, InstanceField_Resolved_DeclaredInSuperclass2) {
834 ASSERT_TRUE(VerifyMethod("InstanceField_Resolved_DeclaredInSuperclass2"));
835 ASSERT_TRUE(HasField("LMySocketTimeoutException;",
836 "bytesTransferred",
837 "I",
838 true,
839 "public",
840 "Ljava/io/InterruptedIOException;"));
841 ASSERT_TRUE(HasAssignable(
Nicolas Geoffray119e8462016-12-21 10:29:43 +0000842 "Ljava/io/InterruptedIOException;", "Ljava/net/SocketTimeoutException;", true));
David Brazdilca3c8c32016-09-06 14:04:48 +0100843}
844
845TEST_F(VerifierDepsTest, InstanceField_Unresolved_ReferrerInBoot) {
846 ASSERT_TRUE(VerifyMethod("InstanceField_Unresolved_ReferrerInBoot"));
847 ASSERT_TRUE(HasClass("Ljava/io/InterruptedIOException;", true, "public"));
848 ASSERT_TRUE(HasField("Ljava/io/InterruptedIOException;", "x", "I", false));
849}
850
851TEST_F(VerifierDepsTest, InstanceField_Unresolved_ReferrerInDex) {
852 ASSERT_TRUE(VerifyMethod("InstanceField_Unresolved_ReferrerInDex"));
853 ASSERT_TRUE(HasField("LMyThreadSet;", "x", "I", false));
854}
855
856TEST_F(VerifierDepsTest, InvokeStatic_Resolved_DeclaredInReferenced) {
857 ASSERT_TRUE(VerifyMethod("InvokeStatic_Resolved_DeclaredInReferenced"));
858 ASSERT_TRUE(HasClass("Ljava/net/Socket;", true, "public"));
859 ASSERT_TRUE(HasMethod("direct",
860 "Ljava/net/Socket;",
861 "setSocketImplFactory",
862 "(Ljava/net/SocketImplFactory;)V",
863 true,
864 "public static",
865 "Ljava/net/Socket;"));
866}
867
868TEST_F(VerifierDepsTest, InvokeStatic_Resolved_DeclaredInSuperclass1) {
869 ASSERT_TRUE(VerifyMethod("InvokeStatic_Resolved_DeclaredInSuperclass1"));
870 ASSERT_TRUE(HasClass("Ljavax/net/ssl/SSLSocket;", true, "public abstract"));
871 ASSERT_TRUE(HasMethod("direct",
872 "Ljavax/net/ssl/SSLSocket;",
873 "setSocketImplFactory",
874 "(Ljava/net/SocketImplFactory;)V",
875 true,
876 "public static",
877 "Ljava/net/Socket;"));
878}
879
880TEST_F(VerifierDepsTest, InvokeStatic_Resolved_DeclaredInSuperclass2) {
881 ASSERT_TRUE(VerifyMethod("InvokeStatic_Resolved_DeclaredInSuperclass2"));
882 ASSERT_TRUE(HasMethod("direct",
883 "LMySSLSocket;",
884 "setSocketImplFactory",
885 "(Ljava/net/SocketImplFactory;)V",
886 true,
887 "public static",
888 "Ljava/net/Socket;"));
889}
890
891TEST_F(VerifierDepsTest, InvokeStatic_DeclaredInInterface1) {
892 ASSERT_TRUE(VerifyMethod("InvokeStatic_DeclaredInInterface1"));
893 ASSERT_TRUE(HasClass("Ljava/util/Map$Entry;", true, "public abstract interface"));
894 ASSERT_TRUE(HasMethod("direct",
895 "Ljava/util/Map$Entry;",
896 "comparingByKey",
897 "()Ljava/util/Comparator;",
898 true,
899 "public static",
900 "Ljava/util/Map$Entry;"));
901}
902
903TEST_F(VerifierDepsTest, InvokeStatic_DeclaredInInterface2) {
904 ASSERT_FALSE(VerifyMethod("InvokeStatic_DeclaredInInterface2"));
905 ASSERT_TRUE(HasClass("Ljava/util/AbstractMap$SimpleEntry;", true, "public"));
906 ASSERT_TRUE(HasMethod("direct",
907 "Ljava/util/AbstractMap$SimpleEntry;",
908 "comparingByKey",
909 "()Ljava/util/Comparator;",
910 false));
911}
912
913TEST_F(VerifierDepsTest, InvokeStatic_Unresolved1) {
914 ASSERT_FALSE(VerifyMethod("InvokeStatic_Unresolved1"));
915 ASSERT_TRUE(HasClass("Ljavax/net/ssl/SSLSocket;", true, "public abstract"));
916 ASSERT_TRUE(HasMethod("direct", "Ljavax/net/ssl/SSLSocket;", "x", "()V", false));
917}
918
919TEST_F(VerifierDepsTest, InvokeStatic_Unresolved2) {
920 ASSERT_FALSE(VerifyMethod("InvokeStatic_Unresolved2"));
921 ASSERT_TRUE(HasMethod("direct", "LMySSLSocket;", "x", "()V", false));
922}
923
924TEST_F(VerifierDepsTest, InvokeDirect_Resolved_DeclaredInReferenced) {
925 ASSERT_TRUE(VerifyMethod("InvokeDirect_Resolved_DeclaredInReferenced"));
926 ASSERT_TRUE(HasClass("Ljava/net/Socket;", true, "public"));
927 ASSERT_TRUE(HasMethod(
928 "direct", "Ljava/net/Socket;", "<init>", "()V", true, "public", "Ljava/net/Socket;"));
929}
930
931TEST_F(VerifierDepsTest, InvokeDirect_Resolved_DeclaredInSuperclass1) {
932 ASSERT_FALSE(VerifyMethod("InvokeDirect_Resolved_DeclaredInSuperclass1"));
933 ASSERT_TRUE(HasClass("Ljavax/net/ssl/SSLSocket;", true, "public abstract"));
934 ASSERT_TRUE(HasMethod("direct",
935 "Ljavax/net/ssl/SSLSocket;",
936 "checkOldImpl",
937 "()V",
938 true,
939 "private",
940 "Ljava/net/Socket;"));
941}
942
943TEST_F(VerifierDepsTest, InvokeDirect_Resolved_DeclaredInSuperclass2) {
944 ASSERT_FALSE(VerifyMethod("InvokeDirect_Resolved_DeclaredInSuperclass2"));
945 ASSERT_TRUE(HasMethod(
946 "direct", "LMySSLSocket;", "checkOldImpl", "()V", true, "private", "Ljava/net/Socket;"));
947}
948
949TEST_F(VerifierDepsTest, InvokeDirect_Unresolved1) {
950 ASSERT_FALSE(VerifyMethod("InvokeDirect_Unresolved1"));
951 ASSERT_TRUE(HasClass("Ljavax/net/ssl/SSLSocket;", true, "public abstract"));
952 ASSERT_TRUE(HasMethod("direct", "Ljavax/net/ssl/SSLSocket;", "x", "()V", false));
953}
954
955TEST_F(VerifierDepsTest, InvokeDirect_Unresolved2) {
956 ASSERT_FALSE(VerifyMethod("InvokeDirect_Unresolved2"));
957 ASSERT_TRUE(HasMethod("direct", "LMySSLSocket;", "x", "()V", false));
958}
959
960TEST_F(VerifierDepsTest, InvokeVirtual_Resolved_DeclaredInReferenced) {
961 ASSERT_TRUE(VerifyMethod("InvokeVirtual_Resolved_DeclaredInReferenced"));
962 ASSERT_TRUE(HasClass("Ljava/lang/Throwable;", true, "public"));
963 ASSERT_TRUE(HasMethod("virtual",
964 "Ljava/lang/Throwable;",
965 "getMessage",
966 "()Ljava/lang/String;",
967 true,
968 "public",
969 "Ljava/lang/Throwable;"));
970 // Type dependency on `this` argument.
Nicolas Geoffray119e8462016-12-21 10:29:43 +0000971 ASSERT_TRUE(HasAssignable("Ljava/lang/Throwable;", "Ljava/net/SocketTimeoutException;", true));
David Brazdilca3c8c32016-09-06 14:04:48 +0100972}
973
974TEST_F(VerifierDepsTest, InvokeVirtual_Resolved_DeclaredInSuperclass1) {
975 ASSERT_TRUE(VerifyMethod("InvokeVirtual_Resolved_DeclaredInSuperclass1"));
976 ASSERT_TRUE(HasClass("Ljava/io/InterruptedIOException;", true, "public"));
977 ASSERT_TRUE(HasMethod("virtual",
978 "Ljava/io/InterruptedIOException;",
979 "getMessage",
980 "()Ljava/lang/String;",
981 true,
982 "public",
983 "Ljava/lang/Throwable;"));
984 // Type dependency on `this` argument.
Nicolas Geoffray119e8462016-12-21 10:29:43 +0000985 ASSERT_TRUE(HasAssignable("Ljava/lang/Throwable;", "Ljava/net/SocketTimeoutException;", true));
David Brazdilca3c8c32016-09-06 14:04:48 +0100986}
987
988TEST_F(VerifierDepsTest, InvokeVirtual_Resolved_DeclaredInSuperclass2) {
989 ASSERT_TRUE(VerifyMethod("InvokeVirtual_Resolved_DeclaredInSuperclass2"));
990 ASSERT_TRUE(HasMethod("virtual",
991 "LMySocketTimeoutException;",
992 "getMessage",
993 "()Ljava/lang/String;",
994 true,
995 "public",
996 "Ljava/lang/Throwable;"));
997}
998
999TEST_F(VerifierDepsTest, InvokeVirtual_Resolved_DeclaredInSuperinterface) {
1000 ASSERT_TRUE(VerifyMethod("InvokeVirtual_Resolved_DeclaredInSuperinterface"));
1001 ASSERT_TRUE(HasMethod("virtual",
1002 "LMyThreadSet;",
1003 "size",
1004 "()I",
1005 true,
1006 "public abstract",
1007 "Ljava/util/Set;"));
1008}
1009
1010TEST_F(VerifierDepsTest, InvokeVirtual_Unresolved1) {
1011 ASSERT_FALSE(VerifyMethod("InvokeVirtual_Unresolved1"));
1012 ASSERT_TRUE(HasClass("Ljava/io/InterruptedIOException;", true, "public"));
1013 ASSERT_TRUE(HasMethod("virtual", "Ljava/io/InterruptedIOException;", "x", "()V", false));
1014}
1015
1016TEST_F(VerifierDepsTest, InvokeVirtual_Unresolved2) {
1017 ASSERT_FALSE(VerifyMethod("InvokeVirtual_Unresolved2"));
1018 ASSERT_TRUE(HasMethod("virtual", "LMySocketTimeoutException;", "x", "()V", false));
1019}
1020
1021TEST_F(VerifierDepsTest, InvokeVirtual_ActuallyDirect) {
1022 ASSERT_FALSE(VerifyMethod("InvokeVirtual_ActuallyDirect"));
1023 ASSERT_TRUE(HasMethod("virtual", "LMyThread;", "activeCount", "()I", false));
1024 ASSERT_TRUE(HasMethod("direct",
1025 "LMyThread;",
1026 "activeCount",
1027 "()I",
1028 true,
1029 "public static",
1030 "Ljava/lang/Thread;"));
1031}
1032
1033TEST_F(VerifierDepsTest, InvokeInterface_Resolved_DeclaredInReferenced) {
1034 ASSERT_TRUE(VerifyMethod("InvokeInterface_Resolved_DeclaredInReferenced"));
1035 ASSERT_TRUE(HasClass("Ljava/lang/Runnable;", true, "public abstract interface"));
1036 ASSERT_TRUE(HasMethod("interface",
1037 "Ljava/lang/Runnable;",
1038 "run",
1039 "()V",
1040 true,
1041 "public abstract",
1042 "Ljava/lang/Runnable;"));
1043}
1044
1045TEST_F(VerifierDepsTest, InvokeInterface_Resolved_DeclaredInSuperclass) {
1046 ASSERT_FALSE(VerifyMethod("InvokeInterface_Resolved_DeclaredInSuperclass"));
1047 ASSERT_TRUE(HasMethod("interface", "LMyThread;", "join", "()V", false));
1048}
1049
1050TEST_F(VerifierDepsTest, InvokeInterface_Resolved_DeclaredInSuperinterface1) {
1051 ASSERT_FALSE(VerifyMethod("InvokeInterface_Resolved_DeclaredInSuperinterface1"));
1052 ASSERT_TRUE(HasMethod("interface",
1053 "LMyThreadSet;",
1054 "run",
1055 "()V",
1056 true,
1057 "public abstract",
1058 "Ljava/lang/Runnable;"));
1059}
1060
1061TEST_F(VerifierDepsTest, InvokeInterface_Resolved_DeclaredInSuperinterface2) {
1062 ASSERT_FALSE(VerifyMethod("InvokeInterface_Resolved_DeclaredInSuperinterface2"));
1063 ASSERT_TRUE(HasMethod("interface",
1064 "LMyThreadSet;",
1065 "isEmpty",
1066 "()Z",
1067 true,
1068 "public abstract",
1069 "Ljava/util/Set;"));
1070}
1071
1072TEST_F(VerifierDepsTest, InvokeInterface_Unresolved1) {
1073 ASSERT_FALSE(VerifyMethod("InvokeInterface_Unresolved1"));
1074 ASSERT_TRUE(HasClass("Ljava/lang/Runnable;", true, "public abstract interface"));
1075 ASSERT_TRUE(HasMethod("interface", "Ljava/lang/Runnable;", "x", "()V", false));
1076}
1077
1078TEST_F(VerifierDepsTest, InvokeInterface_Unresolved2) {
1079 ASSERT_FALSE(VerifyMethod("InvokeInterface_Unresolved2"));
1080 ASSERT_TRUE(HasMethod("interface", "LMyThreadSet;", "x", "()V", false));
1081}
1082
1083TEST_F(VerifierDepsTest, InvokeSuper_ThisAssignable) {
1084 ASSERT_TRUE(VerifyMethod("InvokeSuper_ThisAssignable"));
1085 ASSERT_TRUE(HasClass("Ljava/lang/Runnable;", true, "public abstract interface"));
1086 ASSERT_TRUE(HasAssignable("Ljava/lang/Runnable;", "LMain;", true));
1087 ASSERT_TRUE(HasMethod("interface",
1088 "Ljava/lang/Runnable;",
1089 "run",
1090 "()V",
1091 true,
1092 "public abstract",
1093 "Ljava/lang/Runnable;"));
1094}
1095
1096TEST_F(VerifierDepsTest, InvokeSuper_ThisNotAssignable) {
1097 ASSERT_FALSE(VerifyMethod("InvokeSuper_ThisNotAssignable"));
1098 ASSERT_TRUE(HasClass("Ljava/lang/Integer;", true, "public final"));
Nicolas Geoffray119e8462016-12-21 10:29:43 +00001099 ASSERT_TRUE(HasAssignable("Ljava/lang/Integer;", "Ljava/lang/Thread;", false));
David Brazdilca3c8c32016-09-06 14:04:48 +01001100 ASSERT_TRUE(HasMethod(
1101 "virtual", "Ljava/lang/Integer;", "intValue", "()I", true, "public", "Ljava/lang/Integer;"));
1102}
1103
Nicolas Geoffray0f1cb172017-01-05 15:23:19 +00001104TEST_F(VerifierDepsTest, ArgumentType_ResolvedReferenceArray) {
1105 ASSERT_TRUE(VerifyMethod("ArgumentType_ResolvedReferenceArray"));
1106 ASSERT_TRUE(HasClass("[Ljava/lang/Thread;", true, "public final abstract"));
1107}
1108
1109TEST_F(VerifierDepsTest, NewArray_Resolved) {
1110 ASSERT_TRUE(VerifyMethod("NewArray_Resolved"));
1111 ASSERT_TRUE(HasClass("[Ljava/lang/IllegalStateException;", true, "public final abstract"));
1112}
1113
David Brazdil6f82fbd2016-09-14 11:55:26 +01001114TEST_F(VerifierDepsTest, EncodeDecode) {
1115 VerifyDexFile();
1116
1117 ASSERT_EQ(1u, NumberOfCompiledDexFiles());
1118 ASSERT_TRUE(HasEachKindOfRecord());
1119
1120 std::vector<uint8_t> buffer;
Nicolas Geoffrayd01f60c2016-10-28 14:45:48 +01001121 verifier_deps_->Encode(dex_files_, &buffer);
David Brazdil6f82fbd2016-09-14 11:55:26 +01001122 ASSERT_FALSE(buffer.empty());
1123
Nicolas Geoffraye70dd562016-10-30 21:03:35 +00001124 VerifierDeps decoded_deps(dex_files_, ArrayRef<const uint8_t>(buffer));
David Brazdil6f82fbd2016-09-14 11:55:26 +01001125 ASSERT_TRUE(verifier_deps_->Equals(decoded_deps));
1126}
1127
Nicolas Geoffrayd01f60c2016-10-28 14:45:48 +01001128TEST_F(VerifierDepsTest, EncodeDecodeMulti) {
1129 VerifyDexFile("MultiDex");
1130
1131 ASSERT_GT(NumberOfCompiledDexFiles(), 1u);
1132 std::vector<uint8_t> buffer;
1133 verifier_deps_->Encode(dex_files_, &buffer);
1134 ASSERT_FALSE(buffer.empty());
1135
1136 // Create new DexFile, to mess with std::map order: the verifier deps used
1137 // to iterate over the map, which doesn't guarantee insertion order. We fixed
1138 // this by passing the expected order when encoding/decoding.
1139 std::vector<std::unique_ptr<const DexFile>> first_dex_files = OpenTestDexFiles("VerifierDeps");
1140 std::vector<std::unique_ptr<const DexFile>> second_dex_files = OpenTestDexFiles("MultiDex");
1141 std::vector<const DexFile*> dex_files;
1142 for (auto& dex_file : first_dex_files) {
1143 dex_files.push_back(dex_file.get());
1144 }
1145 for (auto& dex_file : second_dex_files) {
1146 dex_files.push_back(dex_file.get());
1147 }
1148
1149 // Dump the new verifier deps to ensure it can properly read the data.
Nicolas Geoffraye70dd562016-10-30 21:03:35 +00001150 VerifierDeps decoded_deps(dex_files, ArrayRef<const uint8_t>(buffer));
Nicolas Geoffrayd01f60c2016-10-28 14:45:48 +01001151 std::ostringstream stream;
1152 VariableIndentationOutputStream os(&stream);
1153 decoded_deps.Dump(&os);
1154}
1155
Nicolas Geoffray08025182016-10-25 17:20:18 +01001156TEST_F(VerifierDepsTest, UnverifiedClasses) {
1157 VerifyDexFile();
1158 ASSERT_FALSE(HasUnverifiedClass("LMyThread;"));
1159 // Test that a class with a soft failure is recorded.
1160 ASSERT_TRUE(HasUnverifiedClass("LMain;"));
1161 // Test that a class with hard failure is recorded.
1162 ASSERT_TRUE(HasUnverifiedClass("LMyVerificationFailure;"));
1163 // Test that a class with unresolved super is recorded.
1164 ASSERT_FALSE(HasUnverifiedClass("LMyClassWithNoSuper;"));
1165 // Test that a class with unresolved super and hard failure is recorded.
1166 ASSERT_TRUE(HasUnverifiedClass("LMyClassWithNoSuperButFailures;"));
1167}
1168
Nicolas Geoffray8904b6f2016-10-28 19:50:34 +01001169// Returns the next resolution kind in the enum.
1170static MethodResolutionKind GetNextResolutionKind(MethodResolutionKind resolution_kind) {
1171 if (resolution_kind == kDirectMethodResolution) {
1172 return kVirtualMethodResolution;
1173 } else if (resolution_kind == kVirtualMethodResolution) {
1174 return kInterfaceMethodResolution;
1175 } else {
1176 DCHECK_EQ(resolution_kind, kInterfaceMethodResolution);
1177 return kDirectMethodResolution;
1178 }
1179}
1180
1181TEST_F(VerifierDepsTest, VerifyDeps) {
1182 VerifyDexFile();
1183
1184 ASSERT_EQ(1u, NumberOfCompiledDexFiles());
1185 ASSERT_TRUE(HasEachKindOfRecord());
1186
1187 // When validating, we create a new class loader, as
1188 // the existing `class_loader_` may contain erroneous classes,
1189 // that ClassLinker::FindClass won't return.
1190
1191 ScopedObjectAccess soa(Thread::Current());
1192 StackHandleScope<1> hs(soa.Self());
1193 MutableHandle<mirror::ClassLoader> new_class_loader(hs.NewHandle<mirror::ClassLoader>(nullptr));
1194 {
1195 new_class_loader.Assign(soa.Decode<mirror::ClassLoader>(LoadDex("VerifierDeps")));
Nicolas Geoffray6bb7f1b2016-11-03 10:52:49 +00001196 ASSERT_TRUE(verifier_deps_->ValidateDependencies(new_class_loader, soa.Self()));
Nicolas Geoffray8904b6f2016-10-28 19:50:34 +01001197 }
1198
1199 std::vector<uint8_t> buffer;
1200 verifier_deps_->Encode(dex_files_, &buffer);
1201 ASSERT_FALSE(buffer.empty());
1202
1203 {
1204 VerifierDeps decoded_deps(dex_files_, ArrayRef<const uint8_t>(buffer));
1205 new_class_loader.Assign(soa.Decode<mirror::ClassLoader>(LoadDex("VerifierDeps")));
Nicolas Geoffray6bb7f1b2016-11-03 10:52:49 +00001206 ASSERT_TRUE(decoded_deps.ValidateDependencies(new_class_loader, soa.Self()));
Nicolas Geoffray8904b6f2016-10-28 19:50:34 +01001207 }
1208
1209 // Fiddle with the dependencies to make sure we catch any change and fail to verify.
1210
1211 {
1212 // Mess up with the assignable_types.
1213 VerifierDeps decoded_deps(dex_files_, ArrayRef<const uint8_t>(buffer));
1214 VerifierDeps::DexFileDeps* deps = decoded_deps.GetDexFileDeps(*primary_dex_file_);
1215 deps->assignable_types_.insert(*deps->unassignable_types_.begin());
1216 new_class_loader.Assign(soa.Decode<mirror::ClassLoader>(LoadDex("VerifierDeps")));
Nicolas Geoffray6bb7f1b2016-11-03 10:52:49 +00001217 ASSERT_FALSE(decoded_deps.ValidateDependencies(new_class_loader, soa.Self()));
Nicolas Geoffray8904b6f2016-10-28 19:50:34 +01001218 }
1219
1220 {
1221 // Mess up with the unassignable_types.
1222 VerifierDeps decoded_deps(dex_files_, ArrayRef<const uint8_t>(buffer));
1223 VerifierDeps::DexFileDeps* deps = decoded_deps.GetDexFileDeps(*primary_dex_file_);
1224 deps->unassignable_types_.insert(*deps->assignable_types_.begin());
1225 new_class_loader.Assign(soa.Decode<mirror::ClassLoader>(LoadDex("VerifierDeps")));
Nicolas Geoffray6bb7f1b2016-11-03 10:52:49 +00001226 ASSERT_FALSE(decoded_deps.ValidateDependencies(new_class_loader, soa.Self()));
Nicolas Geoffray8904b6f2016-10-28 19:50:34 +01001227 }
1228
1229 // Mess up with classes.
1230 {
1231 VerifierDeps decoded_deps(dex_files_, ArrayRef<const uint8_t>(buffer));
1232 VerifierDeps::DexFileDeps* deps = decoded_deps.GetDexFileDeps(*primary_dex_file_);
1233 bool found = false;
1234 for (const auto& entry : deps->classes_) {
1235 if (entry.IsResolved()) {
1236 deps->classes_.insert(VerifierDeps::ClassResolution(
1237 entry.GetDexTypeIndex(), VerifierDeps::kUnresolvedMarker));
1238 found = true;
1239 break;
1240 }
1241 }
1242 ASSERT_TRUE(found);
1243 new_class_loader.Assign(soa.Decode<mirror::ClassLoader>(LoadDex("VerifierDeps")));
Nicolas Geoffray6bb7f1b2016-11-03 10:52:49 +00001244 ASSERT_FALSE(decoded_deps.ValidateDependencies(new_class_loader, soa.Self()));
Nicolas Geoffray8904b6f2016-10-28 19:50:34 +01001245 }
1246
1247 {
1248 VerifierDeps decoded_deps(dex_files_, ArrayRef<const uint8_t>(buffer));
1249 VerifierDeps::DexFileDeps* deps = decoded_deps.GetDexFileDeps(*primary_dex_file_);
1250 bool found = false;
1251 for (const auto& entry : deps->classes_) {
1252 if (!entry.IsResolved()) {
1253 deps->classes_.insert(VerifierDeps::ClassResolution(
1254 entry.GetDexTypeIndex(), VerifierDeps::kUnresolvedMarker - 1));
1255 found = true;
1256 break;
1257 }
1258 }
1259 ASSERT_TRUE(found);
1260 new_class_loader.Assign(soa.Decode<mirror::ClassLoader>(LoadDex("VerifierDeps")));
Nicolas Geoffray6bb7f1b2016-11-03 10:52:49 +00001261 ASSERT_FALSE(decoded_deps.ValidateDependencies(new_class_loader, soa.Self()));
Nicolas Geoffray8904b6f2016-10-28 19:50:34 +01001262 }
1263
1264 {
1265 VerifierDeps decoded_deps(dex_files_, ArrayRef<const uint8_t>(buffer));
1266 VerifierDeps::DexFileDeps* deps = decoded_deps.GetDexFileDeps(*primary_dex_file_);
1267 bool found = false;
1268 for (const auto& entry : deps->classes_) {
1269 if (entry.IsResolved()) {
1270 deps->classes_.insert(VerifierDeps::ClassResolution(
1271 entry.GetDexTypeIndex(), entry.GetAccessFlags() - 1));
1272 found = true;
1273 break;
1274 }
1275 }
1276 ASSERT_TRUE(found);
1277 new_class_loader.Assign(soa.Decode<mirror::ClassLoader>(LoadDex("VerifierDeps")));
Nicolas Geoffray6bb7f1b2016-11-03 10:52:49 +00001278 ASSERT_FALSE(decoded_deps.ValidateDependencies(new_class_loader, soa.Self()));
Nicolas Geoffray8904b6f2016-10-28 19:50:34 +01001279 }
1280
1281 // Mess up with fields.
1282 {
1283 VerifierDeps decoded_deps(dex_files_, ArrayRef<const uint8_t>(buffer));
1284 VerifierDeps::DexFileDeps* deps = decoded_deps.GetDexFileDeps(*primary_dex_file_);
1285 bool found = false;
1286 for (const auto& entry : deps->fields_) {
1287 if (entry.IsResolved()) {
1288 deps->fields_.insert(VerifierDeps::FieldResolution(entry.GetDexFieldIndex(),
1289 VerifierDeps::kUnresolvedMarker,
1290 entry.GetDeclaringClassIndex()));
1291 found = true;
1292 break;
1293 }
1294 }
1295 ASSERT_TRUE(found);
1296 new_class_loader.Assign(soa.Decode<mirror::ClassLoader>(LoadDex("VerifierDeps")));
Nicolas Geoffray6bb7f1b2016-11-03 10:52:49 +00001297 ASSERT_FALSE(decoded_deps.ValidateDependencies(new_class_loader, soa.Self()));
Nicolas Geoffray8904b6f2016-10-28 19:50:34 +01001298 }
1299
1300 {
1301 VerifierDeps decoded_deps(dex_files_, ArrayRef<const uint8_t>(buffer));
1302 VerifierDeps::DexFileDeps* deps = decoded_deps.GetDexFileDeps(*primary_dex_file_);
1303 bool found = false;
1304 for (const auto& entry : deps->fields_) {
1305 if (!entry.IsResolved()) {
Andreas Gampe8a0128a2016-11-28 07:38:35 -08001306 constexpr dex::StringIndex kStringIndexZero(0); // We know there is a class there.
Nicolas Geoffray8904b6f2016-10-28 19:50:34 +01001307 deps->fields_.insert(VerifierDeps::FieldResolution(0 /* we know there is a field there */,
1308 VerifierDeps::kUnresolvedMarker - 1,
Andreas Gampe8a0128a2016-11-28 07:38:35 -08001309 kStringIndexZero));
Nicolas Geoffray8904b6f2016-10-28 19:50:34 +01001310 found = true;
1311 break;
1312 }
1313 }
1314 ASSERT_TRUE(found);
1315 new_class_loader.Assign(soa.Decode<mirror::ClassLoader>(LoadDex("VerifierDeps")));
Nicolas Geoffray6bb7f1b2016-11-03 10:52:49 +00001316 ASSERT_FALSE(decoded_deps.ValidateDependencies(new_class_loader, soa.Self()));
Nicolas Geoffray8904b6f2016-10-28 19:50:34 +01001317 }
1318
1319 {
1320 VerifierDeps decoded_deps(dex_files_, ArrayRef<const uint8_t>(buffer));
1321 VerifierDeps::DexFileDeps* deps = decoded_deps.GetDexFileDeps(*primary_dex_file_);
1322 bool found = false;
1323 for (const auto& entry : deps->fields_) {
1324 if (entry.IsResolved()) {
1325 deps->fields_.insert(VerifierDeps::FieldResolution(entry.GetDexFieldIndex(),
1326 entry.GetAccessFlags() - 1,
1327 entry.GetDeclaringClassIndex()));
1328 found = true;
1329 break;
1330 }
1331 }
1332 ASSERT_TRUE(found);
1333 new_class_loader.Assign(soa.Decode<mirror::ClassLoader>(LoadDex("VerifierDeps")));
Nicolas Geoffray6bb7f1b2016-11-03 10:52:49 +00001334 ASSERT_FALSE(decoded_deps.ValidateDependencies(new_class_loader, soa.Self()));
Nicolas Geoffray8904b6f2016-10-28 19:50:34 +01001335 }
1336
1337 {
1338 VerifierDeps decoded_deps(dex_files_, ArrayRef<const uint8_t>(buffer));
1339 VerifierDeps::DexFileDeps* deps = decoded_deps.GetDexFileDeps(*primary_dex_file_);
1340 bool found = false;
1341 for (const auto& entry : deps->fields_) {
Andreas Gampe8a0128a2016-11-28 07:38:35 -08001342 constexpr dex::StringIndex kNewTypeIndex(0);
Nicolas Geoffray8904b6f2016-10-28 19:50:34 +01001343 if (entry.GetDeclaringClassIndex() != kNewTypeIndex) {
1344 deps->fields_.insert(VerifierDeps::FieldResolution(entry.GetDexFieldIndex(),
1345 entry.GetAccessFlags(),
1346 kNewTypeIndex));
1347 found = true;
1348 break;
1349 }
1350 }
1351 ASSERT_TRUE(found);
1352 new_class_loader.Assign(soa.Decode<mirror::ClassLoader>(LoadDex("VerifierDeps")));
Nicolas Geoffray6bb7f1b2016-11-03 10:52:49 +00001353 ASSERT_FALSE(decoded_deps.ValidateDependencies(new_class_loader, soa.Self()));
Nicolas Geoffray8904b6f2016-10-28 19:50:34 +01001354 }
1355
1356 // Mess up with methods.
1357 for (MethodResolutionKind resolution_kind :
1358 { kDirectMethodResolution, kVirtualMethodResolution, kInterfaceMethodResolution }) {
1359 {
1360 VerifierDeps decoded_deps(dex_files_, ArrayRef<const uint8_t>(buffer));
1361 VerifierDeps::DexFileDeps* deps = decoded_deps.GetDexFileDeps(*primary_dex_file_);
1362 bool found = false;
1363 std::set<VerifierDeps::MethodResolution>* methods = GetMethods(deps, resolution_kind);
1364 for (const auto& entry : *methods) {
1365 if (entry.IsResolved()) {
1366 methods->insert(VerifierDeps::MethodResolution(entry.GetDexMethodIndex(),
1367 VerifierDeps::kUnresolvedMarker,
1368 entry.GetDeclaringClassIndex()));
1369 found = true;
1370 break;
1371 }
1372 }
1373 ASSERT_TRUE(found);
1374 new_class_loader.Assign(soa.Decode<mirror::ClassLoader>(LoadDex("VerifierDeps")));
Nicolas Geoffray6bb7f1b2016-11-03 10:52:49 +00001375 ASSERT_FALSE(decoded_deps.ValidateDependencies(new_class_loader, soa.Self()));
Nicolas Geoffray8904b6f2016-10-28 19:50:34 +01001376 }
1377
1378 {
1379 VerifierDeps decoded_deps(dex_files_, ArrayRef<const uint8_t>(buffer));
1380 VerifierDeps::DexFileDeps* deps = decoded_deps.GetDexFileDeps(*primary_dex_file_);
1381 bool found = false;
1382 std::set<VerifierDeps::MethodResolution>* methods = GetMethods(deps, resolution_kind);
1383 for (const auto& entry : *methods) {
1384 if (!entry.IsResolved()) {
Andreas Gampe8a0128a2016-11-28 07:38:35 -08001385 constexpr dex::StringIndex kStringIndexZero(0); // We know there is a class there.
Nicolas Geoffray8904b6f2016-10-28 19:50:34 +01001386 methods->insert(VerifierDeps::MethodResolution(0 /* we know there is a method there */,
1387 VerifierDeps::kUnresolvedMarker - 1,
Andreas Gampe8a0128a2016-11-28 07:38:35 -08001388 kStringIndexZero));
Nicolas Geoffray8904b6f2016-10-28 19:50:34 +01001389 found = true;
1390 break;
1391 }
1392 }
1393 ASSERT_TRUE(found);
1394 new_class_loader.Assign(soa.Decode<mirror::ClassLoader>(LoadDex("VerifierDeps")));
Nicolas Geoffray6bb7f1b2016-11-03 10:52:49 +00001395 ASSERT_FALSE(decoded_deps.ValidateDependencies(new_class_loader, soa.Self()));
Nicolas Geoffray8904b6f2016-10-28 19:50:34 +01001396 }
1397
1398 {
1399 VerifierDeps decoded_deps(dex_files_, ArrayRef<const uint8_t>(buffer));
1400 VerifierDeps::DexFileDeps* deps = decoded_deps.GetDexFileDeps(*primary_dex_file_);
1401 bool found = false;
1402 std::set<VerifierDeps::MethodResolution>* methods = GetMethods(deps, resolution_kind);
1403 for (const auto& entry : *methods) {
1404 if (entry.IsResolved()) {
1405 methods->insert(VerifierDeps::MethodResolution(entry.GetDexMethodIndex(),
1406 entry.GetAccessFlags() - 1,
1407 entry.GetDeclaringClassIndex()));
1408 found = true;
1409 break;
1410 }
1411 }
1412 ASSERT_TRUE(found);
1413 new_class_loader.Assign(soa.Decode<mirror::ClassLoader>(LoadDex("VerifierDeps")));
Nicolas Geoffray6bb7f1b2016-11-03 10:52:49 +00001414 ASSERT_FALSE(decoded_deps.ValidateDependencies(new_class_loader, soa.Self()));
Nicolas Geoffray8904b6f2016-10-28 19:50:34 +01001415 }
1416
1417 {
1418 VerifierDeps decoded_deps(dex_files_, ArrayRef<const uint8_t>(buffer));
1419 VerifierDeps::DexFileDeps* deps = decoded_deps.GetDexFileDeps(*primary_dex_file_);
1420 bool found = false;
1421 std::set<VerifierDeps::MethodResolution>* methods = GetMethods(deps, resolution_kind);
1422 for (const auto& entry : *methods) {
Andreas Gampe8a0128a2016-11-28 07:38:35 -08001423 constexpr dex::StringIndex kNewTypeIndex(0);
Nicolas Geoffray8904b6f2016-10-28 19:50:34 +01001424 if (entry.IsResolved() && entry.GetDeclaringClassIndex() != kNewTypeIndex) {
1425 methods->insert(VerifierDeps::MethodResolution(entry.GetDexMethodIndex(),
1426 entry.GetAccessFlags(),
1427 kNewTypeIndex));
1428 found = true;
1429 break;
1430 }
1431 }
1432 ASSERT_TRUE(found);
1433 new_class_loader.Assign(soa.Decode<mirror::ClassLoader>(LoadDex("VerifierDeps")));
Nicolas Geoffray6bb7f1b2016-11-03 10:52:49 +00001434 ASSERT_FALSE(decoded_deps.ValidateDependencies(new_class_loader, soa.Self()));
Nicolas Geoffray8904b6f2016-10-28 19:50:34 +01001435 }
1436
1437 {
1438 VerifierDeps decoded_deps(dex_files_, ArrayRef<const uint8_t>(buffer));
1439 VerifierDeps::DexFileDeps* deps = decoded_deps.GetDexFileDeps(*primary_dex_file_);
1440 bool found = false;
1441 std::set<VerifierDeps::MethodResolution>* methods = GetMethods(deps, resolution_kind);
1442 for (const auto& entry : *methods) {
1443 if (entry.IsResolved()) {
1444 GetMethods(deps, GetNextResolutionKind(resolution_kind))->insert(
1445 VerifierDeps::MethodResolution(entry.GetDexMethodIndex(),
1446 entry.GetAccessFlags(),
1447 entry.GetDeclaringClassIndex()));
1448 found = true;
1449 }
1450 }
1451 ASSERT_TRUE(found);
1452 new_class_loader.Assign(soa.Decode<mirror::ClassLoader>(LoadDex("VerifierDeps")));
Nicolas Geoffray6bb7f1b2016-11-03 10:52:49 +00001453 ASSERT_FALSE(decoded_deps.ValidateDependencies(new_class_loader, soa.Self()));
Nicolas Geoffray8904b6f2016-10-28 19:50:34 +01001454 }
1455
1456 {
1457 VerifierDeps decoded_deps(dex_files_, ArrayRef<const uint8_t>(buffer));
1458 VerifierDeps::DexFileDeps* deps = decoded_deps.GetDexFileDeps(*primary_dex_file_);
1459 bool found = false;
1460 std::set<VerifierDeps::MethodResolution>* methods = GetMethods(deps, resolution_kind);
1461 for (const auto& entry : *methods) {
1462 if (entry.IsResolved()) {
1463 GetMethods(deps, GetNextResolutionKind(GetNextResolutionKind(resolution_kind)))->insert(
1464 VerifierDeps::MethodResolution(entry.GetDexMethodIndex(),
1465 entry.GetAccessFlags(),
1466 entry.GetDeclaringClassIndex()));
1467 found = true;
1468 }
1469 }
1470 ASSERT_TRUE(found);
1471 new_class_loader.Assign(soa.Decode<mirror::ClassLoader>(LoadDex("VerifierDeps")));
Nicolas Geoffray6bb7f1b2016-11-03 10:52:49 +00001472 ASSERT_FALSE(decoded_deps.ValidateDependencies(new_class_loader, soa.Self()));
1473 }
1474 }
1475}
1476
1477TEST_F(VerifierDepsTest, CompilerDriver) {
1478 SetupCompilerDriver();
1479
1480 // Test both multi-dex and single-dex configuration.
1481 for (const char* multi : { "MultiDex", static_cast<const char*>(nullptr) }) {
1482 // Test that the compiler driver behaves as expected when the dependencies
1483 // verify and when they don't verify.
1484 for (bool verify_failure : { false, true }) {
1485 {
1486 ScopedObjectAccess soa(Thread::Current());
1487 LoadDexFile(&soa, "VerifierDeps", multi);
1488 }
1489 VerifyWithCompilerDriver(/* verifier_deps */ nullptr);
1490
1491 std::vector<uint8_t> buffer;
1492 verifier_deps_->Encode(dex_files_, &buffer);
1493
1494 {
1495 ScopedObjectAccess soa(Thread::Current());
1496 LoadDexFile(&soa, "VerifierDeps", multi);
1497 }
1498 verifier::VerifierDeps decoded_deps(dex_files_, ArrayRef<const uint8_t>(buffer));
1499 if (verify_failure) {
1500 // Just taint the decoded VerifierDeps with one invalid entry.
1501 VerifierDeps::DexFileDeps* deps = decoded_deps.GetDexFileDeps(*primary_dex_file_);
1502 bool found = false;
1503 for (const auto& entry : deps->classes_) {
1504 if (entry.IsResolved()) {
1505 deps->classes_.insert(VerifierDeps::ClassResolution(
1506 entry.GetDexTypeIndex(), VerifierDeps::kUnresolvedMarker));
1507 found = true;
1508 break;
1509 }
1510 }
1511 ASSERT_TRUE(found);
1512 }
1513 VerifyWithCompilerDriver(&decoded_deps);
1514
1515 if (verify_failure) {
1516 ASSERT_FALSE(verifier_deps_ == nullptr);
1517 ASSERT_FALSE(verifier_deps_->Equals(decoded_deps));
1518 } else {
1519 ASSERT_TRUE(verifier_deps_ == nullptr);
1520 VerifyClassStatus(decoded_deps);
1521 }
Nicolas Geoffray8904b6f2016-10-28 19:50:34 +01001522 }
1523 }
1524}
1525
David Brazdilca3c8c32016-09-06 14:04:48 +01001526} // namespace verifier
1527} // namespace art