Skip to content

Commit 6f4dc71

Browse files
committed
CFI: Handle dyn with no principal
In user-facing Rust, `dyn` always has at least one predicate following it. Unfortunately, because we filter out marker traits from receivers at callsites and `dyn Sync` is, for example, legal, this results in us having `dyn` types with no predicates on occasion in our alias set encoding. This patch handles cases where there are no predicates in a `dyn` type which are relevant to its alias set. Fixes #122998
1 parent 6a92312 commit 6f4dc71

File tree

2 files changed

+36
-8
lines changed

2 files changed

+36
-8
lines changed

Diff for: compiler/rustc_symbol_mangling/src/typeid/typeid_itanium_cxx_abi.rs

+15-8
Original file line numberDiff line numberDiff line change
@@ -760,7 +760,11 @@ fn transform_predicates<'tcx>(
760760
ty::ExistentialPredicate::AutoTrait(..) => Some(predicate),
761761
})
762762
.collect();
763-
tcx.mk_poly_existential_predicates(&predicates)
763+
if predicates.len() == 0 {
764+
List::empty()
765+
} else {
766+
tcx.mk_poly_existential_predicates(&predicates)
767+
}
764768
}
765769

766770
/// Transforms args for being encoded and used in the substitution dictionary.
@@ -1171,14 +1175,17 @@ fn strip_receiver_auto<'tcx>(
11711175
let ty::Dynamic(preds, lifetime, kind) = ty.kind() else {
11721176
bug!("Tried to strip auto traits from non-dynamic type {ty}");
11731177
};
1174-
let filtered_preds =
1175-
if preds.principal().is_some() {
1178+
let new_rcvr = if preds.principal().is_some() {
1179+
let filtered_preds =
11761180
tcx.mk_poly_existential_predicates_from_iter(preds.into_iter().filter(|pred| {
11771181
!matches!(pred.skip_binder(), ty::ExistentialPredicate::AutoTrait(..))
1178-
}))
1179-
} else {
1180-
ty::List::empty()
1181-
};
1182-
let new_rcvr = Ty::new_dynamic(tcx, filtered_preds, *lifetime, *kind);
1182+
}));
1183+
Ty::new_dynamic(tcx, filtered_preds, *lifetime, *kind)
1184+
} else {
1185+
// If there's no principal type, re-encode it as a unit, since we don't know anything
1186+
// about it. This technically discards the knowledge that it was a type that was made
1187+
// into a trait object at some point, but that's not a lot.
1188+
tcx.types.unit
1189+
};
11831190
tcx.mk_args_trait(new_rcvr, args.into_iter().skip(1))
11841191
}

Diff for: tests/ui/sanitizer/cfi-drop-no-principal.rs

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
// Check that dropping a trait object without a principal trait succeeds
2+
3+
//@ needs-sanitizer-cfi
4+
// FIXME(#122848) Remove only-linux once OSX CFI binaries works
5+
//@ only-linux
6+
//@ compile-flags: --crate-type=bin -Cprefer-dynamic=off -Clto -Zsanitizer=cfi
7+
//@ compile-flags: -C target-feature=-crt-static -C codegen-units=1 -C opt-level=0
8+
// FIXME(#118761) Should be run-pass once the labels on drop are compatible.
9+
// This test is being landed ahead of that to test that the compiler doesn't ICE while labeling the
10+
// callsite for a drop, but the vtable doesn't have the correct label yet.
11+
//@ build-pass
12+
13+
struct CustomDrop;
14+
15+
impl Drop for CustomDrop {
16+
fn drop(&mut self) {}
17+
}
18+
19+
fn main() {
20+
let _ = Box::new(CustomDrop) as Box<dyn Send>;
21+
}

0 commit comments

Comments
 (0)