fix(libc): allocate getcwd's buffer from the application heap #2324

Merged
reyani-dev merged 1 commit from upstream-pr/2324 into main 2026-10-10 04:46:56 +00:00
reyani-dev commented 2026-10-09 15:31:56 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by reyani-dev on 2026-10-09T15:31:56Z.


What

getcwd(NULL, size) returned a buffer from GuestHeap::GuestHeapAllocate. The caller releases it with free(), which goes through the application heap (ApplicationHeapFree). With the default heap, the two are the same allocator, so this worked. When a title registers its own allocator through the malloc-replacement handshake, though, free() hands the replacement a pointer it never returned. Every other libc function that returns memory for the caller to free (strdup, strndup, asprintf, vasprintf, malloc itself) already allocates through ApplicationHeapAllocate; getcwd was the only one that didn't.

getcwd now allocates with ApplicationHeapAllocate_nid_no_patch, and, like strndup, reports ENOMEM and returns null when the allocator returns null. Before, a null buffer would have been written to.

Tested

Windows 11, WinLibs GCC 15.2.0 r7, Release, -DBUILD_TESTING=ON:

  • application_heap_replacement (the registered replacement allocator in ApplicationHeap.cpp):

    • getcwd(NULL, 0) returns the replacement's storage, sized strlen + 1;
    • getcwd(NULL, 200) requests 200 bytes;
    • both are released through ApplicationHeapFree, which asserts that the pointer is its own;
    • with the replacement failing, getcwd(NULL, 0) returns null with errno 12.

    On main the replacement case fails.

  • guest_working_directory freed the buffer with GuestHeapFree and ran without an application heap, which no title does. It now registers an allocator that forwards to the guest heap, as the default heap does. It checks that getcwd(NULL, 0) allocates through it and frees with ApplicationHeapFree.

  • ctest -E "^agc_|video_out": 249/249 with the test update.

  • tools/check_conventions.py passes, and tools/pr_overlap.py --branch reports no overlap. It also merges cleanly with #1369, #1900, #1656 and #1611, which touch the same test files.

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

🤖 Generated with Claude Code

