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