Skip to content

Commit 6df06b4

Browse files
committed
update for Memory API changes
1 parent 516e905 commit 6df06b4

File tree

14 files changed

+154
-102
lines changed

14 files changed

+154
-102
lines changed

src/data_race.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -720,7 +720,7 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: MiriEvalContextExt<'mir, 'tcx> {
720720
if let Some(data_race) = &mut this.memory.extra.data_race {
721721
if data_race.multi_threaded.get() {
722722
let alloc_meta =
723-
this.memory.get_raw_mut(ptr.alloc_id)?.extra.data_race.as_mut().unwrap();
723+
this.memory.get_alloc_extra_mut(ptr.alloc_id)?.data_race.as_mut().unwrap();
724724
alloc_meta.reset_clocks(ptr.offset, size);
725725
}
726726
}
@@ -1024,7 +1024,7 @@ trait EvalContextPrivExt<'mir, 'tcx: 'mir>: MiriEvalContextExt<'mir, 'tcx> {
10241024
let place_ptr = place.ptr.assert_ptr();
10251025
let size = place.layout.size;
10261026
let alloc_meta =
1027-
&this.memory.get_raw(place_ptr.alloc_id)?.extra.data_race.as_ref().unwrap();
1027+
&this.memory.get_alloc_extra(place_ptr.alloc_id)?.data_race.as_ref().unwrap();
10281028
log::trace!(
10291029
"Atomic op({}) with ordering {:?} on memory({:?}, offset={}, size={})",
10301030
description,

src/diagnostics.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -136,13 +136,13 @@ pub fn report_error<'tcx, 'mir>(
136136

137137
// Extra output to help debug specific issues.
138138
match e.kind() {
139-
UndefinedBehavior(UndefinedBehaviorInfo::InvalidUninitBytes(Some(access))) => {
139+
UndefinedBehavior(UndefinedBehaviorInfo::InvalidUninitBytes(Some((alloc_id, access)))) => {
140140
eprintln!(
141141
"Uninitialized read occurred at offsets 0x{:x}..0x{:x} into this allocation:",
142-
access.uninit_ptr.offset.bytes(),
143-
access.uninit_ptr.offset.bytes() + access.uninit_size.bytes(),
142+
access.uninit_offset.bytes(),
143+
access.uninit_offset.bytes() + access.uninit_size.bytes(),
144144
);
145-
eprintln!("{:?}", ecx.memory.dump_alloc(access.uninit_ptr.alloc_id));
145+
eprintln!("{:?}", ecx.memory.dump_alloc(*alloc_id));
146146
}
147147
_ => {}
148148
}

src/helpers.rs

Lines changed: 47 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ use log::trace;
88
use rustc_hir::def_id::{DefId, CRATE_DEF_INDEX};
99
use rustc_middle::mir;
1010
use rustc_middle::ty::{self, layout::TyAndLayout, List, TyCtxt};
11-
use rustc_target::abi::{FieldsShape, LayoutOf, Size, Variants};
11+
use rustc_target::abi::{Align, FieldsShape, LayoutOf, Size, Variants};
1212
use rustc_target::spec::abi::Abi;
1313

1414
use rand::RngCore;
@@ -566,6 +566,52 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx
566566
Duration::new(seconds, nanoseconds)
567567
})
568568
}
569+
570+
fn read_c_str<'a>(&'a self, sptr: Scalar<Tag>) -> InterpResult<'tcx, &'a [u8]>
571+
where
572+
'tcx: 'a,
573+
'mir: 'a,
574+
{
575+
let this = self.eval_context_ref();
576+
let size1 = Size::from_bytes(1);
577+
let ptr = this.force_ptr(sptr)?; // We need to read at least 1 byte, so we can eagerly get a ptr.
578+
579+
// Step 1: determine the length.
580+
let mut len = Size::ZERO;
581+
loop {
582+
let alloc = this.memory.get(ptr.offset(len, this)?.into(), size1, Align::ONE)?.unwrap(); // not a ZST, so we will get a result
583+
let byte = alloc.read_scalar(alloc_range(Size::ZERO, size1))?.to_u8()?;
584+
if byte == 0 {
585+
break;
586+
} else {
587+
len = len + size1;
588+
}
589+
}
590+
591+
// Step 2: get the bytes.
592+
this.memory.read_bytes(ptr.into(), len)
593+
}
594+
595+
fn read_wide_str(&self, sptr: Scalar<Tag>) -> InterpResult<'tcx, Vec<u16>> {
596+
let this = self.eval_context_ref();
597+
let size2 = Size::from_bytes(2);
598+
let align2 = Align::from_bytes(2).unwrap();
599+
600+
let mut ptr = this.force_ptr(sptr)?; // We need to read at least 1 wchar, so we can eagerly get a ptr.
601+
let mut wchars = Vec::new();
602+
loop {
603+
let alloc = this.memory.get(ptr.into(), size2, align2)?.unwrap(); // not a ZST, so we will get a result
604+
let wchar = alloc.read_scalar(alloc_range(Size::ZERO, size2))?.to_u16()?;
605+
if wchar == 0 {
606+
break;
607+
} else {
608+
wchars.push(wchar);
609+
ptr = ptr.offset(size2, this)?;
610+
}
611+
}
612+
613+
Ok(wchars)
614+
}
569615
}
570616

