Skip to content

Commit 201ed98

Browse files
authored
Rollup merge of rust-lang#74105 - npmccallum:naked, r=matthewjasper
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
2 parents d53ba34 + 6b59cac commit 201ed98

File tree

3 files changed

+30
-3
lines changed

3 files changed

+30
-3
lines changed

src/librustc_mir_build/build/mod.rs

+11-1
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ 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;
1314
use rustc_middle::middle::region;
1415
use rustc_middle::mir::*;
1516
use rustc_middle::ty::subst::Subst;
@@ -790,12 +791,22 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
790791
argument_scope: region::Scope,
791792
ast_body: &'tcx hir::Expr<'tcx>,
792793
) -> BlockAnd<()> {
794+
let tcx = self.hir.tcx();
795+
let attrs = tcx.codegen_fn_attrs(fn_def_id);
796+
let naked = attrs.flags.contains(CodegenFnAttrFlags::NAKED);
797+
793798
// Allocate locals for the function arguments
794799
for &ArgInfo(ty, _, arg_opt, _) in arguments.iter() {
795800
let source_info =
796801
SourceInfo::outermost(arg_opt.map_or(self.fn_span, |arg| arg.pat.span));
797802
let arg_local = self.local_decls.push(LocalDecl::with_source_info(ty, source_info));
798803

804+
// Emit function argument debuginfo only for non-naked functions.
805+
// See: https://github.com/rust-lang/rust/issues/42779
806+
if naked {
807+
continue;
808+
}
809+
799810
// If this is a simple binding pattern, give debuginfo a nice name.
800811
if let Some(arg) = arg_opt {
801812
if let Some(ident) = arg.pat.simple_ident() {
@@ -808,7 +819,6 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
808819
}
809820
}
810821

811-
let tcx = self.hir.tcx();
812822
let tcx_hir = tcx.hir();
813823
let hir_tables = self.hir.tables();
814824

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: %a = alloca i{{[0-9]+}}
21+
// CHECK-NEXT: %_1 = 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: %a = alloca i{{[0-9]+}}
42+
// CHECK-NEXT: %_1 = 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.rs

+17
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,10 @@
1818
// gdb-check:$4 = 3000
1919
// gdb-command:continue
2020

21+
// gdb-command:info args
22+
// gdb-check:No arguments.
23+
// gdb-command:continue
24+
2125
// === LLDB TESTS ==================================================================================
2226

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

45+
// lldb-command:frame variable
46+
// lldbg-check:(unsigned long) = 111 (unsigned long) = 222
47+
// lldbr-check:(unsigned long) = 111 (unsigned long) = 222
48+
// lldb-command:continue
4149

50+
#![feature(naked_functions)]
4251
#![feature(omit_gdb_pretty_printer_section)]
4352
#![omit_gdb_pretty_printer_section]
4453

4554
fn main() {
4655

4756
fun(111102, true);
4857
nested(2000, 3000);
58+
naked(111, 222);
4959

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

72+
#[naked]
73+
fn naked(x: usize, y: usize) -> usize {
74+
zzz(); // #break
75+
76+
x + y
77+
}
78+
6279
fn zzz() { () }

0 commit comments

Comments
 (0)