Skip to content
This repository was archived by the owner on May 29, 2024. It is now read-only.

Commit ecffcb5

Browse files
committed
[GR-44603] DecodeAndThrowThrowable needs to handle error codes.
PullRequest: labsjdk-ce-17/103
2 parents dad6955 + 48edd3e commit ecffcb5

File tree

5 files changed

+31
-9
lines changed

5 files changed

+31
-9
lines changed

src/hotspot/share/classfile/vmSymbols.hpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -684,6 +684,7 @@
684684
template(encodeThrowable_name, "encodeThrowable") \
685685
template(encodeThrowable_signature, "(Ljava/lang/Throwable;JI)I") \
686686
template(decodeAndThrowThrowable_name, "decodeAndThrowThrowable") \
687+
template(decodeAndThrowThrowable_signature, "(JZ)V") \
687688
template(classRedefinedCount_name, "classRedefinedCount") \
688689
template(classLoader_name, "classLoader") \
689690
template(componentType_name, "componentType") \

src/hotspot/share/jvmci/jvmciEnv.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -390,7 +390,7 @@ class HotSpotToSharedLibraryExceptionTranslation : public ExceptionTranslation {
390390
JNIAccessMark jni(_to_env, THREAD);
391391
jni()->CallStaticVoidMethod(JNIJVMCI::VMSupport::clazz(),
392392
JNIJVMCI::VMSupport::decodeAndThrowThrowable_method(),
393-
buffer);
393+
buffer, false);
394394
}
395395
public:
396396
HotSpotToSharedLibraryExceptionTranslation(JVMCIEnv* hotspot_env, JVMCIEnv* jni_env, const Handle& throwable) :
@@ -412,11 +412,12 @@ class SharedLibraryToHotSpotExceptionTranslation : public ExceptionTranslation {
412412
void decode(JavaThread* THREAD, Klass* vmSupport, jlong buffer) {
413413
JavaCallArguments jargs;
414414
jargs.push_long(buffer);
415+
jargs.push_int(true);
415416
JavaValue result(T_VOID);
416417
JavaCalls::call_static(&result,
417418
vmSupport,
418419
vmSymbols::decodeAndThrowThrowable_name(),
419-
vmSymbols::long_void_signature(), &jargs, THREAD);
420+
vmSymbols::decodeAndThrowThrowable_signature(), &jargs, THREAD);
420421
}
421422
public:
422423
SharedLibraryToHotSpotExceptionTranslation(JVMCIEnv* hotspot_env, JVMCIEnv* jni_env, jthrowable throwable) :

src/hotspot/share/jvmci/jvmciJavaClasses.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -216,7 +216,7 @@
216216
end_class \
217217
start_class(VMSupport, jdk_internal_vm_VMSupport) \
218218
jvmci_method(CallStaticIntMethod, GetStaticMethodID, call_static, int, VMSupport, encodeThrowable, encodeThrowable_signature, (JVMCIObject throwable, jlong buffer, int buffer_size)) \
219-
jvmci_method(CallStaticVoidMethod, GetStaticMethodID, call_static, void, VMSupport, decodeAndThrowThrowable, long_void_signature, (jlong buffer)) \
219+
jvmci_method(CallStaticVoidMethod, GetStaticMethodID, call_static, void, VMSupport, decodeAndThrowThrowable, decodeAndThrowThrowable_signature, (jlong buffer)) \
220220
end_class \
221221
start_class(ArrayIndexOutOfBoundsException, java_lang_ArrayIndexOutOfBoundsException) \
222222
jvmci_constructor(ArrayIndexOutOfBoundsException, "(Ljava/lang/String;)V") \

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

Lines changed: 25 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -113,15 +113,35 @@ public static byte[] serializeSavedPropertiesToByteArray() throws IOException {
113113
public static native String getVMTemporaryDirectory();
114114

115115
/**
116-
* Decodes the exception encoded in {@code buffer} and throws it.
116+
* Decodes the exception encoded in {@code errorOrBuffer} and throws it.
117117
*
118-
* @param buffer a native byte buffer containing an exception encoded by
118+
* @param errorOrBuffer an error code or a native byte errorOrBuffer containing an exception encoded by
119+
* {@link #encodeThrowable}. Error code values and their meanings are:
120+
*
121+
* <pre>
122+
* 0: native memory for the errorOrBuffer could not be allocated
123+
* -1: an OutOfMemoryError was thrown while encoding the exception
124+
* -2: some other throwable was thrown while encoding the exception
125+
* </pre>
126+
* @param errorOrBuffer a native byte errorOrBuffer containing an exception encoded by
119127
* {@link #encodeThrowable}
128+
* @param inJVMHeap [@code true} if executing in the JVM heap, {@code false} otherwise
120129
*/
121-
public static void decodeAndThrowThrowable(long buffer) throws Throwable {
122-
int encodingLength = U.getInt(buffer);
130+
public static void decodeAndThrowThrowable(long errorOrBuffer, boolean inJVMHeap) throws Throwable {
131+
if (errorOrBuffer >= -2L && errorOrBuffer <= 0) {
132+
String context = String.format("while encoding an exception to translate it %s the JVM heap",
133+
inJVMHeap ? "to" : "from");
134+
if (errorOrBuffer == 0) {
135+
throw new InternalError("native errorOrBuffer could not be allocated " + context);
136+
}
137+
if (errorOrBuffer == -1L) {
138+
throw new OutOfMemoryError("OutOfMemoryError occurred " + context);
139+
}
140+
throw new InternalError("unexpected problem occurred " + context);
141+
}
142+
int encodingLength = U.getInt(errorOrBuffer);
123143
byte[] encoding = new byte[encodingLength];
124-
U.copyMemory(null, buffer + 4, encoding, Unsafe.ARRAY_BYTE_BASE_OFFSET, encodingLength);
144+
U.copyMemory(null, errorOrBuffer + 4, encoding, Unsafe.ARRAY_BYTE_BASE_OFFSET, encodingLength);
125145
throw TranslatedException.decodeThrowable(encoding);
126146
}
127147

test/jdk/jdk/internal/vm/TestTranslatedException.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ private void encodeDecode(Throwable throwable) throws Exception {
8080
bufferSize = -res;
8181
} else {
8282
try {
83-
VMSupport.decodeAndThrowThrowable(buffer);
83+
VMSupport.decodeAndThrowThrowable(buffer, true);
8484
throw new AssertionError("expected decodeAndThrowThrowable to throw an exception");
8585
} catch (Throwable decoded) {
8686
assertThrowableEquals(throwable, decoded);

0 commit comments

Comments
 (0)