fix(libc): implement RTTI cast result callbacks #10416

Merged
MarcZX10 merged 7 commits from upstream-pr/10416 into main 2026-11-24 16:56:00 +00:00
MarcZX10 commented 2026-10-23 22:52:34 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-10-23T22:52:34Z.


What

Implement the Itanium RTTI __do_upcast result and __do_dyncast callbacks. Traverse guest class metadata to preserve subobject offsets, public and private paths, virtual bases, and repeated base ambiguity. Extend the guest dynamic cast fixture to cover those paths. The separate __do_find_public_src callback is not changed in this pull request.

Tested

Linux, GCC 12.2: compiled Runtime.cpp, TypeInfo.cpp and GuestDynamicCast.cpp with -Wall -Wextra -Werror; linked and ran the focused fixture with AddressSanitizer and UndefinedBehaviorSanitizer. The fixture passed for public and private upcasts, repeated-base ambiguity, virtual-base offsets, downcasts, crosscasts and rejected private paths. A full CMake configure was attempted but could not proceed because the SDL2 submodule is absent and the environment lacks XRandR support.

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: #10411
  • Fixes: none
  • Unblocks: none
  • GPUs tested: n/a
  • AI-assisted: yes
<!-- greyfinch-sync:boykopovar/AnyPS5:issue:4990230300 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/10416). Originally posted by [MarcZX10](https://github.com/MarcZX10) on 2026-10-23T22:52:34Z. --- ### What Implement the Itanium RTTI `__do_upcast` result and `__do_dyncast` callbacks. Traverse guest class metadata to preserve subobject offsets, public and private paths, virtual bases, and repeated base ambiguity. Extend the guest dynamic cast fixture to cover those paths. The separate `__do_find_public_src` callback is not changed in this pull request. ### Tested Linux, GCC 12.2: compiled `Runtime.cpp`, `TypeInfo.cpp` and `GuestDynamicCast.cpp` with `-Wall -Wextra -Werror`; linked and ran the focused fixture with AddressSanitizer and UndefinedBehaviorSanitizer. The fixture passed for public and private upcasts, repeated-base ambiguity, virtual-base offsets, downcasts, crosscasts and rejected private paths. A full CMake configure was attempted but could not proceed because the SDL2 submodule is absent and the environment lacks XRandR support. ### Checklist <!-- Rules behind each item: https://github.com/boykopovar/AnyPS5/blob/main/CONTRIBUTING.md --> - [x] Based on current `main`; no other open PR implements the same functions - [x] One topic per PR; follow-ups go in a new PR - [x] No comments in code except [technical debt](https://github.com/boykopovar/AnyPS5/blob/main/docs/dev/CONVENTIONS.md) - [x] Unimplemented paths throw (`NotImplemented_nid_no_patch`); silent stubs are listed under Silent stubs in their library's [technical debt](https://github.com/boykopovar/AnyPS5/tree/main/docs/dev/debt) file - [x] No notes, investigation `.md` files or images added to the repository (attach them to this PR) - [x] New third-party code is a submodule built from source - [x] Generic behaviour, not specific to one title - [x] Depends on: #10411 - [x] Fixes: none - [x] Unblocks: none - [x] GPUs tested: n/a - [x] AI-assisted: yes
harisibrar1 commented 2026-10-24 05:40:00 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by harisibrar1 on 2026-10-24T05:40:00Z.

Review: COMMENTED


Checked against main 7c2400d29b and GCC libsupc++ releases/gcc-14.2.0 (04696df096, vmi_class_type_info.cc __do_upcast/__do_dyncast, dyncast.cc):

  • Runtime.cpp uses ClassTypeInfo::__contained_virtual_mask, __contained_public_mask, __contained_ambig, __not_contained and __contained_public. On libc++abi hosts, main's specifics/itanium/CxxAbi.hpp only declares enum __sub_kind { __unknown = 0 };, so I'd expect the full macOS build to fail to compile. I couldn't build it here, and CI's macOS jobs are relinker-only, so it wouldn't show up there. Adding the enumerators to CxxAbi.hpp, or using integer constants as #10411 does, would avoid it.
  • In DynamicCastTypeInfo, sourceVisitor assigns toSource = baseLocation.path; (L353), so in a virtual diamond the last-visited path wins. Example: W : private D, D : public B1, public B2, B1 : public virtual S, B2 : private virtual S, casting W's S* to D*. B2's private path overwrites B1's public one, D is not treated as a downcast, and the result is null, while libsupc++ (via __find_public_src) and main's own __dynamic_cast_nid_postfix both return D. ORing the paths fixes it.
  • The cross-cast branch counts only public candidates (L380/L394), so a D that appears once publicly and once privately is treated as unique. With W : public X, private Y, public S, X : public D, Y : public D, casting from S to D returns X's D, while libsupc++'s vmi __do_dyncast visits the private copy (non-diamond repeat) and returns NULL as ambiguous. Main's Bases() has the same public-only counting, so this isn't new, but UpcastTypeInfoResult here already treats any second identity as ambiguous. Could the cross-cast check do the same?
  • Small difference: libsupc++'s __do_upcast skips private bases when src_details lacks __non_diamond_repeat_mask, so PrivateType gives false/__unknown there rather than true/__contained_private. It's harmless through contained_public_p, but the test currently pins the non-libsupc++ value.
  • The dyncast tests don't reach the ambiguous branches (L389–403), virtual bases or a diamond, and CallDynamicCast always passes src2dst = 0, which claims a public base at offset 0. One or two diamond fixtures, for example the two above, would cover the risky parts.
  • This lists #10411 as a dependency but isn't stacked on it, and the two conflict in TypeInfo.cpp and GuestDynamicCast.cpp.

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

<!-- greyfinch-sync:boykopovar/AnyPS5:review:5637037966 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/10416#pullrequestreview-5637037966). Originally posted by [harisibrar1](https://github.com/harisibrar1) on 2026-10-24T05:40:00Z. **Review:** COMMENTED --- Checked against main 7c2400d29b and GCC libsupc++ releases/gcc-14.2.0 (04696df096, `vmi_class_type_info.cc` `__do_upcast`/`__do_dyncast`, `dyncast.cc`): - `Runtime.cpp` uses `ClassTypeInfo::__contained_virtual_mask`, `__contained_public_mask`, `__contained_ambig`, `__not_contained` and `__contained_public`. On libc++abi hosts, main's `specifics/itanium/CxxAbi.hpp` only declares `enum __sub_kind { __unknown = 0 };`, so I'd expect the full macOS build to fail to compile. I couldn't build it here, and CI's macOS jobs are relinker-only, so it wouldn't show up there. Adding the enumerators to `CxxAbi.hpp`, or using integer constants as #10411 does, would avoid it. - In `DynamicCastTypeInfo`, `sourceVisitor` assigns `toSource = baseLocation.path;` (L353), so in a virtual diamond the last-visited path wins. Example: `W : private D`, `D : public B1, public B2`, `B1 : public virtual S`, `B2 : private virtual S`, casting W's `S*` to `D*`. B2's private path overwrites B1's public one, D is not treated as a downcast, and the result is null, while libsupc++ (via `__find_public_src`) and main's own `__dynamic_cast_nid_postfix` both return D. ORing the paths fixes it. - The cross-cast branch counts only public candidates (L380/L394), so a D that appears once publicly and once privately is treated as unique. With `W : public X, private Y, public S`, `X : public D`, `Y : public D`, casting from S to D returns X's D, while libsupc++'s vmi `__do_dyncast` visits the private copy (non-diamond repeat) and returns NULL as ambiguous. Main's `Bases()` has the same public-only counting, so this isn't new, but `UpcastTypeInfoResult` here already treats any second identity as ambiguous. Could the cross-cast check do the same? - Small difference: libsupc++'s `__do_upcast` skips private bases when `src_details` lacks `__non_diamond_repeat_mask`, so `PrivateType` gives false/`__unknown` there rather than true/`__contained_private`. It's harmless through `contained_public_p`, but the test currently pins the non-libsupc++ value. - The dyncast tests don't reach the ambiguous branches (L389–403), virtual bases or a diamond, and `CallDynamicCast` always passes `src2dst = 0`, which claims a public base at offset 0. One or two diamond fixtures, for example the two above, would cover the risky parts. - This lists #10411 as a dependency but isn't stacked on it, and the two conflict in `TypeInfo.cpp` and `GuestDynamicCast.cpp`. Reviewed with AI assistance (Claude Code); the checks above were run locally on my machine.
MarcZX10 commented 2026-11-02 04:57:25 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-11-02T04:57:25Z.

Review: COMMENTED


Reviewed — regression claim confirmed by reading, not yet fixed

The early multiple-subobject return runs before the downcast branch, so the reviewer's regression is present at this head; the requested fix and test are not in the diff.

What I ran

  • Live head 0a490de29d1eeec9d32e53492d1e0a0bde151635 (matches batch).
  • gh pr view 10416 (headRefOid, statusCheckRollup); gh api GET of comments, reviews and review comments newer than 2026-10-27T10:55:25Z.
  • git show of core/libs/prx/libc/src/exception/Runtime.cpp and core/libs/tests/GuestDynamicCast.cpp at the head; git diff --stat of the last commit.
  • Nothing was built or run.

Results

Check Result Note
eligibility, conventions, labels, report SUCCESS hosted
relinker (3 OS), relinker-asan, register-context, write-watch SUCCESS hosted
windows SUCCESS hosted
linux SKIPPED hosted

Findings

  • oneandonlydean (2026-10-27T13:52:23Z): the candidates.size() > 1 early return in DynamicCastTypeInfo returns ambiguous before the downcast branch, so a downcast to Mid through A's subobject returns null.
    • Runtime.cpp:382-385 returns on candidates.size() > 1.
    • The downcast branch (downcasts.size() == 1) is at Runtime.cpp:393-398, after it.
    • Verified by reading. Not run.
  • Requested change (move the multiple-subobject check to the crosscast branch, candidates.size() == 1) and a test for the downcast shape: not present. The test file has no type named Mid (grep).
  • maskym's Windows report for this head is a local run, not a request.

Sources

Not verified

  • What GCC and Clang return for the reviewer's downcast shape (not run).
  • Behavior of the existing tests; no build.
<!-- greyfinch-sync:boykopovar/AnyPS5:review:5732248947 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/10416#pullrequestreview-5732248947). Originally posted by [MarcZX10](https://github.com/MarcZX10) on 2026-11-02T04:57:25Z. **Review:** COMMENTED --- # Reviewed — regression claim confirmed by reading, not yet fixed The early multiple-subobject return runs before the downcast branch, so the reviewer's regression is present at this head; the requested fix and test are not in the diff. ## What I ran - Live head 0a490de29d1eeec9d32e53492d1e0a0bde151635 (matches batch). - gh pr view 10416 (headRefOid, statusCheckRollup); gh api GET of comments, reviews and review comments newer than 2026-10-27T10:55:25Z. - git show of core/libs/prx/libc/src/exception/Runtime.cpp and core/libs/tests/GuestDynamicCast.cpp at the head; git diff --stat of the last commit. - Nothing was built or run. ## Results | Check | Result | Note | |---|---|---| | eligibility, conventions, labels, report | SUCCESS | hosted | | relinker (3 OS), relinker-asan, register-context, write-watch | SUCCESS | hosted | | windows | SUCCESS | hosted | | linux | SKIPPED | hosted | ## Findings - oneandonlydean (2026-10-27T13:52:23Z): the `candidates.size() > 1` early return in DynamicCastTypeInfo returns ambiguous before the downcast branch, so a downcast to Mid through A's subobject returns null. - Runtime.cpp:382-385 returns on `candidates.size() > 1`. - The downcast branch (`downcasts.size() == 1`) is at Runtime.cpp:393-398, after it. - Verified by reading. Not run. - Requested change (move the multiple-subobject check to the crosscast branch, `candidates.size() == 1`) and a test for the downcast shape: not present. The test file has no type named Mid (grep). - maskym's Windows report for this head is a local run, not a request. ## Sources - https://github.com/boykopovar/AnyPS5/blob/0a490de29d1eeec9d32e53492d1e0a0bde151635/core/libs/prx/libc/src/exception/Runtime.cpp#L382-L398 - https://github.com/boykopovar/AnyPS5/blob/0a490de29d1eeec9d32e53492d1e0a0bde151635/core/libs/prx/libc/src/exception/Runtime.cpp#L403-L405 ## Not verified - What GCC and Clang return for the reviewer's downcast shape (not run). - Behavior of the existing tests; no build.
Sign in to join this conversation.
No reviewers
No labels
No milestone
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Reference
boykopovar/AnyPS5!10416
No description provided.