DelphinusLab / zkWasm

Apache License 2.0
479 stars 95 forks source link

assemblyscript 'new Array' failed at setup - memory.fill seems broken #107

Closed nom4dv3 closed 1 year ago

nom4dv3 commented 1 year ago

I was trying to add read_bytes_from_i64 support in assemblyscript-compiled wasm, but observed that any new Array/ArrayBuffer/TypedArray would fail at the setup stage on zkwasm.

Minimum sample as code:

export function zkmain():void{
    new Array<u8>(0);
}

compiled with asc test.ts -t test.wat -o test.wasm (test.zip)

Looking into the wat, it seems has something to do with the memory.fill instruction.

I also tried --disable bulk-memory, no luck, can't get rid of memfill, and failed at setup as expected. so it seems there no workaround.

junyu0312 commented 1 year ago

I have tried

asc test.ts -O --noAssert -o demo.wasm --disable bulk-memory

, which emited a loop with many store instructions instead of memory.fill.

The version of asc I used is 0.27.0.

Could you try to compile your assemblyscript again?

nom4dv3 commented 1 year ago

ah right, turns out I didn't add -O together with --disable bulk-memory, causing memfill left in the wasm. Thx for point it out!

But the resulting wasm is still failed on setup. I found it's because new introduced a bunch of things, e.g. importing env.abort etc.

Even I replace the env.abort with an empty func, still no luck.

The only way I found to replace new is to use heap.alloc as it's unsafe operation. Still trying...

nom4dv3 commented 1 year ago

I wrote a Bytes class wrapping Uint8Array, providing a clean version of 'new' based on heap.alloc. And implement a 'read_bytes_from_u64' for as.

https://github.com/hyperoracle/zkwasm-as

xgaozoyoe commented 1 year ago

Cool

https://github.com/hyperoracle/zkwasm-as