8153134: Infinite loop in handle_wrong_method in jmod
authorthartmann
Tue, 18 Oct 2016 09:43:12 +0200
changeset 41715 296ff044b943
parent 41714 fc78950ecfe4
child 41716 86b102d7808a
child 41718 655e5401edee
child 41720 93c64aa0b62f
child 41723 88393c6dd24e
8153134: Infinite loop in handle_wrong_method in jmod Summary: Use Patching_lock to synchronize access between set_code() and clear_code(). Reviewed-by: kvn, dlong
hotspot/src/share/vm/classfile/classLoader.cpp
hotspot/src/share/vm/code/nmethod.cpp
hotspot/src/share/vm/oops/method.cpp
hotspot/src/share/vm/oops/method.hpp
--- a/hotspot/src/share/vm/classfile/classLoader.cpp	Mon Oct 17 16:40:49 2016 -0700
+++ b/hotspot/src/share/vm/classfile/classLoader.cpp	Tue Oct 18 09:43:12 2016 +0200
@@ -2041,7 +2041,6 @@
                 if (nm != NULL && !m->is_method_handle_intrinsic()) {
                   // Throw out the code so that the code cache doesn't fill up
                   nm->make_not_entrant();
-                  m->clear_code();
                 }
                 CompileBroker::compile_method(m, InvocationEntryBci, CompLevel_full_optimization,
                                               methodHandle(), 0, CompileTask::Reason_CTW, THREAD);
@@ -2060,7 +2059,6 @@
             if (nm != NULL && !m->is_method_handle_intrinsic()) {
               // Throw out the code so that the code cache doesn't fill up
               nm->make_not_entrant();
-              m->clear_code();
             }
           }
         }
--- a/hotspot/src/share/vm/code/nmethod.cpp	Mon Oct 17 16:40:49 2016 -0700
+++ b/hotspot/src/share/vm/code/nmethod.cpp	Tue Oct 18 09:43:12 2016 +0200
@@ -1252,7 +1252,7 @@
     if (method() != NULL && (method()->code() == this ||
                              method()->from_compiled_entry() == verified_entry_point())) {
       HandleMark hm;
-      method()->clear_code();
+      method()->clear_code(false /* already owns Patching_lock */);
     }
   } // leave critical region under Patching_lock
 
--- a/hotspot/src/share/vm/oops/method.cpp	Mon Oct 17 16:40:49 2016 -0700
+++ b/hotspot/src/share/vm/oops/method.cpp	Tue Oct 18 09:43:12 2016 +0200
@@ -97,7 +97,7 @@
   // Fix and bury in Method*
   set_interpreter_entry(NULL); // sets i2i entry and from_int
   set_adapter_entry(NULL);
-  clear_code(); // from_c/from_i get set to c2i/i2i
+  clear_code(false /* don't need a lock */); // from_c/from_i get set to c2i/i2i
 
   if (access_flags.is_native()) {
     clear_native_function();
@@ -907,8 +907,8 @@
 }
 
 // Revert to using the interpreter and clear out the nmethod
-void Method::clear_code() {
-
+void Method::clear_code(bool acquire_lock /* = true */) {
+  MutexLockerEx pl(acquire_lock ? Patching_lock : NULL, Mutex::_no_safepoint_check_flag);
   // this may be NULL if c2i adapters have not been made yet
   // Only should happen at allocate time.
   if (adapter() == NULL) {
@@ -1077,6 +1077,7 @@
 
 // Install compiled code.  Instantly it can execute.
 void Method::set_code(methodHandle mh, CompiledMethod *code) {
+  MutexLockerEx pl(Patching_lock, Mutex::_no_safepoint_check_flag);
   assert( code, "use clear_code to remove code" );
   assert( mh->check_code(), "" );
 
--- a/hotspot/src/share/vm/oops/method.hpp	Mon Oct 17 16:40:49 2016 -0700
+++ b/hotspot/src/share/vm/oops/method.hpp	Tue Oct 18 09:43:12 2016 +0200
@@ -436,7 +436,7 @@
   address verified_code_entry();
   bool check_code() const;      // Not inline to avoid circular ref
   CompiledMethod* volatile code() const                 { assert( check_code(), "" ); return (CompiledMethod *)OrderAccess::load_ptr_acquire(&_code); }
-  void clear_code();            // Clear out any compiled code
+  void clear_code(bool acquire_lock = true);    // Clear out any compiled code
   static void set_code(methodHandle mh, CompiledMethod* code);
   void set_adapter_entry(AdapterHandlerEntry* adapter) {
     constMethod()->set_adapter_entry(adapter);