Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update ClassLoader.findNative to support JEP 472 #20840

Merged
merged 1 commit into from
Dec 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion jcl/src/java.base/share/classes/java/lang/Access.java
Original file line number Diff line number Diff line change
Expand Up @@ -592,7 +592,7 @@ public PrintStream initialSystemErr() {
/*[ENDIF] JAVA_SPEC_VERSION >= 23 */

public long findNative(ClassLoader loader, String entryName) {
return ClassLoader.findNative(loader, entryName);
return ClassLoader.findNative0(loader, entryName);
}

@Override
Expand Down
20 changes: 19 additions & 1 deletion jcl/src/java.base/share/classes/java/lang/ClassLoader.java
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,10 @@
import jdk.internal.reflect.CallerSensitiveAdapter;
/*[ENDIF] JAVA_SPEC_VERSION >= 18 */

/*[IF JAVA_SPEC_VERSION >= 24]*/
import jdk.internal.reflect.Reflection;
/*[ENDIF] JAVA_SPEC_VERSION >= 24 */

/*[IF CRIU_SUPPORT]*/
import openj9.internal.criu.NotCheckpointSafe;
/*[ENDIF] CRIU_SUPPORT*/
Expand Down Expand Up @@ -2101,13 +2105,27 @@ static void loadLibrary(Class<?> caller, String libName) {
}
}

