Skip to content
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

Initialize channel Blocks directly on the heap #132738

Merged
merged 2 commits into from
Nov 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions library/std/src/sync/mpmc/list.rs
Original file line number Diff line number Diff line change
Expand Up @@ -63,14 +63,14 @@ struct Block<T> {

impl<T> Block<T> {
/// Creates an empty block.
fn new() -> Block<T> {
fn new() -> Box<Block<T>> {
// SAFETY: This is safe because:
// [1] `Block::next` (AtomicPtr) may be safely zero initialized.
// [2] `Block::slots` (Array) may be safely zero initialized because of [3, 4].
// [3] `Slot::msg` (UnsafeCell) may be safely zero initialized because it
// holds a MaybeUninit.
// [4] `Slot::state` (AtomicUsize) may be safely zero initialized.
unsafe { MaybeUninit::zeroed().assume_init() }
unsafe { Box::new_zeroed().assume_init() }
}

/// Waits until the next pointer is set.
Expand Down Expand Up @@ -199,13 +199,13 @@ impl<T> Channel<T> {
// If we're going to have to install the next block, allocate it in advance in order to
// make the wait for other threads as short as possible.
if offset + 1 == BLOCK_CAP && next_block.is_none() {
next_block = Some(Box::new(Block::<T>::new()));
next_block = Some(Block::<T>::new());
}

// If this is the first message to be sent into the channel, we need to allocate the
// first block and install it.
if block.is_null() {
let new = Box::into_raw(Box::new(Block::<T>::new()));
let new = Box::into_raw(Block::<T>::new());

if self
.tail
Expand Down
29 changes: 29 additions & 0 deletions tests/ui/std/channel-stack-overflow-issue-102246.rs
Copy link
Member

Choose a reason for hiding this comment

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

is there a reason this can't be a std unit test?

Copy link
Member Author

Choose a reason for hiding this comment

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

I couldn't get the old code to fail that way, I think because std tests are still built with optimization, which may be able avoid the stack slot altogether.

Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
//@ run-pass
//@ needs-threads
//@ compile-flags: -Copt-level=0

// The channel's `Block::new` was causing a stack overflow because it held 32 item slots, which is
// 1MiB for this test's `BigStruct` -- instantiated on the stack before moving to `Box::new`.
//
// That block is now initialized directly on the heap.
//
// Ref: https://github.com/rust-lang/rust/issues/102246

use std::sync::mpsc::channel;
use std::thread;

const N: usize = 32_768;
struct BigStruct {
_data: [u8; N],
}

fn main() {
let (sender, receiver) = channel::<BigStruct>();

let thread1 = thread::spawn(move || {
sender.send(BigStruct { _data: [0u8; N] }).unwrap();
});

thread1.join().unwrap();
for _data in receiver.try_iter() {}
}
Loading