Skip to content

Commit b916a07

Browse files
authored
Merge pull request #702 from RalfJung/exit
implement exit
2 parents 1c07cd5 + aaa8ee7 commit b916a07

13 files changed

+29
-16
lines changed

rust-version

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
130dc3e7dac132cf30272ccf4541b512828e2108
1+
9224be5fa39f6170f6e046342976efee5453a1ff

src/fn_call.rs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -149,11 +149,16 @@ pub trait EvalContextExt<'a, 'mir, 'tcx: 'a + 'mir>: crate::MiriEvalContextExt<'
149149
let link_name = link_name.get().trim_end_matches("$UNIX2003");
150150
let tcx = &{this.tcx.tcx};
151151

152-
// First: functions that could diverge.
152+
// First: functions that diverge.
153153
match link_name {
154154
"__rust_start_panic" | "panic_impl" => {
155155
return err!(MachineError("the evaluated program panicked".to_string()));
156156
}
157+
"exit" | "ExitProcess" => {
158+
// it's really u32 for ExitProcess, but we have to put it into the `Exit` error variant anyway
159+
let code = this.read_scalar(args[0])?.to_i32()?;
160+
return err!(Exit(code));
161+
}
157162
_ => if dest.is_none() {
158163
return err!(Unimplemented(
159164
format!("can't call diverging foreign function: {}", link_name),

src/lib.rs

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -242,6 +242,13 @@ pub fn eval_main<'a, 'tcx: 'a>(
242242
}
243243
}
244244
Err(mut e) => {
245+
// Special treatment for some error kinds
246+
let msg = match e.kind {
247+
InterpError::Exit(code) => std::process::exit(code),
248+
InterpError::NoMirFor(..) =>
249+
format!("{}. Did you set `MIRI_SYSROOT` to a Miri-enabled sysroot? You can prepare one with `cargo miri setup`.", e),
250+
_ => e.to_string()
251+
};
245252
e.print_backtrace();
246253
if let Some(frame) = ecx.stack().last() {
247254
let block = &frame.mir.basic_blocks()[frame.block];
@@ -251,11 +258,10 @@ pub fn eval_main<'a, 'tcx: 'a>(
251258
block.terminator().source_info.span
252259
};
253260

254-
let e = e.to_string();
255-
let msg = format!("constant evaluation error: {}", e);
261+
let msg = format!("Miri evaluation error: {}", msg);
256262
let mut err = struct_error(ecx.tcx.tcx.at(span), msg.as_str());
257263
let frames = ecx.generate_stacktrace(None);
258-
err.span_label(span, e);
264+
err.span_label(span, msg);
259265
// We iterate with indices because we need to look at the next frame (the caller).
260266
for idx in 0..frames.len() {
261267
let frame_info = &frames[idx];
@@ -269,7 +275,7 @@ pub fn eval_main<'a, 'tcx: 'a>(
269275
}
270276
err.emit();
271277
} else {
272-
ecx.tcx.sess.err(&e.to_string());
278+
ecx.tcx.sess.err(&msg);
273279
}
274280

275281
for (i, frame) in ecx.stack().iter().enumerate() {

tests/compile-fail/ctlz_nonzero.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,6 @@ pub fn main() {
1010
unsafe {
1111
use crate::rusti::*;
1212

13-
ctlz_nonzero(0u8); //~ ERROR constant evaluation error: ctlz_nonzero called on 0
13+
ctlz_nonzero(0u8); //~ ERROR ctlz_nonzero called on 0
1414
}
1515
}

tests/compile-fail/cttz_nonzero.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,6 @@ pub fn main() {
1010
unsafe {
1111
use crate::rusti::*;
1212

13-
cttz_nonzero(0u8); //~ ERROR constant evaluation error: cttz_nonzero called on 0
13+
cttz_nonzero(0u8); //~ ERROR cttz_nonzero called on 0
1414
}
1515
}

tests/compile-fail/deref_fn_ptr.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ fn f() {}
22

33
fn main() {
44
let x: u8 = unsafe {
5-
*std::mem::transmute::<fn(), *const u8>(f) //~ ERROR constant evaluation error: tried to dereference a function pointer
5+
*std::mem::transmute::<fn(), *const u8>(f) //~ ERROR tried to dereference a function pointer
66
};
77
panic!("this should never print: {}", x);
88
}

tests/compile-fail/getrandom.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,6 @@ fn main() {
88
let mut buf = [0u8; 5];
99
unsafe {
1010
libc::syscall(libc::SYS_getrandom, buf.as_mut_ptr() as *mut libc::c_void, 5 as libc::size_t, 0 as libc::c_uint);
11-
//~^ ERROR constant evaluation error: miri does not support gathering system entropy in deterministic mode!
11+
//~^ ERROR miri does not support gathering system entropy in deterministic mode!
1212
}
1313
}

tests/compile-fail/never_transmute_humans.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ struct Human;
77

88
fn main() {
99
let _x: ! = unsafe {
10-
std::mem::transmute::<Human, !>(Human) //~ ERROR constant evaluation error
11-
//^~ NOTE entered unreachable code
10+
std::mem::transmute::<Human, !>(Human) //~ ERROR entered unreachable code
1211
};
1312
}
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
fn main() {
2-
let x: i32 = unsafe { *std::ptr::null() }; //~ ERROR constant evaluation error: invalid use of NULL pointer
2+
let x: i32 = unsafe { *std::ptr::null() }; //~ ERROR invalid use of NULL pointer
33
panic!("this should never print: {}", x);
44
}
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
fn main() {
2-
let x: () = unsafe { *std::ptr::null() }; //~ ERROR constant evaluation error: invalid use of NULL pointer
2+
let x: () = unsafe { *std::ptr::null() }; //~ ERROR invalid use of NULL pointer
33
panic!("this should never print: {:?}", x);
44
}

0 commit comments

Comments
 (0)