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

Add support for WASM imports #44

Closed
artur-ciocanu opened this issue Nov 28, 2020 · 1 comment
Closed

Add support for WASM imports #44

artur-ciocanu opened this issue Nov 28, 2020 · 1 comment
Assignees
Labels
📑 duplicate This issue or pull request already exists

Comments

@artur-ciocanu
Copy link

Motivation

I was trying to use wasmer-java to create a sample Java application that would be able to import and execute an Open Policy Agent policy compiled to WASM. More details about Open Policy Agent can be found here: https://www.openpolicyagent.org/docs/latest/wasm/.

An Open Policy Agent compiled WASM has a couple of imports, here is an excerpt from a policy.wasm using WASM text format:

  (import "env" "memory" (memory $env.memory 2))
  (import "env" "opa_abort" (func $env.opa_abort (type $t3)))
  (import "env" "opa_builtin0" (func $env.opa_builtin0 (type $t1)))
  (import "env" "opa_builtin1" (func $env.opa_builtin1 (type $t0)))
  (import "env" "opa_builtin2" (func $env.opa_builtin2 (type $t5)))
  (import "env" "opa_builtin3" (func $env.opa_builtin3 (type $t6)))
  (import "env" "opa_builtin4" (func $env.opa_builtin4 (type $t7)))

When trying to load use a policy.wasm from a simple Java app like the one below:

public class Application {

  public static void main(String[] args) throws IOException {
    byte[] wasmBytes = Files.readAllBytes(Paths.get("policy.wasm"));
    Instance instance = new Instance(wasmBytes);

    System.out.println(instance);
  }
}

I get the following exception:

Exception in thread "main" java.lang.RuntimeException: Failed to instantiate the module: 7 link errors: (1 of 7) Import not found, namespace: env, name: opa_abort (2 of 7) Import not found, namespace: env, name: opa_builtin0 (3 of 7) Import not found, namespace: env, name: opa_builtin1 (4 of 7) Import not found, namespace: env, name: opa_builtin2 (5 of 7) Import not found, namespace: env, name: opa_builtin3 (6 of 7) Import not found, namespace: env, name: opa_builtin4 (7 of 7) Import not found, namespace: env, name: memory
	at org.wasmer.Instance.nativeInstantiate(Native Method)
	at org.wasmer.Instance.<init>(Instance.java:45)
	at com.adobe.target.gw.Application.main(Application.java:12)

Proposed solution

As far as I can tell by looking here https://github.com/wasmerio/wasmer-java/blob/master/src/instance.rs#L26, we always pass an empty imports value. It would be great if we could pass some kind of object to Instance.nativeInstantiate(...) that would allow us to populate the "imports" before passing to wasmer runtime.

In the browser when loading WASM we can pass an import object like WebAssembly.instantiate(bufferSource, importObject), so it would be nice to have something similar in wasmer-java.

Alternatives

Not sure if there are other alternatives

Additional context

None

@artur-ciocanu artur-ciocanu added the 🎉 enhancement New feature or request label Nov 28, 2020
@Hywan
Copy link
Contributor

Hywan commented Nov 30, 2020

Thanks for the issue! Duplicate of #11. I'm going to close this one :-).

@Hywan Hywan closed this as completed Nov 30, 2020
@Hywan Hywan self-assigned this Nov 30, 2020
@Hywan Hywan added 📑 duplicate This issue or pull request already exists and removed 🎉 enhancement New feature or request labels Nov 30, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
📑 duplicate This issue or pull request already exists
Projects
None yet
Development

No branches or pull requests

2 participants