Skip to content

Commit bd79fe7

Browse files
authored
Rollup merge of #132702 - 1c3t3a:issue-132615, r=rcvalle
CFI: Append debug location to CFI blocks Currently we're not appending debug locations to the inserted CFI blocks. This shows up in #132615 and #100783. This change fixes that by passing down the debug location to the CFI type-test generation and appending it to the blocks. Credits also belong to `@jakos-sec` who worked with me on this.
2 parents f7273e0 + c210225 commit bd79fe7

6 files changed

Lines changed: 36 additions & 0 deletions

File tree

‎compiler/rustc_codegen_gcc/src/debuginfo.rs‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,10 @@ impl<'a, 'gcc, 'tcx> DebugInfoBuilderMethods for Builder<'a, 'gcc, 'tcx> {
5252
fnclear_dbg_loc(&mutself){
5353
self.location = None;
5454
}
55+
56+
fnget_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‎

Lines changed: 7 additions & 0 deletions
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+
ifletSome(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+
ifletSome(dbg_loc) = dbg_loc {
1594+
self.set_dbg_loc(dbg_loc);
1595+
}
15891596
}
15901597
}
15911598

‎compiler/rustc_codegen_llvm/src/debuginfo/mod.rs‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -206,6 +206,10 @@ impl<'ll> DebugInfoBuilderMethods for Builder<'_, 'll, '_> {
206206
}
207207
}
208208

209+
fnget_dbg_loc(&self) -> Option<&'llDILocation>{
210+
unsafe{ llvm::LLVMGetCurrentDebugLocation2(self.llbuilder)}
211+
}
212+
209213
fninsert_reference_to_gdb_debug_scripts_section_global(&mutself){
210214
gdb::insert_reference_to_gdb_debug_scripts_section_global(self)
211215
}

‎compiler/rustc_codegen_llvm/src/llvm/ffi.rs‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1063,6 +1063,7 @@ unsafe extern "C" {
10631063

10641064
// Metadata
10651065
pubfnLLVMSetCurrentDebugLocation2<'a>(Builder:&Builder<'a>,Loc:*constMetadata);
1066+
pubfnLLVMGetCurrentDebugLocation2<'a>(Builder:&Builder<'a>) -> Option<&'aMetadata>;
10661067

10671068
// Terminators
10681069
pubfnLLVMBuildRetVoid<'a>(B:&Builder<'a>) -> &'aValue;

‎compiler/rustc_codegen_ssa/src/traits/debuginfo.rs‎

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

0 commit comments

Comments
 (0)