Skip to content

Commit 2396109

Browse files
authored
Rollup merge of #143293 - folkertdev:naked-function-kcfi, r=compiler-errors
fix `-Zsanitizer=kcfi` on `#[naked]` functions fixes rust-lang/rust#143266 With `-Zsanitizer=kcfi`, indirect calls happen via generated intermediate shim that forwards the call. The generated shim preserves the attributes of the original, including `#[unsafe(naked)]`. The shim is not a naked function though, and violates its invariants (like having a body that consists of a single `naked_asm!` call). My fix here is to match on the `InstanceKind`, and only use `codegen_naked_asm` when the instance is not a `ReifyShim`. That does beg the question whether there are other `InstanceKind`s that could come up. As far as I can tell the answer is no: calling via `dyn` seems to work find, and `#[track_caller]` is disallowed in combination with `#[naked]`. r? codegen ````@rustbot```` label +A-naked cc ````@maurer```` ````@rcvalle````
2 parents 7ff6135 + e55baf9 commit 2396109

File tree

3 files changed

+3
-3
lines changed

3 files changed

+3
-3
lines changed

src/attributes.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ pub fn from_fn_attrs<'gcc, 'tcx>(
8787
#[cfg_attr(not(feature = "master"), allow(unused_variables))] func: Function<'gcc>,
8888
instance: ty::Instance<'tcx>,
8989
) {
90-
let codegen_fn_attrs = cx.tcx.codegen_fn_attrs(instance.def_id());
90+
let codegen_fn_attrs = cx.tcx.codegen_instance_attrs(instance.def);
9191

9292
#[cfg(feature = "master")]
9393
{

src/callee.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,7 @@ pub fn get_fn<'gcc, 'tcx>(cx: &CodegenCx<'gcc, 'tcx>, instance: Instance<'tcx>)
105105
let is_hidden = if is_generic {
106106
// This is a monomorphization of a generic function.
107107
if !(cx.tcx.sess.opts.share_generics()
108-
|| tcx.codegen_fn_attrs(instance_def_id).inline
108+
|| tcx.codegen_instance_attrs(instance.def).inline
109109
== rustc_attr_data_structures::InlineAttr::Never)
110110
{
111111
// When not sharing generics, all instances are in the same

src/mono_item.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ impl<'gcc, 'tcx> PreDefineCodegenMethods<'tcx> for CodegenCx<'gcc, 'tcx> {
5353
let fn_abi = self.fn_abi_of_instance(instance, ty::List::empty());
5454
self.linkage.set(base::linkage_to_gcc(linkage));
5555
let decl = self.declare_fn(symbol_name, fn_abi);
56-
//let attrs = self.tcx.codegen_fn_attrs(instance.def_id());
56+
//let attrs = self.tcx.codegen_instance_attrs(instance.def);
5757

5858
attributes::from_fn_attrs(self, decl, instance);
5959

0 commit comments

Comments
 (0)