Skip to content

Commit 8879c78

Browse files
author
Doug Simon
committedOct 24, 2023
8317689: [JVMCI] include error message when CreateJavaVM in libgraal fails
Reviewed-by: phofer, thartmann, never
1 parent f9795d0 commit 8879c78

File tree

6 files changed

+33
-13
lines changed

6 files changed

+33
-13
lines changed
 

‎src/hotspot/share/compiler/compileBroker.cpp

+3-1
Original file line numberDiff line numberDiff line change
@@ -2211,7 +2211,9 @@ void CompileBroker::invoke_compiler_on_method(CompileTask* task) {
22112211
} else {
22122212
JVMCIEnv env(thread, &compile_state, __FILE__, __LINE__);
22132213
if (env.init_error() != JNI_OK) {
2214-
failure_reason = os::strdup(err_msg("Error attaching to libjvmci (err: %d)", env.init_error()), mtJVMCI);
2214+
const char* msg = env.init_error_msg();
2215+
failure_reason = os::strdup(err_msg("Error attaching to libjvmci (err: %d, %s)",
2216+
env.init_error(), msg == nullptr ? "unknown" : msg), mtJVMCI);
22152217
bool reason_on_C_heap = true;
22162218
// In case of JNI_ENOMEM, there's a good chance a subsequent attempt to create libjvmci or attach to it
22172219
// might succeed. Other errors most likely indicate a non-recoverable error in the JVMCI runtime.

‎src/hotspot/share/jvmci/jvmciEnv.cpp

+14-7
Original file line numberDiff line numberDiff line change
@@ -147,13 +147,14 @@ void JVMCIEnv::init_env_mode_runtime(JavaThread* thread, JNIEnv* parent_env) {
147147
_is_hotspot = false;
148148

149149
_runtime = JVMCI::compiler_runtime(thread);
150-
_env = _runtime->init_shared_library_javavm(&_init_error);
150+
_env = _runtime->init_shared_library_javavm(&_init_error, &_init_error_msg);
151151
if (_env != nullptr) {
152152
// Creating the JVMCI shared library VM also attaches the current thread
153153
_detach_on_close = true;
154154
} else if (_init_error != JNI_OK) {
155155
// Caller creating this JVMCIEnv must handle the error.
156-
JVMCI_event_1("[%s:%d] Error creating libjvmci (err: %d)", _file, _line, _init_error);
156+
JVMCI_event_1("[%s:%d] Error creating libjvmci (err: %d, %s)", _file, _line,
157+
_init_error, _init_error_msg == nullptr ? "unknown" : _init_error_msg);
157158
return;
158159
} else {
159160
_runtime->GetEnv(thread, (void**)&parent_env, JNI_VERSION_1_2);
@@ -195,17 +196,17 @@ void JVMCIEnv::init_env_mode_runtime(JavaThread* thread, JNIEnv* parent_env) {
195196
}
196197

197198
JVMCIEnv::JVMCIEnv(JavaThread* thread, JVMCICompileState* compile_state, const char* file, int line):
198-
_throw_to_caller(false), _file(file), _line(line), _init_error(JNI_OK), _compile_state(compile_state) {
199+
_throw_to_caller(false), _file(file), _line(line), _init_error(JNI_OK), _init_error_msg(nullptr), _compile_state(compile_state) {
199200
init_env_mode_runtime(thread, nullptr);
200201
}
201202

202203
JVMCIEnv::JVMCIEnv(JavaThread* thread, const char* file, int line):
203-
_throw_to_caller(false), _file(file), _line(line), _init_error(JNI_OK), _compile_state(nullptr) {
204+
_throw_to_caller(false), _file(file), _line(line), _init_error(JNI_OK), _init_error_msg(nullptr), _compile_state(nullptr) {
204205
init_env_mode_runtime(thread, nullptr);
205206
}
206207

207208
JVMCIEnv::JVMCIEnv(JavaThread* thread, JNIEnv* parent_env, const char* file, int line):
208-
_throw_to_caller(true), _file(file), _line(line), _init_error(JNI_OK), _compile_state(nullptr) {
209+
_throw_to_caller(true), _file(file), _line(line), _init_error(JNI_OK), _init_error_msg(nullptr), _compile_state(nullptr) {
209210
assert(parent_env != nullptr, "npe");
210211
init_env_mode_runtime(thread, parent_env);
211212
assert(_env == nullptr || parent_env == _env, "mismatched JNIEnvironment");
@@ -218,6 +219,7 @@ void JVMCIEnv::init(JavaThread* thread, bool is_hotspot, const char* file, int l
218219
_file = file;
219220
_line = line;
220221
_init_error = JNI_OK;
222+
_init_error_msg = nullptr;
221223
if (is_hotspot) {
222224
_env = nullptr;
223225
_pop_frame_on_close = false;
@@ -237,7 +239,8 @@ void JVMCIEnv::check_init(JVMCI_TRAPS) {
237239
if (_init_error == JNI_ENOMEM) {
238240
JVMCI_THROW_MSG(OutOfMemoryError, "JNI_ENOMEM creating or attaching to libjvmci");
239241
}
240-
JVMCI_THROW_MSG(InternalError, err_msg("Error creating or attaching to libjvmci (err: %d)", _init_error));
242+
JVMCI_THROW_MSG(InternalError, err_msg("Error creating or attaching to libjvmci (err: %d, description: %s)",
243+
_init_error, _init_error_msg == nullptr ? "unknown" : _init_error_msg));
241244
}
242245

243246
void JVMCIEnv::check_init(TRAPS) {
@@ -247,7 +250,8 @@ void JVMCIEnv::check_init(TRAPS) {
247250
if (_init_error == JNI_ENOMEM) {
248251
THROW_MSG(vmSymbols::java_lang_OutOfMemoryError(), "JNI_ENOMEM creating or attaching to libjvmci");
249252
}
250-
THROW_MSG(vmSymbols::java_lang_OutOfMemoryError(), err_msg("Error creating or attaching to libjvmci (err: %d)", _init_error));
253+
THROW_MSG(vmSymbols::java_lang_OutOfMemoryError(), err_msg("Error creating or attaching to libjvmci (err: %d, description: %s)",
254+
_init_error, _init_error_msg == nullptr ? "unknown" : _init_error_msg));
251255
}
252256

253257
// Prints a pending exception (if any) and its stack trace to st.
@@ -572,6 +576,9 @@ jboolean JVMCIEnv::transfer_pending_exception(JavaThread* THREAD, JVMCIEnv* peer
572576
}
573577

574578
JVMCIEnv::~JVMCIEnv() {
579+
if (_init_error_msg != nullptr) {
580+
os::free((void*) _init_error_msg);
581+
}
575582
if (_init_error != JNI_OK) {
576583
return;
577584
}

‎src/hotspot/share/jvmci/jvmciEnv.hpp

+7
Original file line numberDiff line numberDiff line change
@@ -172,6 +172,7 @@ class JVMCIEnv : public ResourceObj {
172172
int _init_error; // JNI code returned when creating or attaching to a libjvmci isolate.
173173
// If not JNI_OK, the JVMCIEnv is invalid and should not be used apart from
174174
// calling init_error().
175+
const char* _init_error_msg; // Message for _init_error if available. C heap allocated.
175176

176177
// Translates an exception on the HotSpot heap (i.e., hotspot_env) to an exception on
177178
// the shared library heap (i.e., jni_env). The translation includes the stack and cause(s) of `throwable`.
@@ -217,6 +218,12 @@ class JVMCIEnv : public ResourceObj {
217218
return _init_error;
218219
}
219220

221+
// Gets a message describing a non-zero init_error().
222+
// Valid as long as this JVMCIEnv is valid.
223+
const char* init_error_msg() {
224+
return _init_error_msg;
225+
}
226+
220227
// Checks the value of init_error() and throws an exception in `JVMCI_TRAPS`
221228
// (which must not be this) if it is not JNI_OK.
222229
void check_init(JVMCI_TRAPS);

‎src/hotspot/share/jvmci/jvmciRuntime.cpp

+4-2
Original file line numberDiff line numberDiff line change
@@ -1253,7 +1253,7 @@ bool JVMCIRuntime::detach_thread(JavaThread* thread, const char* reason, bool ca
12531253
return destroyed_javavm;
12541254
}
12551255

1256-
JNIEnv* JVMCIRuntime::init_shared_library_javavm(int* create_JavaVM_err) {
1256+
JNIEnv* JVMCIRuntime::init_shared_library_javavm(int* create_JavaVM_err, const char** err_msg) {
12571257
MutexLocker locker(_lock);
12581258
JavaVM* javaVM = _shared_library_javavm;
12591259
if (javaVM == nullptr) {
@@ -1280,7 +1280,7 @@ JNIEnv* JVMCIRuntime::init_shared_library_javavm(int* create_JavaVM_err) {
12801280
JavaVMInitArgs vm_args;
12811281
vm_args.version = JNI_VERSION_1_2;
12821282
vm_args.ignoreUnrecognized = JNI_TRUE;
1283-
JavaVMOption options[5];
1283+
JavaVMOption options[6];
12841284
jlong javaVM_id = 0;
12851285

12861286
// Protocol: JVMCI shared library JavaVM should support a non-standard "_javavm_id"
@@ -1297,6 +1297,8 @@ JNIEnv* JVMCIRuntime::init_shared_library_javavm(int* create_JavaVM_err) {
12971297
options[3].extraInfo = (void*) _fatal;
12981298
options[4].optionString = (char*) "_fatal_log";
12991299
options[4].extraInfo = (void*) _fatal_log;
1300+
options[5].optionString = (char*) "_createvm_errorstr";
1301+
options[5].extraInfo = (void*) err_msg;
13001302

13011303
vm_args.version = JNI_VERSION_1_2;
13021304
vm_args.options = options;

‎src/hotspot/share/jvmci/jvmciRuntime.hpp

+4-2
Original file line numberDiff line numberDiff line change
@@ -280,8 +280,10 @@ class JVMCIRuntime: public CHeapObj<mtJVMCI> {
280280
// If the JavaVM was created by this call, then the thread-local JNI
281281
// interface pointer for the JavaVM is returned otherwise null is returned.
282282
// If this method tried to create the JavaVM but failed, the error code returned
283-
// by JNI_CreateJavaVM is returned in create_JavaVM_err.
284-
JNIEnv* init_shared_library_javavm(int* create_JavaVM_err);
283+
// by JNI_CreateJavaVM is returned in create_JavaVM_err and, if available, an
284+
// error message is malloc'ed and assigned to err_msg. The caller is responsible
285+
// for freeing err_msg.
286+
JNIEnv* init_shared_library_javavm(int* create_JavaVM_err, const char** err_msg);
285287

286288
// Determines if the JVMCI shared library JavaVM exists for this runtime.
287289
bool has_shared_library_javavm() { return _shared_library_javavm != nullptr; }

‎src/hotspot/share/prims/whitebox.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -381,7 +381,7 @@ WB_ENTRY(jboolean, WB_IsGCSupportedByJVMCICompiler(JNIEnv* env, jobject o, jint
381381
if (EnableJVMCI) {
382382
// Enter the JVMCI env that will be used by the CompileBroker.
383383
JVMCIEnv jvmciEnv(thread, __FILE__, __LINE__);
384-
return jvmciEnv.runtime()->is_gc_supported(&jvmciEnv, (CollectedHeap::Name)name);
384+
return jvmciEnv.init_error() == JNI_OK && jvmciEnv.runtime()->is_gc_supported(&jvmciEnv, (CollectedHeap::Name)name);
385385
}
386386
#endif
387387
return false;

1 commit comments

Comments
 (1)

openjdk-notifier[bot] commented on Oct 24, 2023

@openjdk-notifier[bot]
Please sign in to comment.