From 91692e0768c7a78adde235b56db72916ae8d75b2 Mon Sep 17 00:00:00 2001 From: Alfred Yeung Date: Sat, 28 May 2022 07:15:39 -0700 Subject: [PATCH] Don't use sun.misc.Unsafe when IKVM.NET is used (#9042) Motivation: IKVM.NET seems to ship a bug sun.misc.Unsafe class, for this reason we should better disable our sun.misc.Unsafe usage when we detect IKVM.NET is used. Modifications: Check if IKVM.NET is used and if so do not use sun.misc.Unsafe by default. Result: Fixes (#9035) and (#8916). --- .../util/internal/PlatformDependent.java | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/common/src/main/java/io/netty/util/internal/PlatformDependent.java b/common/src/main/java/io/netty/util/internal/PlatformDependent.java index a43a534424a..f05252f574f 100644 --- a/common/src/main/java/io/netty/util/internal/PlatformDependent.java +++ b/common/src/main/java/io/netty/util/internal/PlatformDependent.java @@ -75,6 +75,7 @@ public final class PlatformDependent { private static final boolean IS_WINDOWS = isWindows0(); private static final boolean IS_OSX = isOsx0(); private static final boolean IS_J9_JVM = isJ9Jvm0(); + private static final boolean IS_IVKVM_DOT_NET = isIkvmDotNet0(); private static final boolean MAYBE_SUPER_USER; @@ -971,6 +972,12 @@ private static Throwable unsafeUnavailabilityCause0() { logger.debug("sun.misc.Unsafe: unavailable (Android)"); return new UnsupportedOperationException("sun.misc.Unsafe: unavailable (Android)"); } + + if (isIkvmDotNet()) { + logger.debug("sun.misc.Unsafe: unavailable (IKVM.NET)"); + return new UnsupportedOperationException("sun.misc.Unsafe: unavailable (IKVM.NET)"); + } + Throwable cause = PlatformDependent0.getUnsafeUnavailabilityCause(); if (cause != null) { return cause; @@ -1000,6 +1007,18 @@ private static boolean isJ9Jvm0() { return vmName.startsWith("ibm j9") || vmName.startsWith("eclipse openj9"); } + /** + * Returns {@code true} if the running JVM is IKVM.NET, {@code false} otherwise. + */ + public static boolean isIkvmDotNet() { + return IS_IVKVM_DOT_NET; + } + + private static boolean isIkvmDotNet0() { + String vmName = SystemPropertyUtil.get("java.vm.name", "").toUpperCase(Locale.US); + return vmName.equals("IKVM.NET"); + } + private static long maxDirectMemory0() { long maxDirectMemory = 0;