Skip to content

Commit 1ce0cf0

Browse files
committed
Auto 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 1799d31 + 2567074 commit 1ce0cf0

File tree

3 files changed

+53
-3
lines changed

3 files changed

+53
-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;
@@ -797,12 +798,22 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
797798
argument_scope: region::Scope,
798799
ast_body: &'tcx hir::Expr<'tcx>,
799800
) -> 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+
800805
// Allocate locals for the function arguments
801806
for &ArgInfo(ty, _, arg_opt, _) in arguments.iter() {
802807
let source_info =
803808
SourceInfo::outermost(arg_opt.map_or(self.fn_span, |arg| arg.pat.span));
804809
let arg_local = self.local_decls.push(LocalDecl::with_source_info(ty, source_info));
805810

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+
806817
// If this is a simple binding pattern, give debuginfo a nice name.
807818
if let Some(arg) = arg_opt {
808819
if let Some(ident) = arg.pat.simple_ident() {
@@ -815,7 +826,6 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
815826
}
816827
}
817828

818-
let tcx = self.hir.tcx();
819829
let tcx_hir = tcx.hir();
820830
let hir_typeck_results = self.hir.typeck_results();
821831

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
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
// min-lldb-version: 310
2+
3+
// We have to ignore android because of this issue:
4+
// https://github.com/rust-lang/rust/issues/74847
5+
// ignore-android
6+
7+
// compile-flags:-g
8+
9+
// === GDB TESTS ===================================================================================
10+
11+
// gdb-command:run
12+
13+
// gdb-command:info args
14+
// gdb-check:No arguments.
15+
// gdb-command:continue
16+
17+
// === LLDB TESTS ==================================================================================
18+
19+
// lldb-command:run
20+
21+
// lldb-command:frame variable
22+
// lldbg-check:(unsigned long) = 111 (unsigned long) = 222
23+
// lldbr-check:(unsigned long) = 111 (unsigned long) = 222
24+
// lldb-command:continue
25+
26+
27+
#![feature(naked_functions)]
28+
#![feature(omit_gdb_pretty_printer_section)]
29+
#![omit_gdb_pretty_printer_section]
30+
31+
fn main() {
32+
naked(111, 222);
33+
}
34+
35+
#[naked]
36+
fn naked(x: usize, y: usize) {
37+
zzz(); // #break
38+
}
39+
40+
fn zzz() { () }

0 commit comments

Comments
 (0)