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

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()`.
This commit is contained in:
Guillaume Chatelet 2024-04-17 10:04:22 +02:00 committed by GitHub
parent 9f3334e993
commit 889dfd4ab3
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -47,14 +47,13 @@
// Functions to unpoison memory
//-----------------------------------------------------------------------------
#if defined(LIBC_HAVE_MEMORY_SANITIZER) && __has_builtin(__builtin_constant_p)
#if defined(LIBC_HAVE_MEMORY_SANITIZER)
// Only perform MSAN unpoison in non-constexpr context.
#include <sanitizer/msan_interface.h>
#define MSAN_UNPOISON(addr, size) \
do { \
if (!__builtin_constant_p(*addr)) { \
if (!__builtin_is_constant_evaluated()) \
__msan_unpoison(addr, size); \
} \
} while (0)
#else
#define MSAN_UNPOISON(ptr, size)