[asan] Align __asan_global_start so that it works with LLD
authorReid Kleckner <rnk@google.com>
Thu, 26 Apr 2018 20:46:50 +0000 (20:46 +0000)
committerReid Kleckner <rnk@google.com>
Thu, 26 Apr 2018 20:46:50 +0000 (20:46 +0000)
Otherwise LLD will not align the .ASAN$GA section start, and
&__asan_globals + 1 will not be the start of the next real ASan global
metadata in .ASAN$GL.

We discovered this issue when attempting to use LLD on Windows in
Chromium: https://crbug.com/837090

llvm-svn: 330990

compiler-rt/lib/asan/asan_globals_win.cc
compiler-rt/test/asan/TestCases/Windows/fuse-lld-globals.cc [new file with mode: 0644]

index 261762b..29ab5eb 100644 (file)
@@ -19,9 +19,9 @@ namespace __asan {
 #pragma section(".ASAN$GA", read, write)  // NOLINT
 #pragma section(".ASAN$GZ", read, write)  // NOLINT
 extern "C" __declspec(allocate(".ASAN$GA"))
-__asan_global __asan_globals_start = {};
+    ALIGNED(sizeof(__asan_global)) __asan_global __asan_globals_start = {};
 extern "C" __declspec(allocate(".ASAN$GZ"))
-__asan_global __asan_globals_end = {};
+    ALIGNED(sizeof(__asan_global)) __asan_global __asan_globals_end = {};
 #pragma comment(linker, "/merge:.ASAN=.data")
 
 static void call_on_globals(void (*hook)(__asan_global *, uptr)) {
diff --git a/compiler-rt/test/asan/TestCases/Windows/fuse-lld-globals.cc b/compiler-rt/test/asan/TestCases/Windows/fuse-lld-globals.cc
new file mode 100644 (file)
index 0000000..4148d56
--- /dev/null
@@ -0,0 +1,18 @@
+// RUN: %clangxx_asan -fuse-ld=lld -O3 %s -o %t && not %run %t 2>&1 | FileCheck %s
+
+#include <string.h>
+int main(int argc, char **argv) {
+  static char XXX[10];
+  static char YYY[10];
+  static char ZZZ[10];
+  memset(XXX, 0, 10);
+  memset(YYY, 0, 10);
+  memset(ZZZ, 0, 10);
+  int res = YYY[argc * 10];  // BOOOM
+  // CHECK: {{READ of size 1 at 0x.* thread T0}}
+  // CHECK: {{    #0 0x.* in main .*fuse-lld-globals.cc:}}[[@LINE-2]]
+  // CHECK: {{0x.* is located 0 bytes to the right of global variable}}
+  // CHECK:   {{.*YYY.* of size 10}}
+  res += XXX[argc] + ZZZ[argc];
+  return res;
+}