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