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