wasm-micro-runtime icon indicating copy to clipboard operation
wasm-micro-runtime copied to clipboard

unable to load module with undeclared function reference

Open peter-jerry-ye opened this issue 1 year ago • 1 comments
trafficstars

The following code may fail :

(module
 (type (func))
 (type (sub (struct (field (ref 0)))))
 (table funcref (elem $closure))
 (func $create (result (ref 1)) (struct.new 1 (ref.func $closure)))
 (func $closure (type 0)))
  • if converted to wasm with wasm-tools (wasm-tools parse main.wat -o main.wasm) or binaryen (wasm-as main.wat -all -o main.wasm) : iwasm main.wasm will fail with WASM module load failed: undeclared function reference
  • if converted to wasm with reference implementation : iwasm will load the module without issue

It should be noticed though that the wasm generated by the wasm-tools or binaryen still passes the validation of the reference implemenetation.

iwasm is built with cmake -DWAMR_BUILD_GC=1 .. on Mac with the latest commit b9db23b9

peter-jerry-ye avatar Feb 20 '24 05:02 peter-jerry-ye

The only difference between the generated codes (apart from the custom sections) is the section 9 (element section)

wasm-tools compiles to: 01 00 41 00 0b 01 01

reference implementation compiles to: 01 06 00 41 00 0b 70 01 d2 01 0b

peter-jerry-ye avatar Feb 20 '24 10:02 peter-jerry-ye

@peter-jerry-ye thanks for reporting the issue, this is because that when GC is enabled, the type of table segment can be (ref func) now besides with funcref, while the original code only checks the latter. I submitted PR https://github.com/bytecodealliance/wasm-micro-runtime/pull/3181 to fix it, please try again.

wenyongh avatar Feb 26 '24 03:02 wenyongh

@wenyongh It solves the issue. Thanks.

peter-jerry-ye avatar Feb 26 '24 05:02 peter-jerry-ye