From c416b2efe89c11db593fe8041c366e0cb63d4eeb Mon Sep 17 00:00:00 2001 From: Tacet Date: Tue, 23 Jan 2024 19:16:53 +0100 Subject: [PATCH] [ASan][JSON] Unpoison memory before its reuse (#79065) This commit unpoisons memory before its reuse (with reinterpret_cast). Required by https://github.com/llvm/llvm-project/pull/79049 Notice that it's a temporary solution to prevent buildbots from failing. Read FIXME for details. --- llvm/include/llvm/Support/JSON.h | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) diff --git a/llvm/include/llvm/Support/JSON.h b/llvm/include/llvm/Support/JSON.h index a81881c52d6c96..8b437bbabd962f 100644 --- a/llvm/include/llvm/Support/JSON.h +++ b/llvm/include/llvm/Support/JSON.h @@ -47,9 +47,10 @@ #define LLVM_SUPPORT_JSON_H #include "llvm/ADT/DenseMap.h" +#include "llvm/ADT/STLFunctionalExtras.h" #include "llvm/ADT/SmallVector.h" #include "llvm/ADT/StringRef.h" -#include "llvm/ADT/STLFunctionalExtras.h" +#include "llvm/Support/Compiler.h" #include "llvm/Support/Error.h" #include "llvm/Support/FormatVariadic.h" #include "llvm/Support/raw_ostream.h" @@ -482,6 +483,18 @@ class Value { friend class Object; template void create(U &&... V) { +#if LLVM_ADDRESS_SANITIZER_BUILD + // Unpoisoning to prevent overwriting poisoned object (e.g., annotated short + // string). Objects that have had their memory poisoned may cause an ASan + // error if their memory is reused without calling their destructor. + // Unpoisoning the memory prevents this error from occurring. + // FIXME: This is a temporary solution to prevent buildbots from failing. + // The more appropriate approach would be to call the object's destructor + // to unpoison memory. This would prevent any potential memory leaks (long + // strings). Read for details: + // https://github.com/llvm/llvm-project/pull/79065#discussion_r1462621761 + __asan_unpoison_memory_region(&Union, sizeof(T)); +#endif new (reinterpret_cast(&Union)) T(std::forward(V)...); } template T &as() const {