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