From c8ae02fb842fcd4b2f3a75e279bd61c141552c55 Mon Sep 17 00:00:00 2001 From: Michael Goulet Date: Fri, 16 Aug 2024 21:33:44 -0400 Subject: [PATCH] CFI: Erase regions when projecting ADT to its transparent non-1zst field --- .../cfi/typeid/itanium_cxx_abi/transform.rs | 2 +- .../sanitizer/cfi/transparent-has-regions.rs | 18 ++++++++++++++++++ 2 files changed, 19 insertions(+), 1 deletion(-) create mode 100644 tests/ui/sanitizer/cfi/transparent-has-regions.rs diff --git a/compiler/rustc_sanitizers/src/cfi/typeid/itanium_cxx_abi/transform.rs b/compiler/rustc_sanitizers/src/cfi/typeid/itanium_cxx_abi/transform.rs index e628c17aca3..dcbd6a11e87 100644 --- a/compiler/rustc_sanitizers/src/cfi/typeid/itanium_cxx_abi/transform.rs +++ b/compiler/rustc_sanitizers/src/cfi/typeid/itanium_cxx_abi/transform.rs @@ -146,7 +146,7 @@ impl<'tcx> TypeFolder> for TransformTy<'tcx> { !is_zst }); if let Some(field) = field { - let ty0 = self.tcx.type_of(field.did).instantiate(self.tcx, args); + let ty0 = self.tcx.erase_regions(field.ty(self.tcx, args)); // Generalize any repr(transparent) user-defined type that is either a // pointer or reference, and either references itself or any other type that // contains or references itself, to avoid a reference cycle. diff --git a/tests/ui/sanitizer/cfi/transparent-has-regions.rs b/tests/ui/sanitizer/cfi/transparent-has-regions.rs new file mode 100644 index 00000000000..b70e1ea1791 --- /dev/null +++ b/tests/ui/sanitizer/cfi/transparent-has-regions.rs @@ -0,0 +1,18 @@ +//@ needs-sanitizer-cfi +//@ compile-flags: -Ccodegen-units=1 -Clto -Ctarget-feature=-crt-static -Zsanitizer=cfi +//@ no-prefer-dynamic +//@ only-x86_64-unknown-linux-gnu +//@ build-pass + +pub trait Trait {} + +impl Trait for i32 {} + +#[repr(transparent)] +struct BoxedTrait(Box); + +fn hello(x: BoxedTrait) {} + +fn main() { + hello(BoxedTrait(Box::new(1))); +}