Skip to content

Commit 4135441

Browse files
committed
don't call Memory::get without checking the pointer first; avoid Memory::get if we just need to know align/size
1 parent 048b00d commit 4135441

File tree

2 files changed

+27
-15
lines changed

2 files changed

+27
-15
lines changed

src/operator.rs

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -206,7 +206,7 @@ impl<'mir, 'tcx> EvalContextExt<'tcx> for super::MiriEvalContext<'mir, 'tcx> {
206206
// on read hardware this can easily happen. Thus for comparisons we require
207207
// both pointers to be live.
208208
if self.pointer_inbounds(left).is_ok() && self.pointer_inbounds(right).is_ok() {
209-
// Two in-bounds pointers in different allocations are different.
209+
// Two in-bounds (and hence live) pointers in different allocations are different.
210210
false
211211
} else {
212212
return err!(InvalidPointerMath);
@@ -303,7 +303,9 @@ impl<'mir, 'tcx> EvalContextExt<'tcx> for super::MiriEvalContext<'mir, 'tcx> {
303303
map_to_primval(left.overflowing_offset(Size::from_bytes(right as u64), self)),
304304

305305
BitAnd if !signed => {
306-
let ptr_base_align = self.memory().get(left.alloc_id)?.align.bytes();
306+
let ptr_base_align = self.memory().get_size_and_align(left.alloc_id, AllocCheck::MaybeDead)
307+
.expect("alloc info with MaybeDead cannot fail")
308+
.1.bytes();
307309
let base_mask = {
308310
// FIXME: use `interpret::truncate`, once that takes a `Size` instead of a `Layout`.
309311
let shift = 128 - self.memory().pointer_size().bits();
@@ -337,7 +339,9 @@ impl<'mir, 'tcx> EvalContextExt<'tcx> for super::MiriEvalContext<'mir, 'tcx> {
337339
Rem if !signed => {
338340
// Doing modulo a divisor of the alignment is allowed.
339341
// (Intuition: modulo a divisor leaks less information.)
340-
let ptr_base_align = self.memory().get(left.alloc_id)?.align.bytes();
342+
let ptr_base_align = self.memory().get_size_and_align(left.alloc_id, AllocCheck::MaybeDead)
343+
.expect("alloc info with MaybeDead cannot fail")
344+
.1.bytes();
341345
let right = right as u64;
342346
let ptr_size = self.memory().pointer_size();
343347
if right == 1 {

src/shims/foreign_items.rs

Lines changed: 20 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -252,9 +252,10 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx
252252
Align::from_bytes(align).unwrap(),
253253
MiriMemoryKind::Rust.into()
254254
);
255+
// We just allocated this, the access cannot fail
255256
this.memory_mut()
256-
.get_mut(ptr.alloc_id)?
257-
.write_repeat(tcx, ptr, 0, Size::from_bytes(size))?;
257+
.get_mut(ptr.alloc_id).unwrap()
258+
.write_repeat(tcx, ptr, 0, Size::from_bytes(size)).unwrap();
258259
this.write_scalar(Scalar::Ptr(ptr), dest)?;
259260
}
260261
"__rust_dealloc" => {
@@ -494,15 +495,15 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx
494495
Align::from_bytes(1).unwrap(),
495496
MiriMemoryKind::Env.into(),
496497
);
497-
{
498-
let alloc = this.memory_mut().get_mut(value_copy.alloc_id)?;
499-
alloc.write_bytes(tcx, value_copy, &value)?;
500-
let trailing_zero_ptr = value_copy.offset(
501-
Size::from_bytes(value.len() as u64),
502-
tcx,
503-
)?;
504-
alloc.write_bytes(tcx, trailing_zero_ptr, &[0])?;
505-
}
498+
// We just allocated these, so the write cannot fail.
499+
let alloc = this.memory_mut().get_mut(value_copy.alloc_id).unwrap();
500+
alloc.write_bytes(tcx, value_copy, &value).unwrap();
501+
let trailing_zero_ptr = value_copy.offset(
502+
Size::from_bytes(value.len() as u64),
503+
tcx,
504+
).unwrap();
505+
alloc.write_bytes(tcx, trailing_zero_ptr, &[0]).unwrap();
506+
506507
if let Some(var) = this.machine.env_vars.insert(
507508
name.to_owned(),
508509
value_copy,
@@ -839,7 +840,14 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx
839840
},
840841
"GetSystemInfo" => {
841842
let system_info = this.deref_operand(args[0])?;
842-
let system_info_ptr = system_info.ptr.to_ptr()?;
843+
let (system_info_ptr, align) = system_info.to_scalar_ptr_align();
844+
let system_info_ptr = this.memory()
845+
.check_ptr_access(
846+
system_info_ptr,
847+
system_info.layout.size,
848+
align,
849+
)?
850+
.expect("cannot be a ZST");
843851
// Initialize with `0`.
844852
this.memory_mut().get_mut(system_info_ptr.alloc_id)?
845853
.write_repeat(tcx, system_info_ptr, 0, system_info.layout.size)?;

0 commit comments

Comments
 (0)