From ca155cae76c7caf7d6c4bbf27e9ac22305d2dbf0 Mon Sep 17 00:00:00 2001 From: Foivos Zakkak Date: Wed, 8 Feb 2023 16:03:18 +0200 Subject: [PATCH] Re-initialize platform dependent netty classes/values at runtime Platform dependent classes should not be initialized at build time to avoid issues when running the native executable on a different platform. This patch registers two platform dependent classes for runtime re-initialization and disables the use of `Unsafe` in netty. Unfortunately when re-initializing `PlatformDependent0` it fails to properly setup unsafe accesses resulting in `NullPointerExceptions` when invoking `putByte`. Disabling unsafe accesses for netty works around this. The above changes result in Quarkus native applications to respect arguments like `-Dio.netty.maxDirectMemory=1024` and `-XX:MaxDirectMemorySize=1g`. Fixes #17839 --- .../java/io/quarkus/netty/deployment/NettyProcessor.java | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/extensions/netty/deployment/src/main/java/io/quarkus/netty/deployment/NettyProcessor.java b/extensions/netty/deployment/src/main/java/io/quarkus/netty/deployment/NettyProcessor.java index 069fb756162a2..de1471c3ae4df 100644 --- a/extensions/netty/deployment/src/main/java/io/quarkus/netty/deployment/NettyProcessor.java +++ b/extensions/netty/deployment/src/main/java/io/quarkus/netty/deployment/NettyProcessor.java @@ -93,7 +93,8 @@ NativeImageConfigBuildItem build( String maxOrder = calculateMaxOrder(config.allocatorMaxOrder, minMaxOrderBuildItems, false); NativeImageConfigBuildItem.Builder builder = NativeImageConfigBuildItem.builder() - //.addNativeImageSystemProperty("io.netty.noUnsafe", "true") + // disable unsafe usage to allow io.netty.internal.PlarformDependent0 to be reinitialized without issues + .addNativeImageSystemProperty("io.netty.noUnsafe", "true") // Use small chunks to avoid a lot of wasted space. Default is 16mb * arenas (derived from core count) // Since buffers are cached to threads, the malloc overhead is temporary anyway .addNativeImageSystemProperty("io.netty.allocator.maxOrder", maxOrder) @@ -109,6 +110,9 @@ NativeImageConfigBuildItem build( .addRuntimeInitializedClass("io.netty.buffer.ByteBufUtil") // The default channel id uses the process id, it should not be cached in the native image. .addRuntimeInitializedClass("io.netty.channel.DefaultChannelId") + // Make sure to re-initialize platform dependent classes/values at runtime + .addRuntimeReinitializedClass("io.netty.util.internal.PlatformDependent") + .addRuntimeReinitializedClass("io.netty.util.internal.PlatformDependent0") .addNativeImageSystemProperty("io.netty.leakDetection.level", "DISABLED"); if (QuarkusClassLoader.isClassPresentAtRuntime("io.netty.handler.codec.http.HttpObjectEncoder")) {