wasm3 / wasm3-rs

Rust wrapper for Wasm3, the fastest WebAssembly interpreter
MIT License
160 stars 44 forks source link

Crash on f64 closure args #9

Closed ericflo closed 4 years ago

ericflo commented 4 years ago

The following code crashes at the module::link_closure step:

use wasm3::{Module, CallContext};

type AudioPlayClosureType = dyn for<'cc> FnMut(&'cc CallContext, (i32, f64, f64, f64, f64)) -> () + 'static;

pub fn link_closures(module: &mut Module) -> Result<()> {
    let tmp = Box::new(make_audio_play());
    match module.link_closure::<(i32, f64, f64, f64, f64), (), Box<AudioPlayClosureType>>("env", "Engine_Audio_Play", tmp) {
        Ok(_) => {},
        Err(err) => info!("Could not link closure: {:?}", err),
    };
    Ok(())
}

fn make_audio_play() -> impl FnMut(&CallContext, (i32, f64, f64, f64, f64)) -> () {
    move |_ctx: &CallContext, args: (i32, f64, f64, f64, f64)| {
        let id = args.0;
        let px = args.1;
        let py = args.2;
        let pz = args.3;
        let volume = args.4;
        info!("Called play on id {:?} at pos ({:?},{:?},{:?}) with volume {:?}", id, px, py, pz, volume);
    }
}

The way I'm running this (compiled as a dll, loaded by a game written in Unity) it's hard to get a crash dump unfortunately, but I've tried various things and whenever there's f64s in the args it crashes, but with just i32s it runs fine.

Veykril commented 4 years ago

The crash occurs due to an out of bounds check here https://github.com/Veykril/wasm3-rs/blob/cb7e54aa4be74afb0beb1792aa7f66138cf10fc4/src/function.rs#L113. Its not specific to f64 but to having more than 3 arguments. 1291c06 should hopefully fix it, let me know if it does.

ericflo commented 4 years ago

That worked, thanks! I was way off on the cause of the bug, apologies for that.

Veykril commented 4 years ago

No problem .It fortunately was a panic in rust and not a crash on the cpp side so by recreating your snippet it was quick to find.