Skip to content

fix(libkernel): safely probe the symbol name of dlsym - #20622

Merged
oneandonlydean merged 2 commits into
boykopovar:mainfrom
milescoviello:fix/kernel-probe-dlsym-name
Nov 25, 2026
Merged

oneandonlydean merged 2 commits into
boykopovar:mainfrom
milescoviello:fix/kernel-probe-dlsym-name

Conversation

@milescoviello

@milescoviello milescoviello commented Nov 15, 2026 •

Copy link
Copy Markdown
Contributor

What

dlsym checked *name and then handed the guest pointer to the module lookups, the host dlsym fallbacks and Nid::ComputeNid, which all read the string in place. A non-null name pointing at unmapped memory faulted the host.

The name is now copied into a host string with CopyGuestString_nid_no_patch first, and every lookup uses the copy. The cap is 64 KiB, far above any real symbol name. An unreadable name returns null and sets a dlerror message ("dlsym: unreadable symbol name"), the same failure channel dlsym uses for an empty name or a missing symbol. The lookup order, the default-scope and -2 handle cases, the NID and #guest fallbacks and the results for valid names are unchanged.

dlsym runs when a title resolves symbols, not per frame, so the copy costs nothing that matters. The debt entry goes in a fragment under docs/dev/debt/changes/ and records this as unmeasured.

Tested

Linux (Ubuntu 26.04.1), GCC 15.2, Release.

