Home | History | Annotate | Download | only in compiler
      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 "class_linker.h"
     22 #include "common_compiler_test.h"
     23 #include "compiler_callbacks.h"
     24 #include "dex/verification_results.h"
     25 #include "dex/verified_method.h"
     26 #include "dex_file.h"
     27 #include "dex_file_types.h"
     28 #include "driver/compiler_options.h"
     29 #include "driver/compiler_driver.h"
     30 #include "handle_scope-inl.h"
     31 #include "indenter.h"
     32 #include "mirror/class_loader.h"
     33 #include "runtime.h"
     34 #include "scoped_thread_state_change-inl.h"
     35 #include "thread.h"
     36 #include "utils/atomic_method_ref_map-inl.h"
     37 #include "verifier/method_verifier-inl.h"
     38 
     39 namespace art {
     40 namespace verifier {
     41 
     42 class 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 
     59 class VerifierDepsTest : public CommonCompilerTest {
     60  public:
     61   void SetUpRuntimeOptions(RuntimeOptions* options) {
     62     CommonCompilerTest::SetUpRuntimeOptions(options);
     63     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(
     70         hs.NewHandle(soa->Decode<mirror::ClassLoader>(class_loader_)));
     71     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 
     81   void SetupCompilerDriver() {
     82     compiler_options_->boot_image_ = false;
     83     compiler_driver_->InitializeThreadPools();
     84   }
     85 
     86   void VerifyWithCompilerDriver(verifier::VerifierDeps* deps) {
     87     TimingLogger timings("Verify", false, false);
     88     // The compiler driver handles the verifier deps in the callbacks, so
     89     // remove what this class did for unit testing.
     90     verifier_deps_.reset(nullptr);
     91     callbacks_->SetVerifierDeps(deps);
     92     compiler_driver_->Verify(class_loader_, dex_files_, &timings);
     93     // The compiler driver may have updated the VerifierDeps in the callback object.
     94     if (callbacks_->GetVerifierDeps() != deps) {
     95       verifier_deps_.reset(callbacks_->GetVerifierDeps());
     96     }
     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     AtomicMethodRefMap<const VerifiedMethod*>* map =
    101         &compiler_driver_->GetVerificationResults()->atomic_verified_methods_;
    102     map->Visit([](const MethodReference& ref ATTRIBUTE_UNUSED, const VerifiedMethod* method) {
    103       delete method;
    104     });
    105     map->ClearEntries();
    106   }
    107 
    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 
    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       compiler_driver_->GetVerificationResults()->AddDexFile(dex_file);
    130     }
    131   }
    132 
    133   void LoadDexFile(ScopedObjectAccess* soa) REQUIRES_SHARED(Locks::mutator_lock_) {
    134     LoadDexFile(soa, "VerifierDeps");
    135     CHECK_EQ(dex_files_.size(), 1u);
    136     klass_Main_ = FindClassByName("LMain;", soa);
    137     CHECK(klass_Main_ != nullptr);
    138   }
    139 
    140   bool VerifyMethod(const std::string& method_name) {
    141     ScopedObjectAccess soa(Thread::Current());
    142     LoadDexFile(&soa);
    143 
    144     StackHandleScope<2> hs(soa.Self());
    145     Handle<mirror::ClassLoader> class_loader_handle(
    146         hs.NewHandle(soa.Decode<mirror::ClassLoader>(class_loader_)));
    147     Handle<mirror::DexCache> dex_cache_handle(hs.NewHandle(klass_Main_->GetDexCache()));
    148 
    149     const DexFile::ClassDef* class_def = klass_Main_->GetClassDef();
    150     const uint8_t* class_data = primary_dex_file_->GetClassData(*class_def);
    151     CHECK(class_data != nullptr);
    152 
    153     ClassDataItemIterator it(*primary_dex_file_, class_data);
    154     while (it.HasNextStaticField() || it.HasNextInstanceField()) {
    155       it.Next();
    156     }
    157 
    158     ArtMethod* method = nullptr;
    159     while (it.HasNextDirectMethod()) {
    160       ArtMethod* resolved_method = class_linker_->ResolveMethod<ClassLinker::kNoICCECheckForCache>(
    161           *primary_dex_file_,
    162           it.GetMemberIndex(),
    163           dex_cache_handle,
    164           class_loader_handle,
    165           nullptr,
    166           it.GetMethodInvokeType(*class_def));
    167       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 
    176     Thread::Current()->SetVerifierDeps(callbacks_->GetVerifierDeps());
    177     MethodVerifier verifier(Thread::Current(),
    178                             primary_dex_file_,
    179                             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();
    192     Thread::Current()->SetVerifierDeps(nullptr);
    193     return !verifier.HasFailures();
    194   }
    195 
    196   void VerifyDexFile(const char* multidex = nullptr) {
    197     {
    198       ScopedObjectAccess soa(Thread::Current());
    199       LoadDexFile(&soa, "VerifierDeps", multidex);
    200     }
    201     SetupCompilerDriver();
    202     VerifyWithCompilerDriver(/* verifier_deps */ nullptr);
    203   }
    204 
    205   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);
    211     mirror::Class* klass_dst = FindClassByName(dst, &soa);
    212     DCHECK(klass_dst != nullptr) << dst;
    213     mirror::Class* klass_src = FindClassByName(src, &soa);
    214     DCHECK(klass_src != nullptr) << src;
    215     verifier_deps_->AddAssignability(*primary_dex_file_,
    216                                      klass_dst,
    217                                      klass_src,
    218                                      is_strict,
    219                                      is_assignable);
    220     return true;
    221   }
    222 
    223   // 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_) {
    232       const std::vector<dex::TypeIndex>& unverified_classes = deps.GetUnverifiedClasses(*dex_file);
    233       std::set<dex::TypeIndex> set(unverified_classes.begin(), unverified_classes.end());
    234       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));
    238         if (cls == nullptr) {
    239           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 
    250   bool HasUnverifiedClass(const std::string& cls) {
    251     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());
    256     DCHECK(type_id != nullptr);
    257     dex::TypeIndex index = dex_file.GetIndexForTypeId(*type_id);
    258     for (const auto& dex_dep : verifier_deps_->dex_deps_) {
    259       for (dex::TypeIndex entry : dex_dep.second->unverified_classes_) {
    260         if (index == entry) {
    261           return true;
    262         }
    263       }
    264     }
    265     return false;
    266   }
    267 
    268   // 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) {
    273     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 = "") {
    294     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 = "") {
    328     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.
    374   bool HasMethod(const std::string& expected_kind,
    375                  const std::string& expected_klass,
    376                  const std::string& expected_name,
    377                  const std::string& expected_signature,
    378                  bool expected_resolved,
    379                  const std::string& expected_access_flags = "",
    380                  const std::string& expected_decl_klass = "") {
    381     for (auto& dex_dep : verifier_deps_->dex_deps_) {
    382       auto& storage = (expected_kind == "direct") ? dex_dep.second->direct_methods_
    383                           : (expected_kind == "virtual") ? dex_dep.second->virtual_methods_
    384                               : dex_dep.second->interface_methods_;
    385       for (auto& entry : storage) {
    386         if (expected_resolved != entry.IsResolved()) {
    387           continue;
    388         }
    389 
    390         const DexFile::MethodId& method_id = dex_dep.first->GetMethodId(entry.GetDexMethodIndex());
    391 
    392         std::string actual_klass = dex_dep.first->StringByTypeIdx(method_id.class_idx_);
    393         if (expected_klass != actual_klass) {
    394           continue;
    395         }
    396 
    397         std::string actual_name = dex_dep.first->StringDataByIdx(method_id.name_idx_);
    398         if (expected_name != actual_name) {
    399           continue;
    400         }
    401 
    402         std::string actual_signature = dex_dep.first->GetMethodSignature(method_id).ToString();
    403         if (expected_signature != actual_signature) {
    404           continue;
    405         }
    406 
    407         if (expected_resolved) {
    408           // Test access flags. Note that PrettyJavaAccessFlags always appends
    409           // a space after the modifiers. Add it to the expected access flags.
    410           std::string actual_access_flags = PrettyJavaAccessFlags(entry.GetAccessFlags());
    411           if (expected_access_flags + " " != actual_access_flags) {
    412             continue;
    413           }
    414 
    415           std::string actual_decl_klass = verifier_deps_->GetStringFromId(
    416               *dex_dep.first, entry.GetDeclaringClassIndex());
    417           if (expected_decl_klass != actual_decl_klass) {
    418             continue;
    419           }
    420         }
    421 
    422         return true;
    423       }
    424     }
    425     return false;
    426   }
    427 
    428   size_t NumberOfCompiledDexFiles() {
    429     return verifier_deps_->dex_deps_.size();
    430   }
    431 
    432   size_t HasEachKindOfRecord() {
    433     bool has_strings = false;
    434     bool has_assignability = false;
    435     bool has_classes = false;
    436     bool has_fields = false;
    437     bool has_methods = false;
    438     bool has_unverified_classes = false;
    439 
    440     for (auto& entry : verifier_deps_->dex_deps_) {
    441       has_strings |= !entry.second->strings_.empty();
    442       has_assignability |= !entry.second->assignable_types_.empty();
    443       has_assignability |= !entry.second->unassignable_types_.empty();
    444       has_classes |= !entry.second->classes_.empty();
    445       has_fields |= !entry.second->fields_.empty();
    446       has_methods |= !entry.second->direct_methods_.empty();
    447       has_methods |= !entry.second->virtual_methods_.empty();
    448       has_methods |= !entry.second->interface_methods_.empty();
    449       has_unverified_classes |= !entry.second->unverified_classes_.empty();
    450     }
    451 
    452     return has_strings &&
    453            has_assignability &&
    454            has_classes &&
    455            has_fields &&
    456            has_methods &&
    457            has_unverified_classes;
    458   }
    459 
    460   static std::set<VerifierDeps::MethodResolution>* GetMethods(
    461       VerifierDeps::DexFileDeps* deps, MethodResolutionKind resolution_kind) {
    462     if (resolution_kind == kDirectMethodResolution) {
    463       return &deps->direct_methods_;
    464     } else if (resolution_kind == kVirtualMethodResolution) {
    465       return &deps->virtual_methods_;
    466     } else {
    467       DCHECK_EQ(resolution_kind, kInterfaceMethodResolution);
    468       return &deps->interface_methods_;
    469     }
    470   }
    471 
    472   std::unique_ptr<verifier::VerifierDeps> verifier_deps_;
    473   std::vector<const DexFile*> dex_files_;
    474   const DexFile* primary_dex_file_;
    475   jobject class_loader_;
    476   mirror::Class* klass_Main_;
    477 };
    478 
    479 TEST_F(VerifierDepsTest, StringToId) {
    480   ScopedObjectAccess soa(Thread::Current());
    481   LoadDexFile(&soa);
    482 
    483   dex::StringIndex id_Main1 = verifier_deps_->GetIdFromString(*primary_dex_file_, "LMain;");
    484   ASSERT_LT(id_Main1.index_, primary_dex_file_->NumStringIds());
    485   ASSERT_EQ("LMain;", verifier_deps_->GetStringFromId(*primary_dex_file_, id_Main1));
    486 
    487   dex::StringIndex id_Main2 = verifier_deps_->GetIdFromString(*primary_dex_file_, "LMain;");
    488   ASSERT_LT(id_Main2.index_, primary_dex_file_->NumStringIds());
    489   ASSERT_EQ("LMain;", verifier_deps_->GetStringFromId(*primary_dex_file_, id_Main2));
    490 
    491   dex::StringIndex id_Lorem1 = verifier_deps_->GetIdFromString(*primary_dex_file_, "Lorem ipsum");
    492   ASSERT_GE(id_Lorem1.index_, primary_dex_file_->NumStringIds());
    493   ASSERT_EQ("Lorem ipsum", verifier_deps_->GetStringFromId(*primary_dex_file_, id_Lorem1));
    494 
    495   dex::StringIndex id_Lorem2 = verifier_deps_->GetIdFromString(*primary_dex_file_, "Lorem ipsum");
    496   ASSERT_GE(id_Lorem2.index_, primary_dex_file_->NumStringIds());
    497   ASSERT_EQ("Lorem ipsum", verifier_deps_->GetStringFromId(*primary_dex_file_, id_Lorem2));
    498 
    499   ASSERT_EQ(id_Main1, id_Main2);
    500   ASSERT_EQ(id_Lorem1, id_Lorem2);
    501   ASSERT_NE(id_Main1, id_Lorem1);
    502 }
    503 
    504 TEST_F(VerifierDepsTest, Assignable_BothInBoot) {
    505   ASSERT_TRUE(TestAssignabilityRecording(/* dst */ "Ljava/util/TimeZone;",
    506                                          /* src */ "Ljava/util/SimpleTimeZone;",
    507                                          /* is_strict */ true,
    508                                          /* is_assignable */ true));
    509   ASSERT_TRUE(HasAssignable("Ljava/util/TimeZone;", "Ljava/util/SimpleTimeZone;", true));
    510 }
    511 
    512 TEST_F(VerifierDepsTest, Assignable_DestinationInBoot1) {
    513   ASSERT_TRUE(TestAssignabilityRecording(/* dst */ "Ljava/net/Socket;",
    514                                          /* src */ "LMySSLSocket;",
    515                                          /* is_strict */ true,
    516                                          /* is_assignable */ true));
    517   ASSERT_TRUE(HasAssignable("Ljava/net/Socket;", "Ljavax/net/ssl/SSLSocket;", true));
    518 }
    519 
    520 TEST_F(VerifierDepsTest, Assignable_DestinationInBoot2) {
    521   ASSERT_TRUE(TestAssignabilityRecording(/* dst */ "Ljava/util/TimeZone;",
    522                                          /* src */ "LMySimpleTimeZone;",
    523                                          /* is_strict */ true,
    524                                          /* is_assignable */ true));
    525   ASSERT_TRUE(HasAssignable("Ljava/util/TimeZone;", "Ljava/util/SimpleTimeZone;", true));
    526 }
    527 
    528 TEST_F(VerifierDepsTest, Assignable_DestinationInBoot3) {
    529   ASSERT_TRUE(TestAssignabilityRecording(/* dst */ "Ljava/util/Collection;",
    530                                          /* src */ "LMyThreadSet;",
    531                                          /* is_strict */ true,
    532                                          /* is_assignable */ true));
    533   ASSERT_TRUE(HasAssignable("Ljava/util/Collection;", "Ljava/util/Set;", true));
    534 }
    535 
    536 TEST_F(VerifierDepsTest, Assignable_BothArrays_Resolved) {
    537   ASSERT_TRUE(TestAssignabilityRecording(/* dst */ "[[Ljava/util/TimeZone;",
    538                                          /* src */ "[[Ljava/util/SimpleTimeZone;",
    539                                          /* is_strict */ true,
    540                                          /* is_assignable */ true));
    541   // If the component types of both arrays are resolved, we optimize the list of
    542   // dependencies by recording a dependency on the component types.
    543   ASSERT_FALSE(HasAssignable("[[Ljava/util/TimeZone;", "[[Ljava/util/SimpleTimeZone;", true));
    544   ASSERT_FALSE(HasAssignable("[Ljava/util/TimeZone;", "[Ljava/util/SimpleTimeZone;", true));
    545   ASSERT_TRUE(HasAssignable("Ljava/util/TimeZone;", "Ljava/util/SimpleTimeZone;", true));
    546 }
    547 
    548 TEST_F(VerifierDepsTest, NotAssignable_BothInBoot) {
    549   ASSERT_TRUE(TestAssignabilityRecording(/* dst */ "Ljava/lang/Exception;",
    550                                          /* src */ "Ljava/util/SimpleTimeZone;",
    551                                          /* is_strict */ true,
    552                                          /* is_assignable */ false));
    553   ASSERT_TRUE(HasAssignable("Ljava/lang/Exception;", "Ljava/util/SimpleTimeZone;", false));
    554 }
    555 
    556 TEST_F(VerifierDepsTest, NotAssignable_DestinationInBoot1) {
    557   ASSERT_TRUE(TestAssignabilityRecording(/* dst */ "Ljava/lang/Exception;",
    558                                          /* src */ "LMySSLSocket;",
    559                                          /* is_strict */ true,
    560                                          /* is_assignable */ false));
    561   ASSERT_TRUE(HasAssignable("Ljava/lang/Exception;", "Ljavax/net/ssl/SSLSocket;", false));
    562 }
    563 
    564 TEST_F(VerifierDepsTest, NotAssignable_DestinationInBoot2) {
    565   ASSERT_TRUE(TestAssignabilityRecording(/* dst */ "Ljava/lang/Exception;",
    566                                          /* src */ "LMySimpleTimeZone;",
    567                                          /* is_strict */ true,
    568                                          /* is_assignable */ false));
    569   ASSERT_TRUE(HasAssignable("Ljava/lang/Exception;", "Ljava/util/SimpleTimeZone;", false));
    570 }
    571 
    572 TEST_F(VerifierDepsTest, NotAssignable_BothArrays) {
    573   ASSERT_TRUE(TestAssignabilityRecording(/* dst */ "[Ljava/lang/Exception;",
    574                                          /* src */ "[Ljava/util/SimpleTimeZone;",
    575                                          /* is_strict */ true,
    576                                          /* is_assignable */ false));
    577   ASSERT_TRUE(HasAssignable("Ljava/lang/Exception;", "Ljava/util/SimpleTimeZone;", false));
    578 }
    579 
    580 TEST_F(VerifierDepsTest, ArgumentType_ResolvedClass) {
    581   ASSERT_TRUE(VerifyMethod("ArgumentType_ResolvedClass"));
    582   ASSERT_TRUE(HasClass("Ljava/lang/Thread;", true, "public"));
    583 }
    584 
    585 TEST_F(VerifierDepsTest, ArgumentType_UnresolvedClass) {
    586   ASSERT_TRUE(VerifyMethod("ArgumentType_UnresolvedClass"));
    587   ASSERT_TRUE(HasClass("LUnresolvedClass;", false));
    588 }
    589 
    590 TEST_F(VerifierDepsTest, ArgumentType_UnresolvedSuper) {
    591   ASSERT_TRUE(VerifyMethod("ArgumentType_UnresolvedSuper"));
    592   ASSERT_TRUE(HasClass("LMySetWithUnresolvedSuper;", false));
    593 }
    594 
    595 TEST_F(VerifierDepsTest, ReturnType_Reference) {
    596   ASSERT_TRUE(VerifyMethod("ReturnType_Reference"));
    597   ASSERT_TRUE(HasAssignable("Ljava/lang/Throwable;", "Ljava/lang/IllegalStateException;", true));
    598 }
    599 
    600 TEST_F(VerifierDepsTest, ReturnType_Array) {
    601   ASSERT_FALSE(VerifyMethod("ReturnType_Array"));
    602   ASSERT_TRUE(HasAssignable("Ljava/lang/Integer;", "Ljava/lang/IllegalStateException;", false));
    603 }
    604 
    605 TEST_F(VerifierDepsTest, InvokeArgumentType) {
    606   ASSERT_TRUE(VerifyMethod("InvokeArgumentType"));
    607   ASSERT_TRUE(HasClass("Ljava/text/SimpleDateFormat;", true, "public"));
    608   ASSERT_TRUE(HasClass("Ljava/util/SimpleTimeZone;", true, "public"));
    609   ASSERT_TRUE(HasMethod("virtual",
    610                         "Ljava/text/SimpleDateFormat;",
    611                         "setTimeZone",
    612                         "(Ljava/util/TimeZone;)V",
    613                         true,
    614                         "public",
    615                         "Ljava/text/DateFormat;"));
    616   ASSERT_TRUE(HasAssignable("Ljava/util/TimeZone;", "Ljava/util/SimpleTimeZone;", true));
    617 }
    618 
    619 TEST_F(VerifierDepsTest, MergeTypes_RegisterLines) {
    620   ASSERT_TRUE(VerifyMethod("MergeTypes_RegisterLines"));
    621   ASSERT_TRUE(HasAssignable("Ljava/lang/Exception;", "Ljava/net/SocketTimeoutException;", true));
    622   ASSERT_TRUE(HasAssignable(
    623       "Ljava/lang/Exception;", "Ljava/util/concurrent/TimeoutException;", true));
    624 }
    625 
    626 TEST_F(VerifierDepsTest, MergeTypes_IfInstanceOf) {
    627   ASSERT_TRUE(VerifyMethod("MergeTypes_IfInstanceOf"));
    628   ASSERT_TRUE(HasAssignable("Ljava/lang/Exception;", "Ljava/net/SocketTimeoutException;", true));
    629   ASSERT_TRUE(HasAssignable(
    630       "Ljava/lang/Exception;", "Ljava/util/concurrent/TimeoutException;", true));
    631   ASSERT_TRUE(HasAssignable("Ljava/net/SocketTimeoutException;", "Ljava/lang/Exception;", false));
    632 }
    633 
    634 TEST_F(VerifierDepsTest, MergeTypes_Unresolved) {
    635   ASSERT_TRUE(VerifyMethod("MergeTypes_Unresolved"));
    636   ASSERT_TRUE(HasAssignable("Ljava/lang/Exception;", "Ljava/net/SocketTimeoutException;", true));
    637   ASSERT_TRUE(HasAssignable(
    638       "Ljava/lang/Exception;", "Ljava/util/concurrent/TimeoutException;", true));
    639 }
    640 
    641 TEST_F(VerifierDepsTest, ConstClass_Resolved) {
    642   ASSERT_TRUE(VerifyMethod("ConstClass_Resolved"));
    643   ASSERT_TRUE(HasClass("Ljava/lang/IllegalStateException;", true, "public"));
    644 }
    645 
    646 TEST_F(VerifierDepsTest, ConstClass_Unresolved) {
    647   ASSERT_TRUE(VerifyMethod("ConstClass_Unresolved"));
    648   ASSERT_TRUE(HasClass("LUnresolvedClass;", false));
    649 }
    650 
    651 TEST_F(VerifierDepsTest, CheckCast_Resolved) {
    652   ASSERT_TRUE(VerifyMethod("CheckCast_Resolved"));
    653   ASSERT_TRUE(HasClass("Ljava/lang/IllegalStateException;", true, "public"));
    654 }
    655 
    656 TEST_F(VerifierDepsTest, CheckCast_Unresolved) {
    657   ASSERT_TRUE(VerifyMethod("CheckCast_Unresolved"));
    658   ASSERT_TRUE(HasClass("LUnresolvedClass;", false));
    659 }
    660 
    661 TEST_F(VerifierDepsTest, InstanceOf_Resolved) {
    662   ASSERT_TRUE(VerifyMethod("InstanceOf_Resolved"));
    663   ASSERT_TRUE(HasClass("Ljava/lang/IllegalStateException;", true, "public"));
    664 }
    665 
    666 TEST_F(VerifierDepsTest, InstanceOf_Unresolved) {
    667   ASSERT_TRUE(VerifyMethod("InstanceOf_Unresolved"));
    668   ASSERT_TRUE(HasClass("LUnresolvedClass;", false));
    669 }
    670 
    671 TEST_F(VerifierDepsTest, NewInstance_Resolved) {
    672   ASSERT_TRUE(VerifyMethod("NewInstance_Resolved"));
    673   ASSERT_TRUE(HasClass("Ljava/lang/IllegalStateException;", true, "public"));
    674 }
    675 
    676 TEST_F(VerifierDepsTest, NewInstance_Unresolved) {
    677   ASSERT_TRUE(VerifyMethod("NewInstance_Unresolved"));
    678   ASSERT_TRUE(HasClass("LUnresolvedClass;", false));
    679 }
    680 
    681 TEST_F(VerifierDepsTest, NewArray_Unresolved) {
    682   ASSERT_TRUE(VerifyMethod("NewArray_Unresolved"));
    683   ASSERT_TRUE(HasClass("[LUnresolvedClass;", false));
    684 }
    685 
    686 TEST_F(VerifierDepsTest, Throw) {
    687   ASSERT_TRUE(VerifyMethod("Throw"));
    688   ASSERT_TRUE(HasAssignable("Ljava/lang/Throwable;", "Ljava/lang/IllegalStateException;", true));
    689 }
    690 
    691 TEST_F(VerifierDepsTest, MoveException_Resolved) {
    692   ASSERT_TRUE(VerifyMethod("MoveException_Resolved"));
    693   ASSERT_TRUE(HasClass("Ljava/io/InterruptedIOException;", true, "public"));
    694   ASSERT_TRUE(HasClass("Ljava/net/SocketTimeoutException;", true, "public"));
    695   ASSERT_TRUE(HasClass("Ljava/util/zip/ZipException;", true, "public"));
    696 
    697   // Testing that all exception types are assignable to Throwable.
    698   ASSERT_TRUE(HasAssignable("Ljava/lang/Throwable;", "Ljava/io/InterruptedIOException;", true));
    699   ASSERT_TRUE(HasAssignable("Ljava/lang/Throwable;", "Ljava/net/SocketTimeoutException;", true));
    700   ASSERT_TRUE(HasAssignable("Ljava/lang/Throwable;", "Ljava/util/zip/ZipException;", true));
    701 
    702   // Testing that the merge type is assignable to Throwable.
    703   ASSERT_TRUE(HasAssignable("Ljava/lang/Throwable;", "Ljava/io/IOException;", true));
    704 
    705   // Merging of exception types.
    706   ASSERT_TRUE(HasAssignable("Ljava/io/IOException;", "Ljava/io/InterruptedIOException;", true));
    707   ASSERT_TRUE(HasAssignable("Ljava/io/IOException;", "Ljava/util/zip/ZipException;", true));
    708   ASSERT_TRUE(HasAssignable(
    709       "Ljava/io/InterruptedIOException;", "Ljava/net/SocketTimeoutException;", true));
    710 }
    711 
    712 TEST_F(VerifierDepsTest, MoveException_Unresolved) {
    713   ASSERT_FALSE(VerifyMethod("MoveException_Unresolved"));
    714   ASSERT_TRUE(HasClass("LUnresolvedException;", false));
    715 }
    716 
    717 TEST_F(VerifierDepsTest, StaticField_Resolved_DeclaredInReferenced) {
    718   ASSERT_TRUE(VerifyMethod("StaticField_Resolved_DeclaredInReferenced"));
    719   ASSERT_TRUE(HasClass("Ljava/lang/System;", true, "public"));
    720   ASSERT_TRUE(HasField("Ljava/lang/System;",
    721                        "out",
    722                        "Ljava/io/PrintStream;",
    723                        true,
    724                        "public static",
    725                        "Ljava/lang/System;"));
    726 }
    727 
    728 TEST_F(VerifierDepsTest, StaticField_Resolved_DeclaredInSuperclass1) {
    729   ASSERT_TRUE(VerifyMethod("StaticField_Resolved_DeclaredInSuperclass1"));
    730   ASSERT_TRUE(HasClass("Ljava/util/SimpleTimeZone;", true, "public"));
    731   ASSERT_TRUE(HasField(
    732       "Ljava/util/SimpleTimeZone;", "LONG", "I", true, "public static", "Ljava/util/TimeZone;"));
    733 }
    734 
    735 TEST_F(VerifierDepsTest, StaticField_Resolved_DeclaredInSuperclass2) {
    736   ASSERT_TRUE(VerifyMethod("StaticField_Resolved_DeclaredInSuperclass2"));
    737   ASSERT_TRUE(HasField(
    738       "LMySimpleTimeZone;", "SHORT", "I", true, "public static", "Ljava/util/TimeZone;"));
    739 }
    740 
    741 TEST_F(VerifierDepsTest, StaticField_Resolved_DeclaredInInterface1) {
    742   ASSERT_TRUE(VerifyMethod("StaticField_Resolved_DeclaredInInterface1"));
    743   ASSERT_TRUE(HasClass("Ljavax/xml/transform/dom/DOMResult;", true, "public"));
    744   ASSERT_TRUE(HasField("Ljavax/xml/transform/dom/DOMResult;",
    745                        "PI_ENABLE_OUTPUT_ESCAPING",
    746                        "Ljava/lang/String;",
    747                        true,
    748                        "public static",
    749                        "Ljavax/xml/transform/Result;"));
    750 }
    751 
    752 TEST_F(VerifierDepsTest, StaticField_Resolved_DeclaredInInterface2) {
    753   ASSERT_TRUE(VerifyMethod("StaticField_Resolved_DeclaredInInterface2"));
    754   ASSERT_TRUE(HasField("LMyDOMResult;",
    755                        "PI_ENABLE_OUTPUT_ESCAPING",
    756                        "Ljava/lang/String;",
    757                        true,
    758                        "public static",
    759                        "Ljavax/xml/transform/Result;"));
    760 }
    761 
    762 TEST_F(VerifierDepsTest, StaticField_Resolved_DeclaredInInterface3) {
    763   ASSERT_TRUE(VerifyMethod("StaticField_Resolved_DeclaredInInterface3"));
    764   ASSERT_TRUE(HasField("LMyResult;",
    765                        "PI_ENABLE_OUTPUT_ESCAPING",
    766                        "Ljava/lang/String;",
    767                        true,
    768                        "public static",
    769                        "Ljavax/xml/transform/Result;"));
    770 }
    771 
    772 TEST_F(VerifierDepsTest, StaticField_Resolved_DeclaredInInterface4) {
    773   ASSERT_TRUE(VerifyMethod("StaticField_Resolved_DeclaredInInterface4"));
    774   ASSERT_TRUE(HasField("LMyDocument;",
    775                        "ELEMENT_NODE",
    776                        "S",
    777                        true,
    778                        "public static",
    779                        "Lorg/w3c/dom/Node;"));
    780 }
    781 
    782 TEST_F(VerifierDepsTest, StaticField_Unresolved_ReferrerInBoot) {
    783   ASSERT_TRUE(VerifyMethod("StaticField_Unresolved_ReferrerInBoot"));
    784   ASSERT_TRUE(HasClass("Ljava/util/TimeZone;", true, "public"));
    785   ASSERT_TRUE(HasField("Ljava/util/TimeZone;", "x", "I", false));
    786 }
    787 
    788 TEST_F(VerifierDepsTest, StaticField_Unresolved_ReferrerInDex) {
    789   ASSERT_TRUE(VerifyMethod("StaticField_Unresolved_ReferrerInDex"));
    790   ASSERT_TRUE(HasField("LMyThreadSet;", "x", "I", false));
    791 }
    792 
    793 TEST_F(VerifierDepsTest, InstanceField_Resolved_DeclaredInReferenced) {
    794   ASSERT_TRUE(VerifyMethod("InstanceField_Resolved_DeclaredInReferenced"));
    795   ASSERT_TRUE(HasClass("Ljava/io/InterruptedIOException;", true, "public"));
    796   ASSERT_TRUE(HasField("Ljava/io/InterruptedIOException;",
    797                        "bytesTransferred",
    798                        "I",
    799                        true,
    800                        "public",
    801                        "Ljava/io/InterruptedIOException;"));
    802   ASSERT_TRUE(HasAssignable(
    803       "Ljava/io/InterruptedIOException;", "Ljava/net/SocketTimeoutException;", true));
    804 }
    805 
    806 TEST_F(VerifierDepsTest, InstanceField_Resolved_DeclaredInSuperclass1) {
    807   ASSERT_TRUE(VerifyMethod("InstanceField_Resolved_DeclaredInSuperclass1"));
    808   ASSERT_TRUE(HasClass("Ljava/net/SocketTimeoutException;", true, "public"));
    809   ASSERT_TRUE(HasField("Ljava/net/SocketTimeoutException;",
    810                        "bytesTransferred",
    811                        "I",
    812                        true,
    813                        "public",
    814                        "Ljava/io/InterruptedIOException;"));
    815   ASSERT_TRUE(HasAssignable(
    816       "Ljava/io/InterruptedIOException;", "Ljava/net/SocketTimeoutException;", true));
    817 }
    818 
    819 TEST_F(VerifierDepsTest, InstanceField_Resolved_DeclaredInSuperclass2) {
    820   ASSERT_TRUE(VerifyMethod("InstanceField_Resolved_DeclaredInSuperclass2"));
    821   ASSERT_TRUE(HasField("LMySocketTimeoutException;",
    822                        "bytesTransferred",
    823                        "I",
    824                        true,
    825                        "public",
    826                        "Ljava/io/InterruptedIOException;"));
    827   ASSERT_TRUE(HasAssignable(
    828       "Ljava/io/InterruptedIOException;", "Ljava/net/SocketTimeoutException;", true));
    829 }
    830 
    831 TEST_F(VerifierDepsTest, InstanceField_Unresolved_ReferrerInBoot) {
    832   ASSERT_TRUE(VerifyMethod("InstanceField_Unresolved_ReferrerInBoot"));
    833   ASSERT_TRUE(HasClass("Ljava/io/InterruptedIOException;", true, "public"));
    834   ASSERT_TRUE(HasField("Ljava/io/InterruptedIOException;", "x", "I", false));
    835 }
    836 
    837 TEST_F(VerifierDepsTest, InstanceField_Unresolved_ReferrerInDex) {
    838   ASSERT_TRUE(VerifyMethod("InstanceField_Unresolved_ReferrerInDex"));
    839   ASSERT_TRUE(HasField("LMyThreadSet;", "x", "I", false));
    840 }
    841 
    842 TEST_F(VerifierDepsTest, InvokeStatic_Resolved_DeclaredInReferenced) {
    843   ASSERT_TRUE(VerifyMethod("InvokeStatic_Resolved_DeclaredInReferenced"));
    844   ASSERT_TRUE(HasClass("Ljava/net/Socket;", true, "public"));
    845   ASSERT_TRUE(HasMethod("direct",
    846                         "Ljava/net/Socket;",
    847                         "setSocketImplFactory",
    848                         "(Ljava/net/SocketImplFactory;)V",
    849                         true,
    850                         "public static",
    851                         "Ljava/net/Socket;"));
    852 }
    853 
    854 TEST_F(VerifierDepsTest, InvokeStatic_Resolved_DeclaredInSuperclass1) {
    855   ASSERT_TRUE(VerifyMethod("InvokeStatic_Resolved_DeclaredInSuperclass1"));
    856   ASSERT_TRUE(HasClass("Ljavax/net/ssl/SSLSocket;", true, "public"));
    857   ASSERT_TRUE(HasMethod("direct",
    858                         "Ljavax/net/ssl/SSLSocket;",
    859                         "setSocketImplFactory",
    860                         "(Ljava/net/SocketImplFactory;)V",
    861                         true,
    862                         "public static",
    863                         "Ljava/net/Socket;"));
    864 }
    865 
    866 TEST_F(VerifierDepsTest, InvokeStatic_Resolved_DeclaredInSuperclass2) {
    867   ASSERT_TRUE(VerifyMethod("InvokeStatic_Resolved_DeclaredInSuperclass2"));
    868   ASSERT_TRUE(HasMethod("direct",
    869                         "LMySSLSocket;",
    870                         "setSocketImplFactory",
    871                         "(Ljava/net/SocketImplFactory;)V",
    872                         true,
    873                         "public static",
    874                         "Ljava/net/Socket;"));
    875 }
    876 
    877 TEST_F(VerifierDepsTest, InvokeStatic_DeclaredInInterface1) {
    878   ASSERT_TRUE(VerifyMethod("InvokeStatic_DeclaredInInterface1"));
    879   ASSERT_TRUE(HasClass("Ljava/util/Map$Entry;", true, "public interface"));
    880   ASSERT_TRUE(HasMethod("direct",
    881                         "Ljava/util/Map$Entry;",
    882                         "comparingByKey",
    883                         "()Ljava/util/Comparator;",
    884                         true,
    885                         "public static",
    886                         "Ljava/util/Map$Entry;"));
    887 }
    888 
    889 TEST_F(VerifierDepsTest, InvokeStatic_DeclaredInInterface2) {
    890   ASSERT_FALSE(VerifyMethod("InvokeStatic_DeclaredInInterface2"));
    891   ASSERT_TRUE(HasClass("Ljava/util/AbstractMap$SimpleEntry;", true, "public"));
    892   ASSERT_TRUE(HasMethod("direct",
    893                         "Ljava/util/AbstractMap$SimpleEntry;",
    894                         "comparingByKey",
    895                         "()Ljava/util/Comparator;",
    896                         false));
    897 }
    898 
    899 TEST_F(VerifierDepsTest, InvokeStatic_Unresolved1) {
    900   ASSERT_FALSE(VerifyMethod("InvokeStatic_Unresolved1"));
    901   ASSERT_TRUE(HasClass("Ljavax/net/ssl/SSLSocket;", true, "public"));
    902   ASSERT_TRUE(HasMethod("direct", "Ljavax/net/ssl/SSLSocket;", "x", "()V", false));
    903 }
    904 
    905 TEST_F(VerifierDepsTest, InvokeStatic_Unresolved2) {
    906   ASSERT_FALSE(VerifyMethod("InvokeStatic_Unresolved2"));
    907   ASSERT_TRUE(HasMethod("direct", "LMySSLSocket;", "x", "()V", false));
    908 }
    909 
    910 TEST_F(VerifierDepsTest, InvokeDirect_Resolved_DeclaredInReferenced) {
    911   ASSERT_TRUE(VerifyMethod("InvokeDirect_Resolved_DeclaredInReferenced"));
    912   ASSERT_TRUE(HasClass("Ljava/net/Socket;", true, "public"));
    913   ASSERT_TRUE(HasMethod(
    914       "direct", "Ljava/net/Socket;", "<init>", "()V", true, "public", "Ljava/net/Socket;"));
    915 }
    916 
    917 TEST_F(VerifierDepsTest, InvokeDirect_Resolved_DeclaredInSuperclass1) {
    918   ASSERT_FALSE(VerifyMethod("InvokeDirect_Resolved_DeclaredInSuperclass1"));
    919   ASSERT_TRUE(HasClass("Ljavax/net/ssl/SSLSocket;", true, "public"));
    920   ASSERT_TRUE(HasMethod("direct",
    921                         "Ljavax/net/ssl/SSLSocket;",
    922                         "checkOldImpl",
    923                         "()V",
    924                         true,
    925                         "private",
    926                         "Ljava/net/Socket;"));
    927 }
    928 
    929 TEST_F(VerifierDepsTest, InvokeDirect_Resolved_DeclaredInSuperclass2) {
    930   ASSERT_FALSE(VerifyMethod("InvokeDirect_Resolved_DeclaredInSuperclass2"));
    931   ASSERT_TRUE(HasMethod(
    932       "direct", "LMySSLSocket;", "checkOldImpl", "()V", true, "private", "Ljava/net/Socket;"));
    933 }
    934 
    935 TEST_F(VerifierDepsTest, InvokeDirect_Unresolved1) {
    936   ASSERT_FALSE(VerifyMethod("InvokeDirect_Unresolved1"));
    937   ASSERT_TRUE(HasClass("Ljavax/net/ssl/SSLSocket;", true, "public"));
    938   ASSERT_TRUE(HasMethod("direct", "Ljavax/net/ssl/SSLSocket;", "x", "()V", false));
    939 }
    940 
    941 TEST_F(VerifierDepsTest, InvokeDirect_Unresolved2) {
    942   ASSERT_FALSE(VerifyMethod("InvokeDirect_Unresolved2"));
    943   ASSERT_TRUE(HasMethod("direct", "LMySSLSocket;", "x", "()V", false));
    944 }
    945 
    946 TEST_F(VerifierDepsTest, InvokeVirtual_Resolved_DeclaredInReferenced) {
    947   ASSERT_TRUE(VerifyMethod("InvokeVirtual_Resolved_DeclaredInReferenced"));
    948   ASSERT_TRUE(HasClass("Ljava/lang/Throwable;", true, "public"));
    949   ASSERT_TRUE(HasMethod("virtual",
    950                         "Ljava/lang/Throwable;",
    951                         "getMessage",
    952                         "()Ljava/lang/String;",
    953                         true,
    954                         "public",
    955                         "Ljava/lang/Throwable;"));
    956   // Type dependency on `this` argument.
    957   ASSERT_TRUE(HasAssignable("Ljava/lang/Throwable;", "Ljava/net/SocketTimeoutException;", true));
    958 }
    959 
    960 TEST_F(VerifierDepsTest, InvokeVirtual_Resolved_DeclaredInSuperclass1) {
    961   ASSERT_TRUE(VerifyMethod("InvokeVirtual_Resolved_DeclaredInSuperclass1"));
    962   ASSERT_TRUE(HasClass("Ljava/io/InterruptedIOException;", true, "public"));
    963   ASSERT_TRUE(HasMethod("virtual",
    964                         "Ljava/io/InterruptedIOException;",
    965                         "getMessage",
    966                         "()Ljava/lang/String;",
    967                         true,
    968                         "public",
    969                         "Ljava/lang/Throwable;"));
    970   // Type dependency on `this` argument.
    971   ASSERT_TRUE(HasAssignable("Ljava/lang/Throwable;", "Ljava/net/SocketTimeoutException;", true));
    972 }
    973 
    974 TEST_F(VerifierDepsTest, InvokeVirtual_Resolved_DeclaredInSuperclass2) {
    975   ASSERT_TRUE(VerifyMethod("InvokeVirtual_Resolved_DeclaredInSuperclass2"));
    976   ASSERT_TRUE(HasMethod("virtual",
    977                         "LMySocketTimeoutException;",
    978                         "getMessage",
    979                         "()Ljava/lang/String;",
    980                         true,
    981                         "public",
    982                         "Ljava/lang/Throwable;"));
    983 }
    984 
    985 TEST_F(VerifierDepsTest, InvokeVirtual_Resolved_DeclaredInSuperinterface) {
    986   ASSERT_TRUE(VerifyMethod("InvokeVirtual_Resolved_DeclaredInSuperinterface"));
    987   ASSERT_TRUE(HasMethod("virtual",
    988                         "LMyThreadSet;",
    989                         "size",
    990                         "()I",
    991                         true,
    992                         "public",
    993                         "Ljava/util/Set;"));
    994 }
    995 
    996 TEST_F(VerifierDepsTest, InvokeVirtual_Unresolved1) {
    997   ASSERT_FALSE(VerifyMethod("InvokeVirtual_Unresolved1"));
    998   ASSERT_TRUE(HasClass("Ljava/io/InterruptedIOException;", true, "public"));
    999   ASSERT_TRUE(HasMethod("virtual", "Ljava/io/InterruptedIOException;", "x", "()V", false));
   1000 }
   1001 
   1002 TEST_F(VerifierDepsTest, InvokeVirtual_Unresolved2) {
   1003   ASSERT_FALSE(VerifyMethod("InvokeVirtual_Unresolved2"));
   1004   ASSERT_TRUE(HasMethod("virtual", "LMySocketTimeoutException;", "x", "()V", false));
   1005 }
   1006 
   1007 TEST_F(VerifierDepsTest, InvokeVirtual_ActuallyDirect) {
   1008   ASSERT_FALSE(VerifyMethod("InvokeVirtual_ActuallyDirect"));
   1009   ASSERT_TRUE(HasMethod("virtual", "LMyThread;", "activeCount", "()I", false));
   1010   ASSERT_TRUE(HasMethod("direct",
   1011                         "LMyThread;",
   1012                         "activeCount",
   1013                         "()I",
   1014                         true,
   1015                         "public static",
   1016                         "Ljava/lang/Thread;"));
   1017 }
   1018 
   1019 TEST_F(VerifierDepsTest, InvokeInterface_Resolved_DeclaredInReferenced) {
   1020   ASSERT_TRUE(VerifyMethod("InvokeInterface_Resolved_DeclaredInReferenced"));
   1021   ASSERT_TRUE(HasClass("Ljava/lang/Runnable;", true, "public interface"));
   1022   ASSERT_TRUE(HasMethod("interface",
   1023                         "Ljava/lang/Runnable;",
   1024                         "run",
   1025                         "()V",
   1026                         true,
   1027                         "public",
   1028                         "Ljava/lang/Runnable;"));
   1029 }
   1030 
   1031 TEST_F(VerifierDepsTest, InvokeInterface_Resolved_DeclaredInSuperclass) {
   1032   ASSERT_FALSE(VerifyMethod("InvokeInterface_Resolved_DeclaredInSuperclass"));
   1033   ASSERT_TRUE(HasMethod("interface", "LMyThread;", "join", "()V", false));
   1034 }
   1035 
   1036 TEST_F(VerifierDepsTest, InvokeInterface_Resolved_DeclaredInSuperinterface1) {
   1037   ASSERT_FALSE(VerifyMethod("InvokeInterface_Resolved_DeclaredInSuperinterface1"));
   1038   ASSERT_TRUE(HasMethod("interface",
   1039                         "LMyThreadSet;",
   1040                         "run",
   1041                         "()V",
   1042                         true,
   1043                         "public",
   1044                         "Ljava/lang/Runnable;"));
   1045 }
   1046 
   1047 TEST_F(VerifierDepsTest, InvokeInterface_Resolved_DeclaredInSuperinterface2) {
   1048   ASSERT_FALSE(VerifyMethod("InvokeInterface_Resolved_DeclaredInSuperinterface2"));
   1049   ASSERT_TRUE(HasMethod("interface",
   1050                         "LMyThreadSet;",
   1051                         "isEmpty",
   1052                         "()Z",
   1053                         true,
   1054                         "public",
   1055                         "Ljava/util/Set;"));
   1056 }
   1057 
   1058 TEST_F(VerifierDepsTest, InvokeInterface_Unresolved1) {
   1059   ASSERT_FALSE(VerifyMethod("InvokeInterface_Unresolved1"));
   1060   ASSERT_TRUE(HasClass("Ljava/lang/Runnable;", true, "public interface"));
   1061   ASSERT_TRUE(HasMethod("interface", "Ljava/lang/Runnable;", "x", "()V", false));
   1062 }
   1063 
   1064 TEST_F(VerifierDepsTest, InvokeInterface_Unresolved2) {
   1065   ASSERT_FALSE(VerifyMethod("InvokeInterface_Unresolved2"));
   1066   ASSERT_TRUE(HasMethod("interface", "LMyThreadSet;", "x", "()V", false));
   1067 }
   1068 
   1069 TEST_F(VerifierDepsTest, InvokeSuper_ThisAssignable) {
   1070   ASSERT_TRUE(VerifyMethod("InvokeSuper_ThisAssignable"));
   1071   ASSERT_TRUE(HasClass("Ljava/lang/Runnable;", true, "public interface"));
   1072   ASSERT_TRUE(HasAssignable("Ljava/lang/Runnable;", "Ljava/lang/Thread;", true));
   1073   ASSERT_TRUE(HasMethod("interface",
   1074                         "Ljava/lang/Runnable;",
   1075                         "run",
   1076                         "()V",
   1077                         true,
   1078                         "public",
   1079                         "Ljava/lang/Runnable;"));
   1080 }
   1081 
   1082 TEST_F(VerifierDepsTest, InvokeSuper_ThisNotAssignable) {
   1083   ASSERT_FALSE(VerifyMethod("InvokeSuper_ThisNotAssignable"));
   1084   ASSERT_TRUE(HasClass("Ljava/lang/Integer;", true, "public"));
   1085   ASSERT_TRUE(HasAssignable("Ljava/lang/Integer;", "Ljava/lang/Thread;", false));
   1086   ASSERT_TRUE(HasMethod(
   1087       "virtual", "Ljava/lang/Integer;", "intValue", "()I", true, "public", "Ljava/lang/Integer;"));
   1088 }
   1089 
   1090 TEST_F(VerifierDepsTest, ArgumentType_ResolvedReferenceArray) {
   1091   ASSERT_TRUE(VerifyMethod("ArgumentType_ResolvedReferenceArray"));
   1092   ASSERT_TRUE(HasClass("[Ljava/lang/Thread;", true, "public"));
   1093 }
   1094 
   1095 TEST_F(VerifierDepsTest, NewArray_Resolved) {
   1096   ASSERT_TRUE(VerifyMethod("NewArray_Resolved"));
   1097   ASSERT_TRUE(HasClass("[Ljava/lang/IllegalStateException;", true, "public"));
   1098 }
   1099 
   1100 TEST_F(VerifierDepsTest, EncodeDecode) {
   1101   VerifyDexFile();
   1102 
   1103   ASSERT_EQ(1u, NumberOfCompiledDexFiles());
   1104   ASSERT_TRUE(HasEachKindOfRecord());
   1105 
   1106   std::vector<uint8_t> buffer;
   1107   verifier_deps_->Encode(dex_files_, &buffer);
   1108   ASSERT_FALSE(buffer.empty());
   1109 
   1110   VerifierDeps decoded_deps(dex_files_, ArrayRef<const uint8_t>(buffer));
   1111   ASSERT_TRUE(verifier_deps_->Equals(decoded_deps));
   1112 }
   1113 
   1114 TEST_F(VerifierDepsTest, EncodeDecodeMulti) {
   1115   VerifyDexFile("MultiDex");
   1116 
   1117   ASSERT_GT(NumberOfCompiledDexFiles(), 1u);
   1118   std::vector<uint8_t> buffer;
   1119   verifier_deps_->Encode(dex_files_, &buffer);
   1120   ASSERT_FALSE(buffer.empty());
   1121 
   1122   // Create new DexFile, to mess with std::map order: the verifier deps used
   1123   // to iterate over the map, which doesn't guarantee insertion order. We fixed
   1124   // this by passing the expected order when encoding/decoding.
   1125   std::vector<std::unique_ptr<const DexFile>> first_dex_files = OpenTestDexFiles("VerifierDeps");
   1126   std::vector<std::unique_ptr<const DexFile>> second_dex_files = OpenTestDexFiles("MultiDex");
   1127   std::vector<const DexFile*> dex_files;
   1128   for (auto& dex_file : first_dex_files) {
   1129     dex_files.push_back(dex_file.get());
   1130   }
   1131   for (auto& dex_file : second_dex_files) {
   1132     dex_files.push_back(dex_file.get());
   1133   }
   1134 
   1135   // Dump the new verifier deps to ensure it can properly read the data.
   1136   VerifierDeps decoded_deps(dex_files, ArrayRef<const uint8_t>(buffer));
   1137   std::ostringstream stream;
   1138   VariableIndentationOutputStream os(&stream);
   1139   decoded_deps.Dump(&os);
   1140 }
   1141 
   1142 TEST_F(VerifierDepsTest, UnverifiedClasses) {
   1143   VerifyDexFile();
   1144   ASSERT_FALSE(HasUnverifiedClass("LMyThread;"));
   1145   // Test that a class with a soft failure is recorded.
   1146   ASSERT_TRUE(HasUnverifiedClass("LMain;"));
   1147   // Test that a class with hard failure is recorded.
   1148   ASSERT_TRUE(HasUnverifiedClass("LMyVerificationFailure;"));
   1149   // Test that a class with unresolved super is recorded.
   1150   ASSERT_TRUE(HasUnverifiedClass("LMyClassWithNoSuper;"));
   1151   // Test that a class with unresolved super and hard failure is recorded.
   1152   ASSERT_TRUE(HasUnverifiedClass("LMyClassWithNoSuperButFailures;"));
   1153 }
   1154 
   1155 // Returns the next resolution kind in the enum.
   1156 static MethodResolutionKind GetNextResolutionKind(MethodResolutionKind resolution_kind) {
   1157   if (resolution_kind == kDirectMethodResolution) {
   1158     return kVirtualMethodResolution;
   1159   } else if (resolution_kind == kVirtualMethodResolution) {
   1160     return kInterfaceMethodResolution;
   1161   } else {
   1162     DCHECK_EQ(resolution_kind, kInterfaceMethodResolution);
   1163     return kDirectMethodResolution;
   1164   }
   1165 }
   1166 
   1167 TEST_F(VerifierDepsTest, VerifyDeps) {
   1168   VerifyDexFile();
   1169 
   1170   ASSERT_EQ(1u, NumberOfCompiledDexFiles());
   1171   ASSERT_TRUE(HasEachKindOfRecord());
   1172 
   1173   // When validating, we create a new class loader, as
   1174   // the existing `class_loader_` may contain erroneous classes,
   1175   // that ClassLinker::FindClass won't return.
   1176 
   1177   ScopedObjectAccess soa(Thread::Current());
   1178   StackHandleScope<1> hs(soa.Self());
   1179   MutableHandle<mirror::ClassLoader> new_class_loader(hs.NewHandle<mirror::ClassLoader>(nullptr));
   1180   {
   1181     new_class_loader.Assign(soa.Decode<mirror::ClassLoader>(LoadDex("VerifierDeps")));
   1182     ASSERT_TRUE(verifier_deps_->ValidateDependencies(new_class_loader, soa.Self()));
   1183   }
   1184 
   1185   std::vector<uint8_t> buffer;
   1186   verifier_deps_->Encode(dex_files_, &buffer);
   1187   ASSERT_FALSE(buffer.empty());
   1188 
   1189   {
   1190     VerifierDeps decoded_deps(dex_files_, ArrayRef<const uint8_t>(buffer));
   1191     new_class_loader.Assign(soa.Decode<mirror::ClassLoader>(LoadDex("VerifierDeps")));
   1192     ASSERT_TRUE(decoded_deps.ValidateDependencies(new_class_loader, soa.Self()));
   1193   }
   1194 
   1195   // Fiddle with the dependencies to make sure we catch any change and fail to verify.
   1196 
   1197   {
   1198     // Mess up with the assignable_types.
   1199     VerifierDeps decoded_deps(dex_files_, ArrayRef<const uint8_t>(buffer));
   1200     VerifierDeps::DexFileDeps* deps = decoded_deps.GetDexFileDeps(*primary_dex_file_);
   1201     deps->assignable_types_.insert(*deps->unassignable_types_.begin());
   1202     new_class_loader.Assign(soa.Decode<mirror::ClassLoader>(LoadDex("VerifierDeps")));
   1203     ASSERT_FALSE(decoded_deps.ValidateDependencies(new_class_loader, soa.Self()));
   1204   }
   1205 
   1206   {
   1207     // Mess up with the unassignable_types.
   1208     VerifierDeps decoded_deps(dex_files_, ArrayRef<const uint8_t>(buffer));
   1209     VerifierDeps::DexFileDeps* deps = decoded_deps.GetDexFileDeps(*primary_dex_file_);
   1210     deps->unassignable_types_.insert(*deps->assignable_types_.begin());
   1211     new_class_loader.Assign(soa.Decode<mirror::ClassLoader>(LoadDex("VerifierDeps")));
   1212     ASSERT_FALSE(decoded_deps.ValidateDependencies(new_class_loader, soa.Self()));
   1213   }
   1214 
   1215   // Mess up with classes.
   1216   {
   1217     VerifierDeps decoded_deps(dex_files_, ArrayRef<const uint8_t>(buffer));
   1218     VerifierDeps::DexFileDeps* deps = decoded_deps.GetDexFileDeps(*primary_dex_file_);
   1219     bool found = false;
   1220     for (const auto& entry : deps->classes_) {
   1221       if (entry.IsResolved()) {
   1222         deps->classes_.insert(VerifierDeps::ClassResolution(
   1223             entry.GetDexTypeIndex(), VerifierDeps::kUnresolvedMarker));
   1224         found = true;
   1225         break;
   1226       }
   1227     }
   1228     ASSERT_TRUE(found);
   1229     new_class_loader.Assign(soa.Decode<mirror::ClassLoader>(LoadDex("VerifierDeps")));
   1230     ASSERT_FALSE(decoded_deps.ValidateDependencies(new_class_loader, soa.Self()));
   1231   }
   1232 
   1233   {
   1234     VerifierDeps decoded_deps(dex_files_, ArrayRef<const uint8_t>(buffer));
   1235     VerifierDeps::DexFileDeps* deps = decoded_deps.GetDexFileDeps(*primary_dex_file_);
   1236     bool found = false;
   1237     for (const auto& entry : deps->classes_) {
   1238       if (!entry.IsResolved()) {
   1239         deps->classes_.insert(VerifierDeps::ClassResolution(
   1240             entry.GetDexTypeIndex(), VerifierDeps::kUnresolvedMarker - 1));
   1241         found = true;
   1242         break;
   1243       }
   1244     }
   1245     ASSERT_TRUE(found);
   1246     new_class_loader.Assign(soa.Decode<mirror::ClassLoader>(LoadDex("VerifierDeps")));
   1247     ASSERT_FALSE(decoded_deps.ValidateDependencies(new_class_loader, soa.Self()));
   1248   }
   1249 
   1250   {
   1251     VerifierDeps decoded_deps(dex_files_, ArrayRef<const uint8_t>(buffer));
   1252     VerifierDeps::DexFileDeps* deps = decoded_deps.GetDexFileDeps(*primary_dex_file_);
   1253     bool found = false;
   1254     for (const auto& entry : deps->classes_) {
   1255       if (entry.IsResolved()) {
   1256         deps->classes_.insert(VerifierDeps::ClassResolution(
   1257             entry.GetDexTypeIndex(), entry.GetAccessFlags() - 1));
   1258         found = true;
   1259         break;
   1260       }
   1261     }
   1262     ASSERT_TRUE(found);
   1263     new_class_loader.Assign(soa.Decode<mirror::ClassLoader>(LoadDex("VerifierDeps")));
   1264     ASSERT_FALSE(decoded_deps.ValidateDependencies(new_class_loader, soa.Self()));
   1265   }
   1266 
   1267   // Mess up with fields.
   1268   {
   1269     VerifierDeps decoded_deps(dex_files_, ArrayRef<const uint8_t>(buffer));
   1270     VerifierDeps::DexFileDeps* deps = decoded_deps.GetDexFileDeps(*primary_dex_file_);
   1271     bool found = false;
   1272     for (const auto& entry : deps->fields_) {
   1273       if (entry.IsResolved()) {
   1274         deps->fields_.insert(VerifierDeps::FieldResolution(entry.GetDexFieldIndex(),
   1275                                                            VerifierDeps::kUnresolvedMarker,
   1276                                                            entry.GetDeclaringClassIndex()));
   1277         found = true;
   1278         break;
   1279       }
   1280     }
   1281     ASSERT_TRUE(found);
   1282     new_class_loader.Assign(soa.Decode<mirror::ClassLoader>(LoadDex("VerifierDeps")));
   1283     ASSERT_FALSE(decoded_deps.ValidateDependencies(new_class_loader, soa.Self()));
   1284   }
   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         constexpr dex::StringIndex kStringIndexZero(0);  // We know there is a class there.
   1293         deps->fields_.insert(VerifierDeps::FieldResolution(0 /* we know there is a field there */,
   1294                                                            VerifierDeps::kUnresolvedMarker - 1,
   1295                                                            kStringIndexZero));
   1296         found = true;
   1297         break;
   1298       }
   1299     }
   1300     ASSERT_TRUE(found);
   1301     new_class_loader.Assign(soa.Decode<mirror::ClassLoader>(LoadDex("VerifierDeps")));
   1302     ASSERT_FALSE(decoded_deps.ValidateDependencies(new_class_loader, soa.Self()));
   1303   }
   1304 
   1305   {
   1306     VerifierDeps decoded_deps(dex_files_, ArrayRef<const uint8_t>(buffer));
   1307     VerifierDeps::DexFileDeps* deps = decoded_deps.GetDexFileDeps(*primary_dex_file_);
   1308     bool found = false;
   1309     for (const auto& entry : deps->fields_) {
   1310       if (entry.IsResolved()) {
   1311         deps->fields_.insert(VerifierDeps::FieldResolution(entry.GetDexFieldIndex(),
   1312                                                            entry.GetAccessFlags() - 1,
   1313                                                            entry.GetDeclaringClassIndex()));
   1314         found = true;
   1315         break;
   1316       }
   1317     }
   1318     ASSERT_TRUE(found);
   1319     new_class_loader.Assign(soa.Decode<mirror::ClassLoader>(LoadDex("VerifierDeps")));
   1320     ASSERT_FALSE(decoded_deps.ValidateDependencies(new_class_loader, soa.Self()));
   1321   }
   1322 
   1323   {
   1324     VerifierDeps decoded_deps(dex_files_, ArrayRef<const uint8_t>(buffer));
   1325     VerifierDeps::DexFileDeps* deps = decoded_deps.GetDexFileDeps(*primary_dex_file_);
   1326     bool found = false;
   1327     for (const auto& entry : deps->fields_) {
   1328       constexpr dex::StringIndex kNewTypeIndex(0);
   1329       if (entry.GetDeclaringClassIndex() != kNewTypeIndex) {
   1330         deps->fields_.insert(VerifierDeps::FieldResolution(entry.GetDexFieldIndex(),
   1331                                                            entry.GetAccessFlags(),
   1332                                                            kNewTypeIndex));
   1333         found = true;
   1334         break;
   1335       }
   1336     }
   1337     ASSERT_TRUE(found);
   1338     new_class_loader.Assign(soa.Decode<mirror::ClassLoader>(LoadDex("VerifierDeps")));
   1339     ASSERT_FALSE(decoded_deps.ValidateDependencies(new_class_loader, soa.Self()));
   1340   }
   1341 
   1342   // Mess up with methods.
   1343   for (MethodResolutionKind resolution_kind :
   1344             { kDirectMethodResolution, kVirtualMethodResolution, kInterfaceMethodResolution }) {
   1345     {
   1346       VerifierDeps decoded_deps(dex_files_, ArrayRef<const uint8_t>(buffer));
   1347       VerifierDeps::DexFileDeps* deps = decoded_deps.GetDexFileDeps(*primary_dex_file_);
   1348       bool found = false;
   1349       std::set<VerifierDeps::MethodResolution>* methods = GetMethods(deps, resolution_kind);
   1350       for (const auto& entry : *methods) {
   1351         if (entry.IsResolved()) {
   1352           methods->insert(VerifierDeps::MethodResolution(entry.GetDexMethodIndex(),
   1353                                                          VerifierDeps::kUnresolvedMarker,
   1354                                                          entry.GetDeclaringClassIndex()));
   1355           found = true;
   1356           break;
   1357         }
   1358       }
   1359       ASSERT_TRUE(found);
   1360       new_class_loader.Assign(soa.Decode<mirror::ClassLoader>(LoadDex("VerifierDeps")));
   1361       ASSERT_FALSE(decoded_deps.ValidateDependencies(new_class_loader, soa.Self()));
   1362     }
   1363 
   1364     {
   1365       VerifierDeps decoded_deps(dex_files_, ArrayRef<const uint8_t>(buffer));
   1366       VerifierDeps::DexFileDeps* deps = decoded_deps.GetDexFileDeps(*primary_dex_file_);
   1367       bool found = false;
   1368       std::set<VerifierDeps::MethodResolution>* methods = GetMethods(deps, resolution_kind);
   1369       for (const auto& entry : *methods) {
   1370         if (!entry.IsResolved()) {
   1371           constexpr dex::StringIndex kStringIndexZero(0);  // We know there is a class there.
   1372           methods->insert(VerifierDeps::MethodResolution(0 /* we know there is a method there */,
   1373                                                          VerifierDeps::kUnresolvedMarker - 1,
   1374                                                          kStringIndexZero));
   1375           found = true;
   1376           break;
   1377         }
   1378       }
   1379       ASSERT_TRUE(found);
   1380       new_class_loader.Assign(soa.Decode<mirror::ClassLoader>(LoadDex("VerifierDeps")));
   1381       ASSERT_FALSE(decoded_deps.ValidateDependencies(new_class_loader, soa.Self()));
   1382     }
   1383 
   1384     {
   1385       VerifierDeps decoded_deps(dex_files_, ArrayRef<const uint8_t>(buffer));
   1386       VerifierDeps::DexFileDeps* deps = decoded_deps.GetDexFileDeps(*primary_dex_file_);
   1387       bool found = false;
   1388       std::set<VerifierDeps::MethodResolution>* methods = GetMethods(deps, resolution_kind);
   1389       for (const auto& entry : *methods) {
   1390         if (entry.IsResolved()) {
   1391           methods->insert(VerifierDeps::MethodResolution(entry.GetDexMethodIndex(),
   1392                                                          entry.GetAccessFlags() - 1,
   1393                                                          entry.GetDeclaringClassIndex()));
   1394           found = true;
   1395           break;
   1396         }
   1397       }
   1398       ASSERT_TRUE(found);
   1399       new_class_loader.Assign(soa.Decode<mirror::ClassLoader>(LoadDex("VerifierDeps")));
   1400       ASSERT_FALSE(decoded_deps.ValidateDependencies(new_class_loader, soa.Self()));
   1401     }
   1402 
   1403     {
   1404       VerifierDeps decoded_deps(dex_files_, ArrayRef<const uint8_t>(buffer));
   1405       VerifierDeps::DexFileDeps* deps = decoded_deps.GetDexFileDeps(*primary_dex_file_);
   1406       bool found = false;
   1407       std::set<VerifierDeps::MethodResolution>* methods = GetMethods(deps, resolution_kind);
   1408       for (const auto& entry : *methods) {
   1409         constexpr dex::StringIndex kNewTypeIndex(0);
   1410         if (entry.IsResolved() && entry.GetDeclaringClassIndex() != kNewTypeIndex) {
   1411           methods->insert(VerifierDeps::MethodResolution(entry.GetDexMethodIndex(),
   1412                                                          entry.GetAccessFlags(),
   1413                                                          kNewTypeIndex));
   1414           found = true;
   1415           break;
   1416         }
   1417       }
   1418       ASSERT_TRUE(found);
   1419       new_class_loader.Assign(soa.Decode<mirror::ClassLoader>(LoadDex("VerifierDeps")));
   1420       ASSERT_FALSE(decoded_deps.ValidateDependencies(new_class_loader, soa.Self()));
   1421     }
   1422 
   1423     // The two tests below make sure that fiddling with the method kind
   1424     // (static, virtual, interface) is detected by `ValidateDependencies`.
   1425 
   1426     // An interface method lookup can succeed with a virtual method lookup on the same class.
   1427     // That's OK, as we only want to make sure there is a method being defined with the right
   1428     // flags. Therefore, polluting the interface methods with virtual methods does not have
   1429     // to fail verification.
   1430     if (resolution_kind != kVirtualMethodResolution) {
   1431       VerifierDeps decoded_deps(dex_files_, ArrayRef<const uint8_t>(buffer));
   1432       VerifierDeps::DexFileDeps* deps = decoded_deps.GetDexFileDeps(*primary_dex_file_);
   1433       bool found = false;
   1434       std::set<VerifierDeps::MethodResolution>* methods = GetMethods(deps, resolution_kind);
   1435       for (const auto& entry : *methods) {
   1436         if (entry.IsResolved()) {
   1437           GetMethods(deps, GetNextResolutionKind(resolution_kind))->insert(
   1438               VerifierDeps::MethodResolution(entry.GetDexMethodIndex(),
   1439                                              entry.GetAccessFlags(),
   1440                                              entry.GetDeclaringClassIndex()));
   1441           found = true;
   1442         }
   1443       }
   1444       ASSERT_TRUE(found);
   1445       new_class_loader.Assign(soa.Decode<mirror::ClassLoader>(LoadDex("VerifierDeps")));
   1446       ASSERT_FALSE(decoded_deps.ValidateDependencies(new_class_loader, soa.Self()));
   1447     }
   1448 
   1449     // See comment above that applies the same way.
   1450     if (resolution_kind != kInterfaceMethodResolution) {
   1451       VerifierDeps decoded_deps(dex_files_, ArrayRef<const uint8_t>(buffer));
   1452       VerifierDeps::DexFileDeps* deps = decoded_deps.GetDexFileDeps(*primary_dex_file_);
   1453       bool found = false;
   1454       std::set<VerifierDeps::MethodResolution>* methods = GetMethods(deps, resolution_kind);
   1455       for (const auto& entry : *methods) {
   1456         if (entry.IsResolved()) {
   1457           GetMethods(deps, GetNextResolutionKind(GetNextResolutionKind(resolution_kind)))->insert(
   1458               VerifierDeps::MethodResolution(entry.GetDexMethodIndex(),
   1459                                              entry.GetAccessFlags(),
   1460                                              entry.GetDeclaringClassIndex()));
   1461           found = true;
   1462         }
   1463       }
   1464       ASSERT_TRUE(found);
   1465       new_class_loader.Assign(soa.Decode<mirror::ClassLoader>(LoadDex("VerifierDeps")));
   1466       ASSERT_FALSE(decoded_deps.ValidateDependencies(new_class_loader, soa.Self()));
   1467     }
   1468   }
   1469 }
   1470 
   1471 TEST_F(VerifierDepsTest, CompilerDriver) {
   1472   SetupCompilerDriver();
   1473 
   1474   // Test both multi-dex and single-dex configuration.
   1475   for (const char* multi : { "MultiDex", static_cast<const char*>(nullptr) }) {
   1476     // Test that the compiler driver behaves as expected when the dependencies
   1477     // verify and when they don't verify.
   1478     for (bool verify_failure : { false, true }) {
   1479       {
   1480         ScopedObjectAccess soa(Thread::Current());
   1481         LoadDexFile(&soa, "VerifierDeps", multi);
   1482       }
   1483       VerifyWithCompilerDriver(/* verifier_deps */ nullptr);
   1484 
   1485       std::vector<uint8_t> buffer;
   1486       verifier_deps_->Encode(dex_files_, &buffer);
   1487 
   1488       {
   1489         ScopedObjectAccess soa(Thread::Current());
   1490         LoadDexFile(&soa, "VerifierDeps", multi);
   1491       }
   1492       verifier::VerifierDeps decoded_deps(dex_files_, ArrayRef<const uint8_t>(buffer));
   1493       if (verify_failure) {
   1494         // Just taint the decoded VerifierDeps with one invalid entry.
   1495         VerifierDeps::DexFileDeps* deps = decoded_deps.GetDexFileDeps(*primary_dex_file_);
   1496         bool found = false;
   1497         for (const auto& entry : deps->classes_) {
   1498           if (entry.IsResolved()) {
   1499             deps->classes_.insert(VerifierDeps::ClassResolution(
   1500                 entry.GetDexTypeIndex(), VerifierDeps::kUnresolvedMarker));
   1501             found = true;
   1502             break;
   1503           }
   1504         }
   1505         ASSERT_TRUE(found);
   1506       }
   1507       VerifyWithCompilerDriver(&decoded_deps);
   1508 
   1509       if (verify_failure) {
   1510         ASSERT_FALSE(verifier_deps_ == nullptr);
   1511         ASSERT_FALSE(verifier_deps_->Equals(decoded_deps));
   1512       } else {
   1513         ASSERT_TRUE(verifier_deps_ == nullptr);
   1514         VerifyClassStatus(decoded_deps);
   1515       }
   1516     }
   1517   }
   1518 }
   1519 
   1520 TEST_F(VerifierDepsTest, MultiDexVerification) {
   1521   VerifyDexFile("VerifierDepsMulti");
   1522   ASSERT_EQ(NumberOfCompiledDexFiles(), 2u);
   1523 
   1524   ASSERT_TRUE(HasUnverifiedClass("LMySoftVerificationFailure;", *dex_files_[1]));
   1525   ASSERT_TRUE(HasUnverifiedClass("LMySub1SoftVerificationFailure;", *dex_files_[0]));
   1526   ASSERT_TRUE(HasUnverifiedClass("LMySub2SoftVerificationFailure;", *dex_files_[0]));
   1527 
   1528   std::vector<uint8_t> buffer;
   1529   verifier_deps_->Encode(dex_files_, &buffer);
   1530   ASSERT_FALSE(buffer.empty());
   1531 }
   1532 
   1533 TEST_F(VerifierDepsTest, NotAssignable_InterfaceWithClassInBoot) {
   1534   ASSERT_TRUE(TestAssignabilityRecording(/* dst */ "Ljava/lang/Exception;",
   1535                                          /* src */ "LIface;",
   1536                                          /* is_strict */ true,
   1537                                          /* is_assignable */ false));
   1538   ASSERT_TRUE(HasAssignable("Ljava/lang/Exception;", "LIface;", false));
   1539 }
   1540 
   1541 TEST_F(VerifierDepsTest, Assignable_Arrays) {
   1542   ASSERT_TRUE(TestAssignabilityRecording(/* dst */ "[LIface;",
   1543                                          /* src */ "[LMyClassExtendingInterface;",
   1544                                          /* is_strict */ false,
   1545                                          /* is_assignable */ true));
   1546   ASSERT_FALSE(HasAssignable(
   1547       "LIface;", "LMyClassExtendingInterface;", /* expected_is_assignable */ true));
   1548   ASSERT_FALSE(HasAssignable(
   1549       "LIface;", "LMyClassExtendingInterface;", /* expected_is_assignable */ false));
   1550 }
   1551 
   1552 }  // namespace verifier
   1553 }  // namespace art
   1554