Skip to content

feat(libc): add libc++ string __grow_by, copy assignment and operator+ - #27578

Open
kiansaeidnia wants to merge 1 commit into
boykopovar:mainfrom
kiansaeidnia:feat/libc-libcxx-string-growth
Open

kiansaeidnia wants to merge 1 commit into
boykopovar:mainfrom
kiansaeidnia:feat/libc-libcxx-string-growth

Conversation

@kiansaeidnia

@kiansaeidnia kiansaeidnia commented Nov 26, 2026 •

Copy link
Copy Markdown
Contributor

What

The RetroArch Azahar core (PPSA99169, import inventory on #5927) imports three libc++ basic_string<char> members that libc did not define. This PR adds them in the new LibcxxStringGrowth.cpp, on the 24-byte libc++ layout the other libc++ string exports use:

  • __grow_by(old_cap, delta_cap, old_sz, n_copy, n_del, n_add);
  • copy assignment, operator=(const basic_string&);
  • operator+(const char*, const basic_string&).

They follow LLVM libc++ 17.0.6 include/string:

  • max_size() is UINT64_MAX - 16 and __recommend rounds s + 1 up to 16, minus one.
  • The new capacity is __recommend(max(old_cap + delta_cap, 2 * old_cap)), or max_size() - 1 once old_cap reaches max_size() / 2 - 16.
  • __grow_by copies the first n_copy bytes and the tail after n_del, frees the old buffer only when it was long, and leaves the size word alone, as libc++ 17 does.
  • Copy assignment returns early for self-assignment, copies the whole representation from a short source to a short destination, reuses the buffer when n < capacity, and otherwise grows the way __grow_by_and_replace does, with its - 1 length check.
  • operator+ builds the result with the libc++ capacity for the total length.
  • length_error and bad_alloc are thrown where libc++ throws them. Storage comes from the guest operator new and operator delete.

Input that no libc++ caller passes, such as an old_cap that does not match the string's capacity or counts beyond its size, throws invalid_argument instead of writing out of bounds, as the existing LibcxxStringErase.cpp does. The wchar_t __grow_by import is left out, because its element width is not settled. The debt fragment records that and says the console provider is unmeasured.

This does not touch the open libc++ string series (#25674, #25708, #25744, #25750, #25759, #25763): none of them defines these three names, and the shared files only get lines added at different places. #25674's reserve rejects sizes above UINT64_MAX - 17, one less than the 17.0.6 max_size() this PR uses. That difference belongs to #25674.

pr_overlap reported one conflict: #27016 (libc++ to_string(float)) adds its declaration at the same place in Api.hpp. The declarations here now sit just above the erase declaration, so the two merge cleanly in either order (checked with git merge-tree). It could not compare libc/CMakeLists.txt or Api.hpp with #7447, #7715, #8150, #8965, #11831, #12682, #12688, #12700, #12717, #12849, #12965, #16791, #17557, #18135, #19181, #23395, #24322, #24910, #24917, #25289 and #26224, because those PRs conflict with main. I searched their diffs, and none adds these names or LibcxxStringGrowth.cpp.

Tested

Windows 11, MinGW-w64 GCC 16.2, Release. The new guest_libcxx_string_growth passes, and so do guest_libcxx_string_erase and guest_libcxx_string_find.

The new test checks:

  • __grow_by from short and long strings, with copied, deleted and added ranges and the exact capacity words;
  • the max_size() - 1 capacity branch, using a hand-made long string near the limit that must give bad_alloc with the string unchanged. With that branch removed, the test aborts;
  • length_error past max_size() for all three members;
  • copy assignment between short and long strings, with buffer reuse, growth and self-assignment;
  • operator+ with short and long results and embedded NULs on the right;
  • invalid_argument for inconsistent sizes, a null left operand and an invalid long string.

LibcxxStringGrowth.cpp and the test were syntax-checked with g++ 11 on Linux (WSL Ubuntu). debt_fold.py --check passes. Not tested in the Azahar core itself.

Checklist

🤖 Generated with Claude Code

Summary by CodeRabbit

  • Bug Fixes
    • Improved compatibility for applications that grow or assign strings, or combine a C-string with a string.
    • String operations now handle capacity changes and invalid or inaccessible string data more reliably.
  • Tests
    • Added coverage for string growth, assignment, and concatenation, including edge cases such as embedded null characters and allocation failures.
  • Documentation
    • Documented the supported string behavior and compatibility considerations.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@coderabbitai

coderabbitai Bot commented Nov 26, 2026 •

Copy link
Copy Markdown

Explain Change →

No actionable comments were generated in the recent review.

Walkthrough

Changes: libc++ string operations

Layer / File(s) Summary
String representation and capacity growth
core/libs/prx/libc/include/Api.hpp, core/libs/prx/libc/src/LibcxxStringGrowth.cpp, core/libs/prx/libc/CMakeLists.txt, core/libs/prx/libc/tests/cmake/guest_libcxx_string_growth.cmake, core/libs/tests/GuestLibcxxStringGrowth.cpp, docs/dev/debt/changes/feat-libc-libcxx-string-growth.md
Adds guest string representation helpers and __grow_by, including validation, capacity calculation, copying, and replacement of long storage. Registers the implementation and tests. The tests cover growth behavior and errors. The change entry describes the listed basic_string<char> operations and notes the omitted wchar_t import.
String copy assignment
core/libs/prx/libc/src/LibcxxStringGrowth.cpp, core/libs/tests/GuestLibcxxStringGrowth.cpp
Adds self-assignment handling, short representation copying, target capacity reuse, and allocation replacement. Tests cover short and long strings, allocation behavior, and invalid inputs.
C-string concatenation
core/libs/prx/libc/src/LibcxxStringGrowth.cpp, core/libs/tests/GuestLibcxxStringGrowth.cpp
Adds concatenation for short and long results, validates input and combined length, and preserves embedded NULs. Tests cover result sizes and invalid inputs.

Suggested reviewers: marczx10, fromthemoonx

Change: Feature

Merge Risk: Minimal · up to 4c9b6

This adds three libc++ string exports to the guest libc, with tests and a change note. No concrete defect was found in the supplied evidence. Testing inside the Azahar core is unreported, but that is not a merge-blocking risk.

Security architecture risk: Low · up to 4c9b6

The new string operations can leave a guest string partly modified when a memory write fails. The supported impact is on guest string state and recovery; the review did not establish new access to host memory or another guest.

Retained concerns

  • Medium · reliability · inferred: The new ABI uses a checked copy as the commit point for a replacement representation. On the Linux fault-copy path, that copy can report failure after writing part of the representation. Cleanup then releases the replacement buffer without restoring the destination, so a caller recovering from the error can observe inconsistent string metadata.
  • Low · reliability · inferred: Copy assignment that reuses a long destination buffer writes contents and a terminator before storing the new size. If a later transfer fails, the operation does not restore earlier writes, so the destination can retain its old size while exposing changed contents.
Security review details

Security Blast Radius

  • inferred — The demonstrated exposure is to callers of the guest libc++ string ABI and to the string objects and buffers they supply. The examined paths do not establish an expansion into another guest or a new privileged service.

Trust Boundaries and Controls

  • observed — Growth checks that the caller's capacity agrees with the guest representation. That check is not proof of heap ownership: the existing delete wrapper passes the pointer to the configured heap callback. The PR adds callers of that wrapper, not a new deallocation interface.
Finishing Touches

Generate unit tests (beta)

  • Create a new PR
  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts
Recent review info

Run configuration

  • Source mode: Auto
  • Repository: Unavailable
  • Branch: Unavailable
  • File: No configuration file from this repository applied
  • Commit: Unavailable
  • Layers: defaults
  • Review profile: CHILL
  • Plan: Advanced
  • Run ID: 5ad2c422-0c0b-4f25-b518-f0f6ef8c1016
Commits

Reviewing files that changed from the base of the PR and between 68f3c90 and 4c9b67c.

Files selected for processing: 6

  • core/libs/prx/libc/CMakeLists.txt
  • core/libs/prx/libc/include/Api.hpp
  • core/libs/prx/libc/src/LibcxxStringGrowth.cpp
  • core/libs/prx/libc/tests/cmake/guest_libcxx_string_growth.cmake
  • core/libs/tests/GuestLibcxxStringGrowth.cpp
  • docs/dev/debt/changes/feat-libc-libcxx-string-growth.md

Included review availability: This review used your included allowance. Your plan provides up to 10 included reviews per hour; 9 remain after this review.

@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 change adds three basic_string<char> ABI exports and focused guest tests; I found no verified blocking defect in the changed code, but did not run the checks.

What I ran

Reviewed head 4c9b67c2b4c27298d3a7a67c40886c4f4cfebd07 using repo_read.py to inspect the prepared diff and targeted source context. No build, test, or merged-preview check was run. No commands were supplied as raw output.

Results

Check Result Note
Changed code review passed Six changed files read
Build and tests not run No execution environment used
Merged preview not run No merge check performed

Findings

Blocking

None.

Non-blocking

None.

Not verified

The author reports Windows 11/MinGW tests, a Linux syntax check, and a debt_fold.py --check pass; these were not independently run. Azahar core behavior and the firmware provider are unmeasured per the debt note. No files were excluded from this review: all six changed files listed by the review manifest were read. No separate files-exclusion listing was available in the review folder.

To get this to a good state

Nothing is required.

@github-actions github-actions Bot added the unblocks title Names a title ID on the Unblocks line label Nov 26, 2026
@oneandonlydean

Copy link
Copy Markdown
Collaborator

Reviewed 4c9b67c2 by reading the code (no build or run). One thing to change: the growth rule, __grow_by, copy assignment and operator+ in core/libs/prx/libc/src/LibcxxStringGrowth.cpp closely follow libc++ 17.0.6 <string>, as the description says, so the file needs the LLVM licence notice. Please add the banner used in LibcxxThreadState.cpp:1-8 with the link https://github.com/llvm/llvm-project/blob/6009708b4367171ccdbf4b5905cb6a803753fe18/libcxx/include/string

I found no other defect.

@Plyxen Plyxen 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 head 4c9b67c2b4. I applied it to main ad7582bad4 together with #27581 and #27579, two other libc++ additions from tonight, which apply cleanly alongside it. On Windows 11 with WinLibs GCC 15.2 (Release), tools/check_conventions.py --head on each head and tools/debt_fold.py --check passed, the build succeeded, and all 4,932 CPU tests passed (I left out the GPU tests).

guest_libcxx_string_growth passed. The exports are new, so on main the test can't link without the change.

I compared the three bodies with LLVM libc++ 17.0.6. __grow_by uses max_size() = UINT64_MAX - 16, the same capacity choice, and copies the two segments. Like libc++, it leaves the size word alone for the caller to set. Copy assignment follows __assign_no_alias for both target forms, reusing the buffer when size < cap and growing through __grow_by_and_replace otherwise. operator+ builds through the __uninitialized_size_tag path, zeroing the short form. The invalid_argument throws for unreadable or inconsistent representations follow the merged LibcxxStringErase.cpp.

AI-assisted: yes (Claude Code)

@github-actions

Copy link
Copy Markdown

Progress report (68f3c90 → 4c9b67c)

📈 System libraries: 47.36% (+0.01%, +3 implemented functions)

✅ 3 implemented
Library Name
libc _ZNSt3__112basic_stringIcNS_11char_traitsIcEENS_9allocatorIcEEE9__grow_byEmmmmmm_nid_postfix
libc _ZNSt3__112basic_stringIcNS_11char_traitsIcEENS_9allocatorIcEEEaSERKS5__nid_postfix
libc _ZNSt3__1plIcNS_11char_traitsIcEENS_9allocatorIcEEEENS_12basic_stringIT_T0_T1_EEPKS6_RKS9__nid_postfix

Changed libraries (implemented exports)

Library Before After Change
libc 1883/1891 (99.58%) 1886/1894 (99.58%) +0.0007%

@github-actions

github-actions Bot commented Nov 27, 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
core/libs/prx/libc/include/Api.hpp
PR 12688 core/libs/prx/libc/CMakeLists.txt
core/libs/prx/libc/include/Api.hpp
PR 12700 core/libs/prx/libc/CMakeLists.txt
core/libs/prx/libc/include/Api.hpp
PR 12717 core/libs/prx/libc/CMakeLists.txt
PR 12849 core/libs/prx/libc/CMakeLists.txt
PR 12965 core/libs/prx/libc/CMakeLists.txt
core/libs/prx/libc/include/Api.hpp
PR 18190 core/libs/prx/libc/include/Api.hpp
PR 23395 core/libs/prx/libc/CMakeLists.txt
PR 26796 core/libs/prx/libc/CMakeLists.txt
PR 27568 core/libs/prx/libc/CMakeLists.txt
core/libs/prx/libc/include/Api.hpp
PR 27569 core/libs/prx/libc/CMakeLists.txt
core/libs/prx/libc/include/Api.hpp
PR 27646 core/libs/prx/libc/CMakeLists.txt
PR 27815 core/libs/prx/libc/CMakeLists.txt
core/libs/prx/libc/include/Api.hpp
PR 27823 core/libs/prx/libc/CMakeLists.txt
core/libs/prx/libc/include/Api.hpp
PR 28071 core/libs/prx/libc/CMakeLists.txt
PR 28657 core/libs/prx/libc/include/Api.hpp
PR 28808 core/libs/prx/libc/CMakeLists.txt
PR 28839 core/libs/prx/libc/CMakeLists.txt
core/libs/prx/libc/include/Api.hpp
PR 29099 core/libs/prx/libc/CMakeLists.txt
core/libs/prx/libc/include/Api.hpp
PR 29111 core/libs/prx/libc/CMakeLists.txt
core/libs/prx/libc/include/Api.hpp
PR 29272 core/libs/prx/libc/CMakeLists.txt
PR 29273 core/libs/prx/libc/CMakeLists.txt

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

The Linux GuestLibcxxStringGrowth fixture passed with the exact implementation. It checks the libc++ representation for both short and heap-backed strings, the retained and newly allocated copy-assignment paths, and allocation cleanup through the guest heap.

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

build docs libc unblocks title Names a title ID on the Unblocks line

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants