blob: 4f06a91448780b8ea9c03437288da31bb64463c5 [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));
Nicolas Geoffray0e2fe0f2016-12-21 16:54:52 +0000527 ASSERT_TRUE(HasAssignable("Ljava/util/Collection;", "Ljava/util/Set;", true));
David Brazdilca3c8c32016-09-06 14:04:48 +0100528}
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
David Brazdilca3c8c32016-09-06 14:04:48 +0100542TEST_F(VerifierDepsTest, NotAssignable_BothInBoot) {
543 ASSERT_TRUE(TestAssignabilityRecording(/* dst */ "Ljava/lang/Exception;",
544 /* src */ "Ljava/util/SimpleTimeZone;",
545 /* is_strict */ true,
546 /* is_assignable */ false));
547 ASSERT_TRUE(HasAssignable("Ljava/lang/Exception;", "Ljava/util/SimpleTimeZone;", false));
548}
549
550TEST_F(VerifierDepsTest, NotAssignable_DestinationInBoot1) {
551 ASSERT_TRUE(TestAssignabilityRecording(/* dst */ "Ljava/lang/Exception;",
552 /* src */ "LMySSLSocket;",
553 /* is_strict */ true,
554 /* is_assignable */ false));
Nicolas Geoffray119e8462016-12-21 10:29:43 +0000555 ASSERT_TRUE(HasAssignable("Ljava/lang/Exception;", "Ljavax/net/ssl/SSLSocket;", false));
David Brazdilca3c8c32016-09-06 14:04:48 +0100556}
557
558TEST_F(VerifierDepsTest, NotAssignable_DestinationInBoot2) {
559 ASSERT_TRUE(TestAssignabilityRecording(/* dst */ "Ljava/lang/Exception;",
560 /* src */ "LMySimpleTimeZone;",
561 /* is_strict */ true,
562 /* is_assignable */ false));
Nicolas Geoffray119e8462016-12-21 10:29:43 +0000563 ASSERT_TRUE(HasAssignable("Ljava/lang/Exception;", "Ljava/util/SimpleTimeZone;", false));
David Brazdilca3c8c32016-09-06 14:04:48 +0100564}
565
566TEST_F(VerifierDepsTest, NotAssignable_BothArrays) {
567 ASSERT_TRUE(TestAssignabilityRecording(/* dst */ "[Ljava/lang/Exception;",
568 /* src */ "[Ljava/util/SimpleTimeZone;",
569 /* is_strict */ true,
570 /* is_assignable */ false));
571 ASSERT_TRUE(HasAssignable("Ljava/lang/Exception;", "Ljava/util/SimpleTimeZone;", false));
572}
573
574TEST_F(VerifierDepsTest, ArgumentType_ResolvedClass) {
575 ASSERT_TRUE(VerifyMethod("ArgumentType_ResolvedClass"));
576 ASSERT_TRUE(HasClass("Ljava/lang/Thread;", true, "public"));
577}
578
David Brazdilca3c8c32016-09-06 14:04:48 +0100579TEST_F(VerifierDepsTest, ArgumentType_UnresolvedClass) {
580 ASSERT_TRUE(VerifyMethod("ArgumentType_UnresolvedClass"));
581 ASSERT_TRUE(HasClass("LUnresolvedClass;", false));
582}
583
584TEST_F(VerifierDepsTest, ArgumentType_UnresolvedSuper) {
585 ASSERT_TRUE(VerifyMethod("ArgumentType_UnresolvedSuper"));
586 ASSERT_TRUE(HasClass("LMySetWithUnresolvedSuper;", false));
587}
588
589TEST_F(VerifierDepsTest, ReturnType_Reference) {
590 ASSERT_TRUE(VerifyMethod("ReturnType_Reference"));
591 ASSERT_TRUE(HasAssignable("Ljava/lang/Throwable;", "Ljava/lang/IllegalStateException;", true));
592}
593
594TEST_F(VerifierDepsTest, ReturnType_Array) {
595 ASSERT_FALSE(VerifyMethod("ReturnType_Array"));
596 ASSERT_TRUE(HasAssignable("Ljava/lang/Integer;", "Ljava/lang/IllegalStateException;", false));
597}
598
599TEST_F(VerifierDepsTest, InvokeArgumentType) {
600 ASSERT_TRUE(VerifyMethod("InvokeArgumentType"));
601 ASSERT_TRUE(HasClass("Ljava/text/SimpleDateFormat;", true, "public"));
602 ASSERT_TRUE(HasClass("Ljava/util/SimpleTimeZone;", true, "public"));
603 ASSERT_TRUE(HasMethod("virtual",
604 "Ljava/text/SimpleDateFormat;",
605 "setTimeZone",
606 "(Ljava/util/TimeZone;)V",
607 true,
608 "public",
609 "Ljava/text/DateFormat;"));
610 ASSERT_TRUE(HasAssignable("Ljava/util/TimeZone;", "Ljava/util/SimpleTimeZone;", true));
611}
612
613TEST_F(VerifierDepsTest, MergeTypes_RegisterLines) {
614 ASSERT_TRUE(VerifyMethod("MergeTypes_RegisterLines"));
Nicolas Geoffray119e8462016-12-21 10:29:43 +0000615 ASSERT_TRUE(HasAssignable("Ljava/lang/Exception;", "Ljava/net/SocketTimeoutException;", true));
David Brazdilca3c8c32016-09-06 14:04:48 +0100616 ASSERT_TRUE(HasAssignable(
617 "Ljava/lang/Exception;", "Ljava/util/concurrent/TimeoutException;", true));
618}
619
620TEST_F(VerifierDepsTest, MergeTypes_IfInstanceOf) {
621 ASSERT_TRUE(VerifyMethod("MergeTypes_IfInstanceOf"));
622 ASSERT_TRUE(HasAssignable("Ljava/lang/Exception;", "Ljava/net/SocketTimeoutException;", true));
623 ASSERT_TRUE(HasAssignable(
624 "Ljava/lang/Exception;", "Ljava/util/concurrent/TimeoutException;", true));
625 ASSERT_TRUE(HasAssignable("Ljava/net/SocketTimeoutException;", "Ljava/lang/Exception;", false));
626}
627
628TEST_F(VerifierDepsTest, MergeTypes_Unresolved) {
629 ASSERT_TRUE(VerifyMethod("MergeTypes_Unresolved"));
630 ASSERT_TRUE(HasAssignable("Ljava/lang/Exception;", "Ljava/net/SocketTimeoutException;", true));
631 ASSERT_TRUE(HasAssignable(
632 "Ljava/lang/Exception;", "Ljava/util/concurrent/TimeoutException;", true));
633}
634
635TEST_F(VerifierDepsTest, ConstClass_Resolved) {
636 ASSERT_TRUE(VerifyMethod("ConstClass_Resolved"));
637 ASSERT_TRUE(HasClass("Ljava/lang/IllegalStateException;", true, "public"));
638}
639
640TEST_F(VerifierDepsTest, ConstClass_Unresolved) {
641 ASSERT_TRUE(VerifyMethod("ConstClass_Unresolved"));
642 ASSERT_TRUE(HasClass("LUnresolvedClass;", false));
643}
644
645TEST_F(VerifierDepsTest, CheckCast_Resolved) {
646 ASSERT_TRUE(VerifyMethod("CheckCast_Resolved"));
647 ASSERT_TRUE(HasClass("Ljava/lang/IllegalStateException;", true, "public"));
648}
649
650TEST_F(VerifierDepsTest, CheckCast_Unresolved) {
651 ASSERT_TRUE(VerifyMethod("CheckCast_Unresolved"));
652 ASSERT_TRUE(HasClass("LUnresolvedClass;", false));
653}
654
655TEST_F(VerifierDepsTest, InstanceOf_Resolved) {
656 ASSERT_TRUE(VerifyMethod("InstanceOf_Resolved"));
657 ASSERT_TRUE(HasClass("Ljava/lang/IllegalStateException;", true, "public"));
658}
659
660TEST_F(VerifierDepsTest, InstanceOf_Unresolved) {
661 ASSERT_TRUE(VerifyMethod("InstanceOf_Unresolved"));
662 ASSERT_TRUE(HasClass("LUnresolvedClass;", false));
663}
664
665TEST_F(VerifierDepsTest, NewInstance_Resolved) {
666 ASSERT_TRUE(VerifyMethod("NewInstance_Resolved"));
667 ASSERT_TRUE(HasClass("Ljava/lang/IllegalStateException;", true, "public"));
668}
669
670TEST_F(VerifierDepsTest, NewInstance_Unresolved) {
671 ASSERT_TRUE(VerifyMethod("NewInstance_Unresolved"));
672 ASSERT_TRUE(HasClass("LUnresolvedClass;", false));
673}
674
David Brazdilca3c8c32016-09-06 14:04:48 +0100675TEST_F(VerifierDepsTest, NewArray_Unresolved) {
676 ASSERT_TRUE(VerifyMethod("NewArray_Unresolved"));
677 ASSERT_TRUE(HasClass("[LUnresolvedClass;", false));
678}
679
680TEST_F(VerifierDepsTest, Throw) {
681 ASSERT_TRUE(VerifyMethod("Throw"));
682 ASSERT_TRUE(HasAssignable("Ljava/lang/Throwable;", "Ljava/lang/IllegalStateException;", true));
683}
684
685TEST_F(VerifierDepsTest, MoveException_Resolved) {
686 ASSERT_TRUE(VerifyMethod("MoveException_Resolved"));
687 ASSERT_TRUE(HasClass("Ljava/io/InterruptedIOException;", true, "public"));
688 ASSERT_TRUE(HasClass("Ljava/net/SocketTimeoutException;", true, "public"));
689 ASSERT_TRUE(HasClass("Ljava/util/zip/ZipException;", true, "public"));
690
691 // Testing that all exception types are assignable to Throwable.
692 ASSERT_TRUE(HasAssignable("Ljava/lang/Throwable;", "Ljava/io/InterruptedIOException;", true));
693 ASSERT_TRUE(HasAssignable("Ljava/lang/Throwable;", "Ljava/net/SocketTimeoutException;", true));
694 ASSERT_TRUE(HasAssignable("Ljava/lang/Throwable;", "Ljava/util/zip/ZipException;", true));
695
696 // Testing that the merge type is assignable to Throwable.
697 ASSERT_TRUE(HasAssignable("Ljava/lang/Throwable;", "Ljava/io/IOException;", true));
698
699 // Merging of exception types.
700 ASSERT_TRUE(HasAssignable("Ljava/io/IOException;", "Ljava/io/InterruptedIOException;", true));
701 ASSERT_TRUE(HasAssignable("Ljava/io/IOException;", "Ljava/util/zip/ZipException;", true));
702 ASSERT_TRUE(HasAssignable(
703 "Ljava/io/InterruptedIOException;", "Ljava/net/SocketTimeoutException;", true));
704}
705
706TEST_F(VerifierDepsTest, MoveException_Unresolved) {
707 ASSERT_FALSE(VerifyMethod("MoveException_Unresolved"));
708 ASSERT_TRUE(HasClass("LUnresolvedException;", false));
709}
710
711TEST_F(VerifierDepsTest, StaticField_Resolved_DeclaredInReferenced) {
712 ASSERT_TRUE(VerifyMethod("StaticField_Resolved_DeclaredInReferenced"));
713 ASSERT_TRUE(HasClass("Ljava/lang/System;", true, "public final"));
714 ASSERT_TRUE(HasField("Ljava/lang/System;",
715 "out",
716 "Ljava/io/PrintStream;",
717 true,
718 "public final static",
719 "Ljava/lang/System;"));
720}
721
722TEST_F(VerifierDepsTest, StaticField_Resolved_DeclaredInSuperclass1) {
723 ASSERT_TRUE(VerifyMethod("StaticField_Resolved_DeclaredInSuperclass1"));
724 ASSERT_TRUE(HasClass("Ljava/util/SimpleTimeZone;", true, "public"));
725 ASSERT_TRUE(HasField(
726 "Ljava/util/SimpleTimeZone;", "LONG", "I", true, "public final static", "Ljava/util/TimeZone;"));
727}
728
729TEST_F(VerifierDepsTest, StaticField_Resolved_DeclaredInSuperclass2) {
730 ASSERT_TRUE(VerifyMethod("StaticField_Resolved_DeclaredInSuperclass2"));
731 ASSERT_TRUE(HasField(
732 "LMySimpleTimeZone;", "SHORT", "I", true, "public final static", "Ljava/util/TimeZone;"));
733}
734
735TEST_F(VerifierDepsTest, StaticField_Resolved_DeclaredInInterface1) {
736 ASSERT_TRUE(VerifyMethod("StaticField_Resolved_DeclaredInInterface1"));
737 ASSERT_TRUE(HasClass("Ljavax/xml/transform/dom/DOMResult;", true, "public"));
738 ASSERT_TRUE(HasField("Ljavax/xml/transform/dom/DOMResult;",
739 "PI_ENABLE_OUTPUT_ESCAPING",
740 "Ljava/lang/String;",
741 true,
742 "public final static",
743 "Ljavax/xml/transform/Result;"));
744}
745
746TEST_F(VerifierDepsTest, StaticField_Resolved_DeclaredInInterface2) {
747 ASSERT_TRUE(VerifyMethod("StaticField_Resolved_DeclaredInInterface2"));
748 ASSERT_TRUE(HasField("LMyDOMResult;",
749 "PI_ENABLE_OUTPUT_ESCAPING",
750 "Ljava/lang/String;",
751 true,
752 "public final static",
753 "Ljavax/xml/transform/Result;"));
754}
755
756TEST_F(VerifierDepsTest, StaticField_Resolved_DeclaredInInterface3) {
757 ASSERT_TRUE(VerifyMethod("StaticField_Resolved_DeclaredInInterface3"));
758 ASSERT_TRUE(HasField("LMyResult;",
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_DeclaredInInterface4) {
767 ASSERT_TRUE(VerifyMethod("StaticField_Resolved_DeclaredInInterface4"));
768 ASSERT_TRUE(HasField("LMyDocument;",
769 "ELEMENT_NODE",
770 "S",
771 true,
772 "public final static",
773 "Lorg/w3c/dom/Node;"));
774}
775
776TEST_F(VerifierDepsTest, StaticField_Unresolved_ReferrerInBoot) {
777 ASSERT_TRUE(VerifyMethod("StaticField_Unresolved_ReferrerInBoot"));
778 ASSERT_TRUE(HasClass("Ljava/util/TimeZone;", true, "public abstract"));
779 ASSERT_TRUE(HasField("Ljava/util/TimeZone;", "x", "I", false));
780}
781
782TEST_F(VerifierDepsTest, StaticField_Unresolved_ReferrerInDex) {
783 ASSERT_TRUE(VerifyMethod("StaticField_Unresolved_ReferrerInDex"));
784 ASSERT_TRUE(HasField("LMyThreadSet;", "x", "I", false));
785}
786
787TEST_F(VerifierDepsTest, InstanceField_Resolved_DeclaredInReferenced) {
788 ASSERT_TRUE(VerifyMethod("InstanceField_Resolved_DeclaredInReferenced"));
789 ASSERT_TRUE(HasClass("Ljava/io/InterruptedIOException;", true, "public"));
790 ASSERT_TRUE(HasField("Ljava/io/InterruptedIOException;",
791 "bytesTransferred",
792 "I",
793 true,
794 "public",
795 "Ljava/io/InterruptedIOException;"));
796 ASSERT_TRUE(HasAssignable(
Nicolas Geoffray119e8462016-12-21 10:29:43 +0000797 "Ljava/io/InterruptedIOException;", "Ljava/net/SocketTimeoutException;", true));
David Brazdilca3c8c32016-09-06 14:04:48 +0100798}
799
800TEST_F(VerifierDepsTest, InstanceField_Resolved_DeclaredInSuperclass1) {
801 ASSERT_TRUE(VerifyMethod("InstanceField_Resolved_DeclaredInSuperclass1"));
802 ASSERT_TRUE(HasClass("Ljava/net/SocketTimeoutException;", true, "public"));
803 ASSERT_TRUE(HasField("Ljava/net/SocketTimeoutException;",
804 "bytesTransferred",
805 "I",
806 true,
807 "public",
808 "Ljava/io/InterruptedIOException;"));
809 ASSERT_TRUE(HasAssignable(
Nicolas Geoffray119e8462016-12-21 10:29:43 +0000810 "Ljava/io/InterruptedIOException;", "Ljava/net/SocketTimeoutException;", true));
David Brazdilca3c8c32016-09-06 14:04:48 +0100811}
812
813TEST_F(VerifierDepsTest, InstanceField_Resolved_DeclaredInSuperclass2) {
814 ASSERT_TRUE(VerifyMethod("InstanceField_Resolved_DeclaredInSuperclass2"));
815 ASSERT_TRUE(HasField("LMySocketTimeoutException;",
816 "bytesTransferred",
817 "I",
818 true,
819 "public",
820 "Ljava/io/InterruptedIOException;"));
821 ASSERT_TRUE(HasAssignable(
Nicolas Geoffray119e8462016-12-21 10:29:43 +0000822 "Ljava/io/InterruptedIOException;", "Ljava/net/SocketTimeoutException;", true));
David Brazdilca3c8c32016-09-06 14:04:48 +0100823}
824
825TEST_F(VerifierDepsTest, InstanceField_Unresolved_ReferrerInBoot) {
826 ASSERT_TRUE(VerifyMethod("InstanceField_Unresolved_ReferrerInBoot"));
827 ASSERT_TRUE(HasClass("Ljava/io/InterruptedIOException;", true, "public"));
828 ASSERT_TRUE(HasField("Ljava/io/InterruptedIOException;", "x", "I", false));
829}
830
831TEST_F(VerifierDepsTest, InstanceField_Unresolved_ReferrerInDex) {
832 ASSERT_TRUE(VerifyMethod("InstanceField_Unresolved_ReferrerInDex"));
833 ASSERT_TRUE(HasField("LMyThreadSet;", "x", "I", false));
834}
835
836TEST_F(VerifierDepsTest, InvokeStatic_Resolved_DeclaredInReferenced) {
837 ASSERT_TRUE(VerifyMethod("InvokeStatic_Resolved_DeclaredInReferenced"));
838 ASSERT_TRUE(HasClass("Ljava/net/Socket;", true, "public"));
839 ASSERT_TRUE(HasMethod("direct",
840 "Ljava/net/Socket;",
841 "setSocketImplFactory",
842 "(Ljava/net/SocketImplFactory;)V",
843 true,
844 "public static",
845 "Ljava/net/Socket;"));
846}
847
848TEST_F(VerifierDepsTest, InvokeStatic_Resolved_DeclaredInSuperclass1) {
849 ASSERT_TRUE(VerifyMethod("InvokeStatic_Resolved_DeclaredInSuperclass1"));
850 ASSERT_TRUE(HasClass("Ljavax/net/ssl/SSLSocket;", true, "public abstract"));
851 ASSERT_TRUE(HasMethod("direct",
852 "Ljavax/net/ssl/SSLSocket;",
853 "setSocketImplFactory",
854 "(Ljava/net/SocketImplFactory;)V",
855 true,
856 "public static",
857 "Ljava/net/Socket;"));
858}
859
860TEST_F(VerifierDepsTest, InvokeStatic_Resolved_DeclaredInSuperclass2) {
861 ASSERT_TRUE(VerifyMethod("InvokeStatic_Resolved_DeclaredInSuperclass2"));
862 ASSERT_TRUE(HasMethod("direct",
863 "LMySSLSocket;",
864 "setSocketImplFactory",
865 "(Ljava/net/SocketImplFactory;)V",
866 true,
867 "public static",
868 "Ljava/net/Socket;"));
869}
870
871TEST_F(VerifierDepsTest, InvokeStatic_DeclaredInInterface1) {
872 ASSERT_TRUE(VerifyMethod("InvokeStatic_DeclaredInInterface1"));
873 ASSERT_TRUE(HasClass("Ljava/util/Map$Entry;", true, "public abstract interface"));
874 ASSERT_TRUE(HasMethod("direct",
875 "Ljava/util/Map$Entry;",
876 "comparingByKey",
877 "()Ljava/util/Comparator;",
878 true,
879 "public static",
880 "Ljava/util/Map$Entry;"));
881}
882
883TEST_F(VerifierDepsTest, InvokeStatic_DeclaredInInterface2) {
884 ASSERT_FALSE(VerifyMethod("InvokeStatic_DeclaredInInterface2"));
885 ASSERT_TRUE(HasClass("Ljava/util/AbstractMap$SimpleEntry;", true, "public"));
886 ASSERT_TRUE(HasMethod("direct",
887 "Ljava/util/AbstractMap$SimpleEntry;",
888 "comparingByKey",
889 "()Ljava/util/Comparator;",
890 false));
891}
892
893TEST_F(VerifierDepsTest, InvokeStatic_Unresolved1) {
894 ASSERT_FALSE(VerifyMethod("InvokeStatic_Unresolved1"));
895 ASSERT_TRUE(HasClass("Ljavax/net/ssl/SSLSocket;", true, "public abstract"));
896 ASSERT_TRUE(HasMethod("direct", "Ljavax/net/ssl/SSLSocket;", "x", "()V", false));
897}
898
899TEST_F(VerifierDepsTest, InvokeStatic_Unresolved2) {
900 ASSERT_FALSE(VerifyMethod("InvokeStatic_Unresolved2"));
901 ASSERT_TRUE(HasMethod("direct", "LMySSLSocket;", "x", "()V", false));
902}
903
904TEST_F(VerifierDepsTest, InvokeDirect_Resolved_DeclaredInReferenced) {
905 ASSERT_TRUE(VerifyMethod("InvokeDirect_Resolved_DeclaredInReferenced"));
906 ASSERT_TRUE(HasClass("Ljava/net/Socket;", true, "public"));
907 ASSERT_TRUE(HasMethod(
908 "direct", "Ljava/net/Socket;", "<init>", "()V", true, "public", "Ljava/net/Socket;"));
909}
910
911TEST_F(VerifierDepsTest, InvokeDirect_Resolved_DeclaredInSuperclass1) {
912 ASSERT_FALSE(VerifyMethod("InvokeDirect_Resolved_DeclaredInSuperclass1"));
913 ASSERT_TRUE(HasClass("Ljavax/net/ssl/SSLSocket;", true, "public abstract"));
914 ASSERT_TRUE(HasMethod("direct",
915 "Ljavax/net/ssl/SSLSocket;",
916 "checkOldImpl",
917 "()V",
918 true,
919 "private",
920 "Ljava/net/Socket;"));
921}
922
923TEST_F(VerifierDepsTest, InvokeDirect_Resolved_DeclaredInSuperclass2) {
924 ASSERT_FALSE(VerifyMethod("InvokeDirect_Resolved_DeclaredInSuperclass2"));
925 ASSERT_TRUE(HasMethod(
926 "direct", "LMySSLSocket;", "checkOldImpl", "()V", true, "private", "Ljava/net/Socket;"));
927}
928
929TEST_F(VerifierDepsTest, InvokeDirect_Unresolved1) {
930 ASSERT_FALSE(VerifyMethod("InvokeDirect_Unresolved1"));
931 ASSERT_TRUE(HasClass("Ljavax/net/ssl/SSLSocket;", true, "public abstract"));
932 ASSERT_TRUE(HasMethod("direct", "Ljavax/net/ssl/SSLSocket;", "x", "()V", false));
933}
934
935TEST_F(VerifierDepsTest, InvokeDirect_Unresolved2) {
936 ASSERT_FALSE(VerifyMethod("InvokeDirect_Unresolved2"));
937 ASSERT_TRUE(HasMethod("direct", "LMySSLSocket;", "x", "()V", false));
938}
939
940TEST_F(VerifierDepsTest, InvokeVirtual_Resolved_DeclaredInReferenced) {
941 ASSERT_TRUE(VerifyMethod("InvokeVirtual_Resolved_DeclaredInReferenced"));
942 ASSERT_TRUE(HasClass("Ljava/lang/Throwable;", true, "public"));
943 ASSERT_TRUE(HasMethod("virtual",
944 "Ljava/lang/Throwable;",
945 "getMessage",
946 "()Ljava/lang/String;",
947 true,
948 "public",
949 "Ljava/lang/Throwable;"));
950 // Type dependency on `this` argument.
Nicolas Geoffray119e8462016-12-21 10:29:43 +0000951 ASSERT_TRUE(HasAssignable("Ljava/lang/Throwable;", "Ljava/net/SocketTimeoutException;", true));
David Brazdilca3c8c32016-09-06 14:04:48 +0100952}
953
954TEST_F(VerifierDepsTest, InvokeVirtual_Resolved_DeclaredInSuperclass1) {
955 ASSERT_TRUE(VerifyMethod("InvokeVirtual_Resolved_DeclaredInSuperclass1"));
956 ASSERT_TRUE(HasClass("Ljava/io/InterruptedIOException;", true, "public"));
957 ASSERT_TRUE(HasMethod("virtual",
958 "Ljava/io/InterruptedIOException;",
959 "getMessage",
960 "()Ljava/lang/String;",
961 true,
962 "public",
963 "Ljava/lang/Throwable;"));
964 // Type dependency on `this` argument.
Nicolas Geoffray119e8462016-12-21 10:29:43 +0000965 ASSERT_TRUE(HasAssignable("Ljava/lang/Throwable;", "Ljava/net/SocketTimeoutException;", true));
David Brazdilca3c8c32016-09-06 14:04:48 +0100966}
967
968TEST_F(VerifierDepsTest, InvokeVirtual_Resolved_DeclaredInSuperclass2) {
969 ASSERT_TRUE(VerifyMethod("InvokeVirtual_Resolved_DeclaredInSuperclass2"));
970 ASSERT_TRUE(HasMethod("virtual",
971 "LMySocketTimeoutException;",
972 "getMessage",
973 "()Ljava/lang/String;",
974 true,
975 "public",
976 "Ljava/lang/Throwable;"));
977}
978
979TEST_F(VerifierDepsTest, InvokeVirtual_Resolved_DeclaredInSuperinterface) {
980 ASSERT_TRUE(VerifyMethod("InvokeVirtual_Resolved_DeclaredInSuperinterface"));
981 ASSERT_TRUE(HasMethod("virtual",
982 "LMyThreadSet;",
983 "size",
984 "()I",
985 true,
986 "public abstract",
987 "Ljava/util/Set;"));
988}
989
990TEST_F(VerifierDepsTest, InvokeVirtual_Unresolved1) {
991 ASSERT_FALSE(VerifyMethod("InvokeVirtual_Unresolved1"));
992 ASSERT_TRUE(HasClass("Ljava/io/InterruptedIOException;", true, "public"));
993 ASSERT_TRUE(HasMethod("virtual", "Ljava/io/InterruptedIOException;", "x", "()V", false));
994}
995
996TEST_F(VerifierDepsTest, InvokeVirtual_Unresolved2) {
997 ASSERT_FALSE(VerifyMethod("InvokeVirtual_Unresolved2"));
998 ASSERT_TRUE(HasMethod("virtual", "LMySocketTimeoutException;", "x", "()V", false));
999}
1000
1001TEST_F(VerifierDepsTest, InvokeVirtual_ActuallyDirect) {
1002 ASSERT_FALSE(VerifyMethod("InvokeVirtual_ActuallyDirect"));
1003 ASSERT_TRUE(HasMethod("virtual", "LMyThread;", "activeCount", "()I", false));
1004 ASSERT_TRUE(HasMethod("direct",
1005 "LMyThread;",
1006 "activeCount",
1007 "()I",
1008 true,
1009 "public static",
1010 "Ljava/lang/Thread;"));
1011}
1012
1013TEST_F(VerifierDepsTest, InvokeInterface_Resolved_DeclaredInReferenced) {
1014 ASSERT_TRUE(VerifyMethod("InvokeInterface_Resolved_DeclaredInReferenced"));
1015 ASSERT_TRUE(HasClass("Ljava/lang/Runnable;", true, "public abstract interface"));
1016 ASSERT_TRUE(HasMethod("interface",
1017 "Ljava/lang/Runnable;",
1018 "run",
1019 "()V",
1020 true,
1021 "public abstract",
1022 "Ljava/lang/Runnable;"));
1023}
1024
1025TEST_F(VerifierDepsTest, InvokeInterface_Resolved_DeclaredInSuperclass) {
1026 ASSERT_FALSE(VerifyMethod("InvokeInterface_Resolved_DeclaredInSuperclass"));
1027 ASSERT_TRUE(HasMethod("interface", "LMyThread;", "join", "()V", false));
1028}
1029
1030TEST_F(VerifierDepsTest, InvokeInterface_Resolved_DeclaredInSuperinterface1) {
1031 ASSERT_FALSE(VerifyMethod("InvokeInterface_Resolved_DeclaredInSuperinterface1"));
1032 ASSERT_TRUE(HasMethod("interface",
1033 "LMyThreadSet;",
1034 "run",
1035 "()V",
1036 true,
1037 "public abstract",
1038 "Ljava/lang/Runnable;"));
1039}
1040
1041TEST_F(VerifierDepsTest, InvokeInterface_Resolved_DeclaredInSuperinterface2) {
1042 ASSERT_FALSE(VerifyMethod("InvokeInterface_Resolved_DeclaredInSuperinterface2"));
1043 ASSERT_TRUE(HasMethod("interface",
1044 "LMyThreadSet;",
1045 "isEmpty",
1046 "()Z",
1047 true,
1048 "public abstract",
1049 "Ljava/util/Set;"));
1050}
1051
1052TEST_F(VerifierDepsTest, InvokeInterface_Unresolved1) {
1053 ASSERT_FALSE(VerifyMethod("InvokeInterface_Unresolved1"));
1054 ASSERT_TRUE(HasClass("Ljava/lang/Runnable;", true, "public abstract interface"));
1055 ASSERT_TRUE(HasMethod("interface", "Ljava/lang/Runnable;", "x", "()V", false));
1056}
1057
1058TEST_F(VerifierDepsTest, InvokeInterface_Unresolved2) {
1059 ASSERT_FALSE(VerifyMethod("InvokeInterface_Unresolved2"));
1060 ASSERT_TRUE(HasMethod("interface", "LMyThreadSet;", "x", "()V", false));
1061}
1062
1063TEST_F(VerifierDepsTest, InvokeSuper_ThisAssignable) {
1064 ASSERT_TRUE(VerifyMethod("InvokeSuper_ThisAssignable"));
1065 ASSERT_TRUE(HasClass("Ljava/lang/Runnable;", true, "public abstract interface"));
Nicolas Geoffray0e2fe0f2016-12-21 16:54:52 +00001066 ASSERT_TRUE(HasAssignable("Ljava/lang/Runnable;", "Ljava/lang/Thread;", true));
David Brazdilca3c8c32016-09-06 14:04:48 +01001067 ASSERT_TRUE(HasMethod("interface",
1068 "Ljava/lang/Runnable;",
1069 "run",
1070 "()V",
1071 true,
1072 "public abstract",
1073 "Ljava/lang/Runnable;"));
1074}
1075
1076TEST_F(VerifierDepsTest, InvokeSuper_ThisNotAssignable) {
1077 ASSERT_FALSE(VerifyMethod("InvokeSuper_ThisNotAssignable"));
1078 ASSERT_TRUE(HasClass("Ljava/lang/Integer;", true, "public final"));
Nicolas Geoffray119e8462016-12-21 10:29:43 +00001079 ASSERT_TRUE(HasAssignable("Ljava/lang/Integer;", "Ljava/lang/Thread;", false));
David Brazdilca3c8c32016-09-06 14:04:48 +01001080 ASSERT_TRUE(HasMethod(
1081 "virtual", "Ljava/lang/Integer;", "intValue", "()I", true, "public", "Ljava/lang/Integer;"));
1082}
1083
Nicolas Geoffray0f1cb172017-01-05 15:23:19 +00001084TEST_F(VerifierDepsTest, ArgumentType_ResolvedReferenceArray) {
1085 ASSERT_TRUE(VerifyMethod("ArgumentType_ResolvedReferenceArray"));
1086 ASSERT_TRUE(HasClass("[Ljava/lang/Thread;", true, "public final abstract"));
1087}
1088
1089TEST_F(VerifierDepsTest, NewArray_Resolved) {
1090 ASSERT_TRUE(VerifyMethod("NewArray_Resolved"));
1091 ASSERT_TRUE(HasClass("[Ljava/lang/IllegalStateException;", true, "public final abstract"));
1092}
1093
David Brazdil6f82fbd2016-09-14 11:55:26 +01001094TEST_F(VerifierDepsTest, EncodeDecode) {
1095 VerifyDexFile();
1096
1097 ASSERT_EQ(1u, NumberOfCompiledDexFiles());
1098 ASSERT_TRUE(HasEachKindOfRecord());
1099
1100 std::vector<uint8_t> buffer;
Nicolas Geoffrayd01f60c2016-10-28 14:45:48 +01001101 verifier_deps_->Encode(dex_files_, &buffer);
David Brazdil6f82fbd2016-09-14 11:55:26 +01001102 ASSERT_FALSE(buffer.empty());
1103
Nicolas Geoffraye70dd562016-10-30 21:03:35 +00001104 VerifierDeps decoded_deps(dex_files_, ArrayRef<const uint8_t>(buffer));
David Brazdil6f82fbd2016-09-14 11:55:26 +01001105 ASSERT_TRUE(verifier_deps_->Equals(decoded_deps));
1106}
1107
Nicolas Geoffrayd01f60c2016-10-28 14:45:48 +01001108TEST_F(VerifierDepsTest, EncodeDecodeMulti) {
1109 VerifyDexFile("MultiDex");
1110
1111 ASSERT_GT(NumberOfCompiledDexFiles(), 1u);
1112 std::vector<uint8_t> buffer;
1113 verifier_deps_->Encode(dex_files_, &buffer);
1114 ASSERT_FALSE(buffer.empty());
1115
1116 // Create new DexFile, to mess with std::map order: the verifier deps used
1117 // to iterate over the map, which doesn't guarantee insertion order. We fixed
1118 // this by passing the expected order when encoding/decoding.
1119 std::vector<std::unique_ptr<const DexFile>> first_dex_files = OpenTestDexFiles("VerifierDeps");
1120 std::vector<std::unique_ptr<const DexFile>> second_dex_files = OpenTestDexFiles("MultiDex");
1121 std::vector<const DexFile*> dex_files;
1122 for (auto& dex_file : first_dex_files) {
1123 dex_files.push_back(dex_file.get());
1124 }
1125 for (auto& dex_file : second_dex_files) {
1126 dex_files.push_back(dex_file.get());
1127 }
1128
1129 // Dump the new verifier deps to ensure it can properly read the data.
Nicolas Geoffraye70dd562016-10-30 21:03:35 +00001130 VerifierDeps decoded_deps(dex_files, ArrayRef<const uint8_t>(buffer));
Nicolas Geoffrayd01f60c2016-10-28 14:45:48 +01001131 std::ostringstream stream;
1132 VariableIndentationOutputStream os(&stream);
1133 decoded_deps.Dump(&os);
1134}
1135
Nicolas Geoffray08025182016-10-25 17:20:18 +01001136TEST_F(VerifierDepsTest, UnverifiedClasses) {
1137 VerifyDexFile();
1138 ASSERT_FALSE(HasUnverifiedClass("LMyThread;"));
1139 // Test that a class with a soft failure is recorded.
1140 ASSERT_TRUE(HasUnverifiedClass("LMain;"));
1141 // Test that a class with hard failure is recorded.
1142 ASSERT_TRUE(HasUnverifiedClass("LMyVerificationFailure;"));
1143 // Test that a class with unresolved super is recorded.
1144 ASSERT_FALSE(HasUnverifiedClass("LMyClassWithNoSuper;"));
1145 // Test that a class with unresolved super and hard failure is recorded.
1146 ASSERT_TRUE(HasUnverifiedClass("LMyClassWithNoSuperButFailures;"));
1147}
1148
Nicolas Geoffray8904b6f2016-10-28 19:50:34 +01001149// Returns the next resolution kind in the enum.
1150static MethodResolutionKind GetNextResolutionKind(MethodResolutionKind resolution_kind) {
1151 if (resolution_kind == kDirectMethodResolution) {
1152 return kVirtualMethodResolution;
1153 } else if (resolution_kind == kVirtualMethodResolution) {
1154 return kInterfaceMethodResolution;
1155 } else {
1156 DCHECK_EQ(resolution_kind, kInterfaceMethodResolution);
1157 return kDirectMethodResolution;
1158 }
1159}
1160
1161TEST_F(VerifierDepsTest, VerifyDeps) {
1162 VerifyDexFile();
1163
1164 ASSERT_EQ(1u, NumberOfCompiledDexFiles());
1165 ASSERT_TRUE(HasEachKindOfRecord());
1166
1167 // When validating, we create a new class loader, as
1168 // the existing `class_loader_` may contain erroneous classes,
1169 // that ClassLinker::FindClass won't return.
1170
1171 ScopedObjectAccess soa(Thread::Current());
1172 StackHandleScope<1> hs(soa.Self());
1173 MutableHandle<mirror::ClassLoader> new_class_loader(hs.NewHandle<mirror::ClassLoader>(nullptr));
1174 {
1175 new_class_loader.Assign(soa.Decode<mirror::ClassLoader>(LoadDex("VerifierDeps")));
Nicolas Geoffray6bb7f1b2016-11-03 10:52:49 +00001176 ASSERT_TRUE(verifier_deps_->ValidateDependencies(new_class_loader, soa.Self()));
Nicolas Geoffray8904b6f2016-10-28 19:50:34 +01001177 }
1178
1179 std::vector<uint8_t> buffer;
1180 verifier_deps_->Encode(dex_files_, &buffer);
1181 ASSERT_FALSE(buffer.empty());
1182
1183 {
1184 VerifierDeps decoded_deps(dex_files_, ArrayRef<const uint8_t>(buffer));
1185 new_class_loader.Assign(soa.Decode<mirror::ClassLoader>(LoadDex("VerifierDeps")));
Nicolas Geoffray6bb7f1b2016-11-03 10:52:49 +00001186 ASSERT_TRUE(decoded_deps.ValidateDependencies(new_class_loader, soa.Self()));
Nicolas Geoffray8904b6f2016-10-28 19:50:34 +01001187 }
1188
1189 // Fiddle with the dependencies to make sure we catch any change and fail to verify.
1190
1191 {
1192 // Mess up with the assignable_types.
1193 VerifierDeps decoded_deps(dex_files_, ArrayRef<const uint8_t>(buffer));
1194 VerifierDeps::DexFileDeps* deps = decoded_deps.GetDexFileDeps(*primary_dex_file_);
1195 deps->assignable_types_.insert(*deps->unassignable_types_.begin());
1196 new_class_loader.Assign(soa.Decode<mirror::ClassLoader>(LoadDex("VerifierDeps")));
Nicolas Geoffray6bb7f1b2016-11-03 10:52:49 +00001197 ASSERT_FALSE(decoded_deps.ValidateDependencies(new_class_loader, soa.Self()));
Nicolas Geoffray8904b6f2016-10-28 19:50:34 +01001198 }
1199
1200 {
1201 // Mess up with the unassignable_types.
1202 VerifierDeps decoded_deps(dex_files_, ArrayRef<const uint8_t>(buffer));
1203 VerifierDeps::DexFileDeps* deps = decoded_deps.GetDexFileDeps(*primary_dex_file_);
1204 deps->unassignable_types_.insert(*deps->assignable_types_.begin());
1205 new_class_loader.Assign(soa.Decode<mirror::ClassLoader>(LoadDex("VerifierDeps")));
Nicolas Geoffray6bb7f1b2016-11-03 10:52:49 +00001206 ASSERT_FALSE(decoded_deps.ValidateDependencies(new_class_loader, soa.Self()));
Nicolas Geoffray8904b6f2016-10-28 19:50:34 +01001207 }
1208
1209 // Mess up with classes.
1210 {
1211 VerifierDeps decoded_deps(dex_files_, ArrayRef<const uint8_t>(buffer));
1212 VerifierDeps::DexFileDeps* deps = decoded_deps.GetDexFileDeps(*primary_dex_file_);
1213 bool found = false;
1214 for (const auto& entry : deps->classes_) {
1215 if (entry.IsResolved()) {
1216 deps->classes_.insert(VerifierDeps::ClassResolution(
1217 entry.GetDexTypeIndex(), VerifierDeps::kUnresolvedMarker));
1218 found = true;
1219 break;
1220 }
1221 }
1222 ASSERT_TRUE(found);
1223 new_class_loader.Assign(soa.Decode<mirror::ClassLoader>(LoadDex("VerifierDeps")));
Nicolas Geoffray6bb7f1b2016-11-03 10:52:49 +00001224 ASSERT_FALSE(decoded_deps.ValidateDependencies(new_class_loader, soa.Self()));
Nicolas Geoffray8904b6f2016-10-28 19:50:34 +01001225 }
1226
1227 {
1228 VerifierDeps decoded_deps(dex_files_, ArrayRef<const uint8_t>(buffer));
1229 VerifierDeps::DexFileDeps* deps = decoded_deps.GetDexFileDeps(*primary_dex_file_);
1230 bool found = false;
1231 for (const auto& entry : deps->classes_) {
1232 if (!entry.IsResolved()) {
1233 deps->classes_.insert(VerifierDeps::ClassResolution(
1234 entry.GetDexTypeIndex(), VerifierDeps::kUnresolvedMarker - 1));
1235 found = true;
1236 break;
1237 }
1238 }
1239 ASSERT_TRUE(found);
1240 new_class_loader.Assign(soa.Decode<mirror::ClassLoader>(LoadDex("VerifierDeps")));
Nicolas Geoffray6bb7f1b2016-11-03 10:52:49 +00001241 ASSERT_FALSE(decoded_deps.ValidateDependencies(new_class_loader, soa.Self()));
Nicolas Geoffray8904b6f2016-10-28 19:50:34 +01001242 }
1243
1244 {
1245 VerifierDeps decoded_deps(dex_files_, ArrayRef<const uint8_t>(buffer));
1246 VerifierDeps::DexFileDeps* deps = decoded_deps.GetDexFileDeps(*primary_dex_file_);
1247 bool found = false;
1248 for (const auto& entry : deps->classes_) {
1249 if (entry.IsResolved()) {
1250 deps->classes_.insert(VerifierDeps::ClassResolution(
1251 entry.GetDexTypeIndex(), entry.GetAccessFlags() - 1));
1252 found = true;
1253 break;
1254 }
1255 }
1256 ASSERT_TRUE(found);
1257 new_class_loader.Assign(soa.Decode<mirror::ClassLoader>(LoadDex("VerifierDeps")));
Nicolas Geoffray6bb7f1b2016-11-03 10:52:49 +00001258 ASSERT_FALSE(decoded_deps.ValidateDependencies(new_class_loader, soa.Self()));
Nicolas Geoffray8904b6f2016-10-28 19:50:34 +01001259 }
1260
1261 // Mess up with fields.
1262 {
1263 VerifierDeps decoded_deps(dex_files_, ArrayRef<const uint8_t>(buffer));
1264 VerifierDeps::DexFileDeps* deps = decoded_deps.GetDexFileDeps(*primary_dex_file_);
1265 bool found = false;
1266 for (const auto& entry : deps->fields_) {
1267 if (entry.IsResolved()) {
1268 deps->fields_.insert(VerifierDeps::FieldResolution(entry.GetDexFieldIndex(),
1269 VerifierDeps::kUnresolvedMarker,
1270 entry.GetDeclaringClassIndex()));
1271 found = true;
1272 break;
1273 }
1274 }
1275 ASSERT_TRUE(found);
1276 new_class_loader.Assign(soa.Decode<mirror::ClassLoader>(LoadDex("VerifierDeps")));
Nicolas Geoffray6bb7f1b2016-11-03 10:52:49 +00001277 ASSERT_FALSE(decoded_deps.ValidateDependencies(new_class_loader, soa.Self()));
Nicolas Geoffray8904b6f2016-10-28 19:50:34 +01001278 }
1279
1280 {
1281 VerifierDeps decoded_deps(dex_files_, ArrayRef<const uint8_t>(buffer));
1282 VerifierDeps::DexFileDeps* deps = decoded_deps.GetDexFileDeps(*primary_dex_file_);
1283 bool found = false;
1284 for (const auto& entry : deps->fields_) {
1285 if (!entry.IsResolved()) {
Andreas Gampe8a0128a2016-11-28 07:38:35 -08001286 constexpr dex::StringIndex kStringIndexZero(0); // We know there is a class there.
Nicolas Geoffray8904b6f2016-10-28 19:50:34 +01001287 deps->fields_.insert(VerifierDeps::FieldResolution(0 /* we know there is a field there */,
1288 VerifierDeps::kUnresolvedMarker - 1,
Andreas Gampe8a0128a2016-11-28 07:38:35 -08001289 kStringIndexZero));
Nicolas Geoffray8904b6f2016-10-28 19:50:34 +01001290 found = true;
1291 break;
1292 }
1293 }
1294 ASSERT_TRUE(found);
1295 new_class_loader.Assign(soa.Decode<mirror::ClassLoader>(LoadDex("VerifierDeps")));
Nicolas Geoffray6bb7f1b2016-11-03 10:52:49 +00001296 ASSERT_FALSE(decoded_deps.ValidateDependencies(new_class_loader, soa.Self()));
Nicolas Geoffray8904b6f2016-10-28 19:50:34 +01001297 }
1298
1299 {
1300 VerifierDeps decoded_deps(dex_files_, ArrayRef<const uint8_t>(buffer));
1301 VerifierDeps::DexFileDeps* deps = decoded_deps.GetDexFileDeps(*primary_dex_file_);
1302 bool found = false;
1303 for (const auto& entry : deps->fields_) {
1304 if (entry.IsResolved()) {
1305 deps->fields_.insert(VerifierDeps::FieldResolution(entry.GetDexFieldIndex(),
1306 entry.GetAccessFlags() - 1,
1307 entry.GetDeclaringClassIndex()));
1308 found = true;
1309 break;
1310 }
1311 }
1312 ASSERT_TRUE(found);
1313 new_class_loader.Assign(soa.Decode<mirror::ClassLoader>(LoadDex("VerifierDeps")));
Nicolas Geoffray6bb7f1b2016-11-03 10:52:49 +00001314 ASSERT_FALSE(decoded_deps.ValidateDependencies(new_class_loader, soa.Self()));
Nicolas Geoffray8904b6f2016-10-28 19:50:34 +01001315 }
1316
1317 {
1318 VerifierDeps decoded_deps(dex_files_, ArrayRef<const uint8_t>(buffer));
1319 VerifierDeps::DexFileDeps* deps = decoded_deps.GetDexFileDeps(*primary_dex_file_);
1320 bool found = false;
1321 for (const auto& entry : deps->fields_) {
Andreas Gampe8a0128a2016-11-28 07:38:35 -08001322 constexpr dex::StringIndex kNewTypeIndex(0);
Nicolas Geoffray8904b6f2016-10-28 19:50:34 +01001323 if (entry.GetDeclaringClassIndex() != kNewTypeIndex) {
1324 deps->fields_.insert(VerifierDeps::FieldResolution(entry.GetDexFieldIndex(),
1325 entry.GetAccessFlags(),
1326 kNewTypeIndex));
1327 found = true;
1328 break;
1329 }
1330 }
1331 ASSERT_TRUE(found);
1332 new_class_loader.Assign(soa.Decode<mirror::ClassLoader>(LoadDex("VerifierDeps")));
Nicolas Geoffray6bb7f1b2016-11-03 10:52:49 +00001333 ASSERT_FALSE(decoded_deps.ValidateDependencies(new_class_loader, soa.Self()));
Nicolas Geoffray8904b6f2016-10-28 19:50:34 +01001334 }
1335
1336 // Mess up with methods.
1337 for (MethodResolutionKind resolution_kind :
1338 { kDirectMethodResolution, kVirtualMethodResolution, kInterfaceMethodResolution }) {
1339 {
1340 VerifierDeps decoded_deps(dex_files_, ArrayRef<const uint8_t>(buffer));
1341 VerifierDeps::DexFileDeps* deps = decoded_deps.GetDexFileDeps(*primary_dex_file_);
1342 bool found = false;
1343 std::set<VerifierDeps::MethodResolution>* methods = GetMethods(deps, resolution_kind);
1344 for (const auto& entry : *methods) {
1345 if (entry.IsResolved()) {
1346 methods->insert(VerifierDeps::MethodResolution(entry.GetDexMethodIndex(),
1347 VerifierDeps::kUnresolvedMarker,
1348 entry.GetDeclaringClassIndex()));
1349 found = true;
1350 break;
1351 }
1352 }
1353 ASSERT_TRUE(found);
1354 new_class_loader.Assign(soa.Decode<mirror::ClassLoader>(LoadDex("VerifierDeps")));
Nicolas Geoffray6bb7f1b2016-11-03 10:52:49 +00001355 ASSERT_FALSE(decoded_deps.ValidateDependencies(new_class_loader, soa.Self()));
Nicolas Geoffray8904b6f2016-10-28 19:50:34 +01001356 }
1357
1358 {
1359 VerifierDeps decoded_deps(dex_files_, ArrayRef<const uint8_t>(buffer));
1360 VerifierDeps::DexFileDeps* deps = decoded_deps.GetDexFileDeps(*primary_dex_file_);
1361 bool found = false;
1362 std::set<VerifierDeps::MethodResolution>* methods = GetMethods(deps, resolution_kind);
1363 for (const auto& entry : *methods) {
1364 if (!entry.IsResolved()) {
Andreas Gampe8a0128a2016-11-28 07:38:35 -08001365 constexpr dex::StringIndex kStringIndexZero(0); // We know there is a class there.
Nicolas Geoffray8904b6f2016-10-28 19:50:34 +01001366 methods->insert(VerifierDeps::MethodResolution(0 /* we know there is a method there */,
1367 VerifierDeps::kUnresolvedMarker - 1,
Andreas Gampe8a0128a2016-11-28 07:38:35 -08001368 kStringIndexZero));
Nicolas Geoffray8904b6f2016-10-28 19:50:34 +01001369 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()) {
1385 methods->insert(VerifierDeps::MethodResolution(entry.GetDexMethodIndex(),
1386 entry.GetAccessFlags() - 1,
1387 entry.GetDeclaringClassIndex()));
1388 found = true;
1389 break;
1390 }
1391 }
1392 ASSERT_TRUE(found);
1393 new_class_loader.Assign(soa.Decode<mirror::ClassLoader>(LoadDex("VerifierDeps")));
Nicolas Geoffray6bb7f1b2016-11-03 10:52:49 +00001394 ASSERT_FALSE(decoded_deps.ValidateDependencies(new_class_loader, soa.Self()));
Nicolas Geoffray8904b6f2016-10-28 19:50:34 +01001395 }
1396
1397 {
1398 VerifierDeps decoded_deps(dex_files_, ArrayRef<const uint8_t>(buffer));
1399 VerifierDeps::DexFileDeps* deps = decoded_deps.GetDexFileDeps(*primary_dex_file_);
1400 bool found = false;
1401 std::set<VerifierDeps::MethodResolution>* methods = GetMethods(deps, resolution_kind);
1402 for (const auto& entry : *methods) {
Andreas Gampe8a0128a2016-11-28 07:38:35 -08001403 constexpr dex::StringIndex kNewTypeIndex(0);
Nicolas Geoffray8904b6f2016-10-28 19:50:34 +01001404 if (entry.IsResolved() && entry.GetDeclaringClassIndex() != kNewTypeIndex) {
1405 methods->insert(VerifierDeps::MethodResolution(entry.GetDexMethodIndex(),
1406 entry.GetAccessFlags(),
1407 kNewTypeIndex));
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) {
1423 if (entry.IsResolved()) {
1424 GetMethods(deps, GetNextResolutionKind(resolution_kind))->insert(
1425 VerifierDeps::MethodResolution(entry.GetDexMethodIndex(),
1426 entry.GetAccessFlags(),
1427 entry.GetDeclaringClassIndex()));
1428 found = true;
1429 }
1430 }
1431 ASSERT_TRUE(found);
1432 new_class_loader.Assign(soa.Decode<mirror::ClassLoader>(LoadDex("VerifierDeps")));
Nicolas Geoffray6bb7f1b2016-11-03 10:52:49 +00001433 ASSERT_FALSE(decoded_deps.ValidateDependencies(new_class_loader, soa.Self()));
Nicolas Geoffray8904b6f2016-10-28 19:50:34 +01001434 }
1435
1436 {
1437 VerifierDeps decoded_deps(dex_files_, ArrayRef<const uint8_t>(buffer));
1438 VerifierDeps::DexFileDeps* deps = decoded_deps.GetDexFileDeps(*primary_dex_file_);
1439 bool found = false;
1440 std::set<VerifierDeps::MethodResolution>* methods = GetMethods(deps, resolution_kind);
1441 for (const auto& entry : *methods) {
1442 if (entry.IsResolved()) {
1443 GetMethods(deps, GetNextResolutionKind(GetNextResolutionKind(resolution_kind)))->insert(
1444 VerifierDeps::MethodResolution(entry.GetDexMethodIndex(),
1445 entry.GetAccessFlags(),
1446 entry.GetDeclaringClassIndex()));
1447 found = true;
1448 }
1449 }
1450 ASSERT_TRUE(found);
1451 new_class_loader.Assign(soa.Decode<mirror::ClassLoader>(LoadDex("VerifierDeps")));
Nicolas Geoffray6bb7f1b2016-11-03 10:52:49 +00001452 ASSERT_FALSE(decoded_deps.ValidateDependencies(new_class_loader, soa.Self()));
1453 }
1454 }
1455}
1456
1457TEST_F(VerifierDepsTest, CompilerDriver) {
1458 SetupCompilerDriver();
1459
1460 // Test both multi-dex and single-dex configuration.
1461 for (const char* multi : { "MultiDex", static_cast<const char*>(nullptr) }) {
1462 // Test that the compiler driver behaves as expected when the dependencies
1463 // verify and when they don't verify.
1464 for (bool verify_failure : { false, true }) {
1465 {
1466 ScopedObjectAccess soa(Thread::Current());
1467 LoadDexFile(&soa, "VerifierDeps", multi);
1468 }
1469 VerifyWithCompilerDriver(/* verifier_deps */ nullptr);
1470
1471 std::vector<uint8_t> buffer;
1472 verifier_deps_->Encode(dex_files_, &buffer);
1473
1474 {
1475 ScopedObjectAccess soa(Thread::Current());
1476 LoadDexFile(&soa, "VerifierDeps", multi);
1477 }
1478 verifier::VerifierDeps decoded_deps(dex_files_, ArrayRef<const uint8_t>(buffer));
1479 if (verify_failure) {
1480 // Just taint the decoded VerifierDeps with one invalid entry.
1481 VerifierDeps::DexFileDeps* deps = decoded_deps.GetDexFileDeps(*primary_dex_file_);
1482 bool found = false;
1483 for (const auto& entry : deps->classes_) {
1484 if (entry.IsResolved()) {
1485 deps->classes_.insert(VerifierDeps::ClassResolution(
1486 entry.GetDexTypeIndex(), VerifierDeps::kUnresolvedMarker));
1487 found = true;
1488 break;
1489 }
1490 }
1491 ASSERT_TRUE(found);
1492 }
1493 VerifyWithCompilerDriver(&decoded_deps);
1494
1495 if (verify_failure) {
1496 ASSERT_FALSE(verifier_deps_ == nullptr);
1497 ASSERT_FALSE(verifier_deps_->Equals(decoded_deps));
1498 } else {
1499 ASSERT_TRUE(verifier_deps_ == nullptr);
1500 VerifyClassStatus(decoded_deps);
1501 }
Nicolas Geoffray8904b6f2016-10-28 19:50:34 +01001502 }
1503 }
1504}
1505
David Brazdilca3c8c32016-09-06 14:04:48 +01001506} // namespace verifier
1507} // namespace art