Skip to content

Commit 9e322c5

Browse files
authored
[TySan] Don't report globals with external storage. (#120565)
Globals with external storage should have been initialized where they are defined. Fixes #120448 PR: #120565
1 parent 4c3e13e commit 9e322c5

File tree

2 files changed

+61
-0
lines changed

2 files changed

+61
-0
lines changed

clang/lib/CodeGen/SanitizerMetadata.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -145,6 +145,9 @@ void SanitizerMetadata::reportGlobal(llvm::GlobalVariable *GV, const VarDecl &D,
145145
for (auto *Attr : D.specific_attrs<NoSanitizeAttr>())
146146
NoSanitizeMask |= Attr->getMask();
147147

148+
if (D.hasExternalStorage())
149+
NoSanitizeMask |= SanitizerKind::Type;
150+
148151
return NoSanitizeMask;
149152
};
150153

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
// NOTE: Assertions have been autogenerated by utils/update_cc_test_checks.py UTC_ARGS: --check-globals all --filter-out "attributes" --filter-out "attributes #" --version 5
2+
// RUN: %clang_cc1 -triple x86_64-linux-gnu -emit-llvm -o - %s -fsanitize=type | FileCheck -check-prefix=CHECK %s
3+
4+
//.
5+
// CHECK: @x = global %struct.CompleteS zeroinitializer, align 8
6+
// CHECK: @y = external global %struct.S, align 1
7+
// CHECK: @__tysan_shadow_memory_address = external global i64
8+
// CHECK: @__tysan_app_memory_mask = external global i64
9+
// CHECK: @__tysan_v1_Simple_20C_2b_2b_20TBAA = linkonce_odr constant { i64, i64, [16 x i8] } { i64 2, i64 0, [16 x i8] c"Simple C++ TBAA\00" }, comdat
10+
// CHECK: @__tysan_v1_omnipotent_20char = linkonce_odr constant { i64, i64, ptr, i64, [16 x i8] } { i64 2, i64 1, ptr @__tysan_v1_Simple_20C_2b_2b_20TBAA, i64 0, [16 x i8] c"omnipotent char\00" }, comdat
11+
// CHECK: @__tysan_v1_int = linkonce_odr constant { i64, i64, ptr, i64, [4 x i8] } { i64 2, i64 1, ptr @__tysan_v1_omnipotent_20char, i64 0, [4 x i8] c"int\00" }, comdat
12+
// CHECK: @__tysan_v1_any_20pointer = linkonce_odr constant { i64, i64, ptr, i64, [12 x i8] } { i64 2, i64 1, ptr @__tysan_v1_omnipotent_20char, i64 0, [12 x i8] c"any pointer\00" }, comdat
13+
// CHECK: @__tysan_v1_p1_20int = linkonce_odr constant { i64, i64, ptr, i64, [7 x i8] } { i64 2, i64 1, ptr @__tysan_v1_any_20pointer, i64 0, [7 x i8] c"p1 int\00" }, comdat
14+
// CHECK: @__tysan_v1___ZTS9CompleteS = linkonce_odr constant { i64, i64, ptr, i64, ptr, i64, [15 x i8] } { i64 2, i64 2, ptr @__tysan_v1_int, i64 0, ptr @__tysan_v1_p1_20int, i64 8, [15 x i8] c"_ZTS9CompleteS\00" }, comdat
15+
// CHECK: @llvm.used = appending global [7 x ptr] [ptr @tysan.module_ctor, ptr @__tysan_v1_Simple_20C_2b_2b_20TBAA, ptr @__tysan_v1_omnipotent_20char, ptr @__tysan_v1_int, ptr @__tysan_v1_any_20pointer, ptr @__tysan_v1_p1_20int, ptr @__tysan_v1___ZTS9CompleteS], section "llvm.metadata"
16+
// CHECK: @llvm.global_ctors = appending global [1 x { i32, ptr, ptr }] [{ i32, ptr, ptr } { i32 0, ptr @tysan.module_ctor, ptr null }]
17+
//.
18+
struct CompleteS {
19+
int x;
20+
int *ptr;
21+
};
22+
23+
void f(CompleteS *);
24+
CompleteS x;
25+
// CHECK-LABEL: define dso_local void @_Z1gv(
26+
// CHECK-SAME: ) #[[ATTR0:[0-9]+]] {
27+
// CHECK: [[ENTRY:.*:]]
28+
// CHECK: call void @_Z1fP9CompleteS(ptr noundef @x)
29+
// CHECK: ret void
30+
//
31+
void g() { f(&x); }
32+
33+
typedef struct S IncompleteS;
34+
void f(IncompleteS *);
35+
extern IncompleteS y;
36+
// CHECK-LABEL: define dso_local void @_Z1hv(
37+
// CHECK-SAME: ) #[[ATTR0]] {
38+
// CHECK: [[ENTRY:.*:]]
39+
// CHECK: call void @_Z1fP1S(ptr noundef @y)
40+
// CHECK: ret void
41+
//
42+
void h() { f(&y); }
43+
//.
44+
// CHECK: attributes #[[ATTR0]] = { mustprogress noinline nounwind optnone sanitize_type "min-legal-vector-width"="0" "no-trapping-math"="true" "stack-protector-buffer-size"="8" "target-features"="+cx8,+mmx,+sse,+sse2,+x87" }
45+
// CHECK: attributes #[[ATTR1:[0-9]+]] = { "no-trapping-math"="true" "stack-protector-buffer-size"="8" "target-features"="+cx8,+mmx,+sse,+sse2,+x87" }
46+
// CHECK: attributes #[[ATTR2:[0-9]+]] = { nounwind "target-features"="+cx8,+mmx,+sse,+sse2,+x87" }
47+
// CHECK: attributes #[[ATTR3:[0-9]+]] = { nounwind }
48+
//.
49+
// CHECK: [[META0:![0-9]+]] = !{ptr @x, [[META1:![0-9]+]]}
50+
// CHECK: [[META1]] = !{!"_ZTS9CompleteS", [[META2:![0-9]+]], i64 0, [[META5:![0-9]+]], i64 8}
51+
// CHECK: [[META2]] = !{!"int", [[META3:![0-9]+]], i64 0}
52+
// CHECK: [[META3]] = !{!"omnipotent char", [[META4:![0-9]+]], i64 0}
53+
// CHECK: [[META4]] = !{!"Simple C++ TBAA"}
54+
// CHECK: [[META5]] = !{!"p1 int", [[META6:![0-9]+]], i64 0}
55+
// CHECK: [[META6]] = !{!"any pointer", [[META3]], i64 0}
56+
// CHECK: [[META7:![0-9]+]] = !{i32 1, !"wchar_size", i32 4}
57+
// CHECK: [[META8:![0-9]+]] = !{!"{{.*}}clang version {{.*}}"}
58+
//.

0 commit comments

Comments
 (0)