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

Incompatible import type for resized table #3197

Closed
benozol opened this issue Sep 23, 2022 · 2 comments · Fixed by #3221
Closed

Incompatible import type for resized table #3197

benozol opened this issue Sep 23, 2022 · 2 comments · Fixed by #3221
Assignees
Labels
bug Something isn't working 🥇 good first issue Good for newcomers priority-high High priority issue
Milestone

Comments

@benozol
Copy link

benozol commented Sep 23, 2022

Describe the bug

A WASM module that imports a table with a minimum size can only be instantiated with an imported table that was created with a compatible table type. Importing a table that was created with a lower minimum size and resized using Table::grow to a compatible size results in an error incompatible import type.

Observed using Wasmer 2.3.0 and 3.0.0-beta.

Steps to reproduce

Using Wasmer 3.0.0-beta:

use wasmer::{imports, Instance, Module, Store, Table, TableType, Type, Value};

const WAT:&str = r#"(module (table (import "env" "table") 100 funcref))"#;

fn wasmer3() -> anyhow::Result<()> {
    let mut store = Store::default();
    let module = Module::new(&store, WAT)?;
    let ty = TableType::new(Type::FuncRef, 0, None);
    let table = Table::new(&mut store, ty, Value::FuncRef(None))?;
    table.grow(&mut store, 100, Value::FuncRef(None))?;
    let imports = imports! {"env" => {"table" => table}};
    let _instance = Instance::new(&mut store, &module, &imports)?;
    Ok(())
}

Expected behavior

After calling table.grow the size of table is compatible with the WASM import declaration, and the instance is created.

Actual behavior

Error while importing "env"."table": incompatible import type. Expected Table(TableType { ty: FuncRef, minimum: 100, maximum: None }) but received Table(TableType { ty: FuncRef, minimum: 0, maximum: None }).

Additional context

The WASM specification states that the limits of the table limits must match (https://www.w3.org/TR/wasm-core-2/valid/types.html#tables), and the minimum limit is updated when growing a table (https://www.w3.org/TR/wasm-core-2/exec/modules.html#grow-table).

In wasmtime and Javascript in Firefox/Chrome/V8 the instantiation is successful in the corresponding code:

Javascript (works)

const module = await WebAssembly.compileStreaming(fetch('test1.wasm'));
const table = new WebAssembly.Table({ initial: 0, element: "anyfunc" });
table.grow(100);
const instance = await WebAssembly.instantiate(module, {env: {table: table}});
console.log(instance);

Wasmtime 1.0.0 (works)

use wasmtime::{Engine, Instance, Module, Store, Table, TableType, Val, ValType};

fn wasmtime() -> anyhow::Result<()> {
    let engine = Engine::default();
    let mut store = Store::new(&engine, ());
    let module = Module::new(&engine, WAT)?;
    let ty = TableType::new(ValType::FuncRef, 0, None);
    let table = Table::new(&mut store, ty, Val::FuncRef(None))?;
    table.grow(&mut store, 100, Val::FuncRef(None))?;
    let _instance = Instance::new(&mut store, &module, &[table.into()])?;
    Ok(())
}

Wasmer 2.3.0 (fails)

use wasmer::{Store, Module, Instance, Table, TableType, Type, Value, imports};

fn wasmer2() -> anyhow::Result<()> {
    let store = Store::default();
    let module = Module::new(&store, WAT)?;
    let ty = TableType::new(Type::FuncRef, 0, None);
    let table = Table::new(&store, ty, Value::FuncRef(None))?;
    table.grow(100, Value::FuncRef(None))?;
    let imports = imports!{"env" => {"table" => table}};
    let _instance = Instance::new(&module, &imports)?;
    Ok(())
}
@benozol benozol added the bug Something isn't working label Sep 23, 2022
@syrusakbary
Copy link
Member

Thanks for the report!

@syrusakbary
Copy link
Member

The issue is that Wasmer currently changes the minimum when calling table.grow (it shouldn't be the case) (if we do table.grow(10) the minimum goes from x to x+10 at the moment).

Once we solve that, the issue shall be solved.

Thanks again for the report!

@syrusakbary syrusakbary added 🥇 good first issue Good for newcomers priority-high High priority issue labels Sep 29, 2022
@syrusakbary syrusakbary added this to the v3.0 milestone Oct 5, 2022
fschutt added a commit that referenced this issue Oct 10, 2022
@fschutt fschutt mentioned this issue Oct 10, 2022
ptitSeb pushed a commit that referenced this issue Oct 20, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working 🥇 good first issue Good for newcomers priority-high High priority issue
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants