Stream: beginners

Topic: wasm4 out of bounds


view this post on Zulip Kiryl Dziamura (Jan 24 2024 at 11:43):

I've been playing with wasm4 platform but ran into out of bounds error

here's an ugly prototype of a maze game. level 3 crashes. if to have a bigger field, let's say 20x20 (currently it's 6x6), the game crashes trying to build even the first level.

I thought it was something with recursion/stack overflow during random traversal. but it should be a pretty straightforward TCO case. also, there are no problems for the roc-cli platform, so it’s probably specific to the allocation mechanism of the wasm4 platform

https://gist.github.com/wontem/50bc86494a59fd7d7fffacc5dabb6e5d

error log

view this post on Zulip Brendan Hansknecht (Jan 24 2024 at 16:16):

looks like it may be a bug with the allocator...not fully sure yet

view this post on Zulip Brendan Hansknecht (Jan 24 2024 at 16:30):

The error currently doesn't make sense to me. Looks to be failing an assert in zig but we have a conditional right before the assert that should make it impossible. Not yet sure what is going on. Will have to debug more later.

view this post on Zulip Kiryl Dziamura (Jan 25 2024 at 08:08):

Skimmed the allocator. Never read allocators or zig and must say, the code is very simple to follow! From what I see, it’s really hardly possible the problem happens because of the allocation for roc.

Looks more like it fails trying to allocate memory on the stack for zig, it just happens to fail during isSet. Will check it later today. Or maybe the trace log is not accurate

view this post on Zulip Kiryl Dziamura (Jan 25 2024 at 10:48):

Yeah, I decreased mem_size and increased stack_size by 10000 and it helped. Now, the question is why the stack overflow is happening. Seems to be a lack of TCO then. How can I check it? Going to check llvm ir, maybe there is a hint.

view this post on Zulip Brendan Hansknecht (Jan 25 2024 at 15:29):

Interesting. That makes sense for the random failure location.

view this post on Zulip Brendan Hansknecht (Jan 25 2024 at 15:33):

could be hitting this compiler bug: https://github.com/roc-lang/roc/issues/6213

view this post on Zulip Brendan Hansknecht (Jan 25 2024 at 15:34):

We need to cleanup our ir and ensure all stack allocas happen outside of looping code. Otherwise, as the loops run, it can still accumulate stack space.

view this post on Zulip Kiryl Dziamura (Jan 25 2024 at 16:56):

I used Task.loop as a workaround. Works well enough

view this post on Zulip Brendan Hansknecht (Jan 25 2024 at 17:03):

Glad you found a workaround


Last updated: Jul 06 2025 at 12:14 UTC