Skip to content

Commit

Permalink
IGNITE-20532 Fix JavaNioAccess handling in JDK 14 and later. (#10969)
Browse files Browse the repository at this point in the history
  • Loading branch information
ivandasch authored Oct 3, 2023
1 parent 97eb317 commit d2af35b
Show file tree
Hide file tree
Showing 8 changed files with 74 additions and 12 deletions.
16 changes: 15 additions & 1 deletion bin/include/jvmdefaults.bat
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ if %java_version% GEQ 9 if %java_version% LSS 11 (
%current_value%
)

if %java_version% GEQ 11 if %java_version% LSS 15 (
if %java_version% GEQ 11 if %java_version% LSS 14 (
set value= ^
--add-exports=java.base/jdk.internal.misc=ALL-UNNAMED ^
--add-exports=java.base/sun.nio.ch=ALL-UNNAMED ^
Expand All @@ -55,8 +55,22 @@ if %java_version% GEQ 11 if %java_version% LSS 15 (
%current_value%
)

if %java_version% GEQ 14 if %java_version% LSS 15 (
set value= ^
--add-exports=java.base/jdk.internal.misc=ALL-UNNAMED ^
--add-exports=java.base/sun.nio.ch=ALL-UNNAMED ^
--add-exports=java.management/com.sun.jmx.mbeanserver=ALL-UNNAMED ^
--add-exports=jdk.internal.jvmstat/sun.jvmstat.monitor=ALL-UNNAMED ^
--add-exports=java.base/sun.reflect.generics.reflectiveObjects=ALL-UNNAMED ^
--add-opens=java.base/jdk.internal.access=ALL-UNNAMED ^
--add-opens=jdk.management/com.sun.management.internal=ALL-UNNAMED ^
--illegal-access=permit ^
%current_value%
)

if %java_version% GEQ 15 (
set value= ^
--add-opens=java.base/jdk.internal.access=ALL-UNNAMED ^
--add-opens=java.base/jdk.internal.misc=ALL-UNNAMED ^
--add-opens=java.base/sun.nio.ch=ALL-UNNAMED ^
--add-opens=java.management/com.sun.jmx.mbeanserver=ALL-UNNAMED ^
Expand Down
15 changes: 14 additions & 1 deletion bin/include/jvmdefaults.sh
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ getJavaSpecificOpts() {
--add-modules=java.xml.bind \
${current_value}"

elif [ "${version}" -ge 11 ] && [ "${version}" -lt 15 ]; then
elif [ "${version}" -ge 11 ] && [ "${version}" -lt 14 ]; then
value="\
--add-exports=java.base/jdk.internal.misc=ALL-UNNAMED \
--add-exports=java.base/sun.nio.ch=ALL-UNNAMED \
Expand All @@ -52,8 +52,21 @@ getJavaSpecificOpts() {
--illegal-access=permit \
${current_value}"

elif [ "${version}" -ge 14 ] && [ "${version}" -lt 15 ]; then
value="\
--add-exports=java.base/jdk.internal.misc=ALL-UNNAMED \
--add-exports=java.base/sun.nio.ch=ALL-UNNAMED \
--add-exports=java.management/com.sun.jmx.mbeanserver=ALL-UNNAMED \
--add-exports=jdk.internal.jvmstat/sun.jvmstat.monitor=ALL-UNNAMED \
--add-exports=java.base/sun.reflect.generics.reflectiveObjects=ALL-UNNAMED \
--add-opens=java.base/jdk.internal.access=ALL-UNNAMED \
--add-opens=jdk.management/com.sun.management.internal=ALL-UNNAMED \
--illegal-access=permit \
${current_value}"

elif [ "${version}" -ge 15 ] ; then
value="\
--add-opens=java.base/jdk.internal.access=ALL-UNNAMED \
--add-opens=java.base/jdk.internal.misc=ALL-UNNAMED \
--add-opens=java.base/sun.nio.ch=ALL-UNNAMED \
--add-opens=java.management/com.sun.jmx.mbeanserver=ALL-UNNAMED \
Expand Down
1 change: 1 addition & 0 deletions deliveries/docker/apache-ignite/run.sh
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,7 @@ elif [ "${version}" -ge 11 ] ; then
--add-exports=jdk.internal.jvmstat/sun.jvmstat.monitor=ALL-UNNAMED \
--add-exports=java.base/sun.reflect.generics.reflectiveObjects=ALL-UNNAMED \
--add-opens=jdk.management/com.sun.management.internal=ALL-UNNAMED \
--add-opens=java.base/jdk.internal.access=ALL-UNNAMED \
--illegal-access=permit \
${JVM_OPTS}"
fi
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,12 @@ public class FeatureChecker {
"--add-exports=jdk.internal.jvmstat/sun.jvmstat.monitor=ALL-UNNAMED\n" +
"--add-exports=java.base/sun.reflect.generics.reflectiveObjects=ALL-UNNAMED\n" +
"--add-opens=jdk.management/com.sun.management.internal=ALL-UNNAMED\n" +
"--add-opens=java.base/jdk.internal.access=ALL-UNNAMED\n" +
"--illegal-access=permit";

/** Required Options to Run on Java 15 and higher. */
public static final String JAVA_15_OPTIONS =
"--add-opens=java.base/jdk.internal.access=ALL-UNNAMED\n" +
"--add-opens=java.base/jdk.internal.misc=ALL-UNNAMED\n" +
"--add-opens=java.base/sun.nio.ch=ALL-UNNAMED\n" +
"--add-opens=java.management/com.sun.jmx.mbeanserver=ALL-UNNAMED\n" +
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,12 @@ public abstract class GridUnsafe {
/** Whether to use newDirectByteBuffer(long, long) constructor */
private static final boolean IS_DIRECT_BUF_LONG_CAP = majorJavaVersion(jdkVersion()) >= 21;

/** Whether {@link jdk.internal.access.JavaNioAccess} has new byte buffer api. */
private static final boolean HAS_JAVA_NIO_ACCESS_NEW_API = majorJavaVersion(jdkVersion()) >= 14;

/** Whether {@link jdk.internal.access.JavaNioAccess#newDirectByteBuffer} has param of type {@link java.lang.foreign.MemorySegment}. */
private static final boolean HAS_JAVA_NIO_ACCESS_MEMORY_SEGMENT_PARAM = majorJavaVersion(jdkVersion()) >= 19;

/** Cleaner code for direct {@code java.nio.ByteBuffer}. */
private static final DirectBufferCleaner DIRECT_BUF_CLEANER =
majorJavaVersion(jdkVersion()) < 9
Expand Down Expand Up @@ -174,6 +180,9 @@ public abstract class GridUnsafe {
directBufCtor = createAndTestNewDirectBufferCtor();
}
catch (Exception e) {
if (!HAS_JAVA_NIO_ACCESS_NEW_API)
throw e;

try {
nioAccessObj = javaNioAccessObject();
directBufMtd = newDirectBufferMethod(nioAccessObj);
Expand Down Expand Up @@ -257,7 +266,11 @@ else if (NEW_DIRECT_BUF_CONSTRUCTOR != null)
@NotNull Method newDirectBufMtd,
@NotNull Object javaNioAccessObj) {
try {
ByteBuffer buf = (ByteBuffer)newDirectBufMtd.invoke(javaNioAccessObj, ptr, len, null);
ByteBuffer buf;
if (HAS_JAVA_NIO_ACCESS_NEW_API)
buf = (ByteBuffer)newDirectBufMtd.invoke(javaNioAccessObj, ptr, len, null, null);
else
buf = (ByteBuffer)newDirectBufMtd.invoke(javaNioAccessObj, ptr, len, null);

assert buf.isDirect();

Expand Down Expand Up @@ -1617,17 +1630,17 @@ private static long bufferAddressOffset() {
* @throws RuntimeException If getting access to the private API is failed.
*/
private static Object javaNioAccessObject() {
String pkgName = miscPackage();
String pkgName = getSharedSecretsPackage();

try {
Class<?> cls = Class.forName(pkgName + ".misc.SharedSecrets");
Class<?> cls = Class.forName(pkgName + ".SharedSecrets");

Method mth = cls.getMethod("getJavaNioAccess");

return mth.invoke(null);
}
catch (ReflectiveOperationException e) {
throw new RuntimeException(pkgName + ".misc.JavaNioAccess class is unavailable."
throw new RuntimeException(pkgName + ".JavaNioAccess class is unavailable."
+ FeatureChecker.JAVA_VER_SPECIFIC_WARN, e);
}
}
Expand All @@ -1644,24 +1657,40 @@ private static Method newDirectBufferMethod(Object nioAccessObj) {
try {
Class<?> cls = nioAccessObj.getClass();

Method mtd = IS_DIRECT_BUF_LONG_CAP ? cls.getMethod("newDirectByteBuffer", long.class, long.class, Object.class) :
cls.getMethod("newDirectByteBuffer", long.class, int.class, Object.class);
Method mtd;
if (HAS_JAVA_NIO_ACCESS_NEW_API) {
Class<?> forthParamCls;
if (HAS_JAVA_NIO_ACCESS_MEMORY_SEGMENT_PARAM)
forthParamCls = Class.forName("java.lang.foreign.MemorySegment");
else
forthParamCls = Class.forName("jdk.internal.access.foreign.MemorySegmentProxy");

mtd = cls.getMethod("newDirectByteBuffer", long.class, int.class, Object.class, forthParamCls);
}
else
mtd = cls.getMethod("newDirectByteBuffer", long.class, int.class, Object.class);

mtd.setAccessible(true);

return mtd;
}
catch (ReflectiveOperationException e) {
throw new RuntimeException(miscPackage() + ".JavaNioAccess#newDirectByteBuffer() method is unavailable."
throw new RuntimeException(getSharedSecretsPackage() + ".JavaNioAccess#newDirectByteBuffer() method is unavailable."
+ FeatureChecker.JAVA_VER_SPECIFIC_WARN, e);
}
}

/** */
@NotNull private static String miscPackage() {
@NotNull private static String getSharedSecretsPackage() {
int javaVer = majorJavaVersion(jdkVersion());

return javaVer < 9 ? "sun" : "jdk.internal";
if (javaVer < 9)
return "sun.misc";

if (javaVer < 12)
return "jdk.internal.misc";

return "jdk.internal.access";
}

/**
Expand Down
1 change: 1 addition & 0 deletions modules/platforms/cpp/core/src/ignition.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,7 @@ namespace ignite
// Those are only needed for Java 15+, but Java 15 can not be detected easily using JNI,
// so just putting it here in case we are running on 15+. It is OK to have them on Java 9-14 too.
// See https://docs.oracle.com/en/java/javase/17/docs/specs/jni/functions.html#getversion
opts.push_back(CopyChars("--add-opens=java.base/jdk.internal.access=ALL-UNNAMED"));
opts.push_back(CopyChars("--add-opens=java.base/jdk.internal.misc=ALL-UNNAMED"));
opts.push_back(CopyChars("--add-opens=java.base/sun.nio.ch=ALL-UNNAMED"));
opts.push_back(CopyChars("--add-opens=java.management/com.sun.jmx.mbeanserver=ALL-UNNAMED"));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ internal sealed unsafe partial class Jvm
"--add-opens=jdk.management/com.sun.management.internal=ALL-UNNAMED",
"--illegal-access=permit",

"--add-opens=java.base/jdk.internal.access=ALL-UNNAMED",
"--add-opens=java.base/jdk.internal.misc=ALL-UNNAMED",
"--add-opens=java.base/sun.nio.ch=ALL-UNNAMED",
"--add-opens=java.management/com.sun.jmx.mbeanserver=ALL-UNNAMED",
Expand Down
3 changes: 2 additions & 1 deletion parent/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -918,7 +918,7 @@
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<configuration>
<argLine>--add-exports=java.base/jdk.internal.misc=ALL-UNNAMED --add-exports=java.base/sun.nio.ch=ALL-UNNAMED --add-exports=java.management/com.sun.jmx.mbeanserver=ALL-UNNAMED --add-exports=jdk.internal.jvmstat/sun.jvmstat.monitor=ALL-UNNAMED --add-exports=java.base/sun.reflect.generics.reflectiveObjects=ALL-UNNAMED --add-opens=jdk.management/com.sun.management.internal=ALL-UNNAMED --illegal-access=permit</argLine>
<argLine>--add-exports=java.base/jdk.internal.misc=ALL-UNNAMED --add-exports=java.base/sun.nio.ch=ALL-UNNAMED --add-exports=java.management/com.sun.jmx.mbeanserver=ALL-UNNAMED --add-exports=jdk.internal.jvmstat/sun.jvmstat.monitor=ALL-UNNAMED --add-exports=java.base/sun.reflect.generics.reflectiveObjects=ALL-UNNAMED --add-opens=jdk.management/com.sun.management.internal=ALL-UNNAMED --add-opens=java.base/jdk.internal.access=ALL-UNNAMED --illegal-access=permit</argLine>
</configuration>
</plugin>

Expand Down Expand Up @@ -1012,6 +1012,7 @@
<artifactId>maven-surefire-plugin</artifactId>
<configuration>
<argLine>
--add-opens=java.base/jdk.internal.access=ALL-UNNAMED
--add-opens=java.base/jdk.internal.misc=ALL-UNNAMED
--add-opens=java.base/sun.nio.ch=ALL-UNNAMED
--add-opens=java.management/com.sun.jmx.mbeanserver=ALL-UNNAMED
Expand Down

0 comments on commit d2af35b

Please sign in to comment.