Skip to content

test(libc): cover std::uncaught_exceptions outside and inside a catch block - #21236

Open
MarcZX10 wants to merge 4 commits into
boykopovar:mainfrom
MarcZX10:test/libc-uncaught-exceptions
Open

MarcZX10 wants to merge 4 commits into
boykopovar:mainfrom
MarcZX10:test/libc-uncaught-exceptions

Conversation

@MarcZX10

Copy link
Copy Markdown
Contributor

What

  • New test guest_uncaught_exceptions (core/libs/tests/GuestUncaughtExceptions.cpp), registered after guest_bsearch_packed in core/libs/prx/libc/CMakeLists.txt. Two commits:
    • _ZSt19uncaught_exceptionsv (std::uncaught_exceptions) returns 0 outside any exception.
    • It also returns 0 inside a catch block, where the exception is no longer uncaught.
  • Implementation: core/libs/prx/libc/src/exception/Runtime.cpp:392, which forwards to __cxa_uncaught_exceptions. No test names it, and no open or merged PR does (checked the PR titles and bodies and the test sources).
  • Limitation: the test throws a host int to reach the catch block. It checks the zero result the standard requires there; it does not exercise the count inside a destructor during unwinding, which needs a guest-side throw and is not covered here.

Tested

  • Not built and not run.
  • git diff --check upstream/main...HEAD: passed. python3 tools/check_conventions.py --base upstream/main: passed.

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
  • Unimplemented paths throw; 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
  • 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

@oneandonlydean

Copy link
Copy Markdown
Collaborator

