< prev index next >

src/share/vm/prims/jni.cpp

Print this page




  15  *
  16  * You should have received a copy of the GNU General Public License version
  17  * 2 along with this work; if not, write to the Free Software Foundation,
  18  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  19  *
  20  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  21  * or visit www.oracle.com if you need additional information or have any
  22  * questions.
  23  *
  24  */
  25 
  26 #include "precompiled.hpp"
  27 #include "ci/ciReplay.hpp"
  28 #include "classfile/altHashing.hpp"
  29 #include "classfile/classLoader.hpp"
  30 #include "classfile/javaClasses.hpp"
  31 #include "classfile/symbolTable.hpp"
  32 #include "classfile/systemDictionary.hpp"
  33 #include "classfile/vmSymbols.hpp"
  34 #include "interpreter/linkResolver.hpp"


  35 #include "utilities/macros.hpp"
  36 #include "utilities/ostream.hpp"
  37 #if INCLUDE_ALL_GCS
  38 #include "gc_implementation/g1/g1SATBCardTableModRefBS.hpp"
  39 #endif // INCLUDE_ALL_GCS
  40 #include "memory/allocation.hpp"
  41 #include "memory/allocation.inline.hpp"
  42 #include "memory/gcLocker.inline.hpp"
  43 #include "memory/oopFactory.hpp"
  44 #include "memory/universe.inline.hpp"
  45 #include "oops/instanceKlass.hpp"
  46 #include "oops/instanceOop.hpp"
  47 #include "oops/markOop.hpp"
  48 #include "oops/method.hpp"
  49 #include "oops/objArrayKlass.hpp"
  50 #include "oops/objArrayOop.hpp"
  51 #include "oops/oop.inline.hpp"
  52 #include "oops/symbol.hpp"
  53 #include "oops/typeArrayKlass.hpp"
  54 #include "oops/typeArrayOop.hpp"


  59 #include "prims/jvm.h"
  60 #include "prims/jvm_misc.hpp"
  61 #include "prims/jvmtiExport.hpp"
  62 #include "prims/jvmtiThreadState.hpp"
  63 #include "runtime/compilationPolicy.hpp"
  64 #include "runtime/fieldDescriptor.hpp"
  65 #include "runtime/fprofiler.hpp"
  66 #include "runtime/handles.inline.hpp"
  67 #include "runtime/interfaceSupport.hpp"
  68 #include "runtime/java.hpp"
  69 #include "runtime/javaCalls.hpp"
  70 #include "runtime/jfieldIDWorkaround.hpp"
  71 #include "runtime/orderAccess.inline.hpp"
  72 #include "runtime/reflection.hpp"
  73 #include "runtime/sharedRuntime.hpp"
  74 #include "runtime/signature.hpp"
  75 #include "runtime/thread.inline.hpp"
  76 #include "runtime/vm_operations.hpp"
  77 #include "services/memTracker.hpp"
  78 #include "services/runtimeService.hpp"
  79 #include "trace/tracing.hpp"
  80 #include "utilities/defaultStream.hpp"
  81 #include "utilities/dtrace.hpp"
  82 #include "utilities/events.hpp"
  83 #include "utilities/histogram.hpp"
  84 #ifdef TARGET_OS_FAMILY_linux
  85 # include "os_linux.inline.hpp"
  86 #endif
  87 #ifdef TARGET_OS_FAMILY_solaris
  88 # include "os_solaris.inline.hpp"
  89 #endif
  90 #ifdef TARGET_OS_FAMILY_windows
  91 # include "os_windows.inline.hpp"
  92 #endif
  93 #ifdef TARGET_OS_FAMILY_bsd
  94 # include "os_bsd.inline.hpp"
  95 #endif
  96 
  97 static jint CurrentVersion = JNI_VERSION_1_8;
  98 
  99 


5002     func = JNI_FastGetField::generate_fast_get_double_field();
5003     if (func != (address)-1) {
5004       jni_NativeInterface.GetDoubleField = (GetDoubleField_t)func;
5005     }
5006   }
5007 }
5008 
5009 // Returns the function structure
5010 struct JNINativeInterface_* jni_functions() {
5011 #if INCLUDE_JNI_CHECK
5012   if (CheckJNICalls) return jni_functions_check();
5013 #endif // INCLUDE_JNI_CHECK
5014   return &jni_NativeInterface;
5015 }
5016 
5017 // Returns the function structure
5018 struct JNINativeInterface_* jni_functions_nocheck() {
5019   return &jni_NativeInterface;
5020 }
5021 








