fix(nid): reject ELF exports that patch to different symbols under one name #5849
Loading…
Reference in a new issue
No description provided.
Delete branch "upstream-pr/5849"
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 wachi35676 on 2026-10-14T13:45:17Z.
What
ElfNidPatcher::PatchNids(core/libs/nid/src/ElfNidPatcher.cpp) renames each defined export to its NID but never checked that the new names are unique. Two exports at different addresses that map to the same name, such asfooandfoo_nid_disambig1,foo_nid_postfixandfoo_nid_disambig12, orX_nid_no_patchandX_nid_no_patch_nid_no_patch_cut, were both written as one name. The patcher printedOKand exited 0, leaving two defined dynamic symbols of the same name where a lookup reaches only one. The PE and Mach-O back ends already reject this (PeNidPatcher.cpp:172-173,MachONidPatcher.cpp:307).The ELF patcher now remembers the address (
st_value,st_shndx) of each patched export name and throwsduplicate patched export name: <name>only when the same name comes from a different address. Aliases of one address are kept, because a lookup of the name resolves to the same code either way. The Linux libc has six such groups fromGCC_GLOBAL_ALIAS(X_nid_no_patch_cut, X_nid_postfix)next toGCC_GLOBAL_ALIAS(X, X_nid_postfix)incore/libs/prx/libc/src/exception/Exports.cpp:__cxa_throw,__cxa_allocate_exception,__cxa_begin_catch,__cxa_end_catch,__cxa_rethrowand_Unwind_Resume. Removing those aliases instead would change libc's export list, which every other library reads through--preserve-export-list.The Linux libc also had one real collision, already present in the patched libc on
main.__gxx_personality_v0was exported twice at different addresses: the forwarding function inPersonality.cpp:105, and the__gxx_personality_v0_nid_no_patch_cutalias of the guest personality (Exports.cpp:21). That alias is removed:GCC_GLOBAL_ALIASonly emits it on Linux, and nothing in the tree references the name;__gxx_personality_v0_nid_postfix, andUnwind.cpp:106accepts either pointer;__gxx_personality_v0and the NID.PE and Mach-O never get these aliases, because
GCC_GLOBAL_ALIASis Linux-only and the_WIN32block defines separate forwarding functions. That is why their stricter checks pass.New
core/libs/nid/tests/test_elf_duplicates.py(ctestnid_elf_duplicates):X,X_nid_postfixandX_nid_no_patch_cutat one address must patch, giving {X,X, nid(X)} at that address;_nid_no_patch_cutat another address must be rejected;Tested
Rebased on
main44df21632.nid_patcherruns for all 126 libraries). With the previous commit of this PR,Patching NIDs for libcfails withduplicate patched export name: __cxa_throw.ctest -E "agc_|video_out": 647 of 648 pass, includingnid_elf_duplicatesand the othernid_*tests. The one failure,guest_named_shm_concurrency("unlink of a read-only shared object did not return EACCES"), comes from running as root in WSL. The same binary passes as a normal user, and this change does not touch shared memory.test_elf_duplicates.pyagainstmain's patcher: 4 failures (the 3 collisions and the different-address case). Against the previous commit: 1 failure (the same-address case). With this change: 4/4 pass.agc_driver_gpu_indirect_arguments_misaligned_writer_ahead_tests, which fails to compile onmainwith MinGW, "requested alignment 65536 exceeds object file maximum 8192", and is not touched here); 424 of 424guest_*,libkernel*,nid*,libc*and stream tests pass, includingnid_elf_duplicatesandpatched_libraries.python tools/check_conventions.py --base origin/mainpasses.The first version of this PR said no library had such a pair. That was wrong for the Linux libc: I had only built it on Windows. Thanks to @adriadam10 for catching it.
Checklist
main; no other open PR implements the same functions (#4721 touches other files incore/libs/nid)NotImplemented_nid_no_patch); silent stubs are listed under Silent stubs in their library's technical debt file.mdfiles or images added to the repository (attach them to this PR)🤖 Generated with Claude Code
Imported from boykopovar/AnyPS5. Originally posted by Dextroid17 on 2026-10-15T02:09:47Z.
I ran this PR's test on Linux.
Setup: this PR's diff applied to main at
8cd937e29(the PR's base is 485 commits behind it), default CMake build (CMAKE_BUILD_TYPE empty, unoptimized), clean environment.ctest -R '^nid_elf_duplicates$', 3 runs each. The test did not open a GPU device, so this is a CPU-only run.test_exports_patched_to_one_name_are_rejectedfail withAssertionError: 0 != 2(the patcher exits 0 with no error instead of rejecting the duplicate).So the new test catches the old behaviour here. I did not check Windows or macOS.
AI-assisted: yes
Imported from boykopovar/AnyPS5. Originally posted by github-actions[bot] on 2026-10-15T12:10:26Z.
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.
main)core/libs/prx/libc/src/exception/Exports.cppL16-22Some 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.
core/libs/CMakeLists.txtcore/libs/CMakeLists.txtcore/libs/CMakeLists.txtcore/libs/CMakeLists.txtImported from boykopovar/AnyPS5. Originally posted by adriadam10 on 2026-10-15T13:43:05Z.
Review: CHANGES_REQUESTED
Thanks for the check, the idea matches the PE and Mach-O back ends. But it breaks the Linux build of libc on current main:
Patching NIDs for libcfails withduplicate patched export name: __cxa_throw. libc defines__cxa_throw,__cxa_throw_nid_postfixand__cxa_throw_nid_no_patch_cutas aliases of one symbol (core/libs/prx/libc/src/exception/Exports.cpp:17and:27), and all three patch to__cxa_throw. The "no library in the tree has such a pair" claim in the description doesn't hold for the Linux libc (nm -Donlibs/unpatched/libc.prxshows the three at0x969b0).nid_patcherruns for every library) and fix the fallout: either remove the redundant alias, or make the check skip exports that resolve to the same address (samest_value) since those are not ambiguous lookups. Say which one you choose and why, and keep the test cases for the real collisions plus one for same-address aliases.Imported from boykopovar/AnyPS5. Originally posted by wachi35676 on 2026-10-15T19:27:29Z.
Thanks, you're right. I had only built the libraries on Windows, and the description's claim was wrong for the Linux libc. Small correction to the symbol list:
__cxa_throwand__cxa_throw_nid_no_patch_cutboth become__cxa_throw, while__cxa_throw_nid_postfixbecomes its NID (vkuuLfhnSZI).I chose the same-address rule. The check now keeps
st_valueandst_shndxfor each patched name and throws only when the name comes from a different address. Aliases of one address resolve to the same code, and removing them would change libc's export list, which the other libraries read through--preserve-export-list.That alone wasn't enough, though. The Linux libc also exported
__gxx_personality_v0twice at different addresses, and the patched libc onmainhas both today:Personality.cpp:105;__gxx_personality_v0_nid_no_patch_cutalias of the guest personality (Exports.cpp:21).So I also removed that one alias. It is Linux-only and unreferenced, the forwarding function passes its arguments straight through,
Unwind.cpp:106accepts either pointer, and the PE libc already exports exactly this pair.Tests.
test_elf_duplicates.pycovers the three real collisions, a same-address alias case that must patch, and a different-address case that must be rejected.main7d9a30804Linux (WSL Ubuntu 24.04, GCC 13.3), rebased on
44df21632:nid_patcherrun on all 126 libraries.ctest -E "agc_|video_out": 647/648 pass. The exception,guest_named_shm_concurrency, fails only because WSL runs as root; the same binary passes as a normal user.Windows: 424/424 related tests pass.
I've rewritten the description to match.
AI-assisted: yes (Claude Code); results above were run on this machine.
Imported from boykopovar/AnyPS5. Originally posted by adriadam10 on 2026-10-16T18:30:13Z.
Review: APPROVED
Re-checked the new head: the same-address rule fixes the libc failure (
Patching NIDs for libcno longer trips on the__cxa_*alias groups), and dropping__gxx_personality_v0_nid_no_patch_cutremoves the one real collision. Core/libs build and ctest pass on Linux (incl.nid_elf_duplicates); the test fails without the patcher change. Merging once the full Build is green.