CharlieTap / chasm

A WebAssembly runtime built on Kotlin Multiplatform
Apache License 2.0
91 stars 3 forks source link

StackOverflowError in MemoryInitExecutorImpl and TableInitExecutorImpl #8

Closed illarionov closed 6 months ago

illarionov commented 6 months ago

I attempted to use this library to execute WebAssembly code but encountered StackOverflowError exception in MemoryInitExecutorImpl and in TableInitExecutorImpl functions. Here are the stack traces:

java.lang.StackOverflowError
    at io.github.charlietap.chasm.executor.invoker.instruction.memory.store.I32StoreSizedExecutorImplKt$I32StoreSizedExecutorImpl$2.invoke-Zyo9ksc(I32StoreSizedExecutorImpl.kt)
    at io.github.charlietap.chasm.executor.invoker.instruction.memory.store.I32StoreSizedExecutorImplKt$I32StoreSizedExecutorImpl$2.invoke(I32StoreSizedExecutorImpl.kt:38)
    at io.github.charlietap.chasm.executor.invoker.instruction.memory.MemoryInitExecutorImplKt$MemoryInitExecutorImpl$1.invoke-MJidatI(MemoryInitExecutorImpl.kt:97)
    at io.github.charlietap.chasm.executor.invoker.instruction.memory.MemoryInitExecutorImplKt$MemoryInitExecutorImpl$1.invoke(MemoryInitExecutorImpl.kt:33)
    at io.github.charlietap.chasm.executor.invoker.instruction.memory.MemoryInitExecutorImplKt.MemoryInitExecutorImpl-O0yBkHM(MemoryInitExecutorImpl.kt:64)
    at io.github.charlietap.chasm.executor.invoker.instruction.memory.MemoryInitExecutorImplKt.MemoryInitExecutorImpl-O0yBkHM(MemoryInitExecutorImpl.kt:70)
java.lang.StackOverflowError
    at io.github.charlietap.chasm.executor.runtime.Stack.peekFrameOrNull(Stack.kt:51)
    at io.github.charlietap.chasm.executor.invoker.instruction.table.TableInitExecutorImplKt$TableInitExecutorImpl$1.invoke-gl31Vf4(TableInitExecutorImpl.kt:86)
    at io.github.charlietap.chasm.executor.invoker.instruction.table.TableInitExecutorImplKt$TableInitExecutorImpl$1.invoke(TableInitExecutorImpl.kt:31)
    at io.github.charlietap.chasm.executor.invoker.instruction.table.TableInitExecutorImplKt.TableInitExecutorImpl(TableInitExecutorImpl.kt:68)
    at io.github.charlietap.chasm.executor.invoker.instruction.table.TableInitExecutorImplKt.TableInitExecutorImpl(TableInitExecutorImpl.kt:74)
    at io.github.charlietap.chasm.executor.invoker.instruction.table.TableInitExecutorImplKt.TableInitExecutorImpl(TableInitExecutorImpl.kt:74)

In version 0.2.2 of the library, I also faced StackOverflowError in MemoryCopyExecutorImpl and MemoryFillExecutorImpl. However, I see that these were fixed in version 0.6.0, and I no longer see those errors.

As a workaround, I made a MemoryInitExecutorImpl and TableInitExecutorImpl tailrec: diff. This fixes the error, but I think there should be a more elegant solution.

This is a repository where I'm experimenting with running WebAssembly code: https://github.com/illarionov/wasm-sqlite-open-helper/tree/chasmDevel. If you require it, I can later attempt to create a smaller reproducible example.

CharlieTap commented 6 months ago

Hey @illarionov, Thank you for looking into this!

These are actually on my list of things to do as all of the recursive executors ended up causing issues, mostly because they are not tailrec but to be honest they're really inefficient. For example memory_init is just about copying bytes from one byte array to another and that can be optimised down to a single copy instruction.

As I know this is causing you an issue I'll add to the top of the list, expect a release in the next couple of days 😃

CharlieTap commented 6 months ago

@illarionov

I've just released a new version, do let me know if it addresses your issues and feel free to close the issue if so.

https://github.com/CharlieTap/chasm/releases/tag/0.6.1

illarionov commented 6 months ago

@CharlieTap everything works now, thanks for the quick fix!