From c02ed2a8e2e8cdbc55db546ce2ba5c7004d5eb64 Mon Sep 17 00:00:00 2001 From: Kostya Serebryany Date: Sat, 6 Feb 2016 03:22:24 +0000 Subject: [PATCH] [asan] properly report an un-aligned global variable instead of just crashing llvm-svn: 259979 --- compiler-rt/lib/asan/asan_globals.cc | 11 +++++++++- compiler-rt/test/asan/TestCases/Linux/odr_c_test.c | 25 ++++++++++++++++++++++ 2 files changed, 35 insertions(+), 1 deletion(-) create mode 100644 compiler-rt/test/asan/TestCases/Linux/odr_c_test.c diff --git a/compiler-rt/lib/asan/asan_globals.cc b/compiler-rt/lib/asan/asan_globals.cc index eb9f1bf..ebe0d10 100644 --- a/compiler-rt/lib/asan/asan_globals.cc +++ b/compiler-rt/lib/asan/asan_globals.cc @@ -144,7 +144,16 @@ static void RegisterGlobal(const Global *g) { ReportGlobal(*g, "Added"); CHECK(flags()->report_globals); CHECK(AddrIsInMem(g->beg)); - CHECK(AddrIsAlignedByGranularity(g->beg)); + if (!AddrIsAlignedByGranularity(g->beg)) { + Report("The following global variable is not properly aligned.\n"); + Report("This may happen if another global with the same name\n"); + Report("resides in another non-instrumented module.\n"); + Report("Or the global comes from a C file built w/o -fno-common.\n"); + Report("In either case this is likely an ODR violation bug,\n"); + Report("but AddressSanitizer can not provide more details.\n"); + ReportODRViolation(g, FindRegistrationSite(g), g, FindRegistrationSite(g)); + CHECK(AddrIsAlignedByGranularity(g->beg)); + } CHECK(AddrIsAlignedByGranularity(g->size_with_redzone)); if (flags()->detect_odr_violation) { // Try detecting ODR (One Definition Rule) violation, i.e. the situation diff --git a/compiler-rt/test/asan/TestCases/Linux/odr_c_test.c b/compiler-rt/test/asan/TestCases/Linux/odr_c_test.c new file mode 100644 index 0000000..ce824bad --- /dev/null +++ b/compiler-rt/test/asan/TestCases/Linux/odr_c_test.c @@ -0,0 +1,25 @@ +// Test that we can properly report an ODR violation +// between an instrumented global and a non-instrumented global. + +// RUN: %clang_asan %s -fPIC -shared -o %t-1.so -DFILE1 +// RUN: %clang_asan %s -fPIC -shared -o %t-2.so -DFILE2 +// RUN: %clang_asan %s -fPIE %t-1.so %t-2.so -Wl,-R`pwd` -o %t +// RUN: not %run %t 2>&1 | FileCheck %s +// +// REQUIRES: x86_64-supported-target +// +// CHECK: The following global variable is not properly aligned. +// CHECK: ERROR: AddressSanitizer: odr-violation +#if defined(FILE1) +__attribute__((aligned(8))) int x; +__attribute__((aligned(1))) char y; +__attribute__((aligned(1))) char ZZZ[100]; +#elif defined(FILE2) +int ZZZ = 1; +#else +extern int ZZZ; +int main() { + return ZZZ; +} +#endif + -- 2.7.4