274 // 3. If we load the class internally, or user classloader uses same thread 275 // loadClassFromxxx or defineClass via parseClassFile Super ... 276 // 3.1 resolve_super_or_fail creates placeholder: T1, Super (super Base) 277 // 3.3 resolve_instance_class_or_null Base, finds placeholder for Base 278 // 3.4 calls resolve_super_or_fail Base 279 // 3.5 finds T1,Base -> throws class circularity 280 //OR 4. If T2 tries to resolve Super via defineClass Super ... 281 // 4.1 resolve_super_or_fail creates placeholder: T2, Super (super Base) 282 // 4.2 resolve_instance_class_or_null Base, finds placeholder for Base (super Super) 283 // 4.3 calls resolve_super_or_fail Super in parallel on own thread T2 284 // 4.4 finds T2, Super -> throws class circularity 285 // Must be called, even if superclass is null, since this is 286 // where the placeholder entry is created which claims this 287 // thread is loading this class/classloader. 288 Klass* SystemDictionary::resolve_super_or_fail(Symbol* child_name, 289 Symbol* class_name, 290 Handle class_loader, 291 Handle protection_domain, 292 bool is_superclass, 293 TRAPS) { 294 295 // Try to get one of the well-known klasses. 296 // They are trusted, and do not participate in circularities. 297 if (LinkWellKnownClasses) { 298 Klass* k = find_well_known_klass(class_name); 299 if (k != NULL) { 300 return k; 301 } 302 } 303 304 // Double-check, if child class is already loaded, just return super-class,interface 305 // Don't add a placedholder if already loaded, i.e. already in system dictionary 306 // Make sure there's a placeholder for the *child* before resolving. 307 // Used as a claim that this thread is currently loading superclass/classloader 308 // Used here for ClassCircularity checks and also for heap verification 309 // (every InstanceKlass in the heap needs to be in the system dictionary 310 // or have a placeholder). 311 // Must check ClassCircularity before checking if super class is already loaded 312 // 313 // We might not already have a placeholder if this child_name was 314 // first seen via resolve_from_stream (jni_DefineClass or JVM_DefineClass); 315 // the name of the class might not be known until the stream is actually 316 // parsed. 317 // Bugs 4643874, 4715493 318 // compute_hash can have a safepoint 319 320 ClassLoaderData* loader_data = class_loader_data(class_loader); 321 unsigned int d_hash = dictionary()->compute_hash(child_name, loader_data); 322 int d_index = dictionary()->hash_to_index(d_hash); 323 unsigned int p_hash = placeholders()->compute_hash(child_name, loader_data); 909 // We're using a No_Safepoint_Verifier to catch any place where we 910 // might potentially do a GC at all. 911 // SystemDictionary::do_unloading() asserts that classes are only 912 // unloaded at a safepoint. 913 No_Safepoint_Verifier nosafepoint; 914 return dictionary()->find(d_index, d_hash, class_name, loader_data, 915 protection_domain, THREAD); 916 } 917 } 918 919 920 // Look for a loaded instance or array klass by name. Do not do any loading. 921 // return NULL in case of error. 922 Klass* SystemDictionary::find_instance_or_array_klass(Symbol* class_name, 923 Handle class_loader, 924 Handle protection_domain, 925 TRAPS) { 926 Klass* k = NULL; 927 assert(class_name != NULL, "class name must be non NULL"); 928 929 // Try to get one of the well-known klasses. 930 if (LinkWellKnownClasses) { 931 k = find_well_known_klass(class_name); 932 if (k != NULL) { 933 return k; 934 } 935 } 936 937 if (FieldType::is_array(class_name)) { 938 // The name refers to an array. Parse the name. 939 // dimension and object_key in FieldArrayInfo are assigned as a 940 // side-effect of this call 941 FieldArrayInfo fd; 942 BasicType t = FieldType::get_array_info(class_name, fd, CHECK_(NULL)); 943 if (t != T_OBJECT) { 944 k = Universe::typeArrayKlassObj(t); 945 } else { 946 k = SystemDictionary::find(fd.object_key(), class_loader, protection_domain, THREAD); 947 } 948 if (k != NULL) { 949 k = Klass::cast(k)->array_klass_or_null(fd.dimension()); 950 } 951 } else { 952 k = find(class_name, class_loader, protection_domain, THREAD); 953 } 954 return k; 955 } 956 957 // Quick range check for names of well-known classes: 958 static Symbol* wk_klass_name_limits[2] = {NULL, NULL}; 959 960 #ifndef PRODUCT 961 static int find_wkk_calls, find_wkk_probes, find_wkk_wins; 962 // counts for "hello world": 3983, 1616, 1075 963 // => 60% hit after limit guard, 25% total win rate 964 #endif 965 966 Klass* SystemDictionary::find_well_known_klass(Symbol* class_name) { 967 // A bounds-check on class_name will quickly get a negative result. 968 NOT_PRODUCT(find_wkk_calls++); 969 if (class_name >= wk_klass_name_limits[0] && 970 class_name <= wk_klass_name_limits[1]) { 971 NOT_PRODUCT(find_wkk_probes++); 972 vmSymbols::SID sid = vmSymbols::find_sid(class_name); 973 if (sid != vmSymbols::NO_SID) { 974 Klass* k = NULL; 975 switch (sid) { 976 #define WK_KLASS_CASE(name, symbol, option) \ 977 case vmSymbols::VM_SYMBOL_ENUM_NAME(symbol): \ 978 if (option == Pre_Link) { \ 979 k = WK_KLASS(name); \ 980 } \ 981 break; 982 WK_KLASSES_DO(WK_KLASS_CASE) 983 #undef WK_KLASS_CASE 984 } 985 NOT_PRODUCT(if (k != NULL) find_wkk_wins++); 986 return k; 987 } 988 } 989 return NULL; 990 } 991 992 // Note: this method is much like resolve_from_stream, but 993 // updates no supplemental data structures. 994 // TODO consolidate the two methods with a helper routine? 995 Klass* SystemDictionary::parse_stream(Symbol* class_name, 996 Handle class_loader, 997 Handle protection_domain, 998 ClassFileStream* st, 999 KlassHandle host_klass, 1000 GrowableArray<Handle>* cp_patches, 1001 TRAPS) { 1002 TempNewSymbol parsed_name = NULL; 1003 1004 // Parse the stream. Note that we do this even though this klass might 1005 // already be present in the SystemDictionary, otherwise we would not 1006 // throw potential ClassFormatErrors. 1007 // 1008 // Note: "name" is updated. 1009 // Further note: a placeholder will be added for this class when 1010 // super classes are loaded (resolve_super_or_fail). We expect this 1011 // to be called for all classes but java.lang.Object; and we preload 1925 } 1926 if ((*klassp) == NULL && try_load) { 1927 if (must_load) { 1928 (*klassp) = resolve_or_fail(symbol, true, CHECK_0); // load required class 1929 } else { 1930 (*klassp) = resolve_or_null(symbol, CHECK_0); // load optional klass 1931 } 1932 } 1933 return ((*klassp) != NULL); 1934 } 1935 1936 void SystemDictionary::initialize_wk_klasses_until(WKID limit_id, WKID &start_id, TRAPS) { 1937 assert((int)start_id <= (int)limit_id, "IDs are out of order!"); 1938 for (int id = (int)start_id; id < (int)limit_id; id++) { 1939 assert(id >= (int)FIRST_WKID && id < (int)WKID_LIMIT, "oob"); 1940 int info = wk_init_info[id - FIRST_WKID]; 1941 int sid = (info >> CEIL_LG_OPTION_LIMIT); 1942 int opt = (info & right_n_bits(CEIL_LG_OPTION_LIMIT)); 1943 1944 initialize_wk_klass((WKID)id, opt, CHECK); 1945 1946 // Update limits, so find_well_known_klass can be very fast: 1947 Symbol* s = vmSymbols::symbol_at((vmSymbols::SID)sid); 1948 if (wk_klass_name_limits[1] == NULL) { 1949 wk_klass_name_limits[0] = wk_klass_name_limits[1] = s; 1950 } else if (wk_klass_name_limits[1] < s) { 1951 wk_klass_name_limits[1] = s; 1952 } else if (wk_klass_name_limits[0] > s) { 1953 wk_klass_name_limits[0] = s; 1954 } 1955 } 1956 1957 // move the starting value forward to the limit: 1958 start_id = limit_id; 1959 } 1960 1961 #ifdef ASSERT 1962 void SystemDictionary::check_wk_pre_link_klasses() { 1963 #define WK_KLASS_CHECK(name, symbol, option) \ 1964 if (option == Pre_Link) { \ 1965 assert(name()->is_public(), ""); \ 1966 } 1967 WK_KLASSES_DO(WK_KLASS_CHECK); 1968 #undef WK_KLASS_CHECK 1969 } 1970 #endif 1971 1972 void SystemDictionary::initialize_preloaded_classes(TRAPS) { 1973 assert(WK_KLASS(Object_klass) == NULL, "preloaded classes should only be initialized once"); 1974 // Preload commonly used klasses 1975 WKID scan = FIRST_WKID; 1976 // first do Object, then String, Class 1977 if (UseSharedSpaces) { 1978 initialize_wk_klasses_through(WK_KLASS_ENUM_NAME(Object_klass), scan, CHECK); 1979 // Initialize the constant pool for the Object_class 1980 InstanceKlass* ik = InstanceKlass::cast(Object_klass()); 1981 ik->constants()->restore_unshareable_info(CHECK); 1982 initialize_wk_klasses_through(WK_KLASS_ENUM_NAME(Class_klass), scan, CHECK); 1983 } else { 1984 initialize_wk_klasses_through(WK_KLASS_ENUM_NAME(Class_klass), scan, CHECK); 1985 } 1986 1987 // Calculate offsets for String and Class classes since they are loaded and 1988 // can be used after this point. 1989 java_lang_String::compute_offsets(); 1990 java_lang_Class::compute_offsets(); 1991 2006 2007 initialize_wk_klasses_through(WK_KLASS_ENUM_NAME(PhantomReference_klass), scan, CHECK); 2008 InstanceKlass::cast(WK_KLASS(SoftReference_klass))->set_reference_type(REF_SOFT); 2009 InstanceKlass::cast(WK_KLASS(WeakReference_klass))->set_reference_type(REF_WEAK); 2010 InstanceKlass::cast(WK_KLASS(FinalReference_klass))->set_reference_type(REF_FINAL); 2011 InstanceKlass::cast(WK_KLASS(PhantomReference_klass))->set_reference_type(REF_PHANTOM); 2012 2013 // JSR 292 classes 2014 WKID jsr292_group_start = WK_KLASS_ENUM_NAME(MethodHandle_klass); 2015 WKID jsr292_group_end = WK_KLASS_ENUM_NAME(VolatileCallSite_klass); 2016 initialize_wk_klasses_until(jsr292_group_start, scan, CHECK); 2017 if (EnableInvokeDynamic) { 2018 initialize_wk_klasses_through(jsr292_group_end, scan, CHECK); 2019 } else { 2020 // Skip the JSR 292 classes, if not enabled. 2021 scan = WKID(jsr292_group_end + 1); 2022 } 2023 2024 initialize_wk_klasses_until(WKID_LIMIT, scan, CHECK); 2025 2026 check_wk_pre_link_klasses(); 2027 2028 _box_klasses[T_BOOLEAN] = WK_KLASS(Boolean_klass); 2029 _box_klasses[T_CHAR] = WK_KLASS(Character_klass); 2030 _box_klasses[T_FLOAT] = WK_KLASS(Float_klass); 2031 _box_klasses[T_DOUBLE] = WK_KLASS(Double_klass); 2032 _box_klasses[T_BYTE] = WK_KLASS(Byte_klass); 2033 _box_klasses[T_SHORT] = WK_KLASS(Short_klass); 2034 _box_klasses[T_INT] = WK_KLASS(Integer_klass); 2035 _box_klasses[T_LONG] = WK_KLASS(Long_klass); 2036 //_box_klasses[T_OBJECT] = WK_KLASS(object_klass); 2037 //_box_klasses[T_ARRAY] = WK_KLASS(object_klass); 2038 2039 #ifdef KERNEL 2040 if (DownloadManager_klass() == NULL) { 2041 warning("Cannot find sun/jkernel/DownloadManager"); 2042 } 2043 #endif // KERNEL 2044 2045 { // Compute whether we should use loadClass or loadClassInternal when loading classes. 2046 Method* method = InstanceKlass::cast(ClassLoader_klass())->find_method(vmSymbols::loadClassInternal_name(), vmSymbols::string_class_signature()); 2047 _has_loadClassInternal = (method != NULL); | 274 // 3. If we load the class internally, or user classloader uses same thread 275 // loadClassFromxxx or defineClass via parseClassFile Super ... 276 // 3.1 resolve_super_or_fail creates placeholder: T1, Super (super Base) 277 // 3.3 resolve_instance_class_or_null Base, finds placeholder for Base 278 // 3.4 calls resolve_super_or_fail Base 279 // 3.5 finds T1,Base -> throws class circularity 280 //OR 4. If T2 tries to resolve Super via defineClass Super ... 281 // 4.1 resolve_super_or_fail creates placeholder: T2, Super (super Base) 282 // 4.2 resolve_instance_class_or_null Base, finds placeholder for Base (super Super) 283 // 4.3 calls resolve_super_or_fail Super in parallel on own thread T2 284 // 4.4 finds T2, Super -> throws class circularity 285 // Must be called, even if superclass is null, since this is 286 // where the placeholder entry is created which claims this 287 // thread is loading this class/classloader. 288 Klass* SystemDictionary::resolve_super_or_fail(Symbol* child_name, 289 Symbol* class_name, 290 Handle class_loader, 291 Handle protection_domain, 292 bool is_superclass, 293 TRAPS) { 294 // Double-check, if child class is already loaded, just return super-class,interface 295 // Don't add a placedholder if already loaded, i.e. already in system dictionary 296 // Make sure there's a placeholder for the *child* before resolving. 297 // Used as a claim that this thread is currently loading superclass/classloader 298 // Used here for ClassCircularity checks and also for heap verification 299 // (every InstanceKlass in the heap needs to be in the system dictionary 300 // or have a placeholder). 301 // Must check ClassCircularity before checking if super class is already loaded 302 // 303 // We might not already have a placeholder if this child_name was 304 // first seen via resolve_from_stream (jni_DefineClass or JVM_DefineClass); 305 // the name of the class might not be known until the stream is actually 306 // parsed. 307 // Bugs 4643874, 4715493 308 // compute_hash can have a safepoint 309 310 ClassLoaderData* loader_data = class_loader_data(class_loader); 311 unsigned int d_hash = dictionary()->compute_hash(child_name, loader_data); 312 int d_index = dictionary()->hash_to_index(d_hash); 313 unsigned int p_hash = placeholders()->compute_hash(child_name, loader_data); 899 // We're using a No_Safepoint_Verifier to catch any place where we 900 // might potentially do a GC at all. 901 // SystemDictionary::do_unloading() asserts that classes are only 902 // unloaded at a safepoint. 903 No_Safepoint_Verifier nosafepoint; 904 return dictionary()->find(d_index, d_hash, class_name, loader_data, 905 protection_domain, THREAD); 906 } 907 } 908 909 910 // Look for a loaded instance or array klass by name. Do not do any loading. 911 // return NULL in case of error. 912 Klass* SystemDictionary::find_instance_or_array_klass(Symbol* class_name, 913 Handle class_loader, 914 Handle protection_domain, 915 TRAPS) { 916 Klass* k = NULL; 917 assert(class_name != NULL, "class name must be non NULL"); 918 919 if (FieldType::is_array(class_name)) { 920 // The name refers to an array. Parse the name. 921 // dimension and object_key in FieldArrayInfo are assigned as a 922 // side-effect of this call 923 FieldArrayInfo fd; 924 BasicType t = FieldType::get_array_info(class_name, fd, CHECK_(NULL)); 925 if (t != T_OBJECT) { 926 k = Universe::typeArrayKlassObj(t); 927 } else { 928 k = SystemDictionary::find(fd.object_key(), class_loader, protection_domain, THREAD); 929 } 930 if (k != NULL) { 931 k = Klass::cast(k)->array_klass_or_null(fd.dimension()); 932 } 933 } else { 934 k = find(class_name, class_loader, protection_domain, THREAD); 935 } 936 return k; 937 } 938 939 // Note: this method is much like resolve_from_stream, but 940 // updates no supplemental data structures. 941 // TODO consolidate the two methods with a helper routine? 942 Klass* SystemDictionary::parse_stream(Symbol* class_name, 943 Handle class_loader, 944 Handle protection_domain, 945 ClassFileStream* st, 946 KlassHandle host_klass, 947 GrowableArray<Handle>* cp_patches, 948 TRAPS) { 949 TempNewSymbol parsed_name = NULL; 950 951 // Parse the stream. Note that we do this even though this klass might 952 // already be present in the SystemDictionary, otherwise we would not 953 // throw potential ClassFormatErrors. 954 // 955 // Note: "name" is updated. 956 // Further note: a placeholder will be added for this class when 957 // super classes are loaded (resolve_super_or_fail). We expect this 958 // to be called for all classes but java.lang.Object; and we preload 1872 } 1873 if ((*klassp) == NULL && try_load) { 1874 if (must_load) { 1875 (*klassp) = resolve_or_fail(symbol, true, CHECK_0); // load required class 1876 } else { 1877 (*klassp) = resolve_or_null(symbol, CHECK_0); // load optional klass 1878 } 1879 } 1880 return ((*klassp) != NULL); 1881 } 1882 1883 void SystemDictionary::initialize_wk_klasses_until(WKID limit_id, WKID &start_id, TRAPS) { 1884 assert((int)start_id <= (int)limit_id, "IDs are out of order!"); 1885 for (int id = (int)start_id; id < (int)limit_id; id++) { 1886 assert(id >= (int)FIRST_WKID && id < (int)WKID_LIMIT, "oob"); 1887 int info = wk_init_info[id - FIRST_WKID]; 1888 int sid = (info >> CEIL_LG_OPTION_LIMIT); 1889 int opt = (info & right_n_bits(CEIL_LG_OPTION_LIMIT)); 1890 1891 initialize_wk_klass((WKID)id, opt, CHECK); 1892 } 1893 1894 // move the starting value forward to the limit: 1895 start_id = limit_id; 1896 } 1897 1898 void SystemDictionary::initialize_preloaded_classes(TRAPS) { 1899 assert(WK_KLASS(Object_klass) == NULL, "preloaded classes should only be initialized once"); 1900 // Preload commonly used klasses 1901 WKID scan = FIRST_WKID; 1902 // first do Object, then String, Class 1903 if (UseSharedSpaces) { 1904 initialize_wk_klasses_through(WK_KLASS_ENUM_NAME(Object_klass), scan, CHECK); 1905 // Initialize the constant pool for the Object_class 1906 InstanceKlass* ik = InstanceKlass::cast(Object_klass()); 1907 ik->constants()->restore_unshareable_info(CHECK); 1908 initialize_wk_klasses_through(WK_KLASS_ENUM_NAME(Class_klass), scan, CHECK); 1909 } else { 1910 initialize_wk_klasses_through(WK_KLASS_ENUM_NAME(Class_klass), scan, CHECK); 1911 } 1912 1913 // Calculate offsets for String and Class classes since they are loaded and 1914 // can be used after this point. 1915 java_lang_String::compute_offsets(); 1916 java_lang_Class::compute_offsets(); 1917 1932 1933 initialize_wk_klasses_through(WK_KLASS_ENUM_NAME(PhantomReference_klass), scan, CHECK); 1934 InstanceKlass::cast(WK_KLASS(SoftReference_klass))->set_reference_type(REF_SOFT); 1935 InstanceKlass::cast(WK_KLASS(WeakReference_klass))->set_reference_type(REF_WEAK); 1936 InstanceKlass::cast(WK_KLASS(FinalReference_klass))->set_reference_type(REF_FINAL); 1937 InstanceKlass::cast(WK_KLASS(PhantomReference_klass))->set_reference_type(REF_PHANTOM); 1938 1939 // JSR 292 classes 1940 WKID jsr292_group_start = WK_KLASS_ENUM_NAME(MethodHandle_klass); 1941 WKID jsr292_group_end = WK_KLASS_ENUM_NAME(VolatileCallSite_klass); 1942 initialize_wk_klasses_until(jsr292_group_start, scan, CHECK); 1943 if (EnableInvokeDynamic) { 1944 initialize_wk_klasses_through(jsr292_group_end, scan, CHECK); 1945 } else { 1946 // Skip the JSR 292 classes, if not enabled. 1947 scan = WKID(jsr292_group_end + 1); 1948 } 1949 1950 initialize_wk_klasses_until(WKID_LIMIT, scan, CHECK); 1951 1952 _box_klasses[T_BOOLEAN] = WK_KLASS(Boolean_klass); 1953 _box_klasses[T_CHAR] = WK_KLASS(Character_klass); 1954 _box_klasses[T_FLOAT] = WK_KLASS(Float_klass); 1955 _box_klasses[T_DOUBLE] = WK_KLASS(Double_klass); 1956 _box_klasses[T_BYTE] = WK_KLASS(Byte_klass); 1957 _box_klasses[T_SHORT] = WK_KLASS(Short_klass); 1958 _box_klasses[T_INT] = WK_KLASS(Integer_klass); 1959 _box_klasses[T_LONG] = WK_KLASS(Long_klass); 1960 //_box_klasses[T_OBJECT] = WK_KLASS(object_klass); 1961 //_box_klasses[T_ARRAY] = WK_KLASS(object_klass); 1962 1963 #ifdef KERNEL 1964 if (DownloadManager_klass() == NULL) { 1965 warning("Cannot find sun/jkernel/DownloadManager"); 1966 } 1967 #endif // KERNEL 1968 1969 { // Compute whether we should use loadClass or loadClassInternal when loading classes. 1970 Method* method = InstanceKlass::cast(ClassLoader_klass())->find_method(vmSymbols::loadClassInternal_name(), vmSymbols::string_class_signature()); 1971 _has_loadClassInternal = (method != NULL); |