Skip to content

should_panic does not capture allocation-induced panics #130024

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

Open
davidv1992 opened this issue Sep 6, 2024 · 2 comments
Open

should_panic does not capture allocation-induced panics #130024

davidv1992 opened this issue Sep 6, 2024 · 2 comments
Labels
A-libtest Area: `#[test]` / the `test` library C-bug Category: This is a bug. T-libs Relevant to the library team, which will review and decide on the PR/issue.

Comments

@davidv1992
Copy link
Contributor

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 davidv1992 added the C-bug Category: This is a bug. label Sep 6, 2024
@rustbot rustbot added the needs-triage This issue may need triage. Remove it if it has been sufficiently triaged. label Sep 6, 2024
@davidv1992
Copy link
Contributor Author

davidv1992 commented Sep 6, 2024

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
Copy link
Member

saethlin commented Sep 6, 2024

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.

@saethlin saethlin added A-libtest Area: `#[test]` / the `test` library T-libs Relevant to the library team, which will review and decide on the PR/issue. and removed needs-triage This issue may need triage. Remove it if it has been sufficiently triaged. labels Sep 6, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-libtest Area: `#[test]` / the `test` library C-bug Category: This is a bug. T-libs Relevant to the library team, which will review and decide on the PR/issue.
Projects
Status: No status
Development

No branches or pull requests

3 participants