Repository navigation
Fix LoongArch64 stack unwinding and re-enable tests - #281
Conversation
- Add ra (link register) to loongarch64 Context structure - Update switchContext to save/restore ra register - Set ra to @returnaddress() in setupContext (matches aarch64) - Create patch for Zig 0.15.x stdlib fp_offset bug - Add CI script to apply stdlib patches automatically - Re-enable loongarch64 testing in main CI workflow - Remove loongarch64 skip from coroutine stack trace test The root cause was that LoongArch64 follows RISC-V calling convention where fp points to top of saved register area (saved fp at fp[-2]), but Zig 0.15.x stdlib was treating it like x86_64/aarch64 (saved fp at fp[0]). This caused infinite loops in stack unwinding. Fixed in Zig 0.16+, patch only needed for 0.15.x.
📝 WalkthroughWalkthroughThe pull request introduces LoongArch64 support by adding a patch mechanism for Zig 0.15.x stdlib, applying a frame pointer offset fix to debug.zig, enabling LoongArch64 in CI workflows, and removing test skips that prevented LoongArch64 test execution. Changes
Estimated code review effort🎯 2 (Simple) | ⏱️ ~12 minutes Possibly related PRs
Poem
🚥 Pre-merge checks | ✅ 3✅ Passed checks (3 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ Finishing touches
🧪 Generate unit tests (beta)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
There was a problem hiding this comment.
Actionable comments posted: 2
🤖 Fix all issues with AI agents
In @.github/workflows/test-coro.yml:
- Around line 48-50: The "Apply Zig stdlib patches" step currently runs
"./patches/apply-zig-patches.sh" but on Windows runners the default shell is
PowerShell and may not execute .sh correctly; update that step to explicitly run
under bash (e.g., set shell: bash -l {0} or invoke bash -lc) or change the run
command to "bash ./patches/apply-zig-patches.sh" so the script always executes
with Bash on the Windows matrix entry.
In @.github/workflows/test.yml:
- Around line 46-48: The "Apply Zig stdlib patches" step (run:
./patches/apply-zig-patches.sh) must explicitly set the shell to bash to avoid
PowerShell on Windows; add a "shell: bash" key to that step in both occurrences
(the step at lines ~46-48 and the similar step at ~84-86) so the .sh script is
executed with bash in each job.
🧹 Nitpick comments (1)
patches/apply-zig-patches.sh (1)
9-12: Prefer structured parsing ofzig envoutput.
zig envreturns JSON; grep/sed parsing is fragile to formatting changes. Consider parsing JSON directly to avoid subtle breakage.
As per learnings, you’re already usingzig envto obtain the stdlib path; this just makes it more reliable.Suggested change
-ZIG_LIB_DIR=$(zig env | grep 'lib_dir' | sed 's/.*"\([^"]*\)".*/\1/') +ZIG_LIB_DIR=$(zig env | python -c 'import json,sys; print(json.load(sys.stdin)["lib_dir"])')Based on learnings: "Use
zig envto get the path to the Zig standard library when needed for reference".
| - name: Apply Zig stdlib patches | ||
| run: ./patches/apply-zig-patches.sh | ||
|
|
There was a problem hiding this comment.
Ensure the patch step runs under bash on Windows.
Windows runners default to PowerShell, which won’t execute .sh reliably. Explicitly set the shell (or invoke bash) to prevent CI failures on the Windows matrix entry.
Suggested change
- - name: Apply Zig stdlib patches
- run: ./patches/apply-zig-patches.sh
+ - name: Apply Zig stdlib patches
+ shell: bash
+ run: ./patches/apply-zig-patches.sh📝 Committable suggestion
‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.
| - name: Apply Zig stdlib patches | |
| run: ./patches/apply-zig-patches.sh | |
| - name: Apply Zig stdlib patches | |
| shell: bash | |
| run: ./patches/apply-zig-patches.sh |
🤖 Prompt for AI Agents
In @.github/workflows/test-coro.yml around lines 48 - 50, The "Apply Zig stdlib
patches" step currently runs "./patches/apply-zig-patches.sh" but on Windows
runners the default shell is PowerShell and may not execute .sh correctly;
update that step to explicitly run under bash (e.g., set shell: bash -l {0} or
invoke bash -lc) or change the run command to "bash
./patches/apply-zig-patches.sh" so the script always executes with Bash on the
Windows matrix entry.
| - name: Apply Zig stdlib patches | ||
| run: ./patches/apply-zig-patches.sh | ||
|
|
There was a problem hiding this comment.
Run the patch step under bash in both jobs.
Same issue here: Windows defaults to PowerShell, so .sh execution is risky. Set the shell explicitly in both places.
Suggested change
- - name: Apply Zig stdlib patches
- run: ./patches/apply-zig-patches.sh
+ - name: Apply Zig stdlib patches
+ shell: bash
+ run: ./patches/apply-zig-patches.sh
...
- - name: Apply Zig stdlib patches
- run: ./patches/apply-zig-patches.sh
+ - name: Apply Zig stdlib patches
+ shell: bash
+ run: ./patches/apply-zig-patches.shAlso applies to: 84-86
🤖 Prompt for AI Agents
In @.github/workflows/test.yml around lines 46 - 48, The "Apply Zig stdlib
patches" step (run: ./patches/apply-zig-patches.sh) must explicitly set the
shell to bash to avoid PowerShell on Windows; add a "shell: bash" key to that
step in both occurrences (the step at lines ~46-48 and the similar step at
~84-86) so the .sh script is executed with bash in each job.
Summary
Fixes stack unwinding on LoongArch64 and re-enables previously disabled tests.
Changes
ra(link register) field to loongarch64 Context structureRoot Cause
LoongArch64 follows the RISC-V calling convention where the frame pointer points to the top of the saved register area, not directly to where the previous frame pointer is saved:
fp[-2](offset -16)fp[-1](offset -8)Zig 0.15.x stdlib was treating it like x86_64/aarch64 (saved fp at
fp[0]), causing the stack unwinder to read garbage data and create cycles in the frame pointer chain. This resulted in:Solution
The patch updates Zig 0.15.x stdlib to use the correct fp_offset for LoongArch64. This is already fixed in Zig 0.16+, so the patch only applies to 0.15.x installations.
Testing
Verified with test program that stack traces now work correctly on loongarch64 via QEMU.