Skip to content

Commit

Permalink
Factor current-span logic into a caching handle
Browse files Browse the repository at this point in the history
  • Loading branch information
saethlin committed May 21, 2022
1 parent b96610b commit 6e0e612
Show file tree
Hide file tree
Showing 4 changed files with 76 additions and 60 deletions.
5 changes: 2 additions & 3 deletions src/helpers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ pub mod convert;

use std::mem;
use std::num::NonZeroUsize;
use std::rc::Rc;
use std::time::Duration;

use log::trace;
Expand Down Expand Up @@ -822,7 +821,7 @@ pub fn isolation_abort_error(name: &str) -> InterpResult<'static> {

/// Retrieve the list of local crates that should have been passed by cargo-miri in
/// MIRI_LOCAL_CRATES and turn them into `CrateNum`s.
pub fn get_local_crates(tcx: &TyCtxt<'_>) -> Rc<[CrateNum]> {
pub fn get_local_crates(tcx: &TyCtxt<'_>) -> Vec<CrateNum> {
// Convert the local crate names from the passed-in config into CrateNums so that they can
// be looked up quickly during execution
let local_crate_names = std::env::var("MIRI_LOCAL_CRATES")
Expand All @@ -836,7 +835,7 @@ pub fn get_local_crates(tcx: &TyCtxt<'_>) -> Rc<[CrateNum]> {
local_crates.push(crate_num);
}
}
Rc::from(local_crates.as_slice())
local_crates
}

/// Formats an AllocRange like [0x1..0x3], for use in diagnostics.
Expand Down
46 changes: 39 additions & 7 deletions src/machine.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ use std::cell::RefCell;
use std::collections::HashSet;
use std::fmt;
use std::num::NonZeroU64;
use std::rc::Rc;
use std::time::Instant;

use rand::rngs::StdRng;
Expand All @@ -25,7 +24,7 @@ use rustc_middle::{
},
};
use rustc_span::def_id::{CrateNum, DefId};
use rustc_span::Symbol;
use rustc_span::{Span, Symbol};
use rustc_target::abi::Size;
use rustc_target::spec::abi::Abi;

Expand Down Expand Up @@ -274,7 +273,7 @@ pub struct Evaluator<'mir, 'tcx> {
pub(crate) backtrace_style: BacktraceStyle,

/// Crates which are considered local for the purposes of error reporting.
pub(crate) local_crates: Rc<[CrateNum]>,
pub(crate) local_crates: Vec<CrateNum>,

/// Mapping extern static names to their base pointer.
extern_statics: FxHashMap<Symbol, Pointer<Tag>>,
Expand Down Expand Up @@ -415,6 +414,10 @@ impl<'mir, 'tcx> Evaluator<'mir, 'tcx> {
let def_id = frame.instance.def_id();
def_id.is_local() || self.local_crates.contains(&def_id.krate)
}

pub(crate) fn current_span(&self) -> CurrentSpan<'_, 'mir, 'tcx> {
CurrentSpan { span: None, machine: self }
}
}

/// A rustc InterpCx for Miri.
Expand Down Expand Up @@ -580,8 +583,7 @@ impl<'mir, 'tcx> Machine<'mir, 'tcx> for Evaluator<'mir, 'tcx> {
alloc.size(),
stacked_borrows,
kind,
&ecx.machine.threads,
ecx.machine.local_crates.clone(),
ecx.machine.current_span(),
))
} else {
None
Expand Down Expand Up @@ -663,7 +665,7 @@ impl<'mir, 'tcx> Machine<'mir, 'tcx> for Evaluator<'mir, 'tcx> {
tag,
range,
machine.stacked_borrows.as_ref().unwrap(),
&machine.threads,
machine.current_span(),
)
} else {
Ok(())
Expand All @@ -687,7 +689,7 @@ impl<'mir, 'tcx> Machine<'mir, 'tcx> for Evaluator<'mir, 'tcx> {
tag,
range,
machine.stacked_borrows.as_ref().unwrap(),
&machine.threads,
machine.current_span(),
)
} else {
Ok(())
Expand Down Expand Up @@ -789,3 +791,33 @@ impl<'mir, 'tcx> Machine<'mir, 'tcx> for Evaluator<'mir, 'tcx> {
res
}
}

