src/share/vm/classfile/systemDictionary.cpp

Print this page
rev 13113 : 8182651: Add TRACE_ONLY conditional macro to support more fine-grained INCLUDE_TRACE programming
Reviewed-by:


  55 #include "oops/methodData.hpp"
  56 #include "oops/objArrayKlass.hpp"
  57 #include "oops/objArrayOop.inline.hpp"
  58 #include "oops/oop.inline.hpp"
  59 #include "oops/symbol.hpp"
  60 #include "oops/typeArrayKlass.hpp"
  61 #include "prims/jvmtiEnvBase.hpp"
  62 #include "prims/resolvedMethodTable.hpp"
  63 #include "prims/methodHandles.hpp"
  64 #include "runtime/arguments.hpp"
  65 #include "runtime/biasedLocking.hpp"
  66 #include "runtime/fieldType.hpp"
  67 #include "runtime/handles.inline.hpp"
  68 #include "runtime/java.hpp"
  69 #include "runtime/javaCalls.hpp"
  70 #include "runtime/mutexLocker.hpp"
  71 #include "runtime/orderAccess.inline.hpp"
  72 #include "runtime/signature.hpp"
  73 #include "services/classLoadingService.hpp"
  74 #include "services/threadService.hpp"
  75 #include "trace/traceMacros.hpp"
  76 #include "utilities/macros.hpp"
  77 #include "utilities/ticks.hpp"
  78 #if INCLUDE_CDS
  79 #include "classfile/sharedClassUtil.hpp"
  80 #include "classfile/systemDictionaryShared.hpp"
  81 #endif
  82 #if INCLUDE_JVMCI
  83 #include "jvmci/jvmciRuntime.hpp"
  84 #endif
  85 #if INCLUDE_TRACE
  86 #include "trace/tracing.hpp"
  87 #endif
  88 
  89 Dictionary*            SystemDictionary::_dictionary          = NULL;
  90 PlaceholderTable*      SystemDictionary::_placeholders        = NULL;
  91 Dictionary*            SystemDictionary::_shared_dictionary   = NULL;
  92 LoaderConstraintTable* SystemDictionary::_loader_constraints  = NULL;
  93 ResolutionErrorTable*  SystemDictionary::_resolution_errors   = NULL;
  94 SymbolPropertyTable*   SystemDictionary::_invoke_method_table = NULL;
  95 
  96 
  97 int         SystemDictionary::_number_of_modifications = 0;


 598         // This logic has the current thread wait once it has done
 599         // all the superclass/superinterface loading it can, until
 600         // the original thread completes the class loading or fails
 601         // If it completes we will use the resulting InstanceKlass
 602         // which we will find below in the systemDictionary.
 603         // We also get here for parallel bootstrap classloader
 604         if (class_loader.is_null()) {
 605           SystemDictionary_lock->wait();
 606         } else {
 607           double_lock_wait(lockObject, THREAD);
 608         }
 609       } else {
 610         // If not in SD and not in PH, other thread's load must have failed
 611         super_load_in_progress = false;
 612       }
 613     }
 614   }
 615   return NULL;
 616 }
 617 
 618 static void post_class_load_event(const Ticks& start_time,
 619                                   InstanceKlass* k,
 620                                   const ClassLoaderData* init_cld) {
 621 #if INCLUDE_TRACE
 622   EventClassLoad event(UNTIMED);
 623   if (event.should_commit()) {
 624     event.set_starttime(start_time);
 625     event.set_loadedClass(k);
 626     event.set_definingClassLoader(k->class_loader_data());
 627     event.set_initiatingClassLoader(init_cld);
 628     event.commit();




 629   }
 630 #endif // INCLUDE_TRACE
 631 }
 632 
 633 static void class_define_event(InstanceKlass* k,
 634                                const ClassLoaderData* def_cld) {
 635 #if INCLUDE_TRACE
 636   EventClassDefine event;
 637   if (event.should_commit()) {
 638     event.set_definedClass(k);
 639     event.set_definingClassLoader(def_cld);
 640     event.commit();
 641   }
 642 #endif // INCLUDE_TRACE
 643 }

 644 
 645 // Be careful when modifying this code: once you have run
 646 // placeholders()->find_and_add(PlaceholderTable::LOAD_INSTANCE),
 647 // you need to find_and_remove it before returning.
 648 // So be careful to not exit with a CHECK_ macro betweeen these calls.
 649 Klass* SystemDictionary::resolve_instance_class_or_null(Symbol* name,
 650                                                         Handle class_loader,
 651                                                         Handle protection_domain,
 652                                                         TRAPS) {
 653   assert(name != NULL && !FieldType::is_array(name) &&
 654          !FieldType::is_obj(name), "invalid class name");
 655 
 656   Ticks class_load_start_time = Ticks::now();
 657 
 658   HandleMark hm(THREAD);
 659 
 660   // Fix for 4474172; see evaluation for more details
 661   class_loader = Handle(THREAD, java_lang_ClassLoader::non_reflection_class_loader(class_loader()));
 662   ClassLoaderData *loader_data = register_loader(class_loader, CHECK_NULL);
 663 
 664   // Do lookup to see if class already exist and the protection domain
 665   // has the right access
 666   // This call uses find which checks protection domain already matches
 667   // All subsequent calls use find_class, and set has_loaded_class so that
 668   // before we return a result we call out to java to check for valid protection domain
 669   // to allow returning the Klass* and add it to the pd_set if it is valid
 670   unsigned int d_hash = dictionary()->compute_hash(name, loader_data);
 671   int d_index = dictionary()->hash_to_index(d_hash);
 672   Klass* probe = dictionary()->find(d_index, d_hash, name, loader_data,
 673                                       protection_domain, THREAD);
 674   if (probe != NULL) return probe;
 675 
 676 


 882             JvmtiExport::post_class_load((JavaThread *) thread, k);
 883           }
 884         }
 885       }
 886     } // load_instance_class loop
 887 
 888     if (load_instance_added == true) {
 889       // clean up placeholder entries for LOAD_INSTANCE success or error
 890       // This brackets the SystemDictionary updates for both defining
 891       // and initiating loaders
 892       MutexLocker mu(SystemDictionary_lock, THREAD);
 893       placeholders()->find_and_remove(p_index, p_hash, name, loader_data, PlaceholderTable::LOAD_INSTANCE, THREAD);
 894       SystemDictionary_lock->notify_all();
 895     }
 896   }
 897 
 898   if (HAS_PENDING_EXCEPTION || k == NULL) {
 899     return NULL;
 900   }
 901 
 902   post_class_load_event(class_load_start_time, k, loader_data);
 903 
 904 #ifdef ASSERT
 905   {
 906     ClassLoaderData* loader_data = k->class_loader_data();
 907     MutexLocker mu(SystemDictionary_lock, THREAD);
 908     Klass* kk = find_class(name, loader_data);
 909     assert(kk == k, "should be present in dictionary");
 910   }
 911 #endif
 912 
 913   // return if the protection domain in NULL
 914   if (protection_domain() == NULL) return k;
 915 
 916   // Check the protection domain has the right access
 917   {
 918     MutexLocker mu(SystemDictionary_lock, THREAD);
 919     // Note that we have an entry, and entries can be deleted only during GC,
 920     // so we cannot allow GC to occur while we're holding this entry.
 921     // We're using a NoSafepointVerifier to catch any place where we
 922     // might potentially do a GC at all.


1005     if (k != NULL) {
1006       k = k->array_klass_or_null(fd.dimension());
1007     }
1008   } else {
1009     k = find(class_name, class_loader, protection_domain, THREAD);
1010   }
1011   return k;
1012 }
1013 
1014 // Note: this method is much like resolve_from_stream, but
1015 // does not publish the classes via the SystemDictionary.
1016 // Handles unsafe_DefineAnonymousClass and redefineclasses
1017 // RedefinedClasses do not add to the class hierarchy
1018 InstanceKlass* SystemDictionary::parse_stream(Symbol* class_name,
1019                                               Handle class_loader,
1020                                               Handle protection_domain,
1021                                               ClassFileStream* st,
1022                                               const InstanceKlass* host_klass,
1023                                               GrowableArray<Handle>* cp_patches,
1024                                               TRAPS) {
1025 
1026   Ticks class_load_start_time = Ticks::now();
1027 
1028   ClassLoaderData* loader_data;
1029   if (host_klass != NULL) {
1030     // Create a new CLD for anonymous class, that uses the same class loader
1031     // as the host_klass
1032     guarantee(host_klass->class_loader() == class_loader(), "should be the same");
1033     guarantee(!DumpSharedSpaces, "must not create anonymous classes when dumping");
1034     loader_data = ClassLoaderData::anonymous_class_loader_data(class_loader(), CHECK_NULL);
1035     loader_data->record_dependency(host_klass, CHECK_NULL);
1036   } else {
1037     loader_data = ClassLoaderData::class_loader_data(class_loader());
1038   }
1039 
1040   assert(st != NULL, "invariant");
1041   assert(st->need_verify(), "invariant");
1042 
1043   // Parse stream and create a klass.
1044   // Note that we do this even though this klass might
1045   // already be present in the SystemDictionary, otherwise we would not
1046   // throw potential ClassFormatErrors.


1065 
1066       // But, do not add to system dictionary.
1067 
1068       // compiled code dependencies need to be validated anyway
1069       notice_modification();
1070     }
1071 
1072     // Rewrite and patch constant pool here.
1073     k->link_class(CHECK_NULL);
1074     if (cp_patches != NULL) {
1075       k->constants()->patch_resolved_references(cp_patches);
1076     }
1077     k->eager_initialize(CHECK_NULL);
1078 
1079     // notify jvmti
1080     if (JvmtiExport::should_post_class_load()) {
1081         assert(THREAD->is_Java_thread(), "thread->is_Java_thread()");
1082         JvmtiExport::post_class_load((JavaThread *) THREAD, k);
1083     }
1084 
1085     post_class_load_event(class_load_start_time, k, loader_data);
1086   }
1087   assert(host_klass != NULL || NULL == cp_patches,
1088          "cp_patches only found with host_klass");
1089 
1090   return k;
1091 }
1092 
1093 // Add a klass to the system from a stream (called by jni_DefineClass and
1094 // JVM_DefineClass).
1095 // Note: class_name can be NULL. In that case we do not know the name of
1096 // the class until we have parsed the stream.
1097 
1098 InstanceKlass* SystemDictionary::resolve_from_stream(Symbol* class_name,
1099                                                      Handle class_loader,
1100                                                      Handle protection_domain,
1101                                                      ClassFileStream* st,
1102                                                      TRAPS) {
1103 
1104   HandleMark hm(THREAD);
1105 


1647 
1648     MutexLocker mu_r(Compile_lock, THREAD);
1649 
1650     // Add to class hierarchy, initialize vtables, and do possible
1651     // deoptimizations.
1652     add_to_hierarchy(k, CHECK); // No exception, but can block
1653 
1654     // Add to systemDictionary - so other classes can see it.
1655     // Grabs and releases SystemDictionary_lock
1656     update_dictionary(d_index, d_hash, p_index, p_hash,
1657                       k, class_loader_h, THREAD);
1658   }
1659   k->eager_initialize(THREAD);
1660 
1661   // notify jvmti
1662   if (JvmtiExport::should_post_class_load()) {
1663       assert(THREAD->is_Java_thread(), "thread->is_Java_thread()");
1664       JvmtiExport::post_class_load((JavaThread *) THREAD, k);
1665 
1666   }
1667   class_define_event(k, loader_data);
1668 }
1669 
1670 // Support parallel classloading
1671 // All parallel class loaders, including bootstrap classloader
1672 // lock a placeholder entry for this class/class_loader pair
1673 // to allow parallel defines of different classes for this class loader
1674 // With AllowParallelDefine flag==true, in case they do not synchronize around
1675 // FindLoadedClass/DefineClass, calls, we check for parallel
1676 // loading for them, wait if a defineClass is in progress
1677 // and return the initial requestor's results
1678 // This flag does not apply to the bootstrap classloader.
1679 // With AllowParallelDefine flag==false, call through to define_instance_class
1680 // which will throw LinkageError: duplicate class definition.
1681 // False is the requested default.
1682 // For better performance, the class loaders should synchronize
1683 // findClass(), i.e. FindLoadedClass/DefineClassIfAbsent or they
1684 // potentially waste time reading and parsing the bytestream.
1685 // Note: VM callers should ensure consistency of k/class_name,class_loader
1686 // Be careful when modifying this code: once you have run
1687 // placeholders()->find_and_add(PlaceholderTable::DEFINE_CLASS),




  55 #include "oops/methodData.hpp"
  56 #include "oops/objArrayKlass.hpp"
  57 #include "oops/objArrayOop.inline.hpp"
  58 #include "oops/oop.inline.hpp"
  59 #include "oops/symbol.hpp"
  60 #include "oops/typeArrayKlass.hpp"
  61 #include "prims/jvmtiEnvBase.hpp"
  62 #include "prims/resolvedMethodTable.hpp"
  63 #include "prims/methodHandles.hpp"
  64 #include "runtime/arguments.hpp"
  65 #include "runtime/biasedLocking.hpp"
  66 #include "runtime/fieldType.hpp"
  67 #include "runtime/handles.inline.hpp"
  68 #include "runtime/java.hpp"
  69 #include "runtime/javaCalls.hpp"
  70 #include "runtime/mutexLocker.hpp"
  71 #include "runtime/orderAccess.inline.hpp"
  72 #include "runtime/signature.hpp"
  73 #include "services/classLoadingService.hpp"
  74 #include "services/threadService.hpp"

  75 #include "utilities/macros.hpp"

  76 #if INCLUDE_CDS
  77 #include "classfile/sharedClassUtil.hpp"
  78 #include "classfile/systemDictionaryShared.hpp"
  79 #endif
  80 #if INCLUDE_JVMCI
  81 #include "jvmci/jvmciRuntime.hpp"
  82 #endif
  83 #if INCLUDE_TRACE
  84 #include "trace/tracing.hpp"
  85 #endif
  86 
  87 Dictionary*            SystemDictionary::_dictionary          = NULL;
  88 PlaceholderTable*      SystemDictionary::_placeholders        = NULL;
  89 Dictionary*            SystemDictionary::_shared_dictionary   = NULL;
  90 LoaderConstraintTable* SystemDictionary::_loader_constraints  = NULL;
  91 ResolutionErrorTable*  SystemDictionary::_resolution_errors   = NULL;
  92 SymbolPropertyTable*   SystemDictionary::_invoke_method_table = NULL;
  93 
  94 
  95 int         SystemDictionary::_number_of_modifications = 0;


 596         // This logic has the current thread wait once it has done
 597         // all the superclass/superinterface loading it can, until
 598         // the original thread completes the class loading or fails
 599         // If it completes we will use the resulting InstanceKlass
 600         // which we will find below in the systemDictionary.
 601         // We also get here for parallel bootstrap classloader
 602         if (class_loader.is_null()) {
 603           SystemDictionary_lock->wait();
 604         } else {
 605           double_lock_wait(lockObject, THREAD);
 606         }
 607       } else {
 608         // If not in SD and not in PH, other thread's load must have failed
 609         super_load_in_progress = false;
 610       }
 611     }
 612   }
 613   return NULL;
 614 }
 615 



 616 #if INCLUDE_TRACE
 617 static void post_class_load_event(EventClassLoad* event,
 618                                   const InstanceKlass* k,
 619                                   const ClassLoaderData* init_cld) {
 620   assert(event != NULL, "invariant");
 621   assert(k != NULL, "invariant");
 622 
 623   if (event->should_commit()) {
 624     event->set_loadedClass(k);
 625     //event->set_definingClassLoader(k->class_loader_data());
 626     event->set_initiatingClassLoader(init_cld);
 627     event->commit();
 628   }

 629 }
 630 
 631 static void post_class_define_event(const InstanceKlass* k,
 632                                     const ClassLoaderData* def_cld) {

 633   EventClassDefine event;
 634   if (event.should_commit()) {
 635     event.set_definedClass(k);
 636     event.set_definingClassLoader(def_cld);
 637     event.commit();
 638   }

 639 }
 640 #endif // INCLUDE_TRACE
 641 
 642 // Be careful when modifying this code: once you have run
 643 // placeholders()->find_and_add(PlaceholderTable::LOAD_INSTANCE),
 644 // you need to find_and_remove it before returning.
 645 // So be careful to not exit with a CHECK_ macro betweeen these calls.
 646 Klass* SystemDictionary::resolve_instance_class_or_null(Symbol* name,
 647                                                         Handle class_loader,
 648                                                         Handle protection_domain,
 649                                                         TRAPS) {
 650   assert(name != NULL && !FieldType::is_array(name) &&
 651          !FieldType::is_obj(name), "invalid class name");
 652 
 653   TRACE_ONLY(EventClassLoad ev;)
 654 
 655   HandleMark hm(THREAD);
 656 
 657   // Fix for 4474172; see evaluation for more details
 658   class_loader = Handle(THREAD, java_lang_ClassLoader::non_reflection_class_loader(class_loader()));
 659   ClassLoaderData *loader_data = register_loader(class_loader, CHECK_NULL);
 660 
 661   // Do lookup to see if class already exist and the protection domain
 662   // has the right access
 663   // This call uses find which checks protection domain already matches
 664   // All subsequent calls use find_class, and set has_loaded_class so that
 665   // before we return a result we call out to java to check for valid protection domain
 666   // to allow returning the Klass* and add it to the pd_set if it is valid
 667   unsigned int d_hash = dictionary()->compute_hash(name, loader_data);
 668   int d_index = dictionary()->hash_to_index(d_hash);
 669   Klass* probe = dictionary()->find(d_index, d_hash, name, loader_data,
 670                                       protection_domain, THREAD);
 671   if (probe != NULL) return probe;
 672 
 673 


 879             JvmtiExport::post_class_load((JavaThread *) thread, k);
 880           }
 881         }
 882       }
 883     } // load_instance_class loop
 884 
 885     if (load_instance_added == true) {
 886       // clean up placeholder entries for LOAD_INSTANCE success or error
 887       // This brackets the SystemDictionary updates for both defining
 888       // and initiating loaders
 889       MutexLocker mu(SystemDictionary_lock, THREAD);
 890       placeholders()->find_and_remove(p_index, p_hash, name, loader_data, PlaceholderTable::LOAD_INSTANCE, THREAD);
 891       SystemDictionary_lock->notify_all();
 892     }
 893   }
 894 
 895   if (HAS_PENDING_EXCEPTION || k == NULL) {
 896     return NULL;
 897   }
 898 
 899   TRACE_ONLY(post_class_load_event(&ev, k, loader_data);)
 900 
 901 #ifdef ASSERT
 902   {
 903     ClassLoaderData* loader_data = k->class_loader_data();
 904     MutexLocker mu(SystemDictionary_lock, THREAD);
 905     Klass* kk = find_class(name, loader_data);
 906     assert(kk == k, "should be present in dictionary");
 907   }
 908 #endif
 909 
 910   // return if the protection domain in NULL
 911   if (protection_domain() == NULL) return k;
 912 
 913   // Check the protection domain has the right access
 914   {
 915     MutexLocker mu(SystemDictionary_lock, THREAD);
 916     // Note that we have an entry, and entries can be deleted only during GC,
 917     // so we cannot allow GC to occur while we're holding this entry.
 918     // We're using a NoSafepointVerifier to catch any place where we
 919     // might potentially do a GC at all.


1002     if (k != NULL) {
1003       k = k->array_klass_or_null(fd.dimension());
1004     }
1005   } else {
1006     k = find(class_name, class_loader, protection_domain, THREAD);
1007   }
1008   return k;
1009 }
1010 
1011 // Note: this method is much like resolve_from_stream, but
1012 // does not publish the classes via the SystemDictionary.
1013 // Handles unsafe_DefineAnonymousClass and redefineclasses
1014 // RedefinedClasses do not add to the class hierarchy
1015 InstanceKlass* SystemDictionary::parse_stream(Symbol* class_name,
1016                                               Handle class_loader,
1017                                               Handle protection_domain,
1018                                               ClassFileStream* st,
1019                                               const InstanceKlass* host_klass,
1020                                               GrowableArray<Handle>* cp_patches,
1021                                               TRAPS) {
1022   TRACE_ONLY(EventClassLoad ev;)

1023 
1024   ClassLoaderData* loader_data;
1025   if (host_klass != NULL) {
1026     // Create a new CLD for anonymous class, that uses the same class loader
1027     // as the host_klass
1028     guarantee(host_klass->class_loader() == class_loader(), "should be the same");
1029     guarantee(!DumpSharedSpaces, "must not create anonymous classes when dumping");
1030     loader_data = ClassLoaderData::anonymous_class_loader_data(class_loader(), CHECK_NULL);
1031     loader_data->record_dependency(host_klass, CHECK_NULL);
1032   } else {
1033     loader_data = ClassLoaderData::class_loader_data(class_loader());
1034   }
1035 
1036   assert(st != NULL, "invariant");
1037   assert(st->need_verify(), "invariant");
1038 
1039   // Parse stream and create a klass.
1040   // Note that we do this even though this klass might
1041   // already be present in the SystemDictionary, otherwise we would not
1042   // throw potential ClassFormatErrors.


1061 
1062       // But, do not add to system dictionary.
1063 
1064       // compiled code dependencies need to be validated anyway
1065       notice_modification();
1066     }
1067 
1068     // Rewrite and patch constant pool here.
1069     k->link_class(CHECK_NULL);
1070     if (cp_patches != NULL) {
1071       k->constants()->patch_resolved_references(cp_patches);
1072     }
1073     k->eager_initialize(CHECK_NULL);
1074 
1075     // notify jvmti
1076     if (JvmtiExport::should_post_class_load()) {
1077         assert(THREAD->is_Java_thread(), "thread->is_Java_thread()");
1078         JvmtiExport::post_class_load((JavaThread *) THREAD, k);
1079     }
1080 
1081     TRACE_ONLY(post_class_load_event(&ev, k, loader_data);)
1082   }
1083   assert(host_klass != NULL || NULL == cp_patches,
1084          "cp_patches only found with host_klass");
1085 
1086   return k;
1087 }
1088 
1089 // Add a klass to the system from a stream (called by jni_DefineClass and
1090 // JVM_DefineClass).
1091 // Note: class_name can be NULL. In that case we do not know the name of
1092 // the class until we have parsed the stream.
1093 
1094 InstanceKlass* SystemDictionary::resolve_from_stream(Symbol* class_name,
1095                                                      Handle class_loader,
1096                                                      Handle protection_domain,
1097                                                      ClassFileStream* st,
1098                                                      TRAPS) {
1099 
1100   HandleMark hm(THREAD);
1101 


1643 
1644     MutexLocker mu_r(Compile_lock, THREAD);
1645 
1646     // Add to class hierarchy, initialize vtables, and do possible
1647     // deoptimizations.
1648     add_to_hierarchy(k, CHECK); // No exception, but can block
1649 
1650     // Add to systemDictionary - so other classes can see it.
1651     // Grabs and releases SystemDictionary_lock
1652     update_dictionary(d_index, d_hash, p_index, p_hash,
1653                       k, class_loader_h, THREAD);
1654   }
1655   k->eager_initialize(THREAD);
1656 
1657   // notify jvmti
1658   if (JvmtiExport::should_post_class_load()) {
1659       assert(THREAD->is_Java_thread(), "thread->is_Java_thread()");
1660       JvmtiExport::post_class_load((JavaThread *) THREAD, k);
1661 
1662   }
1663   TRACE_ONLY(post_class_define_event(k, loader_data);)
1664 }
1665 
1666 // Support parallel classloading
1667 // All parallel class loaders, including bootstrap classloader
1668 // lock a placeholder entry for this class/class_loader pair
1669 // to allow parallel defines of different classes for this class loader
1670 // With AllowParallelDefine flag==true, in case they do not synchronize around
1671 // FindLoadedClass/DefineClass, calls, we check for parallel
1672 // loading for them, wait if a defineClass is in progress
1673 // and return the initial requestor's results
1674 // This flag does not apply to the bootstrap classloader.
1675 // With AllowParallelDefine flag==false, call through to define_instance_class
1676 // which will throw LinkageError: duplicate class definition.
1677 // False is the requested default.
1678 // For better performance, the class loaders should synchronize
1679 // findClass(), i.e. FindLoadedClass/DefineClassIfAbsent or they
1680 // potentially waste time reading and parsing the bytestream.
1681 // Note: VM callers should ensure consistency of k/class_name,class_loader
1682 // Be careful when modifying this code: once you have run
1683 // placeholders()->find_and_add(PlaceholderTable::DEFINE_CLASS),