<!-- greyfinch-sync:boykopovar/AnyPS5:issue:4802523862 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/2324). Originally posted by [reyani-dev](https://github.com/reyani-dev) on 2026-10-09T15:31:56Z. --- ### What `getcwd(NULL, size)` returned a buffer from `GuestHeap::GuestHeapAllocate`. The caller releases it with `free()`, which goes through the application heap (`ApplicationHeapFree`). With the default heap, the two are the same allocator, so this worked. When a title registers its own allocator through the malloc-replacement handshake, though, `free()` hands the replacement a pointer it never returned. Every other libc function that returns memory for the caller to free (`strdup`, `strndup`, `asprintf`, `vasprintf`, `malloc` itself) already allocates through `ApplicationHeapAllocate`; `getcwd` was the only one that didn't. `getcwd` now allocates with `ApplicationHeapAllocate_nid_no_patch`, and, like `strndup`, reports `ENOMEM` and returns null when the allocator returns null. Before, a null buffer would have been written to. ### Tested Windows 11, WinLibs GCC 15.2.0 r7, Release, `-DBUILD_TESTING=ON`: - `application_heap_replacement` (the registered replacement allocator in `ApplicationHeap.cpp`): - `getcwd(NULL, 0)` returns the replacement's storage, sized `strlen + 1`; - `getcwd(NULL, 200)` requests 200 bytes; - both are released through `ApplicationHeapFree`, which asserts that the pointer is its own; - with the replacement failing, `getcwd(NULL, 0)` returns null with errno 12. On `main` the replacement case fails. - `guest_working_directory` freed the buffer with `GuestHeapFree` and ran without an application heap, which no title does. It now registers an allocator that forwards to the guest heap, as the default heap does. It checks that `getcwd(NULL, 0)` allocates through it and frees with `ApplicationHeapFree`. - `ctest -E "^agc_|video_out"`: 249/249 with the test update. - `tools/check_conventions.py` passes, and `tools/pr_overlap.py --branch` reports no overlap. It also merges cleanly with #1369, #1900, #1656 and #1611, which touch the same test files. ### 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 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 🤖 Generated with [Claude Code](https://claude.com/claude-code)
reyani-dev commented 2026-10-09 16:01:25 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by reyani-dev on 2026-10-09T16:01:25Z.


Full Linux and Windows builds and tests for this head (d8c5d142), using the project's Build workflow. I can't start the upstream manual run, so I ran the same build.yml in my fork through a mirror PR. Like the upstream run, the jobs check out the PR head merged into current main (d70b8998):

Job Result
linux success; full build, ctest: 516/516 passed
windows success; full build, ctest: 507/507 passed
relinker (Linux, Windows, macOS) success

Run: https://github.com/AchrafReyani/AnyPS5/actions/runs/37952444259

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6084524726 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/2324#issuecomment-6084524726). Originally posted by [reyani-dev](https://github.com/reyani-dev) on 2026-10-09T16:01:25Z. --- Full Linux and Windows builds and tests for this head (`d8c5d142`), using the project's Build workflow. I can't start the upstream manual run, so I ran the same `build.yml` in my fork through a mirror PR. Like the upstream run, the jobs check out the PR head merged into current `main` (`d70b8998`): | Job | Result | |---|---| | [linux](https://github.com/AchrafReyani/AnyPS5/actions/runs/37952444259/job/113894464258) | success; full build, `ctest`: 516/516 passed | | [windows](https://github.com/AchrafReyani/AnyPS5/actions/runs/37952444259/job/113894464236) | success; full build, `ctest`: 507/507 passed | | relinker (Linux, Windows, macOS) | success | Run: https://github.com/AchrafReyani/AnyPS5/actions/runs/37952444259
reyani-dev commented 2026-10-09 16:01:31 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by reyani-dev on 2026-10-09T16:01:31Z.


Full Linux and Windows builds and tests for this head (d8c5d142), using the project's Build workflow. I can't start the upstream manual run, so I ran the same build.yml in my fork through a mirror PR. Like the upstream run, the jobs check out the PR head merged into current main (d70b8998):

Job Result
linux success; full build, ctest: 516/516 passed
windows success; full build, ctest: 507/507 passed
relinker (Linux, Windows, macOS) success

Run: https://github.com/AchrafReyani/AnyPS5/actions/runs/37952444259

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6084526389 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/2324#issuecomment-6084526389). Originally posted by [reyani-dev](https://github.com/reyani-dev) on 2026-10-09T16:01:31Z. --- Full Linux and Windows builds and tests for this head (`d8c5d142`), using the project's Build workflow. I can't start the upstream manual run, so I ran the same `build.yml` in my fork through a mirror PR. Like the upstream run, the jobs check out the PR head merged into current `main` (`d70b8998`): | Job | Result | |---|---| | [linux](https://github.com/AchrafReyani/AnyPS5/actions/runs/37952444259/job/113894464258) | success; full build, `ctest`: 516/516 passed | | [windows](https://github.com/AchrafReyani/AnyPS5/actions/runs/37952444259/job/113894464236) | success; full build, `ctest`: 507/507 passed | | relinker (Linux, Windows, macOS) | success | Run: https://github.com/AchrafReyani/AnyPS5/actions/runs/37952444259
Kikoarl commented 2026-10-09 17:53:10 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by Kikoarl on 2026-10-09T17:53:10Z.


Tested on Linux (CachyOS x86_64, Intel RPL-S iGPU / Mesa 26.3.0-devel / Vulkan 1.4.363, GCC 16.2.1) at d8c5d142:

  • Build: Built cleanly in RAM (/tmp/anyps5_build) with zero warnings/errors.
  • Conventions: python3 tools/check_conventions.py --base origin/main --head HEAD -> PASS (0 errors).
  • Verification: Verified application heap allocation for getcwd in General.cpp and GuestWorkingDirectory.cpp. Complies with guest allocator lifecycle.

LGTM!

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6086314031 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/2324#issuecomment-6086314031). Originally posted by [Kikoarl](https://github.com/Kikoarl) on 2026-10-09T17:53:10Z. --- Tested on Linux (CachyOS x86_64, Intel RPL-S iGPU / Mesa 26.3.0-devel / Vulkan 1.4.363, GCC 16.2.1) at `d8c5d142`: - **Build**: Built cleanly in RAM (`/tmp/anyps5_build`) with zero warnings/errors. - **Conventions**: `python3 tools/check_conventions.py --base origin/main --head HEAD` -> **PASS (0 errors)**. - **Verification**: Verified application heap allocation for `getcwd` in `General.cpp` and `GuestWorkingDirectory.cpp`. Complies with guest allocator lifecycle. LGTM!
reyani-dev commented 2026-10-09 18:23:04 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by reyani-dev on 2026-10-09T18:23:04Z.


Thanks for testing it on Linux, @Kikoarl. That matches the full Linux and Windows build posted above for the same head.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6086788656 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/2324#issuecomment-6086788656). Originally posted by [reyani-dev](https://github.com/reyani-dev) on 2026-10-09T18:23:04Z. --- Thanks for testing it on Linux, @Kikoarl. That matches the full Linux and Windows build posted above for the same head.
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!2324
No description provided.