Skip to content
Merged
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
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -857,6 +857,10 @@ Matching, body/override precedence and disabling rules are specified in

## SQLite database schema

A callable constructed by `std::bind` can appear as a `Bound` location in
debug exports. Its argument-flow rows are attached to the call site that
invokes it. See [Bound callables](docs/IR.md#bound-callables).

Minimal and full databases record indirect callee and return-destination
variable IDs in `call_sites.callee_var` and `call_sites.return_dst`. Export uses
indexed SQLite joins for indirect-return provenance, building the required
Expand Down
9 changes: 8 additions & 1 deletion crates/trace-analysis/src/constraints.rs
Original file line number Diff line number Diff line change
Expand Up @@ -72,11 +72,14 @@ pub struct CallGraphEdge {
pub resolution: ResolutionKind,
}

#[derive(Debug, Clone)]
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub struct ArgFlowEdge {
pub call_site: trace_ir::CallSiteId,
pub arg_index: u32,
pub actual_var: Option<VarId>,
/// The address temporary passed for a source argument such as `&x`.
/// The row still names `x` as its actual variable.
pub actual_address: Option<VarId>,
pub actual_fn: Option<trace_ir::FnId>,
pub formal: VarId,
}
Expand All @@ -95,6 +98,10 @@ pub enum LocKind {
Function,
/// Interned C string literal (value in `AbstractLocation.desc`).
StringLit,
/// A callable `std::bind` built: a function value naming the function it
/// wraps (`Pag::bound_callables` holds its receiver and bound
/// arguments). See docs/IR.md, "Bound callables".
Bound,
}

#[derive(Debug, Clone)]
Expand Down
5 changes: 4 additions & 1 deletion crates/trace-analysis/src/memory.rs
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,10 @@ fn collect_cells<'a>(
let mut unfoldable = 0usize;
for &loc in targets {
let loc_kind = pag.locations[loc.0 as usize].kind;
if matches!(loc_kind, LocKind::Function | LocKind::StringLit) {
if matches!(
loc_kind,
LocKind::Function | LocKind::Bound | LocKind::StringLit
) {
continue;
}
cells.push(loc);
Expand Down
100 changes: 92 additions & 8 deletions crates/trace-analysis/src/pag.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,21 @@ use indexmap::IndexMap;
use rustc_hash::{FxBuildHasher, FxHashMap, FxHashSet};
use std::sync::Arc;
use trace_ir::{
CallSite, ContainerOwner, FieldId, FlowConstraint, FnId, LocId, PagNodeId, Program, ReturnFlow,
StorageClass, TargetId, TypeDesc, TypeId, VarId,
BindTarget, BoundArg, CallSite, ContainerOwner, FieldId, FlowConstraint, FnId, LocId,
PagNodeId, Program, ReturnFlow, StorageClass, TargetId, TypeDesc, TypeId, VarId,
};

/// What a `Bound` location binds ([`Pag::bound_callables`]): the callable
/// `std::bind` built, as lowering read it off the bind expression
/// (docs/IR.md, "Bound callables").
#[derive(Debug, Clone)]
pub struct BoundCallable {
/// The object bound as a member function's implicit `this`.
pub receiver: Option<BoundArg>,
/// One entry per bound argument, in the order written.
pub args: Vec<BoundArg>,
}

#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub enum PagNodeKind {
Var(VarId),
Expand Down Expand Up @@ -128,6 +139,14 @@ pub struct Pag {
pub indices: SolverIndices,
/// Call results produced by `return_field`, for transitive return expansion.
return_field_values: FxHashSet<VarId>,
/// Each `Bound` location's binding, by location: the function it wraps,
/// with the receiver and arguments `std::bind` holds for it
/// (docs/IR.md, "Bound callables"). Empty unless the program binds.
pub bound_callables: FxHashMap<LocId, BoundCallable>,
/// Variable-target binding facts to revisit as the source gains functions.
pub(crate) bind_sources: FxHashMap<PagNodeId, Vec<usize>>,
/// Each binding fact's realized locations, in discovery order.
bound_origins: FxHashMap<FlowConstraint, Vec<PagNodeId>>,
}

impl Pag {
Expand Down Expand Up @@ -815,7 +834,7 @@ impl Pag {

fn build_flow_constraints(&mut self, program: &Program, models: &ModelLookup) {
let mut container_elements = FxHashMap::default();
for flow in &program.flow {
for (index, flow) in program.flow.iter().enumerate() {
match flow {
FlowConstraint::Copy { dst, src } => {
let dst_n = self.var_node_id(*dst);
Expand Down Expand Up @@ -1058,6 +1077,13 @@ impl Pag {
let src_n = self.var_node_id(*src);
self.add_unwrap(dst_n, src_n);
}
FlowConstraint::BindCallable { target, .. } => match *target {
BindTarget::Function(callee) => self.materialize_binding(program, flow, callee),
BindTarget::Value(var) => {
let source = self.var_node_id(var);
self.bind_sources.entry(source).or_default().push(index);
}
},
}
}
self.expand_call_site_returns(program, models);
Expand Down Expand Up @@ -1550,16 +1576,74 @@ impl Pag {
/// address the actual temporary holds.
pub fn argument_var(&self, cs: &trace_ir::CallSite, idx: u32) -> Option<VarId> {
let actual = cs.var_args.iter().find(|(j, _)| *j == idx)?.1;
let addressed = cs
.addr_of_args()
.contains(&idx)
Some(self.argument_var_for_value(actual, cs.addr_of_args().contains(&idx)))
}

/// The source variable denoted by a collected argument value.
pub(crate) fn argument_var_for_value(&self, actual: VarId, addr_of: bool) -> VarId {
addr_of
.then(|| {
self.var_node
.get(&actual)
.and_then(|&n| self.addressed_var(n))
})
.flatten();
Some(addressed.unwrap_or(actual))
.flatten()
.unwrap_or(actual)
}

/// Each realized target of this exact binding fact, including those
/// discovered during solve. Configuration alternatives can share a destination.
pub fn bound_locations(&self, fact: &FlowConstraint) -> &[PagNodeId] {
self.bound_origins.get(fact).map_or(&[], Vec::as_slice)
}

pub(crate) fn materialize_binding(
&mut self,
program: &Program,
fact: &FlowConstraint,
callee: FnId,
) {
let FlowConstraint::BindCallable { dst, args, .. } = fact else {
unreachable!("binding index must name a BindCallable");
};
let function = program.symbols.function(callee);
if !function.pack_positions.is_empty()
|| self.bound_locations(fact).iter().any(|node| {
let PagNodeKind::Loc(loc) = self.nodes[node.0 as usize].kind else {
unreachable!("binding origin must name a location");
};
self.locations[loc.0 as usize].fn_id == Some(callee)
})
{
return;
}
let mut args = args.clone();
let member = program.symbols.is_member_function(callee) && !function.is_static_member;
let receiver = member.then(|| match args.first().copied() {
Some(BoundArg::Pack(var)) => BoundArg::Stored(var),
Some(_) => args.remove(0),
None => BoundArg::Opaque,
});
let loc = LocId(self.locations.len() as u32);
self.alloc_loc(AbstractLocation {
id: loc,
kind: LocKind::Bound,
var: None,
fn_id: Some(callee),
field: None,
type_id: program.types.unknown(),
desc: function.name.clone(),
});
self.set_location_target(loc, program.symbols.variable(*dst).target);
self.bound_callables
.insert(loc, BoundCallable { receiver, args });
let dst = self.var_node_id(*dst);
let location = self.loc_node[&loc];
self.bound_origins
.entry(fact.clone())
.or_default()
.push(location);
self.add_addr_of(dst, location);
}

pub fn call_target_node(&mut self, cs: trace_ir::CallSiteId) -> PagNodeId {
Expand Down
Loading
Loading