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

#[should_panic] stopped catching panics from extern "C" functions #130864

Closed
str4d opened this issue Sep 26, 2024 · 5 comments
Closed

#[should_panic] stopped catching panics from extern "C" functions #130864

str4d opened this issue Sep 26, 2024 · 5 comments
Labels
C-discussion Category: Discussion or questions that doesn't represent real issues.

Comments

@str4d
Copy link
Contributor

str4d commented Sep 26, 2024

Code

Minimal reproduction (playground):

#[no_mangle]
extern "C" fn function_with_panic(buf: *mut u8) {
    assert!(!buf.is_null());
    println!("Yay I have a buffer!");
}

#[cfg(test)]
mod tests {
    use super::function_with_panic;

    #[test]
    #[should_panic]
    fn test_function_panics() {
        function_with_panic(std::ptr::null_mut());
    }
}

Our codebase has a test that checks passing a null ptr into an extern "C" function causes it to panic. The test now panics, i.e. #[should_panic] is not capturing the panic.

Version it worked on

It most recently worked on: 1.80.1

Version with regression

rustc --version --verbose:

rustc 1.81.0 (eeb90cda1 2024-09-04)
binary: rustc
commit-hash: eeb90cda1969383f56a2637cbd3037bdf598841c
commit-date: 2024-09-04
host: x86_64-unknown-linux-gnu
release: 1.81.0
LLVM version: 18.1.7

Backtrace

Backtrace

     Running tests/foobar.rs (target/debug/deps/foobar-4acf46662842063f)

running 1 test
thread 'tests::test_function_panics' panicked at tests/foobar.rs:3:5:
assertion failed: !buf.is_null()
stack backtrace:
   0: rust_begin_unwind
             at /rustc/eeb90cda1969383f56a2637cbd3037bdf598841c/library/std/src/panicking.rs:665:5
   1: core::panicking::panic_fmt
             at /rustc/eeb90cda1969383f56a2637cbd3037bdf598841c/library/core/src/panicking.rs:74:14
   2: core::panicking::panic
             at /rustc/eeb90cda1969383f56a2637cbd3037bdf598841c/library/core/src/panicking.rs:148:5
   3: function_with_panic
             at ./tests/foobar.rs:3:5
   4: foobar::tests::test_function_panics
             at ./tests/foobar.rs:14:9
   5: foobar::tests::test_function_panics::{{closure}}
             at ./tests/foobar.rs:13:30
   6: core::ops::function::FnOnce::call_once
             at /rustc/eeb90cda1969383f56a2637cbd3037bdf598841c/library/core/src/ops/function.rs:250:5
   7: core::ops::function::FnOnce::call_once
             at /rustc/eeb90cda1969383f56a2637cbd3037bdf598841c/library/core/src/ops/function.rs:250:5
