method_verifier.cc revision dc3761719fb5e2d1ced5708e3c73b965f9ef0c73
1/* 2 * Copyright (C) 2011 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 17#include "method_verifier-inl.h" 18 19#include <iostream> 20 21#include "base/logging.h" 22#include "base/mutex-inl.h" 23#include "class_linker.h" 24#include "compiler_callbacks.h" 25#include "dex_file-inl.h" 26#include "dex_instruction-inl.h" 27#include "dex_instruction_visitor.h" 28#include "field_helper.h" 29#include "gc/accounting/card_table-inl.h" 30#include "indenter.h" 31#include "intern_table.h" 32#include "leb128.h" 33#include "method_helper-inl.h" 34#include "mirror/art_field-inl.h" 35#include "mirror/art_method-inl.h" 36#include "mirror/class.h" 37#include "mirror/class-inl.h" 38#include "mirror/dex_cache-inl.h" 39#include "mirror/object-inl.h" 40#include "mirror/object_array-inl.h" 41#include "register_line-inl.h" 42#include "runtime.h" 43#include "scoped_thread_state_change.h" 44#include "handle_scope-inl.h" 45#include "verifier/dex_gc_map.h" 46 47namespace art { 48namespace verifier { 49 50static constexpr bool gDebugVerify = false; 51// TODO: Add a constant to method_verifier to turn on verbose logging? 52 53void PcToRegisterLineTable::Init(RegisterTrackingMode mode, InstructionFlags* flags, 54 uint32_t insns_size, uint16_t registers_size, 55 MethodVerifier* verifier) { 56 DCHECK_GT(insns_size, 0U); 57 register_lines_.reset(new RegisterLine*[insns_size]()); 58 size_ = insns_size; 59 for (uint32_t i = 0; i < insns_size; i++) { 60 bool interesting = false; 61 switch (mode) { 62 case kTrackRegsAll: 63 interesting = flags[i].IsOpcode(); 64 break; 65 case kTrackCompilerInterestPoints: 66 interesting = flags[i].IsCompileTimeInfoPoint() || flags[i].IsBranchTarget(); 67 break; 68 case kTrackRegsBranches: 69 interesting = flags[i].IsBranchTarget(); 70 break; 71 default: 72 break; 73 } 74 if (interesting) { 75 register_lines_[i] = RegisterLine::Create(registers_size, verifier); 76 } 77 } 78} 79 80PcToRegisterLineTable::~PcToRegisterLineTable() { 81 for (size_t i = 0; i < size_; i++) { 82 delete register_lines_[i]; 83 if (kIsDebugBuild) { 84 register_lines_[i] = nullptr; 85 } 86 } 87} 88 89MethodVerifier::FailureKind MethodVerifier::VerifyClass(mirror::Class* klass, 90 bool allow_soft_failures, 91 std::string* error) { 92 if (klass->IsVerified()) { 93 return kNoFailure; 94 } 95 bool early_failure = false; 96 std::string failure_message; 97 const DexFile& dex_file = klass->GetDexFile(); 98 const DexFile::ClassDef* class_def = klass->GetClassDef(); 99 mirror::Class* super = klass->GetSuperClass(); 100 std::string temp; 101 if (super == NULL && strcmp("Ljava/lang/Object;", klass->GetDescriptor(&temp)) != 0) { 102 early_failure = true; 103 failure_message = " that has no super class"; 104 } else if (super != NULL && super->IsFinal()) { 105 early_failure = true; 106 failure_message = " that attempts to sub-class final class " + PrettyDescriptor(super); 107 } else if (class_def == NULL) { 108 early_failure = true; 109 failure_message = " that isn't present in dex file " + dex_file.GetLocation(); 110 } 111 if (early_failure) { 112 *error = "Verifier rejected class " + PrettyDescriptor(klass) + failure_message; 113 if (Runtime::Current()->IsCompiler()) { 114 ClassReference ref(&dex_file, klass->GetDexClassDefIndex()); 115 Runtime::Current()->GetCompilerCallbacks()->ClassRejected(ref); 116 } 117 return kHardFailure; 118 } 119 StackHandleScope<2> hs(Thread::Current()); 120 Handle<mirror::DexCache> dex_cache(hs.NewHandle(klass->GetDexCache())); 121 Handle<mirror::ClassLoader> class_loader(hs.NewHandle(klass->GetClassLoader())); 122 return VerifyClass(&dex_file, dex_cache, class_loader, class_def, allow_soft_failures, error); 123} 124 125MethodVerifier::FailureKind MethodVerifier::VerifyClass(const DexFile* dex_file, 126 Handle<mirror::DexCache> dex_cache, 127 Handle<mirror::ClassLoader> class_loader, 128 const DexFile::ClassDef* class_def, 129 bool allow_soft_failures, 130 std::string* error) { 131 DCHECK(class_def != nullptr); 132 const byte* class_data = dex_file->GetClassData(*class_def); 133 if (class_data == NULL) { 134 // empty class, probably a marker interface 135 return kNoFailure; 136 } 137 ClassDataItemIterator it(*dex_file, class_data); 138 while (it.HasNextStaticField() || it.HasNextInstanceField()) { 139 it.Next(); 140 } 141 Thread* self = Thread::Current(); 142 size_t error_count = 0; 143 bool hard_fail = false; 144 ClassLinker* linker = Runtime::Current()->GetClassLinker(); 145 int64_t previous_direct_method_idx = -1; 146 while (it.HasNextDirectMethod()) { 147 uint32_t method_idx = it.GetMemberIndex(); 148 if (method_idx == previous_direct_method_idx) { 149 // smali can create dex files with two encoded_methods sharing the same method_idx 150 // http://code.google.com/p/smali/issues/detail?id=119 151 it.Next(); 152 continue; 153 } 154 previous_direct_method_idx = method_idx; 155 InvokeType type = it.GetMethodInvokeType(*class_def); 156 mirror::ArtMethod* method = 157 linker->ResolveMethod(*dex_file, method_idx, dex_cache, class_loader, 158 NullHandle<mirror::ArtMethod>(), type); 159 if (method == NULL) { 160 DCHECK(self->IsExceptionPending()); 161 // We couldn't resolve the method, but continue regardless. 162 self->ClearException(); 163 } 164 StackHandleScope<1> hs(self); 165 Handle<mirror::ArtMethod> h_method(hs.NewHandle(method)); 166 MethodVerifier::FailureKind result = VerifyMethod(method_idx, 167 dex_file, 168 dex_cache, 169 class_loader, 170 class_def, 171 it.GetMethodCodeItem(), 172 h_method, 173 it.GetMemberAccessFlags(), 174 allow_soft_failures, 175 false); 176 if (result != kNoFailure) { 177 if (result == kHardFailure) { 178 hard_fail = true; 179 if (error_count > 0) { 180 *error += "\n"; 181 } 182 *error = "Verifier rejected class "; 183 *error += PrettyDescriptor(dex_file->GetClassDescriptor(*class_def)); 184 *error += " due to bad method "; 185 *error += PrettyMethod(method_idx, *dex_file); 186 } 187 ++error_count; 188 } 189 it.Next(); 190 } 191 int64_t previous_virtual_method_idx = -1; 192 while (it.HasNextVirtualMethod()) { 193 uint32_t method_idx = it.GetMemberIndex(); 194 if (method_idx == previous_virtual_method_idx) { 195 // smali can create dex files with two encoded_methods sharing the same method_idx 196 // http://code.google.com/p/smali/issues/detail?id=119 197 it.Next(); 198 continue; 199 } 200 previous_virtual_method_idx = method_idx; 201 InvokeType type = it.GetMethodInvokeType(*class_def); 202 mirror::ArtMethod* method = 203 linker->ResolveMethod(*dex_file, method_idx, dex_cache, class_loader, 204 NullHandle<mirror::ArtMethod>(), type); 205 if (method == NULL) { 206 DCHECK(self->IsExceptionPending()); 207 // We couldn't resolve the method, but continue regardless. 208 self->ClearException(); 209 } 210 StackHandleScope<1> hs(self); 211 Handle<mirror::ArtMethod> h_method(hs.NewHandle(method)); 212 MethodVerifier::FailureKind result = VerifyMethod(method_idx, 213 dex_file, 214 dex_cache, 215 class_loader, 216 class_def, 217 it.GetMethodCodeItem(), 218 h_method, 219 it.GetMemberAccessFlags(), 220 allow_soft_failures, 221 false); 222 if (result != kNoFailure) { 223 if (result == kHardFailure) { 224 hard_fail = true; 225 if (error_count > 0) { 226 *error += "\n"; 227 } 228 *error = "Verifier rejected class "; 229 *error += PrettyDescriptor(dex_file->GetClassDescriptor(*class_def)); 230 *error += " due to bad method "; 231 *error += PrettyMethod(method_idx, *dex_file); 232 } 233 ++error_count; 234 } 235 it.Next(); 236 } 237 if (error_count == 0) { 238 return kNoFailure; 239 } else { 240 return hard_fail ? kHardFailure : kSoftFailure; 241 } 242} 243 244MethodVerifier::FailureKind MethodVerifier::VerifyMethod(uint32_t method_idx, 245 const DexFile* dex_file, 246 Handle<mirror::DexCache> dex_cache, 247 Handle<mirror::ClassLoader> class_loader, 248 const DexFile::ClassDef* class_def, 249 const DexFile::CodeItem* code_item, 250 Handle<mirror::ArtMethod> method, 251 uint32_t method_access_flags, 252 bool allow_soft_failures, 253 bool need_precise_constants) { 254 MethodVerifier::FailureKind result = kNoFailure; 255 uint64_t start_ns = NanoTime(); 256 257 MethodVerifier verifier(dex_file, &dex_cache, &class_loader, class_def, code_item, 258 method_idx, method, method_access_flags, true, allow_soft_failures, 259 need_precise_constants); 260 if (verifier.Verify()) { 261 // Verification completed, however failures may be pending that didn't cause the verification 262 // to hard fail. 263 CHECK(!verifier.have_pending_hard_failure_); 264 if (verifier.failures_.size() != 0) { 265 if (VLOG_IS_ON(verifier)) { 266 verifier.DumpFailures(VLOG_STREAM(verifier) << "Soft verification failures in " 267 << PrettyMethod(method_idx, *dex_file) << "\n"); 268 } 269 result = kSoftFailure; 270 } 271 } else { 272 // Bad method data. 273 CHECK_NE(verifier.failures_.size(), 0U); 274 CHECK(verifier.have_pending_hard_failure_); 275 verifier.DumpFailures(LOG(INFO) << "Verification error in " 276 << PrettyMethod(method_idx, *dex_file) << "\n"); 277 if (gDebugVerify) { 278 std::cout << "\n" << verifier.info_messages_.str(); 279 verifier.Dump(std::cout); 280 } 281 result = kHardFailure; 282 } 283 uint64_t duration_ns = NanoTime() - start_ns; 284 if (duration_ns > MsToNs(100) && !kIsDebugBuild) { 285 LOG(WARNING) << "Verification of " << PrettyMethod(method_idx, *dex_file) 286 << " took " << PrettyDuration(duration_ns); 287 } 288 return result; 289} 290 291void MethodVerifier::VerifyMethodAndDump(std::ostream& os, uint32_t dex_method_idx, 292 const DexFile* dex_file, 293 Handle<mirror::DexCache> dex_cache, 294 Handle<mirror::ClassLoader> class_loader, 295 const DexFile::ClassDef* class_def, 296 const DexFile::CodeItem* code_item, 297 Handle<mirror::ArtMethod> method, 298 uint32_t method_access_flags) { 299 MethodVerifier verifier(dex_file, &dex_cache, &class_loader, class_def, code_item, 300 dex_method_idx, method, method_access_flags, true, true, true); 301 verifier.Verify(); 302 verifier.DumpFailures(os); 303 os << verifier.info_messages_.str(); 304 verifier.Dump(os); 305} 306 307MethodVerifier::MethodVerifier(const DexFile* dex_file, Handle<mirror::DexCache>* dex_cache, 308 Handle<mirror::ClassLoader>* class_loader, 309 const DexFile::ClassDef* class_def, 310 const DexFile::CodeItem* code_item, uint32_t dex_method_idx, 311 Handle<mirror::ArtMethod> method, uint32_t method_access_flags, 312 bool can_load_classes, bool allow_soft_failures, 313 bool need_precise_constants) 314 : reg_types_(can_load_classes), 315 work_insn_idx_(-1), 316 dex_method_idx_(dex_method_idx), 317 mirror_method_(method), 318 method_access_flags_(method_access_flags), 319 return_type_(nullptr), 320 dex_file_(dex_file), 321 dex_cache_(dex_cache), 322 class_loader_(class_loader), 323 class_def_(class_def), 324 code_item_(code_item), 325 declaring_class_(NULL), 326 interesting_dex_pc_(-1), 327 monitor_enter_dex_pcs_(nullptr), 328 have_pending_hard_failure_(false), 329 have_pending_runtime_throw_failure_(false), 330 new_instance_count_(0), 331 monitor_enter_count_(0), 332 can_load_classes_(can_load_classes), 333 allow_soft_failures_(allow_soft_failures), 334 need_precise_constants_(need_precise_constants), 335 has_check_casts_(false), 336 has_virtual_or_interface_invokes_(false) { 337 Runtime::Current()->AddMethodVerifier(this); 338 DCHECK(class_def != nullptr); 339} 340 341MethodVerifier::~MethodVerifier() { 342 Runtime::Current()->RemoveMethodVerifier(this); 343 STLDeleteElements(&failure_messages_); 344} 345 346void MethodVerifier::FindLocksAtDexPc(mirror::ArtMethod* m, uint32_t dex_pc, 347 std::vector<uint32_t>* monitor_enter_dex_pcs) { 348 StackHandleScope<3> hs(Thread::Current()); 349 Handle<mirror::DexCache> dex_cache(hs.NewHandle(m->GetDexCache())); 350 Handle<mirror::ClassLoader> class_loader(hs.NewHandle(m->GetClassLoader())); 351 Handle<mirror::ArtMethod> method(hs.NewHandle(m)); 352 MethodVerifier verifier(m->GetDexFile(), &dex_cache, &class_loader, &m->GetClassDef(), 353 m->GetCodeItem(), m->GetDexMethodIndex(), method, m->GetAccessFlags(), 354 false, true, false); 355 verifier.interesting_dex_pc_ = dex_pc; 356 verifier.monitor_enter_dex_pcs_ = monitor_enter_dex_pcs; 357 verifier.FindLocksAtDexPc(); 358} 359 360void MethodVerifier::FindLocksAtDexPc() { 361 CHECK(monitor_enter_dex_pcs_ != NULL); 362 CHECK(code_item_ != NULL); // This only makes sense for methods with code. 363 364 // Strictly speaking, we ought to be able to get away with doing a subset of the full method 365 // verification. In practice, the phase we want relies on data structures set up by all the 366 // earlier passes, so we just run the full method verification and bail out early when we've 367 // got what we wanted. 368 Verify(); 369} 370 371mirror::ArtField* MethodVerifier::FindAccessedFieldAtDexPc(mirror::ArtMethod* m, 372 uint32_t dex_pc) { 373 StackHandleScope<3> hs(Thread::Current()); 374 Handle<mirror::DexCache> dex_cache(hs.NewHandle(m->GetDexCache())); 375 Handle<mirror::ClassLoader> class_loader(hs.NewHandle(m->GetClassLoader())); 376 Handle<mirror::ArtMethod> method(hs.NewHandle(m)); 377 MethodVerifier verifier(m->GetDexFile(), &dex_cache, &class_loader, &m->GetClassDef(), 378 m->GetCodeItem(), m->GetDexMethodIndex(), method, m->GetAccessFlags(), 379 true, true, false); 380 return verifier.FindAccessedFieldAtDexPc(dex_pc); 381} 382 383mirror::ArtField* MethodVerifier::FindAccessedFieldAtDexPc(uint32_t dex_pc) { 384 CHECK(code_item_ != NULL); // This only makes sense for methods with code. 385 386 // Strictly speaking, we ought to be able to get away with doing a subset of the full method 387 // verification. In practice, the phase we want relies on data structures set up by all the 388 // earlier passes, so we just run the full method verification and bail out early when we've 389 // got what we wanted. 390 bool success = Verify(); 391 if (!success) { 392 return nullptr; 393 } 394 RegisterLine* register_line = reg_table_.GetLine(dex_pc); 395 if (register_line == NULL) { 396 return nullptr; 397 } 398 const Instruction* inst = Instruction::At(code_item_->insns_ + dex_pc); 399 return GetQuickFieldAccess(inst, register_line); 400} 401 402mirror::ArtMethod* MethodVerifier::FindInvokedMethodAtDexPc(mirror::ArtMethod* m, 403 uint32_t dex_pc) { 404 StackHandleScope<3> hs(Thread::Current()); 405 Handle<mirror::DexCache> dex_cache(hs.NewHandle(m->GetDexCache())); 406 Handle<mirror::ClassLoader> class_loader(hs.NewHandle(m->GetClassLoader())); 407 Handle<mirror::ArtMethod> method(hs.NewHandle(m)); 408 MethodVerifier verifier(m->GetDexFile(), &dex_cache, &class_loader, &m->GetClassDef(), 409 m->GetCodeItem(), m->GetDexMethodIndex(), method, m->GetAccessFlags(), 410 true, true, false); 411 return verifier.FindInvokedMethodAtDexPc(dex_pc); 412} 413 414mirror::ArtMethod* MethodVerifier::FindInvokedMethodAtDexPc(uint32_t dex_pc) { 415 CHECK(code_item_ != NULL); // This only makes sense for methods with code. 416 417 // Strictly speaking, we ought to be able to get away with doing a subset of the full method 418 // verification. In practice, the phase we want relies on data structures set up by all the 419 // earlier passes, so we just run the full method verification and bail out early when we've 420 // got what we wanted. 421 bool success = Verify(); 422 if (!success) { 423 return NULL; 424 } 425 RegisterLine* register_line = reg_table_.GetLine(dex_pc); 426 if (register_line == NULL) { 427 return NULL; 428 } 429 const Instruction* inst = Instruction::At(code_item_->insns_ + dex_pc); 430 const bool is_range = (inst->Opcode() == Instruction::INVOKE_VIRTUAL_RANGE_QUICK); 431 return GetQuickInvokedMethod(inst, register_line, is_range); 432} 433 434bool MethodVerifier::Verify() { 435 // If there aren't any instructions, make sure that's expected, then exit successfully. 436 if (code_item_ == NULL) { 437 if ((method_access_flags_ & (kAccNative | kAccAbstract)) == 0) { 438 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "zero-length code in concrete non-native method"; 439 return false; 440 } else { 441 return true; 442 } 443 } 444 // Sanity-check the register counts. ins + locals = registers, so make sure that ins <= registers. 445 if (code_item_->ins_size_ > code_item_->registers_size_) { 446 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad register counts (ins=" << code_item_->ins_size_ 447 << " regs=" << code_item_->registers_size_; 448 return false; 449 } 450 // Allocate and initialize an array to hold instruction data. 451 insn_flags_.reset(new InstructionFlags[code_item_->insns_size_in_code_units_]()); 452 // Run through the instructions and see if the width checks out. 453 bool result = ComputeWidthsAndCountOps(); 454 // Flag instructions guarded by a "try" block and check exception handlers. 455 result = result && ScanTryCatchBlocks(); 456 // Perform static instruction verification. 457 result = result && VerifyInstructions(); 458 // Perform code-flow analysis and return. 459 result = result && VerifyCodeFlow(); 460 // Compute information for compiler. 461 if (result && Runtime::Current()->IsCompiler()) { 462 result = Runtime::Current()->GetCompilerCallbacks()->MethodVerified(this); 463 } 464 return result; 465} 466 467std::ostream& MethodVerifier::Fail(VerifyError error) { 468 switch (error) { 469 case VERIFY_ERROR_NO_CLASS: 470 case VERIFY_ERROR_NO_FIELD: 471 case VERIFY_ERROR_NO_METHOD: 472 case VERIFY_ERROR_ACCESS_CLASS: 473 case VERIFY_ERROR_ACCESS_FIELD: 474 case VERIFY_ERROR_ACCESS_METHOD: 475 case VERIFY_ERROR_INSTANTIATION: 476 case VERIFY_ERROR_CLASS_CHANGE: 477 if (Runtime::Current()->IsCompiler() || !can_load_classes_) { 478 // If we're optimistically running verification at compile time, turn NO_xxx, ACCESS_xxx, 479 // class change and instantiation errors into soft verification errors so that we re-verify 480 // at runtime. We may fail to find or to agree on access because of not yet available class 481 // loaders, or class loaders that will differ at runtime. In these cases, we don't want to 482 // affect the soundness of the code being compiled. Instead, the generated code runs "slow 483 // paths" that dynamically perform the verification and cause the behavior to be that akin 484 // to an interpreter. 485 error = VERIFY_ERROR_BAD_CLASS_SOFT; 486 } else { 487 // If we fail again at runtime, mark that this instruction would throw and force this 488 // method to be executed using the interpreter with checks. 489 have_pending_runtime_throw_failure_ = true; 490 } 491 break; 492 // Indication that verification should be retried at runtime. 493 case VERIFY_ERROR_BAD_CLASS_SOFT: 494 if (!allow_soft_failures_) { 495 have_pending_hard_failure_ = true; 496 } 497 break; 498 // Hard verification failures at compile time will still fail at runtime, so the class is 499 // marked as rejected to prevent it from being compiled. 500 case VERIFY_ERROR_BAD_CLASS_HARD: { 501 if (Runtime::Current()->IsCompiler()) { 502 ClassReference ref(dex_file_, dex_file_->GetIndexForClassDef(*class_def_)); 503 Runtime::Current()->GetCompilerCallbacks()->ClassRejected(ref); 504 } 505 have_pending_hard_failure_ = true; 506 break; 507 } 508 } 509 failures_.push_back(error); 510 std::string location(StringPrintf("%s: [0x%X] ", PrettyMethod(dex_method_idx_, *dex_file_).c_str(), 511 work_insn_idx_)); 512 std::ostringstream* failure_message = new std::ostringstream(location, std::ostringstream::ate); 513 failure_messages_.push_back(failure_message); 514 return *failure_message; 515} 516 517std::ostream& MethodVerifier::LogVerifyInfo() { 518 return info_messages_ << "VFY: " << PrettyMethod(dex_method_idx_, *dex_file_) 519 << '[' << reinterpret_cast<void*>(work_insn_idx_) << "] : "; 520} 521 522void MethodVerifier::PrependToLastFailMessage(std::string prepend) { 523 size_t failure_num = failure_messages_.size(); 524 DCHECK_NE(failure_num, 0U); 525 std::ostringstream* last_fail_message = failure_messages_[failure_num - 1]; 526 prepend += last_fail_message->str(); 527 failure_messages_[failure_num - 1] = new std::ostringstream(prepend, std::ostringstream::ate); 528 delete last_fail_message; 529} 530 531void MethodVerifier::AppendToLastFailMessage(std::string append) { 532 size_t failure_num = failure_messages_.size(); 533 DCHECK_NE(failure_num, 0U); 534 std::ostringstream* last_fail_message = failure_messages_[failure_num - 1]; 535 (*last_fail_message) << append; 536} 537 538bool MethodVerifier::ComputeWidthsAndCountOps() { 539 const uint16_t* insns = code_item_->insns_; 540 size_t insns_size = code_item_->insns_size_in_code_units_; 541 const Instruction* inst = Instruction::At(insns); 542 size_t new_instance_count = 0; 543 size_t monitor_enter_count = 0; 544 size_t dex_pc = 0; 545 546 while (dex_pc < insns_size) { 547 Instruction::Code opcode = inst->Opcode(); 548 switch (opcode) { 549 case Instruction::APUT_OBJECT: 550 case Instruction::CHECK_CAST: 551 has_check_casts_ = true; 552 break; 553 case Instruction::INVOKE_VIRTUAL: 554 case Instruction::INVOKE_VIRTUAL_RANGE: 555 case Instruction::INVOKE_INTERFACE: 556 case Instruction::INVOKE_INTERFACE_RANGE: 557 has_virtual_or_interface_invokes_ = true; 558 break; 559 case Instruction::MONITOR_ENTER: 560 monitor_enter_count++; 561 break; 562 case Instruction::NEW_INSTANCE: 563 new_instance_count++; 564 break; 565 default: 566 break; 567 } 568 size_t inst_size = inst->SizeInCodeUnits(); 569 insn_flags_[dex_pc].SetLengthInCodeUnits(inst_size); 570 dex_pc += inst_size; 571 inst = inst->Next(); 572 } 573 574 if (dex_pc != insns_size) { 575 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "code did not end where expected (" 576 << dex_pc << " vs. " << insns_size << ")"; 577 return false; 578 } 579 580 new_instance_count_ = new_instance_count; 581 monitor_enter_count_ = monitor_enter_count; 582 return true; 583} 584 585bool MethodVerifier::ScanTryCatchBlocks() { 586 uint32_t tries_size = code_item_->tries_size_; 587 if (tries_size == 0) { 588 return true; 589 } 590 uint32_t insns_size = code_item_->insns_size_in_code_units_; 591 const DexFile::TryItem* tries = DexFile::GetTryItems(*code_item_, 0); 592 593 for (uint32_t idx = 0; idx < tries_size; idx++) { 594 const DexFile::TryItem* try_item = &tries[idx]; 595 uint32_t start = try_item->start_addr_; 596 uint32_t end = start + try_item->insn_count_; 597 if ((start >= end) || (start >= insns_size) || (end > insns_size)) { 598 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad exception entry: startAddr=" << start 599 << " endAddr=" << end << " (size=" << insns_size << ")"; 600 return false; 601 } 602 if (!insn_flags_[start].IsOpcode()) { 603 Fail(VERIFY_ERROR_BAD_CLASS_HARD) 604 << "'try' block starts inside an instruction (" << start << ")"; 605 return false; 606 } 607 for (uint32_t dex_pc = start; dex_pc < end; 608 dex_pc += insn_flags_[dex_pc].GetLengthInCodeUnits()) { 609 insn_flags_[dex_pc].SetInTry(); 610 } 611 } 612 // Iterate over each of the handlers to verify target addresses. 613 const byte* handlers_ptr = DexFile::GetCatchHandlerData(*code_item_, 0); 614 uint32_t handlers_size = DecodeUnsignedLeb128(&handlers_ptr); 615 ClassLinker* linker = Runtime::Current()->GetClassLinker(); 616 for (uint32_t idx = 0; idx < handlers_size; idx++) { 617 CatchHandlerIterator iterator(handlers_ptr); 618 for (; iterator.HasNext(); iterator.Next()) { 619 uint32_t dex_pc= iterator.GetHandlerAddress(); 620 if (!insn_flags_[dex_pc].IsOpcode()) { 621 Fail(VERIFY_ERROR_BAD_CLASS_HARD) 622 << "exception handler starts at bad address (" << dex_pc << ")"; 623 return false; 624 } 625 insn_flags_[dex_pc].SetBranchTarget(); 626 // Ensure exception types are resolved so that they don't need resolution to be delivered, 627 // unresolved exception types will be ignored by exception delivery 628 if (iterator.GetHandlerTypeIndex() != DexFile::kDexNoIndex16) { 629 mirror::Class* exception_type = linker->ResolveType(*dex_file_, 630 iterator.GetHandlerTypeIndex(), 631 *dex_cache_, *class_loader_); 632 if (exception_type == NULL) { 633 DCHECK(Thread::Current()->IsExceptionPending()); 634 Thread::Current()->ClearException(); 635 } 636 } 637 } 638 handlers_ptr = iterator.EndDataPointer(); 639 } 640 return true; 641} 642 643bool MethodVerifier::VerifyInstructions() { 644 const Instruction* inst = Instruction::At(code_item_->insns_); 645 646 /* Flag the start of the method as a branch target, and a GC point due to stack overflow errors */ 647 insn_flags_[0].SetBranchTarget(); 648 insn_flags_[0].SetCompileTimeInfoPoint(); 649 650 uint32_t insns_size = code_item_->insns_size_in_code_units_; 651 for (uint32_t dex_pc = 0; dex_pc < insns_size;) { 652 if (!VerifyInstruction(inst, dex_pc)) { 653 DCHECK_NE(failures_.size(), 0U); 654 return false; 655 } 656 /* Flag instructions that are garbage collection points */ 657 // All invoke points are marked as "Throw" points already. 658 // We are relying on this to also count all the invokes as interesting. 659 if (inst->IsBranch() || inst->IsSwitch() || inst->IsThrow()) { 660 insn_flags_[dex_pc].SetCompileTimeInfoPoint(); 661 } else if (inst->IsReturn()) { 662 insn_flags_[dex_pc].SetCompileTimeInfoPointAndReturn(); 663 } 664 dex_pc += inst->SizeInCodeUnits(); 665 inst = inst->Next(); 666 } 667 return true; 668} 669 670bool MethodVerifier::VerifyInstruction(const Instruction* inst, uint32_t code_offset) { 671 bool result = true; 672 switch (inst->GetVerifyTypeArgumentA()) { 673 case Instruction::kVerifyRegA: 674 result = result && CheckRegisterIndex(inst->VRegA()); 675 break; 676 case Instruction::kVerifyRegAWide: 677 result = result && CheckWideRegisterIndex(inst->VRegA()); 678 break; 679 } 680 switch (inst->GetVerifyTypeArgumentB()) { 681 case Instruction::kVerifyRegB: 682 result = result && CheckRegisterIndex(inst->VRegB()); 683 break; 684 case Instruction::kVerifyRegBField: 685 result = result && CheckFieldIndex(inst->VRegB()); 686 break; 687 case Instruction::kVerifyRegBMethod: 688 result = result && CheckMethodIndex(inst->VRegB()); 689 break; 690 case Instruction::kVerifyRegBNewInstance: 691 result = result && CheckNewInstance(inst->VRegB()); 692 break; 693 case Instruction::kVerifyRegBString: 694 result = result && CheckStringIndex(inst->VRegB()); 695 break; 696 case Instruction::kVerifyRegBType: 697 result = result && CheckTypeIndex(inst->VRegB()); 698 break; 699 case Instruction::kVerifyRegBWide: 700 result = result && CheckWideRegisterIndex(inst->VRegB()); 701 break; 702 } 703 switch (inst->GetVerifyTypeArgumentC()) { 704 case Instruction::kVerifyRegC: 705 result = result && CheckRegisterIndex(inst->VRegC()); 706 break; 707 case Instruction::kVerifyRegCField: 708 result = result && CheckFieldIndex(inst->VRegC()); 709 break; 710 case Instruction::kVerifyRegCNewArray: 711 result = result && CheckNewArray(inst->VRegC()); 712 break; 713 case Instruction::kVerifyRegCType: 714 result = result && CheckTypeIndex(inst->VRegC()); 715 break; 716 case Instruction::kVerifyRegCWide: 717 result = result && CheckWideRegisterIndex(inst->VRegC()); 718 break; 719 } 720 switch (inst->GetVerifyExtraFlags()) { 721 case Instruction::kVerifyArrayData: 722 result = result && CheckArrayData(code_offset); 723 break; 724 case Instruction::kVerifyBranchTarget: 725 result = result && CheckBranchTarget(code_offset); 726 break; 727 case Instruction::kVerifySwitchTargets: 728 result = result && CheckSwitchTargets(code_offset); 729 break; 730 case Instruction::kVerifyVarArgNonZero: 731 // Fall-through. 732 case Instruction::kVerifyVarArg: { 733 if (inst->GetVerifyExtraFlags() == Instruction::kVerifyVarArgNonZero && inst->VRegA() <= 0) { 734 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid arg count (" << inst->VRegA() << ") in " 735 "non-range invoke"; 736 return false; 737 } 738 uint32_t args[Instruction::kMaxVarArgRegs]; 739 inst->GetVarArgs(args); 740 result = result && CheckVarArgRegs(inst->VRegA(), args); 741 break; 742 } 743 case Instruction::kVerifyVarArgRangeNonZero: 744 // Fall-through. 745 case Instruction::kVerifyVarArgRange: 746 if (inst->GetVerifyExtraFlags() == Instruction::kVerifyVarArgRangeNonZero && 747 inst->VRegA() <= 0) { 748 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid arg count (" << inst->VRegA() << ") in " 749 "range invoke"; 750 return false; 751 } 752 result = result && CheckVarArgRangeRegs(inst->VRegA(), inst->VRegC()); 753 break; 754 case Instruction::kVerifyError: 755 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unexpected opcode " << inst->Name(); 756 result = false; 757 break; 758 } 759 if (inst->GetVerifyIsRuntimeOnly() && Runtime::Current()->IsCompiler()) { 760 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "opcode only expected at runtime " << inst->Name(); 761 result = false; 762 } 763 return result; 764} 765 766bool MethodVerifier::CheckRegisterIndex(uint32_t idx) { 767 if (idx >= code_item_->registers_size_) { 768 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "register index out of range (" << idx << " >= " 769 << code_item_->registers_size_ << ")"; 770 return false; 771 } 772 return true; 773} 774 775bool MethodVerifier::CheckWideRegisterIndex(uint32_t idx) { 776 if (idx + 1 >= code_item_->registers_size_) { 777 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "wide register index out of range (" << idx 778 << "+1 >= " << code_item_->registers_size_ << ")"; 779 return false; 780 } 781 return true; 782} 783 784bool MethodVerifier::CheckFieldIndex(uint32_t idx) { 785 if (idx >= dex_file_->GetHeader().field_ids_size_) { 786 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad field index " << idx << " (max " 787 << dex_file_->GetHeader().field_ids_size_ << ")"; 788 return false; 789 } 790 return true; 791} 792 793bool MethodVerifier::CheckMethodIndex(uint32_t idx) { 794 if (idx >= dex_file_->GetHeader().method_ids_size_) { 795 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad method index " << idx << " (max " 796 << dex_file_->GetHeader().method_ids_size_ << ")"; 797 return false; 798 } 799 return true; 800} 801 802bool MethodVerifier::CheckNewInstance(uint32_t idx) { 803 if (idx >= dex_file_->GetHeader().type_ids_size_) { 804 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad type index " << idx << " (max " 805 << dex_file_->GetHeader().type_ids_size_ << ")"; 806 return false; 807 } 808 // We don't need the actual class, just a pointer to the class name. 809 const char* descriptor = dex_file_->StringByTypeIdx(idx); 810 if (descriptor[0] != 'L') { 811 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "can't call new-instance on type '" << descriptor << "'"; 812 return false; 813 } 814 return true; 815} 816 817bool MethodVerifier::CheckStringIndex(uint32_t idx) { 818 if (idx >= dex_file_->GetHeader().string_ids_size_) { 819 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad string index " << idx << " (max " 820 << dex_file_->GetHeader().string_ids_size_ << ")"; 821 return false; 822 } 823 return true; 824} 825 826bool MethodVerifier::CheckTypeIndex(uint32_t idx) { 827 if (idx >= dex_file_->GetHeader().type_ids_size_) { 828 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad type index " << idx << " (max " 829 << dex_file_->GetHeader().type_ids_size_ << ")"; 830 return false; 831 } 832 return true; 833} 834 835bool MethodVerifier::CheckNewArray(uint32_t idx) { 836 if (idx >= dex_file_->GetHeader().type_ids_size_) { 837 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad type index " << idx << " (max " 838 << dex_file_->GetHeader().type_ids_size_ << ")"; 839 return false; 840 } 841 int bracket_count = 0; 842 const char* descriptor = dex_file_->StringByTypeIdx(idx); 843 const char* cp = descriptor; 844 while (*cp++ == '[') { 845 bracket_count++; 846 } 847 if (bracket_count == 0) { 848 /* The given class must be an array type. */ 849 Fail(VERIFY_ERROR_BAD_CLASS_HARD) 850 << "can't new-array class '" << descriptor << "' (not an array)"; 851 return false; 852 } else if (bracket_count > 255) { 853 /* It is illegal to create an array of more than 255 dimensions. */ 854 Fail(VERIFY_ERROR_BAD_CLASS_HARD) 855 << "can't new-array class '" << descriptor << "' (exceeds limit)"; 856 return false; 857 } 858 return true; 859} 860 861bool MethodVerifier::CheckArrayData(uint32_t cur_offset) { 862 const uint32_t insn_count = code_item_->insns_size_in_code_units_; 863 const uint16_t* insns = code_item_->insns_ + cur_offset; 864 const uint16_t* array_data; 865 int32_t array_data_offset; 866 867 DCHECK_LT(cur_offset, insn_count); 868 /* make sure the start of the array data table is in range */ 869 array_data_offset = insns[1] | (((int32_t) insns[2]) << 16); 870 if ((int32_t) cur_offset + array_data_offset < 0 || 871 cur_offset + array_data_offset + 2 >= insn_count) { 872 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid array data start: at " << cur_offset 873 << ", data offset " << array_data_offset 874 << ", count " << insn_count; 875 return false; 876 } 877 /* offset to array data table is a relative branch-style offset */ 878 array_data = insns + array_data_offset; 879 /* make sure the table is 32-bit aligned */ 880 if ((reinterpret_cast<uintptr_t>(array_data) & 0x03) != 0) { 881 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unaligned array data table: at " << cur_offset 882 << ", data offset " << array_data_offset; 883 return false; 884 } 885 uint32_t value_width = array_data[1]; 886 uint32_t value_count = *reinterpret_cast<const uint32_t*>(&array_data[2]); 887 uint32_t table_size = 4 + (value_width * value_count + 1) / 2; 888 /* make sure the end of the switch is in range */ 889 if (cur_offset + array_data_offset + table_size > insn_count) { 890 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid array data end: at " << cur_offset 891 << ", data offset " << array_data_offset << ", end " 892 << cur_offset + array_data_offset + table_size 893 << ", count " << insn_count; 894 return false; 895 } 896 return true; 897} 898 899bool MethodVerifier::CheckBranchTarget(uint32_t cur_offset) { 900 int32_t offset; 901 bool isConditional, selfOkay; 902 if (!GetBranchOffset(cur_offset, &offset, &isConditional, &selfOkay)) { 903 return false; 904 } 905 if (!selfOkay && offset == 0) { 906 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "branch offset of zero not allowed at" 907 << reinterpret_cast<void*>(cur_offset); 908 return false; 909 } 910 // Check for 32-bit overflow. This isn't strictly necessary if we can depend on the runtime 911 // to have identical "wrap-around" behavior, but it's unwise to depend on that. 912 if (((int64_t) cur_offset + (int64_t) offset) != (int64_t) (cur_offset + offset)) { 913 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "branch target overflow " 914 << reinterpret_cast<void*>(cur_offset) << " +" << offset; 915 return false; 916 } 917 const uint32_t insn_count = code_item_->insns_size_in_code_units_; 918 int32_t abs_offset = cur_offset + offset; 919 if (abs_offset < 0 || 920 (uint32_t) abs_offset >= insn_count || 921 !insn_flags_[abs_offset].IsOpcode()) { 922 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid branch target " << offset << " (-> " 923 << reinterpret_cast<void*>(abs_offset) << ") at " 924 << reinterpret_cast<void*>(cur_offset); 925 return false; 926 } 927 insn_flags_[abs_offset].SetBranchTarget(); 928 return true; 929} 930 931bool MethodVerifier::GetBranchOffset(uint32_t cur_offset, int32_t* pOffset, bool* pConditional, 932 bool* selfOkay) { 933 const uint16_t* insns = code_item_->insns_ + cur_offset; 934 *pConditional = false; 935 *selfOkay = false; 936 switch (*insns & 0xff) { 937 case Instruction::GOTO: 938 *pOffset = ((int16_t) *insns) >> 8; 939 break; 940 case Instruction::GOTO_32: 941 *pOffset = insns[1] | (((uint32_t) insns[2]) << 16); 942 *selfOkay = true; 943 break; 944 case Instruction::GOTO_16: 945 *pOffset = (int16_t) insns[1]; 946 break; 947 case Instruction::IF_EQ: 948 case Instruction::IF_NE: 949 case Instruction::IF_LT: 950 case Instruction::IF_GE: 951 case Instruction::IF_GT: 952 case Instruction::IF_LE: 953 case Instruction::IF_EQZ: 954 case Instruction::IF_NEZ: 955 case Instruction::IF_LTZ: 956 case Instruction::IF_GEZ: 957 case Instruction::IF_GTZ: 958 case Instruction::IF_LEZ: 959 *pOffset = (int16_t) insns[1]; 960 *pConditional = true; 961 break; 962 default: 963 return false; 964 break; 965 } 966 return true; 967} 968 969bool MethodVerifier::CheckSwitchTargets(uint32_t cur_offset) { 970 const uint32_t insn_count = code_item_->insns_size_in_code_units_; 971 DCHECK_LT(cur_offset, insn_count); 972 const uint16_t* insns = code_item_->insns_ + cur_offset; 973 /* make sure the start of the switch is in range */ 974 int32_t switch_offset = insns[1] | ((int32_t) insns[2]) << 16; 975 if ((int32_t) cur_offset + switch_offset < 0 || cur_offset + switch_offset + 2 >= insn_count) { 976 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid switch start: at " << cur_offset 977 << ", switch offset " << switch_offset 978 << ", count " << insn_count; 979 return false; 980 } 981 /* offset to switch table is a relative branch-style offset */ 982 const uint16_t* switch_insns = insns + switch_offset; 983 /* make sure the table is 32-bit aligned */ 984 if ((reinterpret_cast<uintptr_t>(switch_insns) & 0x03) != 0) { 985 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unaligned switch table: at " << cur_offset 986 << ", switch offset " << switch_offset; 987 return false; 988 } 989 uint32_t switch_count = switch_insns[1]; 990 int32_t keys_offset, targets_offset; 991 uint16_t expected_signature; 992 if ((*insns & 0xff) == Instruction::PACKED_SWITCH) { 993 /* 0=sig, 1=count, 2/3=firstKey */ 994 targets_offset = 4; 995 keys_offset = -1; 996 expected_signature = Instruction::kPackedSwitchSignature; 997 } else { 998 /* 0=sig, 1=count, 2..count*2 = keys */ 999 keys_offset = 2; 1000 targets_offset = 2 + 2 * switch_count; 1001 expected_signature = Instruction::kSparseSwitchSignature; 1002 } 1003 uint32_t table_size = targets_offset + switch_count * 2; 1004 if (switch_insns[0] != expected_signature) { 1005 Fail(VERIFY_ERROR_BAD_CLASS_HARD) 1006 << StringPrintf("wrong signature for switch table (%x, wanted %x)", 1007 switch_insns[0], expected_signature); 1008 return false; 1009 } 1010 /* make sure the end of the switch is in range */ 1011 if (cur_offset + switch_offset + table_size > (uint32_t) insn_count) { 1012 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid switch end: at " << cur_offset 1013 << ", switch offset " << switch_offset 1014 << ", end " << (cur_offset + switch_offset + table_size) 1015 << ", count " << insn_count; 1016 return false; 1017 } 1018 /* for a sparse switch, verify the keys are in ascending order */ 1019 if (keys_offset > 0 && switch_count > 1) { 1020 int32_t last_key = switch_insns[keys_offset] | (switch_insns[keys_offset + 1] << 16); 1021 for (uint32_t targ = 1; targ < switch_count; targ++) { 1022 int32_t key = (int32_t) switch_insns[keys_offset + targ * 2] | 1023 (int32_t) (switch_insns[keys_offset + targ * 2 + 1] << 16); 1024 if (key <= last_key) { 1025 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid packed switch: last key=" << last_key 1026 << ", this=" << key; 1027 return false; 1028 } 1029 last_key = key; 1030 } 1031 } 1032 /* verify each switch target */ 1033 for (uint32_t targ = 0; targ < switch_count; targ++) { 1034 int32_t offset = (int32_t) switch_insns[targets_offset + targ * 2] | 1035 (int32_t) (switch_insns[targets_offset + targ * 2 + 1] << 16); 1036 int32_t abs_offset = cur_offset + offset; 1037 if (abs_offset < 0 || 1038 abs_offset >= (int32_t) insn_count || 1039 !insn_flags_[abs_offset].IsOpcode()) { 1040 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid switch target " << offset 1041 << " (-> " << reinterpret_cast<void*>(abs_offset) << ") at " 1042 << reinterpret_cast<void*>(cur_offset) 1043 << "[" << targ << "]"; 1044 return false; 1045 } 1046 insn_flags_[abs_offset].SetBranchTarget(); 1047 } 1048 return true; 1049} 1050 1051bool MethodVerifier::CheckVarArgRegs(uint32_t vA, uint32_t arg[]) { 1052 if (vA > Instruction::kMaxVarArgRegs) { 1053 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid arg count (" << vA << ") in non-range invoke)"; 1054 return false; 1055 } 1056 uint16_t registers_size = code_item_->registers_size_; 1057 for (uint32_t idx = 0; idx < vA; idx++) { 1058 if (arg[idx] >= registers_size) { 1059 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid reg index (" << arg[idx] 1060 << ") in non-range invoke (>= " << registers_size << ")"; 1061 return false; 1062 } 1063 } 1064 1065 return true; 1066} 1067 1068bool MethodVerifier::CheckVarArgRangeRegs(uint32_t vA, uint32_t vC) { 1069 uint16_t registers_size = code_item_->registers_size_; 1070 // vA/vC are unsigned 8-bit/16-bit quantities for /range instructions, so there's no risk of 1071 // integer overflow when adding them here. 1072 if (vA + vC > registers_size) { 1073 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid reg index " << vA << "+" << vC 1074 << " in range invoke (> " << registers_size << ")"; 1075 return false; 1076 } 1077 return true; 1078} 1079 1080bool MethodVerifier::VerifyCodeFlow() { 1081 uint16_t registers_size = code_item_->registers_size_; 1082 uint32_t insns_size = code_item_->insns_size_in_code_units_; 1083 1084 if (registers_size * insns_size > 4*1024*1024) { 1085 LOG(WARNING) << "warning: method is huge (regs=" << registers_size 1086 << " insns_size=" << insns_size << ")"; 1087 } 1088 /* Create and initialize table holding register status */ 1089 reg_table_.Init(kTrackCompilerInterestPoints, 1090 insn_flags_.get(), 1091 insns_size, 1092 registers_size, 1093 this); 1094 1095 1096 work_line_.reset(RegisterLine::Create(registers_size, this)); 1097 saved_line_.reset(RegisterLine::Create(registers_size, this)); 1098 1099 /* Initialize register types of method arguments. */ 1100 if (!SetTypesFromSignature()) { 1101 DCHECK_NE(failures_.size(), 0U); 1102 std::string prepend("Bad signature in "); 1103 prepend += PrettyMethod(dex_method_idx_, *dex_file_); 1104 PrependToLastFailMessage(prepend); 1105 return false; 1106 } 1107 /* Perform code flow verification. */ 1108 if (!CodeFlowVerifyMethod()) { 1109 DCHECK_NE(failures_.size(), 0U); 1110 return false; 1111 } 1112 return true; 1113} 1114 1115std::ostream& MethodVerifier::DumpFailures(std::ostream& os) { 1116 DCHECK_EQ(failures_.size(), failure_messages_.size()); 1117 for (size_t i = 0; i < failures_.size(); ++i) { 1118 os << failure_messages_[i]->str() << "\n"; 1119 } 1120 return os; 1121} 1122 1123extern "C" void MethodVerifierGdbDump(MethodVerifier* v) 1124 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { 1125 v->Dump(std::cerr); 1126} 1127 1128void MethodVerifier::Dump(std::ostream& os) { 1129 if (code_item_ == NULL) { 1130 os << "Native method\n"; 1131 return; 1132 } 1133 { 1134 os << "Register Types:\n"; 1135 Indenter indent_filter(os.rdbuf(), kIndentChar, kIndentBy1Count); 1136 std::ostream indent_os(&indent_filter); 1137 reg_types_.Dump(indent_os); 1138 } 1139 os << "Dumping instructions and register lines:\n"; 1140 Indenter indent_filter(os.rdbuf(), kIndentChar, kIndentBy1Count); 1141 std::ostream indent_os(&indent_filter); 1142 const Instruction* inst = Instruction::At(code_item_->insns_); 1143 for (size_t dex_pc = 0; dex_pc < code_item_->insns_size_in_code_units_; 1144 dex_pc += insn_flags_[dex_pc].GetLengthInCodeUnits()) { 1145 RegisterLine* reg_line = reg_table_.GetLine(dex_pc); 1146 if (reg_line != NULL) { 1147 indent_os << reg_line->Dump() << "\n"; 1148 } 1149 indent_os << StringPrintf("0x%04zx", dex_pc) << ": " << insn_flags_[dex_pc].ToString() << " "; 1150 const bool kDumpHexOfInstruction = false; 1151 if (kDumpHexOfInstruction) { 1152 indent_os << inst->DumpHex(5) << " "; 1153 } 1154 indent_os << inst->DumpString(dex_file_) << "\n"; 1155 inst = inst->Next(); 1156 } 1157} 1158 1159static bool IsPrimitiveDescriptor(char descriptor) { 1160 switch (descriptor) { 1161 case 'I': 1162 case 'C': 1163 case 'S': 1164 case 'B': 1165 case 'Z': 1166 case 'F': 1167 case 'D': 1168 case 'J': 1169 return true; 1170 default: 1171 return false; 1172 } 1173} 1174 1175bool MethodVerifier::SetTypesFromSignature() { 1176 RegisterLine* reg_line = reg_table_.GetLine(0); 1177 int arg_start = code_item_->registers_size_ - code_item_->ins_size_; 1178 size_t expected_args = code_item_->ins_size_; /* long/double count as two */ 1179 1180 DCHECK_GE(arg_start, 0); /* should have been verified earlier */ 1181 // Include the "this" pointer. 1182 size_t cur_arg = 0; 1183 if (!IsStatic()) { 1184 // If this is a constructor for a class other than java.lang.Object, mark the first ("this") 1185 // argument as uninitialized. This restricts field access until the superclass constructor is 1186 // called. 1187 RegType& declaring_class = GetDeclaringClass(); 1188 if (IsConstructor() && !declaring_class.IsJavaLangObject()) { 1189 reg_line->SetRegisterType(arg_start + cur_arg, 1190 reg_types_.UninitializedThisArgument(declaring_class)); 1191 } else { 1192 reg_line->SetRegisterType(arg_start + cur_arg, declaring_class); 1193 } 1194 cur_arg++; 1195 } 1196 1197 const DexFile::ProtoId& proto_id = 1198 dex_file_->GetMethodPrototype(dex_file_->GetMethodId(dex_method_idx_)); 1199 DexFileParameterIterator iterator(*dex_file_, proto_id); 1200 1201 for (; iterator.HasNext(); iterator.Next()) { 1202 const char* descriptor = iterator.GetDescriptor(); 1203 if (descriptor == NULL) { 1204 LOG(FATAL) << "Null descriptor"; 1205 } 1206 if (cur_arg >= expected_args) { 1207 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "expected " << expected_args 1208 << " args, found more (" << descriptor << ")"; 1209 return false; 1210 } 1211 switch (descriptor[0]) { 1212 case 'L': 1213 case '[': 1214 // We assume that reference arguments are initialized. The only way it could be otherwise 1215 // (assuming the caller was verified) is if the current method is <init>, but in that case 1216 // it's effectively considered initialized the instant we reach here (in the sense that we 1217 // can return without doing anything or call virtual methods). 1218 { 1219 RegType& reg_type = ResolveClassAndCheckAccess(iterator.GetTypeIdx()); 1220 if (!reg_type.IsNonZeroReferenceTypes()) { 1221 DCHECK(HasFailures()); 1222 return false; 1223 } 1224 reg_line->SetRegisterType(arg_start + cur_arg, reg_type); 1225 } 1226 break; 1227 case 'Z': 1228 reg_line->SetRegisterType(arg_start + cur_arg, reg_types_.Boolean()); 1229 break; 1230 case 'C': 1231 reg_line->SetRegisterType(arg_start + cur_arg, reg_types_.Char()); 1232 break; 1233 case 'B': 1234 reg_line->SetRegisterType(arg_start + cur_arg, reg_types_.Byte()); 1235 break; 1236 case 'I': 1237 reg_line->SetRegisterType(arg_start + cur_arg, reg_types_.Integer()); 1238 break; 1239 case 'S': 1240 reg_line->SetRegisterType(arg_start + cur_arg, reg_types_.Short()); 1241 break; 1242 case 'F': 1243 reg_line->SetRegisterType(arg_start + cur_arg, reg_types_.Float()); 1244 break; 1245 case 'J': 1246 case 'D': { 1247 if (cur_arg + 1 >= expected_args) { 1248 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "expected " << expected_args 1249 << " args, found more (" << descriptor << ")"; 1250 return false; 1251 } 1252 1253 RegType& lo_half = descriptor[0] == 'J' ? reg_types_.LongLo() : reg_types_.DoubleLo(); 1254 RegType& hi_half = descriptor[0] == 'J' ? reg_types_.LongHi() : reg_types_.DoubleHi(); 1255 reg_line->SetRegisterTypeWide(arg_start + cur_arg, lo_half, hi_half); 1256 cur_arg++; 1257 break; 1258 } 1259 default: 1260 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unexpected signature type char '" 1261 << descriptor << "'"; 1262 return false; 1263 } 1264 cur_arg++; 1265 } 1266 if (cur_arg != expected_args) { 1267 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "expected " << expected_args 1268 << " arguments, found " << cur_arg; 1269 return false; 1270 } 1271 const char* descriptor = dex_file_->GetReturnTypeDescriptor(proto_id); 1272 // Validate return type. We don't do the type lookup; just want to make sure that it has the right 1273 // format. Only major difference from the method argument format is that 'V' is supported. 1274 bool result; 1275 if (IsPrimitiveDescriptor(descriptor[0]) || descriptor[0] == 'V') { 1276 result = descriptor[1] == '\0'; 1277 } else if (descriptor[0] == '[') { // single/multi-dimensional array of object/primitive 1278 size_t i = 0; 1279 do { 1280 i++; 1281 } while (descriptor[i] == '['); // process leading [ 1282 if (descriptor[i] == 'L') { // object array 1283 do { 1284 i++; // find closing ; 1285 } while (descriptor[i] != ';' && descriptor[i] != '\0'); 1286 result = descriptor[i] == ';'; 1287 } else { // primitive array 1288 result = IsPrimitiveDescriptor(descriptor[i]) && descriptor[i + 1] == '\0'; 1289 } 1290 } else if (descriptor[0] == 'L') { 1291 // could be more thorough here, but shouldn't be required 1292 size_t i = 0; 1293 do { 1294 i++; 1295 } while (descriptor[i] != ';' && descriptor[i] != '\0'); 1296 result = descriptor[i] == ';'; 1297 } else { 1298 result = false; 1299 } 1300 if (!result) { 1301 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unexpected char in return type descriptor '" 1302 << descriptor << "'"; 1303 } 1304 return result; 1305} 1306 1307bool MethodVerifier::CodeFlowVerifyMethod() { 1308 const uint16_t* insns = code_item_->insns_; 1309 const uint32_t insns_size = code_item_->insns_size_in_code_units_; 1310 1311 /* Begin by marking the first instruction as "changed". */ 1312 insn_flags_[0].SetChanged(); 1313 uint32_t start_guess = 0; 1314 1315 /* Continue until no instructions are marked "changed". */ 1316 while (true) { 1317 // Find the first marked one. Use "start_guess" as a way to find one quickly. 1318 uint32_t insn_idx = start_guess; 1319 for (; insn_idx < insns_size; insn_idx++) { 1320 if (insn_flags_[insn_idx].IsChanged()) 1321 break; 1322 } 1323 if (insn_idx == insns_size) { 1324 if (start_guess != 0) { 1325 /* try again, starting from the top */ 1326 start_guess = 0; 1327 continue; 1328 } else { 1329 /* all flags are clear */ 1330 break; 1331 } 1332 } 1333 // We carry the working set of registers from instruction to instruction. If this address can 1334 // be the target of a branch (or throw) instruction, or if we're skipping around chasing 1335 // "changed" flags, we need to load the set of registers from the table. 1336 // Because we always prefer to continue on to the next instruction, we should never have a 1337 // situation where we have a stray "changed" flag set on an instruction that isn't a branch 1338 // target. 1339 work_insn_idx_ = insn_idx; 1340 if (insn_flags_[insn_idx].IsBranchTarget()) { 1341 work_line_->CopyFromLine(reg_table_.GetLine(insn_idx)); 1342 } else if (kIsDebugBuild) { 1343 /* 1344 * Sanity check: retrieve the stored register line (assuming 1345 * a full table) and make sure it actually matches. 1346 */ 1347 RegisterLine* register_line = reg_table_.GetLine(insn_idx); 1348 if (register_line != NULL) { 1349 if (work_line_->CompareLine(register_line) != 0) { 1350 Dump(std::cout); 1351 std::cout << info_messages_.str(); 1352 LOG(FATAL) << "work_line diverged in " << PrettyMethod(dex_method_idx_, *dex_file_) 1353 << "@" << reinterpret_cast<void*>(work_insn_idx_) << "\n" 1354 << " work_line=" << *work_line_ << "\n" 1355 << " expected=" << *register_line; 1356 } 1357 } 1358 } 1359 if (!CodeFlowVerifyInstruction(&start_guess)) { 1360 std::string prepend(PrettyMethod(dex_method_idx_, *dex_file_)); 1361 prepend += " failed to verify: "; 1362 PrependToLastFailMessage(prepend); 1363 return false; 1364 } 1365 /* Clear "changed" and mark as visited. */ 1366 insn_flags_[insn_idx].SetVisited(); 1367 insn_flags_[insn_idx].ClearChanged(); 1368 } 1369 1370 if (gDebugVerify) { 1371 /* 1372 * Scan for dead code. There's nothing "evil" about dead code 1373 * (besides the wasted space), but it indicates a flaw somewhere 1374 * down the line, possibly in the verifier. 1375 * 1376 * If we've substituted "always throw" instructions into the stream, 1377 * we are almost certainly going to have some dead code. 1378 */ 1379 int dead_start = -1; 1380 uint32_t insn_idx = 0; 1381 for (; insn_idx < insns_size; insn_idx += insn_flags_[insn_idx].GetLengthInCodeUnits()) { 1382 /* 1383 * Switch-statement data doesn't get "visited" by scanner. It 1384 * may or may not be preceded by a padding NOP (for alignment). 1385 */ 1386 if (insns[insn_idx] == Instruction::kPackedSwitchSignature || 1387 insns[insn_idx] == Instruction::kSparseSwitchSignature || 1388 insns[insn_idx] == Instruction::kArrayDataSignature || 1389 (insns[insn_idx] == Instruction::NOP && (insn_idx + 1 < insns_size) && 1390 (insns[insn_idx + 1] == Instruction::kPackedSwitchSignature || 1391 insns[insn_idx + 1] == Instruction::kSparseSwitchSignature || 1392 insns[insn_idx + 1] == Instruction::kArrayDataSignature))) { 1393 insn_flags_[insn_idx].SetVisited(); 1394 } 1395 1396 if (!insn_flags_[insn_idx].IsVisited()) { 1397 if (dead_start < 0) 1398 dead_start = insn_idx; 1399 } else if (dead_start >= 0) { 1400 LogVerifyInfo() << "dead code " << reinterpret_cast<void*>(dead_start) 1401 << "-" << reinterpret_cast<void*>(insn_idx - 1); 1402 dead_start = -1; 1403 } 1404 } 1405 if (dead_start >= 0) { 1406 LogVerifyInfo() << "dead code " << reinterpret_cast<void*>(dead_start) 1407 << "-" << reinterpret_cast<void*>(insn_idx - 1); 1408 } 1409 // To dump the state of the verify after a method, do something like: 1410 // if (PrettyMethod(dex_method_idx_, *dex_file_) == 1411 // "boolean java.lang.String.equals(java.lang.Object)") { 1412 // LOG(INFO) << info_messages_.str(); 1413 // } 1414 } 1415 return true; 1416} 1417 1418bool MethodVerifier::CodeFlowVerifyInstruction(uint32_t* start_guess) { 1419 // If we're doing FindLocksAtDexPc, check whether we're at the dex pc we care about. 1420 // We want the state _before_ the instruction, for the case where the dex pc we're 1421 // interested in is itself a monitor-enter instruction (which is a likely place 1422 // for a thread to be suspended). 1423 if (monitor_enter_dex_pcs_ != NULL && work_insn_idx_ == interesting_dex_pc_) { 1424 monitor_enter_dex_pcs_->clear(); // The new work line is more accurate than the previous one. 1425 for (size_t i = 0; i < work_line_->GetMonitorEnterCount(); ++i) { 1426 monitor_enter_dex_pcs_->push_back(work_line_->GetMonitorEnterDexPc(i)); 1427 } 1428 } 1429 1430 /* 1431 * Once we finish decoding the instruction, we need to figure out where 1432 * we can go from here. There are three possible ways to transfer 1433 * control to another statement: 1434 * 1435 * (1) Continue to the next instruction. Applies to all but 1436 * unconditional branches, method returns, and exception throws. 1437 * (2) Branch to one or more possible locations. Applies to branches 1438 * and switch statements. 1439 * (3) Exception handlers. Applies to any instruction that can 1440 * throw an exception that is handled by an encompassing "try" 1441 * block. 1442 * 1443 * We can also return, in which case there is no successor instruction 1444 * from this point. 1445 * 1446 * The behavior can be determined from the opcode flags. 1447 */ 1448 const uint16_t* insns = code_item_->insns_ + work_insn_idx_; 1449 const Instruction* inst = Instruction::At(insns); 1450 int opcode_flags = Instruction::FlagsOf(inst->Opcode()); 1451 1452 int32_t branch_target = 0; 1453 bool just_set_result = false; 1454 if (gDebugVerify) { 1455 // Generate processing back trace to debug verifier 1456 LogVerifyInfo() << "Processing " << inst->DumpString(dex_file_) << "\n" 1457 << *work_line_.get() << "\n"; 1458 } 1459 1460 /* 1461 * Make a copy of the previous register state. If the instruction 1462 * can throw an exception, we will copy/merge this into the "catch" 1463 * address rather than work_line, because we don't want the result 1464 * from the "successful" code path (e.g. a check-cast that "improves" 1465 * a type) to be visible to the exception handler. 1466 */ 1467 if ((opcode_flags & Instruction::kThrow) != 0 && CurrentInsnFlags()->IsInTry()) { 1468 saved_line_->CopyFromLine(work_line_.get()); 1469 } else if (kIsDebugBuild) { 1470 saved_line_->FillWithGarbage(); 1471 } 1472 1473 1474 // We need to ensure the work line is consistent while performing validation. When we spot a 1475 // peephole pattern we compute a new line for either the fallthrough instruction or the 1476 // branch target. 1477 std::unique_ptr<RegisterLine> branch_line; 1478 std::unique_ptr<RegisterLine> fallthrough_line; 1479 1480 switch (inst->Opcode()) { 1481 case Instruction::NOP: 1482 /* 1483 * A "pure" NOP has no effect on anything. Data tables start with 1484 * a signature that looks like a NOP; if we see one of these in 1485 * the course of executing code then we have a problem. 1486 */ 1487 if (inst->VRegA_10x() != 0) { 1488 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "encountered data table in instruction stream"; 1489 } 1490 break; 1491 1492 case Instruction::MOVE: 1493 work_line_->CopyRegister1(inst->VRegA_12x(), inst->VRegB_12x(), kTypeCategory1nr); 1494 break; 1495 case Instruction::MOVE_FROM16: 1496 work_line_->CopyRegister1(inst->VRegA_22x(), inst->VRegB_22x(), kTypeCategory1nr); 1497 break; 1498 case Instruction::MOVE_16: 1499 work_line_->CopyRegister1(inst->VRegA_32x(), inst->VRegB_32x(), kTypeCategory1nr); 1500 break; 1501 case Instruction::MOVE_WIDE: 1502 work_line_->CopyRegister2(inst->VRegA_12x(), inst->VRegB_12x()); 1503 break; 1504 case Instruction::MOVE_WIDE_FROM16: 1505 work_line_->CopyRegister2(inst->VRegA_22x(), inst->VRegB_22x()); 1506 break; 1507 case Instruction::MOVE_WIDE_16: 1508 work_line_->CopyRegister2(inst->VRegA_32x(), inst->VRegB_32x()); 1509 break; 1510 case Instruction::MOVE_OBJECT: 1511 work_line_->CopyRegister1(inst->VRegA_12x(), inst->VRegB_12x(), kTypeCategoryRef); 1512 break; 1513 case Instruction::MOVE_OBJECT_FROM16: 1514 work_line_->CopyRegister1(inst->VRegA_22x(), inst->VRegB_22x(), kTypeCategoryRef); 1515 break; 1516 case Instruction::MOVE_OBJECT_16: 1517 work_line_->CopyRegister1(inst->VRegA_32x(), inst->VRegB_32x(), kTypeCategoryRef); 1518 break; 1519 1520 /* 1521 * The move-result instructions copy data out of a "pseudo-register" 1522 * with the results from the last method invocation. In practice we 1523 * might want to hold the result in an actual CPU register, so the 1524 * Dalvik spec requires that these only appear immediately after an 1525 * invoke or filled-new-array. 1526 * 1527 * These calls invalidate the "result" register. (This is now 1528 * redundant with the reset done below, but it can make the debug info 1529 * easier to read in some cases.) 1530 */ 1531 case Instruction::MOVE_RESULT: 1532 work_line_->CopyResultRegister1(inst->VRegA_11x(), false); 1533 break; 1534 case Instruction::MOVE_RESULT_WIDE: 1535 work_line_->CopyResultRegister2(inst->VRegA_11x()); 1536 break; 1537 case Instruction::MOVE_RESULT_OBJECT: 1538 work_line_->CopyResultRegister1(inst->VRegA_11x(), true); 1539 break; 1540 1541 case Instruction::MOVE_EXCEPTION: { 1542 /* 1543 * This statement can only appear as the first instruction in an exception handler. We verify 1544 * that as part of extracting the exception type from the catch block list. 1545 */ 1546 RegType& res_type = GetCaughtExceptionType(); 1547 work_line_->SetRegisterType(inst->VRegA_11x(), res_type); 1548 break; 1549 } 1550 case Instruction::RETURN_VOID: 1551 if (!IsConstructor() || work_line_->CheckConstructorReturn()) { 1552 if (!GetMethodReturnType().IsConflict()) { 1553 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "return-void not expected"; 1554 } 1555 } 1556 break; 1557 case Instruction::RETURN: 1558 if (!IsConstructor() || work_line_->CheckConstructorReturn()) { 1559 /* check the method signature */ 1560 RegType& return_type = GetMethodReturnType(); 1561 if (!return_type.IsCategory1Types()) { 1562 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unexpected non-category 1 return type " 1563 << return_type; 1564 } else { 1565 // Compilers may generate synthetic functions that write byte values into boolean fields. 1566 // Also, it may use integer values for boolean, byte, short, and character return types. 1567 const uint32_t vregA = inst->VRegA_11x(); 1568 RegType& src_type = work_line_->GetRegisterType(vregA); 1569 bool use_src = ((return_type.IsBoolean() && src_type.IsByte()) || 1570 ((return_type.IsBoolean() || return_type.IsByte() || 1571 return_type.IsShort() || return_type.IsChar()) && 1572 src_type.IsInteger())); 1573 /* check the register contents */ 1574 bool success = 1575 work_line_->VerifyRegisterType(vregA, use_src ? src_type : return_type); 1576 if (!success) { 1577 AppendToLastFailMessage(StringPrintf(" return-1nr on invalid register v%d", vregA)); 1578 } 1579 } 1580 } 1581 break; 1582 case Instruction::RETURN_WIDE: 1583 if (!IsConstructor() || work_line_->CheckConstructorReturn()) { 1584 /* check the method signature */ 1585 RegType& return_type = GetMethodReturnType(); 1586 if (!return_type.IsCategory2Types()) { 1587 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "return-wide not expected"; 1588 } else { 1589 /* check the register contents */ 1590 const uint32_t vregA = inst->VRegA_11x(); 1591 bool success = work_line_->VerifyRegisterType(vregA, return_type); 1592 if (!success) { 1593 AppendToLastFailMessage(StringPrintf(" return-wide on invalid register v%d", vregA)); 1594 } 1595 } 1596 } 1597 break; 1598 case Instruction::RETURN_OBJECT: 1599 if (!IsConstructor() || work_line_->CheckConstructorReturn()) { 1600 RegType& return_type = GetMethodReturnType(); 1601 if (!return_type.IsReferenceTypes()) { 1602 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "return-object not expected"; 1603 } else { 1604 /* return_type is the *expected* return type, not register value */ 1605 DCHECK(!return_type.IsZero()); 1606 DCHECK(!return_type.IsUninitializedReference()); 1607 const uint32_t vregA = inst->VRegA_11x(); 1608 RegType& reg_type = work_line_->GetRegisterType(vregA); 1609 // Disallow returning uninitialized values and verify that the reference in vAA is an 1610 // instance of the "return_type" 1611 if (reg_type.IsUninitializedTypes()) { 1612 Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "returning uninitialized object '" 1613 << reg_type << "'"; 1614 } else if (!return_type.IsAssignableFrom(reg_type)) { 1615 if (reg_type.IsUnresolvedTypes() || return_type.IsUnresolvedTypes()) { 1616 Fail(VERIFY_ERROR_NO_CLASS) << " can't resolve returned type '" << return_type 1617 << "' or '" << reg_type << "'"; 1618 } else { 1619 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "returning '" << reg_type 1620 << "', but expected from declaration '" << return_type << "'"; 1621 } 1622 } 1623 } 1624 } 1625 break; 1626 1627 /* could be boolean, int, float, or a null reference */ 1628 case Instruction::CONST_4: { 1629 int32_t val = static_cast<int32_t>(inst->VRegB_11n() << 28) >> 28; 1630 work_line_->SetRegisterType(inst->VRegA_11n(), 1631 DetermineCat1Constant(val, need_precise_constants_)); 1632 break; 1633 } 1634 case Instruction::CONST_16: { 1635 int16_t val = static_cast<int16_t>(inst->VRegB_21s()); 1636 work_line_->SetRegisterType(inst->VRegA_21s(), 1637 DetermineCat1Constant(val, need_precise_constants_)); 1638 break; 1639 } 1640 case Instruction::CONST: { 1641 int32_t val = inst->VRegB_31i(); 1642 work_line_->SetRegisterType(inst->VRegA_31i(), 1643 DetermineCat1Constant(val, need_precise_constants_)); 1644 break; 1645 } 1646 case Instruction::CONST_HIGH16: { 1647 int32_t val = static_cast<int32_t>(inst->VRegB_21h() << 16); 1648 work_line_->SetRegisterType(inst->VRegA_21h(), 1649 DetermineCat1Constant(val, need_precise_constants_)); 1650 break; 1651 } 1652 /* could be long or double; resolved upon use */ 1653 case Instruction::CONST_WIDE_16: { 1654 int64_t val = static_cast<int16_t>(inst->VRegB_21s()); 1655 RegType& lo = reg_types_.FromCat2ConstLo(static_cast<int32_t>(val), true); 1656 RegType& hi = reg_types_.FromCat2ConstHi(static_cast<int32_t>(val >> 32), true); 1657 work_line_->SetRegisterTypeWide(inst->VRegA_21s(), lo, hi); 1658 break; 1659 } 1660 case Instruction::CONST_WIDE_32: { 1661 int64_t val = static_cast<int32_t>(inst->VRegB_31i()); 1662 RegType& lo = reg_types_.FromCat2ConstLo(static_cast<int32_t>(val), true); 1663 RegType& hi = reg_types_.FromCat2ConstHi(static_cast<int32_t>(val >> 32), true); 1664 work_line_->SetRegisterTypeWide(inst->VRegA_31i(), lo, hi); 1665 break; 1666 } 1667 case Instruction::CONST_WIDE: { 1668 int64_t val = inst->VRegB_51l(); 1669 RegType& lo = reg_types_.FromCat2ConstLo(static_cast<int32_t>(val), true); 1670 RegType& hi = reg_types_.FromCat2ConstHi(static_cast<int32_t>(val >> 32), true); 1671 work_line_->SetRegisterTypeWide(inst->VRegA_51l(), lo, hi); 1672 break; 1673 } 1674 case Instruction::CONST_WIDE_HIGH16: { 1675 int64_t val = static_cast<uint64_t>(inst->VRegB_21h()) << 48; 1676 RegType& lo = reg_types_.FromCat2ConstLo(static_cast<int32_t>(val), true); 1677 RegType& hi = reg_types_.FromCat2ConstHi(static_cast<int32_t>(val >> 32), true); 1678 work_line_->SetRegisterTypeWide(inst->VRegA_21h(), lo, hi); 1679 break; 1680 } 1681 case Instruction::CONST_STRING: 1682 work_line_->SetRegisterType(inst->VRegA_21c(), reg_types_.JavaLangString()); 1683 break; 1684 case Instruction::CONST_STRING_JUMBO: 1685 work_line_->SetRegisterType(inst->VRegA_31c(), reg_types_.JavaLangString()); 1686 break; 1687 case Instruction::CONST_CLASS: { 1688 // Get type from instruction if unresolved then we need an access check 1689 // TODO: check Compiler::CanAccessTypeWithoutChecks returns false when res_type is unresolved 1690 RegType& res_type = ResolveClassAndCheckAccess(inst->VRegB_21c()); 1691 // Register holds class, ie its type is class, on error it will hold Conflict. 1692 work_line_->SetRegisterType(inst->VRegA_21c(), 1693 res_type.IsConflict() ? res_type 1694 : reg_types_.JavaLangClass(true)); 1695 break; 1696 } 1697 case Instruction::MONITOR_ENTER: 1698 work_line_->PushMonitor(inst->VRegA_11x(), work_insn_idx_); 1699 break; 1700 case Instruction::MONITOR_EXIT: 1701 /* 1702 * monitor-exit instructions are odd. They can throw exceptions, 1703 * but when they do they act as if they succeeded and the PC is 1704 * pointing to the following instruction. (This behavior goes back 1705 * to the need to handle asynchronous exceptions, a now-deprecated 1706 * feature that Dalvik doesn't support.) 1707 * 1708 * In practice we don't need to worry about this. The only 1709 * exceptions that can be thrown from monitor-exit are for a 1710 * null reference and -exit without a matching -enter. If the 1711 * structured locking checks are working, the former would have 1712 * failed on the -enter instruction, and the latter is impossible. 1713 * 1714 * This is fortunate, because issue 3221411 prevents us from 1715 * chasing the "can throw" path when monitor verification is 1716 * enabled. If we can fully verify the locking we can ignore 1717 * some catch blocks (which will show up as "dead" code when 1718 * we skip them here); if we can't, then the code path could be 1719 * "live" so we still need to check it. 1720 */ 1721 opcode_flags &= ~Instruction::kThrow; 1722 work_line_->PopMonitor(inst->VRegA_11x()); 1723 break; 1724 1725 case Instruction::CHECK_CAST: 1726 case Instruction::INSTANCE_OF: { 1727 /* 1728 * If this instruction succeeds, we will "downcast" register vA to the type in vB. (This 1729 * could be a "upcast" -- not expected, so we don't try to address it.) 1730 * 1731 * If it fails, an exception is thrown, which we deal with later by ignoring the update to 1732 * dec_insn.vA when branching to a handler. 1733 */ 1734 const bool is_checkcast = (inst->Opcode() == Instruction::CHECK_CAST); 1735 const uint32_t type_idx = (is_checkcast) ? inst->VRegB_21c() : inst->VRegC_22c(); 1736 RegType& res_type = ResolveClassAndCheckAccess(type_idx); 1737 if (res_type.IsConflict()) { 1738 // If this is a primitive type, fail HARD. 1739 mirror::Class* klass = (*dex_cache_)->GetResolvedType(type_idx); 1740 if (klass != nullptr && klass->IsPrimitive()) { 1741 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "using primitive type " 1742 << dex_file_->StringByTypeIdx(type_idx) << " in instanceof in " 1743 << GetDeclaringClass(); 1744 break; 1745 } 1746 1747 DCHECK_NE(failures_.size(), 0U); 1748 if (!is_checkcast) { 1749 work_line_->SetRegisterType(inst->VRegA_22c(), reg_types_.Boolean()); 1750 } 1751 break; // bad class 1752 } 1753 // TODO: check Compiler::CanAccessTypeWithoutChecks returns false when res_type is unresolved 1754 uint32_t orig_type_reg = (is_checkcast) ? inst->VRegA_21c() : inst->VRegB_22c(); 1755 RegType& orig_type = work_line_->GetRegisterType(orig_type_reg); 1756 if (!res_type.IsNonZeroReferenceTypes()) { 1757 if (is_checkcast) { 1758 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "check-cast on unexpected class " << res_type; 1759 } else { 1760 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "instance-of on unexpected class " << res_type; 1761 } 1762 } else if (!orig_type.IsReferenceTypes()) { 1763 if (is_checkcast) { 1764 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "check-cast on non-reference in v" << orig_type_reg; 1765 } else { 1766 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "instance-of on non-reference in v" << orig_type_reg; 1767 } 1768 } else { 1769 if (is_checkcast) { 1770 work_line_->SetRegisterType(inst->VRegA_21c(), res_type); 1771 } else { 1772 work_line_->SetRegisterType(inst->VRegA_22c(), reg_types_.Boolean()); 1773 } 1774 } 1775 break; 1776 } 1777 case Instruction::ARRAY_LENGTH: { 1778 RegType& res_type = work_line_->GetRegisterType(inst->VRegB_12x()); 1779 if (res_type.IsReferenceTypes()) { 1780 if (!res_type.IsArrayTypes() && !res_type.IsZero()) { // ie not an array or null 1781 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "array-length on non-array " << res_type; 1782 } else { 1783 work_line_->SetRegisterType(inst->VRegA_12x(), reg_types_.Integer()); 1784 } 1785 } else { 1786 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "array-length on non-array " << res_type; 1787 } 1788 break; 1789 } 1790 case Instruction::NEW_INSTANCE: { 1791 RegType& res_type = ResolveClassAndCheckAccess(inst->VRegB_21c()); 1792 if (res_type.IsConflict()) { 1793 DCHECK_NE(failures_.size(), 0U); 1794 break; // bad class 1795 } 1796 // TODO: check Compiler::CanAccessTypeWithoutChecks returns false when res_type is unresolved 1797 // can't create an instance of an interface or abstract class */ 1798 if (!res_type.IsInstantiableTypes()) { 1799 Fail(VERIFY_ERROR_INSTANTIATION) 1800 << "new-instance on primitive, interface or abstract class" << res_type; 1801 // Soft failure so carry on to set register type. 1802 } 1803 RegType& uninit_type = reg_types_.Uninitialized(res_type, work_insn_idx_); 1804 // Any registers holding previous allocations from this address that have not yet been 1805 // initialized must be marked invalid. 1806 work_line_->MarkUninitRefsAsInvalid(uninit_type); 1807 // add the new uninitialized reference to the register state 1808 work_line_->SetRegisterType(inst->VRegA_21c(), uninit_type); 1809 break; 1810 } 1811 case Instruction::NEW_ARRAY: 1812 VerifyNewArray(inst, false, false); 1813 break; 1814 case Instruction::FILLED_NEW_ARRAY: 1815 VerifyNewArray(inst, true, false); 1816 just_set_result = true; // Filled new array sets result register 1817 break; 1818 case Instruction::FILLED_NEW_ARRAY_RANGE: 1819 VerifyNewArray(inst, true, true); 1820 just_set_result = true; // Filled new array range sets result register 1821 break; 1822 case Instruction::CMPL_FLOAT: 1823 case Instruction::CMPG_FLOAT: 1824 if (!work_line_->VerifyRegisterType(inst->VRegB_23x(), reg_types_.Float())) { 1825 break; 1826 } 1827 if (!work_line_->VerifyRegisterType(inst->VRegC_23x(), reg_types_.Float())) { 1828 break; 1829 } 1830 work_line_->SetRegisterType(inst->VRegA_23x(), reg_types_.Integer()); 1831 break; 1832 case Instruction::CMPL_DOUBLE: 1833 case Instruction::CMPG_DOUBLE: 1834 if (!work_line_->VerifyRegisterTypeWide(inst->VRegB_23x(), reg_types_.DoubleLo(), 1835 reg_types_.DoubleHi())) { 1836 break; 1837 } 1838 if (!work_line_->VerifyRegisterTypeWide(inst->VRegC_23x(), reg_types_.DoubleLo(), 1839 reg_types_.DoubleHi())) { 1840 break; 1841 } 1842 work_line_->SetRegisterType(inst->VRegA_23x(), reg_types_.Integer()); 1843 break; 1844 case Instruction::CMP_LONG: 1845 if (!work_line_->VerifyRegisterTypeWide(inst->VRegB_23x(), reg_types_.LongLo(), 1846 reg_types_.LongHi())) { 1847 break; 1848 } 1849 if (!work_line_->VerifyRegisterTypeWide(inst->VRegC_23x(), reg_types_.LongLo(), 1850 reg_types_.LongHi())) { 1851 break; 1852 } 1853 work_line_->SetRegisterType(inst->VRegA_23x(), reg_types_.Integer()); 1854 break; 1855 case Instruction::THROW: { 1856 RegType& res_type = work_line_->GetRegisterType(inst->VRegA_11x()); 1857 if (!reg_types_.JavaLangThrowable(false).IsAssignableFrom(res_type)) { 1858 Fail(res_type.IsUnresolvedTypes() ? VERIFY_ERROR_NO_CLASS : VERIFY_ERROR_BAD_CLASS_SOFT) 1859 << "thrown class " << res_type << " not instanceof Throwable"; 1860 } 1861 break; 1862 } 1863 case Instruction::GOTO: 1864 case Instruction::GOTO_16: 1865 case Instruction::GOTO_32: 1866 /* no effect on or use of registers */ 1867 break; 1868 1869 case Instruction::PACKED_SWITCH: 1870 case Instruction::SPARSE_SWITCH: 1871 /* verify that vAA is an integer, or can be converted to one */ 1872 work_line_->VerifyRegisterType(inst->VRegA_31t(), reg_types_.Integer()); 1873 break; 1874 1875 case Instruction::FILL_ARRAY_DATA: { 1876 /* Similar to the verification done for APUT */ 1877 RegType& array_type = work_line_->GetRegisterType(inst->VRegA_31t()); 1878 /* array_type can be null if the reg type is Zero */ 1879 if (!array_type.IsZero()) { 1880 if (!array_type.IsArrayTypes()) { 1881 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid fill-array-data with array type " 1882 << array_type; 1883 } else { 1884 RegType& component_type = reg_types_.GetComponentType(array_type, 1885 class_loader_->Get()); 1886 DCHECK(!component_type.IsConflict()); 1887 if (component_type.IsNonZeroReferenceTypes()) { 1888 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid fill-array-data with component type " 1889 << component_type; 1890 } else { 1891 // Now verify if the element width in the table matches the element width declared in 1892 // the array 1893 const uint16_t* array_data = insns + (insns[1] | (((int32_t) insns[2]) << 16)); 1894 if (array_data[0] != Instruction::kArrayDataSignature) { 1895 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid magic for array-data"; 1896 } else { 1897 size_t elem_width = Primitive::ComponentSize(component_type.GetPrimitiveType()); 1898 // Since we don't compress the data in Dex, expect to see equal width of data stored 1899 // in the table and expected from the array class. 1900 if (array_data[1] != elem_width) { 1901 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "array-data size mismatch (" << array_data[1] 1902 << " vs " << elem_width << ")"; 1903 } 1904 } 1905 } 1906 } 1907 } 1908 break; 1909 } 1910 case Instruction::IF_EQ: 1911 case Instruction::IF_NE: { 1912 RegType& reg_type1 = work_line_->GetRegisterType(inst->VRegA_22t()); 1913 RegType& reg_type2 = work_line_->GetRegisterType(inst->VRegB_22t()); 1914 bool mismatch = false; 1915 if (reg_type1.IsZero()) { // zero then integral or reference expected 1916 mismatch = !reg_type2.IsReferenceTypes() && !reg_type2.IsIntegralTypes(); 1917 } else if (reg_type1.IsReferenceTypes()) { // both references? 1918 mismatch = !reg_type2.IsReferenceTypes(); 1919 } else { // both integral? 1920 mismatch = !reg_type1.IsIntegralTypes() || !reg_type2.IsIntegralTypes(); 1921 } 1922 if (mismatch) { 1923 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "args to if-eq/if-ne (" << reg_type1 << "," 1924 << reg_type2 << ") must both be references or integral"; 1925 } 1926 break; 1927 } 1928 case Instruction::IF_LT: 1929 case Instruction::IF_GE: 1930 case Instruction::IF_GT: 1931 case Instruction::IF_LE: { 1932 RegType& reg_type1 = work_line_->GetRegisterType(inst->VRegA_22t()); 1933 RegType& reg_type2 = work_line_->GetRegisterType(inst->VRegB_22t()); 1934 if (!reg_type1.IsIntegralTypes() || !reg_type2.IsIntegralTypes()) { 1935 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "args to 'if' (" << reg_type1 << "," 1936 << reg_type2 << ") must be integral"; 1937 } 1938 break; 1939 } 1940 case Instruction::IF_EQZ: 1941 case Instruction::IF_NEZ: { 1942 RegType& reg_type = work_line_->GetRegisterType(inst->VRegA_21t()); 1943 if (!reg_type.IsReferenceTypes() && !reg_type.IsIntegralTypes()) { 1944 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "type " << reg_type 1945 << " unexpected as arg to if-eqz/if-nez"; 1946 } 1947 1948 // Find previous instruction - its existence is a precondition to peephole optimization. 1949 uint32_t instance_of_idx = 0; 1950 if (0 != work_insn_idx_) { 1951 instance_of_idx = work_insn_idx_ - 1; 1952 while (0 != instance_of_idx && !insn_flags_[instance_of_idx].IsOpcode()) { 1953 instance_of_idx--; 1954 } 1955 CHECK(insn_flags_[instance_of_idx].IsOpcode()); 1956 } else { 1957 break; 1958 } 1959 1960 const Instruction* instance_of_inst = Instruction::At(code_item_->insns_ + instance_of_idx); 1961 1962 /* Check for peep-hole pattern of: 1963 * ...; 1964 * instance-of vX, vY, T; 1965 * ifXXX vX, label ; 1966 * ...; 1967 * label: 1968 * ...; 1969 * and sharpen the type of vY to be type T. 1970 * Note, this pattern can't be if: 1971 * - if there are other branches to this branch, 1972 * - when vX == vY. 1973 */ 1974 if (!CurrentInsnFlags()->IsBranchTarget() && 1975 (Instruction::INSTANCE_OF == instance_of_inst->Opcode()) && 1976 (inst->VRegA_21t() == instance_of_inst->VRegA_22c()) && 1977 (instance_of_inst->VRegA_22c() != instance_of_inst->VRegB_22c())) { 1978 // Check the type of the instance-of is different than that of registers type, as if they 1979 // are the same there is no work to be done here. Check that the conversion is not to or 1980 // from an unresolved type as type information is imprecise. If the instance-of is to an 1981 // interface then ignore the type information as interfaces can only be treated as Objects 1982 // and we don't want to disallow field and other operations on the object. If the value 1983 // being instance-of checked against is known null (zero) then allow the optimization as 1984 // we didn't have type information. If the merge of the instance-of type with the original 1985 // type is assignable to the original then allow optimization. This check is performed to 1986 // ensure that subsequent merges don't lose type information - such as becoming an 1987 // interface from a class that would lose information relevant to field checks. 1988 RegType& orig_type = work_line_->GetRegisterType(instance_of_inst->VRegB_22c()); 1989 RegType& cast_type = ResolveClassAndCheckAccess(instance_of_inst->VRegC_22c()); 1990 1991 if (!orig_type.Equals(cast_type) && 1992 !cast_type.IsUnresolvedTypes() && !orig_type.IsUnresolvedTypes() && 1993 cast_type.HasClass() && // Could be conflict type, make sure it has a class. 1994 !cast_type.GetClass()->IsInterface() && 1995 (orig_type.IsZero() || 1996 orig_type.IsStrictlyAssignableFrom(cast_type.Merge(orig_type, ®_types_)))) { 1997 RegisterLine* update_line = RegisterLine::Create(code_item_->registers_size_, this); 1998 if (inst->Opcode() == Instruction::IF_EQZ) { 1999 fallthrough_line.reset(update_line); 2000 } else { 2001 branch_line.reset(update_line); 2002 } 2003 update_line->CopyFromLine(work_line_.get()); 2004 update_line->SetRegisterType(instance_of_inst->VRegB_22c(), cast_type); 2005 if (!insn_flags_[instance_of_idx].IsBranchTarget() && 0 != instance_of_idx) { 2006 // See if instance-of was preceded by a move-object operation, common due to the small 2007 // register encoding space of instance-of, and propagate type information to the source 2008 // of the move-object. 2009 uint32_t move_idx = instance_of_idx - 1; 2010 while (0 != move_idx && !insn_flags_[move_idx].IsOpcode()) { 2011 move_idx--; 2012 } 2013 CHECK(insn_flags_[move_idx].IsOpcode()); 2014 const Instruction* move_inst = Instruction::At(code_item_->insns_ + move_idx); 2015 switch (move_inst->Opcode()) { 2016 case Instruction::MOVE_OBJECT: 2017 if (move_inst->VRegA_12x() == instance_of_inst->VRegB_22c()) { 2018 update_line->SetRegisterType(move_inst->VRegB_12x(), cast_type); 2019 } 2020 break; 2021 case Instruction::MOVE_OBJECT_FROM16: 2022 if (move_inst->VRegA_22x() == instance_of_inst->VRegB_22c()) { 2023 update_line->SetRegisterType(move_inst->VRegB_22x(), cast_type); 2024 } 2025 break; 2026 case Instruction::MOVE_OBJECT_16: 2027 if (move_inst->VRegA_32x() == instance_of_inst->VRegB_22c()) { 2028 update_line->SetRegisterType(move_inst->VRegB_32x(), cast_type); 2029 } 2030 break; 2031 default: 2032 break; 2033 } 2034 } 2035 } 2036 } 2037 2038 break; 2039 } 2040 case Instruction::IF_LTZ: 2041 case Instruction::IF_GEZ: 2042 case Instruction::IF_GTZ: 2043 case Instruction::IF_LEZ: { 2044 RegType& reg_type = work_line_->GetRegisterType(inst->VRegA_21t()); 2045 if (!reg_type.IsIntegralTypes()) { 2046 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "type " << reg_type 2047 << " unexpected as arg to if-ltz/if-gez/if-gtz/if-lez"; 2048 } 2049 break; 2050 } 2051 case Instruction::AGET_BOOLEAN: 2052 VerifyAGet(inst, reg_types_.Boolean(), true); 2053 break; 2054 case Instruction::AGET_BYTE: 2055 VerifyAGet(inst, reg_types_.Byte(), true); 2056 break; 2057 case Instruction::AGET_CHAR: 2058 VerifyAGet(inst, reg_types_.Char(), true); 2059 break; 2060 case Instruction::AGET_SHORT: 2061 VerifyAGet(inst, reg_types_.Short(), true); 2062 break; 2063 case Instruction::AGET: 2064 VerifyAGet(inst, reg_types_.Integer(), true); 2065 break; 2066 case Instruction::AGET_WIDE: 2067 VerifyAGet(inst, reg_types_.LongLo(), true); 2068 break; 2069 case Instruction::AGET_OBJECT: 2070 VerifyAGet(inst, reg_types_.JavaLangObject(false), false); 2071 break; 2072 2073 case Instruction::APUT_BOOLEAN: 2074 VerifyAPut(inst, reg_types_.Boolean(), true); 2075 break; 2076 case Instruction::APUT_BYTE: 2077 VerifyAPut(inst, reg_types_.Byte(), true); 2078 break; 2079 case Instruction::APUT_CHAR: 2080 VerifyAPut(inst, reg_types_.Char(), true); 2081 break; 2082 case Instruction::APUT_SHORT: 2083 VerifyAPut(inst, reg_types_.Short(), true); 2084 break; 2085 case Instruction::APUT: 2086 VerifyAPut(inst, reg_types_.Integer(), true); 2087 break; 2088 case Instruction::APUT_WIDE: 2089 VerifyAPut(inst, reg_types_.LongLo(), true); 2090 break; 2091 case Instruction::APUT_OBJECT: 2092 VerifyAPut(inst, reg_types_.JavaLangObject(false), false); 2093 break; 2094 2095 case Instruction::IGET_BOOLEAN: 2096 VerifyISGet(inst, reg_types_.Boolean(), true, false); 2097 break; 2098 case Instruction::IGET_BYTE: 2099 VerifyISGet(inst, reg_types_.Byte(), true, false); 2100 break; 2101 case Instruction::IGET_CHAR: 2102 VerifyISGet(inst, reg_types_.Char(), true, false); 2103 break; 2104 case Instruction::IGET_SHORT: 2105 VerifyISGet(inst, reg_types_.Short(), true, false); 2106 break; 2107 case Instruction::IGET: 2108 VerifyISGet(inst, reg_types_.Integer(), true, false); 2109 break; 2110 case Instruction::IGET_WIDE: 2111 VerifyISGet(inst, reg_types_.LongLo(), true, false); 2112 break; 2113 case Instruction::IGET_OBJECT: 2114 VerifyISGet(inst, reg_types_.JavaLangObject(false), false, false); 2115 break; 2116 2117 case Instruction::IPUT_BOOLEAN: 2118 VerifyISPut(inst, reg_types_.Boolean(), true, false); 2119 break; 2120 case Instruction::IPUT_BYTE: 2121 VerifyISPut(inst, reg_types_.Byte(), true, false); 2122 break; 2123 case Instruction::IPUT_CHAR: 2124 VerifyISPut(inst, reg_types_.Char(), true, false); 2125 break; 2126 case Instruction::IPUT_SHORT: 2127 VerifyISPut(inst, reg_types_.Short(), true, false); 2128 break; 2129 case Instruction::IPUT: 2130 VerifyISPut(inst, reg_types_.Integer(), true, false); 2131 break; 2132 case Instruction::IPUT_WIDE: 2133 VerifyISPut(inst, reg_types_.LongLo(), true, false); 2134 break; 2135 case Instruction::IPUT_OBJECT: 2136 VerifyISPut(inst, reg_types_.JavaLangObject(false), false, false); 2137 break; 2138 2139 case Instruction::SGET_BOOLEAN: 2140 VerifyISGet(inst, reg_types_.Boolean(), true, true); 2141 break; 2142 case Instruction::SGET_BYTE: 2143 VerifyISGet(inst, reg_types_.Byte(), true, true); 2144 break; 2145 case Instruction::SGET_CHAR: 2146 VerifyISGet(inst, reg_types_.Char(), true, true); 2147 break; 2148 case Instruction::SGET_SHORT: 2149 VerifyISGet(inst, reg_types_.Short(), true, true); 2150 break; 2151 case Instruction::SGET: 2152 VerifyISGet(inst, reg_types_.Integer(), true, true); 2153 break; 2154 case Instruction::SGET_WIDE: 2155 VerifyISGet(inst, reg_types_.LongLo(), true, true); 2156 break; 2157 case Instruction::SGET_OBJECT: 2158 VerifyISGet(inst, reg_types_.JavaLangObject(false), false, true); 2159 break; 2160 2161 case Instruction::SPUT_BOOLEAN: 2162 VerifyISPut(inst, reg_types_.Boolean(), true, true); 2163 break; 2164 case Instruction::SPUT_BYTE: 2165 VerifyISPut(inst, reg_types_.Byte(), true, true); 2166 break; 2167 case Instruction::SPUT_CHAR: 2168 VerifyISPut(inst, reg_types_.Char(), true, true); 2169 break; 2170 case Instruction::SPUT_SHORT: 2171 VerifyISPut(inst, reg_types_.Short(), true, true); 2172 break; 2173 case Instruction::SPUT: 2174 VerifyISPut(inst, reg_types_.Integer(), true, true); 2175 break; 2176 case Instruction::SPUT_WIDE: 2177 VerifyISPut(inst, reg_types_.LongLo(), true, true); 2178 break; 2179 case Instruction::SPUT_OBJECT: 2180 VerifyISPut(inst, reg_types_.JavaLangObject(false), false, true); 2181 break; 2182 2183 case Instruction::INVOKE_VIRTUAL: 2184 case Instruction::INVOKE_VIRTUAL_RANGE: 2185 case Instruction::INVOKE_SUPER: 2186 case Instruction::INVOKE_SUPER_RANGE: { 2187 bool is_range = (inst->Opcode() == Instruction::INVOKE_VIRTUAL_RANGE || 2188 inst->Opcode() == Instruction::INVOKE_SUPER_RANGE); 2189 bool is_super = (inst->Opcode() == Instruction::INVOKE_SUPER || 2190 inst->Opcode() == Instruction::INVOKE_SUPER_RANGE); 2191 mirror::ArtMethod* called_method = VerifyInvocationArgs(inst, METHOD_VIRTUAL, is_range, 2192 is_super); 2193 RegType* return_type = nullptr; 2194 if (called_method != nullptr) { 2195 Thread* self = Thread::Current(); 2196 StackHandleScope<1> hs(self); 2197 Handle<mirror::ArtMethod> h_called_method(hs.NewHandle(called_method)); 2198 MethodHelper mh(h_called_method); 2199 mirror::Class* return_type_class = mh.GetReturnType(can_load_classes_); 2200 if (return_type_class != nullptr) { 2201 return_type = ®_types_.FromClass(h_called_method->GetReturnTypeDescriptor(), 2202 return_type_class, 2203 return_type_class->CannotBeAssignedFromOtherTypes()); 2204 } else { 2205 DCHECK(!can_load_classes_ || self->IsExceptionPending()); 2206 self->ClearException(); 2207 } 2208 } 2209 if (return_type == nullptr) { 2210 uint32_t method_idx = (is_range) ? inst->VRegB_3rc() : inst->VRegB_35c(); 2211 const DexFile::MethodId& method_id = dex_file_->GetMethodId(method_idx); 2212 uint32_t return_type_idx = dex_file_->GetProtoId(method_id.proto_idx_).return_type_idx_; 2213 const char* descriptor = dex_file_->StringByTypeIdx(return_type_idx); 2214 return_type = ®_types_.FromDescriptor(class_loader_->Get(), descriptor, false); 2215 } 2216 if (!return_type->IsLowHalf()) { 2217 work_line_->SetResultRegisterType(*return_type); 2218 } else { 2219 work_line_->SetResultRegisterTypeWide(*return_type, return_type->HighHalf(®_types_)); 2220 } 2221 just_set_result = true; 2222 break; 2223 } 2224 case Instruction::INVOKE_DIRECT: 2225 case Instruction::INVOKE_DIRECT_RANGE: { 2226 bool is_range = (inst->Opcode() == Instruction::INVOKE_DIRECT_RANGE); 2227 mirror::ArtMethod* called_method = VerifyInvocationArgs(inst, METHOD_DIRECT, 2228 is_range, false); 2229 const char* return_type_descriptor; 2230 bool is_constructor; 2231 RegType* return_type = nullptr; 2232 if (called_method == NULL) { 2233 uint32_t method_idx = (is_range) ? inst->VRegB_3rc() : inst->VRegB_35c(); 2234 const DexFile::MethodId& method_id = dex_file_->GetMethodId(method_idx); 2235 is_constructor = strcmp("<init>", dex_file_->StringDataByIdx(method_id.name_idx_)) == 0; 2236 uint32_t return_type_idx = dex_file_->GetProtoId(method_id.proto_idx_).return_type_idx_; 2237 return_type_descriptor = dex_file_->StringByTypeIdx(return_type_idx); 2238 } else { 2239 is_constructor = called_method->IsConstructor(); 2240 return_type_descriptor = called_method->GetReturnTypeDescriptor(); 2241 Thread* self = Thread::Current(); 2242 StackHandleScope<1> hs(self); 2243 Handle<mirror::ArtMethod> h_called_method(hs.NewHandle(called_method)); 2244 MethodHelper mh(h_called_method); 2245 mirror::Class* return_type_class = mh.GetReturnType(can_load_classes_); 2246 if (return_type_class != nullptr) { 2247 return_type = ®_types_.FromClass(return_type_descriptor, 2248 return_type_class, 2249 return_type_class->CannotBeAssignedFromOtherTypes()); 2250 } else { 2251 DCHECK(!can_load_classes_ || self->IsExceptionPending()); 2252 self->ClearException(); 2253 } 2254 } 2255 if (is_constructor) { 2256 /* 2257 * Some additional checks when calling a constructor. We know from the invocation arg check 2258 * that the "this" argument is an instance of called_method->klass. Now we further restrict 2259 * that to require that called_method->klass is the same as this->klass or this->super, 2260 * allowing the latter only if the "this" argument is the same as the "this" argument to 2261 * this method (which implies that we're in a constructor ourselves). 2262 */ 2263 RegType& this_type = work_line_->GetInvocationThis(inst, is_range); 2264 if (this_type.IsConflict()) // failure. 2265 break; 2266 2267 /* no null refs allowed (?) */ 2268 if (this_type.IsZero()) { 2269 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unable to initialize null ref"; 2270 break; 2271 } 2272 2273 /* must be in same class or in superclass */ 2274 // RegType& this_super_klass = this_type.GetSuperClass(®_types_); 2275 // TODO: re-enable constructor type verification 2276 // if (this_super_klass.IsConflict()) { 2277 // Unknown super class, fail so we re-check at runtime. 2278 // Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "super class unknown for '" << this_type << "'"; 2279 // break; 2280 // } 2281 2282 /* arg must be an uninitialized reference */ 2283 if (!this_type.IsUninitializedTypes()) { 2284 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Expected initialization on uninitialized reference " 2285 << this_type; 2286 break; 2287 } 2288 2289 /* 2290 * Replace the uninitialized reference with an initialized one. We need to do this for all 2291 * registers that have the same object instance in them, not just the "this" register. 2292 */ 2293 work_line_->MarkRefsAsInitialized(this_type); 2294 } 2295 if (return_type == nullptr) { 2296 return_type = ®_types_.FromDescriptor(class_loader_->Get(), 2297 return_type_descriptor, false); 2298 } 2299 if (!return_type->IsLowHalf()) { 2300 work_line_->SetResultRegisterType(*return_type); 2301 } else { 2302 work_line_->SetResultRegisterTypeWide(*return_type, return_type->HighHalf(®_types_)); 2303 } 2304 just_set_result = true; 2305 break; 2306 } 2307 case Instruction::INVOKE_STATIC: 2308 case Instruction::INVOKE_STATIC_RANGE: { 2309 bool is_range = (inst->Opcode() == Instruction::INVOKE_STATIC_RANGE); 2310 mirror::ArtMethod* called_method = VerifyInvocationArgs(inst, 2311 METHOD_STATIC, 2312 is_range, 2313 false); 2314 const char* descriptor; 2315 if (called_method == NULL) { 2316 uint32_t method_idx = (is_range) ? inst->VRegB_3rc() : inst->VRegB_35c(); 2317 const DexFile::MethodId& method_id = dex_file_->GetMethodId(method_idx); 2318 uint32_t return_type_idx = dex_file_->GetProtoId(method_id.proto_idx_).return_type_idx_; 2319 descriptor = dex_file_->StringByTypeIdx(return_type_idx); 2320 } else { 2321 descriptor = called_method->GetReturnTypeDescriptor(); 2322 } 2323 RegType& return_type = reg_types_.FromDescriptor(class_loader_->Get(), descriptor, 2324 false); 2325 if (!return_type.IsLowHalf()) { 2326 work_line_->SetResultRegisterType(return_type); 2327 } else { 2328 work_line_->SetResultRegisterTypeWide(return_type, return_type.HighHalf(®_types_)); 2329 } 2330 just_set_result = true; 2331 } 2332 break; 2333 case Instruction::INVOKE_INTERFACE: 2334 case Instruction::INVOKE_INTERFACE_RANGE: { 2335 bool is_range = (inst->Opcode() == Instruction::INVOKE_INTERFACE_RANGE); 2336 mirror::ArtMethod* abs_method = VerifyInvocationArgs(inst, 2337 METHOD_INTERFACE, 2338 is_range, 2339 false); 2340 if (abs_method != NULL) { 2341 mirror::Class* called_interface = abs_method->GetDeclaringClass(); 2342 if (!called_interface->IsInterface() && !called_interface->IsObjectClass()) { 2343 Fail(VERIFY_ERROR_CLASS_CHANGE) << "expected interface class in invoke-interface '" 2344 << PrettyMethod(abs_method) << "'"; 2345 break; 2346 } 2347 } 2348 /* Get the type of the "this" arg, which should either be a sub-interface of called 2349 * interface or Object (see comments in RegType::JoinClass). 2350 */ 2351 RegType& this_type = work_line_->GetInvocationThis(inst, is_range); 2352 if (this_type.IsZero()) { 2353 /* null pointer always passes (and always fails at runtime) */ 2354 } else { 2355 if (this_type.IsUninitializedTypes()) { 2356 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "interface call on uninitialized object " 2357 << this_type; 2358 break; 2359 } 2360 // In the past we have tried to assert that "called_interface" is assignable 2361 // from "this_type.GetClass()", however, as we do an imprecise Join 2362 // (RegType::JoinClass) we don't have full information on what interfaces are 2363 // implemented by "this_type". For example, two classes may implement the same 2364 // interfaces and have a common parent that doesn't implement the interface. The 2365 // join will set "this_type" to the parent class and a test that this implements 2366 // the interface will incorrectly fail. 2367 } 2368 /* 2369 * We don't have an object instance, so we can't find the concrete method. However, all of 2370 * the type information is in the abstract method, so we're good. 2371 */ 2372 const char* descriptor; 2373 if (abs_method == NULL) { 2374 uint32_t method_idx = (is_range) ? inst->VRegB_3rc() : inst->VRegB_35c(); 2375 const DexFile::MethodId& method_id = dex_file_->GetMethodId(method_idx); 2376 uint32_t return_type_idx = dex_file_->GetProtoId(method_id.proto_idx_).return_type_idx_; 2377 descriptor = dex_file_->StringByTypeIdx(return_type_idx); 2378 } else { 2379 descriptor = abs_method->GetReturnTypeDescriptor(); 2380 } 2381 RegType& return_type = reg_types_.FromDescriptor(class_loader_->Get(), descriptor, 2382 false); 2383 if (!return_type.IsLowHalf()) { 2384 work_line_->SetResultRegisterType(return_type); 2385 } else { 2386 work_line_->SetResultRegisterTypeWide(return_type, return_type.HighHalf(®_types_)); 2387 } 2388 just_set_result = true; 2389 break; 2390 } 2391 case Instruction::NEG_INT: 2392 case Instruction::NOT_INT: 2393 work_line_->CheckUnaryOp(inst, reg_types_.Integer(), reg_types_.Integer()); 2394 break; 2395 case Instruction::NEG_LONG: 2396 case Instruction::NOT_LONG: 2397 work_line_->CheckUnaryOpWide(inst, reg_types_.LongLo(), reg_types_.LongHi(), 2398 reg_types_.LongLo(), reg_types_.LongHi()); 2399 break; 2400 case Instruction::NEG_FLOAT: 2401 work_line_->CheckUnaryOp(inst, reg_types_.Float(), reg_types_.Float()); 2402 break; 2403 case Instruction::NEG_DOUBLE: 2404 work_line_->CheckUnaryOpWide(inst, reg_types_.DoubleLo(), reg_types_.DoubleHi(), 2405 reg_types_.DoubleLo(), reg_types_.DoubleHi()); 2406 break; 2407 case Instruction::INT_TO_LONG: 2408 work_line_->CheckUnaryOpToWide(inst, reg_types_.LongLo(), reg_types_.LongHi(), 2409 reg_types_.Integer()); 2410 break; 2411 case Instruction::INT_TO_FLOAT: 2412 work_line_->CheckUnaryOp(inst, reg_types_.Float(), reg_types_.Integer()); 2413 break; 2414 case Instruction::INT_TO_DOUBLE: 2415 work_line_->CheckUnaryOpToWide(inst, reg_types_.DoubleLo(), reg_types_.DoubleHi(), 2416 reg_types_.Integer()); 2417 break; 2418 case Instruction::LONG_TO_INT: 2419 work_line_->CheckUnaryOpFromWide(inst, reg_types_.Integer(), 2420 reg_types_.LongLo(), reg_types_.LongHi()); 2421 break; 2422 case Instruction::LONG_TO_FLOAT: 2423 work_line_->CheckUnaryOpFromWide(inst, reg_types_.Float(), 2424 reg_types_.LongLo(), reg_types_.LongHi()); 2425 break; 2426 case Instruction::LONG_TO_DOUBLE: 2427 work_line_->CheckUnaryOpWide(inst, reg_types_.DoubleLo(), reg_types_.DoubleHi(), 2428 reg_types_.LongLo(), reg_types_.LongHi()); 2429 break; 2430 case Instruction::FLOAT_TO_INT: 2431 work_line_->CheckUnaryOp(inst, reg_types_.Integer(), reg_types_.Float()); 2432 break; 2433 case Instruction::FLOAT_TO_LONG: 2434 work_line_->CheckUnaryOpToWide(inst, reg_types_.LongLo(), reg_types_.LongHi(), 2435 reg_types_.Float()); 2436 break; 2437 case Instruction::FLOAT_TO_DOUBLE: 2438 work_line_->CheckUnaryOpToWide(inst, reg_types_.DoubleLo(), reg_types_.DoubleHi(), 2439 reg_types_.Float()); 2440 break; 2441 case Instruction::DOUBLE_TO_INT: 2442 work_line_->CheckUnaryOpFromWide(inst, reg_types_.Integer(), 2443 reg_types_.DoubleLo(), reg_types_.DoubleHi()); 2444 break; 2445 case Instruction::DOUBLE_TO_LONG: 2446 work_line_->CheckUnaryOpWide(inst, reg_types_.LongLo(), reg_types_.LongHi(), 2447 reg_types_.DoubleLo(), reg_types_.DoubleHi()); 2448 break; 2449 case Instruction::DOUBLE_TO_FLOAT: 2450 work_line_->CheckUnaryOpFromWide(inst, reg_types_.Float(), 2451 reg_types_.DoubleLo(), reg_types_.DoubleHi()); 2452 break; 2453 case Instruction::INT_TO_BYTE: 2454 work_line_->CheckUnaryOp(inst, reg_types_.Byte(), reg_types_.Integer()); 2455 break; 2456 case Instruction::INT_TO_CHAR: 2457 work_line_->CheckUnaryOp(inst, reg_types_.Char(), reg_types_.Integer()); 2458 break; 2459 case Instruction::INT_TO_SHORT: 2460 work_line_->CheckUnaryOp(inst, reg_types_.Short(), reg_types_.Integer()); 2461 break; 2462 2463 case Instruction::ADD_INT: 2464 case Instruction::SUB_INT: 2465 case Instruction::MUL_INT: 2466 case Instruction::REM_INT: 2467 case Instruction::DIV_INT: 2468 case Instruction::SHL_INT: 2469 case Instruction::SHR_INT: 2470 case Instruction::USHR_INT: 2471 work_line_->CheckBinaryOp(inst, reg_types_.Integer(), reg_types_.Integer(), 2472 reg_types_.Integer(), false); 2473 break; 2474 case Instruction::AND_INT: 2475 case Instruction::OR_INT: 2476 case Instruction::XOR_INT: 2477 work_line_->CheckBinaryOp(inst, reg_types_.Integer(), reg_types_.Integer(), 2478 reg_types_.Integer(), true); 2479 break; 2480 case Instruction::ADD_LONG: 2481 case Instruction::SUB_LONG: 2482 case Instruction::MUL_LONG: 2483 case Instruction::DIV_LONG: 2484 case Instruction::REM_LONG: 2485 case Instruction::AND_LONG: 2486 case Instruction::OR_LONG: 2487 case Instruction::XOR_LONG: 2488 work_line_->CheckBinaryOpWide(inst, reg_types_.LongLo(), reg_types_.LongHi(), 2489 reg_types_.LongLo(), reg_types_.LongHi(), 2490 reg_types_.LongLo(), reg_types_.LongHi()); 2491 break; 2492 case Instruction::SHL_LONG: 2493 case Instruction::SHR_LONG: 2494 case Instruction::USHR_LONG: 2495 /* shift distance is Int, making these different from other binary operations */ 2496 work_line_->CheckBinaryOpWideShift(inst, reg_types_.LongLo(), reg_types_.LongHi(), 2497 reg_types_.Integer()); 2498 break; 2499 case Instruction::ADD_FLOAT: 2500 case Instruction::SUB_FLOAT: 2501 case Instruction::MUL_FLOAT: 2502 case Instruction::DIV_FLOAT: 2503 case Instruction::REM_FLOAT: 2504 work_line_->CheckBinaryOp(inst, 2505 reg_types_.Float(), 2506 reg_types_.Float(), 2507 reg_types_.Float(), 2508 false); 2509 break; 2510 case Instruction::ADD_DOUBLE: 2511 case Instruction::SUB_DOUBLE: 2512 case Instruction::MUL_DOUBLE: 2513 case Instruction::DIV_DOUBLE: 2514 case Instruction::REM_DOUBLE: 2515 work_line_->CheckBinaryOpWide(inst, reg_types_.DoubleLo(), reg_types_.DoubleHi(), 2516 reg_types_.DoubleLo(), reg_types_.DoubleHi(), 2517 reg_types_.DoubleLo(), reg_types_.DoubleHi()); 2518 break; 2519 case Instruction::ADD_INT_2ADDR: 2520 case Instruction::SUB_INT_2ADDR: 2521 case Instruction::MUL_INT_2ADDR: 2522 case Instruction::REM_INT_2ADDR: 2523 case Instruction::SHL_INT_2ADDR: 2524 case Instruction::SHR_INT_2ADDR: 2525 case Instruction::USHR_INT_2ADDR: 2526 work_line_->CheckBinaryOp2addr(inst, 2527 reg_types_.Integer(), 2528 reg_types_.Integer(), 2529 reg_types_.Integer(), 2530 false); 2531 break; 2532 case Instruction::AND_INT_2ADDR: 2533 case Instruction::OR_INT_2ADDR: 2534 case Instruction::XOR_INT_2ADDR: 2535 work_line_->CheckBinaryOp2addr(inst, 2536 reg_types_.Integer(), 2537 reg_types_.Integer(), 2538 reg_types_.Integer(), 2539 true); 2540 break; 2541 case Instruction::DIV_INT_2ADDR: 2542 work_line_->CheckBinaryOp2addr(inst, 2543 reg_types_.Integer(), 2544 reg_types_.Integer(), 2545 reg_types_.Integer(), 2546 false); 2547 break; 2548 case Instruction::ADD_LONG_2ADDR: 2549 case Instruction::SUB_LONG_2ADDR: 2550 case Instruction::MUL_LONG_2ADDR: 2551 case Instruction::DIV_LONG_2ADDR: 2552 case Instruction::REM_LONG_2ADDR: 2553 case Instruction::AND_LONG_2ADDR: 2554 case Instruction::OR_LONG_2ADDR: 2555 case Instruction::XOR_LONG_2ADDR: 2556 work_line_->CheckBinaryOp2addrWide(inst, reg_types_.LongLo(), reg_types_.LongHi(), 2557 reg_types_.LongLo(), reg_types_.LongHi(), 2558 reg_types_.LongLo(), reg_types_.LongHi()); 2559 break; 2560 case Instruction::SHL_LONG_2ADDR: 2561 case Instruction::SHR_LONG_2ADDR: 2562 case Instruction::USHR_LONG_2ADDR: 2563 work_line_->CheckBinaryOp2addrWideShift(inst, reg_types_.LongLo(), reg_types_.LongHi(), 2564 reg_types_.Integer()); 2565 break; 2566 case Instruction::ADD_FLOAT_2ADDR: 2567 case Instruction::SUB_FLOAT_2ADDR: 2568 case Instruction::MUL_FLOAT_2ADDR: 2569 case Instruction::DIV_FLOAT_2ADDR: 2570 case Instruction::REM_FLOAT_2ADDR: 2571 work_line_->CheckBinaryOp2addr(inst, 2572 reg_types_.Float(), 2573 reg_types_.Float(), 2574 reg_types_.Float(), 2575 false); 2576 break; 2577 case Instruction::ADD_DOUBLE_2ADDR: 2578 case Instruction::SUB_DOUBLE_2ADDR: 2579 case Instruction::MUL_DOUBLE_2ADDR: 2580 case Instruction::DIV_DOUBLE_2ADDR: 2581 case Instruction::REM_DOUBLE_2ADDR: 2582 work_line_->CheckBinaryOp2addrWide(inst, reg_types_.DoubleLo(), reg_types_.DoubleHi(), 2583 reg_types_.DoubleLo(), reg_types_.DoubleHi(), 2584 reg_types_.DoubleLo(), reg_types_.DoubleHi()); 2585 break; 2586 case Instruction::ADD_INT_LIT16: 2587 case Instruction::RSUB_INT: 2588 case Instruction::MUL_INT_LIT16: 2589 case Instruction::DIV_INT_LIT16: 2590 case Instruction::REM_INT_LIT16: 2591 work_line_->CheckLiteralOp(inst, reg_types_.Integer(), reg_types_.Integer(), false, true); 2592 break; 2593 case Instruction::AND_INT_LIT16: 2594 case Instruction::OR_INT_LIT16: 2595 case Instruction::XOR_INT_LIT16: 2596 work_line_->CheckLiteralOp(inst, reg_types_.Integer(), reg_types_.Integer(), true, true); 2597 break; 2598 case Instruction::ADD_INT_LIT8: 2599 case Instruction::RSUB_INT_LIT8: 2600 case Instruction::MUL_INT_LIT8: 2601 case Instruction::DIV_INT_LIT8: 2602 case Instruction::REM_INT_LIT8: 2603 case Instruction::SHL_INT_LIT8: 2604 case Instruction::SHR_INT_LIT8: 2605 case Instruction::USHR_INT_LIT8: 2606 work_line_->CheckLiteralOp(inst, reg_types_.Integer(), reg_types_.Integer(), false, false); 2607 break; 2608 case Instruction::AND_INT_LIT8: 2609 case Instruction::OR_INT_LIT8: 2610 case Instruction::XOR_INT_LIT8: 2611 work_line_->CheckLiteralOp(inst, reg_types_.Integer(), reg_types_.Integer(), true, false); 2612 break; 2613 2614 // Special instructions. 2615 case Instruction::RETURN_VOID_BARRIER: 2616 if (!IsConstructor() || IsStatic()) { 2617 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "return-void-barrier not expected"; 2618 } 2619 break; 2620 // Note: the following instructions encode offsets derived from class linking. 2621 // As such they use Class*/Field*/AbstractMethod* as these offsets only have 2622 // meaning if the class linking and resolution were successful. 2623 case Instruction::IGET_QUICK: 2624 VerifyIGetQuick(inst, reg_types_.Integer(), true); 2625 break; 2626 case Instruction::IGET_WIDE_QUICK: 2627 VerifyIGetQuick(inst, reg_types_.LongLo(), true); 2628 break; 2629 case Instruction::IGET_OBJECT_QUICK: 2630 VerifyIGetQuick(inst, reg_types_.JavaLangObject(false), false); 2631 break; 2632 case Instruction::IPUT_QUICK: 2633 VerifyIPutQuick(inst, reg_types_.Integer(), true); 2634 break; 2635 case Instruction::IPUT_WIDE_QUICK: 2636 VerifyIPutQuick(inst, reg_types_.LongLo(), true); 2637 break; 2638 case Instruction::IPUT_OBJECT_QUICK: 2639 VerifyIPutQuick(inst, reg_types_.JavaLangObject(false), false); 2640 break; 2641 case Instruction::INVOKE_VIRTUAL_QUICK: 2642 case Instruction::INVOKE_VIRTUAL_RANGE_QUICK: { 2643 bool is_range = (inst->Opcode() == Instruction::INVOKE_VIRTUAL_RANGE_QUICK); 2644 mirror::ArtMethod* called_method = VerifyInvokeVirtualQuickArgs(inst, is_range); 2645 if (called_method != NULL) { 2646 const char* descriptor = called_method->GetReturnTypeDescriptor(); 2647 RegType& return_type = reg_types_.FromDescriptor(class_loader_->Get(), descriptor, 2648 false); 2649 if (!return_type.IsLowHalf()) { 2650 work_line_->SetResultRegisterType(return_type); 2651 } else { 2652 work_line_->SetResultRegisterTypeWide(return_type, return_type.HighHalf(®_types_)); 2653 } 2654 just_set_result = true; 2655 } 2656 break; 2657 } 2658 2659 /* These should never appear during verification. */ 2660 case Instruction::UNUSED_3E: 2661 case Instruction::UNUSED_3F: 2662 case Instruction::UNUSED_40: 2663 case Instruction::UNUSED_41: 2664 case Instruction::UNUSED_42: 2665 case Instruction::UNUSED_43: 2666 case Instruction::UNUSED_79: 2667 case Instruction::UNUSED_7A: 2668 case Instruction::UNUSED_EB: 2669 case Instruction::UNUSED_EC: 2670 case Instruction::UNUSED_ED: 2671 case Instruction::UNUSED_EE: 2672 case Instruction::UNUSED_EF: 2673 case Instruction::UNUSED_F0: 2674 case Instruction::UNUSED_F1: 2675 case Instruction::UNUSED_F2: 2676 case Instruction::UNUSED_F3: 2677 case Instruction::UNUSED_F4: 2678 case Instruction::UNUSED_F5: 2679 case Instruction::UNUSED_F6: 2680 case Instruction::UNUSED_F7: 2681 case Instruction::UNUSED_F8: 2682 case Instruction::UNUSED_F9: 2683 case Instruction::UNUSED_FA: 2684 case Instruction::UNUSED_FB: 2685 case Instruction::UNUSED_FC: 2686 case Instruction::UNUSED_FD: 2687 case Instruction::UNUSED_FE: 2688 case Instruction::UNUSED_FF: 2689 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Unexpected opcode " << inst->DumpString(dex_file_); 2690 break; 2691 2692 /* 2693 * DO NOT add a "default" clause here. Without it the compiler will 2694 * complain if an instruction is missing (which is desirable). 2695 */ 2696 } // end - switch (dec_insn.opcode) 2697 2698 if (have_pending_hard_failure_) { 2699 if (Runtime::Current()->IsCompiler()) { 2700 /* When compiling, check that the last failure is a hard failure */ 2701 CHECK_EQ(failures_[failures_.size() - 1], VERIFY_ERROR_BAD_CLASS_HARD); 2702 } 2703 /* immediate failure, reject class */ 2704 info_messages_ << "Rejecting opcode " << inst->DumpString(dex_file_); 2705 return false; 2706 } else if (have_pending_runtime_throw_failure_) { 2707 /* checking interpreter will throw, mark following code as unreachable */ 2708 opcode_flags = Instruction::kThrow; 2709 } 2710 /* 2711 * If we didn't just set the result register, clear it out. This ensures that you can only use 2712 * "move-result" immediately after the result is set. (We could check this statically, but it's 2713 * not expensive and it makes our debugging output cleaner.) 2714 */ 2715 if (!just_set_result) { 2716 work_line_->SetResultTypeToUnknown(); 2717 } 2718 2719 2720 2721 /* 2722 * Handle "branch". Tag the branch target. 2723 * 2724 * NOTE: instructions like Instruction::EQZ provide information about the 2725 * state of the register when the branch is taken or not taken. For example, 2726 * somebody could get a reference field, check it for zero, and if the 2727 * branch is taken immediately store that register in a boolean field 2728 * since the value is known to be zero. We do not currently account for 2729 * that, and will reject the code. 2730 * 2731 * TODO: avoid re-fetching the branch target 2732 */ 2733 if ((opcode_flags & Instruction::kBranch) != 0) { 2734 bool isConditional, selfOkay; 2735 if (!GetBranchOffset(work_insn_idx_, &branch_target, &isConditional, &selfOkay)) { 2736 /* should never happen after static verification */ 2737 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad branch"; 2738 return false; 2739 } 2740 DCHECK_EQ(isConditional, (opcode_flags & Instruction::kContinue) != 0); 2741 if (!CheckNotMoveException(code_item_->insns_, work_insn_idx_ + branch_target)) { 2742 return false; 2743 } 2744 /* update branch target, set "changed" if appropriate */ 2745 if (NULL != branch_line.get()) { 2746 if (!UpdateRegisters(work_insn_idx_ + branch_target, branch_line.get(), false)) { 2747 return false; 2748 } 2749 } else { 2750 if (!UpdateRegisters(work_insn_idx_ + branch_target, work_line_.get(), false)) { 2751 return false; 2752 } 2753 } 2754 } 2755 2756 /* 2757 * Handle "switch". Tag all possible branch targets. 2758 * 2759 * We've already verified that the table is structurally sound, so we 2760 * just need to walk through and tag the targets. 2761 */ 2762 if ((opcode_flags & Instruction::kSwitch) != 0) { 2763 int offset_to_switch = insns[1] | (((int32_t) insns[2]) << 16); 2764 const uint16_t* switch_insns = insns + offset_to_switch; 2765 int switch_count = switch_insns[1]; 2766 int offset_to_targets, targ; 2767 2768 if ((*insns & 0xff) == Instruction::PACKED_SWITCH) { 2769 /* 0 = sig, 1 = count, 2/3 = first key */ 2770 offset_to_targets = 4; 2771 } else { 2772 /* 0 = sig, 1 = count, 2..count * 2 = keys */ 2773 DCHECK((*insns & 0xff) == Instruction::SPARSE_SWITCH); 2774 offset_to_targets = 2 + 2 * switch_count; 2775 } 2776 2777 /* verify each switch target */ 2778 for (targ = 0; targ < switch_count; targ++) { 2779 int offset; 2780 uint32_t abs_offset; 2781 2782 /* offsets are 32-bit, and only partly endian-swapped */ 2783 offset = switch_insns[offset_to_targets + targ * 2] | 2784 (((int32_t) switch_insns[offset_to_targets + targ * 2 + 1]) << 16); 2785 abs_offset = work_insn_idx_ + offset; 2786 DCHECK_LT(abs_offset, code_item_->insns_size_in_code_units_); 2787 if (!CheckNotMoveException(code_item_->insns_, abs_offset)) { 2788 return false; 2789 } 2790 if (!UpdateRegisters(abs_offset, work_line_.get(), false)) { 2791 return false; 2792 } 2793 } 2794 } 2795 2796 /* 2797 * Handle instructions that can throw and that are sitting in a "try" block. (If they're not in a 2798 * "try" block when they throw, control transfers out of the method.) 2799 */ 2800 if ((opcode_flags & Instruction::kThrow) != 0 && insn_flags_[work_insn_idx_].IsInTry()) { 2801 bool has_catch_all_handler = false; 2802 CatchHandlerIterator iterator(*code_item_, work_insn_idx_); 2803 2804 // Need the linker to try and resolve the handled class to check if it's Throwable. 2805 ClassLinker* linker = Runtime::Current()->GetClassLinker(); 2806 2807 for (; iterator.HasNext(); iterator.Next()) { 2808 uint16_t handler_type_idx = iterator.GetHandlerTypeIndex(); 2809 if (handler_type_idx == DexFile::kDexNoIndex16) { 2810 has_catch_all_handler = true; 2811 } else { 2812 // It is also a catch-all if it is java.lang.Throwable. 2813 mirror::Class* klass = linker->ResolveType(*dex_file_, handler_type_idx, *dex_cache_, 2814 *class_loader_); 2815 if (klass != nullptr) { 2816 if (klass == mirror::Throwable::GetJavaLangThrowable()) { 2817 has_catch_all_handler = true; 2818 } 2819 } else { 2820 // Clear exception. 2821 Thread* self = Thread::Current(); 2822 DCHECK(self->IsExceptionPending()); 2823 self->ClearException(); 2824 } 2825 } 2826 /* 2827 * Merge registers into the "catch" block. We want to use the "savedRegs" rather than 2828 * "work_regs", because at runtime the exception will be thrown before the instruction 2829 * modifies any registers. 2830 */ 2831 if (!UpdateRegisters(iterator.GetHandlerAddress(), saved_line_.get(), false)) { 2832 return false; 2833 } 2834 } 2835 2836 /* 2837 * If the monitor stack depth is nonzero, there must be a "catch all" handler for this 2838 * instruction. This does apply to monitor-exit because of async exception handling. 2839 */ 2840 if (work_line_->MonitorStackDepth() > 0 && !has_catch_all_handler) { 2841 /* 2842 * The state in work_line reflects the post-execution state. If the current instruction is a 2843 * monitor-enter and the monitor stack was empty, we don't need a catch-all (if it throws, 2844 * it will do so before grabbing the lock). 2845 */ 2846 if (inst->Opcode() != Instruction::MONITOR_ENTER || work_line_->MonitorStackDepth() != 1) { 2847 Fail(VERIFY_ERROR_BAD_CLASS_HARD) 2848 << "expected to be within a catch-all for an instruction where a monitor is held"; 2849 return false; 2850 } 2851 } 2852 } 2853 2854 /* Handle "continue". Tag the next consecutive instruction. 2855 * Note: Keep the code handling "continue" case below the "branch" and "switch" cases, 2856 * because it changes work_line_ when performing peephole optimization 2857 * and this change should not be used in those cases. 2858 */ 2859 if ((opcode_flags & Instruction::kContinue) != 0) { 2860 uint32_t next_insn_idx = work_insn_idx_ + CurrentInsnFlags()->GetLengthInCodeUnits(); 2861 if (next_insn_idx >= code_item_->insns_size_in_code_units_) { 2862 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Execution can walk off end of code area"; 2863 return false; 2864 } 2865 // The only way to get to a move-exception instruction is to get thrown there. Make sure the 2866 // next instruction isn't one. 2867 if (!CheckNotMoveException(code_item_->insns_, next_insn_idx)) { 2868 return false; 2869 } 2870 if (NULL != fallthrough_line.get()) { 2871 // Make workline consistent with fallthrough computed from peephole optimization. 2872 work_line_->CopyFromLine(fallthrough_line.get()); 2873 } 2874 if (insn_flags_[next_insn_idx].IsReturn()) { 2875 // For returns we only care about the operand to the return, all other registers are dead. 2876 const Instruction* ret_inst = Instruction::At(code_item_->insns_ + next_insn_idx); 2877 Instruction::Code opcode = ret_inst->Opcode(); 2878 if ((opcode == Instruction::RETURN_VOID) || (opcode == Instruction::RETURN_VOID_BARRIER)) { 2879 work_line_->MarkAllRegistersAsConflicts(); 2880 } else { 2881 if (opcode == Instruction::RETURN_WIDE) { 2882 work_line_->MarkAllRegistersAsConflictsExceptWide(ret_inst->VRegA_11x()); 2883 } else { 2884 work_line_->MarkAllRegistersAsConflictsExcept(ret_inst->VRegA_11x()); 2885 } 2886 } 2887 } 2888 RegisterLine* next_line = reg_table_.GetLine(next_insn_idx); 2889 if (next_line != NULL) { 2890 // Merge registers into what we have for the next instruction, and set the "changed" flag if 2891 // needed. If the merge changes the state of the registers then the work line will be 2892 // updated. 2893 if (!UpdateRegisters(next_insn_idx, work_line_.get(), true)) { 2894 return false; 2895 } 2896 } else { 2897 /* 2898 * We're not recording register data for the next instruction, so we don't know what the 2899 * prior state was. We have to assume that something has changed and re-evaluate it. 2900 */ 2901 insn_flags_[next_insn_idx].SetChanged(); 2902 } 2903 } 2904 2905 /* If we're returning from the method, make sure monitor stack is empty. */ 2906 if ((opcode_flags & Instruction::kReturn) != 0) { 2907 if (!work_line_->VerifyMonitorStackEmpty()) { 2908 return false; 2909 } 2910 } 2911 2912 /* 2913 * Update start_guess. Advance to the next instruction of that's 2914 * possible, otherwise use the branch target if one was found. If 2915 * neither of those exists we're in a return or throw; leave start_guess 2916 * alone and let the caller sort it out. 2917 */ 2918 if ((opcode_flags & Instruction::kContinue) != 0) { 2919 *start_guess = work_insn_idx_ + insn_flags_[work_insn_idx_].GetLengthInCodeUnits(); 2920 } else if ((opcode_flags & Instruction::kBranch) != 0) { 2921 /* we're still okay if branch_target is zero */ 2922 *start_guess = work_insn_idx_ + branch_target; 2923 } 2924 2925 DCHECK_LT(*start_guess, code_item_->insns_size_in_code_units_); 2926 DCHECK(insn_flags_[*start_guess].IsOpcode()); 2927 2928 return true; 2929} // NOLINT(readability/fn_size) 2930 2931RegType& MethodVerifier::ResolveClassAndCheckAccess(uint32_t class_idx) { 2932 const char* descriptor = dex_file_->StringByTypeIdx(class_idx); 2933 RegType& referrer = GetDeclaringClass(); 2934 mirror::Class* klass = (*dex_cache_)->GetResolvedType(class_idx); 2935 RegType& result = 2936 klass != NULL ? reg_types_.FromClass(descriptor, klass, 2937 klass->CannotBeAssignedFromOtherTypes()) 2938 : reg_types_.FromDescriptor(class_loader_->Get(), descriptor, false); 2939 if (result.IsConflict()) { 2940 Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "accessing broken descriptor '" << descriptor 2941 << "' in " << referrer; 2942 return result; 2943 } 2944 if (klass == NULL && !result.IsUnresolvedTypes()) { 2945 (*dex_cache_)->SetResolvedType(class_idx, result.GetClass()); 2946 } 2947 // Check if access is allowed. Unresolved types use xxxWithAccessCheck to 2948 // check at runtime if access is allowed and so pass here. If result is 2949 // primitive, skip the access check. 2950 if (result.IsNonZeroReferenceTypes() && !result.IsUnresolvedTypes() && 2951 !referrer.IsUnresolvedTypes() && !referrer.CanAccess(result)) { 2952 Fail(VERIFY_ERROR_ACCESS_CLASS) << "illegal class access: '" 2953 << referrer << "' -> '" << result << "'"; 2954 } 2955 return result; 2956} 2957 2958RegType& MethodVerifier::GetCaughtExceptionType() { 2959 RegType* common_super = NULL; 2960 if (code_item_->tries_size_ != 0) { 2961 const byte* handlers_ptr = DexFile::GetCatchHandlerData(*code_item_, 0); 2962 uint32_t handlers_size = DecodeUnsignedLeb128(&handlers_ptr); 2963 for (uint32_t i = 0; i < handlers_size; i++) { 2964 CatchHandlerIterator iterator(handlers_ptr); 2965 for (; iterator.HasNext(); iterator.Next()) { 2966 if (iterator.GetHandlerAddress() == (uint32_t) work_insn_idx_) { 2967 if (iterator.GetHandlerTypeIndex() == DexFile::kDexNoIndex16) { 2968 common_super = ®_types_.JavaLangThrowable(false); 2969 } else { 2970 RegType& exception = ResolveClassAndCheckAccess(iterator.GetHandlerTypeIndex()); 2971 if (!reg_types_.JavaLangThrowable(false).IsAssignableFrom(exception)) { 2972 if (exception.IsUnresolvedTypes()) { 2973 // We don't know enough about the type. Fail here and let runtime handle it. 2974 Fail(VERIFY_ERROR_NO_CLASS) << "unresolved exception class " << exception; 2975 return exception; 2976 } else { 2977 Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "unexpected non-exception class " << exception; 2978 return reg_types_.Conflict(); 2979 } 2980 } else if (common_super == nullptr) { 2981 common_super = &exception; 2982 } else if (common_super->Equals(exception)) { 2983 // odd case, but nothing to do 2984 } else { 2985 common_super = &common_super->Merge(exception, ®_types_); 2986 CHECK(reg_types_.JavaLangThrowable(false).IsAssignableFrom(*common_super)); 2987 } 2988 } 2989 } 2990 } 2991 handlers_ptr = iterator.EndDataPointer(); 2992 } 2993 } 2994 if (common_super == NULL) { 2995 /* no catch blocks, or no catches with classes we can find */ 2996 Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "unable to find exception handler"; 2997 return reg_types_.Conflict(); 2998 } 2999 return *common_super; 3000} 3001 3002mirror::ArtMethod* MethodVerifier::ResolveMethodAndCheckAccess(uint32_t dex_method_idx, 3003 MethodType method_type) { 3004 const DexFile::MethodId& method_id = dex_file_->GetMethodId(dex_method_idx); 3005 RegType& klass_type = ResolveClassAndCheckAccess(method_id.class_idx_); 3006 if (klass_type.IsConflict()) { 3007 std::string append(" in attempt to access method "); 3008 append += dex_file_->GetMethodName(method_id); 3009 AppendToLastFailMessage(append); 3010 return NULL; 3011 } 3012 if (klass_type.IsUnresolvedTypes()) { 3013 return NULL; // Can't resolve Class so no more to do here 3014 } 3015 mirror::Class* klass = klass_type.GetClass(); 3016 RegType& referrer = GetDeclaringClass(); 3017 mirror::ArtMethod* res_method = (*dex_cache_)->GetResolvedMethod(dex_method_idx); 3018 if (res_method == NULL) { 3019 const char* name = dex_file_->GetMethodName(method_id); 3020 const Signature signature = dex_file_->GetMethodSignature(method_id); 3021 3022 if (method_type == METHOD_DIRECT || method_type == METHOD_STATIC) { 3023 res_method = klass->FindDirectMethod(name, signature); 3024 } else if (method_type == METHOD_INTERFACE) { 3025 res_method = klass->FindInterfaceMethod(name, signature); 3026 } else { 3027 res_method = klass->FindVirtualMethod(name, signature); 3028 } 3029 if (res_method != NULL) { 3030 (*dex_cache_)->SetResolvedMethod(dex_method_idx, res_method); 3031 } else { 3032 // If a virtual or interface method wasn't found with the expected type, look in 3033 // the direct methods. This can happen when the wrong invoke type is used or when 3034 // a class has changed, and will be flagged as an error in later checks. 3035 if (method_type == METHOD_INTERFACE || method_type == METHOD_VIRTUAL) { 3036 res_method = klass->FindDirectMethod(name, signature); 3037 } 3038 if (res_method == NULL) { 3039 Fail(VERIFY_ERROR_NO_METHOD) << "couldn't find method " 3040 << PrettyDescriptor(klass) << "." << name 3041 << " " << signature; 3042 return NULL; 3043 } 3044 } 3045 } 3046 // Make sure calls to constructors are "direct". There are additional restrictions but we don't 3047 // enforce them here. 3048 if (res_method->IsConstructor() && method_type != METHOD_DIRECT) { 3049 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "rejecting non-direct call to constructor " 3050 << PrettyMethod(res_method); 3051 return NULL; 3052 } 3053 // Disallow any calls to class initializers. 3054 if (res_method->IsClassInitializer()) { 3055 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "rejecting call to class initializer " 3056 << PrettyMethod(res_method); 3057 return NULL; 3058 } 3059 // Check if access is allowed. 3060 if (!referrer.CanAccessMember(res_method->GetDeclaringClass(), res_method->GetAccessFlags())) { 3061 Fail(VERIFY_ERROR_ACCESS_METHOD) << "illegal method access (call " << PrettyMethod(res_method) 3062 << " from " << referrer << ")"; 3063 return res_method; 3064 } 3065 // Check that invoke-virtual and invoke-super are not used on private methods of the same class. 3066 if (res_method->IsPrivate() && method_type == METHOD_VIRTUAL) { 3067 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invoke-super/virtual can't be used on private method " 3068 << PrettyMethod(res_method); 3069 return NULL; 3070 } 3071 // Check that interface methods match interface classes. 3072 if (klass->IsInterface() && method_type != METHOD_INTERFACE) { 3073 Fail(VERIFY_ERROR_CLASS_CHANGE) << "non-interface method " << PrettyMethod(res_method) 3074 << " is in an interface class " << PrettyClass(klass); 3075 return NULL; 3076 } else if (!klass->IsInterface() && method_type == METHOD_INTERFACE) { 3077 Fail(VERIFY_ERROR_CLASS_CHANGE) << "interface method " << PrettyMethod(res_method) 3078 << " is in a non-interface class " << PrettyClass(klass); 3079 return NULL; 3080 } 3081 // See if the method type implied by the invoke instruction matches the access flags for the 3082 // target method. 3083 if ((method_type == METHOD_DIRECT && !res_method->IsDirect()) || 3084 (method_type == METHOD_STATIC && !res_method->IsStatic()) || 3085 ((method_type == METHOD_VIRTUAL || method_type == METHOD_INTERFACE) && res_method->IsDirect()) 3086 ) { 3087 Fail(VERIFY_ERROR_CLASS_CHANGE) << "invoke type (" << method_type << ") does not match method " 3088 " type of " << PrettyMethod(res_method); 3089 return NULL; 3090 } 3091 return res_method; 3092} 3093 3094template <class T> 3095mirror::ArtMethod* MethodVerifier::VerifyInvocationArgsFromIterator(T* it, const Instruction* inst, 3096 MethodType method_type, 3097 bool is_range, 3098 mirror::ArtMethod* res_method) { 3099 // We use vAA as our expected arg count, rather than res_method->insSize, because we need to 3100 // match the call to the signature. Also, we might be calling through an abstract method 3101 // definition (which doesn't have register count values). 3102 const size_t expected_args = (is_range) ? inst->VRegA_3rc() : inst->VRegA_35c(); 3103 /* caught by static verifier */ 3104 DCHECK(is_range || expected_args <= 5); 3105 if (expected_args > code_item_->outs_size_) { 3106 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid argument count (" << expected_args 3107 << ") exceeds outsSize (" << code_item_->outs_size_ << ")"; 3108 return nullptr; 3109 } 3110 3111 uint32_t arg[5]; 3112 if (!is_range) { 3113 inst->GetVarArgs(arg); 3114 } 3115 uint32_t sig_registers = 0; 3116 3117 /* 3118 * Check the "this" argument, which must be an instance of the class that declared the method. 3119 * For an interface class, we don't do the full interface merge (see JoinClass), so we can't do a 3120 * rigorous check here (which is okay since we have to do it at runtime). 3121 */ 3122 if (method_type != METHOD_STATIC) { 3123 RegType& actual_arg_type = work_line_->GetInvocationThis(inst, is_range); 3124 if (actual_arg_type.IsConflict()) { // GetInvocationThis failed. 3125 CHECK(have_pending_hard_failure_); 3126 return nullptr; 3127 } 3128 if (actual_arg_type.IsUninitializedReference()) { 3129 if (res_method) { 3130 if (!res_method->IsConstructor()) { 3131 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "'this' arg must be initialized"; 3132 return nullptr; 3133 } 3134 } else { 3135 // Check whether the name of the called method is "<init>" 3136 const uint32_t method_idx = (is_range) ? inst->VRegB_3rc() : inst->VRegB_35c(); 3137 if (strcmp(dex_file_->GetMethodName(dex_file_->GetMethodId(method_idx)), "<init>") != 0) { 3138 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "'this' arg must be initialized"; 3139 return nullptr; 3140 } 3141 } 3142 } 3143 if (method_type != METHOD_INTERFACE && !actual_arg_type.IsZero()) { 3144 RegType* res_method_class; 3145 if (res_method != nullptr) { 3146 mirror::Class* klass = res_method->GetDeclaringClass(); 3147 std::string temp; 3148 res_method_class = ®_types_.FromClass(klass->GetDescriptor(&temp), klass, 3149 klass->CannotBeAssignedFromOtherTypes()); 3150 } else { 3151 const uint32_t method_idx = (is_range) ? inst->VRegB_3rc() : inst->VRegB_35c(); 3152 const uint16_t class_idx = dex_file_->GetMethodId(method_idx).class_idx_; 3153 res_method_class = ®_types_.FromDescriptor(class_loader_->Get(), 3154 dex_file_->StringByTypeIdx(class_idx), 3155 false); 3156 } 3157 if (!res_method_class->IsAssignableFrom(actual_arg_type)) { 3158 Fail(actual_arg_type.IsUnresolvedTypes() ? VERIFY_ERROR_NO_CLASS: 3159 VERIFY_ERROR_BAD_CLASS_SOFT) << "'this' argument '" << actual_arg_type 3160 << "' not instance of '" << *res_method_class << "'"; 3161 // Continue on soft failures. We need to find possible hard failures to avoid problems in 3162 // the compiler. 3163 if (have_pending_hard_failure_) { 3164 return nullptr; 3165 } 3166 } 3167 } 3168 sig_registers = 1; 3169 } 3170 3171 for ( ; it->HasNext(); it->Next()) { 3172 if (sig_registers >= expected_args) { 3173 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Rejecting invocation, expected " << inst->VRegA() << 3174 " arguments, found " << sig_registers << " or more."; 3175 return nullptr; 3176 } 3177 3178 const char* param_descriptor = it->GetDescriptor(); 3179 3180 if (param_descriptor == nullptr) { 3181 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Rejecting invocation because of missing signature " 3182 "component"; 3183 return nullptr; 3184 } 3185 3186 RegType& reg_type = reg_types_.FromDescriptor(class_loader_->Get(), param_descriptor, 3187 false); 3188 uint32_t get_reg = is_range ? inst->VRegC_3rc() + static_cast<uint32_t>(sig_registers) : 3189 arg[sig_registers]; 3190 if (reg_type.IsIntegralTypes()) { 3191 RegType& src_type = work_line_->GetRegisterType(get_reg); 3192 if (!src_type.IsIntegralTypes()) { 3193 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "register v" << get_reg << " has type " << src_type 3194 << " but expected " << reg_type; 3195 return res_method; 3196 } 3197 } else if (!work_line_->VerifyRegisterType(get_reg, reg_type)) { 3198 // Continue on soft failures. We need to find possible hard failures to avoid problems in the 3199 // compiler. 3200 if (have_pending_hard_failure_) { 3201 return res_method; 3202 } 3203 } 3204 sig_registers += reg_type.IsLongOrDoubleTypes() ? 2 : 1; 3205 } 3206 if (expected_args != sig_registers) { 3207 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Rejecting invocation, expected " << expected_args << 3208 " arguments, found " << sig_registers; 3209 return nullptr; 3210 } 3211 return res_method; 3212} 3213 3214void MethodVerifier::VerifyInvocationArgsUnresolvedMethod(const Instruction* inst, 3215 MethodType method_type, 3216 bool is_range) { 3217 // As the method may not have been resolved, make this static check against what we expect. 3218 // The main reason for this code block is to fail hard when we find an illegal use, e.g., 3219 // wrong number of arguments or wrong primitive types, even if the method could not be resolved. 3220 const uint32_t method_idx = (is_range) ? inst->VRegB_3rc() : inst->VRegB_35c(); 3221 DexFileParameterIterator it(*dex_file_, 3222 dex_file_->GetProtoId(dex_file_->GetMethodId(method_idx).proto_idx_)); 3223 VerifyInvocationArgsFromIterator<DexFileParameterIterator>(&it, inst, method_type, is_range, 3224 nullptr); 3225} 3226 3227class MethodParamListDescriptorIterator { 3228 public: 3229 explicit MethodParamListDescriptorIterator(mirror::ArtMethod* res_method) : 3230 res_method_(res_method), pos_(0), params_(res_method->GetParameterTypeList()), 3231 params_size_(params_ == nullptr ? 0 : params_->Size()) { 3232 } 3233 3234 bool HasNext() { 3235 return pos_ < params_size_; 3236 } 3237 3238 void Next() { 3239 ++pos_; 3240 } 3241 3242 const char* GetDescriptor() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { 3243 return res_method_->GetTypeDescriptorFromTypeIdx(params_->GetTypeItem(pos_).type_idx_); 3244 } 3245 3246 private: 3247 mirror::ArtMethod* res_method_; 3248 size_t pos_; 3249 const DexFile::TypeList* params_; 3250 const size_t params_size_; 3251}; 3252 3253mirror::ArtMethod* MethodVerifier::VerifyInvocationArgs(const Instruction* inst, 3254 MethodType method_type, 3255 bool is_range, 3256 bool is_super) { 3257 // Resolve the method. This could be an abstract or concrete method depending on what sort of call 3258 // we're making. 3259 const uint32_t method_idx = (is_range) ? inst->VRegB_3rc() : inst->VRegB_35c(); 3260 3261 mirror::ArtMethod* res_method = ResolveMethodAndCheckAccess(method_idx, method_type); 3262 if (res_method == NULL) { // error or class is unresolved 3263 // Check what we can statically. 3264 if (!have_pending_hard_failure_) { 3265 VerifyInvocationArgsUnresolvedMethod(inst, method_type, is_range); 3266 } 3267 return nullptr; 3268 } 3269 3270 // If we're using invoke-super(method), make sure that the executing method's class' superclass 3271 // has a vtable entry for the target method. 3272 if (is_super) { 3273 DCHECK(method_type == METHOD_VIRTUAL); 3274 RegType& super = GetDeclaringClass().GetSuperClass(®_types_); 3275 if (super.IsUnresolvedTypes()) { 3276 Fail(VERIFY_ERROR_NO_METHOD) << "unknown super class in invoke-super from " 3277 << PrettyMethod(dex_method_idx_, *dex_file_) 3278 << " to super " << PrettyMethod(res_method); 3279 return nullptr; 3280 } 3281 mirror::Class* super_klass = super.GetClass(); 3282 if (res_method->GetMethodIndex() >= super_klass->GetVTableLength()) { 3283 Fail(VERIFY_ERROR_NO_METHOD) << "invalid invoke-super from " 3284 << PrettyMethod(dex_method_idx_, *dex_file_) 3285 << " to super " << super 3286 << "." << res_method->GetName() 3287 << res_method->GetSignature(); 3288 return nullptr; 3289 } 3290 } 3291 3292 // Process the target method's signature. This signature may or may not 3293 MethodParamListDescriptorIterator it(res_method); 3294 return VerifyInvocationArgsFromIterator<MethodParamListDescriptorIterator>(&it, inst, method_type, 3295 is_range, res_method); 3296} 3297 3298mirror::ArtMethod* MethodVerifier::GetQuickInvokedMethod(const Instruction* inst, 3299 RegisterLine* reg_line, bool is_range) { 3300 DCHECK(inst->Opcode() == Instruction::INVOKE_VIRTUAL_QUICK || 3301 inst->Opcode() == Instruction::INVOKE_VIRTUAL_RANGE_QUICK); 3302 RegType& actual_arg_type = reg_line->GetInvocationThis(inst, is_range); 3303 if (!actual_arg_type.HasClass()) { 3304 VLOG(verifier) << "Failed to get mirror::Class* from '" << actual_arg_type << "'"; 3305 return nullptr; 3306 } 3307 mirror::Class* klass = actual_arg_type.GetClass(); 3308 mirror::Class* dispatch_class; 3309 if (klass->IsInterface()) { 3310 // Derive Object.class from Class.class.getSuperclass(). 3311 mirror::Class* object_klass = klass->GetClass()->GetSuperClass(); 3312 CHECK(object_klass->IsObjectClass()); 3313 dispatch_class = object_klass; 3314 } else { 3315 dispatch_class = klass; 3316 } 3317 CHECK(dispatch_class->HasVTable()) << PrettyDescriptor(dispatch_class); 3318 uint16_t vtable_index = is_range ? inst->VRegB_3rc() : inst->VRegB_35c(); 3319 CHECK_LT(static_cast<int32_t>(vtable_index), dispatch_class->GetVTableLength()) 3320 << PrettyDescriptor(klass); 3321 mirror::ArtMethod* res_method = dispatch_class->GetVTableEntry(vtable_index); 3322 CHECK(!Thread::Current()->IsExceptionPending()); 3323 return res_method; 3324} 3325 3326mirror::ArtMethod* MethodVerifier::VerifyInvokeVirtualQuickArgs(const Instruction* inst, 3327 bool is_range) { 3328 DCHECK(Runtime::Current()->IsStarted()); 3329 mirror::ArtMethod* res_method = GetQuickInvokedMethod(inst, work_line_.get(), 3330 is_range); 3331 if (res_method == NULL) { 3332 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Cannot infer method from " << inst->Name(); 3333 return NULL; 3334 } 3335 CHECK(!res_method->IsDirect() && !res_method->IsStatic()); 3336 3337 // We use vAA as our expected arg count, rather than res_method->insSize, because we need to 3338 // match the call to the signature. Also, we might be calling through an abstract method 3339 // definition (which doesn't have register count values). 3340 RegType& actual_arg_type = work_line_->GetInvocationThis(inst, is_range); 3341 if (actual_arg_type.IsConflict()) { // GetInvocationThis failed. 3342 return NULL; 3343 } 3344 const size_t expected_args = (is_range) ? inst->VRegA_3rc() : inst->VRegA_35c(); 3345 /* caught by static verifier */ 3346 DCHECK(is_range || expected_args <= 5); 3347 if (expected_args > code_item_->outs_size_) { 3348 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid argument count (" << expected_args 3349 << ") exceeds outsSize (" << code_item_->outs_size_ << ")"; 3350 return NULL; 3351 } 3352 3353 /* 3354 * Check the "this" argument, which must be an instance of the class that declared the method. 3355 * For an interface class, we don't do the full interface merge (see JoinClass), so we can't do a 3356 * rigorous check here (which is okay since we have to do it at runtime). 3357 */ 3358 if (actual_arg_type.IsUninitializedReference() && !res_method->IsConstructor()) { 3359 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "'this' arg must be initialized"; 3360 return NULL; 3361 } 3362 if (!actual_arg_type.IsZero()) { 3363 mirror::Class* klass = res_method->GetDeclaringClass(); 3364 std::string temp; 3365 RegType& res_method_class = 3366 reg_types_.FromClass(klass->GetDescriptor(&temp), klass, 3367 klass->CannotBeAssignedFromOtherTypes()); 3368 if (!res_method_class.IsAssignableFrom(actual_arg_type)) { 3369 Fail(actual_arg_type.IsUnresolvedTypes() ? VERIFY_ERROR_NO_CLASS : 3370 VERIFY_ERROR_BAD_CLASS_SOFT) << "'this' argument '" << actual_arg_type 3371 << "' not instance of '" << res_method_class << "'"; 3372 return NULL; 3373 } 3374 } 3375 /* 3376 * Process the target method's signature. This signature may or may not 3377 * have been verified, so we can't assume it's properly formed. 3378 */ 3379 const DexFile::TypeList* params = res_method->GetParameterTypeList(); 3380 size_t params_size = params == NULL ? 0 : params->Size(); 3381 uint32_t arg[5]; 3382 if (!is_range) { 3383 inst->GetVarArgs(arg); 3384 } 3385 size_t actual_args = 1; 3386 for (size_t param_index = 0; param_index < params_size; param_index++) { 3387 if (actual_args >= expected_args) { 3388 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Rejecting invalid call to '" << PrettyMethod(res_method) 3389 << "'. Expected " << expected_args 3390 << " arguments, processing argument " << actual_args 3391 << " (where longs/doubles count twice)."; 3392 return NULL; 3393 } 3394 const char* descriptor = 3395 res_method->GetTypeDescriptorFromTypeIdx(params->GetTypeItem(param_index).type_idx_); 3396 if (descriptor == NULL) { 3397 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Rejecting invocation of " << PrettyMethod(res_method) 3398 << " missing signature component"; 3399 return NULL; 3400 } 3401 RegType& reg_type = reg_types_.FromDescriptor(class_loader_->Get(), descriptor, false); 3402 uint32_t get_reg = is_range ? inst->VRegC_3rc() + actual_args : arg[actual_args]; 3403 if (!work_line_->VerifyRegisterType(get_reg, reg_type)) { 3404 return res_method; 3405 } 3406 actual_args = reg_type.IsLongOrDoubleTypes() ? actual_args + 2 : actual_args + 1; 3407 } 3408 if (actual_args != expected_args) { 3409 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Rejecting invocation of " << PrettyMethod(res_method) 3410 << " expected " << expected_args << " arguments, found " << actual_args; 3411 return NULL; 3412 } else { 3413 return res_method; 3414 } 3415} 3416 3417void MethodVerifier::VerifyNewArray(const Instruction* inst, bool is_filled, bool is_range) { 3418 uint32_t type_idx; 3419 if (!is_filled) { 3420 DCHECK_EQ(inst->Opcode(), Instruction::NEW_ARRAY); 3421 type_idx = inst->VRegC_22c(); 3422 } else if (!is_range) { 3423 DCHECK_EQ(inst->Opcode(), Instruction::FILLED_NEW_ARRAY); 3424 type_idx = inst->VRegB_35c(); 3425 } else { 3426 DCHECK_EQ(inst->Opcode(), Instruction::FILLED_NEW_ARRAY_RANGE); 3427 type_idx = inst->VRegB_3rc(); 3428 } 3429 RegType& res_type = ResolveClassAndCheckAccess(type_idx); 3430 if (res_type.IsConflict()) { // bad class 3431 DCHECK_NE(failures_.size(), 0U); 3432 } else { 3433 // TODO: check Compiler::CanAccessTypeWithoutChecks returns false when res_type is unresolved 3434 if (!res_type.IsArrayTypes()) { 3435 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "new-array on non-array class " << res_type; 3436 } else if (!is_filled) { 3437 /* make sure "size" register is valid type */ 3438 work_line_->VerifyRegisterType(inst->VRegB_22c(), reg_types_.Integer()); 3439 /* set register type to array class */ 3440 RegType& precise_type = reg_types_.FromUninitialized(res_type); 3441 work_line_->SetRegisterType(inst->VRegA_22c(), precise_type); 3442 } else { 3443 // Verify each register. If "arg_count" is bad, VerifyRegisterType() will run off the end of 3444 // the list and fail. It's legal, if silly, for arg_count to be zero. 3445 RegType& expected_type = reg_types_.GetComponentType(res_type, class_loader_->Get()); 3446 uint32_t arg_count = (is_range) ? inst->VRegA_3rc() : inst->VRegA_35c(); 3447 uint32_t arg[5]; 3448 if (!is_range) { 3449 inst->GetVarArgs(arg); 3450 } 3451 for (size_t ui = 0; ui < arg_count; ui++) { 3452 uint32_t get_reg = is_range ? inst->VRegC_3rc() + ui : arg[ui]; 3453 if (!work_line_->VerifyRegisterType(get_reg, expected_type)) { 3454 work_line_->SetResultRegisterType(reg_types_.Conflict()); 3455 return; 3456 } 3457 } 3458 // filled-array result goes into "result" register 3459 RegType& precise_type = reg_types_.FromUninitialized(res_type); 3460 work_line_->SetResultRegisterType(precise_type); 3461 } 3462 } 3463} 3464 3465void MethodVerifier::VerifyAGet(const Instruction* inst, 3466 RegType& insn_type, bool is_primitive) { 3467 RegType& index_type = work_line_->GetRegisterType(inst->VRegC_23x()); 3468 if (!index_type.IsArrayIndexTypes()) { 3469 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Invalid reg type for array index (" << index_type << ")"; 3470 } else { 3471 RegType& array_type = work_line_->GetRegisterType(inst->VRegB_23x()); 3472 if (array_type.IsZero()) { 3473 // Null array class; this code path will fail at runtime. Infer a merge-able type from the 3474 // instruction type. TODO: have a proper notion of bottom here. 3475 if (!is_primitive || insn_type.IsCategory1Types()) { 3476 // Reference or category 1 3477 work_line_->SetRegisterType(inst->VRegA_23x(), reg_types_.Zero()); 3478 } else { 3479 // Category 2 3480 work_line_->SetRegisterTypeWide(inst->VRegA_23x(), reg_types_.FromCat2ConstLo(0, false), 3481 reg_types_.FromCat2ConstHi(0, false)); 3482 } 3483 } else if (!array_type.IsArrayTypes()) { 3484 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "not array type " << array_type << " with aget"; 3485 } else { 3486 /* verify the class */ 3487 RegType& component_type = reg_types_.GetComponentType(array_type, class_loader_->Get()); 3488 if (!component_type.IsReferenceTypes() && !is_primitive) { 3489 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "primitive array type " << array_type 3490 << " source for aget-object"; 3491 } else if (component_type.IsNonZeroReferenceTypes() && is_primitive) { 3492 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "reference array type " << array_type 3493 << " source for category 1 aget"; 3494 } else if (is_primitive && !insn_type.Equals(component_type) && 3495 !((insn_type.IsInteger() && component_type.IsFloat()) || 3496 (insn_type.IsLong() && component_type.IsDouble()))) { 3497 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "array type " << array_type 3498 << " incompatible with aget of type " << insn_type; 3499 } else { 3500 // Use knowledge of the field type which is stronger than the type inferred from the 3501 // instruction, which can't differentiate object types and ints from floats, longs from 3502 // doubles. 3503 if (!component_type.IsLowHalf()) { 3504 work_line_->SetRegisterType(inst->VRegA_23x(), component_type); 3505 } else { 3506 work_line_->SetRegisterTypeWide(inst->VRegA_23x(), component_type, 3507 component_type.HighHalf(®_types_)); 3508 } 3509 } 3510 } 3511 } 3512} 3513 3514void MethodVerifier::VerifyPrimitivePut(RegType& target_type, RegType& insn_type, 3515 const uint32_t vregA) { 3516 // Primitive assignability rules are weaker than regular assignability rules. 3517 bool instruction_compatible; 3518 bool value_compatible; 3519 RegType& value_type = work_line_->GetRegisterType(vregA); 3520 if (target_type.IsIntegralTypes()) { 3521 instruction_compatible = target_type.Equals(insn_type); 3522 value_compatible = value_type.IsIntegralTypes(); 3523 } else if (target_type.IsFloat()) { 3524 instruction_compatible = insn_type.IsInteger(); // no put-float, so expect put-int 3525 value_compatible = value_type.IsFloatTypes(); 3526 } else if (target_type.IsLong()) { 3527 instruction_compatible = insn_type.IsLong(); 3528 RegType& value_type_hi = work_line_->GetRegisterType(vregA + 1); 3529 value_compatible = value_type.IsLongTypes() && value_type.CheckWidePair(value_type_hi); 3530 } else if (target_type.IsDouble()) { 3531 instruction_compatible = insn_type.IsLong(); // no put-double, so expect put-long 3532 RegType& value_type_hi = work_line_->GetRegisterType(vregA + 1); 3533 value_compatible = value_type.IsDoubleTypes() && value_type.CheckWidePair(value_type_hi); 3534 } else { 3535 instruction_compatible = false; // reference with primitive store 3536 value_compatible = false; // unused 3537 } 3538 if (!instruction_compatible) { 3539 // This is a global failure rather than a class change failure as the instructions and 3540 // the descriptors for the type should have been consistent within the same file at 3541 // compile time. 3542 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "put insn has type '" << insn_type 3543 << "' but expected type '" << target_type << "'"; 3544 return; 3545 } 3546 if (!value_compatible) { 3547 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unexpected value in v" << vregA 3548 << " of type " << value_type << " but expected " << target_type << " for put"; 3549 return; 3550 } 3551} 3552 3553void MethodVerifier::VerifyAPut(const Instruction* inst, 3554 RegType& insn_type, bool is_primitive) { 3555 RegType& index_type = work_line_->GetRegisterType(inst->VRegC_23x()); 3556 if (!index_type.IsArrayIndexTypes()) { 3557 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Invalid reg type for array index (" << index_type << ")"; 3558 } else { 3559 RegType& array_type = work_line_->GetRegisterType(inst->VRegB_23x()); 3560 if (array_type.IsZero()) { 3561 // Null array type; this code path will fail at runtime. Infer a merge-able type from the 3562 // instruction type. 3563 } else if (!array_type.IsArrayTypes()) { 3564 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "not array type " << array_type << " with aput"; 3565 } else { 3566 RegType& component_type = reg_types_.GetComponentType(array_type, class_loader_->Get()); 3567 const uint32_t vregA = inst->VRegA_23x(); 3568 if (is_primitive) { 3569 VerifyPrimitivePut(component_type, insn_type, vregA); 3570 } else { 3571 if (!component_type.IsReferenceTypes()) { 3572 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "primitive array type " << array_type 3573 << " source for aput-object"; 3574 } else { 3575 // The instruction agrees with the type of array, confirm the value to be stored does too 3576 // Note: we use the instruction type (rather than the component type) for aput-object as 3577 // incompatible classes will be caught at runtime as an array store exception 3578 work_line_->VerifyRegisterType(vregA, insn_type); 3579 } 3580 } 3581 } 3582 } 3583} 3584 3585mirror::ArtField* MethodVerifier::GetStaticField(int field_idx) { 3586 const DexFile::FieldId& field_id = dex_file_->GetFieldId(field_idx); 3587 // Check access to class 3588 RegType& klass_type = ResolveClassAndCheckAccess(field_id.class_idx_); 3589 if (klass_type.IsConflict()) { // bad class 3590 AppendToLastFailMessage(StringPrintf(" in attempt to access static field %d (%s) in %s", 3591 field_idx, dex_file_->GetFieldName(field_id), 3592 dex_file_->GetFieldDeclaringClassDescriptor(field_id))); 3593 return NULL; 3594 } 3595 if (klass_type.IsUnresolvedTypes()) { 3596 return NULL; // Can't resolve Class so no more to do here, will do checking at runtime. 3597 } 3598 ClassLinker* class_linker = Runtime::Current()->GetClassLinker(); 3599 mirror::ArtField* field = class_linker->ResolveFieldJLS(*dex_file_, field_idx, *dex_cache_, 3600 *class_loader_); 3601 if (field == NULL) { 3602 VLOG(verifier) << "Unable to resolve static field " << field_idx << " (" 3603 << dex_file_->GetFieldName(field_id) << ") in " 3604 << dex_file_->GetFieldDeclaringClassDescriptor(field_id); 3605 DCHECK(Thread::Current()->IsExceptionPending()); 3606 Thread::Current()->ClearException(); 3607 return NULL; 3608 } else if (!GetDeclaringClass().CanAccessMember(field->GetDeclaringClass(), 3609 field->GetAccessFlags())) { 3610 Fail(VERIFY_ERROR_ACCESS_FIELD) << "cannot access static field " << PrettyField(field) 3611 << " from " << GetDeclaringClass(); 3612 return NULL; 3613 } else if (!field->IsStatic()) { 3614 Fail(VERIFY_ERROR_CLASS_CHANGE) << "expected field " << PrettyField(field) << " to be static"; 3615 return NULL; 3616 } 3617 return field; 3618} 3619 3620mirror::ArtField* MethodVerifier::GetInstanceField(RegType& obj_type, int field_idx) { 3621 const DexFile::FieldId& field_id = dex_file_->GetFieldId(field_idx); 3622 // Check access to class 3623 RegType& klass_type = ResolveClassAndCheckAccess(field_id.class_idx_); 3624 if (klass_type.IsConflict()) { 3625 AppendToLastFailMessage(StringPrintf(" in attempt to access instance field %d (%s) in %s", 3626 field_idx, dex_file_->GetFieldName(field_id), 3627 dex_file_->GetFieldDeclaringClassDescriptor(field_id))); 3628 return NULL; 3629 } 3630 if (klass_type.IsUnresolvedTypes()) { 3631 return NULL; // Can't resolve Class so no more to do here 3632 } 3633 ClassLinker* class_linker = Runtime::Current()->GetClassLinker(); 3634 mirror::ArtField* field = class_linker->ResolveFieldJLS(*dex_file_, field_idx, *dex_cache_, 3635 *class_loader_); 3636 if (field == NULL) { 3637 VLOG(verifier) << "Unable to resolve instance field " << field_idx << " (" 3638 << dex_file_->GetFieldName(field_id) << ") in " 3639 << dex_file_->GetFieldDeclaringClassDescriptor(field_id); 3640 DCHECK(Thread::Current()->IsExceptionPending()); 3641 Thread::Current()->ClearException(); 3642 return NULL; 3643 } else if (!GetDeclaringClass().CanAccessMember(field->GetDeclaringClass(), 3644 field->GetAccessFlags())) { 3645 Fail(VERIFY_ERROR_ACCESS_FIELD) << "cannot access instance field " << PrettyField(field) 3646 << " from " << GetDeclaringClass(); 3647 return NULL; 3648 } else if (field->IsStatic()) { 3649 Fail(VERIFY_ERROR_CLASS_CHANGE) << "expected field " << PrettyField(field) 3650 << " to not be static"; 3651 return NULL; 3652 } else if (obj_type.IsZero()) { 3653 // Cannot infer and check type, however, access will cause null pointer exception 3654 return field; 3655 } else { 3656 mirror::Class* klass = field->GetDeclaringClass(); 3657 RegType& field_klass = 3658 reg_types_.FromClass(dex_file_->GetFieldDeclaringClassDescriptor(field_id), 3659 klass, klass->CannotBeAssignedFromOtherTypes()); 3660 if (obj_type.IsUninitializedTypes() && 3661 (!IsConstructor() || GetDeclaringClass().Equals(obj_type) || 3662 !field_klass.Equals(GetDeclaringClass()))) { 3663 // Field accesses through uninitialized references are only allowable for constructors where 3664 // the field is declared in this class 3665 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "cannot access instance field " << PrettyField(field) 3666 << " of a not fully initialized object within the context" 3667 << " of " << PrettyMethod(dex_method_idx_, *dex_file_); 3668 return NULL; 3669 } else if (!field_klass.IsAssignableFrom(obj_type)) { 3670 // Trying to access C1.field1 using reference of type C2, which is neither C1 or a sub-class 3671 // of C1. For resolution to occur the declared class of the field must be compatible with 3672 // obj_type, we've discovered this wasn't so, so report the field didn't exist. 3673 Fail(VERIFY_ERROR_NO_FIELD) << "cannot access instance field " << PrettyField(field) 3674 << " from object of type " << obj_type; 3675 return NULL; 3676 } else { 3677 return field; 3678 } 3679 } 3680} 3681 3682void MethodVerifier::VerifyISGet(const Instruction* inst, RegType& insn_type, 3683 bool is_primitive, bool is_static) { 3684 uint32_t field_idx = is_static ? inst->VRegB_21c() : inst->VRegC_22c(); 3685 mirror::ArtField* field; 3686 if (is_static) { 3687 field = GetStaticField(field_idx); 3688 } else { 3689 RegType& object_type = work_line_->GetRegisterType(inst->VRegB_22c()); 3690 field = GetInstanceField(object_type, field_idx); 3691 } 3692 RegType* field_type = nullptr; 3693 if (field != NULL) { 3694 Thread* self = Thread::Current(); 3695 mirror::Class* field_type_class; 3696 { 3697 StackHandleScope<1> hs(self); 3698 HandleWrapper<mirror::ArtField> h_field(hs.NewHandleWrapper(&field)); 3699 field_type_class = FieldHelper(h_field).GetType(can_load_classes_); 3700 } 3701 if (field_type_class != nullptr) { 3702 field_type = ®_types_.FromClass(field->GetTypeDescriptor(), field_type_class, 3703 field_type_class->CannotBeAssignedFromOtherTypes()); 3704 } else { 3705 DCHECK(!can_load_classes_ || self->IsExceptionPending()); 3706 self->ClearException(); 3707 } 3708 } 3709 if (field_type == nullptr) { 3710 const DexFile::FieldId& field_id = dex_file_->GetFieldId(field_idx); 3711 const char* descriptor = dex_file_->GetFieldTypeDescriptor(field_id); 3712 field_type = ®_types_.FromDescriptor(class_loader_->Get(), descriptor, false); 3713 } 3714 DCHECK(field_type != nullptr); 3715 const uint32_t vregA = (is_static) ? inst->VRegA_21c() : inst->VRegA_22c(); 3716 if (is_primitive) { 3717 if (field_type->Equals(insn_type) || 3718 (field_type->IsFloat() && insn_type.IsInteger()) || 3719 (field_type->IsDouble() && insn_type.IsLong())) { 3720 // expected that read is of the correct primitive type or that int reads are reading 3721 // floats or long reads are reading doubles 3722 } else { 3723 // This is a global failure rather than a class change failure as the instructions and 3724 // the descriptors for the type should have been consistent within the same file at 3725 // compile time 3726 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "expected field " << PrettyField(field) 3727 << " to be of type '" << insn_type 3728 << "' but found type '" << *field_type << "' in get"; 3729 return; 3730 } 3731 } else { 3732 if (!insn_type.IsAssignableFrom(*field_type)) { 3733 Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "expected field " << PrettyField(field) 3734 << " to be compatible with type '" << insn_type 3735 << "' but found type '" << *field_type 3736 << "' in Get-object"; 3737 work_line_->SetRegisterType(vregA, reg_types_.Conflict()); 3738 return; 3739 } 3740 } 3741 if (!field_type->IsLowHalf()) { 3742 work_line_->SetRegisterType(vregA, *field_type); 3743 } else { 3744 work_line_->SetRegisterTypeWide(vregA, *field_type, field_type->HighHalf(®_types_)); 3745 } 3746} 3747 3748void MethodVerifier::VerifyISPut(const Instruction* inst, RegType& insn_type, 3749 bool is_primitive, bool is_static) { 3750 uint32_t field_idx = is_static ? inst->VRegB_21c() : inst->VRegC_22c(); 3751 mirror::ArtField* field; 3752 if (is_static) { 3753 field = GetStaticField(field_idx); 3754 } else { 3755 RegType& object_type = work_line_->GetRegisterType(inst->VRegB_22c()); 3756 field = GetInstanceField(object_type, field_idx); 3757 } 3758 RegType* field_type = nullptr; 3759 if (field != NULL) { 3760 if (field->IsFinal() && field->GetDeclaringClass() != GetDeclaringClass().GetClass()) { 3761 Fail(VERIFY_ERROR_ACCESS_FIELD) << "cannot modify final field " << PrettyField(field) 3762 << " from other class " << GetDeclaringClass(); 3763 return; 3764 } 3765 mirror::Class* field_type_class; 3766 { 3767 StackHandleScope<1> hs(Thread::Current()); 3768 HandleWrapper<mirror::ArtField> h_field(hs.NewHandleWrapper(&field)); 3769 FieldHelper fh(h_field); 3770 field_type_class = fh.GetType(can_load_classes_); 3771 } 3772 if (field_type_class != nullptr) { 3773 field_type = ®_types_.FromClass(field->GetTypeDescriptor(), field_type_class, 3774 field_type_class->CannotBeAssignedFromOtherTypes()); 3775 } else { 3776 Thread* self = Thread::Current(); 3777 DCHECK(!can_load_classes_ || self->IsExceptionPending()); 3778 self->ClearException(); 3779 } 3780 } 3781 if (field_type == nullptr) { 3782 const DexFile::FieldId& field_id = dex_file_->GetFieldId(field_idx); 3783 const char* descriptor = dex_file_->GetFieldTypeDescriptor(field_id); 3784 field_type = ®_types_.FromDescriptor(class_loader_->Get(), descriptor, false); 3785 } 3786 DCHECK(field_type != nullptr); 3787 const uint32_t vregA = (is_static) ? inst->VRegA_21c() : inst->VRegA_22c(); 3788 if (is_primitive) { 3789 VerifyPrimitivePut(*field_type, insn_type, vregA); 3790 } else { 3791 if (!insn_type.IsAssignableFrom(*field_type)) { 3792 Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "expected field " << PrettyField(field) 3793 << " to be compatible with type '" << insn_type 3794 << "' but found type '" << *field_type 3795 << "' in put-object"; 3796 return; 3797 } 3798 work_line_->VerifyRegisterType(vregA, *field_type); 3799 } 3800} 3801 3802mirror::ArtField* MethodVerifier::GetQuickFieldAccess(const Instruction* inst, 3803 RegisterLine* reg_line) { 3804 DCHECK(inst->Opcode() == Instruction::IGET_QUICK || 3805 inst->Opcode() == Instruction::IGET_WIDE_QUICK || 3806 inst->Opcode() == Instruction::IGET_OBJECT_QUICK || 3807 inst->Opcode() == Instruction::IPUT_QUICK || 3808 inst->Opcode() == Instruction::IPUT_WIDE_QUICK || 3809 inst->Opcode() == Instruction::IPUT_OBJECT_QUICK); 3810 RegType& object_type = reg_line->GetRegisterType(inst->VRegB_22c()); 3811 if (!object_type.HasClass()) { 3812 VLOG(verifier) << "Failed to get mirror::Class* from '" << object_type << "'"; 3813 return nullptr; 3814 } 3815 uint32_t field_offset = static_cast<uint32_t>(inst->VRegC_22c()); 3816 mirror::ArtField* f = mirror::ArtField::FindInstanceFieldWithOffset(object_type.GetClass(), 3817 field_offset); 3818 if (f == nullptr) { 3819 VLOG(verifier) << "Failed to find instance field at offset '" << field_offset 3820 << "' from '" << PrettyDescriptor(object_type.GetClass()) << "'"; 3821 } 3822 return f; 3823} 3824 3825void MethodVerifier::VerifyIGetQuick(const Instruction* inst, RegType& insn_type, 3826 bool is_primitive) { 3827 DCHECK(Runtime::Current()->IsStarted()); 3828 mirror::ArtField* field = GetQuickFieldAccess(inst, work_line_.get()); 3829 if (field == NULL) { 3830 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Cannot infer field from " << inst->Name(); 3831 return; 3832 } 3833 mirror::Class* field_type_class; 3834 { 3835 StackHandleScope<1> hs(Thread::Current()); 3836 HandleWrapper<mirror::ArtField> h_field(hs.NewHandleWrapper(&field)); 3837 FieldHelper fh(h_field); 3838 field_type_class = fh.GetType(can_load_classes_); 3839 } 3840 RegType* field_type; 3841 if (field_type_class != nullptr) { 3842 field_type = ®_types_.FromClass(field->GetTypeDescriptor(), field_type_class, 3843 field_type_class->CannotBeAssignedFromOtherTypes()); 3844 } else { 3845 Thread* self = Thread::Current(); 3846 DCHECK(!can_load_classes_ || self->IsExceptionPending()); 3847 self->ClearException(); 3848 field_type = ®_types_.FromDescriptor(field->GetDeclaringClass()->GetClassLoader(), 3849 field->GetTypeDescriptor(), false); 3850 } 3851 DCHECK(field_type != nullptr); 3852 const uint32_t vregA = inst->VRegA_22c(); 3853 if (is_primitive) { 3854 if (field_type->Equals(insn_type) || 3855 (field_type->IsFloat() && insn_type.IsIntegralTypes()) || 3856 (field_type->IsDouble() && insn_type.IsLongTypes())) { 3857 // expected that read is of the correct primitive type or that int reads are reading 3858 // floats or long reads are reading doubles 3859 } else { 3860 // This is a global failure rather than a class change failure as the instructions and 3861 // the descriptors for the type should have been consistent within the same file at 3862 // compile time 3863 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "expected field " << PrettyField(field) 3864 << " to be of type '" << insn_type 3865 << "' but found type '" << *field_type << "' in Get"; 3866 return; 3867 } 3868 } else { 3869 if (!insn_type.IsAssignableFrom(*field_type)) { 3870 Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "expected field " << PrettyField(field) 3871 << " to be compatible with type '" << insn_type 3872 << "' but found type '" << *field_type 3873 << "' in get-object"; 3874 work_line_->SetRegisterType(vregA, reg_types_.Conflict()); 3875 return; 3876 } 3877 } 3878 if (!field_type->IsLowHalf()) { 3879 work_line_->SetRegisterType(vregA, *field_type); 3880 } else { 3881 work_line_->SetRegisterTypeWide(vregA, *field_type, field_type->HighHalf(®_types_)); 3882 } 3883} 3884 3885void MethodVerifier::VerifyIPutQuick(const Instruction* inst, RegType& insn_type, 3886 bool is_primitive) { 3887 DCHECK(Runtime::Current()->IsStarted()); 3888 mirror::ArtField* field = GetQuickFieldAccess(inst, work_line_.get()); 3889 if (field == NULL) { 3890 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Cannot infer field from " << inst->Name(); 3891 return; 3892 } 3893 const char* descriptor = field->GetTypeDescriptor(); 3894 mirror::ClassLoader* loader = field->GetDeclaringClass()->GetClassLoader(); 3895 RegType& field_type = reg_types_.FromDescriptor(loader, descriptor, false); 3896 if (field != NULL) { 3897 if (field->IsFinal() && field->GetDeclaringClass() != GetDeclaringClass().GetClass()) { 3898 Fail(VERIFY_ERROR_ACCESS_FIELD) << "cannot modify final field " << PrettyField(field) 3899 << " from other class " << GetDeclaringClass(); 3900 return; 3901 } 3902 } 3903 const uint32_t vregA = inst->VRegA_22c(); 3904 if (is_primitive) { 3905 // Primitive field assignability rules are weaker than regular assignability rules 3906 bool instruction_compatible; 3907 bool value_compatible; 3908 RegType& value_type = work_line_->GetRegisterType(vregA); 3909 if (field_type.IsIntegralTypes()) { 3910 instruction_compatible = insn_type.IsIntegralTypes(); 3911 value_compatible = value_type.IsIntegralTypes(); 3912 } else if (field_type.IsFloat()) { 3913 instruction_compatible = insn_type.IsInteger(); // no [is]put-float, so expect [is]put-int 3914 value_compatible = value_type.IsFloatTypes(); 3915 } else if (field_type.IsLong()) { 3916 instruction_compatible = insn_type.IsLong(); 3917 value_compatible = value_type.IsLongTypes(); 3918 } else if (field_type.IsDouble()) { 3919 instruction_compatible = insn_type.IsLong(); // no [is]put-double, so expect [is]put-long 3920 value_compatible = value_type.IsDoubleTypes(); 3921 } else { 3922 instruction_compatible = false; // reference field with primitive store 3923 value_compatible = false; // unused 3924 } 3925 if (!instruction_compatible) { 3926 // This is a global failure rather than a class change failure as the instructions and 3927 // the descriptors for the type should have been consistent within the same file at 3928 // compile time 3929 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "expected field " << PrettyField(field) 3930 << " to be of type '" << insn_type 3931 << "' but found type '" << field_type 3932 << "' in put"; 3933 return; 3934 } 3935 if (!value_compatible) { 3936 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unexpected value in v" << vregA 3937 << " of type " << value_type 3938 << " but expected " << field_type 3939 << " for store to " << PrettyField(field) << " in put"; 3940 return; 3941 } 3942 } else { 3943 if (!insn_type.IsAssignableFrom(field_type)) { 3944 Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "expected field " << PrettyField(field) 3945 << " to be compatible with type '" << insn_type 3946 << "' but found type '" << field_type 3947 << "' in put-object"; 3948 return; 3949 } 3950 work_line_->VerifyRegisterType(vregA, field_type); 3951 } 3952} 3953 3954bool MethodVerifier::CheckNotMoveException(const uint16_t* insns, int insn_idx) { 3955 if ((insns[insn_idx] & 0xff) == Instruction::MOVE_EXCEPTION) { 3956 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid use of move-exception"; 3957 return false; 3958 } 3959 return true; 3960} 3961 3962bool MethodVerifier::UpdateRegisters(uint32_t next_insn, RegisterLine* merge_line, 3963 bool update_merge_line) { 3964 bool changed = true; 3965 RegisterLine* target_line = reg_table_.GetLine(next_insn); 3966 if (!insn_flags_[next_insn].IsVisitedOrChanged()) { 3967 /* 3968 * We haven't processed this instruction before, and we haven't touched the registers here, so 3969 * there's nothing to "merge". Copy the registers over and mark it as changed. (This is the 3970 * only way a register can transition out of "unknown", so this is not just an optimization.) 3971 */ 3972 if (!insn_flags_[next_insn].IsReturn()) { 3973 target_line->CopyFromLine(merge_line); 3974 } else { 3975 // Verify that the monitor stack is empty on return. 3976 if (!merge_line->VerifyMonitorStackEmpty()) { 3977 return false; 3978 } 3979 // For returns we only care about the operand to the return, all other registers are dead. 3980 // Initialize them as conflicts so they don't add to GC and deoptimization information. 3981 const Instruction* ret_inst = Instruction::At(code_item_->insns_ + next_insn); 3982 Instruction::Code opcode = ret_inst->Opcode(); 3983 if ((opcode == Instruction::RETURN_VOID) || (opcode == Instruction::RETURN_VOID_BARRIER)) { 3984 target_line->MarkAllRegistersAsConflicts(); 3985 } else { 3986 target_line->CopyFromLine(merge_line); 3987 if (opcode == Instruction::RETURN_WIDE) { 3988 target_line->MarkAllRegistersAsConflictsExceptWide(ret_inst->VRegA_11x()); 3989 } else { 3990 target_line->MarkAllRegistersAsConflictsExcept(ret_inst->VRegA_11x()); 3991 } 3992 } 3993 } 3994 } else { 3995 std::unique_ptr<RegisterLine> copy(gDebugVerify ? 3996 RegisterLine::Create(target_line->NumRegs(), this) : 3997 NULL); 3998 if (gDebugVerify) { 3999 copy->CopyFromLine(target_line); 4000 } 4001 changed = target_line->MergeRegisters(merge_line); 4002 if (have_pending_hard_failure_) { 4003 return false; 4004 } 4005 if (gDebugVerify && changed) { 4006 LogVerifyInfo() << "Merging at [" << reinterpret_cast<void*>(work_insn_idx_) << "]" 4007 << " to [" << reinterpret_cast<void*>(next_insn) << "]: " << "\n" 4008 << *copy.get() << " MERGE\n" 4009 << *merge_line << " ==\n" 4010 << *target_line << "\n"; 4011 } 4012 if (update_merge_line && changed) { 4013 merge_line->CopyFromLine(target_line); 4014 } 4015 } 4016 if (changed) { 4017 insn_flags_[next_insn].SetChanged(); 4018 } 4019 return true; 4020} 4021 4022InstructionFlags* MethodVerifier::CurrentInsnFlags() { 4023 return &insn_flags_[work_insn_idx_]; 4024} 4025 4026RegType& MethodVerifier::GetMethodReturnType() { 4027 if (return_type_ == nullptr) { 4028 if (mirror_method_.Get() != nullptr) { 4029 Thread* self = Thread::Current(); 4030 mirror::Class* return_type_class; 4031 return_type_class = MethodHelper(mirror_method_).GetReturnType(can_load_classes_); 4032 if (return_type_class != nullptr) { 4033 return_type_ = ®_types_.FromClass(mirror_method_->GetReturnTypeDescriptor(), 4034 return_type_class, 4035 return_type_class->CannotBeAssignedFromOtherTypes()); 4036 } else { 4037 DCHECK(!can_load_classes_ || self->IsExceptionPending()); 4038 self->ClearException(); 4039 } 4040 } 4041 if (return_type_ == nullptr) { 4042 const DexFile::MethodId& method_id = dex_file_->GetMethodId(dex_method_idx_); 4043 const DexFile::ProtoId& proto_id = dex_file_->GetMethodPrototype(method_id); 4044 uint16_t return_type_idx = proto_id.return_type_idx_; 4045 const char* descriptor = dex_file_->GetTypeDescriptor(dex_file_->GetTypeId(return_type_idx)); 4046 return_type_ = ®_types_.FromDescriptor(class_loader_->Get(), descriptor, false); 4047 } 4048 } 4049 return *return_type_; 4050} 4051 4052RegType& MethodVerifier::GetDeclaringClass() { 4053 if (declaring_class_ == NULL) { 4054 const DexFile::MethodId& method_id = dex_file_->GetMethodId(dex_method_idx_); 4055 const char* descriptor 4056 = dex_file_->GetTypeDescriptor(dex_file_->GetTypeId(method_id.class_idx_)); 4057 if (mirror_method_.Get() != nullptr) { 4058 mirror::Class* klass = mirror_method_->GetDeclaringClass(); 4059 declaring_class_ = ®_types_.FromClass(descriptor, klass, 4060 klass->CannotBeAssignedFromOtherTypes()); 4061 } else { 4062 declaring_class_ = ®_types_.FromDescriptor(class_loader_->Get(), descriptor, false); 4063 } 4064 } 4065 return *declaring_class_; 4066} 4067 4068std::vector<int32_t> MethodVerifier::DescribeVRegs(uint32_t dex_pc) { 4069 RegisterLine* line = reg_table_.GetLine(dex_pc); 4070 DCHECK(line != nullptr) << "No register line at DEX pc " << StringPrintf("0x%x", dex_pc); 4071 std::vector<int32_t> result; 4072 for (size_t i = 0; i < line->NumRegs(); ++i) { 4073 RegType& type = line->GetRegisterType(i); 4074 if (type.IsConstant()) { 4075 result.push_back(type.IsPreciseConstant() ? kConstant : kImpreciseConstant); 4076 result.push_back(type.ConstantValue()); 4077 } else if (type.IsConstantLo()) { 4078 result.push_back(type.IsPreciseConstantLo() ? kConstant : kImpreciseConstant); 4079 result.push_back(type.ConstantValueLo()); 4080 } else if (type.IsConstantHi()) { 4081 result.push_back(type.IsPreciseConstantHi() ? kConstant : kImpreciseConstant); 4082 result.push_back(type.ConstantValueHi()); 4083 } else if (type.IsIntegralTypes()) { 4084 result.push_back(kIntVReg); 4085 result.push_back(0); 4086 } else if (type.IsFloat()) { 4087 result.push_back(kFloatVReg); 4088 result.push_back(0); 4089 } else if (type.IsLong()) { 4090 result.push_back(kLongLoVReg); 4091 result.push_back(0); 4092 result.push_back(kLongHiVReg); 4093 result.push_back(0); 4094 ++i; 4095 } else if (type.IsDouble()) { 4096 result.push_back(kDoubleLoVReg); 4097 result.push_back(0); 4098 result.push_back(kDoubleHiVReg); 4099 result.push_back(0); 4100 ++i; 4101 } else if (type.IsUndefined() || type.IsConflict() || type.IsHighHalf()) { 4102 result.push_back(kUndefined); 4103 result.push_back(0); 4104 } else { 4105 CHECK(type.IsNonZeroReferenceTypes()); 4106 result.push_back(kReferenceVReg); 4107 result.push_back(0); 4108 } 4109 } 4110 return result; 4111} 4112 4113RegType& MethodVerifier::DetermineCat1Constant(int32_t value, bool precise) { 4114 if (precise) { 4115 // Precise constant type. 4116 return reg_types_.FromCat1Const(value, true); 4117 } else { 4118 // Imprecise constant type. 4119 if (value < -32768) { 4120 return reg_types_.IntConstant(); 4121 } else if (value < -128) { 4122 return reg_types_.ShortConstant(); 4123 } else if (value < 0) { 4124 return reg_types_.ByteConstant(); 4125 } else if (value == 0) { 4126 return reg_types_.Zero(); 4127 } else if (value == 1) { 4128 return reg_types_.One(); 4129 } else if (value < 128) { 4130 return reg_types_.PosByteConstant(); 4131 } else if (value < 32768) { 4132 return reg_types_.PosShortConstant(); 4133 } else if (value < 65536) { 4134 return reg_types_.CharConstant(); 4135 } else { 4136 return reg_types_.IntConstant(); 4137 } 4138 } 4139} 4140 4141void MethodVerifier::Init() { 4142 art::verifier::RegTypeCache::Init(); 4143} 4144 4145void MethodVerifier::Shutdown() { 4146 verifier::RegTypeCache::ShutDown(); 4147} 4148 4149void MethodVerifier::VisitRoots(RootCallback* callback, void* arg) { 4150 reg_types_.VisitRoots(callback, arg); 4151} 4152 4153} // namespace verifier 4154} // namespace art 4155