Skip to content

Commit cb932f4

Browse files
committed
Auto merge of #1006 - RalfJung:bounds, r=RalfJung
audit our bounds checks This simplifies some bounds checks and adds comments.
2 parents ad6af7a + e574c77 commit cb932f4

File tree

4 files changed

+10
-14
lines changed

4 files changed

+10
-14
lines changed

src/eval.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -162,7 +162,7 @@ pub fn create_ecx<'mir, 'tcx: 'mir>(
162162
MiriMemoryKind::Env.into(),
163163
);
164164
ecx.machine.cmd_line = Some(cmd_ptr);
165-
// Store the UTF-16 string.
165+
// Store the UTF-16 string. We just allocated so we know the bounds are fine.
166166
let char_size = Size::from_bytes(2);
167167
let cmd_alloc = ecx.memory.get_mut(cmd_ptr.alloc_id)?;
168168
let mut cur_ptr = cmd_ptr;

src/helpers.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,7 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx
9494
}
9595
let this = self.eval_context_mut();
9696

97+
// Don't forget the bounds check.
9798
let ptr = this.memory.check_ptr_access(
9899
ptr,
99100
Size::from_bytes(len as u64),

src/shims/foreign_items.rs

Lines changed: 6 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx
5050
.memory
5151
.allocate(Size::from_bytes(size), align, kind.into());
5252
if zero_init {
53-
// We just allocated this, the access cannot fail
53+
// We just allocated this, the access is definitely in-bounds.
5454
this.memory
5555
.get_mut(ptr.alloc_id)
5656
.unwrap()
@@ -227,7 +227,7 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx
227227
Align::from_bytes(align).unwrap(),
228228
MiriMemoryKind::Rust.into(),
229229
);
230-
// We just allocated this, the access cannot fail
230+
// We just allocated this, the access is definitely in-bounds.
231231
this.memory
232232
.get_mut(ptr.alloc_id)
233233
.unwrap()
@@ -643,7 +643,7 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx
643643

644644
// Hook pthread calls that go to the thread-local storage memory subsystem.
645645
"pthread_key_create" => {
646-
let key_ptr = this.read_scalar(args[0])?.not_undef()?;
646+
let key_place = this.deref_operand(args[0])?;
647647

648648
// Extract the function type out of the signature (that seems easier than constructing it ourselves).
649649
let dtor = match this.test_null(this.read_scalar(args[1])?.not_undef()?)? {
@@ -668,16 +668,7 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx
668668
throw_unsup!(OutOfTls);
669669
}
670670

671-
let key_ptr = this
672-
.memory
673-
.check_ptr_access(key_ptr, key_layout.size, key_layout.align.abi)?
674-
.expect("cannot be a ZST");
675-
this.memory.get_mut(key_ptr.alloc_id)?.write_scalar(
676-
tcx,
677-
key_ptr,
678-
Scalar::from_uint(key, key_layout.size).into(),
679-
key_layout.size,
680-
)?;
671+
this.write_scalar(Scalar::from_uint(key, key_layout.size), key_place.into())?;
681672

682673
// Return success (`0`).
683674
this.write_null(dest)?;
@@ -856,6 +847,7 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx
856847
let system_info_ptr = this
857848
.check_mplace_access(system_info, None)?
858849
.expect("cannot be a ZST");
850+
// We rely on `deref_operand` doing bounds checks for us.
859851
// Initialize with `0`.
860852
this.memory
861853
.get_mut(system_info_ptr.alloc_id)?
@@ -992,6 +984,7 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx
992984
fn set_last_error(&mut self, scalar: Scalar<Tag>) -> InterpResult<'tcx> {
993985
let this = self.eval_context_mut();
994986
let errno_ptr = this.machine.last_error.unwrap();
987+
// We allocated this during machine initialziation so the bounds are fine.
995988
this.memory.get_mut(errno_ptr.alloc_id)?.write_scalar(
996989
&*this.tcx,
997990
errno_ptr,

src/shims/intrinsics.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -359,6 +359,7 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx
359359
assert!(mplace.meta.is_none());
360360
// not a zst, must be valid pointer
361361
let ptr = mplace.ptr.to_ptr()?;
362+
// we know the return place is in-bounds
362363
this.memory.get_mut(ptr.alloc_id)?.write_repeat(tcx, ptr, 0, dest.layout.size)?;
363364
}
364365
}
@@ -548,6 +549,7 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx
548549
let mplace = this.force_allocation(dest)?;
549550
assert!(mplace.meta.is_none());
550551
let ptr = mplace.ptr.to_ptr()?;
552+
// We know the return place is in-bounds
551553
this.memory
552554
.get_mut(ptr.alloc_id)?
553555
.mark_definedness(ptr, dest.layout.size, false);

0 commit comments

Comments
 (0)