fix(libc): start sceLibcBacktraceSelf's frames at its caller #9240

Merged
milescoviello merged 2 commits from upstream-pr/9240 into main 2026-10-28 04:49:32 +00:00
milescoviello commented 2026-10-21 16:17:35 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by milescoviello on 2026-10-21T16:17:35Z.


What

sceLibcBacktraceSelf and sceLibcBacktraceGetBufferSize build their records in CaptureFrames, which drops the first two captured return addresses: they are taken to be CaptureFrames' own frame and the export's. If the unwinder adds a frame between them, the records start one frame early, inside libc.prx.

An ANYPS5_ENABLE_ASAN build does this. The sanitizer intercepts backtrace(), so the captured list has one more frame at its start, and the first record is the export itself:

frame libc.prx +0xe8c87
frame guest_backtrace_tests +0x5293
frame guest_backtrace_tests +0x284d
...
guest backtrace: first frame is not in the calling module

Both exports now pass __builtin_return_address(0), their caller's return address, to CaptureFrames. The records start at the captured address equal to it, so a guest's first record is its own call site however many frames the unwinder adds. If no captured address matches, the first two are dropped as before.

Tested

Ubuntu 24.04, GCC 13.3, on main cecb540a63.

  • ANYPS5_ENABLE_ASAN build, ctest -R '^guest_backtrace$':
    • main: fails 3 of 3 with "first frame is not in the calling module". The frames above are from main with a print added to the test.
    • With this change: passes 3 of 3.
  • Normal test build: passes 3 of 3.
  • Windows 11 Enterprise 26300, WinLibs GCC 15.2.0 r7 (UCRT), Release, on the _Unwind_Backtrace path: guest_backtrace passes 3 of 3 on main and 3 of 3 with this change.

It came up in an ASan sweep of the CPU-side tests on main, with #9236 and #9238. With all three, the sweep's only failures are the three net_dontwait_blocking_* tests (#8917, #8918) and guest_kernel_stack_protect, which fails intermittently under ASan on main too ("Shadow memory range interleaves with an existing memory mapping", 2 of 3 runs).

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: none needed (libc only)
  • AI-assisted: yes
<!-- greyfinch-sync:boykopovar/AnyPS5:issue:4954758819 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/9240). Originally posted by [milescoviello](https://github.com/milescoviello) on 2026-10-21T16:17:35Z. --- ### What `sceLibcBacktraceSelf` and `sceLibcBacktraceGetBufferSize` build their records in `CaptureFrames`, which drops the first two captured return addresses: they are taken to be `CaptureFrames`' own frame and the export's. If the unwinder adds a frame between them, the records start one frame early, inside `libc.prx`. An `ANYPS5_ENABLE_ASAN` build does this. The sanitizer intercepts `backtrace()`, so the captured list has one more frame at its start, and the first record is the export itself: ``` frame libc.prx +0xe8c87 frame guest_backtrace_tests +0x5293 frame guest_backtrace_tests +0x284d ... guest backtrace: first frame is not in the calling module ``` Both exports now pass `__builtin_return_address(0)`, their caller's return address, to `CaptureFrames`. The records start at the captured address equal to it, so a guest's first record is its own call site however many frames the unwinder adds. If no captured address matches, the first two are dropped as before. ### Tested Ubuntu 24.04, GCC 13.3, on `main` cecb540a63. - `ANYPS5_ENABLE_ASAN` build, `ctest -R '^guest_backtrace$'`: - `main`: fails 3 of 3 with "first frame is not in the calling module". The frames above are from `main` with a print added to the test. - With this change: passes 3 of 3. - Normal test build: passes 3 of 3. - Windows 11 Enterprise 26300, WinLibs GCC 15.2.0 r7 (UCRT), Release, on the `_Unwind_Backtrace` path: `guest_backtrace` passes 3 of 3 on `main` and 3 of 3 with this change. It came up in an ASan sweep of the CPU-side tests on `main`, with #9236 and #9238. With all three, the sweep's only failures are the three `net_dontwait_blocking_*` tests (#8917, #8918) and `guest_kernel_stack_protect`, which fails intermittently under ASan on `main` too ("Shadow memory range interleaves with an existing memory mapping", 2 of 3 runs). ### Checklist <!-- Rules behind each item: https://github.com/boykopovar/AnyPS5/blob/main/CONTRIBUTING.md --> - [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: none needed (libc only) - [x] AI-assisted: yes
harisibrar1 commented 2026-10-24 12:56:55 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by harisibrar1 on 2026-10-24T12:56:55Z.

Review: APPROVED


Checked against main 99feaf0eb8 and core/libs/prx/libc/src/exception/Unwind.cpp on main:

  • This still merges cleanly onto main, and main's CaptureFrames still drops a fixed two frames.
  • Reading the code, the Windows path should behave like Linux: _Unwind_Backtrace_nid_postfix steps once before the first callback and _Unwind_GetIP_nid_postfix returns registers[16] (Unwind.cpp:619, 638-649), so each frame's entry is a return address and __builtin_return_address(0) is a valid key. The index-2 fallback keeps today's behaviour if nothing matches.
  • The overlap with #9344 is only the neighbouring #if defined(__linux__) lines; whichever lands second needs a trivial rebase.
  • Looks good to me.

Reviewed with AI assistance (Claude Code); the checks above were run locally on my machine.

<!-- greyfinch-sync:boykopovar/AnyPS5:review:5638320238 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/9240#pullrequestreview-5638320238). Originally posted by [harisibrar1](https://github.com/harisibrar1) on 2026-10-24T12:56:55Z. **Review:** APPROVED --- Checked against main 99feaf0eb8 and core/libs/prx/libc/src/exception/Unwind.cpp on main: - This still merges cleanly onto main, and main's `CaptureFrames` still drops a fixed two frames. - Reading the code, the Windows path should behave like Linux: `_Unwind_Backtrace_nid_postfix` steps once before the first callback and `_Unwind_GetIP_nid_postfix` returns `registers[16]` (Unwind.cpp:619, 638-649), so each frame's entry is a return address and `__builtin_return_address(0)` is a valid key. The index-2 fallback keeps today's behaviour if nothing matches. - The overlap with #9344 is only the neighbouring `#if defined(__linux__)` lines; whichever lands second needs a trivial rebase. - Looks good to me. Reviewed with AI assistance (Claude Code); the checks above were run locally on my machine.
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!9240
No description provided.