-
Notifications
You must be signed in to change notification settings - Fork 495
Add regression test for CString::clone_into unwind safety #5157
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
Vastargazing
wants to merge
1
commit into
rust-lang:master
from
Vastargazing:tests/cstring-clone-into-alloc-error
+55
−0
Closed
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,55 @@ | ||
| //@compile-flags: -Zmiri-ignore-leaks | ||
| #![feature(alloc_error_hook)] | ||
|
|
||
| use std::alloc::{GlobalAlloc, Layout, System, set_alloc_error_hook}; | ||
| use std::ffi::CString; | ||
| use std::panic::{self, AssertUnwindSafe, catch_unwind}; | ||
| use std::sync::atomic::{AtomicBool, Ordering}; | ||
|
|
||
| // Once armed, the first allocation of 8 bytes or more fails and disarms, so the | ||
| // reallocation inside `clone_into`'s grow path fails while the runtime's own | ||
| // smaller allocations keep succeeding. | ||
| struct OneShotFailingAlloc; | ||
|
|
||
| static ARMED: AtomicBool = AtomicBool::new(false); | ||
|
|
||
| unsafe impl GlobalAlloc for OneShotFailingAlloc { | ||
| unsafe fn alloc(&self, layout: Layout) -> *mut u8 { | ||
| if layout.size() >= 8 && ARMED.swap(false, Ordering::SeqCst) { | ||
| return core::ptr::null_mut(); | ||
| } | ||
| unsafe { System.alloc(layout) } | ||
| } | ||
|
|
||
| unsafe fn dealloc(&self, ptr: *mut u8, layout: Layout) { | ||
| unsafe { System.dealloc(ptr, layout) } | ||
| } | ||
|
|
||
| unsafe fn realloc(&self, ptr: *mut u8, layout: Layout, new_size: usize) -> *mut u8 { | ||
| if new_size >= 8 && ARMED.swap(false, Ordering::SeqCst) { | ||
| return core::ptr::null_mut(); | ||
| } | ||
| unsafe { System.realloc(ptr, layout, new_size) } | ||
| } | ||
| } | ||
|
|
||
| #[global_allocator] | ||
| static ALLOC: OneShotFailingAlloc = OneShotFailingAlloc; | ||
|
|
||
| // `<CStr as ToOwned>::clone_into` must leave the target `CString` valid even when | ||
| // the reallocation for its grow path unwinds. See rust-lang/rust#155707. | ||
| fn main() { | ||
| set_alloc_error_hook(|_| panic!("alloc error")); | ||
| panic::set_hook(Box::new(|_| {})); // keep the caught panic quiet | ||
|
|
||
| let src = CString::new("a fairly long value").unwrap(); | ||
| let mut target = CString::new("x").unwrap(); | ||
|
|
||
| ARMED.store(true, Ordering::SeqCst); | ||
| let res = catch_unwind(AssertUnwindSafe(|| src.as_c_str().clone_into(&mut target))); | ||
| ARMED.store(false, Ordering::SeqCst); | ||
|
|
||
| assert!(res.is_err(), "clone_into should have unwound on the alloc failure"); | ||
| // `target` must still end in its nul terminator; before the fix it was left empty. | ||
| assert_eq!(target.as_bytes_with_nul().last(), Some(&0)); | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
lib?View changes since the review
There was a problem hiding this comment.
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/allocteststest, but a native (non-Miri)alloctestsrun builds with-C prefer-dynamic, so the#[global_allocator]in the test binary doesn't intercept the reallocation insideCString::clone_into(which lives inlibstd). 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 undertests/pass/lib/? (oli-obk suggested moving it here from rust-lang/rust#158763)Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
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 themnot(miri). They only really test what the need to test under Miri, which is fine -- there can be a comment explaining that.There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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/allocteststhe 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?There was a problem hiding this comment.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If it's useful you can also keep that under
if cfg!(miri). Just run as much as possible also natively. :)