From d3a2144815590d9568ccc8c67fa933bff0297ec6 Mon Sep 17 00:00:00 2001 From: Advenam Tacet Date: Mon, 22 Jan 2024 23:12:16 +0100 Subject: [PATCH 1/6] [JSON] Unpoison memory before its reuse This commit unpoisons memory before its reuse (with reinterpret_cast). Required by https://github.com/llvm/llvm-project/pull/79049 --- llvm/include/llvm/Support/JSON.h | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/llvm/include/llvm/Support/JSON.h b/llvm/include/llvm/Support/JSON.h index a81881c52d6c9..95394866f7e9e 100644 --- a/llvm/include/llvm/Support/JSON.h +++ b/llvm/include/llvm/Support/JSON.h @@ -482,6 +482,12 @@ class Value { friend class Object; template void create(U &&... V) { +#if defined(ADDRESS_SANITIZER) || defined(__SANITIZE_ADDRESS__) + // 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. + __asan_unpoison_memory_region(&Union, sizeof(T)); +#endif new (reinterpret_cast(&Union)) T(std::forward(V)...); } template T &as() const { From 3de40ac7ddde3455fca2e1c17e21eb88de6d8f75 Mon Sep 17 00:00:00 2001 From: Advenam Tacet Date: Mon, 22 Jan 2024 23:22:32 +0100 Subject: [PATCH 2/6] clang-format-fix --- llvm/include/llvm/Support/JSON.h | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/llvm/include/llvm/Support/JSON.h b/llvm/include/llvm/Support/JSON.h index 95394866f7e9e..bd712bdfd89b1 100644 --- a/llvm/include/llvm/Support/JSON.h +++ b/llvm/include/llvm/Support/JSON.h @@ -483,9 +483,10 @@ class Value { template void create(U &&... V) { #if defined(ADDRESS_SANITIZER) || defined(__SANITIZE_ADDRESS__) - // 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. + // 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. __asan_unpoison_memory_region(&Union, sizeof(T)); #endif new (reinterpret_cast(&Union)) T(std::forward(V)...); From a30e5bec80cb70255873068299d096dba9009cf0 Mon Sep 17 00:00:00 2001 From: Advenam Tacet Date: Mon, 22 Jan 2024 23:40:44 +0100 Subject: [PATCH 3/6] Use LLVM_ADDRESS_SANITIZER_BUILD --- llvm/include/llvm/Support/JSON.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/llvm/include/llvm/Support/JSON.h b/llvm/include/llvm/Support/JSON.h index bd712bdfd89b1..4b3414e5bba2b 100644 --- a/llvm/include/llvm/Support/JSON.h +++ b/llvm/include/llvm/Support/JSON.h @@ -482,7 +482,7 @@ class Value { friend class Object; template void create(U &&... V) { -#if defined(ADDRESS_SANITIZER) || defined(__SANITIZE_ADDRESS__) +#if defined(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. From 442bc6398eb96e3271f1e1459eb8c23f12f9ce92 Mon Sep 17 00:00:00 2001 From: Advenam Tacet Date: Tue, 23 Jan 2024 08:11:07 +0100 Subject: [PATCH 4/6] Fix use of LLVM_ADDRESS_SANITIZER_BUILD This commit fixes incorrect use of `LLVM_ADDRESS_SANITIZER_BUILD` macro. Details here: https://github.com/llvm/llvm-project/pull/79066#discussion_r1462618308 --- llvm/include/llvm/Support/JSON.h | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/llvm/include/llvm/Support/JSON.h b/llvm/include/llvm/Support/JSON.h index 4b3414e5bba2b..1d257a87f4110 100644 --- a/llvm/include/llvm/Support/JSON.h +++ b/llvm/include/llvm/Support/JSON.h @@ -50,6 +50,7 @@ #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,7 +483,7 @@ class Value { friend class Object; template void create(U &&... V) { -#if defined(LLVM_ADDRESS_SANITIZER_BUILD) +#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. From cd149eb23f6d7e250d75155dbd1975b7acc5a4fe Mon Sep 17 00:00:00 2001 From: Advenam Tacet Date: Tue, 23 Jan 2024 08:20:20 +0100 Subject: [PATCH 5/6] Strange clang-format fix --- llvm/include/llvm/Support/JSON.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/llvm/include/llvm/Support/JSON.h b/llvm/include/llvm/Support/JSON.h index 1d257a87f4110..4b312981018a5 100644 --- a/llvm/include/llvm/Support/JSON.h +++ b/llvm/include/llvm/Support/JSON.h @@ -47,9 +47,9 @@ #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" From a8e9162a7616fae01cd34b8f847bcf7cb1873c81 Mon Sep 17 00:00:00 2001 From: Advenam Tacet Date: Tue, 23 Jan 2024 19:09:21 +0100 Subject: [PATCH 6/6] Add FIXME to the comment. --- llvm/include/llvm/Support/JSON.h | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/llvm/include/llvm/Support/JSON.h b/llvm/include/llvm/Support/JSON.h index 4b312981018a5..8b437bbabd962 100644 --- a/llvm/include/llvm/Support/JSON.h +++ b/llvm/include/llvm/Support/JSON.h @@ -488,6 +488,11 @@ class Value { // 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)...);