Segfault or internal error: stg_ap_v_ret in ghc 9.6.2
## Summary
I have a program that crashes with a segfault when run with sanity checks `-Da -DS` or with `internal error: stg_ap_v_ret` when run without them.
Its rather large, and I don't know how to reduce it (mostly because I don't understand why it happens in the first place). Its the start of a table based ecs implementation.
The core pieces are: [runQuery](https://github.com/1Jajen1/Brokkr/blob/main/brokkr-hecs/hecs-core/src/Hecs/Query/Internal.hs#L87) (and a copy of it which I experimented with: `runQuery2#`). Its called from [progress](https://github.com/1Jajen1/Brokkr/blob/main/brokkr-hecs/hecs-core/src/Hecs/World/Internal.hs#L488) and the system and query data is created and set at [system](https://github.com/1Jajen1/Brokkr/blob/main/brokkr-hecs/hecs-core/src/Hecs/World/Internal.hs#L478) and [query](https://github.com/1Jajen1/Brokkr/blob/main/brokkr-hecs/hecs-core/src/Hecs/World/Internal.hs#L400). Experimentally I confirmed that the first `runQuery` with the `systemQuery` argument runs and calls the second `runQuery`, which fails. `runQuery` works fine on its own (even nested), but fails with the setup in `progress`.
Gdb traced the segfault all the way to `LOOKS_LIKE_CLOSURE_PTR`.
<details><summary>Full output before it crashes:</summary>
```
stg_ap_0_ret... 0x42005e16ba: FUN/2(0x469648, 0x42005e16e8, 0x42005e16d8, 0x42005e1786)
stg_ap_v_ret... 0x42005e16d8: MUT_VAR_CLEAN(var=0x42005e17b8)
Thread 1 "Example" received signal SIGSEGV, Segmentation fault.
0x0000000000bf4c6e in LOOKS_LIKE_CLOSURE_PTR (p=0x1839000000420058)
at rts/include/rts/storage/ClosureMacros.h:269
Downloading source file /builds/ghc/ghc/rts/include/rts/storage/ClosureMacros.h
269 rts/include/rts/storage/ClosureMacros.h: Directory not empty.
(gdb) bt
#0 0x0000000000bf4c6e in LOOKS_LIKE_CLOSURE_PTR (p=0x1839000000420058)
at rts/include/rts/storage/ClosureMacros.h:269
#1 0x0000000000bf4f3b in checkClosureShallow (p=0x183900000042005e)
at rts/sm/Sanity.c:106
#2 0x0000000000bf4e77 in checkSmallBitmap (payload=0x42005fff40, bitmap=0, size=4)
at rts/sm/Sanity.c:75
#3 0x0000000000bf4fe7 in checkStackFrame (c=0x42005fff38) at rts/sm/Sanity.c:133
#4 0x0000000000c09ee9 in stg_ap_v_info ()
#5 0x0000000000000000 in ?? ()
```
</details>
This is as far as I got. I don't really know how to continue from here.
In a previous version I had mutable smallarrays in the mutvars, which I then froze before use. This segfaulted the second anything with the smallarrays was done (getting the size, reading any elements, or freezing). I then changed to immutable smallarrays later, but as you can see it still crashes.
## Steps to reproduce
Use [Brokkr/brokkr-hecs:git](https://github.com/1Jajen1/Brokkr/blob/main/brokkr-hecs/), checkout `minimal-hecs` and run `cabal run hecs:example -- +RTS -Da -DS`. I pushed the last state of my local testing before I gave up.
## Expected behavior
Pls don't segfault :)
## Environment
* GHC version used: ghc 9.6.2
issue