aduros / wasm4

Build retro games using WebAssembly for a fantasy console.
https://wasm4.org
ISC License
1.14k stars 168 forks source link

run-native: assemblyscript hello world fails with 'WASM error: only one memory per module is supported ()' #740

Open nycki93 opened 2 months ago

nycki93 commented 2 months ago

steps to reproduce:

w4 version 2.7.0 node version 20.1.0

w4 new --assemblyscript hello-world
cd hello-world
npm run build
w4 run-native build/cart.wasm

output:

WASM error: only one memory per module is supported ()

tested: the cart works in browser with w4 run cart.wasm.

tested: if the cart is bundled for windows with w4 bundle cart.wasm --windows cart.exe then the exe produces the same error as before.

tested: removing "importMemory": true in asconfig.json seems to fix the issue. is this flag needed?

yamt commented 1 month ago

fwiw, it seems wasm3-specifc as it doesn't happen with toywasm. https://github.com/aduros/wasm4/pull/749 i haven't tried wasmer.

easeway commented 3 weeks ago

This was caused by wasm3 bug, the wasm3 runtime requires exactly one memory section: https://github.com/wasm3/wasm3/blob/main/source/m3_parse.c#L460, however, it's legal to have wasm declare 0 memory sections. The fix will be >1 versus !=1 in the above code.

Or build wasm4 using wasmer as runtime (not tried).

peter-jerry-ye commented 2 weeks ago

It may be related to how the memory is imported, i.e.

(import "env" "memory" (memory 1))

vs

(memory (import "env" "memory") 1)

The former one will fail due to the fact that @easeway mentioned: there will be no memory in memory section

yamt commented 2 weeks ago
(import "env" "memory" (memory 1))

vs

(memory (import "env" "memory") 1)

these two are exactly same.

yamt commented 2 weeks ago

assemblyscript seems to produce an empty memory section, which makes wasm3 unhappy.

other toolchains usually just omits the memory section if a module has no memory defined. wasm3 wrongly assumes this behavior.