Skip to content

Commit ba47433

Browse files
committed
Add ExitStatusExt::aborted_code for Fuchsia platform
1 parent 848c591 commit ba47433

File tree

7 files changed

+55
-14
lines changed

7 files changed

+55
-14
lines changed

compiler/rustc_feature/src/unstable.rs

+2
Original file line numberDiff line numberDiff line change
@@ -475,6 +475,8 @@ declare_features! (
475475
(unstable, fn_align, "1.53.0", Some(82232)),
476476
/// Support delegating implementation of functions to other already implemented functions.
477477
(incomplete, fn_delegation, "1.76.0", Some(118212)),
478+
/// Support ExitStatusExt::aborted_code on Fuchsia platform.
479+
(unstable, fuchsia_exit_status_aborted, "1.79.0", None),
478480
/// Allows impls for the Freeze trait.
479481
(internal, freeze_impls, "1.78.0", Some(121675)),
480482
/// Allows defining gen blocks and `gen fn`.

compiler/rustc_span/src/symbol.rs

+1
Original file line numberDiff line numberDiff line change
@@ -931,6 +931,7 @@ symbols! {
931931
fs_create_dir,
932932
fsub_algebraic,
933933
fsub_fast,
934+
fuchsia_exit_status_aborted,
934935
fundamental,
935936
fused_iterator,
936937
future,

library/std/src/os/fuchsia/mod.rs

+2
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,6 @@
33
#![stable(feature = "raw_ext", since = "1.1.0")]
44

55
pub mod fs;
6+
#[unstable(feature = "fuchsia_exit_status_aborted", issue = "none")]
7+
pub mod process;
68
pub mod raw;

library/std/src/os/fuchsia/process.rs

+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
//! Fuchsia-specific extensions to primitives in the [`std::process`] module.
2+
//!
3+
//! [`std::process`]: crate::process
4+
5+
use crate::process;
6+
use crate::sealed::Sealed;
7+
8+
#[unstable(feature = "fuchsia_exit_status_aborted", issue = "none")]
9+
pub trait ExitStatusExt: Sealed {
10+
/// If the process was aborted, returns the status code.
11+
#[must_use]
12+
fn aborted_code(&self) -> Option<i32>;
13+
}
14+
15+
#[unstable(feature = "fuchsia_exit_status_aborted", issue = "none")]
16+
impl ExitStatusExt for process::ExitStatus {
17+
/// If the process was aborted, returns the status code.
18+
fn aborted_code(&self) -> Option<i32> {
19+
match self.code() {
20+
// Upon an abort, Fuchsia returns the status code ZX_TASK_RETCODE_EXCEPTION_KILL.
21+
code @ Some(-1028) => code,
22+
Some(_) | None => None,
23+
}
24+
}
25+
}

library/test/src/lib.rs

+1
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
#![feature(process_exitcode_internals)]
2424
#![feature(panic_can_unwind)]
2525
#![feature(test)]
26+
#![cfg_attr(target_os = "fuchsia", feature(fuchsia_exit_status_aborted))]
2627
#![allow(internal_features)]
2728

2829
// Public reexports

library/test/src/test_result.rs

+15-13
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
use std::any::Any;
22
use std::process::ExitStatus;
33

4+
#[cfg(target_os = "fuchsia")]
5+
use std::os::fuchsia::process::ExitStatusExt as _;
46
#[cfg(unix)]
5-
use std::os::unix::process::ExitStatusExt;
7+
use std::os::unix::process::ExitStatusExt as _;
68

79
use super::bench::BenchSamples;
810
use super::options::ShouldPanic;
@@ -88,30 +90,30 @@ pub fn get_result_from_exit_code(
8890
time_opts: &Option<time::TestTimeOptions>,
8991
exec_time: &Option<time::TestExecTime>,
9092
) -> TestResult {
91-
let result = match status.code() {
93+
#[cfg(target_os = "fuchsia")]
94+
let result = status.aborted_code().map(|_| TestResult::TrFailed);
95+
#[cfg(not(target_os = "fuchsia"))]
96+
let result: Option<TestResult> = None;
97+
98+
let result = result.unwrap_or_else(|| match status.code() {
9299
Some(TR_OK) => TestResult::TrOk,
93100
// On Windows we use __fastfail to abort, which is documented to use this
94101
// exception code.
95102
#[cfg(windows)]
96103
Some(0xC0000409u32 as i32) => TestResult::TrFailed,
97-
#[cfg(unix)]
104+
#[cfg(any(windows, unix))]
105+
Some(code) => TestResult::TrFailedMsg(format!("got unexpected return code {code}")),
106+
#[cfg(not(any(windows, unix)))]
107+
Some(_) => TestResult::TrFailed,
98108
None => match status.signal() {
109+
#[cfg(unix)]
99110
Some(libc::SIGABRT) => TestResult::TrFailed,
100111
Some(signal) => {
101112
TestResult::TrFailedMsg(format!("child process exited with signal {signal}"))
102113
}
103114
None => unreachable!("status.code() returned None but status.signal() was None"),
104115
},
105-
// Upon an abort, Fuchsia returns the status code ZX_TASK_RETCODE_EXCEPTION_KILL.
106-
#[cfg(target_os = "fuchsia")]
107-
Some(-1028) => TestResult::TrFailed,
108-
#[cfg(not(unix))]
109-
None => TestResult::TrFailedMsg(format!("unknown return code")),
110-
#[cfg(any(windows, unix))]
111-
Some(code) => TestResult::TrFailedMsg(format!("got unexpected return code {code}")),
112-
#[cfg(not(any(windows, unix)))]
113-
Some(_) => TestResult::TrFailed,
114-
};
116+
});
115117

116118
// If test is already failed (or allowed to fail), do not change the result.
117119
if result != TestResult::TrOk {

tests/ui/process/signal-exit-status.rs

+9-1
Original file line numberDiff line numberDiff line change
@@ -2,20 +2,28 @@
22
//@ ignore-wasm32 no processes
33
//@ ignore-sgx no processes
44
//@ ignore-windows
5-
//@ ignore-fuchsia code returned as ZX_TASK_RETCODE_EXCEPTION_KILL, FIXME (#58590)
65

76
#![feature(core_intrinsics)]
7+
#![cfg_attr(target_os = "fuchsia", feature(fuchsia_exit_status_aborted))]
88

99
use std::env;
1010
use std::process::Command;
1111

12+
#[cfg(target_os = "fuchsia")]
13+
use std::os::fuchsia::process::ExitStatusExt;
14+
1215
pub fn main() {
1316
let args: Vec<String> = env::args().collect();
1417
if args.len() >= 2 && args[1] == "signal" {
1518
// Raise an aborting signal without UB
1619
core::intrinsics::abort();
1720
} else {
21+
// Spawn a child process that will raise an aborting signal
1822
let status = Command::new(&args[0]).arg("signal").status().unwrap();
23+
24+
#[cfg(not(target_os = "fuchsia"))]
1925
assert!(status.code().is_none());
26+
#[cfg(target_os = "fuchsia")]
27+
assert!(status.aborted_code().is_some());
2028
}
2129
}

0 commit comments

Comments
 (0)