2

The following Rust compiled to WASM, based on the Wasmer exports_global.rs WASM example, produces incorrect WASM (with cargo build --target=wasm32-wasi):

// (module
//     (global $one (export "one") f32 (f32.const 1))
//     (global $some (export "some") (mut f32) (f32.const 0))

//     (func (export "get_one") (result f32) (global.get $one))
//     (func (export "get_some") (result f32) (global.get $some))

//     (func (export "set_some") (param f32) (global.set $some (local.get 0))))

#[no_mangle]
pub static one: f32 = 1.0;

#[no_mangle]
pub static mut some: f32 = 0.0;

#[no_mangle]
pub extern "C" fn get_one() -> f32 {
    one
}

#[no_mangle]
pub extern "C" fn get_some() -> f32 {
    unsafe { some }
}

#[no_mangle]
pub extern "C" fn set_some(value: f32) {
    unsafe {
        some = value;
    }
}

Inspecting the compiled module, I get:

 wasmer inspect .\target\wasm32-wasi\debug\rust_wasm.wasm
Type: wasm
Size: 2.1 MB
Imports:
  Functions:
  Memories:
  Tables:
  Globals:
Exports:
  Functions:
    "get_one": [] -> [F32]
    "get_some": [] -> [F32]
    "set_some": [F32] -> []
  Memories:
    "memory": not shared (17 pages..)
  Tables:
  Globals:
    "some": I32 (constant)
    "one": I32 (constant)

Specifically, the Globals are wrong. They should both be F32 and some should be mutable. Why is this happening?

junglie85
  • 1,243
  • 10
  • 30
  • Sure that's the **value** of those variables and not the **initialization**? – Finomnis Feb 06 '23 at 14:37
  • AFAIK that's telling me the globals are an `I32` type but according to my declaration they should be `F32`. The example has an assert on the type and that fails because `I32 != F32`. – junglie85 Feb 06 '23 at 16:44
  • I can't find any proof for that right now, but it seems that statics in WASM are exported by-address, not by-value, and the address in WASM linear memory is `i32`. – Cerberus Feb 07 '23 at 04:55
  • What I don't understand is why the commented out WebAssembly Text, also in the link, and associated program runs with directly accessing globals as their F32 type, but in Rust compiled to WASM, something different happens. – junglie85 Feb 08 '23 at 09:07

0 Answers0