bytecodealliance / wasmtime

A fast and secure runtime for WebAssembly
https://wasmtime.dev/
Apache License 2.0
14.82k stars 1.24k forks source link

Allocation failed when running a multi threads wasm file #8793

Closed lazytiger closed 3 weeks ago

lazytiger commented 3 weeks ago

I want to test if I can use the parallel feature in the specs crate.

#[derive(Component)]
#[storage(VecStorage)]
pub struct Int32Comp {
    pub data: i32,
}

pub struct SumSystem;

impl<'a> System<'a> for SumSystem {
    type SystemData = (Write<'a, i32>, ReadStorage<'a, Int32Comp>);

    fn run(&mut self, (mut sum, i32comp): Self::SystemData) {
        for i32comp in (&i32comp).join() {
            *sum += i32comp.data;
        }
    }
}

#[no_mangle]
pub extern "system" fn add(i: i32, j: i32) -> i32 {
    let mut world = World::new();
    world.register::<Int32Comp>();
    for data in i..j {
        world.create_entity().with(Int32Comp { data }).build();
    }
    world.insert(0);
    let pool = ThreadPoolBuilder::new().build().unwrap();
    let mut dispatcher = DispatcherBuilder::new().with(SumSystem, "sum_system", &[]).with_pool(Arc::new(pool)).build();
    dispatcher.dispatch(&mut world);
    let sum = world.fetch::<i32>();
    *sum
}

I have tested max_wasm_stack=200x1024x1024, it fails the same.

I wonder if this is a bug or if I just missed some configuration.

bjorn3 commented 3 weeks ago

What is the max memory size set by the wasm module? Wasm shared memories are required to set some limit to their size. It may be the case that the linker defaulted to a too small size.

lazytiger commented 3 weeks ago

I use default for all the other config parameters. @bjorn3 How can I set max memory size, I don't find an API related to this purpose except max_wasm_stack

lazytiger commented 3 weeks ago

ImportType { module: "env", name: "memory", ty: Memory(MemoryType { ty: Memory { minimum: 17, maximum: Some(17), shared: true, memory64: false } }) }

The shared memory maximum is 17, how can I change the maximum? Thanks

lazytiger commented 3 weeks ago

Thanks for your help. I use the walrus crate to modify the wasm file, and it works.

bjorn3 commented 3 weeks ago

You can set the mac size by passing --max-memory to the linker as alternative.