Skip to content

Add tracing spans to borrow tracker functions #4452

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions src/borrow_tracker/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -260,6 +260,10 @@ impl GlobalStateInner {
kind: MemoryKind,
machine: &MiriMachine<'_>,
) -> AllocState {
let _span = enter_trace_span!(
"new_allocation",
"id = {id:?}, size = {alloc_size:?}, kind = {kind:?}"
);
match self.borrow_tracker_method {
BorrowTrackerMethod::StackedBorrows =>
AllocState::StackedBorrows(Box::new(RefCell::new(Stacks::new_allocation(
Expand All @@ -280,6 +284,8 @@ pub trait EvalContextExt<'tcx>: crate::MiriInterpCxExt<'tcx> {
kind: RetagKind,
val: &ImmTy<'tcx>,
) -> InterpResult<'tcx, ImmTy<'tcx>> {
let _span =
enter_trace_span!("retag_ptr_value", "kind = {kind:?}, val.layout = {:?}", val.layout);
let this = self.eval_context_mut();
let method = this.machine.borrow_tracker.as_ref().unwrap().borrow().borrow_tracker_method;
match method {
Expand All @@ -293,6 +299,7 @@ pub trait EvalContextExt<'tcx>: crate::MiriInterpCxExt<'tcx> {
kind: RetagKind,
place: &PlaceTy<'tcx>,
) -> InterpResult<'tcx> {
let _span = enter_trace_span!("retag_place_contents", "kind = {kind:?}, place = {place:?}");
let this = self.eval_context_mut();
let method = this.machine.borrow_tracker.as_ref().unwrap().borrow().borrow_tracker_method;
match method {
Expand All @@ -302,6 +309,7 @@ pub trait EvalContextExt<'tcx>: crate::MiriInterpCxExt<'tcx> {
}

fn protect_place(&mut self, place: &MPlaceTy<'tcx>) -> InterpResult<'tcx, MPlaceTy<'tcx>> {
let _span = enter_trace_span!("protect_place", "place = {place:?}");
let this = self.eval_context_mut();
let method = this.machine.borrow_tracker.as_ref().unwrap().borrow().borrow_tracker_method;
match method {
Expand All @@ -311,6 +319,7 @@ pub trait EvalContextExt<'tcx>: crate::MiriInterpCxExt<'tcx> {
}

fn expose_tag(&self, alloc_id: AllocId, tag: BorTag) -> InterpResult<'tcx> {
let _span = enter_trace_span!("expose_tag", "alloc_id = {}, tag = {}", alloc_id.0, tag.0);
let this = self.eval_context_ref();
let method = this.machine.borrow_tracker.as_ref().unwrap().borrow().borrow_tracker_method;
match method {
Expand Down Expand Up @@ -354,6 +363,7 @@ pub trait EvalContextExt<'tcx>: crate::MiriInterpCxExt<'tcx> {
&self,
frame: &Frame<'tcx, Provenance, FrameExtra<'tcx>>,
) -> InterpResult<'tcx> {
let _span = enter_trace_span!("on_stack_pop");
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Shouldn't all these be called something like borrow_tracker::on_stack_pop or so? Just on_stack_pop is kind of unspecific.

Is there some way in the analysis tools to group all the borrow_tracker spans together?

let this = self.eval_context_ref();
let borrow_tracker = this.machine.borrow_tracker.as_ref().unwrap();
// The body of this loop needs `borrow_tracker` immutably
Expand Down Expand Up @@ -431,6 +441,7 @@ impl AllocState {
range: AllocRange,
machine: &MiriMachine<'tcx>,
) -> InterpResult<'tcx> {
let _span = enter_trace_span!("before_memory_read");
match self {
AllocState::StackedBorrows(sb) =>
sb.borrow_mut().before_memory_read(alloc_id, prov_extra, range, machine),
Expand All @@ -452,6 +463,7 @@ impl AllocState {
range: AllocRange,
machine: &MiriMachine<'tcx>,
) -> InterpResult<'tcx> {
let _span = enter_trace_span!("before_memory_write");
match self {
AllocState::StackedBorrows(sb) =>
sb.get_mut().before_memory_write(alloc_id, prov_extra, range, machine),
Expand All @@ -473,6 +485,7 @@ impl AllocState {
size: Size,
machine: &MiriMachine<'tcx>,
) -> InterpResult<'tcx> {
let _span = enter_trace_span!("before_memory_deallocation");
match self {
AllocState::StackedBorrows(sb) =>
sb.get_mut().before_memory_deallocation(alloc_id, prov_extra, size, machine),
Expand All @@ -482,6 +495,7 @@ impl AllocState {
}

pub fn remove_unreachable_tags(&self, tags: &FxHashSet<BorTag>) {
let _span = enter_trace_span!("remove_unreachable_tags");
match self {
AllocState::StackedBorrows(sb) => sb.borrow_mut().remove_unreachable_tags(tags),
AllocState::TreeBorrows(tb) => tb.borrow_mut().remove_unreachable_tags(tags),
Expand All @@ -496,6 +510,7 @@ impl AllocState {
tag: BorTag,
alloc_id: AllocId, // diagnostics
) -> InterpResult<'tcx> {
let _span = enter_trace_span!("release_protector");
match self {
AllocState::StackedBorrows(_sb) => interp_ok(()),
AllocState::TreeBorrows(tb) =>
Expand All @@ -506,6 +521,7 @@ impl AllocState {

impl VisitProvenance for AllocState {
fn visit_provenance(&self, visit: &mut VisitWith<'_>) {
let _span = enter_trace_span!("visit_provenance");
match self {
AllocState::StackedBorrows(sb) => sb.visit_provenance(visit),
AllocState::TreeBorrows(tb) => tb.visit_provenance(visit),
Expand Down
22 changes: 22 additions & 0 deletions src/helpers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1431,3 +1431,25 @@ impl ToU64 for usize {
self.try_into().unwrap()
}
}

/// This struct is needed to enforce `#[must_use]` on values produced by [enter_trace_span] even
/// when the "tracing" feature is not enabled.
#[must_use]
pub struct MaybeEnteredSpan {
#[cfg(feature = "tracing")]
pub _entered_span: tracing::span::EnteredSpan,
}

/// Enters a [tracing::info_span] only if the "tracing" feature is enabled, otherwise does nothing.
/// This is like [rustc_const_eval::enter_trace_span] except that it does not depend on the
/// [Machine] trait to check if tracing is enabled, because from the Miri codebase we can directly
/// check whether the "tracing" feature is enabled, unlike from the rustc_const_eval codebase.
#[macro_export]
macro_rules! enter_trace_span {
($($tt:tt)*) => {
$crate::helpers::MaybeEnteredSpan {
#[cfg(feature = "tracing")]
_entered_span: tracing::info_span!($($tt)*).entered()
}
}
}