These are some common issues you may run into while using SwiftWasm.

If you are having trouble that is not listed here, try searching for it in the SwiftWasm issue tracker. If you are still having trouble, please file an issue or contact us at the community Discord server.

RuntimeError: memory access out of bounds

If you encounter this error, there are 3 possible causes:

1. You are trying to access invalid memory in your code

In this case, you need to make sure which memory operations are invalid in your code by UnsafePointer or C code.

2. You missed program initialization defined in WASI Application ABI.

If your application is used as a library, you need to follow WASI reactor ABI.

Please make sure that you followed it by reviewing the Exporting function guide

3. Stack overflow is occurring.

If you are using --stack-first linker option (carton uses it by default), you can face RuntimeError: memory access out of bounds error due to stack overflow.

You have two options to solve this issue:

  1. Avoid recursive calls if possible.

  2. Extend the stack size by linker option -z stack-size=<size>. The default stack size is 64KB

    swift build --triple wasm32-unknown-wasi -Xlinker -z -Xlinker stack-size=131072
  3. Identify which function consumes a lof of stack space by some tools like wasm-stack-consumer

See also: LLVM Bugzilla – wasm32: Allow placing the stack before global data