Skip to content

Commit 2861ceb

Browse files
committed
Rename new fields and move rng to MemoryExtra
1 parent dd732e5 commit 2861ceb

File tree

5 files changed

+37
-41
lines changed

5 files changed

+37
-41
lines changed

src/fn_call.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -979,7 +979,7 @@ fn gen_random<'mir, 'tcx>(
979979
}
980980
let ptr = dest.to_ptr()?;
981981

982-
let data = match &mut this.machine.rng {
982+
let data = match &mut this.memory_mut().extra.rng {
983983
Some(rng) => {
984984
let mut data = vec![0; len];
985985
rng.fill_bytes(&mut data);

src/intptrcast.rs

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,15 +6,17 @@ pub type MemoryState = RefCell<GlobalState>;
66

77
#[derive(Clone, Debug)]
88
pub struct GlobalState {
9-
pub vec: Vec<(u64, AllocId)>,
10-
pub addr: u64,
9+
/// This field is used as a map between the address of each allocation and its `AllocId`
10+
pub int_to_ptr_map: Vec<(u64, AllocId)>,
11+
pub next_base_addr: u64,
1112
}
1213

1314
impl Default for GlobalState {
15+
// FIXME: Query the page size in the future
1416
fn default() -> Self {
1517
GlobalState {
16-
vec: Vec::default(),
17-
addr: 2u64.pow(16)
18+
int_to_ptr_map: Vec::default(),
19+
next_base_addr: 2u64.pow(16)
1820
}
1921
}
2022
}

src/lib.rs

Lines changed: 23 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ mod memory;
2525

2626
use std::collections::HashMap;
2727
use std::borrow::Cow;
28-
use std::cell::RefCell;
28+
use std::cell::Cell;
2929
use std::rc::Rc;
3030

3131
use rand::rngs::StdRng;
@@ -83,9 +83,11 @@ pub fn create_ecx<'mir, 'tcx: 'mir>(
8383
let mut ecx = InterpretCx::new(
8484
tcx.at(syntax::source_map::DUMMY_SP),
8585
ty::ParamEnv::reveal_all(),
86-
Evaluator::new(config.validate, config.seed),
86+
Evaluator::new(config.validate),
8787
);
8888

89+
ecx.memory_mut().extra.rng = config.seed.map(StdRng::seed_from_u64);
90+
8991
let main_instance = ty::Instance::mono(ecx.tcx.tcx, main_id);
9092
let main_mir = ecx.load_mir(main_instance.def)?;
9193

@@ -209,9 +211,7 @@ pub fn create_ecx<'mir, 'tcx: 'mir>(
209211
cur_ptr = cur_ptr.offset(char_size, tcx)?;
210212
}
211213
}
212-
213-
ecx.memory_mut().extra.seed = config.seed.clone();
214-
214+
215215
assert!(args.next().is_none(), "start lang item has more arguments than expected");
216216

217217
Ok(ecx)
@@ -347,14 +347,10 @@ pub struct Evaluator<'tcx> {
347347

348348
/// Whether to enforce the validity invariant.
349349
pub(crate) validate: bool,
350-
351-
/// The random number generator to use if Miri
352-
/// is running in non-deterministic mode
353-
pub(crate) rng: Option<StdRng>
354350
}
355351

356352
impl<'tcx> Evaluator<'tcx> {
357-
fn new(validate: bool, seed: Option<u64>) -> Self {
353+
fn new(validate: bool) -> Self {
358354
Evaluator {
359355
env_vars: HashMap::default(),
360356
argc: None,
@@ -363,7 +359,6 @@ impl<'tcx> Evaluator<'tcx> {
363359
last_error: 0,
364360
tls: TlsData::default(),
365361
validate,
366-
rng: seed.map(|s| StdRng::seed_from_u64(s))
367362
}
368363
}
369364
}
@@ -543,7 +538,7 @@ impl<'mir, 'tcx> Machine<'mir, 'tcx> for Evaluator<'tcx> {
543538
mutability: alloc.mutability,
544539
extra: AllocExtra {
545540
stacks: extra,
546-
base_addr: RefCell::new(None),
541+
base_addr: Cell::new(None),
547542
},
548543
};
549544
(Cow::Owned(alloc), base_tag)
@@ -598,20 +593,20 @@ impl<'mir, 'tcx> Machine<'mir, 'tcx> for Evaluator<'tcx> {
598593
return err!(InvalidNullPointerUsage);
599594
}
600595

