feat(libc): implement sceLibcBacktraceSelf on Linux #2040

Closed
Aryansh1520 wants to merge 2 commits from upstream-pr/2040 into main
Aryansh1520 commented 2026-10-09 05:37:00 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by Aryansh1520 on 2026-10-09T05:37:00Z.


What

PPSA28997 (God of War Sons of Sparta, IL2CPP) calls sceLibcBacktraceGetBufferSize and sceLibcBacktraceSelf. On Linux, the host call stack is captured with backtrace, and each frame is resolved to its module and offset with dladdr. Frames are written as a linked list of records: next pointer, u32 module offset, then the module name at +0x10. GetBufferSize reports the count and the bytes Self will need. The signatures and record layout are inferred from the title and listed in TechnicalDebt, including the unknown u32 at +0xC (written as 0). Windows throws.

Tested

  • Linux (CachyOS, kernel 7.2), GCC: PPSA28997 (God of War Sons of Sparta) runs past the calls.
  • tools/check_conventions.py --base origin/main and tools/pr_overlap.py --branch pass.

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 in TechnicalDebt
  • 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
  • AI-assisted: yes
<!-- greyfinch-sync:boykopovar/AnyPS5:issue:4796456897 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/2040). Originally posted by [Aryansh1520](https://github.com/Aryansh1520) on 2026-10-09T05:37:00Z. --- ### What PPSA28997 (God of War Sons of Sparta, IL2CPP) calls `sceLibcBacktraceGetBufferSize` and `sceLibcBacktraceSelf`. On Linux, the host call stack is captured with `backtrace`, and each frame is resolved to its module and offset with `dladdr`. Frames are written as a linked list of records: next pointer, u32 module offset, then the module name at +0x10. `GetBufferSize` reports the count and the bytes `Self` will need. The signatures and record layout are inferred from the title and listed in TechnicalDebt, including the unknown u32 at +0xC (written as 0). Windows throws. ### Tested - Linux (CachyOS, kernel 7.2), GCC: PPSA28997 (God of War Sons of Sparta) runs past the calls. - `tools/check_conventions.py --base origin/main` and `tools/pr_overlap.py --branch` pass. ### 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 in [TechnicalDebt](https://github.com/boykopovar/AnyPS5/blob/main/docs/dev/TechnicalDebt.md#silent-stubs) - [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] AI-assisted: yes
MarcZX10 commented 2026-10-09 05:57:54 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-10-09T05:57:54Z.


The inferred record layout is documented as technical debt, but this ABI needs a regression that checks GetBufferSize against records written by Self, including a frame limit and a too-small buffer. Also, the PR describes this implementation as Linux-only, while the current non-Windows guard enables it on macOS too; please either gate the implementation to Linux and throw elsewhere, or provide macOS validation. GitHub reports no checks on this head.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6075240205 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/2040#issuecomment-6075240205). Originally posted by [MarcZX10](https://github.com/MarcZX10) on 2026-10-09T05:57:54Z. --- The inferred record layout is documented as technical debt, but this ABI needs a regression that checks GetBufferSize against records written by Self, including a frame limit and a too-small buffer. Also, the PR describes this implementation as Linux-only, while the current non-Windows guard enables it on macOS too; please either gate the implementation to Linux and throw elsewhere, or provide macOS validation. GitHub reports no checks on this head.
oneandonlydean commented 2026-10-09 09:22:56 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by oneandonlydean on 2026-10-09T09:22:56Z.


Same as #2038, for sceLibcBacktraceSelf. Also, #ifndef _WIN32 turns this on for macOS too; please guard it with __linux__ and throw elsewhere.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6078130298 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/2040#issuecomment-6078130298). Originally posted by [oneandonlydean](https://github.com/oneandonlydean) on 2026-10-09T09:22:56Z. --- Same as #2038, for `sceLibcBacktraceSelf`. Also, `#ifndef _WIN32` turns this on for macOS too; please guard it with `__linux__` and throw elsewhere.
Aryansh1520 commented 2026-10-09 10:36:56 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by Aryansh1520 on 2026-10-09T10:36:56Z.


Updated in 75c1fa60:

  • The implementation is now behind #ifdef __linux__. macOS and Windows throw through NotImplemented_nid_no_patch, and the TechnicalDebt entry says so.
  • New Linux test guest_backtrace:
    • GetBufferSize reports the same count and byte size that Self then writes.
    • The record layout: 8-byte aligned records, next pointing to the following record and null on the last, the u32 at +0xC zero, NUL-terminated names at +0x10.
    • The first frame resolves to the calling function.
    • Frame limits 0, 1 and 2 in both functions.
    • Buffers of 0, one record minus one byte, one record, two records minus one byte and two records give 0/1/1/1/2 records, and nothing is written past the buffer size.
  • The test showed the code assumed the frame capture helper is not inlined. Force-inlining it drops the caller's frame and fails the test, so the helper is now [[gnu::noinline]].

Where PPSA28997 stops: after loading its first scene, the title calls sceLibcBacktraceGetBufferSize (sMko2YZqDNQ), imported by Il2cppUserAssemblies.prx. When it was a not-implemented stub in my local build:

[src/Open.cpp:99 sceKernelOpen] path=/app0/Media/level0 flags=0x0 nativeFlags=0x0 mode=00
[src/Open.cpp:99 sceKernelOpen] path=/app0/Media/sharedassets0.assets.resS flags=0x0 nativeFlags=0x0 mode=00
[libc] unhandled exception 'St13runtime_error' thrown from 0x7f2e1f18a7a9
terminate called after throwing an instance of 'std::runtime_error'
  what():  libcUnknown_sMko2YZqDNQ not implemented

Main exports neither function. On main (as of Oct 8) the title stops earlier, at LTov9gMEqCU (std::ios_base::_Addstd, #545).

The build workflows on this head are waiting for approval to run (fork PR).

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6079203534 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/2040#issuecomment-6079203534). Originally posted by [Aryansh1520](https://github.com/Aryansh1520) on 2026-10-09T10:36:56Z. --- Updated in 75c1fa60: - The implementation is now behind `#ifdef __linux__`. macOS and Windows throw through `NotImplemented_nid_no_patch`, and the TechnicalDebt entry says so. - New Linux test `guest_backtrace`: - `GetBufferSize` reports the same count and byte size that `Self` then writes. - The record layout: 8-byte aligned records, `next` pointing to the following record and null on the last, the u32 at +0xC zero, NUL-terminated names at +0x10. - The first frame resolves to the calling function. - Frame limits 0, 1 and 2 in both functions. - Buffers of 0, one record minus one byte, one record, two records minus one byte and two records give 0/1/1/1/2 records, and nothing is written past the buffer size. - The test showed the code assumed the frame capture helper is not inlined. Force-inlining it drops the caller's frame and fails the test, so the helper is now `[[gnu::noinline]]`. Where PPSA28997 stops: after loading its first scene, the title calls `sceLibcBacktraceGetBufferSize` (`sMko2YZqDNQ`), imported by `Il2cppUserAssemblies.prx`. When it was a not-implemented stub in my local build: ``` [src/Open.cpp:99 sceKernelOpen] path=/app0/Media/level0 flags=0x0 nativeFlags=0x0 mode=00 [src/Open.cpp:99 sceKernelOpen] path=/app0/Media/sharedassets0.assets.resS flags=0x0 nativeFlags=0x0 mode=00 [libc] unhandled exception 'St13runtime_error' thrown from 0x7f2e1f18a7a9 terminate called after throwing an instance of 'std::runtime_error' what(): libcUnknown_sMko2YZqDNQ not implemented ``` Main exports neither function. On main (as of Oct 8) the title stops earlier, at `LTov9gMEqCU` (`std::ios_base::_Addstd`, #545). The build workflows on this head are waiting for approval to run (fork PR).
popcoder565 commented 2026-10-09 15:46:35 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by popcoder565 on 2026-10-09T15:46:35Z.


Native Windows run, since no Windows results were posted. Setup: Windows 10 (19045), MinGW-w64 GCC 15.2.0 (WinLibs r7), Release. Head 75c1fa60, merged onto main d70b8998 in one build with 38 other PRs that had no Windows run.

  • Its test, guest_backtrace, is registered only on Linux, so on Windows this only checks that the change builds.
  • Full ctest in that build: 519 of 519. That needed local fixes to three other PRs in the batch that don't build or pass on Windows: #1611, #1796 and #1824 (reported there).

No title was run.

🤖 Addressed by Claude Code

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6084274067 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/2040#issuecomment-6084274067). Originally posted by [popcoder565](https://github.com/popcoder565) on 2026-10-09T15:46:35Z. --- Native Windows run, since no Windows results were posted. Setup: Windows 10 (19045), MinGW-w64 GCC 15.2.0 (WinLibs r7), Release. Head `75c1fa60`, merged onto `main` d70b8998 in one build with 38 other PRs that had no Windows run. - Its test, `guest_backtrace`, is registered only on Linux, so on Windows this only checks that the change builds. - Full ctest in that build: 519 of 519. That needed local fixes to three other PRs in the batch that don't build or pass on Windows: #1611, #1796 and #1824 (reported there). No title was run. _🤖 Addressed by [Claude Code](https://claude.com/claude-code)_
adriadam10 commented 2026-10-09 21:27:48 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by adriadam10 on 2026-10-09T21:27:48Z.

Review: CHANGES_REQUESTED


Thanks for the __linux__ gate and the layout/limit/small-buffer test; build and ctest are green here (guest_backtrace passes, 524/524).

One blocker left:

  • The signatures and the record layout (next, u32 offset, u32 at +0xC, name at +0x10, 8-byte alignment) are "inferred from the title", but no evidence is in the PR, and none of KytyPS5, shadPS4, fpPS4 or prosper has a body for these exports. The test pins the PR's own layout, so it can't catch a wrong guess. Please attach static evidence from the title's call site (instruction offsets showing how the returned list is walked: the next load, the offset use, the name pointer, and which return values are checked), or a log of the title consuming the result. If it can't be confirmed, say which parts are guesses in TechnicalDebt (return value on a too-small buffer, whether count includes truncated frames).
<!-- greyfinch-sync:boykopovar/AnyPS5:review:5475511164 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/2040#pullrequestreview-5475511164). Originally posted by [adriadam10](https://github.com/adriadam10) on 2026-10-09T21:27:48Z. **Review:** CHANGES_REQUESTED --- Thanks for the `__linux__` gate and the layout/limit/small-buffer test; build and ctest are green here (`guest_backtrace` passes, 524/524). One blocker left: - The signatures and the record layout (next, u32 offset, u32 at +0xC, name at +0x10, 8-byte alignment) are "inferred from the title", but no evidence is in the PR, and none of KytyPS5, shadPS4, fpPS4 or prosper has a body for these exports. The test pins the PR's own layout, so it can't catch a wrong guess. Please attach static evidence from the title's call site (instruction offsets showing how the returned list is walked: the `next` load, the offset use, the name pointer, and which return values are checked), or a log of the title consuming the result. If it can't be confirmed, say which parts are guesses in TechnicalDebt (return value on a too-small buffer, whether `count` includes truncated frames).
github-actions[bot] commented 2026-10-09 21:52:07 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by github-actions[bot] on 2026-10-09T21:52:07Z.


Progress report (51f69aa → 75c1fa6)

📈 System libraries: 84.7% (+0.01%, +2 implemented functions)

✅ 2 implemented
Library Name
libc sceLibcBacktraceGetBufferSize
libc sceLibcBacktraceSelf
<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6089867236 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/2040#issuecomment-6089867236). Originally posted by [github-actions[bot]](https://github.com/apps/github-actions) on 2026-10-09T21:52:07Z. --- <!-- progress-report --> ### Progress report (51f69aa → 75c1fa6) 📈 **System libraries**: 84.7% (+0.01%, +2 implemented functions) <details> <summary>✅ 2 implemented</summary> | Library | Name | | - | - | | libc | `sceLibcBacktraceGetBufferSize` | | libc | `sceLibcBacktraceSelf` | </details>
oneandonlydean commented 2026-10-09 22:30:59 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by oneandonlydean on 2026-10-09T22:30:59Z.


Closing: the record layout is inferred and the frames are host frames, so what the title gets back isn't what the console returns. Please reopen with the exact layout, or a version that throws for what isn't known.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6090336804 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/2040#issuecomment-6090336804). Originally posted by [oneandonlydean](https://github.com/oneandonlydean) on 2026-10-09T22:30:59Z. --- Closing: the record layout is inferred and the frames are host frames, so what the title gets back isn't what the console returns. Please reopen with the exact layout, or a version that throws for what isn't known.

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!2040
No description provided.