blob: e9f3f8022dcf1a88cda84b7939106055753022ae [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.
Mathieu Chartier72041a02017-07-14 18:23:25 -070090 if (deps == nullptr) {
91 // Create some verifier deps by default if they are not already specified.
92 deps = new verifier::VerifierDeps(dex_files_);
93 verifier_deps_.reset(deps);
94 }
Nicolas Geoffrayb0bbe8e2016-11-19 10:42:37 +000095 callbacks_->SetVerifierDeps(deps);
96 compiler_driver_->Verify(class_loader_, dex_files_, &timings);
Nicolas Geoffrayb0bbe8e2016-11-19 10:42:37 +000097 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 }
Mathieu Chartier72041a02017-07-14 18:23:25 -0700131 compiler_driver_->SetDexFilesForOatFile(dex_files_);
Nicolas Geoffrayd01f60c2016-10-28 14:45:48 +0100132 }
133
David Brazdilca3c8c32016-09-06 14:04:48 +0100134 void LoadDexFile(ScopedObjectAccess* soa) REQUIRES_SHARED(Locks::mutator_lock_) {
Nicolas Geoffrayd01f60c2016-10-28 14:45:48 +0100135 LoadDexFile(soa, "VerifierDeps");
136 CHECK_EQ(dex_files_.size(), 1u);
David Brazdilca3c8c32016-09-06 14:04:48 +0100137 klass_Main_ = FindClassByName("LMain;", soa);
138 CHECK(klass_Main_ != nullptr);
David Brazdilca3c8c32016-09-06 14:04:48 +0100139 }
140
141 bool VerifyMethod(const std::string& method_name) {
142 ScopedObjectAccess soa(Thread::Current());
143 LoadDexFile(&soa);
144
Nicolas Geoffrayd01f60c2016-10-28 14:45:48 +0100145 StackHandleScope<2> hs(soa.Self());
David Brazdilca3c8c32016-09-06 14:04:48 +0100146 Handle<mirror::ClassLoader> class_loader_handle(
Mathieu Chartier0795f232016-09-27 18:43:30 -0700147 hs.NewHandle(soa.Decode<mirror::ClassLoader>(class_loader_)));
David Brazdilca3c8c32016-09-06 14:04:48 +0100148 Handle<mirror::DexCache> dex_cache_handle(hs.NewHandle(klass_Main_->GetDexCache()));
149
150 const DexFile::ClassDef* class_def = klass_Main_->GetClassDef();
Nicolas Geoffrayd01f60c2016-10-28 14:45:48 +0100151 const uint8_t* class_data = primary_dex_file_->GetClassData(*class_def);
David Brazdilca3c8c32016-09-06 14:04:48 +0100152 CHECK(class_data != nullptr);
153
Nicolas Geoffrayd01f60c2016-10-28 14:45:48 +0100154 ClassDataItemIterator it(*primary_dex_file_, class_data);
Mathieu Chartiere17cf242017-06-19 11:05:51 -0700155 it.SkipAllFields();
David Brazdilca3c8c32016-09-06 14:04:48 +0100156
157 ArtMethod* method = nullptr;
158 while (it.HasNextDirectMethod()) {
Vladimir Markoba118822017-06-12 15:41:56 +0100159 ArtMethod* resolved_method =
160 class_linker_->ResolveMethod<ClassLinker::ResolveMode::kNoChecks>(
161 *primary_dex_file_,
162 it.GetMemberIndex(),
163 dex_cache_handle,
164 class_loader_handle,
165 nullptr,
166 it.GetMethodInvokeType(*class_def));
David Brazdilca3c8c32016-09-06 14:04:48 +0100167 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.
Vladimir Markoba118822017-06-12 15:41:56 +0100374 bool HasMethod(const std::string& expected_klass,
David Brazdilca3c8c32016-09-06 14:04:48 +0100375 const std::string& expected_name,
376 const std::string& expected_signature,
377 bool expected_resolved,
378 const std::string& expected_access_flags = "",
379 const std::string& expected_decl_klass = "") {
David Brazdilca3c8c32016-09-06 14:04:48 +0100380 for (auto& dex_dep : verifier_deps_->dex_deps_) {
Vladimir Markoba118822017-06-12 15:41:56 +0100381 for (const VerifierDeps::MethodResolution& entry : dex_dep.second->methods_) {
David Brazdilca3c8c32016-09-06 14:04:48 +0100382 if (expected_resolved != entry.IsResolved()) {
383 continue;
384 }
385
386 const DexFile::MethodId& method_id = dex_dep.first->GetMethodId(entry.GetDexMethodIndex());
387
388 std::string actual_klass = dex_dep.first->StringByTypeIdx(method_id.class_idx_);
389 if (expected_klass != actual_klass) {
390 continue;
391 }
392
393 std::string actual_name = dex_dep.first->StringDataByIdx(method_id.name_idx_);
394 if (expected_name != actual_name) {
395 continue;
396 }
397
398 std::string actual_signature = dex_dep.first->GetMethodSignature(method_id).ToString();
399 if (expected_signature != actual_signature) {
400 continue;
401 }
402
403 if (expected_resolved) {
404 // Test access flags. Note that PrettyJavaAccessFlags always appends
405 // a space after the modifiers. Add it to the expected access flags.
406 std::string actual_access_flags = PrettyJavaAccessFlags(entry.GetAccessFlags());
407 if (expected_access_flags + " " != actual_access_flags) {
408 continue;
409 }
410
411 std::string actual_decl_klass = verifier_deps_->GetStringFromId(
412 *dex_dep.first, entry.GetDeclaringClassIndex());
413 if (expected_decl_klass != actual_decl_klass) {
414 continue;
415 }
416 }
417
418 return true;
419 }
420 }
421 return false;
422 }
423
David Brazdil6f82fbd2016-09-14 11:55:26 +0100424 size_t NumberOfCompiledDexFiles() {
David Brazdil6f82fbd2016-09-14 11:55:26 +0100425 return verifier_deps_->dex_deps_.size();
426 }
427
428 size_t HasEachKindOfRecord() {
David Brazdil6f82fbd2016-09-14 11:55:26 +0100429 bool has_strings = false;
430 bool has_assignability = false;
431 bool has_classes = false;
432 bool has_fields = false;
433 bool has_methods = false;
Nicolas Geoffray08025182016-10-25 17:20:18 +0100434 bool has_unverified_classes = false;
David Brazdil6f82fbd2016-09-14 11:55:26 +0100435
436 for (auto& entry : verifier_deps_->dex_deps_) {
437 has_strings |= !entry.second->strings_.empty();
438 has_assignability |= !entry.second->assignable_types_.empty();
439 has_assignability |= !entry.second->unassignable_types_.empty();
440 has_classes |= !entry.second->classes_.empty();
441 has_fields |= !entry.second->fields_.empty();
Vladimir Markoba118822017-06-12 15:41:56 +0100442 has_methods |= !entry.second->methods_.empty();
Nicolas Geoffray08025182016-10-25 17:20:18 +0100443 has_unverified_classes |= !entry.second->unverified_classes_.empty();
David Brazdil6f82fbd2016-09-14 11:55:26 +0100444 }
445
Nicolas Geoffray08025182016-10-25 17:20:18 +0100446 return has_strings &&
447 has_assignability &&
448 has_classes &&
449 has_fields &&
450 has_methods &&
451 has_unverified_classes;
David Brazdil6f82fbd2016-09-14 11:55:26 +0100452 }
453
David Brazdilca3c8c32016-09-06 14:04:48 +0100454 std::unique_ptr<verifier::VerifierDeps> verifier_deps_;
Nicolas Geoffrayd01f60c2016-10-28 14:45:48 +0100455 std::vector<const DexFile*> dex_files_;
456 const DexFile* primary_dex_file_;
David Brazdilca3c8c32016-09-06 14:04:48 +0100457 jobject class_loader_;
458 mirror::Class* klass_Main_;
459};
460
461TEST_F(VerifierDepsTest, StringToId) {
462 ScopedObjectAccess soa(Thread::Current());
463 LoadDexFile(&soa);
464
Andreas Gampe8a0128a2016-11-28 07:38:35 -0800465 dex::StringIndex id_Main1 = verifier_deps_->GetIdFromString(*primary_dex_file_, "LMain;");
466 ASSERT_LT(id_Main1.index_, primary_dex_file_->NumStringIds());
Nicolas Geoffrayd01f60c2016-10-28 14:45:48 +0100467 ASSERT_EQ("LMain;", verifier_deps_->GetStringFromId(*primary_dex_file_, id_Main1));
David Brazdilca3c8c32016-09-06 14:04:48 +0100468
Andreas Gampe8a0128a2016-11-28 07:38:35 -0800469 dex::StringIndex id_Main2 = verifier_deps_->GetIdFromString(*primary_dex_file_, "LMain;");
470 ASSERT_LT(id_Main2.index_, primary_dex_file_->NumStringIds());
Nicolas Geoffrayd01f60c2016-10-28 14:45:48 +0100471 ASSERT_EQ("LMain;", verifier_deps_->GetStringFromId(*primary_dex_file_, id_Main2));
David Brazdilca3c8c32016-09-06 14:04:48 +0100472
Andreas Gampe8a0128a2016-11-28 07:38:35 -0800473 dex::StringIndex id_Lorem1 = verifier_deps_->GetIdFromString(*primary_dex_file_, "Lorem ipsum");
474 ASSERT_GE(id_Lorem1.index_, primary_dex_file_->NumStringIds());
Nicolas Geoffrayd01f60c2016-10-28 14:45:48 +0100475 ASSERT_EQ("Lorem ipsum", verifier_deps_->GetStringFromId(*primary_dex_file_, id_Lorem1));
David Brazdilca3c8c32016-09-06 14:04:48 +0100476
Andreas Gampe8a0128a2016-11-28 07:38:35 -0800477 dex::StringIndex id_Lorem2 = verifier_deps_->GetIdFromString(*primary_dex_file_, "Lorem ipsum");
478 ASSERT_GE(id_Lorem2.index_, primary_dex_file_->NumStringIds());
Nicolas Geoffrayd01f60c2016-10-28 14:45:48 +0100479 ASSERT_EQ("Lorem ipsum", verifier_deps_->GetStringFromId(*primary_dex_file_, id_Lorem2));
David Brazdilca3c8c32016-09-06 14:04:48 +0100480
481 ASSERT_EQ(id_Main1, id_Main2);
482 ASSERT_EQ(id_Lorem1, id_Lorem2);
483 ASSERT_NE(id_Main1, id_Lorem1);
484}
485
486TEST_F(VerifierDepsTest, Assignable_BothInBoot) {
487 ASSERT_TRUE(TestAssignabilityRecording(/* dst */ "Ljava/util/TimeZone;",
488 /* src */ "Ljava/util/SimpleTimeZone;",
489 /* is_strict */ true,
490 /* is_assignable */ true));
491 ASSERT_TRUE(HasAssignable("Ljava/util/TimeZone;", "Ljava/util/SimpleTimeZone;", true));
492}
493
494TEST_F(VerifierDepsTest, Assignable_DestinationInBoot1) {
495 ASSERT_TRUE(TestAssignabilityRecording(/* dst */ "Ljava/net/Socket;",
496 /* src */ "LMySSLSocket;",
497 /* is_strict */ true,
498 /* is_assignable */ true));
Nicolas Geoffray119e8462016-12-21 10:29:43 +0000499 ASSERT_TRUE(HasAssignable("Ljava/net/Socket;", "Ljavax/net/ssl/SSLSocket;", true));
David Brazdilca3c8c32016-09-06 14:04:48 +0100500}
501
502TEST_F(VerifierDepsTest, Assignable_DestinationInBoot2) {
503 ASSERT_TRUE(TestAssignabilityRecording(/* dst */ "Ljava/util/TimeZone;",
504 /* src */ "LMySimpleTimeZone;",
505 /* is_strict */ true,
506 /* is_assignable */ true));
Nicolas Geoffray119e8462016-12-21 10:29:43 +0000507 ASSERT_TRUE(HasAssignable("Ljava/util/TimeZone;", "Ljava/util/SimpleTimeZone;", true));
David Brazdilca3c8c32016-09-06 14:04:48 +0100508}
509
510TEST_F(VerifierDepsTest, Assignable_DestinationInBoot3) {
511 ASSERT_TRUE(TestAssignabilityRecording(/* dst */ "Ljava/util/Collection;",
512 /* src */ "LMyThreadSet;",
513 /* is_strict */ true,
514 /* is_assignable */ true));
Nicolas Geoffray0e2fe0f2016-12-21 16:54:52 +0000515 ASSERT_TRUE(HasAssignable("Ljava/util/Collection;", "Ljava/util/Set;", true));
David Brazdilca3c8c32016-09-06 14:04:48 +0100516}
517
518TEST_F(VerifierDepsTest, Assignable_BothArrays_Resolved) {
519 ASSERT_TRUE(TestAssignabilityRecording(/* dst */ "[[Ljava/util/TimeZone;",
520 /* src */ "[[Ljava/util/SimpleTimeZone;",
521 /* is_strict */ true,
522 /* is_assignable */ true));
523 // If the component types of both arrays are resolved, we optimize the list of
524 // dependencies by recording a dependency on the component types.
525 ASSERT_FALSE(HasAssignable("[[Ljava/util/TimeZone;", "[[Ljava/util/SimpleTimeZone;", true));
526 ASSERT_FALSE(HasAssignable("[Ljava/util/TimeZone;", "[Ljava/util/SimpleTimeZone;", true));
527 ASSERT_TRUE(HasAssignable("Ljava/util/TimeZone;", "Ljava/util/SimpleTimeZone;", true));
528}
529
David Brazdilca3c8c32016-09-06 14:04:48 +0100530TEST_F(VerifierDepsTest, NotAssignable_BothInBoot) {
531 ASSERT_TRUE(TestAssignabilityRecording(/* dst */ "Ljava/lang/Exception;",
532 /* src */ "Ljava/util/SimpleTimeZone;",
533 /* is_strict */ true,
534 /* is_assignable */ false));
535 ASSERT_TRUE(HasAssignable("Ljava/lang/Exception;", "Ljava/util/SimpleTimeZone;", false));
536}
537
538TEST_F(VerifierDepsTest, NotAssignable_DestinationInBoot1) {
539 ASSERT_TRUE(TestAssignabilityRecording(/* dst */ "Ljava/lang/Exception;",
540 /* src */ "LMySSLSocket;",
541 /* is_strict */ true,
542 /* is_assignable */ false));
Nicolas Geoffray119e8462016-12-21 10:29:43 +0000543 ASSERT_TRUE(HasAssignable("Ljava/lang/Exception;", "Ljavax/net/ssl/SSLSocket;", false));
David Brazdilca3c8c32016-09-06 14:04:48 +0100544}
545
546TEST_F(VerifierDepsTest, NotAssignable_DestinationInBoot2) {
547 ASSERT_TRUE(TestAssignabilityRecording(/* dst */ "Ljava/lang/Exception;",
548 /* src */ "LMySimpleTimeZone;",
549 /* is_strict */ true,
550 /* is_assignable */ false));
Nicolas Geoffray119e8462016-12-21 10:29:43 +0000551 ASSERT_TRUE(HasAssignable("Ljava/lang/Exception;", "Ljava/util/SimpleTimeZone;", false));
David Brazdilca3c8c32016-09-06 14:04:48 +0100552}
553
554TEST_F(VerifierDepsTest, NotAssignable_BothArrays) {
555 ASSERT_TRUE(TestAssignabilityRecording(/* dst */ "[Ljava/lang/Exception;",
556 /* src */ "[Ljava/util/SimpleTimeZone;",
557 /* is_strict */ true,
558 /* is_assignable */ false));
559 ASSERT_TRUE(HasAssignable("Ljava/lang/Exception;", "Ljava/util/SimpleTimeZone;", false));
560}
561
562TEST_F(VerifierDepsTest, ArgumentType_ResolvedClass) {
563 ASSERT_TRUE(VerifyMethod("ArgumentType_ResolvedClass"));
564 ASSERT_TRUE(HasClass("Ljava/lang/Thread;", true, "public"));
565}
566
David Brazdilca3c8c32016-09-06 14:04:48 +0100567TEST_F(VerifierDepsTest, ArgumentType_UnresolvedClass) {
568 ASSERT_TRUE(VerifyMethod("ArgumentType_UnresolvedClass"));
569 ASSERT_TRUE(HasClass("LUnresolvedClass;", false));
570}
571
572TEST_F(VerifierDepsTest, ArgumentType_UnresolvedSuper) {
573 ASSERT_TRUE(VerifyMethod("ArgumentType_UnresolvedSuper"));
574 ASSERT_TRUE(HasClass("LMySetWithUnresolvedSuper;", false));
575}
576
577TEST_F(VerifierDepsTest, ReturnType_Reference) {
578 ASSERT_TRUE(VerifyMethod("ReturnType_Reference"));
579 ASSERT_TRUE(HasAssignable("Ljava/lang/Throwable;", "Ljava/lang/IllegalStateException;", true));
580}
581
582TEST_F(VerifierDepsTest, ReturnType_Array) {
583 ASSERT_FALSE(VerifyMethod("ReturnType_Array"));
584 ASSERT_TRUE(HasAssignable("Ljava/lang/Integer;", "Ljava/lang/IllegalStateException;", false));
585}
586
587TEST_F(VerifierDepsTest, InvokeArgumentType) {
588 ASSERT_TRUE(VerifyMethod("InvokeArgumentType"));
589 ASSERT_TRUE(HasClass("Ljava/text/SimpleDateFormat;", true, "public"));
590 ASSERT_TRUE(HasClass("Ljava/util/SimpleTimeZone;", true, "public"));
Vladimir Markoba118822017-06-12 15:41:56 +0100591 ASSERT_TRUE(HasMethod("Ljava/text/SimpleDateFormat;",
David Brazdilca3c8c32016-09-06 14:04:48 +0100592 "setTimeZone",
593 "(Ljava/util/TimeZone;)V",
Vladimir Markoba118822017-06-12 15:41:56 +0100594 /* expect_resolved */ true,
David Brazdilca3c8c32016-09-06 14:04:48 +0100595 "public",
596 "Ljava/text/DateFormat;"));
597 ASSERT_TRUE(HasAssignable("Ljava/util/TimeZone;", "Ljava/util/SimpleTimeZone;", true));
598}
599
600TEST_F(VerifierDepsTest, MergeTypes_RegisterLines) {
601 ASSERT_TRUE(VerifyMethod("MergeTypes_RegisterLines"));
Nicolas Geoffray119e8462016-12-21 10:29:43 +0000602 ASSERT_TRUE(HasAssignable("Ljava/lang/Exception;", "Ljava/net/SocketTimeoutException;", true));
David Brazdilca3c8c32016-09-06 14:04:48 +0100603 ASSERT_TRUE(HasAssignable(
604 "Ljava/lang/Exception;", "Ljava/util/concurrent/TimeoutException;", true));
605}
606
607TEST_F(VerifierDepsTest, MergeTypes_IfInstanceOf) {
608 ASSERT_TRUE(VerifyMethod("MergeTypes_IfInstanceOf"));
609 ASSERT_TRUE(HasAssignable("Ljava/lang/Exception;", "Ljava/net/SocketTimeoutException;", true));
610 ASSERT_TRUE(HasAssignable(
611 "Ljava/lang/Exception;", "Ljava/util/concurrent/TimeoutException;", true));
612 ASSERT_TRUE(HasAssignable("Ljava/net/SocketTimeoutException;", "Ljava/lang/Exception;", false));
613}
614
615TEST_F(VerifierDepsTest, MergeTypes_Unresolved) {
616 ASSERT_TRUE(VerifyMethod("MergeTypes_Unresolved"));
617 ASSERT_TRUE(HasAssignable("Ljava/lang/Exception;", "Ljava/net/SocketTimeoutException;", true));
618 ASSERT_TRUE(HasAssignable(
619 "Ljava/lang/Exception;", "Ljava/util/concurrent/TimeoutException;", true));
620}
621
622TEST_F(VerifierDepsTest, ConstClass_Resolved) {
623 ASSERT_TRUE(VerifyMethod("ConstClass_Resolved"));
624 ASSERT_TRUE(HasClass("Ljava/lang/IllegalStateException;", true, "public"));
625}
626
627TEST_F(VerifierDepsTest, ConstClass_Unresolved) {
628 ASSERT_TRUE(VerifyMethod("ConstClass_Unresolved"));
629 ASSERT_TRUE(HasClass("LUnresolvedClass;", false));
630}
631
632TEST_F(VerifierDepsTest, CheckCast_Resolved) {
633 ASSERT_TRUE(VerifyMethod("CheckCast_Resolved"));
634 ASSERT_TRUE(HasClass("Ljava/lang/IllegalStateException;", true, "public"));
635}
636
637TEST_F(VerifierDepsTest, CheckCast_Unresolved) {
638 ASSERT_TRUE(VerifyMethod("CheckCast_Unresolved"));
639 ASSERT_TRUE(HasClass("LUnresolvedClass;", false));
640}
641
642TEST_F(VerifierDepsTest, InstanceOf_Resolved) {
643 ASSERT_TRUE(VerifyMethod("InstanceOf_Resolved"));
644 ASSERT_TRUE(HasClass("Ljava/lang/IllegalStateException;", true, "public"));
645}
646
647TEST_F(VerifierDepsTest, InstanceOf_Unresolved) {
648 ASSERT_TRUE(VerifyMethod("InstanceOf_Unresolved"));
649 ASSERT_TRUE(HasClass("LUnresolvedClass;", false));
650}
651
652TEST_F(VerifierDepsTest, NewInstance_Resolved) {
653 ASSERT_TRUE(VerifyMethod("NewInstance_Resolved"));
654 ASSERT_TRUE(HasClass("Ljava/lang/IllegalStateException;", true, "public"));
655}
656
657TEST_F(VerifierDepsTest, NewInstance_Unresolved) {
658 ASSERT_TRUE(VerifyMethod("NewInstance_Unresolved"));
659 ASSERT_TRUE(HasClass("LUnresolvedClass;", false));
660}
661
David Brazdilca3c8c32016-09-06 14:04:48 +0100662TEST_F(VerifierDepsTest, NewArray_Unresolved) {
663 ASSERT_TRUE(VerifyMethod("NewArray_Unresolved"));
664 ASSERT_TRUE(HasClass("[LUnresolvedClass;", false));
665}
666
667TEST_F(VerifierDepsTest, Throw) {
668 ASSERT_TRUE(VerifyMethod("Throw"));
669 ASSERT_TRUE(HasAssignable("Ljava/lang/Throwable;", "Ljava/lang/IllegalStateException;", true));
670}
671
672TEST_F(VerifierDepsTest, MoveException_Resolved) {
673 ASSERT_TRUE(VerifyMethod("MoveException_Resolved"));
674 ASSERT_TRUE(HasClass("Ljava/io/InterruptedIOException;", true, "public"));
675 ASSERT_TRUE(HasClass("Ljava/net/SocketTimeoutException;", true, "public"));
676 ASSERT_TRUE(HasClass("Ljava/util/zip/ZipException;", true, "public"));
677
678 // Testing that all exception types are assignable to Throwable.
679 ASSERT_TRUE(HasAssignable("Ljava/lang/Throwable;", "Ljava/io/InterruptedIOException;", true));
680 ASSERT_TRUE(HasAssignable("Ljava/lang/Throwable;", "Ljava/net/SocketTimeoutException;", true));
681 ASSERT_TRUE(HasAssignable("Ljava/lang/Throwable;", "Ljava/util/zip/ZipException;", true));
682
683 // Testing that the merge type is assignable to Throwable.
684 ASSERT_TRUE(HasAssignable("Ljava/lang/Throwable;", "Ljava/io/IOException;", true));
685
686 // Merging of exception types.
687 ASSERT_TRUE(HasAssignable("Ljava/io/IOException;", "Ljava/io/InterruptedIOException;", true));
688 ASSERT_TRUE(HasAssignable("Ljava/io/IOException;", "Ljava/util/zip/ZipException;", true));
689 ASSERT_TRUE(HasAssignable(
690 "Ljava/io/InterruptedIOException;", "Ljava/net/SocketTimeoutException;", true));
691}
692
693TEST_F(VerifierDepsTest, MoveException_Unresolved) {
694 ASSERT_FALSE(VerifyMethod("MoveException_Unresolved"));
695 ASSERT_TRUE(HasClass("LUnresolvedException;", false));
696}
697
698TEST_F(VerifierDepsTest, StaticField_Resolved_DeclaredInReferenced) {
699 ASSERT_TRUE(VerifyMethod("StaticField_Resolved_DeclaredInReferenced"));
Nicolas Geoffray6e54f782017-03-08 15:27:09 +0000700 ASSERT_TRUE(HasClass("Ljava/lang/System;", true, "public"));
David Brazdilca3c8c32016-09-06 14:04:48 +0100701 ASSERT_TRUE(HasField("Ljava/lang/System;",
702 "out",
703 "Ljava/io/PrintStream;",
704 true,
Nicolas Geoffray6e54f782017-03-08 15:27:09 +0000705 "public static",
David Brazdilca3c8c32016-09-06 14:04:48 +0100706 "Ljava/lang/System;"));
707}
708
709TEST_F(VerifierDepsTest, StaticField_Resolved_DeclaredInSuperclass1) {
710 ASSERT_TRUE(VerifyMethod("StaticField_Resolved_DeclaredInSuperclass1"));
711 ASSERT_TRUE(HasClass("Ljava/util/SimpleTimeZone;", true, "public"));
712 ASSERT_TRUE(HasField(
Nicolas Geoffray6e54f782017-03-08 15:27:09 +0000713 "Ljava/util/SimpleTimeZone;", "LONG", "I", true, "public static", "Ljava/util/TimeZone;"));
David Brazdilca3c8c32016-09-06 14:04:48 +0100714}
715
716TEST_F(VerifierDepsTest, StaticField_Resolved_DeclaredInSuperclass2) {
717 ASSERT_TRUE(VerifyMethod("StaticField_Resolved_DeclaredInSuperclass2"));
718 ASSERT_TRUE(HasField(
Nicolas Geoffray6e54f782017-03-08 15:27:09 +0000719 "LMySimpleTimeZone;", "SHORT", "I", true, "public static", "Ljava/util/TimeZone;"));
David Brazdilca3c8c32016-09-06 14:04:48 +0100720}
721
722TEST_F(VerifierDepsTest, StaticField_Resolved_DeclaredInInterface1) {
723 ASSERT_TRUE(VerifyMethod("StaticField_Resolved_DeclaredInInterface1"));
724 ASSERT_TRUE(HasClass("Ljavax/xml/transform/dom/DOMResult;", true, "public"));
725 ASSERT_TRUE(HasField("Ljavax/xml/transform/dom/DOMResult;",
726 "PI_ENABLE_OUTPUT_ESCAPING",
727 "Ljava/lang/String;",
728 true,
Nicolas Geoffray6e54f782017-03-08 15:27:09 +0000729 "public static",
David Brazdilca3c8c32016-09-06 14:04:48 +0100730 "Ljavax/xml/transform/Result;"));
731}
732
733TEST_F(VerifierDepsTest, StaticField_Resolved_DeclaredInInterface2) {
734 ASSERT_TRUE(VerifyMethod("StaticField_Resolved_DeclaredInInterface2"));
735 ASSERT_TRUE(HasField("LMyDOMResult;",
736 "PI_ENABLE_OUTPUT_ESCAPING",
737 "Ljava/lang/String;",
738 true,
Nicolas Geoffray6e54f782017-03-08 15:27:09 +0000739 "public static",
David Brazdilca3c8c32016-09-06 14:04:48 +0100740 "Ljavax/xml/transform/Result;"));
741}
742
743TEST_F(VerifierDepsTest, StaticField_Resolved_DeclaredInInterface3) {
744 ASSERT_TRUE(VerifyMethod("StaticField_Resolved_DeclaredInInterface3"));
745 ASSERT_TRUE(HasField("LMyResult;",
746 "PI_ENABLE_OUTPUT_ESCAPING",
747 "Ljava/lang/String;",
748 true,
Nicolas Geoffray6e54f782017-03-08 15:27:09 +0000749 "public static",
David Brazdilca3c8c32016-09-06 14:04:48 +0100750 "Ljavax/xml/transform/Result;"));
751}
752
753TEST_F(VerifierDepsTest, StaticField_Resolved_DeclaredInInterface4) {
754 ASSERT_TRUE(VerifyMethod("StaticField_Resolved_DeclaredInInterface4"));
755 ASSERT_TRUE(HasField("LMyDocument;",
756 "ELEMENT_NODE",
757 "S",
758 true,
Nicolas Geoffray6e54f782017-03-08 15:27:09 +0000759 "public static",
David Brazdilca3c8c32016-09-06 14:04:48 +0100760 "Lorg/w3c/dom/Node;"));
761}
762
763TEST_F(VerifierDepsTest, StaticField_Unresolved_ReferrerInBoot) {
764 ASSERT_TRUE(VerifyMethod("StaticField_Unresolved_ReferrerInBoot"));
Nicolas Geoffray6e54f782017-03-08 15:27:09 +0000765 ASSERT_TRUE(HasClass("Ljava/util/TimeZone;", true, "public"));
David Brazdilca3c8c32016-09-06 14:04:48 +0100766 ASSERT_TRUE(HasField("Ljava/util/TimeZone;", "x", "I", false));
767}
768
769TEST_F(VerifierDepsTest, StaticField_Unresolved_ReferrerInDex) {
770 ASSERT_TRUE(VerifyMethod("StaticField_Unresolved_ReferrerInDex"));
771 ASSERT_TRUE(HasField("LMyThreadSet;", "x", "I", false));
772}
773
774TEST_F(VerifierDepsTest, InstanceField_Resolved_DeclaredInReferenced) {
775 ASSERT_TRUE(VerifyMethod("InstanceField_Resolved_DeclaredInReferenced"));
776 ASSERT_TRUE(HasClass("Ljava/io/InterruptedIOException;", true, "public"));
777 ASSERT_TRUE(HasField("Ljava/io/InterruptedIOException;",
778 "bytesTransferred",
779 "I",
780 true,
781 "public",
782 "Ljava/io/InterruptedIOException;"));
783 ASSERT_TRUE(HasAssignable(
Nicolas Geoffray119e8462016-12-21 10:29:43 +0000784 "Ljava/io/InterruptedIOException;", "Ljava/net/SocketTimeoutException;", true));
David Brazdilca3c8c32016-09-06 14:04:48 +0100785}
786
787TEST_F(VerifierDepsTest, InstanceField_Resolved_DeclaredInSuperclass1) {
788 ASSERT_TRUE(VerifyMethod("InstanceField_Resolved_DeclaredInSuperclass1"));
789 ASSERT_TRUE(HasClass("Ljava/net/SocketTimeoutException;", true, "public"));
790 ASSERT_TRUE(HasField("Ljava/net/SocketTimeoutException;",
791 "bytesTransferred",
792 "I",
793 true,
794 "public",
795 "Ljava/io/InterruptedIOException;"));
796 ASSERT_TRUE(HasAssignable(
Nicolas Geoffray119e8462016-12-21 10:29:43 +0000797 "Ljava/io/InterruptedIOException;", "Ljava/net/SocketTimeoutException;", true));
David Brazdilca3c8c32016-09-06 14:04:48 +0100798}
799
800TEST_F(VerifierDepsTest, InstanceField_Resolved_DeclaredInSuperclass2) {
801 ASSERT_TRUE(VerifyMethod("InstanceField_Resolved_DeclaredInSuperclass2"));
802 ASSERT_TRUE(HasField("LMySocketTimeoutException;",
803 "bytesTransferred",
804 "I",
805 true,
806 "public",
807 "Ljava/io/InterruptedIOException;"));
808 ASSERT_TRUE(HasAssignable(
Nicolas Geoffray119e8462016-12-21 10:29:43 +0000809 "Ljava/io/InterruptedIOException;", "Ljava/net/SocketTimeoutException;", true));
David Brazdilca3c8c32016-09-06 14:04:48 +0100810}
811
812TEST_F(VerifierDepsTest, InstanceField_Unresolved_ReferrerInBoot) {
813 ASSERT_TRUE(VerifyMethod("InstanceField_Unresolved_ReferrerInBoot"));
814 ASSERT_TRUE(HasClass("Ljava/io/InterruptedIOException;", true, "public"));
815 ASSERT_TRUE(HasField("Ljava/io/InterruptedIOException;", "x", "I", false));
816}
817
818TEST_F(VerifierDepsTest, InstanceField_Unresolved_ReferrerInDex) {
819 ASSERT_TRUE(VerifyMethod("InstanceField_Unresolved_ReferrerInDex"));
820 ASSERT_TRUE(HasField("LMyThreadSet;", "x", "I", false));
821}
822
823TEST_F(VerifierDepsTest, InvokeStatic_Resolved_DeclaredInReferenced) {
824 ASSERT_TRUE(VerifyMethod("InvokeStatic_Resolved_DeclaredInReferenced"));
825 ASSERT_TRUE(HasClass("Ljava/net/Socket;", true, "public"));
Vladimir Markoba118822017-06-12 15:41:56 +0100826 ASSERT_TRUE(HasMethod("Ljava/net/Socket;",
David Brazdilca3c8c32016-09-06 14:04:48 +0100827 "setSocketImplFactory",
828 "(Ljava/net/SocketImplFactory;)V",
Vladimir Markoba118822017-06-12 15:41:56 +0100829 /* expect_resolved */ true,
David Brazdilca3c8c32016-09-06 14:04:48 +0100830 "public static",
831 "Ljava/net/Socket;"));
832}
833
834TEST_F(VerifierDepsTest, InvokeStatic_Resolved_DeclaredInSuperclass1) {
835 ASSERT_TRUE(VerifyMethod("InvokeStatic_Resolved_DeclaredInSuperclass1"));
Nicolas Geoffray6e54f782017-03-08 15:27:09 +0000836 ASSERT_TRUE(HasClass("Ljavax/net/ssl/SSLSocket;", true, "public"));
Vladimir Markoba118822017-06-12 15:41:56 +0100837 ASSERT_TRUE(HasMethod("Ljavax/net/ssl/SSLSocket;",
David Brazdilca3c8c32016-09-06 14:04:48 +0100838 "setSocketImplFactory",
839 "(Ljava/net/SocketImplFactory;)V",
Vladimir Markoba118822017-06-12 15:41:56 +0100840 /* expect_resolved */ true,
David Brazdilca3c8c32016-09-06 14:04:48 +0100841 "public static",
842 "Ljava/net/Socket;"));
843}
844
845TEST_F(VerifierDepsTest, InvokeStatic_Resolved_DeclaredInSuperclass2) {
846 ASSERT_TRUE(VerifyMethod("InvokeStatic_Resolved_DeclaredInSuperclass2"));
Vladimir Markoba118822017-06-12 15:41:56 +0100847 ASSERT_TRUE(HasMethod("LMySSLSocket;",
David Brazdilca3c8c32016-09-06 14:04:48 +0100848 "setSocketImplFactory",
849 "(Ljava/net/SocketImplFactory;)V",
Vladimir Markoba118822017-06-12 15:41:56 +0100850 /* expect_resolved */ true,
David Brazdilca3c8c32016-09-06 14:04:48 +0100851 "public static",
852 "Ljava/net/Socket;"));
853}
854
855TEST_F(VerifierDepsTest, InvokeStatic_DeclaredInInterface1) {
856 ASSERT_TRUE(VerifyMethod("InvokeStatic_DeclaredInInterface1"));
Nicolas Geoffray6e54f782017-03-08 15:27:09 +0000857 ASSERT_TRUE(HasClass("Ljava/util/Map$Entry;", true, "public interface"));
Vladimir Markoba118822017-06-12 15:41:56 +0100858 ASSERT_TRUE(HasMethod("Ljava/util/Map$Entry;",
David Brazdilca3c8c32016-09-06 14:04:48 +0100859 "comparingByKey",
860 "()Ljava/util/Comparator;",
Vladimir Markoba118822017-06-12 15:41:56 +0100861 /* expect_resolved */ true,
David Brazdilca3c8c32016-09-06 14:04:48 +0100862 "public static",
863 "Ljava/util/Map$Entry;"));
864}
865
866TEST_F(VerifierDepsTest, InvokeStatic_DeclaredInInterface2) {
867 ASSERT_FALSE(VerifyMethod("InvokeStatic_DeclaredInInterface2"));
868 ASSERT_TRUE(HasClass("Ljava/util/AbstractMap$SimpleEntry;", true, "public"));
Vladimir Markoba118822017-06-12 15:41:56 +0100869 ASSERT_TRUE(HasMethod("Ljava/util/AbstractMap$SimpleEntry;",
David Brazdilca3c8c32016-09-06 14:04:48 +0100870 "comparingByKey",
871 "()Ljava/util/Comparator;",
Vladimir Markoba118822017-06-12 15:41:56 +0100872 /* expect_resolved */ false));
David Brazdilca3c8c32016-09-06 14:04:48 +0100873}
874
875TEST_F(VerifierDepsTest, InvokeStatic_Unresolved1) {
876 ASSERT_FALSE(VerifyMethod("InvokeStatic_Unresolved1"));
Nicolas Geoffray6e54f782017-03-08 15:27:09 +0000877 ASSERT_TRUE(HasClass("Ljavax/net/ssl/SSLSocket;", true, "public"));
Vladimir Markoba118822017-06-12 15:41:56 +0100878 ASSERT_TRUE(HasMethod("Ljavax/net/ssl/SSLSocket;",
879 "x",
880 "()V",
881 /* expect_resolved */ false));
David Brazdilca3c8c32016-09-06 14:04:48 +0100882}
883
884TEST_F(VerifierDepsTest, InvokeStatic_Unresolved2) {
885 ASSERT_FALSE(VerifyMethod("InvokeStatic_Unresolved2"));
Vladimir Markoba118822017-06-12 15:41:56 +0100886 ASSERT_TRUE(HasMethod("LMySSLSocket;",
887 "x",
888 "()V",
889 /* expect_resolved */ false));
David Brazdilca3c8c32016-09-06 14:04:48 +0100890}
891
892TEST_F(VerifierDepsTest, InvokeDirect_Resolved_DeclaredInReferenced) {
893 ASSERT_TRUE(VerifyMethod("InvokeDirect_Resolved_DeclaredInReferenced"));
894 ASSERT_TRUE(HasClass("Ljava/net/Socket;", true, "public"));
Vladimir Markoba118822017-06-12 15:41:56 +0100895 ASSERT_TRUE(HasMethod("Ljava/net/Socket;",
896 "<init>",
897 "()V",
898 /* expect_resolved */ true,
899 "public",
900 "Ljava/net/Socket;"));
David Brazdilca3c8c32016-09-06 14:04:48 +0100901}
902
903TEST_F(VerifierDepsTest, InvokeDirect_Resolved_DeclaredInSuperclass1) {
904 ASSERT_FALSE(VerifyMethod("InvokeDirect_Resolved_DeclaredInSuperclass1"));
Nicolas Geoffray6e54f782017-03-08 15:27:09 +0000905 ASSERT_TRUE(HasClass("Ljavax/net/ssl/SSLSocket;", true, "public"));
Vladimir Markoba118822017-06-12 15:41:56 +0100906 ASSERT_TRUE(HasMethod("Ljavax/net/ssl/SSLSocket;",
David Brazdilca3c8c32016-09-06 14:04:48 +0100907 "checkOldImpl",
908 "()V",
Vladimir Markoba118822017-06-12 15:41:56 +0100909 /* expect_resolved */ true,
David Brazdilca3c8c32016-09-06 14:04:48 +0100910 "private",
911 "Ljava/net/Socket;"));
912}
913
914TEST_F(VerifierDepsTest, InvokeDirect_Resolved_DeclaredInSuperclass2) {
915 ASSERT_FALSE(VerifyMethod("InvokeDirect_Resolved_DeclaredInSuperclass2"));
Vladimir Markoba118822017-06-12 15:41:56 +0100916 ASSERT_TRUE(HasMethod("LMySSLSocket;",
917 "checkOldImpl",
918 "()V",
919 /* expect_resolved */ true,
920 "private",
921 "Ljava/net/Socket;"));
David Brazdilca3c8c32016-09-06 14:04:48 +0100922}
923
924TEST_F(VerifierDepsTest, InvokeDirect_Unresolved1) {
925 ASSERT_FALSE(VerifyMethod("InvokeDirect_Unresolved1"));
Nicolas Geoffray6e54f782017-03-08 15:27:09 +0000926 ASSERT_TRUE(HasClass("Ljavax/net/ssl/SSLSocket;", true, "public"));
Vladimir Markoba118822017-06-12 15:41:56 +0100927 ASSERT_TRUE(HasMethod("Ljavax/net/ssl/SSLSocket;",
928 "x",
929 "()V",
930 /* expect_resolved */ false));
David Brazdilca3c8c32016-09-06 14:04:48 +0100931}
932
933TEST_F(VerifierDepsTest, InvokeDirect_Unresolved2) {
934 ASSERT_FALSE(VerifyMethod("InvokeDirect_Unresolved2"));
Vladimir Markoba118822017-06-12 15:41:56 +0100935 ASSERT_TRUE(HasMethod("LMySSLSocket;",
936 "x",
937 "()V",
938 /* expect_resolved */ false));
David Brazdilca3c8c32016-09-06 14:04:48 +0100939}
940
941TEST_F(VerifierDepsTest, InvokeVirtual_Resolved_DeclaredInReferenced) {
942 ASSERT_TRUE(VerifyMethod("InvokeVirtual_Resolved_DeclaredInReferenced"));
943 ASSERT_TRUE(HasClass("Ljava/lang/Throwable;", true, "public"));
Vladimir Markoba118822017-06-12 15:41:56 +0100944 ASSERT_TRUE(HasMethod("Ljava/lang/Throwable;",
David Brazdilca3c8c32016-09-06 14:04:48 +0100945 "getMessage",
946 "()Ljava/lang/String;",
Vladimir Markoba118822017-06-12 15:41:56 +0100947 /* expect_resolved */ true,
David Brazdilca3c8c32016-09-06 14:04:48 +0100948 "public",
949 "Ljava/lang/Throwable;"));
950 // Type dependency on `this` argument.
Nicolas Geoffray119e8462016-12-21 10:29:43 +0000951 ASSERT_TRUE(HasAssignable("Ljava/lang/Throwable;", "Ljava/net/SocketTimeoutException;", true));
David Brazdilca3c8c32016-09-06 14:04:48 +0100952}
953
954TEST_F(VerifierDepsTest, InvokeVirtual_Resolved_DeclaredInSuperclass1) {
955 ASSERT_TRUE(VerifyMethod("InvokeVirtual_Resolved_DeclaredInSuperclass1"));
956 ASSERT_TRUE(HasClass("Ljava/io/InterruptedIOException;", true, "public"));
Vladimir Markoba118822017-06-12 15:41:56 +0100957 ASSERT_TRUE(HasMethod("Ljava/io/InterruptedIOException;",
David Brazdilca3c8c32016-09-06 14:04:48 +0100958 "getMessage",
959 "()Ljava/lang/String;",
Vladimir Markoba118822017-06-12 15:41:56 +0100960 /* expect_resolved */ true,
David Brazdilca3c8c32016-09-06 14:04:48 +0100961 "public",
962 "Ljava/lang/Throwable;"));
963 // Type dependency on `this` argument.
Nicolas Geoffray119e8462016-12-21 10:29:43 +0000964 ASSERT_TRUE(HasAssignable("Ljava/lang/Throwable;", "Ljava/net/SocketTimeoutException;", true));
David Brazdilca3c8c32016-09-06 14:04:48 +0100965}
966
967TEST_F(VerifierDepsTest, InvokeVirtual_Resolved_DeclaredInSuperclass2) {
968 ASSERT_TRUE(VerifyMethod("InvokeVirtual_Resolved_DeclaredInSuperclass2"));
Vladimir Markoba118822017-06-12 15:41:56 +0100969 ASSERT_TRUE(HasMethod("LMySocketTimeoutException;",
David Brazdilca3c8c32016-09-06 14:04:48 +0100970 "getMessage",
971 "()Ljava/lang/String;",
Vladimir Markoba118822017-06-12 15:41:56 +0100972 /* expect_resolved */ true,
David Brazdilca3c8c32016-09-06 14:04:48 +0100973 "public",
974 "Ljava/lang/Throwable;"));
975}
976
977TEST_F(VerifierDepsTest, InvokeVirtual_Resolved_DeclaredInSuperinterface) {
978 ASSERT_TRUE(VerifyMethod("InvokeVirtual_Resolved_DeclaredInSuperinterface"));
Vladimir Markoba118822017-06-12 15:41:56 +0100979 ASSERT_TRUE(HasMethod("LMyThreadSet;",
David Brazdilca3c8c32016-09-06 14:04:48 +0100980 "size",
981 "()I",
Vladimir Markoba118822017-06-12 15:41:56 +0100982 /* expect_resolved */ true,
Nicolas Geoffray6e54f782017-03-08 15:27:09 +0000983 "public",
David Brazdilca3c8c32016-09-06 14:04:48 +0100984 "Ljava/util/Set;"));
985}
986
987TEST_F(VerifierDepsTest, InvokeVirtual_Unresolved1) {
988 ASSERT_FALSE(VerifyMethod("InvokeVirtual_Unresolved1"));
989 ASSERT_TRUE(HasClass("Ljava/io/InterruptedIOException;", true, "public"));
Vladimir Markoba118822017-06-12 15:41:56 +0100990 ASSERT_TRUE(HasMethod("Ljava/io/InterruptedIOException;",
991 "x",
992 "()V",
993 /* expect_resolved */ false));
David Brazdilca3c8c32016-09-06 14:04:48 +0100994}
995
996TEST_F(VerifierDepsTest, InvokeVirtual_Unresolved2) {
997 ASSERT_FALSE(VerifyMethod("InvokeVirtual_Unresolved2"));
Vladimir Markoba118822017-06-12 15:41:56 +0100998 ASSERT_TRUE(HasMethod("LMySocketTimeoutException;",
999 "x",
1000 "()V",
1001 /* expect_resolved */ false));
David Brazdilca3c8c32016-09-06 14:04:48 +01001002}
1003
1004TEST_F(VerifierDepsTest, InvokeInterface_Resolved_DeclaredInReferenced) {
1005 ASSERT_TRUE(VerifyMethod("InvokeInterface_Resolved_DeclaredInReferenced"));
Nicolas Geoffray6e54f782017-03-08 15:27:09 +00001006 ASSERT_TRUE(HasClass("Ljava/lang/Runnable;", true, "public interface"));
Vladimir Markoba118822017-06-12 15:41:56 +01001007 ASSERT_TRUE(HasMethod("Ljava/lang/Runnable;",
David Brazdilca3c8c32016-09-06 14:04:48 +01001008 "run",
1009 "()V",
Vladimir Markoba118822017-06-12 15:41:56 +01001010 /* expect_resolved */ true,
Nicolas Geoffray6e54f782017-03-08 15:27:09 +00001011 "public",
David Brazdilca3c8c32016-09-06 14:04:48 +01001012 "Ljava/lang/Runnable;"));
1013}
1014
1015TEST_F(VerifierDepsTest, InvokeInterface_Resolved_DeclaredInSuperclass) {
1016 ASSERT_FALSE(VerifyMethod("InvokeInterface_Resolved_DeclaredInSuperclass"));
Vladimir Markoba118822017-06-12 15:41:56 +01001017 // TODO: Maybe we should not record dependency if the invoke type does not match the lookup type.
1018 ASSERT_TRUE(HasMethod("LMyThread;",
1019 "join",
1020 "()V",
1021 /* expect_resolved */ true,
1022 "public",
1023 "Ljava/lang/Thread;"));
David Brazdilca3c8c32016-09-06 14:04:48 +01001024}
1025
1026TEST_F(VerifierDepsTest, InvokeInterface_Resolved_DeclaredInSuperinterface1) {
1027 ASSERT_FALSE(VerifyMethod("InvokeInterface_Resolved_DeclaredInSuperinterface1"));
Vladimir Markoba118822017-06-12 15:41:56 +01001028 // TODO: Maybe we should not record dependency if the invoke type does not match the lookup type.
1029 ASSERT_TRUE(HasMethod("LMyThreadSet;",
David Brazdilca3c8c32016-09-06 14:04:48 +01001030 "run",
1031 "()V",
Vladimir Markoba118822017-06-12 15:41:56 +01001032 /* expect_resolved */ true,
Nicolas Geoffray6e54f782017-03-08 15:27:09 +00001033 "public",
Vladimir Markoba118822017-06-12 15:41:56 +01001034 "Ljava/lang/Thread;"));
David Brazdilca3c8c32016-09-06 14:04:48 +01001035}
1036
1037TEST_F(VerifierDepsTest, InvokeInterface_Resolved_DeclaredInSuperinterface2) {
1038 ASSERT_FALSE(VerifyMethod("InvokeInterface_Resolved_DeclaredInSuperinterface2"));
Vladimir Markoba118822017-06-12 15:41:56 +01001039 ASSERT_TRUE(HasMethod("LMyThreadSet;",
David Brazdilca3c8c32016-09-06 14:04:48 +01001040 "isEmpty",
1041 "()Z",
Vladimir Markoba118822017-06-12 15:41:56 +01001042 /* expect_resolved */ true,
Nicolas Geoffray6e54f782017-03-08 15:27:09 +00001043 "public",
David Brazdilca3c8c32016-09-06 14:04:48 +01001044 "Ljava/util/Set;"));
1045}
1046
1047TEST_F(VerifierDepsTest, InvokeInterface_Unresolved1) {
1048 ASSERT_FALSE(VerifyMethod("InvokeInterface_Unresolved1"));
Nicolas Geoffray6e54f782017-03-08 15:27:09 +00001049 ASSERT_TRUE(HasClass("Ljava/lang/Runnable;", true, "public interface"));
Vladimir Markoba118822017-06-12 15:41:56 +01001050 ASSERT_TRUE(HasMethod("Ljava/lang/Runnable;",
1051 "x",
1052 "()V",
1053 /* expect_resolved */ false));
David Brazdilca3c8c32016-09-06 14:04:48 +01001054}
1055
1056TEST_F(VerifierDepsTest, InvokeInterface_Unresolved2) {
1057 ASSERT_FALSE(VerifyMethod("InvokeInterface_Unresolved2"));
Vladimir Markoba118822017-06-12 15:41:56 +01001058 ASSERT_TRUE(HasMethod("LMyThreadSet;", "x", "()V", /* expect_resolved */ false));
David Brazdilca3c8c32016-09-06 14:04:48 +01001059}
1060
1061TEST_F(VerifierDepsTest, InvokeSuper_ThisAssignable) {
1062 ASSERT_TRUE(VerifyMethod("InvokeSuper_ThisAssignable"));
Nicolas Geoffray6e54f782017-03-08 15:27:09 +00001063 ASSERT_TRUE(HasClass("Ljava/lang/Runnable;", true, "public interface"));
Nicolas Geoffray0e2fe0f2016-12-21 16:54:52 +00001064 ASSERT_TRUE(HasAssignable("Ljava/lang/Runnable;", "Ljava/lang/Thread;", true));
Vladimir Markoba118822017-06-12 15:41:56 +01001065 ASSERT_TRUE(HasMethod("Ljava/lang/Runnable;",
David Brazdilca3c8c32016-09-06 14:04:48 +01001066 "run",
1067 "()V",
Vladimir Markoba118822017-06-12 15:41:56 +01001068 /* expect_resolved */ true,
Nicolas Geoffray6e54f782017-03-08 15:27:09 +00001069 "public",
David Brazdilca3c8c32016-09-06 14:04:48 +01001070 "Ljava/lang/Runnable;"));
1071}
1072
1073TEST_F(VerifierDepsTest, InvokeSuper_ThisNotAssignable) {
1074 ASSERT_FALSE(VerifyMethod("InvokeSuper_ThisNotAssignable"));
Nicolas Geoffray6e54f782017-03-08 15:27:09 +00001075 ASSERT_TRUE(HasClass("Ljava/lang/Integer;", true, "public"));
Nicolas Geoffray119e8462016-12-21 10:29:43 +00001076 ASSERT_TRUE(HasAssignable("Ljava/lang/Integer;", "Ljava/lang/Thread;", false));
Vladimir Markoba118822017-06-12 15:41:56 +01001077 ASSERT_TRUE(HasMethod("Ljava/lang/Integer;",
1078 "intValue", "()I",
1079 /* expect_resolved */ true,
1080 "public", "Ljava/lang/Integer;"));
David Brazdilca3c8c32016-09-06 14:04:48 +01001081}
1082
Nicolas Geoffray0f1cb172017-01-05 15:23:19 +00001083TEST_F(VerifierDepsTest, ArgumentType_ResolvedReferenceArray) {
1084 ASSERT_TRUE(VerifyMethod("ArgumentType_ResolvedReferenceArray"));
Nicolas Geoffray6e54f782017-03-08 15:27:09 +00001085 ASSERT_TRUE(HasClass("[Ljava/lang/Thread;", true, "public"));
Nicolas Geoffray0f1cb172017-01-05 15:23:19 +00001086}
1087
1088TEST_F(VerifierDepsTest, NewArray_Resolved) {
1089 ASSERT_TRUE(VerifyMethod("NewArray_Resolved"));
Nicolas Geoffray6e54f782017-03-08 15:27:09 +00001090 ASSERT_TRUE(HasClass("[Ljava/lang/IllegalStateException;", true, "public"));
Nicolas Geoffray0f1cb172017-01-05 15:23:19 +00001091}
1092
David Brazdil6f82fbd2016-09-14 11:55:26 +01001093TEST_F(VerifierDepsTest, EncodeDecode) {
1094 VerifyDexFile();
1095
1096 ASSERT_EQ(1u, NumberOfCompiledDexFiles());
1097 ASSERT_TRUE(HasEachKindOfRecord());
1098
1099 std::vector<uint8_t> buffer;
Nicolas Geoffrayd01f60c2016-10-28 14:45:48 +01001100 verifier_deps_->Encode(dex_files_, &buffer);
David Brazdil6f82fbd2016-09-14 11:55:26 +01001101 ASSERT_FALSE(buffer.empty());
1102
Nicolas Geoffraye70dd562016-10-30 21:03:35 +00001103 VerifierDeps decoded_deps(dex_files_, ArrayRef<const uint8_t>(buffer));
David Brazdil6f82fbd2016-09-14 11:55:26 +01001104 ASSERT_TRUE(verifier_deps_->Equals(decoded_deps));
1105}
1106
Nicolas Geoffrayd01f60c2016-10-28 14:45:48 +01001107TEST_F(VerifierDepsTest, EncodeDecodeMulti) {
1108 VerifyDexFile("MultiDex");
1109
1110 ASSERT_GT(NumberOfCompiledDexFiles(), 1u);
1111 std::vector<uint8_t> buffer;
1112 verifier_deps_->Encode(dex_files_, &buffer);
1113 ASSERT_FALSE(buffer.empty());
1114
1115 // Create new DexFile, to mess with std::map order: the verifier deps used
1116 // to iterate over the map, which doesn't guarantee insertion order. We fixed
1117 // this by passing the expected order when encoding/decoding.
1118 std::vector<std::unique_ptr<const DexFile>> first_dex_files = OpenTestDexFiles("VerifierDeps");
1119 std::vector<std::unique_ptr<const DexFile>> second_dex_files = OpenTestDexFiles("MultiDex");
1120 std::vector<const DexFile*> dex_files;
1121 for (auto& dex_file : first_dex_files) {
1122 dex_files.push_back(dex_file.get());
1123 }
1124 for (auto& dex_file : second_dex_files) {
1125 dex_files.push_back(dex_file.get());
1126 }
1127
1128 // Dump the new verifier deps to ensure it can properly read the data.
Nicolas Geoffraye70dd562016-10-30 21:03:35 +00001129 VerifierDeps decoded_deps(dex_files, ArrayRef<const uint8_t>(buffer));
Nicolas Geoffrayd01f60c2016-10-28 14:45:48 +01001130 std::ostringstream stream;
1131 VariableIndentationOutputStream os(&stream);
1132 decoded_deps.Dump(&os);
1133}
1134
Nicolas Geoffray08025182016-10-25 17:20:18 +01001135TEST_F(VerifierDepsTest, UnverifiedClasses) {
1136 VerifyDexFile();
1137 ASSERT_FALSE(HasUnverifiedClass("LMyThread;"));
1138 // Test that a class with a soft failure is recorded.
1139 ASSERT_TRUE(HasUnverifiedClass("LMain;"));
1140 // Test that a class with hard failure is recorded.
1141 ASSERT_TRUE(HasUnverifiedClass("LMyVerificationFailure;"));
1142 // Test that a class with unresolved super is recorded.
Nicolas Geoffray7cc3ae52017-03-07 14:33:37 +00001143 ASSERT_TRUE(HasUnverifiedClass("LMyClassWithNoSuper;"));
Nicolas Geoffray08025182016-10-25 17:20:18 +01001144 // Test that a class with unresolved super and hard failure is recorded.
1145 ASSERT_TRUE(HasUnverifiedClass("LMyClassWithNoSuperButFailures;"));
1146}
1147
Nicolas Geoffray8904b6f2016-10-28 19:50:34 +01001148TEST_F(VerifierDepsTest, VerifyDeps) {
1149 VerifyDexFile();
1150
1151 ASSERT_EQ(1u, NumberOfCompiledDexFiles());
1152 ASSERT_TRUE(HasEachKindOfRecord());
1153
1154 // When validating, we create a new class loader, as
1155 // the existing `class_loader_` may contain erroneous classes,
1156 // that ClassLinker::FindClass won't return.
1157
1158 ScopedObjectAccess soa(Thread::Current());
1159 StackHandleScope<1> hs(soa.Self());
1160 MutableHandle<mirror::ClassLoader> new_class_loader(hs.NewHandle<mirror::ClassLoader>(nullptr));
1161 {
1162 new_class_loader.Assign(soa.Decode<mirror::ClassLoader>(LoadDex("VerifierDeps")));
Nicolas Geoffray6bb7f1b2016-11-03 10:52:49 +00001163 ASSERT_TRUE(verifier_deps_->ValidateDependencies(new_class_loader, soa.Self()));
Nicolas Geoffray8904b6f2016-10-28 19:50:34 +01001164 }
1165
1166 std::vector<uint8_t> buffer;
1167 verifier_deps_->Encode(dex_files_, &buffer);
1168 ASSERT_FALSE(buffer.empty());
1169
1170 {
1171 VerifierDeps decoded_deps(dex_files_, ArrayRef<const uint8_t>(buffer));
1172 new_class_loader.Assign(soa.Decode<mirror::ClassLoader>(LoadDex("VerifierDeps")));
Nicolas Geoffray6bb7f1b2016-11-03 10:52:49 +00001173 ASSERT_TRUE(decoded_deps.ValidateDependencies(new_class_loader, soa.Self()));
Nicolas Geoffray8904b6f2016-10-28 19:50:34 +01001174 }
1175
1176 // Fiddle with the dependencies to make sure we catch any change and fail to verify.
1177
1178 {
1179 // Mess up with the assignable_types.
1180 VerifierDeps decoded_deps(dex_files_, ArrayRef<const uint8_t>(buffer));
1181 VerifierDeps::DexFileDeps* deps = decoded_deps.GetDexFileDeps(*primary_dex_file_);
1182 deps->assignable_types_.insert(*deps->unassignable_types_.begin());
1183 new_class_loader.Assign(soa.Decode<mirror::ClassLoader>(LoadDex("VerifierDeps")));
Nicolas Geoffray6bb7f1b2016-11-03 10:52:49 +00001184 ASSERT_FALSE(decoded_deps.ValidateDependencies(new_class_loader, soa.Self()));
Nicolas Geoffray8904b6f2016-10-28 19:50:34 +01001185 }
1186
1187 {
1188 // Mess up with the unassignable_types.
1189 VerifierDeps decoded_deps(dex_files_, ArrayRef<const uint8_t>(buffer));
1190 VerifierDeps::DexFileDeps* deps = decoded_deps.GetDexFileDeps(*primary_dex_file_);
1191 deps->unassignable_types_.insert(*deps->assignable_types_.begin());
1192 new_class_loader.Assign(soa.Decode<mirror::ClassLoader>(LoadDex("VerifierDeps")));
Nicolas Geoffray6bb7f1b2016-11-03 10:52:49 +00001193 ASSERT_FALSE(decoded_deps.ValidateDependencies(new_class_loader, soa.Self()));
Nicolas Geoffray8904b6f2016-10-28 19:50:34 +01001194 }
1195
1196 // Mess up with classes.
1197 {
1198 VerifierDeps decoded_deps(dex_files_, ArrayRef<const uint8_t>(buffer));
1199 VerifierDeps::DexFileDeps* deps = decoded_deps.GetDexFileDeps(*primary_dex_file_);
1200 bool found = false;
1201 for (const auto& entry : deps->classes_) {
1202 if (entry.IsResolved()) {
1203 deps->classes_.insert(VerifierDeps::ClassResolution(
1204 entry.GetDexTypeIndex(), VerifierDeps::kUnresolvedMarker));
1205 found = true;
1206 break;
1207 }
1208 }
1209 ASSERT_TRUE(found);
1210 new_class_loader.Assign(soa.Decode<mirror::ClassLoader>(LoadDex("VerifierDeps")));
Nicolas Geoffray6bb7f1b2016-11-03 10:52:49 +00001211 ASSERT_FALSE(decoded_deps.ValidateDependencies(new_class_loader, soa.Self()));
Nicolas Geoffray8904b6f2016-10-28 19:50:34 +01001212 }
1213
1214 {
1215 VerifierDeps decoded_deps(dex_files_, ArrayRef<const uint8_t>(buffer));
1216 VerifierDeps::DexFileDeps* deps = decoded_deps.GetDexFileDeps(*primary_dex_file_);
1217 bool found = false;
1218 for (const auto& entry : deps->classes_) {
1219 if (!entry.IsResolved()) {
1220 deps->classes_.insert(VerifierDeps::ClassResolution(
1221 entry.GetDexTypeIndex(), VerifierDeps::kUnresolvedMarker - 1));
1222 found = true;
1223 break;
1224 }
1225 }
1226 ASSERT_TRUE(found);
1227 new_class_loader.Assign(soa.Decode<mirror::ClassLoader>(LoadDex("VerifierDeps")));
Nicolas Geoffray6bb7f1b2016-11-03 10:52:49 +00001228 ASSERT_FALSE(decoded_deps.ValidateDependencies(new_class_loader, soa.Self()));
Nicolas Geoffray8904b6f2016-10-28 19:50:34 +01001229 }
1230
1231 {
1232 VerifierDeps decoded_deps(dex_files_, ArrayRef<const uint8_t>(buffer));
1233 VerifierDeps::DexFileDeps* deps = decoded_deps.GetDexFileDeps(*primary_dex_file_);
1234 bool found = false;
1235 for (const auto& entry : deps->classes_) {
1236 if (entry.IsResolved()) {
1237 deps->classes_.insert(VerifierDeps::ClassResolution(
1238 entry.GetDexTypeIndex(), entry.GetAccessFlags() - 1));
1239 found = true;
1240 break;
1241 }
1242 }
1243 ASSERT_TRUE(found);
1244 new_class_loader.Assign(soa.Decode<mirror::ClassLoader>(LoadDex("VerifierDeps")));
Nicolas Geoffray6bb7f1b2016-11-03 10:52:49 +00001245 ASSERT_FALSE(decoded_deps.ValidateDependencies(new_class_loader, soa.Self()));
Nicolas Geoffray8904b6f2016-10-28 19:50:34 +01001246 }
1247
1248 // Mess up with fields.
1249 {
1250 VerifierDeps decoded_deps(dex_files_, ArrayRef<const uint8_t>(buffer));
1251 VerifierDeps::DexFileDeps* deps = decoded_deps.GetDexFileDeps(*primary_dex_file_);
1252 bool found = false;
1253 for (const auto& entry : deps->fields_) {
1254 if (entry.IsResolved()) {
1255 deps->fields_.insert(VerifierDeps::FieldResolution(entry.GetDexFieldIndex(),
1256 VerifierDeps::kUnresolvedMarker,
1257 entry.GetDeclaringClassIndex()));
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 {
1268 VerifierDeps decoded_deps(dex_files_, ArrayRef<const uint8_t>(buffer));
1269 VerifierDeps::DexFileDeps* deps = decoded_deps.GetDexFileDeps(*primary_dex_file_);
1270 bool found = false;
1271 for (const auto& entry : deps->fields_) {
1272 if (!entry.IsResolved()) {
Andreas Gampe8a0128a2016-11-28 07:38:35 -08001273 constexpr dex::StringIndex kStringIndexZero(0); // We know there is a class there.
Nicolas Geoffray8904b6f2016-10-28 19:50:34 +01001274 deps->fields_.insert(VerifierDeps::FieldResolution(0 /* we know there is a field there */,
1275 VerifierDeps::kUnresolvedMarker - 1,
Andreas Gampe8a0128a2016-11-28 07:38:35 -08001276 kStringIndexZero));
Nicolas Geoffray8904b6f2016-10-28 19:50:34 +01001277 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()) {
1292 deps->fields_.insert(VerifierDeps::FieldResolution(entry.GetDexFieldIndex(),
1293 entry.GetAccessFlags() - 1,
1294 entry.GetDeclaringClassIndex()));
1295 found = true;
1296 break;
1297 }
1298 }
1299 ASSERT_TRUE(found);
1300 new_class_loader.Assign(soa.Decode<mirror::ClassLoader>(LoadDex("VerifierDeps")));
Nicolas Geoffray6bb7f1b2016-11-03 10:52:49 +00001301 ASSERT_FALSE(decoded_deps.ValidateDependencies(new_class_loader, soa.Self()));
Nicolas Geoffray8904b6f2016-10-28 19:50:34 +01001302 }
1303
1304 {
1305 VerifierDeps decoded_deps(dex_files_, ArrayRef<const uint8_t>(buffer));
1306 VerifierDeps::DexFileDeps* deps = decoded_deps.GetDexFileDeps(*primary_dex_file_);
1307 bool found = false;
1308 for (const auto& entry : deps->fields_) {
Andreas Gampe8a0128a2016-11-28 07:38:35 -08001309 constexpr dex::StringIndex kNewTypeIndex(0);
Nicolas Geoffray8904b6f2016-10-28 19:50:34 +01001310 if (entry.GetDeclaringClassIndex() != kNewTypeIndex) {
1311 deps->fields_.insert(VerifierDeps::FieldResolution(entry.GetDexFieldIndex(),
1312 entry.GetAccessFlags(),
1313 kNewTypeIndex));
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 // Mess up with methods.
Vladimir Markoba118822017-06-12 15:41:56 +01001324 {
1325 VerifierDeps decoded_deps(dex_files_, ArrayRef<const uint8_t>(buffer));
1326 VerifierDeps::DexFileDeps* deps = decoded_deps.GetDexFileDeps(*primary_dex_file_);
1327 bool found = false;
1328 std::set<VerifierDeps::MethodResolution>* methods = &deps->methods_;
1329 for (const auto& entry : *methods) {
1330 if (entry.IsResolved()) {
1331 methods->insert(VerifierDeps::MethodResolution(entry.GetDexMethodIndex(),
1332 VerifierDeps::kUnresolvedMarker,
1333 entry.GetDeclaringClassIndex()));
1334 found = true;
1335 break;
Nicolas Geoffray8904b6f2016-10-28 19:50:34 +01001336 }
Nicolas Geoffray8904b6f2016-10-28 19:50:34 +01001337 }
Vladimir Markoba118822017-06-12 15:41:56 +01001338 ASSERT_TRUE(found);
1339 new_class_loader.Assign(soa.Decode<mirror::ClassLoader>(LoadDex("VerifierDeps")));
1340 ASSERT_FALSE(decoded_deps.ValidateDependencies(new_class_loader, soa.Self()));
1341 }
Nicolas Geoffray8904b6f2016-10-28 19:50:34 +01001342
Vladimir Markoba118822017-06-12 15:41:56 +01001343 {
1344 VerifierDeps decoded_deps(dex_files_, ArrayRef<const uint8_t>(buffer));
1345 VerifierDeps::DexFileDeps* deps = decoded_deps.GetDexFileDeps(*primary_dex_file_);
1346 bool found = false;
1347 std::set<VerifierDeps::MethodResolution>* methods = &deps->methods_;
1348 for (const auto& entry : *methods) {
1349 if (!entry.IsResolved()) {
1350 constexpr dex::StringIndex kStringIndexZero(0); // We know there is a class there.
1351 methods->insert(VerifierDeps::MethodResolution(0 /* we know there is a method there */,
1352 VerifierDeps::kUnresolvedMarker - 1,
1353 kStringIndexZero));
1354 found = true;
1355 break;
Nicolas Geoffray8904b6f2016-10-28 19:50:34 +01001356 }
Nicolas Geoffray8904b6f2016-10-28 19:50:34 +01001357 }
Vladimir Markoba118822017-06-12 15:41:56 +01001358 ASSERT_TRUE(found);
1359 new_class_loader.Assign(soa.Decode<mirror::ClassLoader>(LoadDex("VerifierDeps")));
1360 ASSERT_FALSE(decoded_deps.ValidateDependencies(new_class_loader, soa.Self()));
1361 }
Nicolas Geoffray8904b6f2016-10-28 19:50:34 +01001362
Vladimir Markoba118822017-06-12 15:41:56 +01001363 {
1364 VerifierDeps decoded_deps(dex_files_, ArrayRef<const uint8_t>(buffer));
1365 VerifierDeps::DexFileDeps* deps = decoded_deps.GetDexFileDeps(*primary_dex_file_);
1366 bool found = false;
1367 std::set<VerifierDeps::MethodResolution>* methods = &deps->methods_;
1368 for (const auto& entry : *methods) {
1369 if (entry.IsResolved()) {
1370 methods->insert(VerifierDeps::MethodResolution(entry.GetDexMethodIndex(),
1371 entry.GetAccessFlags() - 1,
1372 entry.GetDeclaringClassIndex()));
1373 found = true;
1374 break;
Nicolas Geoffray8904b6f2016-10-28 19:50:34 +01001375 }
Nicolas Geoffray8904b6f2016-10-28 19:50:34 +01001376 }
Vladimir Markoba118822017-06-12 15:41:56 +01001377 ASSERT_TRUE(found);
1378 new_class_loader.Assign(soa.Decode<mirror::ClassLoader>(LoadDex("VerifierDeps")));
1379 ASSERT_FALSE(decoded_deps.ValidateDependencies(new_class_loader, soa.Self()));
1380 }
Nicolas Geoffray8904b6f2016-10-28 19:50:34 +01001381
Vladimir Markoba118822017-06-12 15:41:56 +01001382 {
1383 VerifierDeps decoded_deps(dex_files_, ArrayRef<const uint8_t>(buffer));
1384 VerifierDeps::DexFileDeps* deps = decoded_deps.GetDexFileDeps(*primary_dex_file_);
1385 bool found = false;
1386 std::set<VerifierDeps::MethodResolution>* methods = &deps->methods_;
1387 for (const auto& entry : *methods) {
1388 constexpr dex::StringIndex kNewTypeIndex(0);
1389 if (entry.IsResolved() && entry.GetDeclaringClassIndex() != kNewTypeIndex) {
1390 methods->insert(VerifierDeps::MethodResolution(entry.GetDexMethodIndex(),
1391 entry.GetAccessFlags(),
1392 kNewTypeIndex));
1393 found = true;
1394 break;
Nicolas Geoffray8904b6f2016-10-28 19:50:34 +01001395 }
Nicolas Geoffray8904b6f2016-10-28 19:50:34 +01001396 }
Vladimir Markoba118822017-06-12 15:41:56 +01001397 ASSERT_TRUE(found);
1398 new_class_loader.Assign(soa.Decode<mirror::ClassLoader>(LoadDex("VerifierDeps")));
1399 ASSERT_FALSE(decoded_deps.ValidateDependencies(new_class_loader, soa.Self()));
Nicolas Geoffray6bb7f1b2016-11-03 10:52:49 +00001400 }
1401}
1402
1403TEST_F(VerifierDepsTest, CompilerDriver) {
1404 SetupCompilerDriver();
1405
1406 // Test both multi-dex and single-dex configuration.
1407 for (const char* multi : { "MultiDex", static_cast<const char*>(nullptr) }) {
1408 // Test that the compiler driver behaves as expected when the dependencies
1409 // verify and when they don't verify.
1410 for (bool verify_failure : { false, true }) {
1411 {
1412 ScopedObjectAccess soa(Thread::Current());
1413 LoadDexFile(&soa, "VerifierDeps", multi);
1414 }
1415 VerifyWithCompilerDriver(/* verifier_deps */ nullptr);
1416
1417 std::vector<uint8_t> buffer;
1418 verifier_deps_->Encode(dex_files_, &buffer);
1419
1420 {
1421 ScopedObjectAccess soa(Thread::Current());
1422 LoadDexFile(&soa, "VerifierDeps", multi);
1423 }
1424 verifier::VerifierDeps decoded_deps(dex_files_, ArrayRef<const uint8_t>(buffer));
1425 if (verify_failure) {
1426 // Just taint the decoded VerifierDeps with one invalid entry.
1427 VerifierDeps::DexFileDeps* deps = decoded_deps.GetDexFileDeps(*primary_dex_file_);
1428 bool found = false;
1429 for (const auto& entry : deps->classes_) {
1430 if (entry.IsResolved()) {
1431 deps->classes_.insert(VerifierDeps::ClassResolution(
1432 entry.GetDexTypeIndex(), VerifierDeps::kUnresolvedMarker));
1433 found = true;
1434 break;
1435 }
1436 }
1437 ASSERT_TRUE(found);
1438 }
1439 VerifyWithCompilerDriver(&decoded_deps);
1440
1441 if (verify_failure) {
1442 ASSERT_FALSE(verifier_deps_ == nullptr);
1443 ASSERT_FALSE(verifier_deps_->Equals(decoded_deps));
1444 } else {
Nicolas Geoffray6bb7f1b2016-11-03 10:52:49 +00001445 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