blob: 0b1ab758b20680d0c1ab2509221d58dcf7daa8b7 [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
Andreas Gampec6ea7d02017-02-01 16:46:28 -080020#include "art_method-inl.h"
David Brazdilca3c8c32016-09-06 14:04:48 +010021#include "class_linker.h"
Andreas Gamped9911ee2017-03-27 13:27:24 -070022#include "common_compiler_test.h"
David Brazdilca3c8c32016-09-06 14:04:48 +010023#include "compiler_callbacks.h"
Andreas Gamped9911ee2017-03-27 13:27:24 -070024#include "dex/verification_results.h"
25#include "dex/verified_method.h"
David Brazdilca3c8c32016-09-06 14:04:48 +010026#include "dex_file.h"
Andreas Gampea5b09a62016-11-17 15:21:22 -080027#include "dex_file_types.h"
Andreas Gamped9911ee2017-03-27 13:27:24 -070028#include "driver/compiler_options.h"
29#include "driver/compiler_driver.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"
Andreas Gamped9911ee2017-03-27 13:27:24 -070036#include "utils/atomic_method_ref_map-inl.h"
David Brazdilca3c8c32016-09-06 14:04:48 +010037
38namespace art {
39namespace verifier {
40
41class VerifierDepsCompilerCallbacks : public CompilerCallbacks {
42 public:
43 explicit VerifierDepsCompilerCallbacks()
44 : CompilerCallbacks(CompilerCallbacks::CallbackMode::kCompileApp),
45 deps_(nullptr) {}
46
47 void MethodVerified(verifier::MethodVerifier* verifier ATTRIBUTE_UNUSED) OVERRIDE {}
48 void ClassRejected(ClassReference ref ATTRIBUTE_UNUSED) OVERRIDE {}
49 bool IsRelocationPossible() OVERRIDE { return false; }
50
51 verifier::VerifierDeps* GetVerifierDeps() const OVERRIDE { return deps_; }
52 void SetVerifierDeps(verifier::VerifierDeps* deps) { deps_ = deps; }
53
54 private:
55 verifier::VerifierDeps* deps_;
56};
57
Nicolas Geoffray08025182016-10-25 17:20:18 +010058class VerifierDepsTest : public CommonCompilerTest {
David Brazdilca3c8c32016-09-06 14:04:48 +010059 public:
60 void SetUpRuntimeOptions(RuntimeOptions* options) {
Nicolas Geoffray08025182016-10-25 17:20:18 +010061 CommonCompilerTest::SetUpRuntimeOptions(options);
David Brazdilca3c8c32016-09-06 14:04:48 +010062 callbacks_.reset(new VerifierDepsCompilerCallbacks());
63 }
64
65 mirror::Class* FindClassByName(const std::string& name, ScopedObjectAccess* soa)
66 REQUIRES_SHARED(Locks::mutator_lock_) {
67 StackHandleScope<1> hs(Thread::Current());
68 Handle<mirror::ClassLoader> class_loader_handle(
Mathieu Chartier0795f232016-09-27 18:43:30 -070069 hs.NewHandle(soa->Decode<mirror::ClassLoader>(class_loader_)));
David Brazdil6f82fbd2016-09-14 11:55:26 +010070 mirror::Class* klass = class_linker_->FindClass(Thread::Current(),
71 name.c_str(),
72 class_loader_handle);
73 if (klass == nullptr) {
74 DCHECK(Thread::Current()->IsExceptionPending());
75 Thread::Current()->ClearException();
76 }
77 return klass;
78 }
79
Nicolas Geoffray6bb7f1b2016-11-03 10:52:49 +000080 void SetupCompilerDriver() {
81 compiler_options_->boot_image_ = false;
82 compiler_driver_->InitializeThreadPools();
83 }
84
85 void VerifyWithCompilerDriver(verifier::VerifierDeps* deps) {
86 TimingLogger timings("Verify", false, false);
87 // The compiler driver handles the verifier deps in the callbacks, so
88 // remove what this class did for unit testing.
89 verifier_deps_.reset(nullptr);
Nicolas Geoffrayb0bbe8e2016-11-19 10:42:37 +000090 callbacks_->SetVerifierDeps(deps);
91 compiler_driver_->Verify(class_loader_, dex_files_, &timings);
Nicolas Geoffray6bb7f1b2016-11-03 10:52:49 +000092 // The compiler driver may have updated the VerifierDeps in the callback object.
Nicolas Geoffrayb0bbe8e2016-11-19 10:42:37 +000093 if (callbacks_->GetVerifierDeps() != deps) {
94 verifier_deps_.reset(callbacks_->GetVerifierDeps());
95 }
96 callbacks_->SetVerifierDeps(nullptr);
Nicolas Geoffray1d0ae3f2016-12-06 13:40:16 +000097 // Clear entries in the verification results to avoid hitting a DCHECK that
98 // we always succeed inserting a new entry after verifying.
99 AtomicMethodRefMap<const VerifiedMethod*>* map =
100 &compiler_driver_->GetVerificationResults()->atomic_verified_methods_;
101 map->Visit([](const MethodReference& ref ATTRIBUTE_UNUSED, const VerifiedMethod* method) {
102 delete method;
103 });
104 map->ClearEntries();
Nicolas Geoffray6bb7f1b2016-11-03 10:52:49 +0000105 }
106
David Brazdil6f82fbd2016-09-14 11:55:26 +0100107 void SetVerifierDeps(const std::vector<const DexFile*>& dex_files) {
108 verifier_deps_.reset(new verifier::VerifierDeps(dex_files));
109 VerifierDepsCompilerCallbacks* callbacks =
110 reinterpret_cast<VerifierDepsCompilerCallbacks*>(callbacks_.get());
111 callbacks->SetVerifierDeps(verifier_deps_.get());
David Brazdilca3c8c32016-09-06 14:04:48 +0100112 }
113
Nicolas Geoffrayd01f60c2016-10-28 14:45:48 +0100114 void LoadDexFile(ScopedObjectAccess* soa, const char* name1, const char* name2 = nullptr)
115 REQUIRES_SHARED(Locks::mutator_lock_) {
116 class_loader_ = (name2 == nullptr) ? LoadDex(name1) : LoadMultiDex(name1, name2);
117 dex_files_ = GetDexFiles(class_loader_);
118 primary_dex_file_ = dex_files_.front();
119
120 SetVerifierDeps(dex_files_);
121 StackHandleScope<1> hs(soa->Self());
122 Handle<mirror::ClassLoader> loader =
123 hs.NewHandle(soa->Decode<mirror::ClassLoader>(class_loader_));
124 for (const DexFile* dex_file : dex_files_) {
125 class_linker_->RegisterDexFile(*dex_file, loader.Get());
126 }
Nicolas Geoffray1d0ae3f2016-12-06 13:40:16 +0000127 for (const DexFile* dex_file : dex_files_) {
128 compiler_driver_->GetVerificationResults()->AddDexFile(dex_file);
129 }
Nicolas Geoffrayd01f60c2016-10-28 14:45:48 +0100130 }
131
David Brazdilca3c8c32016-09-06 14:04:48 +0100132 void LoadDexFile(ScopedObjectAccess* soa) REQUIRES_SHARED(Locks::mutator_lock_) {
Nicolas Geoffrayd01f60c2016-10-28 14:45:48 +0100133 LoadDexFile(soa, "VerifierDeps");
134 CHECK_EQ(dex_files_.size(), 1u);
David Brazdilca3c8c32016-09-06 14:04:48 +0100135 klass_Main_ = FindClassByName("LMain;", soa);
136 CHECK(klass_Main_ != nullptr);
David Brazdilca3c8c32016-09-06 14:04:48 +0100137 }
138
139 bool VerifyMethod(const std::string& method_name) {
140 ScopedObjectAccess soa(Thread::Current());
141 LoadDexFile(&soa);
142
Nicolas Geoffrayd01f60c2016-10-28 14:45:48 +0100143 StackHandleScope<2> hs(soa.Self());
David Brazdilca3c8c32016-09-06 14:04:48 +0100144 Handle<mirror::ClassLoader> class_loader_handle(
Mathieu Chartier0795f232016-09-27 18:43:30 -0700145 hs.NewHandle(soa.Decode<mirror::ClassLoader>(class_loader_)));
David Brazdilca3c8c32016-09-06 14:04:48 +0100146 Handle<mirror::DexCache> dex_cache_handle(hs.NewHandle(klass_Main_->GetDexCache()));
147
148 const DexFile::ClassDef* class_def = klass_Main_->GetClassDef();
Nicolas Geoffrayd01f60c2016-10-28 14:45:48 +0100149 const uint8_t* class_data = primary_dex_file_->GetClassData(*class_def);
David Brazdilca3c8c32016-09-06 14:04:48 +0100150 CHECK(class_data != nullptr);
151
Nicolas Geoffrayd01f60c2016-10-28 14:45:48 +0100152 ClassDataItemIterator it(*primary_dex_file_, class_data);
David Brazdilca3c8c32016-09-06 14:04:48 +0100153 while (it.HasNextStaticField() || it.HasNextInstanceField()) {
154 it.Next();
155 }
156
157 ArtMethod* method = nullptr;
158 while (it.HasNextDirectMethod()) {
159 ArtMethod* resolved_method = class_linker_->ResolveMethod<ClassLinker::kNoICCECheckForCache>(
Nicolas Geoffrayd01f60c2016-10-28 14:45:48 +0100160 *primary_dex_file_,
David Brazdilca3c8c32016-09-06 14:04:48 +0100161 it.GetMemberIndex(),
162 dex_cache_handle,
163 class_loader_handle,
164 nullptr,
165 it.GetMethodInvokeType(*class_def));
166 CHECK(resolved_method != nullptr);
167 if (method_name == resolved_method->GetName()) {
168 method = resolved_method;
169 break;
170 }
171 it.Next();
172 }
173 CHECK(method != nullptr);
174
Nicolas Geoffray340dafa2016-11-18 16:03:10 +0000175 Thread::Current()->SetVerifierDeps(callbacks_->GetVerifierDeps());
David Brazdilca3c8c32016-09-06 14:04:48 +0100176 MethodVerifier verifier(Thread::Current(),
Nicolas Geoffrayd01f60c2016-10-28 14:45:48 +0100177 primary_dex_file_,
David Brazdilca3c8c32016-09-06 14:04:48 +0100178 dex_cache_handle,
179 class_loader_handle,
180 *class_def,
181 it.GetMethodCodeItem(),
182 it.GetMemberIndex(),
183 method,
184 it.GetMethodAccessFlags(),
185 true /* can_load_classes */,
186 true /* allow_soft_failures */,
187 true /* need_precise_constants */,
188 false /* verify to dump */,
189 true /* allow_thread_suspension */);
190 verifier.Verify();
Nicolas Geoffray340dafa2016-11-18 16:03:10 +0000191 Thread::Current()->SetVerifierDeps(nullptr);
David Brazdilca3c8c32016-09-06 14:04:48 +0100192 return !verifier.HasFailures();
193 }
194
Nicolas Geoffrayd01f60c2016-10-28 14:45:48 +0100195 void VerifyDexFile(const char* multidex = nullptr) {
Nicolas Geoffray08025182016-10-25 17:20:18 +0100196 {
197 ScopedObjectAccess soa(Thread::Current());
Nicolas Geoffrayd01f60c2016-10-28 14:45:48 +0100198 LoadDexFile(&soa, "VerifierDeps", multidex);
David Brazdil6f82fbd2016-09-14 11:55:26 +0100199 }
Nicolas Geoffray6bb7f1b2016-11-03 10:52:49 +0000200 SetupCompilerDriver();
201 VerifyWithCompilerDriver(/* verifier_deps */ nullptr);
David Brazdil6f82fbd2016-09-14 11:55:26 +0100202 }
203
David Brazdilca3c8c32016-09-06 14:04:48 +0100204 bool TestAssignabilityRecording(const std::string& dst,
205 const std::string& src,
206 bool is_strict,
207 bool is_assignable) {
208 ScopedObjectAccess soa(Thread::Current());
209 LoadDexFile(&soa);
David Brazdil6f82fbd2016-09-14 11:55:26 +0100210 mirror::Class* klass_dst = FindClassByName(dst, &soa);
Nicolas Geoffraybdb540d2017-04-19 13:50:34 +0100211 DCHECK(klass_dst != nullptr) << dst;
David Brazdil6f82fbd2016-09-14 11:55:26 +0100212 mirror::Class* klass_src = FindClassByName(src, &soa);
Nicolas Geoffraybdb540d2017-04-19 13:50:34 +0100213 DCHECK(klass_src != nullptr) << src;
Nicolas Geoffrayd01f60c2016-10-28 14:45:48 +0100214 verifier_deps_->AddAssignability(*primary_dex_file_,
David Brazdil6f82fbd2016-09-14 11:55:26 +0100215 klass_dst,
216 klass_src,
David Brazdilca3c8c32016-09-06 14:04:48 +0100217 is_strict,
218 is_assignable);
219 return true;
220 }
221
Nicolas Geoffray6bb7f1b2016-11-03 10:52:49 +0000222 // Check that the status of classes in `class_loader_` match the
223 // expected status in `deps`.
224 void VerifyClassStatus(const verifier::VerifierDeps& deps) {
225 ScopedObjectAccess soa(Thread::Current());
226 StackHandleScope<2> hs(soa.Self());
227 Handle<mirror::ClassLoader> class_loader_handle(
228 hs.NewHandle(soa.Decode<mirror::ClassLoader>(class_loader_)));
229 MutableHandle<mirror::Class> cls(hs.NewHandle<mirror::Class>(nullptr));
230 for (const DexFile* dex_file : dex_files_) {
Andreas Gampea5b09a62016-11-17 15:21:22 -0800231 const std::vector<dex::TypeIndex>& unverified_classes = deps.GetUnverifiedClasses(*dex_file);
232 std::set<dex::TypeIndex> set(unverified_classes.begin(), unverified_classes.end());
Nicolas Geoffray6bb7f1b2016-11-03 10:52:49 +0000233 for (uint32_t i = 0; i < dex_file->NumClassDefs(); ++i) {
234 const DexFile::ClassDef& class_def = dex_file->GetClassDef(i);
235 const char* descriptor = dex_file->GetClassDescriptor(class_def);
236 cls.Assign(class_linker_->FindClass(soa.Self(), descriptor, class_loader_handle));
Andreas Gampefa4333d2017-02-14 11:10:34 -0800237 if (cls == nullptr) {
Nicolas Geoffray6bb7f1b2016-11-03 10:52:49 +0000238 CHECK(soa.Self()->IsExceptionPending());
239 soa.Self()->ClearException();
240 } else if (set.find(class_def.class_idx_) == set.end()) {
241 ASSERT_EQ(cls->GetStatus(), mirror::Class::kStatusVerified);
242 } else {
243 ASSERT_LT(cls->GetStatus(), mirror::Class::kStatusVerified);
244 }
245 }
246 }
247 }
248
Nicolas Geoffray08025182016-10-25 17:20:18 +0100249 bool HasUnverifiedClass(const std::string& cls) {
Nicolas Geoffray7cc3ae52017-03-07 14:33:37 +0000250 return HasUnverifiedClass(cls, *primary_dex_file_);
251 }
252
253 bool HasUnverifiedClass(const std::string& cls, const DexFile& dex_file) {
254 const DexFile::TypeId* type_id = dex_file.FindTypeId(cls.c_str());
Nicolas Geoffray08025182016-10-25 17:20:18 +0100255 DCHECK(type_id != nullptr);
Nicolas Geoffray7cc3ae52017-03-07 14:33:37 +0000256 dex::TypeIndex index = dex_file.GetIndexForTypeId(*type_id);
Nicolas Geoffray08025182016-10-25 17:20:18 +0100257 for (const auto& dex_dep : verifier_deps_->dex_deps_) {
Andreas Gampea5b09a62016-11-17 15:21:22 -0800258 for (dex::TypeIndex entry : dex_dep.second->unverified_classes_) {
Nicolas Geoffray08025182016-10-25 17:20:18 +0100259 if (index == entry) {
260 return true;
261 }
262 }
263 }
264 return false;
265 }
266
David Brazdilca3c8c32016-09-06 14:04:48 +0100267 // Iterates over all assignability records and tries to find an entry which
268 // matches the expected destination/source pair.
269 bool HasAssignable(const std::string& expected_destination,
270 const std::string& expected_source,
271 bool expected_is_assignable) {
David Brazdilca3c8c32016-09-06 14:04:48 +0100272 for (auto& dex_dep : verifier_deps_->dex_deps_) {
273 const DexFile& dex_file = *dex_dep.first;
274 auto& storage = expected_is_assignable ? dex_dep.second->assignable_types_
275 : dex_dep.second->unassignable_types_;
276 for (auto& entry : storage) {
277 std::string actual_destination =
278 verifier_deps_->GetStringFromId(dex_file, entry.GetDestination());
279 std::string actual_source = verifier_deps_->GetStringFromId(dex_file, entry.GetSource());
280 if ((expected_destination == actual_destination) && (expected_source == actual_source)) {
281 return true;
282 }
283 }
284 }
285 return false;
286 }
287
288 // Iterates over all class resolution records, finds an entry which matches
289 // the given class descriptor and tests its properties.
290 bool HasClass(const std::string& expected_klass,
291 bool expected_resolved,
292 const std::string& expected_access_flags = "") {
David Brazdilca3c8c32016-09-06 14:04:48 +0100293 for (auto& dex_dep : verifier_deps_->dex_deps_) {
294 for (auto& entry : dex_dep.second->classes_) {
295 if (expected_resolved != entry.IsResolved()) {
296 continue;
297 }
298
299 std::string actual_klass = dex_dep.first->StringByTypeIdx(entry.GetDexTypeIndex());
300 if (expected_klass != actual_klass) {
301 continue;
302 }
303
304 if (expected_resolved) {
305 // Test access flags. Note that PrettyJavaAccessFlags always appends
306 // a space after the modifiers. Add it to the expected access flags.
307 std::string actual_access_flags = PrettyJavaAccessFlags(entry.GetAccessFlags());
308 if (expected_access_flags + " " != actual_access_flags) {
309 continue;
310 }
311 }
312
313 return true;
314 }
315 }
316 return false;
317 }
318
319 // Iterates over all field resolution records, finds an entry which matches
320 // the given field class+name+type and tests its properties.
321 bool HasField(const std::string& expected_klass,
322 const std::string& expected_name,
323 const std::string& expected_type,
324 bool expected_resolved,
325 const std::string& expected_access_flags = "",
326 const std::string& expected_decl_klass = "") {
David Brazdilca3c8c32016-09-06 14:04:48 +0100327 for (auto& dex_dep : verifier_deps_->dex_deps_) {
328 for (auto& entry : dex_dep.second->fields_) {
329 if (expected_resolved != entry.IsResolved()) {
330 continue;
331 }
332
333 const DexFile::FieldId& field_id = dex_dep.first->GetFieldId(entry.GetDexFieldIndex());
334
335 std::string actual_klass = dex_dep.first->StringByTypeIdx(field_id.class_idx_);
336 if (expected_klass != actual_klass) {
337 continue;
338 }
339
340 std::string actual_name = dex_dep.first->StringDataByIdx(field_id.name_idx_);
341 if (expected_name != actual_name) {
342 continue;
343 }
344
345 std::string actual_type = dex_dep.first->StringByTypeIdx(field_id.type_idx_);
346 if (expected_type != actual_type) {
347 continue;
348 }
349
350 if (expected_resolved) {
351 // Test access flags. Note that PrettyJavaAccessFlags always appends
352 // a space after the modifiers. Add it to the expected access flags.
353 std::string actual_access_flags = PrettyJavaAccessFlags(entry.GetAccessFlags());
354 if (expected_access_flags + " " != actual_access_flags) {
355 continue;
356 }
357
358 std::string actual_decl_klass = verifier_deps_->GetStringFromId(
359 *dex_dep.first, entry.GetDeclaringClassIndex());
360 if (expected_decl_klass != actual_decl_klass) {
361 continue;
362 }
363 }
364
365 return true;
366 }
367 }
368 return false;
369 }
370
371 // Iterates over all method resolution records, finds an entry which matches
372 // the given field kind+class+name+signature and tests its properties.
373 bool HasMethod(const std::string& expected_kind,
374 const std::string& expected_klass,
375 const std::string& expected_name,
376 const std::string& expected_signature,
377 bool expected_resolved,
378 const std::string& expected_access_flags = "",
379 const std::string& expected_decl_klass = "") {
David Brazdilca3c8c32016-09-06 14:04:48 +0100380 for (auto& dex_dep : verifier_deps_->dex_deps_) {
381 auto& storage = (expected_kind == "direct") ? dex_dep.second->direct_methods_
382 : (expected_kind == "virtual") ? dex_dep.second->virtual_methods_
383 : dex_dep.second->interface_methods_;
384 for (auto& entry : storage) {
385 if (expected_resolved != entry.IsResolved()) {
386 continue;
387 }
388
389 const DexFile::MethodId& method_id = dex_dep.first->GetMethodId(entry.GetDexMethodIndex());
390
391 std::string actual_klass = dex_dep.first->StringByTypeIdx(method_id.class_idx_);
392 if (expected_klass != actual_klass) {
393 continue;
394 }
395
396 std::string actual_name = dex_dep.first->StringDataByIdx(method_id.name_idx_);
397 if (expected_name != actual_name) {
398 continue;
399 }
400
401 std::string actual_signature = dex_dep.first->GetMethodSignature(method_id).ToString();
402 if (expected_signature != actual_signature) {
403 continue;
404 }
405
406 if (expected_resolved) {
407 // Test access flags. Note that PrettyJavaAccessFlags always appends
408 // a space after the modifiers. Add it to the expected access flags.
409 std::string actual_access_flags = PrettyJavaAccessFlags(entry.GetAccessFlags());
410 if (expected_access_flags + " " != actual_access_flags) {
411 continue;
412 }
413
414 std::string actual_decl_klass = verifier_deps_->GetStringFromId(
415 *dex_dep.first, entry.GetDeclaringClassIndex());
416 if (expected_decl_klass != actual_decl_klass) {
417 continue;
418 }
419 }
420
421 return true;
422 }
423 }
424 return false;
425 }
426
David Brazdil6f82fbd2016-09-14 11:55:26 +0100427 size_t NumberOfCompiledDexFiles() {
David Brazdil6f82fbd2016-09-14 11:55:26 +0100428 return verifier_deps_->dex_deps_.size();
429 }
430
431 size_t HasEachKindOfRecord() {
David Brazdil6f82fbd2016-09-14 11:55:26 +0100432 bool has_strings = false;
433 bool has_assignability = false;
434 bool has_classes = false;
435 bool has_fields = false;
436 bool has_methods = false;
Nicolas Geoffray08025182016-10-25 17:20:18 +0100437 bool has_unverified_classes = false;
David Brazdil6f82fbd2016-09-14 11:55:26 +0100438
439 for (auto& entry : verifier_deps_->dex_deps_) {
440 has_strings |= !entry.second->strings_.empty();
441 has_assignability |= !entry.second->assignable_types_.empty();
442 has_assignability |= !entry.second->unassignable_types_.empty();
443 has_classes |= !entry.second->classes_.empty();
444 has_fields |= !entry.second->fields_.empty();
445 has_methods |= !entry.second->direct_methods_.empty();
446 has_methods |= !entry.second->virtual_methods_.empty();
447 has_methods |= !entry.second->interface_methods_.empty();
Nicolas Geoffray08025182016-10-25 17:20:18 +0100448 has_unverified_classes |= !entry.second->unverified_classes_.empty();
David Brazdil6f82fbd2016-09-14 11:55:26 +0100449 }
450
Nicolas Geoffray08025182016-10-25 17:20:18 +0100451 return has_strings &&
452 has_assignability &&
453 has_classes &&
454 has_fields &&
455 has_methods &&
456 has_unverified_classes;
David Brazdil6f82fbd2016-09-14 11:55:26 +0100457 }
458
Nicolas Geoffray8904b6f2016-10-28 19:50:34 +0100459 static std::set<VerifierDeps::MethodResolution>* GetMethods(
460 VerifierDeps::DexFileDeps* deps, MethodResolutionKind resolution_kind) {
461 if (resolution_kind == kDirectMethodResolution) {
462 return &deps->direct_methods_;
463 } else if (resolution_kind == kVirtualMethodResolution) {
464 return &deps->virtual_methods_;
465 } else {
466 DCHECK_EQ(resolution_kind, kInterfaceMethodResolution);
467 return &deps->interface_methods_;
468 }
469 }
470
David Brazdilca3c8c32016-09-06 14:04:48 +0100471 std::unique_ptr<verifier::VerifierDeps> verifier_deps_;
Nicolas Geoffrayd01f60c2016-10-28 14:45:48 +0100472 std::vector<const DexFile*> dex_files_;
473 const DexFile* primary_dex_file_;
David Brazdilca3c8c32016-09-06 14:04:48 +0100474 jobject class_loader_;
475 mirror::Class* klass_Main_;
476};
477
478TEST_F(VerifierDepsTest, StringToId) {
479 ScopedObjectAccess soa(Thread::Current());
480 LoadDexFile(&soa);
481
Andreas Gampe8a0128a2016-11-28 07:38:35 -0800482 dex::StringIndex id_Main1 = verifier_deps_->GetIdFromString(*primary_dex_file_, "LMain;");
483 ASSERT_LT(id_Main1.index_, primary_dex_file_->NumStringIds());
Nicolas Geoffrayd01f60c2016-10-28 14:45:48 +0100484 ASSERT_EQ("LMain;", verifier_deps_->GetStringFromId(*primary_dex_file_, id_Main1));
David Brazdilca3c8c32016-09-06 14:04:48 +0100485
Andreas Gampe8a0128a2016-11-28 07:38:35 -0800486 dex::StringIndex id_Main2 = verifier_deps_->GetIdFromString(*primary_dex_file_, "LMain;");
487 ASSERT_LT(id_Main2.index_, primary_dex_file_->NumStringIds());
Nicolas Geoffrayd01f60c2016-10-28 14:45:48 +0100488 ASSERT_EQ("LMain;", verifier_deps_->GetStringFromId(*primary_dex_file_, id_Main2));
David Brazdilca3c8c32016-09-06 14:04:48 +0100489
Andreas Gampe8a0128a2016-11-28 07:38:35 -0800490 dex::StringIndex id_Lorem1 = verifier_deps_->GetIdFromString(*primary_dex_file_, "Lorem ipsum");
491 ASSERT_GE(id_Lorem1.index_, primary_dex_file_->NumStringIds());
Nicolas Geoffrayd01f60c2016-10-28 14:45:48 +0100492 ASSERT_EQ("Lorem ipsum", verifier_deps_->GetStringFromId(*primary_dex_file_, id_Lorem1));
David Brazdilca3c8c32016-09-06 14:04:48 +0100493
Andreas Gampe8a0128a2016-11-28 07:38:35 -0800494 dex::StringIndex id_Lorem2 = verifier_deps_->GetIdFromString(*primary_dex_file_, "Lorem ipsum");
495 ASSERT_GE(id_Lorem2.index_, primary_dex_file_->NumStringIds());
Nicolas Geoffrayd01f60c2016-10-28 14:45:48 +0100496 ASSERT_EQ("Lorem ipsum", verifier_deps_->GetStringFromId(*primary_dex_file_, id_Lorem2));
David Brazdilca3c8c32016-09-06 14:04:48 +0100497
498 ASSERT_EQ(id_Main1, id_Main2);
499 ASSERT_EQ(id_Lorem1, id_Lorem2);
500 ASSERT_NE(id_Main1, id_Lorem1);
501}
502
503TEST_F(VerifierDepsTest, Assignable_BothInBoot) {
504 ASSERT_TRUE(TestAssignabilityRecording(/* dst */ "Ljava/util/TimeZone;",
505 /* src */ "Ljava/util/SimpleTimeZone;",
506 /* is_strict */ true,
507 /* is_assignable */ true));
508 ASSERT_TRUE(HasAssignable("Ljava/util/TimeZone;", "Ljava/util/SimpleTimeZone;", true));
509}
510
511TEST_F(VerifierDepsTest, Assignable_DestinationInBoot1) {
512 ASSERT_TRUE(TestAssignabilityRecording(/* dst */ "Ljava/net/Socket;",
513 /* src */ "LMySSLSocket;",
514 /* is_strict */ true,
515 /* is_assignable */ true));
Nicolas Geoffray119e8462016-12-21 10:29:43 +0000516 ASSERT_TRUE(HasAssignable("Ljava/net/Socket;", "Ljavax/net/ssl/SSLSocket;", true));
David Brazdilca3c8c32016-09-06 14:04:48 +0100517}
518
519TEST_F(VerifierDepsTest, Assignable_DestinationInBoot2) {
520 ASSERT_TRUE(TestAssignabilityRecording(/* dst */ "Ljava/util/TimeZone;",
521 /* src */ "LMySimpleTimeZone;",
522 /* is_strict */ true,
523 /* is_assignable */ true));
Nicolas Geoffray119e8462016-12-21 10:29:43 +0000524 ASSERT_TRUE(HasAssignable("Ljava/util/TimeZone;", "Ljava/util/SimpleTimeZone;", true));
David Brazdilca3c8c32016-09-06 14:04:48 +0100525}
526
527TEST_F(VerifierDepsTest, Assignable_DestinationInBoot3) {
528 ASSERT_TRUE(TestAssignabilityRecording(/* dst */ "Ljava/util/Collection;",
529 /* src */ "LMyThreadSet;",
530 /* is_strict */ true,
531 /* is_assignable */ true));
Nicolas Geoffray0e2fe0f2016-12-21 16:54:52 +0000532 ASSERT_TRUE(HasAssignable("Ljava/util/Collection;", "Ljava/util/Set;", true));
David Brazdilca3c8c32016-09-06 14:04:48 +0100533}
534
535TEST_F(VerifierDepsTest, Assignable_BothArrays_Resolved) {
536 ASSERT_TRUE(TestAssignabilityRecording(/* dst */ "[[Ljava/util/TimeZone;",
537 /* src */ "[[Ljava/util/SimpleTimeZone;",
538 /* is_strict */ true,
539 /* is_assignable */ true));
540 // If the component types of both arrays are resolved, we optimize the list of
541 // dependencies by recording a dependency on the component types.
542 ASSERT_FALSE(HasAssignable("[[Ljava/util/TimeZone;", "[[Ljava/util/SimpleTimeZone;", true));
543 ASSERT_FALSE(HasAssignable("[Ljava/util/TimeZone;", "[Ljava/util/SimpleTimeZone;", true));
544 ASSERT_TRUE(HasAssignable("Ljava/util/TimeZone;", "Ljava/util/SimpleTimeZone;", true));
545}
546
David Brazdilca3c8c32016-09-06 14:04:48 +0100547TEST_F(VerifierDepsTest, NotAssignable_BothInBoot) {
548 ASSERT_TRUE(TestAssignabilityRecording(/* dst */ "Ljava/lang/Exception;",
549 /* src */ "Ljava/util/SimpleTimeZone;",
550 /* is_strict */ true,
551 /* is_assignable */ false));
552 ASSERT_TRUE(HasAssignable("Ljava/lang/Exception;", "Ljava/util/SimpleTimeZone;", false));
553}
554
555TEST_F(VerifierDepsTest, NotAssignable_DestinationInBoot1) {
556 ASSERT_TRUE(TestAssignabilityRecording(/* dst */ "Ljava/lang/Exception;",
557 /* src */ "LMySSLSocket;",
558 /* is_strict */ true,
559 /* is_assignable */ false));
Nicolas Geoffray119e8462016-12-21 10:29:43 +0000560 ASSERT_TRUE(HasAssignable("Ljava/lang/Exception;", "Ljavax/net/ssl/SSLSocket;", false));
David Brazdilca3c8c32016-09-06 14:04:48 +0100561}
562
563TEST_F(VerifierDepsTest, NotAssignable_DestinationInBoot2) {
564 ASSERT_TRUE(TestAssignabilityRecording(/* dst */ "Ljava/lang/Exception;",
565 /* src */ "LMySimpleTimeZone;",
566 /* is_strict */ true,
567 /* is_assignable */ false));
Nicolas Geoffray119e8462016-12-21 10:29:43 +0000568 ASSERT_TRUE(HasAssignable("Ljava/lang/Exception;", "Ljava/util/SimpleTimeZone;", false));
David Brazdilca3c8c32016-09-06 14:04:48 +0100569}
570
571TEST_F(VerifierDepsTest, NotAssignable_BothArrays) {
572 ASSERT_TRUE(TestAssignabilityRecording(/* dst */ "[Ljava/lang/Exception;",
573 /* src */ "[Ljava/util/SimpleTimeZone;",
574 /* is_strict */ true,
575 /* is_assignable */ false));
576 ASSERT_TRUE(HasAssignable("Ljava/lang/Exception;", "Ljava/util/SimpleTimeZone;", false));
577}
578
579TEST_F(VerifierDepsTest, ArgumentType_ResolvedClass) {
580 ASSERT_TRUE(VerifyMethod("ArgumentType_ResolvedClass"));
581 ASSERT_TRUE(HasClass("Ljava/lang/Thread;", true, "public"));
582}
583
David Brazdilca3c8c32016-09-06 14:04:48 +0100584TEST_F(VerifierDepsTest, ArgumentType_UnresolvedClass) {
585 ASSERT_TRUE(VerifyMethod("ArgumentType_UnresolvedClass"));
586 ASSERT_TRUE(HasClass("LUnresolvedClass;", false));
587}
588
589TEST_F(VerifierDepsTest, ArgumentType_UnresolvedSuper) {
590 ASSERT_TRUE(VerifyMethod("ArgumentType_UnresolvedSuper"));
591 ASSERT_TRUE(HasClass("LMySetWithUnresolvedSuper;", false));
592}
593
594TEST_F(VerifierDepsTest, ReturnType_Reference) {
595 ASSERT_TRUE(VerifyMethod("ReturnType_Reference"));
596 ASSERT_TRUE(HasAssignable("Ljava/lang/Throwable;", "Ljava/lang/IllegalStateException;", true));
597}
598
599TEST_F(VerifierDepsTest, ReturnType_Array) {
600 ASSERT_FALSE(VerifyMethod("ReturnType_Array"));
601 ASSERT_TRUE(HasAssignable("Ljava/lang/Integer;", "Ljava/lang/IllegalStateException;", false));
602}
603
604TEST_F(VerifierDepsTest, InvokeArgumentType) {
605 ASSERT_TRUE(VerifyMethod("InvokeArgumentType"));
606 ASSERT_TRUE(HasClass("Ljava/text/SimpleDateFormat;", true, "public"));
607 ASSERT_TRUE(HasClass("Ljava/util/SimpleTimeZone;", true, "public"));
608 ASSERT_TRUE(HasMethod("virtual",
609 "Ljava/text/SimpleDateFormat;",
610 "setTimeZone",
611 "(Ljava/util/TimeZone;)V",
612 true,
613 "public",
614 "Ljava/text/DateFormat;"));
615 ASSERT_TRUE(HasAssignable("Ljava/util/TimeZone;", "Ljava/util/SimpleTimeZone;", true));
616}
617
618TEST_F(VerifierDepsTest, MergeTypes_RegisterLines) {
619 ASSERT_TRUE(VerifyMethod("MergeTypes_RegisterLines"));
Nicolas Geoffray119e8462016-12-21 10:29:43 +0000620 ASSERT_TRUE(HasAssignable("Ljava/lang/Exception;", "Ljava/net/SocketTimeoutException;", true));
David Brazdilca3c8c32016-09-06 14:04:48 +0100621 ASSERT_TRUE(HasAssignable(
622 "Ljava/lang/Exception;", "Ljava/util/concurrent/TimeoutException;", true));
623}
624
625TEST_F(VerifierDepsTest, MergeTypes_IfInstanceOf) {
626 ASSERT_TRUE(VerifyMethod("MergeTypes_IfInstanceOf"));
627 ASSERT_TRUE(HasAssignable("Ljava/lang/Exception;", "Ljava/net/SocketTimeoutException;", true));
628 ASSERT_TRUE(HasAssignable(
629 "Ljava/lang/Exception;", "Ljava/util/concurrent/TimeoutException;", true));
630 ASSERT_TRUE(HasAssignable("Ljava/net/SocketTimeoutException;", "Ljava/lang/Exception;", false));
631}
632
633TEST_F(VerifierDepsTest, MergeTypes_Unresolved) {
634 ASSERT_TRUE(VerifyMethod("MergeTypes_Unresolved"));
635 ASSERT_TRUE(HasAssignable("Ljava/lang/Exception;", "Ljava/net/SocketTimeoutException;", true));
636 ASSERT_TRUE(HasAssignable(
637 "Ljava/lang/Exception;", "Ljava/util/concurrent/TimeoutException;", true));
638}
639
640TEST_F(VerifierDepsTest, ConstClass_Resolved) {
641 ASSERT_TRUE(VerifyMethod("ConstClass_Resolved"));
642 ASSERT_TRUE(HasClass("Ljava/lang/IllegalStateException;", true, "public"));
643}
644
645TEST_F(VerifierDepsTest, ConstClass_Unresolved) {
646 ASSERT_TRUE(VerifyMethod("ConstClass_Unresolved"));
647 ASSERT_TRUE(HasClass("LUnresolvedClass;", false));
648}
649
650TEST_F(VerifierDepsTest, CheckCast_Resolved) {
651 ASSERT_TRUE(VerifyMethod("CheckCast_Resolved"));
652 ASSERT_TRUE(HasClass("Ljava/lang/IllegalStateException;", true, "public"));
653}
654
655TEST_F(VerifierDepsTest, CheckCast_Unresolved) {
656 ASSERT_TRUE(VerifyMethod("CheckCast_Unresolved"));
657 ASSERT_TRUE(HasClass("LUnresolvedClass;", false));
658}
659
660TEST_F(VerifierDepsTest, InstanceOf_Resolved) {
661 ASSERT_TRUE(VerifyMethod("InstanceOf_Resolved"));
662 ASSERT_TRUE(HasClass("Ljava/lang/IllegalStateException;", true, "public"));
663}
664
665TEST_F(VerifierDepsTest, InstanceOf_Unresolved) {
666 ASSERT_TRUE(VerifyMethod("InstanceOf_Unresolved"));
667 ASSERT_TRUE(HasClass("LUnresolvedClass;", false));
668}
669
670TEST_F(VerifierDepsTest, NewInstance_Resolved) {
671 ASSERT_TRUE(VerifyMethod("NewInstance_Resolved"));
672 ASSERT_TRUE(HasClass("Ljava/lang/IllegalStateException;", true, "public"));
673}
674
675TEST_F(VerifierDepsTest, NewInstance_Unresolved) {
676 ASSERT_TRUE(VerifyMethod("NewInstance_Unresolved"));
677 ASSERT_TRUE(HasClass("LUnresolvedClass;", false));
678}
679
David Brazdilca3c8c32016-09-06 14:04:48 +0100680TEST_F(VerifierDepsTest, NewArray_Unresolved) {
681 ASSERT_TRUE(VerifyMethod("NewArray_Unresolved"));
682 ASSERT_TRUE(HasClass("[LUnresolvedClass;", false));
683}
684
685TEST_F(VerifierDepsTest, Throw) {
686 ASSERT_TRUE(VerifyMethod("Throw"));
687 ASSERT_TRUE(HasAssignable("Ljava/lang/Throwable;", "Ljava/lang/IllegalStateException;", true));
688}
689
690TEST_F(VerifierDepsTest, MoveException_Resolved) {
691 ASSERT_TRUE(VerifyMethod("MoveException_Resolved"));
692 ASSERT_TRUE(HasClass("Ljava/io/InterruptedIOException;", true, "public"));
693 ASSERT_TRUE(HasClass("Ljava/net/SocketTimeoutException;", true, "public"));
694 ASSERT_TRUE(HasClass("Ljava/util/zip/ZipException;", true, "public"));
695
696 // Testing that all exception types are assignable to Throwable.
697 ASSERT_TRUE(HasAssignable("Ljava/lang/Throwable;", "Ljava/io/InterruptedIOException;", true));
698 ASSERT_TRUE(HasAssignable("Ljava/lang/Throwable;", "Ljava/net/SocketTimeoutException;", true));
699 ASSERT_TRUE(HasAssignable("Ljava/lang/Throwable;", "Ljava/util/zip/ZipException;", true));
700
701 // Testing that the merge type is assignable to Throwable.
702 ASSERT_TRUE(HasAssignable("Ljava/lang/Throwable;", "Ljava/io/IOException;", true));
703
704 // Merging of exception types.
705 ASSERT_TRUE(HasAssignable("Ljava/io/IOException;", "Ljava/io/InterruptedIOException;", true));
706 ASSERT_TRUE(HasAssignable("Ljava/io/IOException;", "Ljava/util/zip/ZipException;", true));
707 ASSERT_TRUE(HasAssignable(
708 "Ljava/io/InterruptedIOException;", "Ljava/net/SocketTimeoutException;", true));
709}
710
711TEST_F(VerifierDepsTest, MoveException_Unresolved) {
712 ASSERT_FALSE(VerifyMethod("MoveException_Unresolved"));
713 ASSERT_TRUE(HasClass("LUnresolvedException;", false));
714}
715
716TEST_F(VerifierDepsTest, StaticField_Resolved_DeclaredInReferenced) {
717 ASSERT_TRUE(VerifyMethod("StaticField_Resolved_DeclaredInReferenced"));
Nicolas Geoffray6e54f782017-03-08 15:27:09 +0000718 ASSERT_TRUE(HasClass("Ljava/lang/System;", true, "public"));
David Brazdilca3c8c32016-09-06 14:04:48 +0100719 ASSERT_TRUE(HasField("Ljava/lang/System;",
720 "out",
721 "Ljava/io/PrintStream;",
722 true,
Nicolas Geoffray6e54f782017-03-08 15:27:09 +0000723 "public static",
David Brazdilca3c8c32016-09-06 14:04:48 +0100724 "Ljava/lang/System;"));
725}
726
727TEST_F(VerifierDepsTest, StaticField_Resolved_DeclaredInSuperclass1) {
728 ASSERT_TRUE(VerifyMethod("StaticField_Resolved_DeclaredInSuperclass1"));
729 ASSERT_TRUE(HasClass("Ljava/util/SimpleTimeZone;", true, "public"));
730 ASSERT_TRUE(HasField(
Nicolas Geoffray6e54f782017-03-08 15:27:09 +0000731 "Ljava/util/SimpleTimeZone;", "LONG", "I", true, "public static", "Ljava/util/TimeZone;"));
David Brazdilca3c8c32016-09-06 14:04:48 +0100732}
733
734TEST_F(VerifierDepsTest, StaticField_Resolved_DeclaredInSuperclass2) {
735 ASSERT_TRUE(VerifyMethod("StaticField_Resolved_DeclaredInSuperclass2"));
736 ASSERT_TRUE(HasField(
Nicolas Geoffray6e54f782017-03-08 15:27:09 +0000737 "LMySimpleTimeZone;", "SHORT", "I", true, "public static", "Ljava/util/TimeZone;"));
David Brazdilca3c8c32016-09-06 14:04:48 +0100738}
739
740TEST_F(VerifierDepsTest, StaticField_Resolved_DeclaredInInterface1) {
741 ASSERT_TRUE(VerifyMethod("StaticField_Resolved_DeclaredInInterface1"));
742 ASSERT_TRUE(HasClass("Ljavax/xml/transform/dom/DOMResult;", true, "public"));
743 ASSERT_TRUE(HasField("Ljavax/xml/transform/dom/DOMResult;",
744 "PI_ENABLE_OUTPUT_ESCAPING",
745 "Ljava/lang/String;",
746 true,
Nicolas Geoffray6e54f782017-03-08 15:27:09 +0000747 "public static",
David Brazdilca3c8c32016-09-06 14:04:48 +0100748 "Ljavax/xml/transform/Result;"));
749}
750
751TEST_F(VerifierDepsTest, StaticField_Resolved_DeclaredInInterface2) {
752 ASSERT_TRUE(VerifyMethod("StaticField_Resolved_DeclaredInInterface2"));
753 ASSERT_TRUE(HasField("LMyDOMResult;",
754 "PI_ENABLE_OUTPUT_ESCAPING",
755 "Ljava/lang/String;",
756 true,
Nicolas Geoffray6e54f782017-03-08 15:27:09 +0000757 "public static",
David Brazdilca3c8c32016-09-06 14:04:48 +0100758 "Ljavax/xml/transform/Result;"));
759}
760
761TEST_F(VerifierDepsTest, StaticField_Resolved_DeclaredInInterface3) {
762 ASSERT_TRUE(VerifyMethod("StaticField_Resolved_DeclaredInInterface3"));
763 ASSERT_TRUE(HasField("LMyResult;",
764 "PI_ENABLE_OUTPUT_ESCAPING",
765 "Ljava/lang/String;",
766 true,
Nicolas Geoffray6e54f782017-03-08 15:27:09 +0000767 "public static",
David Brazdilca3c8c32016-09-06 14:04:48 +0100768 "Ljavax/xml/transform/Result;"));
769}
770
771TEST_F(VerifierDepsTest, StaticField_Resolved_DeclaredInInterface4) {
772 ASSERT_TRUE(VerifyMethod("StaticField_Resolved_DeclaredInInterface4"));
773 ASSERT_TRUE(HasField("LMyDocument;",
774 "ELEMENT_NODE",
775 "S",
776 true,
Nicolas Geoffray6e54f782017-03-08 15:27:09 +0000777 "public static",
David Brazdilca3c8c32016-09-06 14:04:48 +0100778 "Lorg/w3c/dom/Node;"));
779}
780
781TEST_F(VerifierDepsTest, StaticField_Unresolved_ReferrerInBoot) {
782 ASSERT_TRUE(VerifyMethod("StaticField_Unresolved_ReferrerInBoot"));
Nicolas Geoffray6e54f782017-03-08 15:27:09 +0000783 ASSERT_TRUE(HasClass("Ljava/util/TimeZone;", true, "public"));
David Brazdilca3c8c32016-09-06 14:04:48 +0100784 ASSERT_TRUE(HasField("Ljava/util/TimeZone;", "x", "I", false));
785}
786
787TEST_F(VerifierDepsTest, StaticField_Unresolved_ReferrerInDex) {
788 ASSERT_TRUE(VerifyMethod("StaticField_Unresolved_ReferrerInDex"));
789 ASSERT_TRUE(HasField("LMyThreadSet;", "x", "I", false));
790}
791
792TEST_F(VerifierDepsTest, InstanceField_Resolved_DeclaredInReferenced) {
793 ASSERT_TRUE(VerifyMethod("InstanceField_Resolved_DeclaredInReferenced"));
794 ASSERT_TRUE(HasClass("Ljava/io/InterruptedIOException;", true, "public"));
795 ASSERT_TRUE(HasField("Ljava/io/InterruptedIOException;",
796 "bytesTransferred",
797 "I",
798 true,
799 "public",
800 "Ljava/io/InterruptedIOException;"));
801 ASSERT_TRUE(HasAssignable(
Nicolas Geoffray119e8462016-12-21 10:29:43 +0000802 "Ljava/io/InterruptedIOException;", "Ljava/net/SocketTimeoutException;", true));
David Brazdilca3c8c32016-09-06 14:04:48 +0100803}
804
805TEST_F(VerifierDepsTest, InstanceField_Resolved_DeclaredInSuperclass1) {
806 ASSERT_TRUE(VerifyMethod("InstanceField_Resolved_DeclaredInSuperclass1"));
807 ASSERT_TRUE(HasClass("Ljava/net/SocketTimeoutException;", true, "public"));
808 ASSERT_TRUE(HasField("Ljava/net/SocketTimeoutException;",
809 "bytesTransferred",
810 "I",
811 true,
812 "public",
813 "Ljava/io/InterruptedIOException;"));
814 ASSERT_TRUE(HasAssignable(
Nicolas Geoffray119e8462016-12-21 10:29:43 +0000815 "Ljava/io/InterruptedIOException;", "Ljava/net/SocketTimeoutException;", true));
David Brazdilca3c8c32016-09-06 14:04:48 +0100816}
817
818TEST_F(VerifierDepsTest, InstanceField_Resolved_DeclaredInSuperclass2) {
819 ASSERT_TRUE(VerifyMethod("InstanceField_Resolved_DeclaredInSuperclass2"));
820 ASSERT_TRUE(HasField("LMySocketTimeoutException;",
821 "bytesTransferred",
822 "I",
823 true,
824 "public",
825 "Ljava/io/InterruptedIOException;"));
826 ASSERT_TRUE(HasAssignable(
Nicolas Geoffray119e8462016-12-21 10:29:43 +0000827 "Ljava/io/InterruptedIOException;", "Ljava/net/SocketTimeoutException;", true));
David Brazdilca3c8c32016-09-06 14:04:48 +0100828}
829
830TEST_F(VerifierDepsTest, InstanceField_Unresolved_ReferrerInBoot) {
831 ASSERT_TRUE(VerifyMethod("InstanceField_Unresolved_ReferrerInBoot"));
832 ASSERT_TRUE(HasClass("Ljava/io/InterruptedIOException;", true, "public"));
833 ASSERT_TRUE(HasField("Ljava/io/InterruptedIOException;", "x", "I", false));
834}
835
836TEST_F(VerifierDepsTest, InstanceField_Unresolved_ReferrerInDex) {
837 ASSERT_TRUE(VerifyMethod("InstanceField_Unresolved_ReferrerInDex"));
838 ASSERT_TRUE(HasField("LMyThreadSet;", "x", "I", false));
839}
840
841TEST_F(VerifierDepsTest, InvokeStatic_Resolved_DeclaredInReferenced) {
842 ASSERT_TRUE(VerifyMethod("InvokeStatic_Resolved_DeclaredInReferenced"));
843 ASSERT_TRUE(HasClass("Ljava/net/Socket;", true, "public"));
844 ASSERT_TRUE(HasMethod("direct",
845 "Ljava/net/Socket;",
846 "setSocketImplFactory",
847 "(Ljava/net/SocketImplFactory;)V",
848 true,
849 "public static",
850 "Ljava/net/Socket;"));
851}
852
853TEST_F(VerifierDepsTest, InvokeStatic_Resolved_DeclaredInSuperclass1) {
854 ASSERT_TRUE(VerifyMethod("InvokeStatic_Resolved_DeclaredInSuperclass1"));
Nicolas Geoffray6e54f782017-03-08 15:27:09 +0000855 ASSERT_TRUE(HasClass("Ljavax/net/ssl/SSLSocket;", true, "public"));
David Brazdilca3c8c32016-09-06 14:04:48 +0100856 ASSERT_TRUE(HasMethod("direct",
857 "Ljavax/net/ssl/SSLSocket;",
858 "setSocketImplFactory",
859 "(Ljava/net/SocketImplFactory;)V",
860 true,
861 "public static",
862 "Ljava/net/Socket;"));
863}
864
865TEST_F(VerifierDepsTest, InvokeStatic_Resolved_DeclaredInSuperclass2) {
866 ASSERT_TRUE(VerifyMethod("InvokeStatic_Resolved_DeclaredInSuperclass2"));
867 ASSERT_TRUE(HasMethod("direct",
868 "LMySSLSocket;",
869 "setSocketImplFactory",
870 "(Ljava/net/SocketImplFactory;)V",
871 true,
872 "public static",
873 "Ljava/net/Socket;"));
874}
875
876TEST_F(VerifierDepsTest, InvokeStatic_DeclaredInInterface1) {
877 ASSERT_TRUE(VerifyMethod("InvokeStatic_DeclaredInInterface1"));
Nicolas Geoffray6e54f782017-03-08 15:27:09 +0000878 ASSERT_TRUE(HasClass("Ljava/util/Map$Entry;", true, "public interface"));
David Brazdilca3c8c32016-09-06 14:04:48 +0100879 ASSERT_TRUE(HasMethod("direct",
880 "Ljava/util/Map$Entry;",
881 "comparingByKey",
882 "()Ljava/util/Comparator;",
883 true,
884 "public static",
885 "Ljava/util/Map$Entry;"));
886}
887
888TEST_F(VerifierDepsTest, InvokeStatic_DeclaredInInterface2) {
889 ASSERT_FALSE(VerifyMethod("InvokeStatic_DeclaredInInterface2"));
890 ASSERT_TRUE(HasClass("Ljava/util/AbstractMap$SimpleEntry;", true, "public"));
891 ASSERT_TRUE(HasMethod("direct",
892 "Ljava/util/AbstractMap$SimpleEntry;",
893 "comparingByKey",
894 "()Ljava/util/Comparator;",
895 false));
896}
897
898TEST_F(VerifierDepsTest, InvokeStatic_Unresolved1) {
899 ASSERT_FALSE(VerifyMethod("InvokeStatic_Unresolved1"));
Nicolas Geoffray6e54f782017-03-08 15:27:09 +0000900 ASSERT_TRUE(HasClass("Ljavax/net/ssl/SSLSocket;", true, "public"));
David Brazdilca3c8c32016-09-06 14:04:48 +0100901 ASSERT_TRUE(HasMethod("direct", "Ljavax/net/ssl/SSLSocket;", "x", "()V", false));
902}
903
904TEST_F(VerifierDepsTest, InvokeStatic_Unresolved2) {
905 ASSERT_FALSE(VerifyMethod("InvokeStatic_Unresolved2"));
906 ASSERT_TRUE(HasMethod("direct", "LMySSLSocket;", "x", "()V", false));
907}
908
909TEST_F(VerifierDepsTest, InvokeDirect_Resolved_DeclaredInReferenced) {
910 ASSERT_TRUE(VerifyMethod("InvokeDirect_Resolved_DeclaredInReferenced"));
911 ASSERT_TRUE(HasClass("Ljava/net/Socket;", true, "public"));
912 ASSERT_TRUE(HasMethod(
913 "direct", "Ljava/net/Socket;", "<init>", "()V", true, "public", "Ljava/net/Socket;"));
914}
915
916TEST_F(VerifierDepsTest, InvokeDirect_Resolved_DeclaredInSuperclass1) {
917 ASSERT_FALSE(VerifyMethod("InvokeDirect_Resolved_DeclaredInSuperclass1"));
Nicolas Geoffray6e54f782017-03-08 15:27:09 +0000918 ASSERT_TRUE(HasClass("Ljavax/net/ssl/SSLSocket;", true, "public"));
David Brazdilca3c8c32016-09-06 14:04:48 +0100919 ASSERT_TRUE(HasMethod("direct",
920 "Ljavax/net/ssl/SSLSocket;",
921 "checkOldImpl",
922 "()V",
923 true,
924 "private",
925 "Ljava/net/Socket;"));
926}
927
928TEST_F(VerifierDepsTest, InvokeDirect_Resolved_DeclaredInSuperclass2) {
929 ASSERT_FALSE(VerifyMethod("InvokeDirect_Resolved_DeclaredInSuperclass2"));
930 ASSERT_TRUE(HasMethod(
931 "direct", "LMySSLSocket;", "checkOldImpl", "()V", true, "private", "Ljava/net/Socket;"));
932}
933
934TEST_F(VerifierDepsTest, InvokeDirect_Unresolved1) {
935 ASSERT_FALSE(VerifyMethod("InvokeDirect_Unresolved1"));
Nicolas Geoffray6e54f782017-03-08 15:27:09 +0000936 ASSERT_TRUE(HasClass("Ljavax/net/ssl/SSLSocket;", true, "public"));
David Brazdilca3c8c32016-09-06 14:04:48 +0100937 ASSERT_TRUE(HasMethod("direct", "Ljavax/net/ssl/SSLSocket;", "x", "()V", false));
938}
939
940TEST_F(VerifierDepsTest, InvokeDirect_Unresolved2) {
941 ASSERT_FALSE(VerifyMethod("InvokeDirect_Unresolved2"));
942 ASSERT_TRUE(HasMethod("direct", "LMySSLSocket;", "x", "()V", false));
943}
944
945TEST_F(VerifierDepsTest, InvokeVirtual_Resolved_DeclaredInReferenced) {
946 ASSERT_TRUE(VerifyMethod("InvokeVirtual_Resolved_DeclaredInReferenced"));
947 ASSERT_TRUE(HasClass("Ljava/lang/Throwable;", true, "public"));
948 ASSERT_TRUE(HasMethod("virtual",
949 "Ljava/lang/Throwable;",
950 "getMessage",
951 "()Ljava/lang/String;",
952 true,
953 "public",
954 "Ljava/lang/Throwable;"));
955 // Type dependency on `this` argument.
Nicolas Geoffray119e8462016-12-21 10:29:43 +0000956 ASSERT_TRUE(HasAssignable("Ljava/lang/Throwable;", "Ljava/net/SocketTimeoutException;", true));
David Brazdilca3c8c32016-09-06 14:04:48 +0100957}
958
959TEST_F(VerifierDepsTest, InvokeVirtual_Resolved_DeclaredInSuperclass1) {
960 ASSERT_TRUE(VerifyMethod("InvokeVirtual_Resolved_DeclaredInSuperclass1"));
961 ASSERT_TRUE(HasClass("Ljava/io/InterruptedIOException;", true, "public"));
962 ASSERT_TRUE(HasMethod("virtual",
963 "Ljava/io/InterruptedIOException;",
964 "getMessage",
965 "()Ljava/lang/String;",
966 true,
967 "public",
968 "Ljava/lang/Throwable;"));
969 // Type dependency on `this` argument.
Nicolas Geoffray119e8462016-12-21 10:29:43 +0000970 ASSERT_TRUE(HasAssignable("Ljava/lang/Throwable;", "Ljava/net/SocketTimeoutException;", true));
David Brazdilca3c8c32016-09-06 14:04:48 +0100971}
972
973TEST_F(VerifierDepsTest, InvokeVirtual_Resolved_DeclaredInSuperclass2) {
974 ASSERT_TRUE(VerifyMethod("InvokeVirtual_Resolved_DeclaredInSuperclass2"));
975 ASSERT_TRUE(HasMethod("virtual",
976 "LMySocketTimeoutException;",
977 "getMessage",
978 "()Ljava/lang/String;",
979 true,
980 "public",
981 "Ljava/lang/Throwable;"));
982}
983
984TEST_F(VerifierDepsTest, InvokeVirtual_Resolved_DeclaredInSuperinterface) {
985 ASSERT_TRUE(VerifyMethod("InvokeVirtual_Resolved_DeclaredInSuperinterface"));
986 ASSERT_TRUE(HasMethod("virtual",
987 "LMyThreadSet;",
988 "size",
989 "()I",
990 true,
Nicolas Geoffray6e54f782017-03-08 15:27:09 +0000991 "public",
David Brazdilca3c8c32016-09-06 14:04:48 +0100992 "Ljava/util/Set;"));
993}
994
995TEST_F(VerifierDepsTest, InvokeVirtual_Unresolved1) {
996 ASSERT_FALSE(VerifyMethod("InvokeVirtual_Unresolved1"));
997 ASSERT_TRUE(HasClass("Ljava/io/InterruptedIOException;", true, "public"));
998 ASSERT_TRUE(HasMethod("virtual", "Ljava/io/InterruptedIOException;", "x", "()V", false));
999}
1000
1001TEST_F(VerifierDepsTest, InvokeVirtual_Unresolved2) {
1002 ASSERT_FALSE(VerifyMethod("InvokeVirtual_Unresolved2"));
1003 ASSERT_TRUE(HasMethod("virtual", "LMySocketTimeoutException;", "x", "()V", false));
1004}
1005
1006TEST_F(VerifierDepsTest, InvokeVirtual_ActuallyDirect) {
1007 ASSERT_FALSE(VerifyMethod("InvokeVirtual_ActuallyDirect"));
1008 ASSERT_TRUE(HasMethod("virtual", "LMyThread;", "activeCount", "()I", false));
1009 ASSERT_TRUE(HasMethod("direct",
1010 "LMyThread;",
1011 "activeCount",
1012 "()I",
1013 true,
1014 "public static",
1015 "Ljava/lang/Thread;"));
1016}
1017
1018TEST_F(VerifierDepsTest, InvokeInterface_Resolved_DeclaredInReferenced) {
1019 ASSERT_TRUE(VerifyMethod("InvokeInterface_Resolved_DeclaredInReferenced"));
Nicolas Geoffray6e54f782017-03-08 15:27:09 +00001020 ASSERT_TRUE(HasClass("Ljava/lang/Runnable;", true, "public interface"));
David Brazdilca3c8c32016-09-06 14:04:48 +01001021 ASSERT_TRUE(HasMethod("interface",
1022 "Ljava/lang/Runnable;",
1023 "run",
1024 "()V",
1025 true,
Nicolas Geoffray6e54f782017-03-08 15:27:09 +00001026 "public",
David Brazdilca3c8c32016-09-06 14:04:48 +01001027 "Ljava/lang/Runnable;"));
1028}
1029
1030TEST_F(VerifierDepsTest, InvokeInterface_Resolved_DeclaredInSuperclass) {
1031 ASSERT_FALSE(VerifyMethod("InvokeInterface_Resolved_DeclaredInSuperclass"));
1032 ASSERT_TRUE(HasMethod("interface", "LMyThread;", "join", "()V", false));
1033}
1034
1035TEST_F(VerifierDepsTest, InvokeInterface_Resolved_DeclaredInSuperinterface1) {
1036 ASSERT_FALSE(VerifyMethod("InvokeInterface_Resolved_DeclaredInSuperinterface1"));
1037 ASSERT_TRUE(HasMethod("interface",
1038 "LMyThreadSet;",
1039 "run",
1040 "()V",
1041 true,
Nicolas Geoffray6e54f782017-03-08 15:27:09 +00001042 "public",
David Brazdilca3c8c32016-09-06 14:04:48 +01001043 "Ljava/lang/Runnable;"));
1044}
1045
1046TEST_F(VerifierDepsTest, InvokeInterface_Resolved_DeclaredInSuperinterface2) {
1047 ASSERT_FALSE(VerifyMethod("InvokeInterface_Resolved_DeclaredInSuperinterface2"));
1048 ASSERT_TRUE(HasMethod("interface",
1049 "LMyThreadSet;",
1050 "isEmpty",
1051 "()Z",
1052 true,
Nicolas Geoffray6e54f782017-03-08 15:27:09 +00001053 "public",
David Brazdilca3c8c32016-09-06 14:04:48 +01001054 "Ljava/util/Set;"));
1055}
1056
1057TEST_F(VerifierDepsTest, InvokeInterface_Unresolved1) {
1058 ASSERT_FALSE(VerifyMethod("InvokeInterface_Unresolved1"));
Nicolas Geoffray6e54f782017-03-08 15:27:09 +00001059 ASSERT_TRUE(HasClass("Ljava/lang/Runnable;", true, "public interface"));
David Brazdilca3c8c32016-09-06 14:04:48 +01001060 ASSERT_TRUE(HasMethod("interface", "Ljava/lang/Runnable;", "x", "()V", false));
1061}
1062
1063TEST_F(VerifierDepsTest, InvokeInterface_Unresolved2) {
1064 ASSERT_FALSE(VerifyMethod("InvokeInterface_Unresolved2"));
1065 ASSERT_TRUE(HasMethod("interface", "LMyThreadSet;", "x", "()V", false));
1066}
1067
1068TEST_F(VerifierDepsTest, InvokeSuper_ThisAssignable) {
1069 ASSERT_TRUE(VerifyMethod("InvokeSuper_ThisAssignable"));
Nicolas Geoffray6e54f782017-03-08 15:27:09 +00001070 ASSERT_TRUE(HasClass("Ljava/lang/Runnable;", true, "public interface"));
Nicolas Geoffray0e2fe0f2016-12-21 16:54:52 +00001071 ASSERT_TRUE(HasAssignable("Ljava/lang/Runnable;", "Ljava/lang/Thread;", true));
David Brazdilca3c8c32016-09-06 14:04:48 +01001072 ASSERT_TRUE(HasMethod("interface",
1073 "Ljava/lang/Runnable;",
1074 "run",
1075 "()V",
1076 true,
Nicolas Geoffray6e54f782017-03-08 15:27:09 +00001077 "public",
David Brazdilca3c8c32016-09-06 14:04:48 +01001078 "Ljava/lang/Runnable;"));
1079}
1080
1081TEST_F(VerifierDepsTest, InvokeSuper_ThisNotAssignable) {
1082 ASSERT_FALSE(VerifyMethod("InvokeSuper_ThisNotAssignable"));
Nicolas Geoffray6e54f782017-03-08 15:27:09 +00001083 ASSERT_TRUE(HasClass("Ljava/lang/Integer;", true, "public"));
Nicolas Geoffray119e8462016-12-21 10:29:43 +00001084 ASSERT_TRUE(HasAssignable("Ljava/lang/Integer;", "Ljava/lang/Thread;", false));
David Brazdilca3c8c32016-09-06 14:04:48 +01001085 ASSERT_TRUE(HasMethod(
1086 "virtual", "Ljava/lang/Integer;", "intValue", "()I", true, "public", "Ljava/lang/Integer;"));
1087}
1088
Nicolas Geoffray0f1cb172017-01-05 15:23:19 +00001089TEST_F(VerifierDepsTest, ArgumentType_ResolvedReferenceArray) {
1090 ASSERT_TRUE(VerifyMethod("ArgumentType_ResolvedReferenceArray"));
Nicolas Geoffray6e54f782017-03-08 15:27:09 +00001091 ASSERT_TRUE(HasClass("[Ljava/lang/Thread;", true, "public"));
Nicolas Geoffray0f1cb172017-01-05 15:23:19 +00001092}
1093
1094TEST_F(VerifierDepsTest, NewArray_Resolved) {
1095 ASSERT_TRUE(VerifyMethod("NewArray_Resolved"));
Nicolas Geoffray6e54f782017-03-08 15:27:09 +00001096 ASSERT_TRUE(HasClass("[Ljava/lang/IllegalStateException;", true, "public"));
Nicolas Geoffray0f1cb172017-01-05 15:23:19 +00001097}
1098
David Brazdil6f82fbd2016-09-14 11:55:26 +01001099TEST_F(VerifierDepsTest, EncodeDecode) {
1100 VerifyDexFile();
1101
1102 ASSERT_EQ(1u, NumberOfCompiledDexFiles());
1103 ASSERT_TRUE(HasEachKindOfRecord());
1104
1105 std::vector<uint8_t> buffer;
Nicolas Geoffrayd01f60c2016-10-28 14:45:48 +01001106 verifier_deps_->Encode(dex_files_, &buffer);
David Brazdil6f82fbd2016-09-14 11:55:26 +01001107 ASSERT_FALSE(buffer.empty());
1108
Nicolas Geoffraye70dd562016-10-30 21:03:35 +00001109 VerifierDeps decoded_deps(dex_files_, ArrayRef<const uint8_t>(buffer));
David Brazdil6f82fbd2016-09-14 11:55:26 +01001110 ASSERT_TRUE(verifier_deps_->Equals(decoded_deps));
1111}
1112
Nicolas Geoffrayd01f60c2016-10-28 14:45:48 +01001113TEST_F(VerifierDepsTest, EncodeDecodeMulti) {
1114 VerifyDexFile("MultiDex");
1115
1116 ASSERT_GT(NumberOfCompiledDexFiles(), 1u);
1117 std::vector<uint8_t> buffer;
1118 verifier_deps_->Encode(dex_files_, &buffer);
1119 ASSERT_FALSE(buffer.empty());
1120
1121 // Create new DexFile, to mess with std::map order: the verifier deps used
1122 // to iterate over the map, which doesn't guarantee insertion order. We fixed
1123 // this by passing the expected order when encoding/decoding.
1124 std::vector<std::unique_ptr<const DexFile>> first_dex_files = OpenTestDexFiles("VerifierDeps");
1125 std::vector<std::unique_ptr<const DexFile>> second_dex_files = OpenTestDexFiles("MultiDex");
1126 std::vector<const DexFile*> dex_files;
1127 for (auto& dex_file : first_dex_files) {
1128 dex_files.push_back(dex_file.get());
1129 }
1130 for (auto& dex_file : second_dex_files) {
1131 dex_files.push_back(dex_file.get());
1132 }
1133
1134 // Dump the new verifier deps to ensure it can properly read the data.
Nicolas Geoffraye70dd562016-10-30 21:03:35 +00001135 VerifierDeps decoded_deps(dex_files, ArrayRef<const uint8_t>(buffer));
Nicolas Geoffrayd01f60c2016-10-28 14:45:48 +01001136 std::ostringstream stream;
1137 VariableIndentationOutputStream os(&stream);
1138 decoded_deps.Dump(&os);
1139}
1140
Nicolas Geoffray08025182016-10-25 17:20:18 +01001141TEST_F(VerifierDepsTest, UnverifiedClasses) {
1142 VerifyDexFile();
1143 ASSERT_FALSE(HasUnverifiedClass("LMyThread;"));
1144 // Test that a class with a soft failure is recorded.
1145 ASSERT_TRUE(HasUnverifiedClass("LMain;"));
1146 // Test that a class with hard failure is recorded.
1147 ASSERT_TRUE(HasUnverifiedClass("LMyVerificationFailure;"));
1148 // Test that a class with unresolved super is recorded.
Nicolas Geoffray7cc3ae52017-03-07 14:33:37 +00001149 ASSERT_TRUE(HasUnverifiedClass("LMyClassWithNoSuper;"));
Nicolas Geoffray08025182016-10-25 17:20:18 +01001150 // Test that a class with unresolved super and hard failure is recorded.
1151 ASSERT_TRUE(HasUnverifiedClass("LMyClassWithNoSuperButFailures;"));
1152}
1153
Nicolas Geoffray8904b6f2016-10-28 19:50:34 +01001154// Returns the next resolution kind in the enum.
1155static MethodResolutionKind GetNextResolutionKind(MethodResolutionKind resolution_kind) {
1156 if (resolution_kind == kDirectMethodResolution) {
1157 return kVirtualMethodResolution;
1158 } else if (resolution_kind == kVirtualMethodResolution) {
1159 return kInterfaceMethodResolution;
1160 } else {
1161 DCHECK_EQ(resolution_kind, kInterfaceMethodResolution);
1162 return kDirectMethodResolution;
1163 }
1164}
1165
1166TEST_F(VerifierDepsTest, VerifyDeps) {
1167 VerifyDexFile();
1168
1169 ASSERT_EQ(1u, NumberOfCompiledDexFiles());
1170 ASSERT_TRUE(HasEachKindOfRecord());
1171
1172 // When validating, we create a new class loader, as
1173 // the existing `class_loader_` may contain erroneous classes,
1174 // that ClassLinker::FindClass won't return.
1175
1176 ScopedObjectAccess soa(Thread::Current());
1177 StackHandleScope<1> hs(soa.Self());
1178 MutableHandle<mirror::ClassLoader> new_class_loader(hs.NewHandle<mirror::ClassLoader>(nullptr));
1179 {
1180 new_class_loader.Assign(soa.Decode<mirror::ClassLoader>(LoadDex("VerifierDeps")));
Nicolas Geoffray6bb7f1b2016-11-03 10:52:49 +00001181 ASSERT_TRUE(verifier_deps_->ValidateDependencies(new_class_loader, soa.Self()));
Nicolas Geoffray8904b6f2016-10-28 19:50:34 +01001182 }
1183
1184 std::vector<uint8_t> buffer;
1185 verifier_deps_->Encode(dex_files_, &buffer);
1186 ASSERT_FALSE(buffer.empty());
1187
1188 {
1189 VerifierDeps decoded_deps(dex_files_, ArrayRef<const uint8_t>(buffer));
1190 new_class_loader.Assign(soa.Decode<mirror::ClassLoader>(LoadDex("VerifierDeps")));
Nicolas Geoffray6bb7f1b2016-11-03 10:52:49 +00001191 ASSERT_TRUE(decoded_deps.ValidateDependencies(new_class_loader, soa.Self()));
Nicolas Geoffray8904b6f2016-10-28 19:50:34 +01001192 }
1193
1194 // Fiddle with the dependencies to make sure we catch any change and fail to verify.
1195
1196 {
1197 // Mess up with the assignable_types.
1198 VerifierDeps decoded_deps(dex_files_, ArrayRef<const uint8_t>(buffer));
1199 VerifierDeps::DexFileDeps* deps = decoded_deps.GetDexFileDeps(*primary_dex_file_);
1200 deps->assignable_types_.insert(*deps->unassignable_types_.begin());
1201 new_class_loader.Assign(soa.Decode<mirror::ClassLoader>(LoadDex("VerifierDeps")));
Nicolas Geoffray6bb7f1b2016-11-03 10:52:49 +00001202 ASSERT_FALSE(decoded_deps.ValidateDependencies(new_class_loader, soa.Self()));
Nicolas Geoffray8904b6f2016-10-28 19:50:34 +01001203 }
1204
1205 {
1206 // Mess up with the unassignable_types.
1207 VerifierDeps decoded_deps(dex_files_, ArrayRef<const uint8_t>(buffer));
1208 VerifierDeps::DexFileDeps* deps = decoded_deps.GetDexFileDeps(*primary_dex_file_);
1209 deps->unassignable_types_.insert(*deps->assignable_types_.begin());
1210 new_class_loader.Assign(soa.Decode<mirror::ClassLoader>(LoadDex("VerifierDeps")));
Nicolas Geoffray6bb7f1b2016-11-03 10:52:49 +00001211 ASSERT_FALSE(decoded_deps.ValidateDependencies(new_class_loader, soa.Self()));
Nicolas Geoffray8904b6f2016-10-28 19:50:34 +01001212 }
1213
1214 // Mess up with classes.
1215 {
1216 VerifierDeps decoded_deps(dex_files_, ArrayRef<const uint8_t>(buffer));
1217 VerifierDeps::DexFileDeps* deps = decoded_deps.GetDexFileDeps(*primary_dex_file_);
1218 bool found = false;
1219 for (const auto& entry : deps->classes_) {
1220 if (entry.IsResolved()) {
1221 deps->classes_.insert(VerifierDeps::ClassResolution(
1222 entry.GetDexTypeIndex(), VerifierDeps::kUnresolvedMarker));
1223 found = true;
1224 break;
1225 }
1226 }
1227 ASSERT_TRUE(found);
1228 new_class_loader.Assign(soa.Decode<mirror::ClassLoader>(LoadDex("VerifierDeps")));
Nicolas Geoffray6bb7f1b2016-11-03 10:52:49 +00001229 ASSERT_FALSE(decoded_deps.ValidateDependencies(new_class_loader, soa.Self()));
Nicolas Geoffray8904b6f2016-10-28 19:50:34 +01001230 }
1231
1232 {
1233 VerifierDeps decoded_deps(dex_files_, ArrayRef<const uint8_t>(buffer));
1234 VerifierDeps::DexFileDeps* deps = decoded_deps.GetDexFileDeps(*primary_dex_file_);
1235 bool found = false;
1236 for (const auto& entry : deps->classes_) {
1237 if (!entry.IsResolved()) {
1238 deps->classes_.insert(VerifierDeps::ClassResolution(
1239 entry.GetDexTypeIndex(), VerifierDeps::kUnresolvedMarker - 1));
1240 found = true;
1241 break;
1242 }
1243 }
1244 ASSERT_TRUE(found);
1245 new_class_loader.Assign(soa.Decode<mirror::ClassLoader>(LoadDex("VerifierDeps")));
Nicolas Geoffray6bb7f1b2016-11-03 10:52:49 +00001246 ASSERT_FALSE(decoded_deps.ValidateDependencies(new_class_loader, soa.Self()));
Nicolas Geoffray8904b6f2016-10-28 19:50:34 +01001247 }
1248
1249 {
1250 VerifierDeps decoded_deps(dex_files_, ArrayRef<const uint8_t>(buffer));
1251 VerifierDeps::DexFileDeps* deps = decoded_deps.GetDexFileDeps(*primary_dex_file_);
1252 bool found = false;
1253 for (const auto& entry : deps->classes_) {
1254 if (entry.IsResolved()) {
1255 deps->classes_.insert(VerifierDeps::ClassResolution(
1256 entry.GetDexTypeIndex(), entry.GetAccessFlags() - 1));
1257 found = true;
1258 break;
1259 }
1260 }
1261 ASSERT_TRUE(found);
1262 new_class_loader.Assign(soa.Decode<mirror::ClassLoader>(LoadDex("VerifierDeps")));
Nicolas Geoffray6bb7f1b2016-11-03 10:52:49 +00001263 ASSERT_FALSE(decoded_deps.ValidateDependencies(new_class_loader, soa.Self()));
Nicolas Geoffray8904b6f2016-10-28 19:50:34 +01001264 }
1265
1266 // Mess up with fields.
1267 {
1268 VerifierDeps decoded_deps(dex_files_, ArrayRef<const uint8_t>(buffer));
1269 VerifierDeps::DexFileDeps* deps = decoded_deps.GetDexFileDeps(*primary_dex_file_);
1270 bool found = false;
1271 for (const auto& entry : deps->fields_) {
1272 if (entry.IsResolved()) {
1273 deps->fields_.insert(VerifierDeps::FieldResolution(entry.GetDexFieldIndex(),
1274 VerifierDeps::kUnresolvedMarker,
1275 entry.GetDeclaringClassIndex()));
1276 found = true;
1277 break;
1278 }
1279 }
1280 ASSERT_TRUE(found);
1281 new_class_loader.Assign(soa.Decode<mirror::ClassLoader>(LoadDex("VerifierDeps")));
Nicolas Geoffray6bb7f1b2016-11-03 10:52:49 +00001282 ASSERT_FALSE(decoded_deps.ValidateDependencies(new_class_loader, soa.Self()));
Nicolas Geoffray8904b6f2016-10-28 19:50:34 +01001283 }
1284
1285 {
1286 VerifierDeps decoded_deps(dex_files_, ArrayRef<const uint8_t>(buffer));
1287 VerifierDeps::DexFileDeps* deps = decoded_deps.GetDexFileDeps(*primary_dex_file_);
1288 bool found = false;
1289 for (const auto& entry : deps->fields_) {
1290 if (!entry.IsResolved()) {
Andreas Gampe8a0128a2016-11-28 07:38:35 -08001291 constexpr dex::StringIndex kStringIndexZero(0); // We know there is a class there.
Nicolas Geoffray8904b6f2016-10-28 19:50:34 +01001292 deps->fields_.insert(VerifierDeps::FieldResolution(0 /* we know there is a field there */,
1293 VerifierDeps::kUnresolvedMarker - 1,
Andreas Gampe8a0128a2016-11-28 07:38:35 -08001294 kStringIndexZero));
Nicolas Geoffray8904b6f2016-10-28 19:50:34 +01001295 found = true;
1296 break;
1297 }
1298 }
1299 ASSERT_TRUE(found);
1300 new_class_loader.Assign(soa.Decode<mirror::ClassLoader>(LoadDex("VerifierDeps")));
Nicolas Geoffray6bb7f1b2016-11-03 10:52:49 +00001301 ASSERT_FALSE(decoded_deps.ValidateDependencies(new_class_loader, soa.Self()));
Nicolas Geoffray8904b6f2016-10-28 19:50:34 +01001302 }
1303
1304 {
1305 VerifierDeps decoded_deps(dex_files_, ArrayRef<const uint8_t>(buffer));
1306 VerifierDeps::DexFileDeps* deps = decoded_deps.GetDexFileDeps(*primary_dex_file_);
1307 bool found = false;
1308 for (const auto& entry : deps->fields_) {
1309 if (entry.IsResolved()) {
1310 deps->fields_.insert(VerifierDeps::FieldResolution(entry.GetDexFieldIndex(),
1311 entry.GetAccessFlags() - 1,
1312 entry.GetDeclaringClassIndex()));
1313 found = true;
1314 break;
1315 }
1316 }
1317 ASSERT_TRUE(found);
1318 new_class_loader.Assign(soa.Decode<mirror::ClassLoader>(LoadDex("VerifierDeps")));
Nicolas Geoffray6bb7f1b2016-11-03 10:52:49 +00001319 ASSERT_FALSE(decoded_deps.ValidateDependencies(new_class_loader, soa.Self()));
Nicolas Geoffray8904b6f2016-10-28 19:50:34 +01001320 }
1321
1322 {
1323 VerifierDeps decoded_deps(dex_files_, ArrayRef<const uint8_t>(buffer));
1324 VerifierDeps::DexFileDeps* deps = decoded_deps.GetDexFileDeps(*primary_dex_file_);
1325 bool found = false;
1326 for (const auto& entry : deps->fields_) {
Andreas Gampe8a0128a2016-11-28 07:38:35 -08001327 constexpr dex::StringIndex kNewTypeIndex(0);
Nicolas Geoffray8904b6f2016-10-28 19:50:34 +01001328 if (entry.GetDeclaringClassIndex() != kNewTypeIndex) {
1329 deps->fields_.insert(VerifierDeps::FieldResolution(entry.GetDexFieldIndex(),
1330 entry.GetAccessFlags(),
1331 kNewTypeIndex));
1332 found = true;
1333 break;
1334 }
1335 }
1336 ASSERT_TRUE(found);
1337 new_class_loader.Assign(soa.Decode<mirror::ClassLoader>(LoadDex("VerifierDeps")));
Nicolas Geoffray6bb7f1b2016-11-03 10:52:49 +00001338 ASSERT_FALSE(decoded_deps.ValidateDependencies(new_class_loader, soa.Self()));
Nicolas Geoffray8904b6f2016-10-28 19:50:34 +01001339 }
1340
1341 // Mess up with methods.
1342 for (MethodResolutionKind resolution_kind :
1343 { kDirectMethodResolution, kVirtualMethodResolution, kInterfaceMethodResolution }) {
1344 {
1345 VerifierDeps decoded_deps(dex_files_, ArrayRef<const uint8_t>(buffer));
1346 VerifierDeps::DexFileDeps* deps = decoded_deps.GetDexFileDeps(*primary_dex_file_);
1347 bool found = false;
1348 std::set<VerifierDeps::MethodResolution>* methods = GetMethods(deps, resolution_kind);
1349 for (const auto& entry : *methods) {
1350 if (entry.IsResolved()) {
1351 methods->insert(VerifierDeps::MethodResolution(entry.GetDexMethodIndex(),
1352 VerifierDeps::kUnresolvedMarker,
1353 entry.GetDeclaringClassIndex()));
1354 found = true;
1355 break;
1356 }
1357 }
1358 ASSERT_TRUE(found);
1359 new_class_loader.Assign(soa.Decode<mirror::ClassLoader>(LoadDex("VerifierDeps")));
Nicolas Geoffray6bb7f1b2016-11-03 10:52:49 +00001360 ASSERT_FALSE(decoded_deps.ValidateDependencies(new_class_loader, soa.Self()));
Nicolas Geoffray8904b6f2016-10-28 19:50:34 +01001361 }
1362
1363 {
1364 VerifierDeps decoded_deps(dex_files_, ArrayRef<const uint8_t>(buffer));
1365 VerifierDeps::DexFileDeps* deps = decoded_deps.GetDexFileDeps(*primary_dex_file_);
1366 bool found = false;
1367 std::set<VerifierDeps::MethodResolution>* methods = GetMethods(deps, resolution_kind);
1368 for (const auto& entry : *methods) {
1369 if (!entry.IsResolved()) {
Andreas Gampe8a0128a2016-11-28 07:38:35 -08001370 constexpr dex::StringIndex kStringIndexZero(0); // We know there is a class there.
Nicolas Geoffray8904b6f2016-10-28 19:50:34 +01001371 methods->insert(VerifierDeps::MethodResolution(0 /* we know there is a method there */,
1372 VerifierDeps::kUnresolvedMarker - 1,
Andreas Gampe8a0128a2016-11-28 07:38:35 -08001373 kStringIndexZero));
Nicolas Geoffray8904b6f2016-10-28 19:50:34 +01001374 found = true;
1375 break;
1376 }
1377 }
1378 ASSERT_TRUE(found);
1379 new_class_loader.Assign(soa.Decode<mirror::ClassLoader>(LoadDex("VerifierDeps")));
Nicolas Geoffray6bb7f1b2016-11-03 10:52:49 +00001380 ASSERT_FALSE(decoded_deps.ValidateDependencies(new_class_loader, soa.Self()));
Nicolas Geoffray8904b6f2016-10-28 19:50:34 +01001381 }
1382
1383 {
1384 VerifierDeps decoded_deps(dex_files_, ArrayRef<const uint8_t>(buffer));
1385 VerifierDeps::DexFileDeps* deps = decoded_deps.GetDexFileDeps(*primary_dex_file_);
1386 bool found = false;
1387 std::set<VerifierDeps::MethodResolution>* methods = GetMethods(deps, resolution_kind);
1388 for (const auto& entry : *methods) {
1389 if (entry.IsResolved()) {
1390 methods->insert(VerifierDeps::MethodResolution(entry.GetDexMethodIndex(),
1391 entry.GetAccessFlags() - 1,
1392 entry.GetDeclaringClassIndex()));
1393 found = true;
1394 break;
1395 }
1396 }
1397 ASSERT_TRUE(found);
1398 new_class_loader.Assign(soa.Decode<mirror::ClassLoader>(LoadDex("VerifierDeps")));
Nicolas Geoffray6bb7f1b2016-11-03 10:52:49 +00001399 ASSERT_FALSE(decoded_deps.ValidateDependencies(new_class_loader, soa.Self()));
Nicolas Geoffray8904b6f2016-10-28 19:50:34 +01001400 }
1401
1402 {
1403 VerifierDeps decoded_deps(dex_files_, ArrayRef<const uint8_t>(buffer));
1404 VerifierDeps::DexFileDeps* deps = decoded_deps.GetDexFileDeps(*primary_dex_file_);
1405 bool found = false;
1406 std::set<VerifierDeps::MethodResolution>* methods = GetMethods(deps, resolution_kind);
1407 for (const auto& entry : *methods) {
Andreas Gampe8a0128a2016-11-28 07:38:35 -08001408 constexpr dex::StringIndex kNewTypeIndex(0);
Nicolas Geoffray8904b6f2016-10-28 19:50:34 +01001409 if (entry.IsResolved() && entry.GetDeclaringClassIndex() != kNewTypeIndex) {
1410 methods->insert(VerifierDeps::MethodResolution(entry.GetDexMethodIndex(),
1411 entry.GetAccessFlags(),
1412 kNewTypeIndex));
1413 found = true;
1414 break;
1415 }
1416 }
1417 ASSERT_TRUE(found);
1418 new_class_loader.Assign(soa.Decode<mirror::ClassLoader>(LoadDex("VerifierDeps")));
Nicolas Geoffray6bb7f1b2016-11-03 10:52:49 +00001419 ASSERT_FALSE(decoded_deps.ValidateDependencies(new_class_loader, soa.Self()));
Nicolas Geoffray8904b6f2016-10-28 19:50:34 +01001420 }
1421
Nicolas Geoffray865cf902017-01-18 14:34:48 +00001422 // The two tests below make sure that fiddling with the method kind
1423 // (static, virtual, interface) is detected by `ValidateDependencies`.
1424
1425 // An interface method lookup can succeed with a virtual method lookup on the same class.
1426 // That's OK, as we only want to make sure there is a method being defined with the right
1427 // flags. Therefore, polluting the interface methods with virtual methods does not have
1428 // to fail verification.
1429 if (resolution_kind != kVirtualMethodResolution) {
Nicolas Geoffray8904b6f2016-10-28 19:50:34 +01001430 VerifierDeps decoded_deps(dex_files_, ArrayRef<const uint8_t>(buffer));
1431 VerifierDeps::DexFileDeps* deps = decoded_deps.GetDexFileDeps(*primary_dex_file_);
1432 bool found = false;
1433 std::set<VerifierDeps::MethodResolution>* methods = GetMethods(deps, resolution_kind);
1434 for (const auto& entry : *methods) {
1435 if (entry.IsResolved()) {
1436 GetMethods(deps, GetNextResolutionKind(resolution_kind))->insert(
1437 VerifierDeps::MethodResolution(entry.GetDexMethodIndex(),
1438 entry.GetAccessFlags(),
1439 entry.GetDeclaringClassIndex()));
1440 found = true;
1441 }
1442 }
1443 ASSERT_TRUE(found);
1444 new_class_loader.Assign(soa.Decode<mirror::ClassLoader>(LoadDex("VerifierDeps")));
Nicolas Geoffray6bb7f1b2016-11-03 10:52:49 +00001445 ASSERT_FALSE(decoded_deps.ValidateDependencies(new_class_loader, soa.Self()));
Nicolas Geoffray8904b6f2016-10-28 19:50:34 +01001446 }
1447
Nicolas Geoffray865cf902017-01-18 14:34:48 +00001448 // See comment above that applies the same way.
1449 if (resolution_kind != kInterfaceMethodResolution) {
Nicolas Geoffray8904b6f2016-10-28 19:50:34 +01001450 VerifierDeps decoded_deps(dex_files_, ArrayRef<const uint8_t>(buffer));
1451 VerifierDeps::DexFileDeps* deps = decoded_deps.GetDexFileDeps(*primary_dex_file_);
1452 bool found = false;
1453 std::set<VerifierDeps::MethodResolution>* methods = GetMethods(deps, resolution_kind);
1454 for (const auto& entry : *methods) {
1455 if (entry.IsResolved()) {
1456 GetMethods(deps, GetNextResolutionKind(GetNextResolutionKind(resolution_kind)))->insert(
1457 VerifierDeps::MethodResolution(entry.GetDexMethodIndex(),
1458 entry.GetAccessFlags(),
1459 entry.GetDeclaringClassIndex()));
1460 found = true;
1461 }
1462 }
1463 ASSERT_TRUE(found);
1464 new_class_loader.Assign(soa.Decode<mirror::ClassLoader>(LoadDex("VerifierDeps")));
Nicolas Geoffray6bb7f1b2016-11-03 10:52:49 +00001465 ASSERT_FALSE(decoded_deps.ValidateDependencies(new_class_loader, soa.Self()));
1466 }
1467 }
1468}
1469
1470TEST_F(VerifierDepsTest, CompilerDriver) {
1471 SetupCompilerDriver();
1472
1473 // Test both multi-dex and single-dex configuration.
1474 for (const char* multi : { "MultiDex", static_cast<const char*>(nullptr) }) {
1475 // Test that the compiler driver behaves as expected when the dependencies
1476 // verify and when they don't verify.
1477 for (bool verify_failure : { false, true }) {
1478 {
1479 ScopedObjectAccess soa(Thread::Current());
1480 LoadDexFile(&soa, "VerifierDeps", multi);
1481 }
1482 VerifyWithCompilerDriver(/* verifier_deps */ nullptr);
1483
1484 std::vector<uint8_t> buffer;
1485 verifier_deps_->Encode(dex_files_, &buffer);
1486
1487 {
1488 ScopedObjectAccess soa(Thread::Current());
1489 LoadDexFile(&soa, "VerifierDeps", multi);
1490 }
1491 verifier::VerifierDeps decoded_deps(dex_files_, ArrayRef<const uint8_t>(buffer));
1492 if (verify_failure) {
1493 // Just taint the decoded VerifierDeps with one invalid entry.
1494 VerifierDeps::DexFileDeps* deps = decoded_deps.GetDexFileDeps(*primary_dex_file_);
1495 bool found = false;
1496 for (const auto& entry : deps->classes_) {
1497 if (entry.IsResolved()) {
1498 deps->classes_.insert(VerifierDeps::ClassResolution(
1499 entry.GetDexTypeIndex(), VerifierDeps::kUnresolvedMarker));
1500 found = true;
1501 break;
1502 }
1503 }
1504 ASSERT_TRUE(found);
1505 }
1506 VerifyWithCompilerDriver(&decoded_deps);
1507
1508 if (verify_failure) {
1509 ASSERT_FALSE(verifier_deps_ == nullptr);
1510 ASSERT_FALSE(verifier_deps_->Equals(decoded_deps));
1511 } else {
1512 ASSERT_TRUE(verifier_deps_ == nullptr);
1513 VerifyClassStatus(decoded_deps);
1514 }
Nicolas Geoffray8904b6f2016-10-28 19:50:34 +01001515 }
1516 }
1517}
1518
Nicolas Geoffray7cc3ae52017-03-07 14:33:37 +00001519TEST_F(VerifierDepsTest, MultiDexVerification) {
1520 VerifyDexFile("VerifierDepsMulti");
1521 ASSERT_EQ(NumberOfCompiledDexFiles(), 2u);
1522
1523 ASSERT_TRUE(HasUnverifiedClass("LMySoftVerificationFailure;", *dex_files_[1]));
1524 ASSERT_TRUE(HasUnverifiedClass("LMySub1SoftVerificationFailure;", *dex_files_[0]));
1525 ASSERT_TRUE(HasUnverifiedClass("LMySub2SoftVerificationFailure;", *dex_files_[0]));
1526
1527 std::vector<uint8_t> buffer;
1528 verifier_deps_->Encode(dex_files_, &buffer);
1529 ASSERT_FALSE(buffer.empty());
1530}
1531
Nicolas Geoffrayfc38e912017-03-16 16:51:59 +00001532TEST_F(VerifierDepsTest, NotAssignable_InterfaceWithClassInBoot) {
1533 ASSERT_TRUE(TestAssignabilityRecording(/* dst */ "Ljava/lang/Exception;",
1534 /* src */ "LIface;",
1535 /* is_strict */ true,
1536 /* is_assignable */ false));
1537 ASSERT_TRUE(HasAssignable("Ljava/lang/Exception;", "LIface;", false));
1538}
1539
Nicolas Geoffraybdb540d2017-04-19 13:50:34 +01001540TEST_F(VerifierDepsTest, Assignable_Arrays) {
1541 ASSERT_TRUE(TestAssignabilityRecording(/* dst */ "[LIface;",
1542 /* src */ "[LMyClassExtendingInterface;",
1543 /* is_strict */ false,
1544 /* is_assignable */ true));
1545 ASSERT_FALSE(HasAssignable(
1546 "LIface;", "LMyClassExtendingInterface;", /* expected_is_assignable */ true));
1547 ASSERT_FALSE(HasAssignable(
1548 "LIface;", "LMyClassExtendingInterface;", /* expected_is_assignable */ false));
1549}
1550
David Brazdilca3c8c32016-09-06 14:04:48 +01001551} // namespace verifier
1552} // namespace art