Skip to content

[SYCL] Fix potential misaligned allocation size #18375

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
merged 2 commits into from
May 9, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 12 additions & 7 deletions sycl/source/detail/device_binary_image.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -594,20 +594,25 @@ DynRTDeviceBinaryImage::DynRTDeviceBinaryImage(
const size_t PropertySectionSize = PropertyCount * PaddedPropertyByteSize;

// Allocate the memory aligned to the property entry alignment.
#ifdef _MSC_VER
// Note: MSVC does not implement std::aligned_alloc.
Data = std::unique_ptr<char[], std::function<void(void *)>>(
#ifdef _MSC_VER
static_cast<char *>(_aligned_malloc(sizeof(char) * PropertySectionSize +
PropertyContentByteSize,
PropertyAlignment)),
_aligned_free
_aligned_free);
#else
static_cast<char *>(std::aligned_alloc(
PropertyAlignment,
sizeof(char) * PropertySectionSize + PropertyContentByteSize)),
std::free
// std::aligned_alloc requires the allocation size to be a multiple of the
// alignment, so we may over-allocate a little.
const size_t AllocSize =
sizeof(char) * PropertySectionSize + PropertyContentByteSize;
const size_t AlignedAllocSize = (AllocSize + PropertyAlignment - 1) /
PropertyAlignment * PropertyAlignment;
Data = std::unique_ptr<char[], std::function<void(void *)>>(
static_cast<char *>(
std::aligned_alloc(PropertyAlignment, AlignedAllocSize)),
std::free);
#endif
);

auto NextFreeProperty =
reinterpret_cast<sycl_device_binary_property>(Data.get());
Expand Down