blob: 4d55eb08b2b21da50fe401785442354a774a69e7 [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"
Andreas Gampe6d7abbd2017-04-24 13:19:09 -070031#include "indenter.h"
David Brazdilca3c8c32016-09-06 14:04:48 +010032#include "mirror/class_loader.h"
33#include "runtime.h"
Mathieu Chartier0795f232016-09-27 18:43:30 -070034#include "scoped_thread_state_change-inl.h"
Andreas Gampe6d7abbd2017-04-24 13:19:09 -070035#include "thread.h"
Andreas Gamped9911ee2017-03-27 13:27:24 -070036#include "utils/atomic_method_ref_map-inl.h"
Andreas Gampe6d7abbd2017-04-24 13:19:09 -070037#include "verifier/method_verifier-inl.h"
David Brazdilca3c8c32016-09-06 14:04:48 +010038
39namespace art {
40namespace verifier {
41
42class VerifierDepsCompilerCallbacks : public CompilerCallbacks {
43 public:
44 explicit VerifierDepsCompilerCallbacks()
45 : CompilerCallbacks(CompilerCallbacks::CallbackMode::kCompileApp),
46 deps_(nullptr) {}
47
48 void MethodVerified(verifier::MethodVerifier* verifier ATTRIBUTE_UNUSED) OVERRIDE {}
49 void ClassRejected(ClassReference ref ATTRIBUTE_UNUSED) OVERRIDE {}
50 bool IsRelocationPossible() OVERRIDE { return false; }
51
52 verifier::VerifierDeps* GetVerifierDeps() const OVERRIDE { return deps_; }
53 void SetVerifierDeps(verifier::VerifierDeps* deps) { deps_ = deps; }
54
55 private:
56 verifier::VerifierDeps* deps_;
57};
58
Nicolas Geoffray08025182016-10-25 17:20:18 +010059class VerifierDepsTest : public CommonCompilerTest {
David Brazdilca3c8c32016-09-06 14:04:48 +010060 public:
61 void SetUpRuntimeOptions(RuntimeOptions* options) {
Nicolas Geoffray08025182016-10-25 17:20:18 +010062 CommonCompilerTest::SetUpRuntimeOptions(options);
David Brazdilca3c8c32016-09-06 14:04:48 +010063 callbacks_.reset(new VerifierDepsCompilerCallbacks());
64 }
65
66 mirror::Class* FindClassByName(const std::string& name, ScopedObjectAccess* soa)
67 REQUIRES_SHARED(Locks::mutator_lock_) {
68 StackHandleScope<1> hs(Thread::Current());
69 Handle<mirror::ClassLoader> class_loader_handle(
Mathieu Chartier0795f232016-09-27 18:43:30 -070070 hs.NewHandle(soa->Decode<mirror::ClassLoader>(class_loader_)));
David Brazdil6f82fbd2016-09-14 11:55:26 +010071 mirror::Class* klass = class_linker_->FindClass(Thread::Current(),
72 name.c_str(),
73 class_loader_handle);
74 if (klass == nullptr) {
75 DCHECK(Thread::Current()->IsExceptionPending());
76 Thread::Current()->ClearException();
77 }
78 return klass;
79 }
80
Nicolas Geoffray6bb7f1b2016-11-03 10:52:49 +000081 void SetupCompilerDriver() {
82 compiler_options_->boot_image_ = false;
83 compiler_driver_->InitializeThreadPools();
84 }
85
86 void VerifyWithCompilerDriver(verifier::VerifierDeps* deps) {
87 TimingLogger timings("Verify", false, false);
88 // The compiler driver handles the verifier deps in the callbacks, so
89 // remove what this class did for unit testing.
90 verifier_deps_.reset(nullptr);
Nicolas Geoffrayb0bbe8e2016-11-19 10:42:37 +000091 callbacks_->SetVerifierDeps(deps);
92 compiler_driver_->Verify(class_loader_, dex_files_, &timings);
Nicolas Geoffray6bb7f1b2016-11-03 10:52:49 +000093 // The compiler driver may have updated the VerifierDeps in the callback object.
Nicolas Geoffrayb0bbe8e2016-11-19 10:42:37 +000094 if (callbacks_->GetVerifierDeps() != deps) {
95 verifier_deps_.reset(callbacks_->GetVerifierDeps());
96 }
97 callbacks_->SetVerifierDeps(nullptr);
Nicolas Geoffray1d0ae3f2016-12-06 13:40:16 +000098 // Clear entries in the verification results to avoid hitting a DCHECK that
99 // we always succeed inserting a new entry after verifying.
100 AtomicMethodRefMap<const VerifiedMethod*>* map =
101 &compiler_driver_->GetVerificationResults()->atomic_verified_methods_;
102 map->Visit([](const MethodReference& ref ATTRIBUTE_UNUSED, const VerifiedMethod* method) {
103 delete method;
104 });
105 map->ClearEntries();
Nicolas Geoffray6bb7f1b2016-11-03 10:52:49 +0000106 }
107
David Brazdil6f82fbd2016-09-14 11:55:26 +0100108 void SetVerifierDeps(const std::vector<const DexFile*>& dex_files) {
109 verifier_deps_.reset(new verifier::VerifierDeps(dex_files));
110 VerifierDepsCompilerCallbacks* callbacks =
111 reinterpret_cast<VerifierDepsCompilerCallbacks*>(callbacks_.get());
112 callbacks->SetVerifierDeps(verifier_deps_.get());
David Brazdilca3c8c32016-09-06 14:04:48 +0100113 }
114
Nicolas Geoffrayd01f60c2016-10-28 14:45:48 +0100115 void LoadDexFile(ScopedObjectAccess* soa, const char* name1, const char* name2 = nullptr)
116 REQUIRES_SHARED(Locks::mutator_lock_) {
117 class_loader_ = (name2 == nullptr) ? LoadDex(name1) : LoadMultiDex(name1, name2);
118 dex_files_ = GetDexFiles(class_loader_);
119 primary_dex_file_ = dex_files_.front();
120
121 SetVerifierDeps(dex_files_);
122 StackHandleScope<1> hs(soa->Self());
123 Handle<mirror::ClassLoader> loader =
124 hs.NewHandle(soa->Decode<mirror::ClassLoader>(class_loader_));
125 for (const DexFile* dex_file : dex_files_) {
126 class_linker_->RegisterDexFile(*dex_file, loader.Get());
127 }
Nicolas Geoffray1d0ae3f2016-12-06 13:40:16 +0000128 for (const DexFile* dex_file : dex_files_) {
129 compiler_driver_->GetVerificationResults()->AddDexFile(dex_file);
130 }
Nicolas Geoffrayd01f60c2016-10-28 14:45:48 +0100131 }
132
David Brazdilca3c8c32016-09-06 14:04:48 +0100133 void LoadDexFile(ScopedObjectAccess* soa) REQUIRES_SHARED(Locks::mutator_lock_) {
Nicolas Geoffrayd01f60c2016-10-28 14:45:48 +0100134 LoadDexFile(soa, "VerifierDeps");
135 CHECK_EQ(dex_files_.size(), 1u);
David Brazdilca3c8c32016-09-06 14:04:48 +0100136 klass_Main_ = FindClassByName("LMain;", soa);
137 CHECK(klass_Main_ != nullptr);
David Brazdilca3c8c32016-09-06 14:04:48 +0100138 }
139
140 bool VerifyMethod(const std::string& method_name) {
141 ScopedObjectAccess soa(Thread::Current());
142 LoadDexFile(&soa);
143
Nicolas Geoffrayd01f60c2016-10-28 14:45:48 +0100144 StackHandleScope<2> hs(soa.Self());
David Brazdilca3c8c32016-09-06 14:04:48 +0100145 Handle<mirror::ClassLoader> class_loader_handle(
Mathieu Chartier0795f232016-09-27 18:43:30 -0700146 hs.NewHandle(soa.Decode<mirror::ClassLoader>(class_loader_)));
David Brazdilca3c8c32016-09-06 14:04:48 +0100147 Handle<mirror::DexCache> dex_cache_handle(hs.NewHandle(klass_Main_->GetDexCache()));
148
149 const DexFile::ClassDef* class_def = klass_Main_->GetClassDef();
Nicolas Geoffrayd01f60c2016-10-28 14:45:48 +0100150 const uint8_t* class_data = primary_dex_file_->GetClassData(*class_def);
David Brazdilca3c8c32016-09-06 14:04:48 +0100151 CHECK(class_data != nullptr);
152
Nicolas Geoffrayd01f60c2016-10-28 14:45:48 +0100153 ClassDataItemIterator it(*primary_dex_file_, class_data);
David Brazdilca3c8c32016-09-06 14:04:48 +0100154 while (it.HasNextStaticField() || it.HasNextInstanceField()) {
155 it.Next();
156 }
157
158 ArtMethod* method = nullptr;
159 while (it.HasNextDirectMethod()) {
160 ArtMethod* resolved_method = class_linker_->ResolveMethod<ClassLinker::kNoICCECheckForCache>(
Nicolas Geoffrayd01f60c2016-10-28 14:45:48 +0100161 *primary_dex_file_,
David Brazdilca3c8c32016-09-06 14:04:48 +0100162 it.GetMemberIndex(),
163 dex_cache_handle,
164 class_loader_handle,
165 nullptr,
166 it.GetMethodInvokeType(*class_def));
167 CHECK(resolved_method != nullptr);
168 if (method_name == resolved_method->GetName()) {
169 method = resolved_method;
170 break;
171 }
172 it.Next();
173 }
174 CHECK(method != nullptr);
175
Nicolas Geoffray340dafa2016-11-18 16:03:10 +0000176 Thread::Current()->SetVerifierDeps(callbacks_->GetVerifierDeps());
David Brazdilca3c8c32016-09-06 14:04:48 +0100177 MethodVerifier verifier(Thread::Current(),
Nicolas Geoffrayd01f60c2016-10-28 14:45:48 +0100178 primary_dex_file_,
David Brazdilca3c8c32016-09-06 14:04:48 +0100179 dex_cache_handle,
180 class_loader_handle,
181 *class_def,
182 it.GetMethodCodeItem(),
183 it.GetMemberIndex(),
184 method,
185 it.GetMethodAccessFlags(),
186 true /* can_load_classes */,
187 true /* allow_soft_failures */,
188 true /* need_precise_constants */,
189 false /* verify to dump */,
190 true /* allow_thread_suspension */);
191 verifier.Verify();
Nicolas Geoffray340dafa2016-11-18 16:03:10 +0000192 Thread::Current()->SetVerifierDeps(nullptr);
David Brazdilca3c8c32016-09-06 14:04:48 +0100193 return !verifier.HasFailures();
194 }
195
Nicolas Geoffrayd01f60c2016-10-28 14:45:48 +0100196 void VerifyDexFile(const char* multidex = nullptr) {
Nicolas Geoffray08025182016-10-25 17:20:18 +0100197 {
198 ScopedObjectAccess soa(Thread::Current());
Nicolas Geoffrayd01f60c2016-10-28 14:45:48 +0100199 LoadDexFile(&soa, "VerifierDeps", multidex);
David Brazdil6f82fbd2016-09-14 11:55:26 +0100200 }
Nicolas Geoffray6bb7f1b2016-11-03 10:52:49 +0000201 SetupCompilerDriver();
202 VerifyWithCompilerDriver(/* verifier_deps */ nullptr);
David Brazdil6f82fbd2016-09-14 11:55:26 +0100203 }
204
David Brazdilca3c8c32016-09-06 14:04:48 +0100205 bool TestAssignabilityRecording(const std::string& dst,
206 const std::string& src,
207 bool is_strict,
208 bool is_assignable) {
209 ScopedObjectAccess soa(Thread::Current());
210 LoadDexFile(&soa);
David Brazdil6f82fbd2016-09-14 11:55:26 +0100211 mirror::Class* klass_dst = FindClassByName(dst, &soa);
Nicolas Geoffraybdb540d2017-04-19 13:50:34 +0100212 DCHECK(klass_dst != nullptr) << dst;
David Brazdil6f82fbd2016-09-14 11:55:26 +0100213 mirror::Class* klass_src = FindClassByName(src, &soa);
Nicolas Geoffraybdb540d2017-04-19 13:50:34 +0100214 DCHECK(klass_src != nullptr) << src;
Nicolas Geoffrayd01f60c2016-10-28 14:45:48 +0100215 verifier_deps_->AddAssignability(*primary_dex_file_,
David Brazdil6f82fbd2016-09-14 11:55:26 +0100216 klass_dst,
217 klass_src,
David Brazdilca3c8c32016-09-06 14:04:48 +0100218 is_strict,
219 is_assignable);
220 return true;
221 }
222
Nicolas Geoffray6bb7f1b2016-11-03 10:52:49 +0000223 // Check that the status of classes in `class_loader_` match the
224 // expected status in `deps`.
225 void VerifyClassStatus(const verifier::VerifierDeps& deps) {
226 ScopedObjectAccess soa(Thread::Current());
227 StackHandleScope<2> hs(soa.Self());
228 Handle<mirror::ClassLoader> class_loader_handle(
229 hs.NewHandle(soa.Decode<mirror::ClassLoader>(class_loader_)));
230 MutableHandle<mirror::Class> cls(hs.NewHandle<mirror::Class>(nullptr));
231 for (const DexFile* dex_file : dex_files_) {
Andreas Gampea5b09a62016-11-17 15:21:22 -0800232 const std::vector<dex::TypeIndex>& unverified_classes = deps.GetUnverifiedClasses(*dex_file);
233 std::set<dex::TypeIndex> set(unverified_classes.begin(), unverified_classes.end());
Nicolas Geoffray6bb7f1b2016-11-03 10:52:49 +0000234 for (uint32_t i = 0; i < dex_file->NumClassDefs(); ++i) {
235 const DexFile::ClassDef& class_def = dex_file->GetClassDef(i);
236 const char* descriptor = dex_file->GetClassDescriptor(class_def);
237 cls.Assign(class_linker_->FindClass(soa.Self(), descriptor, class_loader_handle));
Andreas Gampefa4333d2017-02-14 11:10:34 -0800238 if (cls == nullptr) {
Nicolas Geoffray6bb7f1b2016-11-03 10:52:49 +0000239 CHECK(soa.Self()->IsExceptionPending());
240 soa.Self()->ClearException();
241 } else if (set.find(class_def.class_idx_) == set.end()) {
242 ASSERT_EQ(cls->GetStatus(), mirror::Class::kStatusVerified);
243 } else {
244 ASSERT_LT(cls->GetStatus(), mirror::Class::kStatusVerified);
245 }
246 }
247 }
248 }
249
Nicolas Geoffray08025182016-10-25 17:20:18 +0100250 bool HasUnverifiedClass(const std::string& cls) {
Nicolas Geoffray7cc3ae52017-03-07 14:33:37 +0000251 return HasUnverifiedClass(cls, *primary_dex_file_);
252 }
253
254 bool HasUnverifiedClass(const std::string& cls, const DexFile& dex_file) {
255 const DexFile::TypeId* type_id = dex_file.FindTypeId(cls.c_str());
Nicolas Geoffray08025182016-10-25 17:20:18 +0100256 DCHECK(type_id != nullptr);
Nicolas Geoffray7cc3ae52017-03-07 14:33:37 +0000257 dex::TypeIndex index = dex_file.GetIndexForTypeId(*type_id);
Nicolas Geoffray08025182016-10-25 17:20:18 +0100258 for (const auto& dex_dep : verifier_deps_->dex_deps_) {
Andreas Gampea5b09a62016-11-17 15:21:22 -0800259 for (dex::TypeIndex entry : dex_dep.second->unverified_classes_) {
Nicolas Geoffray08025182016-10-25 17:20:18 +0100260 if (index == entry) {
261 return true;
262 }
263 }
264 }
265 return false;
266 }
267
David Brazdilca3c8c32016-09-06 14:04:48 +0100268 // Iterates over all assignability records and tries to find an entry which
269 // matches the expected destination/source pair.
270 bool HasAssignable(const std::string& expected_destination,
271 const std::string& expected_source,
272 bool expected_is_assignable) {
David Brazdilca3c8c32016-09-06 14:04:48 +0100273 for (auto& dex_dep : verifier_deps_->dex_deps_) {
274 const DexFile& dex_file = *dex_dep.first;
275 auto& storage = expected_is_assignable ? dex_dep.second->assignable_types_
276 : dex_dep.second->unassignable_types_;
277 for (auto& entry : storage) {
278 std::string actual_destination =
279 verifier_deps_->GetStringFromId(dex_file, entry.GetDestination());
280 std::string actual_source = verifier_deps_->GetStringFromId(dex_file, entry.GetSource());
281 if ((expected_destination == actual_destination) && (expected_source == actual_source)) {
282 return true;
283 }
284 }
285 }
286 return false;
287 }
288
289 // Iterates over all class resolution records, finds an entry which matches
290 // the given class descriptor and tests its properties.
291 bool HasClass(const std::string& expected_klass,
292 bool expected_resolved,
293 const std::string& expected_access_flags = "") {
David Brazdilca3c8c32016-09-06 14:04:48 +0100294 for (auto& dex_dep : verifier_deps_->dex_deps_) {
295 for (auto& entry : dex_dep.second->classes_) {
296 if (expected_resolved != entry.IsResolved()) {
297 continue;
298 }
299
300 std::string actual_klass = dex_dep.first->StringByTypeIdx(entry.GetDexTypeIndex());
301 if (expected_klass != actual_klass) {
302 continue;
303 }
304
305 if (expected_resolved) {
306 // Test access flags. Note that PrettyJavaAccessFlags always appends
307 // a space after the modifiers. Add it to the expected access flags.
308 std::string actual_access_flags = PrettyJavaAccessFlags(entry.GetAccessFlags());
309 if (expected_access_flags + " " != actual_access_flags) {
310 continue;
311 }
312 }
313
314 return true;
315 }
316 }
317 return false;
318 }
319
320 // Iterates over all field resolution records, finds an entry which matches
321 // the given field class+name+type and tests its properties.
322 bool HasField(const std::string& expected_klass,
323 const std::string& expected_name,
324 const std::string& expected_type,
325 bool expected_resolved,
326 const std::string& expected_access_flags = "",
327 const std::string& expected_decl_klass = "") {
David Brazdilca3c8c32016-09-06 14:04:48 +0100328 for (auto& dex_dep : verifier_deps_->dex_deps_) {
329 for (auto& entry : dex_dep.second->fields_) {
330 if (expected_resolved != entry.IsResolved()) {
331 continue;
332 }
333
334 const DexFile::FieldId& field_id = dex_dep.first->GetFieldId(entry.GetDexFieldIndex());
335
336 std::string actual_klass = dex_dep.first->StringByTypeIdx(field_id.class_idx_);
337 if (expected_klass != actual_klass) {
338 continue;
339 }
340
341 std::string actual_name = dex_dep.first->StringDataByIdx(field_id.name_idx_);
342 if (expected_name != actual_name) {
343 continue;
344 }
345
346 std::string actual_type = dex_dep.first->StringByTypeIdx(field_id.type_idx_);
347 if (expected_type != actual_type) {
348 continue;
349 }
350
351 if (expected_resolved) {
352 // Test access flags. Note that PrettyJavaAccessFlags always appends
353 // a space after the modifiers. Add it to the expected access flags.
354 std::string actual_access_flags = PrettyJavaAccessFlags(entry.GetAccessFlags());
355 if (expected_access_flags + " " != actual_access_flags) {
356 continue;
357 }
358
359 std::string actual_decl_klass = verifier_deps_->GetStringFromId(
360 *dex_dep.first, entry.GetDeclaringClassIndex());
361 if (expected_decl_klass != actual_decl_klass) {
362 continue;
363 }
364 }
365
366 return true;
367 }
368 }
369 return false;
370 }
371
372 // Iterates over all method resolution records, finds an entry which matches
373 // the given field kind+class+name+signature and tests its properties.
374 bool HasMethod(const std::string& expected_kind,
375 const std::string& expected_klass,
376 const std::string& expected_name,
377 const std::string& expected_signature,
378 bool expected_resolved,
379 const std::string& expected_access_flags = "",
380 const std::string& expected_decl_klass = "") {
David Brazdilca3c8c32016-09-06 14:04:48 +0100381 for (auto& dex_dep : verifier_deps_->dex_deps_) {
382 auto& storage = (expected_kind == "direct") ? dex_dep.second->direct_methods_
383 : (expected_kind == "virtual") ? dex_dep.second->virtual_methods_
384 : dex_dep.second->interface_methods_;
385 for (auto& entry : storage) {
386 if (expected_resolved != entry.IsResolved()) {
387 continue;
388 }
389
390 const DexFile::MethodId& method_id = dex_dep.first->GetMethodId(entry.GetDexMethodIndex());
391
392 std::string actual_klass = dex_dep.first->StringByTypeIdx(method_id.class_idx_);
393 if (expected_klass != actual_klass) {
394 continue;
395 }
396
397 std::string actual_name = dex_dep.first->StringDataByIdx(method_id.name_idx_);
398 if (expected_name != actual_name) {
399 continue;
400 }
401
402 std::string actual_signature = dex_dep.first->GetMethodSignature(method_id).ToString();
403 if (expected_signature != actual_signature) {
404 continue;
405 }
406
407 if (expected_resolved) {
408 // Test access flags. Note that PrettyJavaAccessFlags always appends
409 // a space after the modifiers. Add it to the expected access flags.
410 std::string actual_access_flags = PrettyJavaAccessFlags(entry.GetAccessFlags());
411 if (expected_access_flags + " " != actual_access_flags) {
412 continue;
413 }
414
415 std::string actual_decl_klass = verifier_deps_->GetStringFromId(
416 *dex_dep.first, entry.GetDeclaringClassIndex());
417 if (expected_decl_klass != actual_decl_klass) {
418 continue;
419 }
420 }
421
422 return true;
423 }
424 }
425 return false;
426 }
427
David Brazdil6f82fbd2016-09-14 11:55:26 +0100428 size_t NumberOfCompiledDexFiles() {
David Brazdil6f82fbd2016-09-14 11:55:26 +0100429 return verifier_deps_->dex_deps_.size();
430 }
431
432 size_t HasEachKindOfRecord() {
David Brazdil6f82fbd2016-09-14 11:55:26 +0100433 bool has_strings = false;
434 bool has_assignability = false;
435 bool has_classes = false;
436 bool has_fields = false;
437 bool has_methods = false;
Nicolas Geoffray08025182016-10-25 17:20:18 +0100438 bool has_unverified_classes = false;
David Brazdil6f82fbd2016-09-14 11:55:26 +0100439
440 for (auto& entry : verifier_deps_->dex_deps_) {
441 has_strings |= !entry.second->strings_.empty();
442 has_assignability |= !entry.second->assignable_types_.empty();
443 has_assignability |= !entry.second->unassignable_types_.empty();
444 has_classes |= !entry.second->classes_.empty();
445 has_fields |= !entry.second->fields_.empty();
446 has_methods |= !entry.second->direct_methods_.empty();
447 has_methods |= !entry.second->virtual_methods_.empty();
448 has_methods |= !entry.second->interface_methods_.empty();
Nicolas Geoffray08025182016-10-25 17:20:18 +0100449 has_unverified_classes |= !entry.second->unverified_classes_.empty();
David Brazdil6f82fbd2016-09-14 11:55:26 +0100450 }
451
Nicolas Geoffray08025182016-10-25 17:20:18 +0100452 return has_strings &&
453 has_assignability &&
454 has_classes &&
455 has_fields &&
456 has_methods &&
457 has_unverified_classes;
David Brazdil6f82fbd2016-09-14 11:55:26 +0100458 }
459
Nicolas Geoffray8904b6f2016-10-28 19:50:34 +0100460 static std::set<VerifierDeps::MethodResolution>* GetMethods(
461 VerifierDeps::DexFileDeps* deps, MethodResolutionKind resolution_kind) {
462 if (resolution_kind == kDirectMethodResolution) {
463 return &deps->direct_methods_;
464 } else if (resolution_kind == kVirtualMethodResolution) {
465 return &deps->virtual_methods_;
466 } else {
467 DCHECK_EQ(resolution_kind, kInterfaceMethodResolution);
468 return &deps->interface_methods_;
469 }
470 }
471
David Brazdilca3c8c32016-09-06 14:04:48 +0100472 std::unique_ptr<verifier::VerifierDeps> verifier_deps_;
Nicolas Geoffrayd01f60c2016-10-28 14:45:48 +0100473 std::vector<const DexFile*> dex_files_;
474 const DexFile* primary_dex_file_;
David Brazdilca3c8c32016-09-06 14:04:48 +0100475 jobject class_loader_;
476 mirror::Class* klass_Main_;
477};
478
479TEST_F(VerifierDepsTest, StringToId) {
480 ScopedObjectAccess soa(Thread::Current());
481 LoadDexFile(&soa);
482
Andreas Gampe8a0128a2016-11-28 07:38:35 -0800483 dex::StringIndex id_Main1 = verifier_deps_->GetIdFromString(*primary_dex_file_, "LMain;");
484 ASSERT_LT(id_Main1.index_, primary_dex_file_->NumStringIds());
Nicolas Geoffrayd01f60c2016-10-28 14:45:48 +0100485 ASSERT_EQ("LMain;", verifier_deps_->GetStringFromId(*primary_dex_file_, id_Main1));
David Brazdilca3c8c32016-09-06 14:04:48 +0100486
Andreas Gampe8a0128a2016-11-28 07:38:35 -0800487 dex::StringIndex id_Main2 = verifier_deps_->GetIdFromString(*primary_dex_file_, "LMain;");
488 ASSERT_LT(id_Main2.index_, primary_dex_file_->NumStringIds());
Nicolas Geoffrayd01f60c2016-10-28 14:45:48 +0100489 ASSERT_EQ("LMain;", verifier_deps_->GetStringFromId(*primary_dex_file_, id_Main2));
David Brazdilca3c8c32016-09-06 14:04:48 +0100490
Andreas Gampe8a0128a2016-11-28 07:38:35 -0800491 dex::StringIndex id_Lorem1 = verifier_deps_->GetIdFromString(*primary_dex_file_, "Lorem ipsum");
492 ASSERT_GE(id_Lorem1.index_, primary_dex_file_->NumStringIds());
Nicolas Geoffrayd01f60c2016-10-28 14:45:48 +0100493 ASSERT_EQ("Lorem ipsum", verifier_deps_->GetStringFromId(*primary_dex_file_, id_Lorem1));
David Brazdilca3c8c32016-09-06 14:04:48 +0100494
Andreas Gampe8a0128a2016-11-28 07:38:35 -0800495 dex::StringIndex id_Lorem2 = verifier_deps_->GetIdFromString(*primary_dex_file_, "Lorem ipsum");
496 ASSERT_GE(id_Lorem2.index_, primary_dex_file_->NumStringIds());
Nicolas Geoffrayd01f60c2016-10-28 14:45:48 +0100497 ASSERT_EQ("Lorem ipsum", verifier_deps_->GetStringFromId(*primary_dex_file_, id_Lorem2));
David Brazdilca3c8c32016-09-06 14:04:48 +0100498
499 ASSERT_EQ(id_Main1, id_Main2);
500 ASSERT_EQ(id_Lorem1, id_Lorem2);
501 ASSERT_NE(id_Main1, id_Lorem1);
502}
503
504TEST_F(VerifierDepsTest, Assignable_BothInBoot) {
505 ASSERT_TRUE(TestAssignabilityRecording(/* dst */ "Ljava/util/TimeZone;",
506 /* src */ "Ljava/util/SimpleTimeZone;",
507 /* is_strict */ true,
508 /* is_assignable */ true));
509 ASSERT_TRUE(HasAssignable("Ljava/util/TimeZone;", "Ljava/util/SimpleTimeZone;", true));
510}
511
512TEST_F(VerifierDepsTest, Assignable_DestinationInBoot1) {
513 ASSERT_TRUE(TestAssignabilityRecording(/* dst */ "Ljava/net/Socket;",
514 /* src */ "LMySSLSocket;",
515 /* is_strict */ true,
516 /* is_assignable */ true));
Nicolas Geoffray119e8462016-12-21 10:29:43 +0000517 ASSERT_TRUE(HasAssignable("Ljava/net/Socket;", "Ljavax/net/ssl/SSLSocket;", true));
David Brazdilca3c8c32016-09-06 14:04:48 +0100518}
519
520TEST_F(VerifierDepsTest, Assignable_DestinationInBoot2) {
521 ASSERT_TRUE(TestAssignabilityRecording(/* dst */ "Ljava/util/TimeZone;",
522 /* src */ "LMySimpleTimeZone;",
523 /* is_strict */ true,
524 /* is_assignable */ true));
Nicolas Geoffray119e8462016-12-21 10:29:43 +0000525 ASSERT_TRUE(HasAssignable("Ljava/util/TimeZone;", "Ljava/util/SimpleTimeZone;", true));
David Brazdilca3c8c32016-09-06 14:04:48 +0100526}
527
528TEST_F(VerifierDepsTest, Assignable_DestinationInBoot3) {
529 ASSERT_TRUE(TestAssignabilityRecording(/* dst */ "Ljava/util/Collection;",
530 /* src */ "LMyThreadSet;",
531 /* is_strict */ true,
532 /* is_assignable */ true));
Nicolas Geoffray0e2fe0f2016-12-21 16:54:52 +0000533 ASSERT_TRUE(HasAssignable("Ljava/util/Collection;", "Ljava/util/Set;", true));
David Brazdilca3c8c32016-09-06 14:04:48 +0100534}
535
536TEST_F(VerifierDepsTest, Assignable_BothArrays_Resolved) {
537 ASSERT_TRUE(TestAssignabilityRecording(/* dst */ "[[Ljava/util/TimeZone;",
538 /* src */ "[[Ljava/util/SimpleTimeZone;",
539 /* is_strict */ true,
540 /* is_assignable */ true));
541 // If the component types of both arrays are resolved, we optimize the list of
542 // dependencies by recording a dependency on the component types.
543 ASSERT_FALSE(HasAssignable("[[Ljava/util/TimeZone;", "[[Ljava/util/SimpleTimeZone;", true));
544 ASSERT_FALSE(HasAssignable("[Ljava/util/TimeZone;", "[Ljava/util/SimpleTimeZone;", true));
545 ASSERT_TRUE(HasAssignable("Ljava/util/TimeZone;", "Ljava/util/SimpleTimeZone;", true));
546}
547
David Brazdilca3c8c32016-09-06 14:04:48 +0100548TEST_F(VerifierDepsTest, NotAssignable_BothInBoot) {
549 ASSERT_TRUE(TestAssignabilityRecording(/* dst */ "Ljava/lang/Exception;",
550 /* src */ "Ljava/util/SimpleTimeZone;",
551 /* is_strict */ true,
552 /* is_assignable */ false));
553 ASSERT_TRUE(HasAssignable("Ljava/lang/Exception;", "Ljava/util/SimpleTimeZone;", false));
554}
555
556TEST_F(VerifierDepsTest, NotAssignable_DestinationInBoot1) {
557 ASSERT_TRUE(TestAssignabilityRecording(/* dst */ "Ljava/lang/Exception;",
558 /* src */ "LMySSLSocket;",
559 /* is_strict */ true,
560 /* is_assignable */ false));
Nicolas Geoffray119e8462016-12-21 10:29:43 +0000561 ASSERT_TRUE(HasAssignable("Ljava/lang/Exception;", "Ljavax/net/ssl/SSLSocket;", false));
David Brazdilca3c8c32016-09-06 14:04:48 +0100562}
563
564TEST_F(VerifierDepsTest, NotAssignable_DestinationInBoot2) {
565 ASSERT_TRUE(TestAssignabilityRecording(/* dst */ "Ljava/lang/Exception;",
566 /* src */ "LMySimpleTimeZone;",
567 /* is_strict */ true,
568 /* is_assignable */ false));
Nicolas Geoffray119e8462016-12-21 10:29:43 +0000569 ASSERT_TRUE(HasAssignable("Ljava/lang/Exception;", "Ljava/util/SimpleTimeZone;", false));
David Brazdilca3c8c32016-09-06 14:04:48 +0100570}
571
572TEST_F(VerifierDepsTest, NotAssignable_BothArrays) {
573 ASSERT_TRUE(TestAssignabilityRecording(/* dst */ "[Ljava/lang/Exception;",
574 /* src */ "[Ljava/util/SimpleTimeZone;",
575 /* is_strict */ true,
576 /* is_assignable */ false));
577 ASSERT_TRUE(HasAssignable("Ljava/lang/Exception;", "Ljava/util/SimpleTimeZone;", false));
578}
579
580TEST_F(VerifierDepsTest, ArgumentType_ResolvedClass) {
581 ASSERT_TRUE(VerifyMethod("ArgumentType_ResolvedClass"));
582 ASSERT_TRUE(HasClass("Ljava/lang/Thread;", true, "public"));
583}
584
David Brazdilca3c8c32016-09-06 14:04:48 +0100585TEST_F(VerifierDepsTest, ArgumentType_UnresolvedClass) {
586 ASSERT_TRUE(VerifyMethod("ArgumentType_UnresolvedClass"));
587 ASSERT_TRUE(HasClass("LUnresolvedClass;", false));
588}
589
590TEST_F(VerifierDepsTest, ArgumentType_UnresolvedSuper) {
591 ASSERT_TRUE(VerifyMethod("ArgumentType_UnresolvedSuper"));
592 ASSERT_TRUE(HasClass("LMySetWithUnresolvedSuper;", false));
593}
594
595TEST_F(VerifierDepsTest, ReturnType_Reference) {
596 ASSERT_TRUE(VerifyMethod("ReturnType_Reference"));
597 ASSERT_TRUE(HasAssignable("Ljava/lang/Throwable;", "Ljava/lang/IllegalStateException;", true));
598}
599
600TEST_F(VerifierDepsTest, ReturnType_Array) {
601 ASSERT_FALSE(VerifyMethod("ReturnType_Array"));
602 ASSERT_TRUE(HasAssignable("Ljava/lang/Integer;", "Ljava/lang/IllegalStateException;", false));
603}
604
605TEST_F(VerifierDepsTest, InvokeArgumentType) {
606 ASSERT_TRUE(VerifyMethod("InvokeArgumentType"));
607 ASSERT_TRUE(HasClass("Ljava/text/SimpleDateFormat;", true, "public"));
608 ASSERT_TRUE(HasClass("Ljava/util/SimpleTimeZone;", true, "public"));
609 ASSERT_TRUE(HasMethod("virtual",
610 "Ljava/text/SimpleDateFormat;",
611 "setTimeZone",
612 "(Ljava/util/TimeZone;)V",
613 true,
614 "public",
615 "Ljava/text/DateFormat;"));
616 ASSERT_TRUE(HasAssignable("Ljava/util/TimeZone;", "Ljava/util/SimpleTimeZone;", true));
617}
618
619TEST_F(VerifierDepsTest, MergeTypes_RegisterLines) {
620 ASSERT_TRUE(VerifyMethod("MergeTypes_RegisterLines"));
Nicolas Geoffray119e8462016-12-21 10:29:43 +0000621 ASSERT_TRUE(HasAssignable("Ljava/lang/Exception;", "Ljava/net/SocketTimeoutException;", true));
David Brazdilca3c8c32016-09-06 14:04:48 +0100622 ASSERT_TRUE(HasAssignable(
623 "Ljava/lang/Exception;", "Ljava/util/concurrent/TimeoutException;", true));
624}
625
626TEST_F(VerifierDepsTest, MergeTypes_IfInstanceOf) {
627 ASSERT_TRUE(VerifyMethod("MergeTypes_IfInstanceOf"));
628 ASSERT_TRUE(HasAssignable("Ljava/lang/Exception;", "Ljava/net/SocketTimeoutException;", true));
629 ASSERT_TRUE(HasAssignable(
630 "Ljava/lang/Exception;", "Ljava/util/concurrent/TimeoutException;", true));
631 ASSERT_TRUE(HasAssignable("Ljava/net/SocketTimeoutException;", "Ljava/lang/Exception;", false));
632}
633
634TEST_F(VerifierDepsTest, MergeTypes_Unresolved) {
635 ASSERT_TRUE(VerifyMethod("MergeTypes_Unresolved"));
636 ASSERT_TRUE(HasAssignable("Ljava/lang/Exception;", "Ljava/net/SocketTimeoutException;", true));
637 ASSERT_TRUE(HasAssignable(
638 "Ljava/lang/Exception;", "Ljava/util/concurrent/TimeoutException;", true));
639}
640
641TEST_F(VerifierDepsTest, ConstClass_Resolved) {
642 ASSERT_TRUE(VerifyMethod("ConstClass_Resolved"));
643 ASSERT_TRUE(HasClass("Ljava/lang/IllegalStateException;", true, "public"));
644}
645
646TEST_F(VerifierDepsTest, ConstClass_Unresolved) {
647 ASSERT_TRUE(VerifyMethod("ConstClass_Unresolved"));
648 ASSERT_TRUE(HasClass("LUnresolvedClass;", false));
649}
650
651TEST_F(VerifierDepsTest, CheckCast_Resolved) {
652 ASSERT_TRUE(VerifyMethod("CheckCast_Resolved"));
653 ASSERT_TRUE(HasClass("Ljava/lang/IllegalStateException;", true, "public"));
654}
655
656TEST_F(VerifierDepsTest, CheckCast_Unresolved) {
657 ASSERT_TRUE(VerifyMethod("CheckCast_Unresolved"));
658 ASSERT_TRUE(HasClass("LUnresolvedClass;", false));
659}
660
661TEST_F(VerifierDepsTest, InstanceOf_Resolved) {
662 ASSERT_TRUE(VerifyMethod("InstanceOf_Resolved"));
663 ASSERT_TRUE(HasClass("Ljava/lang/IllegalStateException;", true, "public"));
664}
665
666TEST_F(VerifierDepsTest, InstanceOf_Unresolved) {
667 ASSERT_TRUE(VerifyMethod("InstanceOf_Unresolved"));
668 ASSERT_TRUE(HasClass("LUnresolvedClass;", false));
669}
670
671TEST_F(VerifierDepsTest, NewInstance_Resolved) {
672 ASSERT_TRUE(VerifyMethod("NewInstance_Resolved"));
673 ASSERT_TRUE(HasClass("Ljava/lang/IllegalStateException;", true, "public"));
674}
675
676TEST_F(VerifierDepsTest, NewInstance_Unresolved) {
677 ASSERT_TRUE(VerifyMethod("NewInstance_Unresolved"));
678 ASSERT_TRUE(HasClass("LUnresolvedClass;", false));
679}
680
David Brazdilca3c8c32016-09-06 14:04:48 +0100681TEST_F(VerifierDepsTest, NewArray_Unresolved) {
682 ASSERT_TRUE(VerifyMethod("NewArray_Unresolved"));
683 ASSERT_TRUE(HasClass("[LUnresolvedClass;", false));
684}
685
686TEST_F(VerifierDepsTest, Throw) {
687 ASSERT_TRUE(VerifyMethod("Throw"));
688 ASSERT_TRUE(HasAssignable("Ljava/lang/Throwable;", "Ljava/lang/IllegalStateException;", true));
689}
690
691TEST_F(VerifierDepsTest, MoveException_Resolved) {
692 ASSERT_TRUE(VerifyMethod("MoveException_Resolved"));
693 ASSERT_TRUE(HasClass("Ljava/io/InterruptedIOException;", true, "public"));
694 ASSERT_TRUE(HasClass("Ljava/net/SocketTimeoutException;", true, "public"));
695 ASSERT_TRUE(HasClass("Ljava/util/zip/ZipException;", true, "public"));
696
697 // Testing that all exception types are assignable to Throwable.
698 ASSERT_TRUE(HasAssignable("Ljava/lang/Throwable;", "Ljava/io/InterruptedIOException;", true));
699 ASSERT_TRUE(HasAssignable("Ljava/lang/Throwable;", "Ljava/net/SocketTimeoutException;", true));
700 ASSERT_TRUE(HasAssignable("Ljava/lang/Throwable;", "Ljava/util/zip/ZipException;", true));
701
702 // Testing that the merge type is assignable to Throwable.
703 ASSERT_TRUE(HasAssignable("Ljava/lang/Throwable;", "Ljava/io/IOException;", true));
704
705 // Merging of exception types.
706 ASSERT_TRUE(HasAssignable("Ljava/io/IOException;", "Ljava/io/InterruptedIOException;", true));
707 ASSERT_TRUE(HasAssignable("Ljava/io/IOException;", "Ljava/util/zip/ZipException;", true));
708 ASSERT_TRUE(HasAssignable(
709 "Ljava/io/InterruptedIOException;", "Ljava/net/SocketTimeoutException;", true));
710}
711
712TEST_F(VerifierDepsTest, MoveException_Unresolved) {
713 ASSERT_FALSE(VerifyMethod("MoveException_Unresolved"));
714 ASSERT_TRUE(HasClass("LUnresolvedException;", false));
715}
716
717TEST_F(VerifierDepsTest, StaticField_Resolved_DeclaredInReferenced) {
718 ASSERT_TRUE(VerifyMethod("StaticField_Resolved_DeclaredInReferenced"));
Nicolas Geoffray6e54f782017-03-08 15:27:09 +0000719 ASSERT_TRUE(HasClass("Ljava/lang/System;", true, "public"));
David Brazdilca3c8c32016-09-06 14:04:48 +0100720 ASSERT_TRUE(HasField("Ljava/lang/System;",
721 "out",
722 "Ljava/io/PrintStream;",
723 true,
Nicolas Geoffray6e54f782017-03-08 15:27:09 +0000724 "public static",
David Brazdilca3c8c32016-09-06 14:04:48 +0100725 "Ljava/lang/System;"));
726}
727
728TEST_F(VerifierDepsTest, StaticField_Resolved_DeclaredInSuperclass1) {
729 ASSERT_TRUE(VerifyMethod("StaticField_Resolved_DeclaredInSuperclass1"));
730 ASSERT_TRUE(HasClass("Ljava/util/SimpleTimeZone;", true, "public"));
731 ASSERT_TRUE(HasField(
Nicolas Geoffray6e54f782017-03-08 15:27:09 +0000732 "Ljava/util/SimpleTimeZone;", "LONG", "I", true, "public static", "Ljava/util/TimeZone;"));
David Brazdilca3c8c32016-09-06 14:04:48 +0100733}
734
735TEST_F(VerifierDepsTest, StaticField_Resolved_DeclaredInSuperclass2) {
736 ASSERT_TRUE(VerifyMethod("StaticField_Resolved_DeclaredInSuperclass2"));
737 ASSERT_TRUE(HasField(
Nicolas Geoffray6e54f782017-03-08 15:27:09 +0000738 "LMySimpleTimeZone;", "SHORT", "I", true, "public static", "Ljava/util/TimeZone;"));
David Brazdilca3c8c32016-09-06 14:04:48 +0100739}
740
741TEST_F(VerifierDepsTest, StaticField_Resolved_DeclaredInInterface1) {
742 ASSERT_TRUE(VerifyMethod("StaticField_Resolved_DeclaredInInterface1"));
743 ASSERT_TRUE(HasClass("Ljavax/xml/transform/dom/DOMResult;", true, "public"));
744 ASSERT_TRUE(HasField("Ljavax/xml/transform/dom/DOMResult;",
745 "PI_ENABLE_OUTPUT_ESCAPING",
746 "Ljava/lang/String;",
747 true,
Nicolas Geoffray6e54f782017-03-08 15:27:09 +0000748 "public static",
David Brazdilca3c8c32016-09-06 14:04:48 +0100749 "Ljavax/xml/transform/Result;"));
750}
751
752TEST_F(VerifierDepsTest, StaticField_Resolved_DeclaredInInterface2) {
753 ASSERT_TRUE(VerifyMethod("StaticField_Resolved_DeclaredInInterface2"));
754 ASSERT_TRUE(HasField("LMyDOMResult;",
755 "PI_ENABLE_OUTPUT_ESCAPING",
756 "Ljava/lang/String;",
757 true,
Nicolas Geoffray6e54f782017-03-08 15:27:09 +0000758 "public static",
David Brazdilca3c8c32016-09-06 14:04:48 +0100759 "Ljavax/xml/transform/Result;"));
760}
761
762TEST_F(VerifierDepsTest, StaticField_Resolved_DeclaredInInterface3) {
763 ASSERT_TRUE(VerifyMethod("StaticField_Resolved_DeclaredInInterface3"));
764 ASSERT_TRUE(HasField("LMyResult;",
765 "PI_ENABLE_OUTPUT_ESCAPING",
766 "Ljava/lang/String;",
767 true,
Nicolas Geoffray6e54f782017-03-08 15:27:09 +0000768 "public static",
David Brazdilca3c8c32016-09-06 14:04:48 +0100769 "Ljavax/xml/transform/Result;"));
770}
771
772TEST_F(VerifierDepsTest, StaticField_Resolved_DeclaredInInterface4) {
773 ASSERT_TRUE(VerifyMethod("StaticField_Resolved_DeclaredInInterface4"));
774 ASSERT_TRUE(HasField("LMyDocument;",
775 "ELEMENT_NODE",
776 "S",
777 true,
Nicolas Geoffray6e54f782017-03-08 15:27:09 +0000778 "public static",
David Brazdilca3c8c32016-09-06 14:04:48 +0100779 "Lorg/w3c/dom/Node;"));
780}
781
782TEST_F(VerifierDepsTest, StaticField_Unresolved_ReferrerInBoot) {
783 ASSERT_TRUE(VerifyMethod("StaticField_Unresolved_ReferrerInBoot"));
Nicolas Geoffray6e54f782017-03-08 15:27:09 +0000784 ASSERT_TRUE(HasClass("Ljava/util/TimeZone;", true, "public"));
David Brazdilca3c8c32016-09-06 14:04:48 +0100785 ASSERT_TRUE(HasField("Ljava/util/TimeZone;", "x", "I", false));
786}
787
788TEST_F(VerifierDepsTest, StaticField_Unresolved_ReferrerInDex) {
789 ASSERT_TRUE(VerifyMethod("StaticField_Unresolved_ReferrerInDex"));
790 ASSERT_TRUE(HasField("LMyThreadSet;", "x", "I", false));
791}
792
793TEST_F(VerifierDepsTest, InstanceField_Resolved_DeclaredInReferenced) {
794 ASSERT_TRUE(VerifyMethod("InstanceField_Resolved_DeclaredInReferenced"));
795 ASSERT_TRUE(HasClass("Ljava/io/InterruptedIOException;", true, "public"));
796 ASSERT_TRUE(HasField("Ljava/io/InterruptedIOException;",
797 "bytesTransferred",
798 "I",
799 true,
800 "public",
801 "Ljava/io/InterruptedIOException;"));
802 ASSERT_TRUE(HasAssignable(
Nicolas Geoffray119e8462016-12-21 10:29:43 +0000803 "Ljava/io/InterruptedIOException;", "Ljava/net/SocketTimeoutException;", true));
David Brazdilca3c8c32016-09-06 14:04:48 +0100804}
805
806TEST_F(VerifierDepsTest, InstanceField_Resolved_DeclaredInSuperclass1) {
807 ASSERT_TRUE(VerifyMethod("InstanceField_Resolved_DeclaredInSuperclass1"));
808 ASSERT_TRUE(HasClass("Ljava/net/SocketTimeoutException;", true, "public"));
809 ASSERT_TRUE(HasField("Ljava/net/SocketTimeoutException;",
810 "bytesTransferred",
811 "I",
812 true,
813 "public",
814 "Ljava/io/InterruptedIOException;"));
815 ASSERT_TRUE(HasAssignable(
Nicolas Geoffray119e8462016-12-21 10:29:43 +0000816 "Ljava/io/InterruptedIOException;", "Ljava/net/SocketTimeoutException;", true));
David Brazdilca3c8c32016-09-06 14:04:48 +0100817}
818
819TEST_F(VerifierDepsTest, InstanceField_Resolved_DeclaredInSuperclass2) {
820 ASSERT_TRUE(VerifyMethod("InstanceField_Resolved_DeclaredInSuperclass2"));
821 ASSERT_TRUE(HasField("LMySocketTimeoutException;",
822 "bytesTransferred",
823 "I",
824 true,
825 "public",
826 "Ljava/io/InterruptedIOException;"));
827 ASSERT_TRUE(HasAssignable(
Nicolas Geoffray119e8462016-12-21 10:29:43 +0000828 "Ljava/io/InterruptedIOException;", "Ljava/net/SocketTimeoutException;", true));
David Brazdilca3c8c32016-09-06 14:04:48 +0100829}
830
831TEST_F(VerifierDepsTest, InstanceField_Unresolved_ReferrerInBoot) {
832 ASSERT_TRUE(VerifyMethod("InstanceField_Unresolved_ReferrerInBoot"));
833 ASSERT_TRUE(HasClass("Ljava/io/InterruptedIOException;", true, "public"));
834 ASSERT_TRUE(HasField("Ljava/io/InterruptedIOException;", "x", "I", false));
835}
836
837TEST_F(VerifierDepsTest, InstanceField_Unresolved_ReferrerInDex) {
838 ASSERT_TRUE(VerifyMethod("InstanceField_Unresolved_ReferrerInDex"));
839 ASSERT_TRUE(HasField("LMyThreadSet;", "x", "I", false));
840}
841
842TEST_F(VerifierDepsTest, InvokeStatic_Resolved_DeclaredInReferenced) {
843 ASSERT_TRUE(VerifyMethod("InvokeStatic_Resolved_DeclaredInReferenced"));
844 ASSERT_TRUE(HasClass("Ljava/net/Socket;", true, "public"));
845 ASSERT_TRUE(HasMethod("direct",
846 "Ljava/net/Socket;",
847 "setSocketImplFactory",
848 "(Ljava/net/SocketImplFactory;)V",
849 true,
850 "public static",
851 "Ljava/net/Socket;"));
852}
853
854TEST_F(VerifierDepsTest, InvokeStatic_Resolved_DeclaredInSuperclass1) {
855 ASSERT_TRUE(VerifyMethod("InvokeStatic_Resolved_DeclaredInSuperclass1"));
Nicolas Geoffray6e54f782017-03-08 15:27:09 +0000856 ASSERT_TRUE(HasClass("Ljavax/net/ssl/SSLSocket;", true, "public"));
David Brazdilca3c8c32016-09-06 14:04:48 +0100857 ASSERT_TRUE(HasMethod("direct",
858 "Ljavax/net/ssl/SSLSocket;",
859 "setSocketImplFactory",
860 "(Ljava/net/SocketImplFactory;)V",
861 true,
862 "public static",
863 "Ljava/net/Socket;"));
864}
865
866TEST_F(VerifierDepsTest, InvokeStatic_Resolved_DeclaredInSuperclass2) {
867 ASSERT_TRUE(VerifyMethod("InvokeStatic_Resolved_DeclaredInSuperclass2"));
868 ASSERT_TRUE(HasMethod("direct",
869 "LMySSLSocket;",
870 "setSocketImplFactory",
871 "(Ljava/net/SocketImplFactory;)V",
872 true,
873 "public static",
874 "Ljava/net/Socket;"));
875}
876
877TEST_F(VerifierDepsTest, InvokeStatic_DeclaredInInterface1) {
878 ASSERT_TRUE(VerifyMethod("InvokeStatic_DeclaredInInterface1"));
Nicolas Geoffray6e54f782017-03-08 15:27:09 +0000879 ASSERT_TRUE(HasClass("Ljava/util/Map$Entry;", true, "public interface"));
David Brazdilca3c8c32016-09-06 14:04:48 +0100880 ASSERT_TRUE(HasMethod("direct",
881 "Ljava/util/Map$Entry;",
882 "comparingByKey",
883 "()Ljava/util/Comparator;",
884 true,
885 "public static",
886 "Ljava/util/Map$Entry;"));
887}
888
889TEST_F(VerifierDepsTest, InvokeStatic_DeclaredInInterface2) {
890 ASSERT_FALSE(VerifyMethod("InvokeStatic_DeclaredInInterface2"));
891 ASSERT_TRUE(HasClass("Ljava/util/AbstractMap$SimpleEntry;", true, "public"));
892 ASSERT_TRUE(HasMethod("direct",
893 "Ljava/util/AbstractMap$SimpleEntry;",
894 "comparingByKey",
895 "()Ljava/util/Comparator;",
896 false));
897}
898
899TEST_F(VerifierDepsTest, InvokeStatic_Unresolved1) {
900 ASSERT_FALSE(VerifyMethod("InvokeStatic_Unresolved1"));
Nicolas Geoffray6e54f782017-03-08 15:27:09 +0000901 ASSERT_TRUE(HasClass("Ljavax/net/ssl/SSLSocket;", true, "public"));
David Brazdilca3c8c32016-09-06 14:04:48 +0100902 ASSERT_TRUE(HasMethod("direct", "Ljavax/net/ssl/SSLSocket;", "x", "()V", false));
903}
904
905TEST_F(VerifierDepsTest, InvokeStatic_Unresolved2) {
906 ASSERT_FALSE(VerifyMethod("InvokeStatic_Unresolved2"));
907 ASSERT_TRUE(HasMethod("direct", "LMySSLSocket;", "x", "()V", false));
908}
909
910TEST_F(VerifierDepsTest, InvokeDirect_Resolved_DeclaredInReferenced) {
911 ASSERT_TRUE(VerifyMethod("InvokeDirect_Resolved_DeclaredInReferenced"));
912 ASSERT_TRUE(HasClass("Ljava/net/Socket;", true, "public"));
913 ASSERT_TRUE(HasMethod(
914 "direct", "Ljava/net/Socket;", "<init>", "()V", true, "public", "Ljava/net/Socket;"));
915}
916
917TEST_F(VerifierDepsTest, InvokeDirect_Resolved_DeclaredInSuperclass1) {
918 ASSERT_FALSE(VerifyMethod("InvokeDirect_Resolved_DeclaredInSuperclass1"));
Nicolas Geoffray6e54f782017-03-08 15:27:09 +0000919 ASSERT_TRUE(HasClass("Ljavax/net/ssl/SSLSocket;", true, "public"));
David Brazdilca3c8c32016-09-06 14:04:48 +0100920 ASSERT_TRUE(HasMethod("direct",
921 "Ljavax/net/ssl/SSLSocket;",
922 "checkOldImpl",
923 "()V",
924 true,
925 "private",
926 "Ljava/net/Socket;"));
927}
928
929TEST_F(VerifierDepsTest, InvokeDirect_Resolved_DeclaredInSuperclass2) {
930 ASSERT_FALSE(VerifyMethod("InvokeDirect_Resolved_DeclaredInSuperclass2"));
931 ASSERT_TRUE(HasMethod(
932 "direct", "LMySSLSocket;", "checkOldImpl", "()V", true, "private", "Ljava/net/Socket;"));
933}
934
935TEST_F(VerifierDepsTest, InvokeDirect_Unresolved1) {
936 ASSERT_FALSE(VerifyMethod("InvokeDirect_Unresolved1"));
Nicolas Geoffray6e54f782017-03-08 15:27:09 +0000937 ASSERT_TRUE(HasClass("Ljavax/net/ssl/SSLSocket;", true, "public"));
David Brazdilca3c8c32016-09-06 14:04:48 +0100938 ASSERT_TRUE(HasMethod("direct", "Ljavax/net/ssl/SSLSocket;", "x", "()V", false));
939}
940
941TEST_F(VerifierDepsTest, InvokeDirect_Unresolved2) {
942 ASSERT_FALSE(VerifyMethod("InvokeDirect_Unresolved2"));
943 ASSERT_TRUE(HasMethod("direct", "LMySSLSocket;", "x", "()V", false));
944}
945
946TEST_F(VerifierDepsTest, InvokeVirtual_Resolved_DeclaredInReferenced) {
947 ASSERT_TRUE(VerifyMethod("InvokeVirtual_Resolved_DeclaredInReferenced"));
948 ASSERT_TRUE(HasClass("Ljava/lang/Throwable;", true, "public"));
949 ASSERT_TRUE(HasMethod("virtual",
950 "Ljava/lang/Throwable;",
951 "getMessage",
952 "()Ljava/lang/String;",
953 true,
954 "public",
955 "Ljava/lang/Throwable;"));
956 // Type dependency on `this` argument.
Nicolas Geoffray119e8462016-12-21 10:29:43 +0000957 ASSERT_TRUE(HasAssignable("Ljava/lang/Throwable;", "Ljava/net/SocketTimeoutException;", true));
David Brazdilca3c8c32016-09-06 14:04:48 +0100958}
959
960TEST_F(VerifierDepsTest, InvokeVirtual_Resolved_DeclaredInSuperclass1) {
961 ASSERT_TRUE(VerifyMethod("InvokeVirtual_Resolved_DeclaredInSuperclass1"));
962 ASSERT_TRUE(HasClass("Ljava/io/InterruptedIOException;", true, "public"));
963 ASSERT_TRUE(HasMethod("virtual",
964 "Ljava/io/InterruptedIOException;",
965 "getMessage",
966 "()Ljava/lang/String;",
967 true,
968 "public",
969 "Ljava/lang/Throwable;"));
970 // Type dependency on `this` argument.
Nicolas Geoffray119e8462016-12-21 10:29:43 +0000971 ASSERT_TRUE(HasAssignable("Ljava/lang/Throwable;", "Ljava/net/SocketTimeoutException;", true));
David Brazdilca3c8c32016-09-06 14:04:48 +0100972}
973
974TEST_F(VerifierDepsTest, InvokeVirtual_Resolved_DeclaredInSuperclass2) {
975 ASSERT_TRUE(VerifyMethod("InvokeVirtual_Resolved_DeclaredInSuperclass2"));
976 ASSERT_TRUE(HasMethod("virtual",
977 "LMySocketTimeoutException;",
978 "getMessage",
979 "()Ljava/lang/String;",
980 true,
981 "public",
982 "Ljava/lang/Throwable;"));
983}
984
985TEST_F(VerifierDepsTest, InvokeVirtual_Resolved_DeclaredInSuperinterface) {
986 ASSERT_TRUE(VerifyMethod("InvokeVirtual_Resolved_DeclaredInSuperinterface"));
987 ASSERT_TRUE(HasMethod("virtual",
988 "LMyThreadSet;",
989 "size",
990 "()I",
991 true,
Nicolas Geoffray6e54f782017-03-08 15:27:09 +0000992 "public",
David Brazdilca3c8c32016-09-06 14:04:48 +0100993 "Ljava/util/Set;"));
994}
995
996TEST_F(VerifierDepsTest, InvokeVirtual_Unresolved1) {
997 ASSERT_FALSE(VerifyMethod("InvokeVirtual_Unresolved1"));
998 ASSERT_TRUE(HasClass("Ljava/io/InterruptedIOException;", true, "public"));
999 ASSERT_TRUE(HasMethod("virtual", "Ljava/io/InterruptedIOException;", "x", "()V", false));
1000}
1001
1002TEST_F(VerifierDepsTest, InvokeVirtual_Unresolved2) {
1003 ASSERT_FALSE(VerifyMethod("InvokeVirtual_Unresolved2"));
1004 ASSERT_TRUE(HasMethod("virtual", "LMySocketTimeoutException;", "x", "()V", false));
1005}
1006
1007TEST_F(VerifierDepsTest, InvokeVirtual_ActuallyDirect) {
1008 ASSERT_FALSE(VerifyMethod("InvokeVirtual_ActuallyDirect"));
1009 ASSERT_TRUE(HasMethod("virtual", "LMyThread;", "activeCount", "()I", false));
1010 ASSERT_TRUE(HasMethod("direct",
1011 "LMyThread;",
1012 "activeCount",
1013 "()I",
1014 true,
1015 "public static",
1016 "Ljava/lang/Thread;"));
1017}
1018
1019TEST_F(VerifierDepsTest, InvokeInterface_Resolved_DeclaredInReferenced) {
1020 ASSERT_TRUE(VerifyMethod("InvokeInterface_Resolved_DeclaredInReferenced"));
Nicolas Geoffray6e54f782017-03-08 15:27:09 +00001021 ASSERT_TRUE(HasClass("Ljava/lang/Runnable;", true, "public interface"));
David Brazdilca3c8c32016-09-06 14:04:48 +01001022 ASSERT_TRUE(HasMethod("interface",
1023 "Ljava/lang/Runnable;",
1024 "run",
1025 "()V",
1026 true,
Nicolas Geoffray6e54f782017-03-08 15:27:09 +00001027 "public",
David Brazdilca3c8c32016-09-06 14:04:48 +01001028 "Ljava/lang/Runnable;"));
1029}
1030
1031TEST_F(VerifierDepsTest, InvokeInterface_Resolved_DeclaredInSuperclass) {
1032 ASSERT_FALSE(VerifyMethod("InvokeInterface_Resolved_DeclaredInSuperclass"));
1033 ASSERT_TRUE(HasMethod("interface", "LMyThread;", "join", "()V", false));
1034}
1035
1036TEST_F(VerifierDepsTest, InvokeInterface_Resolved_DeclaredInSuperinterface1) {
1037 ASSERT_FALSE(VerifyMethod("InvokeInterface_Resolved_DeclaredInSuperinterface1"));
1038 ASSERT_TRUE(HasMethod("interface",
1039 "LMyThreadSet;",
1040 "run",
1041 "()V",
1042 true,
Nicolas Geoffray6e54f782017-03-08 15:27:09 +00001043 "public",
David Brazdilca3c8c32016-09-06 14:04:48 +01001044 "Ljava/lang/Runnable;"));
1045}
1046
1047TEST_F(VerifierDepsTest, InvokeInterface_Resolved_DeclaredInSuperinterface2) {
1048 ASSERT_FALSE(VerifyMethod("InvokeInterface_Resolved_DeclaredInSuperinterface2"));
1049 ASSERT_TRUE(HasMethod("interface",
1050 "LMyThreadSet;",
1051 "isEmpty",
1052 "()Z",
1053 true,
Nicolas Geoffray6e54f782017-03-08 15:27:09 +00001054 "public",
David Brazdilca3c8c32016-09-06 14:04:48 +01001055 "Ljava/util/Set;"));
1056}
1057
1058TEST_F(VerifierDepsTest, InvokeInterface_Unresolved1) {
1059 ASSERT_FALSE(VerifyMethod("InvokeInterface_Unresolved1"));
Nicolas Geoffray6e54f782017-03-08 15:27:09 +00001060 ASSERT_TRUE(HasClass("Ljava/lang/Runnable;", true, "public interface"));
David Brazdilca3c8c32016-09-06 14:04:48 +01001061 ASSERT_TRUE(HasMethod("interface", "Ljava/lang/Runnable;", "x", "()V", false));
1062}
1063
1064TEST_F(VerifierDepsTest, InvokeInterface_Unresolved2) {
1065 ASSERT_FALSE(VerifyMethod("InvokeInterface_Unresolved2"));
1066 ASSERT_TRUE(HasMethod("interface", "LMyThreadSet;", "x", "()V", false));
1067}
1068
1069TEST_F(VerifierDepsTest, InvokeSuper_ThisAssignable) {
1070 ASSERT_TRUE(VerifyMethod("InvokeSuper_ThisAssignable"));
Nicolas Geoffray6e54f782017-03-08 15:27:09 +00001071 ASSERT_TRUE(HasClass("Ljava/lang/Runnable;", true, "public interface"));
Nicolas Geoffray0e2fe0f2016-12-21 16:54:52 +00001072 ASSERT_TRUE(HasAssignable("Ljava/lang/Runnable;", "Ljava/lang/Thread;", true));
David Brazdilca3c8c32016-09-06 14:04:48 +01001073 ASSERT_TRUE(HasMethod("interface",
1074 "Ljava/lang/Runnable;",
1075 "run",
1076 "()V",
1077 true,
Nicolas Geoffray6e54f782017-03-08 15:27:09 +00001078 "public",
David Brazdilca3c8c32016-09-06 14:04:48 +01001079 "Ljava/lang/Runnable;"));
1080}
1081
1082TEST_F(VerifierDepsTest, InvokeSuper_ThisNotAssignable) {
1083 ASSERT_FALSE(VerifyMethod("InvokeSuper_ThisNotAssignable"));
Nicolas Geoffray6e54f782017-03-08 15:27:09 +00001084 ASSERT_TRUE(HasClass("Ljava/lang/Integer;", true, "public"));
Nicolas Geoffray119e8462016-12-21 10:29:43 +00001085 ASSERT_TRUE(HasAssignable("Ljava/lang/Integer;", "Ljava/lang/Thread;", false));
David Brazdilca3c8c32016-09-06 14:04:48 +01001086 ASSERT_TRUE(HasMethod(
1087 "virtual", "Ljava/lang/Integer;", "intValue", "()I", true, "public", "Ljava/lang/Integer;"));
1088}
1089
Nicolas Geoffray0f1cb172017-01-05 15:23:19 +00001090TEST_F(VerifierDepsTest, ArgumentType_ResolvedReferenceArray) {
1091 ASSERT_TRUE(VerifyMethod("ArgumentType_ResolvedReferenceArray"));
Nicolas Geoffray6e54f782017-03-08 15:27:09 +00001092 ASSERT_TRUE(HasClass("[Ljava/lang/Thread;", true, "public"));
Nicolas Geoffray0f1cb172017-01-05 15:23:19 +00001093}
1094
1095TEST_F(VerifierDepsTest, NewArray_Resolved) {
1096 ASSERT_TRUE(VerifyMethod("NewArray_Resolved"));
Nicolas Geoffray6e54f782017-03-08 15:27:09 +00001097 ASSERT_TRUE(HasClass("[Ljava/lang/IllegalStateException;", true, "public"));
Nicolas Geoffray0f1cb172017-01-05 15:23:19 +00001098}
1099
David Brazdil6f82fbd2016-09-14 11:55:26 +01001100TEST_F(VerifierDepsTest, EncodeDecode) {
1101 VerifyDexFile();
1102
1103 ASSERT_EQ(1u, NumberOfCompiledDexFiles());
1104 ASSERT_TRUE(HasEachKindOfRecord());
1105
1106 std::vector<uint8_t> buffer;
Nicolas Geoffrayd01f60c2016-10-28 14:45:48 +01001107 verifier_deps_->Encode(dex_files_, &buffer);
David Brazdil6f82fbd2016-09-14 11:55:26 +01001108 ASSERT_FALSE(buffer.empty());
1109
Nicolas Geoffraye70dd562016-10-30 21:03:35 +00001110 VerifierDeps decoded_deps(dex_files_, ArrayRef<const uint8_t>(buffer));
David Brazdil6f82fbd2016-09-14 11:55:26 +01001111 ASSERT_TRUE(verifier_deps_->Equals(decoded_deps));
1112}
1113
Nicolas Geoffrayd01f60c2016-10-28 14:45:48 +01001114TEST_F(VerifierDepsTest, EncodeDecodeMulti) {
1115 VerifyDexFile("MultiDex");
1116
1117 ASSERT_GT(NumberOfCompiledDexFiles(), 1u);
1118 std::vector<uint8_t> buffer;
1119 verifier_deps_->Encode(dex_files_, &buffer);
1120 ASSERT_FALSE(buffer.empty());
1121
1122 // Create new DexFile, to mess with std::map order: the verifier deps used
1123 // to iterate over the map, which doesn't guarantee insertion order. We fixed
1124 // this by passing the expected order when encoding/decoding.
1125 std::vector<std::unique_ptr<const DexFile>> first_dex_files = OpenTestDexFiles("VerifierDeps");
1126 std::vector<std::unique_ptr<const DexFile>> second_dex_files = OpenTestDexFiles("MultiDex");
1127 std::vector<const DexFile*> dex_files;
1128 for (auto& dex_file : first_dex_files) {
1129 dex_files.push_back(dex_file.get());
1130 }
1131 for (auto& dex_file : second_dex_files) {
1132 dex_files.push_back(dex_file.get());
1133 }
1134
1135 // Dump the new verifier deps to ensure it can properly read the data.
Nicolas Geoffraye70dd562016-10-30 21:03:35 +00001136 VerifierDeps decoded_deps(dex_files, ArrayRef<const uint8_t>(buffer));
Nicolas Geoffrayd01f60c2016-10-28 14:45:48 +01001137 std::ostringstream stream;
1138 VariableIndentationOutputStream os(&stream);
1139 decoded_deps.Dump(&os);
1140}
1141
Nicolas Geoffray08025182016-10-25 17:20:18 +01001142TEST_F(VerifierDepsTest, UnverifiedClasses) {
1143 VerifyDexFile();
1144 ASSERT_FALSE(HasUnverifiedClass("LMyThread;"));
1145 // Test that a class with a soft failure is recorded.
1146 ASSERT_TRUE(HasUnverifiedClass("LMain;"));
1147 // Test that a class with hard failure is recorded.
1148 ASSERT_TRUE(HasUnverifiedClass("LMyVerificationFailure;"));
1149 // Test that a class with unresolved super is recorded.
Nicolas Geoffray7cc3ae52017-03-07 14:33:37 +00001150 ASSERT_TRUE(HasUnverifiedClass("LMyClassWithNoSuper;"));
Nicolas Geoffray08025182016-10-25 17:20:18 +01001151 // Test that a class with unresolved super and hard failure is recorded.
1152 ASSERT_TRUE(HasUnverifiedClass("LMyClassWithNoSuperButFailures;"));
1153}
1154
Nicolas Geoffray8904b6f2016-10-28 19:50:34 +01001155// Returns the next resolution kind in the enum.
1156static MethodResolutionKind GetNextResolutionKind(MethodResolutionKind resolution_kind) {
1157 if (resolution_kind == kDirectMethodResolution) {
1158 return kVirtualMethodResolution;
1159 } else if (resolution_kind == kVirtualMethodResolution) {
1160 return kInterfaceMethodResolution;
1161 } else {
1162 DCHECK_EQ(resolution_kind, kInterfaceMethodResolution);
1163 return kDirectMethodResolution;
1164 }
1165}
1166
1167TEST_F(VerifierDepsTest, VerifyDeps) {
1168 VerifyDexFile();
1169
1170 ASSERT_EQ(1u, NumberOfCompiledDexFiles());
1171 ASSERT_TRUE(HasEachKindOfRecord());
1172
1173 // When validating, we create a new class loader, as
1174 // the existing `class_loader_` may contain erroneous classes,
1175 // that ClassLinker::FindClass won't return.
1176
1177 ScopedObjectAccess soa(Thread::Current());
1178 StackHandleScope<1> hs(soa.Self());
1179 MutableHandle<mirror::ClassLoader> new_class_loader(hs.NewHandle<mirror::ClassLoader>(nullptr));
1180 {
1181 new_class_loader.Assign(soa.Decode<mirror::ClassLoader>(LoadDex("VerifierDeps")));
Nicolas Geoffray6bb7f1b2016-11-03 10:52:49 +00001182 ASSERT_TRUE(verifier_deps_->ValidateDependencies(new_class_loader, soa.Self()));
Nicolas Geoffray8904b6f2016-10-28 19:50:34 +01001183 }
1184
1185 std::vector<uint8_t> buffer;
1186 verifier_deps_->Encode(dex_files_, &buffer);
1187 ASSERT_FALSE(buffer.empty());
1188
1189 {
1190 VerifierDeps decoded_deps(dex_files_, ArrayRef<const uint8_t>(buffer));
1191 new_class_loader.Assign(soa.Decode<mirror::ClassLoader>(LoadDex("VerifierDeps")));
Nicolas Geoffray6bb7f1b2016-11-03 10:52:49 +00001192 ASSERT_TRUE(decoded_deps.ValidateDependencies(new_class_loader, soa.Self()));
Nicolas Geoffray8904b6f2016-10-28 19:50:34 +01001193 }
1194
1195 // Fiddle with the dependencies to make sure we catch any change and fail to verify.
1196
1197 {
1198 // Mess up with the assignable_types.
1199 VerifierDeps decoded_deps(dex_files_, ArrayRef<const uint8_t>(buffer));
1200 VerifierDeps::DexFileDeps* deps = decoded_deps.GetDexFileDeps(*primary_dex_file_);
1201 deps->assignable_types_.insert(*deps->unassignable_types_.begin());
1202 new_class_loader.Assign(soa.Decode<mirror::ClassLoader>(LoadDex("VerifierDeps")));
Nicolas Geoffray6bb7f1b2016-11-03 10:52:49 +00001203 ASSERT_FALSE(decoded_deps.ValidateDependencies(new_class_loader, soa.Self()));
Nicolas Geoffray8904b6f2016-10-28 19:50:34 +01001204 }
1205
1206 {
1207 // Mess up with the unassignable_types.
1208 VerifierDeps decoded_deps(dex_files_, ArrayRef<const uint8_t>(buffer));
1209 VerifierDeps::DexFileDeps* deps = decoded_deps.GetDexFileDeps(*primary_dex_file_);
1210 deps->unassignable_types_.insert(*deps->assignable_types_.begin());
1211 new_class_loader.Assign(soa.Decode<mirror::ClassLoader>(LoadDex("VerifierDeps")));
Nicolas Geoffray6bb7f1b2016-11-03 10:52:49 +00001212 ASSERT_FALSE(decoded_deps.ValidateDependencies(new_class_loader, soa.Self()));
Nicolas Geoffray8904b6f2016-10-28 19:50:34 +01001213 }
1214
1215 // Mess up with classes.
1216 {
1217 VerifierDeps decoded_deps(dex_files_, ArrayRef<const uint8_t>(buffer));
1218 VerifierDeps::DexFileDeps* deps = decoded_deps.GetDexFileDeps(*primary_dex_file_);
1219 bool found = false;
1220 for (const auto& entry : deps->classes_) {
1221 if (entry.IsResolved()) {
1222 deps->classes_.insert(VerifierDeps::ClassResolution(
1223 entry.GetDexTypeIndex(), VerifierDeps::kUnresolvedMarker));
1224 found = true;
1225 break;
1226 }
1227 }
1228 ASSERT_TRUE(found);
1229 new_class_loader.Assign(soa.Decode<mirror::ClassLoader>(LoadDex("VerifierDeps")));
Nicolas Geoffray6bb7f1b2016-11-03 10:52:49 +00001230 ASSERT_FALSE(decoded_deps.ValidateDependencies(new_class_loader, soa.Self()));
Nicolas Geoffray8904b6f2016-10-28 19:50:34 +01001231 }
1232
1233 {
1234 VerifierDeps decoded_deps(dex_files_, ArrayRef<const uint8_t>(buffer));
1235 VerifierDeps::DexFileDeps* deps = decoded_deps.GetDexFileDeps(*primary_dex_file_);
1236 bool found = false;
1237 for (const auto& entry : deps->classes_) {
1238 if (!entry.IsResolved()) {
1239 deps->classes_.insert(VerifierDeps::ClassResolution(
1240 entry.GetDexTypeIndex(), VerifierDeps::kUnresolvedMarker - 1));
1241 found = true;
1242 break;
1243 }
1244 }
1245 ASSERT_TRUE(found);
1246 new_class_loader.Assign(soa.Decode<mirror::ClassLoader>(LoadDex("VerifierDeps")));
Nicolas Geoffray6bb7f1b2016-11-03 10:52:49 +00001247 ASSERT_FALSE(decoded_deps.ValidateDependencies(new_class_loader, soa.Self()));
Nicolas Geoffray8904b6f2016-10-28 19:50:34 +01001248 }
1249
1250 {
1251 VerifierDeps decoded_deps(dex_files_, ArrayRef<const uint8_t>(buffer));
1252 VerifierDeps::DexFileDeps* deps = decoded_deps.GetDexFileDeps(*primary_dex_file_);
1253 bool found = false;
1254 for (const auto& entry : deps->classes_) {
1255 if (entry.IsResolved()) {
1256 deps->classes_.insert(VerifierDeps::ClassResolution(
1257 entry.GetDexTypeIndex(), entry.GetAccessFlags() - 1));
1258 found = true;
1259 break;
1260 }
1261 }
1262 ASSERT_TRUE(found);
1263 new_class_loader.Assign(soa.Decode<mirror::ClassLoader>(LoadDex("VerifierDeps")));
Nicolas Geoffray6bb7f1b2016-11-03 10:52:49 +00001264 ASSERT_FALSE(decoded_deps.ValidateDependencies(new_class_loader, soa.Self()));
Nicolas Geoffray8904b6f2016-10-28 19:50:34 +01001265 }
1266
1267 // Mess up with fields.
1268 {
1269 VerifierDeps decoded_deps(dex_files_, ArrayRef<const uint8_t>(buffer));
1270 VerifierDeps::DexFileDeps* deps = decoded_deps.GetDexFileDeps(*primary_dex_file_);
1271 bool found = false;
1272 for (const auto& entry : deps->fields_) {
1273 if (entry.IsResolved()) {
1274 deps->fields_.insert(VerifierDeps::FieldResolution(entry.GetDexFieldIndex(),
1275 VerifierDeps::kUnresolvedMarker,
1276 entry.GetDeclaringClassIndex()));
1277 found = true;
1278 break;
1279 }
1280 }
1281 ASSERT_TRUE(found);
1282 new_class_loader.Assign(soa.Decode<mirror::ClassLoader>(LoadDex("VerifierDeps")));
Nicolas Geoffray6bb7f1b2016-11-03 10:52:49 +00001283 ASSERT_FALSE(decoded_deps.ValidateDependencies(new_class_loader, soa.Self()));
Nicolas Geoffray8904b6f2016-10-28 19:50:34 +01001284 }
1285
1286 {
1287 VerifierDeps decoded_deps(dex_files_, ArrayRef<const uint8_t>(buffer));
1288 VerifierDeps::DexFileDeps* deps = decoded_deps.GetDexFileDeps(*primary_dex_file_);
1289 bool found = false;
1290 for (const auto& entry : deps->fields_) {
1291 if (!entry.IsResolved()) {
Andreas Gampe8a0128a2016-11-28 07:38:35 -08001292 constexpr dex::StringIndex kStringIndexZero(0); // We know there is a class there.
Nicolas Geoffray8904b6f2016-10-28 19:50:34 +01001293 deps->fields_.insert(VerifierDeps::FieldResolution(0 /* we know there is a field there */,
1294 VerifierDeps::kUnresolvedMarker - 1,
Andreas Gampe8a0128a2016-11-28 07:38:35 -08001295 kStringIndexZero));
Nicolas Geoffray8904b6f2016-10-28 19:50:34 +01001296 found = true;
1297 break;
1298 }
1299 }
1300 ASSERT_TRUE(found);
1301 new_class_loader.Assign(soa.Decode<mirror::ClassLoader>(LoadDex("VerifierDeps")));
Nicolas Geoffray6bb7f1b2016-11-03 10:52:49 +00001302 ASSERT_FALSE(decoded_deps.ValidateDependencies(new_class_loader, soa.Self()));
Nicolas Geoffray8904b6f2016-10-28 19:50:34 +01001303 }
1304
1305 {
1306 VerifierDeps decoded_deps(dex_files_, ArrayRef<const uint8_t>(buffer));
1307 VerifierDeps::DexFileDeps* deps = decoded_deps.GetDexFileDeps(*primary_dex_file_);
1308 bool found = false;
1309 for (const auto& entry : deps->fields_) {
1310 if (entry.IsResolved()) {
1311 deps->fields_.insert(VerifierDeps::FieldResolution(entry.GetDexFieldIndex(),
1312 entry.GetAccessFlags() - 1,
1313 entry.GetDeclaringClassIndex()));
1314 found = true;
1315 break;
1316 }
1317 }
1318 ASSERT_TRUE(found);
1319 new_class_loader.Assign(soa.Decode<mirror::ClassLoader>(LoadDex("VerifierDeps")));
Nicolas Geoffray6bb7f1b2016-11-03 10:52:49 +00001320 ASSERT_FALSE(decoded_deps.ValidateDependencies(new_class_loader, soa.Self()));
Nicolas Geoffray8904b6f2016-10-28 19:50:34 +01001321 }
1322
1323 {
1324 VerifierDeps decoded_deps(dex_files_, ArrayRef<const uint8_t>(buffer));
1325 VerifierDeps::DexFileDeps* deps = decoded_deps.GetDexFileDeps(*primary_dex_file_);
1326 bool found = false;
1327 for (const auto& entry : deps->fields_) {
Andreas Gampe8a0128a2016-11-28 07:38:35 -08001328 constexpr dex::StringIndex kNewTypeIndex(0);
Nicolas Geoffray8904b6f2016-10-28 19:50:34 +01001329 if (entry.GetDeclaringClassIndex() != kNewTypeIndex) {
1330 deps->fields_.insert(VerifierDeps::FieldResolution(entry.GetDexFieldIndex(),
1331 entry.GetAccessFlags(),
1332 kNewTypeIndex));
1333 found = true;
1334 break;
1335 }
1336 }
1337 ASSERT_TRUE(found);
1338 new_class_loader.Assign(soa.Decode<mirror::ClassLoader>(LoadDex("VerifierDeps")));
Nicolas Geoffray6bb7f1b2016-11-03 10:52:49 +00001339 ASSERT_FALSE(decoded_deps.ValidateDependencies(new_class_loader, soa.Self()));
Nicolas Geoffray8904b6f2016-10-28 19:50:34 +01001340 }
1341
1342 // Mess up with methods.
1343 for (MethodResolutionKind resolution_kind :
1344 { kDirectMethodResolution, kVirtualMethodResolution, kInterfaceMethodResolution }) {
1345 {
1346 VerifierDeps decoded_deps(dex_files_, ArrayRef<const uint8_t>(buffer));
1347 VerifierDeps::DexFileDeps* deps = decoded_deps.GetDexFileDeps(*primary_dex_file_);
1348 bool found = false;
1349 std::set<VerifierDeps::MethodResolution>* methods = GetMethods(deps, resolution_kind);
1350 for (const auto& entry : *methods) {
1351 if (entry.IsResolved()) {
1352 methods->insert(VerifierDeps::MethodResolution(entry.GetDexMethodIndex(),
1353 VerifierDeps::kUnresolvedMarker,
1354 entry.GetDeclaringClassIndex()));
1355 found = true;
1356 break;
1357 }
1358 }
1359 ASSERT_TRUE(found);
1360 new_class_loader.Assign(soa.Decode<mirror::ClassLoader>(LoadDex("VerifierDeps")));
Nicolas Geoffray6bb7f1b2016-11-03 10:52:49 +00001361 ASSERT_FALSE(decoded_deps.ValidateDependencies(new_class_loader, soa.Self()));
Nicolas Geoffray8904b6f2016-10-28 19:50:34 +01001362 }
1363
1364 {
1365 VerifierDeps decoded_deps(dex_files_, ArrayRef<const uint8_t>(buffer));
1366 VerifierDeps::DexFileDeps* deps = decoded_deps.GetDexFileDeps(*primary_dex_file_);
1367 bool found = false;
1368 std::set<VerifierDeps::MethodResolution>* methods = GetMethods(deps, resolution_kind);
1369 for (const auto& entry : *methods) {
1370 if (!entry.IsResolved()) {
Andreas Gampe8a0128a2016-11-28 07:38:35 -08001371 constexpr dex::StringIndex kStringIndexZero(0); // We know there is a class there.
Nicolas Geoffray8904b6f2016-10-28 19:50:34 +01001372 methods->insert(VerifierDeps::MethodResolution(0 /* we know there is a method there */,
1373 VerifierDeps::kUnresolvedMarker - 1,
Andreas Gampe8a0128a2016-11-28 07:38:35 -08001374 kStringIndexZero));
Nicolas Geoffray8904b6f2016-10-28 19:50:34 +01001375 found = true;
1376 break;
1377 }
1378 }
1379 ASSERT_TRUE(found);
1380 new_class_loader.Assign(soa.Decode<mirror::ClassLoader>(LoadDex("VerifierDeps")));
Nicolas Geoffray6bb7f1b2016-11-03 10:52:49 +00001381 ASSERT_FALSE(decoded_deps.ValidateDependencies(new_class_loader, soa.Self()));
Nicolas Geoffray8904b6f2016-10-28 19:50:34 +01001382 }
1383
1384 {
1385 VerifierDeps decoded_deps(dex_files_, ArrayRef<const uint8_t>(buffer));
1386 VerifierDeps::DexFileDeps* deps = decoded_deps.GetDexFileDeps(*primary_dex_file_);
1387 bool found = false;
1388 std::set<VerifierDeps::MethodResolution>* methods = GetMethods(deps, resolution_kind);
1389 for (const auto& entry : *methods) {
1390 if (entry.IsResolved()) {
1391 methods->insert(VerifierDeps::MethodResolution(entry.GetDexMethodIndex(),
1392 entry.GetAccessFlags() - 1,
1393 entry.GetDeclaringClassIndex()));
1394 found = true;
1395 break;
1396 }
1397 }
1398 ASSERT_TRUE(found);
1399 new_class_loader.Assign(soa.Decode<mirror::ClassLoader>(LoadDex("VerifierDeps")));
Nicolas Geoffray6bb7f1b2016-11-03 10:52:49 +00001400 ASSERT_FALSE(decoded_deps.ValidateDependencies(new_class_loader, soa.Self()));
Nicolas Geoffray8904b6f2016-10-28 19:50:34 +01001401 }
1402
1403 {
1404 VerifierDeps decoded_deps(dex_files_, ArrayRef<const uint8_t>(buffer));
1405 VerifierDeps::DexFileDeps* deps = decoded_deps.GetDexFileDeps(*primary_dex_file_);
1406 bool found = false;
1407 std::set<VerifierDeps::MethodResolution>* methods = GetMethods(deps, resolution_kind);
1408 for (const auto& entry : *methods) {
Andreas Gampe8a0128a2016-11-28 07:38:35 -08001409 constexpr dex::StringIndex kNewTypeIndex(0);
Nicolas Geoffray8904b6f2016-10-28 19:50:34 +01001410 if (entry.IsResolved() && entry.GetDeclaringClassIndex() != kNewTypeIndex) {
1411 methods->insert(VerifierDeps::MethodResolution(entry.GetDexMethodIndex(),
1412 entry.GetAccessFlags(),
1413 kNewTypeIndex));
1414 found = true;
1415 break;
1416 }
1417 }
1418 ASSERT_TRUE(found);
1419 new_class_loader.Assign(soa.Decode<mirror::ClassLoader>(LoadDex("VerifierDeps")));
Nicolas Geoffray6bb7f1b2016-11-03 10:52:49 +00001420 ASSERT_FALSE(decoded_deps.ValidateDependencies(new_class_loader, soa.Self()));
Nicolas Geoffray8904b6f2016-10-28 19:50:34 +01001421 }
1422
Nicolas Geoffray865cf902017-01-18 14:34:48 +00001423 // The two tests below make sure that fiddling with the method kind
1424 // (static, virtual, interface) is detected by `ValidateDependencies`.
1425
1426 // An interface method lookup can succeed with a virtual method lookup on the same class.
1427 // That's OK, as we only want to make sure there is a method being defined with the right
1428 // flags. Therefore, polluting the interface methods with virtual methods does not have
1429 // to fail verification.
1430 if (resolution_kind != kVirtualMethodResolution) {
Nicolas Geoffray8904b6f2016-10-28 19:50:34 +01001431 VerifierDeps decoded_deps(dex_files_, ArrayRef<const uint8_t>(buffer));
1432 VerifierDeps::DexFileDeps* deps = decoded_deps.GetDexFileDeps(*primary_dex_file_);
1433 bool found = false;
1434 std::set<VerifierDeps::MethodResolution>* methods = GetMethods(deps, resolution_kind);
1435 for (const auto& entry : *methods) {
1436 if (entry.IsResolved()) {
1437 GetMethods(deps, GetNextResolutionKind(resolution_kind))->insert(
1438 VerifierDeps::MethodResolution(entry.GetDexMethodIndex(),
1439 entry.GetAccessFlags(),
1440 entry.GetDeclaringClassIndex()));
1441 found = true;
1442 }
1443 }
1444 ASSERT_TRUE(found);
1445 new_class_loader.Assign(soa.Decode<mirror::ClassLoader>(LoadDex("VerifierDeps")));
Nicolas Geoffray6bb7f1b2016-11-03 10:52:49 +00001446 ASSERT_FALSE(decoded_deps.ValidateDependencies(new_class_loader, soa.Self()));
Nicolas Geoffray8904b6f2016-10-28 19:50:34 +01001447 }
1448
Nicolas Geoffray865cf902017-01-18 14:34:48 +00001449 // See comment above that applies the same way.
1450 if (resolution_kind != kInterfaceMethodResolution) {
Nicolas Geoffray8904b6f2016-10-28 19:50:34 +01001451 VerifierDeps decoded_deps(dex_files_, ArrayRef<const uint8_t>(buffer));
1452 VerifierDeps::DexFileDeps* deps = decoded_deps.GetDexFileDeps(*primary_dex_file_);
1453 bool found = false;
1454 std::set<VerifierDeps::MethodResolution>* methods = GetMethods(deps, resolution_kind);
1455 for (const auto& entry : *methods) {
1456 if (entry.IsResolved()) {
1457 GetMethods(deps, GetNextResolutionKind(GetNextResolutionKind(resolution_kind)))->insert(
1458 VerifierDeps::MethodResolution(entry.GetDexMethodIndex(),
1459 entry.GetAccessFlags(),
1460 entry.GetDeclaringClassIndex()));
1461 found = true;
1462 }
1463 }
1464 ASSERT_TRUE(found);
1465 new_class_loader.Assign(soa.Decode<mirror::ClassLoader>(LoadDex("VerifierDeps")));
Nicolas Geoffray6bb7f1b2016-11-03 10:52:49 +00001466 ASSERT_FALSE(decoded_deps.ValidateDependencies(new_class_loader, soa.Self()));
1467 }
1468 }
1469}
1470
1471TEST_F(VerifierDepsTest, CompilerDriver) {
1472 SetupCompilerDriver();
1473
1474 // Test both multi-dex and single-dex configuration.
1475 for (const char* multi : { "MultiDex", static_cast<const char*>(nullptr) }) {
1476 // Test that the compiler driver behaves as expected when the dependencies
1477 // verify and when they don't verify.
1478 for (bool verify_failure : { false, true }) {
1479 {
1480 ScopedObjectAccess soa(Thread::Current());
1481 LoadDexFile(&soa, "VerifierDeps", multi);
1482 }
1483 VerifyWithCompilerDriver(/* verifier_deps */ nullptr);
1484
1485 std::vector<uint8_t> buffer;
1486 verifier_deps_->Encode(dex_files_, &buffer);
1487
1488 {
1489 ScopedObjectAccess soa(Thread::Current());
1490 LoadDexFile(&soa, "VerifierDeps", multi);
1491 }
1492 verifier::VerifierDeps decoded_deps(dex_files_, ArrayRef<const uint8_t>(buffer));
1493 if (verify_failure) {
1494 // Just taint the decoded VerifierDeps with one invalid entry.
1495 VerifierDeps::DexFileDeps* deps = decoded_deps.GetDexFileDeps(*primary_dex_file_);
1496 bool found = false;
1497 for (const auto& entry : deps->classes_) {
1498 if (entry.IsResolved()) {
1499 deps->classes_.insert(VerifierDeps::ClassResolution(
1500 entry.GetDexTypeIndex(), VerifierDeps::kUnresolvedMarker));
1501 found = true;
1502 break;
1503 }
1504 }
1505 ASSERT_TRUE(found);
1506 }
1507 VerifyWithCompilerDriver(&decoded_deps);
1508
1509 if (verify_failure) {
1510 ASSERT_FALSE(verifier_deps_ == nullptr);
1511 ASSERT_FALSE(verifier_deps_->Equals(decoded_deps));
1512 } else {
1513 ASSERT_TRUE(verifier_deps_ == nullptr);
1514 VerifyClassStatus(decoded_deps);
1515 }
Nicolas Geoffray8904b6f2016-10-28 19:50:34 +01001516 }
1517 }
1518}
1519
Nicolas Geoffray7cc3ae52017-03-07 14:33:37 +00001520TEST_F(VerifierDepsTest, MultiDexVerification) {
1521 VerifyDexFile("VerifierDepsMulti");
1522 ASSERT_EQ(NumberOfCompiledDexFiles(), 2u);
1523
1524 ASSERT_TRUE(HasUnverifiedClass("LMySoftVerificationFailure;", *dex_files_[1]));
1525 ASSERT_TRUE(HasUnverifiedClass("LMySub1SoftVerificationFailure;", *dex_files_[0]));
1526 ASSERT_TRUE(HasUnverifiedClass("LMySub2SoftVerificationFailure;", *dex_files_[0]));
1527
1528 std::vector<uint8_t> buffer;
1529 verifier_deps_->Encode(dex_files_, &buffer);
1530 ASSERT_FALSE(buffer.empty());
1531}
1532
Nicolas Geoffrayfc38e912017-03-16 16:51:59 +00001533TEST_F(VerifierDepsTest, NotAssignable_InterfaceWithClassInBoot) {
1534 ASSERT_TRUE(TestAssignabilityRecording(/* dst */ "Ljava/lang/Exception;",
1535 /* src */ "LIface;",
1536 /* is_strict */ true,
1537 /* is_assignable */ false));
1538 ASSERT_TRUE(HasAssignable("Ljava/lang/Exception;", "LIface;", false));
1539}
1540
Nicolas Geoffraybdb540d2017-04-19 13:50:34 +01001541TEST_F(VerifierDepsTest, Assignable_Arrays) {
1542 ASSERT_TRUE(TestAssignabilityRecording(/* dst */ "[LIface;",
1543 /* src */ "[LMyClassExtendingInterface;",
1544 /* is_strict */ false,
1545 /* is_assignable */ true));
1546 ASSERT_FALSE(HasAssignable(
1547 "LIface;", "LMyClassExtendingInterface;", /* expected_is_assignable */ true));
1548 ASSERT_FALSE(HasAssignable(
1549 "LIface;", "LMyClassExtendingInterface;", /* expected_is_assignable */ false));
1550}
1551
David Brazdilca3c8c32016-09-06 14:04:48 +01001552} // namespace verifier
1553} // namespace art