Skip to content

Commit

Permalink
fix wasm linked module with imported function instantiation (#606)
Browse files Browse the repository at this point in the history
* fix wasm linked module with imported function instantiation

* update linked module imports test to have imported fn in env namespace
  • Loading branch information
munjalpatel authored Jul 11, 2024
1 parent 0ed71a2 commit ea867f3
Show file tree
Hide file tree
Showing 8 changed files with 60 additions and 1 deletion.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ priv/native/libwasmex.so
test/wasm_source/target/*
test/wasm_link_test/target/*
test/wasm_link_dep_test/target/*
test/wasm_link_import_test/target/*

.mix_tasks
**/.DS_Store
Expand Down
2 changes: 1 addition & 1 deletion native/wasmex/src/instance.rs
Original file line number Diff line number Diff line change
Expand Up @@ -75,8 +75,8 @@ fn link_and_create_instance(
wasmtime_wasi::add_to_linker(&mut linker, |s: &mut StoreData| s.wasi.as_mut().unwrap())
.map_err(|err| Error::Term(Box::new(err.to_string())))?;
}
link_modules(&mut linker, store_or_caller, linked_modules)?;
link_imports(&mut linker, imports)?;
link_modules(&mut linker, store_or_caller, linked_modules)?;
linker
.instantiate(store_or_caller, module)
.map_err(|err| Error::Term(Box::new(err.to_string())))
Expand Down
15 changes: 15 additions & 0 deletions test/test_helper.exs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ defmodule TestHelper do
@wasm_test_source_dir "#{Path.dirname(__ENV__.file)}/wasm_test"
@wasm_link_test_source_dir "#{Path.dirname(__ENV__.file)}/wasm_link_test"
@wasm_link_dep_test_source_dir "#{Path.dirname(__ENV__.file)}/wasm_link_dep_test"
@wasm_link_import_test_source_dir "#{Path.dirname(__ENV__.file)}/wasm_link_import_test"
@wasm_import_test_source_dir "#{Path.dirname(__ENV__.file)}/wasm_import_test"
@wasi_test_source_dir "#{Path.dirname(__ENV__.file)}/wasi_test"

Expand All @@ -15,6 +16,10 @@ defmodule TestHelper do
do:
"#{@wasm_link_dep_test_source_dir}/target/wasm32-unknown-unknown/debug/wasmex_link_dep_test.wasm"

def wasm_link_import_test_file_path,
do:
"#{@wasm_link_import_test_source_dir}/target/wasm32-unknown-unknown/debug/wasmex_link_import_test.wasm"

def wasm_import_test_file_path,
do: "#{@wasm_import_test_source_dir}/target/wasm32-unknown-unknown/debug/wasmex_test.wasm"

Expand All @@ -24,6 +29,7 @@ defmodule TestHelper do
def precompile_wasm_files do
{"", 0} = System.cmd("cargo", ["build"], cd: @wasm_test_source_dir)
{"", 0} = System.cmd("cargo", ["build"], cd: @wasm_import_test_source_dir)
{"", 0} = System.cmd("cargo", ["build"], cd: @wasm_link_import_test_source_dir)
{"", 0} = System.cmd("cargo", ["build"], cd: @wasi_test_source_dir)

{"", 0} =
Expand Down Expand Up @@ -80,6 +86,15 @@ defmodule TestHelper do
%{store: store, module: wasm_module}
end

def wasm_link_import_module do
{:ok, store} = Wasmex.Store.new()

{:ok, wasm_module} =
Wasmex.Module.compile(store, File.read!(TestHelper.wasm_link_import_test_file_path()))

%{store: store, module: wasm_module}
end

def wasm_import_module do
{:ok, store} = Wasmex.Store.new()

Expand Down
2 changes: 2 additions & 0 deletions test/wasm_link_import_test/.cargo/config.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
[build]
target = "wasm32-unknown-unknown"
7 changes: 7 additions & 0 deletions test/wasm_link_import_test/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 9 additions & 0 deletions test/wasm_link_import_test/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
[package]
name = "wasmex_link_import_test"
version = "0.1.0"
edition = "2021"

[dependencies]

[lib]
crate-type =["cdylib"]
9 changes: 9 additions & 0 deletions test/wasm_link_import_test/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
extern "C" {
fn imported_sum(a: i32, b: i32) -> i32;
}

#[no_mangle]
pub extern "C" fn sum(from: i32, to: i32) -> i32 {
let result = unsafe { imported_sum(from, to) };
result
}
16 changes: 16 additions & 0 deletions test/wasm_link_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -68,4 +68,20 @@ defmodule WasmLinkTest do

assert Wasmex.call_function(pid, "calc_seq", [1, 5]) == {:ok, [15]}
end

test "linking wasm modules with imports" do
calculator_wasm = File.read!(TestHelper.wasm_link_test_file_path())
utils_wasm = File.read!(TestHelper.wasm_link_import_test_file_path())

imports = %{
env: %{
imported_sum: {:fn, [:i32, :i32], [:i32], fn _context, a, b -> a + b end}
}
}

links = %{utils: %{bytes: utils_wasm}}
{:ok, pid} = Wasmex.start_link(%{bytes: calculator_wasm, links: links, imports: imports})

assert Wasmex.call_function(pid, "sum_range", [1, 5]) == {:ok, [15]}
end
end

0 comments on commit ea867f3

Please sign in to comment.