[libc-commits] [libc] ba38640 - [libc] Added checks to src and dest types in bit_cast
Mikhail R. Gadelha via libc-commits
libc-commits at lists.llvm.org
Thu Apr 20 16:38:33 PDT 2023
Author: Mikhail R. Gadelha
Date: 2023-04-20T20:31:29-03:00
New Revision: ba38640b9901d239e32e12c6569f7364d00af922
URL: https://github.com/llvm/llvm-project/commit/ba38640b9901d239e32e12c6569f7364d00af922
DIFF: https://github.com/llvm/llvm-project/commit/ba38640b9901d239e32e12c6569f7364d00af922.diff
LOG: [libc] Added checks to src and dest types in bit_cast
This patch adds assertions to prevent the compilation when we try to
bit cast a type that is not trivially copyable when using
__builtin_bit_cast, or when we try to bit cast a type that is not
trivially copyable and trivially constructable when using memcpy.
Reviewed By: sivachandra
Differential Revision: https://reviews.llvm.org/D148739
Added:
Modified:
libc/src/__support/CPP/bit.h
libc/src/__support/CPP/type_traits.h
Removed:
################################################################################
diff --git a/libc/src/__support/CPP/bit.h b/libc/src/__support/CPP/bit.h
index 37248c77fe9d7..4241f2b52b543 100644
--- a/libc/src/__support/CPP/bit.h
+++ b/libc/src/__support/CPP/bit.h
@@ -9,6 +9,7 @@
#ifndef LLVM_LIBC_SUPPORT_CPP_BIT_H
#define LLVM_LIBC_SUPPORT_CPP_BIT_H
+#include "src/__support/CPP/type_traits.h"
#include "src/__support/macros/config.h" // LIBC_HAS_BUILTIN
namespace __llvm_libc::cpp {
@@ -25,9 +26,15 @@ namespace __llvm_libc::cpp {
// GCC >= 8 and Clang >= 6.
template <class To, class From> constexpr To bit_cast(const From &from) {
static_assert(sizeof(To) == sizeof(From), "To and From must be of same size");
+ static_assert(cpp::is_trivially_copyable<To>::value &&
+ cpp::is_trivially_copyable<From>::value,
+ "Cannot bit-cast instances of non-trivially copyable classes.");
#if defined(LLVM_LIBC_HAS_BUILTIN_BIT_CAST)
return __builtin_bit_cast(To, from);
#else
+ static_assert(cpp::is_trivially_constructible<To>::value,
+ "This implementation additionally requires destination type to "
+ "be trivially constructible");
To to;
char *dst = reinterpret_cast<char *>(&to);
const char *src = reinterpret_cast<const char *>(&from);
diff --git a/libc/src/__support/CPP/type_traits.h b/libc/src/__support/CPP/type_traits.h
index 7457bbefed67b..e5614021ca17a 100644
--- a/libc/src/__support/CPP/type_traits.h
+++ b/libc/src/__support/CPP/type_traits.h
@@ -12,9 +12,7 @@
namespace __llvm_libc {
namespace cpp {
-template <typename T> struct type_identity {
- using type = T;
-};
+template <typename T> struct type_identity { using type = T; };
template <bool B, typename T> struct enable_if;
template <typename T> struct enable_if<true, T> : type_identity<T> {};
@@ -28,6 +26,14 @@ template <typename T, T v> struct integral_constant {
using true_type = cpp::integral_constant<bool, true>;
using false_type = cpp::integral_constant<bool, false>;
+template <class T>
+struct is_trivially_copyable
+ : public integral_constant<bool, __is_trivially_copyable(T)> {};
+
+template <class T, class... Args>
+struct is_trivially_constructible
+ : integral_constant<bool, __is_trivially_constructible(T, Args...)> {};
+
template <typename T, typename U> struct is_same : cpp::false_type {};
template <typename T> struct is_same<T, T> : cpp::true_type {};
template <typename T, typename U>
More information about the libc-commits
mailing list