1 /*
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
17 // Test is in compiler, as it uses compiler related code.
18 #include "verifier/verifier_deps.h"
19
20 #include "art_method-inl.h"
21 #include "base/indenter.h"
22 #include "class_linker.h"
23 #include "common_compiler_driver_test.h"
24 #include "compiler_callbacks.h"
25 #include "dex/class_accessor-inl.h"
26 #include "dex/class_iterator.h"
27 #include "dex/dex_file-inl.h"
28 #include "dex/dex_file_types.h"
29 #include "dex/verification_results.h"
30 #include "dex/verified_method.h"
31 #include "driver/compiler_driver-inl.h"
32 #include "driver/compiler_options.h"
33 #include "handle_scope-inl.h"
34 #include "mirror/class_loader.h"
35 #include "runtime.h"
36 #include "scoped_thread_state_change-inl.h"
37 #include "thread.h"
38 #include "utils/atomic_dex_ref_map-inl.h"
39 #include "verifier/method_verifier-inl.h"
40
41 namespace art {
42 namespace verifier {
43
44 class VerifierDepsCompilerCallbacks : public CompilerCallbacks {
45 public:
VerifierDepsCompilerCallbacks()46 VerifierDepsCompilerCallbacks()
47 : CompilerCallbacks(CompilerCallbacks::CallbackMode::kCompileApp),
48 deps_(nullptr) {}
49
MethodVerified(verifier::MethodVerifier * verifier ATTRIBUTE_UNUSED)50 void MethodVerified(verifier::MethodVerifier* verifier ATTRIBUTE_UNUSED) override {}
ClassRejected(ClassReference ref ATTRIBUTE_UNUSED)51 void ClassRejected(ClassReference ref ATTRIBUTE_UNUSED) override {}
52
GetVerifierDeps() const53 verifier::VerifierDeps* GetVerifierDeps() const override { return deps_; }
SetVerifierDeps(verifier::VerifierDeps * deps)54 void SetVerifierDeps(verifier::VerifierDeps* deps) override { deps_ = deps; }
55
56 private:
57 verifier::VerifierDeps* deps_;
58 };
59
60 class VerifierDepsTest : public CommonCompilerDriverTest {
61 public:
SetUpRuntimeOptions(RuntimeOptions * options)62 void SetUpRuntimeOptions(RuntimeOptions* options) override {
63 CommonCompilerTest::SetUpRuntimeOptions(options);
64 callbacks_.reset(new VerifierDepsCompilerCallbacks());
65 }
66
FindClassByName(ScopedObjectAccess & soa,const std::string & name)67 ObjPtr<mirror::Class> FindClassByName(ScopedObjectAccess& soa, const std::string& name)
68 REQUIRES_SHARED(Locks::mutator_lock_) {
69 StackHandleScope<1> hs(soa.Self());
70 Handle<mirror::ClassLoader> class_loader_handle(
71 hs.NewHandle(soa.Decode<mirror::ClassLoader>(class_loader_)));
72 ObjPtr<mirror::Class> klass =
73 class_linker_->FindClass(soa.Self(), name.c_str(), class_loader_handle);
74 if (klass == nullptr) {
75 DCHECK(soa.Self()->IsExceptionPending());
76 soa.Self()->ClearException();
77 }
78 return klass;
79 }
80
SetupCompilerDriver()81 void SetupCompilerDriver() {
82 compiler_options_->image_type_ = CompilerOptions::ImageType::kNone;
83 compiler_driver_->InitializeThreadPools();
84 }
85
VerifyWithCompilerDriver(verifier::VerifierDeps * verifier_deps)86 void VerifyWithCompilerDriver(verifier::VerifierDeps* verifier_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 if (verifier_deps == nullptr) {
91 // Create some verifier deps by default if they are not already specified.
92 verifier_deps = new verifier::VerifierDeps(dex_files_);
93 verifier_deps_.reset(verifier_deps);
94 }
95 callbacks_->SetVerifierDeps(verifier_deps);
96 compiler_driver_->Verify(class_loader_, dex_files_, &timings, verification_results_.get());
97 callbacks_->SetVerifierDeps(nullptr);
98 // Clear entries in the verification results to avoid hitting a DCHECK that
99 // we always succeed inserting a new entry after verifying.
100 AtomicDexRefMap<MethodReference, const VerifiedMethod*>* map =
101 &verification_results_->atomic_verified_methods_;
102 map->Visit([](const DexFileReference& ref ATTRIBUTE_UNUSED, const VerifiedMethod* method) {
103 delete method;
104 });
105 map->ClearEntries();
106 }
107
SetVerifierDeps(const std::vector<const DexFile * > & dex_files)108 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());
113 }
114
LoadDexFile(ScopedObjectAccess & soa,const char * name1,const char * name2=nullptr)115 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 }
128 for (const DexFile* dex_file : dex_files_) {
129 verification_results_->AddDexFile(dex_file);
130 }
131 SetDexFilesForOatFile(dex_files_);
132 }
133
LoadDexFile(ScopedObjectAccess & soa)134 void LoadDexFile(ScopedObjectAccess& soa) REQUIRES_SHARED(Locks::mutator_lock_) {
135 LoadDexFile(soa, "VerifierDeps");
136 CHECK_EQ(dex_files_.size(), 1u);
137 klass_Main_ = FindClassByName(soa, "LMain;");
138 CHECK(klass_Main_ != nullptr);
139 }
140
VerifyMethod(const std::string & method_name)141 bool VerifyMethod(const std::string& method_name) {
142 ScopedObjectAccess soa(Thread::Current());
143 LoadDexFile(soa);
144
145 StackHandleScope<2> hs(soa.Self());
146 Handle<mirror::ClassLoader> class_loader_handle(
147 hs.NewHandle(soa.Decode<mirror::ClassLoader>(class_loader_)));
148 Handle<mirror::DexCache> dex_cache_handle(hs.NewHandle(klass_Main_->GetDexCache()));
149
150 const dex::ClassDef* class_def = klass_Main_->GetClassDef();
151 ClassAccessor accessor(*primary_dex_file_, *class_def);
152
153 bool has_failures = true;
154 bool found_method = false;
155
156 for (const ClassAccessor::Method& method : accessor.GetMethods()) {
157 ArtMethod* resolved_method =
158 class_linker_->ResolveMethod<ClassLinker::ResolveMode::kNoChecks>(
159 method.GetIndex(),
160 dex_cache_handle,
161 class_loader_handle,
162 /* referrer= */ nullptr,
163 method.GetInvokeType(class_def->access_flags_));
164 CHECK(resolved_method != nullptr);
165 if (method_name == resolved_method->GetName()) {
166 std::unique_ptr<MethodVerifier> verifier(
167 MethodVerifier::CreateVerifier(soa.Self(),
168 callbacks_->GetVerifierDeps(),
169 primary_dex_file_,
170 dex_cache_handle,
171 class_loader_handle,
172 *class_def,
173 method.GetCodeItem(),
174 method.GetIndex(),
175 resolved_method,
176 method.GetAccessFlags(),
177 /* can_load_classes= */ true,
178 /* allow_soft_failures= */ true,
179 /* need_precise_constants= */ true,
180 /* verify to dump */ false,
181 /* allow_thread_suspension= */ true,
182 /* api_level= */ 0));
183 verifier->Verify();
184 soa.Self()->SetVerifierDeps(nullptr);
185 has_failures = verifier->HasFailures();
186 found_method = true;
187 }
188 }
189 CHECK(found_method) << "Expected to find method " << method_name;
190 return !has_failures;
191 }
192
VerifyDexFile(const char * multidex=nullptr)193 void VerifyDexFile(const char* multidex = nullptr) {
194 {
195 ScopedObjectAccess soa(Thread::Current());
196 LoadDexFile(soa, "VerifierDeps", multidex);
197 }
198 SetupCompilerDriver();
199 VerifyWithCompilerDriver(/* verifier_deps= */ nullptr);
200 }
201
TestAssignabilityRecording(const std::string & dst,const std::string & src)202 bool TestAssignabilityRecording(const std::string& dst, const std::string& src) {
203 ScopedObjectAccess soa(Thread::Current());
204 LoadDexFile(soa);
205 StackHandleScope<1> hs(soa.Self());
206 Handle<mirror::Class> klass_dst = hs.NewHandle(FindClassByName(soa, dst));
207 DCHECK(klass_dst != nullptr) << dst;
208 ObjPtr<mirror::Class> klass_src = FindClassByName(soa, src);
209 DCHECK(klass_src != nullptr) << src;
210 verifier_deps_->AddAssignability(*primary_dex_file_,
211 primary_dex_file_->GetClassDef(0),
212 klass_dst.Get(),
213 klass_src);
214 return true;
215 }
216
217 // Check that the status of classes in `class_loader_` match the
218 // expected status in `deps`.
VerifyClassStatus(const verifier::VerifierDeps & deps)219 void VerifyClassStatus(const verifier::VerifierDeps& deps) {
220 ScopedObjectAccess soa(Thread::Current());
221 StackHandleScope<2> hs(soa.Self());
222 Handle<mirror::ClassLoader> class_loader_handle(
223 hs.NewHandle(soa.Decode<mirror::ClassLoader>(class_loader_)));
224 MutableHandle<mirror::Class> cls(hs.NewHandle<mirror::Class>(nullptr));
225 for (const DexFile* dex_file : dex_files_) {
226 const std::vector<bool>& verified_classes = deps.GetVerifiedClasses(*dex_file);
227 ASSERT_EQ(verified_classes.size(), dex_file->NumClassDefs());
228 for (uint32_t i = 0; i < dex_file->NumClassDefs(); ++i) {
229 const dex::ClassDef& class_def = dex_file->GetClassDef(i);
230 const char* descriptor = dex_file->GetClassDescriptor(class_def);
231 cls.Assign(class_linker_->FindClass(soa.Self(), descriptor, class_loader_handle));
232 if (cls == nullptr) {
233 CHECK(soa.Self()->IsExceptionPending());
234 soa.Self()->ClearException();
235 } else if (&cls->GetDexFile() != dex_file) {
236 // Ignore classes from different dex files.
237 } else if (verified_classes[i]) {
238 ASSERT_EQ(cls->GetStatus(), ClassStatus::kVerifiedNeedsAccessChecks);
239 } else {
240 ASSERT_LT(cls->GetStatus(), ClassStatus::kVerified);
241 }
242 }
243 }
244 }
245
GetClassDefIndex(const std::string & cls,const DexFile & dex_file)246 uint16_t GetClassDefIndex(const std::string& cls, const DexFile& dex_file) {
247 const dex::TypeId* type_id = dex_file.FindTypeId(cls.c_str());
248 DCHECK(type_id != nullptr);
249 dex::TypeIndex type_idx = dex_file.GetIndexForTypeId(*type_id);
250 const dex::ClassDef* class_def = dex_file.FindClassDef(type_idx);
251 DCHECK(class_def != nullptr);
252 return dex_file.GetIndexForClassDef(*class_def);
253 }
254
HasVerifiedClass(const std::string & cls)255 bool HasVerifiedClass(const std::string& cls) {
256 return HasVerifiedClass(cls, *primary_dex_file_);
257 }
258
HasUnverifiedClass(const std::string & cls)259 bool HasUnverifiedClass(const std::string& cls) {
260 return !HasVerifiedClass(cls, *primary_dex_file_);
261 }
262
HasUnverifiedClass(const std::string & cls,const DexFile & dex_file)263 bool HasUnverifiedClass(const std::string& cls, const DexFile& dex_file) {
264 return !HasVerifiedClass(cls, dex_file);
265 }
266
HasVerifiedClass(const std::string & cls,const DexFile & dex_file)267 bool HasVerifiedClass(const std::string& cls, const DexFile& dex_file) {
268 uint16_t class_def_idx = GetClassDefIndex(cls, dex_file);
269 return verifier_deps_->GetVerifiedClasses(dex_file)[class_def_idx];
270 }
271
272 // Iterates over all assignability records and tries to find an entry which
273 // matches the expected destination/source pair.
HasAssignable(const std::string & expected_destination,const std::string & expected_source) const274 bool HasAssignable(const std::string& expected_destination,
275 const std::string& expected_source) const {
276 for (auto& dex_dep : verifier_deps_->dex_deps_) {
277 const DexFile& dex_file = *dex_dep.first;
278 auto& storage = dex_dep.second->assignable_types_;
279 for (auto& set : storage) {
280 for (auto& entry : set) {
281 std::string actual_destination =
282 verifier_deps_->GetStringFromId(dex_file, entry.GetDestination());
283 std::string actual_source = verifier_deps_->GetStringFromId(dex_file, entry.GetSource());
284 if ((expected_destination == actual_destination) && (expected_source == actual_source)) {
285 return true;
286 }
287 }
288 }
289 }
290 return false;
291 }
292
NumberOfCompiledDexFiles()293 size_t NumberOfCompiledDexFiles() {
294 return verifier_deps_->dex_deps_.size();
295 }
296
HasBoolValue(const std::vector<bool> & vec,bool value)297 bool HasBoolValue(const std::vector<bool>& vec, bool value) {
298 return std::count(vec.begin(), vec.end(), value) > 0;
299 }
300
HasEachKindOfRecord()301 bool HasEachKindOfRecord() {
302 bool has_strings = false;
303 bool has_assignability = false;
304 bool has_verified_classes = false;
305 bool has_unverified_classes = false;
306
307 for (auto& entry : verifier_deps_->dex_deps_) {
308 has_strings |= !entry.second->strings_.empty();
309 has_assignability |= !entry.second->assignable_types_.empty();
310 has_verified_classes |= HasBoolValue(entry.second->verified_classes_, true);
311 has_unverified_classes |= HasBoolValue(entry.second->verified_classes_, false);
312 }
313
314 return has_strings &&
315 has_assignability &&
316 has_verified_classes &&
317 has_unverified_classes;
318 }
319
320 // Load the dex file again with a new class loader, decode the VerifierDeps
321 // in `buffer`, allow the caller to modify the deps and then run validation.
322 template<typename Fn>
RunValidation(Fn fn,const std::vector<uint8_t> & buffer,std::string * error_msg)323 bool RunValidation(Fn fn, const std::vector<uint8_t>& buffer, std::string* error_msg) {
324 ScopedObjectAccess soa(Thread::Current());
325
326 jobject second_loader = LoadDex("VerifierDeps");
327 const auto& second_dex_files = GetDexFiles(second_loader);
328
329 VerifierDeps decoded_deps(second_dex_files, /*output_only=*/ false);
330 bool parsed = decoded_deps.ParseStoredData(second_dex_files, ArrayRef<const uint8_t>(buffer));
331 CHECK(parsed);
332 VerifierDeps::DexFileDeps* decoded_dex_deps =
333 decoded_deps.GetDexFileDeps(*second_dex_files.front());
334
335 // Let the test modify the dependencies.
336 fn(*decoded_dex_deps);
337
338 StackHandleScope<1> hs(soa.Self());
339 Handle<mirror::ClassLoader> new_class_loader =
340 hs.NewHandle<mirror::ClassLoader>(soa.Decode<mirror::ClassLoader>(second_loader));
341
342 return decoded_deps.ValidateDependencies(soa.Self(),
343 new_class_loader,
344 error_msg);
345 }
346
347 std::unique_ptr<verifier::VerifierDeps> verifier_deps_;
348 std::vector<const DexFile*> dex_files_;
349 const DexFile* primary_dex_file_;
350 jobject class_loader_;
351 ObjPtr<mirror::Class> klass_Main_;
352 };
353
TEST_F(VerifierDepsTest,StringToId)354 TEST_F(VerifierDepsTest, StringToId) {
355 ScopedObjectAccess soa(Thread::Current());
356 LoadDexFile(soa);
357
358 dex::StringIndex id_Main1 = verifier_deps_->GetIdFromString(*primary_dex_file_, "LMain;");
359 ASSERT_LT(id_Main1.index_, primary_dex_file_->NumStringIds());
360 ASSERT_EQ("LMain;", verifier_deps_->GetStringFromId(*primary_dex_file_, id_Main1));
361
362 dex::StringIndex id_Main2 = verifier_deps_->GetIdFromString(*primary_dex_file_, "LMain;");
363 ASSERT_LT(id_Main2.index_, primary_dex_file_->NumStringIds());
364 ASSERT_EQ("LMain;", verifier_deps_->GetStringFromId(*primary_dex_file_, id_Main2));
365
366 dex::StringIndex id_Lorem1 = verifier_deps_->GetIdFromString(*primary_dex_file_, "Lorem ipsum");
367 ASSERT_GE(id_Lorem1.index_, primary_dex_file_->NumStringIds());
368 ASSERT_EQ("Lorem ipsum", verifier_deps_->GetStringFromId(*primary_dex_file_, id_Lorem1));
369
370 dex::StringIndex id_Lorem2 = verifier_deps_->GetIdFromString(*primary_dex_file_, "Lorem ipsum");
371 ASSERT_GE(id_Lorem2.index_, primary_dex_file_->NumStringIds());
372 ASSERT_EQ("Lorem ipsum", verifier_deps_->GetStringFromId(*primary_dex_file_, id_Lorem2));
373
374 ASSERT_EQ(id_Main1, id_Main2);
375 ASSERT_EQ(id_Lorem1, id_Lorem2);
376 ASSERT_NE(id_Main1, id_Lorem1);
377 }
378
TEST_F(VerifierDepsTest,Assignable_BothInBoot)379 TEST_F(VerifierDepsTest, Assignable_BothInBoot) {
380 ASSERT_TRUE(TestAssignabilityRecording(/* dst= */ "Ljava/util/TimeZone;",
381 /* src= */ "Ljava/util/SimpleTimeZone;"));
382 ASSERT_TRUE(HasAssignable("Ljava/util/TimeZone;", "Ljava/util/SimpleTimeZone;"));
383 }
384
TEST_F(VerifierDepsTest,Assignable_BothArrays_Resolved)385 TEST_F(VerifierDepsTest, Assignable_BothArrays_Resolved) {
386 ASSERT_TRUE(TestAssignabilityRecording(/* dst= */ "[[Ljava/util/TimeZone;",
387 /* src= */ "[[Ljava/util/SimpleTimeZone;"));
388 // If the component types of both arrays are resolved, we optimize the list of
389 // dependencies by recording a dependency on the component types.
390 ASSERT_FALSE(HasAssignable("[[Ljava/util/TimeZone;", "[[Ljava/util/SimpleTimeZone;"));
391 ASSERT_FALSE(HasAssignable("[Ljava/util/TimeZone;", "[Ljava/util/SimpleTimeZone;"));
392 ASSERT_TRUE(HasAssignable("Ljava/util/TimeZone;", "Ljava/util/SimpleTimeZone;"));
393 }
394
TEST_F(VerifierDepsTest,ReturnType_Reference)395 TEST_F(VerifierDepsTest, ReturnType_Reference) {
396 ASSERT_TRUE(VerifyMethod("ReturnType_Reference"));
397 ASSERT_TRUE(HasAssignable("Ljava/lang/Throwable;", "Ljava/lang/IllegalStateException;"));
398 }
399
TEST_F(VerifierDepsTest,InvokeArgumentType)400 TEST_F(VerifierDepsTest, InvokeArgumentType) {
401 ASSERT_TRUE(VerifyMethod("InvokeArgumentType"));
402 ASSERT_TRUE(HasAssignable("Ljava/util/TimeZone;", "Ljava/util/SimpleTimeZone;"));
403 }
404
TEST_F(VerifierDepsTest,MergeTypes_RegisterLines)405 TEST_F(VerifierDepsTest, MergeTypes_RegisterLines) {
406 ASSERT_TRUE(VerifyMethod("MergeTypes_RegisterLines"));
407 ASSERT_TRUE(HasAssignable("Ljava/lang/Exception;", "LMySocketTimeoutException;"));
408 ASSERT_TRUE(HasAssignable(
409 "Ljava/lang/Exception;", "Ljava/util/concurrent/TimeoutException;"));
410 }
411
TEST_F(VerifierDepsTest,MergeTypes_IfInstanceOf)412 TEST_F(VerifierDepsTest, MergeTypes_IfInstanceOf) {
413 ASSERT_TRUE(VerifyMethod("MergeTypes_IfInstanceOf"));
414 ASSERT_TRUE(HasAssignable("Ljava/lang/Exception;", "Ljava/net/SocketTimeoutException;"));
415 ASSERT_TRUE(HasAssignable(
416 "Ljava/lang/Exception;", "Ljava/util/concurrent/TimeoutException;"));
417 }
418
TEST_F(VerifierDepsTest,MergeTypes_Unresolved)419 TEST_F(VerifierDepsTest, MergeTypes_Unresolved) {
420 ASSERT_TRUE(VerifyMethod("MergeTypes_Unresolved"));
421 ASSERT_TRUE(HasAssignable("Ljava/lang/Exception;", "Ljava/net/SocketTimeoutException;"));
422 ASSERT_TRUE(HasAssignable(
423 "Ljava/lang/Exception;", "Ljava/util/concurrent/TimeoutException;"));
424 }
425
TEST_F(VerifierDepsTest,Throw)426 TEST_F(VerifierDepsTest, Throw) {
427 ASSERT_TRUE(VerifyMethod("Throw"));
428 ASSERT_TRUE(HasAssignable("Ljava/lang/Throwable;", "Ljava/lang/IllegalStateException;"));
429 }
430
TEST_F(VerifierDepsTest,MoveException_Resolved)431 TEST_F(VerifierDepsTest, MoveException_Resolved) {
432 ASSERT_TRUE(VerifyMethod("MoveException_Resolved"));
433
434 // Testing that all exception types are assignable to Throwable.
435 ASSERT_TRUE(HasAssignable("Ljava/lang/Throwable;", "Ljava/io/InterruptedIOException;"));
436 ASSERT_TRUE(HasAssignable("Ljava/lang/Throwable;", "Ljava/net/SocketTimeoutException;"));
437 ASSERT_TRUE(HasAssignable("Ljava/lang/Throwable;", "Ljava/util/zip/ZipException;"));
438
439 // Testing that the merge type is assignable to Throwable.
440 ASSERT_TRUE(HasAssignable("Ljava/lang/Throwable;", "Ljava/io/IOException;"));
441
442 // Merging of exception types.
443 ASSERT_TRUE(HasAssignable("Ljava/io/IOException;", "Ljava/io/InterruptedIOException;"));
444 ASSERT_TRUE(HasAssignable("Ljava/io/IOException;", "Ljava/util/zip/ZipException;"));
445 ASSERT_TRUE(HasAssignable(
446 "Ljava/io/InterruptedIOException;", "Ljava/net/SocketTimeoutException;"));
447 }
448
TEST_F(VerifierDepsTest,InstanceField_Resolved_DeclaredInReferenced)449 TEST_F(VerifierDepsTest, InstanceField_Resolved_DeclaredInReferenced) {
450 ASSERT_TRUE(VerifyMethod("InstanceField_Resolved_DeclaredInReferenced"));
451 ASSERT_TRUE(HasAssignable(
452 "Ljava/io/InterruptedIOException;", "LMySocketTimeoutException;"));
453 }
454
TEST_F(VerifierDepsTest,InstanceField_Resolved_DeclaredInSuperclass1)455 TEST_F(VerifierDepsTest, InstanceField_Resolved_DeclaredInSuperclass1) {
456 ASSERT_TRUE(VerifyMethod("InstanceField_Resolved_DeclaredInSuperclass1"));
457 ASSERT_TRUE(HasAssignable(
458 "Ljava/io/InterruptedIOException;", "LMySocketTimeoutException;"));
459 }
460
TEST_F(VerifierDepsTest,InstanceField_Resolved_DeclaredInSuperclass2)461 TEST_F(VerifierDepsTest, InstanceField_Resolved_DeclaredInSuperclass2) {
462 ASSERT_TRUE(VerifyMethod("InstanceField_Resolved_DeclaredInSuperclass2"));
463 ASSERT_TRUE(HasAssignable(
464 "Ljava/io/InterruptedIOException;", "LMySocketTimeoutException;"));
465 }
466
TEST_F(VerifierDepsTest,InvokeVirtual_Resolved_DeclaredInReferenced)467 TEST_F(VerifierDepsTest, InvokeVirtual_Resolved_DeclaredInReferenced) {
468 ASSERT_TRUE(VerifyMethod("InvokeVirtual_Resolved_DeclaredInReferenced"));
469 // Type dependency on `this` argument.
470 ASSERT_TRUE(HasAssignable("Ljava/lang/Throwable;", "LMySocketTimeoutException;"));
471 }
472
TEST_F(VerifierDepsTest,InvokeVirtual_Resolved_DeclaredInSuperclass1)473 TEST_F(VerifierDepsTest, InvokeVirtual_Resolved_DeclaredInSuperclass1) {
474 ASSERT_TRUE(VerifyMethod("InvokeVirtual_Resolved_DeclaredInSuperclass1"));
475 // Type dependency on `this` argument.
476 ASSERT_TRUE(HasAssignable("Ljava/lang/Throwable;", "LMySocketTimeoutException;"));
477 }
478
TEST_F(VerifierDepsTest,InvokeSuper_ThisAssignable)479 TEST_F(VerifierDepsTest, InvokeSuper_ThisAssignable) {
480 ASSERT_TRUE(VerifyMethod("InvokeSuper_ThisAssignable"));
481 ASSERT_TRUE(HasAssignable("Ljava/lang/Runnable;", "LMain;"));
482 }
483
TEST_F(VerifierDepsTest,EncodeDecode)484 TEST_F(VerifierDepsTest, EncodeDecode) {
485 VerifyDexFile();
486
487 ASSERT_EQ(1u, NumberOfCompiledDexFiles());
488 ASSERT_TRUE(HasEachKindOfRecord());
489
490 std::vector<uint8_t> buffer;
491 verifier_deps_->Encode(dex_files_, &buffer);
492 ASSERT_FALSE(buffer.empty());
493
494 VerifierDeps decoded_deps(dex_files_, /*output_only=*/ false);
495 bool parsed = decoded_deps.ParseStoredData(dex_files_, ArrayRef<const uint8_t>(buffer));
496 ASSERT_TRUE(parsed);
497 ASSERT_TRUE(verifier_deps_->Equals(decoded_deps));
498 }
499
TEST_F(VerifierDepsTest,EncodeDecodeMulti)500 TEST_F(VerifierDepsTest, EncodeDecodeMulti) {
501 VerifyDexFile("MultiDex");
502
503 ASSERT_GT(NumberOfCompiledDexFiles(), 1u);
504 std::vector<uint8_t> buffer;
505 verifier_deps_->Encode(dex_files_, &buffer);
506 ASSERT_FALSE(buffer.empty());
507
508 // Create new DexFile, to mess with std::map order: the verifier deps used
509 // to iterate over the map, which doesn't guarantee insertion order. We fixed
510 // this by passing the expected order when encoding/decoding.
511 std::vector<std::unique_ptr<const DexFile>> first_dex_files = OpenTestDexFiles("VerifierDeps");
512 std::vector<std::unique_ptr<const DexFile>> second_dex_files = OpenTestDexFiles("MultiDex");
513 std::vector<const DexFile*> dex_files;
514 for (auto& dex_file : first_dex_files) {
515 dex_files.push_back(dex_file.get());
516 }
517 for (auto& dex_file : second_dex_files) {
518 dex_files.push_back(dex_file.get());
519 }
520
521 // Dump the new verifier deps to ensure it can properly read the data.
522 VerifierDeps decoded_deps(dex_files, /*output_only=*/ false);
523 bool parsed = decoded_deps.ParseStoredData(dex_files, ArrayRef<const uint8_t>(buffer));
524 ASSERT_TRUE(parsed);
525 std::ostringstream stream;
526 VariableIndentationOutputStream os(&stream);
527 decoded_deps.Dump(&os);
528 }
529
TEST_F(VerifierDepsTest,UnverifiedClasses)530 TEST_F(VerifierDepsTest, UnverifiedClasses) {
531 VerifyDexFile();
532 ASSERT_FALSE(HasUnverifiedClass("LMyThread;"));
533 // Test that a class with a soft failure is recorded.
534 ASSERT_TRUE(HasUnverifiedClass("LMain;"));
535 // Test that a class with hard failure is recorded.
536 ASSERT_TRUE(HasUnverifiedClass("LMyVerificationFailure;"));
537 // Test that a class with unresolved super and hard failure is recorded.
538 ASSERT_TRUE(HasUnverifiedClass("LMyClassWithNoSuperButFailures;"));
539 // Test that a class with unresolved super can be verified.
540 ASSERT_TRUE(HasVerifiedClass("LMyClassWithNoSuper;"));
541 }
542
TEST_F(VerifierDepsTest,UnverifiedOrder)543 TEST_F(VerifierDepsTest, UnverifiedOrder) {
544 ScopedObjectAccess soa(Thread::Current());
545 jobject loader = LoadDex("VerifierDeps");
546 std::vector<const DexFile*> dex_files = GetDexFiles(loader);
547 ASSERT_GT(dex_files.size(), 0u);
548 const DexFile* dex_file = dex_files[0];
549 VerifierDeps deps1(dex_files);
550 deps1.MaybeRecordVerificationStatus(&deps1,
551 *dex_file,
552 dex_file->GetClassDef(0u),
553 verifier::FailureKind::kHardFailure);
554 deps1.MaybeRecordVerificationStatus(&deps1,
555 *dex_file,
556 dex_file->GetClassDef(1u),
557 verifier::FailureKind::kHardFailure);
558 VerifierDeps deps2(dex_files);
559 deps2.MaybeRecordVerificationStatus(&deps2,
560 *dex_file,
561 dex_file->GetClassDef(1u),
562 verifier::FailureKind::kHardFailure);
563 deps2.MaybeRecordVerificationStatus(&deps2,
564 *dex_file,
565 dex_file->GetClassDef(0u),
566 verifier::FailureKind::kHardFailure);
567 std::vector<uint8_t> buffer1;
568 deps1.Encode(dex_files, &buffer1);
569 std::vector<uint8_t> buffer2;
570 deps2.Encode(dex_files, &buffer2);
571 EXPECT_EQ(buffer1, buffer2);
572 }
573
TEST_F(VerifierDepsTest,VerifyDeps)574 TEST_F(VerifierDepsTest, VerifyDeps) {
575 std::string error_msg;
576
577 VerifyDexFile();
578 ASSERT_EQ(1u, NumberOfCompiledDexFiles());
579 ASSERT_TRUE(HasEachKindOfRecord());
580
581 // When validating, we create a new class loader, as
582 // the existing `class_loader_` may contain erroneous classes,
583 // that ClassLinker::FindClass won't return.
584
585 std::vector<uint8_t> buffer;
586 verifier_deps_->Encode(dex_files_, &buffer);
587 ASSERT_FALSE(buffer.empty());
588
589 // Check that dependencies are satisfied after decoding `buffer`.
590 ASSERT_TRUE(RunValidation([](VerifierDeps::DexFileDeps&) {}, buffer, &error_msg))
591 << error_msg;
592 }
593
TEST_F(VerifierDepsTest,CompilerDriver)594 TEST_F(VerifierDepsTest, CompilerDriver) {
595 SetupCompilerDriver();
596
597 // Test both multi-dex and single-dex configuration.
598 for (const char* multi : { "MultiDex", static_cast<const char*>(nullptr) }) {
599 // Test that the compiler driver behaves as expected when the dependencies
600 // verify and when they don't verify.
601 for (bool verify_failure : { false, true }) {
602 {
603 ScopedObjectAccess soa(Thread::Current());
604 LoadDexFile(soa, "VerifierDeps", multi);
605 }
606 VerifyWithCompilerDriver(/* verifier_deps= */ nullptr);
607
608 std::vector<uint8_t> buffer;
609 verifier_deps_->Encode(dex_files_, &buffer);
610
611 {
612 ScopedObjectAccess soa(Thread::Current());
613 LoadDexFile(soa, "VerifierDeps", multi);
614 }
615 VerifierDeps decoded_deps(dex_files_, /*output_only=*/ false);
616 bool parsed = decoded_deps.ParseStoredData(dex_files_, ArrayRef<const uint8_t>(buffer));
617 ASSERT_TRUE(parsed);
618 VerifyWithCompilerDriver(&decoded_deps);
619
620 if (verify_failure) {
621 ASSERT_FALSE(verifier_deps_ == nullptr);
622 ASSERT_FALSE(verifier_deps_->Equals(decoded_deps));
623 } else {
624 VerifyClassStatus(decoded_deps);
625 }
626 }
627 }
628 }
629
TEST_F(VerifierDepsTest,MultiDexVerification)630 TEST_F(VerifierDepsTest, MultiDexVerification) {
631 VerifyDexFile("VerifierDepsMulti");
632 ASSERT_EQ(NumberOfCompiledDexFiles(), 2u);
633
634 ASSERT_TRUE(HasUnverifiedClass("LMySoftVerificationFailure;", *dex_files_[1]));
635 ASSERT_TRUE(HasUnverifiedClass("LMySub1SoftVerificationFailure;", *dex_files_[0]));
636 ASSERT_TRUE(HasUnverifiedClass("LMySub2SoftVerificationFailure;", *dex_files_[0]));
637
638 std::vector<uint8_t> buffer;
639 verifier_deps_->Encode(dex_files_, &buffer);
640 ASSERT_FALSE(buffer.empty());
641 }
642
TEST_F(VerifierDepsTest,Assignable_Arrays)643 TEST_F(VerifierDepsTest, Assignable_Arrays) {
644 ASSERT_TRUE(TestAssignabilityRecording(/* dst= */ "[LIface;",
645 /* src= */ "[LMyClassExtendingInterface;"));
646 ASSERT_FALSE(HasAssignable(
647 "LIface;", "LMyClassExtendingInterface;"));
648 }
649
650 } // namespace verifier
651 } // namespace art
652