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