Skip to content

Commit 9ea79fe

Browse files
committed
CFI: Append debug location to CFI blocks
1 parent f460864 commit 9ea79fe

File tree

6 files changed

+48
-0
lines changed

6 files changed

+48
-0
lines changed

compiler/rustc_codegen_gcc/src/debuginfo.rs

+4
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,10 @@ impl<'a, 'gcc, 'tcx> DebugInfoBuilderMethods for Builder<'a, 'gcc, 'tcx> {
5252
fn clear_dbg_loc(&mut self) {
5353
self.location = None;
5454
}
55+
56+
fn get_dbg_loc(&self) -> Option<Self::DILocation> {
57+
self.location
58+
}
5559
}
5660

5761
/// Generate the `debug_context` in an MIR Body.

compiler/rustc_codegen_llvm/src/builder.rs

+7
Original file line numberDiff line numberDiff line change
@@ -1574,6 +1574,7 @@ impl<'a, 'll, 'tcx> Builder<'a, 'll, 'tcx> {
15741574
cfi::typeid_for_fnabi(self.tcx, fn_abi, options)
15751575
};
15761576
let typeid_metadata = self.cx.typeid_metadata(typeid).unwrap();
1577+
let dbg_loc = self.get_dbg_loc();
15771578

15781579
// Test whether the function pointer is associated with the type identifier.
15791580
let cond = self.type_test(llfn, typeid_metadata);
@@ -1582,10 +1583,16 @@ impl<'a, 'll, 'tcx> Builder<'a, 'll, 'tcx> {
15821583
self.cond_br(cond, bb_pass, bb_fail);
15831584

15841585
self.switch_to_block(bb_fail);
1586+
if let Some(dbg_loc) = dbg_loc {
1587+
self.set_dbg_loc(dbg_loc);
1588+
}
15851589
self.abort();
15861590
self.unreachable();
15871591

15881592
self.switch_to_block(bb_pass);
1593+
if let Some(dbg_loc) = dbg_loc {
1594+
self.set_dbg_loc(dbg_loc);
1595+
}
15891596
}
15901597
}
15911598

compiler/rustc_codegen_llvm/src/debuginfo/mod.rs

+4
Original file line numberDiff line numberDiff line change
@@ -206,6 +206,10 @@ impl<'ll> DebugInfoBuilderMethods for Builder<'_, 'll, '_> {
206206
}
207207
}
208208

209+
fn get_dbg_loc(&self) -> Option<&'ll DILocation> {
210+
Some(unsafe { llvm::LLVMGetCurrentDebugLocation2(self.llbuilder) })
211+
}
212+
209213
fn insert_reference_to_gdb_debug_scripts_section_global(&mut self) {
210214
gdb::insert_reference_to_gdb_debug_scripts_section_global(self)
211215
}

compiler/rustc_codegen_llvm/src/llvm/ffi.rs

+1
Original file line numberDiff line numberDiff line change
@@ -1063,6 +1063,7 @@ unsafe extern "C" {
10631063

10641064
// Metadata
10651065
pub fn LLVMSetCurrentDebugLocation2<'a>(Builder: &Builder<'a>, Loc: *const Metadata);
1066+
pub fn LLVMGetCurrentDebugLocation2<'a>(Builder: &Builder<'a>) -> &'a Metadata;
10661067

10671068
// Terminators
10681069
pub fn LLVMBuildRetVoid<'a>(B: &Builder<'a>) -> &'a Value;

compiler/rustc_codegen_ssa/src/traits/debuginfo.rs

+1
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,7 @@ pub trait DebugInfoBuilderMethods: BackendTypes {
8181
);
8282
fn set_dbg_loc(&mut self, dbg_loc: Self::DILocation);
8383
fn clear_dbg_loc(&mut self);
84+
fn get_dbg_loc(&self) -> Option<Self::DILocation>;
8485
fn insert_reference_to_gdb_debug_scripts_section_global(&mut self);
8586
fn set_var_name(&mut self, value: Self::Value, name: &str);
8687
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
// Verifies that the parent block's debug information are assigned to the inserted cfi block.
2+
3+
//
4+
5+
//@ needs-sanitizer-cfi
6+
7+
//@ compile-flags: -Clto -Cno-prepopulate-passes -Copt-level=0 -Zsanitizer=cfi -Ctarget-feature=-crt-static -Cdebuginfo=1
8+
9+
#![crate_type = "lib"]
10+
11+
pub fn foo(f: fn(i32) -> i32, arg: i32) -> i32 {
12+
// CHECK-LABEL: define{{.*}}foo{{.*}}!dbg !{{[0-9]+}} !type !{{[0-9]+}} !type !{{[0-9]+}} !type !{{[0-9]+}} !type !{{[0-9]+}}
13+
14+
// CHECK: start:
15+
16+
// CHECK: [[TT:%.+]] = call i1 @llvm.type.test(ptr {{%f|%0}}, metadata !"{{[[:print:]]+}}"), !dbg !{{[0-9]+}}
17+
18+
// CHECK-NEXT: br i1 [[TT]], label %type_test.pass, label %type_test.fail, !dbg !{{[0-9]+}}
19+
20+
// CHECK: type_test.pass: ; preds = %start
21+
22+
// CHECK-NEXT: {{%.+}} = call i32 %f(i32{{.*}} %arg), !dbg !{{[0-9]+}}
23+
24+
// CHECK: type_test.fail: ; preds = %start
25+
26+
// CHECK-NEXT: call void @llvm.trap(), !dbg !{{[0-9]+}}
27+
28+
// CHECK-NEXT: unreachable, !dbg !{{[0-9]+}}
29+
30+
f(arg)
31+
}

0 commit comments

Comments
 (0)