flowistry_pdg_construction/local_analysis.rs
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962
use std::{borrow::Cow, collections::HashSet, fmt::Display, hash::Hash, iter};
use flowistry::mir::{placeinfo::PlaceInfo, FlowistryInput};
use flowistry_pdg::RichLocation;
use itertools::Itertools;
use log::{debug, log_enabled, trace, Level};
use rustc_errors::DiagCtxtHandle;
use rustc_hash::{FxHashMap, FxHashSet};
use rustc_hir::def_id::DefId;
use rustc_index::IndexVec;
use rustc_middle::{
mir::{
visit::Visitor, AggregateKind, BasicBlock, Body, HasLocalDecls, Location, Operand, Place,
Rvalue, Statement, Terminator, TerminatorEdges, TerminatorKind, RETURN_PLACE,
},
ty::{
AdtKind, EarlyBinder, GenericArgKind, GenericArgsRef, Instance, Ty, TyCtxt, TyKind,
TypingEnv,
},
};
use rustc_mir_dataflow::{self as df, fmt::DebugWithContext, Analysis};
use rustc_span::{source_map::Spanned, DesugaringKind, Span};
use rustc_utils::{mir::control_dependencies::ControlDependencies, AdtDefExt, BodyExt, PlaceExt};
use crate::{
approximation::ApproximationHandler,
async_support::{self, *},
body_cache::CachedBody,
calling_convention::*,
graph::{DepEdge, DepNode, OneHopLocation, PartialGraph, SourceUse, TargetUse},
mutation::{ModularMutationVisitor, Mutation, Time},
utils::{
self, handle_shims, is_async, is_virtual, place_ty_eq, try_monomorphize, ShimResult,
ShimType, TyAsFnResult,
},
CallChangeCallback, CallChanges, CallInfo, InlineMissReason, MemoPdgConstructor, SkipCall,
};
#[derive(PartialEq, Eq, Default, Clone, Debug)]
pub(crate) struct InstructionState<'tcx> {
last_mutation: FxHashMap<Place<'tcx>, FxHashSet<RichLocation>>,
}
impl<C> DebugWithContext<C> for InstructionState<'_> {}
impl df::JoinSemiLattice for InstructionState<'_> {
fn join(&mut self, other: &Self) -> bool {
utils::hashmap_join(
&mut self.last_mutation,
&other.last_mutation,
utils::hashset_join,
)
}
}
pub(crate) struct LocalAnalysis<'tcx, 'a, K> {
pub(crate) memo: &'a MemoPdgConstructor<'tcx, K>,
pub(super) root: Instance<'tcx>,
// TODO: We should generally be using mono_body, this one is only used in
// retyping. Try and find out if I can use mono_body there too or
// encapsulate this away so we don't accidentally use this polymorphic one.
body_with_facts: &'tcx CachedBody<'tcx>,
pub(crate) mono_body: Body<'tcx>,
pub(crate) def_id: DefId,
pub(crate) place_info: PlaceInfo<'tcx>,
control_dependencies: ControlDependencies<BasicBlock>,
pub(crate) body_assignments: utils::BodyAssignments,
start_loc: FxHashSet<RichLocation>,
pub(crate) param_env: TypingEnv<'tcx>,
k: K,
}
impl<'tcx, 'a, K> LocalAnalysis<'tcx, 'a, K> {
pub(super) fn generic_args(&self) -> GenericArgsRef<'tcx> {
self.root.args
}
fn dump_mir(&self) {
use std::io::Write;
let path = self.tcx().def_path_str(self.def_id) + ".mir";
let mut f = std::fs::File::create(path.as_str()).unwrap();
write!(f, "{}", self.mono_body.to_string(self.tcx()).unwrap()).unwrap();
}
/// Returns all pairs of `(src, edge)`` such that the given `location` is control-dependent on `edge`
/// with input `src`.
pub(crate) fn find_control_inputs(
&self,
location: Location,
) -> Vec<(DepNode<'tcx, OneHopLocation>, DepEdge<OneHopLocation>)> {
let mut blocks_seen = HashSet::<BasicBlock>::from_iter(Some(location.block));
let mut block_queue = vec![location.block];
let mut out = vec![];
while let Some(block) = block_queue.pop() {
if let Some(ctrl_deps) = self.control_dependencies.dependent_on(block) {
for dep in ctrl_deps.iter() {
let ctrl_loc = self.mono_body.terminator_loc(dep);
let Terminator {
kind: TerminatorKind::SwitchInt { discr, .. },
source_info,
} = self.mono_body.basic_blocks[dep].terminator()
else {
if blocks_seen.insert(dep) {
block_queue.push(dep);
}
continue;
};
if matches!(
source_info.span.desugaring_kind(),
Some(DesugaringKind::Await)
) {
// We are dealing with control flow that was introduced
// by the "await" state machine. We don't care about
// this sine it's possible semantic impact is negligible.
continue;
}
let Some(ctrl_place) = discr.place() else {
continue;
};
let at = ctrl_loc.into();
let src = self.make_dep_node(ctrl_place, ctrl_loc);
let edge = DepEdge::control(at, SourceUse::Operand, TargetUse::Assign);
out.push((src, edge));
}
}
}
out
}
fn call_change_callback(&self) -> &dyn CallChangeCallback<'tcx, K> {
self.memo.call_change_callback.as_ref()
}
pub(crate) fn async_info(&self) -> &AsyncInfo {
&self.memo.async_info
}
fn make_dep_node(
&self,
place: Place<'tcx>,
location: impl Into<RichLocation>,
) -> DepNode<'tcx, OneHopLocation> {
let location = location.into();
debug!(
"Creating dep node for {place:?} (base ty {:?}) in {} at {:?}",
Place::from(place.local)
.ty(self.body_with_facts.body(), self.tcx())
.ty,
self.tcx().def_path_str(self.def_id),
location,
);
debug!(
"Place type is {:?}",
place.ty(&self.mono_body, self.tcx()).ty,
);
DepNode::new(
place,
location.into(),
self.tcx(),
&self.mono_body,
self.def_id,
is_split(
place.ty(&self.mono_body, self.tcx()).ty,
self.def_id,
self.tcx(),
),
)
}
/// Returns the aliases of `place`. See [`PlaceInfo::aliases`] for details.
pub(crate) fn aliases(&'a self, place: Place<'tcx>) -> impl Iterator<Item = Place<'tcx>> + 'a {
// MASSIVE HACK ALERT:
// The issue is that monomorphization erases regions, due to how it's implemented in rustc.
// However, Flowistry's alias analysis uses regions to figure out aliases.
// To workaround this incompatibility, when we receive a monomorphized place, we try to
// recompute its type in the context of the original region-containing body as far as possible.
//
// For example, say _2: (&'0 impl Foo,) in the original body and _2: (&(i32, i32),) in the monomorphized body.
// Say we ask for aliases (*(_2.0)).0. Then we will retype ((*_2.0).0).0 and receive back (*_2.0: &'0 impl Foo).
// We can ask for the aliases in the context of the original body, receiving e.g. {_1}.
// Then we reproject the aliases with the remaining projection, to create {_1.0}.
//
// This is a massive hack bc it's inefficient and I'm not certain that
// it's sound.
let body = self.body_with_facts.body();
let place_retyped = utils::retype_place(place, self.tcx(), body, self.def_id);
self.place_info
.aliases(place_retyped)
.iter()
.map(move |unnormalized_alias| {
// The place we get back is not monomorphized, since aliases are
// calculated on the original body. And because rustc will crash
// if we have regions in the type, we erase those first.
let alias = self.normalize_place(unnormalized_alias);
// If the type of the alias is not the same as the retyped
// place, then adding the remaining projections from the
// original place won't work so we overtaint to the entire
// alias.
if !place_ty_eq(
unnormalized_alias.ty(body, self.tcx()),
place_retyped.ty(body, self.tcx()),
) {
//println!("Overtainting alias {alias:?} because type {:?} != {:?}", ta.ty, tb.ty);
return alias;
}
//let alias = self.tcx().erase_regions(*alias);
let mut projection = alias.projection.to_vec();
projection.extend(&place.projection[place_retyped.projection.len()..]);
Place::make(alias.local, &projection, self.tcx())
//println!("Alias: {p:?} for base alias {alias:?}, place: {place:?}, retyped: {place_retyped:?}");
})
}
pub fn normalize_place(&self, place: &Place<'tcx>) -> Place<'tcx> {
let place = self.tcx().erase_regions(*place);
// Normalize the place to remove regions and other things that are not
// needed for the PDG.
self.tcx()
.try_instantiate_and_normalize_erasing_regions(
self.generic_args(),
self.param_env,
EarlyBinder::bind(place),
)
.unwrap_or_else(|err| {
panic!(
"Failed to normalize place {place:?} in {}: {err:?}",
self.tcx().def_path_str(self.def_id)
)
})
}
pub(crate) fn tcx(&self) -> TyCtxt<'tcx> {
self.memo.tcx
}
pub(crate) fn dcx(&self) -> DiagCtxtHandle<'tcx> {
self.tcx().dcx()
}
/// Returns all nodes `src` such that `src` is:
/// 1. Part of the value of `input`
/// 2. The most-recently modified location for `src`
pub(crate) fn find_data_inputs(
&self,
state: &InstructionState<'tcx>,
input: Place<'tcx>,
) -> Vec<DepNode<'tcx, OneHopLocation>> {
trace!("Finding inputs for place {input:?}");
// Include all sources of indirection (each reference in the chain) as relevant places.
let provenance = input
.refs_in_projection(&self.mono_body, self.tcx())
.map(|(place_ref, _)| Place::from_ref(place_ref, self.tcx()));
let inputs = iter::once(input).chain(provenance);
inputs
// **POINTER-SENSITIVITY:**
// If `input` involves indirection via dereferences, then resolve it to the direct places it could point to.
.flat_map(|place| self.aliases(place))
.flat_map(|alias| {
// **FIELD-SENSITIVITY:**
// Find all places that have been mutated which conflict with `alias.`
let conflicts = state
.last_mutation
.iter()
.map(|(k, locs)| (*k, locs))
.filter(move |(place, _)| {
if place.is_indirect() && place.is_arg(&self.mono_body) {
// HACK: `places_conflict` seems to consider it a bug is `borrow_place`
// includes a dereference, which should only happen if `borrow_place`
// is an argument. So we special case that condition and just compare for local equality.
//
// TODO: this is not field-sensitive!
place.local == alias.local
} else {
trace!("Checking conflict status of {place:?} and {alias:?}");
utils::places_conflict(self.tcx(), &self.mono_body, *place, alias)
}
});
// Special case: if the `alias` is an un-mutated argument, then include it as a conflict
// coming from the special start location.
let alias_last_mut = if alias.is_arg(&self.mono_body) {
Some((alias, &self.start_loc))
} else {
None
};
// println!("Alias {alias:?} for place {input:?}");
// For each `conflict`` last mutated at the locations `last_mut`:
conflicts
.chain(alias_last_mut)
.flat_map(|(conflict, last_mut_locs)| {
//println!("Conflict {conflict:?} for place {input:?}");
// For each last mutated location:
last_mut_locs.iter().map(move |last_mut_loc| {
// Return <CONFLICT> @ <LAST_MUT_LOC> as an input node.
self.make_dep_node(conflict, *last_mut_loc)
})
})
})
.collect()
}
pub(crate) fn find_outputs(
&self,
mutated: Place<'tcx>,
location: Location,
) -> Vec<(Place<'tcx>, DepNode<'tcx, OneHopLocation>)> {
// **POINTER-SENSITIVITY:**
// If `mutated` involves indirection via dereferences, then resolve it to the direct places it could point to.
let aliases = self.aliases(mutated).collect_vec();
// **FIELD-SENSITIVITY:** we do NOT deal with fields on *writes* (in this function),
// only on *reads* (in `add_input_to_op`).
// For each mutated `dst`:
aliases
.iter()
.map(|dst| {
// Create a destination node for (DST @ CURRENT_LOC).
(*dst, self.make_dep_node(*dst, location))
})
.collect()
}
/// Updates the last-mutated location for `dst` to the given `location`.
fn apply_mutation(
&self,
state: &mut InstructionState<'tcx>,
location: Location,
mutated: Place<'tcx>,
) {
let mutated = self.normalize_place(&mutated);
self.find_outputs(mutated, location)
.into_iter()
.for_each(|(dst, _)| {
// Create a destination node for (DST @ CURRENT_LOC).
//debug_assert_resolved!(dst);
// Clear all previous mutations.
let dst_mutations = state.last_mutation.entry(dst).or_default();
dst_mutations.clear();
// Register that `dst` is mutated at the current location.
dst_mutations.insert(RichLocation::Location(location));
})
}
/// Resolve a function [`Operand`] to a specific [`DefId`] and generic arguments if possible.
pub(crate) fn operand_to_def_id(&self, func: &Operand<'tcx>) -> TyAsFnResult<'tcx> {
let ty = func.ty(&self.mono_body, self.tcx());
utils::type_as_fn(self.tcx(), ty)
}
fn fmt_fn(&self, def_id: DefId) -> String {
self.tcx().def_path_str(def_id)
}
}
impl<'tcx, 'a, K: Hash + Eq + Clone> LocalAnalysis<'tcx, 'a, K> {
/// Creates [`GraphConstructor`] for a function resolved as `fn_resolution`
/// in a given `calling_context`.
///
/// Returns `None`, if this body should not be analyzed.
pub(crate) fn new(
memo: &'a MemoPdgConstructor<'tcx, K>,
root: Instance<'tcx>,
k: K,
) -> Option<LocalAnalysis<'tcx, 'a, K>> {
let tcx = memo.tcx;
let def_id = root.def_id();
let body_with_facts = memo.body_cache.try_get(def_id)?;
let param_env = TypingEnv::post_analysis(tcx, def_id).with_post_analysis_normalized(tcx);
let body = try_monomorphize(
root,
tcx,
param_env,
body_with_facts.body(),
tcx.def_span(def_id),
)
.unwrap();
let place_info = PlaceInfo::build(tcx, def_id, body_with_facts);
let control_dependencies = body.control_dependencies();
let mut start_loc = FxHashSet::default();
start_loc.insert(RichLocation::Start);
let body_assignments = utils::find_body_assignments(&body);
let slf = LocalAnalysis {
memo,
root,
body_with_facts,
mono_body: body,
place_info,
control_dependencies,
start_loc,
def_id,
body_assignments,
param_env,
k,
};
if memo.dump_mir {
slf.dump_mir();
}
Some(slf)
}
pub(crate) fn determine_call_handling<'b>(
&'b self,
location: Location,
func: Cow<'_, Operand<'tcx>>,
args: Cow<'b, [Spanned<Operand<'tcx>>]>,
span: Span,
) -> Option<CallHandling<'tcx, 'b, K>> {
let tcx = self.tcx();
trace!(
"Considering call at {location:?} in {:?}",
self.tcx().def_path_str(self.def_id)
);
let (called_def_id, generic_args) = match self.operand_to_def_id(&func) {
TyAsFnResult::Resolved {
def_id,
generic_args,
} => (def_id, generic_args),
TyAsFnResult::FnPtr => {
self.memo
.maybe_span_err(span, "Operand is a function pointer");
return None;
}
TyAsFnResult::NotAFunction => {
self.dcx().span_err(span, "Operand is not a function");
return None;
}
};
trace!(
"Resolved call to function: {} with generic args {generic_args:?}",
self.fmt_fn(called_def_id)
);
// Monomorphize the called function with the known generic_args.
let typing_env = self
.mono_body
.typing_env(tcx)
.with_post_analysis_normalized(tcx);
let Some(mut resolved_fn) =
utils::try_resolve_function(self.tcx(), called_def_id, typing_env, generic_args)
else {
let dynamics = generic_args.iter()
.flat_map(|g| g.walk())
.filter(|arg| matches!(arg.unpack(), GenericArgKind::Type(t) if matches!(t.kind(), TyKind::Dynamic(..))))
.collect::<Box<[_]>>();
let mut msg = format!(
"instance resolution for call to function {} failed.",
tcx.def_path_str(called_def_id)
);
if !dynamics.is_empty() {
use std::fmt::Write;
write!(msg, " Dynamic arguments ").unwrap();
let mut first = true;
for dyn_ in dynamics.iter() {
if !first {
write!(msg, ", ").unwrap();
}
first = false;
write!(msg, "`{dyn_}`").unwrap();
}
write!(
msg,
" were found.\n\
These may have been injected by Paralegal to instantiate generics \n\
at the entrypoint (location of #[paralegal::analyze]).\n\
A likely reason why this may cause this resolution to fail is if the\n\
method or function this attempts to resolve has a `Sized` constraint.\n\
Such a constraint can be implicit if this is a type variable in a\n\
trait definition and no refutation (`?Sized` constraint) is present."
)
.unwrap();
self.dcx().span_warn(span, msg);
} else {
self.dcx().span_err(span, msg);
}
return None;
};
let call_kind = match handle_shims(resolved_fn, self.tcx(), typing_env, span) {
ShimResult::IsHandledShim {
instance,
shim_type,
} => {
resolved_fn = instance;
CallKind::Indirect {
shim: Some(shim_type),
}
}
ShimResult::IsNonHandleableShim => {
trace!("Bailing because shim cannot behandled (like function pointer)");
return None;
}
ShimResult::IsNotShim => {
self.classify_call_kind(called_def_id, resolved_fn, &args, span)
}
};
let resolved_def_id = resolved_fn.def_id();
if log_enabled!(Level::Trace) && called_def_id != resolved_def_id {
let (called, resolved) = (self.fmt_fn(called_def_id), self.fmt_fn(resolved_def_id));
trace!(" `{called}` monomorphized to `{resolved}`",);
}
if let Some(handler) = self.can_approximate_async_functions(resolved_def_id, span) {
return Some(CallHandling::ApproxAsyncSM(handler));
};
trace!(" Handling call! with kind {}", call_kind);
// Recursively generate the PDG for the child function.
let info = CallInfo {
callee: resolved_fn,
call_string: location,
async_parent: if let CallKind::AsyncPoll(poll) = &call_kind {
// Special case for async. We ask for skipping not on the closure, but
// on the "async" function that created it. This is needed for
// consistency in skipping. Normally, when "poll" is inlined, mutations
// introduced by the creator of the future are not recorded and instead
// handled here, on the closure. But if the closure is skipped we need
// those mutations to occur. To ensure this we always ask for the
// "CallChanges" on the creator so that both creator and closure have
// the same view of whether they are inlined or "Skip"ped.
poll.async_fn_parent
} else {
None
},
span,
arguments: &args,
caller_body: &self.mono_body,
param_env: typing_env,
cache_key: &self.k,
};
let call_changes = self.call_change_callback().on_inline(info);
// Handle async functions at the time of polling, not when the future is created.
if is_async(tcx, resolved_def_id) {
trace!(" Bailing because func is async");
// If a skip was requested then "poll" will not be inlined later so we
// bail with "None" here and perform the mutations. Otherwise we bail with
// "Some", knowing that handling "poll" later will handle the mutations.
return (!matches!(
&call_changes,
CallChanges {
skip: SkipCall::Skip,
..
}
))
.then_some(CallHandling::ApproxAsyncFn);
}
let (calling_convention, precise, new_cache_key) = match call_changes.skip {
SkipCall::Skip => {
trace!(" Bailing because user callback said to bail");
return None;
}
SkipCall::Replace {
instance,
calling_convention,
cache_key,
} => {
trace!(" Replacing call as instructed by user");
resolved_fn = instance;
(calling_convention, false, cache_key)
}
SkipCall::NoSkip(cache_key) => (
CallingConvention::from_call_kind(&call_kind, args),
true,
cache_key,
),
};
if is_virtual(tcx, resolved_def_id) {
trace!(" bailing because is unresolvable trait method");
self.call_change_callback().on_inline_miss(
resolved_fn,
typing_env,
location,
self.root,
InlineMissReason::TraitMethod,
span,
);
return None;
}
let cache_key = (resolved_fn, new_cache_key);
if self.memo.is_recursion(resolved_fn) {
trace!(" bailing because recursion");
None
} else {
Some(CallHandling::Ready {
calling_convention,
descriptor: cache_key,
precise,
})
}
}
/// Attempt to inline a call to a function.
///
/// The return indicates whether we were successfully able to perform the inlining.
fn handle_call(
&self,
state: &mut InstructionState<'tcx>,
location: Location,
func: &Operand<'tcx>,
args: &[Spanned<Operand<'tcx>>],
destination: Place<'tcx>,
span: Span,
) -> bool {
// Note: my comments here will use "child" to refer to the callee and
// "parent" to refer to the caller, since the words are most visually distinct.
let Some(preamble) =
self.determine_call_handling(location, Cow::Borrowed(func), Cow::Borrowed(args), span)
else {
return false;
};
debug!("Call handling is {}", preamble.as_ref());
let (_descriptor, child_constructor, calling_convention, precise) = match preamble {
CallHandling::Ready {
descriptor,
calling_convention,
precise,
} => (
descriptor.clone(),
self.memo
.construct_for(descriptor)
.expect("Existence check should have already happened"),
calling_convention,
precise,
),
CallHandling::ApproxAsyncFn => {
// Register a synthetic assignment of `future = (arg0, arg1, ...)`.
let rvalue = Rvalue::Aggregate(
Box::new(AggregateKind::Tuple),
IndexVec::from_iter(args.iter().map(|o| o.node.clone())),
);
self.modular_mutation_visitor(state)
.visit_assign(&destination, &rvalue, location);
return true;
}
CallHandling::ApproxAsyncSM(handler) => {
handler(
self,
&mut self.modular_mutation_visitor(state),
args,
destination,
location,
);
return true;
}
};
debug!(
"Inlining call at {span:?} in {} at {:?} with handling {}",
self.tcx().def_path_debug_str(self.def_id),
location,
calling_convention.as_ref()
);
let parentable_dsts = child_constructor.parentable_dsts();
let parent_body = &self.mono_body;
let place_translator = PlaceTranslator::new(
self.async_info(),
self.def_id,
parent_body,
self.tcx(),
destination,
&calling_convention,
precise,
);
// For each destination node CHILD that is parentable to PLACE,
// add an edge from CHILD -> PLACE.
//
// PRECISION TODO: for a given child place, we only want to connect
// the *last* nodes in the child function to the parent, not *all* of them.
trace!("CHILD -> PARENT EDGES:");
for (child_dst, _) in parentable_dsts {
if let Some(parent_place) = place_translator.translate_to_parent(child_dst.place) {
self.apply_mutation(state, location, parent_place);
}
}
true
}
fn modular_mutation_visitor<'b: 'a>(
&'b self,
state: &'a mut InstructionState<'tcx>,
) -> ModularMutationVisitor<'b, 'tcx, impl FnMut(Location, Mutation<'tcx>) + 'b> {
ModularMutationVisitor::new(
&self.place_info,
move |location, mutation: Mutation<'tcx>| {
self.apply_mutation(state, location, mutation.mutated)
},
)
}
pub(crate) fn construct_partial(&'a self) -> PartialGraph<'tcx, K> {
let mut analysis = self.iterate_to_fixpoint(self.tcx(), &self.mono_body, None);
let mut final_state = PartialGraph::new(
self.generic_args(),
self.def_id,
self.mono_body.arg_count,
self.mono_body.local_decls(),
self.k.clone(),
);
analysis.visit_reachable_with(&self.mono_body, &mut final_state);
let all_returns = self
.mono_body
.all_returns()
.map(|ret| ret.block)
.collect_vec();
let mut analysis = analysis.into_results_cursor(&self.mono_body);
for block in all_returns {
analysis.seek_to_block_end(block);
let return_state = analysis.get();
for (place, locations) in &return_state.last_mutation {
let ret_kind = if place.local == RETURN_PLACE {
TargetUse::Return
} else if let Some(num) = other_as_arg(*place, &self.mono_body) {
TargetUse::MutArg(num)
} else {
continue;
};
for location in locations {
let src = self.make_dep_node(*place, *location);
let dst = self.make_dep_node(*place, RichLocation::End);
let edge = DepEdge::data(
self.mono_body.terminator_loc(block).into(),
SourceUse::Operand,
ret_kind,
);
final_state.edges.insert((src, dst, edge));
}
}
}
final_state
}
/// Determine the type of call-site.
///
/// The error case is if we tried to resolve this as async and failed. We
/// know it *is* async but we couldn't determine the information needed to
/// analyze the function, therefore we will have to approximate it.
///
/// In case of async, checks whether the function call, described by the unresolved `def_id`
/// and the resolved instance `resolved_fn` is a call to [`<T as
/// Future>::poll`](std::future::Future::poll) where `T` is the type of an
/// `async fn` or `async {}` created generator.
///
/// Resolves the original arguments that constituted the generator.
fn classify_call_kind<'b>(
&'b self,
def_id: DefId,
resolved_fn: Instance<'tcx>,
original_args: &'b [Spanned<Operand<'tcx>>],
span: Span,
) -> CallKind<'tcx> {
let lang_items = self.tcx().lang_items();
// Why is calling `is_async_fn_or_block` here necessary? Because the
// rewriting of arguments only needs to take place if rustc
// automatically created that implementation of `poll` for us. If this
// is a manual `poll` implementation, the signature of the resolved
// function and the signature of `poll` will be the same.
//
// Why is this important? For example, an auto-generated async closure's
// return gets wrapped in `Ready` automatically, whereas a manual
// implementation does it explicitly.
if lang_items.future_poll_fn() == Some(def_id)
&& async_support::is_async_fn_or_block(self.tcx(), resolved_fn)
{
return match self.find_async_args(resolved_fn, original_args, span) {
Ok(poll) => CallKind::AsyncPoll(poll),
Err(str) => self.tcx().dcx().span_fatal(span, str),
};
}
self.try_indirect_call_kind(resolved_fn.def_id())
.unwrap_or(CallKind::Direct)
}
fn try_indirect_call_kind(&self, def_id: DefId) -> Option<CallKind<'tcx>> {
self.tcx()
.is_closure_like(def_id)
.then_some(CallKind::Indirect { shim: None })
}
fn terminator_visitor<'b: 'a>(
&'b self,
state: &'b mut InstructionState<'tcx>,
time: Time,
) -> ModularMutationVisitor<'b, 'tcx, impl FnMut(Location, Mutation<'tcx>) + 'b> {
let mut vis = self.modular_mutation_visitor(state);
vis.set_time(time);
vis
}
fn handle_terminator(
&self,
terminator: &Terminator<'tcx>,
state: &mut InstructionState<'tcx>,
location: Location,
time: Time,
) where
K: Clone + Eq + Hash,
{
if let TerminatorKind::Call {
func,
args,
destination,
..
} = &terminator.kind
{
if self.handle_call(
state,
location,
func,
args,
*destination,
terminator.source_info.span,
) {
return;
}
}
// Fallback: call the visitor
self.terminator_visitor(state, time)
.visit_terminator(terminator, location)
}
}
impl<'a, 'tcx, K: Hash + Eq + Clone> df::Analysis<'tcx> for &'a LocalAnalysis<'tcx, 'a, K> {
type Domain = InstructionState<'tcx>;
const NAME: &'static str = "LocalPdgConstruction";
fn bottom_value(&self, _body: &Body<'tcx>) -> Self::Domain {
InstructionState::default()
}
fn initialize_start_block(&self, _body: &Body<'tcx>, _state: &mut Self::Domain) {}
fn apply_primary_statement_effect(
&mut self,
state: &mut Self::Domain,
statement: &Statement<'tcx>,
location: Location,
) {
self.modular_mutation_visitor(state)
.visit_statement(statement, location)
}
fn apply_primary_terminator_effect<'mir>(
&mut self,
state: &mut Self::Domain,
terminator: &'mir Terminator<'tcx>,
location: Location,
) -> TerminatorEdges<'mir, 'tcx> {
self.handle_terminator(terminator, state, location, Time::Unspecified);
terminator.edges()
}
fn apply_call_return_effect(
&mut self,
_state: &mut Self::Domain,
_block: BasicBlock,
_return_places: rustc_middle::mir::CallReturnPlaces<'_, 'tcx>,
) {
}
}
pub enum CallKind<'tcx> {
/// A standard function call like `f(x)`.
Direct,
/// A call to a function variable, like `fn foo(f: impl Fn()) { f() }`
Indirect {
/// The call takes place via a shim that implements `FnOnce` for a `Fn`
/// or `FnMut` closure.
shim: Option<ShimType>,
},
/// A poll to an async function, like `f.await`.
AsyncPoll(AsyncFnPollEnv<'tcx>),
}
impl Display for CallKind<'_> {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
Self::Direct => f.write_str("direct")?,
Self::AsyncPoll(_) => f.write_str("async poll")?,
Self::Indirect { shim: None } => f.write_str("indirect")?,
Self::Indirect { shim: Some(shim) } => write!(f, "({} shim)", shim.as_ref())?,
}
Ok(())
}
}
#[derive(strum::AsRefStr)]
pub(crate) enum CallHandling<'tcx, 'a, K> {
ApproxAsyncFn,
Ready {
calling_convention: CallingConvention<'tcx>,
descriptor: (Instance<'tcx>, K),
precise: bool,
},
ApproxAsyncSM(ApproximationHandler<'tcx, 'a, K>),
}
fn other_as_arg<'tcx>(place: Place<'tcx>, body: &Body<'tcx>) -> Option<u8> {
(body.local_kind(place.local) == rustc_middle::mir::LocalKind::Arg)
.then(|| place.local.as_u32() as u8 - 1)
}
/// This used to be implemented as `place_info.children(place).iter().any(|p| *p
/// != place)`, but this is more efficient and should cause fewer spurious
/// errors, since it only explores the type shallowly.
fn is_split<'tcx>(ty: Ty<'tcx>, context: DefId, tcx: TyCtxt<'tcx>) -> bool {
match ty.kind() {
_ if ty.is_box() => true,
TyKind::Tuple(fields) => !fields.is_empty(),
TyKind::Adt(def, ..) => match def.adt_kind() {
AdtKind::Struct => def.all_visible_fields(context, tcx).next().is_some(),
AdtKind::Union => false,
AdtKind::Enum => def.all_fields().next().is_some(),
},
TyKind::Array(..) | TyKind::Slice(..) => true,
TyKind::Ref(..) => false,
TyKind::RawPtr(..) => true,
TyKind::Closure(_, args) | TyKind::Coroutine(_, args) => {
is_split(args.as_closure().tupled_upvars_ty(), context, tcx)
}
TyKind::FnDef(..)
| TyKind::FnPtr(..)
| TyKind::Foreign(..)
| TyKind::Dynamic(..)
| TyKind::Param(..)
| TyKind::Never => false,
_ if ty.is_primitive_ty() => false,
_ => {
log::warn!("unimplemented {ty:?} ({:?})", ty.kind());
false
}
}
}