Cannot invoke default methods of interfaces implemented in polyglot languages without explicit --add-reads
Describe GraalVM and your environment :
- GraalVM version or commit id if built from source: 21.1.0
- CE or EE: CE
- JDK version: JDK16
- OS and OS Version: macOS 11.3
- Architecture: amd64
- The output of
java -Xinternalversion:
OpenJDK 64-Bit Server VM (16.0.1+9-jvmci-21.1-b05) for bsd-amd64 JRE (16.0.1+9-jvmci-21.1-b05),
built on Apr 16 2021 02:12:48 by "graal1" with clang 4.2.1 Compatible Apple LLVM 11.0.0 (clang-1100.0.33.17)
Have you verified this issue still happens when using the latest snapshot?
Yes
Describe the issue
It should be possible to invoke public default methods on a Java interface that has been implemented using a GraalVM polyglot language.
Prior to GraalVM 21, this was not possible because of the limitation described in JDK-8226916, which prevented the Truffle runtime from accessing the target method even if it was public and the target module exported it. This limitation was removed in JDK 14.
While it is now possible for the lookup to succeed, however, the Truffle runtime will still fail to access the target method if that is in a module that Truffle itself does not read. It is therefore now possible to get everything working by passing --add-reads org.graalvm.truffle=your.package.here on the command line, but --add-reads is a non-standard option, so this is a workaround rather than a solution.
I believe the correct solution would be for Truffle to use Module.addReads() to give itself access to public exports of target modules on demand.
Code snippet or code repository that reproduces the issue
Consider the interface
public interface Example {
default String overridden() { return "hello from Java"; }
default String inherited() { return "goodbye from Java"; }
}
And an implementation in JavaScript that leaves one of the default methods unimplemented:
example = {
overridden: function() { return "hello from JS" }
}
Now try to use this via the GraalVM polyglot API:
var src = Source.newBuilder("js", res).build();
var ctx = Context.newBuilder("js").allowAllAccess(true).build();
var example = ctx.eval(src).as(Example.class);
example.overridden();
example.inherited();
The call to overridden will succeed, because the JS implementation is always accessible.
The call to inherited will throw an IllegalAccessException unless --add-reads org.graalvm.truffle=... is used.
The full example can be found at https://github.com/shoaniki/truffle-repro.
Steps to reproduce the issue Please include both build steps as well as run steps
git clone https://github.com/shoaniki/truffle-repro.git- Ensure JAVA_HOME points to a JDK 16 build of GraalVM.
cd truffle-repro; ./gradlew run
Expected behavior
The output should be
Running test case with GraalVM CE 21.2.0-dev (Java 16)
Expecting to fail if Truffle does not choose to read the module
hello from JS
goodbye from Java
However, it will currently actually say
Running test case with GraalVM CE 21.2.0-dev (Java 16)
Expecting to fail if Truffle does not choose to read the module
hello from JS
symbolic reference class is not accessible: interface org.example.repro.Example, from interface
org.example.repro.Example (module org.example.repro), previous lookup
com.oracle.truffle.polyglot.FunctionProxyHandler (module org.graalvm.truffle)
Additional context
Details
Full stack trace of the failing lookup with GraalVM CE 21.1.0 JDK16:
java.lang.IllegalAccessException: symbolic reference class is not accessible: interface org.example.repro.Example, from interface org.example.repro.Example (module org.example.repro), previous lookup com.oracle.truffle.polyglot.FunctionProxyHandler (module org.graalvm.truffle)
at java.base/java.lang.invoke.MemberName.makeAccessException(MemberName.java:957)
at java.base/java.lang.invoke.MethodHandles$Lookup.checkSymbolicClass(MethodHandles.java:3675)
at java.base/java.lang.invoke.MethodHandles$Lookup.resolveOrFail(MethodHandles.java:3647)
at java.base/java.lang.invoke.MethodHandles$Lookup.findSpecial(MethodHandles.java:2997)
at org.graalvm.truffle/com.oracle.truffle.polyglot.FunctionProxyHandler.invokeDefault(HostInteropReflect.java:589)
at org.graalvm.truffle/com.oracle.truffle.polyglot.ObjectProxyHandler.invoke(HostInteropReflect.java:772)
at jdk.proxy2/jdk.proxy2.$Proxy36.inherited(Unknown Source)
at org.example.repro/org.example.repro.Main.test(Main.java:27)
at org.example.repro/org.example.repro.Main.main(Main.java:49)
Tracked internally on GR-31309
That is an excellent bug report. Thank you! We will investigate ASAP.
Fixed by https://github.com/oracle/graal/commit/29d856e26b6aeb048d94e99a89aaddaf5541e875. Thanks for the detailed report!