Skip to content

Commit

Permalink
analyze: pointee: allow propagating CTys interprocedurally
Browse files Browse the repository at this point in the history
  • Loading branch information
spernsteiner committed Dec 2, 2024
1 parent 18351a9 commit 555f7d4
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 48 deletions.
15 changes: 2 additions & 13 deletions c2rust-analyze/src/analyze.rs
Original file line number Diff line number Diff line change
Expand Up @@ -627,12 +627,6 @@ fn run(tcx: TyCtxt) {
assert!(loop_count <= 1000);
let old_global_pointee_types = global_pointee_types.clone();

// Clear the `incomplete` flags for all global pointers. See comment in
// `pointee_types::solve::solve_constraints`.
for (_, tys) in global_pointee_types.iter_mut() {
tys.incomplete = false;
}

for &ldid in &all_fn_ldids {
if gacx.fn_analysis_invalid(ldid.to_def_id()) {
continue;
Expand Down Expand Up @@ -2276,15 +2270,10 @@ fn print_function_pointee_types<'tcx>(

for ptr in all_pointer_ids {
let tys = &pointee_types[ptr];
if tys.ltys.is_empty() && !tys.incomplete {
if tys.tys.is_empty() {
continue;
}
debug!(
" pointer {:?}: {:?}{}",
ptr,
tys.ltys,
if tys.incomplete { " (INCOMPLETE)" } else { "" }
);
debug!(" pointer {:?}: {:?}", ptr, tys.tys,);
}
}
}
Expand Down
64 changes: 29 additions & 35 deletions c2rust-analyze/src/pointee_type/solve.rs
Original file line number Diff line number Diff line change
Expand Up @@ -130,25 +130,42 @@ pub fn propagate_types<'tcx>(
#[derive(Clone, PartialEq, Eq, Debug, Default)]
pub struct PointeeTypes<'tcx> {
/// The possible pointee types for this pointer.
pub ltys: HashSet<LTy<'tcx>>,
/// If set, `ltys` is incomplete - the analysis identified pointee types that couldn't be
/// exported into global scope.
pub incomplete: bool,
pub tys: HashSet<CTy<'tcx>>,
}

impl<'tcx> PointeeTypes<'tcx> {
/// Get the sole `LTy` in this set, if there is exactly one.
pub fn get_sole_lty(&self) -> Option<LTy<'tcx>> {
if self.incomplete || self.ltys.len() != 1 {
if self.tys.len() != 1 {
return None;
}
let lty = *self.ltys.iter().next().unwrap();
Some(lty)
match self.tys.iter().copied().next()? {
CTy::Var(_) => None,
CTy::Ty(lty) => Some(lty),
}
}

pub fn merge(&mut self, other: PointeeTypes<'tcx>) {
self.ltys.extend(other.ltys);
self.incomplete |= other.incomplete;
self.tys.extend(other.tys);
}

pub fn simplify(&mut self, vars: &VarTable<'tcx>) {
let mut add = Vec::new();
let mut remove = Vec::new();
for &cty in &self.tys {
let rep = vars.cty_rep(cty);
if rep != cty {
remove.push(cty);
add.push(rep);
}
}

for cty in remove {
self.tys.remove(&cty);
}
for cty in add {
self.tys.insert(cty);
}
}
}

Expand All @@ -159,9 +176,7 @@ fn import<'tcx>(
) {
for (ptr, tys) in pointee_tys.iter() {
let ty_set = &mut ty_sets[ptr];
for &lty in &tys.ltys {
ty_set.insert(CTy::Ty(lty));
}
ty_set.extend(tys.tys.iter().copied());
}
}

Expand All @@ -171,25 +186,10 @@ fn export<'tcx>(
ty_sets: PointerTable<HashSet<CTy<'tcx>>>,
mut pointee_tys: PointerTableMut<PointeeTypes<'tcx>>,
) {
let local_ptr_range = pointee_tys.local().range();
for (ptr, ctys) in ty_sets.iter() {
let out = &mut pointee_tys[ptr];
for &cty in ctys {
if let CTy::Ty(lty) = var_table.cty_rep(cty) {
let mut ok = true;
lty.for_each_label(&mut |p| {
if local_ptr_range.contains(p) {
ok = false;
}
});
if ok {
out.ltys.insert(lty);
continue;
}
}
// If we failed to export this `CTy`, mark the `PointeeTypes` incomplete.
out.incomplete = true;
}
out.tys.extend(ctys.iter().copied());
out.simplify(var_table);
}
}

Expand All @@ -198,12 +198,6 @@ pub fn solve_constraints<'tcx>(
vars: &VarTable<'tcx>,
mut pointee_tys: PointerTableMut<PointeeTypes<'tcx>>,
) {
// Clear the `incomplete` flags for all local pointers. If there are still non-exportable
// types for those pointers, the flag will be set again in `export()`.
for (_, tys) in pointee_tys.local_mut().iter_mut() {
tys.incomplete = false;
}

let mut ty_sets = OwnedPointerTable::with_len_of(&pointee_tys.borrow());
import(pointee_tys.borrow(), ty_sets.borrow_mut());
init_type_sets(cset, ty_sets.borrow_mut());
Expand Down

0 comments on commit 555f7d4

Please sign in to comment.