Skip to content

Commit 6ce8f43

Browse files
committed
give extra context to ABI mismatch errors
1 parent 784f545 commit 6ce8f43

7 files changed

+28
-8
lines changed

src/diagnostics.rs

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -199,6 +199,7 @@ pub fn report_error<'tcx, 'mir>(
199199
e: InterpErrorInfo<'tcx>,
200200
) -> Option<(i64, bool)> {
201201
use InterpError::*;
202+
use UndefinedBehaviorInfo::*;
202203

203204
let mut msg = vec![];
204205

@@ -271,7 +272,7 @@ pub fn report_error<'tcx, 'mir>(
271272
(title, helps)
272273
} else {
273274
let title = match e.kind() {
274-
UndefinedBehavior(UndefinedBehaviorInfo::ValidationError(validation_err))
275+
UndefinedBehavior(ValidationError(validation_err))
275276
if matches!(
276277
validation_err.kind,
277278
ValidationErrorKind::PointerAsInt { .. } | ValidationErrorKind::PartialPointer
@@ -299,7 +300,7 @@ pub fn report_error<'tcx, 'mir>(
299300
let helps = match e.kind() {
300301
Unsupported(_) =>
301302
vec![(None, format!("this is likely not a bug in the program; it indicates that the program performed an operation that the interpreter does not support"))],
302-
UndefinedBehavior(UndefinedBehaviorInfo::AlignmentCheckFailed { .. })
303+
UndefinedBehavior(AlignmentCheckFailed { .. })
303304
if ecx.machine.check_alignment == AlignmentCheck::Symbolic
304305
=>
305306
vec![
@@ -311,13 +312,20 @@ pub fn report_error<'tcx, 'mir>(
311312
(None, format!("this indicates a bug in the program: it performed an invalid operation, and caused Undefined Behavior")),
312313
(None, format!("see https://doc.rust-lang.org/nightly/reference/behavior-considered-undefined.html for further information")),
313314
];
314-
if let UndefinedBehaviorInfo::PointerUseAfterFree(alloc_id, _) | UndefinedBehaviorInfo::PointerOutOfBounds { alloc_id, .. } = info {
315-
if let Some(span) = ecx.machine.allocated_span(*alloc_id) {
316-
helps.push((Some(span), format!("{:?} was allocated here:", alloc_id)));
315+
match info {
316+
PointerUseAfterFree(alloc_id, _) | PointerOutOfBounds { alloc_id, .. } => {
317+
if let Some(span) = ecx.machine.allocated_span(*alloc_id) {
318+
helps.push((Some(span), format!("{:?} was allocated here:", alloc_id)));
319+
}
320+
if let Some(span) = ecx.machine.deallocated_span(*alloc_id) {
321+
helps.push((Some(span), format!("{:?} was deallocated here:", alloc_id)));
322+
}
317323
}
318-
if let Some(span) = ecx.machine.deallocated_span(*alloc_id) {
319-
helps.push((Some(span), format!("{:?} was deallocated here:", alloc_id)));
324+
AbiMismatchArgument { .. } | AbiMismatchReturn { .. } => {
325+
helps.push((None, format!("this means these two types are not *guaranteed* to be ABI-compatible across all targets")));
326+
helps.push((None, format!("if you think this code should be accepted anyway, please report an issue")));
320327
}
328+
_ => {},
321329
}
322330
helps
323331
}
@@ -339,7 +347,7 @@ pub fn report_error<'tcx, 'mir>(
339347
// We want to dump the allocation if this is `InvalidUninitBytes`. Since `format_error` consumes `e`, we compute the outut early.
340348
let mut extra = String::new();
341349
match e.kind() {
342-
UndefinedBehavior(UndefinedBehaviorInfo::InvalidUninitBytes(Some((alloc_id, access)))) => {
350+
UndefinedBehavior(InvalidUninitBytes(Some((alloc_id, access)))) => {
343351
writeln!(
344352
extra,
345353
"Uninitialized memory occurred at {alloc_id:?}{range:?}, in this allocation:",

tests/fail/function_pointers/abi_mismatch_array_vs_struct.stderr

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ LL | g(Default::default())
66
|
77
= help: this indicates a bug in the program: it performed an invalid operation, and caused Undefined Behavior
88
= help: see https://doc.rust-lang.org/nightly/reference/behavior-considered-undefined.html for further information
9+
= help: this means these two types are not *guaranteed* to be ABI-compatible across all targets
10+
= help: if you think this code should be accepted anyway, please report an issue
911
= note: BACKTRACE:
1012
= note: inside `main` at $DIR/abi_mismatch_array_vs_struct.rs:LL:CC
1113

tests/fail/function_pointers/abi_mismatch_int_vs_float.stderr

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ LL | g(42)
66
|
77
= help: this indicates a bug in the program: it performed an invalid operation, and caused Undefined Behavior
88
= help: see https://doc.rust-lang.org/nightly/reference/behavior-considered-undefined.html for further information
9+
= help: this means these two types are not *guaranteed* to be ABI-compatible across all targets
10+
= help: if you think this code should be accepted anyway, please report an issue
911
= note: BACKTRACE:
1012
= note: inside `main` at $DIR/abi_mismatch_int_vs_float.rs:LL:CC
1113

tests/fail/function_pointers/abi_mismatch_raw_pointer.stderr

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ LL | g(&42 as *const i32)
66
|
77
= help: this indicates a bug in the program: it performed an invalid operation, and caused Undefined Behavior
88
= help: see https://doc.rust-lang.org/nightly/reference/behavior-considered-undefined.html for further information
9+
= help: this means these two types are not *guaranteed* to be ABI-compatible across all targets
10+
= help: if you think this code should be accepted anyway, please report an issue
911
= note: BACKTRACE:
1012
= note: inside `main` at $DIR/abi_mismatch_raw_pointer.rs:LL:CC
1113

tests/fail/function_pointers/abi_mismatch_return_type.stderr

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ LL | g()
66
|
77
= help: this indicates a bug in the program: it performed an invalid operation, and caused Undefined Behavior
88
= help: see https://doc.rust-lang.org/nightly/reference/behavior-considered-undefined.html for further information
9+
= help: this means these two types are not *guaranteed* to be ABI-compatible across all targets
10+
= help: if you think this code should be accepted anyway, please report an issue
911
= note: BACKTRACE:
1012
= note: inside `main` at $DIR/abi_mismatch_return_type.rs:LL:CC
1113

tests/fail/function_pointers/abi_mismatch_simple.stderr

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ LL | g(42)
66
|
77
= help: this indicates a bug in the program: it performed an invalid operation, and caused Undefined Behavior
88
= help: see https://doc.rust-lang.org/nightly/reference/behavior-considered-undefined.html for further information
9+
= help: this means these two types are not *guaranteed* to be ABI-compatible across all targets
10+
= help: if you think this code should be accepted anyway, please report an issue
911
= note: BACKTRACE:
1012
= note: inside `main` at $DIR/abi_mismatch_simple.rs:LL:CC
1113

tests/fail/function_pointers/abi_mismatch_vector.stderr

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ LL | g(Default::default())
66
|
77
= help: this indicates a bug in the program: it performed an invalid operation, and caused Undefined Behavior
88
= help: see https://doc.rust-lang.org/nightly/reference/behavior-considered-undefined.html for further information
9+
= help: this means these two types are not *guaranteed* to be ABI-compatible across all targets
10+
= help: if you think this code should be accepted anyway, please report an issue
911
= note: BACKTRACE:
1012
= note: inside `main` at $DIR/abi_mismatch_vector.rs:LL:CC
1113

0 commit comments

Comments
 (0)