[llvm] [ASan][JSON] Unpoison memory before its reuse (PR #79065)

via llvm-commits llvm-commits at lists.llvm.org
Mon Jan 22 23:11:44 PST 2024


https://github.com/AdvenamTacet updated https://github.com/llvm/llvm-project/pull/79065

>From d3a2144815590d9568ccc8c67fa933bff0297ec6 Mon Sep 17 00:00:00 2001
From: Advenam Tacet <advenam.tacet at trailofbits.com>
Date: Mon, 22 Jan 2024 23:12:16 +0100
Subject: [PATCH 1/4] [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 a81881c52d6c96..95394866f7e9ef 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 <typename T, typename... U> 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<T *>(&Union)) T(std::forward<U>(V)...);
   }
   template <typename T> T &as() const {

>From 3de40ac7ddde3455fca2e1c17e21eb88de6d8f75 Mon Sep 17 00:00:00 2001
From: Advenam Tacet <advenam.tacet at trailofbits.com>
Date: Mon, 22 Jan 2024 23:22:32 +0100
Subject: [PATCH 2/4] 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 95394866f7e9ef..bd712bdfd89b14 100644
--- a/llvm/include/llvm/Support/JSON.h
+++ b/llvm/include/llvm/Support/JSON.h
@@ -483,9 +483,10 @@ class Value {
 
   template <typename T, typename... U> 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<T *>(&Union)) T(std::forward<U>(V)...);

>From a30e5bec80cb70255873068299d096dba9009cf0 Mon Sep 17 00:00:00 2001
From: Advenam Tacet <advenam.tacet at trailofbits.com>
Date: Mon, 22 Jan 2024 23:40:44 +0100
Subject: [PATCH 3/4] 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 bd712bdfd89b14..4b3414e5bba2ba 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 <typename T, typename... U> 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 41ef369febae192191a1615005deae75d50c055b Mon Sep 17 00:00:00 2001
From: Advenam Tacet <advenam.tacet at trailofbits.com>
Date: Tue, 23 Jan 2024 08:11:07 +0100
Subject: [PATCH 4/4] 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 | 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 4b3414e5bba2ba..76b8ce44f08d88 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 <typename T, typename... U> 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.



More information about the llvm-commits mailing list