Add missing sanitizer metadata plumbing from CFE.
authorMitch Phillips <31459023+hctim@users.noreply.github.com>
Wed, 13 Jul 2022 15:39:31 +0000 (08:39 -0700)
committerMitch Phillips <31459023+hctim@users.noreply.github.com>
Wed, 13 Jul 2022 15:54:41 +0000 (08:54 -0700)
clang misses attaching sanitizer metadata for external globals.

Reviewed By: eugenis

Differential Revision: https://reviews.llvm.org/D129492

clang/lib/CodeGen/CodeGenModule.cpp
clang/test/CodeGen/asan-globals.cpp
clang/test/CodeGen/hwasan-globals.cpp
clang/test/CodeGen/memtag-globals.cpp

index f832a06..06123ca 100644 (file)
@@ -4293,6 +4293,9 @@ CodeGenModule::GetOrCreateLLVMGlobal(StringRef MangledName, llvm::Type *Ty,
       getCUDARuntime().handleVarRegistration(D, *GV);
   }
 
+  if (D)
+    SanitizerMD->reportGlobal(GV, *D);
+
   LangAS ExpectedAS =
       D ? D->getType().getAddressSpace()
         : (LangOpts.OpenCL ? LangAS::opencl_global : LangAS::Default);
index a3b786f..4a370cb 100644 (file)
@@ -12,6 +12,7 @@ int dyn_init_global = global;
 int __attribute__((no_sanitize("address"))) attributed_global;
 int __attribute__((disable_sanitizer_instrumentation)) disable_instrumentation_global;
 int ignorelisted_global;
+extern int __attribute__((no_sanitize("address"))) external_global;
 
 int __attribute__((section("__DATA, __common"))) sectioned_global; // KASAN - ignore globals in a section
 extern "C" {
@@ -21,6 +22,7 @@ int __special_global; // KASAN - ignore globals with __-prefix
 void func() {
   static int static_var = 0;
   const char *literal = "Hello, world!";
+  external_global = 1;
 }
 
 // GLOBS:     @{{.*}}extra_global{{.*}} ={{.*}} global
@@ -49,6 +51,8 @@ void func() {
 // GLOBS:     @{{.*}} = {{.*}}c"Hello, world!\00"
 // GLOBS-NOT: no_sanitize_address
 
+// GLOBS: @{{.*}}external_global{{.*}} ={{.*}} no_sanitize_address
+
 /// Without -fasynchronous-unwind-tables, ctor and dtor get the uwtable attribute.
 // CHECK-LABEL: define internal void @asan.module_ctor() #[[#ATTR:]] {
 // ASAN-NEXT: call void @__asan_init
@@ -83,3 +87,4 @@ void func() {
 // IGNORELIST-SRC:     @{{.*}}__special_global{{.*}} ={{.*}} global {{.*}} no_sanitize_address
 // IGNORELIST-SRC:     @{{.*}}static_var{{.*}} ={{.*}} global {{.*}} no_sanitize_address
 // IGNORELIST-SRC:     @{{.*}} ={{.*}} c"Hello, world!\00"{{.*}} no_sanitize_address
+// IGNORELIST-SRC:     @{{.*}}external_global{{.*}} ={{.*}} no_sanitize_address
index d9f2e69..0ab6025 100644 (file)
@@ -14,15 +14,18 @@ int global;
 int __attribute__((no_sanitize("hwaddress"))) attributed_global;
 int __attribute__((disable_sanitizer_instrumentation)) disable_instrumentation_global;
 int ignorelisted_global;
+extern int __attribute__((no_sanitize("hwaddress"))) external_global;
 
 void func() {
   static int static_var = 0;
   const char *literal = "Hello, world!";
+  external_global = 1;
 }
 
 // CHECK: @{{.*}}attributed_global{{.*}} ={{.*}} global {{.*}}, no_sanitize_hwaddress
 // CHECK: @{{.*}}disable_instrumentation_global{{.*}} ={{.*}} global {{.*}}, no_sanitize_hwaddress
 // CHECK: @{{.*}}ignorelisted_global{{.*}} ={{.*}} global {{.*}}, no_sanitize_hwaddress
+// CHECK: @{{.*}}external_global{{.*}} ={{.*}}, no_sanitize_hwaddress
 // CHECK: @{{.*}}extra_global{{.*}}.hwasan{{.*}} =
 // CHECK: @{{.*}}global{{.*}}.hwasan{{.*}} =
 // CHECK: @{{.*}}static_var{{.*}}.hwasan{{.*}} =
@@ -34,4 +37,5 @@ void func() {
 // IGNORELIST: @{{.*}}ignorelisted_globa{{.*}} ={{.*}} global {{.*}}, no_sanitize_hwaddress
 // IGNORELIST: @{{.*}}static_var{{.*}} ={{.*}} global {{.*}}, no_sanitize_hwaddress
 // IGNORELIST: @{{.*}} = {{.*}} c"Hello, world!\00"{{.*}}, no_sanitize_hwaddress
+// IGNORELIST: @{{.*}}external_global{{.*}} ={{.*}}, no_sanitize_hwaddress
 // IGNORELIST: @{{.*}}extra_global{{.*}}.hwasan{{.*}} =
index da801cc..b4f5dc0 100644 (file)
@@ -11,10 +11,12 @@ int global;
 int __attribute__((no_sanitize("memtag"))) attributed_global;
 int __attribute__((disable_sanitizer_instrumentation)) disable_instrumentation_global;
 int ignorelisted_global;
+extern int external_global;
 
 void func() {
   static int static_var = 0;
   const char *literal = "Hello, world!";
+  external_global = 1;
 }
 
 // CHECK: @{{.*}}extra_global{{.*}} ={{.*}} sanitize_memtag
@@ -29,6 +31,7 @@ void func() {
 
 // CHECK: @{{.*}}static_var{{.*}} ={{.*}} sanitize_memtag
 // CHECK: @{{.*}} = {{.*}} c"Hello, world!\00"{{.*}} sanitize_memtag
+// CHECK: @{{.*}}external_global{{.*}} ={{.*}} sanitize_memtag
 
 // IGNORELIST: @{{.*}}extra_global{{.*}} ={{.*}} sanitize_memtag
 
@@ -44,3 +47,5 @@ void func() {
 // IGNORELIST-NOT: sanitize_memtag
 // IGNORELIST:     @{{.*}} = {{.*}} c"Hello, world!\00"{{.*}}
 // IGNORELIST-NOT: sanitize_memtag
+// IGNORELIST:     @{{.*}}external_global{{.*}} =
+// IGNORELIST-NOT: sanitize_memtag