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" |
| 22 | #include "compiler/driver/compiler_options.h" |
| 23 | #include "compiler/driver/compiler_driver.h" |
David Brazdil | ca3c8c3 | 2016-09-06 14:04:48 +0100 | [diff] [blame] | 24 | #include "compiler_callbacks.h" |
| 25 | #include "dex_file.h" |
| 26 | #include "handle_scope-inl.h" |
Nicolas Geoffray | 0802518 | 2016-10-25 17:20:18 +0100 | [diff] [blame^] | 27 | #include "verifier/method_verifier-inl.h" |
David Brazdil | ca3c8c3 | 2016-09-06 14:04:48 +0100 | [diff] [blame] | 28 | #include "mirror/class_loader.h" |
| 29 | #include "runtime.h" |
| 30 | #include "thread.h" |
Mathieu Chartier | 0795f23 | 2016-09-27 18:43:30 -0700 | [diff] [blame] | 31 | #include "scoped_thread_state_change-inl.h" |
David Brazdil | ca3c8c3 | 2016-09-06 14:04:48 +0100 | [diff] [blame] | 32 | |
| 33 | namespace art { |
| 34 | namespace verifier { |
| 35 | |
| 36 | class VerifierDepsCompilerCallbacks : public CompilerCallbacks { |
| 37 | public: |
| 38 | explicit VerifierDepsCompilerCallbacks() |
| 39 | : CompilerCallbacks(CompilerCallbacks::CallbackMode::kCompileApp), |
| 40 | deps_(nullptr) {} |
| 41 | |
| 42 | void MethodVerified(verifier::MethodVerifier* verifier ATTRIBUTE_UNUSED) OVERRIDE {} |
| 43 | void ClassRejected(ClassReference ref ATTRIBUTE_UNUSED) OVERRIDE {} |
| 44 | bool IsRelocationPossible() OVERRIDE { return false; } |
| 45 | |
| 46 | verifier::VerifierDeps* GetVerifierDeps() const OVERRIDE { return deps_; } |
| 47 | void SetVerifierDeps(verifier::VerifierDeps* deps) { deps_ = deps; } |
| 48 | |
| 49 | private: |
| 50 | verifier::VerifierDeps* deps_; |
| 51 | }; |
| 52 | |
Nicolas Geoffray | 0802518 | 2016-10-25 17:20:18 +0100 | [diff] [blame^] | 53 | class VerifierDepsTest : public CommonCompilerTest { |
David Brazdil | ca3c8c3 | 2016-09-06 14:04:48 +0100 | [diff] [blame] | 54 | public: |
| 55 | void SetUpRuntimeOptions(RuntimeOptions* options) { |
Nicolas Geoffray | 0802518 | 2016-10-25 17:20:18 +0100 | [diff] [blame^] | 56 | CommonCompilerTest::SetUpRuntimeOptions(options); |
David Brazdil | ca3c8c3 | 2016-09-06 14:04:48 +0100 | [diff] [blame] | 57 | callbacks_.reset(new VerifierDepsCompilerCallbacks()); |
| 58 | } |
| 59 | |
| 60 | mirror::Class* FindClassByName(const std::string& name, ScopedObjectAccess* soa) |
| 61 | REQUIRES_SHARED(Locks::mutator_lock_) { |
| 62 | StackHandleScope<1> hs(Thread::Current()); |
| 63 | Handle<mirror::ClassLoader> class_loader_handle( |
Mathieu Chartier | 0795f23 | 2016-09-27 18:43:30 -0700 | [diff] [blame] | 64 | hs.NewHandle(soa->Decode<mirror::ClassLoader>(class_loader_))); |
David Brazdil | 6f82fbd | 2016-09-14 11:55:26 +0100 | [diff] [blame] | 65 | mirror::Class* klass = class_linker_->FindClass(Thread::Current(), |
| 66 | name.c_str(), |
| 67 | class_loader_handle); |
| 68 | if (klass == nullptr) { |
| 69 | DCHECK(Thread::Current()->IsExceptionPending()); |
| 70 | Thread::Current()->ClearException(); |
| 71 | } |
| 72 | return klass; |
| 73 | } |
| 74 | |
| 75 | void SetVerifierDeps(const std::vector<const DexFile*>& dex_files) { |
| 76 | verifier_deps_.reset(new verifier::VerifierDeps(dex_files)); |
| 77 | VerifierDepsCompilerCallbacks* callbacks = |
| 78 | reinterpret_cast<VerifierDepsCompilerCallbacks*>(callbacks_.get()); |
| 79 | callbacks->SetVerifierDeps(verifier_deps_.get()); |
David Brazdil | ca3c8c3 | 2016-09-06 14:04:48 +0100 | [diff] [blame] | 80 | } |
| 81 | |
| 82 | void LoadDexFile(ScopedObjectAccess* soa) REQUIRES_SHARED(Locks::mutator_lock_) { |
| 83 | class_loader_ = LoadDex("VerifierDeps"); |
| 84 | std::vector<const DexFile*> dex_files = GetDexFiles(class_loader_); |
| 85 | CHECK_EQ(dex_files.size(), 1u); |
| 86 | dex_file_ = dex_files.front(); |
| 87 | |
David Brazdil | 6f82fbd | 2016-09-14 11:55:26 +0100 | [diff] [blame] | 88 | SetVerifierDeps(dex_files); |
| 89 | |
Mathieu Chartier | 0795f23 | 2016-09-27 18:43:30 -0700 | [diff] [blame] | 90 | ObjPtr<mirror::ClassLoader> loader = soa->Decode<mirror::ClassLoader>(class_loader_); |
Mathieu Chartier | 1cc62e4 | 2016-10-03 18:01:28 -0700 | [diff] [blame] | 91 | class_linker_->RegisterDexFile(*dex_file_, loader.Ptr()); |
David Brazdil | ca3c8c3 | 2016-09-06 14:04:48 +0100 | [diff] [blame] | 92 | |
| 93 | klass_Main_ = FindClassByName("LMain;", soa); |
| 94 | CHECK(klass_Main_ != nullptr); |
David Brazdil | ca3c8c3 | 2016-09-06 14:04:48 +0100 | [diff] [blame] | 95 | } |
| 96 | |
| 97 | bool VerifyMethod(const std::string& method_name) { |
| 98 | ScopedObjectAccess soa(Thread::Current()); |
| 99 | LoadDexFile(&soa); |
| 100 | |
| 101 | StackHandleScope<2> hs(Thread::Current()); |
| 102 | Handle<mirror::ClassLoader> class_loader_handle( |
Mathieu Chartier | 0795f23 | 2016-09-27 18:43:30 -0700 | [diff] [blame] | 103 | hs.NewHandle(soa.Decode<mirror::ClassLoader>(class_loader_))); |
David Brazdil | ca3c8c3 | 2016-09-06 14:04:48 +0100 | [diff] [blame] | 104 | Handle<mirror::DexCache> dex_cache_handle(hs.NewHandle(klass_Main_->GetDexCache())); |
| 105 | |
| 106 | const DexFile::ClassDef* class_def = klass_Main_->GetClassDef(); |
| 107 | const uint8_t* class_data = dex_file_->GetClassData(*class_def); |
| 108 | CHECK(class_data != nullptr); |
| 109 | |
| 110 | ClassDataItemIterator it(*dex_file_, class_data); |
| 111 | while (it.HasNextStaticField() || it.HasNextInstanceField()) { |
| 112 | it.Next(); |
| 113 | } |
| 114 | |
| 115 | ArtMethod* method = nullptr; |
| 116 | while (it.HasNextDirectMethod()) { |
| 117 | ArtMethod* resolved_method = class_linker_->ResolveMethod<ClassLinker::kNoICCECheckForCache>( |
| 118 | *dex_file_, |
| 119 | it.GetMemberIndex(), |
| 120 | dex_cache_handle, |
| 121 | class_loader_handle, |
| 122 | nullptr, |
| 123 | it.GetMethodInvokeType(*class_def)); |
| 124 | CHECK(resolved_method != nullptr); |
| 125 | if (method_name == resolved_method->GetName()) { |
| 126 | method = resolved_method; |
| 127 | break; |
| 128 | } |
| 129 | it.Next(); |
| 130 | } |
| 131 | CHECK(method != nullptr); |
| 132 | |
| 133 | MethodVerifier verifier(Thread::Current(), |
| 134 | dex_file_, |
| 135 | dex_cache_handle, |
| 136 | class_loader_handle, |
| 137 | *class_def, |
| 138 | it.GetMethodCodeItem(), |
| 139 | it.GetMemberIndex(), |
| 140 | method, |
| 141 | it.GetMethodAccessFlags(), |
| 142 | true /* can_load_classes */, |
| 143 | true /* allow_soft_failures */, |
| 144 | true /* need_precise_constants */, |
| 145 | false /* verify to dump */, |
| 146 | true /* allow_thread_suspension */); |
| 147 | verifier.Verify(); |
| 148 | return !verifier.HasFailures(); |
| 149 | } |
| 150 | |
David Brazdil | 6f82fbd | 2016-09-14 11:55:26 +0100 | [diff] [blame] | 151 | void VerifyDexFile() { |
| 152 | std::string error_msg; |
Nicolas Geoffray | 0802518 | 2016-10-25 17:20:18 +0100 | [diff] [blame^] | 153 | { |
| 154 | ScopedObjectAccess soa(Thread::Current()); |
| 155 | LoadDexFile(&soa); |
David Brazdil | 6f82fbd | 2016-09-14 11:55:26 +0100 | [diff] [blame] | 156 | } |
Nicolas Geoffray | 0802518 | 2016-10-25 17:20:18 +0100 | [diff] [blame^] | 157 | SetVerifierDeps({ dex_file_ }); |
| 158 | TimingLogger timings("Verify", false, false); |
| 159 | std::vector<const DexFile*> dex_files; |
| 160 | dex_files.push_back(dex_file_); |
| 161 | compiler_options_->boot_image_ = false; |
| 162 | compiler_driver_->InitializeThreadPools(); |
| 163 | compiler_driver_->Verify(class_loader_, dex_files, &timings); |
David Brazdil | 6f82fbd | 2016-09-14 11:55:26 +0100 | [diff] [blame] | 164 | } |
| 165 | |
David Brazdil | ca3c8c3 | 2016-09-06 14:04:48 +0100 | [diff] [blame] | 166 | bool TestAssignabilityRecording(const std::string& dst, |
| 167 | const std::string& src, |
| 168 | bool is_strict, |
| 169 | bool is_assignable) { |
| 170 | ScopedObjectAccess soa(Thread::Current()); |
| 171 | LoadDexFile(&soa); |
David Brazdil | 6f82fbd | 2016-09-14 11:55:26 +0100 | [diff] [blame] | 172 | mirror::Class* klass_dst = FindClassByName(dst, &soa); |
| 173 | DCHECK(klass_dst != nullptr); |
| 174 | mirror::Class* klass_src = FindClassByName(src, &soa); |
| 175 | DCHECK(klass_src != nullptr); |
David Brazdil | ca3c8c3 | 2016-09-06 14:04:48 +0100 | [diff] [blame] | 176 | verifier_deps_->AddAssignability(*dex_file_, |
David Brazdil | 6f82fbd | 2016-09-14 11:55:26 +0100 | [diff] [blame] | 177 | klass_dst, |
| 178 | klass_src, |
David Brazdil | ca3c8c3 | 2016-09-06 14:04:48 +0100 | [diff] [blame] | 179 | is_strict, |
| 180 | is_assignable); |
| 181 | return true; |
| 182 | } |
| 183 | |
Nicolas Geoffray | 0802518 | 2016-10-25 17:20:18 +0100 | [diff] [blame^] | 184 | bool HasUnverifiedClass(const std::string& cls) { |
| 185 | const DexFile::TypeId* type_id = dex_file_->FindTypeId(cls.c_str()); |
| 186 | DCHECK(type_id != nullptr); |
| 187 | uint16_t index = dex_file_->GetIndexForTypeId(*type_id); |
| 188 | MutexLock mu(Thread::Current(), *Locks::verifier_deps_lock_); |
| 189 | for (const auto& dex_dep : verifier_deps_->dex_deps_) { |
| 190 | for (uint16_t entry : dex_dep.second->unverified_classes_) { |
| 191 | if (index == entry) { |
| 192 | return true; |
| 193 | } |
| 194 | } |
| 195 | } |
| 196 | return false; |
| 197 | } |
| 198 | |
David Brazdil | ca3c8c3 | 2016-09-06 14:04:48 +0100 | [diff] [blame] | 199 | // Iterates over all assignability records and tries to find an entry which |
| 200 | // matches the expected destination/source pair. |
| 201 | bool HasAssignable(const std::string& expected_destination, |
| 202 | const std::string& expected_source, |
| 203 | bool expected_is_assignable) { |
| 204 | MutexLock mu(Thread::Current(), *Locks::verifier_deps_lock_); |
| 205 | for (auto& dex_dep : verifier_deps_->dex_deps_) { |
| 206 | const DexFile& dex_file = *dex_dep.first; |
| 207 | auto& storage = expected_is_assignable ? dex_dep.second->assignable_types_ |
| 208 | : dex_dep.second->unassignable_types_; |
| 209 | for (auto& entry : storage) { |
| 210 | std::string actual_destination = |
| 211 | verifier_deps_->GetStringFromId(dex_file, entry.GetDestination()); |
| 212 | std::string actual_source = verifier_deps_->GetStringFromId(dex_file, entry.GetSource()); |
| 213 | if ((expected_destination == actual_destination) && (expected_source == actual_source)) { |
| 214 | return true; |
| 215 | } |
| 216 | } |
| 217 | } |
| 218 | return false; |
| 219 | } |
| 220 | |
| 221 | // Iterates over all class resolution records, finds an entry which matches |
| 222 | // the given class descriptor and tests its properties. |
| 223 | bool HasClass(const std::string& expected_klass, |
| 224 | bool expected_resolved, |
| 225 | const std::string& expected_access_flags = "") { |
| 226 | MutexLock mu(Thread::Current(), *Locks::verifier_deps_lock_); |
| 227 | for (auto& dex_dep : verifier_deps_->dex_deps_) { |
| 228 | for (auto& entry : dex_dep.second->classes_) { |
| 229 | if (expected_resolved != entry.IsResolved()) { |
| 230 | continue; |
| 231 | } |
| 232 | |
| 233 | std::string actual_klass = dex_dep.first->StringByTypeIdx(entry.GetDexTypeIndex()); |
| 234 | if (expected_klass != actual_klass) { |
| 235 | continue; |
| 236 | } |
| 237 | |
| 238 | if (expected_resolved) { |
| 239 | // Test access flags. Note that PrettyJavaAccessFlags always appends |
| 240 | // a space after the modifiers. Add it to the expected access flags. |
| 241 | std::string actual_access_flags = PrettyJavaAccessFlags(entry.GetAccessFlags()); |
| 242 | if (expected_access_flags + " " != actual_access_flags) { |
| 243 | continue; |
| 244 | } |
| 245 | } |
| 246 | |
| 247 | return true; |
| 248 | } |
| 249 | } |
| 250 | return false; |
| 251 | } |
| 252 | |
| 253 | // Iterates over all field resolution records, finds an entry which matches |
| 254 | // the given field class+name+type and tests its properties. |
| 255 | bool HasField(const std::string& expected_klass, |
| 256 | const std::string& expected_name, |
| 257 | const std::string& expected_type, |
| 258 | bool expected_resolved, |
| 259 | const std::string& expected_access_flags = "", |
| 260 | const std::string& expected_decl_klass = "") { |
| 261 | MutexLock mu(Thread::Current(), *Locks::verifier_deps_lock_); |
| 262 | for (auto& dex_dep : verifier_deps_->dex_deps_) { |
| 263 | for (auto& entry : dex_dep.second->fields_) { |
| 264 | if (expected_resolved != entry.IsResolved()) { |
| 265 | continue; |
| 266 | } |
| 267 | |
| 268 | const DexFile::FieldId& field_id = dex_dep.first->GetFieldId(entry.GetDexFieldIndex()); |
| 269 | |
| 270 | std::string actual_klass = dex_dep.first->StringByTypeIdx(field_id.class_idx_); |
| 271 | if (expected_klass != actual_klass) { |
| 272 | continue; |
| 273 | } |
| 274 | |
| 275 | std::string actual_name = dex_dep.first->StringDataByIdx(field_id.name_idx_); |
| 276 | if (expected_name != actual_name) { |
| 277 | continue; |
| 278 | } |
| 279 | |
| 280 | std::string actual_type = dex_dep.first->StringByTypeIdx(field_id.type_idx_); |
| 281 | if (expected_type != actual_type) { |
| 282 | continue; |
| 283 | } |
| 284 | |
| 285 | if (expected_resolved) { |
| 286 | // Test access flags. Note that PrettyJavaAccessFlags always appends |
| 287 | // a space after the modifiers. Add it to the expected access flags. |
| 288 | std::string actual_access_flags = PrettyJavaAccessFlags(entry.GetAccessFlags()); |
| 289 | if (expected_access_flags + " " != actual_access_flags) { |
| 290 | continue; |
| 291 | } |
| 292 | |
| 293 | std::string actual_decl_klass = verifier_deps_->GetStringFromId( |
| 294 | *dex_dep.first, entry.GetDeclaringClassIndex()); |
| 295 | if (expected_decl_klass != actual_decl_klass) { |
| 296 | continue; |
| 297 | } |
| 298 | } |
| 299 | |
| 300 | return true; |
| 301 | } |
| 302 | } |
| 303 | return false; |
| 304 | } |
| 305 | |
| 306 | // Iterates over all method resolution records, finds an entry which matches |
| 307 | // the given field kind+class+name+signature and tests its properties. |
| 308 | bool HasMethod(const std::string& expected_kind, |
| 309 | const std::string& expected_klass, |
| 310 | const std::string& expected_name, |
| 311 | const std::string& expected_signature, |
| 312 | bool expected_resolved, |
| 313 | const std::string& expected_access_flags = "", |
| 314 | const std::string& expected_decl_klass = "") { |
| 315 | MutexLock mu(Thread::Current(), *Locks::verifier_deps_lock_); |
| 316 | for (auto& dex_dep : verifier_deps_->dex_deps_) { |
| 317 | auto& storage = (expected_kind == "direct") ? dex_dep.second->direct_methods_ |
| 318 | : (expected_kind == "virtual") ? dex_dep.second->virtual_methods_ |
| 319 | : dex_dep.second->interface_methods_; |
| 320 | for (auto& entry : storage) { |
| 321 | if (expected_resolved != entry.IsResolved()) { |
| 322 | continue; |
| 323 | } |
| 324 | |
| 325 | const DexFile::MethodId& method_id = dex_dep.first->GetMethodId(entry.GetDexMethodIndex()); |
| 326 | |
| 327 | std::string actual_klass = dex_dep.first->StringByTypeIdx(method_id.class_idx_); |
| 328 | if (expected_klass != actual_klass) { |
| 329 | continue; |
| 330 | } |
| 331 | |
| 332 | std::string actual_name = dex_dep.first->StringDataByIdx(method_id.name_idx_); |
| 333 | if (expected_name != actual_name) { |
| 334 | continue; |
| 335 | } |
| 336 | |
| 337 | std::string actual_signature = dex_dep.first->GetMethodSignature(method_id).ToString(); |
| 338 | if (expected_signature != actual_signature) { |
| 339 | continue; |
| 340 | } |
| 341 | |
| 342 | if (expected_resolved) { |
| 343 | // Test access flags. Note that PrettyJavaAccessFlags always appends |
| 344 | // a space after the modifiers. Add it to the expected access flags. |
| 345 | std::string actual_access_flags = PrettyJavaAccessFlags(entry.GetAccessFlags()); |
| 346 | if (expected_access_flags + " " != actual_access_flags) { |
| 347 | continue; |
| 348 | } |
| 349 | |
| 350 | std::string actual_decl_klass = verifier_deps_->GetStringFromId( |
| 351 | *dex_dep.first, entry.GetDeclaringClassIndex()); |
| 352 | if (expected_decl_klass != actual_decl_klass) { |
| 353 | continue; |
| 354 | } |
| 355 | } |
| 356 | |
| 357 | return true; |
| 358 | } |
| 359 | } |
| 360 | return false; |
| 361 | } |
| 362 | |
David Brazdil | 6f82fbd | 2016-09-14 11:55:26 +0100 | [diff] [blame] | 363 | size_t NumberOfCompiledDexFiles() { |
| 364 | MutexLock mu(Thread::Current(), *Locks::verifier_deps_lock_); |
| 365 | return verifier_deps_->dex_deps_.size(); |
| 366 | } |
| 367 | |
| 368 | size_t HasEachKindOfRecord() { |
| 369 | MutexLock mu(Thread::Current(), *Locks::verifier_deps_lock_); |
| 370 | |
| 371 | bool has_strings = false; |
| 372 | bool has_assignability = false; |
| 373 | bool has_classes = false; |
| 374 | bool has_fields = false; |
| 375 | bool has_methods = false; |
Nicolas Geoffray | 0802518 | 2016-10-25 17:20:18 +0100 | [diff] [blame^] | 376 | bool has_unverified_classes = false; |
David Brazdil | 6f82fbd | 2016-09-14 11:55:26 +0100 | [diff] [blame] | 377 | |
| 378 | for (auto& entry : verifier_deps_->dex_deps_) { |
| 379 | has_strings |= !entry.second->strings_.empty(); |
| 380 | has_assignability |= !entry.second->assignable_types_.empty(); |
| 381 | has_assignability |= !entry.second->unassignable_types_.empty(); |
| 382 | has_classes |= !entry.second->classes_.empty(); |
| 383 | has_fields |= !entry.second->fields_.empty(); |
| 384 | has_methods |= !entry.second->direct_methods_.empty(); |
| 385 | has_methods |= !entry.second->virtual_methods_.empty(); |
| 386 | has_methods |= !entry.second->interface_methods_.empty(); |
Nicolas Geoffray | 0802518 | 2016-10-25 17:20:18 +0100 | [diff] [blame^] | 387 | has_unverified_classes |= !entry.second->unverified_classes_.empty(); |
David Brazdil | 6f82fbd | 2016-09-14 11:55:26 +0100 | [diff] [blame] | 388 | } |
| 389 | |
Nicolas Geoffray | 0802518 | 2016-10-25 17:20:18 +0100 | [diff] [blame^] | 390 | return has_strings && |
| 391 | has_assignability && |
| 392 | has_classes && |
| 393 | has_fields && |
| 394 | has_methods && |
| 395 | has_unverified_classes; |
David Brazdil | 6f82fbd | 2016-09-14 11:55:26 +0100 | [diff] [blame] | 396 | } |
| 397 | |
David Brazdil | ca3c8c3 | 2016-09-06 14:04:48 +0100 | [diff] [blame] | 398 | std::unique_ptr<verifier::VerifierDeps> verifier_deps_; |
| 399 | const DexFile* dex_file_; |
| 400 | jobject class_loader_; |
| 401 | mirror::Class* klass_Main_; |
| 402 | }; |
| 403 | |
| 404 | TEST_F(VerifierDepsTest, StringToId) { |
| 405 | ScopedObjectAccess soa(Thread::Current()); |
| 406 | LoadDexFile(&soa); |
| 407 | |
| 408 | MutexLock mu(Thread::Current(), *Locks::verifier_deps_lock_); |
| 409 | |
| 410 | uint32_t id_Main1 = verifier_deps_->GetIdFromString(*dex_file_, "LMain;"); |
| 411 | ASSERT_LT(id_Main1, dex_file_->NumStringIds()); |
| 412 | ASSERT_EQ("LMain;", verifier_deps_->GetStringFromId(*dex_file_, id_Main1)); |
| 413 | |
| 414 | uint32_t id_Main2 = verifier_deps_->GetIdFromString(*dex_file_, "LMain;"); |
| 415 | ASSERT_LT(id_Main2, dex_file_->NumStringIds()); |
| 416 | ASSERT_EQ("LMain;", verifier_deps_->GetStringFromId(*dex_file_, id_Main2)); |
| 417 | |
| 418 | uint32_t id_Lorem1 = verifier_deps_->GetIdFromString(*dex_file_, "Lorem ipsum"); |
| 419 | ASSERT_GE(id_Lorem1, dex_file_->NumStringIds()); |
| 420 | ASSERT_EQ("Lorem ipsum", verifier_deps_->GetStringFromId(*dex_file_, id_Lorem1)); |
| 421 | |
| 422 | uint32_t id_Lorem2 = verifier_deps_->GetIdFromString(*dex_file_, "Lorem ipsum"); |
| 423 | ASSERT_GE(id_Lorem2, dex_file_->NumStringIds()); |
| 424 | ASSERT_EQ("Lorem ipsum", verifier_deps_->GetStringFromId(*dex_file_, id_Lorem2)); |
| 425 | |
| 426 | ASSERT_EQ(id_Main1, id_Main2); |
| 427 | ASSERT_EQ(id_Lorem1, id_Lorem2); |
| 428 | ASSERT_NE(id_Main1, id_Lorem1); |
| 429 | } |
| 430 | |
| 431 | TEST_F(VerifierDepsTest, Assignable_BothInBoot) { |
| 432 | ASSERT_TRUE(TestAssignabilityRecording(/* dst */ "Ljava/util/TimeZone;", |
| 433 | /* src */ "Ljava/util/SimpleTimeZone;", |
| 434 | /* is_strict */ true, |
| 435 | /* is_assignable */ true)); |
| 436 | ASSERT_TRUE(HasAssignable("Ljava/util/TimeZone;", "Ljava/util/SimpleTimeZone;", true)); |
| 437 | } |
| 438 | |
| 439 | TEST_F(VerifierDepsTest, Assignable_DestinationInBoot1) { |
| 440 | ASSERT_TRUE(TestAssignabilityRecording(/* dst */ "Ljava/net/Socket;", |
| 441 | /* src */ "LMySSLSocket;", |
| 442 | /* is_strict */ true, |
| 443 | /* is_assignable */ true)); |
| 444 | ASSERT_TRUE(HasAssignable("Ljava/net/Socket;", "LMySSLSocket;", true)); |
| 445 | } |
| 446 | |
| 447 | TEST_F(VerifierDepsTest, Assignable_DestinationInBoot2) { |
| 448 | ASSERT_TRUE(TestAssignabilityRecording(/* dst */ "Ljava/util/TimeZone;", |
| 449 | /* src */ "LMySimpleTimeZone;", |
| 450 | /* is_strict */ true, |
| 451 | /* is_assignable */ true)); |
| 452 | ASSERT_TRUE(HasAssignable("Ljava/util/TimeZone;", "LMySimpleTimeZone;", true)); |
| 453 | } |
| 454 | |
| 455 | TEST_F(VerifierDepsTest, Assignable_DestinationInBoot3) { |
| 456 | ASSERT_TRUE(TestAssignabilityRecording(/* dst */ "Ljava/util/Collection;", |
| 457 | /* src */ "LMyThreadSet;", |
| 458 | /* is_strict */ true, |
| 459 | /* is_assignable */ true)); |
| 460 | ASSERT_TRUE(HasAssignable("Ljava/util/Collection;", "LMyThreadSet;", true)); |
| 461 | } |
| 462 | |
| 463 | TEST_F(VerifierDepsTest, Assignable_BothArrays_Resolved) { |
| 464 | ASSERT_TRUE(TestAssignabilityRecording(/* dst */ "[[Ljava/util/TimeZone;", |
| 465 | /* src */ "[[Ljava/util/SimpleTimeZone;", |
| 466 | /* is_strict */ true, |
| 467 | /* is_assignable */ true)); |
| 468 | // If the component types of both arrays are resolved, we optimize the list of |
| 469 | // dependencies by recording a dependency on the component types. |
| 470 | ASSERT_FALSE(HasAssignable("[[Ljava/util/TimeZone;", "[[Ljava/util/SimpleTimeZone;", true)); |
| 471 | ASSERT_FALSE(HasAssignable("[Ljava/util/TimeZone;", "[Ljava/util/SimpleTimeZone;", true)); |
| 472 | ASSERT_TRUE(HasAssignable("Ljava/util/TimeZone;", "Ljava/util/SimpleTimeZone;", true)); |
| 473 | } |
| 474 | |
| 475 | TEST_F(VerifierDepsTest, Assignable_BothArrays_Erroneous) { |
| 476 | ASSERT_TRUE(TestAssignabilityRecording(/* dst */ "[[Ljava/util/TimeZone;", |
| 477 | /* src */ "[[LMyErroneousTimeZone;", |
| 478 | /* is_strict */ true, |
| 479 | /* is_assignable */ true)); |
| 480 | // If the component type of an array is erroneous, we record the dependency on |
| 481 | // the array type. |
| 482 | ASSERT_FALSE(HasAssignable("[[Ljava/util/TimeZone;", "[[LMyErroneousTimeZone;", true)); |
| 483 | ASSERT_TRUE(HasAssignable("[Ljava/util/TimeZone;", "[LMyErroneousTimeZone;", true)); |
| 484 | ASSERT_FALSE(HasAssignable("Ljava/util/TimeZone;", "LMyErroneousTimeZone;", true)); |
| 485 | } |
| 486 | |
| 487 | // We test that VerifierDeps does not try to optimize by storing assignability |
| 488 | // of the component types. This is due to the fact that the component type may |
| 489 | // be an erroneous class, even though the array type has resolved status. |
| 490 | |
| 491 | TEST_F(VerifierDepsTest, Assignable_ArrayToInterface1) { |
| 492 | ASSERT_TRUE(TestAssignabilityRecording(/* dst */ "Ljava/io/Serializable;", |
| 493 | /* src */ "[Ljava/util/TimeZone;", |
| 494 | /* is_strict */ true, |
| 495 | /* is_assignable */ true)); |
| 496 | ASSERT_TRUE(HasAssignable("Ljava/io/Serializable;", "[Ljava/util/TimeZone;", true)); |
| 497 | } |
| 498 | |
| 499 | TEST_F(VerifierDepsTest, Assignable_ArrayToInterface2) { |
| 500 | ASSERT_TRUE(TestAssignabilityRecording(/* dst */ "Ljava/io/Serializable;", |
| 501 | /* src */ "[LMyThreadSet;", |
| 502 | /* is_strict */ true, |
| 503 | /* is_assignable */ true)); |
| 504 | ASSERT_TRUE(HasAssignable("Ljava/io/Serializable;", "[LMyThreadSet;", true)); |
| 505 | } |
| 506 | |
| 507 | TEST_F(VerifierDepsTest, NotAssignable_BothInBoot) { |
| 508 | ASSERT_TRUE(TestAssignabilityRecording(/* dst */ "Ljava/lang/Exception;", |
| 509 | /* src */ "Ljava/util/SimpleTimeZone;", |
| 510 | /* is_strict */ true, |
| 511 | /* is_assignable */ false)); |
| 512 | ASSERT_TRUE(HasAssignable("Ljava/lang/Exception;", "Ljava/util/SimpleTimeZone;", false)); |
| 513 | } |
| 514 | |
| 515 | TEST_F(VerifierDepsTest, NotAssignable_DestinationInBoot1) { |
| 516 | ASSERT_TRUE(TestAssignabilityRecording(/* dst */ "Ljava/lang/Exception;", |
| 517 | /* src */ "LMySSLSocket;", |
| 518 | /* is_strict */ true, |
| 519 | /* is_assignable */ false)); |
| 520 | ASSERT_TRUE(HasAssignable("Ljava/lang/Exception;", "LMySSLSocket;", false)); |
| 521 | } |
| 522 | |
| 523 | TEST_F(VerifierDepsTest, NotAssignable_DestinationInBoot2) { |
| 524 | ASSERT_TRUE(TestAssignabilityRecording(/* dst */ "Ljava/lang/Exception;", |
| 525 | /* src */ "LMySimpleTimeZone;", |
| 526 | /* is_strict */ true, |
| 527 | /* is_assignable */ false)); |
| 528 | ASSERT_TRUE(HasAssignable("Ljava/lang/Exception;", "LMySimpleTimeZone;", false)); |
| 529 | } |
| 530 | |
| 531 | TEST_F(VerifierDepsTest, NotAssignable_BothArrays) { |
| 532 | ASSERT_TRUE(TestAssignabilityRecording(/* dst */ "[Ljava/lang/Exception;", |
| 533 | /* src */ "[Ljava/util/SimpleTimeZone;", |
| 534 | /* is_strict */ true, |
| 535 | /* is_assignable */ false)); |
| 536 | ASSERT_TRUE(HasAssignable("Ljava/lang/Exception;", "Ljava/util/SimpleTimeZone;", false)); |
| 537 | } |
| 538 | |
| 539 | TEST_F(VerifierDepsTest, ArgumentType_ResolvedClass) { |
| 540 | ASSERT_TRUE(VerifyMethod("ArgumentType_ResolvedClass")); |
| 541 | ASSERT_TRUE(HasClass("Ljava/lang/Thread;", true, "public")); |
| 542 | } |
| 543 | |
| 544 | TEST_F(VerifierDepsTest, ArgumentType_ResolvedReferenceArray) { |
| 545 | ASSERT_TRUE(VerifyMethod("ArgumentType_ResolvedReferenceArray")); |
| 546 | ASSERT_TRUE(HasClass("[Ljava/lang/Thread;", true, "public final abstract")); |
| 547 | } |
| 548 | |
| 549 | TEST_F(VerifierDepsTest, ArgumentType_ResolvedPrimitiveArray) { |
| 550 | ASSERT_TRUE(VerifyMethod("ArgumentType_ResolvedPrimitiveArray")); |
| 551 | ASSERT_TRUE(HasClass("[B", true, "public final abstract")); |
| 552 | } |
| 553 | |
| 554 | TEST_F(VerifierDepsTest, ArgumentType_UnresolvedClass) { |
| 555 | ASSERT_TRUE(VerifyMethod("ArgumentType_UnresolvedClass")); |
| 556 | ASSERT_TRUE(HasClass("LUnresolvedClass;", false)); |
| 557 | } |
| 558 | |
| 559 | TEST_F(VerifierDepsTest, ArgumentType_UnresolvedSuper) { |
| 560 | ASSERT_TRUE(VerifyMethod("ArgumentType_UnresolvedSuper")); |
| 561 | ASSERT_TRUE(HasClass("LMySetWithUnresolvedSuper;", false)); |
| 562 | } |
| 563 | |
| 564 | TEST_F(VerifierDepsTest, ReturnType_Reference) { |
| 565 | ASSERT_TRUE(VerifyMethod("ReturnType_Reference")); |
| 566 | ASSERT_TRUE(HasAssignable("Ljava/lang/Throwable;", "Ljava/lang/IllegalStateException;", true)); |
| 567 | } |
| 568 | |
| 569 | TEST_F(VerifierDepsTest, ReturnType_Array) { |
| 570 | ASSERT_FALSE(VerifyMethod("ReturnType_Array")); |
| 571 | ASSERT_TRUE(HasAssignable("Ljava/lang/Integer;", "Ljava/lang/IllegalStateException;", false)); |
| 572 | } |
| 573 | |
| 574 | TEST_F(VerifierDepsTest, InvokeArgumentType) { |
| 575 | ASSERT_TRUE(VerifyMethod("InvokeArgumentType")); |
| 576 | ASSERT_TRUE(HasClass("Ljava/text/SimpleDateFormat;", true, "public")); |
| 577 | ASSERT_TRUE(HasClass("Ljava/util/SimpleTimeZone;", true, "public")); |
| 578 | ASSERT_TRUE(HasMethod("virtual", |
| 579 | "Ljava/text/SimpleDateFormat;", |
| 580 | "setTimeZone", |
| 581 | "(Ljava/util/TimeZone;)V", |
| 582 | true, |
| 583 | "public", |
| 584 | "Ljava/text/DateFormat;")); |
| 585 | ASSERT_TRUE(HasAssignable("Ljava/util/TimeZone;", "Ljava/util/SimpleTimeZone;", true)); |
| 586 | } |
| 587 | |
| 588 | TEST_F(VerifierDepsTest, MergeTypes_RegisterLines) { |
| 589 | ASSERT_TRUE(VerifyMethod("MergeTypes_RegisterLines")); |
| 590 | ASSERT_TRUE(HasAssignable("Ljava/lang/Exception;", "LMySocketTimeoutException;", true)); |
| 591 | ASSERT_TRUE(HasAssignable( |
| 592 | "Ljava/lang/Exception;", "Ljava/util/concurrent/TimeoutException;", true)); |
| 593 | } |
| 594 | |
| 595 | TEST_F(VerifierDepsTest, MergeTypes_IfInstanceOf) { |
| 596 | ASSERT_TRUE(VerifyMethod("MergeTypes_IfInstanceOf")); |
| 597 | ASSERT_TRUE(HasAssignable("Ljava/lang/Exception;", "Ljava/net/SocketTimeoutException;", true)); |
| 598 | ASSERT_TRUE(HasAssignable( |
| 599 | "Ljava/lang/Exception;", "Ljava/util/concurrent/TimeoutException;", true)); |
| 600 | ASSERT_TRUE(HasAssignable("Ljava/net/SocketTimeoutException;", "Ljava/lang/Exception;", false)); |
| 601 | } |
| 602 | |
| 603 | TEST_F(VerifierDepsTest, MergeTypes_Unresolved) { |
| 604 | ASSERT_TRUE(VerifyMethod("MergeTypes_Unresolved")); |
| 605 | ASSERT_TRUE(HasAssignable("Ljava/lang/Exception;", "Ljava/net/SocketTimeoutException;", true)); |
| 606 | ASSERT_TRUE(HasAssignable( |
| 607 | "Ljava/lang/Exception;", "Ljava/util/concurrent/TimeoutException;", true)); |
| 608 | } |
| 609 | |
| 610 | TEST_F(VerifierDepsTest, ConstClass_Resolved) { |
| 611 | ASSERT_TRUE(VerifyMethod("ConstClass_Resolved")); |
| 612 | ASSERT_TRUE(HasClass("Ljava/lang/IllegalStateException;", true, "public")); |
| 613 | } |
| 614 | |
| 615 | TEST_F(VerifierDepsTest, ConstClass_Unresolved) { |
| 616 | ASSERT_TRUE(VerifyMethod("ConstClass_Unresolved")); |
| 617 | ASSERT_TRUE(HasClass("LUnresolvedClass;", false)); |
| 618 | } |
| 619 | |
| 620 | TEST_F(VerifierDepsTest, CheckCast_Resolved) { |
| 621 | ASSERT_TRUE(VerifyMethod("CheckCast_Resolved")); |
| 622 | ASSERT_TRUE(HasClass("Ljava/lang/IllegalStateException;", true, "public")); |
| 623 | } |
| 624 | |
| 625 | TEST_F(VerifierDepsTest, CheckCast_Unresolved) { |
| 626 | ASSERT_TRUE(VerifyMethod("CheckCast_Unresolved")); |
| 627 | ASSERT_TRUE(HasClass("LUnresolvedClass;", false)); |
| 628 | } |
| 629 | |
| 630 | TEST_F(VerifierDepsTest, InstanceOf_Resolved) { |
| 631 | ASSERT_TRUE(VerifyMethod("InstanceOf_Resolved")); |
| 632 | ASSERT_TRUE(HasClass("Ljava/lang/IllegalStateException;", true, "public")); |
| 633 | } |
| 634 | |
| 635 | TEST_F(VerifierDepsTest, InstanceOf_Unresolved) { |
| 636 | ASSERT_TRUE(VerifyMethod("InstanceOf_Unresolved")); |
| 637 | ASSERT_TRUE(HasClass("LUnresolvedClass;", false)); |
| 638 | } |
| 639 | |
| 640 | TEST_F(VerifierDepsTest, NewInstance_Resolved) { |
| 641 | ASSERT_TRUE(VerifyMethod("NewInstance_Resolved")); |
| 642 | ASSERT_TRUE(HasClass("Ljava/lang/IllegalStateException;", true, "public")); |
| 643 | } |
| 644 | |
| 645 | TEST_F(VerifierDepsTest, NewInstance_Unresolved) { |
| 646 | ASSERT_TRUE(VerifyMethod("NewInstance_Unresolved")); |
| 647 | ASSERT_TRUE(HasClass("LUnresolvedClass;", false)); |
| 648 | } |
| 649 | |
| 650 | TEST_F(VerifierDepsTest, NewArray_Resolved) { |
| 651 | ASSERT_TRUE(VerifyMethod("NewArray_Resolved")); |
| 652 | ASSERT_TRUE(HasClass("[Ljava/lang/IllegalStateException;", true, "public final abstract")); |
| 653 | } |
| 654 | |
| 655 | TEST_F(VerifierDepsTest, NewArray_Unresolved) { |
| 656 | ASSERT_TRUE(VerifyMethod("NewArray_Unresolved")); |
| 657 | ASSERT_TRUE(HasClass("[LUnresolvedClass;", false)); |
| 658 | } |
| 659 | |
| 660 | TEST_F(VerifierDepsTest, Throw) { |
| 661 | ASSERT_TRUE(VerifyMethod("Throw")); |
| 662 | ASSERT_TRUE(HasAssignable("Ljava/lang/Throwable;", "Ljava/lang/IllegalStateException;", true)); |
| 663 | } |
| 664 | |
| 665 | TEST_F(VerifierDepsTest, MoveException_Resolved) { |
| 666 | ASSERT_TRUE(VerifyMethod("MoveException_Resolved")); |
| 667 | ASSERT_TRUE(HasClass("Ljava/io/InterruptedIOException;", true, "public")); |
| 668 | ASSERT_TRUE(HasClass("Ljava/net/SocketTimeoutException;", true, "public")); |
| 669 | ASSERT_TRUE(HasClass("Ljava/util/zip/ZipException;", true, "public")); |
| 670 | |
| 671 | // Testing that all exception types are assignable to Throwable. |
| 672 | ASSERT_TRUE(HasAssignable("Ljava/lang/Throwable;", "Ljava/io/InterruptedIOException;", true)); |
| 673 | ASSERT_TRUE(HasAssignable("Ljava/lang/Throwable;", "Ljava/net/SocketTimeoutException;", true)); |
| 674 | ASSERT_TRUE(HasAssignable("Ljava/lang/Throwable;", "Ljava/util/zip/ZipException;", true)); |
| 675 | |
| 676 | // Testing that the merge type is assignable to Throwable. |
| 677 | ASSERT_TRUE(HasAssignable("Ljava/lang/Throwable;", "Ljava/io/IOException;", true)); |
| 678 | |
| 679 | // Merging of exception types. |
| 680 | ASSERT_TRUE(HasAssignable("Ljava/io/IOException;", "Ljava/io/InterruptedIOException;", true)); |
| 681 | ASSERT_TRUE(HasAssignable("Ljava/io/IOException;", "Ljava/util/zip/ZipException;", true)); |
| 682 | ASSERT_TRUE(HasAssignable( |
| 683 | "Ljava/io/InterruptedIOException;", "Ljava/net/SocketTimeoutException;", true)); |
| 684 | } |
| 685 | |
| 686 | TEST_F(VerifierDepsTest, MoveException_Unresolved) { |
| 687 | ASSERT_FALSE(VerifyMethod("MoveException_Unresolved")); |
| 688 | ASSERT_TRUE(HasClass("LUnresolvedException;", false)); |
| 689 | } |
| 690 | |
| 691 | TEST_F(VerifierDepsTest, StaticField_Resolved_DeclaredInReferenced) { |
| 692 | ASSERT_TRUE(VerifyMethod("StaticField_Resolved_DeclaredInReferenced")); |
| 693 | ASSERT_TRUE(HasClass("Ljava/lang/System;", true, "public final")); |
| 694 | ASSERT_TRUE(HasField("Ljava/lang/System;", |
| 695 | "out", |
| 696 | "Ljava/io/PrintStream;", |
| 697 | true, |
| 698 | "public final static", |
| 699 | "Ljava/lang/System;")); |
| 700 | } |
| 701 | |
| 702 | TEST_F(VerifierDepsTest, StaticField_Resolved_DeclaredInSuperclass1) { |
| 703 | ASSERT_TRUE(VerifyMethod("StaticField_Resolved_DeclaredInSuperclass1")); |
| 704 | ASSERT_TRUE(HasClass("Ljava/util/SimpleTimeZone;", true, "public")); |
| 705 | ASSERT_TRUE(HasField( |
| 706 | "Ljava/util/SimpleTimeZone;", "LONG", "I", true, "public final static", "Ljava/util/TimeZone;")); |
| 707 | } |
| 708 | |
| 709 | TEST_F(VerifierDepsTest, StaticField_Resolved_DeclaredInSuperclass2) { |
| 710 | ASSERT_TRUE(VerifyMethod("StaticField_Resolved_DeclaredInSuperclass2")); |
| 711 | ASSERT_TRUE(HasField( |
| 712 | "LMySimpleTimeZone;", "SHORT", "I", true, "public final static", "Ljava/util/TimeZone;")); |
| 713 | } |
| 714 | |
| 715 | TEST_F(VerifierDepsTest, StaticField_Resolved_DeclaredInInterface1) { |
| 716 | ASSERT_TRUE(VerifyMethod("StaticField_Resolved_DeclaredInInterface1")); |
| 717 | ASSERT_TRUE(HasClass("Ljavax/xml/transform/dom/DOMResult;", true, "public")); |
| 718 | ASSERT_TRUE(HasField("Ljavax/xml/transform/dom/DOMResult;", |
| 719 | "PI_ENABLE_OUTPUT_ESCAPING", |
| 720 | "Ljava/lang/String;", |
| 721 | true, |
| 722 | "public final static", |
| 723 | "Ljavax/xml/transform/Result;")); |
| 724 | } |
| 725 | |
| 726 | TEST_F(VerifierDepsTest, StaticField_Resolved_DeclaredInInterface2) { |
| 727 | ASSERT_TRUE(VerifyMethod("StaticField_Resolved_DeclaredInInterface2")); |
| 728 | ASSERT_TRUE(HasField("LMyDOMResult;", |
| 729 | "PI_ENABLE_OUTPUT_ESCAPING", |
| 730 | "Ljava/lang/String;", |
| 731 | true, |
| 732 | "public final static", |
| 733 | "Ljavax/xml/transform/Result;")); |
| 734 | } |
| 735 | |
| 736 | TEST_F(VerifierDepsTest, StaticField_Resolved_DeclaredInInterface3) { |
| 737 | ASSERT_TRUE(VerifyMethod("StaticField_Resolved_DeclaredInInterface3")); |
| 738 | ASSERT_TRUE(HasField("LMyResult;", |
| 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_DeclaredInInterface4) { |
| 747 | ASSERT_TRUE(VerifyMethod("StaticField_Resolved_DeclaredInInterface4")); |
| 748 | ASSERT_TRUE(HasField("LMyDocument;", |
| 749 | "ELEMENT_NODE", |
| 750 | "S", |
| 751 | true, |
| 752 | "public final static", |
| 753 | "Lorg/w3c/dom/Node;")); |
| 754 | } |
| 755 | |
| 756 | TEST_F(VerifierDepsTest, StaticField_Unresolved_ReferrerInBoot) { |
| 757 | ASSERT_TRUE(VerifyMethod("StaticField_Unresolved_ReferrerInBoot")); |
| 758 | ASSERT_TRUE(HasClass("Ljava/util/TimeZone;", true, "public abstract")); |
| 759 | ASSERT_TRUE(HasField("Ljava/util/TimeZone;", "x", "I", false)); |
| 760 | } |
| 761 | |
| 762 | TEST_F(VerifierDepsTest, StaticField_Unresolved_ReferrerInDex) { |
| 763 | ASSERT_TRUE(VerifyMethod("StaticField_Unresolved_ReferrerInDex")); |
| 764 | ASSERT_TRUE(HasField("LMyThreadSet;", "x", "I", false)); |
| 765 | } |
| 766 | |
| 767 | TEST_F(VerifierDepsTest, InstanceField_Resolved_DeclaredInReferenced) { |
| 768 | ASSERT_TRUE(VerifyMethod("InstanceField_Resolved_DeclaredInReferenced")); |
| 769 | ASSERT_TRUE(HasClass("Ljava/io/InterruptedIOException;", true, "public")); |
| 770 | ASSERT_TRUE(HasField("Ljava/io/InterruptedIOException;", |
| 771 | "bytesTransferred", |
| 772 | "I", |
| 773 | true, |
| 774 | "public", |
| 775 | "Ljava/io/InterruptedIOException;")); |
| 776 | ASSERT_TRUE(HasAssignable( |
| 777 | "Ljava/io/InterruptedIOException;", "LMySocketTimeoutException;", true)); |
| 778 | } |
| 779 | |
| 780 | TEST_F(VerifierDepsTest, InstanceField_Resolved_DeclaredInSuperclass1) { |
| 781 | ASSERT_TRUE(VerifyMethod("InstanceField_Resolved_DeclaredInSuperclass1")); |
| 782 | ASSERT_TRUE(HasClass("Ljava/net/SocketTimeoutException;", true, "public")); |
| 783 | ASSERT_TRUE(HasField("Ljava/net/SocketTimeoutException;", |
| 784 | "bytesTransferred", |
| 785 | "I", |
| 786 | true, |
| 787 | "public", |
| 788 | "Ljava/io/InterruptedIOException;")); |
| 789 | ASSERT_TRUE(HasAssignable( |
| 790 | "Ljava/io/InterruptedIOException;", "LMySocketTimeoutException;", true)); |
| 791 | } |
| 792 | |
| 793 | TEST_F(VerifierDepsTest, InstanceField_Resolved_DeclaredInSuperclass2) { |
| 794 | ASSERT_TRUE(VerifyMethod("InstanceField_Resolved_DeclaredInSuperclass2")); |
| 795 | ASSERT_TRUE(HasField("LMySocketTimeoutException;", |
| 796 | "bytesTransferred", |
| 797 | "I", |
| 798 | true, |
| 799 | "public", |
| 800 | "Ljava/io/InterruptedIOException;")); |
| 801 | ASSERT_TRUE(HasAssignable( |
| 802 | "Ljava/io/InterruptedIOException;", "LMySocketTimeoutException;", true)); |
| 803 | } |
| 804 | |
| 805 | TEST_F(VerifierDepsTest, InstanceField_Unresolved_ReferrerInBoot) { |
| 806 | ASSERT_TRUE(VerifyMethod("InstanceField_Unresolved_ReferrerInBoot")); |
| 807 | ASSERT_TRUE(HasClass("Ljava/io/InterruptedIOException;", true, "public")); |
| 808 | ASSERT_TRUE(HasField("Ljava/io/InterruptedIOException;", "x", "I", false)); |
| 809 | } |
| 810 | |
| 811 | TEST_F(VerifierDepsTest, InstanceField_Unresolved_ReferrerInDex) { |
| 812 | ASSERT_TRUE(VerifyMethod("InstanceField_Unresolved_ReferrerInDex")); |
| 813 | ASSERT_TRUE(HasField("LMyThreadSet;", "x", "I", false)); |
| 814 | } |
| 815 | |
| 816 | TEST_F(VerifierDepsTest, InvokeStatic_Resolved_DeclaredInReferenced) { |
| 817 | ASSERT_TRUE(VerifyMethod("InvokeStatic_Resolved_DeclaredInReferenced")); |
| 818 | ASSERT_TRUE(HasClass("Ljava/net/Socket;", true, "public")); |
| 819 | ASSERT_TRUE(HasMethod("direct", |
| 820 | "Ljava/net/Socket;", |
| 821 | "setSocketImplFactory", |
| 822 | "(Ljava/net/SocketImplFactory;)V", |
| 823 | true, |
| 824 | "public static", |
| 825 | "Ljava/net/Socket;")); |
| 826 | } |
| 827 | |
| 828 | TEST_F(VerifierDepsTest, InvokeStatic_Resolved_DeclaredInSuperclass1) { |
| 829 | ASSERT_TRUE(VerifyMethod("InvokeStatic_Resolved_DeclaredInSuperclass1")); |
| 830 | ASSERT_TRUE(HasClass("Ljavax/net/ssl/SSLSocket;", true, "public abstract")); |
| 831 | ASSERT_TRUE(HasMethod("direct", |
| 832 | "Ljavax/net/ssl/SSLSocket;", |
| 833 | "setSocketImplFactory", |
| 834 | "(Ljava/net/SocketImplFactory;)V", |
| 835 | true, |
| 836 | "public static", |
| 837 | "Ljava/net/Socket;")); |
| 838 | } |
| 839 | |
| 840 | TEST_F(VerifierDepsTest, InvokeStatic_Resolved_DeclaredInSuperclass2) { |
| 841 | ASSERT_TRUE(VerifyMethod("InvokeStatic_Resolved_DeclaredInSuperclass2")); |
| 842 | ASSERT_TRUE(HasMethod("direct", |
| 843 | "LMySSLSocket;", |
| 844 | "setSocketImplFactory", |
| 845 | "(Ljava/net/SocketImplFactory;)V", |
| 846 | true, |
| 847 | "public static", |
| 848 | "Ljava/net/Socket;")); |
| 849 | } |
| 850 | |
| 851 | TEST_F(VerifierDepsTest, InvokeStatic_DeclaredInInterface1) { |
| 852 | ASSERT_TRUE(VerifyMethod("InvokeStatic_DeclaredInInterface1")); |
| 853 | ASSERT_TRUE(HasClass("Ljava/util/Map$Entry;", true, "public abstract interface")); |
| 854 | ASSERT_TRUE(HasMethod("direct", |
| 855 | "Ljava/util/Map$Entry;", |
| 856 | "comparingByKey", |
| 857 | "()Ljava/util/Comparator;", |
| 858 | true, |
| 859 | "public static", |
| 860 | "Ljava/util/Map$Entry;")); |
| 861 | } |
| 862 | |
| 863 | TEST_F(VerifierDepsTest, InvokeStatic_DeclaredInInterface2) { |
| 864 | ASSERT_FALSE(VerifyMethod("InvokeStatic_DeclaredInInterface2")); |
| 865 | ASSERT_TRUE(HasClass("Ljava/util/AbstractMap$SimpleEntry;", true, "public")); |
| 866 | ASSERT_TRUE(HasMethod("direct", |
| 867 | "Ljava/util/AbstractMap$SimpleEntry;", |
| 868 | "comparingByKey", |
| 869 | "()Ljava/util/Comparator;", |
| 870 | false)); |
| 871 | } |
| 872 | |
| 873 | TEST_F(VerifierDepsTest, InvokeStatic_Unresolved1) { |
| 874 | ASSERT_FALSE(VerifyMethod("InvokeStatic_Unresolved1")); |
| 875 | ASSERT_TRUE(HasClass("Ljavax/net/ssl/SSLSocket;", true, "public abstract")); |
| 876 | ASSERT_TRUE(HasMethod("direct", "Ljavax/net/ssl/SSLSocket;", "x", "()V", false)); |
| 877 | } |
| 878 | |
| 879 | TEST_F(VerifierDepsTest, InvokeStatic_Unresolved2) { |
| 880 | ASSERT_FALSE(VerifyMethod("InvokeStatic_Unresolved2")); |
| 881 | ASSERT_TRUE(HasMethod("direct", "LMySSLSocket;", "x", "()V", false)); |
| 882 | } |
| 883 | |
| 884 | TEST_F(VerifierDepsTest, InvokeDirect_Resolved_DeclaredInReferenced) { |
| 885 | ASSERT_TRUE(VerifyMethod("InvokeDirect_Resolved_DeclaredInReferenced")); |
| 886 | ASSERT_TRUE(HasClass("Ljava/net/Socket;", true, "public")); |
| 887 | ASSERT_TRUE(HasMethod( |
| 888 | "direct", "Ljava/net/Socket;", "<init>", "()V", true, "public", "Ljava/net/Socket;")); |
| 889 | } |
| 890 | |
| 891 | TEST_F(VerifierDepsTest, InvokeDirect_Resolved_DeclaredInSuperclass1) { |
| 892 | ASSERT_FALSE(VerifyMethod("InvokeDirect_Resolved_DeclaredInSuperclass1")); |
| 893 | ASSERT_TRUE(HasClass("Ljavax/net/ssl/SSLSocket;", true, "public abstract")); |
| 894 | ASSERT_TRUE(HasMethod("direct", |
| 895 | "Ljavax/net/ssl/SSLSocket;", |
| 896 | "checkOldImpl", |
| 897 | "()V", |
| 898 | true, |
| 899 | "private", |
| 900 | "Ljava/net/Socket;")); |
| 901 | } |
| 902 | |
| 903 | TEST_F(VerifierDepsTest, InvokeDirect_Resolved_DeclaredInSuperclass2) { |
| 904 | ASSERT_FALSE(VerifyMethod("InvokeDirect_Resolved_DeclaredInSuperclass2")); |
| 905 | ASSERT_TRUE(HasMethod( |
| 906 | "direct", "LMySSLSocket;", "checkOldImpl", "()V", true, "private", "Ljava/net/Socket;")); |
| 907 | } |
| 908 | |
| 909 | TEST_F(VerifierDepsTest, InvokeDirect_Unresolved1) { |
| 910 | ASSERT_FALSE(VerifyMethod("InvokeDirect_Unresolved1")); |
| 911 | ASSERT_TRUE(HasClass("Ljavax/net/ssl/SSLSocket;", true, "public abstract")); |
| 912 | ASSERT_TRUE(HasMethod("direct", "Ljavax/net/ssl/SSLSocket;", "x", "()V", false)); |
| 913 | } |
| 914 | |
| 915 | TEST_F(VerifierDepsTest, InvokeDirect_Unresolved2) { |
| 916 | ASSERT_FALSE(VerifyMethod("InvokeDirect_Unresolved2")); |
| 917 | ASSERT_TRUE(HasMethod("direct", "LMySSLSocket;", "x", "()V", false)); |
| 918 | } |
| 919 | |
| 920 | TEST_F(VerifierDepsTest, InvokeVirtual_Resolved_DeclaredInReferenced) { |
| 921 | ASSERT_TRUE(VerifyMethod("InvokeVirtual_Resolved_DeclaredInReferenced")); |
| 922 | ASSERT_TRUE(HasClass("Ljava/lang/Throwable;", true, "public")); |
| 923 | ASSERT_TRUE(HasMethod("virtual", |
| 924 | "Ljava/lang/Throwable;", |
| 925 | "getMessage", |
| 926 | "()Ljava/lang/String;", |
| 927 | true, |
| 928 | "public", |
| 929 | "Ljava/lang/Throwable;")); |
| 930 | // Type dependency on `this` argument. |
| 931 | ASSERT_TRUE(HasAssignable("Ljava/lang/Throwable;", "LMySocketTimeoutException;", true)); |
| 932 | } |
| 933 | |
| 934 | TEST_F(VerifierDepsTest, InvokeVirtual_Resolved_DeclaredInSuperclass1) { |
| 935 | ASSERT_TRUE(VerifyMethod("InvokeVirtual_Resolved_DeclaredInSuperclass1")); |
| 936 | ASSERT_TRUE(HasClass("Ljava/io/InterruptedIOException;", true, "public")); |
| 937 | ASSERT_TRUE(HasMethod("virtual", |
| 938 | "Ljava/io/InterruptedIOException;", |
| 939 | "getMessage", |
| 940 | "()Ljava/lang/String;", |
| 941 | true, |
| 942 | "public", |
| 943 | "Ljava/lang/Throwable;")); |
| 944 | // Type dependency on `this` argument. |
| 945 | ASSERT_TRUE(HasAssignable("Ljava/lang/Throwable;", "LMySocketTimeoutException;", true)); |
| 946 | } |
| 947 | |
| 948 | TEST_F(VerifierDepsTest, InvokeVirtual_Resolved_DeclaredInSuperclass2) { |
| 949 | ASSERT_TRUE(VerifyMethod("InvokeVirtual_Resolved_DeclaredInSuperclass2")); |
| 950 | ASSERT_TRUE(HasMethod("virtual", |
| 951 | "LMySocketTimeoutException;", |
| 952 | "getMessage", |
| 953 | "()Ljava/lang/String;", |
| 954 | true, |
| 955 | "public", |
| 956 | "Ljava/lang/Throwable;")); |
| 957 | } |
| 958 | |
| 959 | TEST_F(VerifierDepsTest, InvokeVirtual_Resolved_DeclaredInSuperinterface) { |
| 960 | ASSERT_TRUE(VerifyMethod("InvokeVirtual_Resolved_DeclaredInSuperinterface")); |
| 961 | ASSERT_TRUE(HasMethod("virtual", |
| 962 | "LMyThreadSet;", |
| 963 | "size", |
| 964 | "()I", |
| 965 | true, |
| 966 | "public abstract", |
| 967 | "Ljava/util/Set;")); |
| 968 | } |
| 969 | |
| 970 | TEST_F(VerifierDepsTest, InvokeVirtual_Unresolved1) { |
| 971 | ASSERT_FALSE(VerifyMethod("InvokeVirtual_Unresolved1")); |
| 972 | ASSERT_TRUE(HasClass("Ljava/io/InterruptedIOException;", true, "public")); |
| 973 | ASSERT_TRUE(HasMethod("virtual", "Ljava/io/InterruptedIOException;", "x", "()V", false)); |
| 974 | } |
| 975 | |
| 976 | TEST_F(VerifierDepsTest, InvokeVirtual_Unresolved2) { |
| 977 | ASSERT_FALSE(VerifyMethod("InvokeVirtual_Unresolved2")); |
| 978 | ASSERT_TRUE(HasMethod("virtual", "LMySocketTimeoutException;", "x", "()V", false)); |
| 979 | } |
| 980 | |
| 981 | TEST_F(VerifierDepsTest, InvokeVirtual_ActuallyDirect) { |
| 982 | ASSERT_FALSE(VerifyMethod("InvokeVirtual_ActuallyDirect")); |
| 983 | ASSERT_TRUE(HasMethod("virtual", "LMyThread;", "activeCount", "()I", false)); |
| 984 | ASSERT_TRUE(HasMethod("direct", |
| 985 | "LMyThread;", |
| 986 | "activeCount", |
| 987 | "()I", |
| 988 | true, |
| 989 | "public static", |
| 990 | "Ljava/lang/Thread;")); |
| 991 | } |
| 992 | |
| 993 | TEST_F(VerifierDepsTest, InvokeInterface_Resolved_DeclaredInReferenced) { |
| 994 | ASSERT_TRUE(VerifyMethod("InvokeInterface_Resolved_DeclaredInReferenced")); |
| 995 | ASSERT_TRUE(HasClass("Ljava/lang/Runnable;", true, "public abstract interface")); |
| 996 | ASSERT_TRUE(HasMethod("interface", |
| 997 | "Ljava/lang/Runnable;", |
| 998 | "run", |
| 999 | "()V", |
| 1000 | true, |
| 1001 | "public abstract", |
| 1002 | "Ljava/lang/Runnable;")); |
| 1003 | } |
| 1004 | |
| 1005 | TEST_F(VerifierDepsTest, InvokeInterface_Resolved_DeclaredInSuperclass) { |
| 1006 | ASSERT_FALSE(VerifyMethod("InvokeInterface_Resolved_DeclaredInSuperclass")); |
| 1007 | ASSERT_TRUE(HasMethod("interface", "LMyThread;", "join", "()V", false)); |
| 1008 | } |
| 1009 | |
| 1010 | TEST_F(VerifierDepsTest, InvokeInterface_Resolved_DeclaredInSuperinterface1) { |
| 1011 | ASSERT_FALSE(VerifyMethod("InvokeInterface_Resolved_DeclaredInSuperinterface1")); |
| 1012 | ASSERT_TRUE(HasMethod("interface", |
| 1013 | "LMyThreadSet;", |
| 1014 | "run", |
| 1015 | "()V", |
| 1016 | true, |
| 1017 | "public abstract", |
| 1018 | "Ljava/lang/Runnable;")); |
| 1019 | } |
| 1020 | |
| 1021 | TEST_F(VerifierDepsTest, InvokeInterface_Resolved_DeclaredInSuperinterface2) { |
| 1022 | ASSERT_FALSE(VerifyMethod("InvokeInterface_Resolved_DeclaredInSuperinterface2")); |
| 1023 | ASSERT_TRUE(HasMethod("interface", |
| 1024 | "LMyThreadSet;", |
| 1025 | "isEmpty", |
| 1026 | "()Z", |
| 1027 | true, |
| 1028 | "public abstract", |
| 1029 | "Ljava/util/Set;")); |
| 1030 | } |
| 1031 | |
| 1032 | TEST_F(VerifierDepsTest, InvokeInterface_Unresolved1) { |
| 1033 | ASSERT_FALSE(VerifyMethod("InvokeInterface_Unresolved1")); |
| 1034 | ASSERT_TRUE(HasClass("Ljava/lang/Runnable;", true, "public abstract interface")); |
| 1035 | ASSERT_TRUE(HasMethod("interface", "Ljava/lang/Runnable;", "x", "()V", false)); |
| 1036 | } |
| 1037 | |
| 1038 | TEST_F(VerifierDepsTest, InvokeInterface_Unresolved2) { |
| 1039 | ASSERT_FALSE(VerifyMethod("InvokeInterface_Unresolved2")); |
| 1040 | ASSERT_TRUE(HasMethod("interface", "LMyThreadSet;", "x", "()V", false)); |
| 1041 | } |
| 1042 | |
| 1043 | TEST_F(VerifierDepsTest, InvokeSuper_ThisAssignable) { |
| 1044 | ASSERT_TRUE(VerifyMethod("InvokeSuper_ThisAssignable")); |
| 1045 | ASSERT_TRUE(HasClass("Ljava/lang/Runnable;", true, "public abstract interface")); |
| 1046 | ASSERT_TRUE(HasAssignable("Ljava/lang/Runnable;", "LMain;", true)); |
| 1047 | ASSERT_TRUE(HasMethod("interface", |
| 1048 | "Ljava/lang/Runnable;", |
| 1049 | "run", |
| 1050 | "()V", |
| 1051 | true, |
| 1052 | "public abstract", |
| 1053 | "Ljava/lang/Runnable;")); |
| 1054 | } |
| 1055 | |
| 1056 | TEST_F(VerifierDepsTest, InvokeSuper_ThisNotAssignable) { |
| 1057 | ASSERT_FALSE(VerifyMethod("InvokeSuper_ThisNotAssignable")); |
| 1058 | ASSERT_TRUE(HasClass("Ljava/lang/Integer;", true, "public final")); |
| 1059 | ASSERT_TRUE(HasAssignable("Ljava/lang/Integer;", "LMain;", false)); |
| 1060 | ASSERT_TRUE(HasMethod( |
| 1061 | "virtual", "Ljava/lang/Integer;", "intValue", "()I", true, "public", "Ljava/lang/Integer;")); |
| 1062 | } |
| 1063 | |
David Brazdil | 6f82fbd | 2016-09-14 11:55:26 +0100 | [diff] [blame] | 1064 | TEST_F(VerifierDepsTest, EncodeDecode) { |
| 1065 | VerifyDexFile(); |
| 1066 | |
| 1067 | ASSERT_EQ(1u, NumberOfCompiledDexFiles()); |
| 1068 | ASSERT_TRUE(HasEachKindOfRecord()); |
| 1069 | |
| 1070 | std::vector<uint8_t> buffer; |
| 1071 | verifier_deps_->Encode(&buffer); |
| 1072 | ASSERT_FALSE(buffer.empty()); |
| 1073 | |
| 1074 | VerifierDeps decoded_deps({ dex_file_ }, ArrayRef<uint8_t>(buffer)); |
| 1075 | ASSERT_TRUE(verifier_deps_->Equals(decoded_deps)); |
| 1076 | } |
| 1077 | |
Nicolas Geoffray | 0802518 | 2016-10-25 17:20:18 +0100 | [diff] [blame^] | 1078 | TEST_F(VerifierDepsTest, UnverifiedClasses) { |
| 1079 | VerifyDexFile(); |
| 1080 | ASSERT_FALSE(HasUnverifiedClass("LMyThread;")); |
| 1081 | // Test that a class with a soft failure is recorded. |
| 1082 | ASSERT_TRUE(HasUnverifiedClass("LMain;")); |
| 1083 | // Test that a class with hard failure is recorded. |
| 1084 | ASSERT_TRUE(HasUnverifiedClass("LMyVerificationFailure;")); |
| 1085 | // Test that a class with unresolved super is recorded. |
| 1086 | ASSERT_FALSE(HasUnverifiedClass("LMyClassWithNoSuper;")); |
| 1087 | // Test that a class with unresolved super and hard failure is recorded. |
| 1088 | ASSERT_TRUE(HasUnverifiedClass("LMyClassWithNoSuperButFailures;")); |
| 1089 | } |
| 1090 | |
David Brazdil | ca3c8c3 | 2016-09-06 14:04:48 +0100 | [diff] [blame] | 1091 | } // namespace verifier |
| 1092 | } // namespace art |