From a48ecb76397f47afeeb80b5de9ef7557fc4f0c02 Mon Sep 17 00:00:00 2001 From: Chandler Carruth Date: Sun, 30 Mar 2014 11:20:25 +0000 Subject: [PATCH] Don't mark the declarations of the TSan annotation functions as weak. That causes references to them to be weak references which can collapse to null if no definition is provided. We call these functions unconditionally, so a definition *must* be provided. Make the definitions provided in the .cpp file weak by re-declaring them as weak just prior to defining them. This should keep compilers which cannot attach the weak attribute to the definition happy while actually resolving the symbols correctly during the link. You might ask yourself upon reading this commit log: how did *any* of this work before? Well, fun story. It turns out we have some code in Support (BumpPtrAllocator) which both uses virtual dispatch and has out-of-line vtables used by that virtual dispatch. If you move the virtual dispatch into its header in *just* the right way, the optimizer gets to devirtualize, and remove all references to the vtable. Then the sad part: the references to this one vtable were the only strong symbol uses in the support library for llvm-tblgen AFAICT. At least, after doing something just like this, these symbols stopped getting their weak definition and random calls to them would segfault instead. Yay software. llvm-svn: 205137 --- llvm/include/llvm/Support/Valgrind.h | 10 ++++------ llvm/lib/Support/Valgrind.cpp | 14 +++++++++++--- 2 files changed, 15 insertions(+), 9 deletions(-) diff --git a/llvm/include/llvm/Support/Valgrind.h b/llvm/include/llvm/Support/Valgrind.h index 7ae40af754fc..cebf75c49c19 100644 --- a/llvm/include/llvm/Support/Valgrind.h +++ b/llvm/include/llvm/Support/Valgrind.h @@ -24,12 +24,10 @@ // tsan (Thread Sanitizer) is a valgrind-based tool that detects these exact // functions by name. extern "C" { -LLVM_ATTRIBUTE_WEAK void AnnotateHappensAfter(const char *file, int line, - const volatile void *cv); -LLVM_ATTRIBUTE_WEAK void AnnotateHappensBefore(const char *file, int line, - const volatile void *cv); -LLVM_ATTRIBUTE_WEAK void AnnotateIgnoreWritesBegin(const char *file, int line); -LLVM_ATTRIBUTE_WEAK void AnnotateIgnoreWritesEnd(const char *file, int line); +void AnnotateHappensAfter(const char *file, int line, const volatile void *cv); +void AnnotateHappensBefore(const char *file, int line, const volatile void *cv); +void AnnotateIgnoreWritesBegin(const char *file, int line); +void AnnotateIgnoreWritesEnd(const char *file, int line); } #endif diff --git a/llvm/lib/Support/Valgrind.cpp b/llvm/lib/Support/Valgrind.cpp index 2b250a357758..2c6d6aaadfff 100644 --- a/llvm/lib/Support/Valgrind.cpp +++ b/llvm/lib/Support/Valgrind.cpp @@ -55,13 +55,21 @@ void llvm::sys::ValgrindDiscardTranslations(const void *Addr, size_t Len) { #if LLVM_ENABLE_THREADS != 0 && !defined(NDEBUG) // These functions require no implementation, tsan just looks at the arguments -// they're called with. +// they're called with. However, they are required to be weak as some other +// application or library may already be providing these definitions for the +// same reason we are. extern "C" { +LLVM_ATTRIBUTE_WEAK void AnnotateHappensAfter(const char *file, int line, + const volatile void *cv); +void AnnotateHappensAfter(const char *file, int line, const volatile void *cv) { +} +LLVM_ATTRIBUTE_WEAK void AnnotateHappensBefore(const char *file, int line, + const volatile void *cv); void AnnotateHappensBefore(const char *file, int line, const volatile void *cv) {} -void AnnotateHappensAfter(const char *file, int line, - const volatile void *cv) {} +LLVM_ATTRIBUTE_WEAK void AnnotateIgnoreWritesBegin(const char *file, int line); void AnnotateIgnoreWritesBegin(const char *file, int line) {} +LLVM_ATTRIBUTE_WEAK void AnnotateIgnoreWritesEnd(const char *file, int line); void AnnotateIgnoreWritesEnd(const char *file, int line) {} } #endif