wabt icon indicating copy to clipboard operation
wabt copied to clipboard

wat2wasm: data section memory index

Open dy opened this issue 1 year ago • 5 comments

Take the following code:

(module
  (memory $n 1)
  (memory $m 1)
  (data (memory $m) (i32.const 0))
)

Compile.

The data section has bytes:

; data segment header 0
0000012: 02                                        ; segment flags
0000013: 41                                        ; i32.const
0000014: 00                                        ; i32 literal
0000015: 0b                                        ; end

As you can see from spec, flag == 2 expects memory index u32, but wabt omits it.

If you try to compile that code in JS, you'll see the error:

CompileError: WebAssembly.Module(): invalid memory index 65 for data section (having 2 memories) @+18

Adding memory index after segment flag solves the issue (watr has that fixed).

dy avatar Dec 06 '24 04:12 dy

Hmm, I can't speak to the web demo, but this seems to work fine in the actual main branch of the codebase. Are you able to bisect and identify when this was fixed in the code? We might just need to update the demo.

keithw avatar Dec 06 '24 04:12 keithw

It seems have been fixed for at least 2 years - I could not compile past 1.0.32. I guess demo only needs to update the libwabt.js.

dy avatar Dec 06 '24 20:12 dy

@keithw that's weird: I've rebuilt the libwabt.js (see PR), but it still has same issue! Whereas bin/wat2wasm compiles just fine. It makes me wonder if issue is in some js include or something like that.

dy avatar Dec 07 '24 02:12 dy

I assume you enabled the multi-memory feature (which is still off by default)?

sbc100 avatar Dec 07 '24 18:12 sbc100

Without that feature wabt throws error on validation stage.

dy avatar Dec 08 '24 02:12 dy