about summary refs log tree commit diff
path: root/tests/ui/std/channel-stack-overflow-issue-102246.rs
diff options
context:
space:
mode:
authorJosh Stone <jistone@redhat.com>2024-11-07 09:51:52 -0800
committerJosh Stone <jistone@redhat.com>2024-11-07 10:09:45 -0800
commit03383ad1021bd0ba34c28d7fda16e634a9ec8df4 (patch)
treef87b1a10d1601ed6431008e08e3c80ffc49afaa0 /tests/ui/std/channel-stack-overflow-issue-102246.rs
parent3d1dba830a564d1118361345d7ada47a05241f45 (diff)
downloadrust-03383ad1021bd0ba34c28d7fda16e634a9ec8df4.tar.gz
rust-03383ad1021bd0ba34c28d7fda16e634a9ec8df4.zip
Initialize channel `Block`s directly on the heap
The channel's `Block::new` was causing a stack overflow because it held
32 item slots, instantiated on the stack before moving to `Box::new`.
The 32x multiplier made modestly-large item sizes untenable.

That block is now initialized directly on the heap.

Fixes #102246
Diffstat (limited to 'tests/ui/std/channel-stack-overflow-issue-102246.rs')
-rw-r--r--tests/ui/std/channel-stack-overflow-issue-102246.rs28
1 files changed, 28 insertions, 0 deletions
diff --git a/tests/ui/std/channel-stack-overflow-issue-102246.rs b/tests/ui/std/channel-stack-overflow-issue-102246.rs
new file mode 100644
index 00000000000..52902fc563a
--- /dev/null
+++ b/tests/ui/std/channel-stack-overflow-issue-102246.rs
@@ -0,0 +1,28 @@
+//@ run-pass
+//@ 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() {}
+}