note: Some details are omitted, run with `RUST_BACKTRACE=full` for a verbose backtrace.
thread 'tests::test_function_panics' panicked at library/core/src/panicking.rs:221:5:
panic in a function that cannot unwind
stack backtrace:
   0:     0x5555555cfe45 - std::backtrace_rs::backtrace::libunwind::trace::h649ab3318d3445c5
                               at /rustc/eeb90cda1969383f56a2637cbd3037bdf598841c/library/std/src/../../backtrace/src/backtrace/libunwind.rs:116:5
   1:     0x5555555cfe45 - std::backtrace_rs::backtrace::trace_unsynchronized::hf4bb60c3387150c3
                               at /rustc/eeb90cda1969383f56a2637cbd3037bdf598841c/library/std/src/../../backtrace/src/backtrace/mod.rs:66:5
   2:     0x5555555cfe45 - std::sys::backtrace::_print_fmt::hd9186c800e44bd00
                               at /rustc/eeb90cda1969383f56a2637cbd3037bdf598841c/library/std/src/sys/backtrace.rs:65:5
   3:     0x5555555cfe45 - <std::sys::backtrace::BacktraceLock::print::DisplayBacktrace as core::fmt::Display>::fmt::h1b9dad2a88e955ff
                               at /rustc/eeb90cda1969383f56a2637cbd3037bdf598841c/library/std/src/sys/backtrace.rs:40:26
   4:     0x5555555f532b - core::fmt::rt::Argument::fmt::h351a7824f737a6a0
                               at /rustc/eeb90cda1969383f56a2637cbd3037bdf598841c/library/core/src/fmt/rt.rs:173:76
   5:     0x5555555f532b - core::fmt::write::h4b5a1270214bc4a7
                               at /rustc/eeb90cda1969383f56a2637cbd3037bdf598841c/library/core/src/fmt/mod.rs:1182:21
   6:     0x5555555cd80f - std::io::Write::write_fmt::hd04af345a50c312d
                               at /rustc/eeb90cda1969383f56a2637cbd3037bdf598841c/library/std/src/io/mod.rs:1827:15
   7:     0x5555555d1661 - std::sys::backtrace::BacktraceLock::print::h68d41b51481bce5c
                               at /rustc/eeb90cda1969383f56a2637cbd3037bdf598841c/library/std/src/sys/backtrace.rs:43:9
   8:     0x5555555d1661 - std::panicking::default_hook::{{closure}}::h96ab15e9936be7ed
                               at /rustc/eeb90cda1969383f56a2637cbd3037bdf598841c/library/std/src/panicking.rs:269:22
   9:     0x5555555d133c - std::panicking::default_hook::h3cacb9c27561ad33
                               at /rustc/eeb90cda1969383f56a2637cbd3037bdf598841c/library/std/src/panicking.rs:296:9
  10:     0x55555559e24a - <alloc::boxed::Box<F,A> as core::ops::function::Fn<Args>>::call::hd212b1446b2b2077
                               at /rustc/eeb90cda1969383f56a2637cbd3037bdf598841c/library/alloc/src/boxed.rs:2084:9
  11:     0x55555559e24a - test::test_main::{{closure}}::hd15ff34f3f68988b
                               at /rustc/eeb90cda1969383f56a2637cbd3037bdf598841c/library/test/src/lib.rs:136:21
  12:     0x5555555d1eff - <alloc::boxed::Box<F,A> as core::ops::function::Fn<Args>>::call::hce7569f4ca5d1b64
                               at /rustc/eeb90cda1969383f56a2637cbd3037bdf598841c/library/alloc/src/boxed.rs:2084:9
  13:     0x5555555d1eff - std::panicking::rust_panic_with_hook::hfe205f6954b2c97b
                               at /rustc/eeb90cda1969383f56a2637cbd3037bdf598841c/library/std/src/panicking.rs:808:13
  14:     0x5555555d1af3 - std::panicking::begin_panic_handler::{{closure}}::h6cb44b3a50f28c44
                               at /rustc/eeb90cda1969383f56a2637cbd3037bdf598841c/library/std/src/panicking.rs:667:13
  15:     0x5555555d0309 - std::sys::backtrace::__rust_end_short_backtrace::hf1c1f2a92799bb0e
                               at /rustc/eeb90cda1969383f56a2637cbd3037bdf598841c/library/std/src/sys/backtrace.rs:168:18
  16:     0x5555555d17b4 - rust_begin_unwind
                               at /rustc/eeb90cda1969383f56a2637cbd3037bdf598841c/library/std/src/panicking.rs:665:5
  17:     0x555555567275 - core::panicking::panic_nounwind_fmt::runtime::h907a0444fa61a6dc
                               at /rustc/eeb90cda1969383f56a2637cbd3037bdf598841c/library/core/src/panicking.rs:112:18
  18:     0x555555567275 - core::panicking::panic_nounwind_fmt::h4c4dc67d0bbc166c
                               at /rustc/eeb90cda1969383f56a2637cbd3037bdf598841c/library/core/src/panicking.rs:122:5
  19:     0x555555567302 - core::panicking::panic_nounwind::hb98133c151c787e4
                               at /rustc/eeb90cda1969383f56a2637cbd3037bdf598841c/library/core/src/panicking.rs:221:5
  20:     0x5555555673e6 - core::panicking::panic_cannot_unwind::he9511e6e72319a3e
                               at /rustc/eeb90cda1969383f56a2637cbd3037bdf598841c/library/core/src/panicking.rs:309:5
  21:     0x55555556847d - function_with_panic
                               at /home/str4d/dev/rust/smol/foobar/tests/foobar.rs:2:1
  22:     0x555555568df0 - foobar::tests::test_function_panics::h5b23a1242e1adc78
                               at /home/str4d/dev/rust/smol/foobar/tests/foobar.rs:14:9
  23:     0x555555567ac7 - foobar::tests::test_function_panics::{{closure}}::h236e06321c7a0225
                               at /home/str4d/dev/rust/smol/foobar/tests/foobar.rs:13:30
  24:     0x555555568a96 - core::ops::function::FnOnce::call_once::hb5bf55494e5b6942
                               at /rustc/eeb90cda1969383f56a2637cbd3037bdf598841c/library/core/src/ops/function.rs:250:5
  25:     0x5555555a288b - core::ops::function::FnOnce::call_once::h81f56a195fe4862e
                               at /rustc/eeb90cda1969383f56a2637cbd3037bdf598841c/library/core/src/ops/function.rs:250:5
  26:     0x5555555a288b - test::__rust_begin_short_backtrace::h919c79c8b896f9e2
                               at /rustc/eeb90cda1969383f56a2637cbd3037bdf598841c/library/test/src/lib.rs:624:18
  27:     0x5555555a2135 - test::run_test_in_process::{{closure}}::h7b3d5751c5b4dd75
                               at /rustc/eeb90cda1969383f56a2637cbd3037bdf598841c/library/test/src/lib.rs:647:60
  28:     0x5555555a2135 - <core::panic::unwind_safe::AssertUnwindSafe<F> as core::ops::function::FnOnce<()>>::call_once::hdabd61465e4dbd80
                               at /rustc/eeb90cda1969383f56a2637cbd3037bdf598841c/library/core/src/panic/unwind_safe.rs:272:9
  29:     0x5555555a2135 - std::panicking::try::do_call::hc813c79fd64b0a90
                               at /rustc/eeb90cda1969383f56a2637cbd3037bdf598841c/library/std/src/panicking.rs:557:40
  30:     0x5555555a2135 - std::panicking::try::h055c5de7e7bfc209
                               at /rustc/eeb90cda1969383f56a2637cbd3037bdf598841c/library/std/src/panicking.rs:521:19
  31:     0x5555555a2135 - std::panic::catch_unwind::h4265d6525195c807
                               at /rustc/eeb90cda1969383f56a2637cbd3037bdf598841c/library/std/src/panic.rs:350:14
  32:     0x5555555a2135 - test::run_test_in_process::he72c277a35f96567
                               at /rustc/eeb90cda1969383f56a2637cbd3037bdf598841c/library/test/src/lib.rs:647:27
  33:     0x5555555a2135 - test::run_test::{{closure}}::h974e632522c0fbcf
                               at /rustc/eeb90cda1969383f56a2637cbd3037bdf598841c/library/test/src/lib.rs:568:43
  34:     0x55555556a124 - test::run_test::{{closure}}::hdc2c89ce8b601dda
                               at /rustc/eeb90cda1969383f56a2637cbd3037bdf598841c/library/test/src/lib.rs:598:41
  35:     0x55555556a124 - std::sys::backtrace::__rust_begin_short_backtrace::h342cb8e53aeb2076
                               at /rustc/eeb90cda1969383f56a2637cbd3037bdf598841c/library/std/src/sys/backtrace.rs:152:18
  36:     0x55555556d852 - std::thread::Builder::spawn_unchecked_::{{closure}}::{{closure}}::h67b1b5c1709ad95b
                               at /rustc/eeb90cda1969383f56a2637cbd3037bdf598841c/library/std/src/thread/mod.rs:538:17
  37:     0x55555556d852 - <core::panic::unwind_safe::AssertUnwindSafe<F> as core::ops::function::FnOnce<()>>::call_once::hd8c7a030ea8b7676
                               at /rustc/eeb90cda1969383f56a2637cbd3037bdf598841c/library/core/src/panic/unwind_safe.rs:272:9
  38:     0x55555556d852 - std::panicking::try::do_call::h512c2ab2c15b7d31
                               at /rustc/eeb90cda1969383f56a2637cbd3037bdf598841c/library/std/src/panicking.rs:557:40
  39:     0x55555556d852 - std::panicking::try::h5c2903f8937bc868
                               at /rustc/eeb90cda1969383f56a2637cbd3037bdf598841c/library/std/src/panicking.rs:521:19
  40:     0x55555556d852 - std::panic::catch_unwind::h242c80217c2dbece
                               at /rustc/eeb90cda1969383f56a2637cbd3037bdf598841c/library/std/src/panic.rs:350:14
  41:     0x55555556d852 - std::thread::Builder::spawn_unchecked_::{{closure}}::h6cb4494ebdd8caf7
                               at /rustc/eeb90cda1969383f56a2637cbd3037bdf598841c/library/std/src/thread/mod.rs:537:30
  42:     0x55555556d852 - core::ops::function::FnOnce::call_once{{vtable.shim}}::h42193b008049ba94
                               at /rustc/eeb90cda1969383f56a2637cbd3037bdf598841c/library/core/src/ops/function.rs:250:5
  43:     0x5555555d5fcb - <alloc::boxed::Box<F,A> as core::ops::function::FnOnce<Args>>::call_once::ha1963004222e7822
                               at /rustc/eeb90cda1969383f56a2637cbd3037bdf598841c/library/alloc/src/boxed.rs:2070:9
  44:     0x5555555d5fcb - <alloc::boxed::Box<F,A> as core::ops::function::FnOnce<Args>>::call_once::h1086ced1f7c494c2
                               at /rustc/eeb90cda1969383f56a2637cbd3037bdf598841c/library/alloc/src/boxed.rs:2070:9
  45:     0x5555555d5fcb - std::sys::pal::unix::thread::Thread::new::thread_start::ha8af9c992ef0b208
                               at /rustc/eeb90cda1969383f56a2637cbd3037bdf598841c/library/std/src/sys/pal/unix/thread.rs:108:17
  46:     0x7ffff7c94ac3 - start_thread
                               at ./nptl/pthread_create.c:442:8
  47:     0x7ffff7d26850 - __GI___clone3
                               at ./misc/../sysdeps/unix/sysv/linux/x86_64/clone3.S:81
  48:                0x0 - <unknown>
thread caused non-unwinding panic. aborting.
error: test failed, to rerun pass `--test foobar`

@str4d str4d added C-bug Category: This is a bug. regression-untriaged Untriaged performance or correctness regression. labels Sep 26, 2024
@rustbot rustbot added I-prioritize Issue: Indicates that prioritization has been requested for this issue. needs-triage This issue may need triage. Remove it if it has been sufficiently triaged. labels Sep 26, 2024
@str4d
Copy link
Contributor Author

str4d commented Sep 26, 2024

Possibly related: #130024 (that issue involves an allocator, but is also about #[should_panic] not capturing a panic and was also reported in the past few weeks).

@cuviper
Copy link
Member

cuviper commented Sep 26, 2024

The test now panics, i.e. #[should_panic] is not capturing the panic.

The message of that panic is important: "panic in a function that cannot unwind" -- and since it cannot unwind, it aborts. Thus the test harness has no chance to catch it.

This is an intended consequence of #116088 that extern "C" is not allowed to unwind, but your test works with extern "C-unwind", which was stabilized since 1.71.

@cuviper cuviper added the F-c_unwind `#![feature(c_unwind)]` label Sep 26, 2024
@str4d
Copy link
Contributor Author

str4d commented Sep 26, 2024

Aha! This was mentioned in the 1.81.0 release blog post, but when I looked there I didn't realise that's what the problem was, because I was focused on the #[should_panic] and there was no mention of it (and right beforehand was mention of changes to the panic handler, which is why I focused on #[should_panic]); it was only when I later went to make a minimal reproduction that I tied it to extern "C".

@str4d
Copy link
Contributor Author

str4d commented Sep 26, 2024

If this is expected then I'm happy for this issue to just be closed. But perhaps there's a slightly less noisy way the test can report its error? Without RUST_BACKTRACE=1 I still got a long backtrace from the test harness itself.

@workingjubilee
Copy link
Member

I'm not sure how we would do that without a total rewrite of libtest, which may or may not be happening but in any case is beyond the scope of this issue, so I'm closing this as wontfix (since the sudden abrupt detonation of threads that panic inside extern "C" fn is intended, and libtest sucking even harder than usual is just collateral damage).

@workingjubilee workingjubilee closed this as not planned Won't fix, can't repro, duplicate, stale Sep 26, 2024
@jieyouxu jieyouxu added C-discussion Category: Discussion or questions that doesn't represent real issues. and removed C-bug Category: This is a bug. I-prioritize Issue: Indicates that prioritization has been requested for this issue. F-c_unwind `#![feature(c_unwind)]` regression-untriaged Untriaged performance or correctness regression. needs-triage This issue may need triage. Remove it if it has been sufficiently triaged. labels Sep 26, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
C-discussion Category: Discussion or questions that doesn't represent real issues.
Projects
None yet
Development

No branches or pull requests

5 participants