Skip to content

Commit 765d6e6

Browse files
committed
Auto merge of #120326 - tmandry:abort-in-tests, r=<try>
Actually abort in -Zpanic-abort-tests When a test fails in panic=abort, it can be useful to have a debugger or other tooling hook into the `abort()` call for debugging. Doing this some other way would require it to hard code details of Rust's panic machinery. There's no reason we couldn't have done this in the first place; using a single exit code for "success" or "failed" was just simpler. Now we are aware of the special exit codes for posix and windows platforms, logging a special error if an unrecognized code is used on those platforms, and falling back to just "failure" on other platforms. This continues to account for `#[should_panic]` inside the test process itself, so there's no risk of misinterpreting a random call to `abort()` as an expected panic. Any exit code besides `TR_OK` is logged as a test failure. As an added benefit, this would allow us to support panic=immediate_abort (but not `#[should_panic]`), without noise about unexpected exit codes when a test fails.
2 parents 0c1fb2a + 9610404 commit 765d6e6

File tree

6 files changed

+42
-41
lines changed

6 files changed

+42
-41
lines changed

.github/workflows/ci.yml

+5
Original file line numberDiff line numberDiff line change
@@ -571,6 +571,11 @@ jobs:
571571
env:
572572
CODEGEN_BACKENDS: "llvm,cranelift"
573573
os: ubuntu-20.04-16core-64gb
574+
- name: x86_64-msvc
575+
env:
576+
RUST_CONFIGURE_ARGS: "--build=x86_64-pc-windows-msvc --enable-profiler"
577+
SCRIPT: make ci-msvc
578+
os: windows-2019-8core-32gb
574579
timeout-minutes: 600
575580
runs-on: "${{ matrix.os }}"
576581
steps:

library/test/src/helpers/exit_code.rs

-20
This file was deleted.

library/test/src/helpers/mod.rs

-1
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,5 @@
22
//! but used in `libtest`.
33
44
pub mod concurrency;
5-
pub mod exit_code;
65
pub mod metrics;
76
pub mod shuffle;

library/test/src/lib.rs

+2-13
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,6 @@ mod tests;
8585
use core::any::Any;
8686
use event::{CompletedTest, TestEvent};
8787
use helpers::concurrency::get_concurrency;
88-
use helpers::exit_code::get_exit_code;
8988
use helpers::shuffle::{get_shuffle_seed, shuffle_tests};
9089
use options::RunStrategy;
9190
use test_result::*;
@@ -712,17 +711,7 @@ fn spawn_test_subprocess(
712711
formatters::write_stderr_delimiter(&mut test_output, &desc.name);
713712
test_output.extend_from_slice(&stderr);
714713

715-
let result = match (|| -> Result<TestResult, String> {
716-
let exit_code = get_exit_code(status)?;
717-
Ok(get_result_from_exit_code(&desc, exit_code, &time_opts, &exec_time))
718-
})() {
719-
Ok(r) => r,
720-
Err(e) => {
721-
write!(&mut test_output, "Unexpected error: {e}").unwrap();
722-
TrFailed
723-
}
724-
};
725-
714+
let result = get_result_from_exit_code(&desc, status, &time_opts, &exec_time);
726715
(result, test_output, exec_time)
727716
})();
728717

@@ -751,7 +740,7 @@ fn run_test_in_spawned_subprocess(desc: TestDesc, runnable_test: RunnableTest) -
751740
if let TrOk = test_result {
752741
process::exit(test_result::TR_OK);
753742
} else {
754-
process::exit(test_result::TR_FAILED);
743+
process::abort();
755744
}
756745
});
757746
let record_result2 = record_result.clone();

library/test/src/test_result.rs

+30-7
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,8 @@
11
use std::any::Any;
2+
use std::process::ExitStatus;
3+
4+
#[cfg(unix)]
5+
use std::os::unix::process::ExitStatusExt;
26

37
use super::bench::BenchSamples;
48
use super::options::ShouldPanic;
@@ -7,11 +11,16 @@ use super::types::TestDesc;
711

812
pub use self::TestResult::*;
913

10-
// Return codes for secondary process.
14+
// Return code for secondary process.
1115
// Start somewhere other than 0 so we know the return code means what we think
1216
// it means.
1317
pub const TR_OK: i32 = 50;
14-
pub const TR_FAILED: i32 = 51;
18+
19+
#[cfg(unix)]
20+
const SIGABRT: i32 = 6;
21+
22+
#[cfg(windows)]
23+
const EXIT_ABORTED: i32 = -1073740791;
1524

1625
#[derive(Debug, Clone, PartialEq)]
1726
pub enum TestResult {
@@ -81,14 +90,28 @@ pub fn calc_result<'a>(
8190
/// Creates a `TestResult` depending on the exit code of test subprocess.
8291
pub fn get_result_from_exit_code(
8392
desc: &TestDesc,
84-
code: i32,
93+
status: ExitStatus,
8594
time_opts: &Option<time::TestTimeOptions>,
8695
exec_time: &Option<time::TestExecTime>,
8796
) -> TestResult {
88-
let result = match code {
89-
TR_OK => TestResult::TrOk,
90-
TR_FAILED => TestResult::TrFailed,
91-
_ => TestResult::TrFailedMsg(format!("got unexpected return code {code}")),
97+
let result = match status.code() {
98+
Some(TR_OK) => TestResult::TrOk,
99+
#[cfg(windows)]
100+
Some(EXIT_ABORTED) => TestResult::TrFailed,
101+
#[cfg(unix)]
102+
None => match status.signal() {
103+
Some(SIGABRT) => TestResult::TrFailed,
104+
Some(signal) => {
105+
TestResult::TrFailedMsg(format!("child process exited with signal {signal}"))
106+
}
107+
None => unreachable!("status.code() returned None but status.signal() was None"),
108+
},
109+
#[cfg(not(unix))]
110+
None => TestResult::TrFailedMsg(format!("unknown return code")),
111+
#[cfg(any(windows, unix))]
112+
Some(code) => TestResult::TrFailedMsg(format!("got unexpected return code {code}")),
113+
#[cfg(not(any(windows, unix)))]
114+
Some(_) => TestResult::TrFailed,
92115
};
93116

94117
// If test is already failed (or allowed to fail), do not change the result.

src/ci/github-actions/ci.yml

+5
Original file line numberDiff line numberDiff line change
@@ -729,6 +729,11 @@ jobs:
729729
env:
730730
CODEGEN_BACKENDS: llvm,cranelift
731731
<<: *job-linux-16c
732+
- name: x86_64-msvc
733+
env:
734+
RUST_CONFIGURE_ARGS: --build=x86_64-pc-windows-msvc --enable-profiler
735+
SCRIPT: make ci-msvc
736+
<<: *job-windows-8c
732737

733738

734739
master:

0 commit comments

Comments
 (0)