71 #include "services/threadService.hpp" 72 #include "utilities/dtrace.hpp" 73 #include "utilities/macros.hpp" 74 #include "utilities/stringUtils.hpp" 75 #include "logging/log.hpp" 76 #ifdef COMPILER1 77 #include "c1/c1_Compiler.hpp" 78 #endif 79 80 #ifdef DTRACE_ENABLED 81 82 83 #define HOTSPOT_CLASS_INITIALIZATION_required HOTSPOT_CLASS_INITIALIZATION_REQUIRED 84 #define HOTSPOT_CLASS_INITIALIZATION_recursive HOTSPOT_CLASS_INITIALIZATION_RECURSIVE 85 #define HOTSPOT_CLASS_INITIALIZATION_concurrent HOTSPOT_CLASS_INITIALIZATION_CONCURRENT 86 #define HOTSPOT_CLASS_INITIALIZATION_erroneous HOTSPOT_CLASS_INITIALIZATION_ERRONEOUS 87 #define HOTSPOT_CLASS_INITIALIZATION_super__failed HOTSPOT_CLASS_INITIALIZATION_SUPER_FAILED 88 #define HOTSPOT_CLASS_INITIALIZATION_clinit HOTSPOT_CLASS_INITIALIZATION_CLINIT 89 #define HOTSPOT_CLASS_INITIALIZATION_error HOTSPOT_CLASS_INITIALIZATION_ERROR 90 #define HOTSPOT_CLASS_INITIALIZATION_end HOTSPOT_CLASS_INITIALIZATION_END 91 #define DTRACE_CLASSINIT_PROBE(type, clss, thread_type) \ 92 { \ 93 char* data = NULL; \ 94 int len = 0; \ 95 Symbol* name = (clss)->name(); \ 96 if (name != NULL) { \ 97 data = (char*)name->bytes(); \ 98 len = name->utf8_length(); \ 99 } \ 100 HOTSPOT_CLASS_INITIALIZATION_##type( \ 101 data, len, (clss)->class_loader(), thread_type); \ 102 } 103 104 #define DTRACE_CLASSINIT_PROBE_WAIT(type, clss, thread_type, wait) \ 105 { \ 106 char* data = NULL; \ 107 int len = 0; \ 108 Symbol* name = (clss)->name(); \ 109 if (name != NULL) { \ 110 data = (char*)name->bytes(); \ 111 len = name->utf8_length(); \ 112 } \ 113 HOTSPOT_CLASS_INITIALIZATION_##type( \ 114 data, len, (clss)->class_loader(), thread_type, wait); \ 115 } 116 117 #else // ndef DTRACE_ENABLED 118 119 #define DTRACE_CLASSINIT_PROBE(type, clss, thread_type) 120 #define DTRACE_CLASSINIT_PROBE_WAIT(type, clss, thread_type, wait) 121 122 #endif // ndef DTRACE_ENABLED 123 124 volatile int InstanceKlass::_total_instanceKlass_count = 0; 125 126 static inline bool is_class_loader(const Symbol* class_name, 127 const ClassFileParser& parser) { 128 assert(class_name != NULL, "invariant"); 129 130 if (class_name == vmSymbols::java_lang_ClassLoader()) { 131 return true; 132 } 133 134 if (SystemDictionary::ClassLoader_klass_loaded()) { 135 const Klass* const super_klass = parser.super_klass(); 136 if (super_klass != NULL) { 137 if (super_klass->is_subtype_of(SystemDictionary::ClassLoader_klass())) { 138 return true; 139 } 140 } 368 } 369 set_annotations(NULL); 370 } 371 372 bool InstanceKlass::should_be_initialized() const { 373 return !is_initialized(); 374 } 375 376 klassItable* InstanceKlass::itable() const { 377 return new klassItable(const_cast<InstanceKlass*>(this)); 378 } 379 380 void InstanceKlass::eager_initialize(Thread *thread) { 381 if (!EagerInitialization) return; 382 383 if (this->is_not_initialized()) { 384 // abort if the the class has a class initializer 385 if (this->class_initializer() != NULL) return; 386 387 // abort if it is java.lang.Object (initialization is handled in genesis) 388 Klass* super = this->super(); 389 if (super == NULL) return; 390 391 // abort if the super class should be initialized 392 if (!InstanceKlass::cast(super)->is_initialized()) return; 393 394 // call body to expose the this pointer 395 eager_initialize_impl(this); 396 } 397 } 398 399 // JVMTI spec thinks there are signers and protection domain in the 400 // instanceKlass. These accessors pretend these fields are there. 401 // The hprof specification also thinks these fields are in InstanceKlass. 402 oop InstanceKlass::protection_domain() const { 403 // return the protection_domain from the mirror 404 return java_lang_Class::protection_domain(java_mirror()); 405 } 406 407 // To remove these from requires an incompatible change and CCC request. 408 objArrayOop InstanceKlass::signers() const { 409 // return the signers from the mirror 410 return java_lang_Class::signers(java_mirror()); 411 } 412 413 oop InstanceKlass::init_lock() const { 414 // return the init lock from the mirror 415 oop lock = java_lang_Class::init_lock(java_mirror()); 416 // Prevent reordering with any access of initialization state 417 OrderAccess::loadload(); 418 assert((oop)lock != NULL || !is_not_initialized(), // initialized or in_error state 419 "only fully initialized state can have a null lock"); 420 return lock; 421 } 422 423 // Set the initialization lock to null so the object can be GC'ed. Any racing 424 // threads to get this lock will see a null lock and will not lock. 425 // That's okay because they all check for initialized state after getting 426 // the lock and return. 427 void InstanceKlass::fence_and_clear_init_lock() { 428 // make sure previous stores are all done, notably the init_state. 429 OrderAccess::storestore(); 430 java_lang_Class::set_init_lock(java_mirror(), NULL); 431 assert(!is_not_initialized(), "class must be initialized now"); 432 } 433 434 void InstanceKlass::eager_initialize_impl(InstanceKlass* this_k) { 435 EXCEPTION_MARK; 436 HandleMark hm(THREAD); 437 Handle init_lock(THREAD, this_k->init_lock()); 438 ObjectLocker ol(init_lock, THREAD, init_lock() != NULL); 439 440 // abort if someone beat us to the initialization 441 if (!this_k->is_not_initialized()) return; // note: not equivalent to is_initialized() 442 443 ClassState old_state = this_k->init_state(); 444 link_class_impl(this_k, true, THREAD); 445 if (HAS_PENDING_EXCEPTION) { 446 CLEAR_PENDING_EXCEPTION; 447 // Abort if linking the class throws an exception. 448 449 // Use a test to avoid redundantly resetting the state if there's 450 // no change. Set_init_state() asserts that state changes make 451 // progress, whereas here we might just be spinning in place. 452 if( old_state != this_k->_init_state ) 453 this_k->set_init_state (old_state); 454 } else { 455 // linking successfull, mark class as initialized 456 this_k->set_init_state (fully_initialized); 457 this_k->fence_and_clear_init_lock(); 458 // trace 459 if (log_is_enabled(Info, class, init)) { 460 ResourceMark rm(THREAD); 461 log_info(class, init)("[Initialized %s without side effects]", this_k->external_name()); 462 } 463 } 464 } 465 466 467 // See "The Virtual Machine Specification" section 2.16.5 for a detailed explanation of the class initialization 468 // process. The step comments refers to the procedure described in that section. 469 // Note: implementation moved to static method to expose the this pointer. 470 void InstanceKlass::initialize(TRAPS) { 471 if (this->should_be_initialized()) { 472 initialize_impl(this, CHECK); 473 // Note: at this point the class may be initialized 474 // OR it may be in the state of being initialized 475 // in case of recursive initialization! 476 } else { 477 assert(is_initialized(), "sanity check"); 478 } 479 } 480 481 482 bool InstanceKlass::verify_code( 483 InstanceKlass* this_k, bool throw_verifyerror, TRAPS) { 484 // 1) Verify the bytecodes 485 Verifier::Mode mode = 486 throw_verifyerror ? Verifier::ThrowException : Verifier::NoException; 487 return Verifier::verify(this_k, mode, this_k->should_verify_class(), THREAD); 488 } 489 490 491 // Used exclusively by the shared spaces dump mechanism to prevent 492 // classes mapped into the shared regions in new VMs from appearing linked. 493 494 void InstanceKlass::unlink_class() { 495 assert(is_linked(), "must be linked"); 496 _init_state = loaded; 497 } 498 499 void InstanceKlass::link_class(TRAPS) { 500 assert(is_loaded(), "must be loaded"); 501 if (!is_linked()) { 502 link_class_impl(this, true, CHECK); 503 } 504 } 505 506 // Called to verify that a class can link during initialization, without 507 // throwing a VerifyError. 508 bool InstanceKlass::link_class_or_fail(TRAPS) { 509 assert(is_loaded(), "must be loaded"); 510 if (!is_linked()) { 511 link_class_impl(this, false, CHECK_false); 512 } 513 return is_linked(); 514 } 515 516 bool InstanceKlass::link_class_impl( 517 InstanceKlass* this_k, bool throw_verifyerror, TRAPS) { 518 if (DumpSharedSpaces && this_k->is_in_error_state()) { 519 // This is for CDS dumping phase only -- we use the in_error_state to indicate that 520 // the class has failed verification. Throwing the NoClassDefFoundError here is just 521 // a convenient way to stop repeat attempts to verify the same (bad) class. 522 // 523 // Note that the NoClassDefFoundError is not part of the JLS, and should not be thrown 524 // if we are executing Java code. This is not a problem for CDS dumping phase since 525 // it doesn't execute any Java code. 526 ResourceMark rm(THREAD); 527 THROW_MSG_(vmSymbols::java_lang_NoClassDefFoundError(), 528 this_k->external_name(), false); 529 } 530 // return if already verified 531 if (this_k->is_linked()) { 532 return true; 533 } 534 535 // Timing 536 // timer handles recursion 537 assert(THREAD->is_Java_thread(), "non-JavaThread in link_class_impl"); 538 JavaThread* jt = (JavaThread*)THREAD; 539 540 // link super class before linking this class 541 Klass* super = this_k->super(); 542 if (super != NULL) { 543 if (super->is_interface()) { // check if super class is an interface 544 ResourceMark rm(THREAD); 545 Exceptions::fthrow( 546 THREAD_AND_LOCATION, 547 vmSymbols::java_lang_IncompatibleClassChangeError(), 548 "class %s has interface %s as super class", 549 this_k->external_name(), 550 super->external_name() 551 ); 552 return false; 553 } 554 555 InstanceKlass* ik_super = InstanceKlass::cast(super); 556 link_class_impl(ik_super, throw_verifyerror, CHECK_false); 557 } 558 559 // link all interfaces implemented by this class before linking this class 560 Array<Klass*>* interfaces = this_k->local_interfaces(); 561 int num_interfaces = interfaces->length(); 562 for (int index = 0; index < num_interfaces; index++) { 563 InstanceKlass* interk = InstanceKlass::cast(interfaces->at(index)); 564 link_class_impl(interk, throw_verifyerror, CHECK_false); 565 } 566 567 // in case the class is linked in the process of linking its superclasses 568 if (this_k->is_linked()) { 569 return true; 570 } 571 572 // trace only the link time for this klass that includes 573 // the verification time 574 PerfClassTraceTime vmtimer(ClassLoader::perf_class_link_time(), 575 ClassLoader::perf_class_link_selftime(), 576 ClassLoader::perf_classes_linked(), 577 jt->get_thread_stat()->perf_recursion_counts_addr(), 578 jt->get_thread_stat()->perf_timers_addr(), 579 PerfClassTraceTime::CLASS_LINK); 580 581 // verification & rewriting 582 { 583 HandleMark hm(THREAD); 584 Handle init_lock(THREAD, this_k->init_lock()); 585 ObjectLocker ol(init_lock, THREAD, init_lock() != NULL); 586 // rewritten will have been set if loader constraint error found 587 // on an earlier link attempt 588 // don't verify or rewrite if already rewritten 589 // 590 591 if (!this_k->is_linked()) { 592 if (!this_k->is_rewritten()) { 593 { 594 bool verify_ok = verify_code(this_k, throw_verifyerror, THREAD); 595 if (!verify_ok) { 596 return false; 597 } 598 } 599 600 // Just in case a side-effect of verify linked this class already 601 // (which can sometimes happen since the verifier loads classes 602 // using custom class loaders, which are free to initialize things) 603 if (this_k->is_linked()) { 604 return true; 605 } 606 607 // also sets rewritten 608 this_k->rewrite_class(CHECK_false); 609 } else if (this_k->is_shared()) { 610 SystemDictionaryShared::check_verification_constraints(this_k, CHECK_false); 611 } 612 613 // relocate jsrs and link methods after they are all rewritten 614 this_k->link_methods(CHECK_false); 615 616 // Initialize the vtable and interface table after 617 // methods have been rewritten since rewrite may 618 // fabricate new Method*s. 619 // also does loader constraint checking 620 // 621 // initialize_vtable and initialize_itable need to be rerun for 622 // a shared class if the class is not loaded by the NULL classloader. 623 ClassLoaderData * loader_data = this_k->class_loader_data(); 624 if (!(this_k->is_shared() && 625 loader_data->is_the_null_class_loader_data())) { 626 ResourceMark rm(THREAD); 627 this_k->vtable()->initialize_vtable(true, CHECK_false); 628 this_k->itable()->initialize_itable(true, CHECK_false); 629 } 630 #ifdef ASSERT 631 else { 632 ResourceMark rm(THREAD); 633 this_k->vtable()->verify(tty, true); 634 // In case itable verification is ever added. 635 // this_k->itable()->verify(tty, true); 636 } 637 #endif 638 this_k->set_init_state(linked); 639 if (JvmtiExport::should_post_class_prepare()) { 640 Thread *thread = THREAD; 641 assert(thread->is_Java_thread(), "thread->is_Java_thread()"); 642 JvmtiExport::post_class_prepare((JavaThread *) thread, this_k); 643 } 644 } 645 } 646 return true; 647 } 648 649 650 // Rewrite the byte codes of all of the methods of a class. 651 // The rewriter must be called exactly once. Rewriting must happen after 652 // verification but before the first method of the class is executed. 653 void InstanceKlass::rewrite_class(TRAPS) { 654 assert(is_loaded(), "must be loaded"); 655 if (is_rewritten()) { 656 assert(is_shared(), "rewriting an unshared class?"); 657 return; 658 } 659 Rewriter::rewrite(this, CHECK); 660 set_rewritten(); 661 } 662 663 // Now relocate and link method entry points after class is rewritten. 664 // This is outside is_rewritten flag. In case of an exception, it can be 665 // executed more than once. 666 void InstanceKlass::link_methods(TRAPS) { 667 int len = methods()->length(); 668 for (int i = len-1; i >= 0; i--) { 669 methodHandle m(THREAD, methods()->at(i)); 670 671 // Set up method entry points for compiler and interpreter . 672 m->link_method(m, CHECK); 673 } 674 } 675 676 // Eagerly initialize superinterfaces that declare default methods (concrete instance: any access) 677 void InstanceKlass::initialize_super_interfaces(InstanceKlass* this_k, TRAPS) { 678 assert (this_k->has_nonstatic_concrete_methods(), "caller should have checked this"); 679 for (int i = 0; i < this_k->local_interfaces()->length(); ++i) { 680 Klass* iface = this_k->local_interfaces()->at(i); 681 InstanceKlass* ik = InstanceKlass::cast(iface); 682 683 // Initialization is depth first search ie. we start with top of the inheritance tree 684 // has_nonstatic_concrete_methods drives searching superinterfaces since it 685 // means has_nonstatic_concrete_methods in its superinterface hierarchy 686 if (ik->has_nonstatic_concrete_methods()) { 687 ik->initialize_super_interfaces(ik, CHECK); 688 } 689 690 // Only initialize() interfaces that "declare" concrete methods. 691 if (ik->should_be_initialized() && ik->declares_nonstatic_concrete_methods()) { 692 ik->initialize(CHECK); 693 } 694 } 695 } 696 697 void InstanceKlass::initialize_impl(InstanceKlass* this_k, TRAPS) { 698 HandleMark hm(THREAD); 699 700 // Make sure klass is linked (verified) before initialization 701 // A class could already be verified, since it has been reflected upon. 702 this_k->link_class(CHECK); 703 704 DTRACE_CLASSINIT_PROBE(required, this_k, -1); 705 706 bool wait = false; 707 708 // refer to the JVM book page 47 for description of steps 709 // Step 1 710 { 711 Handle init_lock(THREAD, this_k->init_lock()); 712 ObjectLocker ol(init_lock, THREAD, init_lock() != NULL); 713 714 Thread *self = THREAD; // it's passed the current thread 715 716 // Step 2 717 // If we were to use wait() instead of waitInterruptibly() then 718 // we might end up throwing IE from link/symbol resolution sites 719 // that aren't expected to throw. This would wreak havoc. See 6320309. 720 while(this_k->is_being_initialized() && !this_k->is_reentrant_initialization(self)) { 721 wait = true; 722 ol.waitUninterruptibly(CHECK); 723 } 724 725 // Step 3 726 if (this_k->is_being_initialized() && this_k->is_reentrant_initialization(self)) { 727 DTRACE_CLASSINIT_PROBE_WAIT(recursive, this_k, -1,wait); 728 return; 729 } 730 731 // Step 4 732 if (this_k->is_initialized()) { 733 DTRACE_CLASSINIT_PROBE_WAIT(concurrent, this_k, -1,wait); 734 return; 735 } 736 737 // Step 5 738 if (this_k->is_in_error_state()) { 739 DTRACE_CLASSINIT_PROBE_WAIT(erroneous, this_k, -1,wait); 740 ResourceMark rm(THREAD); 741 const char* desc = "Could not initialize class "; 742 const char* className = this_k->external_name(); 743 size_t msglen = strlen(desc) + strlen(className) + 1; 744 char* message = NEW_RESOURCE_ARRAY(char, msglen); 745 if (NULL == message) { 746 // Out of memory: can't create detailed error message 747 THROW_MSG(vmSymbols::java_lang_NoClassDefFoundError(), className); 748 } else { 749 jio_snprintf(message, msglen, "%s%s", desc, className); 750 THROW_MSG(vmSymbols::java_lang_NoClassDefFoundError(), message); 751 } 752 } 753 754 // Step 6 755 this_k->set_init_state(being_initialized); 756 this_k->set_init_thread(self); 757 } 758 759 // Step 7 760 // Next, if C is a class rather than an interface, initialize it's super class and super 761 // interfaces. 762 if (!this_k->is_interface()) { 763 Klass* super_klass = this_k->super(); 764 if (super_klass != NULL && super_klass->should_be_initialized()) { 765 super_klass->initialize(THREAD); 766 } 767 // If C implements any interface that declares a non-static, concrete method, 768 // the initialization of C triggers initialization of its super interfaces. 769 // Only need to recurse if has_nonstatic_concrete_methods which includes declaring and 770 // having a superinterface that declares, non-static, concrete methods 771 if (!HAS_PENDING_EXCEPTION && this_k->has_nonstatic_concrete_methods()) { 772 this_k->initialize_super_interfaces(this_k, THREAD); 773 } 774 775 // If any exceptions, complete abruptly, throwing the same exception as above. 776 if (HAS_PENDING_EXCEPTION) { 777 Handle e(THREAD, PENDING_EXCEPTION); 778 CLEAR_PENDING_EXCEPTION; 779 { 780 EXCEPTION_MARK; 781 // Locks object, set state, and notify all waiting threads 782 this_k->set_initialization_state_and_notify(initialization_error, THREAD); 783 CLEAR_PENDING_EXCEPTION; 784 } 785 DTRACE_CLASSINIT_PROBE_WAIT(super__failed, this_k, -1,wait); 786 THROW_OOP(e()); 787 } 788 } 789 790 791 // Look for aot compiled methods for this klass, including class initializer. 792 AOTLoader::load_for_klass(this_k, THREAD); 793 794 // Step 8 795 { 796 assert(THREAD->is_Java_thread(), "non-JavaThread in initialize_impl"); 797 JavaThread* jt = (JavaThread*)THREAD; 798 DTRACE_CLASSINIT_PROBE_WAIT(clinit, this_k, -1,wait); 799 // Timer includes any side effects of class initialization (resolution, 800 // etc), but not recursive entry into call_class_initializer(). 801 PerfClassTraceTime timer(ClassLoader::perf_class_init_time(), 802 ClassLoader::perf_class_init_selftime(), 803 ClassLoader::perf_classes_inited(), 804 jt->get_thread_stat()->perf_recursion_counts_addr(), 805 jt->get_thread_stat()->perf_timers_addr(), 806 PerfClassTraceTime::CLASS_CLINIT); 807 this_k->call_class_initializer(THREAD); 808 } 809 810 // Step 9 811 if (!HAS_PENDING_EXCEPTION) { 812 this_k->set_initialization_state_and_notify(fully_initialized, CHECK); 813 { ResourceMark rm(THREAD); 814 debug_only(this_k->vtable()->verify(tty, true);) 815 } 816 } 817 else { 818 // Step 10 and 11 819 Handle e(THREAD, PENDING_EXCEPTION); 820 CLEAR_PENDING_EXCEPTION; 821 // JVMTI has already reported the pending exception 822 // JVMTI internal flag reset is needed in order to report ExceptionInInitializerError 823 JvmtiExport::clear_detected_exception((JavaThread*)THREAD); 824 { 825 EXCEPTION_MARK; 826 this_k->set_initialization_state_and_notify(initialization_error, THREAD); 827 CLEAR_PENDING_EXCEPTION; // ignore any exception thrown, class initialization error is thrown below 828 // JVMTI has already reported the pending exception 829 // JVMTI internal flag reset is needed in order to report ExceptionInInitializerError 830 JvmtiExport::clear_detected_exception((JavaThread*)THREAD); 831 } 832 DTRACE_CLASSINIT_PROBE_WAIT(error, this_k, -1,wait); 833 if (e->is_a(SystemDictionary::Error_klass())) { 834 THROW_OOP(e()); 835 } else { 836 JavaCallArguments args(e); 837 THROW_ARG(vmSymbols::java_lang_ExceptionInInitializerError(), 838 vmSymbols::throwable_void_signature(), 839 &args); 840 } 841 } 842 DTRACE_CLASSINIT_PROBE_WAIT(end, this_k, -1,wait); 843 } 844 845 846 // Note: implementation moved to static method to expose the this pointer. 847 void InstanceKlass::set_initialization_state_and_notify(ClassState state, TRAPS) { 848 set_initialization_state_and_notify_impl(this, state, CHECK); 849 } 850 851 void InstanceKlass::set_initialization_state_and_notify_impl(InstanceKlass* this_k, ClassState state, TRAPS) { 852 Handle init_lock(THREAD, this_k->init_lock()); 853 if (init_lock() != NULL) { 854 ObjectLocker ol(init_lock, THREAD); 855 this_k->set_init_state(state); 856 this_k->fence_and_clear_init_lock(); 857 ol.notify_all(CHECK); 858 } else { 859 assert(init_lock() != NULL, "The initialization state should never be set twice"); 860 this_k->set_init_state(state); 861 } 862 } 863 864 // The embedded _implementor field can only record one implementor. 865 // When there are more than one implementors, the _implementor field 866 // is set to the interface Klass* itself. Following are the possible 867 // values for the _implementor field: 868 // NULL - no implementor 869 // implementor Klass* - one implementor 870 // self - more than one implementor 871 // 872 // The _implementor field only exists for interfaces. 873 void InstanceKlass::add_implementor(Klass* k) { 874 assert(Compile_lock->owned_by_self(), ""); 875 assert(is_interface(), "not interface"); 876 // Filter out my subinterfaces. 877 // (Note: Interfaces are never on the subklass list.) 878 if (InstanceKlass::cast(k)->is_interface()) return; 879 880 // Filter out subclasses whose supers already implement me. 1020 if (has_finalizer_flag && !RegisterFinalizersAtInit) { 1021 i = register_finalizer(i, CHECK_NULL); 1022 } 1023 return i; 1024 } 1025 1026 void InstanceKlass::check_valid_for_instantiation(bool throwError, TRAPS) { 1027 if (is_interface() || is_abstract()) { 1028 ResourceMark rm(THREAD); 1029 THROW_MSG(throwError ? vmSymbols::java_lang_InstantiationError() 1030 : vmSymbols::java_lang_InstantiationException(), external_name()); 1031 } 1032 if (this == SystemDictionary::Class_klass()) { 1033 ResourceMark rm(THREAD); 1034 THROW_MSG(throwError ? vmSymbols::java_lang_IllegalAccessError() 1035 : vmSymbols::java_lang_IllegalAccessException(), external_name()); 1036 } 1037 } 1038 1039 Klass* InstanceKlass::array_klass_impl(bool or_null, int n, TRAPS) { 1040 return array_klass_impl(this, or_null, n, THREAD); 1041 } 1042 1043 Klass* InstanceKlass::array_klass_impl(InstanceKlass* this_k, bool or_null, int n, TRAPS) { 1044 // Need load-acquire for lock-free read 1045 if (this_k->array_klasses_acquire() == NULL) { 1046 if (or_null) return NULL; 1047 1048 ResourceMark rm; 1049 JavaThread *jt = (JavaThread *)THREAD; 1050 { 1051 // Atomic creation of array_klasses 1052 MutexLocker mc(Compile_lock, THREAD); // for vtables 1053 MutexLocker ma(MultiArray_lock, THREAD); 1054 1055 // Check if update has already taken place 1056 if (this_k->array_klasses() == NULL) { 1057 Klass* k = ObjArrayKlass::allocate_objArray_klass(this_k->class_loader_data(), 1, this_k, CHECK_NULL); 1058 // use 'release' to pair with lock-free load 1059 this_k->release_set_array_klasses(k); 1060 } 1061 } 1062 } 1063 // _this will always be set at this point 1064 ObjArrayKlass* oak = (ObjArrayKlass*)this_k->array_klasses(); 1065 if (or_null) { 1066 return oak->array_klass_or_null(n); 1067 } 1068 return oak->array_klass(n, THREAD); 1069 } 1070 1071 Klass* InstanceKlass::array_klass_impl(bool or_null, TRAPS) { 1072 return array_klass_impl(or_null, 1, THREAD); 1073 } 1074 1075 void InstanceKlass::call_class_initializer(TRAPS) { 1076 call_class_initializer_impl(this, THREAD); 1077 } 1078 1079 static int call_class_initializer_impl_counter = 0; // for debugging 1080 1081 Method* InstanceKlass::class_initializer() const { 1082 Method* clinit = find_method( 1083 vmSymbols::class_initializer_name(), vmSymbols::void_method_signature()); 1084 if (clinit != NULL && clinit->has_valid_initializer_flags()) { 1085 return clinit; 1086 } 1087 return NULL; 1088 } 1089 1090 void InstanceKlass::call_class_initializer_impl(InstanceKlass* this_k, TRAPS) { 1091 if (ReplayCompiles && 1092 (ReplaySuppressInitializers == 1 || 1093 ReplaySuppressInitializers >= 2 && this_k->class_loader() != NULL)) { 1094 // Hide the existence of the initializer for the purpose of replaying the compile 1095 return; 1096 } 1097 1098 methodHandle h_method(THREAD, this_k->class_initializer()); 1099 assert(!this_k->is_initialized(), "we cannot initialize twice"); 1100 if (log_is_enabled(Info, class, init)) { 1101 ResourceMark rm; 1102 outputStream* log = Log(class, init)::info_stream(); 1103 log->print("%d Initializing ", call_class_initializer_impl_counter++); 1104 this_k->name()->print_value_on(log); 1105 log->print_cr("%s (" INTPTR_FORMAT ")", h_method() == NULL ? "(no method)" : "", p2i(this_k)); 1106 } 1107 if (h_method() != NULL) { 1108 JavaCallArguments args; // No arguments 1109 JavaValue result(T_VOID); 1110 JavaCalls::call(&result, h_method, &args, CHECK); // Static call (no args) 1111 } 1112 } 1113 1114 1115 void InstanceKlass::mask_for(const methodHandle& method, int bci, 1116 InterpreterOopMap* entry_for) { 1117 // Lazily create the _oop_map_cache at first request 1118 // Lock-free access requires load_ptr_acquire. 1119 OopMapCache* oop_map_cache = 1120 static_cast<OopMapCache*>(OrderAccess::load_ptr_acquire(&_oop_map_cache)); 1121 if (oop_map_cache == NULL) { 1122 MutexLocker x(OopMapCacheAlloc_lock); 1123 // Check if _oop_map_cache was allocated while we were waiting for this lock 1124 if ((oop_map_cache = _oop_map_cache) == NULL) { 1125 oop_map_cache = new OopMapCache(); 1236 int len = methods()->length(); 1237 for (int index = 0; index < len; index++) { 1238 Method* m = methods()->at(index); 1239 assert(m->is_method(), "must be method"); 1240 f(m); 1241 } 1242 } 1243 1244 1245 void InstanceKlass::do_local_static_fields(FieldClosure* cl) { 1246 for (JavaFieldStream fs(this); !fs.done(); fs.next()) { 1247 if (fs.access_flags().is_static()) { 1248 fieldDescriptor& fd = fs.field_descriptor(); 1249 cl->do_field(&fd); 1250 } 1251 } 1252 } 1253 1254 1255 void InstanceKlass::do_local_static_fields(void f(fieldDescriptor*, Handle, TRAPS), Handle mirror, TRAPS) { 1256 do_local_static_fields_impl(this, f, mirror, CHECK); 1257 } 1258 1259 1260 void InstanceKlass::do_local_static_fields_impl(InstanceKlass* this_k, 1261 void f(fieldDescriptor* fd, Handle, TRAPS), Handle mirror, TRAPS) { 1262 for (JavaFieldStream fs(this_k); !fs.done(); fs.next()) { 1263 if (fs.access_flags().is_static()) { 1264 fieldDescriptor& fd = fs.field_descriptor(); 1265 f(&fd, mirror, CHECK); 1266 } 1267 } 1268 } 1269 1270 1271 static int compare_fields_by_offset(int* a, int* b) { 1272 return a[0] - b[0]; 1273 } 1274 1275 void InstanceKlass::do_nonstatic_fields(FieldClosure* cl) { 1276 InstanceKlass* super = superklass(); 1277 if (super != NULL) { 1278 super->do_nonstatic_fields(cl); 1279 } 1280 fieldDescriptor fd; 1281 int length = java_fields_count(); 1282 // In DebugInfo nonstatic fields are sorted by offset. 1601 // Do NOT return private or static methods, new in JDK8 which are not externally visible 1602 // They should only be found in the initial InterfaceMethodRef 1603 Method* InstanceKlass::lookup_method_in_all_interfaces(Symbol* name, 1604 Symbol* signature, 1605 DefaultsLookupMode defaults_mode) const { 1606 Array<Klass*>* all_ifs = transitive_interfaces(); 1607 int num_ifs = all_ifs->length(); 1608 InstanceKlass *ik = NULL; 1609 for (int i = 0; i < num_ifs; i++) { 1610 ik = InstanceKlass::cast(all_ifs->at(i)); 1611 Method* m = ik->lookup_method(name, signature); 1612 if (m != NULL && m->is_public() && !m->is_static() && 1613 ((defaults_mode != skip_defaults) || !m->is_default_method())) { 1614 return m; 1615 } 1616 } 1617 return NULL; 1618 } 1619 1620 /* jni_id_for_impl for jfieldIds only */ 1621 JNIid* InstanceKlass::jni_id_for_impl(InstanceKlass* this_k, int offset) { 1622 MutexLocker ml(JfieldIdCreation_lock); 1623 // Retry lookup after we got the lock 1624 JNIid* probe = this_k->jni_ids() == NULL ? NULL : this_k->jni_ids()->find(offset); 1625 if (probe == NULL) { 1626 // Slow case, allocate new static field identifier 1627 probe = new JNIid(this_k, offset, this_k->jni_ids()); 1628 this_k->set_jni_ids(probe); 1629 } 1630 return probe; 1631 } 1632 1633 1634 /* jni_id_for for jfieldIds only */ 1635 JNIid* InstanceKlass::jni_id_for(int offset) { 1636 JNIid* probe = jni_ids() == NULL ? NULL : jni_ids()->find(offset); 1637 if (probe == NULL) { 1638 probe = jni_id_for_impl(this, offset); 1639 } 1640 return probe; 1641 } 1642 1643 u2 InstanceKlass::enclosing_method_data(int offset) const { 1644 const Array<jushort>* const inner_class_list = inner_classes(); 1645 if (inner_class_list == NULL) { 1646 return 0; 1647 } 1648 const int length = inner_class_list->length(); 1649 if (length % inner_class_next_offset == 0) { 1650 return 0; 1651 } 1652 const int index = length - enclosing_method_attribute_size; 1653 assert(offset < enclosing_method_attribute_size, "invalid offset"); 1654 return inner_class_list->at(index + offset); 1655 } 1656 1657 void InstanceKlass::set_enclosing_method_indices(u2 class_index, 1658 u2 method_index) { | 71 #include "services/threadService.hpp" 72 #include "utilities/dtrace.hpp" 73 #include "utilities/macros.hpp" 74 #include "utilities/stringUtils.hpp" 75 #include "logging/log.hpp" 76 #ifdef COMPILER1 77 #include "c1/c1_Compiler.hpp" 78 #endif 79 80 #ifdef DTRACE_ENABLED 81 82 83 #define HOTSPOT_CLASS_INITIALIZATION_required HOTSPOT_CLASS_INITIALIZATION_REQUIRED 84 #define HOTSPOT_CLASS_INITIALIZATION_recursive HOTSPOT_CLASS_INITIALIZATION_RECURSIVE 85 #define HOTSPOT_CLASS_INITIALIZATION_concurrent HOTSPOT_CLASS_INITIALIZATION_CONCURRENT 86 #define HOTSPOT_CLASS_INITIALIZATION_erroneous HOTSPOT_CLASS_INITIALIZATION_ERRONEOUS 87 #define HOTSPOT_CLASS_INITIALIZATION_super__failed HOTSPOT_CLASS_INITIALIZATION_SUPER_FAILED 88 #define HOTSPOT_CLASS_INITIALIZATION_clinit HOTSPOT_CLASS_INITIALIZATION_CLINIT 89 #define HOTSPOT_CLASS_INITIALIZATION_error HOTSPOT_CLASS_INITIALIZATION_ERROR 90 #define HOTSPOT_CLASS_INITIALIZATION_end HOTSPOT_CLASS_INITIALIZATION_END 91 #define DTRACE_CLASSINIT_PROBE(type, thread_type) \ 92 { \ 93 char* data = NULL; \ 94 int len = 0; \ 95 Symbol* clss_name = name(); \ 96 if (clss_name != NULL) { \ 97 data = (char*)clss_name->bytes(); \ 98 len = clss_name->utf8_length(); \ 99 } \ 100 HOTSPOT_CLASS_INITIALIZATION_##type( \ 101 data, len, class_loader(), thread_type); \ 102 } 103 104 #define DTRACE_CLASSINIT_PROBE_WAIT(type, thread_type, wait) \ 105 { \ 106 char* data = NULL; \ 107 int len = 0; \ 108 Symbol* clss_name = name(); \ 109 if (clss_name != NULL) { \ 110 data = (char*)clss_name->bytes(); \ 111 len = clss_name->utf8_length(); \ 112 } \ 113 HOTSPOT_CLASS_INITIALIZATION_##type( \ 114 data, len, class_loader(), thread_type, wait); \ 115 } 116 117 #else // ndef DTRACE_ENABLED 118 119 #define DTRACE_CLASSINIT_PROBE(type, thread_type) 120 #define DTRACE_CLASSINIT_PROBE_WAIT(type, thread_type, wait) 121 122 #endif // ndef DTRACE_ENABLED 123 124 volatile int InstanceKlass::_total_instanceKlass_count = 0; 125 126 static inline bool is_class_loader(const Symbol* class_name, 127 const ClassFileParser& parser) { 128 assert(class_name != NULL, "invariant"); 129 130 if (class_name == vmSymbols::java_lang_ClassLoader()) { 131 return true; 132 } 133 134 if (SystemDictionary::ClassLoader_klass_loaded()) { 135 const Klass* const super_klass = parser.super_klass(); 136 if (super_klass != NULL) { 137 if (super_klass->is_subtype_of(SystemDictionary::ClassLoader_klass())) { 138 return true; 139 } 140 } 368 } 369 set_annotations(NULL); 370 } 371 372 bool InstanceKlass::should_be_initialized() const { 373 return !is_initialized(); 374 } 375 376 klassItable* InstanceKlass::itable() const { 377 return new klassItable(const_cast<InstanceKlass*>(this)); 378 } 379 380 void InstanceKlass::eager_initialize(Thread *thread) { 381 if (!EagerInitialization) return; 382 383 if (this->is_not_initialized()) { 384 // abort if the the class has a class initializer 385 if (this->class_initializer() != NULL) return; 386 387 // abort if it is java.lang.Object (initialization is handled in genesis) 388 Klass* super_klass = super(); 389 if (super_klass == NULL) return; 390 391 // abort if the super class should be initialized 392 if (!InstanceKlass::cast(super_klass)->is_initialized()) return; 393 394 // call body to expose the this pointer 395 eager_initialize_impl(); 396 } 397 } 398 399 // JVMTI spec thinks there are signers and protection domain in the 400 // instanceKlass. These accessors pretend these fields are there. 401 // The hprof specification also thinks these fields are in InstanceKlass. 402 oop InstanceKlass::protection_domain() const { 403 // return the protection_domain from the mirror 404 return java_lang_Class::protection_domain(java_mirror()); 405 } 406 407 // To remove these from requires an incompatible change and CCC request. 408 objArrayOop InstanceKlass::signers() const { 409 // return the signers from the mirror 410 return java_lang_Class::signers(java_mirror()); 411 } 412 413 oop InstanceKlass::init_lock() const { 414 // return the init lock from the mirror 415 oop lock = java_lang_Class::init_lock(java_mirror()); 416 // Prevent reordering with any access of initialization state 417 OrderAccess::loadload(); 418 assert((oop)lock != NULL || !is_not_initialized(), // initialized or in_error state 419 "only fully initialized state can have a null lock"); 420 return lock; 421 } 422 423 // Set the initialization lock to null so the object can be GC'ed. Any racing 424 // threads to get this lock will see a null lock and will not lock. 425 // That's okay because they all check for initialized state after getting 426 // the lock and return. 427 void InstanceKlass::fence_and_clear_init_lock() { 428 // make sure previous stores are all done, notably the init_state. 429 OrderAccess::storestore(); 430 java_lang_Class::set_init_lock(java_mirror(), NULL); 431 assert(!is_not_initialized(), "class must be initialized now"); 432 } 433 434 void InstanceKlass::eager_initialize_impl() { 435 EXCEPTION_MARK; 436 HandleMark hm(THREAD); 437 Handle h_init_lock(THREAD, init_lock()); 438 ObjectLocker ol(h_init_lock, THREAD, init_lock() != NULL); 439 440 // abort if someone beat us to the initialization 441 if (!is_not_initialized()) return; // note: not equivalent to is_initialized() 442 443 ClassState old_state = init_state(); 444 link_class_impl(true, THREAD); 445 if (HAS_PENDING_EXCEPTION) { 446 CLEAR_PENDING_EXCEPTION; 447 // Abort if linking the class throws an exception. 448 449 // Use a test to avoid redundantly resetting the state if there's 450 // no change. Set_init_state() asserts that state changes make 451 // progress, whereas here we might just be spinning in place. 452 if (old_state != _init_state) 453 set_init_state(old_state); 454 } else { 455 // linking successfull, mark class as initialized 456 set_init_state(fully_initialized); 457 fence_and_clear_init_lock(); 458 // trace 459 if (log_is_enabled(Info, class, init)) { 460 ResourceMark rm(THREAD); 461 log_info(class, init)("[Initialized %s without side effects]", external_name()); 462 } 463 } 464 } 465 466 467 // See "The Virtual Machine Specification" section 2.16.5 for a detailed explanation of the class initialization 468 // process. The step comments refers to the procedure described in that section. 469 // Note: implementation moved to static method to expose the this pointer. 470 void InstanceKlass::initialize(TRAPS) { 471 if (this->should_be_initialized()) { 472 initialize_impl(CHECK); 473 // Note: at this point the class may be initialized 474 // OR it may be in the state of being initialized 475 // in case of recursive initialization! 476 } else { 477 assert(is_initialized(), "sanity check"); 478 } 479 } 480 481 482 bool InstanceKlass::verify_code(bool throw_verifyerror, TRAPS) { 483 // 1) Verify the bytecodes 484 Verifier::Mode mode = 485 throw_verifyerror ? Verifier::ThrowException : Verifier::NoException; 486 return Verifier::verify(this, mode, should_verify_class(), THREAD); 487 } 488 489 490 // Used exclusively by the shared spaces dump mechanism to prevent 491 // classes mapped into the shared regions in new VMs from appearing linked. 492 493 void InstanceKlass::unlink_class() { 494 assert(is_linked(), "must be linked"); 495 _init_state = loaded; 496 } 497 498 void InstanceKlass::link_class(TRAPS) { 499 assert(is_loaded(), "must be loaded"); 500 if (!is_linked()) { 501 link_class_impl(true, CHECK); 502 } 503 } 504 505 // Called to verify that a class can link during initialization, without 506 // throwing a VerifyError. 507 bool InstanceKlass::link_class_or_fail(TRAPS) { 508 assert(is_loaded(), "must be loaded"); 509 if (!is_linked()) { 510 link_class_impl(false, CHECK_false); 511 } 512 return is_linked(); 513 } 514 515 bool InstanceKlass::link_class_impl(bool throw_verifyerror, TRAPS) { 516 if (DumpSharedSpaces && is_in_error_state()) { 517 // This is for CDS dumping phase only -- we use the in_error_state to indicate that 518 // the class has failed verification. Throwing the NoClassDefFoundError here is just 519 // a convenient way to stop repeat attempts to verify the same (bad) class. 520 // 521 // Note that the NoClassDefFoundError is not part of the JLS, and should not be thrown 522 // if we are executing Java code. This is not a problem for CDS dumping phase since 523 // it doesn't execute any Java code. 524 ResourceMark rm(THREAD); 525 THROW_MSG_(vmSymbols::java_lang_NoClassDefFoundError(), external_name(), false); 526 } 527 // return if already verified 528 if (is_linked()) { 529 return true; 530 } 531 532 // Timing 533 // timer handles recursion 534 assert(THREAD->is_Java_thread(), "non-JavaThread in link_class_impl"); 535 JavaThread* jt = (JavaThread*)THREAD; 536 537 // link super class before linking this class 538 Klass* super_klass = super(); 539 if (super_klass != NULL) { 540 if (super_klass->is_interface()) { // check if super class is an interface 541 ResourceMark rm(THREAD); 542 Exceptions::fthrow( 543 THREAD_AND_LOCATION, 544 vmSymbols::java_lang_IncompatibleClassChangeError(), 545 "class %s has interface %s as super class", 546 external_name(), 547 super_klass->external_name() 548 ); 549 return false; 550 } 551 552 InstanceKlass* ik_super = InstanceKlass::cast(super_klass); 553 ik_super->link_class_impl(throw_verifyerror, CHECK_false); 554 } 555 556 // link all interfaces implemented by this class before linking this class 557 Array<Klass*>* interfaces = local_interfaces(); 558 int num_interfaces = interfaces->length(); 559 for (int index = 0; index < num_interfaces; index++) { 560 InstanceKlass* interk = InstanceKlass::cast(interfaces->at(index)); 561 interk->link_class_impl(throw_verifyerror, CHECK_false); 562 } 563 564 // in case the class is linked in the process of linking its superclasses 565 if (is_linked()) { 566 return true; 567 } 568 569 // trace only the link time for this klass that includes 570 // the verification time 571 PerfClassTraceTime vmtimer(ClassLoader::perf_class_link_time(), 572 ClassLoader::perf_class_link_selftime(), 573 ClassLoader::perf_classes_linked(), 574 jt->get_thread_stat()->perf_recursion_counts_addr(), 575 jt->get_thread_stat()->perf_timers_addr(), 576 PerfClassTraceTime::CLASS_LINK); 577 578 // verification & rewriting 579 { 580 HandleMark hm(THREAD); 581 Handle h_init_lock(THREAD, init_lock()); 582 ObjectLocker ol(h_init_lock, THREAD, init_lock() != NULL); 583 // rewritten will have been set if loader constraint error found 584 // on an earlier link attempt 585 // don't verify or rewrite if already rewritten 586 // 587 588 if (!is_linked()) { 589 if (!is_rewritten()) { 590 { 591 bool verify_ok = verify_code(throw_verifyerror, THREAD); 592 if (!verify_ok) { 593 return false; 594 } 595 } 596 597 // Just in case a side-effect of verify linked this class already 598 // (which can sometimes happen since the verifier loads classes 599 // using custom class loaders, which are free to initialize things) 600 if (is_linked()) { 601 return true; 602 } 603 604 // also sets rewritten 605 rewrite_class(CHECK_false); 606 } else if (is_shared()) { 607 SystemDictionaryShared::check_verification_constraints(this, CHECK_false); 608 } 609 610 // relocate jsrs and link methods after they are all rewritten 611 link_methods(CHECK_false); 612 613 // Initialize the vtable and interface table after 614 // methods have been rewritten since rewrite may 615 // fabricate new Method*s. 616 // also does loader constraint checking 617 // 618 // initialize_vtable and initialize_itable need to be rerun for 619 // a shared class if the class is not loaded by the NULL classloader. 620 ClassLoaderData * loader_data = class_loader_data(); 621 if (!(is_shared() && 622 loader_data->is_the_null_class_loader_data())) { 623 ResourceMark rm(THREAD); 624 vtable()->initialize_vtable(true, CHECK_false); 625 itable()->initialize_itable(true, CHECK_false); 626 } 627 #ifdef ASSERT 628 else { 629 ResourceMark rm(THREAD); 630 vtable()->verify(tty, true); 631 // In case itable verification is ever added. 632 // itable()->verify(tty, true); 633 } 634 #endif 635 set_init_state(linked); 636 if (JvmtiExport::should_post_class_prepare()) { 637 Thread *thread = THREAD; 638 assert(thread->is_Java_thread(), "thread->is_Java_thread()"); 639 JvmtiExport::post_class_prepare((JavaThread *) thread, this); 640 } 641 } 642 } 643 return true; 644 } 645 646 647 // Rewrite the byte codes of all of the methods of a class. 648 // The rewriter must be called exactly once. Rewriting must happen after 649 // verification but before the first method of the class is executed. 650 void InstanceKlass::rewrite_class(TRAPS) { 651 assert(is_loaded(), "must be loaded"); 652 if (is_rewritten()) { 653 assert(is_shared(), "rewriting an unshared class?"); 654 return; 655 } 656 Rewriter::rewrite(this, CHECK); 657 set_rewritten(); 658 } 659 660 // Now relocate and link method entry points after class is rewritten. 661 // This is outside is_rewritten flag. In case of an exception, it can be 662 // executed more than once. 663 void InstanceKlass::link_methods(TRAPS) { 664 int len = methods()->length(); 665 for (int i = len-1; i >= 0; i--) { 666 methodHandle m(THREAD, methods()->at(i)); 667 668 // Set up method entry points for compiler and interpreter . 669 m->link_method(m, CHECK); 670 } 671 } 672 673 // Eagerly initialize superinterfaces that declare default methods (concrete instance: any access) 674 void InstanceKlass::initialize_super_interfaces(TRAPS) { 675 assert (has_nonstatic_concrete_methods(), "caller should have checked this"); 676 for (int i = 0; i < local_interfaces()->length(); ++i) { 677 Klass* iface = local_interfaces()->at(i); 678 InstanceKlass* ik = InstanceKlass::cast(iface); 679 680 // Initialization is depth first search ie. we start with top of the inheritance tree 681 // has_nonstatic_concrete_methods drives searching superinterfaces since it 682 // means has_nonstatic_concrete_methods in its superinterface hierarchy 683 if (ik->has_nonstatic_concrete_methods()) { 684 ik->initialize_super_interfaces(CHECK); 685 } 686 687 // Only initialize() interfaces that "declare" concrete methods. 688 if (ik->should_be_initialized() && ik->declares_nonstatic_concrete_methods()) { 689 ik->initialize(CHECK); 690 } 691 } 692 } 693 694 void InstanceKlass::initialize_impl(TRAPS) { 695 HandleMark hm(THREAD); 696 697 // Make sure klass is linked (verified) before initialization 698 // A class could already be verified, since it has been reflected upon. 699 link_class(CHECK); 700 701 DTRACE_CLASSINIT_PROBE(required, -1); 702 703 bool wait = false; 704 705 // refer to the JVM book page 47 for description of steps 706 // Step 1 707 { 708 Handle h_init_lock(THREAD, init_lock()); 709 ObjectLocker ol(h_init_lock, THREAD, init_lock() != NULL); 710 711 Thread *self = THREAD; // it's passed the current thread 712 713 // Step 2 714 // If we were to use wait() instead of waitInterruptibly() then 715 // we might end up throwing IE from link/symbol resolution sites 716 // that aren't expected to throw. This would wreak havoc. See 6320309. 717 while(is_being_initialized() && !is_reentrant_initialization(self)) { 718 wait = true; 719 ol.waitUninterruptibly(CHECK); 720 } 721 722 // Step 3 723 if (is_being_initialized() && is_reentrant_initialization(self)) { 724 DTRACE_CLASSINIT_PROBE_WAIT(recursive, -1, wait); 725 return; 726 } 727 728 // Step 4 729 if (is_initialized()) { 730 DTRACE_CLASSINIT_PROBE_WAIT(concurrent, -1, wait); 731 return; 732 } 733 734 // Step 5 735 if (is_in_error_state()) { 736 DTRACE_CLASSINIT_PROBE_WAIT(erroneous, -1, wait); 737 ResourceMark rm(THREAD); 738 const char* desc = "Could not initialize class "; 739 const char* className = external_name(); 740 size_t msglen = strlen(desc) + strlen(className) + 1; 741 char* message = NEW_RESOURCE_ARRAY(char, msglen); 742 if (NULL == message) { 743 // Out of memory: can't create detailed error message 744 THROW_MSG(vmSymbols::java_lang_NoClassDefFoundError(), className); 745 } else { 746 jio_snprintf(message, msglen, "%s%s", desc, className); 747 THROW_MSG(vmSymbols::java_lang_NoClassDefFoundError(), message); 748 } 749 } 750 751 // Step 6 752 set_init_state(being_initialized); 753 set_init_thread(self); 754 } 755 756 // Step 7 757 // Next, if C is a class rather than an interface, initialize it's super class and super 758 // interfaces. 759 if (!is_interface()) { 760 Klass* super_klass = super(); 761 if (super_klass != NULL && super_klass->should_be_initialized()) { 762 super_klass->initialize(THREAD); 763 } 764 // If C implements any interface that declares a non-static, concrete method, 765 // the initialization of C triggers initialization of its super interfaces. 766 // Only need to recurse if has_nonstatic_concrete_methods which includes declaring and 767 // having a superinterface that declares, non-static, concrete methods 768 if (!HAS_PENDING_EXCEPTION && has_nonstatic_concrete_methods()) { 769 initialize_super_interfaces(THREAD); 770 } 771 772 // If any exceptions, complete abruptly, throwing the same exception as above. 773 if (HAS_PENDING_EXCEPTION) { 774 Handle e(THREAD, PENDING_EXCEPTION); 775 CLEAR_PENDING_EXCEPTION; 776 { 777 EXCEPTION_MARK; 778 // Locks object, set state, and notify all waiting threads 779 set_initialization_state_and_notify(initialization_error, THREAD); 780 CLEAR_PENDING_EXCEPTION; 781 } 782 DTRACE_CLASSINIT_PROBE_WAIT(super__failed, -1, wait); 783 THROW_OOP(e()); 784 } 785 } 786 787 788 // Look for aot compiled methods for this klass, including class initializer. 789 AOTLoader::load_for_klass(this, THREAD); 790 791 // Step 8 792 { 793 assert(THREAD->is_Java_thread(), "non-JavaThread in initialize_impl"); 794 JavaThread* jt = (JavaThread*)THREAD; 795 DTRACE_CLASSINIT_PROBE_WAIT(clinit, -1, wait); 796 // Timer includes any side effects of class initialization (resolution, 797 // etc), but not recursive entry into call_class_initializer(). 798 PerfClassTraceTime timer(ClassLoader::perf_class_init_time(), 799 ClassLoader::perf_class_init_selftime(), 800 ClassLoader::perf_classes_inited(), 801 jt->get_thread_stat()->perf_recursion_counts_addr(), 802 jt->get_thread_stat()->perf_timers_addr(), 803 PerfClassTraceTime::CLASS_CLINIT); 804 call_class_initializer(THREAD); 805 } 806 807 // Step 9 808 if (!HAS_PENDING_EXCEPTION) { 809 set_initialization_state_and_notify(fully_initialized, CHECK); 810 { ResourceMark rm(THREAD); 811 debug_only(vtable()->verify(tty, true);) 812 } 813 } 814 else { 815 // Step 10 and 11 816 Handle e(THREAD, PENDING_EXCEPTION); 817 CLEAR_PENDING_EXCEPTION; 818 // JVMTI has already reported the pending exception 819 // JVMTI internal flag reset is needed in order to report ExceptionInInitializerError 820 JvmtiExport::clear_detected_exception((JavaThread*)THREAD); 821 { 822 EXCEPTION_MARK; 823 set_initialization_state_and_notify(initialization_error, THREAD); 824 CLEAR_PENDING_EXCEPTION; // ignore any exception thrown, class initialization error is thrown below 825 // JVMTI has already reported the pending exception 826 // JVMTI internal flag reset is needed in order to report ExceptionInInitializerError 827 JvmtiExport::clear_detected_exception((JavaThread*)THREAD); 828 } 829 DTRACE_CLASSINIT_PROBE_WAIT(error, -1, wait); 830 if (e->is_a(SystemDictionary::Error_klass())) { 831 THROW_OOP(e()); 832 } else { 833 JavaCallArguments args(e); 834 THROW_ARG(vmSymbols::java_lang_ExceptionInInitializerError(), 835 vmSymbols::throwable_void_signature(), 836 &args); 837 } 838 } 839 DTRACE_CLASSINIT_PROBE_WAIT(end, -1, wait); 840 } 841 842 843 // Note: implementation moved to static method to expose the this pointer. 844 void InstanceKlass::set_initialization_state_and_notify(ClassState state, TRAPS) { 845 set_initialization_state_and_notify_impl(state, CHECK); 846 } 847 848 void InstanceKlass::set_initialization_state_and_notify_impl(ClassState state, TRAPS) { 849 Handle h_init_lock(THREAD, init_lock()); 850 if (init_lock() != NULL) { 851 ObjectLocker ol(h_init_lock, THREAD); 852 set_init_state(state); 853 fence_and_clear_init_lock(); 854 ol.notify_all(CHECK); 855 } else { 856 assert(init_lock() != NULL, "The initialization state should never be set twice"); 857 set_init_state(state); 858 } 859 } 860 861 // The embedded _implementor field can only record one implementor. 862 // When there are more than one implementors, the _implementor field 863 // is set to the interface Klass* itself. Following are the possible 864 // values for the _implementor field: 865 // NULL - no implementor 866 // implementor Klass* - one implementor 867 // self - more than one implementor 868 // 869 // The _implementor field only exists for interfaces. 870 void InstanceKlass::add_implementor(Klass* k) { 871 assert(Compile_lock->owned_by_self(), ""); 872 assert(is_interface(), "not interface"); 873 // Filter out my subinterfaces. 874 // (Note: Interfaces are never on the subklass list.) 875 if (InstanceKlass::cast(k)->is_interface()) return; 876 877 // Filter out subclasses whose supers already implement me. 1017 if (has_finalizer_flag && !RegisterFinalizersAtInit) { 1018 i = register_finalizer(i, CHECK_NULL); 1019 } 1020 return i; 1021 } 1022 1023 void InstanceKlass::check_valid_for_instantiation(bool throwError, TRAPS) { 1024 if (is_interface() || is_abstract()) { 1025 ResourceMark rm(THREAD); 1026 THROW_MSG(throwError ? vmSymbols::java_lang_InstantiationError() 1027 : vmSymbols::java_lang_InstantiationException(), external_name()); 1028 } 1029 if (this == SystemDictionary::Class_klass()) { 1030 ResourceMark rm(THREAD); 1031 THROW_MSG(throwError ? vmSymbols::java_lang_IllegalAccessError() 1032 : vmSymbols::java_lang_IllegalAccessException(), external_name()); 1033 } 1034 } 1035 1036 Klass* InstanceKlass::array_klass_impl(bool or_null, int n, TRAPS) { 1037 // Need load-acquire for lock-free read 1038 if (array_klasses_acquire() == NULL) { 1039 if (or_null) return NULL; 1040 1041 ResourceMark rm; 1042 JavaThread *jt = (JavaThread *)THREAD; 1043 { 1044 // Atomic creation of array_klasses 1045 MutexLocker mc(Compile_lock, THREAD); // for vtables 1046 MutexLocker ma(MultiArray_lock, THREAD); 1047 1048 // Check if update has already taken place 1049 if (array_klasses() == NULL) { 1050 Klass* k = ObjArrayKlass::allocate_objArray_klass(class_loader_data(), 1, this, CHECK_NULL); 1051 // use 'release' to pair with lock-free load 1052 release_set_array_klasses(k); 1053 } 1054 } 1055 } 1056 // _this will always be set at this point 1057 ObjArrayKlass* oak = (ObjArrayKlass*)array_klasses(); 1058 if (or_null) { 1059 return oak->array_klass_or_null(n); 1060 } 1061 return oak->array_klass(n, THREAD); 1062 } 1063 1064 Klass* InstanceKlass::array_klass_impl(bool or_null, TRAPS) { 1065 return array_klass_impl(or_null, 1, THREAD); 1066 } 1067 1068 void InstanceKlass::call_class_initializer(TRAPS) { 1069 call_class_initializer_impl(THREAD); 1070 } 1071 1072 static int call_class_initializer_impl_counter = 0; // for debugging 1073 1074 Method* InstanceKlass::class_initializer() const { 1075 Method* clinit = find_method( 1076 vmSymbols::class_initializer_name(), vmSymbols::void_method_signature()); 1077 if (clinit != NULL && clinit->has_valid_initializer_flags()) { 1078 return clinit; 1079 } 1080 return NULL; 1081 } 1082 1083 void InstanceKlass::call_class_initializer_impl(TRAPS) { 1084 if (ReplayCompiles && 1085 (ReplaySuppressInitializers == 1 || 1086 ReplaySuppressInitializers >= 2 && class_loader() != NULL)) { 1087 // Hide the existence of the initializer for the purpose of replaying the compile 1088 return; 1089 } 1090 1091 methodHandle h_method(THREAD, class_initializer()); 1092 assert(!is_initialized(), "we cannot initialize twice"); 1093 if (log_is_enabled(Info, class, init)) { 1094 ResourceMark rm; 1095 outputStream* log = Log(class, init)::info_stream(); 1096 log->print("%d Initializing ", call_class_initializer_impl_counter++); 1097 name()->print_value_on(log); 1098 log->print_cr("%s (" INTPTR_FORMAT ")", h_method() == NULL ? "(no method)" : "", p2i(this)); 1099 } 1100 if (h_method() != NULL) { 1101 JavaCallArguments args; // No arguments 1102 JavaValue result(T_VOID); 1103 JavaCalls::call(&result, h_method, &args, CHECK); // Static call (no args) 1104 } 1105 } 1106 1107 1108 void InstanceKlass::mask_for(const methodHandle& method, int bci, 1109 InterpreterOopMap* entry_for) { 1110 // Lazily create the _oop_map_cache at first request 1111 // Lock-free access requires load_ptr_acquire. 1112 OopMapCache* oop_map_cache = 1113 static_cast<OopMapCache*>(OrderAccess::load_ptr_acquire(&_oop_map_cache)); 1114 if (oop_map_cache == NULL) { 1115 MutexLocker x(OopMapCacheAlloc_lock); 1116 // Check if _oop_map_cache was allocated while we were waiting for this lock 1117 if ((oop_map_cache = _oop_map_cache) == NULL) { 1118 oop_map_cache = new OopMapCache(); 1229 int len = methods()->length(); 1230 for (int index = 0; index < len; index++) { 1231 Method* m = methods()->at(index); 1232 assert(m->is_method(), "must be method"); 1233 f(m); 1234 } 1235 } 1236 1237 1238 void InstanceKlass::do_local_static_fields(FieldClosure* cl) { 1239 for (JavaFieldStream fs(this); !fs.done(); fs.next()) { 1240 if (fs.access_flags().is_static()) { 1241 fieldDescriptor& fd = fs.field_descriptor(); 1242 cl->do_field(&fd); 1243 } 1244 } 1245 } 1246 1247 1248 void InstanceKlass::do_local_static_fields(void f(fieldDescriptor*, Handle, TRAPS), Handle mirror, TRAPS) { 1249 do_local_static_fields_impl(f, mirror, CHECK); 1250 } 1251 1252 1253 void InstanceKlass::do_local_static_fields_impl(void f(fieldDescriptor* fd, Handle, TRAPS), 1254 Handle mirror, TRAPS) { 1255 for (JavaFieldStream fs(this); !fs.done(); fs.next()) { 1256 if (fs.access_flags().is_static()) { 1257 fieldDescriptor& fd = fs.field_descriptor(); 1258 f(&fd, mirror, CHECK); 1259 } 1260 } 1261 } 1262 1263 1264 static int compare_fields_by_offset(int* a, int* b) { 1265 return a[0] - b[0]; 1266 } 1267 1268 void InstanceKlass::do_nonstatic_fields(FieldClosure* cl) { 1269 InstanceKlass* super = superklass(); 1270 if (super != NULL) { 1271 super->do_nonstatic_fields(cl); 1272 } 1273 fieldDescriptor fd; 1274 int length = java_fields_count(); 1275 // In DebugInfo nonstatic fields are sorted by offset. 1594 // Do NOT return private or static methods, new in JDK8 which are not externally visible 1595 // They should only be found in the initial InterfaceMethodRef 1596 Method* InstanceKlass::lookup_method_in_all_interfaces(Symbol* name, 1597 Symbol* signature, 1598 DefaultsLookupMode defaults_mode) const { 1599 Array<Klass*>* all_ifs = transitive_interfaces(); 1600 int num_ifs = all_ifs->length(); 1601 InstanceKlass *ik = NULL; 1602 for (int i = 0; i < num_ifs; i++) { 1603 ik = InstanceKlass::cast(all_ifs->at(i)); 1604 Method* m = ik->lookup_method(name, signature); 1605 if (m != NULL && m->is_public() && !m->is_static() && 1606 ((defaults_mode != skip_defaults) || !m->is_default_method())) { 1607 return m; 1608 } 1609 } 1610 return NULL; 1611 } 1612 1613 /* jni_id_for_impl for jfieldIds only */ 1614 JNIid* InstanceKlass::jni_id_for_impl(int offset) { 1615 MutexLocker ml(JfieldIdCreation_lock); 1616 // Retry lookup after we got the lock 1617 JNIid* probe = jni_ids() == NULL ? NULL : jni_ids()->find(offset); 1618 if (probe == NULL) { 1619 // Slow case, allocate new static field identifier 1620 probe = new JNIid(this, offset, jni_ids()); 1621 set_jni_ids(probe); 1622 } 1623 return probe; 1624 } 1625 1626 1627 /* jni_id_for for jfieldIds only */ 1628 JNIid* InstanceKlass::jni_id_for(int offset) { 1629 JNIid* probe = jni_ids() == NULL ? NULL : jni_ids()->find(offset); 1630 if (probe == NULL) { 1631 probe = jni_id_for_impl(offset); 1632 } 1633 return probe; 1634 } 1635 1636 u2 InstanceKlass::enclosing_method_data(int offset) const { 1637 const Array<jushort>* const inner_class_list = inner_classes(); 1638 if (inner_class_list == NULL) { 1639 return 0; 1640 } 1641 const int length = inner_class_list->length(); 1642 if (length % inner_class_next_offset == 0) { 1643 return 0; 1644 } 1645 const int index = length - enclosing_method_attribute_size; 1646 assert(offset < enclosing_method_attribute_size, "invalid offset"); 1647 return inner_class_list->at(index + offset); 1648 } 1649 1650 void InstanceKlass::set_enclosing_method_indices(u2 class_index, 1651 u2 method_index) { |