From 0fffc844d6d4c8e196a8448a45e7ff542446f708 Mon Sep 17 00:00:00 2001 From: Norman Maurer Date: Tue, 29 Aug 2017 14:13:49 +0200 Subject: [PATCH] Only load native transport if running architecture match the compiled library architecture. Motivation: We should only try to load the native artifacts if the architecture we are currently running on is the same as the one the native libraries were compiled for. Modifications: Include architecture in native lib name and append the current arch when trying to load these. This will fail then if its not the same as the arch of the compiled arch. Result: Fixes [#7150]. --- .../util/internal/PlatformDependent.java | 95 +++++++++++++++++++ .../java/io/netty/handler/ssl/OpenSsl.java | 95 +------------------ transport-native-epoll/pom.xml | 4 +- .../java/io/netty/channel/epoll/Native.java | 3 +- transport-native-kqueue/pom.xml | 12 +-- .../java/io/netty/channel/kqueue/Native.java | 3 +- 6 files changed, 111 insertions(+), 101 deletions(-) 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 662013929a..23f8cc5276 100644 --- a/common/src/main/java/io/netty/util/internal/PlatformDependent.java +++ b/common/src/main/java/io/netty/util/internal/PlatformDependent.java @@ -92,6 +92,8 @@ public final class PlatformDependent { private static final File TMPDIR = tmpdir0(); private static final int BIT_MODE = bitMode0(); + private static final String NORMALIZED_ARCH = normalizeArch(SystemPropertyUtil.get("os.arch", "")); + private static final String NORMALIZED_OS = normalizeOs(SystemPropertyUtil.get("os.name", "")); private static final int ADDRESS_SIZE = addressSize0(); private static final boolean USE_DIRECT_BUFFER_NO_CLEANER; @@ -1238,6 +1240,99 @@ public final class PlatformDependent { } } + public static String normalizedArch() { + return NORMALIZED_ARCH; + } + + public static String normalizedOs() { + return NORMALIZED_OS; + } + + private static String normalize(String value) { + return value.toLowerCase(Locale.US).replaceAll("[^a-z0-9]+", ""); + } + + private static String normalizeArch(String value) { + value = normalize(value); + if (value.matches("^(x8664|amd64|ia32e|em64t|x64)$")) { + return "x86_64"; + } + if (value.matches("^(x8632|x86|i[3-6]86|ia32|x32)$")) { + return "x86_32"; + } + if (value.matches("^(ia64|itanium64)$")) { + return "itanium_64"; + } + if (value.matches("^(sparc|sparc32)$")) { + return "sparc_32"; + } + if (value.matches("^(sparcv9|sparc64)$")) { + return "sparc_64"; + } + if (value.matches("^(arm|arm32)$")) { + return "arm_32"; + } + if ("aarch64".equals(value)) { + return "aarch_64"; + } + if (value.matches("^(ppc|ppc32)$")) { + return "ppc_32"; + } + if ("ppc64".equals(value)) { + return "ppc_64"; + } + if ("ppc64le".equals(value)) { + return "ppcle_64"; + } + if ("s390".equals(value)) { + return "s390_32"; + } + if ("s390x".equals(value)) { + return "s390_64"; + } + + return "unknown"; + } + + private static String normalizeOs(String value) { + value = normalize(value); + if (value.startsWith("aix")) { + return "aix"; + } + if (value.startsWith("hpux")) { + return "hpux"; + } + if (value.startsWith("os400")) { + // Avoid the names such as os4000 + if (value.length() <= 5 || !Character.isDigit(value.charAt(5))) { + return "os400"; + } + } + if (value.startsWith("linux")) { + return "linux"; + } + if (value.startsWith("macosx") || value.startsWith("osx")) { + return "osx"; + } + if (value.startsWith("freebsd")) { + return "freebsd"; + } + if (value.startsWith("openbsd")) { + return "openbsd"; + } + if (value.startsWith("netbsd")) { + return "netbsd"; + } + if (value.startsWith("solaris") || value.startsWith("sunos")) { + return "sunos"; + } + if (value.startsWith("windows")) { + return "windows"; + } + + return "unknown"; + } + private static final class AtomicLongCounter extends AtomicLong implements LongCounter { private static final long serialVersionUID = 4074772784610639305L; diff --git a/handler/src/main/java/io/netty/handler/ssl/OpenSsl.java b/handler/src/main/java/io/netty/handler/ssl/OpenSsl.java index e2aca77441..59f009bf4c 100644 --- a/handler/src/main/java/io/netty/handler/ssl/OpenSsl.java +++ b/handler/src/main/java/io/netty/handler/ssl/OpenSsl.java @@ -25,6 +25,7 @@ import io.netty.internal.tcnative.SSLContext; import io.netty.util.ReferenceCountUtil; import io.netty.util.ReferenceCounted; import io.netty.util.internal.NativeLibraryLoader; +import io.netty.util.internal.PlatformDependent; import io.netty.util.internal.SystemPropertyUtil; import io.netty.util.internal.logging.InternalLogger; import io.netty.util.internal.logging.InternalLoggerFactory; @@ -36,7 +37,6 @@ import java.util.Arrays; import java.util.Collections; import java.util.LinkedHashSet; import java.util.List; -import java.util.Locale; import java.util.Set; import static io.netty.handler.ssl.SslUtils.DEFAULT_CIPHER_SUITES; @@ -56,8 +56,6 @@ import static io.netty.handler.ssl.SslUtils.PROTOCOL_TLS_V1_2; public final class OpenSsl { private static final InternalLogger logger = InternalLoggerFactory.getInstance(OpenSsl.class); - private static final String LINUX = "linux"; - private static final String UNKNOWN = "unknown"; private static final Throwable UNAVAILABILITY_CAUSE; static final List DEFAULT_CIPHERS; @@ -404,14 +402,14 @@ public final class OpenSsl { private OpenSsl() { } private static void loadTcNative() throws Exception { - String os = normalizeOs(SystemPropertyUtil.get("os.name", "")); - String arch = normalizeArch(SystemPropertyUtil.get("os.arch", "")); + String os = PlatformDependent.normalizedOs(); + String arch = PlatformDependent.normalizedArch(); Set libNames = new LinkedHashSet(4); // First, try loading the platform-specific library. Platform-specific // libraries will be available if using a tcnative uber jar. libNames.add("netty_tcnative_" + os + '_' + arch); - if (LINUX.equalsIgnoreCase(os)) { + if ("linux".equalsIgnoreCase(os)) { // Fedora SSL lib so naming (libssl.so.10 vs libssl.so.1.0.0).. libNames.add("netty_tcnative_" + os + '_' + arch + "_fedora"); } @@ -426,91 +424,6 @@ public final class OpenSsl { return Library.initialize(); } - private static String normalizeOs(String value) { - value = normalize(value); - if (value.startsWith("aix")) { - return "aix"; - } - if (value.startsWith("hpux")) { - return "hpux"; - } - if (value.startsWith("os400")) { - // Avoid the names such as os4000 - if (value.length() <= 5 || !Character.isDigit(value.charAt(5))) { - return "os400"; - } - } - if (value.startsWith(LINUX)) { - return LINUX; - } - if (value.startsWith("macosx") || value.startsWith("osx")) { - return "osx"; - } - if (value.startsWith("freebsd")) { - return "freebsd"; - } - if (value.startsWith("openbsd")) { - return "openbsd"; - } - if (value.startsWith("netbsd")) { - return "netbsd"; - } - if (value.startsWith("solaris") || value.startsWith("sunos")) { - return "sunos"; - } - if (value.startsWith("windows")) { - return "windows"; - } - - return UNKNOWN; - } - - private static String normalizeArch(String value) { - value = normalize(value); - if (value.matches("^(x8664|amd64|ia32e|em64t|x64)$")) { - return "x86_64"; - } - if (value.matches("^(x8632|x86|i[3-6]86|ia32|x32)$")) { - return "x86_32"; - } - if (value.matches("^(ia64|itanium64)$")) { - return "itanium_64"; - } - if (value.matches("^(sparc|sparc32)$")) { - return "sparc_32"; - } - if (value.matches("^(sparcv9|sparc64)$")) { - return "sparc_64"; - } - if (value.matches("^(arm|arm32)$")) { - return "arm_32"; - } - if ("aarch64".equals(value)) { - return "aarch_64"; - } - if (value.matches("^(ppc|ppc32)$")) { - return "ppc_32"; - } - if ("ppc64".equals(value)) { - return "ppc_64"; - } - if ("ppc64le".equals(value)) { - return "ppcle_64"; - } - if ("s390".equals(value)) { - return "s390_32"; - } - if ("s390x".equals(value)) { - return "s390_64"; - } - - return UNKNOWN; - } - - private static String normalize(String value) { - return value.toLowerCase(Locale.US).replaceAll("[^a-z0-9]+", ""); - } - static void releaseIfNeeded(ReferenceCounted counted) { if (counted.refCnt() > 0) { ReferenceCountUtil.safeRelease(counted); diff --git a/transport-native-epoll/pom.xml b/transport-native-epoll/pom.xml index a84d9631ec..1882867f51 100644 --- a/transport-native-epoll/pom.xml +++ b/transport-native-epoll/pom.xml @@ -144,7 +144,7 @@ build-native-lib - netty_transport_native_epoll + netty_transport_native_epoll_${os.detected.arch} ${project.basedir}/src/main/c ${project.build.outputDirectory}