Skip to content

Commit b2a8a8a

Browse files
authored
Rollup merge of #71569 - samrat:miri-ub-on-size-mismatch, r=RalfJung
[miri] Throw UB if target size and data size don't match Issue: rust-lang/miri#1355 If an extern C function is defined as ``` extern "C" { fn malloc(size: u32) -> *mut std::ffi::c_void; } ``` on a 64-bit machine(ie. pointer sizes don't match), return undefined behaviour from Miri when [converting the argument into machine_usize](https://github.com/rust-lang/miri/blob/master/src/shims/foreign_items.rs#L200)
2 parents d128774 + c8d8c42 commit b2a8a8a

File tree

2 files changed

+16
-1
lines changed

2 files changed

+16
-1
lines changed

src/librustc_middle/mir/interpret/error.rs

+10
Original file line numberDiff line numberDiff line change
@@ -360,6 +360,11 @@ pub enum UndefinedBehaviorInfo {
360360
InvalidUndefBytes(Option<Pointer>),
361361
/// Working with a local that is not currently live.
362362
DeadLocal,
363+
/// Data size is not equal to target size.
364+
ScalarSizeMismatch {
365+
target_size: u64,
366+
data_size: u64,
367+
},
363368
}
364369

365370
impl fmt::Debug for UndefinedBehaviorInfo {
@@ -421,6 +426,11 @@ impl fmt::Debug for UndefinedBehaviorInfo {
421426
"using uninitialized data, but this operation requires initialized memory"
422427
),
423428
DeadLocal => write!(f, "accessing a dead local variable"),
429+
ScalarSizeMismatch { target_size, data_size } => write!(
430+
f,
431+
"scalar size mismatch: expected {} bytes but got {} bytes instead",
432+
target_size, data_size
433+
),
424434
}
425435
}
426436
}

src/librustc_middle/mir/interpret/value.rs

+6-1
Original file line numberDiff line numberDiff line change
@@ -393,7 +393,12 @@ impl<'tcx, Tag> Scalar<Tag> {
393393
assert_ne!(target_size.bytes(), 0, "you should never look at the bits of a ZST");
394394
match self {
395395
Scalar::Raw { data, size } => {
396-
assert_eq!(target_size.bytes(), u64::from(size));
396+
if target_size.bytes() != u64::from(size) {
397+
throw_ub!(ScalarSizeMismatch {
398+
target_size: target_size.bytes(),
399+
data_size: u64::from(size),
400+
});
401+
}
397402
Scalar::check_data(data, size);
398403
Ok(data)
399404
}

0 commit comments

Comments
 (0)