Skip to content

[Native Image] --enable-native-access does not work for JPMS module #10489

Closed
@sgammon

Description

@sgammon

Describe the Issue

(1) Building a jar with a module-info.class present that has a main-class should work
(2) Additionally, identifying that entrypoint module within --enable-native-access=... should allow native access

Using the latest version of GraalVM can resolve many issues.

GraalVM Version

java version "23.0.1" 2024-10-15
Java(TM) SE Runtime Environment Oracle GraalVM 23.0.1+11.1 (build 23.0.1+11-jvmci-b01)
Java HotSpot(TM) 64-Bit Server VM Oracle GraalVM 23.0.1+11.1 (build 23.0.1+11-jvmci-b01, mixed mode, sharing)

Operating System and Version

Linux CALVIN 5.15.167.4-microsoft-standard-WSL2 #1 SMP Tue Nov 5 00:21:55 UTC 2024 x86_64 x86_64 x86_64 GNU/Linux

Build Command

native-image --module-path target/classes -m example.repro/example.Entry --enable-native-access=example.repro --initialize-at-build-time= -o target/repro --features=example.NativeFfmFeature -H:+UnlockExperimentalVMOptions -H:+ForeignAPISupport

Expected Behavior

Comparing with the JVM command:

java --module-path target/classes.jar --enable-native-access=example.repro -m example.repro/example.Entry

I expected the output:

Repro start
*
**
***
****
*****
******
*******
********
Repro finished

Actual Behavior

Invoking the native image instead produces:

Repro start
Exception in thread "main" java.lang.IllegalCallerException: Illegal native access from: module example.repro
        at java.base@23.0.1/java.lang.Module.ensureNativeAccess(Module.java:312)
        at java.base@23.0.1/java.lang.System$2.ensureNativeAccess(System.java:2543)
        at java.base@23.0.1/jdk.internal.reflect.Reflection.ensureNativeAccess(Reflection.java:122)
        at java.base@23.0.1/jdk.internal.foreign.layout.ValueLayouts$OfAddressImpl.withTargetLayout(ValueLayouts.java:335)
        at example.repro/example.Entry.invokeStrdup(Entry.java:31)
        at example.repro/example.Entry.main(Entry.java:10)
        at java.base@23.0.1/java.lang.invoke.LambdaForm$DMH/sa346b79c.invokeStaticInit(LambdaForm$DMH)

Steps to Reproduce

  1. Clone https://github.com/sgammon/gvm-module-native-repro
  2. make
  3. Or, see sample output in README

Additional Context

No response

Build Log Output and Error Messages

rm -fr ./target
rm -fr libs && mkdir -p libs
cd libs && wget https://repo1.maven.org/maven2/org/graalvm/sdk/nativeimage/24.1.1/nativeimage-24.1.1.jar
--2025-01-15 15:17:51--  https://repo1.maven.org/maven2/org/graalvm/sdk/nativeimage/24.1.1/nativeimage-24.1.1.jar
Resolving repo1.maven.org (repo1.maven.org)... 199.232.192.209, 199.232.196.209, 2a04:4e42:4c::209, ...
Connecting to repo1.maven.org (repo1.maven.org)|199.232.192.209|:443... connected.
HTTP request sent, awaiting response... 200 OK
Length: 226628 (221K) [application/java-archive]
Saving to: ‘nativeimage-24.1.1.jar’

nativeimage-24.1.1.jar                                              100%[=================================================================================================================================================================>] 221.32K  --.-KB/s    in 0.03s

2025-01-15 15:17:51 (6.91 MB/s) - ‘nativeimage-24.1.1.jar’ saved [226628/226628]

cd libs && wget https://repo1.maven.org/maven2/org/graalvm/sdk/word/24.1.1/word-24.1.1.jar
--2025-01-15 15:17:51--  https://repo1.maven.org/maven2/org/graalvm/sdk/word/24.1.1/word-24.1.1.jar
Resolving repo1.maven.org (repo1.maven.org)... 199.232.192.209, 199.232.196.209, 2a04:4e42:4d::209, ...
Connecting to repo1.maven.org (repo1.maven.org)|199.232.192.209|:443... connected.
HTTP request sent, awaiting response... 200 OK
Length: 22529 (22K) [application/java-archive]
Saving to: ‘word-24.1.1.jar’

word-24.1.1.jar                                                     100%[=================================================================================================================================================================>]  22.00K  --.-KB/s    in 0.002s

2025-01-15 15:17:51 (8.73 MB/s) - ‘word-24.1.1.jar’ saved [22529/22529]

mkdir -p target/classes
javac -d target/classes --module-path libs/nativeimage-24.1.1.jar:libs/word-24.1.1.jar Entry.java NativeFfmFeature.java module-info.java
cd target/classes && jar --create --file ../classes.jar --main-class example.Entry --module-version 1.0 example module-info.class
jar --file=target/classes.jar --describe-module
example.repro@1.0 jar:file:///home/sam/workspace/2am/labs/repro/gvm-module-entry/target/classes.jar!/module-info.class
requires java.base
requires org.graalvm.nativeimage static
requires org.graalvm.word static
contains example
main-class example.Entry

native-image --module-path target/classes -m example.repro/example.Entry --enable-native-access=example.repro --initialize-at-build-time= -o target/repro --features=example.NativeFfmFeature -H:+UnlockExperimentalVMOptions -H:+ForeignAPISupport
WARNING: Unknown module: example.repro specified to --enable-native-access
========================================================================================================================
GraalVM Native Image: Generating 'repro' (executable)...
========================================================================================================================
[1/8] Initializing...                                                                                    (2.5s @ 0.07GB)
 Java version: 23.0.1+11, vendor version: Oracle GraalVM 23.0.1+11.1
 Graal compiler: optimization level: 2, target machine: x86-64-v3, PGO: ML-inferred
 C compiler: gcc (linux, x86_64, 13.3.0)
 Garbage collector: Serial GC (max heap size: 80% of RAM)
 2 user-specific feature(s):
 - com.oracle.svm.thirdparty.gson.GsonFeature
 - example.NativeFfmFeature
------------------------------------------------------------------------------------------------------------------------
 1 experimental option(s) unlocked:
 - '-H:+ForeignAPISupport' (origin(s): command line)
------------------------------------------------------------------------------------------------------------------------
Build resources:
 - 12.67GB of memory (42.0% of 30.17GB system memory, determined at start)
 - 32 thread(s) (100.0% of 32 available processor(s), determined at start)
[2/8] Performing analysis...  [*****]                                                                    (2.2s @ 0.29GB)
    2,596 reachable types   (63.4% of    4,097 total)
    2,429 reachable fields  (39.2% of    6,199 total)
   12,499 reachable methods (42.1% of   29,679 total)
      903 types,    17 fields, and   142 methods registered for reflection
       49 types,    34 fields, and    48 methods registered for JNI access
        1 downcalls and 0 upcalls registered for foreign access
        4 native libraries: dl, pthread, rt, z
[3/8] Building universe...                                                                               (0.7s @ 0.31GB)
[4/8] Parsing methods...      [*]                                                                        (0.8s @ 0.33GB)
[5/8] Inlining methods...     [***]                                                                      (0.3s @ 0.37GB)
[6/8] Compiling methods...    [***]                                                                      (6.2s @ 0.45GB)
[7/8] Laying out methods...   [*]                                                                        (0.7s @ 0.40GB)
[8/8] Creating image...       [*]                                                                        (0.6s @ 0.55GB)
   4.47MB (46.30%) for code area:     6,066 compilation units
   4.44MB (45.97%) for image heap:   65,179 objects and 52 resources
 763.89kB ( 7.73%) for other data
   9.65MB in total
------------------------------------------------------------------------------------------------------------------------
Top 10 origins of code area:                                Top 10 object types in image heap:
   2.61MB java.base                                            1.06MB byte[] for code metadata
   1.51MB svm.jar (Native Image)                             821.43kB byte[] for java.lang.String
  88.75kB com.oracle.svm.svm_enterprise                      481.09kB heap alignment
  36.97kB org.graalvm.nativeimage.base                       450.55kB java.lang.Class
  34.55kB jdk.proxy2                                         443.13kB java.lang.String
  33.42kB example.repro                                      138.88kB java.util.HashMap$Node
  28.39kB jdk.proxy1                                         138.70kB byte[] for reflection metadata
  27.34kB jdk.graal.compiler                                 121.69kB com.oracle.svm.core.hub.DynamicHubCompanion
  21.26kB org.graalvm.collections                            114.52kB char[]
  21.06kB jdk.internal.vm.ci                                  86.02kB java.lang.Object[]
  18.70kB for 5 more packages                                657.84kB for 685 more object types
                            Use '--emit build-report' to create a report with more details.
------------------------------------------------------------------------------------------------------------------------
Security report:
 - Binary includes Java deserialization.
 - Use '--enable-sbom' to assemble a Software Bill of Materials (SBOM).
------------------------------------------------------------------------------------------------------------------------
Recommendations:
 G1GC: Use the G1 GC ('--gc=G1') for improved latency and throughput.
 PGO:  Use Profile-Guided Optimizations ('--pgo') for improved throughput.
 HEAP: Set max heap for improved and more predictable memory usage.
 CPU:  Enable more CPU features with '-march=native' for improved performance.
 QBM:  Use the quick build mode ('-Ob') to speed up builds during development.
------------------------------------------------------------------------------------------------------------------------
                       1.0s (6.6% of total time) in 221 GCs | Peak RSS: 1.44GB | CPU load: 16.01
------------------------------------------------------------------------------------------------------------------------
Build artifacts:
 /home/sam/workspace/2am/labs/repro/gvm-module-entry/target/repro (executable)
========================================================================================================================
Finished generating 'repro' in 14.5s.
java --module-path target/classes.jar --enable-native-access=example.repro -m example.repro/example.Entry
Repro start
*
**
***
****
*****
******
*******
********
Repro finished
./target/repro
Repro start
Exception in thread "main" java.lang.IllegalCallerException: Illegal native access from: module example.repro
        at java.base@23.0.1/java.lang.Module.ensureNativeAccess(Module.java:312)
        at java.base@23.0.1/java.lang.System$2.ensureNativeAccess(System.java:2543)
        at java.base@23.0.1/jdk.internal.reflect.Reflection.ensureNativeAccess(Reflection.java:122)
        at java.base@23.0.1/jdk.internal.foreign.layout.ValueLayouts$OfAddressImpl.withTargetLayout(ValueLayouts.java:335)
        at example.repro/example.Entry.invokeStrdup(Entry.java:31)
        at example.repro/example.Entry.main(Entry.java:10)
        at java.base@23.0.1/java.lang.invoke.LambdaForm$DMH/sa346b79c.invokeStaticInit(LambdaForm$DMH)
make: *** [Makefile:24: test] Error 1

Metadata

Metadata

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions