Skip to content

Commit 2934fe0

Browse files
Tamir Dubersteinfacebook-github-bot
authored andcommitted
Use posix_memalign on all POSIX platforms
Summary: The feature detection seems to be incorrect, resulting in fallback to the manual implementation, which in turn returns untagged pointers under hwasan. Replace the feature detection with a check on `_POSIX_VERSION` and use `posix_memalign` which is available on quite old platforms as well. Remove rounding shenanigans while I'm here; the documentation on ET_ALIGNED_ALLOC already stipulates that the caller is responsible for such rounding. Differential Revision: D75806635
1 parent 1bc36c7 commit 2934fe0

File tree

2 files changed

+18
-19
lines changed

2 files changed

+18
-19
lines changed

extension/data_loader/file_descriptor_data_loader.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -164,9 +164,10 @@ Result<FreeableBuffer> FileDescriptorDataLoader::load(
164164
if (aligned_buffer == nullptr) {
165165
ET_LOG(
166166
Error,
167-
"Reading from %s at offset %zu: ET_ALIGNED_ALLOC(%zd) failed",
167+
"Reading from %s at offset %zu: ET_ALIGNED_ALLOC(%zd, %zd) failed",
168168
file_descriptor_uri_,
169169
offset,
170+
alignment_,
170171
size);
171172
return Error::MemoryAllocationFailed;
172173
}

runtime/platform/compiler.h

Lines changed: 16 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -183,36 +183,34 @@ using ssize_t = ptrdiff_t;
183183
* alignment.
184184
*/
185185
#if defined(_MSC_VER)
186-
#include <malloc.h>
187-
#define ET_ALIGNED_ALLOC(alignment, size) \
188-
_aligned_malloc(((size + alignment - 1) & ~(alignment - 1)), (alignment))
186+
#include <cstdlib> // For _aligned_malloc and _aligned_free
187+
#define ET_ALIGNED_ALLOC(alignment, size) _aligned_malloc((size), (alignment))
189188
#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) {
189+
#else
190+
#include <unistd.h> // For _POSIX_VERSION
191+
#if defined(_POSIX_VERSION) && (_POSIX_VERSION >= 200112L)
192+
#include <cstdlib> // For posix_memalign and free
193+
inline void* et_posix_aligned_alloc(size_t alignment, size_t size) {
193194
void* ptr = nullptr;
194195
// The address of the allocated memory must be a multiple of sizeof(void*).
195196
if (alignment < sizeof(void*)) {
196197
alignment = sizeof(void*);
197198
}
198-
if (posix_memalign(
199-
&ptr, alignment, (size + alignment - 1) & ~(alignment - 1)) != 0) {
199+
if (posix_memalign(&ptr, alignment, size) != 0) {
200200
return nullptr;
201201
}
202202
return ptr;
203203
}
204204
#define ET_ALIGNED_ALLOC(alignment, size) \
205-
et_apple_aligned_alloc((alignment), (size))
206-
#define ET_ALIGNED_FREE(ptr) free(ptr)
207-
#elif __has_builtin(__builtin_aligned_alloc) || defined(_ISOC11_SOURCE)
208-
// Linux and posix systems that support aligned_alloc and are >= C++17.
209-
#include <cstdlib>
210-
#define ET_ALIGNED_ALLOC(alignment, size) \
211-
::aligned_alloc(alignment, (size + alignment - 1) & ~(alignment - 1))
205+
et_posix_aligned_alloc((alignment), (size))
212206
#define ET_ALIGNED_FREE(ptr) free(ptr)
213207
#else
214-
// If the platform doesn't support aligned_alloc, fallback to malloc.
215-
#include <stdint.h>
208+
#if defined(__has_feature) && __has_feature(hwaddress_sanitizer)
209+
#error native aligned allocation is not available on this platform, and the fallback is not compatible with HWASAN.
210+
#endif
211+
212+
// If the platform doesn't support posix_memalign, fallback to malloc.
213+
#include <cstdint>
216214
#include <cstdlib>
217215
inline void* et_aligned_malloc(size_t alignment, size_t size) {
218216
// Place to store the offset to the original pointer.
@@ -263,7 +261,7 @@ inline void et_aligned_free(void* ptr) {
263261

264262
#define ET_ALIGNED_ALLOC(alignment, size) et_aligned_malloc((alignment), (size))
265263
#define ET_ALIGNED_FREE(ptr) et_aligned_free(ptr)
266-
264+
#endif
267265
#endif
268266

269267
// DEPRECATED: Use the non-underscore-prefixed versions instead.

0 commit comments

Comments
 (0)