Skip to content

Commit 91eadab

Browse files
committed
cfi: Store type erasure witness for Argument
CFI/KCFI work by enforcing that indirect function calls are always called at the type they were defined at. The type erasure in Argument works by casting the reference to the value to be formatted to an Opaque and also casting the function to format it to take an Opaque reference. While this is *ABI* safe, which is why we get away with it normally, it does transform the type that the function is called at. This means that at the call-site, CFI expects the type of the function to be `fn(&Opaque, ` even though it is really `fn(&T, ` for some particular `T`. This patch avoids this by adding `cast_stub`, a witness to the type erasure that will cast the `&Opaque` and `fn(&Opaque` back to their original types before invoking the function. This change is guarded by the enablement of CFI as it will require an additional pointer-sized value per `Argument`, and an additional jump during formatting, and we'd prefer not to pay that if we don't need the types to be correct at the indirect call invocation.
1 parent 99322d8 commit 91eadab

File tree

1 file changed

+32
-0
lines changed
  • library/core/src/fmt

1 file changed

+32
-0
lines changed

library/core/src/fmt/rt.rs

+32
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,12 @@ enum ArgumentType<'a> {
7272
// was derived from a `&'a T`.
7373
value: NonNull<()>,
7474
formatter: unsafe fn(NonNull<()>, &mut Formatter<'_>) -> Result,
75+
#[cfg(any(sanitize = "cfi", sanitize = "kcfi"))]
76+
cast_stub: unsafe fn(
77+
unsafe fn(NonNull<()>, &mut Formatter<'_>) -> Result,
78+
NonNull<()>,
79+
f: &mut Formatter<'_>,
80+
) -> Result,
7581
_lifetime: PhantomData<&'a ()>,
7682
},
7783
Count(usize),
@@ -93,6 +99,21 @@ pub struct Argument<'a> {
9399
ty: ArgumentType<'a>,
94100
}
95101

102+
/// This function acts as a witness to an earlier type erasure when constructing an
103+
/// `ArgumentType`. The type parameter `T` should be instantiated to the erased type.
104+
/// SAFETY: This function should be called on a value and formatter which underwent a
105+
/// T->Opaque cast at their definition site.
106+
#[cfg(any(sanitize = "cfi", sanitize = "kcfi"))]
107+
unsafe fn cast_stub<T>(
108+
formatter: unsafe fn(NonNull<()>, &mut Formatter<'_>) -> Result,
109+
value: NonNull<()>,
110+
f: &mut Formatter<'_>,
111+
) -> Result {
112+
let value: &T = mem::transmute(value);
113+
let formatter: fn(&T, &mut Formatter<'_>) -> Result = mem::transmute(formatter);
114+
formatter(value, f)
115+
}
116+
96117
#[rustc_diagnostic_item = "ArgumentMethods"]
97118
impl<'a> Argument<'a> {
98119
#[inline(always)]
@@ -104,6 +125,8 @@ impl<'a> Argument<'a> {
104125
value: NonNull::from(x).cast(),
105126
// SAFETY: function pointers always have the same layout.
106127
formatter: unsafe { mem::transmute(f) },
128+
#[cfg(any(sanitize = "cfi", sanitize = "kcfi"))]
129+
cast_stub: cast_stub::<T>,
107130
_lifetime: PhantomData,
108131
},
109132
}
@@ -163,6 +186,15 @@ impl<'a> Argument<'a> {
163186
#[inline(always)]
164187
pub(super) unsafe fn fmt(&self, f: &mut Formatter<'_>) -> Result {
165188
match self.ty {
189+
#[cfg(any(sanitize = "cfi", sanitize = "kcfi"))]
190+
// SAFETY:
191+
// This is the `cast_stub` that was prepared alongside the
192+
// formatter and value, so it should have the `T` instantiated to
193+
// the erased type that `value` points at.
194+
ArgumentType::Placeholder { formatter, value, cast_stub, .. } => unsafe {
195+
cast_stub(formatter, value, f)
196+
},
197+
#[cfg(not(any(sanitize = "cfi", sanitize = "kcfi")))]
166198
// SAFETY:
167199
// Because of the invariant that if `formatter` had the type
168200
// `fn(&T, _) -> _` then `value` has type `&'b T` where `'b` is

0 commit comments

Comments
 (0)