blob: 72e2a6ce9f4570c3167b76d96dad11fc1fab5f87 [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 Gamped482e732017-04-24 17:59:09 -070028#include "driver/compiler_driver-inl.h"
Andreas Gamped9911ee2017-03-27 13:27:24 -070029#include "driver/compiler_options.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"
Mathieu Chartier93764b82017-07-17 14:51:53 -070036#include "utils/atomic_dex_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.
Mathieu Chartier93764b82017-07-17 14:51:53 -0700100 AtomicDexRefMap<const VerifiedMethod*>* map =
Nicolas Geoffray1d0ae3f2016-12-06 13:40:16 +0000101 &compiler_driver_->GetVerificationResults()->atomic_verified_methods_;
Mathieu Chartier93764b82017-07-17 14:51:53 -0700102 map->Visit([](const DexFileReference& ref ATTRIBUTE_UNUSED, const VerifiedMethod* method) {
Nicolas Geoffray1d0ae3f2016-12-06 13:40:16 +0000103 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);
Mathieu Chartiere17cf242017-06-19 11:05:51 -0700154 it.SkipAllFields();
David Brazdilca3c8c32016-09-06 14:04:48 +0100155
156 ArtMethod* method = nullptr;
157 while (it.HasNextDirectMethod()) {
Vladimir Markoba118822017-06-12 15:41:56 +0100158 ArtMethod* resolved_method =
159 class_linker_->ResolveMethod<ClassLinker::ResolveMode::kNoChecks>(
160 *primary_dex_file_,
161 it.GetMemberIndex(),
162 dex_cache_handle,
163 class_loader_handle,
164 nullptr,
165 it.GetMethodInvokeType(*class_def));
David Brazdilca3c8c32016-09-06 14:04:48 +0100166 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.
Vladimir Markoba118822017-06-12 15:41:56 +0100373 bool HasMethod(const std::string& expected_klass,
David Brazdilca3c8c32016-09-06 14:04:48 +0100374 const std::string& expected_name,
375 const std::string& expected_signature,
376 bool expected_resolved,
377 const std::string& expected_access_flags = "",
378 const std::string& expected_decl_klass = "") {
David Brazdilca3c8c32016-09-06 14:04:48 +0100379 for (auto& dex_dep : verifier_deps_->dex_deps_) {
Vladimir Markoba118822017-06-12 15:41:56 +0100380 for (const VerifierDeps::MethodResolution& entry : dex_dep.second->methods_) {
David Brazdilca3c8c32016-09-06 14:04:48 +0100381 if (expected_resolved != entry.IsResolved()) {
382 continue;
383 }
384
385 const DexFile::MethodId& method_id = dex_dep.first->GetMethodId(entry.GetDexMethodIndex());
386
387 std::string actual_klass = dex_dep.first->StringByTypeIdx(method_id.class_idx_);
388 if (expected_klass != actual_klass) {
389 continue;
390 }
391
392 std::string actual_name = dex_dep.first->StringDataByIdx(method_id.name_idx_);
393 if (expected_name != actual_name) {
394 continue;
395 }
396
397 std::string actual_signature = dex_dep.first->GetMethodSignature(method_id).ToString();
398 if (expected_signature != actual_signature) {
399 continue;
400 }
401
402 if (expected_resolved) {
403 // Test access flags. Note that PrettyJavaAccessFlags always appends
404 // a space after the modifiers. Add it to the expected access flags.
405 std::string actual_access_flags = PrettyJavaAccessFlags(entry.GetAccessFlags());
406 if (expected_access_flags + " " != actual_access_flags) {
407 continue;
408 }
409
410 std::string actual_decl_klass = verifier_deps_->GetStringFromId(
411 *dex_dep.first, entry.GetDeclaringClassIndex());
412 if (expected_decl_klass != actual_decl_klass) {
413 continue;
414 }
415 }
416
417 return true;
418 }
419 }
420 return false;
421 }
422
David Brazdil6f82fbd2016-09-14 11:55:26 +0100423 size_t NumberOfCompiledDexFiles() {
David Brazdil6f82fbd2016-09-14 11:55:26 +0100424 return verifier_deps_->dex_deps_.size();
425 }
426
427 size_t HasEachKindOfRecord() {
David Brazdil6f82fbd2016-09-14 11:55:26 +0100428 bool has_strings = false;
429 bool has_assignability = false;
430 bool has_classes = false;
431 bool has_fields = false;
432 bool has_methods = false;
Nicolas Geoffray08025182016-10-25 17:20:18 +0100433 bool has_unverified_classes = false;
David Brazdil6f82fbd2016-09-14 11:55:26 +0100434
435 for (auto& entry : verifier_deps_->dex_deps_) {
436 has_strings |= !entry.second->strings_.empty();
437 has_assignability |= !entry.second->assignable_types_.empty();
438 has_assignability |= !entry.second->unassignable_types_.empty();
439 has_classes |= !entry.second->classes_.empty();
440 has_fields |= !entry.second->fields_.empty();
Vladimir Markoba118822017-06-12 15:41:56 +0100441 has_methods |= !entry.second->methods_.empty();
Nicolas Geoffray08025182016-10-25 17:20:18 +0100442 has_unverified_classes |= !entry.second->unverified_classes_.empty();
David Brazdil6f82fbd2016-09-14 11:55:26 +0100443 }
444
Nicolas Geoffray08025182016-10-25 17:20:18 +0100445 return has_strings &&
446 has_assignability &&
447 has_classes &&
448 has_fields &&
449 has_methods &&
450 has_unverified_classes;
David Brazdil6f82fbd2016-09-14 11:55:26 +0100451 }
452
David Brazdilca3c8c32016-09-06 14:04:48 +0100453 std::unique_ptr<verifier::VerifierDeps> verifier_deps_;
Nicolas Geoffrayd01f60c2016-10-28 14:45:48 +0100454 std::vector<const DexFile*> dex_files_;
455 const DexFile* primary_dex_file_;
David Brazdilca3c8c32016-09-06 14:04:48 +0100456 jobject class_loader_;
457 mirror::Class* klass_Main_;
458};
459
460TEST_F(VerifierDepsTest, StringToId) {
461 ScopedObjectAccess soa(Thread::Current());
462 LoadDexFile(&soa);
463
Andreas Gampe8a0128a2016-11-28 07:38:35 -0800464 dex::StringIndex id_Main1 = verifier_deps_->GetIdFromString(*primary_dex_file_, "LMain;");
465 ASSERT_LT(id_Main1.index_, primary_dex_file_->NumStringIds());
Nicolas Geoffrayd01f60c2016-10-28 14:45:48 +0100466 ASSERT_EQ("LMain;", verifier_deps_->GetStringFromId(*primary_dex_file_, id_Main1));
David Brazdilca3c8c32016-09-06 14:04:48 +0100467
Andreas Gampe8a0128a2016-11-28 07:38:35 -0800468 dex::StringIndex id_Main2 = verifier_deps_->GetIdFromString(*primary_dex_file_, "LMain;");
469 ASSERT_LT(id_Main2.index_, primary_dex_file_->NumStringIds());
Nicolas Geoffrayd01f60c2016-10-28 14:45:48 +0100470 ASSERT_EQ("LMain;", verifier_deps_->GetStringFromId(*primary_dex_file_, id_Main2));
David Brazdilca3c8c32016-09-06 14:04:48 +0100471
Andreas Gampe8a0128a2016-11-28 07:38:35 -0800472 dex::StringIndex id_Lorem1 = verifier_deps_->GetIdFromString(*primary_dex_file_, "Lorem ipsum");
473 ASSERT_GE(id_Lorem1.index_, primary_dex_file_->NumStringIds());
Nicolas Geoffrayd01f60c2016-10-28 14:45:48 +0100474 ASSERT_EQ("Lorem ipsum", verifier_deps_->GetStringFromId(*primary_dex_file_, id_Lorem1));
David Brazdilca3c8c32016-09-06 14:04:48 +0100475
Andreas Gampe8a0128a2016-11-28 07:38:35 -0800476 dex::StringIndex id_Lorem2 = verifier_deps_->GetIdFromString(*primary_dex_file_, "Lorem ipsum");
477 ASSERT_GE(id_Lorem2.index_, primary_dex_file_->NumStringIds());
Nicolas Geoffrayd01f60c2016-10-28 14:45:48 +0100478 ASSERT_EQ("Lorem ipsum", verifier_deps_->GetStringFromId(*primary_dex_file_, id_Lorem2));
David Brazdilca3c8c32016-09-06 14:04:48 +0100479
480 ASSERT_EQ(id_Main1, id_Main2);
481 ASSERT_EQ(id_Lorem1, id_Lorem2);
482 ASSERT_NE(id_Main1, id_Lorem1);
483}
484
485TEST_F(VerifierDepsTest, Assignable_BothInBoot) {
486 ASSERT_TRUE(TestAssignabilityRecording(/* dst */ "Ljava/util/TimeZone;",
487 /* src */ "Ljava/util/SimpleTimeZone;",
488 /* is_strict */ true,
489 /* is_assignable */ true));
490 ASSERT_TRUE(HasAssignable("Ljava/util/TimeZone;", "Ljava/util/SimpleTimeZone;", true));
491}
492
493TEST_F(VerifierDepsTest, Assignable_DestinationInBoot1) {
494 ASSERT_TRUE(TestAssignabilityRecording(/* dst */ "Ljava/net/Socket;",
495 /* src */ "LMySSLSocket;",
496 /* is_strict */ true,
497 /* is_assignable */ true));
Nicolas Geoffray119e8462016-12-21 10:29:43 +0000498 ASSERT_TRUE(HasAssignable("Ljava/net/Socket;", "Ljavax/net/ssl/SSLSocket;", true));
David Brazdilca3c8c32016-09-06 14:04:48 +0100499}
500
501TEST_F(VerifierDepsTest, Assignable_DestinationInBoot2) {
502 ASSERT_TRUE(TestAssignabilityRecording(/* dst */ "Ljava/util/TimeZone;",
503 /* src */ "LMySimpleTimeZone;",
504 /* is_strict */ true,
505 /* is_assignable */ true));
Nicolas Geoffray119e8462016-12-21 10:29:43 +0000506 ASSERT_TRUE(HasAssignable("Ljava/util/TimeZone;", "Ljava/util/SimpleTimeZone;", true));
David Brazdilca3c8c32016-09-06 14:04:48 +0100507}
508
509TEST_F(VerifierDepsTest, Assignable_DestinationInBoot3) {
510 ASSERT_TRUE(TestAssignabilityRecording(/* dst */ "Ljava/util/Collection;",
511 /* src */ "LMyThreadSet;",
512 /* is_strict */ true,
513 /* is_assignable */ true));
Nicolas Geoffray0e2fe0f2016-12-21 16:54:52 +0000514 ASSERT_TRUE(HasAssignable("Ljava/util/Collection;", "Ljava/util/Set;", true));
David Brazdilca3c8c32016-09-06 14:04:48 +0100515}
516
517TEST_F(VerifierDepsTest, Assignable_BothArrays_Resolved) {
518 ASSERT_TRUE(TestAssignabilityRecording(/* dst */ "[[Ljava/util/TimeZone;",
519 /* src */ "[[Ljava/util/SimpleTimeZone;",
520 /* is_strict */ true,
521 /* is_assignable */ true));
522 // If the component types of both arrays are resolved, we optimize the list of
523 // dependencies by recording a dependency on the component types.
524 ASSERT_FALSE(HasAssignable("[[Ljava/util/TimeZone;", "[[Ljava/util/SimpleTimeZone;", true));
525 ASSERT_FALSE(HasAssignable("[Ljava/util/TimeZone;", "[Ljava/util/SimpleTimeZone;", true));
526 ASSERT_TRUE(HasAssignable("Ljava/util/TimeZone;", "Ljava/util/SimpleTimeZone;", true));
527}
528
David Brazdilca3c8c32016-09-06 14:04:48 +0100529TEST_F(VerifierDepsTest, NotAssignable_BothInBoot) {
530 ASSERT_TRUE(TestAssignabilityRecording(/* dst */ "Ljava/lang/Exception;",
531 /* src */ "Ljava/util/SimpleTimeZone;",
532 /* is_strict */ true,
533 /* is_assignable */ false));
534 ASSERT_TRUE(HasAssignable("Ljava/lang/Exception;", "Ljava/util/SimpleTimeZone;", false));
535}
536
537TEST_F(VerifierDepsTest, NotAssignable_DestinationInBoot1) {
538 ASSERT_TRUE(TestAssignabilityRecording(/* dst */ "Ljava/lang/Exception;",
539 /* src */ "LMySSLSocket;",
540 /* is_strict */ true,
541 /* is_assignable */ false));
Nicolas Geoffray119e8462016-12-21 10:29:43 +0000542 ASSERT_TRUE(HasAssignable("Ljava/lang/Exception;", "Ljavax/net/ssl/SSLSocket;", false));
David Brazdilca3c8c32016-09-06 14:04:48 +0100543}
544
545TEST_F(VerifierDepsTest, NotAssignable_DestinationInBoot2) {
546 ASSERT_TRUE(TestAssignabilityRecording(/* dst */ "Ljava/lang/Exception;",
547 /* src */ "LMySimpleTimeZone;",
548 /* is_strict */ true,
549 /* is_assignable */ false));
Nicolas Geoffray119e8462016-12-21 10:29:43 +0000550 ASSERT_TRUE(HasAssignable("Ljava/lang/Exception;", "Ljava/util/SimpleTimeZone;", false));
David Brazdilca3c8c32016-09-06 14:04:48 +0100551}
552
553TEST_F(VerifierDepsTest, NotAssignable_BothArrays) {
554 ASSERT_TRUE(TestAssignabilityRecording(/* dst */ "[Ljava/lang/Exception;",
555 /* src */ "[Ljava/util/SimpleTimeZone;",
556 /* is_strict */ true,
557 /* is_assignable */ false));
558 ASSERT_TRUE(HasAssignable("Ljava/lang/Exception;", "Ljava/util/SimpleTimeZone;", false));
559}
560
561TEST_F(VerifierDepsTest, ArgumentType_ResolvedClass) {
562 ASSERT_TRUE(VerifyMethod("ArgumentType_ResolvedClass"));
563 ASSERT_TRUE(HasClass("Ljava/lang/Thread;", true, "public"));
564}
565
David Brazdilca3c8c32016-09-06 14:04:48 +0100566TEST_F(VerifierDepsTest, ArgumentType_UnresolvedClass) {
567 ASSERT_TRUE(VerifyMethod("ArgumentType_UnresolvedClass"));
568 ASSERT_TRUE(HasClass("LUnresolvedClass;", false));
569}
570
571TEST_F(VerifierDepsTest, ArgumentType_UnresolvedSuper) {
572 ASSERT_TRUE(VerifyMethod("ArgumentType_UnresolvedSuper"));
573 ASSERT_TRUE(HasClass("LMySetWithUnresolvedSuper;", false));
574}
575
576TEST_F(VerifierDepsTest, ReturnType_Reference) {
577 ASSERT_TRUE(VerifyMethod("ReturnType_Reference"));
578 ASSERT_TRUE(HasAssignable("Ljava/lang/Throwable;", "Ljava/lang/IllegalStateException;", true));
579}
580
581TEST_F(VerifierDepsTest, ReturnType_Array) {
582 ASSERT_FALSE(VerifyMethod("ReturnType_Array"));
583 ASSERT_TRUE(HasAssignable("Ljava/lang/Integer;", "Ljava/lang/IllegalStateException;", false));
584}
585
586TEST_F(VerifierDepsTest, InvokeArgumentType) {
587 ASSERT_TRUE(VerifyMethod("InvokeArgumentType"));
588 ASSERT_TRUE(HasClass("Ljava/text/SimpleDateFormat;", true, "public"));
589 ASSERT_TRUE(HasClass("Ljava/util/SimpleTimeZone;", true, "public"));
Vladimir Markoba118822017-06-12 15:41:56 +0100590 ASSERT_TRUE(HasMethod("Ljava/text/SimpleDateFormat;",
David Brazdilca3c8c32016-09-06 14:04:48 +0100591 "setTimeZone",
592 "(Ljava/util/TimeZone;)V",
Vladimir Markoba118822017-06-12 15:41:56 +0100593 /* expect_resolved */ true,
David Brazdilca3c8c32016-09-06 14:04:48 +0100594 "public",
595 "Ljava/text/DateFormat;"));
596 ASSERT_TRUE(HasAssignable("Ljava/util/TimeZone;", "Ljava/util/SimpleTimeZone;", true));
597}
598
599TEST_F(VerifierDepsTest, MergeTypes_RegisterLines) {
600 ASSERT_TRUE(VerifyMethod("MergeTypes_RegisterLines"));
Nicolas Geoffray119e8462016-12-21 10:29:43 +0000601 ASSERT_TRUE(HasAssignable("Ljava/lang/Exception;", "Ljava/net/SocketTimeoutException;", true));
David Brazdilca3c8c32016-09-06 14:04:48 +0100602 ASSERT_TRUE(HasAssignable(
603 "Ljava/lang/Exception;", "Ljava/util/concurrent/TimeoutException;", true));
604}
605
606TEST_F(VerifierDepsTest, MergeTypes_IfInstanceOf) {
607 ASSERT_TRUE(VerifyMethod("MergeTypes_IfInstanceOf"));
608 ASSERT_TRUE(HasAssignable("Ljava/lang/Exception;", "Ljava/net/SocketTimeoutException;", true));
609 ASSERT_TRUE(HasAssignable(
610 "Ljava/lang/Exception;", "Ljava/util/concurrent/TimeoutException;", true));
611 ASSERT_TRUE(HasAssignable("Ljava/net/SocketTimeoutException;", "Ljava/lang/Exception;", false));
612}
613
614TEST_F(VerifierDepsTest, MergeTypes_Unresolved) {
615 ASSERT_TRUE(VerifyMethod("MergeTypes_Unresolved"));
616 ASSERT_TRUE(HasAssignable("Ljava/lang/Exception;", "Ljava/net/SocketTimeoutException;", true));
617 ASSERT_TRUE(HasAssignable(
618 "Ljava/lang/Exception;", "Ljava/util/concurrent/TimeoutException;", true));
619}
620
621TEST_F(VerifierDepsTest, ConstClass_Resolved) {
622 ASSERT_TRUE(VerifyMethod("ConstClass_Resolved"));
623 ASSERT_TRUE(HasClass("Ljava/lang/IllegalStateException;", true, "public"));
624}
625
626TEST_F(VerifierDepsTest, ConstClass_Unresolved) {
627 ASSERT_TRUE(VerifyMethod("ConstClass_Unresolved"));
628 ASSERT_TRUE(HasClass("LUnresolvedClass;", false));
629}
630
631TEST_F(VerifierDepsTest, CheckCast_Resolved) {
632 ASSERT_TRUE(VerifyMethod("CheckCast_Resolved"));
633 ASSERT_TRUE(HasClass("Ljava/lang/IllegalStateException;", true, "public"));
634}
635
636TEST_F(VerifierDepsTest, CheckCast_Unresolved) {
637 ASSERT_TRUE(VerifyMethod("CheckCast_Unresolved"));
638 ASSERT_TRUE(HasClass("LUnresolvedClass;", false));
639}
640
641TEST_F(VerifierDepsTest, InstanceOf_Resolved) {
642 ASSERT_TRUE(VerifyMethod("InstanceOf_Resolved"));
643 ASSERT_TRUE(HasClass("Ljava/lang/IllegalStateException;", true, "public"));
644}
645
646TEST_F(VerifierDepsTest, InstanceOf_Unresolved) {
647 ASSERT_TRUE(VerifyMethod("InstanceOf_Unresolved"));
648 ASSERT_TRUE(HasClass("LUnresolvedClass;", false));
649}
650
651TEST_F(VerifierDepsTest, NewInstance_Resolved) {
652 ASSERT_TRUE(VerifyMethod("NewInstance_Resolved"));
653 ASSERT_TRUE(HasClass("Ljava/lang/IllegalStateException;", true, "public"));
654}
655
656TEST_F(VerifierDepsTest, NewInstance_Unresolved) {
657 ASSERT_TRUE(VerifyMethod("NewInstance_Unresolved"));
658 ASSERT_TRUE(HasClass("LUnresolvedClass;", false));
659}
660
David Brazdilca3c8c32016-09-06 14:04:48 +0100661TEST_F(VerifierDepsTest, NewArray_Unresolved) {
662 ASSERT_TRUE(VerifyMethod("NewArray_Unresolved"));
663 ASSERT_TRUE(HasClass("[LUnresolvedClass;", false));
664}
665
666TEST_F(VerifierDepsTest, Throw) {
667 ASSERT_TRUE(VerifyMethod("Throw"));
668 ASSERT_TRUE(HasAssignable("Ljava/lang/Throwable;", "Ljava/lang/IllegalStateException;", true));
669}
670
671TEST_F(VerifierDepsTest, MoveException_Resolved) {
672 ASSERT_TRUE(VerifyMethod("MoveException_Resolved"));
673 ASSERT_TRUE(HasClass("Ljava/io/InterruptedIOException;", true, "public"));
674 ASSERT_TRUE(HasClass("Ljava/net/SocketTimeoutException;", true, "public"));
675 ASSERT_TRUE(HasClass("Ljava/util/zip/ZipException;", true, "public"));
676
677 // Testing that all exception types are assignable to Throwable.
678 ASSERT_TRUE(HasAssignable("Ljava/lang/Throwable;", "Ljava/io/InterruptedIOException;", true));
679 ASSERT_TRUE(HasAssignable("Ljava/lang/Throwable;", "Ljava/net/SocketTimeoutException;", true));
680 ASSERT_TRUE(HasAssignable("Ljava/lang/Throwable;", "Ljava/util/zip/ZipException;", true));
681
682 // Testing that the merge type is assignable to Throwable.
683 ASSERT_TRUE(HasAssignable("Ljava/lang/Throwable;", "Ljava/io/IOException;", true));
684
685 // Merging of exception types.
686 ASSERT_TRUE(HasAssignable("Ljava/io/IOException;", "Ljava/io/InterruptedIOException;", true));
687 ASSERT_TRUE(HasAssignable("Ljava/io/IOException;", "Ljava/util/zip/ZipException;", true));
688 ASSERT_TRUE(HasAssignable(
689 "Ljava/io/InterruptedIOException;", "Ljava/net/SocketTimeoutException;", true));
690}
691
692TEST_F(VerifierDepsTest, MoveException_Unresolved) {
693 ASSERT_FALSE(VerifyMethod("MoveException_Unresolved"));
694 ASSERT_TRUE(HasClass("LUnresolvedException;", false));
695}
696
697TEST_F(VerifierDepsTest, StaticField_Resolved_DeclaredInReferenced) {
698 ASSERT_TRUE(VerifyMethod("StaticField_Resolved_DeclaredInReferenced"));
Nicolas Geoffray6e54f782017-03-08 15:27:09 +0000699 ASSERT_TRUE(HasClass("Ljava/lang/System;", true, "public"));
David Brazdilca3c8c32016-09-06 14:04:48 +0100700 ASSERT_TRUE(HasField("Ljava/lang/System;",
701 "out",
702 "Ljava/io/PrintStream;",
703 true,
Nicolas Geoffray6e54f782017-03-08 15:27:09 +0000704 "public static",
David Brazdilca3c8c32016-09-06 14:04:48 +0100705 "Ljava/lang/System;"));
706}
707
708TEST_F(VerifierDepsTest, StaticField_Resolved_DeclaredInSuperclass1) {
709 ASSERT_TRUE(VerifyMethod("StaticField_Resolved_DeclaredInSuperclass1"));
710 ASSERT_TRUE(HasClass("Ljava/util/SimpleTimeZone;", true, "public"));
711 ASSERT_TRUE(HasField(
Nicolas Geoffray6e54f782017-03-08 15:27:09 +0000712 "Ljava/util/SimpleTimeZone;", "LONG", "I", true, "public static", "Ljava/util/TimeZone;"));
David Brazdilca3c8c32016-09-06 14:04:48 +0100713}
714
715TEST_F(VerifierDepsTest, StaticField_Resolved_DeclaredInSuperclass2) {
716 ASSERT_TRUE(VerifyMethod("StaticField_Resolved_DeclaredInSuperclass2"));
717 ASSERT_TRUE(HasField(
Nicolas Geoffray6e54f782017-03-08 15:27:09 +0000718 "LMySimpleTimeZone;", "SHORT", "I", true, "public static", "Ljava/util/TimeZone;"));
David Brazdilca3c8c32016-09-06 14:04:48 +0100719}
720
721TEST_F(VerifierDepsTest, StaticField_Resolved_DeclaredInInterface1) {
722 ASSERT_TRUE(VerifyMethod("StaticField_Resolved_DeclaredInInterface1"));
723 ASSERT_TRUE(HasClass("Ljavax/xml/transform/dom/DOMResult;", true, "public"));
724 ASSERT_TRUE(HasField("Ljavax/xml/transform/dom/DOMResult;",
725 "PI_ENABLE_OUTPUT_ESCAPING",
726 "Ljava/lang/String;",
727 true,
Nicolas Geoffray6e54f782017-03-08 15:27:09 +0000728 "public static",
David Brazdilca3c8c32016-09-06 14:04:48 +0100729 "Ljavax/xml/transform/Result;"));
730}
731
732TEST_F(VerifierDepsTest, StaticField_Resolved_DeclaredInInterface2) {
733 ASSERT_TRUE(VerifyMethod("StaticField_Resolved_DeclaredInInterface2"));
734 ASSERT_TRUE(HasField("LMyDOMResult;",
735 "PI_ENABLE_OUTPUT_ESCAPING",
736 "Ljava/lang/String;",
737 true,
Nicolas Geoffray6e54f782017-03-08 15:27:09 +0000738 "public static",
David Brazdilca3c8c32016-09-06 14:04:48 +0100739 "Ljavax/xml/transform/Result;"));
740}
741
742TEST_F(VerifierDepsTest, StaticField_Resolved_DeclaredInInterface3) {
743 ASSERT_TRUE(VerifyMethod("StaticField_Resolved_DeclaredInInterface3"));
744 ASSERT_TRUE(HasField("LMyResult;",
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_DeclaredInInterface4) {
753 ASSERT_TRUE(VerifyMethod("StaticField_Resolved_DeclaredInInterface4"));
754 ASSERT_TRUE(HasField("LMyDocument;",
755 "ELEMENT_NODE",
756 "S",
757 true,
Nicolas Geoffray6e54f782017-03-08 15:27:09 +0000758 "public static",
David Brazdilca3c8c32016-09-06 14:04:48 +0100759 "Lorg/w3c/dom/Node;"));
760}
761
762TEST_F(VerifierDepsTest, StaticField_Unresolved_ReferrerInBoot) {
763 ASSERT_TRUE(VerifyMethod("StaticField_Unresolved_ReferrerInBoot"));
Nicolas Geoffray6e54f782017-03-08 15:27:09 +0000764 ASSERT_TRUE(HasClass("Ljava/util/TimeZone;", true, "public"));
David Brazdilca3c8c32016-09-06 14:04:48 +0100765 ASSERT_TRUE(HasField("Ljava/util/TimeZone;", "x", "I", false));
766}
767
768TEST_F(VerifierDepsTest, StaticField_Unresolved_ReferrerInDex) {
769 ASSERT_TRUE(VerifyMethod("StaticField_Unresolved_ReferrerInDex"));
770 ASSERT_TRUE(HasField("LMyThreadSet;", "x", "I", false));
771}
772
773TEST_F(VerifierDepsTest, InstanceField_Resolved_DeclaredInReferenced) {
774 ASSERT_TRUE(VerifyMethod("InstanceField_Resolved_DeclaredInReferenced"));
775 ASSERT_TRUE(HasClass("Ljava/io/InterruptedIOException;", true, "public"));
776 ASSERT_TRUE(HasField("Ljava/io/InterruptedIOException;",
777 "bytesTransferred",
778 "I",
779 true,
780 "public",
781 "Ljava/io/InterruptedIOException;"));
782 ASSERT_TRUE(HasAssignable(
Nicolas Geoffray119e8462016-12-21 10:29:43 +0000783 "Ljava/io/InterruptedIOException;", "Ljava/net/SocketTimeoutException;", true));
David Brazdilca3c8c32016-09-06 14:04:48 +0100784}
785
786TEST_F(VerifierDepsTest, InstanceField_Resolved_DeclaredInSuperclass1) {
787 ASSERT_TRUE(VerifyMethod("InstanceField_Resolved_DeclaredInSuperclass1"));
788 ASSERT_TRUE(HasClass("Ljava/net/SocketTimeoutException;", true, "public"));
789 ASSERT_TRUE(HasField("Ljava/net/SocketTimeoutException;",
790 "bytesTransferred",
791 "I",
792 true,
793 "public",
794 "Ljava/io/InterruptedIOException;"));
795 ASSERT_TRUE(HasAssignable(
Nicolas Geoffray119e8462016-12-21 10:29:43 +0000796 "Ljava/io/InterruptedIOException;", "Ljava/net/SocketTimeoutException;", true));
David Brazdilca3c8c32016-09-06 14:04:48 +0100797}
798
799TEST_F(VerifierDepsTest, InstanceField_Resolved_DeclaredInSuperclass2) {
800 ASSERT_TRUE(VerifyMethod("InstanceField_Resolved_DeclaredInSuperclass2"));
801 ASSERT_TRUE(HasField("LMySocketTimeoutException;",
802 "bytesTransferred",
803 "I",
804 true,
805 "public",
806 "Ljava/io/InterruptedIOException;"));
807 ASSERT_TRUE(HasAssignable(
Nicolas Geoffray119e8462016-12-21 10:29:43 +0000808 "Ljava/io/InterruptedIOException;", "Ljava/net/SocketTimeoutException;", true));
David Brazdilca3c8c32016-09-06 14:04:48 +0100809}
810
811TEST_F(VerifierDepsTest, InstanceField_Unresolved_ReferrerInBoot) {
812 ASSERT_TRUE(VerifyMethod("InstanceField_Unresolved_ReferrerInBoot"));
813 ASSERT_TRUE(HasClass("Ljava/io/InterruptedIOException;", true, "public"));
814 ASSERT_TRUE(HasField("Ljava/io/InterruptedIOException;", "x", "I", false));
815}
816
817TEST_F(VerifierDepsTest, InstanceField_Unresolved_ReferrerInDex) {
818 ASSERT_TRUE(VerifyMethod("InstanceField_Unresolved_ReferrerInDex"));
819 ASSERT_TRUE(HasField("LMyThreadSet;", "x", "I", false));
820}
821
822TEST_F(VerifierDepsTest, InvokeStatic_Resolved_DeclaredInReferenced) {
823 ASSERT_TRUE(VerifyMethod("InvokeStatic_Resolved_DeclaredInReferenced"));
824 ASSERT_TRUE(HasClass("Ljava/net/Socket;", true, "public"));
Vladimir Markoba118822017-06-12 15:41:56 +0100825 ASSERT_TRUE(HasMethod("Ljava/net/Socket;",
David Brazdilca3c8c32016-09-06 14:04:48 +0100826 "setSocketImplFactory",
827 "(Ljava/net/SocketImplFactory;)V",
Vladimir Markoba118822017-06-12 15:41:56 +0100828 /* expect_resolved */ true,
David Brazdilca3c8c32016-09-06 14:04:48 +0100829 "public static",
830 "Ljava/net/Socket;"));
831}
832
833TEST_F(VerifierDepsTest, InvokeStatic_Resolved_DeclaredInSuperclass1) {
834 ASSERT_TRUE(VerifyMethod("InvokeStatic_Resolved_DeclaredInSuperclass1"));
Nicolas Geoffray6e54f782017-03-08 15:27:09 +0000835 ASSERT_TRUE(HasClass("Ljavax/net/ssl/SSLSocket;", true, "public"));
Vladimir Markoba118822017-06-12 15:41:56 +0100836 ASSERT_TRUE(HasMethod("Ljavax/net/ssl/SSLSocket;",
David Brazdilca3c8c32016-09-06 14:04:48 +0100837 "setSocketImplFactory",
838 "(Ljava/net/SocketImplFactory;)V",
Vladimir Markoba118822017-06-12 15:41:56 +0100839 /* expect_resolved */ true,
David Brazdilca3c8c32016-09-06 14:04:48 +0100840 "public static",
841 "Ljava/net/Socket;"));
842}
843
844TEST_F(VerifierDepsTest, InvokeStatic_Resolved_DeclaredInSuperclass2) {
845 ASSERT_TRUE(VerifyMethod("InvokeStatic_Resolved_DeclaredInSuperclass2"));
Vladimir Markoba118822017-06-12 15:41:56 +0100846 ASSERT_TRUE(HasMethod("LMySSLSocket;",
David Brazdilca3c8c32016-09-06 14:04:48 +0100847 "setSocketImplFactory",
848 "(Ljava/net/SocketImplFactory;)V",
Vladimir Markoba118822017-06-12 15:41:56 +0100849 /* expect_resolved */ true,
David Brazdilca3c8c32016-09-06 14:04:48 +0100850 "public static",
851 "Ljava/net/Socket;"));
852}
853
854TEST_F(VerifierDepsTest, InvokeStatic_DeclaredInInterface1) {
855 ASSERT_TRUE(VerifyMethod("InvokeStatic_DeclaredInInterface1"));
Nicolas Geoffray6e54f782017-03-08 15:27:09 +0000856 ASSERT_TRUE(HasClass("Ljava/util/Map$Entry;", true, "public interface"));
Vladimir Markoba118822017-06-12 15:41:56 +0100857 ASSERT_TRUE(HasMethod("Ljava/util/Map$Entry;",
David Brazdilca3c8c32016-09-06 14:04:48 +0100858 "comparingByKey",
859 "()Ljava/util/Comparator;",
Vladimir Markoba118822017-06-12 15:41:56 +0100860 /* expect_resolved */ true,
David Brazdilca3c8c32016-09-06 14:04:48 +0100861 "public static",
862 "Ljava/util/Map$Entry;"));
863}
864
865TEST_F(VerifierDepsTest, InvokeStatic_DeclaredInInterface2) {
866 ASSERT_FALSE(VerifyMethod("InvokeStatic_DeclaredInInterface2"));
867 ASSERT_TRUE(HasClass("Ljava/util/AbstractMap$SimpleEntry;", true, "public"));
Vladimir Markoba118822017-06-12 15:41:56 +0100868 ASSERT_TRUE(HasMethod("Ljava/util/AbstractMap$SimpleEntry;",
David Brazdilca3c8c32016-09-06 14:04:48 +0100869 "comparingByKey",
870 "()Ljava/util/Comparator;",
Vladimir Markoba118822017-06-12 15:41:56 +0100871 /* expect_resolved */ false));
David Brazdilca3c8c32016-09-06 14:04:48 +0100872}
873
874TEST_F(VerifierDepsTest, InvokeStatic_Unresolved1) {
875 ASSERT_FALSE(VerifyMethod("InvokeStatic_Unresolved1"));
Nicolas Geoffray6e54f782017-03-08 15:27:09 +0000876 ASSERT_TRUE(HasClass("Ljavax/net/ssl/SSLSocket;", true, "public"));
Vladimir Markoba118822017-06-12 15:41:56 +0100877 ASSERT_TRUE(HasMethod("Ljavax/net/ssl/SSLSocket;",
878 "x",
879 "()V",
880 /* expect_resolved */ false));
David Brazdilca3c8c32016-09-06 14:04:48 +0100881}
882
883TEST_F(VerifierDepsTest, InvokeStatic_Unresolved2) {
884 ASSERT_FALSE(VerifyMethod("InvokeStatic_Unresolved2"));
Vladimir Markoba118822017-06-12 15:41:56 +0100885 ASSERT_TRUE(HasMethod("LMySSLSocket;",
886 "x",
887 "()V",
888 /* expect_resolved */ false));
David Brazdilca3c8c32016-09-06 14:04:48 +0100889}
890
891TEST_F(VerifierDepsTest, InvokeDirect_Resolved_DeclaredInReferenced) {
892 ASSERT_TRUE(VerifyMethod("InvokeDirect_Resolved_DeclaredInReferenced"));
893 ASSERT_TRUE(HasClass("Ljava/net/Socket;", true, "public"));
Vladimir Markoba118822017-06-12 15:41:56 +0100894 ASSERT_TRUE(HasMethod("Ljava/net/Socket;",
895 "<init>",
896 "()V",
897 /* expect_resolved */ true,
898 "public",
899 "Ljava/net/Socket;"));
David Brazdilca3c8c32016-09-06 14:04:48 +0100900}
901
902TEST_F(VerifierDepsTest, InvokeDirect_Resolved_DeclaredInSuperclass1) {
903 ASSERT_FALSE(VerifyMethod("InvokeDirect_Resolved_DeclaredInSuperclass1"));
Nicolas Geoffray6e54f782017-03-08 15:27:09 +0000904 ASSERT_TRUE(HasClass("Ljavax/net/ssl/SSLSocket;", true, "public"));
Vladimir Markoba118822017-06-12 15:41:56 +0100905 ASSERT_TRUE(HasMethod("Ljavax/net/ssl/SSLSocket;",
David Brazdilca3c8c32016-09-06 14:04:48 +0100906 "checkOldImpl",
907 "()V",
Vladimir Markoba118822017-06-12 15:41:56 +0100908 /* expect_resolved */ true,
David Brazdilca3c8c32016-09-06 14:04:48 +0100909 "private",
910 "Ljava/net/Socket;"));
911}
912
913TEST_F(VerifierDepsTest, InvokeDirect_Resolved_DeclaredInSuperclass2) {
914 ASSERT_FALSE(VerifyMethod("InvokeDirect_Resolved_DeclaredInSuperclass2"));
Vladimir Markoba118822017-06-12 15:41:56 +0100915 ASSERT_TRUE(HasMethod("LMySSLSocket;",
916 "checkOldImpl",
917 "()V",
918 /* expect_resolved */ true,
919 "private",
920 "Ljava/net/Socket;"));
David Brazdilca3c8c32016-09-06 14:04:48 +0100921}
922
923TEST_F(VerifierDepsTest, InvokeDirect_Unresolved1) {
924 ASSERT_FALSE(VerifyMethod("InvokeDirect_Unresolved1"));
Nicolas Geoffray6e54f782017-03-08 15:27:09 +0000925 ASSERT_TRUE(HasClass("Ljavax/net/ssl/SSLSocket;", true, "public"));
Vladimir Markoba118822017-06-12 15:41:56 +0100926 ASSERT_TRUE(HasMethod("Ljavax/net/ssl/SSLSocket;",
927 "x",
928 "()V",
929 /* expect_resolved */ false));
David Brazdilca3c8c32016-09-06 14:04:48 +0100930}
931
932TEST_F(VerifierDepsTest, InvokeDirect_Unresolved2) {
933 ASSERT_FALSE(VerifyMethod("InvokeDirect_Unresolved2"));
Vladimir Markoba118822017-06-12 15:41:56 +0100934 ASSERT_TRUE(HasMethod("LMySSLSocket;",
935 "x",
936 "()V",
937 /* expect_resolved */ false));
David Brazdilca3c8c32016-09-06 14:04:48 +0100938}
939
940TEST_F(VerifierDepsTest, InvokeVirtual_Resolved_DeclaredInReferenced) {
941 ASSERT_TRUE(VerifyMethod("InvokeVirtual_Resolved_DeclaredInReferenced"));
942 ASSERT_TRUE(HasClass("Ljava/lang/Throwable;", true, "public"));
Vladimir Markoba118822017-06-12 15:41:56 +0100943 ASSERT_TRUE(HasMethod("Ljava/lang/Throwable;",
David Brazdilca3c8c32016-09-06 14:04:48 +0100944 "getMessage",
945 "()Ljava/lang/String;",
Vladimir Markoba118822017-06-12 15:41:56 +0100946 /* expect_resolved */ true,
David Brazdilca3c8c32016-09-06 14:04:48 +0100947 "public",
948 "Ljava/lang/Throwable;"));
949 // Type dependency on `this` argument.
Nicolas Geoffray119e8462016-12-21 10:29:43 +0000950 ASSERT_TRUE(HasAssignable("Ljava/lang/Throwable;", "Ljava/net/SocketTimeoutException;", true));
David Brazdilca3c8c32016-09-06 14:04:48 +0100951}
952
953TEST_F(VerifierDepsTest, InvokeVirtual_Resolved_DeclaredInSuperclass1) {
954 ASSERT_TRUE(VerifyMethod("InvokeVirtual_Resolved_DeclaredInSuperclass1"));
955 ASSERT_TRUE(HasClass("Ljava/io/InterruptedIOException;", true, "public"));
Vladimir Markoba118822017-06-12 15:41:56 +0100956 ASSERT_TRUE(HasMethod("Ljava/io/InterruptedIOException;",
David Brazdilca3c8c32016-09-06 14:04:48 +0100957 "getMessage",
958 "()Ljava/lang/String;",
Vladimir Markoba118822017-06-12 15:41:56 +0100959 /* expect_resolved */ true,
David Brazdilca3c8c32016-09-06 14:04:48 +0100960 "public",
961 "Ljava/lang/Throwable;"));
962 // Type dependency on `this` argument.
Nicolas Geoffray119e8462016-12-21 10:29:43 +0000963 ASSERT_TRUE(HasAssignable("Ljava/lang/Throwable;", "Ljava/net/SocketTimeoutException;", true));
David Brazdilca3c8c32016-09-06 14:04:48 +0100964}
965
966TEST_F(VerifierDepsTest, InvokeVirtual_Resolved_DeclaredInSuperclass2) {
967 ASSERT_TRUE(VerifyMethod("InvokeVirtual_Resolved_DeclaredInSuperclass2"));
Vladimir Markoba118822017-06-12 15:41:56 +0100968 ASSERT_TRUE(HasMethod("LMySocketTimeoutException;",
David Brazdilca3c8c32016-09-06 14:04:48 +0100969 "getMessage",
970 "()Ljava/lang/String;",
Vladimir Markoba118822017-06-12 15:41:56 +0100971 /* expect_resolved */ true,
David Brazdilca3c8c32016-09-06 14:04:48 +0100972 "public",
973 "Ljava/lang/Throwable;"));
974}
975
976TEST_F(VerifierDepsTest, InvokeVirtual_Resolved_DeclaredInSuperinterface) {
977 ASSERT_TRUE(VerifyMethod("InvokeVirtual_Resolved_DeclaredInSuperinterface"));
Vladimir Markoba118822017-06-12 15:41:56 +0100978 ASSERT_TRUE(HasMethod("LMyThreadSet;",
David Brazdilca3c8c32016-09-06 14:04:48 +0100979 "size",
980 "()I",
Vladimir Markoba118822017-06-12 15:41:56 +0100981 /* expect_resolved */ true,
Nicolas Geoffray6e54f782017-03-08 15:27:09 +0000982 "public",
David Brazdilca3c8c32016-09-06 14:04:48 +0100983 "Ljava/util/Set;"));
984}
985
986TEST_F(VerifierDepsTest, InvokeVirtual_Unresolved1) {
987 ASSERT_FALSE(VerifyMethod("InvokeVirtual_Unresolved1"));
988 ASSERT_TRUE(HasClass("Ljava/io/InterruptedIOException;", true, "public"));
Vladimir Markoba118822017-06-12 15:41:56 +0100989 ASSERT_TRUE(HasMethod("Ljava/io/InterruptedIOException;",
990 "x",
991 "()V",
992 /* expect_resolved */ false));
David Brazdilca3c8c32016-09-06 14:04:48 +0100993}
994
995TEST_F(VerifierDepsTest, InvokeVirtual_Unresolved2) {
996 ASSERT_FALSE(VerifyMethod("InvokeVirtual_Unresolved2"));
Vladimir Markoba118822017-06-12 15:41:56 +0100997 ASSERT_TRUE(HasMethod("LMySocketTimeoutException;",
998 "x",
999 "()V",
1000 /* expect_resolved */ false));
David Brazdilca3c8c32016-09-06 14:04:48 +01001001}
1002
1003TEST_F(VerifierDepsTest, InvokeInterface_Resolved_DeclaredInReferenced) {
1004 ASSERT_TRUE(VerifyMethod("InvokeInterface_Resolved_DeclaredInReferenced"));
Nicolas Geoffray6e54f782017-03-08 15:27:09 +00001005 ASSERT_TRUE(HasClass("Ljava/lang/Runnable;", true, "public interface"));
Vladimir Markoba118822017-06-12 15:41:56 +01001006 ASSERT_TRUE(HasMethod("Ljava/lang/Runnable;",
David Brazdilca3c8c32016-09-06 14:04:48 +01001007 "run",
1008 "()V",
Vladimir Markoba118822017-06-12 15:41:56 +01001009 /* expect_resolved */ true,
Nicolas Geoffray6e54f782017-03-08 15:27:09 +00001010 "public",
David Brazdilca3c8c32016-09-06 14:04:48 +01001011 "Ljava/lang/Runnable;"));
1012}
1013
1014TEST_F(VerifierDepsTest, InvokeInterface_Resolved_DeclaredInSuperclass) {
1015 ASSERT_FALSE(VerifyMethod("InvokeInterface_Resolved_DeclaredInSuperclass"));
Vladimir Markoba118822017-06-12 15:41:56 +01001016 // TODO: Maybe we should not record dependency if the invoke type does not match the lookup type.
1017 ASSERT_TRUE(HasMethod("LMyThread;",
1018 "join",
1019 "()V",
1020 /* expect_resolved */ true,
1021 "public",
1022 "Ljava/lang/Thread;"));
David Brazdilca3c8c32016-09-06 14:04:48 +01001023}
1024
1025TEST_F(VerifierDepsTest, InvokeInterface_Resolved_DeclaredInSuperinterface1) {
1026 ASSERT_FALSE(VerifyMethod("InvokeInterface_Resolved_DeclaredInSuperinterface1"));
Vladimir Markoba118822017-06-12 15:41:56 +01001027 // TODO: Maybe we should not record dependency if the invoke type does not match the lookup type.
1028 ASSERT_TRUE(HasMethod("LMyThreadSet;",
David Brazdilca3c8c32016-09-06 14:04:48 +01001029 "run",
1030 "()V",
Vladimir Markoba118822017-06-12 15:41:56 +01001031 /* expect_resolved */ true,
Nicolas Geoffray6e54f782017-03-08 15:27:09 +00001032 "public",
Vladimir Markoba118822017-06-12 15:41:56 +01001033 "Ljava/lang/Thread;"));
David Brazdilca3c8c32016-09-06 14:04:48 +01001034}
1035
1036TEST_F(VerifierDepsTest, InvokeInterface_Resolved_DeclaredInSuperinterface2) {
1037 ASSERT_FALSE(VerifyMethod("InvokeInterface_Resolved_DeclaredInSuperinterface2"));
Vladimir Markoba118822017-06-12 15:41:56 +01001038 ASSERT_TRUE(HasMethod("LMyThreadSet;",
David Brazdilca3c8c32016-09-06 14:04:48 +01001039 "isEmpty",
1040 "()Z",
Vladimir Markoba118822017-06-12 15:41:56 +01001041 /* expect_resolved */ true,
Nicolas Geoffray6e54f782017-03-08 15:27:09 +00001042 "public",
David Brazdilca3c8c32016-09-06 14:04:48 +01001043 "Ljava/util/Set;"));
1044}
1045
1046TEST_F(VerifierDepsTest, InvokeInterface_Unresolved1) {
1047 ASSERT_FALSE(VerifyMethod("InvokeInterface_Unresolved1"));
Nicolas Geoffray6e54f782017-03-08 15:27:09 +00001048 ASSERT_TRUE(HasClass("Ljava/lang/Runnable;", true, "public interface"));
Vladimir Markoba118822017-06-12 15:41:56 +01001049 ASSERT_TRUE(HasMethod("Ljava/lang/Runnable;",
1050 "x",
1051 "()V",
1052 /* expect_resolved */ false));
David Brazdilca3c8c32016-09-06 14:04:48 +01001053}
1054
1055TEST_F(VerifierDepsTest, InvokeInterface_Unresolved2) {
1056 ASSERT_FALSE(VerifyMethod("InvokeInterface_Unresolved2"));
Vladimir Markoba118822017-06-12 15:41:56 +01001057 ASSERT_TRUE(HasMethod("LMyThreadSet;", "x", "()V", /* expect_resolved */ false));
David Brazdilca3c8c32016-09-06 14:04:48 +01001058}
1059
1060TEST_F(VerifierDepsTest, InvokeSuper_ThisAssignable) {
1061 ASSERT_TRUE(VerifyMethod("InvokeSuper_ThisAssignable"));
Nicolas Geoffray6e54f782017-03-08 15:27:09 +00001062 ASSERT_TRUE(HasClass("Ljava/lang/Runnable;", true, "public interface"));
Nicolas Geoffray0e2fe0f2016-12-21 16:54:52 +00001063 ASSERT_TRUE(HasAssignable("Ljava/lang/Runnable;", "Ljava/lang/Thread;", true));
Vladimir Markoba118822017-06-12 15:41:56 +01001064 ASSERT_TRUE(HasMethod("Ljava/lang/Runnable;",
David Brazdilca3c8c32016-09-06 14:04:48 +01001065 "run",
1066 "()V",
Vladimir Markoba118822017-06-12 15:41:56 +01001067 /* expect_resolved */ true,
Nicolas Geoffray6e54f782017-03-08 15:27:09 +00001068 "public",
David Brazdilca3c8c32016-09-06 14:04:48 +01001069 "Ljava/lang/Runnable;"));
1070}
1071
1072TEST_F(VerifierDepsTest, InvokeSuper_ThisNotAssignable) {
1073 ASSERT_FALSE(VerifyMethod("InvokeSuper_ThisNotAssignable"));
Nicolas Geoffray6e54f782017-03-08 15:27:09 +00001074 ASSERT_TRUE(HasClass("Ljava/lang/Integer;", true, "public"));
Nicolas Geoffray119e8462016-12-21 10:29:43 +00001075 ASSERT_TRUE(HasAssignable("Ljava/lang/Integer;", "Ljava/lang/Thread;", false));
Vladimir Markoba118822017-06-12 15:41:56 +01001076 ASSERT_TRUE(HasMethod("Ljava/lang/Integer;",
1077 "intValue", "()I",
1078 /* expect_resolved */ true,
1079 "public", "Ljava/lang/Integer;"));
David Brazdilca3c8c32016-09-06 14:04:48 +01001080}
1081
Nicolas Geoffray0f1cb172017-01-05 15:23:19 +00001082TEST_F(VerifierDepsTest, ArgumentType_ResolvedReferenceArray) {
1083 ASSERT_TRUE(VerifyMethod("ArgumentType_ResolvedReferenceArray"));
Nicolas Geoffray6e54f782017-03-08 15:27:09 +00001084 ASSERT_TRUE(HasClass("[Ljava/lang/Thread;", true, "public"));
Nicolas Geoffray0f1cb172017-01-05 15:23:19 +00001085}
1086
1087TEST_F(VerifierDepsTest, NewArray_Resolved) {
1088 ASSERT_TRUE(VerifyMethod("NewArray_Resolved"));
Nicolas Geoffray6e54f782017-03-08 15:27:09 +00001089 ASSERT_TRUE(HasClass("[Ljava/lang/IllegalStateException;", true, "public"));
Nicolas Geoffray0f1cb172017-01-05 15:23:19 +00001090}
1091
David Brazdil6f82fbd2016-09-14 11:55:26 +01001092TEST_F(VerifierDepsTest, EncodeDecode) {
1093 VerifyDexFile();
1094
1095 ASSERT_EQ(1u, NumberOfCompiledDexFiles());
1096 ASSERT_TRUE(HasEachKindOfRecord());
1097
1098 std::vector<uint8_t> buffer;
Nicolas Geoffrayd01f60c2016-10-28 14:45:48 +01001099 verifier_deps_->Encode(dex_files_, &buffer);
David Brazdil6f82fbd2016-09-14 11:55:26 +01001100 ASSERT_FALSE(buffer.empty());
1101
Nicolas Geoffraye70dd562016-10-30 21:03:35 +00001102 VerifierDeps decoded_deps(dex_files_, ArrayRef<const uint8_t>(buffer));
David Brazdil6f82fbd2016-09-14 11:55:26 +01001103 ASSERT_TRUE(verifier_deps_->Equals(decoded_deps));
1104}
1105
Nicolas Geoffrayd01f60c2016-10-28 14:45:48 +01001106TEST_F(VerifierDepsTest, EncodeDecodeMulti) {
1107 VerifyDexFile("MultiDex");
1108
1109 ASSERT_GT(NumberOfCompiledDexFiles(), 1u);
1110 std::vector<uint8_t> buffer;
1111 verifier_deps_->Encode(dex_files_, &buffer);
1112 ASSERT_FALSE(buffer.empty());
1113
1114 // Create new DexFile, to mess with std::map order: the verifier deps used
1115 // to iterate over the map, which doesn't guarantee insertion order. We fixed
1116 // this by passing the expected order when encoding/decoding.
1117 std::vector<std::unique_ptr<const DexFile>> first_dex_files = OpenTestDexFiles("VerifierDeps");
1118 std::vector<std::unique_ptr<const DexFile>> second_dex_files = OpenTestDexFiles("MultiDex");
1119 std::vector<const DexFile*> dex_files;
1120 for (auto& dex_file : first_dex_files) {
1121 dex_files.push_back(dex_file.get());
1122 }
1123 for (auto& dex_file : second_dex_files) {
1124 dex_files.push_back(dex_file.get());
1125 }
1126
1127 // Dump the new verifier deps to ensure it can properly read the data.
Nicolas Geoffraye70dd562016-10-30 21:03:35 +00001128 VerifierDeps decoded_deps(dex_files, ArrayRef<const uint8_t>(buffer));
Nicolas Geoffrayd01f60c2016-10-28 14:45:48 +01001129 std::ostringstream stream;
1130 VariableIndentationOutputStream os(&stream);
1131 decoded_deps.Dump(&os);
1132}
1133
Nicolas Geoffray08025182016-10-25 17:20:18 +01001134TEST_F(VerifierDepsTest, UnverifiedClasses) {
1135 VerifyDexFile();
1136 ASSERT_FALSE(HasUnverifiedClass("LMyThread;"));
1137 // Test that a class with a soft failure is recorded.
1138 ASSERT_TRUE(HasUnverifiedClass("LMain;"));
1139 // Test that a class with hard failure is recorded.
1140 ASSERT_TRUE(HasUnverifiedClass("LMyVerificationFailure;"));
1141 // Test that a class with unresolved super is recorded.
Nicolas Geoffray7cc3ae52017-03-07 14:33:37 +00001142 ASSERT_TRUE(HasUnverifiedClass("LMyClassWithNoSuper;"));
Nicolas Geoffray08025182016-10-25 17:20:18 +01001143 // Test that a class with unresolved super and hard failure is recorded.
1144 ASSERT_TRUE(HasUnverifiedClass("LMyClassWithNoSuperButFailures;"));
1145}
1146
Nicolas Geoffray8904b6f2016-10-28 19:50:34 +01001147TEST_F(VerifierDepsTest, VerifyDeps) {
1148 VerifyDexFile();
1149
1150 ASSERT_EQ(1u, NumberOfCompiledDexFiles());
1151 ASSERT_TRUE(HasEachKindOfRecord());
1152
1153 // When validating, we create a new class loader, as
1154 // the existing `class_loader_` may contain erroneous classes,
1155 // that ClassLinker::FindClass won't return.
1156
1157 ScopedObjectAccess soa(Thread::Current());
1158 StackHandleScope<1> hs(soa.Self());
1159 MutableHandle<mirror::ClassLoader> new_class_loader(hs.NewHandle<mirror::ClassLoader>(nullptr));
1160 {
1161 new_class_loader.Assign(soa.Decode<mirror::ClassLoader>(LoadDex("VerifierDeps")));
Nicolas Geoffray6bb7f1b2016-11-03 10:52:49 +00001162 ASSERT_TRUE(verifier_deps_->ValidateDependencies(new_class_loader, soa.Self()));
Nicolas Geoffray8904b6f2016-10-28 19:50:34 +01001163 }
1164
1165 std::vector<uint8_t> buffer;
1166 verifier_deps_->Encode(dex_files_, &buffer);
1167 ASSERT_FALSE(buffer.empty());
1168
1169 {
1170 VerifierDeps decoded_deps(dex_files_, ArrayRef<const uint8_t>(buffer));
1171 new_class_loader.Assign(soa.Decode<mirror::ClassLoader>(LoadDex("VerifierDeps")));
Nicolas Geoffray6bb7f1b2016-11-03 10:52:49 +00001172 ASSERT_TRUE(decoded_deps.ValidateDependencies(new_class_loader, soa.Self()));
Nicolas Geoffray8904b6f2016-10-28 19:50:34 +01001173 }
1174
1175 // Fiddle with the dependencies to make sure we catch any change and fail to verify.
1176
1177 {
1178 // Mess up with the assignable_types.
1179 VerifierDeps decoded_deps(dex_files_, ArrayRef<const uint8_t>(buffer));
1180 VerifierDeps::DexFileDeps* deps = decoded_deps.GetDexFileDeps(*primary_dex_file_);
1181 deps->assignable_types_.insert(*deps->unassignable_types_.begin());
1182 new_class_loader.Assign(soa.Decode<mirror::ClassLoader>(LoadDex("VerifierDeps")));
Nicolas Geoffray6bb7f1b2016-11-03 10:52:49 +00001183 ASSERT_FALSE(decoded_deps.ValidateDependencies(new_class_loader, soa.Self()));
Nicolas Geoffray8904b6f2016-10-28 19:50:34 +01001184 }
1185
1186 {
1187 // Mess up with the unassignable_types.
1188 VerifierDeps decoded_deps(dex_files_, ArrayRef<const uint8_t>(buffer));
1189 VerifierDeps::DexFileDeps* deps = decoded_deps.GetDexFileDeps(*primary_dex_file_);
1190 deps->unassignable_types_.insert(*deps->assignable_types_.begin());
1191 new_class_loader.Assign(soa.Decode<mirror::ClassLoader>(LoadDex("VerifierDeps")));
Nicolas Geoffray6bb7f1b2016-11-03 10:52:49 +00001192 ASSERT_FALSE(decoded_deps.ValidateDependencies(new_class_loader, soa.Self()));
Nicolas Geoffray8904b6f2016-10-28 19:50:34 +01001193 }
1194
1195 // Mess up with classes.
1196 {
1197 VerifierDeps decoded_deps(dex_files_, ArrayRef<const uint8_t>(buffer));
1198 VerifierDeps::DexFileDeps* deps = decoded_deps.GetDexFileDeps(*primary_dex_file_);
1199 bool found = false;
1200 for (const auto& entry : deps->classes_) {
1201 if (entry.IsResolved()) {
1202 deps->classes_.insert(VerifierDeps::ClassResolution(
1203 entry.GetDexTypeIndex(), VerifierDeps::kUnresolvedMarker));
1204 found = true;
1205 break;
1206 }
1207 }
1208 ASSERT_TRUE(found);
1209 new_class_loader.Assign(soa.Decode<mirror::ClassLoader>(LoadDex("VerifierDeps")));
Nicolas Geoffray6bb7f1b2016-11-03 10:52:49 +00001210 ASSERT_FALSE(decoded_deps.ValidateDependencies(new_class_loader, soa.Self()));
Nicolas Geoffray8904b6f2016-10-28 19:50:34 +01001211 }
1212
1213 {
1214 VerifierDeps decoded_deps(dex_files_, ArrayRef<const uint8_t>(buffer));
1215 VerifierDeps::DexFileDeps* deps = decoded_deps.GetDexFileDeps(*primary_dex_file_);
1216 bool found = false;
1217 for (const auto& entry : deps->classes_) {
1218 if (!entry.IsResolved()) {
1219 deps->classes_.insert(VerifierDeps::ClassResolution(
1220 entry.GetDexTypeIndex(), VerifierDeps::kUnresolvedMarker - 1));
1221 found = true;
1222 break;
1223 }
1224 }
1225 ASSERT_TRUE(found);
1226 new_class_loader.Assign(soa.Decode<mirror::ClassLoader>(LoadDex("VerifierDeps")));
Nicolas Geoffray6bb7f1b2016-11-03 10:52:49 +00001227 ASSERT_FALSE(decoded_deps.ValidateDependencies(new_class_loader, soa.Self()));
Nicolas Geoffray8904b6f2016-10-28 19:50:34 +01001228 }
1229
1230 {
1231 VerifierDeps decoded_deps(dex_files_, ArrayRef<const uint8_t>(buffer));
1232 VerifierDeps::DexFileDeps* deps = decoded_deps.GetDexFileDeps(*primary_dex_file_);
1233 bool found = false;
1234 for (const auto& entry : deps->classes_) {
1235 if (entry.IsResolved()) {
1236 deps->classes_.insert(VerifierDeps::ClassResolution(
1237 entry.GetDexTypeIndex(), entry.GetAccessFlags() - 1));
1238 found = true;
1239 break;
1240 }
1241 }
1242 ASSERT_TRUE(found);
1243 new_class_loader.Assign(soa.Decode<mirror::ClassLoader>(LoadDex("VerifierDeps")));
Nicolas Geoffray6bb7f1b2016-11-03 10:52:49 +00001244 ASSERT_FALSE(decoded_deps.ValidateDependencies(new_class_loader, soa.Self()));
Nicolas Geoffray8904b6f2016-10-28 19:50:34 +01001245 }
1246
1247 // Mess up with fields.
1248 {
1249 VerifierDeps decoded_deps(dex_files_, ArrayRef<const uint8_t>(buffer));
1250 VerifierDeps::DexFileDeps* deps = decoded_deps.GetDexFileDeps(*primary_dex_file_);
1251 bool found = false;
1252 for (const auto& entry : deps->fields_) {
1253 if (entry.IsResolved()) {
1254 deps->fields_.insert(VerifierDeps::FieldResolution(entry.GetDexFieldIndex(),
1255 VerifierDeps::kUnresolvedMarker,
1256 entry.GetDeclaringClassIndex()));
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 {
1267 VerifierDeps decoded_deps(dex_files_, ArrayRef<const uint8_t>(buffer));
1268 VerifierDeps::DexFileDeps* deps = decoded_deps.GetDexFileDeps(*primary_dex_file_);
1269 bool found = false;
1270 for (const auto& entry : deps->fields_) {
1271 if (!entry.IsResolved()) {
Andreas Gampe8a0128a2016-11-28 07:38:35 -08001272 constexpr dex::StringIndex kStringIndexZero(0); // We know there is a class there.
Nicolas Geoffray8904b6f2016-10-28 19:50:34 +01001273 deps->fields_.insert(VerifierDeps::FieldResolution(0 /* we know there is a field there */,
1274 VerifierDeps::kUnresolvedMarker - 1,
Andreas Gampe8a0128a2016-11-28 07:38:35 -08001275 kStringIndexZero));
Nicolas Geoffray8904b6f2016-10-28 19:50:34 +01001276 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()) {
1291 deps->fields_.insert(VerifierDeps::FieldResolution(entry.GetDexFieldIndex(),
1292 entry.GetAccessFlags() - 1,
1293 entry.GetDeclaringClassIndex()));
1294 found = true;
1295 break;
1296 }
1297 }
1298 ASSERT_TRUE(found);
1299 new_class_loader.Assign(soa.Decode<mirror::ClassLoader>(LoadDex("VerifierDeps")));
Nicolas Geoffray6bb7f1b2016-11-03 10:52:49 +00001300 ASSERT_FALSE(decoded_deps.ValidateDependencies(new_class_loader, soa.Self()));
Nicolas Geoffray8904b6f2016-10-28 19:50:34 +01001301 }
1302
1303 {
1304 VerifierDeps decoded_deps(dex_files_, ArrayRef<const uint8_t>(buffer));
1305 VerifierDeps::DexFileDeps* deps = decoded_deps.GetDexFileDeps(*primary_dex_file_);
1306 bool found = false;
1307 for (const auto& entry : deps->fields_) {
Andreas Gampe8a0128a2016-11-28 07:38:35 -08001308 constexpr dex::StringIndex kNewTypeIndex(0);
Nicolas Geoffray8904b6f2016-10-28 19:50:34 +01001309 if (entry.GetDeclaringClassIndex() != kNewTypeIndex) {
1310 deps->fields_.insert(VerifierDeps::FieldResolution(entry.GetDexFieldIndex(),
1311 entry.GetAccessFlags(),
1312 kNewTypeIndex));
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 // Mess up with methods.
Vladimir Markoba118822017-06-12 15:41:56 +01001323 {
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 std::set<VerifierDeps::MethodResolution>* methods = &deps->methods_;
1328 for (const auto& entry : *methods) {
1329 if (entry.IsResolved()) {
1330 methods->insert(VerifierDeps::MethodResolution(entry.GetDexMethodIndex(),
1331 VerifierDeps::kUnresolvedMarker,
1332 entry.GetDeclaringClassIndex()));
1333 found = true;
1334 break;
Nicolas Geoffray8904b6f2016-10-28 19:50:34 +01001335 }
Nicolas Geoffray8904b6f2016-10-28 19:50:34 +01001336 }
Vladimir Markoba118822017-06-12 15:41:56 +01001337 ASSERT_TRUE(found);
1338 new_class_loader.Assign(soa.Decode<mirror::ClassLoader>(LoadDex("VerifierDeps")));
1339 ASSERT_FALSE(decoded_deps.ValidateDependencies(new_class_loader, soa.Self()));
1340 }
Nicolas Geoffray8904b6f2016-10-28 19:50:34 +01001341
Vladimir Markoba118822017-06-12 15:41:56 +01001342 {
1343 VerifierDeps decoded_deps(dex_files_, ArrayRef<const uint8_t>(buffer));
1344 VerifierDeps::DexFileDeps* deps = decoded_deps.GetDexFileDeps(*primary_dex_file_);
1345 bool found = false;
1346 std::set<VerifierDeps::MethodResolution>* methods = &deps->methods_;
1347 for (const auto& entry : *methods) {
1348 if (!entry.IsResolved()) {
1349 constexpr dex::StringIndex kStringIndexZero(0); // We know there is a class there.
1350 methods->insert(VerifierDeps::MethodResolution(0 /* we know there is a method there */,
1351 VerifierDeps::kUnresolvedMarker - 1,
1352 kStringIndexZero));
1353 found = true;
1354 break;
Nicolas Geoffray8904b6f2016-10-28 19:50:34 +01001355 }
Nicolas Geoffray8904b6f2016-10-28 19:50:34 +01001356 }
Vladimir Markoba118822017-06-12 15:41:56 +01001357 ASSERT_TRUE(found);
1358 new_class_loader.Assign(soa.Decode<mirror::ClassLoader>(LoadDex("VerifierDeps")));
1359 ASSERT_FALSE(decoded_deps.ValidateDependencies(new_class_loader, soa.Self()));
1360 }
Nicolas Geoffray8904b6f2016-10-28 19:50:34 +01001361
Vladimir Markoba118822017-06-12 15:41:56 +01001362 {
1363 VerifierDeps decoded_deps(dex_files_, ArrayRef<const uint8_t>(buffer));
1364 VerifierDeps::DexFileDeps* deps = decoded_deps.GetDexFileDeps(*primary_dex_file_);
1365 bool found = false;
1366 std::set<VerifierDeps::MethodResolution>* methods = &deps->methods_;
1367 for (const auto& entry : *methods) {
1368 if (entry.IsResolved()) {
1369 methods->insert(VerifierDeps::MethodResolution(entry.GetDexMethodIndex(),
1370 entry.GetAccessFlags() - 1,
1371 entry.GetDeclaringClassIndex()));
1372 found = true;
1373 break;
Nicolas Geoffray8904b6f2016-10-28 19:50:34 +01001374 }
Nicolas Geoffray8904b6f2016-10-28 19:50:34 +01001375 }
Vladimir Markoba118822017-06-12 15:41:56 +01001376 ASSERT_TRUE(found);
1377 new_class_loader.Assign(soa.Decode<mirror::ClassLoader>(LoadDex("VerifierDeps")));
1378 ASSERT_FALSE(decoded_deps.ValidateDependencies(new_class_loader, soa.Self()));
1379 }
Nicolas Geoffray8904b6f2016-10-28 19:50:34 +01001380
Vladimir Markoba118822017-06-12 15:41:56 +01001381 {
1382 VerifierDeps decoded_deps(dex_files_, ArrayRef<const uint8_t>(buffer));
1383 VerifierDeps::DexFileDeps* deps = decoded_deps.GetDexFileDeps(*primary_dex_file_);
1384 bool found = false;
1385 std::set<VerifierDeps::MethodResolution>* methods = &deps->methods_;
1386 for (const auto& entry : *methods) {
1387 constexpr dex::StringIndex kNewTypeIndex(0);
1388 if (entry.IsResolved() && entry.GetDeclaringClassIndex() != kNewTypeIndex) {
1389 methods->insert(VerifierDeps::MethodResolution(entry.GetDexMethodIndex(),
1390 entry.GetAccessFlags(),
1391 kNewTypeIndex));
1392 found = true;
1393 break;
Nicolas Geoffray8904b6f2016-10-28 19:50:34 +01001394 }
Nicolas Geoffray8904b6f2016-10-28 19:50:34 +01001395 }
Vladimir Markoba118822017-06-12 15:41:56 +01001396 ASSERT_TRUE(found);
1397 new_class_loader.Assign(soa.Decode<mirror::ClassLoader>(LoadDex("VerifierDeps")));
1398 ASSERT_FALSE(decoded_deps.ValidateDependencies(new_class_loader, soa.Self()));
Nicolas Geoffray6bb7f1b2016-11-03 10:52:49 +00001399 }
1400}
1401
1402TEST_F(VerifierDepsTest, CompilerDriver) {
1403 SetupCompilerDriver();
1404
1405 // Test both multi-dex and single-dex configuration.
1406 for (const char* multi : { "MultiDex", static_cast<const char*>(nullptr) }) {
1407 // Test that the compiler driver behaves as expected when the dependencies
1408 // verify and when they don't verify.
1409 for (bool verify_failure : { false, true }) {
1410 {
1411 ScopedObjectAccess soa(Thread::Current());
1412 LoadDexFile(&soa, "VerifierDeps", multi);
1413 }
1414 VerifyWithCompilerDriver(/* verifier_deps */ nullptr);
1415
1416 std::vector<uint8_t> buffer;
1417 verifier_deps_->Encode(dex_files_, &buffer);
1418
1419 {
1420 ScopedObjectAccess soa(Thread::Current());
1421 LoadDexFile(&soa, "VerifierDeps", multi);
1422 }
1423 verifier::VerifierDeps decoded_deps(dex_files_, ArrayRef<const uint8_t>(buffer));
1424 if (verify_failure) {
1425 // Just taint the decoded VerifierDeps with one invalid entry.
1426 VerifierDeps::DexFileDeps* deps = decoded_deps.GetDexFileDeps(*primary_dex_file_);
1427 bool found = false;
1428 for (const auto& entry : deps->classes_) {
1429 if (entry.IsResolved()) {
1430 deps->classes_.insert(VerifierDeps::ClassResolution(
1431 entry.GetDexTypeIndex(), VerifierDeps::kUnresolvedMarker));
1432 found = true;
1433 break;
1434 }
1435 }
1436 ASSERT_TRUE(found);
1437 }
1438 VerifyWithCompilerDriver(&decoded_deps);
1439
1440 if (verify_failure) {
1441 ASSERT_FALSE(verifier_deps_ == nullptr);
1442 ASSERT_FALSE(verifier_deps_->Equals(decoded_deps));
1443 } else {
1444 ASSERT_TRUE(verifier_deps_ == nullptr);
1445 VerifyClassStatus(decoded_deps);
1446 }
Nicolas Geoffray8904b6f2016-10-28 19:50:34 +01001447 }
1448 }
1449}
1450
Nicolas Geoffray7cc3ae52017-03-07 14:33:37 +00001451TEST_F(VerifierDepsTest, MultiDexVerification) {
1452 VerifyDexFile("VerifierDepsMulti");
1453 ASSERT_EQ(NumberOfCompiledDexFiles(), 2u);
1454
1455 ASSERT_TRUE(HasUnverifiedClass("LMySoftVerificationFailure;", *dex_files_[1]));
1456 ASSERT_TRUE(HasUnverifiedClass("LMySub1SoftVerificationFailure;", *dex_files_[0]));
1457 ASSERT_TRUE(HasUnverifiedClass("LMySub2SoftVerificationFailure;", *dex_files_[0]));
1458
1459 std::vector<uint8_t> buffer;
1460 verifier_deps_->Encode(dex_files_, &buffer);
1461 ASSERT_FALSE(buffer.empty());
1462}
1463
Nicolas Geoffrayfc38e912017-03-16 16:51:59 +00001464TEST_F(VerifierDepsTest, NotAssignable_InterfaceWithClassInBoot) {
1465 ASSERT_TRUE(TestAssignabilityRecording(/* dst */ "Ljava/lang/Exception;",
1466 /* src */ "LIface;",
1467 /* is_strict */ true,
1468 /* is_assignable */ false));
1469 ASSERT_TRUE(HasAssignable("Ljava/lang/Exception;", "LIface;", false));
1470}
1471
Nicolas Geoffraybdb540d2017-04-19 13:50:34 +01001472TEST_F(VerifierDepsTest, Assignable_Arrays) {
1473 ASSERT_TRUE(TestAssignabilityRecording(/* dst */ "[LIface;",
1474 /* src */ "[LMyClassExtendingInterface;",
1475 /* is_strict */ false,
1476 /* is_assignable */ true));
1477 ASSERT_FALSE(HasAssignable(
1478 "LIface;", "LMyClassExtendingInterface;", /* expected_is_assignable */ true));
1479 ASSERT_FALSE(HasAssignable(
1480 "LIface;", "LMyClassExtendingInterface;", /* expected_is_assignable */ false));
1481}
1482
David Brazdilca3c8c32016-09-06 14:04:48 +01001483} // namespace verifier
1484} // namespace art