#[derive(Clone)]
pub struct CurrentSpan<'a, 'tcx, 'mir> {
span: Option<Span>,
machine: &'a Evaluator<'tcx, 'mir>,
}

impl<'a, 'tcx, 'mir> CurrentSpan<'a, 'tcx, 'mir> {
pub fn get(&mut self) -> rustc_span::Span {
if self.span.is_none() {
self.span = Some(self.current_span());
}
self.span.unwrap()
}

#[inline(never)]
fn current_span(&self) -> Span {
self.machine
.threads
.active_thread_stack()
.into_iter()
.rev()
.find(|frame| {
let def_id = frame.instance.def_id();
def_id.is_local() || self.machine.local_crates.contains(&def_id.krate)
})
.map(|frame| frame.current_span())
.unwrap_or(rustc_span::DUMMY_SP)
}
}
46 changes: 22 additions & 24 deletions src/stacked_borrows.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ use log::trace;
use std::cell::RefCell;
use std::fmt;
use std::num::NonZeroU64;
use std::rc::Rc;

use rustc_data_structures::fx::{FxHashMap, FxHashSet};
use rustc_hir::Mutability;
Expand All @@ -14,17 +13,15 @@ use rustc_middle::ty::{
self,
layout::{HasParamEnv, LayoutOf},
};
use rustc_span::def_id::CrateNum;
use rustc_span::DUMMY_SP;
use rustc_target::abi::Size;
use std::collections::HashSet;

use crate::machine::CurrentSpan;
use crate::*;

pub mod diagnostics;
use diagnostics::AllocHistory;

use diagnostics::TagHistory;
use diagnostics::{AllocHistory, TagHistory};

pub type PtrId = NonZeroU64;
pub type CallId = NonZeroU64;
Expand Down Expand Up @@ -376,7 +373,7 @@ impl<'tcx> Stack {
tag: SbTag,
(alloc_id, alloc_range, offset): (AllocId, AllocRange, Size), // just for debug printing and error messages
global: &mut GlobalStateInner,
threads: &ThreadManager<'_, 'tcx>,
current_span: &mut CurrentSpan<'_, '_, 'tcx>,
alloc_history: &mut AllocHistory,
) -> InterpResult<'tcx> {
// Two main steps: Find granting item, remove incompatible items above.
Expand All @@ -400,7 +397,7 @@ impl<'tcx> Stack {
global,
alloc_history,
)?;
alloc_history.log_invalidation(item.tag, alloc_range, threads);
alloc_history.log_invalidation(item.tag, alloc_range, current_span);
}
} else {
// On a read, *disable* all `Unique` above the granting item. This ensures U2 for read accesses.
Expand All @@ -422,7 +419,7 @@ impl<'tcx> Stack {
alloc_history,
)?;
item.perm = Permission::Disabled;
alloc_history.log_invalidation(item.tag, alloc_range, threads);
alloc_history.log_invalidation(item.tag, alloc_range, current_span);
}
}
}
Expand Down Expand Up @@ -471,7 +468,7 @@ impl<'tcx> Stack {
new: Item,
(alloc_id, alloc_range, offset): (AllocId, AllocRange, Size), // just for debug printing and error messages
global: &mut GlobalStateInner,
threads: &ThreadManager<'_, 'tcx>,
current_span: &mut CurrentSpan<'_, '_, 'tcx>,
alloc_history: &mut AllocHistory,
) -> InterpResult<'tcx> {
// Figure out which access `perm` corresponds to.
Expand Down Expand Up @@ -505,7 +502,7 @@ impl<'tcx> Stack {
derived_from,
(alloc_id, alloc_range, offset),
global,
threads,
current_span,
alloc_history,
)?;

