Repository: netty/netty
Issue: native-image compilation of epoll transport fails
Labels: graal

### Expected behavior
My small repro compiles as a native image and works as expected if I use Nio* family of transports. My expectation was that switching to epoll transport would similarly work.

### Actual behavior
If I switch to epoll (linux-x86_64) I get a compilation error:

```
Error: Classes that should be initialized at run time got initialized during image building:
 io.netty.util.AbstractReferenceCounted the class was requested to be initialized at build time (from the command line). io.netty.util.AbstractReferenceCounted has been initialized without the native-image initialization instrumentation and the stack trace can't be tracked. Try avoiding to initialize the class that caused initialization of io.netty.util.AbstractReferenceCounted

com.oracle.svm.core.util.UserError$UserException: Classes that should be initialized at run time got initialized during image building:
 io.netty.util.AbstractReferenceCounted the class was requested to be initialized at build time (from the command line). io.netty.util.AbstractReferenceCounted has been initialized without the native-image initialization instrumentation and the stack trace can't be tracked. Try avoiding to initialize the class that caused initialization of io.netty.util.AbstractReferenceCounted

	at com.oracle.svm.core.util.UserError.abort(UserError.java:65)
	at com.oracle.svm.hosted.classinitialization.ConfigurableClassInitialization.checkDelayedInitialization(ConfigurableClassInitialization.java:510)
	at com.oracle.svm.hosted.classinitialization.ClassInitializationFeature.duringAnalysis(ClassInitializationFeature.java:187)
	at com.oracle.svm.hosted.NativeImageGenerator.lambda$runPointsToAnalysis$8(NativeImageGenerator.java:710)
	at com.oracle.svm.hosted.FeatureHandler.forEachFeature(FeatureHandler.java:63)
	at com.oracle.svm.hosted.NativeImageGenerator.runPointsToAnalysis(NativeImageGenerator.java:710)
	at com.oracle.svm.hosted.NativeImageGenerator.doRun(NativeImageGenerator.java:530)
	at com.oracle.svm.hosted.NativeImageGenerator.lambda$run$0(NativeImageGenerator.java:445)
	at java.base/java.util.concurrent.ForkJoinTask$AdaptedRunnableAction.exec(ForkJoinTask.java:1407)
	at java.base/java.util.concurrent.ForkJoinTask.doExec(ForkJoinTask.java:290)
	at java.base/java.util.concurrent.ForkJoinPool$WorkQueue.topLevelExec(ForkJoinPool.java:1020)
	at java.base/java.util.concurrent.ForkJoinPool.scan(ForkJoinPool.java:1656)
	at java.base/java.util.concurrent.ForkJoinPool.runWorker(ForkJoinPool.java:1594)
	at java.base/java.util.concurrent.ForkJoinWorkerThread.run(ForkJoinWorkerThread.java:177)
```

### Minimal yet complete reproducer code (or URL to code)

Here's a repro: https://github.com/perezd/netty-epoll-native-repro

w/ Bazel 3.5.0 installed, run this command from the root of the project:

```
bazel run :main-native
```

### Netty version
4.1.52.Final (linux-x86_64)

### JVM version (e.g. `java -version`)
java -version
openjdk version "11.0.8" 2020-07-14
OpenJDK Runtime Environment (build 11.0.8+10-post-Ubuntu-0ubuntu120.04)
OpenJDK 64-Bit Server VM (build 11.0.8+10-post-Ubuntu-0ubuntu120.04, mixed mode, sharing)

### OS version (e.g. `uname -a`)
Linux desktop 5.4.0-48-generic #52-Ubuntu SMP Thu Sep 10 10:58:49 UTC 2020 x86_64 x86_64 x86_64 GNU/Linux

I've searched around and attempted various workarounds but nothing seems to work.


Issue comments:
Can you add instructions for building and running your repro?
Sorry yes, updated comment.
@perezd How do you make the repro use a Netty version that's built and installed into the local `~/.m2/` repository? I'm trying to make it use a local `4.1.53.Final-SNAPSHOT` version, but just changing the `NETTY_VERSION` constant in the `WORKSPACE` file is not enough.
This would work, if you add the jars to the BUILD file:
https://docs.bazel.build/versions/master/be/java.html#java_import

Then just changes `deps` to point to that new target and it should work.
I think this was fixed ?
/cc @chrisvest 
I get the following error, which I don't understand, when I run the reproducer build with a 4.1.54.Final-SNAPSHOT version of Netty:

```
[chris@localhost netty-epoll-native-repro]$ bazel run :main-native --sandbox_debug
INFO: Analyzed target //:main-native (1 packages loaded, 11 targets configured).
INFO: Found 1 target...
INFO: From Extracting interface //third_party:netty:
1603979213.739407504: src/main/tools/linux-sandbox.cc:152: calling pipe(2)...
1603979213.739430730: src/main/tools/linux-sandbox.cc:171: calling clone(2)...
1603979213.739633277: src/main/tools/linux-sandbox.cc:180: linux-sandbox-pid1 has PID 30613
1603979213.739668113: src/main/tools/linux-sandbox-pid1.cc:434: Pid1Main started
1603979213.739768241: src/main/tools/linux-sandbox.cc:197: done manipulating pipes
1603979213.739791637: src/main/tools/linux-sandbox-pid1.cc:176: working dir: /home/chris/.cache/bazel/_bazel_chris/79c5fdc3f25858db1b7fbca7ad47271b/sandbox/linux-sandbox/48/execroot/grl
1603979213.739807831: src/main/tools/linux-sandbox-pid1.cc:208: writable: /home/chris/.cache/bazel/_bazel_chris/79c5fdc3f25858db1b7fbca7ad47271b/sandbox/linux-sandbox/48/execroot/grl
1603979213.739815223: src/main/tools/linux-sandbox-pid1.cc:208: writable: /tmp
1603979213.739820767: src/main/tools/linux-sandbox-pid1.cc:208: writable: /dev/shm
1603979213.739867607: src/main/tools/linux-sandbox-pid1.cc:278: remount ro: /
1603979213.739877071: src/main/tools/linux-sandbox-pid1.cc:278: remount ro: /sys
1603979213.739882570: src/main/tools/linux-sandbox-pid1.cc:278: remount ro: /sys/kernel/security
1603979213.739889160: src/main/tools/linux-sandbox-pid1.cc:278: remount ro: /sys/fs/cgroup
1603979213.739894040: src/main/tools/linux-sandbox-pid1.cc:278: remount ro: /sys/fs/pstore
1603979213.739899400: src/main/tools/linux-sandbox-pid1.cc:278: remount ro: /sys/fs/bpf
1603979213.739904071: src/main/tools/linux-sandbox-pid1.cc:278: remount ro: /sys/kernel/config
1603979213.739908633: src/main/tools/linux-sandbox-pid1.cc:278: remount ro: /sys/fs/selinux
1603979213.739913332: src/main/tools/linux-sandbox-pid1.cc:278: remount ro: /sys/kernel/tracing
1603979213.739917879: src/main/tools/linux-sandbox-pid1.cc:278: remount ro: /sys/kernel/debug
1603979213.739922570: src/main/tools/linux-sandbox-pid1.cc:278: remount ro: /sys/fs/fuse/connections
1603979213.739928426: src/main/tools/linux-sandbox-pid1.cc:278: remount ro: /dev
1603979213.739932441: src/main/tools/linux-sandbox-pid1.cc:278: remount rw: /dev/shm
1603979213.739954786: src/main/tools/linux-sandbox-pid1.cc:278: remount ro: /dev/pts
1603979213.739960071: src/main/tools/linux-sandbox-pid1.cc:278: remount ro: /dev/mqueue
1603979213.739963898: src/main/tools/linux-sandbox-pid1.cc:278: remount ro: /dev/hugepages
1603979213.739967826: src/main/tools/linux-sandbox-pid1.cc:278: remount ro: /run
1603979213.739972297: src/main/tools/linux-sandbox-pid1.cc:278: remount ro: /run/vmblock-fuse
1603979213.739976810: src/main/tools/linux-sandbox-pid1.cc:278: remount ro: /run/user/1000
1603979213.739981507: src/main/tools/linux-sandbox-pid1.cc:278: remount ro: /run/user/1000/gvfs
1603979213.739985922: src/main/tools/linux-sandbox-pid1.cc:278: remount ro: /proc
1603979213.739990035: src/main/tools/linux-sandbox-pid1.cc:278: remount ro: /proc/sys/fs/binfmt_misc
1603979213.739996764: src/main/tools/linux-sandbox-pid1.cc:278: remount rw: /tmp
1603979213.740000021: src/main/tools/linux-sandbox-pid1.cc:278: remount ro: /boot
1603979213.740003624: src/main/tools/linux-sandbox-pid1.cc:278: remount ro: /var/lib/nfs/rpc_pipefs
1603979213.740010159: src/main/tools/linux-sandbox-pid1.cc:278: remount ro: /mnt/hgfs
1603979213.740017960: src/main/tools/linux-sandbox-pid1.cc:278: remount rw: /home/chris/.cache/bazel/_bazel_chris/79c5fdc3f25858db1b7fbca7ad47271b/sandbox/linux-sandbox/48/execroot/grl
1603979213.740022873: src/main/tools/linux-sandbox-pid1.cc:278: remount rw: /home/chris/.cache/bazel/_bazel_chris/79c5fdc3f25858db1b7fbca7ad47271b/sandbox/linux-sandbox/48/execroot/grl
1603979213.740026346: src/main/tools/linux-sandbox-pid1.cc:278: remount rw: /tmp
1603979213.740029038: src/main/tools/linux-sandbox-pid1.cc:278: remount rw: /dev/shm
1603979213.740089170: src/main/tools/linux-sandbox-pid1.cc:363: calling fork...
1603979213.740196745: src/main/tools/linux-sandbox-pid1.cc:393: child started with PID 2
1603979213.757556894: src/main/tools/linux-sandbox-pid1.cc:410: wait returned pid=2, status=0x00
1603979213.757569016: src/main/tools/linux-sandbox-pid1.cc:428: child exited normally with code 0
1603979213.757889995: src/main/tools/linux-sandbox.cc:233: child exited normally with code 0
ERROR: /home/chris/netty-epoll-native-repro/BUILD:19:13: Action main-native-bin failed (Exit 1): linux-sandbox failed: error executing command 
  (cd /home/chris/.cache/bazel/_bazel_chris/79c5fdc3f25858db1b7fbca7ad47271b/sandbox/linux-sandbox/50/execroot/grl && \
  exec env - \
    PATH=/usr/bin:/bin \
    TMPDIR=/tmp \
  /home/chris/.cache/bazel/_bazel_chris/install/792f43108e6f3e1e71623e5a11a5a382/linux-sandbox -t 15 -w /home/chris/.cache/bazel/_bazel_chris/79c5fdc3f25858db1b7fbca7ad47271b/sandbox/linux-sandbox/50/execroot/grl -w /tmp -w /dev/shm -D -- external/graal/bin/native-image --no-server --no-fallback -cp bazel-out/k8-fastbuild/bin/libmain.jar:third_party/netty/netty-common-4.1.54.Final-SNAPSHOT.jar:third_party/netty/netty-buffer-4.1.54.Final-SNAPSHOT.jar:third_party/netty/netty-codec-4.1.54.Final-SNAPSHOT.jar:third_party/netty/netty-codec-http-4.1.54.Final-SNAPSHOT.jar:third_party/netty/netty-transport-4.1.54.Final-SNAPSHOT.jar:third_party/netty/netty-transport-native-unix-common-4.1.54.Final-SNAPSHOT.jar:third_party/netty/netty-transport-native-epoll-4.1.54.Final-SNAPSHOT.jar:third_party/netty/netty-handler-4.1.54.Final-SNAPSHOT.jar '-H:Class=Main' '-H:Name=bazel-out/k8-fastbuild/bin/main-native-bin' '-H:CCompilerPath=/usr/bin/gcc' -H:+ReportExceptionStackTraces -H:+TraceClassInitialization --static) linux-sandbox failed: error executing command 
  (cd /home/chris/.cache/bazel/_bazel_chris/79c5fdc3f25858db1b7fbca7ad47271b/sandbox/linux-sandbox/50/execroot/grl && \
  exec env - \
    PATH=/usr/bin:/bin \
    TMPDIR=/tmp \
  /home/chris/.cache/bazel/_bazel_chris/install/792f43108e6f3e1e71623e5a11a5a382/linux-sandbox -t 15 -w /home/chris/.cache/bazel/_bazel_chris/79c5fdc3f25858db1b7fbca7ad47271b/sandbox/linux-sandbox/50/execroot/grl -w /tmp -w /dev/shm -D -- external/graal/bin/native-image --no-server --no-fallback -cp bazel-out/k8-fastbuild/bin/libmain.jar:third_party/netty/netty-common-4.1.54.Final-SNAPSHOT.jar:third_party/netty/netty-buffer-4.1.54.Final-SNAPSHOT.jar:third_party/netty/netty-codec-4.1.54.Final-SNAPSHOT.jar:third_party/netty/netty-codec-http-4.1.54.Final-SNAPSHOT.jar:third_party/netty/netty-transport-4.1.54.Final-SNAPSHOT.jar:third_party/netty/netty-transport-native-unix-common-4.1.54.Final-SNAPSHOT.jar:third_party/netty/netty-transport-native-epoll-4.1.54.Final-SNAPSHOT.jar:third_party/netty/netty-handler-4.1.54.Final-SNAPSHOT.jar '-H:Class=Main' '-H:Name=bazel-out/k8-fastbuild/bin/main-native-bin' '-H:CCompilerPath=/usr/bin/gcc' -H:+ReportExceptionStackTraces -H:+TraceClassInitialization --static)
1603979213.837758895: src/main/tools/linux-sandbox.cc:152: calling pipe(2)...
1603979213.837780965: src/main/tools/linux-sandbox.cc:171: calling clone(2)...
1603979213.837993852: src/main/tools/linux-sandbox.cc:180: linux-sandbox-pid1 has PID 30618
1603979213.838038905: src/main/tools/linux-sandbox-pid1.cc:434: Pid1Main started
1603979213.838124208: src/main/tools/linux-sandbox.cc:197: done manipulating pipes
1603979213.838170860: src/main/tools/linux-sandbox-pid1.cc:176: working dir: /home/chris/.cache/bazel/_bazel_chris/79c5fdc3f25858db1b7fbca7ad47271b/sandbox/linux-sandbox/50/execroot/grl
1603979213.838188901: src/main/tools/linux-sandbox-pid1.cc:208: writable: /home/chris/.cache/bazel/_bazel_chris/79c5fdc3f25858db1b7fbca7ad47271b/sandbox/linux-sandbox/50/execroot/grl
1603979213.838195746: src/main/tools/linux-sandbox-pid1.cc:208: writable: /tmp
1603979213.838201268: src/main/tools/linux-sandbox-pid1.cc:208: writable: /dev/shm
1603979213.838264214: src/main/tools/linux-sandbox-pid1.cc:278: remount ro: /
1603979213.838274941: src/main/tools/linux-sandbox-pid1.cc:278: remount ro: /sys
1603979213.838280548: src/main/tools/linux-sandbox-pid1.cc:278: remount ro: /sys/kernel/security
1603979213.838287149: src/main/tools/linux-sandbox-pid1.cc:278: remount ro: /sys/fs/cgroup
1603979213.838292243: src/main/tools/linux-sandbox-pid1.cc:278: remount ro: /sys/fs/pstore
1603979213.838297864: src/main/tools/linux-sandbox-pid1.cc:278: remount ro: /sys/fs/bpf
1603979213.838302218: src/main/tools/linux-sandbox-pid1.cc:278: remount ro: /sys/kernel/config
1603979213.838306876: src/main/tools/linux-sandbox-pid1.cc:278: remount ro: /sys/fs/selinux
1603979213.838311431: src/main/tools/linux-sandbox-pid1.cc:278: remount ro: /sys/kernel/tracing
1603979213.838315842: src/main/tools/linux-sandbox-pid1.cc:278: remount ro: /sys/kernel/debug
1603979213.838320456: src/main/tools/linux-sandbox-pid1.cc:278: remount ro: /sys/fs/fuse/connections
1603979213.838326254: src/main/tools/linux-sandbox-pid1.cc:278: remount ro: /dev
1603979213.838330415: src/main/tools/linux-sandbox-pid1.cc:278: remount rw: /dev/shm
1603979213.838353771: src/main/tools/linux-sandbox-pid1.cc:278: remount ro: /dev/pts
1603979213.838358470: src/main/tools/linux-sandbox-pid1.cc:278: remount ro: /dev/mqueue
1603979213.838362393: src/main/tools/linux-sandbox-pid1.cc:278: remount ro: /dev/hugepages
1603979213.838366317: src/main/tools/linux-sandbox-pid1.cc:278: remount ro: /run
1603979213.838370249: src/main/tools/linux-sandbox-pid1.cc:278: remount ro: /run/vmblock-fuse
1603979213.838374337: src/main/tools/linux-sandbox-pid1.cc:278: remount ro: /run/user/1000
1603979213.838378989: src/main/tools/linux-sandbox-pid1.cc:278: remount ro: /run/user/1000/gvfs
1603979213.838383515: src/main/tools/linux-sandbox-pid1.cc:278: remount ro: /proc
1603979213.838387608: src/main/tools/linux-sandbox-pid1.cc:278: remount ro: /proc/sys/fs/binfmt_misc
1603979213.838394500: src/main/tools/linux-sandbox-pid1.cc:278: remount rw: /tmp
1603979213.838397649: src/main/tools/linux-sandbox-pid1.cc:278: remount ro: /boot
1603979213.838401352: src/main/tools/linux-sandbox-pid1.cc:278: remount ro: /var/lib/nfs/rpc_pipefs
1603979213.838407632: src/main/tools/linux-sandbox-pid1.cc:278: remount ro: /mnt/hgfs
1603979213.838414908: src/main/tools/linux-sandbox-pid1.cc:278: remount rw: /home/chris/.cache/bazel/_bazel_chris/79c5fdc3f25858db1b7fbca7ad47271b/sandbox/linux-sandbox/50/execroot/grl
1603979213.838419872: src/main/tools/linux-sandbox-pid1.cc:278: remount rw: /home/chris/.cache/bazel/_bazel_chris/79c5fdc3f25858db1b7fbca7ad47271b/sandbox/linux-sandbox/50/execroot/grl
1603979213.838423186: src/main/tools/linux-sandbox-pid1.cc:278: remount rw: /tmp
1603979213.838425830: src/main/tools/linux-sandbox-pid1.cc:278: remount rw: /dev/shm
1603979213.838471119: src/main/tools/linux-sandbox-pid1.cc:363: calling fork...
1603979213.838560767: src/main/tools/linux-sandbox-pid1.cc:393: child started with PID 2
Fatal error: com.oracle.graal.pointsto.util.AnalysisError$ParsingError: Error encountered while parsing com.oracle.svm.reflect.ClassLoader_defineClass_c6c343b4d6dc22ca64eb2d8503b13ac9c340dcb3.invoke(java.lang.Object, java.lang.Object[]) 
Parsing context:
	parsing java.lang.reflect.Method.invoke(Method.java:566)
	parsing io.netty.util.internal.CleanerJava9.freeDirectBuffer(CleanerJava9.java:88)
	parsing io.netty.util.internal.PlatformDependent.freeDirectBuffer(PlatformDependent.java:483)
	parsing io.netty.channel.unix.Buffer.free(Buffer.java:33)
	parsing io.netty.channel.epoll.EpollEventArray.free(EpollEventArray.java:92)
	parsing io.netty.channel.epoll.EpollEventLoop.cleanup(EpollEventLoop.java:552)
	parsing io.netty.util.concurrent.SingleThreadEventExecutor$4.run(SingleThreadEventExecutor.java:1036)
	parsing com.oracle.svm.core.jdk.RuntimeSupport.executeHooks(RuntimeSupport.java:144)
	parsing com.oracle.svm.core.jdk.RuntimeSupport.executeTearDownHooks(RuntimeSupport.java:121)
	parsing com.oracle.svm.core.graal.snippets.CEntryPointSnippets.tearDownIsolate(CEntryPointSnippets.java:361)

	at com.oracle.graal.pointsto.util.AnalysisError.parsingError(AnalysisError.java:138)
	at com.oracle.graal.pointsto.flow.MethodTypeFlow.doParse(MethodTypeFlow.java:327)
	at com.oracle.graal.pointsto.flow.MethodTypeFlow.ensureParsed(MethodTypeFlow.java:300)
	at com.oracle.graal.pointsto.flow.MethodTypeFlow.addContext(MethodTypeFlow.java:107)
	at com.oracle.graal.pointsto.DefaultAnalysisPolicy$DefaultVirtualInvokeTypeFlow.onObservedUpdate(DefaultAnalysisPolicy.java:191)
	at com.oracle.graal.pointsto.flow.TypeFlow.notifyObservers(TypeFlow.java:343)
	at com.oracle.graal.pointsto.flow.TypeFlow.update(TypeFlow.java:385)
	at com.oracle.graal.pointsto.BigBang$2.run(BigBang.java:511)
	at com.oracle.graal.pointsto.util.CompletionExecutor.lambda$execute$0(CompletionExecutor.java:171)
	at java.base/java.util.concurrent.ForkJoinTask$RunnableExecuteAction.exec(ForkJoinTask.java:1426)
	at java.base/java.util.concurrent.ForkJoinTask.doExec(ForkJoinTask.java:290)
	at java.base/java.util.concurrent.ForkJoinPool$WorkQueue.topLevelExec(ForkJoinPool.java:1020)
	at java.base/java.util.concurrent.ForkJoinPool.scan(ForkJoinPool.java:1656)
	at java.base/java.util.concurrent.ForkJoinPool.runWorker(ForkJoinPool.java:1594)
	at java.base/java.util.concurrent.ForkJoinWorkerThread.run(ForkJoinWorkerThread.java:177)
Caused by: com.oracle.svm.hosted.substitute.DeletedElementException: Unsupported method java.lang.ClassLoader.defineClass(String, byte[], int, int) is reachable: The declaring class of this element has been substituted, but this element is not present in the substitution class
To diagnose the issue, you can add the option --report-unsupported-elements-at-runtime. The unsupported element is then reported at run time when it is accessed the first time.
	at com.oracle.svm.hosted.substitute.AnnotationSubstitutionProcessor.lookup(AnnotationSubstitutionProcessor.java:183)
	at com.oracle.graal.pointsto.infrastructure.SubstitutionProcessor$ChainedSubstitutionProcessor.lookup(SubstitutionProcessor.java:128)
	at com.oracle.graal.pointsto.infrastructure.SubstitutionProcessor$ChainedSubstitutionProcessor.lookup(SubstitutionProcessor.java:128)
	at com.oracle.graal.pointsto.meta.AnalysisUniverse.lookupAllowUnresolved(AnalysisUniverse.java:397)
	at com.oracle.graal.pointsto.meta.AnalysisUniverse.lookup(AnalysisUniverse.java:377)
	at com.oracle.graal.pointsto.meta.AnalysisUniverse.lookup(AnalysisUniverse.java:75)
	at com.oracle.graal.pointsto.infrastructure.UniverseMetaAccess.lookupJavaMethod(UniverseMetaAccess.java:93)
	at com.oracle.graal.pointsto.meta.AnalysisMetaAccess.lookupJavaMethod(AnalysisMetaAccess.java:66)
	at com.oracle.graal.pointsto.meta.AnalysisMetaAccess.lookupJavaMethod(AnalysisMetaAccess.java:39)
	at com.oracle.svm.reflect.hosted.ReflectionSubstitutionType$ReflectiveInvokeMethod.buildGraph(ReflectionSubstitutionType.java:511)
	at com.oracle.graal.pointsto.meta.AnalysisMethod.buildGraph(AnalysisMethod.java:319)
	at com.oracle.graal.pointsto.flow.MethodTypeFlowBuilder.parse(MethodTypeFlowBuilder.java:185)
	at com.oracle.graal.pointsto.flow.MethodTypeFlowBuilder.apply(MethodTypeFlowBuilder.java:340)
	at com.oracle.graal.pointsto.flow.MethodTypeFlow.doParse(MethodTypeFlow.java:310)
	... 13 more
Error: Image build request failed with exit status 1
1603979228.378204816: src/main/tools/linux-sandbox-pid1.cc:410: wait returned pid=2, status=0x100
1603979228.378218295: src/main/tools/linux-sandbox-pid1.cc:428: child exited normally with code 1
1603979228.378818653: src/main/tools/linux-sandbox.cc:233: child exited normally with code 1
[bazel-out/k8-fastbuild/bin/main-native-bin:21]    classlist:   2,611.26 ms
[bazel-out/k8-fastbuild/bin/main-native-bin:21]        (cap):     589.29 ms
[bazel-out/k8-fastbuild/bin/main-native-bin:21]        setup:   1,557.43 ms
[bazel-out/k8-fastbuild/bin/main-native-bin:21]     analysis:   9,680.26 ms
Target //:main-native failed to build
Use --verbose_failures to see the command lines of failed build steps.
INFO: Elapsed time: 14.778s, Critical Path: 14.64s
INFO: 6 processes: 4 internal, 1 linux-sandbox, 1 worker.
FAILED: Build did NOT complete successfully
FAILED: Build did NOT complete successfully
[chris@localhost netty-epoll-native-repro]$ 
```

These are the changes I made to the reproducer repo in order to get it sort-of building with custom jar files:

```patch
diff --git a/BUILD b/BUILD
index 1c8f0c3..c953f9b 100644
--- a/BUILD
+++ b/BUILD
@@ -5,10 +5,11 @@ java_library(
     name = "main",
     srcs = ["Main.java"],
     deps = [
-        "@maven//:io_netty_netty_codec_http",
-        "@maven//:io_netty_netty_transport",
-        "@maven//:io_netty_netty_transport_native_epoll_linux_x86_64",
-        "@maven//:io_netty_netty_handler",
+    	"//third_party:netty"
+        #"@maven//:io_netty_netty_codec_http",
+        #"@maven//:io_netty_netty_transport",
+        #"@maven//:io_netty_netty_transport_native_epoll_linux_x86_64",
+        #"@maven//:io_netty_netty_handler",
     ],
 )
 
diff --git a/WORKSPACE b/WORKSPACE
index 1beffb0..e0cb416 100644
--- a/WORKSPACE
+++ b/WORKSPACE
@@ -4,8 +4,8 @@ load("@bazel_tools//tools/build_defs/repo:http.bzl", "http_archive")
 
 http_archive(
     name = "rules_graal",
-    sha256 = "c1445dd6263b17b110da69178c238dc0925bbea1bffc93c46f5af4f8027245cc",
-    strip_prefix = "rules_graal-master",
+    sha256 = "f5fc694e0abe24699987aab1c4054536c1792ed8a17123a9d7223511fac2367d",
+    strip_prefix = "rules_graal-1b5f6deef8352c4b64541bb0f699fd2af5ae6b0b",
     urls = [
         "https://github.com/andyscott/rules_graal/archive/1b5f6deef8352c4b64541bb0f699fd2af5ae6b0b.zip",
     ],
@@ -72,3 +72,4 @@ maven_install(
 load("@maven//:defs.bzl", "pinned_maven_install")
 
 pinned_maven_install()
+
diff --git a/third_party/BUILD b/third_party/BUILD
new file mode 100644
index 0000000..aa16a03
--- /dev/null
+++ b/third_party/BUILD
@@ -0,0 +1,14 @@
+package(default_visibility = ["//visibility:public"])
+java_import(
+	name = "netty",
+	jars = [
+	    "netty/netty-common-4.1.54.Final-SNAPSHOT.jar",
+	    "netty/netty-buffer-4.1.54.Final-SNAPSHOT.jar",
+	    "netty/netty-codec-4.1.54.Final-SNAPSHOT.jar",
+	    "netty/netty-codec-http-4.1.54.Final-SNAPSHOT.jar",
+	    "netty/netty-transport-4.1.54.Final-SNAPSHOT.jar",
+	    "netty/netty-transport-native-unix-common-4.1.54.Final-SNAPSHOT.jar",
+	    "netty/netty-transport-native-epoll-4.1.54.Final-SNAPSHOT.jar",
+	    "netty/netty-handler-4.1.54.Final-SNAPSHOT.jar",
+	],
+)
```
The Epoll transport is currently not supported in Graal because it loads classes from JNI code, and those classes needs to be initialised at runtime.

You can work around this by adding something like this to the `native-image` command:

```
--initialize-at-run-time=io.netty.handler.ssl.util.ThreadLocalInsecureRandom,io.netty.channel.unix,io.netty.channel.epoll,io.netty.channel.kqueue
```
@perezd I tried making epoll work in Graal native-image in #11163, but it's running into issues I don't fully understand. If you have any ideas feel free to chime in.
Bit out if my depths here but wondering if it could have to do with JNI config? https://www.graalvm.org/reference-manual/native-image/JNI/

Does netty use reflection related to JNI? This could cause an issue potentially?
@chrisvest  So where does this stand now that https://github.com/netty/netty/pull/11163 has been closed?
@ScottPierce You will have to use NIO transport with native-image for the foreseeable future. 
What if we don't have the ability to choose? The library we use just pulls this stuff in.
@ScottPierce we are happy to review a PR but we clearly not have the needed knowledge to fix this ourselves. Sorry 
I wasn't being snarky. I was more asking if I can exclude dependencies and the pull in the right one and have a reasonable expectation of it still working. Thanks! 
@ScottPierce I guess that is more the question for the library that pulls in netty :)
Hello, I try to build a native image of a **Micronaut** service and have lots of similar errors 

> (Error: Classes that should be initialized at run time got initialized during image building:
>  io.grpc.netty.shaded.io.netty.buffer.ByteBufUtil the class was requested to be initialized at run time (from jar:file:///home/jacek/.gradle/caches/modules-2/files-2.1/io.grpc/grpc-netty-shaded/1.41.0/43fee785c101b3bfa3f7d916d8b47d5d736cbff2/grpc-netty-shaded-1.41.0.jar!/META-INF/native-image/io.grpc.netty.shaded.io.netty/buffer/native-image.properties with 'io.grpc.netty.shaded.io.netty.buffer.ByteBufUtil'). To see why io.grpc.netty.shaded.io.netty.buffer.ByteBufUtil got initialized use --trace-class-initialization=io.grpc.netty.shaded.io.netty.buffer.ByteBufUtil
> io.grpc.netty.shaded.io.netty.buffer.ByteBufAllocator the class was requested to be initialized at run time (from jar:file:///home/jacek/.gradle/caches/modules-2/files-2.1/io.grpc/grpc-netty-shaded/1.41.0/43fee785c101b3bfa3f7d916d8b47d5d736cbff2/grpc-netty-shaded-1.41.0.jar!/META-INF/native-image/io.grpc.netty.shaded.io.netty/buffer/native-image.properties with 'io.grpc.netty.shaded.io.netty.buffer.ByteBufAllocator'). To see why io.grpc.netty.shaded.io.netty.buffer.ByteBufAllocator got initialized use --trace-class-initialization=io.grpc.netty.shaded.io.netty.buffer.ByteBufAllocator
> .............

Is there a known solution or a workaround for this?
From various suggestions I tried
```
  buildArgs.add(
        "--initialize-at-run-time=" +
         "io.grpc.netty.shaded.io.netty.util.internal.logging.Log4JLogger," +
            "io.grpc.netty.shaded.io.netty.handler.ssl.JdkNpnApplicationProtocolNegotiator," +
            "io.netty.handler.ssl.util.ThreadLocalInsecureRandom," +
            "io.netty.channel.unix,io.netty.channel.epoll,io.netty.channel.kqueue"
)
```
but it didn't help.
Is there some radical workaround which maybe sacrifice performance but at least allow to successfully compile?
I also ran into this again with Netty 4.1.70 and GraalVM 21.3.0 but was able to get it working with:
```
  --initialize-at-run-time=io.netty.channel.DefaultFileRegion
  --initialize-at-run-time=io.netty.channel.epoll.Native
  --initialize-at-run-time=io.netty.channel.epoll.Epoll
  --initialize-at-run-time=io.netty.channel.epoll.EpollEventLoop
  --initialize-at-run-time=io.netty.channel.epoll.EpollEventArray
  --initialize-at-run-time=io.netty.channel.kqueue.KQueue
  --initialize-at-run-time=io.netty.channel.kqueue.KQueueEventLoop
  --initialize-at-run-time=io.netty.channel.kqueue.KQueueEventArray
  --initialize-at-run-time=io.netty.channel.kqueue.Native
  --initialize-at-run-time=io.netty.channel.unix.Limits
  --initialize-at-run-time=io.netty.channel.unix.Errors
  --initialize-at-run-time=io.netty.channel.unix.IovArray
```
I tinkered a bit with epoll and GraalVM 23.0 - I managed to build the original sample project, but I couldn't figure out how to tweak Bazel to use a newer GraalVM version - I've created the following sample repository that uses Gradle: https://github.com/gradinac/netty-epoll-native-image. This project uses the original reproducer from @perezd, but uses Gradle as the build system

I've hit two different categories of issues:
 1. Class initialization problems - Netty 4.1 is by default fully initialized at image build time. However, epoll classes require native code and shouldn't be initialized at image build time. On top of that, these classes also use other classes in their static initializer that shouldn't be used during the image build (example: `io.netty.util.AbstractReferenceCounted`). Thank you @jamesward for the above list! :) It works without any modifications, but I've modified it a little bit in the repo. A sidenote: for Gradle and Maven projects, users can also add the [Native Build Tools](https://github.com/graalvm/native-build-tools) to their project and enable the metadata repository. Netty 4.1 metadata in this repository will automatically disable build-time initialization of Netty
 2. Missing metadata - it appears we are missing some metadata for epoll, especially for JNI. Netty also appears to have many ways to fetch the native transport library, out of which one is to fetch it as a Java resource and extract it under `/tmp` at runtime. That approach seems to work fine, so I've also added a `resource-config.json` that includes this library if epoll is used.

If it's okay, I'll create a PR that adds the flags and the metadata to Netty 4.1



Pull request review comments:

Patch that resolved the issue:
diff --git a/transport-classes-epoll/src/main/resources/META-INF/native-image/io.netty/netty-transport-classes-epoll/jni-config.json b/transport-classes-epoll/src/main/resources/META-INF/native-image/io.netty/netty-transport-classes-epoll/jni-config.json
new file mode 100644
index 00000000000..493b2ed220a
--- /dev/null
+++ b/transport-classes-epoll/src/main/resources/META-INF/native-image/io.netty/netty-transport-classes-epoll/jni-config.json
@@ -0,0 +1,170 @@
+[
+  {
+    "condition": {
+      "typeReachable": "io.netty.channel.epoll.Native"
+    },
+    "name":"io.netty.channel.ChannelException"
+  },
+  {
+    "condition": {
+      "typeReachable": "io.netty.channel.epoll.Native"
+    },
+    "name":"io.netty.channel.DefaultFileRegion",
+    "allDeclaredFields": true
+  },
+  {
+    "condition": {
+      "typeReachable": "io.netty.channel.epoll.Native"
+    },
+    "name":"io.netty.channel.epoll.LinuxSocket"
+  },
+  {
+    "condition": {
+      "typeReachable": "io.netty.channel.epoll.Native"
+    },
+    "name":"io.netty.channel.epoll.Native"
+  },
+  {
+    "condition": {
+      "typeReachable": "io.netty.channel.epoll.Native"
+    },
+    "name":"io.netty.channel.epoll.NativeDatagramPacketArray$NativeDatagramPacket",
+    "allDeclaredFields": true
+  },
+  {
+    "condition": {
+      "typeReachable": "io.netty.channel.epoll.Native"
+    },
+    "name":"io.netty.channel.epoll.NativeStaticallyReferencedJniMethods"
+  },
+  {
+    "condition": {
+      "typeReachable": "io.netty.channel.epoll.Native"
+    },
+    "name":"io.netty.channel.unix.Buffer"
+  },
+  {
+    "condition": {
+      "typeReachable": "io.netty.channel.epoll.Native"
+    },
+    "name":"io.netty.channel.unix.DatagramSocketAddress",
+    "methods":[{"name":"<init>","parameterTypes":["byte[]","int","int","int","io.netty.channel.unix.DatagramSocketAddress"] }]
+  },
+  {
+    "condition": {
+      "typeReachable": "io.netty.channel.epoll.Native"
+    },
+    "name":"io.netty.channel.unix.DomainDatagramSocketAddress",
+    "methods":[{"name":"<init>","parameterTypes":["byte[]","int","io.netty.channel.unix.DomainDatagramSocketAddress"] }]
+  },
+  {
+    "condition": {
+      "typeReachable": "io.netty.channel.epoll.Native"
+    },
+    "name":"io.netty.channel.unix.ErrorsStaticallyReferencedJniMethods"
+  },
+  {
+    "condition": {
+      "typeReachable": "io.netty.channel.epoll.Native"
+    },
+    "name":"io.netty.channel.unix.FileDescriptor"
+  },
+  {
+    "condition": {
+      "typeReachable": "io.netty.channel.epoll.Native"
+    },
+    "name":"io.netty.channel.unix.LimitsStaticallyReferencedJniMethods"
+  },
+  {
+    "condition": {
+      "typeReachable": "io.netty.channel.epoll.Native"
+    },
+    "name":"io.netty.channel.unix.PeerCredentials",
+    "methods":[{"name":"<init>","parameterTypes":["int","int","int[]"] }]
+  },
+  {
+    "condition": {
+      "typeReachable": "io.netty.channel.epoll.Native"
+    },
+    "name":"io.netty.channel.unix.Socket"
+  },
+  {
+    "condition": {
+      "typeReachable": "io.netty.channel.epoll.Native"
+    },
+    "name":"java.io.FileDescriptor",
+    "fields":[{"name":"fd"}]
+  },
+  {
+    "condition": {
+      "typeReachable": "io.netty.channel.epoll.Native"
+    },
+    "name":"java.io.IOException"
+  },
+  {
+    "condition": {
+      "typeReachable": "io.netty.channel.epoll.Native"
+    },
+    "name":"java.lang.Boolean",
+    "methods":[{"name":"getBoolean","parameterTypes":["java.lang.String"] }]
+  },
+  {
+    "condition": {
+      "typeReachable": "io.netty.channel.epoll.Native"
+    },
+    "name":"java.lang.OutOfMemoryError"
+  },
+  {
+    "condition": {
+      "typeReachable": "io.netty.channel.epoll.Native"
+    },
+    "name":"java.lang.RuntimeException"
+  },
+  {
+    "condition": {
+      "typeReachable": "io.netty.channel.epoll.Native"
+    },
+    "name":"java.net.InetSocketAddress",
+    "methods":[{"name":"<init>","parameterTypes":["java.lang.String","int"] }]
+  },
+  {
+    "condition": {
+      "typeReachable": "io.netty.channel.epoll.Native"
+    },
+    "name":"java.net.PortUnreachableException"
+  },
+  {
+    "condition": {
+      "typeReachable": "io.netty.channel.epoll.Native"
+    },
+    "name":"java.nio.Buffer",
+    "fields":[
+      {"name":"limit"},
+      {"name":"position"}
+    ],
+    "methods":[
+      {"name":"limit","parameterTypes":[] },
+      {"name":"position","parameterTypes":[] }
+    ]
+  },
+  {
+    "condition": {
+      "typeReachable": "io.netty.channel.epoll.Native"
+    },
+    "name":"java.nio.DirectByteBuffer"
+  },
+  {
+    "condition": {
+      "typeReachable": "io.netty.channel.epoll.Native"
+    },
+    "name":"java.nio.channels.ClosedChannelException",
+    "methods":[{"name":"<init>","parameterTypes":[] }]
+  },
+  {
+    "condition": {
+      "typeReachable": "io.netty.channel.epoll.Native"
+    },
+    "name":"sun.nio.ch.FileChannelImpl",
+    "fields":[{"name":"fd"}]
+  }
+]
diff --git a/transport-classes-epoll/src/main/resources/META-INF/native-image/io.netty/netty-transport-classes-epoll/native-image.properties b/transport-classes-epoll/src/main/resources/META-INF/native-image/io.netty/netty-transport-classes-epoll/native-image.properties
new file mode 100644
index 00000000000..ff444c869bf
--- /dev/null
+++ b/transport-classes-epoll/src/main/resources/META-INF/native-image/io.netty/netty-transport-classes-epoll/native-image.properties
@@ -0,0 +1,15 @@
+# Copyright 2023 The Netty Project
+#
+# The Netty Project licenses this file to you under the Apache License,
+# version 2.0 (the "License"); you may not use this file except in compliance
+# with the License. You may obtain a copy of the License at:
+#
+#   https://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
+# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
+# License for the specific language governing permissions and limitations
+# under the License.
+
+Args = --initialize-at-run-time=io.netty.channel.epoll,io.netty.channel.unix.Limits,io.netty.channel.unix.IovArray,io.netty.channel.unix.Errors
diff --git a/transport-classes-epoll/src/main/resources/META-INF/native-image/io.netty/netty-transport-classes-epoll/reflect-config.json b/transport-classes-epoll/src/main/resources/META-INF/native-image/io.netty/netty-transport-classes-epoll/reflect-config.json
new file mode 100644
index 00000000000..74aa081bc99
--- /dev/null
+++ b/transport-classes-epoll/src/main/resources/META-INF/native-image/io.netty/netty-transport-classes-epoll/reflect-config.json
@@ -0,0 +1,16 @@
+[
+  {
+    "condition": {
+      "typeReachable": "io.netty.channel.epoll.Native"
+    },
+    "name":"io.netty.channel.epoll.EpollServerSocketChannel",
+    "allDeclaredConstructors": true
+  },
+  {
+    "condition": {
+      "typeReachable": "io.netty.channel.epoll.Native"
+    },
+    "name": "io.netty.util.internal.NativeLibraryUtil",
+    "allDeclaredMethods": true
+  }
+]
diff --git a/transport-classes-epoll/src/main/resources/META-INF/native-image/io.netty/netty-transport-classes-epoll/resource-config.json b/transport-classes-epoll/src/main/resources/META-INF/native-image/io.netty/netty-transport-classes-epoll/resource-config.json
new file mode 100644
index 00000000000..4998b6e6039
--- /dev/null
+++ b/transport-classes-epoll/src/main/resources/META-INF/native-image/io.netty/netty-transport-classes-epoll/resource-config.json
@@ -0,0 +1,13 @@
+{
+  "resources":{
+    "includes":[
+      {
+        "condition": {
+          "typeReachable": "io.netty.channel.epoll.Native"
+        },
+        "pattern":".*libnetty_transport_native_epoll_.*\\.so"
+      }
+    ]},
+  "bundles":[]
+}
+


Test patch: