Skip to content

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

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged

Conversation

gchatelet
Copy link
Contributor

@gchatelet gchatelet commented Apr 15, 2024

Prior to this patch, calling cpp::bit_cast<T> in constexpr expressions under -fsanitize=memory would fail with the following message "non-constexpr function '__msan_unpoison' cannot be used in a constant expression".

This patch makes sure that the __msan_unpoison expression is guarded by !__builtin_is_constant_evaluated().

@llvmbot
Copy link
Member

llvmbot commented Apr 15, 2024

@llvm/pr-subscribers-libc

Author: Guillaume Chatelet (gchatelet)

Changes

Full diff: https://github.com/llvm/llvm-project/pull/88719.diff

1 Files Affected:

  • (modified) libc/src/__support/macros/sanitizer.h (+11-4)
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)

Copy link
Member

@nickdesaulniers nickdesaulniers left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@ramosian-glider should __msan_unpoison be callable from constexpr contexts?

@gchatelet
Copy link
Contributor Author

Thx for challenging my patch 🙏👍

Copy link
Member

@nickdesaulniers nickdesaulniers left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the fix!

@gchatelet gchatelet merged commit 889dfd4 into llvm:main Apr 17, 2024
@gchatelet gchatelet deleted the fix_msan_poison_called_in_constexpr_context branch April 17, 2024 08:04
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants