channel: Handle possible allocation failure in list::Block::new.#1147
Merged
taiki-e merged 1 commit intocrossbeam-rs:masterfrom Nov 8, 2024
Merged
channel: Handle possible allocation failure in list::Block::new.#1147taiki-e merged 1 commit intocrossbeam-rs:masterfrom
list::Block::new.#1147taiki-e merged 1 commit intocrossbeam-rs:masterfrom
Conversation
82ccdf7 to
15395b9
Compare
Contributor
|
Ouch, thanks for catching this so quickly! |
taiki-e
approved these changes
Nov 8, 2024
Member
taiki-e
left a comment
There was a problem hiding this comment.
Thanks!
This is the second time this mistake happens with crossbeam repo (#690) and perhaps we should introduce a safe helper that returns Option<NonNull>...
taiki-e
pushed a commit
that referenced
this pull request
Dec 15, 2024
taiki-e
added a commit
that referenced
this pull request
Dec 15, 2024
taiki-e
added a commit
that referenced
this pull request
Dec 15, 2024
taiki-e
added a commit
that referenced
this pull request
Dec 15, 2024
taiki-e
pushed a commit
that referenced
this pull request
Dec 15, 2024
taiki-e
added a commit
that referenced
this pull request
Dec 15, 2024
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
std::alloc::alloc_zeroedcan fail by returning nullptr, which is UB to pass toBox::from_raw.This checks the returned pointer for null, and if so calls
std::alloc::handle_alloc_errorwhich diverges.Also adds a should-never-fail assertion that
layoutis non-zero-sized, which is a precondition ofalloc_zeroed.cc @cuviper #1146
MIRI reproducer
$ cargo add crossbeam-channel --git https://github.com/crossbeam-rs/crossbeam $ cargo +nightly miri run Finished `dev` profile [unoptimized + debuginfo] target(s) in 0.02s Running `/home/zachary/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/bin/cargo-miri runner /home/zachary/opt_mount/zachary/cargo-target/miri/x86_64-unknown-linux-gnu/debug/crossbeam-nullptr` error: Undefined Behavior: constructing invalid value: encountered 0, but expected something greater or equal to 1 --> /home/zachary/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/rustlib/src/rust/library/core/src/ptr/unique.rs:90:36 | 90 | unsafe { Unique { pointer: NonNull::new_unchecked(ptr), _marker: PhantomData } } | ^^^^^^^^^^^^^^^^^^^^^^^^^^^ constructing invalid value: encountered 0, but expected something greater or equal to 1 | = help: this indicates a bug in the program: it performed an invalid operation, and caused Undefined Behavior = help: see https://doc.rust-lang.org/nightly/reference/behavior-considered-undefined.html for further information = note: BACKTRACE: = note: inside `std::ptr::Unique::<crossbeam_channel::flavors::list::Block<u32>>::new_unchecked` at /home/zachary/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/rustlib/src/rust/library/core/src/ptr/unique.rs:90:36: 90:63 = note: inside `std::boxed::Box::<crossbeam_channel::flavors::list::Block<u32>>::from_raw_in` at /home/zachary/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/rustlib/src/rust/library/alloc/src/boxed.rs:1167:22: 1167:48 = note: inside `std::boxed::Box::<crossbeam_channel::flavors::list::Block<u32>>::from_raw` at /home/zachary/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/rustlib/src/rust/library/alloc/src/boxed.rs:1058:18: 1058:48 = note: inside `crossbeam_channel::flavors::list::Block::<u32>::new` at /home/zachary/.cargo/git/checkouts/crossbeam-5d5b005504a37dac/d2c53ef/crossbeam-channel/src/flavors/list.rs:85:18: 85:60 = note: inside `crossbeam_channel::flavors::list::Channel::<u32>::start_send` at /home/zachary/.cargo/git/checkouts/crossbeam-5d5b005504a37dac/d2c53ef/crossbeam-channel/src/flavors/list.rs:233:41: 233:58 = note: inside `crossbeam_channel::flavors::list::Channel::<u32>::send` at /home/zachary/.cargo/git/checkouts/crossbeam-5d5b005504a37dac/d2c53ef/crossbeam-channel/src/flavors/list.rs:428:17: 428:39 = note: inside `crossbeam_channel::Sender::<u32>::send` at /home/zachary/.cargo/git/checkouts/crossbeam-5d5b005504a37dac/d2c53ef/crossbeam-channel/src/channel.rs:444:41: 444:61 note: inside `main` --> src/main.rs:48:13 | 48 | let _ = s.send(42); | ^^^^^^^^^^ note: some details are omitted, run with `MIRIFLAGS=-Zmiri-backtrace=full` for a verbose backtrace error: aborting due to 1 previous errorWith this patch, we get an (expected) abort instead of UB:
$ cargo add crossbeam-channel --git https://github.com/zachs18/crossbeam --branch handle_allocation_failure $ cargo +nightly miri run Finished `dev` profile [unoptimized + debuginfo] target(s) in 0.02s Running `/home/zachary/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/bin/cargo-miri runner /home/zachary/opt_mount/zachary/cargo-target/miri/x86_64-unknown-linux-gnu/debug/crossbeam-nullptr` memory allocation of 504 bytes failed error: abnormal termination: the program aborted execution --> /home/zachary/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/rustlib/src/rust/library/std/src/sys/pal/unix/mod.rs:373:14 | 373 | unsafe { libc::abort() } | ^^^^^^^^^^^^^ the program aborted execution | = note: BACKTRACE: = note: inside `std::sys::pal::unix::abort_internal` at /home/zachary/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/rustlib/src/rust/library/std/src/sys/pal/unix/mod.rs:373:14: 373:27 = note: inside `std::process::abort` at /home/zachary/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/rustlib/src/rust/library/std/src/process.rs:2374:5: 2374:33 = note: inside `std::alloc::rust_oom` at /home/zachary/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/rustlib/src/rust/library/std/src/alloc.rs:376:5: 376:28 = note: inside `std::alloc::_::__rg_oom` at /home/zachary/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/rustlib/src/rust/library/std/src/alloc.rs:371:1: 371:37 = note: inside `std::alloc::handle_alloc_error::rt_error` at /home/zachary/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/rustlib/src/rust/library/alloc/src/alloc.rs:403:13: 403:70 = note: inside `std::alloc::handle_alloc_error` at /home/zachary/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/rustlib/src/rust/library/alloc/src/alloc.rs:409:9: 409:75 = note: inside `crossbeam_channel::flavors::list::Block::<u32>::new` at /home/zachary/.cargo/git/checkouts/crossbeam-e091cf7cf0ef731e/988e165/crossbeam-channel/src/flavors/list.rs:83:13: 83:39 = note: inside `crossbeam_channel::flavors::list::Channel::<u32>::start_send` at /home/zachary/.cargo/git/checkouts/crossbeam-e091cf7cf0ef731e/988e165/crossbeam-channel/src/flavors/list.rs:240:41: 240:58 = note: inside `crossbeam_channel::flavors::list::Channel::<u32>::send` at /home/zachary/.cargo/git/checkouts/crossbeam-e091cf7cf0ef731e/988e165/crossbeam-channel/src/flavors/list.rs:435:17: 435:39 = note: inside `crossbeam_channel::Sender::<u32>::send` at /home/zachary/.cargo/git/checkouts/crossbeam-e091cf7cf0ef731e/988e165/crossbeam-channel/src/channel.rs:444:41: 444:61 note: inside `main` --> src/main.rs:48:13 | 48 | let _ = s.send(42); | ^^^^^^^^^^ note: some details are omitted, run with `MIRIFLAGS=-Zmiri-backtrace=full` for a verbose backtrace error: aborting due to 1 previous errorDetails