graal icon indicating copy to clipboard operation
graal copied to clipboard

Graalvm-wasm is not compatible to wasm code compiled by JWebAssembly

Open i059917 opened this issue 2 years ago • 3 comments

Describe the issue Graalvm-wasm is not compatible to wasm code compiled by JWebAssembly

Steps to reproduce the issue

  1. Use graalvm to load wasm file which is compiled by JWebAssembly. https://github.com/i-net-software/JWebAssembly
  2. Error happens with message Invalid value type: 0x02 when context.evaluate(source).
  3. The same wasm file can be load and executed by wasmer-java library.
  4. The wasm filed is attached

Describe GraalVM and your environment:

  • GraalVM version 22.1
  • JDK major version: 11
  • OS: MacOS 12.4
  • Architecture: ARM64/M1

My host code

import java.nio.file.Files;
import java.nio.file.Path;

import org.graalvm.polyglot.Context;
import org.graalvm.polyglot.Source;
import org.graalvm.polyglot.Value;
import org.graalvm.polyglot.io.ByteSequence;

public class WASMTest {

	public static void main(String[] args) throws Exception {
		
		byte[] binary = Files.readAllBytes(Path.of("./JWebassemblyTest-1.0-SNAPSHOT.wasm"));
		Context.Builder contextBuilder = Context.newBuilder("wasm");
		Source.Builder sourceBuilder = Source.newBuilder("wasm", ByteSequence.create(binary), null);
		Source source = sourceBuilder.build();
		Context context = contextBuilder.build();

		context.eval(source);

		Value bindings = context.getBindings("wasm");
		Value discount = bindings.getMember("main").getMember("discount");

		Value result = discount.execute();
		System.out.println(result);
	}
}

My source code compile to WASM

import de.inetsoftware.jwebassembly.api.annotation.Export;

public class HelloWorld {

    @Export
    public static int discount()
    {
        return 10;
    }

    @Export
    public static int discountCountry(int code)
    {
        if(code == 400)
        {
            return 5;
        }
        else if(code == 500)
        {
            return 10;
        }
        return 15;
    }
}
Error message:
    Exception in thread "main" Invalid value type: 0x02
	at org.graalvm.sdk/org.graalvm.polyglot.Context.eval(Context.java:399)
	at WASMTest.main(WASMTest.java:19)

JWebassemblyTest-1.0-SNAPSHOT.wasm.zip

i059917 avatar Jun 20 '22 09:06 i059917

Update: I made it work if I keep only one java method in my source code and compile it to wasm by JWebAssembly, it works with Graalvm-wasm. In above example, I have two methods...

Is this a limit of Graalvm-wasm?

i059917 avatar Jun 20 '22 13:06 i059917

Hi, Thank you for reporting this, we will take a look into it and get back to you

oubidar-Abderrahim avatar Jun 24 '22 14:06 oubidar-Abderrahim

Your example requires WebAssembly Multi-Value support, which is currently not part of GraalWasm.

flohuemer avatar Jun 29 '22 10:06 flohuemer

Hi all,

Apparently the latest version of Graal ( 22.3.0 ) supports Multi-Value: https://docs.oracle.com/en/graalvm/enterprise/22/docs/release-notes/

@i059917 Can you re-check?

PedroBatista avatar Dec 05 '22 16:12 PedroBatista