Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
1 task done
sgammon opened this issue Jan 15, 2025 · 5 comments
Open
1 task done

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

sgammon opened this issue Jan 15, 2025 · 5 comments
Assignees

Comments

@sgammon
Copy link

sgammon commented Jan 15, 2025

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 [email protected]/java.lang.Module.ensureNativeAccess(Module.java:312)
        at [email protected]/java.lang.System$2.ensureNativeAccess(System.java:2543)
        at [email protected]/jdk.internal.reflect.Reflection.ensureNativeAccess(Reflection.java:122)
        at [email protected]/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 [email protected]/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
[email protected] 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 [email protected]/java.lang.Module.ensureNativeAccess(Module.java:312)
        at [email protected]/java.lang.System$2.ensureNativeAccess(System.java:2543)
        at [email protected]/jdk.internal.reflect.Reflection.ensureNativeAccess(Reflection.java:122)
        at [email protected]/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 [email protected]/java.lang.invoke.LambdaForm$DMH/sa346b79c.invokeStaticInit(LambdaForm$DMH)
make: *** [Makefile:24: test] Error 1
@sgammon
Copy link
Author

sgammon commented Jan 15, 2025

(Invoking with --module-path target/classes.jar produces the same result.)

@sgammon
Copy link
Author

sgammon commented Jan 15, 2025

Appending --list-modules reveals:

➜  gvm-module-entry git:(master) ✗ native-image ... --add-modules=example.repro --enable-native-access=example.repro ... --list-modules

WARNING: Unknown module: example.repro specified to --enable-native-access
example.repro file:///.../target/classes/
org.graalvm.nativeimage.librarysupport file:///usr/lib/jvm/gvm.jdk23/lib/svm/library-support.jar
...

@sgammon sgammon changed the title [Native Image] --enable-native-access does not work for entrypoint JPMS module [Native Image] --enable-native-access does not work for JPMS module Jan 15, 2025
@sgammon
Copy link
Author

sgammon commented Jan 15, 2025

See branch module-dep in the repro for an example which builds two JARs, and attempts to use native access within the transitive JAR. Same error.

@sgammon
Copy link
Author

sgammon commented Jan 16, 2025

cc / #8113

This issue disappears on the latest GVM EA Java 25 release:

native-image -march=native -Os --module-path target/entry.jar --emit=build-report --no-fallback --gc=epsilon -o target/entry --add-modules=ALL-MODULE-PATH --enable-native-access=com.twoam.interop -H:+UnlockExperimentalVMOptions -H:+ForeignAPISupport --module com.twoam.interop
========================================================================================================================
GraalVM Native Image: Generating 'entry' (executable)...
========================================================================================================================
[1/8] Initializing...                                                                                    (3.0s @ 0.15GB)
 Java version: 25+4-LTS, vendor version: Oracle GraalVM 25-dev+4.1
 Graal compiler: optimization level: s, target machine: native, PGO: off
 C compiler: gcc (linux, x86_64, 13.3.0)
 Garbage collector: Epsilon GC (max heap size: 80% of RAM)
 1 user-specific feature(s):
 - com.oracle.svm.thirdparty.gson.GsonFeature
------------------------------------------------------------------------------------------------------------------------
 1 experimental option(s) unlocked:
 - '-H:+ForeignAPISupport' (origin(s): command line)
------------------------------------------------------------------------------------------------------------------------
Build resources:
 - 17.61GB of memory (58.4% 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...  [*****]                                                                    (3.2s @ 0.40GB)
    3,648 reachable types   (70.9% of    5,148 total)
    3,952 reachable fields  (39.9% of    9,904 total)
   18,456 reachable methods (46.3% of   39,871 total)
    1,150 types,    11 fields, and   165 methods registered for reflection
       57 types,    58 fields, and    52 methods registered for JNI access
        0 downcalls and 0 upcalls registered for foreign access
        4 native libraries: dl, pthread, rt, z
[3/8] Building universe...                                                                               (0.8s @ 0.57GB)
[4/8] Parsing methods...      [*]                                                                        (0.4s @ 0.55GB)
[5/8] Inlining methods...     [***]                                                                      (0.1s @ 0.51GB)
[6/8] Compiling methods...    [**]                                                                       (2.4s @ 0.37GB)
[7/8] Laying out methods...   [*]                                                                        (0.9s @ 0.40GB)
[8/8] Creating image...       [*]                                                                        (0.9s @ 0.36GB)
   3.10MB (26.63%) for code area:    11,102 compilation units
   6.56MB (56.31%) for image heap:  112,676 objects and 55 resources
   1.99MB (17.06%) for other data
  11.65MB in total
------------------------------------------------------------------------------------------------------------------------
Top 10 origins of code area:                                Top 10 object types in image heap:
   2.14MB java.base                                            1.33MB byte[] for java.lang.String
 710.68kB svm.jar (Native Image)                               1.00MB byte[] for code metadata
  75.17kB java.logging                                       792.89kB java.lang.String
  47.78kB com.oracle.svm.svm_enterprise                      632.05kB java.lang.Class
  13.04kB org.graalvm.nativeimage.base                       427.30kB heap alignment
  10.63kB jdk.proxy2                                         274.09kB byte[] for general heap data
   8.56kB jdk.internal.vm.ci                                 256.66kB java.util.HashMap$Node
   8.44kB jdk.proxy1                                         193.87kB byte[] for reflection metadata
   8.36kB org.graalvm.collections                            171.41kB byte[] for embedded resources
   6.81kB jdk.graal.compiler                                 171.00kB com.oracle.svm.core.hub.DynamicHubCompanion
   9.34kB for 6 more packages                                  1.38MB for 1075 more object types
                                   For more details, open 'entry-build-report.html'.
------------------------------------------------------------------------------------------------------------------------
Security report:
 - Binary includes Java deserialization.
 - Use '--enable-sbom' to assemble a Software Bill of Materials (SBOM).
------------------------------------------------------------------------------------------------------------------------
Recommendations:
 PGO:  Use Profile-Guided Optimizations ('--pgo') for improved throughput.
 HEAP: Set max heap for improved and more predictable memory usage.
 QBM:  Use the quick build mode ('-Ob') to speed up builds during development.
------------------------------------------------------------------------------------------------------------------------
                        0.8s (6.1% of total time) in 87 GCs | Peak RSS: 1.62GB | CPU load: 14.10
------------------------------------------------------------------------------------------------------------------------
Build artifacts:
 /home/sam/workspace/2am/labs/interop/target/entry (executable)
 /home/sam/workspace/2am/labs/interop/target/entry-build-report.html (build_info)
========================================================================================================================
Finished generating 'entry' in 12.4s.

The warning also disappears.

@zakkak
Copy link
Collaborator

zakkak commented Jan 17, 2025

If I am not mistaken this is fixed by #10076

@oubidar-Abderrahim oubidar-Abderrahim self-assigned this Jan 17, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants