[libc-commits] [libc] [libc][msan] Fix "non-constexpr function '__msan_unpoison' cannot be used in a constant expression" (PR #88719)

Guillaume Chatelet via libc-commits libc-commits at lists.llvm.org
Tue Apr 16 05:10:48 PDT 2024


https://github.com/gchatelet updated https://github.com/llvm/llvm-project/pull/88719

>From 03a8e9152ac4d28a2041ec94b9a09750b90e5f7c Mon Sep 17 00:00:00 2001
From: Guillaume Chatelet <gchatelet at google.com>
Date: Mon, 15 Apr 2024 12:04:49 +0000
Subject: [PATCH 1/2] [libc][msan] Fix "non-constexpr function
 '__msan_unpoison' cannot be used in a constant expression"

---
 libc/src/__support/macros/sanitizer.h | 15 +++++++++++----
 1 file changed, 11 insertions(+), 4 deletions(-)

diff --git a/libc/src/__support/macros/sanitizer.h b/libc/src/__support/macros/sanitizer.h
index bd9b62b7121a14..9a2fe8c4bcc14d 100644
--- a/libc/src/__support/macros/sanitizer.h
+++ b/libc/src/__support/macros/sanitizer.h
@@ -47,14 +47,21 @@
 // Functions to unpoison memory
 //-----------------------------------------------------------------------------
 
-#if defined(LIBC_HAVE_MEMORY_SANITIZER) && __has_builtin(__builtin_constant_p)
-// Only perform MSAN unpoison in non-constexpr context.
+#if defined(LIBC_HAVE_MEMORY_SANITIZER)
+// Only perform MSAN unpoison in non-constexpr context and silence
+// '-Wconstant-evaluated' when MSAN_UNPOISON is called from manifestly constant
+// contexts.
 #include <sanitizer/msan_interface.h>
 #define MSAN_UNPOISON(addr, size)                                              \
   do {                                                                         \
-    if (!__builtin_constant_p(*addr)) {                                        \
+    _Pragma("GCC diagnostic push \"-Wconstant-evaluated\"");                   \
+    _Pragma("GCC diagnostic ignored \"-Wconstant-evaluated\"");                \
+    _Pragma("clang diagnostic push \"-Wconstant-evaluated\"");                 \
+    _Pragma("clang diagnostic ignored \"-Wconstant-evaluated\"");              \
+    if constexpr (!__builtin_is_constant_evaluated())                          \
       __msan_unpoison(addr, size);                                             \
-    }                                                                          \
+    _Pragma("clang diagnostic pop");                                           \
+    _Pragma("GCC diagnostic pop");                                             \
   } while (0)
 #else
 #define MSAN_UNPOISON(ptr, size)

>From 0b164f452326a79e3e54bc9522e1855dd4142cb3 Mon Sep 17 00:00:00 2001
From: Guillaume Chatelet <gchatelet at google.com>
Date: Tue, 16 Apr 2024 12:10:33 +0000
Subject: [PATCH 2/2] remove `constexpr` and associated pragmas

---
 libc/src/__support/macros/sanitizer.h | 12 ++----------
 1 file changed, 2 insertions(+), 10 deletions(-)

diff --git a/libc/src/__support/macros/sanitizer.h b/libc/src/__support/macros/sanitizer.h
index 9a2fe8c4bcc14d..baf44f7996cabb 100644
--- a/libc/src/__support/macros/sanitizer.h
+++ b/libc/src/__support/macros/sanitizer.h
@@ -48,20 +48,12 @@
 //-----------------------------------------------------------------------------
 
 #if defined(LIBC_HAVE_MEMORY_SANITIZER)
-// Only perform MSAN unpoison in non-constexpr context and silence
-// '-Wconstant-evaluated' when MSAN_UNPOISON is called from manifestly constant
-// contexts.
+// Only perform MSAN unpoison in non-constexpr context.
 #include <sanitizer/msan_interface.h>
 #define MSAN_UNPOISON(addr, size)                                              \
   do {                                                                         \
-    _Pragma("GCC diagnostic push \"-Wconstant-evaluated\"");                   \
-    _Pragma("GCC diagnostic ignored \"-Wconstant-evaluated\"");                \
-    _Pragma("clang diagnostic push \"-Wconstant-evaluated\"");                 \
-    _Pragma("clang diagnostic ignored \"-Wconstant-evaluated\"");              \
-    if constexpr (!__builtin_is_constant_evaluated())                          \
+    if (!__builtin_is_constant_evaluated())                                    \
       __msan_unpoison(addr, size);                                             \
-    _Pragma("clang diagnostic pop");                                           \
-    _Pragma("GCC diagnostic pop");                                             \
   } while (0)
 #else
 #define MSAN_UNPOISON(ptr, size)



More information about the libc-commits mailing list