Skip to content

Commit

Permalink
8318737: Fallback linker passes bad JNI handle
Browse files Browse the repository at this point in the history
Reviewed-by: alanb
  • Loading branch information
JornVernee committed Oct 26, 2023
1 parent 9864951 commit 3885dc5
Show file tree
Hide file tree
Showing 7 changed files with 32 additions and 16 deletions.
8 changes: 2 additions & 6 deletions src/hotspot/share/runtime/jniHandles.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -199,13 +199,9 @@ jobjectRefType JNIHandles::handle_type(JavaThread* thread, jobject handle) {
default:
ShouldNotReachHere();
}
} else {
} else if (is_local_handle(thread, handle) || is_frame_handle(thread, handle)) {
// Not in global storage. Might be a local handle.
if (is_local_handle(thread, handle) || is_frame_handle(thread, handle)) {
result = JNILocalRefType;
} else {
ShouldNotReachHere();
}
result = JNILocalRefType;
}
return result;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,11 +43,14 @@ private static boolean tryLoadLibrary() {
public Boolean run() {
try {
System.loadLibrary("fallbackLinker");
init();
return true;
} catch (UnsatisfiedLinkError ule) {
return false;
}
if (!init()) {
// library failed to initialize. Do not silently mark as unsupported
throw new ExceptionInInitializerError("Fallback library failed to initialize");
}
return true;
}
});
}
Expand Down Expand Up @@ -201,7 +204,7 @@ private static void checkStatus(int code) {
}
}

private static native void init();
private static native boolean init();

private static native long sizeofCif();

Expand Down
23 changes: 20 additions & 3 deletions src/java.base/share/native/libfallbackLinker/fallbackLinker.c
Original file line number Diff line number Diff line change
Expand Up @@ -42,12 +42,29 @@ static jclass LibFallback_class;
static jmethodID LibFallback_doUpcall_ID;
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) {
(*env)->GetJavaVM(env, &VM);
LibFallback_class = (*env)->FindClass(env, "jdk/internal/foreign/abi/fallback/LibFallback");
jint result = (*env)->GetJavaVM(env, &VM);
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_class, "doUpcall", LibFallback_doUpcall_sig);
CHECK_NULL(LibFallback_doUpcall_ID)

return JNI_TRUE;
}

JNIEXPORT jlong JNICALL
Expand Down
2 changes: 1 addition & 1 deletion test/jdk/java/foreign/TestDowncallScope.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
* @modules java.base/jdk.internal.foreign
* @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
* TestDowncallScope
*
Expand Down
2 changes: 1 addition & 1 deletion test/jdk/java/foreign/TestDowncallStack.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
* @modules java.base/jdk.internal.foreign
* @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
* TestDowncallStack
*/
Expand Down
2 changes: 1 addition & 1 deletion test/jdk/java/foreign/TestUpcallScope.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
* @modules java.base/jdk.internal.foreign
* @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
* TestUpcallScope
*/
Expand Down
2 changes: 1 addition & 1 deletion test/jdk/java/foreign/TestUpcallStack.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
* @modules java.base/jdk.internal.foreign
* @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
* TestUpcallStack
*/
Expand Down

3 comments on commit 3885dc5

@openjdk-notifier
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@shipilev
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

/backport jdk21u-dev

@openjdk
Copy link

@openjdk openjdk bot commented on 3885dc5 Feb 19, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@shipilev the backport was successfully created on the branch backport-shipilev-3885dc5b in my personal fork of openjdk/jdk21u-dev. To create a pull request with this backport targeting openjdk/jdk21u-dev:master, just click the following link:

➡️ Create pull request

The title of the pull request is automatically filled in correctly and below you find a suggestion for the pull request body:

Hi all,

This pull request contains a backport of commit 3885dc5b from the openjdk/jdk repository.

The commit being backported was authored by Jorn Vernee on 26 Oct 2023 and was reviewed by Alan Bateman.

Thanks!

If you need to update the source branch of the pull then run the following commands in a local clone of your personal fork of openjdk/jdk21u-dev:

$ git fetch https://github.com/openjdk-bots/jdk21u-dev.git backport-shipilev-3885dc5b:backport-shipilev-3885dc5b
$ git checkout backport-shipilev-3885dc5b
# make changes
$ git add paths/to/changed/files
$ git commit --message 'Describe additional changes made'
$ git push https://github.com/openjdk-bots/jdk21u-dev.git backport-shipilev-3885dc5b

Please sign in to comment.