11-24, 09131a8ceb: test pin from Helldiverr's run. Helldiverr's Windows run (Ryzen 9 9950X3D, Windows 11 Pro 26300, WinLibs GCC 15.2, this head's parent 2a769e4875 merged onto main 4e86679b36): guest_dynamic_loader passes in 3 of 3 runs, with the source reverted it segfaults on the unmapped name, and 3 of 4 mutations are caught. The survivor: an unreadable name and an empty name both return null with a dlerror message, so ignoring a failed copy still passed. 09131a8ceb adds their pin: the message after the unmapped name must say unreadable. Linux (Ubuntu 26.04.1, GCC 15.2, Release), the head merged onto main adacf0bd83: guest_dynamic_loader passes in 3 of 3 runs, and with a failed copy ignored it aborts.

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 and the licence notice of code adapted from upstream (see AGENTS.md)
  • 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: n/a
  • AI-assisted: yes

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@github-actions

github-actions Bot commented Nov 15, 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 18112 dlsym
PR 18190 dlsym
PR 24322 dlsym
PR 24917 dlsym

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 11661 core/libs/prx/libkernel/Module/src/DynamicLoader.cpp
PR 11689 core/libs/prx/libkernel/Module/src/DynamicLoader.cpp
PR 11698 core/libs/prx/libkernel/Module/src/DynamicLoader.cpp
PR 18190 core/libs/prx/libkernel/Module/src/DynamicLoader.cpp

@maskym

maskym commented Nov 17, 2026

Copy link
Copy Markdown
Contributor

Windows result for head 2a769e4875, merged alone into main 33cbb0b388 (the PR's Tested section names no Windows run).

Host: Windows 11 26300, Intel i7-12700K, WinLibs GCC 15.2.0 (x86_64-ucrt-posix-seh, r6), Release, BUILD_TESTING=ON, Ninja. main itself fails to compile one test on Windows at this commit (UploadCacheReadOnlyFlush.cpp:177, setenv; #19899 fixes it), so the tree was built with ninja -k 0. This PR adds no other build failure.

guest_dynamic_loader, the test this PR adds or extends, passes 3 of 3 runs. Not run on a PS5.

AI-assisted: yes (Claude Code), the runs and the numbers above are from this machine.

@milescoviello

Copy link
Copy Markdown
Contributor Author

Thanks @maskym for the Windows run. I've added it to the Tested section, linked to your comment.

This was referenced Nov 21, 2026
@kiwe147

kiwe147 commented Nov 21, 2026

Copy link
Copy Markdown

Tested head 2a769e4875, applied with git apply on main fd4aeed483 together with the other 29 PRs of one batch (#24470, #24601, #17439, #17546, #21117, #21261, #21741, #21995, #22067, #22398, #23111, #23346, #23864, #24377, #24475, #24556, #14106, #14109, #19966, #20050, #20161, #21237, #22215, #22764, #22884, #22938, #23745, #24399, #24460). All applied without conflicts and the whole tree built.

Setup: Windows 11 Pro 10.0.26200, Ryzen 7 9800X3D, WinLibs GCC 15.2.0 r7, Release, BUILD_TESTING=ON. GPUs: NVIDIA GeForce RTX 3080 (driver 621.24) and the AMD Radeon iGPU of the CPU (gfx1036, driver 32.0.21036.18). No validation layer. iGPU run: ANYPS5_REQUIRE_VULKAN=1 ANYPS5_NO_SHADER_CACHE=1 ANYPS5_GPU="AMD Radeon", agc_/video tests only.

tests RTX 3080 iGPU
guest_dynamic_loader, guest_dynamic_loader_variant_names (2 tests, matched by name to the test file this PR changes) all pass outside the iGPU subset

Whole batch: the full ctest -j 12 (6525 tests) on the RTX 3080 has 5 failing tests; the 4 that also fail on main fd4aeed483 are agc_driver_stencil_plane_memory, agc_driver_msaa_dcc_unwatch, agc_storage_write_back_host_retile, and pr_overlap_names, which needs a newer Python than my 3.10. Traced to other PRs of the batch, each by reverting it and testing again: #21117 (guest_pad); none is in this PR's area. The 2125 agc_/video tests on the iGPU have 1 failing: agc_driver_stencil_plane_memory (agc_driver_stencil_plane_memory also fails on main).

This is one combined tree, not a run of this PR alone.

AI-assisted: yes (Claude Code)

@milescoviello

Copy link
Copy Markdown
Contributor Author

Thanks @kiwe147 for including this one in the batch. The guest_dynamic_loader tests passing is a useful Windows data point, and I added it to the description, noted as a combined batch tree rather than this PR alone.

@MarcZX10 MarcZX10 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 — looks good

Nothing blocks this change; the inspected implementation copies the guest symbol name before lookup and handles unreadable and empty names through dlerror.

What I ran

Reviewed head 2a769e48751102b44b4c585a539eba7920fb785f using the prepared diff and repo_read.py. No commands or tests were run.

Results

Check Result Note
Diff and targeted source review passed Three changed files read
Tests not run Results in the description are author supplied

Findings

Blocking

None.

Non-blocking

None.

Not verified

I did not run tests or independently verify the reported platform runs. All changed files listed in the manifest were read: core/libs/prx/libkernel/Module/src/DynamicLoader.cpp, core/libs/tests/GuestDynamicLoader.cpp, and docs/dev/debt/changes/fix-kernel-probe-dlsym-name.md. Also read targeted context in core/libs/prx/libc/src/General.cpp. No changed files were excluded according to the manifest.

To get this to a good state

Nothing is required.

@Helldiverr

Copy link
Copy Markdown
Contributor

Windows run at 2a769e4875, merged onto main 4e86679b36. The fix works on Windows:

  • guest_dynamic_loader passes 3 out of 3 runs on the merge.
  • With the source reverted, it segfaults on the new unmapped-name case.
  • Three of the four mutations are caught.

The survivor shows that the test cannot tell an unreadable name from an empty one. A one-line pin closes that.

Existing runs

There are two Windows runs at 2a769e4, and both only report passes. maskym ran the PR alone (guest_dynamic_loader, 3 of 3 pass). kiwe147 ran it in a combined tree with 29 other PRs (pass). This run adds the negative control, the mutations and a probe for the survivor.

Code

dlsym_nid_postfix first copies the name with CopyGuestString_nid_no_patch(guestName, 65536, copied) (line 536). All the later lookups use that copy. A null name still reaches the existing empty-name branch (line 538). No problems found.

Tests

The focused set is ctest --test-dir build -R '^guest_dynamic_loader$' -V. guest_dynamic_loader_variant_names uses another executable and another source file, so it is not in the set.

Tree Result
main 1/1 passed
merge, DynamicLoader.cpp from main, PR test kept 1/1 failed: SegFault (dlsym(module, 0x10))
merge, run 1 1/1 passed (0.27 s)
merge, run 2 1/1 passed (0.31 s)
merge, run 3 1/1 passed (0.29 s)

Mutations

Each mutation rebuilds the merge with one change at DynamicLoader.cpp line 536, then runs the focused set. A failing Require aborts with 0xc0000409.

# Change Result
M1 the unreadable branch returns null without calling Error caught; abort
M2 the unreadable branch returns handle instead of null caught; abort
M3 copy limit 8 bytes instead of 65536 caught; abort (the GuestModuleAdd lookups fail)
M4 != 0 changed to < 0, so a failed copy is ignored survives

In M4, copied stays empty, so the call falls through to the empty-name branch. That branch also returns null and also sets a dlerror message. The test only checks dlerror() != nullptr after the unmapped name (test line 170), so both paths look the same.

Probe

This pin replaces test line 170:

    const char* unreadable = dlerror_nid_postfix();
    Require(unreadable != nullptr && std::strstr(unreadable, "unreadable") != nullptr);

With the pin, the merge passes (1/1, 0.65 s) and M4 is caught (abort, 0xc0000409). The test already includes <cstring> and uses std::strstr on dlerror text in FailsAsUnrelinked.

Checks

  • check_conventions --base 4e86679b36 passed.
  • debt_fold --check passed. debt_fold --show libkernel places the dlsym symbol name entry.

Host

  • CPU: Ryzen 9 9950X3D
  • OS: Windows 11 Pro 26300
  • Toolchain: WinLibs GCC 15.2, Release
  • Commits: head 2a769e4, merged cleanly onto main 4e86679 as bca24a3f91.

AI-assisted: yes

An unreadable name and an empty name both return null and set a dlerror
message, so the test could not tell them apart. Check that the message
for an unreadable name says so. From Helldiverr's Windows mutation run
on boykopovar#20622.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@milescoviello

Copy link
Copy Markdown
Contributor Author

Thanks @Helldiverr for the Windows run and the mutations. 09131a8ceb adds your pin, so the test now tells an unreadable name from an empty one by the dlerror text. On Linux, merged onto current main, guest_dynamic_loader passes in 3 of 3 runs, and with your M4 (< 0) it aborts. Your run and the pin are now in Tested.

@MarcZX10 MarcZX10 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 — looks good

The reviewed change safely copies the guest symbol name before lookup and adds coverage for unreadable and empty names; no defect was found in the files read.

What I ran

Reviewed head 09131a8ceb3d55e58b24a545496bef7ac0f1f88d using repo_read.py to inspect the prepared diff and relevant source ranges. No tests were run.

Results

Check Result Note
Diff and targeted code review passed Changed behavior and relevant helper inspected
Tests not run Reported runs are author supplied

Findings

Blocking

None.

Non-blocking

None.

Not verified

No tests were run independently. The review bundle and all changed files were read: DynamicLoader.cpp, GuestDynamicLoader.cpp, and fix-kernel-probe-dlsym-name.md. No changed files were excluded. Reported Linux and Windows runs were not independently verified.

To get this to a good state

Nothing is required.

@oneandonlydean
oneandonlydean merged commit 71780d8 into boykopovar:main Nov 25, 2026
16 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.

6 participants