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

via llvm-commits llvm-commits at lists.llvm.org
Mon Jan 22 23:20:55 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/5] [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 a81881c52d6c960..95394866f7e9ef1 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/5] 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 95394866f7e9ef1..bd712bdfd89b14a 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/5] 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 bd712bdfd89b14a..4b3414e5bba2ba2 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 442bc6398eb96e3271f1e1459eb8c23f12f9ce92 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/5] 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 4b3414e5bba2ba2..1d257a87f411059 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 <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.

>From cd149eb23f6d7e250d75155dbd1975b7acc5a4fe Mon Sep 17 00:00:00 2001
From: Advenam Tacet <advenam.tacet at trailofbits.com>
Date: Tue, 23 Jan 2024 08:20:20 +0100
Subject: [PATCH 5/5] 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 1d257a87f411059..4b312981018a5d8 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"



More information about the llvm-commits mailing list