5022 
5023 // Invocation API
5024 
5025 
5026 // Forward declaration
5027 extern const struct JNIInvokeInterface_ jni_InvokeInterface;
5028 
5029 // Global invocation API vars
5030 volatile jint vm_created = 0;
5031 // Indicate whether it is safe to recreate VM
5032 volatile jint safe_to_recreate_vm = 1;
5033 struct JavaVM_ main_vm = {&jni_InvokeInterface};
5034 
5035 
5036 #define JAVASTACKSIZE (400 * 1024)    /* Default size of a thread java stack */
5037 enum { VERIFY_NONE, VERIFY_REMOTE, VERIFY_ALL };
5038 
5039 #ifndef USDT2
5040 HS_DTRACE_PROBE_DECL1(hotspot_jni, GetDefaultJavaVMInitArgs__entry, void*);
5041 DT_RETURN_MARK_DECL(GetDefaultJavaVMInitArgs, jint);


5224    * sets safe_to_recreate_vm to 1, such that any new call to
5225    * JNI_CreateJavaVM will immediately fail using the above logic.
5226    */
5227   bool can_try_again = true;
5228 
5229   result = Threads::create_vm((JavaVMInitArgs*) args, &can_try_again);
5230   if (result == JNI_OK) {
5231     JavaThread *thread = JavaThread::current();
5232     /* thread is thread_in_vm here */
5233     *vm = (JavaVM *)(&main_vm);
5234     *(JNIEnv**)penv = thread->jni_environment();
5235 
5236     // Tracks the time application was running before GC
5237     RuntimeService::record_application_start();
5238 
5239     // Notify JVMTI
5240     if (JvmtiExport::should_post_thread_life()) {
5241        JvmtiExport::post_thread_start(thread);
5242     }
5243 
5244     EventThreadStart event;
5245     if (event.should_commit()) {
5246       event.set_javalangthread(java_lang_Thread::thread_id(thread->threadObj()));
5247       event.commit();
5248     }
5249 
5250 #ifndef PRODUCT
5251   #ifndef CALL_TEST_FUNC_WITH_WRAPPER_IF_NEEDED
5252     #define CALL_TEST_FUNC_WITH_WRAPPER_IF_NEEDED(f) f()
5253   #endif
5254 
5255     // Check if we should compile all classes on bootclasspath
5256     if (CompileTheWorld) ClassLoader::compile_the_world();
5257     if (ReplayCompiles) ciReplay::replay(thread);
5258 
5259     // Some platforms (like Win*) need a wrapper around these test
5260     // functions in order to properly handle error conditions.
5261     CALL_TEST_FUNC_WITH_WRAPPER_IF_NEEDED(test_error_handler);
5262     CALL_TEST_FUNC_WITH_WRAPPER_IF_NEEDED(execute_internal_vm_tests);
5263 #endif
5264 
5265     // Since this is not a JVM_ENTRY we have to set the thread state manually before leaving.
5266     ThreadStateTransition::transition_and_fence(thread, _thread_in_vm, _thread_in_native);
5267   } else {
5268     if (can_try_again) {


5439   if (attach_failed) {
5440     // Added missing cleanup
5441     thread->cleanup_failed_attach_current_thread();
5442     return JNI_ERR;
5443   }
5444 
5445   // mark the thread as no longer attaching
5446   // this uses a fence to push the change through so we don't have
5447   // to regrab the threads_lock
5448   thread->set_done_attaching_via_jni();
5449 
5450   // Set java thread status.
5451   java_lang_Thread::set_thread_status(thread->threadObj(),
5452               java_lang_Thread::RUNNABLE);
5453 
5454   // Notify the debugger
5455   if (JvmtiExport::should_post_thread_life()) {
5456     JvmtiExport::post_thread_start(thread);
5457   }
5458 
5459   EventThreadStart event;
5460   if (event.should_commit()) {
5461     event.set_javalangthread(java_lang_Thread::thread_id(thread->threadObj()));
5462     event.commit();
5463   }
5464 
5465   *(JNIEnv**)penv = thread->jni_environment();
5466 
5467   // Now leaving the VM, so change thread_state. This is normally automatically taken care
5468   // of in the JVM_ENTRY. But in this situation we have to do it manually. Notice, that by
5469   // using ThreadStateTransition::transition, we do a callback to the safepoint code if
5470   // needed.
5471 
5472   ThreadStateTransition::transition_and_fence(thread, _thread_in_vm, _thread_in_native);
5473 
5474   // Perform any platform dependent FPU setup
5475   os::setup_fpu();
5476 
5477   return JNI_OK;
5478 }
5479 
5480 
5481 jint JNICALL jni_AttachCurrentThread(JavaVM *vm, void **penv, void *_args) {
5482 #ifndef USDT2
5483   DTRACE_PROBE3(hotspot_jni, AttachCurrentThread__entry, vm, penv, _args);




  15  *
  16  * You should have received a copy of the GNU General Public License version
  17  * 2 along with this work; if not, write to the Free Software Foundation,
  18  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  19  *
  20  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  21  * or visit www.oracle.com if you need additional information or have any
  22  * questions.
  23  *
  24  */
  25 
  26 #include "precompiled.hpp"
  27 #include "ci/ciReplay.hpp"
  28 #include "classfile/altHashing.hpp"
  29 #include "classfile/classLoader.hpp"
  30 #include "classfile/javaClasses.hpp"
  31 #include "classfile/symbolTable.hpp"
  32 #include "classfile/systemDictionary.hpp"
  33 #include "classfile/vmSymbols.hpp"
  34 #include "interpreter/linkResolver.hpp"
  35 #include "jfr/jfrEvents.hpp"
  36 #include "jfr/support/jfrThreadId.hpp"
  37 #include "utilities/macros.hpp"
  38 #include "utilities/ostream.hpp"
  39 #if INCLUDE_ALL_GCS
  40 #include "gc_implementation/g1/g1SATBCardTableModRefBS.hpp"
  41 #endif // INCLUDE_ALL_GCS
  42 #include "memory/allocation.hpp"
  43 #include "memory/allocation.inline.hpp"
  44 #include "memory/gcLocker.inline.hpp"
  45 #include "memory/oopFactory.hpp"
  46 #include "memory/universe.inline.hpp"
  47 #include "oops/instanceKlass.hpp"
  48 #include "oops/instanceOop.hpp"
  49 #include "oops/markOop.hpp"
  50 #include "oops/method.hpp"
  51 #include "oops/objArrayKlass.hpp"
  52 #include "oops/objArrayOop.hpp"
  53 #include "oops/oop.inline.hpp"
  54 #include "oops/symbol.hpp"
  55 #include "oops/typeArrayKlass.hpp"
  56 #include "oops/typeArrayOop.hpp"


  61 #include "prims/jvm.h"
  62 #include "prims/jvm_misc.hpp"
  63 #include "prims/jvmtiExport.hpp"
  64 #include "prims/jvmtiThreadState.hpp"
  65 #include "runtime/compilationPolicy.hpp"
  66 #include "runtime/fieldDescriptor.hpp"
  67 #include "runtime/fprofiler.hpp"
  68 #include "runtime/handles.inline.hpp"
  69 #include "runtime/interfaceSupport.hpp"
  70 #include "runtime/java.hpp"
  71 #include "runtime/javaCalls.hpp"
  72 #include "runtime/jfieldIDWorkaround.hpp"
  73 #include "runtime/orderAccess.inline.hpp"
  74 #include "runtime/reflection.hpp"
  75 #include "runtime/sharedRuntime.hpp"
  76 #include "runtime/signature.hpp"
  77 #include "runtime/thread.inline.hpp"
  78 #include "runtime/vm_operations.hpp"
  79 #include "services/memTracker.hpp"
  80 #include "services/runtimeService.hpp"

  81 #include "utilities/defaultStream.hpp"
  82 #include "utilities/dtrace.hpp"
  83 #include "utilities/events.hpp"
  84 #include "utilities/histogram.hpp"
  85 #ifdef TARGET_OS_FAMILY_linux
  86 # include "os_linux.inline.hpp"
  87 #endif
  88 #ifdef TARGET_OS_FAMILY_solaris
  89 # include "os_solaris.inline.hpp"
  90 #endif
  91 #ifdef TARGET_OS_FAMILY_windows
  92 # include "os_windows.inline.hpp"
  93 #endif
  94 #ifdef TARGET_OS_FAMILY_bsd
  95 # include "os_bsd.inline.hpp"
  96 #endif
  97 
  98 static jint CurrentVersion = JNI_VERSION_1_8;
  99 
 100 


5003     func = JNI_FastGetField::generate_fast_get_double_field();
5004     if (func != (address)-1) {
5005       jni_NativeInterface.GetDoubleField = (GetDoubleField_t)func;
5006     }
5007   }
5008 }
5009 
5010 // Returns the function structure
5011 struct JNINativeInterface_* jni_functions() {
5012 #if INCLUDE_JNI_CHECK
5013   if (CheckJNICalls) return jni_functions_check();
5014 #endif // INCLUDE_JNI_CHECK
5015   return &jni_NativeInterface;
5016 }
5017 
5018 // Returns the function structure
5019 struct JNINativeInterface_* jni_functions_nocheck() {
5020   return &jni_NativeInterface;
5021 }
5022 
5023 static void post_thread_start_event(const JavaThread* jt) {
5024   assert(jt != NULL, "invariant");
5025   EventThreadStart event;
5026   if (event.should_commit()) {
5027     event.set_thread(JFR_THREAD_ID(jt));
5028     event.commit();
5029   }
5030 }
5031 
5032 // Invocation API
5033 
5034 
5035 // Forward declaration
5036 extern const struct JNIInvokeInterface_ jni_InvokeInterface;
5037 
5038 // Global invocation API vars
5039 volatile jint vm_created = 0;
5040 // Indicate whether it is safe to recreate VM
5041 volatile jint safe_to_recreate_vm = 1;
5042 struct JavaVM_ main_vm = {&jni_InvokeInterface};
5043 
5044 
5045 #define JAVASTACKSIZE (400 * 1024)    /* Default size of a thread java stack */
5046 enum { VERIFY_NONE, VERIFY_REMOTE, VERIFY_ALL };
5047 
5048 #ifndef USDT2
5049 HS_DTRACE_PROBE_DECL1(hotspot_jni, GetDefaultJavaVMInitArgs__entry, void*);
5050 DT_RETURN_MARK_DECL(GetDefaultJavaVMInitArgs, jint);


5233    * sets safe_to_recreate_vm to 1, such that any new call to
5234    * JNI_CreateJavaVM will immediately fail using the above logic.
5235    */
5236   bool can_try_again = true;
5237 
5238   result = Threads::create_vm((JavaVMInitArgs*) args, &can_try_again);
5239   if (result == JNI_OK) {
5240     JavaThread *thread = JavaThread::current();
5241     /* thread is thread_in_vm here */
5242     *vm = (JavaVM *)(&main_vm);
5243     *(JNIEnv**)penv = thread->jni_environment();
5244 
5245     // Tracks the time application was running before GC
5246     RuntimeService::record_application_start();
5247 
5248     // Notify JVMTI
5249     if (JvmtiExport::should_post_thread_life()) {
5250        JvmtiExport::post_thread_start(thread);
5251     }
5252 
5253     post_thread_start_event(thread);




5254 
5255 #ifndef PRODUCT
5256   #ifndef CALL_TEST_FUNC_WITH_WRAPPER_IF_NEEDED
5257     #define CALL_TEST_FUNC_WITH_WRAPPER_IF_NEEDED(f) f()
5258   #endif
5259 
5260     // Check if we should compile all classes on bootclasspath
5261     if (CompileTheWorld) ClassLoader::compile_the_world();
5262     if (ReplayCompiles) ciReplay::replay(thread);
5263 
5264     // Some platforms (like Win*) need a wrapper around these test
5265     // functions in order to properly handle error conditions.
5266     CALL_TEST_FUNC_WITH_WRAPPER_IF_NEEDED(test_error_handler);
5267     CALL_TEST_FUNC_WITH_WRAPPER_IF_NEEDED(execute_internal_vm_tests);
5268 #endif
5269 
5270     // Since this is not a JVM_ENTRY we have to set the thread state manually before leaving.
5271     ThreadStateTransition::transition_and_fence(thread, _thread_in_vm, _thread_in_native);
5272   } else {
5273     if (can_try_again) {


5444   if (attach_failed) {
5445     // Added missing cleanup
5446     thread->cleanup_failed_attach_current_thread();
5447     return JNI_ERR;
5448   }
5449 
5450   // mark the thread as no longer attaching
5451   // this uses a fence to push the change through so we don't have
5452   // to regrab the threads_lock
5453   thread->set_done_attaching_via_jni();
5454 
5455   // Set java thread status.
5456   java_lang_Thread::set_thread_status(thread->threadObj(),
5457               java_lang_Thread::RUNNABLE);
5458 
5459   // Notify the debugger
5460   if (JvmtiExport::should_post_thread_life()) {
5461     JvmtiExport::post_thread_start(thread);
5462   }
5463 
5464   post_thread_start_event(thread);




5465 
5466   *(JNIEnv**)penv = thread->jni_environment();
5467 
5468   // Now leaving the VM, so change thread_state. This is normally automatically taken care
5469   // of in the JVM_ENTRY. But in this situation we have to do it manually. Notice, that by
5470   // using ThreadStateTransition::transition, we do a callback to the safepoint code if
5471   // needed.
5472 
5473   ThreadStateTransition::transition_and_fence(thread, _thread_in_vm, _thread_in_native);
5474 
5475   // Perform any platform dependent FPU setup
5476   os::setup_fpu();
5477 
5478   return JNI_OK;
5479 }
5480 
5481 
5482 jint JNICALL jni_AttachCurrentThread(JavaVM *vm, void **penv, void *_args) {
5483 #ifndef USDT2
5484   DTRACE_PROBE3(hotspot_jni, AttachCurrentThread__entry, vm, penv, _args);


< prev index next >