fix(libSceFiber): unpoison the stack a contextless fiber abandons under ASan #20009

Closed
Kal0s-design wants to merge 1 commit from upstream-pr/20009 into main
Kal0s-design commented 2026-11-14 11:36:42 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by Kal0s-design on 2026-11-14T11:36:42Z.


What

fiber_contextless is red in the Sanitizers nightly since 2026-11-14 (run 43553066656, main 072e08e338; it passed on 11-13):

AddressSanitizer: CHECK failed: asan_thread.cpp:369 "((ptr[0] == kCurrentStackFrameMagic)) != (0)" (0x0, 0x0)
    #8 __asan_report_store8
    #9 sceFiberGetInfo core/libs/prx/libSceFiber/Export.cpp:633
    #10 ContextlessEntry core/libs/tests/Fiber.cpp:370
    #11 Aps5FiberMain_nid_no_patch core/libs/prx/libSceFiber/Export.cpp:397

The store is into localInfo, a local of sceFiberGetInfo itself. Its shadow byte is f1, a left redzone, left there by an earlier frame at the same address.

Why. A fiber without a context runs on the calling thread's stack, below the frame that ran it. When it returns to the thread or switches to another fiber, its frames are abandoned: no epilogue runs, so the redzones they poisoned stay in the shadow. GCC's instrumented prologue writes only the redzone bytes of the new frame and relies on the variable bytes being zero, so the next function at the same stack address stores into a local that the shadow still marks as a redzone. a6073c813a (#19385) gave sceFiberGetInfo its first locals, which is why the test turned red then; main today fails the same way as unknown-crash on the same store.

The change. CompletePendingSuspend unpoisons [savedStack, threadStack) when the suspended fiber has no context: that is exactly the range its frames occupied, from the stack pointer saved at the switch up to the thread's saved stack pointer. Compiled only under ASan (__SANITIZE_ADDRESS__ or ANYPS5_ENABLE_ASAN); the other builds get an empty function. Fibers with a context keep their frames, since they resume.

Tested

Linux, GCC 16, ASan + UBSan with the Sanitizers workflow's flags, main f0c9b11f91 and this head:

  • fiber_contextless: fails on main with the CHECK above (the same store, reported as unknown-crash since #19856); passes 3 of 3 with this change, through ctest.
  • fiber: passes.
  • TSan build (-DANYPS5_ENABLE_TSAN=ON): both tests pass, no report.

Windows 11, MinGW GCC 15, Release: fiber and fiber_contextless pass (the change compiles to nothing there).

check_conventions.py passes.

Checklist

  • Based on current main; no other open PR implements the same functions
  • One topic per PR; follow-ups go in a new PR
  • No comments in code except technical debt
  • Unimplemented paths throw (NotImplemented_nid_no_patch); silent stubs are listed under Silent stubs in their library's technical debt file
  • No notes, investigation .md files or images added to the repository (attach them to this PR)
  • New third-party code is a submodule built from source
  • Generic behaviour, not specific to one title
  • Depends on: none
  • Fixes: none
  • Unblocks: none
  • GPUs tested: n/a
  • AI-assisted: yes

🤖 Generated with Claude Code

<!-- greyfinch-sync:boykopovar/AnyPS5:issue:5310807915 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/20009). Originally posted by [Kal0s-design](https://github.com/Kal0s-design) on 2026-11-14T11:36:42Z. --- ### What `fiber_contextless` is red in the Sanitizers nightly since 2026-11-14 ([run 43553066656](https://github.com/boykopovar/AnyPS5/actions/runs/43553066656), `main` `072e08e338`; it passed on 11-13): ``` AddressSanitizer: CHECK failed: asan_thread.cpp:369 "((ptr[0] == kCurrentStackFrameMagic)) != (0)" (0x0, 0x0) #8 __asan_report_store8 #9 sceFiberGetInfo core/libs/prx/libSceFiber/Export.cpp:633 #10 ContextlessEntry core/libs/tests/Fiber.cpp:370 #11 Aps5FiberMain_nid_no_patch core/libs/prx/libSceFiber/Export.cpp:397 ``` The store is into `localInfo`, a local of `sceFiberGetInfo` itself. Its shadow byte is `f1`, a left redzone, left there by an earlier frame at the same address. **Why.** A fiber without a context runs on the calling thread's stack, below the frame that ran it. When it returns to the thread or switches to another fiber, its frames are abandoned: no epilogue runs, so the redzones they poisoned stay in the shadow. GCC's instrumented prologue writes only the redzone bytes of the new frame and relies on the variable bytes being zero, so the next function at the same stack address stores into a local that the shadow still marks as a redzone. `a6073c813a` (#19385) gave `sceFiberGetInfo` its first locals, which is why the test turned red then; `main` today fails the same way as `unknown-crash` on the same store. **The change.** `CompletePendingSuspend` unpoisons `[savedStack, threadStack)` when the suspended fiber has no context: that is exactly the range its frames occupied, from the stack pointer saved at the switch up to the thread's saved stack pointer. Compiled only under ASan (`__SANITIZE_ADDRESS__` or `ANYPS5_ENABLE_ASAN`); the other builds get an empty function. Fibers with a context keep their frames, since they resume. ### Tested Linux, GCC 16, ASan + UBSan with the Sanitizers workflow's flags, `main` `f0c9b11f91` and this head: - `fiber_contextless`: fails on `main` with the CHECK above (the same store, reported as `unknown-crash` since #19856); passes 3 of 3 with this change, through `ctest`. - `fiber`: passes. - TSan build (`-DANYPS5_ENABLE_TSAN=ON`): both tests pass, no report. Windows 11, MinGW GCC 15, Release: `fiber` and `fiber_contextless` pass (the change compiles to nothing there). `check_conventions.py` passes. ### Checklist - [x] Based on current `main`; no other open PR implements the same functions - [x] One topic per PR; follow-ups go in a new PR - [x] No comments in code except [technical debt](https://github.com/boykopovar/AnyPS5/blob/main/docs/dev/CONVENTIONS.md) - [x] Unimplemented paths throw (`NotImplemented_nid_no_patch`); silent stubs are listed under Silent stubs in their library's [technical debt](https://github.com/boykopovar/AnyPS5/tree/main/docs/dev/debt) file - [x] No notes, investigation `.md` files or images added to the repository (attach them to this PR) - [x] New third-party code is a submodule built from source - [x] Generic behaviour, not specific to one title - [x] Depends on: none - [x] Fixes: none - [x] Unblocks: none - [x] GPUs tested: n/a - [x] AI-assisted: yes 🤖 Generated with [Claude Code](https://claude.com/claude-code)

Pull request closed

Sign in to join this conversation.
No reviewers
No labels
No milestone
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Reference
boykopovar/AnyPS5!20009
No description provided.