wasmer: Wasmer can't run Doom

I understand that this is a somewhat nebulous, perhaps frivolous issue. Here it is anyway.

Wasmer fails to run a Doom Wasm port. This article by Cornelius Diekmann documents the process of porting Doom to WebAssembly and results in this demo. In my own explorations of the subject, I found that the Doom Wasm module runs as expected in the browser (tested on Chrome, Safari, Firefox) and Wasmtime, but fails using Wasmer.

The only clue as to what is going on is the following trap message on invocation of the main export function.

out of bounds memory access

Wasmer version: 4.2.0

Steps to reproduce

Descriptions for recreating the issue can be found in my write up. Alternatively, the module can be initialized with relatively few imports. I’ve attached the Wasm module in question.

doom.wasm.zip

Expected behaviour

Invocation of the main export function should result in the following expected logs.

Hello, World, from JS Console! Answer=42 (101010 in binary) Hello, world from rust! 🦀🦀🦀 (println! working) Starting D_DoomMain Triggering a printf Doom’s screen is 320x200 mallocing 12 bytes at 7078176

Actual behavior

Invocation of the main export function results in the following logs before the function invocation fails.

Hello, World, from JS Console! Answer=42 (101010 in binary) Hello, world from rust! 🦀🦀🦀 (println! working)

About this issue

  • Original URL
  • State: closed
  • Created 3 years ago
  • Comments: 17 (17 by maintainers)

Most upvoted comments

Here’s a minimal rust repro:

fn main() {
    // (import "env" "memory" (memory 17))
    let code = r#"
(module
  (type (func))
  (import "env" "func" (func $imported_func (type 0)))
  (func $other_func (type 0))
  (func $func (type 0)
    i32.const 1
    call_indirect (type 0)
  )
  (table 3 3 funcref)
  (export "func" (func $func))
  (elem (i32.const 1) func $imported_func $other_func)
) "#;

    let engine = wasmer::Engine::default();
    let module = wasmer::Module::new(&engine, code).unwrap();
    let mut store = wasmer::Store::new(engine.clone());

    let ty = wasmer::FunctionType::new(vec![], vec![]);
    let func_dynamic = wasmer::Function::new(&mut store, ty, |_args| {
        dbg!("hello callback dynamic");
        Ok(vec![])
    });

    let func_typed = wasmer::Function::new_typed(&mut store, || {
        dbg!("hello callback typed");
    });

    let imports = wasmer::imports! {
        "env" => {
            "func" => func_dynamic,
            // "memory" => mem,
        }
    };

    let instance = wasmer::Instance::new(&mut store, &module, &imports).unwrap();

    let func = instance.exports.get_function("func").unwrap();
    let out = func.call(&mut store, &[]).unwrap();
    dbg!(out);
}

The underlying issue is in Function::new, which is also used from C as typed functions aren’t possible via the WASM C API. Functions created through Function::new don’t get a func_ptr which, at runtime, causes a call instruction to the zero address, hence the error. Under the LLVM backend, this turns into an IndirectCallToNull trap because the LLVM backend checks the func_ptr. The cranelift and singlepass backends don’t, and instead get a sigsegv which is translated into an out of bounds memory access by the VM layer.

I’m still investigating how this can be fixed, since I have little familiarity with the compiler backends; I took this issue out of my love for computer games 😄

@ashtonmeuser Thanks for reporting this! This may in fact help us with something we’re working on right now, so I’ll investigate the issue.