Expand Down Expand Up @@ -533,13 +530,13 @@ impl<'tcx> Stack {
/// Map per-stack operations to higher-level per-location-range operations.
impl<'tcx> Stacks {
/// Creates new stack with initial tag.
fn new(size: Size, perm: Permission, tag: SbTag, local_crates: Rc<[CrateNum]>) -> Self {
fn new(size: Size, perm: Permission, tag: SbTag) -> Self {
let item = Item { perm, tag, protector: None };
let stack = Stack { borrows: vec![item] };

Stacks {
stacks: RefCell::new(RangeMap::new(size, stack)),
history: RefCell::new(AllocHistory::new(local_crates)),
history: RefCell::new(AllocHistory::new()),
}
}

Expand Down Expand Up @@ -579,8 +576,7 @@ impl Stacks {
size: Size,
state: &GlobalState,
kind: MemoryKind<MiriMemoryKind>,
threads: &ThreadManager<'_, '_>,
local_crates: Rc<[CrateNum]>,
mut current_span: CurrentSpan<'_, '_, '_>,
) -> Self {
let mut extra = state.borrow_mut();
let (base_tag, perm) = match kind {
Expand Down Expand Up @@ -614,12 +610,12 @@ impl Stacks {
(tag, Permission::SharedReadWrite)
}
};
let stacks = Stacks::new(size, perm, base_tag, local_crates);
let stacks = Stacks::new(size, perm, base_tag);
stacks.history.borrow_mut().log_creation(
None,
base_tag,
alloc_range(Size::ZERO, size),
threads,
&mut current_span,
);
stacks
}
Expand All @@ -631,7 +627,7 @@ impl Stacks {
tag: SbTag,
range: AllocRange,
state: &GlobalState,
threads: &ThreadManager<'_, 'tcx>,
mut current_span: CurrentSpan<'_, '_, 'tcx>,
) -> InterpResult<'tcx> {
trace!(
"read access with tag {:?}: {:?}, size {}",
Expand All @@ -646,7 +642,7 @@ impl Stacks {
tag,
(alloc_id, range, offset),
&mut state,
threads,
&mut current_span,
history,
)
})
Expand All @@ -659,7 +655,7 @@ impl Stacks {
tag: SbTag,
range: AllocRange,
state: &GlobalState,
threads: &ThreadManager<'_, 'tcx>,
mut current_span: CurrentSpan<'_, '_, 'tcx>,
) -> InterpResult<'tcx> {
trace!(
"write access with tag {:?}: {:?}, size {}",
Expand All @@ -674,7 +670,7 @@ impl Stacks {
tag,
(alloc_id, range, offset),
&mut state,
threads,
&mut current_span,
history,
)
})
Expand Down Expand Up @@ -723,6 +719,7 @@ trait EvalContextPrivExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx
}
let (alloc_id, base_offset, orig_tag) = this.ptr_get_alloc_id(place.ptr)?;

let mut current_span = this.machine.current_span();
{
let extra = this.get_alloc_extra(alloc_id)?;
let stacked_borrows =
Expand All @@ -732,10 +729,10 @@ trait EvalContextPrivExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx
Some(orig_tag),
new_tag,
alloc_range(base_offset, size),
&this.machine.threads,
&mut current_span,
);
if protect {
alloc_history.log_protector(orig_tag, new_tag, &this.machine.threads);
alloc_history.log_protector(orig_tag, new_tag, &mut current_span);
}
}

Expand Down Expand Up @@ -804,7 +801,7 @@ trait EvalContextPrivExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx
item,
(alloc_id, range, offset),
&mut *global,
&this.machine.threads,
&mut current_span,
history,
)
})
Expand All @@ -821,13 +818,14 @@ trait EvalContextPrivExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx
let item = Item { perm, tag: new_tag, protector };
let range = alloc_range(base_offset, size);
let mut global = machine.stacked_borrows.as_ref().unwrap().borrow_mut();
let mut current_span = machine.current_span();
stacked_borrows.for_each_mut(range, |offset, stack, history| {
stack.grant(
orig_tag,
item,
(alloc_id, range, offset),
&mut global,
&machine.threads,
&mut current_span,
history,
)
})?;
Expand Down
Loading

0 comments on commit 6e0e612

Please sign in to comment.