rust-lang / rust

Empowering everyone to build reliable and efficient software.
https://www.rust-lang.org
Other
98.37k stars 12.72k forks source link

`should_panic` does not capture allocation-induced panics #130024

Open davidv1992 opened 2 months ago

davidv1992 commented 2 months ago

When writing tests for custom allocators, I ran into the problem that an allocation-induced panic is not captured properly by should_panic. This is in my opinion a bug, as the behaviour the test shows is still very much like a panic.

Example code to trigger the issue:

use core::alloc::Layout;
use alloc::alloc::handle_alloc_error

#[test]
#[should_panic]
fn sample_test() {
    let layout = Layout::new::<u64>();
    handle_alloc_error(layout);
}

this uses an explicit call to handle_alloc_error, but the actual test cases I am writing create Vectors with a custom allocator that limits the amount of memory that can be allocated, which is a more realistic use case.

Meta

rustc --version --verbose:

rustc 1.83.0-nightly (9c01301c5 2024-09-05)
binary: rustc
commit-hash: 9c01301c52df5d2d7b6fe337707a74e011d68d6f
commit-date: 2024-09-05
host: x86_64-unknown-linux-gnu
release: 1.83.0-nightly
LLVM version: 19.1.0
davidv1992 commented 2 months ago

Note, workaround for the time being is to add

std::alloc::set_alloc_error_hook(|layout| panic!("memory allocation of {} bytes failed", layout.size()));

to the start of each test case. Not ideal, but workable.

saethlin commented 2 months ago

The default alloc error handler creates a non-unwinding panic, which breaks #[should_panic], because that feature, like all of libtest, is based on catching unwinds.