Skip to content

Handle --jvm flag in process with JNI instantiated JVM#12843

Merged
mergify[bot] merged 65 commits intodevelopfrom
wip/jtulach/Jni12842
May 9, 2025
Merged

Handle --jvm flag in process with JNI instantiated JVM#12843
mergify[bot] merged 65 commits intodevelopfrom
wip/jtulach/Jni12842

Conversation

@JaroslavTulach
Copy link
Copy Markdown
Member

@JaroslavTulach JaroslavTulach commented Apr 14, 2025

Pull Request Description

Fixes #12842 by designing a JNI OS environment, providing its implementations for supported platforms and using it to handle --jvm flag. All the Standard Library Tests (jvm) use this new launch-in-the-same-process technique.

Checklist

Please ensure that the following checklist has been satisfied before submitting the PR:

  • All code follows the
    Scala,
    Java,
  • Unit tests have been written where possible.

@JaroslavTulach JaroslavTulach added CI: No changelog needed Do not require a changelog entry for this PR. -compiler labels Apr 14, 2025
@JaroslavTulach JaroslavTulach self-assigned this Apr 14, 2025
@JaroslavTulach JaroslavTulach changed the title API for access to JVM via JNI Handle --jvm flag in process with JNI instantiated JVM Apr 14, 2025
@JaroslavTulach
Copy link
Copy Markdown
Member Author

JaroslavTulach commented Apr 22, 2025

Now the problem is in executing Generic_JDBC_Tests - e.g. the switch to JVM mode isn't yet working correctly:

[org.enso.runner.JavaFinder] No appropriate JDK found in the distribution runtimes. Trying system-wide JDK.
Error occurred during initialization of boot layer
java.lang.UnsatisfiedLinkError: 'int sun.nio.fs.UnixNativeDispatcher.init()'

Unsatisfied link problem on Linux (update on May 7, 2025: got it working after removing java.desktop module from native image). We have linkage problem on Windows:

jvm.lib(jni.obj) : error LNK2005: JNI_GetDefaultJavaVMInitArgs already defined in enso.obj
jvm.lib(jni.obj) : error LNK2005: JNI_CreateJavaVM already defined in enso.obj
jvm.lib(jni.obj) : error LNK2005: JNI_GetCreatedJavaVMs already defined in enso.obj
net.lib(net_util_md.obj) : error LNK2005: DllMain already defined in jvm.lib(os_windows.obj)

Update on May 2, 2025: the Windows version works on GraalVM for JDK21, but not on any newer version!

On Mac the --jvm option already works OK.

@JaroslavTulach
Copy link
Copy Markdown
Member Author

JaroslavTulach commented Apr 30, 2025

Linux

Trying to isolate the UnsatisfiedLinkError...

Error occurred during initialization of boot layer
java.lang.UnsatisfiedLinkError: 'int sun.nio.fs.UnixNativeDispatcher.init()'

looks like the set of enabled modules is the cause of the problem! With:

diff --git build.sbt build.sbt
diff --git build.sbt build.sbt
index bbdeef42ce..00f114fa64 100644
$ git diff
diff --git build.sbt build.sbt
diff --git build.sbt build.sbt
index bbdeef42ce..764f9bf079 100644
--- build.sbt
+++ build.sbt
@@ -3912,7 +3912,7 @@ lazy val `engine-runner` = project
       val NI_MODULES =
         "org.graalvm.nativeimage,org.graalvm.nativeimage.builder,org.graalvm.nativeimage.base,org.graalvm.nativeimage.driver,org.graalvm.nativeimage.librarysupport,org.graalvm.nativeimage.objectfile,org.graalvm.nativeimage.pointsto,com.oracle.graal.graal_enterprise,com.oracle.svm.svm_enterprise"
       val JDK_MODULES =
-        "java.desktop,java.naming,java.net.http,jdk.charsets,jdk.crypto.ec,jdk.localedata,jdk.httpserver,java.rmi"
+        "java.naming,java.net.http,jdk.charsets,jdk.crypto.ec,jdk.localedata,jdk.httpserver,java.rmi"
       val DEBUG_MODULES  = "jdk.jdwp.agent"
       val PYTHON_MODULES = "jdk.security.auth,java.naming"

it is possible to execute

enso$ ./built-distribution/enso-engine-*/enso-*/bin/enso --jvm --run test/Base_Tests

after building native image executable with ENSO_LAUNCHER=-ls,fast,debug.


@Override
public List<String> getLibraries() {
return List.of("jvm");
Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Copy Markdown
Member Author

@JaroslavTulach JaroslavTulach May 9, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

),
assemblyMergeStrategy := { case _ =>
MergeStrategy.preferProject
}
Copy link
Copy Markdown
Member Author

@JaroslavTulach JaroslavTulach May 9, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  • done by @Akirathan at 179aff3
  • thank you - it's a complicated change - I could never do it myself
  • but the CI is green!
  • finally we can move pass this JNI struggle...

private static Map<Integer, HSSFColor> indexHash;
private static Map<HSSFColorPredefined, HSSFColor> enumList;

private final int index;
Copy link
Copy Markdown
Member Author

@JaroslavTulach JaroslavTulach May 9, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@JaroslavTulach JaroslavTulach requested a review from Akirathan May 9, 2025 07:46
Copy link
Copy Markdown
Contributor

@Akirathan Akirathan left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So if I understand it correctly, all the Standard Library Tests (jvm) use the new launch-in-the-same-process technique?

@JaroslavTulach JaroslavTulach added the CI: Ready to merge This PR is eligible for automatic merge label May 9, 2025
@mergify mergify bot merged commit 59eb1f2 into develop May 9, 2025
78 of 80 checks passed
@mergify mergify bot deleted the wip/jtulach/Jni12842 branch May 9, 2025 17:40
@JaroslavTulach
Copy link
Copy Markdown
Member Author

JaroslavTulach commented May 11, 2025

This change is already present in this Nightly Release:

These values should be smaller, because we removed java.desktop dependency from native image.

Akirathan pushed a commit that referenced this pull request May 12, 2025
Frizi pushed a commit that referenced this pull request May 13, 2025
Fixes #12842 by designing a _JNI OS environment_, providing its implementations for supported platforms and using it to handle `--jvm` flag. All the _Standard Library Tests (jvm)_ use this new launch-in-the-same-process technique.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

-compiler CI: Clean build required CI runners will be cleaned before and after this PR is built. CI: No changelog needed Do not require a changelog entry for this PR. CI: Ready to merge This PR is eligible for automatic merge

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Use JNI to handle --jvm invocation "in process"

3 participants