🐳

M4 Mac miniでDockerコンテナ内でのnative-imageビルド時にSIGILLでクラッシュする場合の対処

2025/02/25に公開

事象

Micronautプロジェクトの環境構築時に遭遇。
./gradlew dockerBuildNative(Dockerコンテナでのネイティブイメージビルド)の実行時に下記のようなエラーが発生してビルドが異常終了する。

Step 20/29 : RUN native-image -cp /home/app/libs/*.jar:/home/app/resources:/home/app/application.jar --no-fallback -o application -H:ConfigurationFileDirectories=/home/app/config-dirs/generateResourcesConfigFile,/home/app/config-dirs/ch.qos.logback/logback-classic/1.4.9 io.micronaut.function.aws.runtime.MicronautLambdaRuntime
 ---> Running in ac7a4cab515e
#
# A fatal error has been detected by the Java Runtime Environment:
#
#  SIGILL (0x4) at pc=0x0000ffff864b369c, pid=19, tid=20
#
# JRE version:  (21.0.6+8) (build )
# Java VM: Java HotSpot(TM) 64-Bit Server VM (21.0.6+8-LTS-jvmci-23.1-b55, mixed mode, tiered, jvmci, jvmci compiler, compressed oops, compressed class ptrs, parallel gc, linux-aarch64)
# Problematic frame:
# j  java.lang.System.registerNatives()V+0 java.base
#
# No core dump will be written. Core dumps have been disabled. To enable core dumping, try "ulimit -c unlimited" before starting Java again
#
# An error report file with more information is saved as:
# /home/app/hs_err_pid19.log
[0.024s][warning][os] Loading hsdis library failed
#
# The crash happened outside the Java Virtual Machine in native code.
# See problematic frame for where to report the bug.
#
Error: Image build request for 'application' (pid: 19, path: /home/app) failed with exit status 134

対処法

native-image実行時のオプションに-J-XX:UseSVE=0を加える。
gradleのプロジェクトの場合はgraalvmNativeタスクに下記のような設定を渡してやればOK。

graalvmNative {
    binaries {
        main {
            jvmArgs(
                "-XX:UseSVE=0"
            )
        }
    }
}

-XX:UseSVE=0を指定することで、JVMがSVE(Scalable Vector Extension)を使用しないように強制し、クラッシュを回避できるらしい。

参考

https://github.com/quarkusio/quarkus/issues/45842
https://github.com/docker/for-mac/issues/7583
https://forums.docker.com/t/image-builds-fail-on-new-macbook-despite-working-fine-on-prior-apple-silicon/145772

Native Image に限らず、Apple M4 環境のDocker上でJVMを動かす際に類似の問題が発生することがありそう。

Discussion