From e2b3ea3d4163ecbbc0f8ef843a11c6248d74ba08 Mon Sep 17 00:00:00 2001 From: Stuart Pernsteiner Date: Tue, 26 Mar 2024 14:12:35 -0700 Subject: [PATCH] analyze: in pointwise mode, mark all fields and statics FIXED --- c2rust-analyze/src/analyze.rs | 35 ++++++++++++++++++++++++++++++++++- 1 file changed, 34 insertions(+), 1 deletion(-) diff --git a/c2rust-analyze/src/analyze.rs b/c2rust-analyze/src/analyze.rs index ba5e28ba39..9aad495302 100644 --- a/c2rust-analyze/src/analyze.rs +++ b/c2rust-analyze/src/analyze.rs @@ -387,6 +387,31 @@ fn mark_foreign_fixed<'tcx>( } } +fn mark_all_statics_fixed<'tcx>(gacx: &mut GlobalAnalysisCtxt<'tcx>, gasn: &mut GlobalAssignment) { + for (did, lty) in gacx.static_tys.iter() { + make_ty_fixed(gasn, lty); + + // Also fix the `addr_of_static` permissions. + let ptr = gacx.addr_of_static[&did]; + gasn.flags[ptr].insert(FlagSet::FIXED); + } +} + +fn mark_all_structs_fixed<'tcx>( + gacx: &mut GlobalAnalysisCtxt<'tcx>, + gasn: &mut GlobalAssignment, + tcx: TyCtxt<'tcx>, +) { + for adt_did in &gacx.adt_metadata.struct_dids { + let adt_def = tcx.adt_def(adt_did); + let fields = adt_def.all_fields(); + for field in fields { + let field_lty = gacx.field_ltys[&field.did]; + make_ty_fixed(gasn, field_lty); + } + } +} + fn parse_def_id(s: &str) -> Result { // DefId debug output looks like `DefId(0:1 ~ alias1[0dc4]::{use#0})`. The ` ~ name` part may // be omitted if the name/DefPath info is not available at the point in the compiler where the @@ -546,6 +571,8 @@ fn run(tcx: TyCtxt) { // Load the list of fixed defs early, so any errors are reported immediately. let fixed_defs = get_fixed_defs(tcx).unwrap(); + let rewrite_pointwise = true; + let mut gacx = GlobalAnalysisCtxt::new(tcx); let mut func_info = HashMap::new(); @@ -848,6 +875,13 @@ fn run(tcx: TyCtxt) { mark_foreign_fixed(&mut gacx, &mut gasn, tcx); + if rewrite_pointwise { + // In pointwise mode, we restrict rewriting to a single fn at a time. All statics and + // struct fields are marked `FIXED` so they won't be rewritten. + mark_all_statics_fixed(&mut gacx, &mut gasn); + mark_all_structs_fixed(&mut gacx, &mut gasn, tcx); + } + for (ptr, perms) in gacx.known_fn_ptr_perms() { let existing_perms = &mut gasn.perms[ptr]; existing_perms.remove(PermissionSet::UNIQUE); @@ -1199,7 +1233,6 @@ fn run(tcx: TyCtxt) { } } - let rewrite_pointwise = true; if !rewrite_pointwise { run2( None,