8318737: Fallback linker passes bad JNI handle

Reviewed-by: alanb
This commit is contained in:
Jorn Vernee 2023-10-26 15:54:02 +00:00
parent 9864951dce
commit 3885dc5b9a
7 changed files with 32 additions and 16 deletions

View File

@ -199,13 +199,9 @@ jobjectRefType JNIHandles::handle_type(JavaThread* thread, jobject handle) {
default: default:
ShouldNotReachHere(); ShouldNotReachHere();
} }
} else { } else if (is_local_handle(thread, handle) || is_frame_handle(thread, handle)) {
// Not in global storage. Might be a local handle. // Not in global storage. Might be a local handle.
if (is_local_handle(thread, handle) || is_frame_handle(thread, handle)) { result = JNILocalRefType;
result = JNILocalRefType;
} else {
ShouldNotReachHere();
}
} }
return result; return result;
} }

View File

@ -43,11 +43,14 @@ final class LibFallback {
public Boolean run() { public Boolean run() {
try { try {
System.loadLibrary("fallbackLinker"); System.loadLibrary("fallbackLinker");
init();
return true;
} catch (UnsatisfiedLinkError ule) { } catch (UnsatisfiedLinkError ule) {
return false; return false;
} }
if (!init()) {
// library failed to initialize. Do not silently mark as unsupported
throw new ExceptionInInitializerError("Fallback library failed to initialize");
}
return true;
} }
}); });
} }
@ -201,7 +204,7 @@ final class LibFallback {
} }
} }
private static native void init(); private static native boolean init();
private static native long sizeofCif(); private static native long sizeofCif();

View File

@ -42,12 +42,29 @@ static jclass LibFallback_class;
static jmethodID LibFallback_doUpcall_ID; static jmethodID LibFallback_doUpcall_ID;
static const char* LibFallback_doUpcall_sig = "(JJLjava/lang/invoke/MethodHandle;)V"; static const char* LibFallback_doUpcall_sig = "(JJLjava/lang/invoke/MethodHandle;)V";
JNIEXPORT void JNICALL #define CHECK_NULL(expr) \
if (expr == NULL) { \
return JNI_FALSE; \
}
JNIEXPORT jboolean JNICALL
Java_jdk_internal_foreign_abi_fallback_LibFallback_init(JNIEnv* env, jclass cls) { Java_jdk_internal_foreign_abi_fallback_LibFallback_init(JNIEnv* env, jclass cls) {
(*env)->GetJavaVM(env, &VM); jint result = (*env)->GetJavaVM(env, &VM);
LibFallback_class = (*env)->FindClass(env, "jdk/internal/foreign/abi/fallback/LibFallback"); if (result != 0) {
return JNI_FALSE;
}
jclass LibFallback_class_local = (*env)->FindClass(env, "jdk/internal/foreign/abi/fallback/LibFallback");
CHECK_NULL(LibFallback_class_local)
LibFallback_class = (*env)->NewGlobalRef(env, LibFallback_class_local);
CHECK_NULL(LibFallback_class)
LibFallback_doUpcall_ID = (*env)->GetStaticMethodID(env, LibFallback_doUpcall_ID = (*env)->GetStaticMethodID(env,
LibFallback_class, "doUpcall", LibFallback_doUpcall_sig); LibFallback_class, "doUpcall", LibFallback_doUpcall_sig);
CHECK_NULL(LibFallback_doUpcall_ID)
return JNI_TRUE;
} }
JNIEXPORT jlong JNICALL JNIEXPORT jlong JNICALL

View File

@ -26,7 +26,7 @@
* @modules java.base/jdk.internal.foreign * @modules java.base/jdk.internal.foreign
* @build NativeTestHelper CallGeneratorHelper TestDowncallBase * @build NativeTestHelper CallGeneratorHelper TestDowncallBase
* *
* @run testng/othervm -XX:+IgnoreUnrecognizedVMOptions -XX:-VerifyDependencies * @run testng/othervm -Xcheck:jni -XX:+IgnoreUnrecognizedVMOptions -XX:-VerifyDependencies
* --enable-native-access=ALL-UNNAMED -Dgenerator.sample.factor=17 * --enable-native-access=ALL-UNNAMED -Dgenerator.sample.factor=17
* TestDowncallScope * TestDowncallScope
* *

View File

@ -26,7 +26,7 @@
* @modules java.base/jdk.internal.foreign * @modules java.base/jdk.internal.foreign
* @build NativeTestHelper CallGeneratorHelper TestDowncallBase * @build NativeTestHelper CallGeneratorHelper TestDowncallBase
* *
* @run testng/othervm -XX:+IgnoreUnrecognizedVMOptions -XX:-VerifyDependencies * @run testng/othervm -Xcheck:jni -XX:+IgnoreUnrecognizedVMOptions -XX:-VerifyDependencies
* --enable-native-access=ALL-UNNAMED -Dgenerator.sample.factor=17 * --enable-native-access=ALL-UNNAMED -Dgenerator.sample.factor=17
* TestDowncallStack * TestDowncallStack
*/ */

View File

@ -26,7 +26,7 @@
* @modules java.base/jdk.internal.foreign * @modules java.base/jdk.internal.foreign
* @build NativeTestHelper CallGeneratorHelper TestUpcallBase * @build NativeTestHelper CallGeneratorHelper TestUpcallBase
* *
* @run testng/othervm -XX:+IgnoreUnrecognizedVMOptions -XX:-VerifyDependencies * @run testng/othervm -Xcheck:jni -XX:+IgnoreUnrecognizedVMOptions -XX:-VerifyDependencies
* --enable-native-access=ALL-UNNAMED -Dgenerator.sample.factor=17 * --enable-native-access=ALL-UNNAMED -Dgenerator.sample.factor=17
* TestUpcallScope * TestUpcallScope
*/ */

View File

@ -27,7 +27,7 @@
* @modules java.base/jdk.internal.foreign * @modules java.base/jdk.internal.foreign
* @build NativeTestHelper CallGeneratorHelper TestUpcallBase * @build NativeTestHelper CallGeneratorHelper TestUpcallBase
* *
* @run testng/othervm -XX:+IgnoreUnrecognizedVMOptions -XX:-VerifyDependencies * @run testng/othervm -Xcheck:jni -XX:+IgnoreUnrecognizedVMOptions -XX:-VerifyDependencies
* --enable-native-access=ALL-UNNAMED -Dgenerator.sample.factor=17 * --enable-native-access=ALL-UNNAMED -Dgenerator.sample.factor=17
* TestUpcallStack * TestUpcallStack
*/ */