Skip to content

Commit 6b56aef

Browse files
committed
Auto merge of #1239 - elichai:2020-03-abort-intrinsics, r=RalfJung
Abort instead of panic on asserting intrinsics This fixes #1222 replacing the panic with an abort and a corresponding message. the stack trace is already printed, this just adds an optional message the caller can pass, and I just pass the same message we passed to the panic but now to the abort instead. r? @RalfJung
2 parents 5e7fd2b + 0826899 commit 6b56aef

File tree

8 files changed

+23
-62
lines changed

8 files changed

+23
-62
lines changed

src/diagnostics.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,8 @@ pub fn report_diagnostic<'tcx, 'mir>(
2020
let info = info.downcast_ref::<TerminationInfo>().expect("invalid MachineStop payload");
2121
match info {
2222
TerminationInfo::Exit(code) => return Some(*code),
23-
TerminationInfo::Abort => format!("the evaluated program aborted execution"),
23+
TerminationInfo::Abort(None) => format!("the evaluated program aborted execution"),
24+
TerminationInfo::Abort(Some(msg)) => format!("the evaluated program aborted execution: {}", msg),
2425
}
2526
}
2627
err_unsup!(NoMirFor(..)) => format!(

src/eval.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ impl Default for MiriConfig {
5454
/// Details of premature program termination.
5555
pub enum TerminationInfo {
5656
Exit(i64),
57-
Abort,
57+
Abort(Option<String>),
5858
}
5959

6060
/// Returns a freshly created `InterpCx`, along with an `MPlaceTy` representing

src/machine.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -273,7 +273,7 @@ impl<'mir, 'tcx> Machine<'mir, 'tcx> for Evaluator<'tcx> {
273273

274274
#[inline(always)]
275275
fn abort(_ecx: &mut InterpCx<'mir, 'tcx, Self>) -> InterpResult<'tcx, !> {
276-
throw_machine_stop!(TerminationInfo::Abort)
276+
throw_machine_stop!(TerminationInfo::Abort(None))
277277
}
278278

279279
#[inline(always)]

src/shims/intrinsics.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -410,15 +410,15 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx
410410
"assert_uninit_valid" => {
411411
let ty = substs.type_at(0);
412412
let layout = this.layout_of(ty)?;
413-
// Return here because we panicked instead of returning normally from the intrinsic.
413+
// Abort here because the caller might not be panic safe.
414414
if layout.abi.is_uninhabited() {
415-
return this.start_panic(&format!("attempted to instantiate uninhabited type `{}`", ty), unwind);
415+
throw_machine_stop!(TerminationInfo::Abort(Some(format!("attempted to instantiate uninhabited type `{}`", ty))))
416416
}
417417
if intrinsic_name == "assert_zero_valid" && !layout.might_permit_raw_init(this, /*zero:*/ true).unwrap() {
418-
return this.start_panic(&format!("attempted to zero-initialize type `{}`, which is invalid", ty), unwind);
418+
throw_machine_stop!(TerminationInfo::Abort(Some(format!("attempted to zero-initialize type `{}`, which is invalid", ty))))
419419
}
420420
if intrinsic_name == "assert_uninit_valid" && !layout.might_permit_raw_init(this, /*zero:*/ false).unwrap() {
421-
return this.start_panic(&format!("attempted to leave type `{}` uninitialized, which is invalid", ty), unwind);
421+
throw_machine_stop!(TerminationInfo::Abort(Some(format!("attempted to leave type `{}` uninitialized, which is invalid", ty))))
422422
}
423423
}
424424

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
// error-pattern: the evaluated program aborted execution: attempted to zero-initialize type `fn()`, which is invalid
2+
3+
#[allow(deprecated, invalid_value)]
4+
fn main() {
5+
unsafe { std::mem::zeroed::<fn()>() };
6+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
// error-pattern: the evaluated program aborted execution: attempted to instantiate uninhabited type `!`
2+
#![feature(never_type)]
3+
4+
#[allow(deprecated, invalid_value)]
5+
fn main() {
6+
unsafe { std::mem::uninitialized::<!>() };
7+
}

tests/run-pass/panic/catch_panic.rs

-37
Original file line numberDiff line numberDiff line change
@@ -67,43 +67,6 @@ fn main() {
6767
|_old_val| { let _val = 1/0; loop {} },
6868
);
6969

70-
// libcore panics from shims.
71-
#[allow(deprecated, invalid_value)]
72-
{
73-
test(
74-
Some("attempted to instantiate uninhabited type `!`"),
75-
|_old_val| unsafe { std::mem::uninitialized::<!>() },
76-
);
77-
test(
78-
Some("attempted to instantiate uninhabited type `!`"),
79-
|_old_val| unsafe { std::mem::zeroed::<!>() },
80-
);
81-
test(
82-
Some("attempted to leave type `fn()` uninitialized, which is invalid"),
83-
|_old_val| unsafe { std::mem::uninitialized::<fn()>(); loop {} },
84-
);
85-
test(
86-
Some("attempted to zero-initialize type `fn()`, which is invalid"),
87-
|_old_val| unsafe { std::mem::zeroed::<fn()>(); loop {} },
88-
);
89-
test(
90-
Some("attempted to leave type `*const dyn std::marker::Sync` uninitialized, which is invalid"),
91-
|_old_val| unsafe { std::mem::uninitialized::<*const dyn Sync>(); loop {} },
92-
);
93-
test(
94-
Some("attempted to zero-initialize type `*mut dyn std::marker::Sync`, which is invalid"),
95-
|_old_val| unsafe { std::mem::zeroed::<*mut dyn Sync>(); loop {} },
96-
);
97-
test(
98-
Some("attempted to leave type `&u8` uninitialized, which is invalid"),
99-
|_old_val| unsafe { std::mem::uninitialized::<&u8>(); loop {} },
100-
);
101-
test(
102-
Some("attempted to zero-initialize type `&u8`, which is invalid"),
103-
|_old_val| unsafe { std::mem::zeroed::<&u8>(); loop {} },
104-
);
105-
}
106-
10770
test(
10871
Some("align_offset: align is not a power-of-two"),
10972
|_old_val| { (0usize as *const u8).align_offset(3); loop {} },

tests/run-pass/panic/catch_panic.stderr

+2-18
Original file line numberDiff line numberDiff line change
@@ -16,27 +16,11 @@ thread 'main' panicked at 'index out of bounds: the len is 3 but the index is 4'
1616
Caught panic message (String): index out of bounds: the len is 3 but the index is 4
1717
thread 'main' panicked at 'attempt to divide by zero', $DIR/catch_panic.rs:67:33
1818
Caught panic message (String): attempt to divide by zero
19-
thread 'main' panicked at 'attempted to instantiate uninhabited type `!`', $LOC
20-
Caught panic message (String): attempted to instantiate uninhabited type `!`
21-
thread 'main' panicked at 'attempted to instantiate uninhabited type `!`', $LOC
22-
Caught panic message (String): attempted to instantiate uninhabited type `!`
23-
thread 'main' panicked at 'attempted to leave type `fn()` uninitialized, which is invalid', $LOC
24-
Caught panic message (String): attempted to leave type `fn()` uninitialized, which is invalid
25-
thread 'main' panicked at 'attempted to zero-initialize type `fn()`, which is invalid', $LOC
26-
Caught panic message (String): attempted to zero-initialize type `fn()`, which is invalid
27-
thread 'main' panicked at 'attempted to leave type `*const dyn std::marker::Sync` uninitialized, which is invalid', $LOC
28-
Caught panic message (String): attempted to leave type `*const dyn std::marker::Sync` uninitialized, which is invalid
29-
thread 'main' panicked at 'attempted to zero-initialize type `*mut dyn std::marker::Sync`, which is invalid', $LOC
30-
Caught panic message (String): attempted to zero-initialize type `*mut dyn std::marker::Sync`, which is invalid
31-
thread 'main' panicked at 'attempted to leave type `&u8` uninitialized, which is invalid', $LOC
32-
Caught panic message (String): attempted to leave type `&u8` uninitialized, which is invalid
33-
thread 'main' panicked at 'attempted to zero-initialize type `&u8`, which is invalid', $LOC
34-
Caught panic message (String): attempted to zero-initialize type `&u8`, which is invalid
3519
thread 'main' panicked at 'align_offset: align is not a power-of-two', $LOC
3620
Caught panic message (String): align_offset: align is not a power-of-two
37-
thread 'main' panicked at 'assertion failed: false', $DIR/catch_panic.rs:113:29
21+
thread 'main' panicked at 'assertion failed: false', $DIR/catch_panic.rs:76:29
3822
Caught panic message (&str): assertion failed: false
39-
thread 'main' panicked at 'assertion failed: false', $DIR/catch_panic.rs:114:29
23+
thread 'main' panicked at 'assertion failed: false', $DIR/catch_panic.rs:77:29
4024
Caught panic message (&str): assertion failed: false
4125
thread 'main' panicked at 'attempt to copy from unaligned or null pointer', $LOC
4226
Caught panic message (String): attempt to copy from unaligned or null pointer

0 commit comments

Comments
 (0)