571617
/// Check that the number of args is what we expect.

src/machine.rs

Lines changed: 50 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -478,14 +478,14 @@ impl<'mir, 'tcx> Machine<'mir, 'tcx> for Evaluator<'mir, 'tcx> {
478478
let alloc = alloc.into_owned();
479479
let (stacks, base_tag) = if let Some(stacked_borrows) = &memory_extra.stacked_borrows {
480480
let (stacks, base_tag) =
481-
Stacks::new_allocation(id, alloc.size, Rc::clone(stacked_borrows), kind);
481+
Stacks::new_allocation(id, alloc.size(), Rc::clone(stacked_borrows), kind);
482482
(Some(stacks), base_tag)
483483
} else {
484484
// No stacks, no tag.
485485
(None, Tag::Untagged)
486486
};
487487
let race_alloc = if let Some(data_race) = &memory_extra.data_race {
488-
Some(data_race::AllocExtra::new_allocation(&data_race, alloc.size, kind))
488+
Some(data_race::AllocExtra::new_allocation(&data_race, alloc.size(), kind))
489489
} else {
490490
None
491491
};
@@ -506,15 +506,57 @@ impl<'mir, 'tcx> Machine<'mir, 'tcx> for Evaluator<'mir, 'tcx> {
506506
}
507507

508508
#[inline(always)]
509-
fn before_deallocation(
510-
memory_extra: &mut Self::MemoryExtra,
511-
id: AllocId,
509+
fn memory_read(
510+
_memory_extra: &Self::MemoryExtra,
511+
alloc: &Allocation<Tag, AllocExtra>,
512+
ptr: Pointer<Tag>,
513+
size: Size,
512514
) -> InterpResult<'tcx> {
513-
if Some(id) == memory_extra.tracked_alloc_id {
514-
register_diagnostic(NonHaltingDiagnostic::FreedAlloc(id));
515+
if let Some(data_race) = &alloc.extra.data_race {
516+
data_race.read(ptr, size)?;
515517
}
518+
if let Some(stacked_borrows) = &alloc.extra.stacked_borrows {
519+
stacked_borrows.memory_read(ptr, size)
520+
} else {
521+
Ok(())
522+
}
523+
}
516524

517-
Ok(())
525+
#[inline(always)]
526+
fn memory_written(
527+
_memory_extra: &mut Self::MemoryExtra,
528+
alloc: &mut Allocation<Tag, AllocExtra>,
529+
ptr: Pointer<Tag>,
530+
size: Size,
531+
) -> InterpResult<'tcx> {
532+
if let Some(data_race) = &mut alloc.extra.data_race {
533+
data_race.write(ptr, size)?;
534+
}
535+
if let Some(stacked_borrows) = &mut alloc.extra.stacked_borrows {
536+
stacked_borrows.memory_written(ptr, size)
537+
} else {
538+
Ok(())
539+
}
540+
}
541+
542+
#[inline(always)]
543+
fn memory_deallocated(
544+
memory_extra: &mut Self::MemoryExtra,
545+
alloc: &mut Allocation<Tag, AllocExtra>,
546+
ptr: Pointer<Tag>,
547+
) -> InterpResult<'tcx> {
548+
let size = alloc.size();
549+
if Some(ptr.alloc_id) == memory_extra.tracked_alloc_id {
550+
register_diagnostic(NonHaltingDiagnostic::FreedAlloc(ptr.alloc_id));
551+
}
552+
if let Some(data_race) = &mut alloc.extra.data_race {
553+
data_race.deallocate(ptr, size)?;
554+
}
555+
if let Some(stacked_borrows) = &mut alloc.extra.stacked_borrows {
556+
stacked_borrows.memory_deallocated(ptr, size)
557+
} else {
558+
Ok(())
559+
}
518560
}
519561

