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