Skip to content

Commit 1e4cec9

Browse files
Merge pull request #79524 from rastogishubham/FixASANBug
When emitting an LValue of a LoadExpr during SILGen, the Expression passed as a SILLocation for the LValue was the parent LoadExpr instead of the SubExpr that contains the LValue for the load. This leads to the SILLocation to be marked as implicit, because a LoadExpr is always an implicit expression. An implicit SILLocation is not emitted when doing IRGen as it is considered to be hidden from debug info and thus we lose the debug location for the LValue of a LoadExpr. This patch fixes this issue and thus, preserves the location information.
2 parents e83f731 + 52a5ed8 commit 1e4cec9

File tree

2 files changed

+28
-13
lines changed

2 files changed

+28
-13
lines changed

lib/SILGen/SILGenExpr.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1094,7 +1094,8 @@ RValue RValueEmitter::visitLoadExpr(LoadExpr *E, SGFContext C) {
10941094
// We can't load at immediate +0 from the lvalue without deeper analysis,
10951095
// since the access will be immediately ended and might invalidate the value
10961096
// we loaded.
1097-
return SGF.emitLoadOfLValue(E, std::move(lv), C.withFollowingSideEffects());
1097+
return SGF.emitLoadOfLValue(E->getSubExpr(), std::move(lv),
1098+
C.withFollowingSideEffects());
10981099
}
10991100

11001101
SILValue SILGenFunction::emitTemporaryAllocation(SILLocation loc, SILType ty,

test/DebugInfo/shadowcopy-linetable.swift

Lines changed: 26 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,35 @@
1-
// RUN: %target-swift-frontend %s -emit-ir -g -o - | %FileCheck %s
2-
1+
// RUN: %target-swift-frontend %s -emit-sil -Xllvm -sil-print-debuginfo -Xllvm -sil-print-debuginfo-verbose -g -o - | %FileCheck %s --check-prefix=SILCHECK
2+
// RUN: %target-swift-frontend %s -emit-ir -g -o - | %FileCheck %s --check-prefix=IRCHECK
33
func markUsed<T>(_ t: T) {}
44

55
func foo(_ x: inout Int64) {
6+
7+
// Make sure that the begin_access, load, and end_access instructions
8+
// are not marked as isImplicit: true. They should not be implicit
9+
// because they are generated from a member_ref_expr which is the
10+
// lvalue SubExpr of a LoadExpr. LoadExpr's are always implicit, but
11+
// that doesn't necessarily mean their SubExprs are implicit as well.
12+
// SILCHECK: sil hidden @$s4main3fooyys5Int64VzF
13+
// SILCHECK: debug_value %0, var, name "x", argno 1, expr op_deref, loc {{.*}} isImplicit: false, isAutoGenerated: false, isHiddenFromDebugInfo: false, scope 7 // id: %1
14+
// SILCHECK: begin_access [read] [static] %0, loc {{.*}} isImplicit: false, isAutoGenerated: false, isHiddenFromDebugInfo: false
15+
// SILCHECK-NEXT: load %2, loc {{.*}} isImplicit: false, isAutoGenerated: false, isHiddenFromDebugInfo: false
16+
// SILCHECK-NEXT: end_access %2, loc * {{.*}} isImplicit: false, isAutoGenerated: true, isHiddenFromDebugInfo: true
17+
618
// Make sure the shadow copy is being made in the prologue or (at
719
// line 0), but the code to load the value from the inout storage is
820
// not.
9-
// CHECK: define hidden swiftcc void @"$s4main3fooyys5Int64VzF"
10-
// CHECK: %[[X:.*]] = alloca ptr, align {{(4|8)}}
11-
// CHECK-NEXT: #dbg_declare
12-
// CHECK-NEXT: call void @llvm.memset.{{.*}}(ptr align {{(4|8)}} %[[X]], i8 0
13-
// CHECK: store ptr %0, ptr %[[X]], align {{(4|8)}}
14-
// CHECK-SAME: !dbg ![[LOC0:.*]]
15-
// CHECK-NEXT: getelementptr inbounds %Ts5Int64V, ptr %0, i32 0, i32 0,
16-
// CHECK-SAME: !dbg ![[LOC0]]
17-
// CHECK: ![[LOC0]] = !DILocation(line: 0,
18-
// CHECK: !DILocation(line: [[@LINE+1]],
21+
// IRCHECK: define hidden swiftcc void @"$s4main3fooyys5Int64VzF"
22+
// IRCHECK: %[[X:.*]] = alloca ptr, align {{(4|8)}}
23+
// IRCHECK-NEXT: #dbg_declare
24+
// IRCHECK-NEXT: call void @llvm.memset.{{.*}}(ptr align {{(4|8)}} %[[X]], i8 0
25+
// IRCHECK: store ptr %0, ptr %[[X]], align {{(4|8)}}
26+
// IRCHECK-SAME: !dbg ![[LOC0:.*]]
27+
// IRCHECK-NEXT: %[[VALUE:.*]] = getelementptr inbounds %Ts5Int64V, ptr %0, i32 0, i32 0,
28+
// IRCHECK-SAME: !dbg ![[LOCLOAD:.*]]
29+
// IRCHECK-NEXT: load i64, ptr %[[VALUE]], align {{(4|8)}}
30+
// IRCHECK-SAME: !dbg ![[LOCLOAD]]
31+
// IRCHECK: ![[LOC0]] = !DILocation(line: 0,
32+
// IRCHECK: !DILocation(line: [[@LINE+1]],
1933
x = x + 2
2034
}
2135

0 commit comments

Comments
 (0)