Skip to content

Commit 734b4a6

Browse files
committed
manual death signal
1 parent f322d86 commit 734b4a6

File tree

7 files changed

+42
-17
lines changed

7 files changed

+42
-17
lines changed

src/alloc_addresses/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -480,7 +480,7 @@ pub trait EvalContextExt<'tcx>: crate::MiriInterpCxExt<'tcx> {
480480
/// provenances should be exposed. Note that if `prepare_exposed_for_native_call` was not
481481
/// called before the FFI (with `paranoid` set to false) then some of the writes may be
482482
/// lost!
483-
#[cfg(all(unix, any(target_arch = "x86", target_arch = "x86_64")))]
483+
#[cfg(target_os = "linux")]
484484
fn apply_events(&mut self, _events: crate::shims::trace::MemEvents) -> InterpResult<'tcx> {
485485
let this = self.eval_context_mut();
486486
let _exposed: Vec<AllocId> =

src/bin/miri.rs

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,15 @@ fn entry_fn(tcx: TyCtxt<'_>) -> (DefId, MiriEntryFnType) {
130130
}
131131
}
132132

133+
/// If for whatever reason the supervisor process exists but can't see that
134+
/// we died, inform it manually.
135+
#[inline]
136+
fn exit(return_code: i32) -> ! {
137+
#[cfg(target_os = "linux")]
138+
miri::kill_sv(return_code);
139+
std::process::exit(return_code)
140+
}
141+
133142
impl rustc_driver::Callbacks for MiriCompilerCalls {
134143
fn config(&mut self, config: &mut Config) {
135144
config.override_queries = Some(|_, providers| {
@@ -213,7 +222,7 @@ impl rustc_driver::Callbacks for MiriCompilerCalls {
213222
if !many_seeds.keep_going {
214223
// `abort_if_errors` would actually not stop, since `par_for_each` waits for the
215224
// rest of the to finish, so we just exit immediately.
216-
std::process::exit(return_code);
225+
exit(return_code);
217226
}
218227
exit_code.store(return_code, Ordering::Relaxed);
219228
num_failed.fetch_add(1, Ordering::Relaxed);
@@ -223,15 +232,15 @@ impl rustc_driver::Callbacks for MiriCompilerCalls {
223232
if num_failed > 0 {
224233
eprintln!("{num_failed}/{total} SEEDS FAILED", total = many_seeds.seeds.count());
225234
}
226-
std::process::exit(exit_code.0.into_inner());
235+
exit(exit_code.0.into_inner());
227236
} else {
228237
let return_code = miri::eval_entry(tcx, entry_def_id, entry_type, &config, None)
229238
.unwrap_or_else(|| {
230239
tcx.dcx().abort_if_errors();
231240
rustc_driver::EXIT_FAILURE
232241
});
233242

234-
std::process::exit(return_code);
243+
exit(return_code);
235244
}
236245

237246
// Unreachable.
@@ -328,7 +337,7 @@ impl rustc_driver::Callbacks for MiriBeRustCompilerCalls {
328337

329338
fn show_error(msg: &impl std::fmt::Display) -> ! {
330339
eprintln!("fatal error: {msg}");
331-
std::process::exit(1)
340+
exit(1)
332341
}
333342

334343
macro_rules! show_error {
@@ -402,7 +411,7 @@ fn run_compiler_and_exit(
402411
// Invoke compiler, and handle return code.
403412
let exit_code =
404413
rustc_driver::catch_with_exit_code(move || rustc_driver::run_compiler(args, callbacks));
405-
std::process::exit(exit_code)
414+
exit(exit_code)
406415
}
407416

408417
/// Parses a comma separated list of `T` from the given string:
@@ -481,7 +490,7 @@ fn main() {
481490
let env_snapshot = env::vars_os().collect::<Vec<_>>();
482491

483492
let args = rustc_driver::catch_fatal_errors(|| rustc_driver::args::raw_args(&early_dcx))
484-
.unwrap_or_else(|_| std::process::exit(rustc_driver::EXIT_FAILURE));
493+
.unwrap_or_else(|_| exit(rustc_driver::EXIT_FAILURE));
485494

486495
// Install the ctrlc handler that sets `rustc_const_eval::CTRL_C_RECEIVED`, even if
487496
// MIRI_BE_RUSTC is set.

src/lib.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ mod borrow_tracker;
7676
mod clock;
7777
mod concurrency;
7878
mod diagnostics;
79-
#[cfg(all(unix, any(target_arch = "x86", target_arch = "x86_64")))]
79+
#[cfg(target_os = "linux")]
8080
mod discrete_alloc;
8181
mod eval;
8282
mod helpers;
@@ -99,6 +99,10 @@ pub use rustc_const_eval::interpret::{self, AllocMap, Provenance as _};
9999
use rustc_middle::{bug, span_bug};
100100
use tracing::{info, trace};
101101

102+
// Let bin/miri.rs kill the supervisor process.
103+
#[cfg(target_os = "linux")]
104+
pub use crate::shims::trace::kill_sv;
105+
102106
// Type aliases that set the provenance parameter.
103107
pub type Pointer = interpret::Pointer<Option<machine::Provenance>>;
104108
pub type StrictPointer = interpret::Pointer<machine::Provenance>;

src/machine.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -739,7 +739,7 @@ impl<'tcx> MiriMachine<'tcx> {
739739
// undefined behaviour in Miri itself!
740740
(
741741
unsafe {
742-
#[cfg(all(unix, any(target_arch = "x86", target_arch = "x86_64")))]
742+
#[cfg(target_os = "linux")]
743743
discrete_alloc::MachineAlloc::enable();
744744
libloading::Library::new(lib_file_path)
745745
.expect("failed to read specified extern shared object file")

src/shims/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ pub mod os_str;
1919
pub mod panic;
2020
pub mod time;
2121
pub mod tls;
22-
#[cfg(all(unix, any(target_arch = "x86", target_arch = "x86_64")))]
22+
#[cfg(target_os = "linux")]
2323
pub mod trace;
2424

2525
pub use self::files::FdTable;

src/shims/native_lib.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -181,15 +181,15 @@ pub trait EvalContextExt<'tcx>: crate::MiriInterpCxExt<'tcx> {
181181
}
182182

183183
// Prepare all exposed memory, depending on whether we have a supervisor process.
184-
#[cfg(all(unix, any(target_arch = "x86", target_arch = "x86_64")))]
184+
#[cfg(target_os = "linux")]
185185
if super::trace::Supervisor::init().is_ok() {
186186
this.prepare_exposed_for_native_call(false)?;
187187
} else {
188188
//this.prepare_exposed_for_native_call(true)?;
189189
//eprintln!("Oh noes!")
190190
panic!("No ptrace!");
191191
}
192-
#[cfg(not(all(unix, any(target_arch = "x86", target_arch = "x86_64"))))]
192+
#[cfg(not(target_os = "linux"))]
193193
this.prepare_exposed_for_native_call(true)?;
194194

195195
// Convert them to `libffi::high::Arg` type.
@@ -200,7 +200,7 @@ pub trait EvalContextExt<'tcx>: crate::MiriInterpCxExt<'tcx> {
200200

201201
// Call the function and store output, depending on return type in the function signature.
202202
let ret = this.call_native_with_args(link_name, dest, code_ptr, libffi_args)?;
203-
#[cfg(all(unix, any(target_arch = "x86", target_arch = "x86_64")))]
203+
#[cfg(target_os = "linux")]
204204
if let Some(events) = super::trace::Supervisor::get_events() {
205205
this.apply_events(events)?;
206206
}
@@ -209,7 +209,7 @@ pub trait EvalContextExt<'tcx>: crate::MiriInterpCxExt<'tcx> {
209209
}
210210
}
211211

212-
#[cfg(all(unix, any(target_arch = "x86", target_arch = "x86_64")))]
212+
#[cfg(target_os = "linux")]
213213
unsafe fn do_native_call<T: libffi::high::CType>(ptr: CodePtr, args: &[ffi::Arg<'_>]) -> T {
214214
use shims::trace::Supervisor;
215215

@@ -221,7 +221,7 @@ unsafe fn do_native_call<T: libffi::high::CType>(ptr: CodePtr, args: &[ffi::Arg<
221221
}
222222
}
223223

224-
#[cfg(not(all(unix, any(target_arch = "x86", target_arch = "x86_64"))))]
224+
#[cfg(not(target_os = "linux"))]
225225
#[inline(always)]
226226
unsafe fn do_native_call<T: libffi::high::CType>(ptr: CodePtr, args: &[ffi::Arg<'_>]) -> T {
227227
unsafe { ffi::call(ptr, args) }

src/shims/trace.rs

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,15 @@ pub struct Supervisor {
8282
r_event: ipc::IpcReceiver<MemEvents>,
8383
}
8484

85+
pub fn kill_sv(code: i32) {
86+
if let Ok(mut sv_guard) = SUPERVISOR.lock() {
87+
if let Some(sv) = sv_guard.take() {
88+
let _ = sv.t_message.send(TraceRequest::Die(code));
89+
*sv_guard = Some(sv);
90+
}
91+
}
92+
}
93+
8594
impl Supervisor {
8695
pub fn init() -> Result<(), ()> {
8796
let ptrace_status = std::fs::read_to_string("/proc/sys/kernel/yama/ptrace_scope");
@@ -185,7 +194,7 @@ impl Supervisor {
185194
pub enum TraceRequest {
186195
BeginFfi(Vec<u64>),
187196
EndFfi,
188-
Die,
197+
Die(i32),
189198
}
190199

191200
#[derive(serde::Serialize, serde::Deserialize, Debug)]
@@ -427,7 +436,10 @@ fn sv_loop(listener: ChildListener, t_event: ipc::IpcSender<MemEvents>) -> ! {
427436
signal::kill(main_pid, signal::SIGCONT).unwrap();
428437
}
429438

430-
TraceRequest::Die => break 'listen,
439+
TraceRequest::Die(child_code) => {
440+
retcode = child_code;
441+
break 'listen;
442+
}
431443
},
432444
ExecEvent::Died(child_code) => {
433445
if child_code != 0 {

0 commit comments

Comments
 (0)