Skip to content

Commit 7045519

Browse files
committed
Add missing sanitizer metadata plumbing from CFE.
clang misses attaching sanitizer metadata for external globals. Reviewed By: eugenis Differential Revision: https://reviews.llvm.org/D129492
1 parent 90e5a8a commit 7045519

File tree

4 files changed

+17
-0
lines changed

4 files changed

+17
-0
lines changed

clang/lib/CodeGen/CodeGenModule.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4293,6 +4293,9 @@ CodeGenModule::GetOrCreateLLVMGlobal(StringRef MangledName, llvm::Type *Ty,
42934293
getCUDARuntime().handleVarRegistration(D, *GV);
42944294
}
42954295

4296+
if (D)
4297+
SanitizerMD->reportGlobal(GV, *D);
4298+
42964299
LangAS ExpectedAS =
42974300
D ? D->getType().getAddressSpace()
42984301
: (LangOpts.OpenCL ? LangAS::opencl_global : LangAS::Default);

clang/test/CodeGen/asan-globals.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ int dyn_init_global = global;
1212
int __attribute__((no_sanitize("address"))) attributed_global;
1313
int __attribute__((disable_sanitizer_instrumentation)) disable_instrumentation_global;
1414
int ignorelisted_global;
15+
extern int __attribute__((no_sanitize("address"))) external_global;
1516

1617
int __attribute__((section("__DATA, __common"))) sectioned_global; // KASAN - ignore globals in a section
1718
extern "C" {
@@ -21,6 +22,7 @@ int __special_global; // KASAN - ignore globals with __-prefix
2122
void func() {
2223
static int static_var = 0;
2324
const char *literal = "Hello, world!";
25+
external_global = 1;
2426
}
2527

2628
// GLOBS: @{{.*}}extra_global{{.*}} ={{.*}} global
@@ -49,6 +51,8 @@ void func() {
4951
// GLOBS: @{{.*}} = {{.*}}c"Hello, world!\00"
5052
// GLOBS-NOT: no_sanitize_address
5153

54+
// GLOBS: @{{.*}}external_global{{.*}} ={{.*}} no_sanitize_address
55+
5256
/// Without -fasynchronous-unwind-tables, ctor and dtor get the uwtable attribute.
5357
// CHECK-LABEL: define internal void @asan.module_ctor() #[[#ATTR:]] {
5458
// ASAN-NEXT: call void @__asan_init
@@ -83,3 +87,4 @@ void func() {
8387
// IGNORELIST-SRC: @{{.*}}__special_global{{.*}} ={{.*}} global {{.*}} no_sanitize_address
8488
// IGNORELIST-SRC: @{{.*}}static_var{{.*}} ={{.*}} global {{.*}} no_sanitize_address
8589
// IGNORELIST-SRC: @{{.*}} ={{.*}} c"Hello, world!\00"{{.*}} no_sanitize_address
90+
// IGNORELIST-SRC: @{{.*}}external_global{{.*}} ={{.*}} no_sanitize_address

clang/test/CodeGen/hwasan-globals.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,15 +14,18 @@ int global;
1414
int __attribute__((no_sanitize("hwaddress"))) attributed_global;
1515
int __attribute__((disable_sanitizer_instrumentation)) disable_instrumentation_global;
1616
int ignorelisted_global;
17+
extern int __attribute__((no_sanitize("hwaddress"))) external_global;
1718

1819
void func() {
1920
static int static_var = 0;
2021
const char *literal = "Hello, world!";
22+
external_global = 1;
2123
}
2224

2325
// CHECK: @{{.*}}attributed_global{{.*}} ={{.*}} global {{.*}}, no_sanitize_hwaddress
2426
// CHECK: @{{.*}}disable_instrumentation_global{{.*}} ={{.*}} global {{.*}}, no_sanitize_hwaddress
2527
// CHECK: @{{.*}}ignorelisted_global{{.*}} ={{.*}} global {{.*}}, no_sanitize_hwaddress
28+
// CHECK: @{{.*}}external_global{{.*}} ={{.*}}, no_sanitize_hwaddress
2629
// CHECK: @{{.*}}extra_global{{.*}}.hwasan{{.*}} =
2730
// CHECK: @{{.*}}global{{.*}}.hwasan{{.*}} =
2831
// CHECK: @{{.*}}static_var{{.*}}.hwasan{{.*}} =
@@ -34,4 +37,5 @@ void func() {
3437
// IGNORELIST: @{{.*}}ignorelisted_globa{{.*}} ={{.*}} global {{.*}}, no_sanitize_hwaddress
3538
// IGNORELIST: @{{.*}}static_var{{.*}} ={{.*}} global {{.*}}, no_sanitize_hwaddress
3639
// IGNORELIST: @{{.*}} = {{.*}} c"Hello, world!\00"{{.*}}, no_sanitize_hwaddress
40+
// IGNORELIST: @{{.*}}external_global{{.*}} ={{.*}}, no_sanitize_hwaddress
3741
// IGNORELIST: @{{.*}}extra_global{{.*}}.hwasan{{.*}} =

clang/test/CodeGen/memtag-globals.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,12 @@ int global;
1111
int __attribute__((no_sanitize("memtag"))) attributed_global;
1212
int __attribute__((disable_sanitizer_instrumentation)) disable_instrumentation_global;
1313
int ignorelisted_global;
14+
extern int external_global;
1415

1516
void func() {
1617
static int static_var = 0;
1718
const char *literal = "Hello, world!";
19+
external_global = 1;
1820
}
1921

2022
// CHECK: @{{.*}}extra_global{{.*}} ={{.*}} sanitize_memtag
@@ -29,6 +31,7 @@ void func() {
2931

3032
// CHECK: @{{.*}}static_var{{.*}} ={{.*}} sanitize_memtag
3133
// CHECK: @{{.*}} = {{.*}} c"Hello, world!\00"{{.*}} sanitize_memtag
34+
// CHECK: @{{.*}}external_global{{.*}} ={{.*}} sanitize_memtag
3235

3336
// IGNORELIST: @{{.*}}extra_global{{.*}} ={{.*}} sanitize_memtag
3437

@@ -44,3 +47,5 @@ void func() {
4447
// IGNORELIST-NOT: sanitize_memtag
4548
// IGNORELIST: @{{.*}} = {{.*}} c"Hello, world!\00"{{.*}}
4649
// IGNORELIST-NOT: sanitize_memtag
50+
// IGNORELIST: @{{.*}}external_global{{.*}} =
51+
// IGNORELIST-NOT: sanitize_memtag

0 commit comments

Comments
 (0)