The test body looks right (std::uncaught_exceptions forwards to __cxa_uncaught_exceptions at Runtime.cpp:392, and zero outside and inside a catch is the standard's answer). The CMake block is missing what every neighbouring libc test has, so it will fail on Windows: it needs configure_windows_unwind(guest_uncaught_exceptions_tests) (the test throws a host int), a set_tests_properties(guest_uncaught_exceptions PROPERTIES TIMEOUT 20), and the if(WIN32) ENVIRONMENT_MODIFICATION "PATH=path_list_prepend:$<TARGET_FILE_DIR:libc>" block (the exe links the libc shared library and cannot start without it), as in guest_stream_purge and guest_bsearch_packed. The block is also inserted between guest_bsearch_packed's add_test and its set_tests_properties; please put it after that block's endif().

@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 8f2134e822:

  • Conventions: tools/check_conventions.py passed; git diff --check clean.
  • Build: guest_uncaught_exceptions_tests built cleanly.
  • CTest (guest_uncaught_exceptions):
    1/1 Test #958: guest_uncaught_exceptions ........   Passed    0.01 sec
    100% tests passed, 0 tests failed out of 1
    
  • Code reading:
    • Checked core/libs/tests/GuestUncaughtExceptions.cpp:1-14 for pointer safety, exception safety, and C++20 portability when invoking _ZSt19uncaught_exceptionsv_nid_postfix() before and inside a catch (int) handler.
    • Checked core/libs/prx/libc/CMakeLists.txt:74-88 target registration, linking against libc, unwind configuration, test timeout, and Windows DLL PATH setup against adjacent libc test targets.
    • Verified AGENTS.md compliance: no code comments in core/libs/tests/GuestUncaughtExceptions.cpp or core/libs/prx/libc/CMakeLists.txt, no extra untracked files, and tools/check_conventions.py passed.
    • Verified Linux build and CTest execution of guest_uncaught_exceptions passed.
  • Concerns:
    • core/libs/prx/libc/CMakeLists.txt:79-82: guest_uncaught_exceptions_tests is spliced between add_test(NAME guest_bsearch_packed ...) and set_tests_properties(guest_bsearch_packed ...), and is missing configure_windows_unwind(guest_uncaught_exceptions_tests), set_tests_properties(guest_uncaught_exceptions PROPERTIES TIMEOUT ...), and the if(WIN32) set_tests_properties(guest_uncaught_exceptions PROPERTIES ENVIRONMENT_MODIFICATION "PATH=path_list_prepend:$<TARGET_FILE_DIR:libc>") endif() block that every other libc-linked test in this file sets. On Windows, CTest runs guest_uncaught_exceptions_tests.exe without $<TARGET_FILE_DIR:libc> on PATH (failing to locate libc.dll at startup) and without the Windows unwind configuration applied to libc test executables.

Requesting changes for the points above.

@github-actions

github-actions Bot commented Nov 16, 2026 •

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 7715 core/libs/prx/libc/CMakeLists.txt
PR 8965 core/libs/prx/libc/CMakeLists.txt
PR 12682 core/libs/prx/libc/CMakeLists.txt
PR 12688 core/libs/prx/libc/CMakeLists.txt
PR 12700 core/libs/prx/libc/CMakeLists.txt
PR 12717 core/libs/prx/libc/CMakeLists.txt
PR 12849 core/libs/prx/libc/CMakeLists.txt
PR 12965 core/libs/prx/libc/CMakeLists.txt
PR 23395 core/libs/prx/libc/CMakeLists.txt
PR 26796 core/libs/prx/libc/CMakeLists.txt
PR 27540 core/libs/prx/libc/CMakeLists.txt
PR 27568 core/libs/prx/libc/CMakeLists.txt
PR 27569 core/libs/prx/libc/CMakeLists.txt
PR 27646 core/libs/prx/libc/CMakeLists.txt
PR 27815 core/libs/prx/libc/CMakeLists.txt
PR 27823 core/libs/prx/libc/CMakeLists.txt
PR 28071 core/libs/prx/libc/CMakeLists.txt
PR 28808 core/libs/prx/libc/CMakeLists.txt
PR 28839 core/libs/prx/libc/CMakeLists.txt
PR 29099 core/libs/prx/libc/CMakeLists.txt
PR 29111 core/libs/prx/libc/CMakeLists.txt
PR 29272 core/libs/prx/libc/CMakeLists.txt
PR 29273 core/libs/prx/libc/CMakeLists.txt

@MarcZX10
MarcZX10 force-pushed the test/libc-uncaught-exceptions branch from 8f2134e to db6f88e Compare November 19, 2026 16:16

@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 db6f88ede2:

  • Conventions: tools/check_conventions.py passed.
  • Build: guest_uncaught_exceptions_tests built cleanly.
  • CTest (guest_uncaught_exceptions):
    1/1 Test #1073: guest_uncaught_exceptions ........   Passed    0.02 sec
    100% tests passed, 0 tests failed out of 1
    
  • Code reading:
    • Verified core/libs/tests/GuestUncaughtExceptions.cpp:1-14 correctly tests _ZSt19uncaught_exceptionsv_nid_postfix outside and inside a catch(int) block per the C++ standard.
    • Verified core/libs/prx/libc/CMakeLists.txt:89-93 adds guest_uncaught_exceptions_tests and calls configure_windows_unwind.
    • Checked core/libs/prx/libc/CMakeLists.txt:89-93 target placement and test properties against neighbouring test targets (guest_bsearch_packed, guest_stream_purge).
    • Verified test execution, linkage with libc, and absence of comments or technical debt violations per AGENTS.md.
  • Concerns:
    • core/libs/prx/libc/CMakeLists.txt:89-93: guest_uncaught_exceptions_tests is spliced between add_test(NAME guest_bsearch_packed ...) and set_tests_properties(guest_bsearch_packed ...), and guest_uncaught_exceptions is missing set_tests_properties(guest_uncaught_exceptions PROPERTIES TIMEOUT 20) and the if(WIN32) set_tests_properties(guest_uncaught_exceptions PROPERTIES ENVIRONMENT_MODIFICATION "PATH=path_list_prepend:$<TARGET_FILE_DIR:libc>") endif() block required for Windows CTest runs to locate libc.dll.

Requesting changes for the points above.

@oneandonlydean

Copy link
Copy Markdown
Collaborator

@MarcZX10 the test itself is right, but core/libs/prx/libc/CMakeLists.txt on db6f88e still needs the same three fixes PrivacyOnion listed:

  1. Add set_tests_properties(guest_uncaught_exceptions PROPERTIES TIMEOUT 20).
  2. Add the if(WIN32) ENVIRONMENT_MODIFICATION "PATH=path_list_prepend:$<TARGET_FILE_DIR:libc>" block, so the Windows exe finds libc.dll.
  3. Move the new block after the endif() of the guest_bsearch_packed block. It is spliced between that add_test and its set_tests_properties now.
    The configure_windows_unwind addition looks good.

@kiwe147

kiwe147 commented Nov 21, 2026

Copy link
Copy Markdown

Tested head db6f88ede2, applied with git apply on main fd4aeed483 together with the other 29 PRs of one batch (#24576, #24603, #14196, #14527, #17718, #18671, #22753, #22792, #23503, #24245, #24480, #24553, #24714, #21765, #21987, #22030, #22107, #22397, #22571, #23158, #23816, #24067, #18401, #19101, #19398, #19657, #21563, #24584, #24628). 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_uncaught_exceptions (1 test, matched by name to the test file this PR changes) all pass outside the iGPU subset

Whole batch: the full ctest -j 12 (6523 tests) on the RTX 3080 has 7 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. Intermittent in this run (pass when rerun alone, or known flaky): agc_driver_store_over_untracked, agc_driver_store_over_untracked, agc_storage_write_back_host_retile. Traced to other PRs of the batch, each by reverting it and testing again: #19101 (guest_video_out_window_margins_bounds); none is in this PR's area. Failing tests I could not trace to a single PR (reverting the likely candidates did not clear them): agc_driver_image_gather_lod. The 2125 agc_/video tests on the iGPU have 5 failing: agc_driver_image_gather_lod, agc_driver_stencil_plane_memory, agc_driver_store_over_untracked, agc_storage_write_back_host_retile, guest_video_out_window_margins_bounds (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)

Give guest_uncaught_exceptions the 20s timeout and the Windows PATH
environment modification that its neighbouring libc tests set, and move
its block after guest_bsearch_packed's properties instead of splicing it
between that test's add_test and set_tests_properties.

Co-Authored-By: Claude Haiku 5.5 <noreply@anthropic.com>
@MarcZX10

Copy link
Copy Markdown
Contributor Author

@oneandonlydean @PrivacyOnion

Pushed 30deb597ec to fork/test/libc-uncaught-exceptions (fast-forward from db6f88ede2). Only core/libs/prx/libc/CMakeLists.txt changed:

  1. Added set_tests_properties(guest_uncaught_exceptions PROPERTIES TIMEOUT 20).
  2. Added the if(WIN32) ENVIRONMENT_MODIFICATION "PATH=path_list_prepend:$<TARGET_FILE_DIR:libc>" block for guest_uncaught_exceptions.
  3. Moved the new test block after the endif() of the guest_bsearch_packed block. guest_bsearch_packed's set_tests_properties and its Windows block now directly follow its add_test.

configure_windows_unwind(guest_uncaught_exceptions_tests) was already there and is unchanged. The test source is unchanged.

Verified: git diff --check clean; tools/check_conventions.py --base origin/main reports all checks passed.
Not verified: no CMake configure or build was run on this machine (full builds take hours here), so the Windows CTest run is not checked on my side.

@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 30deb597ec:

  • Conventions: tools/check_conventions.py passed.
  • Build: guest_uncaught_exceptions_tests built cleanly.
  • CTest (guest_uncaught_exceptions):
    1/1 Test #1139: guest_uncaught_exceptions ........   Passed    0.00 sec
    100% tests passed, 0 tests failed out of 1
    
  • Code reading:
    • Verified core/libs/tests/GuestUncaughtExceptions.cpp correctly calls _ZSt19uncaught_exceptionsv_nid_postfix outside an active exception and inside a catch block, validating the C++ standard return value of 0.
    • Verified core/libs/prx/libc/CMakeLists.txt:95-104 correctly links libc, includes configure_windows_unwind for the throwing test, sets TIMEOUT 20, and adds the Windows ENVIRONMENT_MODIFICATION block.
    • Confirmed that the new test target guest_uncaught_exceptions_tests is properly positioned after guest_bsearch_packed's endif() block, resolving the previous splicing defect requested by maintainer oneandonlydean.
    • Verified AGENTS.md conventions compliance: zero comments in code, no extraneous files added, and conventions check passed.
    • Verified cross-platform safety: clean execution and test passes on Linux x86_64 and Windows 11.

Looks Good To Me

@Helldiverr

Copy link
Copy Markdown
Contributor

Windows run: guest_uncaught_exceptions passes three times on the merge, next to windows_exceptions from main. Making _ZSt19uncaught_exceptionsv return 1 fails it. Of 3 mutations, the new test catches 1: a stub that always returns 0 survives every test here, and dropping the increment at the throw is caught only by windows_exceptions. A local destructor-during-unwind case, using a host throw, catches both and passes three times.

Existing runs.

  • PrivacyOnion ran the test on Linux at the head, at 8f2134e822 and at db6f88ede2, pass-only.
  • kiwe147 ran it on Windows at db6f88ede2, applied with 29 other PRs onto fd4aeed483, pass-only.
  • The head 30deb597ec changes only CMakeLists.txt since then. The author did not build it.

What this run adds.

  • A Windows run at the head, merged onto current main.
  • A negative control and 3 mutations, each also run against windows_exceptions.
  • A local destructor case, with the mutations rerun against it.

Code.

  • _ZSt19uncaught_exceptionsv_nid_postfix (Runtime.cpp:408 on main) returns the globals.uncaught counter. ThrowFromSite increments it (:316) and __cxa_begin_catch decrements it (:349).
  • oneandonlydean pointed out that the test body is right: 0 outside an exception and 0 inside a catch is what the standard requires. Both values are also what a stub returning 0 gives, and that stub passes here.
  • The counter is never above 0 at either check point. So the test also passes when the throw does not increment it, because the decrement at :349 is guarded by if (globals.uncaught).
  • On main, windows_exceptions (WindowsExceptions.cpp:270-284) checks the count in a destructor, but through std::uncaught_exceptions. On Windows, that symbol is bound to NativeStdUncaughtExceptions (Exports.cpp:80-81), not to the export this PR tests. That is why windows_exceptions does not fail the negative control.
  • The CMake points oneandonlydean and PrivacyOnion raised (TIMEOUT, the Windows PATH block, and the splice into guest_bsearch_packed) are fixed at CMakeLists.txt:95-104 in the head.

PR body.

  • What cites Runtime.cpp:392. On main 0789312659, the function is at :408.
  • The limitation says the destructor case "needs a guest-side throw". A host throw already goes through the prx runtime: Exports.cpp:24 aliases __cxa_throw to __cxa_throw_nid_postfix, and on Windows Exports.cpp:52-54 binds it to ThrowFromSite. Dropping the decrement fails the new test, which shows that the host throw 1 reaches __cxa_begin_catch_nid_postfix. On main, ExceptionRuntime.cpp:26 asserts __cxa_uncaught_exceptions_nid_postfix() > 0 in a destructor after a host throw. That test is registered for UNIX only, and I did not run it.

Suggestion. I added this case locally only to GuestUncaughtExceptions.cpp, with the struct after Require and the block before return 0;. It passes three times on the merge, and it fails both the stub and the dropped increment. I ran it on Windows only.

struct Probe {
    int& seen;
    ~Probe() { seen = _ZSt19uncaught_exceptionsv_nid_postfix(); }
};
    int seen = -1;
    try {
        Probe probe{seen};
        throw 2;
    } catch (int) {
    }
    Require(seen == 1);
    Require(_ZSt19uncaught_exceptionsv_nid_postfix() == 0);

Negative control and mutations. Each change ran once on the merge, against guest_uncaught_exceptions (new) and windows_exceptions (main). The last column is guest_uncaught_exceptions with the local case.

Change (Runtime.cpp) new windows_exceptions new, with local case
Negative control: _ZSt19uncaught_exceptionsv returns 1 (:408) fails passes not run
_ZSt19uncaught_exceptionsv returns 0 (:408) passes passes fails
__cxa_begin_catch does not decrement (:349) fails fails not run
ThrowFromSite does not increment (:316) passes fails fails

Tests.

test main merge 1 merge 2 merge 3
guest_uncaught_exceptions not present pass pass pass
windows_exceptions pass pass pass pass
guest_uncaught_exceptions with the local case not present pass pass pass

On the merge, tools/check_conventions.py --base upstream/main passes with no findings, and tools/debt_fold.py --check exits 0.

  • CPU: Ryzen 9 9950X3D
  • OS: Windows 11 Pro 26300
  • Toolchain: WinLibs GCC 15.2, Release
  • Commits: head 30deb597ec, merged onto main 0789312659 as 12fb630ee9

AI-assisted: yes

@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 and ran the added GuestUncaughtExceptions fixture on Linux against the existing libc PRX. Both assertions passed. The fixture checks the zero count before the throw and after its handler has been entered.

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

Projects

None yet

Development

Successfully merging this pull request may close these issues.

6 participants