601-
if memory.extra.seed.is_none() {
596+
if memory.extra.rng.is_none() {
602597
return err!(ReadBytesAsPointer);
603598
}
604599

605600
let extra = memory.extra.intptrcast.borrow();
606601

607-
match extra.vec.binary_search_by_key(&int, |(int, _)| *int) {
602+
match extra.int_to_ptr_map.binary_search_by_key(&int, |(int, _)| *int) {
608603
Ok(pos) => {
609-
let (_, alloc_id) = extra.vec[pos];
604+
let (_, alloc_id) = extra.int_to_ptr_map[pos];
610605
Ok(Pointer::new_with_tag(alloc_id, Size::from_bytes(0), Tag::Untagged))
611606
}
612607
Err(pos) => {
613608
if pos > 0 {
614-
let (glb, alloc_id) = extra.vec[pos - 1];
609+
let (glb, alloc_id) = extra.int_to_ptr_map[pos - 1];
615610
let offset = int - glb;
616611
if offset <= memory.get(alloc_id)?.bytes.len() as u64 {
617612
Ok(Pointer::new_with_tag(alloc_id, Size::from_bytes(offset), Tag::Untagged))
@@ -629,36 +624,32 @@ impl<'mir, 'tcx> Machine<'mir, 'tcx> for Evaluator<'tcx> {
629624
ptr: Pointer<Self::PointerTag>,
630625
memory: &Memory<'mir, 'tcx, Self>,
631626
) -> InterpResult<'tcx, u64> {
632-
if memory.extra.seed.is_none() {
627+
if memory.extra.rng.is_none() {
633628
return err!(ReadPointerAsBytes);
634629
}
635630

636631
let mut extra = memory.extra.intptrcast.borrow_mut();
637632

638633
let alloc = memory.get(ptr.alloc_id)?;
639634

640-
let mut base_addr = alloc.extra.base_addr.borrow_mut();
641-
642-
let addr = match *base_addr {
643-
Some(addr) => addr,
635+
let base_addr = match alloc.extra.base_addr.get() {
636+
Some(base_addr) => base_addr,
644637
None => {
645-
let addr = extra.addr;
646-
extra.addr += alloc.bytes.len() as u64;
638+
let base_addr = extra.next_base_addr;
639+
extra.next_base_addr += alloc.bytes.len() as u64;
647640

648-
*base_addr = Some(addr);
641+
alloc.extra.base_addr.set(Some(base_addr));
649642

650-
let elem = (addr, ptr.alloc_id);
643+
let elem = (base_addr, ptr.alloc_id);
651644

652-
if let Err(pos) = extra.vec.binary_search(&elem) {
653-
extra.vec.insert(pos, elem);
654-
} else {
655-
return err!(Unreachable);
656-
}
645+
// Given that `next_base_addr` increases in each allocation, pushing the
646+
// corresponding tuple keeps `int_to_ptr_map` sorted
647+
extra.int_to_ptr_map.push(elem);
657648

658-
addr
649+
base_addr
659650
}
660651
};
661652

662-
Ok(addr + ptr.offset.bytes())
653+
Ok(base_addr + ptr.offset.bytes())
663654
}
664655
}

src/memory.rs

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
use std::cell::RefCell;
1+
use std::cell::Cell;
2+
use rand::rngs::StdRng;
23

34
use rustc_mir::interpret::{Pointer, Allocation, AllocationExtra, InterpResult};
45
use rustc_target::abi::Size;
@@ -10,13 +11,15 @@ use crate::stacked_borrows::{Tag, AccessKind};
1011
pub struct MemoryState {
1112
pub stacked: stacked_borrows::MemoryState,
1213
pub intptrcast: intptrcast::MemoryState,
13-
pub seed: Option<u64>,
14+
/// The random number generator to use if Miri
15+
/// is running in non-deterministic mode
16+
pub(crate) rng: Option<StdRng>
1417
}
1518

1619
#[derive(Debug, Clone,)]
1720
pub struct AllocExtra {
1821
pub stacks: stacked_borrows::Stacks,
19-
pub base_addr: RefCell<Option<u64>>,
22+
pub base_addr: Cell<Option<u64>>,
2023
}
2124

2225
impl AllocationExtra<Tag> for AllocExtra {

src/operator.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ impl<'mir, 'tcx> EvalContextExt<'tcx> for super::MiriEvalContext<'mir, 'tcx> {
4444

4545
trace!("ptr_op: {:?} {:?} {:?}", *left, bin_op, *right);
4646

47-
if self.memory().extra.seed.is_some() && bin_op != Offset {
47+
if self.memory().extra.rng.is_some() && bin_op != Offset {
4848
let l_bits = self.force_bits(left.imm.to_scalar()?, left.layout.size)?;
4949
let r_bits = self.force_bits(right.imm.to_scalar()?, right.layout.size)?;
5050

0 commit comments

Comments
 (0)