Skip to content

Linker does not properly define imports (C API) #2211

@7Hazard

Description

@7Hazard

Using the C API, defining imports with the Linker do not work properly. When attempting to instantiate the module in question, it errors with the following:
unknown import: `env::Test` has not been defined

This is how the module is attempted to be instantiated:

wasm_trap_t* trap = nullptr;
wasm_instance_t* instance = nullptr;

wasm_engine_t* engine = wasm_engine_new();
wasm_store_t* store = wasm_store_new(engine);
wasmtime_linker_t* linker = wasmtime_linker_new(store);

// Read wasm file
wasm_byte_vec_t wasm;
std::ifstream file("wasmtest.wasm", std::ios::binary | std::ios::ate);
if(!file.good()) {
    std::cout << "File wasmtest.wasm bad" << std::endl;
    return 1;
}
auto filesize = file.tellg();
file.seekg(0, std::ios::beg);
wasm_byte_vec_new_uninitialized(&wasm, filesize);
if(!file.read(wasm.data, filesize))
{
    std::cout << "Could not read file" << std::endl;
    return 1;
}

// Compile module
wasm_module_t* module = nullptr;
error = wasmtime_module_new(engine, &wasm, &module);
if (error != nullptr)
{
    logWasmError("Failed to compile module", error, nullptr);
    return 1;
}
wasm_byte_vec_delete(&wasm);

// MAKE IMPORTS
{
    auto import = [](const wasm_val_t args[], wasm_val_t results[])->wasm_trap_t*{
        printf("Hello world");
        return nullptr;
    };
    wasm_valtype_vec_t types;
    wasm_valtype_vec_new_uninitialized(&types, 0);
    wasm_functype_t *type = wasm_functype_new(&types, &types);
    auto function = wasm_func_new(store, type, import);
    auto extern_ = wasm_func_as_extern(function);
    
    wasm_name_t module;
    wasm_name_new_from_string(&module, "env");
    wasm_name_t func;
    wasm_name_new_from_string(&func, "Test");
    wasmtime_linker_define(linker, &module, &func, extern_);
    wasm_name_delete(&func);
}

// INSTANTIATE MODULE
error = wasmtime_linker_instantiate(linker, module, &instance, &trap);
if (error || trap)
{
    logWasmError("Failed to instantiate module", error, trap);
    return 1;
}

This is the module that is being attempted to be loaded (compiled with clang):

extern "C" void Test();

extern "C" void main() {
    Test();
}

The imports in the wat looks like this:

(type $t0 (func))
(import "env" "Test" (func $Test (type $t0)))

This was tested on Windows 10, using the C API build from https://github.com/bytecodealliance/wasmtime/releases/tag/v0.19.0.

Metadata

Metadata

Assignees

Labels

bugIncorrect behavior in the current implementation that needs fixing

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions