65 #include "oops/recordComponent.hpp"
66 #include "oops/symbol.hpp"
67 #include "prims/jvmtiExport.hpp"
68 #include "prims/jvmtiRedefineClasses.hpp"
69 #include "prims/jvmtiThreadState.hpp"
70 #include "prims/methodComparator.hpp"
71 #include "runtime/atomic.hpp"
72 #include "runtime/biasedLocking.hpp"
73 #include "runtime/fieldDescriptor.inline.hpp"
74 #include "runtime/handles.inline.hpp"
75 #include "runtime/javaCalls.hpp"
76 #include "runtime/mutexLocker.hpp"
77 #include "runtime/orderAccess.hpp"
78 #include "runtime/thread.inline.hpp"
79 #include "services/classLoadingService.hpp"
80 #include "services/threadService.hpp"
81 #include "utilities/dtrace.hpp"
82 #include "utilities/events.hpp"
83 #include "utilities/macros.hpp"
84 #include "utilities/stringUtils.hpp"
85 #ifdef COMPILER1
86 #include "c1/c1_Compiler.hpp"
87 #endif
88 #if INCLUDE_JFR
89 #include "jfr/jfrEvents.hpp"
90 #endif
91
92
93 #ifdef DTRACE_ENABLED
94
95
96 #define HOTSPOT_CLASS_INITIALIZATION_required HOTSPOT_CLASS_INITIALIZATION_REQUIRED
97 #define HOTSPOT_CLASS_INITIALIZATION_recursive HOTSPOT_CLASS_INITIALIZATION_RECURSIVE
98 #define HOTSPOT_CLASS_INITIALIZATION_concurrent HOTSPOT_CLASS_INITIALIZATION_CONCURRENT
99 #define HOTSPOT_CLASS_INITIALIZATION_erroneous HOTSPOT_CLASS_INITIALIZATION_ERRONEOUS
100 #define HOTSPOT_CLASS_INITIALIZATION_super__failed HOTSPOT_CLASS_INITIALIZATION_SUPER_FAILED
101 #define HOTSPOT_CLASS_INITIALIZATION_clinit HOTSPOT_CLASS_INITIALIZATION_CLINIT
102 #define HOTSPOT_CLASS_INITIALIZATION_error HOTSPOT_CLASS_INITIALIZATION_ERROR
103 #define HOTSPOT_CLASS_INITIALIZATION_end HOTSPOT_CLASS_INITIALIZATION_END
104 #define DTRACE_CLASSINIT_PROBE(type, thread_type) \
2625 int dest_index = 0;
2626 dest[dest_index++] = JVM_SIGNATURE_CLASS;
2627
2628 // Add the actual class name
2629 for (int src_index = 0; src_index < src_length; ) {
2630 dest[dest_index++] = src[src_index++];
2631 }
2632
2633 // If we have a hash, append it
2634 for (int hash_index = 0; hash_index < hash_len; ) {
2635 dest[dest_index++] = hash_buf[hash_index++];
2636 }
2637
2638 // Add the semicolon and the NULL
2639 dest[dest_index++] = JVM_SIGNATURE_ENDCLASS;
2640 dest[dest_index] = '\0';
2641 return dest;
2642 }
2643
2644 // Used to obtain the package name from a fully qualified class name.
2645 Symbol* InstanceKlass::package_from_name(const Symbol* name, TRAPS) {
2646 if (name == NULL) {
2647 return NULL;
2648 } else {
2649 if (name->utf8_length() <= 0) {
2650 return NULL;
2651 }
2652 ResourceMark rm(THREAD);
2653 const char* package_name = ClassLoader::package_from_name((const char*) name->as_C_string());
2654 if (package_name == NULL) {
2655 return NULL;
2656 }
2657 Symbol* pkg_name = SymbolTable::new_symbol(package_name);
2658 return pkg_name;
2659 }
2660 }
2661
2662 ModuleEntry* InstanceKlass::module() const {
2663 // For an unsafe anonymous class return the host class' module
2664 if (is_unsafe_anonymous()) {
2665 assert(unsafe_anonymous_host() != NULL, "unsafe anonymous class must have a host class");
2666 return unsafe_anonymous_host()->module();
2667 }
2668
2669 // Class is in a named package
2670 if (!in_unnamed_package()) {
2671 return _package_entry->module();
2672 }
2673
2674 // Class is in an unnamed package, return its loader's unnamed module
2675 return class_loader_data()->unnamed_module();
2676 }
2677
2678 void InstanceKlass::set_package(ClassLoaderData* loader_data, TRAPS) {
2679
2680 // ensure java/ packages only loaded by boot or platform builtin loaders
2681 check_prohibited_package(name(), loader_data, CHECK);
2682
2683 TempNewSymbol pkg_name = package_from_name(name(), CHECK);
2684
2685 if (pkg_name != NULL && loader_data != NULL) {
2686
2687 // Find in class loader's package entry table.
2688 _package_entry = loader_data->packages()->lookup_only(pkg_name);
2689
2690 // If the package name is not found in the loader's package
2691 // entry table, it is an indication that the package has not
2692 // been defined. Consider it defined within the unnamed module.
2693 if (_package_entry == NULL) {
2694 ResourceMark rm(THREAD);
2695
2696 if (!ModuleEntryTable::javabase_defined()) {
2697 // Before java.base is defined during bootstrapping, define all packages in
2698 // the java.base module. If a non-java.base package is erroneously placed
2699 // in the java.base module it will be caught later when java.base
2700 // is defined by ModuleEntryTable::verify_javabase_packages check.
2701 assert(ModuleEntryTable::javabase_moduleEntry() != NULL, JAVA_BASE_NAME " module is NULL");
2702 _package_entry = loader_data->packages()->lookup(pkg_name, ModuleEntryTable::javabase_moduleEntry());
2703 } else {
2759 }
2760
2761 return false;
2762 }
2763
2764 // return true if this class and other_class are in the same package. Classloader
2765 // and classname information is enough to determine a class's package
2766 bool InstanceKlass::is_same_class_package(oop other_class_loader,
2767 const Symbol* other_class_name) const {
2768 if (class_loader() != other_class_loader) {
2769 return false;
2770 }
2771 if (name()->fast_compare(other_class_name) == 0) {
2772 return true;
2773 }
2774
2775 {
2776 ResourceMark rm;
2777
2778 bool bad_class_name = false;
2779 const char* other_pkg =
2780 ClassLoader::package_from_name((const char*) other_class_name->as_C_string(), &bad_class_name);
2781 if (bad_class_name) {
2782 return false;
2783 }
2784 // Check that package_from_name() returns NULL, not "", if there is no package.
2785 assert(other_pkg == NULL || strlen(other_pkg) > 0, "package name is empty string");
2786
2787 const Symbol* const this_package_name =
2788 this->package() != NULL ? this->package()->name() : NULL;
2789
2790 if (this_package_name == NULL || other_pkg == NULL) {
2791 // One of the two doesn't have a package. Only return true if the other
2792 // one also doesn't have a package.
2793 return (const char*)this_package_name == other_pkg;
2794 }
2795
2796 // Check if package is identical
2797 return this_package_name->equals(other_pkg);
2798 }
2799 }
2800
2801 // Returns true iff super_method can be overridden by a method in targetclassname
2802 // See JLS 3rd edition 8.4.6.1
2803 // Assumes name-signature match
2804 // "this" is InstanceKlass of super_method which must exist
2805 // note that the InstanceKlass of the method in the targetclassname has not always been created yet
2806 bool InstanceKlass::is_override(const methodHandle& super_method, Handle targetclassloader, Symbol* targetclassname, TRAPS) {
2807 // Private methods can not be overridden
2808 if (super_method->is_private()) {
2809 return false;
2810 }
2811 // If super method is accessible, then override
2812 if ((super_method->is_protected()) ||
2813 (super_method->is_public())) {
2814 return true;
2815 }
2816 // Package-private methods are not inherited outside of package
2817 assert(super_method->is_package_private(), "must be package private");
2818 return(is_same_class_package(targetclassloader(), targetclassname));
2819 }
2820
2821 // Only boot and platform class loaders can define classes in "java/" packages.
2822 void InstanceKlass::check_prohibited_package(Symbol* class_name,
2823 ClassLoaderData* loader_data,
2824 TRAPS) {
2825 if (!loader_data->is_boot_class_loader_data() &&
2826 !loader_data->is_platform_class_loader_data() &&
2827 class_name != NULL) {
2828 ResourceMark rm(THREAD);
2829 char* name = class_name->as_C_string();
2830 if (strncmp(name, JAVAPKG, JAVAPKG_LEN) == 0 && name[JAVAPKG_LEN] == '/') {
2831 TempNewSymbol pkg_name = InstanceKlass::package_from_name(class_name, CHECK);
2832 assert(pkg_name != NULL, "Error in parsing package name starting with 'java/'");
2833 name = pkg_name->as_C_string();
2834 const char* class_loader_name = loader_data->loader_name_and_id();
2835 StringUtils::replace_no_expand(name, "/", ".");
2836 const char* msg_text1 = "Class loader (instance of): ";
2837 const char* msg_text2 = " tried to load prohibited package name: ";
2838 size_t len = strlen(msg_text1) + strlen(class_loader_name) + strlen(msg_text2) + strlen(name) + 1;
2839 char* message = NEW_RESOURCE_ARRAY_IN_THREAD(THREAD, char, len);
2840 jio_snprintf(message, len, "%s%s%s%s", msg_text1, class_loader_name, msg_text2, name);
2841 THROW_MSG(vmSymbols::java_lang_SecurityException(), message);
2842 }
2843 }
2844 return;
2845 }
2846
2847 bool InstanceKlass::find_inner_classes_attr(int* ooff, int* noff, TRAPS) const {
2848 constantPoolHandle i_cp(THREAD, constants());
2849 for (InnerClassesIterator iter(this); !iter.done(); iter.next()) {
2850 int ioff = iter.inner_class_info_index();
2851 if (ioff != 0) {
|
65 #include "oops/recordComponent.hpp"
66 #include "oops/symbol.hpp"
67 #include "prims/jvmtiExport.hpp"
68 #include "prims/jvmtiRedefineClasses.hpp"
69 #include "prims/jvmtiThreadState.hpp"
70 #include "prims/methodComparator.hpp"
71 #include "runtime/atomic.hpp"
72 #include "runtime/biasedLocking.hpp"
73 #include "runtime/fieldDescriptor.inline.hpp"
74 #include "runtime/handles.inline.hpp"
75 #include "runtime/javaCalls.hpp"
76 #include "runtime/mutexLocker.hpp"
77 #include "runtime/orderAccess.hpp"
78 #include "runtime/thread.inline.hpp"
79 #include "services/classLoadingService.hpp"
80 #include "services/threadService.hpp"
81 #include "utilities/dtrace.hpp"
82 #include "utilities/events.hpp"
83 #include "utilities/macros.hpp"
84 #include "utilities/stringUtils.hpp"
85 #include "utilities/utf8.hpp"
86 #ifdef COMPILER1
87 #include "c1/c1_Compiler.hpp"
88 #endif
89 #if INCLUDE_JFR
90 #include "jfr/jfrEvents.hpp"
91 #endif
92
93
94 #ifdef DTRACE_ENABLED
95
96
97 #define HOTSPOT_CLASS_INITIALIZATION_required HOTSPOT_CLASS_INITIALIZATION_REQUIRED
98 #define HOTSPOT_CLASS_INITIALIZATION_recursive HOTSPOT_CLASS_INITIALIZATION_RECURSIVE
99 #define HOTSPOT_CLASS_INITIALIZATION_concurrent HOTSPOT_CLASS_INITIALIZATION_CONCURRENT
100 #define HOTSPOT_CLASS_INITIALIZATION_erroneous HOTSPOT_CLASS_INITIALIZATION_ERRONEOUS
101 #define HOTSPOT_CLASS_INITIALIZATION_super__failed HOTSPOT_CLASS_INITIALIZATION_SUPER_FAILED
102 #define HOTSPOT_CLASS_INITIALIZATION_clinit HOTSPOT_CLASS_INITIALIZATION_CLINIT
103 #define HOTSPOT_CLASS_INITIALIZATION_error HOTSPOT_CLASS_INITIALIZATION_ERROR
104 #define HOTSPOT_CLASS_INITIALIZATION_end HOTSPOT_CLASS_INITIALIZATION_END
105 #define DTRACE_CLASSINIT_PROBE(type, thread_type) \
2626 int dest_index = 0;
2627 dest[dest_index++] = JVM_SIGNATURE_CLASS;
2628
2629 // Add the actual class name
2630 for (int src_index = 0; src_index < src_length; ) {
2631 dest[dest_index++] = src[src_index++];
2632 }
2633
2634 // If we have a hash, append it
2635 for (int hash_index = 0; hash_index < hash_len; ) {
2636 dest[dest_index++] = hash_buf[hash_index++];
2637 }
2638
2639 // Add the semicolon and the NULL
2640 dest[dest_index++] = JVM_SIGNATURE_ENDCLASS;
2641 dest[dest_index] = '\0';
2642 return dest;
2643 }
2644
2645 // Used to obtain the package name from a fully qualified class name.
2646 Symbol* InstanceKlass::package_from_name(const Symbol* name, bool* bad_class_name) {
2647 if (name == NULL) {
2648 if (bad_class_name != NULL) {
2649 *bad_class_name = true;
2650 }
2651 return NULL;
2652 }
2653
2654 int utf_len = name->utf8_length();
2655 if (utf_len == 0 ) {
2656 return NULL;
2657 }
2658 const jbyte* base = (const jbyte*)name->base();
2659 const jbyte* start = base;
2660 const jbyte* end = UTF8::strrchr(start, utf_len, JVM_SIGNATURE_SLASH);
2661 if (end == NULL) {
2662 return NULL;
2663 }
2664 // Skip over '['s
2665 if (*start == JVM_SIGNATURE_ARRAY) {
2666 do {
2667 start++;
2668 } while (*start == JVM_SIGNATURE_ARRAY);
2669
2670 // Fully qualified class names should not contain a 'L'.
2671 // Set bad_class_name to true to indicate that the package name
2672 // could not be obtained due to an error condition.
2673 // In this situation, is_same_class_package returns false.
2674 if (start != base && *start == JVM_SIGNATURE_CLASS) {
2675 if (bad_class_name != NULL) {
2676 *bad_class_name = true;
2677 }
2678 return NULL;
2679 }
2680 }
2681 if (start == end || end == base + utf_len) {
2682 // A package or class name could have just the slash character in the name.
2683 if (bad_class_name != NULL) {
2684 *bad_class_name = true;
2685 }
2686 return NULL;
2687 }
2688 Symbol* pkg_name = SymbolTable::new_symbol(name, start - base, end - base);
2689 return pkg_name;
2690 }
2691
2692 ModuleEntry* InstanceKlass::module() const {
2693 // For an unsafe anonymous class return the host class' module
2694 if (is_unsafe_anonymous()) {
2695 assert(unsafe_anonymous_host() != NULL, "unsafe anonymous class must have a host class");
2696 return unsafe_anonymous_host()->module();
2697 }
2698
2699 // Class is in a named package
2700 if (!in_unnamed_package()) {
2701 return _package_entry->module();
2702 }
2703
2704 // Class is in an unnamed package, return its loader's unnamed module
2705 return class_loader_data()->unnamed_module();
2706 }
2707
2708 void InstanceKlass::set_package(ClassLoaderData* loader_data, TRAPS) {
2709
2710 // ensure java/ packages only loaded by boot or platform builtin loaders
2711 check_prohibited_package(name(), loader_data, CHECK);
2712
2713 TempNewSymbol pkg_name = package_from_name(name());
2714
2715 if (pkg_name != NULL && loader_data != NULL) {
2716
2717 // Find in class loader's package entry table.
2718 _package_entry = loader_data->packages()->lookup_only(pkg_name);
2719
2720 // If the package name is not found in the loader's package
2721 // entry table, it is an indication that the package has not
2722 // been defined. Consider it defined within the unnamed module.
2723 if (_package_entry == NULL) {
2724 ResourceMark rm(THREAD);
2725
2726 if (!ModuleEntryTable::javabase_defined()) {
2727 // Before java.base is defined during bootstrapping, define all packages in
2728 // the java.base module. If a non-java.base package is erroneously placed
2729 // in the java.base module it will be caught later when java.base
2730 // is defined by ModuleEntryTable::verify_javabase_packages check.
2731 assert(ModuleEntryTable::javabase_moduleEntry() != NULL, JAVA_BASE_NAME " module is NULL");
2732 _package_entry = loader_data->packages()->lookup(pkg_name, ModuleEntryTable::javabase_moduleEntry());
2733 } else {
2789 }
2790
2791 return false;
2792 }
2793
2794 // return true if this class and other_class are in the same package. Classloader
2795 // and classname information is enough to determine a class's package
2796 bool InstanceKlass::is_same_class_package(oop other_class_loader,
2797 const Symbol* other_class_name) const {
2798 if (class_loader() != other_class_loader) {
2799 return false;
2800 }
2801 if (name()->fast_compare(other_class_name) == 0) {
2802 return true;
2803 }
2804
2805 {
2806 ResourceMark rm;
2807
2808 bool bad_class_name = false;
2809 TempNewSymbol other_pkg = InstanceKlass::package_from_name(other_class_name, &bad_class_name);
2810 if (bad_class_name) {
2811 return false;
2812 }
2813 // Check that package_from_name() returns NULL, not "", if there is no package.
2814 assert(other_pkg == NULL || other_pkg->utf8_length() > 0, "package name is empty string");
2815
2816 const Symbol* const this_package_name =
2817 this->package() != NULL ? this->package()->name() : NULL;
2818
2819 if (this_package_name == NULL || other_pkg == NULL) {
2820 // One of the two doesn't have a package. Only return true if the other
2821 // one also doesn't have a package.
2822 return this_package_name == other_pkg;
2823 }
2824
2825 // Check if package is identical
2826 return this_package_name->fast_compare(other_pkg) == 0;
2827 }
2828 }
2829
2830 // Returns true iff super_method can be overridden by a method in targetclassname
2831 // See JLS 3rd edition 8.4.6.1
2832 // Assumes name-signature match
2833 // "this" is InstanceKlass of super_method which must exist
2834 // note that the InstanceKlass of the method in the targetclassname has not always been created yet
2835 bool InstanceKlass::is_override(const methodHandle& super_method, Handle targetclassloader, Symbol* targetclassname, TRAPS) {
2836 // Private methods can not be overridden
2837 if (super_method->is_private()) {
2838 return false;
2839 }
2840 // If super method is accessible, then override
2841 if ((super_method->is_protected()) ||
2842 (super_method->is_public())) {
2843 return true;
2844 }
2845 // Package-private methods are not inherited outside of package
2846 assert(super_method->is_package_private(), "must be package private");
2847 return(is_same_class_package(targetclassloader(), targetclassname));
2848 }
2849
2850 // Only boot and platform class loaders can define classes in "java/" packages.
2851 void InstanceKlass::check_prohibited_package(Symbol* class_name,
2852 ClassLoaderData* loader_data,
2853 TRAPS) {
2854 if (!loader_data->is_boot_class_loader_data() &&
2855 !loader_data->is_platform_class_loader_data() &&
2856 class_name != NULL) {
2857 ResourceMark rm(THREAD);
2858 char* name = class_name->as_C_string();
2859 if (strncmp(name, JAVAPKG, JAVAPKG_LEN) == 0 && name[JAVAPKG_LEN] == '/') {
2860 TempNewSymbol pkg_name = InstanceKlass::package_from_name(class_name);
2861 assert(pkg_name != NULL, "Error in parsing package name starting with 'java/'");
2862 name = pkg_name->as_C_string();
2863 const char* class_loader_name = loader_data->loader_name_and_id();
2864 StringUtils::replace_no_expand(name, "/", ".");
2865 const char* msg_text1 = "Class loader (instance of): ";
2866 const char* msg_text2 = " tried to load prohibited package name: ";
2867 size_t len = strlen(msg_text1) + strlen(class_loader_name) + strlen(msg_text2) + strlen(name) + 1;
2868 char* message = NEW_RESOURCE_ARRAY_IN_THREAD(THREAD, char, len);
2869 jio_snprintf(message, len, "%s%s%s%s", msg_text1, class_loader_name, msg_text2, name);
2870 THROW_MSG(vmSymbols::java_lang_SecurityException(), message);
2871 }
2872 }
2873 return;
2874 }
2875
2876 bool InstanceKlass::find_inner_classes_attr(int* ooff, int* noff, TRAPS) const {
2877 constantPoolHandle i_cp(THREAD, constants());
2878 for (InnerClassesIterator iter(this); !iter.done(); iter.next()) {
2879 int ioff = iter.inner_class_info_index();
2880 if (ioff != 0) {
|