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

Does wasmer guaranties execution of instruction in atomic way in case of trap? #4596

Open
grishasobol opened this issue Apr 23, 2024 · 1 comment
Labels
🕵️ needs investigation The issue/PR needs further investigation priority-high High priority issue ❓ question I've a question!

Comments

@grishasobol
Copy link
Contributor

Summary

Hi wasmer team! I would like to ask you, whether wasmer guaranties, that store is reverted back to the state right before an instruction which cause a trap during execution?

Additional details

To clarify the question, let's consider some examples.

  1. Here we have OutOfBounds error. During execution a trap is appearing and signal handler stops code execution without any store recovering. So, the question is whether wasmer guaranties, that after execution store will be in state exactly before i32.store execution?
#[test]
fn test1() {
    let wat = r#"
        (module
            (memory (export "memory") 1)
            (func (export "main")
                i32.const 2
                global.set $a
                i32.const 0x10001
                i32.const 42
                i32.store
            )
            (global $a (export "a") (mut i32) (i32.const 1))
        )
    "#;
    let mut store = wasmer::Store::default();
    let module = wasmer::Module::new(&store, wat).unwrap();
    let imported_objects = wasmer::imports! {};
    let instance = wasmer::Instance::new(&mut store, &module, &imported_objects).unwrap();

    let res = instance
        .exports
        .get_function("main")
        .unwrap()
        .call(&mut store, &[]);
    assert!(res.is_err());

    let res = instance.exports.get_global("a").unwrap().get(&mut store);
    assert_eq!(res, wasmer::Value::I32(2));

    let mut buf = [0u8; 0x10000];
    instance
        .exports
        .get_memory("memory")
        .unwrap()
        .view(&mut store)
        .read(0, buf.as_mut_slice())
        .unwrap();
    assert_eq!(buf, [0u8; 0x10000]);
}
  1. Here we have StackOverflow error. During execution a trap is appearing and signal handler stops code execution without any store recovering. So, the question is whether wasmer guaranties, that after execution store will be the same state as it was before execution of instruction which causes the trap?
#[test]
fn test2() {
    let wat = r#"
        (module
            (func (param i32)
                local.get 0
                global.set $a
                local.get 0
                i32.const 1
                i32.add
                call 0
            )
            (func (export "main")
                i32.const 0
                call 0
            )
            (global $a (export "a") (mut i32) (i32.const 1))
        )
    "#;
    let mut store = wasmer::Store::default();
    let module = wasmer::Module::new(&store, wat).unwrap();
    let imported_objects = wasmer::imports! {};
    let instance = wasmer::Instance::new(&mut store, &module, &imported_objects).unwrap();

    let res = instance
        .exports
        .get_function("main")
        .unwrap()
        .call(&mut store, &[]);
    assert!(res.is_err());

    let res = instance.exports.get_global("a").unwrap().get(&mut store);
    assert_eq!(res, wasmer::Value::I32(65439));
}
@grishasobol grishasobol added the ❓ question I've a question! label Apr 23, 2024
@syrusakbary syrusakbary added 🕵️ needs investigation The issue/PR needs further investigation priority-high High priority issue labels Apr 30, 2024
@syrusakbary
Copy link
Member

In general, the store shall be able to be reused. We need to investigate more from the cases you commented, as if thats not happening, that's not the desired behavior.
Thanks for creating the issue @grishasobol

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
🕵️ needs investigation The issue/PR needs further investigation priority-high High priority issue ❓ question I've a question!
Projects
None yet
Development

No branches or pull requests

2 participants