Skip to content

fix(libc): use the default heap for an executable without process parameters - #11979

Merged
oneandonlydean merged 5 commits into
boykopovar:mainfrom
xXPBSXx:carry/6713-default-heap-no-procparam
Oct 28, 2026
Merged

oneandonlydean merged 5 commits into
boykopovar:mainfrom
xXPBSXx:carry/6713-default-heap-no-procparam

Conversation

@xXPBSXx

@xXPBSXx xXPBSXx commented Oct 27, 2026 •

Copy link
Copy Markdown
Contributor

What

Carries #6713 (karimibrahim07, head 668d51adbd; both commits keep their authorship), rebased onto current main after a conflict in docs/dev/debt/libc.md, as offered in the hand-off comment on that PR. The rebase adds only the new "Application heap without process parameters" line to docs/dev/debt/libc.md. The review asked to reconcile this PR's debt notes with #10947 (merged on 27 October); after the rebase this PR adds only that one line to the debt file, and its own nearbyintf and _Thrd_sleep notes are no longer part of the diff. Whether the entries on main agree with each other is not checked here.

An ELF without a PT_SCE_PROCPARAM segment, such as a ps5-payload-dev SDK program, got no .procpar section when relinked for Windows. Its first allocation then stopped with application heap: process metadata is missing; relink the executable (read in the code, not run on the unchanged main). That message, added together with the .procpar writer in 9f2b41b9a1, asks for a relink, but relinking such an ELF cannot add process parameters it does not have.

  • WindowsPePatcher now writes a .procpar section with address 0 and size 0 when the ELF has no PT_SCE_PROCPARAM segment.
  • ApplicationProcessParameters_nid_no_patch returns a static zeroed 0x40-byte block for that record, so callers that read the compiled SDK version at offset 16 see 0.
  • ApplicationHeapInitialize_nid_no_patch recognises that block by its address and registers the heap's existing default allocator, as it already does for an all-null replacement table. sceKernelGetProcParam returns the result of that lookup, so it returns the same block (read in the code).

