Skip to content

Fix LoongArch64 stack unwinding and re-enable tests - #281

Merged
lalinsky merged 1 commit into
mainfrom
fix-loongarch64-unwinding
Feb 5, 2026
Merged

lalinsky merged 1 commit into
mainfrom
fix-loongarch64-unwinding

Conversation

@lalinsky

@lalinsky lalinsky commented Feb 5, 2026

Copy link
Copy Markdown
Owner

Summary

Fixes stack unwinding on LoongArch64 and re-enables previously disabled tests.

Changes

  • Coroutine context: Add ra (link register) field to loongarch64 Context structure
  • Context switching: Update switchContext to save/restore ra register at correct offset
  • Context setup: Set ra to @returnaddress() in setupContext (matching aarch64 behavior)
  • Zig stdlib patch: Create patch for Zig 0.15.x fp_offset bug affecting LoongArch64
  • CI automation: Add script to automatically apply stdlib patches during CI
  • Re-enable tests: Re-enable loongarch64 testing in main CI workflow
  • Remove skip: Remove loongarch64 skip from coroutine stack trace test

Root 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:

  • Saved frame pointer at fp[-2] (offset -16)
  • Saved return address at 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:

  • Infinite loops during stack trace collection
  • Tests hanging in loop.deinit() and panic handlers
  • Empty or incomplete stack traces

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.

- 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.
@coderabbitai

coderabbitai Bot commented Feb 5, 2026 •

Copy link
Copy Markdown
Contributor
📝 Walkthrough

Walkthrough

The 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

Cohort / File(s) Summary
CI Workflow Updates
.github/workflows/test-coro.yml, .github/workflows/test.yml
Added "Apply Zig stdlib patches" step to run the patching script after Zig setup. Enabled LoongArch64 matrix entry in test.yml by uncommenting.
Patch Infrastructure
patches/apply-zig-patches.sh, patches/zig-0.15-loongarch64-fp-offset.patch
New shell script that applies LoongArch64 frame pointer offset patches to Zig 0.15.x stdlib, checking if already applied. Accompanying patch file modifies debug.zig to treat LoongArch frame pointer offsets identically to RISCV.
Test Adjustment
src/coro/coroutines.zig
Removed loongarch64 runtime skip from the "Coroutine: stack trace" test, enabling the test to run on all architectures including loongarch64.

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~12 minutes

Possibly related PRs

Poem

🏗️ A long march through frames now clear,
Patches stitched without a fear,
LoongArch tests run bright and dear,
Zig's debug fixed for all to cheer! 🎉

🚥 Pre-merge checks | ✅ 3
✅ Passed checks (3 passed)
Check name Status Explanation
Title check ✅ Passed Title accurately summarizes the main objective: fixing LoongArch64 stack unwinding and re-enabling tests.
Description check ✅ Passed Description clearly relates to the changeset, explaining the root cause, solution, and testing approach for the LoongArch64 fixes.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing touches
  • 📝 Generate docstrings
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Post copyable unit tests in a comment
  • Commit unit tests in branch fix-loongarch64-unwinding

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.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 of zig env output.

zig env returns JSON; grep/sed parsing is fragile to formatting changes. Consider parsing JSON directly to avoid subtle breakage.
As per learnings, you’re already using zig env to 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 env to get the path to the Zig standard library when needed for reference".

Comment on lines +48 to +50
- name: Apply Zig stdlib patches
run: ./patches/apply-zig-patches.sh

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue | 🟠 Major

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.

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
🤖 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.

Comment on lines +46 to +48
- name: Apply Zig stdlib patches
run: ./patches/apply-zig-patches.sh

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue | 🟠 Major

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.sh

Also 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.

@lalinsky
lalinsky merged commit 225a506 into main Feb 5, 2026
38 checks passed
@lalinsky
lalinsky deleted the fix-loongarch64-unwinding branch February 5, 2026 09:34
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant