Skip to content

Commit 2654ddc

Browse files
lucylqfacebook-github-bot
authored andcommitted
Use std::align_alloc in file_data_loader (pytorch#10660)
Summary: Issue with aligned buffers: P1800967583 The alignment requested is 16, and std::max_align_t is also 16. This means we do not need to pad the size to meet any alignment. However, the buffer we get from malloc is aligned to 8, not 16. When we try to align the buffer, we overflow and error out. Seems like malloc is not guaranteed to return 8 or 16 byte-aligned buffers, so also a bit hard to test definitively. So far we've only seen this when the buffer size is small (size 2, 4) ``` The malloc(), calloc(), realloc(), and reallocarray() functions return a pointer to the allocated memory, which is suitably aligned for any type that fits into the requested size or less. ``` Use std::aligned_alloc (C++17) to ensure buffer is aligned. Reviewed By: larryliu0820 Differential Revision: D74041198
1 parent 4129ebe commit 2654ddc

File tree

2 files changed

+51
-57
lines changed

2 files changed

+51
-57
lines changed

extension/data_loader/file_data_loader.cpp

+13-57
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121

2222
#include <executorch/runtime/core/error.h>
2323
#include <executorch/runtime/core/result.h>
24+
#include <executorch/runtime/platform/compiler.h>
2425
#include <executorch/runtime/platform/log.h>
2526

2627
// Some platforms (e.g. Xtensa) do not support pread() that we use to read the
@@ -49,20 +50,6 @@ namespace {
4950
static bool is_power_of_2(size_t value) {
5051
return value > 0 && (value & ~(value - 1)) == value;
5152
}
52-
53-
/**
54-
* Returns the next alignment for a given pointer.
55-
*/
56-
static uint8_t* align_pointer(void* ptr, size_t alignment) {
57-
intptr_t addr = reinterpret_cast<intptr_t>(ptr);
58-
if ((addr & (alignment - 1)) == 0) {
59-
// Already aligned.
60-
return reinterpret_cast<uint8_t*>(ptr);
61-
}
62-
// Bump forward.
63-
addr = (addr | (alignment - 1)) + 1;
64-
return reinterpret_cast<uint8_t*>(addr);
65-
}
6653
} // namespace
6754

6855
FileDataLoader::~FileDataLoader() {
@@ -129,13 +116,13 @@ namespace {
129116
/**
130117
* FreeableBuffer::FreeFn-compatible callback.
131118
*
132-
* `context` is actually a ptrdiff_t value (not a pointer) that contains the
133-
* offset in bytes between `data` and the actual pointer to free.
119+
* `context` is the original buffer pointer. It is allocated with
120+
* ET_ALIGNED_ALLOC, and must be freed with ET_ALIGNED_FREE.
121+
*
122+
* `data` and `size` are unused.
134123
*/
135124
void FreeSegment(void* context, void* data, ET_UNUSED size_t size) {
136-
ptrdiff_t offset = reinterpret_cast<ptrdiff_t>(context);
137-
ET_DCHECK_MSG(offset >= 0, "Unexpected offset %ld", (long int)offset);
138-
std::free(static_cast<uint8_t*>(data) - offset);
125+
ET_ALIGNED_FREE(context);
139126
}
140127
} // namespace
141128

@@ -163,57 +150,26 @@ Result<FreeableBuffer> FileDataLoader::load(
163150
}
164151

165152
// Allocate memory for the FreeableBuffer.
166-
size_t alloc_size = size;
167-
if (alignment_ > alignof(std::max_align_t)) {
168-
// malloc() will align to smaller values, but we must manually align to
169-
// larger values.
170-
alloc_size += alignment_;
171-
}
172-
void* buffer = std::malloc(alloc_size);
173-
if (buffer == nullptr) {
153+
void* aligned_buffer = ET_ALIGNED_ALLOC(alignment_, size);
154+
if (aligned_buffer == nullptr) {
174155
ET_LOG(
175156
Error,
176-
"Reading from %s at offset %zu: malloc(%zd) failed",
157+
"Reading from %s at offset %zu: ET_ALIGNED_ALLOC(%zd, %zd) failed",
177158
file_name_,
178159
offset,
160+
alignment_,
179161
size);
180162
return Error::MemoryAllocationFailed;
181163
}
182164

183-
// Align.
184-
void* aligned_buffer = align_pointer(buffer, alignment_);
185-
186-
// Assert that the alignment didn't overflow the buffer.
187-
ET_DCHECK_MSG(
188-
reinterpret_cast<uintptr_t>(aligned_buffer) + size <=
189-
reinterpret_cast<uintptr_t>(buffer) + alloc_size,
190-
"aligned_buffer %p + size %zu > buffer %p + alloc_size %zu",
191-
aligned_buffer,
192-
size,
193-
buffer,
194-
alloc_size);
195-
196165
auto err = load_into(offset, size, segment_info, aligned_buffer);
197166
if (err != Error::Ok) {
198-
// Free `buffer`, which is what malloc() gave us, not `aligned_buffer`.
199-
std::free(buffer);
167+
ET_ALIGNED_FREE(aligned_buffer);
200168
return err;
201169
}
202170

203-
// We can't naively free this pointer, since it may not be what malloc() gave
204-
// us. Pass the offset to the real buffer as context. This is the number of
205-
// bytes that need to be subtracted from the FreeableBuffer::data() pointer to
206-
// find the actual pointer to free.
207-
return FreeableBuffer(
208-
aligned_buffer,
209-
size,
210-
FreeSegment,
211-
/*free_fn_context=*/
212-
reinterpret_cast<void*>(
213-
// Using signed types here because it will produce a signed ptrdiff_t
214-
// value, though for us it will always be non-negative.
215-
reinterpret_cast<intptr_t>(aligned_buffer) -
216-
reinterpret_cast<intptr_t>(buffer)));
171+
// Pass the aligned_buffer pointer as context to FreeSegment.
172+
return FreeableBuffer(aligned_buffer, size, FreeSegment, aligned_buffer);
217173
}
218174

219175
Result<size_t> FileDataLoader::size() const {

runtime/platform/compiler.h

+38
Original file line numberDiff line numberDiff line change
@@ -171,6 +171,44 @@
171171
using ssize_t = ptrdiff_t;
172172
#endif
173173

174+
/**
175+
* Platform-specific aligned memory allocation and deallocation.
176+
*
177+
* Usage:
178+
* void* ptr = ET_ALIGNED_ALLOC(alignment, size);
179+
* // use ptr...
180+
* ET_ALIGNED_FREE(ptr);
181+
*
182+
* Note: alignment must be a power of 2 and size must be an integral multiple of
183+
* alignment.
184+
*/
185+
#if defined(_MSC_VER)
186+
#include <malloc.h>
187+
#define ET_ALIGNED_ALLOC(alignment, size) \
188+
_aligned_malloc(((size + alignment - 1) & ~(alignment - 1)), (alignment))
189+
#define ET_ALIGNED_FREE(ptr) _aligned_free(ptr)
190+
#elif defined(__APPLE__)
191+
#include <stdlib.h> // For posix_memalign and free
192+
inline void* et_apple_aligned_alloc(size_t alignment, size_t size) {
193+
void* ptr = nullptr;
194+
// The address of the allocated memory must be a multiple of sizeof(void*).
195+
alignment = std::max(alignment, sizeof(void*));
196+
if (posix_memalign(
197+
&ptr, alignment, (size + alignment - 1) & ~(alignment - 1)) != 0) {
198+
return nullptr;
199+
}
200+
return ptr;
201+
}
202+
#define ET_ALIGNED_ALLOC(alignment, size) \
203+
et_apple_aligned_alloc((alignment), (size))
204+
#define ET_ALIGNED_FREE(ptr) free(ptr)
205+
#else
206+
// Linux and other posix systems.
207+
#define ET_ALIGNED_ALLOC(alignment, size) \
208+
::aligned_alloc(alignment, (size + alignment - 1) & ~(alignment - 1))
209+
#define ET_ALIGNED_FREE(ptr) free(ptr)
210+
#endif
211+
174212
// DEPRECATED: Use the non-underscore-prefixed versions instead.
175213
// TODO(T199005537): Remove these once all users have stopped using them.
176214
#define __ET_DEPRECATED ET_DEPRECATED

0 commit comments

Comments
 (0)