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