static long findNative(ClassLoader loader, String entryName) {
/*[IF JAVA_SPEC_VERSION >= 24]*/
static long findNative1(ClassLoader loader, String entryName, Class<?> cls, String javaName) {
long address = findNative0(loader, entryName);
Copy link
Contributor

Choose a reason for hiding this comment

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

Does the spec specify this order of checks? As in, if we look for a method that doesnt exist in a class that doesnt have native access, which error shows up?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

The spec does not explicitly define the order of checks, but we can infer it from the available OpenJDK tests. Currently, we are aligned with the RI since we pass the majority of failing tests in #20354.


if ((loader != null) && (address != 0)) {
Reflection.ensureNativeAccess(cls, cls, javaName, true);
}

return address;
}
/*[ENDIF] JAVA_SPEC_VERSION >= 24 */

static long findNative0(ClassLoader loader, String entryName) {
NativeLibraries nativelib;

if ((loader == null) || (loader == bootstrapClassLoader)) {
nativelib = BootLoader.getNativeLibraries();
} else {
nativelib = loader.nativelibs;
}

return nativelib.find(entryName);
}
/*[ENDIF] JAVA_SPEC_VERSION >= 15 */
Expand Down
3 changes: 2 additions & 1 deletion runtime/oti/vmconstantpool.xml
Original file line number Diff line number Diff line change
Expand Up @@ -474,7 +474,8 @@ SPDX-License-Identifier: EPL-2.0 OR Apache-2.0 OR GPL-2.0-only WITH Classpath-ex
<virtualmethodref class="java/lang/ClassLoader" name="loadClass" signature="(Ljava/lang/String;)Ljava/lang/Class;"/>
<specialmethodref class="java/lang/Thread" name="uncaughtException" signature="(Ljava/lang/Throwable;)V"/>
<specialmethodref class="java/lang/Thread" name="&lt;init>" signature="(Ljava/lang/String;Ljava/lang/Object;IZ)V"/>
<staticmethodref class="java/lang/ClassLoader" name="findNative" signature="(Ljava/lang/ClassLoader;Ljava/lang/String;)J" versions="17-"/>
<staticmethodref class="java/lang/ClassLoader" name="findNative0" signature="(Ljava/lang/ClassLoader;Ljava/lang/String;)J" versions="17-23"/>
<staticmethodref class="java/lang/ClassLoader" name="findNative1" signature="(Ljava/lang/ClassLoader;Ljava/lang/String;Ljava/lang/Class;Ljava/lang/String;)J" versions="24-"/>

<fieldref class="java/lang/J9VMInternals$ClassInitializationLock" name="theClass" signature="Ljava/lang/Class;"/>

Expand Down
31 changes: 23 additions & 8 deletions runtime/vm/bindnatv.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1105,17 +1105,32 @@ lookupJNINative(J9VMThread *currentThread, J9NativeLibrary *nativeLibrary, J9Met
#if JAVA_SPEC_VERSION >= 17
if (NULL == nativeLibrary) {
internalAcquireVMAccess(currentThread);
j9object_t entryName = vm->memoryManagerFunctions->j9gc_createJavaLangString(currentThread, (U_8*)symbolName, strlen(symbolName), 0);
J9MemoryManagerFunctions *mmFuncs = vm->memoryManagerFunctions;
j9object_t entryName = mmFuncs->j9gc_createJavaLangString(currentThread, (U_8*)symbolName, strlen(symbolName), 0);
if (NULL != entryName) {
j9object_t classLoaderObject = J9_CLASS_FROM_METHOD(nativeMethod)->classLoader->classLoaderObject;
J9Method *findNativeMethod = J9VMJAVALANGCLASSLOADER_FINDNATIVE_METHOD(vm);
UDATA args[] = {(UDATA)classLoaderObject, (UDATA)entryName};
internalRunStaticMethod(currentThread, findNativeMethod, TRUE, (sizeof(args) / sizeof(UDATA)), args);
functionAddress = (UDATA*)(*(U_64*)&(currentThread->returnValue));
#if JAVA_SPEC_VERSION >= 24
J9ROMMethod *nativeROMMethod = J9_ROM_METHOD_FROM_RAM_METHOD(nativeMethod);
j9object_t javaName = mmFuncs->j9gc_createJavaLangStringWithUTFCache(currentThread, J9ROMMETHOD_NAME(nativeROMMethod));
if (NULL != javaName)
#endif /* JAVA_SPEC_VERSION >= 24 */
{
J9Class *nativeMethodCls = J9_CLASS_FROM_METHOD(nativeMethod);
j9object_t classLoaderObject = nativeMethodCls->classLoader->classLoaderObject;
#if JAVA_SPEC_VERSION >= 24
j9object_t classObject = nativeMethodCls->classObject;
J9Method *findNativeMethod = J9VMJAVALANGCLASSLOADER_FINDNATIVE1_METHOD(vm);
UDATA args[] = {(UDATA)classLoaderObject, (UDATA)entryName, (UDATA)classObject, (UDATA)javaName};
#else /* JAVA_SPEC_VERSION >= 24 */
J9Method *findNativeMethod = J9VMJAVALANGCLASSLOADER_FINDNATIVE0_METHOD(vm);
UDATA args[] = {(UDATA)classLoaderObject, (UDATA)entryName};
#endif /* JAVA_SPEC_VERSION >= 24 */
internalRunStaticMethod(currentThread, findNativeMethod, TRUE, (sizeof(args) / sizeof(UDATA)), args);
functionAddress = (UDATA*)(*(U_64*)&(currentThread->returnValue));
#if defined(J9VM_OPT_JAVA_OFFLOAD_SUPPORT)
doSwitching = ((UDATA)functionAddress) & J9_NATIVE_LIBRARY_SWITCH_MASK;
functionAddress = (UDATA *)(((UDATA)functionAddress) & ~(UDATA)J9_NATIVE_LIBRARY_SWITCH_MASK);
doSwitching = ((UDATA)functionAddress) & J9_NATIVE_LIBRARY_SWITCH_MASK;
functionAddress = (UDATA *)(((UDATA)functionAddress) & ~(UDATA)J9_NATIVE_LIBRARY_SWITCH_MASK);
#endif /* defined(J9VM_OPT_JAVA_OFFLOAD_SUPPORT) */
}
}
/* always clear pending exception, might retry later */
VM_VMHelpers::clearException(currentThread);
Expand Down