Skip to content

Commit

Permalink
Rollup merge of rust-lang#75417 - npmccallum:naked, r=matthewjasper
Browse files Browse the repository at this point in the history
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
  • Loading branch information
tmandry authored Aug 14, 2020
2 parents 8c361aa + 050fb38 commit 1f73e89
Show file tree
Hide file tree
Showing 4 changed files with 11 additions and 55 deletions.
8 changes: 8 additions & 0 deletions src/librustc_codegen_ssa/mir/debuginfo.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use crate::traits::*;
use rustc_hir::def_id::CrateNum;
use rustc_index::vec::IndexVec;
use rustc_middle::middle::codegen_fn_attrs::CodegenFnAttrFlags;
use rustc_middle::mir;
use rustc_middle::ty;
use rustc_session::config::DebugInfo;
Expand Down Expand Up @@ -216,6 +217,13 @@ impl<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>> FunctionCx<'a, 'tcx, Bx> {
LocalRef::Operand(None) => return,

LocalRef::Operand(Some(operand)) => {
// Don't spill operands onto the stack in naked functions.
// See: https://github.com/rust-lang/rust/issues/42779
let attrs = bx.tcx().codegen_fn_attrs(self.instance.def_id());
if attrs.flags.contains(CodegenFnAttrFlags::NAKED) {
return;
}

// "Spill" the value onto the stack, for debuginfo,
// without forcing non-debuginfo uses of the local
// to also load from the stack every single time.
Expand Down
12 changes: 1 addition & 11 deletions src/librustc_mir_build/build/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ use rustc_hir::lang_items;
use rustc_hir::{GeneratorKind, HirIdMap, Node};
use rustc_index::vec::{Idx, IndexVec};
use rustc_infer::infer::TyCtxtInferExt;
use rustc_middle::middle::codegen_fn_attrs::CodegenFnAttrFlags;
use rustc_middle::middle::region;
use rustc_middle::mir::*;
use rustc_middle::ty::subst::Subst;
Expand Down Expand Up @@ -798,22 +797,12 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
argument_scope: region::Scope,
ast_body: &'tcx hir::Expr<'tcx>,
) -> BlockAnd<()> {
let tcx = self.hir.tcx();
let attrs = tcx.codegen_fn_attrs(fn_def_id);
let naked = attrs.flags.contains(CodegenFnAttrFlags::NAKED);

// Allocate locals for the function arguments
for &ArgInfo(ty, _, arg_opt, _) in arguments.iter() {
let source_info =
SourceInfo::outermost(arg_opt.map_or(self.fn_span, |arg| arg.pat.span));
let arg_local = self.local_decls.push(LocalDecl::with_source_info(ty, source_info));

// Emit function argument debuginfo only for non-naked functions.
// See: https://github.com/rust-lang/rust/issues/42779
if naked {
continue;
}

// If this is a simple binding pattern, give debuginfo a nice name.
if let Some(arg) = arg_opt {
if let Some(ident) = arg.pat.simple_ident() {
Expand All @@ -826,6 +815,7 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
}
}

let tcx = self.hir.tcx();
let tcx_hir = tcx.hir();
let hir_typeck_results = self.hir.typeck_results();

Expand Down
4 changes: 2 additions & 2 deletions src/test/codegen/naked-functions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ pub fn naked_empty() {
// CHECK-NEXT: define void @naked_with_args(i{{[0-9]+( %0)?}})
pub fn naked_with_args(a: isize) {
// CHECK-NEXT: {{.+}}:
// CHECK-NEXT: %_1 = alloca i{{[0-9]+}}
// CHECK-NEXT: %a = alloca i{{[0-9]+}}
&a; // keep variable in an alloca
// CHECK: ret void
}
Expand All @@ -39,7 +39,7 @@ pub fn naked_with_return() -> isize {
#[naked]
pub fn naked_with_args_and_return(a: isize) -> isize {
// CHECK-NEXT: {{.+}}:
// CHECK-NEXT: %_1 = alloca i{{[0-9]+}}
// CHECK-NEXT: %a = alloca i{{[0-9]+}}
&a; // keep variable in an alloca
// CHECK: ret i{{[0-9]+}} %{{[0-9]+}}
a
Expand Down
42 changes: 0 additions & 42 deletions src/test/debuginfo/function-arguments-naked.rs

This file was deleted.

0 comments on commit 1f73e89

Please sign in to comment.