Skip to content

resolveTableIdx gives wrong function index when using multiple wasm modules #45

@doehyunbaek

Description

@doehyunbaek

Repository reproducing the problem: https://github.com/doehyunbaek/wasabi_multimodule_fidx

Currently, resolveTableIdx relies on name property of the function to resolve function index.

// NOTE We want to get the _index_ of the resolved function to the analysis code, but the
// WebAssembly API only gives us a _function object_.
// HACK We can abuse the `.name` property of the function object to get the index.
// See the MDN, which says the "name property is the toString() result of the function's
// index in the wasm module".
// https://developer.mozilla.org/en-US/docs/WebAssembly/Exported_functions
const resolvedFunctionIdx = parseInt(resolvedFunction.name);

This does not work when the function is imported from other WebAssembly instance and the index of the function in its imported module and the index of the function in its declared module is different, e.g.

module1.wat

(module
    (func $private_func_0 (;0;) )
    (func $private_func_1 (;1;) )
    (func $shared_func (;2;) (param $0 i32) (result i32)
        local.get $0
        i32.const 1
        i32.add
    )
    (export "shared_func" (func $shared_func))
)

module2.wat

(module
    (import "env" "shared_func" (func $shared_func (;0;) ))
)

For module2, resolveTableIdx would resolve shared_func to 2, which is different from expected behavior, which is 0.

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions