Skip to content

Commit d1caa3f

Browse files
committed
8313899: JVMCI exception Translation can fail in TranslatedException.<clinit>
Backport-of: 6f5c903d10aa5f7ff979a79f121609c167f88eff
1 parent c993c0c commit d1caa3f

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
@@ -505,8 +505,7 @@ class HotSpotToSharedLibraryExceptionTranslation : public ExceptionTranslation {
505505
private:
506506
const Handle& _throwable;
507507

508-
int encode(JavaThread* THREAD, jlong buffer, int buffer_size) {
509-
Klass* vmSupport = SystemDictionary::resolve_or_fail(vmSymbols::jdk_internal_vm_VMSupport(), true, THREAD);
508+
bool handle_pending_exception(JavaThread* THREAD, jlong buffer, int buffer_size) {
510509
if (HAS_PENDING_EXCEPTION) {
511510
Handle throwable = Handle(THREAD, PENDING_EXCEPTION);
512511
Symbol *ex_name = throwable->klass()->name();
@@ -523,6 +522,14 @@ class HotSpotToSharedLibraryExceptionTranslation : public ExceptionTranslation {
523522
JVMCI_event_1("error translating exception: %s", char_buffer);
524523
decode(THREAD, _encode_fail, buffer);
525524
}
525+
return true;
526+
}
527+
return false;
528+
}
529+
530+
int encode(JavaThread* THREAD, jlong buffer, int buffer_size) {
531+
Klass* vmSupport = SystemDictionary::resolve_or_fail(vmSymbols::jdk_internal_vm_VMSupport(), true, THREAD);
532+
if (handle_pending_exception(THREAD, buffer, buffer_size)) {
526533
return 0;
527534
}
528535
JavaCallArguments jargs;
@@ -534,6 +541,9 @@ class HotSpotToSharedLibraryExceptionTranslation : public ExceptionTranslation {
534541
vmSupport,
535542
vmSymbols::encodeThrowable_name(),
536543
vmSymbols::encodeThrowable_signature(), &jargs, THREAD);
544+
if (handle_pending_exception(THREAD, buffer, buffer_size)) {
545+
return 0;
546+
}
537547
return result.get_jint();
538548
}
539549

src/hotspot/share/jvmci/jvmciRuntime.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1231,11 +1231,13 @@ JNIEnv* JVMCIRuntime::init_shared_library_javavm(int* create_JavaVM_err) {
12311231
MutexLocker locker(_lock);
12321232
JavaVM* javaVM = _shared_library_javavm;
12331233
if (javaVM == nullptr) {
1234+
#ifdef ASSERT
12341235
const char* val = Arguments::PropertyList_get_value(Arguments::system_properties(), "test.jvmci.forceEnomemOnLibjvmciInit");
12351236
if (val != nullptr && strcmp(val, "true") == 0) {
12361237
*create_JavaVM_err = JNI_ENOMEM;
12371238
return nullptr;
12381239
}
1240+
#endif
12391241

12401242
char* sl_path;
12411243
void* sl_handle = JVMCI::get_shared_library(sl_path, true);
@@ -2059,12 +2061,14 @@ void JVMCIRuntime::compile_method(JVMCIEnv* JVMCIENV, JVMCICompiler* compiler, c
20592061

20602062
JVMCIObject result_object = JVMCIENV->call_HotSpotJVMCIRuntime_compileMethod(receiver, jvmci_method, entry_bci,
20612063
(jlong) compile_state, compile_state->task()->compile_id());
2064+
#ifdef ASSERT
20622065
if (JVMCIENV->has_pending_exception()) {
20632066
const char* val = Arguments::PropertyList_get_value(Arguments::system_properties(), "test.jvmci.compileMethodExceptionIsFatal");
20642067
if (val != nullptr && strcmp(val, "true") == 0) {
20652068
fatal_exception(JVMCIENV, "testing JVMCI fatal exception handling");
20662069
}
20672070
}
2071+
#endif
20682072

20692073
if (after_compiler_upcall(JVMCIENV, compiler, method, "call_HotSpotJVMCIRuntime_compileMethod")) {
20702074
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.createLimitedTestJavaProcessBuilder(

0 commit comments

Comments
 (0)