Skip to content

Commit 6f5c903

Browse files
author
Doug Simon
committed
8313899: JVMCI exception Translation can fail in TranslatedException.<clinit>
Reviewed-by: never, thartmann
1 parent d97de82 commit 6f5c903

File tree

6 files changed

+59
-2
lines changed

6 files changed

+59
-2
lines changed

src/hotspot/share/jvmci/jvmciCompilerToVM.cpp

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@
5252
#include "prims/jvmtiExport.hpp"
5353
#include "prims/methodHandles.hpp"
5454
#include "prims/nativeLookup.hpp"
55+
#include "runtime/arguments.hpp"
5556
#include "runtime/atomic.hpp"
5657
#include "runtime/deoptimization.hpp"
5758
#include "runtime/fieldDescriptor.inline.hpp"
@@ -585,6 +586,18 @@ C2V_VMENTRY_NULL(jobject, lookupType, (JNIEnv* env, jobject, jstring jname, ARGU
585586
JVMCI_THROW_MSG_0(InternalError, err_msg("Primitive type %s should be handled in Java code", str));
586587
}
587588

589+
#ifdef ASSERT
590+
const char* val = Arguments::PropertyList_get_value(Arguments::system_properties(), "test.jvmci.lookupTypeException");
591+
if (val != nullptr) {
592+
if (strstr(val, "<trace>") != nullptr) {
593+
tty->print_cr("CompilerToVM.lookupType: %s", str);
594+
} else if (strstr(val, str) != nullptr) {
595+
THROW_MSG_0(vmSymbols::java_lang_Exception(),
596+
err_msg("lookupTypeException: %s", str));
597+
}
598+
}
599+
#endif
600+
588601
JVMCIKlassHandle resolved_klass(THREAD);
589602
Klass* accessing_klass = UNPACK_PAIR(Klass, accessing_klass);
590603
Handle class_loader;

src/hotspot/share/jvmci/jvmciEnv.cpp

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -433,8 +433,7 @@ class HotSpotToSharedLibraryExceptionTranslation : public ExceptionTranslation {
433433
private:
434434
const Handle& _throwable;
435435

436-
int encode(JavaThread* THREAD, jlong buffer, int buffer_size) {
437-
Klass* vmSupport = SystemDictionary::resolve_or_fail(vmSymbols::jdk_internal_vm_VMSupport(), true, THREAD);
436+
bool handle_pending_exception(JavaThread* THREAD, jlong buffer, int buffer_size) {
438437
if (HAS_PENDING_EXCEPTION) {
439438
Handle throwable = Handle(THREAD, PENDING_EXCEPTION);
440439
Symbol *ex_name = throwable->klass()->name();
@@ -451,6 +450,14 @@ class HotSpotToSharedLibraryExceptionTranslation : public ExceptionTranslation {
451450
JVMCI_event_1("error translating exception: %s", char_buffer);
452451
decode(THREAD, _encode_fail, buffer);
453452
}
453+
return true;
454+
}
455+
return false;
456+
}
457+
458+
int encode(JavaThread* THREAD, jlong buffer, int buffer_size) {
459+
Klass* vmSupport = SystemDictionary::resolve_or_fail(vmSymbols::jdk_internal_vm_VMSupport(), true, THREAD);
460+
if (handle_pending_exception(THREAD, buffer, buffer_size)) {
454461
return 0;
455462
}
456463
JavaCallArguments jargs;
@@ -462,6 +469,9 @@ class HotSpotToSharedLibraryExceptionTranslation : public ExceptionTranslation {
462469
vmSupport,
463470
vmSymbols::encodeThrowable_name(),
464471
vmSymbols::encodeThrowable_signature(), &jargs, THREAD);
472+
if (handle_pending_exception(THREAD, buffer, buffer_size)) {
473+
return 0;
474+
}
465475
return result.get_jint();
466476
}
467477

src/hotspot/share/jvmci/jvmciRuntime.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1247,11 +1247,13 @@ JNIEnv* JVMCIRuntime::init_shared_library_javavm(int* create_JavaVM_err) {
12471247
MutexLocker locker(_lock);
12481248
JavaVM* javaVM = _shared_library_javavm;
12491249
if (javaVM == nullptr) {
1250+
#ifdef ASSERT
12501251
const char* val = Arguments::PropertyList_get_value(Arguments::system_properties(), "test.jvmci.forceEnomemOnLibjvmciInit");
12511252
if (val != nullptr && strcmp(val, "true") == 0) {
12521253
*create_JavaVM_err = JNI_ENOMEM;
12531254
return nullptr;
12541255
}
1256+
#endif
12551257

12561258
char* sl_path;
12571259
void* sl_handle = JVMCI::get_shared_library(sl_path, true);
@@ -2062,12 +2064,14 @@ void JVMCIRuntime::compile_method(JVMCIEnv* JVMCIENV, JVMCICompiler* compiler, c
20622064

20632065
JVMCIObject result_object = JVMCIENV->call_HotSpotJVMCIRuntime_compileMethod(receiver, jvmci_method, entry_bci,
20642066
(jlong) compile_state, compile_state->task()->compile_id());
2067+
#ifdef ASSERT
20652068
if (JVMCIENV->has_pending_exception()) {
20662069
const char* val = Arguments::PropertyList_get_value(Arguments::system_properties(), "test.jvmci.compileMethodExceptionIsFatal");
20672070
if (val != nullptr && strcmp(val, "true") == 0) {
20682071
fatal_exception(JVMCIENV, "testing JVMCI fatal exception handling");
20692072
}
20702073
}
2074+
#endif
20712075

20722076
if (after_compiler_upcall(JVMCIENV, compiler, method, "call_HotSpotJVMCIRuntime_compileMethod")) {
20732077
return;

src/java.base/share/classes/jdk/internal/vm/TranslatedException.java

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@
2424
*/
2525
package jdk.internal.vm;
2626

27+
import jdk.internal.misc.VM;
28+
2729
import java.io.ByteArrayInputStream;
2830
import java.io.ByteArrayOutputStream;
2931
import java.io.DataInputStream;
@@ -56,6 +58,7 @@ final class TranslatedException extends Exception {
5658
*/
5759
private static final byte[] FALLBACK_ENCODED_THROWABLE_BYTES;
5860
static {
61+
maybeFailClinit();
5962
try {
6063
FALLBACK_ENCODED_THROWABLE_BYTES =
6164
encodeThrowable(new TranslatedException("error during encoding",
@@ -67,6 +70,22 @@ final class TranslatedException extends Exception {
6770
}
6871
}
6972

73+
/**
74+
* Helper to test exception translation.
75+
*/
76+
private static void maybeFailClinit() {
77+
String className = VM.getSavedProperty("test.jvmci.TranslatedException.clinit.throw");
78+
if (className != null) {
79+
try {
80+
throw (Throwable) Class.forName(className).getDeclaredConstructor().newInstance();
81+
} catch (RuntimeException | Error e) {
82+
throw e;
83+
} catch (Throwable e) {
84+
throw new InternalError(e);
85+
}
86+
}
87+
}
88+
7089
/**
7190
* Class name of exception that could not be instantiated.
7291
*/

test/hotspot/jtreg/compiler/jvmci/TestUncaughtErrorInCompileMethod.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,10 @@
2424
/*
2525
* @test
2626
* @summary Tests handling of an exception thrown by HotSpotJVMCIRuntime.compileMethod.
27+
* Requires a debug VM as it uses test.jvmci.compileMethodExceptionIsFatal
28+
* which is only read in a debug VM.
2729
* @requires vm.jvmci
30+
* @requires vm.debug
2831
* @library /test/lib /
2932
* @modules jdk.internal.vm.ci/jdk.vm.ci.hotspot
3033
* jdk.internal.vm.ci/jdk.vm.ci.code

test/hotspot/jtreg/compiler/jvmci/jdk.vm.ci.hotspot.test/src/jdk/vm/ci/hotspot/test/TestHotSpotJVMCIRuntime.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
* jdk.internal.vm.ci/jdk.vm.ci.common
3333
* @library /compiler/jvmci/jdk.vm.ci.hotspot.test/src
3434
* /compiler/jvmci/jdk.vm.ci.code.test/src
35+
* @library /test/lib
3536
* @run testng/othervm
3637
* -XX:+UnlockExperimentalVMOptions -XX:+EnableJVMCI -XX:-UseJVMCICompiler
3738
* jdk.vm.ci.hotspot.test.TestHotSpotJVMCIRuntime
@@ -53,6 +54,8 @@
5354
import jdk.vm.ci.meta.MetaAccessProvider;
5455
import jdk.vm.ci.meta.ResolvedJavaType;
5556

57+
import jdk.test.lib.Platform;
58+
5659
public class TestHotSpotJVMCIRuntime {
5760

5861
@Test
@@ -157,6 +160,11 @@ public static void main(String[] args) {
157160

158161
@Test
159162
public void jniEnomemTest() throws Exception {
163+
if (!Platform.isDebugBuild()) {
164+
// The test.jvmci.forceEnomemOnLibjvmciInit property is only
165+
// read in a debug VM.
166+
return;
167+
}
160168
String[] names = {"translate", "attachCurrentThread", "registerNativeMethods"};
161169
for (String name : names) {
162170
ProcessBuilder pb = ProcessTools.createJavaProcessBuilder(

0 commit comments

Comments
 (0)