tessi / wasmex

Execute WebAssembly from Elixir
MIT License
546 stars 31 forks source link

Instantiation fails when linked module is dependent on imports #600

Closed munjalpatel closed 2 months ago

munjalpatel commented 2 months ago

The following test fails when instantiating a module that has a linked module which depends on imports.

test "extism kernel test" do
    wasm_modules_dir = "#{Path.dirname(__ENV__.file)}/example_wasm_files"

    config_get = fn %{_memory: %Wasmex.Memory{}, _caller: %Wasmex.StoreOrCaller{}} = _context,
                    i ->
      IO.puts("Inside config_get")
      IO.inspect(i)
    end

    imports = %{
      env: %{
        config_get: {:fn, [:i64], [:i64], config_get}
      }
    }

    plugin_module = File.read!("#{wasm_modules_dir}/count_vowels.wasm")
    extism = File.read!("#{wasm_modules_dir}/extism.wasm")

    links = %{
      "extism:host/env" => %{bytes: extism}
    }

    {:ok, pid} = Wasmex.start_link(%{bytes: plugin_module, links: links, imports: imports})
    result = Wasmex.call_function(pid, "count_vowels", [])

    IO.inspect(result)
  end

Error:

** (MatchError) no match of right hand side value: {:error, "Could not instantiate linked module: unknown import: `env::config_get` has not been defined"}
tessi commented 2 months ago

hey @munjalpatel thanks for reporting the issue! I've seen the discussion on discord and want to have a look too. are you OK with sharing your example wasm files here for reproducibility?

munjalpatel commented 2 months ago

@tessi here are the links:

count_vowels.wasm: https://modsurfer.dylibso.com/module?hash=93898457953d30d016f712ccf4336ce7e9971db5f7f3aff1edd252764f75d5d7

kernel.wasm: https://modsurfer.dylibso.com/module?hash=3cffbac1f4f47ed5fc6ca103f095144f2356329fb1345a5507911ef949383bfa

BTW, I have working solution on my fork. Still need to add tests and update docs. I'll create PR tonight or sometime tomorrow.

tessi commented 2 months ago

BTW, I have working solution on my fork. Still need to add tests and update docs. I'll create PR tonight or sometime tomorrow.

sounds perfect, thanks! 💛