Skip to content

Commit 1d81d8e

Browse files
committed
CFI: Enable vtable shimming
1 parent 7fd0f60 commit 1d81d8e

File tree

3 files changed

+26
-4
lines changed

3 files changed

+26
-4
lines changed

compiler/rustc_middle/src/ty/vtable.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ pub(super) fn vtable_allocation_provider<'tcx>(
9494
VtblEntry::Vacant => continue,
9595
VtblEntry::Method(instance) => {
9696
// Prepare the fn ptr we write into the vtable.
97-
let instance = instance.polymorphize(tcx);
97+
let instance = instance.cfi_shim(tcx, invoke_trait).polymorphize(tcx);
9898
let fn_alloc_id = tcx.reserve_and_set_fn_alloc(instance);
9999
let fn_ptr = Pointer::from(fn_alloc_id);
100100
Scalar::from_pointer(fn_ptr, &tcx)

compiler/rustc_monomorphize/src/collector.rs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1226,9 +1226,8 @@ fn create_mono_items_for_vtable_methods<'tcx>(
12261226
// all super trait items already covered, so skip them.
12271227
None
12281228
}
1229-
VtblEntry::Method(instance) => {
1230-
Some(*instance).filter(|instance| should_codegen_locally(tcx, instance))
1231-
}
1229+
VtblEntry::Method(instance) => Some(instance.cfi_shim(tcx, invoke_trait))
1230+
.filter(|instance| should_codegen_locally(tcx, instance)),
12321231
})
12331232
.map(|item| create_fn_mono_item(tcx, item, source));
12341233
output.extend(methods);
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
// Check alternate receivers work
2+
3+
//@ needs-sanitizer-cfi
4+
//@ compile-flags: --crate-type=bin -Cprefer-dynamic=off -Clto -Zsanitizer=cfi
5+
//@ compile-flags: -C codegen-units=1 -C opt-level=0
6+
//@ run-pass
7+
8+
use std::sync::Arc;
9+
10+
trait Fooable {
11+
fn foo(self: Arc<Self>);
12+
}
13+
14+
struct Bar;
15+
16+
impl Fooable for Bar {
17+
fn foo(self: Arc<Self>) {}
18+
}
19+
20+
fn main() {
21+
let bar: Arc<dyn Fooable> = Arc::new(Bar);
22+
bar.foo();
23+
}

0 commit comments

Comments
 (0)