Skip to content

Add regression test for CString::clone_into unwind safety#5157

Closed
Vastargazing wants to merge 1 commit into
rust-lang:masterfrom
Vastargazing:tests/cstring-clone-into-alloc-error
Closed

Add regression test for CString::clone_into unwind safety#5157
Vastargazing wants to merge 1 commit into
rust-lang:masterfrom
Vastargazing:tests/cstring-clone-into-alloc-error

Conversation

@Vastargazing

Copy link
Copy Markdown

Regression test for the panic-unsoundness fixed in rust-lang/rust#155707

rust-lang/rust#70201 added a clone_into override for CStr that moved the target CString's buffer out (leaving it empty) before growing a Vec. On allocation failure the destination CString could lose its trailing nul terminator, leading to UB. rust-lang/rust#155707 fixed this but didn't add a test.

It only reproduces under Miri: a #[global_allocator] in a library test doesn't intercept the reallocation inside CString::clone_into because library tests link std with -C prefer-dynamic, so it can't live in library/alloctests. Moved here from rust-lang/rust#158763 per @oli-obk (no rustc change required).

r? @oli-obk

CString::clone_into reuses the target's allocation by moving the buffer into a Vec and growing it. If that growth's allocation fails and the alloc error hook unwinds, the target has to be left as a valid CString, but nothing covered that path.

Add a test that fails the reallocation under a panicking alloc error hook and checks that the caught unwind leaves the target's nul terminator in place.
@rustbot rustbot added the S-waiting-on-review Status: Waiting for a review to complete label Jul 4, 2026
@rustbot

rustbot commented Jul 4, 2026

Copy link
Copy Markdown
Collaborator

Thanks for the pull request, and welcome! The Rust Project is excited to review your changes, and you should hear from @oli-obk (or someone else) some time within the next two weeks.

Please see the contribution instructions for more information. Namely, in order to ensure the minimum review times lag, PR authors and assigned reviewers should ensure that the review label (S-waiting-on-review and S-waiting-on-author) stays updated, invoking these commands when appropriate:

  • @rustbot author: the review is finished, PR author should check the comments and take action accordingly
  • @rustbot review: the author is ready for a review, this PR will be queued again in the reviewer's queue

@RalfJung RalfJung Jul 4, 2026

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

  • See my comment in your previous PR -- the test should contain references to the issue(s) it is about
  • Since this tests the standard library, not Miri, then if we must have this test here (do we?), it should be in a folder. Maybe call it lib?

View changes since the review

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

Will add the issue references

On the location: I originally wrote this as a library/alloctests test, but a native (non-Miri) alloctests run builds with -C prefer-dynamic, so the #[global_allocator] in the test binary doesn't intercept the reallocation inside CString::clone_into (which lives in libstd). As a result, the test no longer reproduces the bug - I verified this. It only works when the allocator is intercepted, i.e. under Miri.

So if it lives in library/alloctests, it would need to be Miri-only (e.g. #[cfg_attr(not(miri), ignore)]) and rely on the liballoc-under-Miri CI run. Would you prefer that, or should I keep it here under tests/pass/lib/? (oli-obk suggested moving it here from rust-lang/rust#158763)

@RalfJung RalfJung Jul 4, 2026

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

We have a bunch of tests in library/* that are mainly for Miri. They also run as regular tests, nothing wrong with that. So no need to make them not(miri). They only really test what the need to test under Miri, which is fine -- there can be a comment explaining that.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

makes sense - I'll move it to library/alloctests. I'll drop the assert that requires the unwind (so it passes as a normal test, where the allocator can't intercept libstd) and keep the check that the target is still a valid CString, with a comment that the failure path only really runs under Miri. Plus the issue references. I'll open that against rust-lang/rust and close this PR.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

Moved it locally and verified: passes as a regular test, and under ./x miri library/alloctests the allocator fires and it catches the bug.

One thing before I open the PR: the x86_64-gnu-miri job runs liballoc with --test-args notest, so as far as I can tell this test would compile under Miri in CI but not run there - the only CI run would be the trivial native one. Is that the coverage you intended, or would you rather keep it as a Miri test (it ran across the full matrix here), or wire it into CI differently?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

There is another job, something with gnu-aux, that runs this file.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I'll drop the assert that requires the unwind

If it's useful you can also keep that under if cfg!(miri). Just run as much as possible also natively. :)

@Vastargazing

Copy link
Copy Markdown
Author

Closing - moving this to library/alloctests in rust-lang/rust per the discussion (rust-lang/rust#158807). Thanks @RalfJung, @oli-obk.

@rustbot rustbot removed the S-waiting-on-review Status: Waiting for a review to complete label Jul 5, 2026
JonathanBrouwer added a commit to JonathanBrouwer/rust that referenced this pull request Jul 5, 2026
…-alloc-error, r=RalfJung

Add regression test for CString::clone_into unwind safety

Regression test for the panic-safety fix in rust-lang#155707

rust-lang#70201 gave `<CStr as ToOwned>::clone_into` a path that moved the target `CString`'s buffer out (leaving it empty) before growing a `Vec`; if that growth's allocation failed and unwound, the target was left without its nul terminator, which is UB. rust-lang#155707 fixed this but didn't add a test.

The failing allocator here is only honored under Miri - in a normal build a `#[global_allocator]` in a library test doesn't intercept the reallocation inside `CString::clone_into` (it lives in libstd, linked `-C prefer-dynamic`). So the test passes as a regular test and does the real check under Miri, with the unwind assertion gated on `cfg!(miri)`; it runs under Miri in CI via the library-tests-under-Miri job.

This started as rust-lang/miri#5157, but per @RalfJung a std regression test belongs with the standard library.

r? @RalfJung
jhpratt added a commit to jhpratt/rust that referenced this pull request Jul 6, 2026
…-alloc-error, r=RalfJung

Add regression test for CString::clone_into unwind safety

Regression test for the panic-safety fix in rust-lang#155707

rust-lang#70201 gave `<CStr as ToOwned>::clone_into` a path that moved the target `CString`'s buffer out (leaving it empty) before growing a `Vec`; if that growth's allocation failed and unwound, the target was left without its nul terminator, which is UB. rust-lang#155707 fixed this but didn't add a test.

The failing allocator here is only honored under Miri - in a normal build a `#[global_allocator]` in a library test doesn't intercept the reallocation inside `CString::clone_into` (it lives in libstd, linked `-C prefer-dynamic`). So the test passes as a regular test and does the real check under Miri, with the unwind assertion gated on `cfg!(miri)`; it runs under Miri in CI via the library-tests-under-Miri job.

This started as rust-lang/miri#5157, but per @RalfJung a std regression test belongs with the standard library.

r? @RalfJung
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants