graal icon indicating copy to clipboard operation
graal copied to clipboard

Invariant contract violation for receiver

Open pminearo opened this issue 4 years ago • 6 comments

Describe GraalVM and your environment :

  • GraalVM version 22.1.0
  • CE or EE: CE
  • JDK version: JDK17
  • OS and OS Version: MacOs
  • Architecture: aarch64
  • The output of java -Xinternalversion:
$ java -Xinternalversion
OpenJDK 64-Bit Server VM (17.0.3+7-jvmci-22.1-b06) for bsd-aarch64 JRE (17.0.3+7-jvmci-22.1-b06), built on Apr 20 2022 04:58:20 by "graal" with clang Apple LLVM 12.0.0 (clang-1200.0.32.29)

Have you verified this issue still happens when using the latest snapshot? No, only working with stable releases.

Describe the issue I am trying to get around https://github.com/oracle/graal/issues/4593 (at least until this is added) by using a JavaScript Proxy on top of a Polyglot Proxy Object which contains one of our Java Objects. Below is a simplified version of our code, but it does represent what is happening.

I am trying to figure out how to access properties from a JavaScript Proxy in Java.

final ProxyObject target = // Some Polyglot Proxied Object
final Value proxy = context.eval(IScriptConstants.SCRIPT_TYPE_JS, "(function (target) {return new Proxy(target, {});})");
final Value proxiedObject = proxy.execute(target);
final Object property = proxiedObject.getMember("someProperty");

This code gives me the following exception when I call getMember():

org.graalvm.polyglot.PolyglotException: java.lang.AssertionError: Invariant contract violation for receiver DynamicObject<Proxy>@707ca986(com.oracle.truffle.js.runtime.builtins.JSProxyObject) and identifier someProperty

If the Polyglot Value is a JavaScript Proxy, what is the proper way to access a property? Barring my situation, it may come up that there is a Polyglot Value that represents a JavaScript Proxy. It would be good to know how to access members in this situation. — OR -- Is this not allowed?

I posted this to the Slack Community and got this response.

[Christian Humer](https://app.slack.com/team/UPDMKLNQ0) (https://graalvm.slack.com/archives/CNBFR78F9/p1654109851699239?thread_ts=1654106811.413229&cid=CNBFR78F9)
 org.graalvm.polyglot.PolyglotException: java.lang.AssertionError: Invariant contract violation for receiver
That is a bug. you should not be able to see this.

Code snippet or code repository that reproduces the issue

public static void main(final String[] args) {

        final Map<String, Object> map = new HashMap<>();
        map.put("id", "99");

        final ProxyObject target = ProxyObject.fromMap(map);
        final Context context = Context.newBuilder().allowAllAccess(true).build();
        final Value proxy = context.eval("js", "(function (target) {return new Proxy(target, {});})");
        final Value proxiedObject = proxy.execute(target);
        final Object property = proxiedObject.getMember("id");
    }

Steps to reproduce the issue Please include both build steps as well as run steps

  1. Run Code above

Expected behavior To be able to access properties within a JavaScript Proxy from a Polyglot Value in Java.

Additional context Add any other context about the problem here. Specially important are stack traces or log output. Feel free to link to gists or to screenshots if necesary

Details
 org.graalvm.polyglot.PolyglotException: java.lang.AssertionError: Invariant contract violation for receiver DynamicObject<Proxy>@54336976(com.oracle.truffle.js.runtime.builtins.JSProxyObject) and identifier id.

	at org.graalvm.truffle/com.oracle.truffle.api.interop.InteropLibrary$Asserts.readMember(InteropLibrary.java:3358)
	at org.graalvm.truffle/com.oracle.truffle.polyglot.PolyglotValueDispatch$InteropValue$GetMemberNode.doCached(PolyglotValueDispatch.java:3964)
	at org.graalvm.truffle/com.oracle.truffle.polyglot.PolyglotValueDispatchFactory$InteropValueFactory$GetMemberNodeGen.executeAndSpecialize(PolyglotValueDispatchFactory.java:5491)
	at org.graalvm.truffle/com.oracle.truffle.polyglot.PolyglotValueDispatchFactory$InteropValueFactory$GetMemberNodeGen.executeImpl(PolyglotValueDispatchFactory.java:5438)
	at org.graalvm.truffle/com.oracle.truffle.polyglot.HostToGuestRootNode.execute(HostToGuestRootNode.java:124)
	at org.graalvm.sdk/org.graalvm.polyglot.Value.getMember(Value.java:754)
	....
	....
Original Internal Error: 
java.lang.AssertionError: Invariant contract violation for receiver DynamicObject<Proxy>@54336976(com.oracle.truffle.js.runtime.builtins.JSProxyObject) and identifier id.
	at org.graalvm.truffle/com.oracle.truffle.api.interop.InteropLibrary$Asserts.readMember(InteropLibrary.java:3358)
	at org.graalvm.truffle/com.oracle.truffle.polyglot.PolyglotValueDispatch$InteropValue$GetMemberNode.doCached(PolyglotValueDispatch.java:3964)
	at org.graalvm.truffle/com.oracle.truffle.polyglot.PolyglotValueDispatchFactory$InteropValueFactory$GetMemberNodeGen.executeAndSpecialize(PolyglotValueDispatchFactory.java:5491)
	at org.graalvm.truffle/com.oracle.truffle.polyglot.PolyglotValueDispatchFactory$InteropValueFactory$GetMemberNodeGen.executeImpl(PolyglotValueDispatchFactory.java:5438)
	at org.graalvm.truffle/com.oracle.truffle.polyglot.HostToGuestRootNode.execute(HostToGuestRootNode.java:124)
	at jdk.internal.vm.compiler/org.graalvm.compiler.truffle.runtime.OptimizedCallTarget.executeRootNode(OptimizedCallTarget.java:659)
	at jdk.internal.vm.compiler/org.graalvm.compiler.truffle.runtime.OptimizedCallTarget.profiledPERoot(OptimizedCallTarget.java:631)
	at jdk.internal.vm.compiler/org.graalvm.compiler.truffle.runtime.OptimizedCallTarget.callBoundary(OptimizedCallTarget.java:564)
	at jdk.internal.vm.compiler/org.graalvm.compiler.truffle.runtime.OptimizedCallTarget.doInvoke(OptimizedCallTarget.java:548)
	at jdk.internal.vm.compiler/org.graalvm.compiler.truffle.runtime.GraalRuntimeSupport.callProfiled(GraalRuntimeSupport.java:241)
	at org.graalvm.truffle/com.oracle.truffle.polyglot.PolyglotValueDispatch$InteropValue.getMember(PolyglotValueDispatch.java:2226)
	at org.graalvm.sdk/org.graalvm.polyglot.Value.getMember(Value.java:754)
	....
	....  

pminearo avatar Jun 01 '22 19:06 pminearo

Thank you for the report. Your test-case hits an incomplete branch in graal-js (in JSProxy.getOwnProperty()). The mentioned exception is a consequence of the omission in this method. I will take care of this omission.

iamstolis avatar Jun 01 '22 20:06 iamstolis

Fixed by https://github.com/oracle/graaljs/commit/07341818f204b566ee0474f0f755c174fdd565d6.

iamstolis avatar Jun 02 '22 17:06 iamstolis

Thanks for fixing this so fast! Will this fix be included in the next release of GraalVM?

pminearo avatar Jun 02 '22 21:06 pminearo

Will this fix be included in the next release of GraalVM?

Yes, it will be part of GraalVM 22.2.0 that is planned for July.

iamstolis avatar Jun 03 '22 08:06 iamstolis

I wanted to test this out the fix before the release in July. I pulled down graalvm-ce-java17-darwin-aarch64-dev.tar.gz on Friday and my test still failed with the same exception. I am assuming this fix is not in that dev build? If my assumption is correct, do I need to include the dev build for graal-js in my my Maven dependency until the release?

pminearo avatar Jun 06 '22 15:06 pminearo

I wanted to test this out the fix before the release in July. I pulled down graalvm-ce-java17-darwin-aarch64-dev.tar.gz on Friday and my test still failed with the same exception. I am assuming this fix is not in that dev build?

Note that the fix was made in graaljs repository (not in graal one). The development builds are based on the content of graal repository (and on the versions imported by this repository). So, the imported version of graaljs must be bumped in graal for the fix/changes to appear in the development builds. We (graaljs) team usually bump the version during weekend. We performed the last bump ~40 hours ago.

In other words, the mentioned dev build does not contain the fix, I believe, but the fix should be part of the next development build, I guess.

iamstolis avatar Jun 06 '22 16:06 iamstolis