Skip to content

OpenVINO™ is an open source toolkit for optimizing and deploying AI inference

[core] Fix aligned_alloc for alignments below sizeof(void*) (#38338)

Pranav Singh92880ef59ef8
diff --git a/src/common/util/include/openvino/util/memory.hpp b/src/common/util/include/openvino/util/memory.hpp
index 21e3dc9255a..7b8fa80ac53 100644
--- a/src/common/util/include/openvino/util/memory.hpp
+++ b/src/common/util/include/openvino/util/memory.hpp
@@ -7,6 +7,7 @@
#include <cstddef>
#include <cstdint>
#include <cstdlib>
+#include <limits>
#include <memory>
#include <optional>
#include <string>
@@ -31,6 +32,19 @@ constexpr size_t align_size_up(size_t size, size_t alignment) noexcept {
return (size + alignment - 1) & ~(alignment - 1);
}
+/**
+ * @brief Rounds @p size up to the nearest multiple of @p alignment, detecting overflow.
+ *
+ * @param size Value to round up.
+ * @param alignment Alignment boundary. Must be a power of two and greater than zero.
+ * @return Smallest value >= @p size that is a multiple of @p alignment, or `std::nullopt` if it does not fit in size_t.
+ */
+constexpr std::optional<size_t> align_size_up_overflow(size_t size, size_t alignment) noexcept {
+ return (size > std::numeric_limits<size_t>::max() - (alignment - 1))
+ ? std::nullopt
+ : std::optional<size_t>{align_size_up(size, alignment)};
+}
+
/**
* @brief Rounds @p size down to the nearest multiple of @p alignment.
*
@@ -71,8 +85,8 @@ constexpr AlignedRegion align_region(uintptr_t base, size_t raw_len, size_t alig
*
*
* @param size Number of bytes to allocate. Must be greater than zero.
- * @param alignment Desired alignment in bytes. Must be a power of two.
- * Passing `0` applies no specific alignment constraint (`alignof(std::max_align_t)` is used).
+ * @param alignment Desired alignment in bytes. Must be `0` or a power of two.
+ * If it is less than `alignof(std::max_align_t)`, `alignof(std::max_align_t)` is used.
* @return Pointer to the allocated memory, or `nullptr` on failure.
*/
void* aligned_alloc(size_t size, size_t alignment) noexcept;
diff --git a/src/common/util/src/os/lin/lin_memory.cpp b/src/common/util/src/os/lin/lin_memory.cpp
index ff1dd7b5bc0..1483b02f5f9 100644
--- a/src/common/util/src/os/lin/lin_memory.cpp
+++ b/src/common/util/src/os/lin/lin_memory.cpp
@@ -27,10 +27,13 @@ void madvise_hint(void* ptr, size_t size) noexcept {
} // namespace
void* aligned_alloc(size_t size, size_t alignment) noexcept {
- if (alignment == 0) {
+ // Some std::aligned_alloc implementations (for example on macOS) return nullptr for
+ // alignments smaller than sizeof(void*), so small alignments are raised as documented.
+ if (alignment < alignof(std::max_align_t)) {
alignment = alignof(std::max_align_t);
}
- return std::aligned_alloc(alignment, align_size_up(size, alignment));
+ const auto aligned_size = align_size_up_overflow(size, alignment);
+ return aligned_size ? std::aligned_alloc(alignment, *aligned_size) : nullptr;
}
void aligned_free(void* ptr) noexcept {
diff --git a/src/common/util/src/os/win/win_memory.cpp b/src/common/util/src/os/win/win_memory.cpp
index f898114fde5..b0640b41874 100644
--- a/src/common/util/src/os/win/win_memory.cpp
+++ b/src/common/util/src/os/win/win_memory.cpp
@@ -20,10 +20,11 @@
namespace ov::util {
void* aligned_alloc(size_t size, size_t alignment) noexcept {
- if (alignment == 0) {
+ if (alignment < alignof(std::max_align_t)) {
alignment = alignof(std::max_align_t);
}
- return _aligned_malloc(size, alignment);
+ const auto aligned_size = align_size_up_overflow(size, alignment);
+ return aligned_size ? _aligned_malloc(*aligned_size, alignment) : nullptr;
}
void aligned_free(void* ptr) noexcept {
diff --git a/src/tests/test_utils/common_test_utils/tests/memory_test.cpp b/src/tests/test_utils/common_test_utils/tests/memory_test.cpp
index 00dbb4762f2..520ef3c7241 100644
--- a/src/tests/test_utils/common_test_utils/tests/memory_test.cpp
+++ b/src/tests/test_utils/common_test_utils/tests/memory_test.cpp
@@ -7,6 +7,7 @@
#include <cstdint>
#include <filesystem>
+#include <limits>
#include <system_error>
#include <vector>
@@ -30,6 +31,15 @@ static_assert(ov::util::align_size_up(8, 8) == 8);
static_assert(ov::util::align_size_up(9, 8) == 16);
static_assert(ov::util::align_size_up(9, alignof(std::max_align_t)) == 16);
+static_assert(ov::util::align_size_up_overflow(0, 64) == 0);
+static_assert(ov::util::align_size_up_overflow(65, 64) == 128);
+static_assert(ov::util::align_size_up_overflow(std::numeric_limits<size_t>::max(), 1) ==
+ std::numeric_limits<size_t>::max());
+static_assert(!ov::util::align_size_up_overflow(std::numeric_limits<size_t>::max() - 1, 8).has_value());
+static_assert(!ov::util::align_size_up_overflow(std::numeric_limits<size_t>::max() - 62, 64).has_value());
+static_assert(ov::util::align_size_up_overflow(std::numeric_limits<size_t>::max() - 63, 64) ==
+ std::numeric_limits<size_t>::max() - 63);
+
static_assert(ov::util::align_size_down(0, 64) == 0);
static_assert(ov::util::align_size_down(63, 64) == 0);
static_assert(ov::util::align_size_down(64, 64) == 64);
@@ -151,6 +161,22 @@ TEST_F(AlignedAllocTest, zero_alignment_uses_default_alignment) {
util::aligned_free(ptr);
}
+TEST_F(AlignedAllocTest, small_alignment_uses_max_align_t) {
+ for (size_t align = 1; align < alignof(std::max_align_t); align <<= 1) {
+ void* ptr = util::aligned_alloc(3, align);
+ ASSERT_NE(nullptr, ptr) << "align=" << align;
+ EXPECT_EQ(0u, reinterpret_cast<uintptr_t>(ptr) % alignof(std::max_align_t)) << "align=" << align;
+ util::aligned_free(ptr);
+ }
+}
+
+TEST_F(AlignedAllocTest, size_too_big_for_alignment_returns_null) {
+ constexpr auto max_size = std::numeric_limits<size_t>::max();
+ for (auto align : {size_t{0}, size_t{1}, alignof(std::max_align_t), size_t{64}}) {
+ EXPECT_EQ(nullptr, util::aligned_alloc(max_size - 1, align)) << "align=" << align;
+ }
+}
+
TEST_F(AlignedAllocTest, free_nullptr_is_noop) {
EXPECT_NO_FATAL_FAILURE(util::aligned_free(nullptr));
}