Skip to content

Commit

Permalink
Merge pull request #20840 from babsingh/main12
Browse files Browse the repository at this point in the history
Update ClassLoader.findNative to support JEP 472
  • Loading branch information
tajila authored Dec 18, 2024
2 parents f806233 + 6f9e4d2 commit 85b9c02
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 11 deletions.
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);

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

0 comments on commit 85b9c02

Please sign in to comment.