Skip to content

Commit

Permalink
KCFI: Use legal charset in shim encoding
Browse files Browse the repository at this point in the history
To separate `ReifyReason::FnPtr` from `ReifyReason::VTable`, we
hyphenated the shims. Hyphens are not actually legal, but underscores
are, so use those instead.
  • Loading branch information
maurer committed Apr 8, 2024
1 parent ab3dba9 commit 233d94e
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 2 deletions.
4 changes: 2 additions & 2 deletions compiler/rustc_symbol_mangling/src/v0.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,8 @@ pub(super) fn mangle<'tcx>(
ty::InstanceDef::ThreadLocalShim(_) => Some("tls"),
ty::InstanceDef::VTableShim(_) => Some("vtable"),
ty::InstanceDef::ReifyShim(_, None) => Some("reify"),
ty::InstanceDef::ReifyShim(_, Some(ReifyReason::FnPtr)) => Some("reify-fnptr"),
ty::InstanceDef::ReifyShim(_, Some(ReifyReason::Vtable)) => Some("reify-vtable"),
ty::InstanceDef::ReifyShim(_, Some(ReifyReason::FnPtr)) => Some("reify_fnptr"),
ty::InstanceDef::ReifyShim(_, Some(ReifyReason::Vtable)) => Some("reify_vtable"),

ty::InstanceDef::ConstructCoroutineInClosureShim { .. }
| ty::InstanceDef::CoroutineKindShim { .. } => Some("fn_once"),
Expand Down
30 changes: 30 additions & 0 deletions tests/ui/sanitizer/kcfi-mangling.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
// Check KCFI extra mangling works correctly on v0

//@ needs-sanitizer-kcfi
//@ no-prefer-dynamic
//@ compile-flags: -C panic=abort -Zsanitizer=kcfi -C symbol-mangling-version=v0
//@ build-pass

trait Foo {
fn foo(&self);
}

struct Bar;
impl Foo for Bar {
fn foo(&self) {}
}

struct Baz;
impl Foo for Baz {
#[track_caller]
fn foo(&self) {}
}

fn main() {
// Produces `ReifyShim(_, ReifyReason::FnPtr)`
let f: fn(&Bar) = Bar::foo;
f(&Bar);
// Produces `ReifyShim(_, ReifyReason::Vtable)`
let v: &dyn Foo = &Baz as _;
v.foo();
}

0 comments on commit 233d94e

Please sign in to comment.