Skip to content

fix(libc): validate the guest exception_ptr in _M_addref and _M_release before dereference - #23921

Open
amazedturnip wants to merge 1 commit into
boykopovar:mainfrom
amazedturnip:fix/libc-exception-pointer-null-self
Open

amazedturnip wants to merge 1 commit into
boykopovar:mainfrom
amazedturnip:fix/libc-exception-pointer-null-self

Conversation

@amazedturnip

Copy link
Copy Markdown
Contributor

What

_ZNSt15__exception_ptr13exception_ptr9_M_addrefEv_nid_postfix and _M_releaseEv_nid_postfix dereferenced self->object (and, in the libc++ branch, wrote back to self) with no null check, so a guest code path holding a null std::exception_ptr crashed the host process with SIGSEGV. Added an if (!self) return; guard at the top of both exports, in both preprocessor branches (the libstdc++ forwarder branch used by Linux g++/Windows MSYS2 CI and the libc++ GuestExceptionPointer branch), mirroring the existing null-self convention already used by sibling export __cxa_exception_type in the same file.

Game impact: guest code that copies or destroys a default-constructed (null) std::exception_ptr no longer segfaults the emulator; both calls become safe no-ops, matching how the sibling accessor treats null self.

Tested

  • Linux (WSL2, Release, Ninja): ctest -R '^guest_unwind_accessors$' passes 3 of 3 runs, plus unwind/exception neighbours (guest_libc_internal_exception_*, unwind_callback_abi_*, unwind_cursor_*) all pass.
  • Negative control: reverting the source to upstream/main makes the test fail 3 of 3 with a genuine host SIGSEGV at address 0x0 inside libc.prx (not an uncaught C++ exception).
  • Not verified: no console, Windows/MSYS2 CI branch not run locally, macOS clang libc++ branch only compiled by CI.

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

@PrivacyOnion PrivacyOnion 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.

Verified on Linux (Ubuntu 24.04, GCC 13.3, Ninja, Release) against fb70eef260:

  • Conventions: tools/check_conventions.py passed.
  • Build: guest_unwind_accessors_tests built cleanly.
  • CTest (guest_unwind_accessors):
    1/1 Test #1403: guest_unwind_accessors ...........   Passed    0.01 sec
    100% tests passed, 0 tests failed out of 1
    
  • Negative control: with core/libs/prx/libc/src/exception/ExceptionPointer.cpp reverted to main and the new tests kept, the suite fails:
    1/1 Test #1403: guest_unwind_accessors ...........***Exception: SegFault  1.84 sec
    0% tests passed, 1 tests failed out of 1
    
    Restoring the change makes it pass again.
  • Code reading:
    • Checked _ZNSt15__exception_ptr13exception_ptr9_M_addrefEv_nid_postfix and _M_releaseEv_nid_postfix in both libstdc++ and libc++ (#else) branches in core/libs/prx/libc/src/exception/ExceptionPointer.cpp for nullptr guard semantics
    • Verified that null self pointer check in ExceptionPointer.cpp:24,31,107,113 matches void return type and prevents SIGSEGV/null dereference when guest code manipulates a null or uninitialized exception pointer object
    • Verified ABI compatibility and calling convention: APS5_VABI and noexcept attributes are preserved across both function signatures in ExceptionPointer.cpp
    • Verified test coverage in core/libs/tests/GuestUnwindAccessors.cpp:132-137 (CheckExceptionPointerNullSelf) calling both addref and release exports with nullptr and verifying errno preservation
    • Checked AGENTS.md compliance: no comments introduced, no prohibited stubs or fake returns, clean conventions gate pass

Looks Good To Me

@github-actions github-actions Bot added the libc label Nov 19, 2026
@kiwe147

kiwe147 commented Nov 20, 2026

Copy link
Copy Markdown

Tested head fb70eef260, applied with git apply on main 86d2ba2 together with the other eight PRs of one batch (#23955, #24031, #24070, #24072, #24073, #24077, #24081, #24130). All nine 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 runs: ANYPS5_REQUIRE_VULKAN=1 ANYPS5_NO_SHADER_CACHE=1 ANYPS5_GPU="AMD Radeon".

test RTX 3080
guest_unwind_accessors (the new null-self check), guest_exception_handler, guest_exception_fault, guest_unwind_module_info all pass

Whole batch: the full ctest -j 12 (6496 tests) on the RTX 3080 has 4 failures, and all 4 also fail on main 86d2ba2 (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). The 2099 agc_/video tests on the iGPU ran twice. The second run fails only agc_driver_stencil_plane_memory and agc_driver_store_over_untracked, both known on main. The first run also failed agc_driver_image_table_packed_guards once; it passes 12 of 12 alone on both GPUs and did not recur.

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

AI-assisted: yes (Claude Code)

@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 adds null-self guards to both exception pointer exports in both implementation branches, with a focused regression check at head fb70eef26006684ba1ab779dd35ff7b3b023145f.

What I ran

No commands or tests were run. I inspected the prepared diff and read both changed files at the pinned head.

Results

Check Result Note
Diff and source review passed Both implementation branches inspected
Tests not run Author reports Linux runs; not independently verified

Findings

Blocking

None.

Non-blocking

None.

Not verified

The author's reported Linux test runs and negative control were not independently run. Console behavior and Windows/MSYS2 and macOS libc++ builds were not verified. Both files listed as reviewed in the manifest were read; no excluded changed files were listed.

To get this to a good state

Nothing is required.

@github-actions

Copy link
Copy Markdown

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 23395 core/libs/prx/libc/src/exception/ExceptionPointer.cpp

@ligmaSec ligmaSec left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Built the submitted exception-pointer implementation and ran the exact GuestUnwindAccessors fixture on Linux. It passed the null-self cases for _M_addref and _M_release, the errno preservation check, and the existing unwind-accessor checks.

AI-assisted: yes.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants