Skip to content

Commit 85b9c02

Browse files
authored
Merge pull request #20840 from babsingh/main12
Update ClassLoader.findNative to support JEP 472
2 parents f806233 + 6f9e4d2 commit 85b9c02

File tree

4 files changed

+45
-11
lines changed

4 files changed

+45
-11
lines changed

jcl/src/java.base/share/classes/java/lang/Access.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -592,7 +592,7 @@ public PrintStream initialSystemErr() {
592592
/*[ENDIF] JAVA_SPEC_VERSION >= 23 */
593593

594594
public long findNative(ClassLoader loader, String entryName) {
595-
return ClassLoader.findNative(loader, entryName);
595+
return ClassLoader.findNative0(loader, entryName);
596596
}
597597

598598
@Override

jcl/src/java.base/share/classes/java/lang/ClassLoader.java

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,10 @@
7272
import jdk.internal.reflect.CallerSensitiveAdapter;
7373
/*[ENDIF] JAVA_SPEC_VERSION >= 18 */
7474

75+
/*[IF JAVA_SPEC_VERSION >= 24]*/
76+
import jdk.internal.reflect.Reflection;
77+
/*[ENDIF] JAVA_SPEC_VERSION >= 24 */
78+
7579
/*[IF CRIU_SUPPORT]*/
7680
import openj9.internal.criu.NotCheckpointSafe;
7781
/*[ENDIF] CRIU_SUPPORT*/
@@ -2101,13 +2105,27 @@ static void loadLibrary(Class<?> caller, String libName) {
21012105
}
21022106
}
21032107

2104-
static long findNative(ClassLoader loader, String entryName) {
2108+
/*[IF JAVA_SPEC_VERSION >= 24]*/
2109+
static long findNative1(ClassLoader loader, String entryName, Class<?> cls, String javaName) {
2110+
long address = findNative0(loader, entryName);
2111+
2112+
if ((loader != null) && (address != 0)) {
2113+
Reflection.ensureNativeAccess(cls, cls, javaName, true);
2114+
}
2115+
2116+
return address;
2117+
}
2118+
/*[ENDIF] JAVA_SPEC_VERSION >= 24 */
2119+
2120+
static long findNative0(ClassLoader loader, String entryName) {
21052121
NativeLibraries nativelib;
2122+
21062123
if ((loader == null) || (loader == bootstrapClassLoader)) {
21072124
nativelib = BootLoader.getNativeLibraries();
21082125
} else {
21092126
nativelib = loader.nativelibs;
21102127
}
2128+
21112129
return nativelib.find(entryName);
21122130
}
21132131
/*[ENDIF] JAVA_SPEC_VERSION >= 15 */

runtime/oti/vmconstantpool.xml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -474,7 +474,8 @@ SPDX-License-Identifier: EPL-2.0 OR Apache-2.0 OR GPL-2.0-only WITH Classpath-ex
474474
<virtualmethodref class="java/lang/ClassLoader" name="loadClass" signature="(Ljava/lang/String;)Ljava/lang/Class;"/>
475475
<specialmethodref class="java/lang/Thread" name="uncaughtException" signature="(Ljava/lang/Throwable;)V"/>
476476
<specialmethodref class="java/lang/Thread" name="&lt;init>" signature="(Ljava/lang/String;Ljava/lang/Object;IZ)V"/>
477-
<staticmethodref class="java/lang/ClassLoader" name="findNative" signature="(Ljava/lang/ClassLoader;Ljava/lang/String;)J" versions="17-"/>
477+
<staticmethodref class="java/lang/ClassLoader" name="findNative0" signature="(Ljava/lang/ClassLoader;Ljava/lang/String;)J" versions="17-23"/>
478+
<staticmethodref class="java/lang/ClassLoader" name="findNative1" signature="(Ljava/lang/ClassLoader;Ljava/lang/String;Ljava/lang/Class;Ljava/lang/String;)J" versions="24-"/>
478479

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

runtime/vm/bindnatv.cpp

Lines changed: 23 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1105,17 +1105,32 @@ lookupJNINative(J9VMThread *currentThread, J9NativeLibrary *nativeLibrary, J9Met
11051105
#if JAVA_SPEC_VERSION >= 17
11061106
if (NULL == nativeLibrary) {
11071107
internalAcquireVMAccess(currentThread);
1108-
j9object_t entryName = vm->memoryManagerFunctions->j9gc_createJavaLangString(currentThread, (U_8*)symbolName, strlen(symbolName), 0);
1108+
J9MemoryManagerFunctions *mmFuncs = vm->memoryManagerFunctions;
1109+
j9object_t entryName = mmFuncs->j9gc_createJavaLangString(currentThread, (U_8*)symbolName, strlen(symbolName), 0);
11091110
if (NULL != entryName) {
1110-
j9object_t classLoaderObject = J9_CLASS_FROM_METHOD(nativeMethod)->classLoader->classLoaderObject;
1111-
J9Method *findNativeMethod = J9VMJAVALANGCLASSLOADER_FINDNATIVE_METHOD(vm);
1112-
UDATA args[] = {(UDATA)classLoaderObject, (UDATA)entryName};
1113-
internalRunStaticMethod(currentThread, findNativeMethod, TRUE, (sizeof(args) / sizeof(UDATA)), args);
1114-
functionAddress = (UDATA*)(*(U_64*)&(currentThread->returnValue));
1111+
#if JAVA_SPEC_VERSION >= 24
1112+
J9ROMMethod *nativeROMMethod = J9_ROM_METHOD_FROM_RAM_METHOD(nativeMethod);
1113+
j9object_t javaName = mmFuncs->j9gc_createJavaLangStringWithUTFCache(currentThread, J9ROMMETHOD_NAME(nativeROMMethod));
1114+
if (NULL != javaName)
1115+
#endif /* JAVA_SPEC_VERSION >= 24 */
1116+
{
1117+
J9Class *nativeMethodCls = J9_CLASS_FROM_METHOD(nativeMethod);
1118+
j9object_t classLoaderObject = nativeMethodCls->classLoader->classLoaderObject;
1119+
#if JAVA_SPEC_VERSION >= 24
1120+
j9object_t classObject = nativeMethodCls->classObject;
1121+
J9Method *findNativeMethod = J9VMJAVALANGCLASSLOADER_FINDNATIVE1_METHOD(vm);
1122+
UDATA args[] = {(UDATA)classLoaderObject, (UDATA)entryName, (UDATA)classObject, (UDATA)javaName};
1123+
#else /* JAVA_SPEC_VERSION >= 24 */
1124+
J9Method *findNativeMethod = J9VMJAVALANGCLASSLOADER_FINDNATIVE0_METHOD(vm);
1125+
UDATA args[] = {(UDATA)classLoaderObject, (UDATA)entryName};
1126+
#endif /* JAVA_SPEC_VERSION >= 24 */
1127+
internalRunStaticMethod(currentThread, findNativeMethod, TRUE, (sizeof(args) / sizeof(UDATA)), args);
1128+
functionAddress = (UDATA*)(*(U_64*)&(currentThread->returnValue));
11151129
#if defined(J9VM_OPT_JAVA_OFFLOAD_SUPPORT)
1116-
doSwitching = ((UDATA)functionAddress) & J9_NATIVE_LIBRARY_SWITCH_MASK;
1117-
functionAddress = (UDATA *)(((UDATA)functionAddress) & ~(UDATA)J9_NATIVE_LIBRARY_SWITCH_MASK);
1130+
doSwitching = ((UDATA)functionAddress) & J9_NATIVE_LIBRARY_SWITCH_MASK;
1131+
functionAddress = (UDATA *)(((UDATA)functionAddress) & ~(UDATA)J9_NATIVE_LIBRARY_SWITCH_MASK);
11181132
#endif /* defined(J9VM_OPT_JAVA_OFFLOAD_SUPPORT) */
1133+
}
11191134
}
11201135
/* always clear pending exception, might retry later */
11211136
VM_VMHelpers::clearException(currentThread);

0 commit comments

Comments
 (0)