520562
fn after_static_mem_initialized(
@@ -601,53 +643,3 @@ impl<'mir, 'tcx> Machine<'mir, 'tcx> for Evaluator<'mir, 'tcx> {
601643
intptrcast::GlobalState::ptr_to_int(ptr, memory)
602644
}
603645
}
604-
605-
impl AllocationExtra<Tag> for AllocExtra {
606-
#[inline(always)]
607-
fn memory_read<'tcx>(
608-
alloc: &Allocation<Tag, AllocExtra>,
609-
ptr: Pointer<Tag>,
610-
size: Size,
611-
) -> InterpResult<'tcx> {
612-
if let Some(data_race) = &alloc.extra.data_race {
613-
data_race.read(ptr, size)?;
614-
}
615-
if let Some(stacked_borrows) = &alloc.extra.stacked_borrows {
616-
stacked_borrows.memory_read(ptr, size)
617-
} else {
618-
Ok(())
619-
}
620-
}
621-
622-
#[inline(always)]
623-
fn memory_written<'tcx>(
624-
alloc: &mut Allocation<Tag, AllocExtra>,
625-
ptr: Pointer<Tag>,
626-
size: Size,
627-
) -> InterpResult<'tcx> {
628-
if let Some(data_race) = &mut alloc.extra.data_race {
629-
data_race.write(ptr, size)?;
630-
}
631-
if let Some(stacked_borrows) = &mut alloc.extra.stacked_borrows {
632-
stacked_borrows.memory_written(ptr, size)
633-
} else {
634-
Ok(())
635-
}
636-
}
637-
638-
#[inline(always)]
639-
fn memory_deallocated<'tcx>(
640-
alloc: &mut Allocation<Tag, AllocExtra>,
641-
ptr: Pointer<Tag>,
642-
size: Size,
643-
) -> InterpResult<'tcx> {
644-
if let Some(data_race) = &mut alloc.extra.data_race {
645-
data_race.deallocate(ptr, size)?;
646-
}
647-
if let Some(stacked_borrows) = &mut alloc.extra.stacked_borrows {
648-
stacked_borrows.memory_deallocated(ptr, size)
649-
} else {
650-
Ok(())
651-
}
652-
}
653-
}

src/shims/foreign_items.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -405,7 +405,7 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx
405405
check_abi(abi, Abi::C { unwind: false })?;
406406
let &[ref ptr] = check_arg_count(args)?;
407407
let ptr = this.read_scalar(ptr)?.check_init()?;
408-
let n = this.memory.read_c_str(ptr)?.len();
408+
let n = this.read_c_str(ptr)?.len();
409409
this.write_scalar(Scalar::from_machine_usize(u64::try_from(n).unwrap(), this), dest)?;
410410
}
411411

src/shims/intrinsics.rs

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -574,7 +574,8 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx
574574
// even if the type they wrap would be less aligned (e.g. AtomicU64 on 32bit must
575575
// be 8-aligned).
576576
let align = Align::from_bytes(place.layout.size.bytes()).unwrap();
577-
this.memory.check_ptr_access(place.ptr, place.layout.size, align)?;
577+
this.memory.check_ptr_access_align(place.ptr, place.layout.size, align, CheckInAllocMsg::MemoryAccessTest)?;
578+
// Perform regular access.
578579
this.write_scalar(val, dest)?;
579580
Ok(())
580581
}
@@ -594,7 +595,7 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx
594595
// even if the type they wrap would be less aligned (e.g. AtomicU64 on 32bit must
595596
// be 8-aligned).
596597
let align = Align::from_bytes(place.layout.size.bytes()).unwrap();
597-
this.memory.check_ptr_access(place.ptr, place.layout.size, align)?;
598+
this.memory.check_ptr_access_align(place.ptr, place.layout.size, align, CheckInAllocMsg::MemoryAccessTest)?;
598599

599600
// Perform atomic store
600601
this.write_scalar_atomic(val, &place, atomic)?;
@@ -644,7 +645,7 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx
644645
// even if the type they wrap would be less aligned (e.g. AtomicU64 on 32bit must
645646
// be 8-aligned).
646647
let align = Align::from_bytes(place.layout.size.bytes()).unwrap();
647-
this.memory.check_ptr_access(place.ptr, place.layout.size, align)?;
648+
this.memory.check_ptr_access_align(place.ptr, place.layout.size, align, CheckInAllocMsg::MemoryAccessTest)?;
648649

649650
match atomic_op {
650651
AtomicOp::Min => {
@@ -681,7 +682,7 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx
681682
// even if the type they wrap would be less aligned (e.g. AtomicU64 on 32bit must
682683
// be 8-aligned).
683684
let align = Align::from_bytes(place.layout.size.bytes()).unwrap();
684-
this.memory.check_ptr_access(place.ptr, place.layout.size, align)?;
685+
this.memory.check_ptr_access_align(place.ptr, place.layout.size, align, CheckInAllocMsg::MemoryAccessTest)?;
685686

686687
let old = this.atomic_exchange_scalar(&place, new, atomic)?;
687688
this.write_scalar(old, dest)?; // old value is returned
@@ -707,7 +708,7 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx
707708
// even if the type they wrap would be less aligned (e.g. AtomicU64 on 32bit must
708709
// be 8-aligned).
709710
let align = Align::from_bytes(place.layout.size.bytes()).unwrap();
710-
this.memory.check_ptr_access(place.ptr, place.layout.size, align)?;
711+
this.memory.check_ptr_access_align(place.ptr, place.layout.size, align, CheckInAllocMsg::MemoryAccessTest)?;
711712

712713
let old = this.atomic_compare_exchange_scalar(
713714
&place,

0 commit comments

Comments
 (0)