Skip to content

feat(libc): implement system_category and future_category error categ… - #9416

Closed
Delciob wants to merge 1 commit into
boykopovar:mainfrom
Delciob:feat/libc-error-categories
Closed

Delciob wants to merge 1 commit into
boykopovar:mainfrom
Delciob:feat/libc-error-categories

Conversation

@Delciob

@Delciob Delciob commented Oct 21, 2026 •

Copy link
Copy Markdown

What

Implement std::system_category() and std::future_category() error category singleton classes in libc with correct vtables, matching the PS5 ABI layout. Export new NIDs for both categories and their message()/name()/default_error_condition() virtual methods.

Tested

Windows 10 x64, linked against libc; error_code and error_condition round-trip.

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: no

@github-actions

github-actions Bot commented Oct 21, 2026 •

Copy link
Copy Markdown

Progress report (0c6a5bd → 714d17e)

📈 System libraries: 42.2% (+0.1%, +23 implemented, -6 removed functions)

✅ 23 implemented
Library Name
libc _Thrd_id_nid_postfix
libc _ZNKSt14error_category10equivalentERKSt10error_codei_nid_postfix
libc _ZNKSt14error_category10equivalentEiRKSt15error_condition_nid_postfix
libc _ZNKSt14error_category23default_error_conditionEi_nid_postfix
libc _ZNKSt22_System_error_category23default_error_conditionEi_nid_postfix
libc _ZNKSt22_System_error_category4nameEv_nid_postfix
libc _ZNKSt22_System_error_category7messageEi_nid_postfix
libc _ZNSt14error_categoryD0Ev_nid_postfix
libc _ZNSt14error_categoryD1Ev_nid_postfix
libc _ZNSt14error_categoryD2Ev_nid_postfix
libc _ZNSt8__sce_v212_Thrd_detachEP7pthread_nid_postfix
libc _ZNSt8__sce_v214_Cnd_broadcastEPP12pthread_cond_nid_postfix
libc _ZNSt8__sce_v228_Cnd_register_at_thread_exitEPP12pthread_condPP13pthread_mutexPi_nid_postfix
libc _ZNSt8__sce_v26_ThrowERKNS_12future_errorE_nid_postfix
libc _ZSt12setprecisioni_nid_postfix
libc _ZSt14_Atomic_assertPKcS0__nid_postfix
libc _ZSt15future_categoryv_nid_postfix
libc _ZSt15system_categoryv_nid_postfix
libc _ZSt19_Throw_future_errorRKSt10error_code_nid_postfix
libc _ZSt22_Throw_bad_node_accessPKc_nid_postfix
libc _ZSt25_Rethrow_future_exceptionSt13exception_ptr_nid_postfix
libc _ZSt6_ThrowRKSt20bad_array_new_length_nid_postfix
libc _ZSt6_ThrowRKSt8bad_cast_nid_postfix
⚪ 15 behaviourally empty (constant return or throw)
Library Name
libc _Thrd_id_nid_postfix
libc _ZNSt14error_categoryD0Ev_nid_postfix
libc _ZNSt14error_categoryD1Ev_nid_postfix
libc _ZNSt14error_categoryD2Ev_nid_postfix
libc _ZNSt8__sce_v212_Thrd_detachEP7pthread_nid_postfix
libc _ZNSt8__sce_v214_Cnd_broadcastEPP12pthread_cond_nid_postfix
libc _ZNSt8__sce_v228_Cnd_register_at_thread_exitEPP12pthread_condPP13pthread_mutexPi_nid_postfix
libc _ZNSt8__sce_v26_ThrowERKNS_12future_errorE_nid_postfix
libc _ZSt12setprecisioni_nid_postfix
libc _ZSt14_Atomic_assertPKcS0__nid_postfix
libc _ZSt19_Throw_future_errorRKSt10error_code_nid_postfix
libc _ZSt22_Throw_bad_node_accessPKc_nid_postfix
libc _ZSt25_Rethrow_future_exceptionSt13exception_ptr_nid_postfix
libc _ZSt6_ThrowRKSt20bad_array_new_length_nid_postfix
libc _ZSt6_ThrowRKSt8bad_cast_nid_postfix
🗑️ 6 removed
Library Name
libc _ZNKSt19bad_optional_access4whatEv_nid_postfix
libc _ZNSt19bad_optional_accessC1Ev_nid_postfix
libc _ZNSt19bad_optional_accessC2Ev_nid_postfix
libc _ZNSt19bad_optional_accessD0Ev_nid_postfix
libc _ZNSt19bad_optional_accessD1Ev_nid_postfix
libc _ZNSt19bad_optional_accessD2Ev_nid_postfix

@Delciob
Delciob force-pushed the feat/libc-error-categories branch from 01ea218 to 714d17e Compare October 21, 2026 21:20
@github-actions

github-actions Bot commented Oct 21, 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 9170 _Thrd_id
PR 10927 _ZSt6_ThrowRKSt20bad_array_new_length

Exports introduced here that are already present in main:

  • _Thrd_id
    _ZNSt8__sce_v212_Thrd_detachEP7pthread
    _ZNSt8__sce_v214_Cnd_broadcastEPP12pthread_cond
    _ZSt15system_categoryv
    _ZSt4setwi
    __std_find_trivial_1
    __std_reverse_trivially_swappable_1

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 7851 core/libs/prx/libc/Export.cpp
PR 10419 core/libs/prx/libc/src/exception/StandardExceptions.cpp
PR 10608 core/libs/prx/libc/src/exception/StandardExceptions.cpp
PR 10927 core/libs/prx/libc/src/exception/StandardExceptions.cpp

static bool APS5_VABI GenericCategoryEquivalentCondition_nid_no_patch(const CategoryObject* self, int value, const ErrorCode* condition) {
return condition->category == self && condition->value == value;
if (!condition) return false;
return condition->category == self && (condition->value == value || condition->value_high == value);

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.

ErrorCode matches the observed guest layout { int value; const CategoryObject* category; }: on x86_64 the pointer is at offset 8, leaving bytes 4–7 as alignment padding. value_high overlays that padding, which callers do not initialize as a second value. If the padding happens to equal the queried code while the actual value differs, this returns true for an unrelated error. The same extra-word comparison appears in the generic/system/future equivalence methods and the exported base-category equivalence methods below. Please compare the actual 32-bit value only and add a regression case that poisons the padding bytes.

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

Requesting one correctness fix before approval: the equivalence implementations compare value_high, which is the alignment padding in the observed { int value; category pointer; } guest layout. That can report equality for a different error code when padding matches. I left an inline comment at the first comparison; the same issue occurs in the system/future category and base-category comparisons. Please remove the padding comparisons and add a regression case with nonzero padding. The current hosted run also has a failing macOS x86_64 relinker test and a conventions failure; I could not retrieve the hosted failure log through gh during this review.

@ycbuzi

ycbuzi commented Oct 21, 2026

Copy link
Copy Markdown
Contributor

Windows run of 714d17e merged onto main a6dcea9 (6e4850c067, clean), the description lists no platform. Windows 11 Home 10.0.26200, MinGW-w64 GCC 15.2.0 (ucrt-posix-seh r7), Release, BUILD_TESTING=ON.

libc.prx does not link:

VectorAlgorithms.cpp: multiple definition of `__std_find_trivial_1_nid_postfix'
VectorAlgorithms.cpp: multiple definition of `__std_reverse_trivially_swappable_1_nid_postfix'
LocaleSupport.cpp: multiple definition of `_ZSt4setwi_nid_postfix'
Threads.cpp: multiple definition of `_Thrd_id_nid_postfix'
collect2.exe: error: ld returned 1 exit status

The four already exist in src/VectorAlgorithms.cpp, src/LocaleSupport.cpp and src/Threads.cpp on this PR's own base (0c6a5bd), and the PR adds second definitions in Export.cpp with placeholder bodies (_ZSt4setwi returns its argument, _Thrd_id returns 1, __std_find_trivial_1 returns nullptr, __std_reverse_trivially_swappable_1 does nothing). Duplicate strong definitions are a link error on any platform, so this is not specific to Windows. Not run: the tests, since the library does not build.

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

Review

I found a blocking duplicate definition and additional behavior that needs to be brought within the repository’s implementation rules before this PR can be considered.

Duplicate _Thrd_id_nid_postfix

This PR adds a strong C-linkage _Thrd_id_nid_postfix definition in libc/Export.cpp returning 1, while the existing definition in libc/src/Threads.cpp remains in this branch. The libc target includes both files in target_sources, so the library has two definitions of the same symbol. The current product Linux/Windows jobs were skipped, which explains why this has not been caught by those jobs.

There is also an open PR, #9170, that replaces the existing stub with a scePthreadSelf() forward based on two console-body readings and a focused test. Please remove the duplicate placeholder here and coordinate the single implementation and landing order with that author; update Depends on if the PRs are intentionally stacked. The constant 1 is not supported by the evidence cited in #9170.

Silent placeholders and scope

The new exports also include silent no-op/null placeholders, including _Cnd_broadcast, _Cnd_register_at_thread_exit, _Thrd_detach, __std_find_trivial_1, and __std_reverse_trivially_swappable_1. These are not category implementations, and the latter two return/do nothing for all inputs. CONTRIBUTING requires unimplemented paths to throw or to be explicitly recorded under the library’s Silent stubs debt. Please remove these unrelated placeholders from this PR or implement and test them, with the required debt entries if any are intentionally silent.

Category behavior

SystemCategoryEquivalentCondition accepts a condition from self even when GenericErrorMessage(value) selects GenericCategory. The category contract compares against default_error_condition(value); the MSVC STL implementation does that exact comparison. As written, a system-category condition can compare equivalent where the default condition is generic. Please remove that extra acceptance and add a regression check for both the expected generic match and the rejected system-category match.

The PR also adds no committed tests for the new category names, messages, default conditions, or equivalence behavior. The reported Windows round-trip is useful but not reproducible from this branch. Please add focused tests for those behaviors.

Current validation

  • git diff --check against current main: passed.
  • Current conventions check: passed.
  • Current CI: conventions, eligibility, labels, and report passed; register-context, most relinker, ASan relinker, and write-watch jobs passed.
  • relinker (macos-latest, clang++, x86_64) failed in its CTest step after the build. The log is unavailable through the current run-log endpoint, and the job is relinker-only, so I cannot attribute that failure to this libc change. Please rerun or report its cause.
  • Linux and Windows product-test jobs were skipped.
  • This head is 474 commits behind current main; please update it and rerun the focused build/tests after resolving the symbol and overlap.

The new category objects merit focused validation, but the duplicate symbol, silent placeholders, and system-category equivalence mismatch need correction first.

@hypermoist

hypermoist commented Oct 22, 2026 •

Copy link
Copy Markdown
Contributor

@Delciob the overlap report lists _ZSt15system_categoryv here and in #9014, which I opened about eleven hours earlier. The two also change the same lines of StandardExceptions.cpp: the ErrorCode struct and the generic category's condition functions. #8901 touches the same file around line 253, too.

The two differ in where the value lives. #9014 took the layout from the 12.00 libSceLibcInternal (read from disassembly, not measured on a console):

  • error_category::equivalent(const error_code&, int) (0x1b760) compares the value at +4 and the category at +8.
  • Both default_error_condition overrides (0x1b710, 0x1bba0) return the value in the high half of the first qword, so +0 stays 0.

This PR stores the value at both +0 and +4 and accepts a match on either. A guest that builds an error_code itself, as PPSA29340's std::filesystem code does (0 at +0, the category at +8, value() read from +4), gets the right answer from both PRs. But a condition this PR returns has a non-zero first dword, which the console never produces.

A proposal, since everything else here is new:

  1. feat(libc): add std::system_category and keep error_code values at offset 4 #9014 goes first with system_category and the +4 layout.
  2. This PR then drops its own SystemCategory, the value_high field and the double compare, and keeps future_category, the error_category base exports and the rest. It would say Depends on: #9014.

If you'd rather have it the other way round, I'm fine with that as long as the layout matches the console; then I'll rebase #9014 onto yours and keep only the layout fix and its tests.

Separately, the PlainWhat hunk removes the bad_variant_access and bad_optional_access names. That looks unrelated to the categories. Was it intended?

🤖 Generated with Claude Code

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

Checked against main 830ccd1 and the PR's own tests (core/libs/tests/GuestBadOptionalAccess.cpp, ExceptionRuntime.cpp):

  • Following up on the PlainWhat question: the same hunk removes the bad_optional_access type info, vtable and ctor/dtor exports, and the bad_variant_access type info and vtable, which main defines in StandardExceptions.cpp. GuestBadOptionalAccess.cpp still references _ZTISt19bad_optional_access_nid_postfix, _ZTVSt19bad_optional_access_nid_postfix and _ZNSt19bad_optional_accessC1Ev_nid_postfix, so guest_bad_optional_access_tests no longer links. Could the removal be reverted?
  • _ZSt26__throw_bad_variant_accessv and _ZSt26_Throw_bad_optional_accessv now throw std::runtime_error (Export.cpp:36-37, 48) instead of main's ThrowPlain(...) with the guest vtable. ExceptionRuntime.cpp:257-260 catches std::bad_variant_access& / std::bad_optional_access&, so the exception escapes and the test aborts. A guest catch on those types would miss in the same way. Keeping main's versions would fix both.
  • The new _ZSt6_ThrowRKSt8bad_cast, _ZSt6_ThrowRKSt20bad_array_new_length, _Throw_future_error and _Rethrow_future_exception also throw a host std::runtime_error, so guest handlers for bad_cast/future_error won't match. These probably belong in their own PR, built with ThrowPlain-style guest objects.
  • _ZSt14_Atomic_assert loses its NotImplemented_nid_no_patch (Export.cpp:23) and becomes a silent stub with no debt entry. Was that intended?

Reviewed with AI assistance (Claude Code); the checks above were run locally on my machine.

@edubox

edubox commented Oct 23, 2026

Copy link
Copy Markdown
Contributor

Heads-up: this PR adds

int APS5_VABI _ZSt4setwi_nid_postfix(int n) { return n; }
int APS5_VABI _ZSt12setprecisioni_nid_postfix(int n) { return n; }

to libc/Export.cpp. _ZSt4setwi is already implemented in LocaleSupport.cpp (merged in #6585), so those two definitions collide at link time. And setprecision is not a plain int: the libc.prx bundled in PPSA02530 exports 1h8hFQghR7w as a 10-byte function that returns the setter in RAX and the 32-bit value in RDX, with the setter

mov %esi,0x1c(%rdi)   # ios_base::precision
ret

I have that implementation plus a guest_locale test ready, backed by that binary. Could you drop both stub lines from this PR? They are outside the error_category scope, and the setprecision one is a silent stub. Once this lands (or closes) I will send the setprecision change on its own.

@github-actions github-actions Bot added the needs rebase Conflicts with main, rebase needed label Oct 23, 2026
@rooklz

rooklz commented Oct 23, 2026

Copy link
Copy Markdown
Contributor

@Delciob the overlap report pairs this PR with #9170 on _Thrd_id: this diff adds uint64_t _Thrd_id_nid_postfix() { return 1; } to libc/Export.cpp, a placeholder outside the error-category topic. The console's export returns scePthreadSelf() unchanged (readings of two titles' libc.prx on #4164 and #8711, and a title that compares all 64 bits of it with the id its _Thrd_create stored, on #9170), so a constant 1 would break that comparison. Could you drop that one function from this PR? #9170 implements it with that evidence and a guest_libc_threads check, and the repository's overlap rule otherwise holds both until the duplicate is resolved. If you'd rather keep it here, say so and I'll record the order on #9170 instead.

@rooklz

rooklz commented Oct 24, 2026

Copy link
Copy Markdown
Contributor

@Delciob a measurement on a PS5 for the _Thrd_id line here (uint64_t _Thrd_id_nid_postfix() { return 1; }). A homebrew payload called the console's _Thrd_id next to scePthreadSelf and pthread_self, in the main thread and in a second thread:

main   _Thrd_id=0x8804ac020 scePthreadSelf=8804ac020 pthread_self=8804ac020
main   _Thrd_id=0x8804ac020 scePthreadSelf=8804ac020 pthread_self=8804ac020
worker _Thrd_id=0x8804db4a0 scePthreadSelf=8804db4a0 pthread_self=8804db4a0

So it returns the calling thread's scePthreadSelf() pointer, different per thread, which is what the two disassembly readings on #4164 and #8711 found and what #9170 implements. A constant 1 would make PPSA23827's self-join check (it compares _Thrd_id() against the id _Thrd_create stored) misfire. Could you drop the _Thrd_id definition from this PR so the two can both land? The rest of the PR doesn't need it.

@edubox

edubox commented Oct 25, 2026

Copy link
Copy Markdown
Contributor

Heads-up: this PR adds a placeholder for _ZSt6_ThrowRKSt20bad_array_new_length to libc/Export.cpp (void* argument, throws std::runtime_error). #10927 implements that export with the console behaviour: the libc.prx bundled with PPSA02530 exports it at 0xcb2d0 as a 52-byte function that ignores its argument and throws the static std::bad_array_new_length object. The two definitions collide at link time.

Could you drop that line from this PR, as requested for _Thrd_id, _ZSt4setwi and setprecision? If this PR lands first, #10927 can replace the placeholder on rebase; if #10927 lands first, this one drops the line. The export is outside the error-category topic either way.

@adriadam10 adriadam10 self-assigned this Oct 25, 2026
@adriadam10

Copy link
Copy Markdown
Collaborator

Closing this one; it is welcome back as a small PR once the points below are addressed.

  • Mixed topics: besides system_category/future_category, libc/Export.cpp adds about 15 unrelated placeholder exports (_Thrd_id returning 1, setw/setprecision returning their argument, no-op Cnd_broadcast/Thrd_detach/Cnd_register_at_thread_exit, __std_find_trivial_1 returning null, runtime_error throws for _Throw*). Returning made-up results where main threw is not allowed; an export that cannot be implemented calls NotImplemented_nid_no_patch(__func__). _Atomic_assert also loses its NotImplemented_nid_no_patch call (Export.cpp:23).
  • Several collide with other work: _Thrd_id (fix(libc): return the calling thread from _Thrd_id as the console's libc.prx does #9170; a PS5 measurement posted here shows it returns scePthreadSelf()), _ZSt4setwi (already in LocaleSupport.cpp), _ZSt6_ThrowRKSt20bad_array_new_length (feat(libc): implement std::_Throw(const std::bad_array_new_length&) #10927), and _ZSt15system_categoryv (feat(libc): add std::system_category and keep error_code values at offset 4 #9014). Windows reports multiple definition link errors for these (ycbuzi).
  • StandardExceptions.cpp deletes the real bad_optional_access/bad_variant_access type info, vtables and constructors and replaces the throws with std::runtime_error; GuestBadOptionalAccess.cpp still references them.
  • ErrorCode.value_high is alignment padding in the observed layout, yet equivalence compares it (StandardExceptions.cpp:245, 249, 280, 284, 324, 328, 366, 371); this is the unaddressed request from MarcZX10.
  • No test and no docs/dev/debt/libc.md entry; "Tested" gives no evidence. The conventions and macOS x86_64 relinker checks are red and the branch conflicts with main.

A PR with only the two categories, layouts taken from a cited source, a guest test pinning values (including nonzero padding) and the debt lines would be reviewable.

@adriadam10 adriadam10 closed this Oct 25, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

libc needs rebase Conflicts with main, rebase needed

Projects

None yet

Development

Successfully merging this pull request may close these issues.

8 participants