src/share/vm/runtime/thread.cpp

Print this page
rev 8963 : 8194653: Deadlock involving FileSystems.getDefault and System.loadLibrary call
Preload classes in the j.n.f.FileSystems.getDefault() path to avoid a potential
deadlock between class loading and native library loading.


 971   size_t usable_stack_size = _stack_size - stack_guard_size;
 972 
 973   return ((adr < stack_base()) && (adr >= stack_base() - usable_stack_size));
 974 }
 975 
 976 
 977 // We had to move these methods here, because vm threads get into ObjectSynchronizer::enter
 978 // However, there is a note in JavaThread::is_lock_owned() about the VM threads not being
 979 // used for compilation in the future. If that change is made, the need for these methods
 980 // should be revisited, and they should be removed if possible.
 981 
 982 bool Thread::is_lock_owned(address adr) const {
 983   return on_local_stack(adr);
 984 }
 985 
 986 bool Thread::set_as_starting_thread() {
 987  // NOTE: this must be called inside the main thread.
 988   return os::create_main_thread((JavaThread*)this);
 989 }
 990 







 991 static void initialize_class(Symbol* class_name, TRAPS) {
 992   Klass* klass = SystemDictionary::resolve_or_fail(class_name, true, CHECK);
 993   InstanceKlass::cast(klass)->initialize(CHECK);
 994 }
 995 
 996 
 997 // Creates the initial ThreadGroup
 998 static Handle create_initial_thread_group(TRAPS) {
 999   Klass* k = SystemDictionary::resolve_or_fail(vmSymbols::java_lang_ThreadGroup(), true, CHECK_NH);
1000   instanceKlassHandle klass (THREAD, k);
1001 
1002   Handle system_instance = klass->allocate_instance_handle(CHECK_NH);
1003   {
1004     JavaValue result(T_VOID);
1005     JavaCalls::call_special(&result,
1006                             system_instance,
1007                             klass,
1008                             vmSymbols::object_initializer_name(),
1009                             vmSymbols::void_method_signature(),
1010                             CHECK_NH);
1011   }
1012   Universe::set_system_thread_group(system_instance());
1013 


3643 
3644   if (CleanChunkPoolAsync) {
3645     Chunk::start_chunk_pool_cleaner_task();
3646   }
3647 
3648   // initialize compiler(s)
3649 #if defined(COMPILER1) || defined(COMPILER2) || defined(SHARK)
3650   CompileBroker::compilation_init();
3651 #endif
3652 
3653   if (EnableInvokeDynamic) {
3654     // Pre-initialize some JSR292 core classes to avoid deadlock during class loading.
3655     // It is done after compilers are initialized, because otherwise compilations of
3656     // signature polymorphic MH intrinsics can be missed
3657     // (see SystemDictionary::find_method_handle_intrinsic).
3658     initialize_class(vmSymbols::java_lang_invoke_MethodHandle(), CHECK_0);
3659     initialize_class(vmSymbols::java_lang_invoke_MemberName(), CHECK_0);
3660     initialize_class(vmSymbols::java_lang_invoke_MethodHandleNatives(), CHECK_0);
3661   }
3662 




















3663 #if INCLUDE_MANAGEMENT
3664   Management::initialize(THREAD);
3665 #endif // INCLUDE_MANAGEMENT
3666 
3667   if (HAS_PENDING_EXCEPTION) {
3668     // management agent fails to start possibly due to
3669     // configuration problem and is responsible for printing
3670     // stack trace if appropriate. Simply exit VM.
3671     vm_exit(1);
3672   }
3673 
3674   if (Arguments::has_profile())       FlatProfiler::engage(main_thread, true);
3675   if (MemProfiling)                   MemProfiler::engage();
3676   StatSampler::engage();
3677   if (CheckJNICalls)                  JniPeriodicChecker::engage();
3678 
3679   BiasedLocking::init();
3680 
3681 #if INCLUDE_RTM_OPT
3682   RTMLockingCounters::init();




 971   size_t usable_stack_size = _stack_size - stack_guard_size;
 972 
 973   return ((adr < stack_base()) && (adr >= stack_base() - usable_stack_size));
 974 }
 975 
 976 
 977 // We had to move these methods here, because vm threads get into ObjectSynchronizer::enter
 978 // However, there is a note in JavaThread::is_lock_owned() about the VM threads not being
 979 // used for compilation in the future. If that change is made, the need for these methods
 980 // should be revisited, and they should be removed if possible.
 981 
 982 bool Thread::is_lock_owned(address adr) const {
 983   return on_local_stack(adr);
 984 }
 985 
 986 bool Thread::set_as_starting_thread() {
 987  // NOTE: this must be called inside the main thread.
 988   return os::create_main_thread((JavaThread*)this);
 989 }
 990 
 991 // Helper to initialize_class for callers that need the klass result
 992 static Klass *initialize_class_and_return(Symbol* class_name, TRAPS) {
 993   Klass* klass = SystemDictionary::resolve_or_fail(class_name, true, CHECK_NULL);
 994   InstanceKlass::cast(klass)->initialize(CHECK_NULL);
 995   return klass;
 996 }
 997 
 998 static void initialize_class(Symbol* class_name, TRAPS) {
 999   (void)initialize_class_and_return(class_name, CHECK);

1000 }
1001 
1002 
1003 // Creates the initial ThreadGroup
1004 static Handle create_initial_thread_group(TRAPS) {
1005   Klass* k = SystemDictionary::resolve_or_fail(vmSymbols::java_lang_ThreadGroup(), true, CHECK_NH);
1006   instanceKlassHandle klass (THREAD, k);
1007 
1008   Handle system_instance = klass->allocate_instance_handle(CHECK_NH);
1009   {
1010     JavaValue result(T_VOID);
1011     JavaCalls::call_special(&result,
1012                             system_instance,
1013                             klass,
1014                             vmSymbols::object_initializer_name(),
1015                             vmSymbols::void_method_signature(),
1016                             CHECK_NH);
1017   }
1018   Universe::set_system_thread_group(system_instance());
1019 


3649 
3650   if (CleanChunkPoolAsync) {
3651     Chunk::start_chunk_pool_cleaner_task();
3652   }
3653 
3654   // initialize compiler(s)
3655 #if defined(COMPILER1) || defined(COMPILER2) || defined(SHARK)
3656   CompileBroker::compilation_init();
3657 #endif
3658 
3659   if (EnableInvokeDynamic) {
3660     // Pre-initialize some JSR292 core classes to avoid deadlock during class loading.
3661     // It is done after compilers are initialized, because otherwise compilations of
3662     // signature polymorphic MH intrinsics can be missed
3663     // (see SystemDictionary::find_method_handle_intrinsic).
3664     initialize_class(vmSymbols::java_lang_invoke_MethodHandle(), CHECK_0);
3665     initialize_class(vmSymbols::java_lang_invoke_MemberName(), CHECK_0);
3666     initialize_class(vmSymbols::java_lang_invoke_MethodHandleNatives(), CHECK_0);
3667   }
3668 
3669   // See        : bugid 8194653
3670   // Background : Deadlock as described by https://bugs.openjdk.java.net/browse/JDK-8194653
3671   // Mitigation : Eliminates a race condition on call to
3672   //              java.nio.file.FileSystems::getDefault() where two threads (one inside
3673   //              a loadLibrary() call) could cause a deadlock if they both referenced this
3674   //              code path.  Priming the code path during startup eliminates the deadlock
3675   //              (<clinit> and loadLibrary0 are single threaded).
3676   //              Note the JVM is at the end of initialization and the getDefault() call
3677   //              is safe to make.
3678   // Future Fix : OpenJDK 9 beyond address the issue with a different (fixed) startup
3679   //              sequence.  For OpenJDK 8 this should be sufficient.
3680   {
3681     Klass* k = initialize_class_and_return(vmSymbols::java_nio_file_FileSystems(), CHECK_0);
3682     instanceKlassHandle klass (THREAD, k);
3683     JavaValue result(T_OBJECT);
3684 
3685     JavaCalls::call_static(&result, klass, vmSymbols::getDefault_name(),
3686                                            vmSymbols::getDefault_signature(), CHECK_0);
3687   }
3688 
3689 #if INCLUDE_MANAGEMENT
3690   Management::initialize(THREAD);
3691 #endif // INCLUDE_MANAGEMENT
3692 
3693   if (HAS_PENDING_EXCEPTION) {
3694     // management agent fails to start possibly due to
3695     // configuration problem and is responsible for printing
3696     // stack trace if appropriate. Simply exit VM.
3697     vm_exit(1);
3698   }
3699 
3700   if (Arguments::has_profile())       FlatProfiler::engage(main_thread, true);
3701   if (MemProfiling)                   MemProfiler::engage();
3702   StatSampler::engage();
3703   if (CheckJNICalls)                  JniPeriodicChecker::engage();
3704 
3705   BiasedLocking::init();
3706 
3707 #if INCLUDE_RTM_OPT
3708   RTMLockingCounters::init();