fix(libc): allocate getcwd's buffer from the application heap #2324
Loading…
Reference in a new issue
No description provided.
Delete branch "upstream-pr/2324"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
Imported from boykopovar/AnyPS5. Originally posted by reyani-dev on 2026-10-09T15:31:56Z.
What
getcwd(NULL, size)returned a buffer fromGuestHeap::GuestHeapAllocate. The caller releases it withfree(), 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,mallocitself) already allocates throughApplicationHeapAllocate;getcwdwas the only one that didn't.getcwdnow allocates withApplicationHeapAllocate_nid_no_patch, and, likestrndup, reportsENOMEMand 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 inApplicationHeap.cpp):getcwd(NULL, 0)returns the replacement's storage, sizedstrlen + 1;getcwd(NULL, 200)requests 200 bytes;ApplicationHeapFree, which asserts that the pointer is its own;getcwd(NULL, 0)returns null with errno 12.On
mainthe replacement case fails.guest_working_directoryfreed the buffer withGuestHeapFreeand 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 thatgetcwd(NULL, 0)allocates through it and frees withApplicationHeapFree.ctest -E "^agc_|video_out": 249/249 with the test update.tools/check_conventions.pypasses, andtools/pr_overlap.py --branchreports no overlap. It also merges cleanly with #1369, #1900, #1656 and #1611, which touch the same test files.Checklist
main; no other open PR implements the same functionsNotImplemented_nid_no_patch); silent stubs are listed in TechnicalDebt.mdfiles or images added to the repository (attach them to this PR)🤖 Generated with Claude Code
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 samebuild.ymlin my fork through a mirror PR. Like the upstream run, the jobs check out the PR head merged into currentmain(d70b8998):ctest: 516/516 passedctest: 507/507 passedRun: https://github.com/AchrafReyani/AnyPS5/actions/runs/37952444259
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 samebuild.ymlin my fork through a mirror PR. Like the upstream run, the jobs check out the PR head merged into currentmain(d70b8998):ctest: 516/516 passedctest: 507/507 passedRun: https://github.com/AchrafReyani/AnyPS5/actions/runs/37952444259
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:/tmp/anyps5_build) with zero warnings/errors.python3 tools/check_conventions.py --base origin/main --head HEAD-> PASS (0 errors).getcwdinGeneral.cppandGuestWorkingDirectory.cpp. Complies with guest allocator lifecycle.LGTM!
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.