An executable without any .procpar section still gets the relink message (application_heap_stats_missing still passes). The new entry in docs/dev/debt/libc.md records that, on the console, such a program runs inside its launcher's process, whose parameters aren't modelled (this is the entry's wording, not something measured here). On Linux the process-parameter lookup is unchanged and still throws application heap: invalid or missing process parameters when it finds none (read in ApplicationProcessParameters.cpp, not run here).

Overlap: 170 of the 1295 open pull requests change at least one of the files this PR touches; git merge-tree of this branch with each of their heads, each on the merge base git finds, reports a conflict for 22 of them. 20 of those already conflict with main on their own (almost all in docs/dev/debt/libc.md), so this change adds no landing order for them. #11302 (in docs/dev/debt/libc.md) and #11463 (in core/relinker/CMakeLists.txt) conflict only with this branch; whichever lands second rebases and keeps both changes. The authors of #11302 and #11463 are asked here which of the pull requests should land first, and this PR waits for their answer before the landing order is fixed. No pull request of mine is a dependency (Depends on: none).

Tested

Windows 10 Pro (19045), WinLibs GCC 15.2.0 r7, Ninja Release, BUILD_TESTING=ON, and Ubuntu 24.04 under WSL2 (a virtualised kernel, not bare-metal; GCC 13.3, Release), both on main 8ff4baf534 with this branch on top; each arm built fresh (ninja exit code 0 read), 5 runs each:

  • Windows, this branch: application_heap* (10 tests, including the new application_heap_without_parameters) and the new relinker test windows_process_parameters pass, 11 of 11, in all five runs.
  • Windows, same main with ApplicationHeap.cpp, ApplicationProcessParameters.cpp and WindowsPePatcher.cpp reverted and the tests kept: 2 of 11 fail in all five runs. application_heap_without_parameters stops with exit code 0xc0000409, and windows_process_parameters fails.
  • Linux (WSL2), this branch: the application_heap* tests that exist on Linux and windows_process_parameters pass, 13 of 13, in all five runs. With the three files reverted, 1 of 13 fails in all five runs: windows_process_parameters.
  • Green arm repeated after the rebase onto main eece5da035 (before the final rebase onto 1089f2bbfd, which changes none of the PR's files or test targets): 3 runs each, Windows 11 of 11 and Linux (WSL2) 13 of 13.
  • Follow-up commit (zeroed block instead of null): Windows, 3 runs, 11 of 11 pass; with the production files of the previous head (0cb4dde2ee, which returns null) and the new test, application_heap_without_parameters fails in all three runs (1 of 11); Linux (WSL2), 3 runs, 13 of 13 pass. The callers that read offset 16 were not run.
  • A second run of the same head, built and run independently in a separate worktree and session on the same machine, gave the same result: on Windows 3 runs of 11 of 11, and with the three files reverted 2 of 11 failing in all three runs (the same two tests); on Linux (WSL2) 3 runs of 13 of 13. The Linux run with the files reverted was not repeated there.
  • python tools/check_conventions.py --base upstream/main passes with this title and description; git merge-tree against main 1089f2bbfd is clean.
  • Not run here: the full suite, macOS, and the Quake homebrew run described on fix(libc): use the default heap for an executable without process parameters #6713 (it needs fix(relinker): resolve symbol-name imports through NIDs on Windows #6697, fix(relinker): leave weak imports of bundled modules unbound on Windows #6702 and feat(libc): implement qsort_s #6707).

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
  • Unblocks: none
  • Fixes: none
  • GPUs tested: n/a
  • AI-assisted: yes

Co-authored by the original author of #6713.

AI-assisted (Claude Code).

🤖 Generated with Claude Code

…ameters

An ELF without a PT_SCE_PROCPARAM segment, such as a ps5-payload-dev
SDK program, had no .procpar section after relinking, so the first
allocation stopped with "process metadata is missing; relink the
executable".

The Windows relinker now writes a .procpar section that records no
parameters for such an ELF, and the application heap registers its
default allocator when it finds that record. An executable without
any .procpar section still gets the relink message.
@github-actions

github-actions Bot commented Oct 27, 2026 •

Copy link
Copy Markdown

Other open pull requests touch the same code. Merging one will break the other, or both implement the same thing. If this one builds on them, list them in Depends on.

Pull request Conflicting files (lines on main) Same exports Same new files
PR 6713 core/relinker/relinker/tests/test_windows_process_parameters.py
PR 12605 docs/dev/debt/libc.md L16-17

Some comparisons could not be completed because a pull request conflicts with the base branch. Review these shared files manually before concluding that there is no overlap.

Pull request Shared files not compared
PR 5021 core/libs/prx/libc/CMakeLists.txt
core/libs/prx/libc/src/ApplicationProcessParameters.cpp
core/relinker/CMakeLists.txt
docs/dev/debt/libc.md
PR 6085 core/libs/prx/libc/CMakeLists.txt
PR 6713 core/libs/prx/libc/CMakeLists.txt
core/libs/prx/libc/src/ApplicationHeap.cpp
core/libs/prx/libc/src/ApplicationProcessParameters.cpp
core/libs/tests/ApplicationHeap.cpp
core/relinker/CMakeLists.txt
core/relinker/elfpatcher/src/windows/WindowsPePatcher.cpp
core/relinker/relinker/tests/test_windows_process_parameters.py
docs/dev/debt/libc.md
PR 7081 core/libs/prx/libc/CMakeLists.txt
docs/dev/debt/libc.md
PR 7386 docs/dev/debt/libc.md
PR 7447 core/libs/prx/libc/CMakeLists.txt
docs/dev/debt/libc.md
PR 7715 core/libs/prx/libc/CMakeLists.txt
docs/dev/debt/libc.md
PR 8150 core/libs/prx/libc/CMakeLists.txt
docs/dev/debt/libc.md
PR 8937 core/libs/prx/libc/CMakeLists.txt
docs/dev/debt/libc.md
PR 9371 docs/dev/debt/libc.md
PR 9447 docs/dev/debt/libc.md
PR 9526 docs/dev/debt/libc.md
PR 9775 core/libs/prx/libc/CMakeLists.txt
PR 9793 core/relinker/CMakeLists.txt
PR 9929 core/libs/prx/libc/CMakeLists.txt
docs/dev/debt/libc.md
PR 10002 docs/dev/debt/libc.md
PR 10253 docs/dev/debt/libc.md
PR 10324 core/libs/prx/libc/CMakeLists.txt
docs/dev/debt/libc.md
PR 10433 docs/dev/debt/libc.md
PR 10620 docs/dev/debt/libc.md
PR 10861 core/relinker/CMakeLists.txt
core/relinker/elfpatcher/src/windows/WindowsPePatcher.cpp
PR 11270 docs/dev/debt/libc.md
PR 11332 docs/dev/debt/libc.md
PR 11625 core/libs/prx/libc/CMakeLists.txt
docs/dev/debt/libc.md
PR 11849 docs/dev/debt/libc.md
PR 11872 core/libs/prx/libc/CMakeLists.txt
docs/dev/debt/libc.md
PR 11892 docs/dev/debt/libc.md
PR 11969 core/libs/prx/libc/CMakeLists.txt
docs/dev/debt/libc.md
PR 12397 docs/dev/debt/libc.md

@oneandonlydean

Copy link
Copy Markdown
Collaborator

@xXPBSXx ApplicationProcessParameters_nid_no_patch() now returns null on Windows for the no-procparam record, but libSceRtc/Export.cpp:123, libSceHttp/src/Uri.cpp:254, libSceSsl/Export.cpp:726, libSceAppContent/Export.cpp:36, libkernel_sys/Export.cpp:315 and libkernel/System/src/Coredump.cpp:30 (and the other callers in core/libs/prx) memcpy from process+16 without a null check, which would crash where main threw a message. Could the no-parameter case return a static zeroed block (or could those callers handle null), with a test that sceKernelGetProcParam's result is safe to read at offset 16?

…process parameters

Callers of ApplicationProcessParameters_nid_no_patch read the compiled SDK
version at offset 16 without a null check, so the null result for the
no-parameters record would crash them. The lookup now returns a static
zeroed 0x40-byte block for that record, and the heap recognises it by
address and keeps registering its default allocator.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
@xXPBSXx

xXPBSXx commented Oct 27, 2026

Copy link
Copy Markdown
Contributor Author

Thanks, confirmed: the callers you list, and the others in core/libs/prx that read the compiled SDK version at offset 16 of ApplicationProcessParameters_nid_no_patch(), do not check for null (read in the code, not run), and a search of the callers found none that compares the result with null. A follow-up commit on this branch makes the lookup return a static zeroed 0x40-byte block for the no-parameters record instead of null; the heap recognises that block by its address and still registers its default allocator. sceKernelGetProcParam returns the lookup's result, so it returns the same block and offset 16 reads 0 there too.

application_heap_without_parameters now also checks that the returned pointer is not null and that its first 0x40 bytes are zero. On Windows, built at the new head, all 11 tests pass (3 of 3 runs); with the production files of the previous head (0cb4dde2ee, which returns null) and the new test, application_heap_without_parameters fails in all three runs (1 of 11). The Linux (WSL2) set passes 13 of 13 (3 of 3 runs). The callers themselves were not run.

AI-assisted (Claude Code).

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
@github-actions github-actions Bot added needs rebase Conflicts with main, rebase needed libc build relinker docs and removed needs rebase Conflicts with main, rebase needed labels Oct 27, 2026
@xXPBSXx

xXPBSXx commented Oct 27, 2026

Copy link
Copy Markdown
Contributor Author

main is merged into the branch (head 944b4d753e): the conflict was in docs/dev/debt/libc.md only, both entries are kept, and the three carried commits are unchanged. The Windows set passes 11 of 11 and the Linux set 13 of 13 on the merge head.

AI-assisted (Claude Code).

@rooklz rooklz left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reviewed the relinker half on the head 944b4d753e, merged onto main 895ead67f7. I could not build the libc half or its application_heap_without_parameters test: they are Windows-only, and I only have macOS here.

Ran

  • Relinker-only build of the merge, ctest 371 of 371 passed. The new windows_process_parameters test passes.
  • The windows-latest failure on this run is shader_cache_native_paths timing out after 20.36 s. This PR changes no shader files.
  • Section headers the relinker writes for main_fixture(): .procpar has RVA 0x11000, VirtualSize 8, SizeOfRawData 0, PointerToRawData 0, flags 0x40000040. The loader zero-fills it, so the reader gets address 0 and size 0, which is what the new branch in ApplicationProcessParameters_nid_no_patch expects.

Mutants of the WindowsPePatcher.cpp hunk, run against the new test

mutant result
hunk removed killed
std::vector<std::uint8_t>(8, 1) killed
empty data killed
std::vector<std::uint8_t>(4) survives
std::vector<std::uint8_t>(16) survives
flag 0x40u changed to 0x80u survives

Raw size and raw offset are both 0 here, so stored is b"" and the zero check passes whatever the section holds. The test only checks that the section exists. The reader rejects any VirtualSize other than 8 (invalid process metadata section), so mutants 4 and 16 would fail at runtime on Windows while this test still passes. This is not blocking, but one more assertion catches both of them. The head still passes with it:

import struct
...
        header, = struct.unpack_from("<I", data, 0x3C)
        optional_size, = struct.unpack_from("<H", data, header + 20)
        index = [section[0] for section in pe_sections(data)].index(b".procpar")
        virtual_size, = struct.unpack_from("<I", data, header + 24 + optional_size + index * 40 + 8)
        assert virtual_size == 8, virtual_size

I only read the libc side. Using found instead of result != nullptr keeps the duplicate check working when the empty record comes first. The new debt line says that returning a zeroed block, not null, from sceKernelGetProcParam has not been measured, which is right.

AI-assisted: yes

@xXPBSXx

xXPBSXx commented Oct 28, 2026

Copy link
Copy Markdown
Contributor Author

Thanks for the mutant run and for reading the relinker half. You are right that windows_process_parameters only checks that the .procpar section exists: the reader rejects any VirtualSize other than 8, so the 4-byte and 16-byte variants would pass the test and fail at runtime. I will add an assertion on the section's VirtualSize (8) to that test in a follow-up commit and report the test result on that head; until then the test covers the section's presence and its zero content only.

AI-assisted (Claude Code).

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
@xXPBSXx

xXPBSXx commented Oct 28, 2026

Copy link
Copy Markdown
Contributor Author

The follow-up commit d618f5cf19 adds the assertion on the .procpar VirtualSize (8) to windows_process_parameters. On a relinker-only build the test passes on the head (3 of 3 runs), and it now fails when the section is written with 4 or with 16 bytes (AssertionError: 4 and AssertionError: 16); it passes again after the change is reverted. Linux was not rerun on this head.

AI-assisted (Claude Code).

@oneandonlydean oneandonlydean self-assigned this Oct 28, 2026
@oneandonlydean
oneandonlydean merged commit b76d41e into boykopovar:main Oct 28, 2026
17 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants