Skip to content

Commit 1f73e89

Browse files
authored
Rollup merge of rust-lang#75417 - npmccallum:naked, r=matthewjasper
Don't spill operands onto the stack in naked functions Currently, the code spills operands onto the stack for the purpose of debuginfo. However, naked functions can only contain an asm block. Therefore, attempting to spill the operands on the stack is undefined behavior. Fixes rust-lang#42779 cc rust-lang#32408 Note that this PR reverts rust-lang#74105 which ultimately didn't fix the problem. cc @haraldh @Amanieu @matthewjasper
2 parents 8c361aa + 050fb38 commit 1f73e89

File tree

4 files changed

+11
-55
lines changed

4 files changed

+11
-55
lines changed

src/librustc_codegen_ssa/mir/debuginfo.rs

+8
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
use crate::traits::*;
22
use rustc_hir::def_id::CrateNum;
33
use rustc_index::vec::IndexVec;
4+
use rustc_middle::middle::codegen_fn_attrs::CodegenFnAttrFlags;
45
use rustc_middle::mir;
56
use rustc_middle::ty;
67
use rustc_session::config::DebugInfo;
@@ -216,6 +217,13 @@ impl<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>> FunctionCx<'a, 'tcx, Bx> {
216217
LocalRef::Operand(None) => return,
217218

218219
LocalRef::Operand(Some(operand)) => {
220+
// Don't spill operands onto the stack in naked functions.
221+
// See: https://github.com/rust-lang/rust/issues/42779
222+
let attrs = bx.tcx().codegen_fn_attrs(self.instance.def_id());
223+
if attrs.flags.contains(CodegenFnAttrFlags::NAKED) {
224+
return;
225+
}
226+
219227
// "Spill" the value onto the stack, for debuginfo,
220228
// without forcing non-debuginfo uses of the local
221229
// to also load from the stack every single time.

src/librustc_mir_build/build/mod.rs

+1-11
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ use rustc_hir::lang_items;
1010
use rustc_hir::{GeneratorKind, HirIdMap, Node};
1111
use rustc_index::vec::{Idx, IndexVec};
1212
use rustc_infer::infer::TyCtxtInferExt;
13-
use rustc_middle::middle::codegen_fn_attrs::CodegenFnAttrFlags;
1413
use rustc_middle::middle::region;
1514
use rustc_middle::mir::*;
1615
use rustc_middle::ty::subst::Subst;
@@ -798,22 +797,12 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
798797
argument_scope: region::Scope,
799798
ast_body: &'tcx hir::Expr<'tcx>,
800799
) -> BlockAnd<()> {
801-
let tcx = self.hir.tcx();
802-
let attrs = tcx.codegen_fn_attrs(fn_def_id);
803-
let naked = attrs.flags.contains(CodegenFnAttrFlags::NAKED);
804-
805800
// Allocate locals for the function arguments
806801
for &ArgInfo(ty, _, arg_opt, _) in arguments.iter() {
807802
let source_info =
808803
SourceInfo::outermost(arg_opt.map_or(self.fn_span, |arg| arg.pat.span));
809804
let arg_local = self.local_decls.push(LocalDecl::with_source_info(ty, source_info));
810805

811-
// Emit function argument debuginfo only for non-naked functions.
812-
// See: https://github.com/rust-lang/rust/issues/42779
813-
if naked {
814-
continue;
815-
}
816-
817806
// If this is a simple binding pattern, give debuginfo a nice name.
818807
if let Some(arg) = arg_opt {
819808
if let Some(ident) = arg.pat.simple_ident() {
@@ -826,6 +815,7 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
826815
}
827816
}
828817

818+
let tcx = self.hir.tcx();
829819
let tcx_hir = tcx.hir();
830820
let hir_typeck_results = self.hir.typeck_results();
831821

src/test/codegen/naked-functions.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ pub fn naked_empty() {
1818
// CHECK-NEXT: define void @naked_with_args(i{{[0-9]+( %0)?}})
1919
pub fn naked_with_args(a: isize) {
2020
// CHECK-NEXT: {{.+}}:
21-
// CHECK-NEXT: %_1 = alloca i{{[0-9]+}}
21+
// CHECK-NEXT: %a = alloca i{{[0-9]+}}
2222
&a; // keep variable in an alloca
2323
// CHECK: ret void
2424
}
@@ -39,7 +39,7 @@ pub fn naked_with_return() -> isize {
3939
#[naked]
4040
pub fn naked_with_args_and_return(a: isize) -> isize {
4141
// CHECK-NEXT: {{.+}}:
42-
// CHECK-NEXT: %_1 = alloca i{{[0-9]+}}
42+
// CHECK-NEXT: %a = alloca i{{[0-9]+}}
4343
&a; // keep variable in an alloca
4444
// CHECK: ret i{{[0-9]+}} %{{[0-9]+}}
4545
a

src/test/debuginfo/function-arguments-naked.rs

-42
This file was deleted.

0 commit comments

Comments
 (0)