21 * questions.
22 *
23 */
24
25 #include "precompiled.hpp"
26 #include "classfile/classLoaderData.inline.hpp"
27 #include "classfile/dictionary.hpp"
28 #include "classfile/javaClasses.hpp"
29 #include "classfile/loaderConstraints.hpp"
30 #include "classfile/placeholders.hpp"
31 #include "classfile/resolutionErrors.hpp"
32 #include "classfile/systemDictionary.hpp"
33 #if INCLUDE_CDS
34 #include "classfile/sharedClassUtil.hpp"
35 #include "classfile/systemDictionaryShared.hpp"
36 #endif
37 #include "classfile/vmSymbols.hpp"
38 #include "compiler/compileBroker.hpp"
39 #include "interpreter/bytecodeStream.hpp"
40 #include "interpreter/interpreter.hpp"
41 #include "memory/filemap.hpp"
42 #include "memory/gcLocker.hpp"
43 #include "memory/oopFactory.hpp"
44 #include "oops/instanceKlass.hpp"
45 #include "oops/instanceRefKlass.hpp"
46 #include "oops/klass.inline.hpp"
47 #include "oops/methodData.hpp"
48 #include "oops/objArrayKlass.hpp"
49 #include "oops/oop.inline.hpp"
50 #include "oops/oop.inline2.hpp"
51 #include "oops/typeArrayKlass.hpp"
52 #include "prims/jvmtiEnvBase.hpp"
53 #include "prims/methodHandles.hpp"
54 #include "runtime/arguments.hpp"
55 #include "runtime/biasedLocking.hpp"
56 #include "runtime/fieldType.hpp"
57 #include "runtime/handles.inline.hpp"
58 #include "runtime/java.hpp"
59 #include "runtime/javaCalls.hpp"
60 #include "runtime/mutexLocker.hpp"
61 #include "runtime/orderAccess.inline.hpp"
62 #include "runtime/signature.hpp"
63 #include "services/classLoadingService.hpp"
64 #include "services/threadService.hpp"
65 #include "utilities/macros.hpp"
66 #include "utilities/ticks.hpp"
67 #if INCLUDE_TRACE
68 #include "trace/tracing.hpp"
69 #endif
70
71 Dictionary* SystemDictionary::_dictionary = NULL;
72 PlaceholderTable* SystemDictionary::_placeholders = NULL;
73 Dictionary* SystemDictionary::_shared_dictionary = NULL;
74 LoaderConstraintTable* SystemDictionary::_loader_constraints = NULL;
75 ResolutionErrorTable* SystemDictionary::_resolution_errors = NULL;
76 SymbolPropertyTable* SystemDictionary::_invoke_method_table = NULL;
77
78
79 int SystemDictionary::_number_of_modifications = 0;
80 int SystemDictionary::_sdgeneration = 0;
81 const int SystemDictionary::_primelist[_prime_array_size] = {1009,2017,4049,5051,10103,
82 20201,40423,99991};
83
84 oop SystemDictionary::_system_loader_lock_obj = NULL;
85
86 Klass* SystemDictionary::_well_known_klasses[SystemDictionary::WKID_LIMIT]
87 = { NULL /*, NULL...*/ };
88
89 Klass* SystemDictionary::_box_klasses[T_VOID+1] = { NULL /*, NULL...*/ };
581 // This logic has the current thread wait once it has done
582 // all the superclass/superinterface loading it can, until
583 // the original thread completes the class loading or fails
584 // If it completes we will use the resulting InstanceKlass
585 // which we will find below in the systemDictionary.
586 // We also get here for parallel bootstrap classloader
587 if (class_loader.is_null()) {
588 SystemDictionary_lock->wait();
589 } else {
590 double_lock_wait(lockObject, THREAD);
591 }
592 } else {
593 // If not in SD and not in PH, other thread's load must have failed
594 super_load_in_progress = false;
595 }
596 }
597 }
598 return (nh);
599 }
600
601
602 Klass* SystemDictionary::resolve_instance_class_or_null(Symbol* name,
603 Handle class_loader,
604 Handle protection_domain,
605 TRAPS) {
606 assert(name != NULL && !FieldType::is_array(name) &&
607 !FieldType::is_obj(name), "invalid class name");
608
609 Ticks class_load_start_time = Ticks::now();
610
611 // UseNewReflection
612 // Fix for 4474172; see evaluation for more details
613 class_loader = Handle(THREAD, java_lang_ClassLoader::non_reflection_class_loader(class_loader()));
614 ClassLoaderData *loader_data = register_loader(class_loader, CHECK_NULL);
615
616 // Do lookup to see if class already exist and the protection domain
617 // has the right access
618 // This call uses find which checks protection domain already matches
619 // All subsequent calls use find_class, and set has_loaded_class so that
620 // before we return a result we call out to java to check for valid protection domain
621 // to allow returning the Klass* and add it to the pd_set if it is valid
622 unsigned int d_hash = dictionary()->compute_hash(name, loader_data);
623 int d_index = dictionary()->hash_to_index(d_hash);
624 Klass* probe = dictionary()->find(d_index, d_hash, name, loader_data,
625 protection_domain, THREAD);
626 if (probe != NULL) return probe;
627
628
629 // Non-bootstrap class loaders will call out to class loader and
831 JvmtiExport::post_class_load((JavaThread *) thread, k());
832 }
833 }
834 }
835 } // load_instance_class loop
836
837 if (load_instance_added == true) {
838 // clean up placeholder entries for LOAD_INSTANCE success or error
839 // This brackets the SystemDictionary updates for both defining
840 // and initiating loaders
841 MutexLocker mu(SystemDictionary_lock, THREAD);
842 placeholders()->find_and_remove(p_index, p_hash, name, loader_data, PlaceholderTable::LOAD_INSTANCE, THREAD);
843 SystemDictionary_lock->notify_all();
844 }
845 }
846
847 if (HAS_PENDING_EXCEPTION || k.is_null()) {
848 return NULL;
849 }
850
851 post_class_load_event(class_load_start_time, k, class_loader);
852
853 #ifdef ASSERT
854 {
855 ClassLoaderData* loader_data = k->class_loader_data();
856 MutexLocker mu(SystemDictionary_lock, THREAD);
857 Klass* kk = find_class(name, loader_data);
858 assert(kk == k(), "should be present in dictionary");
859 }
860 #endif
861
862 // return if the protection domain in NULL
863 if (protection_domain() == NULL) return k();
864
865 // Check the protection domain has the right access
866 {
867 MutexLocker mu(SystemDictionary_lock, THREAD);
868 // Note that we have an entry, and entries can be deleted only during GC,
869 // so we cannot allow GC to occur while we're holding this entry.
870 // We're using a No_Safepoint_Verifier to catch any place where we
871 // might potentially do a GC at all.
956 k = k->array_klass_or_null(fd.dimension());
957 }
958 } else {
959 k = find(class_name, class_loader, protection_domain, THREAD);
960 }
961 return k;
962 }
963
964 // Note: this method is much like resolve_from_stream, but
965 // updates no supplemental data structures.
966 // TODO consolidate the two methods with a helper routine?
967 Klass* SystemDictionary::parse_stream(Symbol* class_name,
968 Handle class_loader,
969 Handle protection_domain,
970 ClassFileStream* st,
971 KlassHandle host_klass,
972 GrowableArray<Handle>* cp_patches,
973 TRAPS) {
974 TempNewSymbol parsed_name = NULL;
975
976 Ticks class_load_start_time = Ticks::now();
977
978 ClassLoaderData* loader_data;
979 if (host_klass.not_null()) {
980 // Create a new CLD for anonymous class, that uses the same class loader
981 // as the host_klass
982 assert(EnableInvokeDynamic, "");
983 guarantee(host_klass->class_loader() == class_loader(), "should be the same");
984 guarantee(!DumpSharedSpaces, "must not create anonymous classes when dumping");
985 loader_data = ClassLoaderData::anonymous_class_loader_data(class_loader(), CHECK_NULL);
986 loader_data->record_dependency(host_klass(), CHECK_NULL);
987 } else {
988 loader_data = ClassLoaderData::class_loader_data(class_loader());
989 }
990
991 // Parse the stream. Note that we do this even though this klass might
992 // already be present in the SystemDictionary, otherwise we would not
993 // throw potential ClassFormatErrors.
994 //
995 // Note: "name" is updated.
996
1017
1018 // But, do not add to system dictionary.
1019
1020 // compiled code dependencies need to be validated anyway
1021 notice_modification();
1022 }
1023
1024 // Rewrite and patch constant pool here.
1025 k->link_class(CHECK_NULL);
1026 if (cp_patches != NULL) {
1027 k->constants()->patch_resolved_references(cp_patches);
1028 }
1029 k->eager_initialize(CHECK_NULL);
1030
1031 // notify jvmti
1032 if (JvmtiExport::should_post_class_load()) {
1033 assert(THREAD->is_Java_thread(), "thread->is_Java_thread()");
1034 JvmtiExport::post_class_load((JavaThread *) THREAD, k());
1035 }
1036
1037 post_class_load_event(class_load_start_time, k, class_loader);
1038 }
1039 assert(host_klass.not_null() || cp_patches == NULL,
1040 "cp_patches only found with host_klass");
1041
1042 return k();
1043 }
1044
1045 // Add a klass to the system from a stream (called by jni_DefineClass and
1046 // JVM_DefineClass).
1047 // Note: class_name can be NULL. In that case we do not know the name of
1048 // the class until we have parsed the stream.
1049
1050 Klass* SystemDictionary::resolve_from_stream(Symbol* class_name,
1051 Handle class_loader,
1052 Handle protection_domain,
1053 ClassFileStream* st,
1054 bool verify,
1055 TRAPS) {
1056
1057 // Classloaders that support parallelism, e.g. bootstrap classloader,
1059 bool DoObjectLock = true;
1060 if (is_parallelCapable(class_loader)) {
1061 DoObjectLock = false;
1062 }
1063
1064 ClassLoaderData* loader_data = register_loader(class_loader, CHECK_NULL);
1065
1066 // Make sure we are synchronized on the class loader before we proceed
1067 Handle lockObject = compute_loader_lock_object(class_loader, THREAD);
1068 check_loader_lock_contention(lockObject, THREAD);
1069 ObjectLocker ol(lockObject, THREAD, DoObjectLock);
1070
1071 TempNewSymbol parsed_name = NULL;
1072
1073 // Parse the stream. Note that we do this even though this klass might
1074 // already be present in the SystemDictionary, otherwise we would not
1075 // throw potential ClassFormatErrors.
1076 //
1077 // Note: "name" is updated.
1078
1079 instanceKlassHandle k = ClassFileParser(st).parseClassFile(class_name,
1080 loader_data,
1081 protection_domain,
1082 parsed_name,
1083 verify,
1084 THREAD);
1085
1086 const char* pkg = "java/";
1087 size_t pkglen = strlen(pkg);
1088 if (!HAS_PENDING_EXCEPTION &&
1089 !class_loader.is_null() &&
1090 parsed_name != NULL &&
1091 parsed_name->utf8_length() >= (int)pkglen &&
1092 !strncmp((const char*)parsed_name->bytes(), pkg, pkglen)) {
1093 // It is illegal to define classes in the "java." package from
1094 // JVM_DefineClass or jni_DefineClass unless you're the bootclassloader
1095 ResourceMark rm(THREAD);
1096 char* name = parsed_name->as_C_string();
1097 char* index = strrchr(name, '/');
1098 assert(index != NULL, "must be");
1099 *index = '\0'; // chop to just the package name
1100 while ((index = strchr(name, '/')) != NULL) {
1101 *index = '.'; // replace '/' with '.' in package name
1102 }
1103 const char* fmt = "Prohibited package name: %s";
1104 size_t len = strlen(fmt) + strlen(name);
1105 char* message = NEW_RESOURCE_ARRAY(char, len);
1106 jio_snprintf(message, len, fmt, name);
1107 Exceptions::_throw_msg(THREAD_AND_LOCATION,
1108 vmSymbols::java_lang_SecurityException(), message);
1109 }
1110
1111 if (!HAS_PENDING_EXCEPTION) {
1112 assert(parsed_name != NULL, "Sanity");
1113 assert(class_name == NULL || class_name == parsed_name, "name mismatch");
1114 // Verification prevents us from creating names with dots in them, this
1115 // asserts that that's the case.
1116 assert(is_internal_format(parsed_name),
1117 "external class name format used internally");
1118
1119 // Add class just loaded
1120 // If a class loader supports parallel classloading handle parallel define requests
1121 // find_or_define_instance_class may return a different InstanceKlass
1122 if (is_parallelCapable(class_loader)) {
1123 k = find_or_define_instance_class(class_name, class_loader, k, THREAD);
1124 } else {
1125 define_instance_class(k, THREAD);
1126 }
1127 }
1128
1129 // Make sure we have an entry in the SystemDictionary on success
1130 debug_only( {
1131 if (!HAS_PENDING_EXCEPTION) {
1132 assert(parsed_name != NULL, "parsed_name is still null?");
1133 Symbol* h_name = k->name();
1134 ClassLoaderData *defining_loader_data = k->class_loader_data();
1135
1136 MutexLocker mu(SystemDictionary_lock, THREAD);
1137
1138 Klass* check = find_class(parsed_name, loader_data);
1359 assert(result.get_type() == T_OBJECT, "just checking");
1360 oop obj = (oop) result.get_jobject();
1361
1362 // Primitive classes return null since forName() can not be
1363 // used to obtain any of the Class objects representing primitives or void
1364 if ((obj != NULL) && !(java_lang_Class::is_primitive(obj))) {
1365 instanceKlassHandle k =
1366 instanceKlassHandle(THREAD, java_lang_Class::as_Klass(obj));
1367 // For user defined Java class loaders, check that the name returned is
1368 // the same as that requested. This check is done for the bootstrap
1369 // loader when parsing the class file.
1370 if (class_name == k->name()) {
1371 return k;
1372 }
1373 }
1374 // Class is not found or has the wrong name, return NULL
1375 return nh;
1376 }
1377 }
1378
1379 void SystemDictionary::define_instance_class(instanceKlassHandle k, TRAPS) {
1380
1381 ClassLoaderData* loader_data = k->class_loader_data();
1382 Handle class_loader_h(THREAD, loader_data->class_loader());
1383
1384 for (uintx it = 0; it < GCExpandToAllocateDelayMillis; it++){}
1385
1386 // for bootstrap and other parallel classloaders don't acquire lock,
1387 // use placeholder token
1388 // If a parallelCapable class loader calls define_instance_class instead of
1389 // find_or_define_instance_class to get here, we have a timing
1390 // hole with systemDictionary updates and check_constraints
1391 if (!class_loader_h.is_null() && !is_parallelCapable(class_loader_h)) {
1392 assert(ObjectSynchronizer::current_thread_holds_lock((JavaThread*)THREAD,
1393 compute_loader_lock_object(class_loader_h, THREAD)),
1394 "define called without lock");
1395 }
1396
1397 // Check class-loading constraints. Throw exception if violation is detected.
1398 // Grabs and releases SystemDictionary_lock
1429 MutexLocker mu_r(Compile_lock, THREAD);
1430
1431 // Add to class hierarchy, initialize vtables, and do possible
1432 // deoptimizations.
1433 add_to_hierarchy(k, CHECK); // No exception, but can block
1434
1435 // Add to systemDictionary - so other classes can see it.
1436 // Grabs and releases SystemDictionary_lock
1437 update_dictionary(d_index, d_hash, p_index, p_hash,
1438 k, class_loader_h, THREAD);
1439 }
1440 k->eager_initialize(THREAD);
1441
1442 // notify jvmti
1443 if (JvmtiExport::should_post_class_load()) {
1444 assert(THREAD->is_Java_thread(), "thread->is_Java_thread()");
1445 JvmtiExport::post_class_load((JavaThread *) THREAD, k());
1446
1447 }
1448
1449 }
1450
1451 // Support parallel classloading
1452 // All parallel class loaders, including bootstrap classloader
1453 // lock a placeholder entry for this class/class_loader pair
1454 // to allow parallel defines of different classes for this class loader
1455 // With AllowParallelDefine flag==true, in case they do not synchronize around
1456 // FindLoadedClass/DefineClass, calls, we check for parallel
1457 // loading for them, wait if a defineClass is in progress
1458 // and return the initial requestor's results
1459 // This flag does not apply to the bootstrap classloader.
1460 // With AllowParallelDefine flag==false, call through to define_instance_class
1461 // which will throw LinkageError: duplicate class definition.
1462 // False is the requested default.
1463 // For better performance, the class loaders should synchronize
1464 // findClass(), i.e. FindLoadedClass/DefineClassIfAbsent or they
1465 // potentially waste time reading and parsing the bytestream.
1466 // Note: VM callers should ensure consistency of k/class_name,class_loader
1467 instanceKlassHandle SystemDictionary::find_or_define_instance_class(Symbol* class_name, Handle class_loader, instanceKlassHandle k, TRAPS) {
1468
2659 }
2660
2661
2662 void SystemDictionary::verify() {
2663 guarantee(dictionary() != NULL, "Verify of system dictionary failed");
2664 guarantee(constraints() != NULL,
2665 "Verify of loader constraints failed");
2666 guarantee(dictionary()->number_of_entries() >= 0 &&
2667 placeholders()->number_of_entries() >= 0,
2668 "Verify of system dictionary failed");
2669
2670 // Verify dictionary
2671 dictionary()->verify();
2672
2673 GCMutexLocker mu(SystemDictionary_lock);
2674 placeholders()->verify();
2675
2676 // Verify constraint table
2677 guarantee(constraints() != NULL, "Verify of loader constraints failed");
2678 constraints()->verify(dictionary(), placeholders());
2679 }
2680
2681 // utility function for class load event
2682 void SystemDictionary::post_class_load_event(const Ticks& start_time,
2683 instanceKlassHandle k,
2684 Handle initiating_loader) {
2685 #if INCLUDE_TRACE
2686 EventClassLoad event(UNTIMED);
2687 if (event.should_commit()) {
2688 event.set_starttime(start_time);
2689 event.set_loadedClass(k());
2690 oop defining_class_loader = k->class_loader();
2691 event.set_definingClassLoader(defining_class_loader != NULL ?
2692 defining_class_loader->klass() : (Klass*)NULL);
2693 oop class_loader = initiating_loader.is_null() ? (oop)NULL : initiating_loader();
2694 event.set_initiatingClassLoader(class_loader != NULL ?
2695 class_loader->klass() : (Klass*)NULL);
2696 event.commit();
2697 }
2698 #endif // INCLUDE_TRACE
2699 }
2700
2701 #ifndef PRODUCT
2702
2703 // statistics code
2704 class ClassStatistics: AllStatic {
2705 private:
2706 static int nclasses; // number of classes
2707 static int nmethods; // number of methods
2708 static int nmethoddata; // number of methodData
2709 static int class_size; // size of class objects in words
2710 static int method_size; // size of method objects in words
2711 static int debug_size; // size of debug info in methods
2712 static int methoddata_size; // size of methodData objects in words
2713
2714 static void do_class(Klass* k) {
2715 nclasses++;
2716 class_size += k->size();
2717 if (k->oop_is_instance()) {
2718 InstanceKlass* ik = (InstanceKlass*)k;
|
21 * questions.
22 *
23 */
24
25 #include "precompiled.hpp"
26 #include "classfile/classLoaderData.inline.hpp"
27 #include "classfile/dictionary.hpp"
28 #include "classfile/javaClasses.hpp"
29 #include "classfile/loaderConstraints.hpp"
30 #include "classfile/placeholders.hpp"
31 #include "classfile/resolutionErrors.hpp"
32 #include "classfile/systemDictionary.hpp"
33 #if INCLUDE_CDS
34 #include "classfile/sharedClassUtil.hpp"
35 #include "classfile/systemDictionaryShared.hpp"
36 #endif
37 #include "classfile/vmSymbols.hpp"
38 #include "compiler/compileBroker.hpp"
39 #include "interpreter/bytecodeStream.hpp"
40 #include "interpreter/interpreter.hpp"
41 #include "jfr/jfrEvents.hpp"
42 #include "memory/filemap.hpp"
43 #include "memory/gcLocker.hpp"
44 #include "memory/oopFactory.hpp"
45 #include "oops/instanceKlass.hpp"
46 #include "oops/instanceRefKlass.hpp"
47 #include "oops/klass.inline.hpp"
48 #include "oops/methodData.hpp"
49 #include "oops/objArrayKlass.hpp"
50 #include "oops/oop.inline.hpp"
51 #include "oops/oop.inline2.hpp"
52 #include "oops/typeArrayKlass.hpp"
53 #include "prims/jvmtiEnvBase.hpp"
54 #include "prims/methodHandles.hpp"
55 #include "runtime/arguments.hpp"
56 #include "runtime/biasedLocking.hpp"
57 #include "runtime/fieldType.hpp"
58 #include "runtime/handles.inline.hpp"
59 #include "runtime/java.hpp"
60 #include "runtime/javaCalls.hpp"
61 #include "runtime/mutexLocker.hpp"
62 #include "runtime/orderAccess.inline.hpp"
63 #include "runtime/signature.hpp"
64 #include "services/classLoadingService.hpp"
65 #include "services/threadService.hpp"
66 #include "utilities/macros.hpp"
67 #include "utilities/ticks.hpp"
68
69 Dictionary* SystemDictionary::_dictionary = NULL;
70 PlaceholderTable* SystemDictionary::_placeholders = NULL;
71 Dictionary* SystemDictionary::_shared_dictionary = NULL;
72 LoaderConstraintTable* SystemDictionary::_loader_constraints = NULL;
73 ResolutionErrorTable* SystemDictionary::_resolution_errors = NULL;
74 SymbolPropertyTable* SystemDictionary::_invoke_method_table = NULL;
75
76
77 int SystemDictionary::_number_of_modifications = 0;
78 int SystemDictionary::_sdgeneration = 0;
79 const int SystemDictionary::_primelist[_prime_array_size] = {1009,2017,4049,5051,10103,
80 20201,40423,99991};
81
82 oop SystemDictionary::_system_loader_lock_obj = NULL;
83
84 Klass* SystemDictionary::_well_known_klasses[SystemDictionary::WKID_LIMIT]
85 = { NULL /*, NULL...*/ };
86
87 Klass* SystemDictionary::_box_klasses[T_VOID+1] = { NULL /*, NULL...*/ };
579 // This logic has the current thread wait once it has done
580 // all the superclass/superinterface loading it can, until
581 // the original thread completes the class loading or fails
582 // If it completes we will use the resulting InstanceKlass
583 // which we will find below in the systemDictionary.
584 // We also get here for parallel bootstrap classloader
585 if (class_loader.is_null()) {
586 SystemDictionary_lock->wait();
587 } else {
588 double_lock_wait(lockObject, THREAD);
589 }
590 } else {
591 // If not in SD and not in PH, other thread's load must have failed
592 super_load_in_progress = false;
593 }
594 }
595 }
596 return (nh);
597 }
598
599 // utility function for class load event
600 static void post_class_load_event(EventClassLoad &event,
601 instanceKlassHandle k,
602 Handle initiating_loader) {
603 #if INCLUDE_JFR
604 if (event.should_commit()) {
605 event.set_loadedClass(k());
606 event.set_definingClassLoader(k->class_loader_data());
607 oop class_loader = initiating_loader.is_null() ? (oop)NULL : initiating_loader();
608 event.set_initiatingClassLoader(class_loader != NULL ?
609 ClassLoaderData::class_loader_data_or_null(class_loader) :
610 (ClassLoaderData*)NULL);
611 event.commit();
612 }
613 #endif // INCLUDE_JFR
614 }
615
616 Klass* SystemDictionary::resolve_instance_class_or_null(Symbol* name,
617 Handle class_loader,
618 Handle protection_domain,
619 TRAPS) {
620 assert(name != NULL && !FieldType::is_array(name) &&
621 !FieldType::is_obj(name), "invalid class name");
622
623 EventClassLoad class_load_start_event;
624
625 // UseNewReflection
626 // Fix for 4474172; see evaluation for more details
627 class_loader = Handle(THREAD, java_lang_ClassLoader::non_reflection_class_loader(class_loader()));
628 ClassLoaderData *loader_data = register_loader(class_loader, CHECK_NULL);
629
630 // Do lookup to see if class already exist and the protection domain
631 // has the right access
632 // This call uses find which checks protection domain already matches
633 // All subsequent calls use find_class, and set has_loaded_class so that
634 // before we return a result we call out to java to check for valid protection domain
635 // to allow returning the Klass* and add it to the pd_set if it is valid
636 unsigned int d_hash = dictionary()->compute_hash(name, loader_data);
637 int d_index = dictionary()->hash_to_index(d_hash);
638 Klass* probe = dictionary()->find(d_index, d_hash, name, loader_data,
639 protection_domain, THREAD);
640 if (probe != NULL) return probe;
641
642
643 // Non-bootstrap class loaders will call out to class loader and
845 JvmtiExport::post_class_load((JavaThread *) thread, k());
846 }
847 }
848 }
849 } // load_instance_class loop
850
851 if (load_instance_added == true) {
852 // clean up placeholder entries for LOAD_INSTANCE success or error
853 // This brackets the SystemDictionary updates for both defining
854 // and initiating loaders
855 MutexLocker mu(SystemDictionary_lock, THREAD);
856 placeholders()->find_and_remove(p_index, p_hash, name, loader_data, PlaceholderTable::LOAD_INSTANCE, THREAD);
857 SystemDictionary_lock->notify_all();
858 }
859 }
860
861 if (HAS_PENDING_EXCEPTION || k.is_null()) {
862 return NULL;
863 }
864
865 post_class_load_event(class_load_start_event, k, class_loader);
866
867 #ifdef ASSERT
868 {
869 ClassLoaderData* loader_data = k->class_loader_data();
870 MutexLocker mu(SystemDictionary_lock, THREAD);
871 Klass* kk = find_class(name, loader_data);
872 assert(kk == k(), "should be present in dictionary");
873 }
874 #endif
875
876 // return if the protection domain in NULL
877 if (protection_domain() == NULL) return k();
878
879 // Check the protection domain has the right access
880 {
881 MutexLocker mu(SystemDictionary_lock, THREAD);
882 // Note that we have an entry, and entries can be deleted only during GC,
883 // so we cannot allow GC to occur while we're holding this entry.
884 // We're using a No_Safepoint_Verifier to catch any place where we
885 // might potentially do a GC at all.
970 k = k->array_klass_or_null(fd.dimension());
971 }
972 } else {
973 k = find(class_name, class_loader, protection_domain, THREAD);
974 }
975 return k;
976 }
977
978 // Note: this method is much like resolve_from_stream, but
979 // updates no supplemental data structures.
980 // TODO consolidate the two methods with a helper routine?
981 Klass* SystemDictionary::parse_stream(Symbol* class_name,
982 Handle class_loader,
983 Handle protection_domain,
984 ClassFileStream* st,
985 KlassHandle host_klass,
986 GrowableArray<Handle>* cp_patches,
987 TRAPS) {
988 TempNewSymbol parsed_name = NULL;
989
990 EventClassLoad class_load_start_event;
991
992 ClassLoaderData* loader_data;
993 if (host_klass.not_null()) {
994 // Create a new CLD for anonymous class, that uses the same class loader
995 // as the host_klass
996 assert(EnableInvokeDynamic, "");
997 guarantee(host_klass->class_loader() == class_loader(), "should be the same");
998 guarantee(!DumpSharedSpaces, "must not create anonymous classes when dumping");
999 loader_data = ClassLoaderData::anonymous_class_loader_data(class_loader(), CHECK_NULL);
1000 loader_data->record_dependency(host_klass(), CHECK_NULL);
1001 } else {
1002 loader_data = ClassLoaderData::class_loader_data(class_loader());
1003 }
1004
1005 // Parse the stream. Note that we do this even though this klass might
1006 // already be present in the SystemDictionary, otherwise we would not
1007 // throw potential ClassFormatErrors.
1008 //
1009 // Note: "name" is updated.
1010
1031
1032 // But, do not add to system dictionary.
1033
1034 // compiled code dependencies need to be validated anyway
1035 notice_modification();
1036 }
1037
1038 // Rewrite and patch constant pool here.
1039 k->link_class(CHECK_NULL);
1040 if (cp_patches != NULL) {
1041 k->constants()->patch_resolved_references(cp_patches);
1042 }
1043 k->eager_initialize(CHECK_NULL);
1044
1045 // notify jvmti
1046 if (JvmtiExport::should_post_class_load()) {
1047 assert(THREAD->is_Java_thread(), "thread->is_Java_thread()");
1048 JvmtiExport::post_class_load((JavaThread *) THREAD, k());
1049 }
1050
1051 post_class_load_event(class_load_start_event, k, class_loader);
1052 }
1053 assert(host_klass.not_null() || cp_patches == NULL,
1054 "cp_patches only found with host_klass");
1055
1056 return k();
1057 }
1058
1059 // Add a klass to the system from a stream (called by jni_DefineClass and
1060 // JVM_DefineClass).
1061 // Note: class_name can be NULL. In that case we do not know the name of
1062 // the class until we have parsed the stream.
1063
1064 Klass* SystemDictionary::resolve_from_stream(Symbol* class_name,
1065 Handle class_loader,
1066 Handle protection_domain,
1067 ClassFileStream* st,
1068 bool verify,
1069 TRAPS) {
1070
1071 // Classloaders that support parallelism, e.g. bootstrap classloader,
1073 bool DoObjectLock = true;
1074 if (is_parallelCapable(class_loader)) {
1075 DoObjectLock = false;
1076 }
1077
1078 ClassLoaderData* loader_data = register_loader(class_loader, CHECK_NULL);
1079
1080 // Make sure we are synchronized on the class loader before we proceed
1081 Handle lockObject = compute_loader_lock_object(class_loader, THREAD);
1082 check_loader_lock_contention(lockObject, THREAD);
1083 ObjectLocker ol(lockObject, THREAD, DoObjectLock);
1084
1085 TempNewSymbol parsed_name = NULL;
1086
1087 // Parse the stream. Note that we do this even though this klass might
1088 // already be present in the SystemDictionary, otherwise we would not
1089 // throw potential ClassFormatErrors.
1090 //
1091 // Note: "name" is updated.
1092
1093 ClassFileParser parser(st);
1094 instanceKlassHandle k = parser.parseClassFile(class_name,
1095 loader_data,
1096 protection_domain,
1097 parsed_name,
1098 verify,
1099 THREAD);
1100
1101 const char* pkg = "java/";
1102 size_t pkglen = strlen(pkg);
1103 if (!HAS_PENDING_EXCEPTION &&
1104 !class_loader.is_null() &&
1105 parsed_name != NULL &&
1106 parsed_name->utf8_length() >= (int)pkglen &&
1107 !strncmp((const char*)parsed_name->bytes(), pkg, pkglen)) {
1108 // It is illegal to define classes in the "java." package from
1109 // JVM_DefineClass or jni_DefineClass unless you're the bootclassloader
1110 ResourceMark rm(THREAD);
1111 char* name = parsed_name->as_C_string();
1112 char* index = strrchr(name, '/');
1113 assert(index != NULL, "must be");
1114 *index = '\0'; // chop to just the package name
1115 while ((index = strchr(name, '/')) != NULL) {
1116 *index = '.'; // replace '/' with '.' in package name
1117 }
1118 const char* fmt = "Prohibited package name: %s";
1119 size_t len = strlen(fmt) + strlen(name);
1120 char* message = NEW_RESOURCE_ARRAY(char, len);
1121 jio_snprintf(message, len, fmt, name);
1122 Exceptions::_throw_msg(THREAD_AND_LOCATION,
1123 vmSymbols::java_lang_SecurityException(), message);
1124 }
1125
1126 if (!HAS_PENDING_EXCEPTION) {
1127 assert(parsed_name != NULL, "Sanity");
1128 assert(class_name == NULL || class_name == parsed_name, "name mismatch");
1129 // Verification prevents us from creating names with dots in them, this
1130 // asserts that that's the case.
1131 assert(is_internal_format(parsed_name),
1132 "external class name format used internally");
1133
1134 #if INCLUDE_JFR
1135 {
1136 InstanceKlass* ik = k();
1137 ON_KLASS_CREATION(ik, parser, THREAD);
1138 k = instanceKlassHandle(ik);
1139 }
1140 #endif
1141
1142 // Add class just loaded
1143 // If a class loader supports parallel classloading handle parallel define requests
1144 // find_or_define_instance_class may return a different InstanceKlass
1145 if (is_parallelCapable(class_loader)) {
1146 k = find_or_define_instance_class(class_name, class_loader, k, THREAD);
1147 } else {
1148 define_instance_class(k, THREAD);
1149 }
1150 }
1151
1152 // Make sure we have an entry in the SystemDictionary on success
1153 debug_only( {
1154 if (!HAS_PENDING_EXCEPTION) {
1155 assert(parsed_name != NULL, "parsed_name is still null?");
1156 Symbol* h_name = k->name();
1157 ClassLoaderData *defining_loader_data = k->class_loader_data();
1158
1159 MutexLocker mu(SystemDictionary_lock, THREAD);
1160
1161 Klass* check = find_class(parsed_name, loader_data);
1382 assert(result.get_type() == T_OBJECT, "just checking");
1383 oop obj = (oop) result.get_jobject();
1384
1385 // Primitive classes return null since forName() can not be
1386 // used to obtain any of the Class objects representing primitives or void
1387 if ((obj != NULL) && !(java_lang_Class::is_primitive(obj))) {
1388 instanceKlassHandle k =
1389 instanceKlassHandle(THREAD, java_lang_Class::as_Klass(obj));
1390 // For user defined Java class loaders, check that the name returned is
1391 // the same as that requested. This check is done for the bootstrap
1392 // loader when parsing the class file.
1393 if (class_name == k->name()) {
1394 return k;
1395 }
1396 }
1397 // Class is not found or has the wrong name, return NULL
1398 return nh;
1399 }
1400 }
1401
1402 static void post_class_define_event(InstanceKlass* k, const ClassLoaderData* def_cld) {
1403 EventClassDefine event;
1404 if (event.should_commit()) {
1405 event.set_definedClass(k);
1406 event.set_definingClassLoader(def_cld);
1407 event.commit();
1408 }
1409 }
1410
1411 void SystemDictionary::define_instance_class(instanceKlassHandle k, TRAPS) {
1412
1413 ClassLoaderData* loader_data = k->class_loader_data();
1414 Handle class_loader_h(THREAD, loader_data->class_loader());
1415
1416 for (uintx it = 0; it < GCExpandToAllocateDelayMillis; it++){}
1417
1418 // for bootstrap and other parallel classloaders don't acquire lock,
1419 // use placeholder token
1420 // If a parallelCapable class loader calls define_instance_class instead of
1421 // find_or_define_instance_class to get here, we have a timing
1422 // hole with systemDictionary updates and check_constraints
1423 if (!class_loader_h.is_null() && !is_parallelCapable(class_loader_h)) {
1424 assert(ObjectSynchronizer::current_thread_holds_lock((JavaThread*)THREAD,
1425 compute_loader_lock_object(class_loader_h, THREAD)),
1426 "define called without lock");
1427 }
1428
1429 // Check class-loading constraints. Throw exception if violation is detected.
1430 // Grabs and releases SystemDictionary_lock
1461 MutexLocker mu_r(Compile_lock, THREAD);
1462
1463 // Add to class hierarchy, initialize vtables, and do possible
1464 // deoptimizations.
1465 add_to_hierarchy(k, CHECK); // No exception, but can block
1466
1467 // Add to systemDictionary - so other classes can see it.
1468 // Grabs and releases SystemDictionary_lock
1469 update_dictionary(d_index, d_hash, p_index, p_hash,
1470 k, class_loader_h, THREAD);
1471 }
1472 k->eager_initialize(THREAD);
1473
1474 // notify jvmti
1475 if (JvmtiExport::should_post_class_load()) {
1476 assert(THREAD->is_Java_thread(), "thread->is_Java_thread()");
1477 JvmtiExport::post_class_load((JavaThread *) THREAD, k());
1478
1479 }
1480
1481 post_class_define_event(k(), loader_data);
1482 }
1483
1484 // Support parallel classloading
1485 // All parallel class loaders, including bootstrap classloader
1486 // lock a placeholder entry for this class/class_loader pair
1487 // to allow parallel defines of different classes for this class loader
1488 // With AllowParallelDefine flag==true, in case they do not synchronize around
1489 // FindLoadedClass/DefineClass, calls, we check for parallel
1490 // loading for them, wait if a defineClass is in progress
1491 // and return the initial requestor's results
1492 // This flag does not apply to the bootstrap classloader.
1493 // With AllowParallelDefine flag==false, call through to define_instance_class
1494 // which will throw LinkageError: duplicate class definition.
1495 // False is the requested default.
1496 // For better performance, the class loaders should synchronize
1497 // findClass(), i.e. FindLoadedClass/DefineClassIfAbsent or they
1498 // potentially waste time reading and parsing the bytestream.
1499 // Note: VM callers should ensure consistency of k/class_name,class_loader
1500 instanceKlassHandle SystemDictionary::find_or_define_instance_class(Symbol* class_name, Handle class_loader, instanceKlassHandle k, TRAPS) {
1501
2692 }
2693
2694
2695 void SystemDictionary::verify() {
2696 guarantee(dictionary() != NULL, "Verify of system dictionary failed");
2697 guarantee(constraints() != NULL,
2698 "Verify of loader constraints failed");
2699 guarantee(dictionary()->number_of_entries() >= 0 &&
2700 placeholders()->number_of_entries() >= 0,
2701 "Verify of system dictionary failed");
2702
2703 // Verify dictionary
2704 dictionary()->verify();
2705
2706 GCMutexLocker mu(SystemDictionary_lock);
2707 placeholders()->verify();
2708
2709 // Verify constraint table
2710 guarantee(constraints() != NULL, "Verify of loader constraints failed");
2711 constraints()->verify(dictionary(), placeholders());
2712 }
2713
2714 #ifndef PRODUCT
2715
2716 // statistics code
2717 class ClassStatistics: AllStatic {
2718 private:
2719 static int nclasses; // number of classes
2720 static int nmethods; // number of methods
2721 static int nmethoddata; // number of methodData
2722 static int class_size; // size of class objects in words
2723 static int method_size; // size of method objects in words
2724 static int debug_size; // size of debug info in methods
2725 static int methoddata_size; // size of methodData objects in words
2726
2727 static void do_class(Klass* k) {
2728 nclasses++;
2729 class_size += k->size();
2730 if (k->oop_is_instance()) {
2731 InstanceKlass* ik = (InstanceKlass*)k;
|