626 // threads to get this lock will see a null lock and will not lock. 627 // That's okay because they all check for initialized state after getting 628 // the lock and return. 629 void InstanceKlass::fence_and_clear_init_lock() { 630 // make sure previous stores are all done, notably the init_state. 631 OrderAccess::storestore(); 632 java_lang_Class::set_init_lock(java_mirror(), NULL); 633 assert(!is_not_initialized(), "class must be initialized now"); 634 } 635 636 void InstanceKlass::eager_initialize_impl() { 637 EXCEPTION_MARK; 638 HandleMark hm(THREAD); 639 Handle h_init_lock(THREAD, init_lock()); 640 ObjectLocker ol(h_init_lock, THREAD, h_init_lock() != NULL); 641 642 // abort if someone beat us to the initialization 643 if (!is_not_initialized()) return; // note: not equivalent to is_initialized() 644 645 ClassState old_state = init_state(); 646 link_class_impl(true, THREAD); 647 if (HAS_PENDING_EXCEPTION) { 648 CLEAR_PENDING_EXCEPTION; 649 // Abort if linking the class throws an exception. 650 651 // Use a test to avoid redundantly resetting the state if there's 652 // no change. Set_init_state() asserts that state changes make 653 // progress, whereas here we might just be spinning in place. 654 if (old_state != _init_state) 655 set_init_state(old_state); 656 } else { 657 // linking successfull, mark class as initialized 658 set_init_state(fully_initialized); 659 fence_and_clear_init_lock(); 660 // trace 661 if (log_is_enabled(Info, class, init)) { 662 ResourceMark rm(THREAD); 663 log_info(class, init)("[Initialized %s without side effects]", external_name()); 664 } 665 } 666 } 667 668 669 // See "The Virtual Machine Specification" section 2.16.5 for a detailed explanation of the class initialization 670 // process. The step comments refers to the procedure described in that section. 671 // Note: implementation moved to static method to expose the this pointer. 672 void InstanceKlass::initialize(TRAPS) { 673 if (this->should_be_initialized()) { 674 initialize_impl(CHECK); 675 // Note: at this point the class may be initialized 676 // OR it may be in the state of being initialized 677 // in case of recursive initialization! 678 } else { 679 assert(is_initialized(), "sanity check"); 680 } 681 } 682 683 684 bool InstanceKlass::verify_code(bool throw_verifyerror, TRAPS) { 685 // 1) Verify the bytecodes 686 Verifier::Mode mode = 687 throw_verifyerror ? Verifier::ThrowException : Verifier::NoException; 688 return Verifier::verify(this, mode, should_verify_class(), THREAD); 689 } 690 691 692 // Used exclusively by the shared spaces dump mechanism to prevent 693 // classes mapped into the shared regions in new VMs from appearing linked. 694 695 void InstanceKlass::unlink_class() { 696 assert(is_linked(), "must be linked"); 697 _init_state = loaded; 698 } 699 700 void InstanceKlass::link_class(TRAPS) { 701 assert(is_loaded(), "must be loaded"); 702 if (!is_linked()) { 703 link_class_impl(true, CHECK); 704 } 705 } 706 707 // Called to verify that a class can link during initialization, without 708 // throwing a VerifyError. 709 bool InstanceKlass::link_class_or_fail(TRAPS) { 710 assert(is_loaded(), "must be loaded"); 711 if (!is_linked()) { 712 link_class_impl(false, CHECK_false); 713 } 714 return is_linked(); 715 } 716 717 bool InstanceKlass::link_class_impl(bool throw_verifyerror, TRAPS) { 718 if (DumpSharedSpaces && is_in_error_state()) { 719 // This is for CDS dumping phase only -- we use the in_error_state to indicate that 720 // the class has failed verification. Throwing the NoClassDefFoundError here is just 721 // a convenient way to stop repeat attempts to verify the same (bad) class. 722 // 723 // Note that the NoClassDefFoundError is not part of the JLS, and should not be thrown 724 // if we are executing Java code. This is not a problem for CDS dumping phase since 725 // it doesn't execute any Java code. 726 ResourceMark rm(THREAD); 727 Exceptions::fthrow(THREAD_AND_LOCATION, 728 vmSymbols::java_lang_NoClassDefFoundError(), 729 "Class %s, or one of its supertypes, failed class initialization", 730 external_name()); 731 return false; 732 } 733 // return if already verified 734 if (is_linked()) { 735 return true; 736 } 737 739 // timer handles recursion 740 assert(THREAD->is_Java_thread(), "non-JavaThread in link_class_impl"); 741 JavaThread* jt = (JavaThread*)THREAD; 742 743 // link super class before linking this class 744 Klass* super_klass = super(); 745 if (super_klass != NULL) { 746 if (super_klass->is_interface()) { // check if super class is an interface 747 ResourceMark rm(THREAD); 748 Exceptions::fthrow( 749 THREAD_AND_LOCATION, 750 vmSymbols::java_lang_IncompatibleClassChangeError(), 751 "class %s has interface %s as super class", 752 external_name(), 753 super_klass->external_name() 754 ); 755 return false; 756 } 757 758 InstanceKlass* ik_super = InstanceKlass::cast(super_klass); 759 ik_super->link_class_impl(throw_verifyerror, CHECK_false); 760 } 761 762 // link all interfaces implemented by this class before linking this class 763 Array<InstanceKlass*>* interfaces = local_interfaces(); 764 int num_interfaces = interfaces->length(); 765 for (int index = 0; index < num_interfaces; index++) { 766 InstanceKlass* interk = interfaces->at(index); 767 interk->link_class_impl(throw_verifyerror, CHECK_false); 768 } 769 770 // in case the class is linked in the process of linking its superclasses 771 if (is_linked()) { 772 return true; 773 } 774 775 // trace only the link time for this klass that includes 776 // the verification time 777 PerfClassTraceTime vmtimer(ClassLoader::perf_class_link_time(), 778 ClassLoader::perf_class_link_selftime(), 779 ClassLoader::perf_classes_linked(), 780 jt->get_thread_stat()->perf_recursion_counts_addr(), 781 jt->get_thread_stat()->perf_timers_addr(), 782 PerfClassTraceTime::CLASS_LINK); 783 784 // verification & rewriting 785 { 786 HandleMark hm(THREAD); 787 Handle h_init_lock(THREAD, init_lock()); 788 ObjectLocker ol(h_init_lock, THREAD, h_init_lock() != NULL); 789 // rewritten will have been set if loader constraint error found 790 // on an earlier link attempt 791 // don't verify or rewrite if already rewritten 792 // 793 794 if (!is_linked()) { 795 if (!is_rewritten()) { 796 { 797 bool verify_ok = verify_code(throw_verifyerror, THREAD); 798 if (!verify_ok) { 799 return false; 800 } 801 } 802 803 // Just in case a side-effect of verify linked this class already 804 // (which can sometimes happen since the verifier loads classes 805 // using custom class loaders, which are free to initialize things) 806 if (is_linked()) { 807 return true; 808 } 809 810 // also sets rewritten 811 rewrite_class(CHECK_false); 812 } else if (is_shared()) { 813 SystemDictionaryShared::check_verification_constraints(this, CHECK_false); 814 } 815 816 // relocate jsrs and link methods after they are all rewritten 817 link_methods(CHECK_false); | 626 // threads to get this lock will see a null lock and will not lock. 627 // That's okay because they all check for initialized state after getting 628 // the lock and return. 629 void InstanceKlass::fence_and_clear_init_lock() { 630 // make sure previous stores are all done, notably the init_state. 631 OrderAccess::storestore(); 632 java_lang_Class::set_init_lock(java_mirror(), NULL); 633 assert(!is_not_initialized(), "class must be initialized now"); 634 } 635 636 void InstanceKlass::eager_initialize_impl() { 637 EXCEPTION_MARK; 638 HandleMark hm(THREAD); 639 Handle h_init_lock(THREAD, init_lock()); 640 ObjectLocker ol(h_init_lock, THREAD, h_init_lock() != NULL); 641 642 // abort if someone beat us to the initialization 643 if (!is_not_initialized()) return; // note: not equivalent to is_initialized() 644 645 ClassState old_state = init_state(); 646 link_class_impl(THREAD); 647 if (HAS_PENDING_EXCEPTION) { 648 CLEAR_PENDING_EXCEPTION; 649 // Abort if linking the class throws an exception. 650 651 // Use a test to avoid redundantly resetting the state if there's 652 // no change. Set_init_state() asserts that state changes make 653 // progress, whereas here we might just be spinning in place. 654 if (old_state != _init_state) 655 set_init_state(old_state); 656 } else { 657 // linking successfull, mark class as initialized 658 set_init_state(fully_initialized); 659 fence_and_clear_init_lock(); 660 // trace 661 if (log_is_enabled(Info, class, init)) { 662 ResourceMark rm(THREAD); 663 log_info(class, init)("[Initialized %s without side effects]", external_name()); 664 } 665 } 666 } 667 668 669 // See "The Virtual Machine Specification" section 2.16.5 for a detailed explanation of the class initialization 670 // process. The step comments refers to the procedure described in that section. 671 // Note: implementation moved to static method to expose the this pointer. 672 void InstanceKlass::initialize(TRAPS) { 673 if (this->should_be_initialized()) { 674 initialize_impl(CHECK); 675 // Note: at this point the class may be initialized 676 // OR it may be in the state of being initialized 677 // in case of recursive initialization! 678 } else { 679 assert(is_initialized(), "sanity check"); 680 } 681 } 682 683 684 bool InstanceKlass::verify_code(TRAPS) { 685 // 1) Verify the bytecodes 686 return Verifier::verify(this, should_verify_class(), THREAD); 687 } 688 689 690 // Used exclusively by the shared spaces dump mechanism to prevent 691 // classes mapped into the shared regions in new VMs from appearing linked. 692 693 void InstanceKlass::unlink_class() { 694 assert(is_linked(), "must be linked"); 695 _init_state = loaded; 696 } 697 698 void InstanceKlass::link_class(TRAPS) { 699 assert(is_loaded(), "must be loaded"); 700 if (!is_linked()) { 701 link_class_impl(CHECK); 702 } 703 } 704 705 // Called to verify that a class can link during initialization, without 706 // throwing a VerifyError. 707 bool InstanceKlass::link_class_or_fail(TRAPS) { 708 assert(is_loaded(), "must be loaded"); 709 if (!is_linked()) { 710 link_class_impl(CHECK_false); 711 } 712 return is_linked(); 713 } 714 715 bool InstanceKlass::link_class_impl(TRAPS) { 716 if (DumpSharedSpaces && is_in_error_state()) { 717 // This is for CDS dumping phase only -- we use the in_error_state to indicate that 718 // the class has failed verification. Throwing the NoClassDefFoundError here is just 719 // a convenient way to stop repeat attempts to verify the same (bad) class. 720 // 721 // Note that the NoClassDefFoundError is not part of the JLS, and should not be thrown 722 // if we are executing Java code. This is not a problem for CDS dumping phase since 723 // it doesn't execute any Java code. 724 ResourceMark rm(THREAD); 725 Exceptions::fthrow(THREAD_AND_LOCATION, 726 vmSymbols::java_lang_NoClassDefFoundError(), 727 "Class %s, or one of its supertypes, failed class initialization", 728 external_name()); 729 return false; 730 } 731 // return if already verified 732 if (is_linked()) { 733 return true; 734 } 735 737 // timer handles recursion 738 assert(THREAD->is_Java_thread(), "non-JavaThread in link_class_impl"); 739 JavaThread* jt = (JavaThread*)THREAD; 740 741 // link super class before linking this class 742 Klass* super_klass = super(); 743 if (super_klass != NULL) { 744 if (super_klass->is_interface()) { // check if super class is an interface 745 ResourceMark rm(THREAD); 746 Exceptions::fthrow( 747 THREAD_AND_LOCATION, 748 vmSymbols::java_lang_IncompatibleClassChangeError(), 749 "class %s has interface %s as super class", 750 external_name(), 751 super_klass->external_name() 752 ); 753 return false; 754 } 755 756 InstanceKlass* ik_super = InstanceKlass::cast(super_klass); 757 ik_super->link_class_impl(CHECK_false); 758 } 759 760 // link all interfaces implemented by this class before linking this class 761 Array<InstanceKlass*>* interfaces = local_interfaces(); 762 int num_interfaces = interfaces->length(); 763 for (int index = 0; index < num_interfaces; index++) { 764 InstanceKlass* interk = interfaces->at(index); 765 interk->link_class_impl(CHECK_false); 766 } 767 768 // in case the class is linked in the process of linking its superclasses 769 if (is_linked()) { 770 return true; 771 } 772 773 // trace only the link time for this klass that includes 774 // the verification time 775 PerfClassTraceTime vmtimer(ClassLoader::perf_class_link_time(), 776 ClassLoader::perf_class_link_selftime(), 777 ClassLoader::perf_classes_linked(), 778 jt->get_thread_stat()->perf_recursion_counts_addr(), 779 jt->get_thread_stat()->perf_timers_addr(), 780 PerfClassTraceTime::CLASS_LINK); 781 782 // verification & rewriting 783 { 784 HandleMark hm(THREAD); 785 Handle h_init_lock(THREAD, init_lock()); 786 ObjectLocker ol(h_init_lock, THREAD, h_init_lock() != NULL); 787 // rewritten will have been set if loader constraint error found 788 // on an earlier link attempt 789 // don't verify or rewrite if already rewritten 790 // 791 792 if (!is_linked()) { 793 if (!is_rewritten()) { 794 { 795 bool verify_ok = verify_code(THREAD); 796 if (!verify_ok) { 797 return false; 798 } 799 } 800 801 // Just in case a side-effect of verify linked this class already 802 // (which can sometimes happen since the verifier loads classes 803 // using custom class loaders, which are free to initialize things) 804 if (is_linked()) { 805 return true; 806 } 807 808 // also sets rewritten 809 rewrite_class(CHECK_false); 810 } else if (is_shared()) { 811 SystemDictionaryShared::check_verification_constraints(this, CHECK_false); 812 } 813 814 // relocate jsrs and link methods after they are all rewritten 815 link_methods(CHECK_false); |