NativeImageWin32MppImpls - UnsupportedFeatureException in GraalVM 23
Attempting to build a native image with Oracle GraalVM 23 leads to the following build-time error:
Fatal error: com.oracle.graal.pointsto.constraints.UnsupportedFeatureException: An object of type 'com.github.ajalt.mordant.internal.nativeimage.NativeImageWin32MppImpls' was found in the image heap. This type, however, is marked for initialization at image run time for the following reason: classes are initialized at run time by default.
This is not allowed for correctness reasons: All objects that are stored in the image heap must be initialized at build time.
You now have two options to resolve this:
1) If it is intended that objects of type 'com.github.ajalt.mordant.internal.nativeimage.NativeImageWin32MppImpls' are persisted in the image heap, add
'--initialize-at-build-time=com.github.ajalt.mordant.internal.nativeimage.NativeImageWin32MppImpls'
to the native-image arguments. Note that initializing new types can store additional objects to the heap. It is advised to check the static fields of 'com.github.ajalt.mordant.internal.nativeimage.NativeImageWin32MppImpls' to see if they are safe for build-time initialization, and that they do not contain any sensitive data that should not become part of the image.
2) If these objects should not be stored in the image heap, you can use
'--trace-object-instantiation=com.github.ajalt.mordant.internal.nativeimage.NativeImageWin32MppImpls'
to find classes that instantiate these objects. Once you found such a class, you can mark it explicitly for run time initialization with
'--initialize-at-run-time=<culprit>'
to prevent the instantiation of the object.
If you are seeing this message after upgrading to a new GraalVM release, this means that some objects ended up in the image heap without their type being marked with --initialize-at-build-time.
To fix this, include '--initialize-at-build-time=com.github.ajalt.mordant.internal.nativeimage.NativeImageWin32MppImpls' in your configuration. If the classes do not originate from your code, it is advised to update all library or framework dependencies to the latest version before addressing this error.
The following detailed trace displays from which field in the code the object was reached.
Object was reached by
scanning root com.github.ajalt.mordant.internal.nativeimage.NativeImageWin32MppImpls@b7b8425: com.github.ajalt.mordant.internal.nativeimage.NativeImageWin32MppImpls@b7b8425 embedded in
com.github.ajalt.mordant.internal.MppImplKt.stdinInteractive(MppImpl.kt:140)
parsing method com.github.ajalt.mordant.internal.MppImplKt.stdinInteractive(MppImpl.kt:140) reachable via the parsing context <no parsing context available>
It appears that some specific effort has been put in to supporting GraalVM, so wanted to flag.
We test against graal 22 in CI. Do you know if something changed in 23 that would cause this? What OS are you running this on?
I'm afraid I don't have a ton of context, though the release notes appear to include something relevant.
Deprecated Functionality
The old class initialization strategy, which was deprecated in GraalVM for JDK 22, is now removed. The option --StrictImageHeap no longer has any effect. Reporting unsupported elements at run time is now enabled by default. The option --report-unsupported-elements-at-runtime is deprecated.
I'm using Windows x64 -- I'm only coincidentally using mordant (as a dependency of Clikt)
As a temporary workaround, you can exclude the three :mordant-jvm-* modules through gradle or use clikt-core. You'll lose some functionality, but it should compile at least.
I used the suggested workaround (--initialize-at-build-time=com.github.ajalt.mordant.internal.nativeimage.NativeImageWin32MppImpls) which seemed to work too!
Are you using the latest mordant version ? The class NativeImageWin32MppImpls no longer exists. Missing --initialize-at-build-time should have been fixed by 45b19ff418b7c2c5fbf94859374b6d04f581aab8. In any case, the workaround works for older mordant versions :+1:
@ajalt While looking at this, I found that I forgot to remove https://github.com/ajalt/mordant/blob/4643f10b0698efcd49a3f5481441d0ce290d6930/mordant-jvm-jna/src/jvmMain/kotlin/com/github/ajalt/mordant/terminal/terminalinterface/jna/TerminalInterfaceProvider.jna.kt#L10-L13 which is now useless since that method will never be called in native images. Should I open a PR ?
@hubvd yes thank you!