@@ -67,6 +67,21 @@ JVMCICompileState::JVMCICompileState(CompileTask* task, JVMCICompiler* compiler)
6767 }
6868}
6969
70+ void JVMCICompileState::set_failure (bool retryable, const char * reason, bool reason_on_C_heap) {
71+ if (_failure_reason != nullptr && _failure_reason_on_C_heap) {
72+ os::free ((void *) _failure_reason);
73+ }
74+ _failure_reason = reason;
75+ _failure_reason_on_C_heap = reason_on_C_heap;
76+ _retryable = retryable;
77+ }
78+
79+ void JVMCICompileState::notify_libjvmci_oome () {
80+ const char * msg = " Out of memory initializing libjvmci or attaching it to the current thread" ;
81+ set_failure (true , msg);
82+ _compiler->on_upcall (msg);
83+ }
84+
7085// Update global JVMCI compilation ticks after 512 thread-local JVMCI compilation ticks.
7186// This mitigates the overhead of the atomic operation used for the global update.
7287#define THREAD_TICKS_PER_GLOBAL_TICKS (2 << 9 )
@@ -172,7 +187,7 @@ void JVMCIEnv::copy_saved_properties(jbyte* properties, int properties_len, JVMC
172187 }
173188}
174189
175- void JVMCIEnv::init_env_mode_runtime (JavaThread* thread, JNIEnv* parent_env, bool attach_OOME_is_fatal ) {
190+ void JVMCIEnv::init_env_mode_runtime (JavaThread* thread, JNIEnv* parent_env, bool jni_enomem_is_fatal ) {
176191 assert (thread != nullptr , " npe" );
177192 _env = nullptr ;
178193 _pop_frame_on_close = false ;
@@ -204,11 +219,18 @@ void JVMCIEnv::init_env_mode_runtime(JavaThread* thread, JNIEnv* parent_env, boo
204219 _is_hotspot = false ;
205220
206221 _runtime = JVMCI::compiler_runtime (thread);
207- _env = _runtime-> init_shared_library_javavm () ;
208-
222+ int create_JavaVM_err = JNI_OK ;
223+ _env = _runtime-> init_shared_library_javavm (&create_JavaVM_err);
209224 if (_env != nullptr ) {
210225 // Creating the JVMCI shared library VM also attaches the current thread
211226 _detach_on_close = true ;
227+ } else if (create_JavaVM_err != JNI_OK) {
228+ if (!jni_enomem_is_fatal && create_JavaVM_err == JNI_ENOMEM) {
229+ _jni_enomem = true ;
230+ return ;
231+ } else {
232+ fatal (" JNI_CreateJavaVM failed with return value %d" , create_JavaVM_err);
233+ }
212234 } else {
213235 _runtime->GetEnv (thread, (void **)&parent_env, JNI_VERSION_1_2);
214236 if (parent_env != nullptr ) {
@@ -227,9 +249,9 @@ void JVMCIEnv::init_env_mode_runtime(JavaThread* thread, JNIEnv* parent_env, boo
227249 jint attach_result = _runtime->AttachCurrentThread (thread, (void **) &_env, &attach_args);
228250 if (attach_result == JNI_OK) {
229251 _detach_on_close = true ;
230- } else if (!attach_OOME_is_fatal && attach_result == JNI_ENOMEM) {
252+ } else if (!jni_enomem_is_fatal && attach_result == JNI_ENOMEM) {
231253 _env = nullptr ;
232- _attach_threw_OOME = true ;
254+ _jni_enomem = true ;
233255 return ;
234256 } else {
235257 fatal (" Error attaching current thread (%s) to JVMCI shared library JNI interface" , attach_args.name );
@@ -251,40 +273,41 @@ void JVMCIEnv::init_env_mode_runtime(JavaThread* thread, JNIEnv* parent_env, boo
251273}
252274
253275JVMCIEnv::JVMCIEnv (JavaThread* thread, JVMCICompileState* compile_state, const char * file, int line):
254- _throw_to_caller(false ), _file(file), _line(line), _attach_threw_OOME(false ), _compile_state(compile_state) {
255- // In case of OOME, there's a good chance a subsequent attempt to attach might succeed.
256- // Other errors most likely indicate a non-recoverable error in the JVMCI runtime.
257- init_env_mode_runtime (thread, nullptr , false );
258- if (_attach_threw_OOME) {
276+ _throw_to_caller(false ), _file(file), _line(line), _jni_enomem(false ), _compile_state(compile_state) {
277+ // In case of JNI_ENOMEM, there's a good chance a subsequent attempt to create libjvmci or attach to it
278+ // might succeed. Other errors most likely indicate a non-recoverable error in the JVMCI runtime.
279+ bool jni_enomem_is_fatal = false ;
280+ init_env_mode_runtime (thread, nullptr , jni_enomem_is_fatal);
281+ if (_jni_enomem) {
259282 compile_state->set_failure (true , " Out of memory while attaching JVMCI compiler to current thread" );
260283 }
261284}
262285
263286JVMCIEnv::JVMCIEnv (JavaThread* thread, const char * file, int line):
264- _throw_to_caller(false ), _file(file), _line(line), _attach_threw_OOME (false ), _compile_state(nullptr ) {
287+ _throw_to_caller(false ), _file(file), _line(line), _jni_enomem (false ), _compile_state(nullptr ) {
265288 init_env_mode_runtime (thread, nullptr );
266289}
267290
268291JVMCIEnv::JVMCIEnv (JavaThread* thread, JNIEnv* parent_env, const char * file, int line):
269- _throw_to_caller(true ), _file(file), _line(line), _attach_threw_OOME (false ), _compile_state(nullptr ) {
292+ _throw_to_caller(true ), _file(file), _line(line), _jni_enomem (false ), _compile_state(nullptr ) {
270293 init_env_mode_runtime (thread, parent_env);
271294 assert (_env == nullptr || parent_env == _env, " mismatched JNIEnvironment" );
272295}
273296
274- void JVMCIEnv::init (JavaThread* thread, bool is_hotspot, const char * file, int line) {
297+ void JVMCIEnv::init (JavaThread* thread, bool is_hotspot, bool jni_enomem_is_fatal, const char * file, int line) {
275298 _compile_state = nullptr ;
276299 _throw_to_caller = false ;
277300 _file = file;
278301 _line = line;
279- _attach_threw_OOME = false ;
302+ _jni_enomem = false ;
280303 if (is_hotspot) {
281304 _env = nullptr ;
282305 _pop_frame_on_close = false ;
283306 _detach_on_close = false ;
284307 _is_hotspot = true ;
285308 _runtime = JVMCI::java_runtime ();
286309 } else {
287- init_env_mode_runtime (thread, nullptr );
310+ init_env_mode_runtime (thread, nullptr , jni_enomem_is_fatal );
288311 }
289312}
290313
@@ -464,7 +487,7 @@ jboolean JVMCIEnv::transfer_pending_exception(JavaThread* THREAD, JVMCIEnv* peer
464487}
465488
466489JVMCIEnv::~JVMCIEnv () {
467- if (_attach_threw_OOME ) {
490+ if (_jni_enomem ) {
468491 return ;
469492 }
470493 if (_throw_to_caller) {
@@ -775,6 +798,7 @@ DO_THROW(IllegalArgumentException)
775798DO_THROW(InvalidInstalledCodeException)
776799DO_THROW(UnsatisfiedLinkError)
777800DO_THROW(UnsupportedOperationException)
801+ DO_THROW(OutOfMemoryError)
778802DO_THROW(ClassNotFoundException)
779803
780804#undef DO_THROW
0 commit comments