Skip to content

Commit c6ecbb9

Browse files
authored
Unrolled build for #143293
Rollup merge of #143293 - folkertdev:naked-function-kcfi, r=compiler-errors fix `-Zsanitizer=kcfi` on `#[naked]` functions fixes #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 1aa5b22 + 9c8ab89 commit c6ecbb9

File tree

22 files changed

+147
-36
lines changed

22 files changed

+147
-36
lines changed

compiler/rustc_codegen_cranelift/src/driver/aot.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -530,8 +530,8 @@ fn codegen_cgu_content(
530530
for (mono_item, item_data) in mono_items {
531531
match mono_item {
532532
MonoItem::Fn(instance) => {
533-
if tcx.codegen_fn_attrs(instance.def_id()).flags.contains(CodegenFnAttrFlags::NAKED)
534-
{
533+
let flags = tcx.codegen_instance_attrs(instance.def).flags;
534+
if flags.contains(CodegenFnAttrFlags::NAKED) {
535535
rustc_codegen_ssa::mir::naked_asm::codegen_naked_asm(
536536
&mut GlobalAsmContext { tcx, global_asm: &mut cx.global_asm },
537537
instance,

compiler/rustc_codegen_cranelift/src/driver/jit.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,7 @@ fn codegen_and_compile_fn<'tcx>(
127127
module: &mut dyn Module,
128128
instance: Instance<'tcx>,
129129
) {
130-
if tcx.codegen_fn_attrs(instance.def_id()).flags.contains(CodegenFnAttrFlags::NAKED) {
130+
if tcx.codegen_instance_attrs(instance.def).flags.contains(CodegenFnAttrFlags::NAKED) {
131131
tcx.dcx()
132132
.span_fatal(tcx.def_span(instance.def_id()), "Naked asm is not supported in JIT mode");
133133
}

compiler/rustc_codegen_cranelift/src/driver/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ fn predefine_mono_items<'tcx>(
3535
is_compiler_builtins,
3636
);
3737
let is_naked = tcx
38-
.codegen_fn_attrs(instance.def_id())
38+
.codegen_instance_attrs(instance.def)
3939
.flags
4040
.contains(CodegenFnAttrFlags::NAKED);
4141
module

compiler/rustc_codegen_gcc/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
{

compiler/rustc_codegen_gcc/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

compiler/rustc_codegen_gcc/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

compiler/rustc_codegen_llvm/src/attributes.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -344,7 +344,7 @@ pub(crate) fn llfn_attrs_from_instance<'ll, 'tcx>(
344344
llfn: &'ll Value,
345345
instance: ty::Instance<'tcx>,
346346
) {
347-
let codegen_fn_attrs = cx.tcx.codegen_fn_attrs(instance.def_id());
347+
let codegen_fn_attrs = cx.tcx.codegen_instance_attrs(instance.def);
348348

349349
let mut to_add = SmallVec::<[_; 16]>::new();
350350

compiler/rustc_codegen_llvm/src/callee.rs

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

compiler/rustc_codegen_llvm/src/mono_item.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,8 +55,8 @@ impl<'tcx> PreDefineCodegenMethods<'tcx> for CodegenCx<'_, 'tcx> {
5555
let fn_abi = self.fn_abi_of_instance(instance, ty::List::empty());
5656
let lldecl = self.declare_fn(symbol_name, fn_abi, Some(instance));
5757
llvm::set_linkage(lldecl, base::linkage_to_llvm(linkage));
58-
let attrs = self.tcx.codegen_fn_attrs(instance.def_id());
59-
base::set_link_section(lldecl, attrs);
58+
let attrs = self.tcx.codegen_instance_attrs(instance.def);
59+
base::set_link_section(lldecl, &attrs);
6060
if (linkage == Linkage::LinkOnceODR || linkage == Linkage::WeakODR)
6161
&& self.tcx.sess.target.supports_comdat()
6262
{

compiler/rustc_codegen_ssa/src/mir/debuginfo.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -356,7 +356,7 @@ impl<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>> FunctionCx<'a, 'tcx, Bx> {
356356
LocalRef::Operand(operand) => {
357357
// Don't spill operands onto the stack in naked functions.
358358
// See: https://github.com/rust-lang/rust/issues/42779
359-
let attrs = bx.tcx().codegen_fn_attrs(self.instance.def_id());
359+
let attrs = bx.tcx().codegen_instance_attrs(self.instance.def);
360360
if attrs.flags.contains(CodegenFnAttrFlags::NAKED) {
361361
return;
362362
}

0 commit comments

Comments
 (0)