1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904
|
/*
* Copyright (C) 2012 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include "common_throws.h"
#include <sstream>
#include <android-base/logging.h>
#include <android-base/stringprintf.h>
#include "art_field-inl.h"
#include "art_method-inl.h"
#include "class_linker-inl.h"
#include "debug_print.h"
#include "dex/dex_file-inl.h"
#include "dex/dex_instruction-inl.h"
#include "dex/invoke_type.h"
#include "mirror/class-inl.h"
#include "mirror/method_type.h"
#include "mirror/object-inl.h"
#include "mirror/object_array-inl.h"
#include "nativehelper/scoped_local_ref.h"
#include "obj_ptr-inl.h"
#include "thread.h"
#include "well_known_classes.h"
namespace art {
using android::base::StringAppendV;
using android::base::StringPrintf;
static void AddReferrerLocation(std::ostream& os, ObjPtr<mirror::Class> referrer)
REQUIRES_SHARED(Locks::mutator_lock_) {
if (referrer != nullptr) {
std::string location(referrer->GetLocation());
if (!location.empty()) {
os << " (declaration of '" << referrer->PrettyDescriptor()
<< "' appears in " << location << ")";
}
}
}
static void ThrowException(const char* exception_descriptor) REQUIRES_SHARED(Locks::mutator_lock_) {
Thread* self = Thread::Current();
self->ThrowNewException(exception_descriptor, nullptr);
}
static void ThrowException(const char* exception_descriptor,
ObjPtr<mirror::Class> referrer,
const char* fmt,
va_list* args = nullptr)
REQUIRES_SHARED(Locks::mutator_lock_) {
std::ostringstream msg;
if (args != nullptr) {
std::string vmsg;
StringAppendV(&vmsg, fmt, *args);
msg << vmsg;
} else {
msg << fmt;
}
AddReferrerLocation(msg, referrer);
Thread* self = Thread::Current();
self->ThrowNewException(exception_descriptor, msg.str().c_str());
}
static void ThrowWrappedException(const char* exception_descriptor,
ObjPtr<mirror::Class> referrer,
const char* fmt,
va_list* args = nullptr)
REQUIRES_SHARED(Locks::mutator_lock_) {
std::ostringstream msg;
if (args != nullptr) {
std::string vmsg;
StringAppendV(&vmsg, fmt, *args);
msg << vmsg;
} else {
msg << fmt;
}
AddReferrerLocation(msg, referrer);
Thread* self = Thread::Current();
self->ThrowNewWrappedException(exception_descriptor, msg.str().c_str());
}
// AbstractMethodError
void ThrowAbstractMethodError(ArtMethod* method) {
ThrowException("Ljava/lang/AbstractMethodError;", nullptr,
StringPrintf("abstract method \"%s\"",
ArtMethod::PrettyMethod(method).c_str()).c_str());
}
void ThrowAbstractMethodError(uint32_t method_idx, const DexFile& dex_file) {
ThrowException("Ljava/lang/AbstractMethodError;", /* referrer= */ nullptr,
StringPrintf("abstract method \"%s\"",
dex_file.PrettyMethod(method_idx,
/* with_signature= */ true).c_str()).c_str());
}
// ArithmeticException
void ThrowArithmeticExceptionDivideByZero() {
ThrowException("Ljava/lang/ArithmeticException;", nullptr, "divide by zero");
}
// ArrayIndexOutOfBoundsException
void ThrowArrayIndexOutOfBoundsException(int index, int length) {
ThrowException("Ljava/lang/ArrayIndexOutOfBoundsException;", nullptr,
StringPrintf("length=%d; index=%d", length, index).c_str());
}
// ArrayStoreException
void ThrowArrayStoreException(ObjPtr<mirror::Class> element_class,
ObjPtr<mirror::Class> array_class) {
ThrowException("Ljava/lang/ArrayStoreException;", nullptr,
StringPrintf("%s cannot be stored in an array of type %s",
mirror::Class::PrettyDescriptor(element_class).c_str(),
mirror::Class::PrettyDescriptor(array_class).c_str()).c_str());
}
// BootstrapMethodError
void ThrowBootstrapMethodError(const char* fmt, ...) {
va_list args;
va_start(args, fmt);
ThrowException("Ljava/lang/BootstrapMethodError;", nullptr, fmt, &args);
va_end(args);
}
void ThrowWrappedBootstrapMethodError(const char* fmt, ...) {
va_list args;
va_start(args, fmt);
ThrowWrappedException("Ljava/lang/BootstrapMethodError;", nullptr, fmt, &args);
va_end(args);
}
// ClassCastException
void ThrowClassCastException(ObjPtr<mirror::Class> dest_type, ObjPtr<mirror::Class> src_type) {
DumpB77342775DebugData(dest_type, src_type);
ThrowException("Ljava/lang/ClassCastException;", nullptr,
StringPrintf("%s cannot be cast to %s",
mirror::Class::PrettyDescriptor(src_type).c_str(),
mirror::Class::PrettyDescriptor(dest_type).c_str()).c_str());
}
void ThrowClassCastException(const char* msg) {
ThrowException("Ljava/lang/ClassCastException;", nullptr, msg);
}
// ClassCircularityError
void ThrowClassCircularityError(ObjPtr<mirror::Class> c) {
std::ostringstream msg;
msg << mirror::Class::PrettyDescriptor(c);
ThrowException("Ljava/lang/ClassCircularityError;", c, msg.str().c_str());
}
void ThrowClassCircularityError(ObjPtr<mirror::Class> c, const char* fmt, ...) {
va_list args;
va_start(args, fmt);
ThrowException("Ljava/lang/ClassCircularityError;", c, fmt, &args);
va_end(args);
}
// ClassFormatError
void ThrowClassFormatError(ObjPtr<mirror::Class> referrer, const char* fmt, ...) {
va_list args;
va_start(args, fmt);
ThrowException("Ljava/lang/ClassFormatError;", referrer, fmt, &args);
va_end(args);
}
// IllegalAccessError
void ThrowIllegalAccessErrorClass(ObjPtr<mirror::Class> referrer, ObjPtr<mirror::Class> accessed) {
std::ostringstream msg;
msg << "Illegal class access: '" << mirror::Class::PrettyDescriptor(referrer)
<< "' attempting to access '" << mirror::Class::PrettyDescriptor(accessed) << "'";
ThrowException("Ljava/lang/IllegalAccessError;", referrer, msg.str().c_str());
}
void ThrowIllegalAccessErrorClassForMethodDispatch(ObjPtr<mirror::Class> referrer,
ObjPtr<mirror::Class> accessed,
ArtMethod* called,
InvokeType type) {
std::ostringstream msg;
msg << "Illegal class access ('" << mirror::Class::PrettyDescriptor(referrer)
<< "' attempting to access '"
<< mirror::Class::PrettyDescriptor(accessed) << "') in attempt to invoke " << type
<< " method " << ArtMethod::PrettyMethod(called).c_str();
ThrowException("Ljava/lang/IllegalAccessError;", referrer, msg.str().c_str());
}
void ThrowIllegalAccessErrorMethod(ObjPtr<mirror::Class> referrer, ArtMethod* accessed) {
std::ostringstream msg;
msg << "Method '" << ArtMethod::PrettyMethod(accessed) << "' is inaccessible to class '"
<< mirror::Class::PrettyDescriptor(referrer) << "'";
ThrowException("Ljava/lang/IllegalAccessError;", referrer, msg.str().c_str());
}
void ThrowIllegalAccessErrorField(ObjPtr<mirror::Class> referrer, ArtField* accessed) {
std::ostringstream msg;
msg << "Field '" << ArtField::PrettyField(accessed, false) << "' is inaccessible to class '"
<< mirror::Class::PrettyDescriptor(referrer) << "'";
ThrowException("Ljava/lang/IllegalAccessError;", referrer, msg.str().c_str());
}
void ThrowIllegalAccessErrorFinalField(ArtMethod* referrer, ArtField* accessed) {
std::ostringstream msg;
msg << "Final field '" << ArtField::PrettyField(accessed, false)
<< "' cannot be written to by method '" << ArtMethod::PrettyMethod(referrer) << "'";
ThrowException("Ljava/lang/IllegalAccessError;",
referrer != nullptr ? referrer->GetDeclaringClass() : nullptr,
msg.str().c_str());
}
void ThrowIllegalAccessError(ObjPtr<mirror::Class> referrer, const char* fmt, ...) {
va_list args;
va_start(args, fmt);
ThrowException("Ljava/lang/IllegalAccessError;", referrer, fmt, &args);
va_end(args);
}
// IllegalAccessException
void ThrowIllegalAccessException(const char* msg) {
ThrowException("Ljava/lang/IllegalAccessException;", nullptr, msg);
}
// IllegalArgumentException
void ThrowIllegalArgumentException(const char* msg) {
ThrowException("Ljava/lang/IllegalArgumentException;", nullptr, msg);
}
// IllegalStateException
void ThrowIllegalStateException(const char* msg) {
ThrowException("Ljava/lang/IllegalStateException;", nullptr, msg);
}
// IncompatibleClassChangeError
void ThrowIncompatibleClassChangeError(InvokeType expected_type, InvokeType found_type,
ArtMethod* method, ArtMethod* referrer) {
std::ostringstream msg;
msg << "The method '" << ArtMethod::PrettyMethod(method) << "' was expected to be of type "
<< expected_type << " but instead was found to be of type " << found_type;
ThrowException("Ljava/lang/IncompatibleClassChangeError;",
referrer != nullptr ? referrer->GetDeclaringClass() : nullptr,
msg.str().c_str());
}
void ThrowIncompatibleClassChangeErrorClassForInterfaceSuper(ArtMethod* method,
ObjPtr<mirror::Class> target_class,
ObjPtr<mirror::Object> this_object,
ArtMethod* referrer) {
// Referrer is calling interface_method on this_object, however, the interface_method isn't
// implemented by this_object.
CHECK(this_object != nullptr);
std::ostringstream msg;
msg << "Class '" << mirror::Class::PrettyDescriptor(this_object->GetClass())
<< "' does not implement interface '" << mirror::Class::PrettyDescriptor(target_class)
<< "' in call to '"
<< ArtMethod::PrettyMethod(method) << "'";
DumpB77342775DebugData(target_class, this_object->GetClass());
ThrowException("Ljava/lang/IncompatibleClassChangeError;",
referrer != nullptr ? referrer->GetDeclaringClass() : nullptr,
msg.str().c_str());
}
void ThrowIncompatibleClassChangeErrorClassForInterfaceDispatch(ArtMethod* interface_method,
ObjPtr<mirror::Object> this_object,
ArtMethod* referrer) {
// Referrer is calling interface_method on this_object, however, the interface_method isn't
// implemented by this_object.
CHECK(this_object != nullptr);
std::ostringstream msg;
msg << "Class '" << mirror::Class::PrettyDescriptor(this_object->GetClass())
<< "' does not implement interface '"
<< mirror::Class::PrettyDescriptor(interface_method->GetDeclaringClass())
<< "' in call to '" << ArtMethod::PrettyMethod(interface_method) << "'";
DumpB77342775DebugData(interface_method->GetDeclaringClass(), this_object->GetClass());
ThrowException("Ljava/lang/IncompatibleClassChangeError;",
referrer != nullptr ? referrer->GetDeclaringClass() : nullptr,
msg.str().c_str());
}
void ThrowIncompatibleClassChangeErrorField(ArtField* resolved_field, bool is_static,
ArtMethod* referrer) {
std::ostringstream msg;
msg << "Expected '" << ArtField::PrettyField(resolved_field) << "' to be a "
<< (is_static ? "static" : "instance") << " field" << " rather than a "
<< (is_static ? "instance" : "static") << " field";
ThrowException("Ljava/lang/IncompatibleClassChangeError;", referrer->GetDeclaringClass(),
msg.str().c_str());
}
void ThrowIncompatibleClassChangeError(ObjPtr<mirror::Class> referrer, const char* fmt, ...) {
va_list args;
va_start(args, fmt);
ThrowException("Ljava/lang/IncompatibleClassChangeError;", referrer, fmt, &args);
va_end(args);
}
void ThrowIncompatibleClassChangeErrorForMethodConflict(ArtMethod* method) {
DCHECK(method != nullptr);
ThrowException("Ljava/lang/IncompatibleClassChangeError;",
/*referrer=*/nullptr,
StringPrintf("Conflicting default method implementations %s",
ArtMethod::PrettyMethod(method).c_str()).c_str());
}
// IndexOutOfBoundsException
void ThrowIndexOutOfBoundsException(int index, int length) {
ThrowException("Ljava/lang/IndexOutOfBoundsException;", nullptr,
StringPrintf("length=%d; index=%d", length, index).c_str());
}
// InternalError
void ThrowInternalError(const char* fmt, ...) {
va_list args;
va_start(args, fmt);
ThrowException("Ljava/lang/InternalError;", nullptr, fmt, &args);
va_end(args);
}
// IOException
void ThrowIOException(const char* fmt, ...) {
va_list args;
va_start(args, fmt);
ThrowException("Ljava/io/IOException;", nullptr, fmt, &args);
va_end(args);
}
void ThrowWrappedIOException(const char* fmt, ...) {
va_list args;
va_start(args, fmt);
ThrowWrappedException("Ljava/io/IOException;", nullptr, fmt, &args);
va_end(args);
}
// LinkageError
void ThrowLinkageError(ObjPtr<mirror::Class> referrer, const char* fmt, ...) {
va_list args;
va_start(args, fmt);
ThrowException("Ljava/lang/LinkageError;", referrer, fmt, &args);
va_end(args);
}
void ThrowWrappedLinkageError(ObjPtr<mirror::Class> referrer, const char* fmt, ...) {
va_list args;
va_start(args, fmt);
ThrowWrappedException("Ljava/lang/LinkageError;", referrer, fmt, &args);
va_end(args);
}
// NegativeArraySizeException
void ThrowNegativeArraySizeException(int size) {
ThrowException("Ljava/lang/NegativeArraySizeException;", nullptr,
StringPrintf("%d", size).c_str());
}
void ThrowNegativeArraySizeException(const char* msg) {
ThrowException("Ljava/lang/NegativeArraySizeException;", nullptr, msg);
}
// NoSuchFieldError
void ThrowNoSuchFieldError(std::string_view scope,
ObjPtr<mirror::Class> c,
std::string_view type,
std::string_view name) {
std::ostringstream msg;
std::string temp;
msg << "No " << scope << "field " << name << " of type " << type
<< " in class " << c->GetDescriptor(&temp) << " or its superclasses";
ThrowException("Ljava/lang/NoSuchFieldError;", c, msg.str().c_str());
}
void ThrowNoSuchFieldException(ObjPtr<mirror::Class> c, std::string_view name) {
std::ostringstream msg;
std::string temp;
msg << "No field " << name << " in class " << c->GetDescriptor(&temp);
ThrowException("Ljava/lang/NoSuchFieldException;", c, msg.str().c_str());
}
// NoSuchMethodError
void ThrowNoSuchMethodError(InvokeType type,
ObjPtr<mirror::Class> c,
std::string_view name,
const Signature& signature) {
std::ostringstream msg;
std::string temp;
msg << "No " << type << " method " << name << signature
<< " in class " << c->GetDescriptor(&temp) << " or its super classes";
ThrowException("Ljava/lang/NoSuchMethodError;", c, msg.str().c_str());
}
// NullPointerException
void ThrowNullPointerExceptionForFieldAccess(ArtField* field, bool is_read) {
std::ostringstream msg;
msg << "Attempt to " << (is_read ? "read from" : "write to")
<< " field '" << ArtField::PrettyField(field, true) << "' on a null object reference";
ThrowException("Ljava/lang/NullPointerException;", nullptr, msg.str().c_str());
}
static void ThrowNullPointerExceptionForMethodAccessImpl(uint32_t method_idx,
const DexFile& dex_file,
InvokeType type)
REQUIRES_SHARED(Locks::mutator_lock_) {
std::ostringstream msg;
msg << "Attempt to invoke " << type << " method '"
<< dex_file.PrettyMethod(method_idx, true) << "' on a null object reference";
ThrowException("Ljava/lang/NullPointerException;", nullptr, msg.str().c_str());
}
void ThrowNullPointerExceptionForMethodAccess(uint32_t method_idx, InvokeType type) {
const DexFile& dex_file = *Thread::Current()->GetCurrentMethod(nullptr)->GetDexFile();
ThrowNullPointerExceptionForMethodAccessImpl(method_idx, dex_file, type);
}
void ThrowNullPointerExceptionForMethodAccess(ArtMethod* method, InvokeType type) {
ThrowNullPointerExceptionForMethodAccessImpl(method->GetDexMethodIndex(),
*method->GetDexFile(),
type);
}
static bool IsValidReadBarrierImplicitCheck(uintptr_t addr) {
DCHECK(kEmitCompilerReadBarrier);
uint32_t monitor_offset = mirror::Object::MonitorOffset().Uint32Value();
if (kUseBakerReadBarrier &&
(kRuntimeISA == InstructionSet::kX86 || kRuntimeISA == InstructionSet::kX86_64)) {
constexpr uint32_t gray_byte_position = LockWord::kReadBarrierStateShift / kBitsPerByte;
monitor_offset += gray_byte_position;
}
return addr == monitor_offset;
}
static bool IsValidImplicitCheck(uintptr_t addr, const Instruction& instr)
REQUIRES_SHARED(Locks::mutator_lock_) {
if (!CanDoImplicitNullCheckOn(addr)) {
return false;
}
switch (instr.Opcode()) {
case Instruction::INVOKE_DIRECT:
case Instruction::INVOKE_DIRECT_RANGE:
case Instruction::INVOKE_VIRTUAL:
case Instruction::INVOKE_VIRTUAL_RANGE:
case Instruction::INVOKE_INTERFACE:
case Instruction::INVOKE_INTERFACE_RANGE:
case Instruction::INVOKE_POLYMORPHIC:
case Instruction::INVOKE_POLYMORPHIC_RANGE:
case Instruction::INVOKE_SUPER:
case Instruction::INVOKE_SUPER_RANGE:
case Instruction::INVOKE_VIRTUAL_QUICK:
case Instruction::INVOKE_VIRTUAL_RANGE_QUICK: {
// Without inlining, we could just check that the offset is the class offset.
// However, when inlining, the compiler can (validly) merge the null check with a field access
// on the same object. Note that the stack map at the NPE will reflect the invoke's location,
// which is the caller.
return true;
}
case Instruction::IGET_OBJECT:
if (kEmitCompilerReadBarrier && IsValidReadBarrierImplicitCheck(addr)) {
return true;
}
FALLTHROUGH_INTENDED;
case Instruction::IGET:
case Instruction::IGET_WIDE:
case Instruction::IGET_BOOLEAN:
case Instruction::IGET_BYTE:
case Instruction::IGET_CHAR:
case Instruction::IGET_SHORT:
case Instruction::IPUT:
case Instruction::IPUT_WIDE:
case Instruction::IPUT_OBJECT:
case Instruction::IPUT_BOOLEAN:
case Instruction::IPUT_BYTE:
case Instruction::IPUT_CHAR:
case Instruction::IPUT_SHORT: {
// We might be doing an implicit null check with an offset that doesn't correspond
// to the instruction, for example with two field accesses and the first one being
// eliminated or re-ordered.
return true;
}
case Instruction::IGET_OBJECT_QUICK:
if (kEmitCompilerReadBarrier && IsValidReadBarrierImplicitCheck(addr)) {
return true;
}
FALLTHROUGH_INTENDED;
case Instruction::IGET_QUICK:
case Instruction::IGET_BOOLEAN_QUICK:
case Instruction::IGET_BYTE_QUICK:
case Instruction::IGET_CHAR_QUICK:
case Instruction::IGET_SHORT_QUICK:
case Instruction::IGET_WIDE_QUICK:
case Instruction::IPUT_QUICK:
case Instruction::IPUT_BOOLEAN_QUICK:
case Instruction::IPUT_BYTE_QUICK:
case Instruction::IPUT_CHAR_QUICK:
case Instruction::IPUT_SHORT_QUICK:
case Instruction::IPUT_WIDE_QUICK:
case Instruction::IPUT_OBJECT_QUICK: {
// We might be doing an implicit null check with an offset that doesn't correspond
// to the instruction, for example with two field accesses and the first one being
// eliminated or re-ordered.
return true;
}
case Instruction::AGET_OBJECT:
if (kEmitCompilerReadBarrier && IsValidReadBarrierImplicitCheck(addr)) {
return true;
}
FALLTHROUGH_INTENDED;
case Instruction::AGET:
case Instruction::AGET_WIDE:
case Instruction::AGET_BOOLEAN:
case Instruction::AGET_BYTE:
case Instruction::AGET_CHAR:
case Instruction::AGET_SHORT:
case Instruction::APUT:
case Instruction::APUT_WIDE:
case Instruction::APUT_OBJECT:
case Instruction::APUT_BOOLEAN:
case Instruction::APUT_BYTE:
case Instruction::APUT_CHAR:
case Instruction::APUT_SHORT:
case Instruction::FILL_ARRAY_DATA:
case Instruction::ARRAY_LENGTH: {
// The length access should crash. We currently do not do implicit checks on
// the array access itself.
return (addr == 0u) || (addr == mirror::Array::LengthOffset().Uint32Value());
}
default: {
// We have covered all the cases where an NPE could occur.
// Note that this must be kept in sync with the compiler, and adding
// any new way to do implicit checks in the compiler should also update
// this code.
return false;
}
}
}
void ThrowNullPointerExceptionFromDexPC(bool check_address, uintptr_t addr) {
uint32_t throw_dex_pc;
ArtMethod* method = Thread::Current()->GetCurrentMethod(&throw_dex_pc);
CodeItemInstructionAccessor accessor(method->DexInstructions());
CHECK_LT(throw_dex_pc, accessor.InsnsSizeInCodeUnits());
const Instruction& instr = accessor.InstructionAt(throw_dex_pc);
if (check_address && !IsValidImplicitCheck(addr, instr)) {
const DexFile* dex_file = method->GetDexFile();
LOG(FATAL) << "Invalid address for an implicit NullPointerException check: "
<< "0x" << std::hex << addr << std::dec
<< ", at "
<< instr.DumpString(dex_file)
<< " in "
<< method->PrettyMethod();
}
switch (instr.Opcode()) {
case Instruction::INVOKE_DIRECT:
ThrowNullPointerExceptionForMethodAccess(instr.VRegB_35c(), kDirect);
break;
case Instruction::INVOKE_DIRECT_RANGE:
ThrowNullPointerExceptionForMethodAccess(instr.VRegB_3rc(), kDirect);
break;
case Instruction::INVOKE_VIRTUAL:
ThrowNullPointerExceptionForMethodAccess(instr.VRegB_35c(), kVirtual);
break;
case Instruction::INVOKE_VIRTUAL_RANGE:
ThrowNullPointerExceptionForMethodAccess(instr.VRegB_3rc(), kVirtual);
break;
case Instruction::INVOKE_SUPER:
ThrowNullPointerExceptionForMethodAccess(instr.VRegB_35c(), kSuper);
break;
case Instruction::INVOKE_SUPER_RANGE:
ThrowNullPointerExceptionForMethodAccess(instr.VRegB_3rc(), kSuper);
break;
case Instruction::INVOKE_INTERFACE:
ThrowNullPointerExceptionForMethodAccess(instr.VRegB_35c(), kInterface);
break;
case Instruction::INVOKE_INTERFACE_RANGE:
ThrowNullPointerExceptionForMethodAccess(instr.VRegB_3rc(), kInterface);
break;
case Instruction::INVOKE_POLYMORPHIC:
ThrowNullPointerExceptionForMethodAccess(instr.VRegB_45cc(), kVirtual);
break;
case Instruction::INVOKE_POLYMORPHIC_RANGE:
ThrowNullPointerExceptionForMethodAccess(instr.VRegB_4rcc(), kVirtual);
break;
case Instruction::INVOKE_VIRTUAL_QUICK:
case Instruction::INVOKE_VIRTUAL_RANGE_QUICK: {
uint16_t method_idx = method->GetIndexFromQuickening(throw_dex_pc);
if (method_idx != DexFile::kDexNoIndex16) {
// NPE with precise message.
ThrowNullPointerExceptionForMethodAccess(method_idx, kVirtual);
} else {
// NPE with imprecise message.
ThrowNullPointerException("Attempt to invoke a virtual method on a null object reference");
}
break;
}
case Instruction::IGET:
case Instruction::IGET_WIDE:
case Instruction::IGET_OBJECT:
case Instruction::IGET_BOOLEAN:
case Instruction::IGET_BYTE:
case Instruction::IGET_CHAR:
case Instruction::IGET_SHORT: {
ArtField* field =
Runtime::Current()->GetClassLinker()->ResolveField(instr.VRegC_22c(), method, false);
Thread::Current()->ClearException(); // Resolution may fail, ignore.
ThrowNullPointerExceptionForFieldAccess(field, /* is_read= */ true);
break;
}
case Instruction::IGET_QUICK:
case Instruction::IGET_BOOLEAN_QUICK:
case Instruction::IGET_BYTE_QUICK:
case Instruction::IGET_CHAR_QUICK:
case Instruction::IGET_SHORT_QUICK:
case Instruction::IGET_WIDE_QUICK:
case Instruction::IGET_OBJECT_QUICK: {
uint16_t field_idx = method->GetIndexFromQuickening(throw_dex_pc);
ArtField* field = nullptr;
CHECK_NE(field_idx, DexFile::kDexNoIndex16);
field = Runtime::Current()->GetClassLinker()->ResolveField(
field_idx, method, /* is_static= */ false);
Thread::Current()->ClearException(); // Resolution may fail, ignore.
ThrowNullPointerExceptionForFieldAccess(field, /* is_read= */ true);
break;
}
case Instruction::IPUT:
case Instruction::IPUT_WIDE:
case Instruction::IPUT_OBJECT:
case Instruction::IPUT_BOOLEAN:
case Instruction::IPUT_BYTE:
case Instruction::IPUT_CHAR:
case Instruction::IPUT_SHORT: {
ArtField* field = Runtime::Current()->GetClassLinker()->ResolveField(
instr.VRegC_22c(), method, /* is_static= */ false);
Thread::Current()->ClearException(); // Resolution may fail, ignore.
ThrowNullPointerExceptionForFieldAccess(field, /* is_read= */ false);
break;
}
case Instruction::IPUT_QUICK:
case Instruction::IPUT_BOOLEAN_QUICK:
case Instruction::IPUT_BYTE_QUICK:
case Instruction::IPUT_CHAR_QUICK:
case Instruction::IPUT_SHORT_QUICK:
case Instruction::IPUT_WIDE_QUICK:
case Instruction::IPUT_OBJECT_QUICK: {
uint16_t field_idx = method->GetIndexFromQuickening(throw_dex_pc);
ArtField* field = nullptr;
CHECK_NE(field_idx, DexFile::kDexNoIndex16);
field = Runtime::Current()->GetClassLinker()->ResolveField(
field_idx, method, /* is_static= */ false);
Thread::Current()->ClearException(); // Resolution may fail, ignore.
ThrowNullPointerExceptionForFieldAccess(field, /* is_read= */ false);
break;
}
case Instruction::AGET:
case Instruction::AGET_WIDE:
case Instruction::AGET_OBJECT:
case Instruction::AGET_BOOLEAN:
case Instruction::AGET_BYTE:
case Instruction::AGET_CHAR:
case Instruction::AGET_SHORT:
ThrowException("Ljava/lang/NullPointerException;", nullptr,
"Attempt to read from null array");
break;
case Instruction::APUT:
case Instruction::APUT_WIDE:
case Instruction::APUT_OBJECT:
case Instruction::APUT_BOOLEAN:
case Instruction::APUT_BYTE:
case Instruction::APUT_CHAR:
case Instruction::APUT_SHORT:
ThrowException("Ljava/lang/NullPointerException;", nullptr,
"Attempt to write to null array");
break;
case Instruction::ARRAY_LENGTH:
ThrowException("Ljava/lang/NullPointerException;", nullptr,
"Attempt to get length of null array");
break;
case Instruction::FILL_ARRAY_DATA: {
ThrowException("Ljava/lang/NullPointerException;", nullptr,
"Attempt to write to null array");
break;
}
case Instruction::MONITOR_ENTER:
case Instruction::MONITOR_EXIT: {
ThrowException("Ljava/lang/NullPointerException;", nullptr,
"Attempt to do a synchronize operation on a null object");
break;
}
default: {
const DexFile* dex_file = method->GetDexFile();
LOG(FATAL) << "NullPointerException at an unexpected instruction: "
<< instr.DumpString(dex_file)
<< " in "
<< method->PrettyMethod();
UNREACHABLE();
}
}
}
void ThrowNullPointerException(const char* msg) {
ThrowException("Ljava/lang/NullPointerException;", nullptr, msg);
}
void ThrowNullPointerException() {
ThrowException("Ljava/lang/NullPointerException;");
}
// ReadOnlyBufferException
void ThrowReadOnlyBufferException() {
Thread::Current()->ThrowNewException("Ljava/nio/ReadOnlyBufferException;", nullptr);
}
// RuntimeException
void ThrowRuntimeException(const char* fmt, ...) {
va_list args;
va_start(args, fmt);
ThrowException("Ljava/lang/RuntimeException;", nullptr, fmt, &args);
va_end(args);
}
// SecurityException
void ThrowSecurityException(const char* fmt, ...) {
va_list args;
va_start(args, fmt);
ThrowException("Ljava/lang/SecurityException;", nullptr, fmt, &args);
va_end(args);
}
// Stack overflow.
void ThrowStackOverflowError(Thread* self) {
if (self->IsHandlingStackOverflow()) {
LOG(ERROR) << "Recursive stack overflow.";
// We don't fail here because SetStackEndForStackOverflow will print better diagnostics.
}
self->SetStackEndForStackOverflow(); // Allow space on the stack for constructor to execute.
JNIEnvExt* env = self->GetJniEnv();
std::string msg("stack size ");
msg += PrettySize(self->GetStackSize());
// Avoid running Java code for exception initialization.
// TODO: Checks to make this a bit less brittle.
//
// Note: this lambda ensures that the destruction of the ScopedLocalRefs will run in the extended
// stack, which is important for modes with larger stack sizes (e.g., ASAN). Using a lambda
// instead of a block simplifies the control flow.
auto create_and_throw = [&]() REQUIRES_SHARED(Locks::mutator_lock_) {
// Allocate an uninitialized object.
ScopedLocalRef<jobject> exc(env,
env->AllocObject(WellKnownClasses::java_lang_StackOverflowError));
if (exc == nullptr) {
LOG(WARNING) << "Could not allocate StackOverflowError object.";
return;
}
// "Initialize".
// StackOverflowError -> VirtualMachineError -> Error -> Throwable -> Object.
// Only Throwable has "custom" fields:
// String detailMessage.
// Throwable cause (= this).
// List<Throwable> suppressedExceptions (= Collections.emptyList()).
// Object stackState;
// StackTraceElement[] stackTrace;
// Only Throwable has a non-empty constructor:
// this.stackTrace = EmptyArray.STACK_TRACE_ELEMENT;
// fillInStackTrace();
// detailMessage.
// TODO: Use String::FromModifiedUTF...?
ScopedLocalRef<jstring> s(env, env->NewStringUTF(msg.c_str()));
if (s == nullptr) {
LOG(WARNING) << "Could not throw new StackOverflowError because JNI NewStringUTF failed.";
return;
}
env->SetObjectField(exc.get(), WellKnownClasses::java_lang_Throwable_detailMessage, s.get());
// cause.
env->SetObjectField(exc.get(), WellKnownClasses::java_lang_Throwable_cause, exc.get());
// suppressedExceptions.
ScopedLocalRef<jobject> emptylist(env, env->GetStaticObjectField(
WellKnownClasses::java_util_Collections,
WellKnownClasses::java_util_Collections_EMPTY_LIST));
CHECK(emptylist != nullptr);
env->SetObjectField(exc.get(),
WellKnownClasses::java_lang_Throwable_suppressedExceptions,
emptylist.get());
// stackState is set as result of fillInStackTrace. fillInStackTrace calls
// nativeFillInStackTrace.
ScopedLocalRef<jobject> stack_state_val(env, nullptr);
{
ScopedObjectAccessUnchecked soa(env); // TODO: Is this necessary?
stack_state_val.reset(soa.Self()->CreateInternalStackTrace<false>(soa));
}
if (stack_state_val != nullptr) {
env->SetObjectField(exc.get(),
WellKnownClasses::java_lang_Throwable_stackState,
stack_state_val.get());
// stackTrace.
ScopedLocalRef<jobject> stack_trace_elem(env, env->GetStaticObjectField(
WellKnownClasses::libcore_util_EmptyArray,
WellKnownClasses::libcore_util_EmptyArray_STACK_TRACE_ELEMENT));
env->SetObjectField(exc.get(),
WellKnownClasses::java_lang_Throwable_stackTrace,
stack_trace_elem.get());
} else {
LOG(WARNING) << "Could not create stack trace.";
// Note: we'll create an exception without stack state, which is valid.
}
// Throw the exception.
self->SetException(self->DecodeJObject(exc.get())->AsThrowable());
};
create_and_throw();
CHECK(self->IsExceptionPending());
bool explicit_overflow_check = Runtime::Current()->ExplicitStackOverflowChecks();
self->ResetDefaultStackEnd(); // Return to default stack size.
// And restore protection if implicit checks are on.
if (!explicit_overflow_check) {
self->ProtectStack();
}
}
// StringIndexOutOfBoundsException
void ThrowStringIndexOutOfBoundsException(int index, int length) {
ThrowException("Ljava/lang/StringIndexOutOfBoundsException;", nullptr,
StringPrintf("length=%d; index=%d", length, index).c_str());
}
// UnsupportedOperationException
void ThrowUnsupportedOperationException() {
ThrowException("Ljava/lang/UnsupportedOperationException;");
}
// VerifyError
void ThrowVerifyError(ObjPtr<mirror::Class> referrer, const char* fmt, ...) {
va_list args;
va_start(args, fmt);
ThrowException("Ljava/lang/VerifyError;", referrer, fmt, &args);
va_end(args);
}
// WrongMethodTypeException
void ThrowWrongMethodTypeException(ObjPtr<mirror::MethodType> expected_type,
ObjPtr<mirror::MethodType> actual_type) {
ThrowWrongMethodTypeException(expected_type->PrettyDescriptor(), actual_type->PrettyDescriptor());
}
void ThrowWrongMethodTypeException(const std::string& expected_descriptor,
const std::string& actual_descriptor) {
std::ostringstream msg;
msg << "Expected " << expected_descriptor << " but was " << actual_descriptor;
ThrowException("Ljava/lang/invoke/WrongMethodTypeException;", nullptr, msg.str().c_str());
}
} // namespace art
|