Skip to content

Commit

Permalink
Rollup merge of rust-lang#74105 - npmccallum:naked, r=matthewjasper
Browse files Browse the repository at this point in the history
Suppress debuginfo on naked function arguments

A function that has no prologue cannot be reasonably expected to support
debuginfo. In fact, the existing code (before this patch) would generate
invalid instructions that caused crashes. We can solve this easily by
just not emitting the debuginfo in this case.

Fixes rust-lang#42779
cc rust-lang#32408
  • Loading branch information
Manishearth authored Jul 8, 2020
2 parents d53ba34 + 6b59cac commit 201ed98
Showing 3 changed files with 30 additions and 3 deletions.
12 changes: 11 additions & 1 deletion src/librustc_mir_build/build/mod.rs
Original file line number Diff line number Diff line change
@@ -10,6 +10,7 @@ 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;
@@ -790,12 +791,22 @@ 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() {
@@ -808,7 +819,6 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
}
}

let tcx = self.hir.tcx();
let tcx_hir = tcx.hir();
let hir_tables = self.hir.tables();

4 changes: 2 additions & 2 deletions src/test/codegen/naked-functions.rs
Original file line number Diff line number Diff line change
@@ -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: %a = alloca i{{[0-9]+}}
// CHECK-NEXT: %_1 = alloca i{{[0-9]+}}
&a; // keep variable in an alloca
// CHECK: ret void
}
@@ -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: %a = alloca i{{[0-9]+}}
// CHECK-NEXT: %_1 = alloca i{{[0-9]+}}
&a; // keep variable in an alloca
// CHECK: ret i{{[0-9]+}} %{{[0-9]+}}
a
17 changes: 17 additions & 0 deletions src/test/debuginfo/function-arguments.rs
Original file line number Diff line number Diff line change
@@ -18,6 +18,10 @@
// gdb-check:$4 = 3000
// gdb-command:continue

// gdb-command:info args
// gdb-check:No arguments.
// gdb-command:continue

// === LLDB TESTS ==================================================================================

// lldb-command:run
@@ -38,14 +42,20 @@
// lldbr-check:(i64) b = 3000
// lldb-command:continue

// lldb-command:frame variable
// lldbg-check:(unsigned long) = 111 (unsigned long) = 222
// lldbr-check:(unsigned long) = 111 (unsigned long) = 222
// lldb-command:continue

#![feature(naked_functions)]
#![feature(omit_gdb_pretty_printer_section)]
#![omit_gdb_pretty_printer_section]

fn main() {

fun(111102, true);
nested(2000, 3000);
naked(111, 222);

fn nested(a: i32, b: i64) -> (i32, i64) {
zzz(); // #break
@@ -59,4 +69,11 @@ fn fun(x: isize, y: bool) -> (isize, bool) {
(x, y)
}

#[naked]
fn naked(x: usize, y: usize) -> usize {
zzz(); // #break

x + y
}

fn zzz() { () }

0 comments on commit 201ed98

Please sign in to comment.