[libcxx-commits] [libcxx] [libc++] Diagnoses insufficiently aligned pointers for std::assume_aligned during constant evaluation (PR #73775)

Rajveer Singh Bharadwaj via libcxx-commits libcxx-commits at lists.llvm.org
Sun Jan 21 05:28:12 PST 2024


https://github.com/Rajveer100 updated https://github.com/llvm/llvm-project/pull/73775

>From e17563810b68240fb4389638389c858e508a4dda Mon Sep 17 00:00:00 2001
From: Rajveer <rajveer.developer at icloud.com>
Date: Wed, 29 Nov 2023 15:55:25 +0530
Subject: [PATCH] [libc++] Diagnoses insufficiently aligned pointers for
 std::assume_aligned during constant evaluation

Resolves Issue #64078

This is a libc++ enhancement when violating alignment assumption for __builtin_assume_aligned.
---
 libcxx/include/__memory/assume_aligned.h      |  1 +
 .../assume_aligned.const_eval.verify.cpp      | 38 +++++++++++++++++++
 2 files changed, 39 insertions(+)
 create mode 100644 libcxx/test/libcxx/utilities/memory/ptr.align/assume_aligned.const_eval.verify.cpp

diff --git a/libcxx/include/__memory/assume_aligned.h b/libcxx/include/__memory/assume_aligned.h
index c66fb49ebb3c01..5b3686d25ec248 100644
--- a/libcxx/include/__memory/assume_aligned.h
+++ b/libcxx/include/__memory/assume_aligned.h
@@ -27,6 +27,7 @@ _LIBCPP_NODISCARD _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX14 _Tp* __ass
   static_assert(_Np != 0 && (_Np & (_Np - 1)) == 0, "std::assume_aligned<N>(p) requires N to be a power of two");
 
   if (__libcpp_is_constant_evaluated()) {
+    (void)__builtin_assume_aligned(__ptr, _Np);
     return __ptr;
   } else {
     _LIBCPP_ASSERT_UNCATEGORIZED(reinterpret_cast<uintptr_t>(__ptr) % _Np == 0, "Alignment assumption is violated");
diff --git a/libcxx/test/libcxx/utilities/memory/ptr.align/assume_aligned.const_eval.verify.cpp b/libcxx/test/libcxx/utilities/memory/ptr.align/assume_aligned.const_eval.verify.cpp
new file mode 100644
index 00000000000000..51ba6688ed29b1
--- /dev/null
+++ b/libcxx/test/libcxx/utilities/memory/ptr.align/assume_aligned.const_eval.verify.cpp
@@ -0,0 +1,38 @@
+//===----------------------------------------------------------------------===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+
+#include <memory>
+#include <cstddef>
+
+template <size_t Size>
+constexpr bool test() {
+  char data[1];
+
+  auto data1 = std::assume_aligned<Size>(data);
+  delete data1;
+
+  return true;
+}
+
+static_assert(test<2>(), "asserted 2 bytes."); // expected-error {{static assertion expression is not an integral constant expression}}
+// expected-note@* {{alignment of the base pointee object (1 byte) is less than the asserted 2 bytes}}
+
+static_assert(test<4>(), "asserted 4 bytes."); // expected-error {{static assertion expression is not an integral constant expression}}
+// expected-note@* {{alignment of the base pointee object (1 byte) is less than the asserted 4 bytes}}
+
+static_assert(test<8>(), "asserted 8 bytes."); // expected-error {{static assertion expression is not an integral constant expression}}
+// expected-note@* {{alignment of the base pointee object (1 byte) is less than the asserted 8 bytes}}
+
+static_assert(test<16>(), "asserted 16 bytes."); // expected-error {{static assertion expression is not an integral constant expression}}
+// expected-note@* {{alignment of the base pointee object (1 byte) is less than the asserted 16 bytes}}
+
+static_assert(test<32>(), "asserted 32 bytes."); // expected-error {{static assertion expression is not an integral constant expression}}
+// expected-note@* {{alignment of the base pointee object (1 byte) is less than the asserted 32 bytes}}
+
+static_assert(test<64>(), "asserted 64 bytes."); // expected-error {{static assertion expression is not an integral constant expression}}
+// expected-note@* {{alignment of the base pointee object (1 byte) is less than the asserted 64 bytes}}



More information about the libcxx-commits mailing list