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