• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 ATTRIBUTE_UNUSED)49   void AddUncompilableMethod(MethodReference ref ATTRIBUTE_UNUSED) override {}
AddUncompilableClass(ClassReference ref ATTRIBUTE_UNUSED)50   void AddUncompilableClass(ClassReference ref 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:
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,std::string * error_msg)313   bool RunValidation(Fn fn, const std::vector<uint8_t>& buffer, std::string* error_msg) {
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.ValidateDependencies(soa.Self(),
333                                              new_class_loader,
334                                              second_dex_files,
335                                              error_msg);
336   }
337 
338   std::unique_ptr<verifier::VerifierDeps> verifier_deps_;
339   std::vector<const DexFile*> dex_files_;
340   const DexFile* primary_dex_file_;
341   jobject class_loader_;
342   ObjPtr<mirror::Class> klass_Main_;
343 };
344 
TEST_F(VerifierDepsTest,StringToId)345 TEST_F(VerifierDepsTest, StringToId) {
346   ScopedObjectAccess soa(Thread::Current());
347   LoadDexFile(soa);
348 
349   dex::StringIndex id_Main1 = verifier_deps_->GetIdFromString(*primary_dex_file_, "LMain;");
350   ASSERT_LT(id_Main1.index_, primary_dex_file_->NumStringIds());
351   ASSERT_EQ("LMain;", verifier_deps_->GetStringFromId(*primary_dex_file_, id_Main1));
352 
353   dex::StringIndex id_Main2 = verifier_deps_->GetIdFromString(*primary_dex_file_, "LMain;");
354   ASSERT_LT(id_Main2.index_, primary_dex_file_->NumStringIds());
355   ASSERT_EQ("LMain;", verifier_deps_->GetStringFromId(*primary_dex_file_, id_Main2));
356 
357   dex::StringIndex id_Lorem1 = verifier_deps_->GetIdFromString(*primary_dex_file_, "Lorem ipsum");
358   ASSERT_GE(id_Lorem1.index_, primary_dex_file_->NumStringIds());
359   ASSERT_EQ("Lorem ipsum", verifier_deps_->GetStringFromId(*primary_dex_file_, id_Lorem1));
360 
361   dex::StringIndex id_Lorem2 = verifier_deps_->GetIdFromString(*primary_dex_file_, "Lorem ipsum");
362   ASSERT_GE(id_Lorem2.index_, primary_dex_file_->NumStringIds());
363   ASSERT_EQ("Lorem ipsum", verifier_deps_->GetStringFromId(*primary_dex_file_, id_Lorem2));
364 
365   ASSERT_EQ(id_Main1, id_Main2);
366   ASSERT_EQ(id_Lorem1, id_Lorem2);
367   ASSERT_NE(id_Main1, id_Lorem1);
368 }
369 
TEST_F(VerifierDepsTest,Assignable_BothInBoot)370 TEST_F(VerifierDepsTest, Assignable_BothInBoot) {
371   ASSERT_TRUE(TestAssignabilityRecording(/* dst= */ "Ljava/util/TimeZone;",
372                                          /* src= */ "Ljava/util/SimpleTimeZone;"));
373   ASSERT_TRUE(HasAssignable("Ljava/util/TimeZone;", "Ljava/util/SimpleTimeZone;"));
374 }
375 
TEST_F(VerifierDepsTest,Assignable_BothArrays_Resolved)376 TEST_F(VerifierDepsTest, Assignable_BothArrays_Resolved) {
377   ASSERT_TRUE(TestAssignabilityRecording(/* dst= */ "[[Ljava/util/TimeZone;",
378                                          /* src= */ "[[Ljava/util/SimpleTimeZone;"));
379   // If the component types of both arrays are resolved, we optimize the list of
380   // dependencies by recording a dependency on the component types.
381   ASSERT_FALSE(HasAssignable("[[Ljava/util/TimeZone;", "[[Ljava/util/SimpleTimeZone;"));
382   ASSERT_FALSE(HasAssignable("[Ljava/util/TimeZone;", "[Ljava/util/SimpleTimeZone;"));
383   ASSERT_TRUE(HasAssignable("Ljava/util/TimeZone;", "Ljava/util/SimpleTimeZone;"));
384 }
385 
TEST_F(VerifierDepsTest,ReturnType_Reference)386 TEST_F(VerifierDepsTest, ReturnType_Reference) {
387   ASSERT_TRUE(VerifyMethod("ReturnType_Reference"));
388   ASSERT_TRUE(HasAssignable("Ljava/lang/Throwable;", "Ljava/lang/IllegalStateException;"));
389 }
390 
TEST_F(VerifierDepsTest,InvokeArgumentType)391 TEST_F(VerifierDepsTest, InvokeArgumentType) {
392   ASSERT_TRUE(VerifyMethod("InvokeArgumentType"));
393   ASSERT_TRUE(HasAssignable("Ljava/util/TimeZone;", "Ljava/util/SimpleTimeZone;"));
394 }
395 
TEST_F(VerifierDepsTest,MergeTypes_RegisterLines)396 TEST_F(VerifierDepsTest, MergeTypes_RegisterLines) {
397   ASSERT_TRUE(VerifyMethod("MergeTypes_RegisterLines"));
398   ASSERT_TRUE(HasAssignable("Ljava/lang/Exception;", "LMySocketTimeoutException;"));
399   ASSERT_TRUE(HasAssignable(
400       "Ljava/lang/Exception;", "Ljava/util/concurrent/TimeoutException;"));
401 }
402 
TEST_F(VerifierDepsTest,MergeTypes_IfInstanceOf)403 TEST_F(VerifierDepsTest, MergeTypes_IfInstanceOf) {
404   ASSERT_TRUE(VerifyMethod("MergeTypes_IfInstanceOf"));
405   ASSERT_TRUE(HasAssignable("Ljava/lang/Exception;", "Ljava/net/SocketTimeoutException;"));
406   ASSERT_TRUE(HasAssignable(
407       "Ljava/lang/Exception;", "Ljava/util/concurrent/TimeoutException;"));
408 }
409 
TEST_F(VerifierDepsTest,MergeTypes_Unresolved)410 TEST_F(VerifierDepsTest, MergeTypes_Unresolved) {
411   ASSERT_TRUE(VerifyMethod("MergeTypes_Unresolved"));
412   ASSERT_TRUE(HasAssignable("Ljava/lang/Exception;", "Ljava/net/SocketTimeoutException;"));
413   ASSERT_TRUE(HasAssignable(
414       "Ljava/lang/Exception;", "Ljava/util/concurrent/TimeoutException;"));
415 }
416 
TEST_F(VerifierDepsTest,Throw)417 TEST_F(VerifierDepsTest, Throw) {
418   ASSERT_TRUE(VerifyMethod("Throw"));
419   ASSERT_TRUE(HasAssignable("Ljava/lang/Throwable;", "Ljava/lang/IllegalStateException;"));
420 }
421 
TEST_F(VerifierDepsTest,MoveException_Resolved)422 TEST_F(VerifierDepsTest, MoveException_Resolved) {
423   ASSERT_TRUE(VerifyMethod("MoveException_Resolved"));
424 
425   // Testing that all exception types are assignable to Throwable.
426   ASSERT_TRUE(HasAssignable("Ljava/lang/Throwable;", "Ljava/io/InterruptedIOException;"));
427   ASSERT_TRUE(HasAssignable("Ljava/lang/Throwable;", "Ljava/net/SocketTimeoutException;"));
428   ASSERT_TRUE(HasAssignable("Ljava/lang/Throwable;", "Ljava/util/zip/ZipException;"));
429 
430   // Testing that the merge type is assignable to Throwable.
431   ASSERT_TRUE(HasAssignable("Ljava/lang/Throwable;", "Ljava/io/IOException;"));
432 
433   // Merging of exception types.
434   ASSERT_TRUE(HasAssignable("Ljava/io/IOException;", "Ljava/io/InterruptedIOException;"));
435   ASSERT_TRUE(HasAssignable("Ljava/io/IOException;", "Ljava/util/zip/ZipException;"));
436   ASSERT_TRUE(HasAssignable(
437       "Ljava/io/InterruptedIOException;", "Ljava/net/SocketTimeoutException;"));
438 }
439 
TEST_F(VerifierDepsTest,InstanceField_Resolved_DeclaredInReferenced)440 TEST_F(VerifierDepsTest, InstanceField_Resolved_DeclaredInReferenced) {
441   ASSERT_TRUE(VerifyMethod("InstanceField_Resolved_DeclaredInReferenced"));
442   ASSERT_TRUE(HasAssignable(
443       "Ljava/io/InterruptedIOException;", "LMySocketTimeoutException;"));
444 }
445 
TEST_F(VerifierDepsTest,InstanceField_Resolved_DeclaredInSuperclass1)446 TEST_F(VerifierDepsTest, InstanceField_Resolved_DeclaredInSuperclass1) {
447   ASSERT_TRUE(VerifyMethod("InstanceField_Resolved_DeclaredInSuperclass1"));
448   ASSERT_TRUE(HasAssignable(
449       "Ljava/io/InterruptedIOException;", "LMySocketTimeoutException;"));
450 }
451 
TEST_F(VerifierDepsTest,InstanceField_Resolved_DeclaredInSuperclass2)452 TEST_F(VerifierDepsTest, InstanceField_Resolved_DeclaredInSuperclass2) {
453   ASSERT_TRUE(VerifyMethod("InstanceField_Resolved_DeclaredInSuperclass2"));
454   ASSERT_TRUE(HasAssignable(
455       "Ljava/io/InterruptedIOException;", "LMySocketTimeoutException;"));
456 }
457 
TEST_F(VerifierDepsTest,InvokeVirtual_Resolved_DeclaredInReferenced)458 TEST_F(VerifierDepsTest, InvokeVirtual_Resolved_DeclaredInReferenced) {
459   ASSERT_TRUE(VerifyMethod("InvokeVirtual_Resolved_DeclaredInReferenced"));
460   // Type dependency on `this` argument.
461   ASSERT_TRUE(HasAssignable("Ljava/lang/Throwable;", "LMySocketTimeoutException;"));
462 }
463 
TEST_F(VerifierDepsTest,InvokeVirtual_Resolved_DeclaredInSuperclass1)464 TEST_F(VerifierDepsTest, InvokeVirtual_Resolved_DeclaredInSuperclass1) {
465   ASSERT_TRUE(VerifyMethod("InvokeVirtual_Resolved_DeclaredInSuperclass1"));
466   // Type dependency on `this` argument.
467   ASSERT_TRUE(HasAssignable("Ljava/lang/Throwable;", "LMySocketTimeoutException;"));
468 }
469 
TEST_F(VerifierDepsTest,InvokeSuper_ThisAssignable)470 TEST_F(VerifierDepsTest, InvokeSuper_ThisAssignable) {
471   ASSERT_TRUE(VerifyMethod("InvokeSuper_ThisAssignable"));
472   ASSERT_TRUE(HasAssignable("Ljava/lang/Runnable;", "LMain;"));
473 }
474 
TEST_F(VerifierDepsTest,EncodeDecode)475 TEST_F(VerifierDepsTest, EncodeDecode) {
476   VerifyDexFile();
477 
478   ASSERT_EQ(1u, NumberOfCompiledDexFiles());
479   ASSERT_TRUE(HasEachKindOfRecord());
480 
481   std::vector<uint8_t> buffer;
482   verifier_deps_->Encode(dex_files_, &buffer);
483   ASSERT_FALSE(buffer.empty());
484 
485   VerifierDeps decoded_deps(dex_files_, /*output_only=*/ false);
486   bool parsed = decoded_deps.ParseStoredData(dex_files_, ArrayRef<const uint8_t>(buffer));
487   ASSERT_TRUE(parsed);
488   ASSERT_TRUE(verifier_deps_->Equals(decoded_deps));
489 }
490 
TEST_F(VerifierDepsTest,EncodeDecodeMulti)491 TEST_F(VerifierDepsTest, EncodeDecodeMulti) {
492   VerifyDexFile("MultiDex");
493 
494   ASSERT_GT(NumberOfCompiledDexFiles(), 1u);
495   std::vector<uint8_t> buffer;
496   verifier_deps_->Encode(dex_files_, &buffer);
497   ASSERT_FALSE(buffer.empty());
498 
499   // Create new DexFile, to mess with std::map order: the verifier deps used
500   // to iterate over the map, which doesn't guarantee insertion order. We fixed
501   // this by passing the expected order when encoding/decoding.
502   std::vector<std::unique_ptr<const DexFile>> first_dex_files = OpenTestDexFiles("VerifierDeps");
503   std::vector<std::unique_ptr<const DexFile>> second_dex_files = OpenTestDexFiles("MultiDex");
504   std::vector<const DexFile*> dex_files;
505   dex_files.reserve(first_dex_files.size() + second_dex_files.size());
506   for (auto& dex_file : first_dex_files) {
507     dex_files.push_back(dex_file.get());
508   }
509   for (auto& dex_file : second_dex_files) {
510     dex_files.push_back(dex_file.get());
511   }
512 
513   // Dump the new verifier deps to ensure it can properly read the data.
514   VerifierDeps decoded_deps(dex_files, /*output_only=*/ false);
515   bool parsed = decoded_deps.ParseStoredData(dex_files, ArrayRef<const uint8_t>(buffer));
516   ASSERT_TRUE(parsed);
517   std::ostringstream stream;
518   VariableIndentationOutputStream os(&stream);
519   decoded_deps.Dump(&os);
520 }
521 
TEST_F(VerifierDepsTest,UnverifiedClasses)522 TEST_F(VerifierDepsTest, UnverifiedClasses) {
523   VerifyDexFile();
524   ASSERT_FALSE(HasUnverifiedClass("LMyThread;"));
525   // Test that a class with a soft failure is recorded.
526   ASSERT_TRUE(HasUnverifiedClass("LMain;"));
527   // Test that a class with hard failure is recorded.
528   ASSERT_TRUE(HasUnverifiedClass("LMyVerificationFailure;"));
529   // Test that a class with unresolved super and hard failure is recorded.
530   ASSERT_TRUE(HasUnverifiedClass("LMyClassWithNoSuperButFailures;"));
531   // Test that a class with unresolved super can be verified.
532   ASSERT_TRUE(HasVerifiedClass("LMyClassWithNoSuper;"));
533 }
534 
TEST_F(VerifierDepsTest,UnverifiedOrder)535 TEST_F(VerifierDepsTest, UnverifiedOrder) {
536   ScopedObjectAccess soa(Thread::Current());
537   jobject loader = LoadDex("VerifierDeps");
538   std::vector<const DexFile*> dex_files = GetDexFiles(loader);
539   ASSERT_GT(dex_files.size(), 0u);
540   const DexFile* dex_file = dex_files[0];
541   VerifierDeps deps1(dex_files);
542   deps1.MaybeRecordVerificationStatus(&deps1,
543                                       *dex_file,
544                                       dex_file->GetClassDef(0u),
545                                       verifier::FailureKind::kHardFailure);
546   deps1.MaybeRecordVerificationStatus(&deps1,
547                                       *dex_file,
548                                       dex_file->GetClassDef(1u),
549                                       verifier::FailureKind::kHardFailure);
550   VerifierDeps deps2(dex_files);
551   deps2.MaybeRecordVerificationStatus(&deps2,
552                                       *dex_file,
553                                       dex_file->GetClassDef(1u),
554                                       verifier::FailureKind::kHardFailure);
555   deps2.MaybeRecordVerificationStatus(&deps2,
556                                       *dex_file,
557                                       dex_file->GetClassDef(0u),
558                                       verifier::FailureKind::kHardFailure);
559   std::vector<uint8_t> buffer1;
560   deps1.Encode(dex_files, &buffer1);
561   std::vector<uint8_t> buffer2;
562   deps2.Encode(dex_files, &buffer2);
563   EXPECT_EQ(buffer1, buffer2);
564 }
565 
TEST_F(VerifierDepsTest,VerifyDeps)566 TEST_F(VerifierDepsTest, VerifyDeps) {
567   std::string error_msg;
568 
569   VerifyDexFile();
570   ASSERT_EQ(1u, NumberOfCompiledDexFiles());
571   ASSERT_TRUE(HasEachKindOfRecord());
572 
573   // When validating, we create a new class loader, as
574   // the existing `class_loader_` may contain erroneous classes,
575   // that ClassLinker::FindClass won't return.
576 
577   std::vector<uint8_t> buffer;
578   verifier_deps_->Encode(dex_files_, &buffer);
579   ASSERT_FALSE(buffer.empty());
580 
581   // Check that dependencies are satisfied after decoding `buffer`.
582   ASSERT_TRUE(RunValidation([](VerifierDeps::DexFileDeps&) {}, buffer, &error_msg))
583       << error_msg;
584 }
585 
TEST_F(VerifierDepsTest,CompilerDriver)586 TEST_F(VerifierDepsTest, CompilerDriver) {
587   SetupCompilerDriver();
588 
589   // Test both multi-dex and single-dex configuration.
590   for (const char* multi : { "MultiDex", static_cast<const char*>(nullptr) }) {
591     // Test that the compiler driver behaves as expected when the dependencies
592     // verify and when they don't verify.
593     for (bool verify_failure : { false, true }) {
594       {
595         ScopedObjectAccess soa(Thread::Current());
596         LoadDexFile(soa, "VerifierDeps", multi);
597       }
598       VerifyWithCompilerDriver(/* verifier_deps= */ nullptr);
599 
600       std::vector<uint8_t> buffer;
601       verifier_deps_->Encode(dex_files_, &buffer);
602 
603       {
604         ScopedObjectAccess soa(Thread::Current());
605         LoadDexFile(soa, "VerifierDeps", multi);
606       }
607       VerifierDeps decoded_deps(dex_files_, /*output_only=*/ false);
608       bool parsed = decoded_deps.ParseStoredData(dex_files_, ArrayRef<const uint8_t>(buffer));
609       ASSERT_TRUE(parsed);
610       VerifyWithCompilerDriver(&decoded_deps);
611 
612       if (verify_failure) {
613         ASSERT_FALSE(verifier_deps_ == nullptr);
614         ASSERT_FALSE(verifier_deps_->Equals(decoded_deps));
615       } else {
616         VerifyClassStatus(decoded_deps);
617       }
618     }
619   }
620 }
621 
TEST_F(VerifierDepsTest,MultiDexVerification)622 TEST_F(VerifierDepsTest, MultiDexVerification) {
623   VerifyDexFile("VerifierDepsMulti");
624   ASSERT_EQ(NumberOfCompiledDexFiles(), 2u);
625 
626   ASSERT_TRUE(HasUnverifiedClass("LMySoftVerificationFailure;", *dex_files_[1]));
627   ASSERT_TRUE(HasUnverifiedClass("LMySub1SoftVerificationFailure;", *dex_files_[0]));
628   ASSERT_TRUE(HasUnverifiedClass("LMySub2SoftVerificationFailure;", *dex_files_[0]));
629 
630   std::vector<uint8_t> buffer;
631   verifier_deps_->Encode(dex_files_, &buffer);
632   ASSERT_FALSE(buffer.empty());
633 }
634 
635 }  // namespace verifier
636 }  // namespace art
637