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

Fix indirect call to dynamic imported function #4929

Merged
merged 4 commits into from
Jul 16, 2024
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 24 additions & 24 deletions Cargo.lock

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

9 changes: 6 additions & 3 deletions lib/compiler/src/engine/resolver.rs
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ fn get_runtime_size(context: &StoreObjects, extern_: &VMExtern) -> Option<u32> {
pub fn resolve_imports(
module: &ModuleInfo,
imports: &[VMExtern],
context: &StoreObjects,
context: &mut StoreObjects,
finished_dynamic_function_trampolines: &BoxedSlice<FunctionIndex, FunctionBodyPtr>,
memory_styles: &PrimaryMap<MemoryIndex, MemoryStyle>,
_table_styles: &PrimaryMap<TableIndex, TableStyle>,
Expand Down Expand Up @@ -104,14 +104,17 @@ pub fn resolve_imports(
}
match *resolved {
VMExtern::Function(handle) => {
let f = handle.get(context);
let f = handle.get_mut(context);
let address = match f.kind {
VMFunctionKind::Dynamic => {
// If this is a dynamic imported function,
// the address of the function is the address of the
// reverse trampoline.
let index = FunctionIndex::new(function_imports.len());
finished_dynamic_function_trampolines[index].0 as *mut VMFunctionBody as _
let ptr = finished_dynamic_function_trampolines[index].0
Arshia001 marked this conversation as resolved.
Show resolved Hide resolved
as *mut VMFunctionBody as _;
unsafe { f.anyfunc.as_ptr().as_mut() }.func_ptr = ptr;
ptr
}
VMFunctionKind::Static => unsafe { f.anyfunc.as_ptr().as_ref().func_ptr },
};
Expand Down
9 changes: 7 additions & 2 deletions tests/compilers/imports.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,18 @@ use wasmer::*;

fn get_module(store: &Store) -> Result<Module> {
let wat = r#"
(import "host" "0" (func))
(type (func))
(import "host" "0" (func $host_func_0 (type 0)))
Arshia001 marked this conversation as resolved.
Show resolved Hide resolved
(import "host" "1" (func (param i32) (result i32)))
(import "host" "2" (func (param i32) (param i64)))
(import "host" "3" (func (param i32 i64 i32 f32 f64)))
(memory $mem 1)
(export "memory" (memory $mem))

(func $foo
call 0
i32.const 1
call_indirect (type 0)

i32.const 0
call 1
i32.const 1
Expand All @@ -37,6 +40,8 @@ fn get_module(store: &Store) -> Result<Module> {
f64.const 500
call 3
)
(table 2 2 funcref)
(elem (i32.const 1) func $host_func_0)
(start $foo)
"#;

Expand Down
Loading