Skip to content
Open
Show file tree
Hide file tree
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
30 changes: 30 additions & 0 deletions kernels/optimized/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,36 @@ if(NOT EXECUTORCH_ROOT)
set(EXECUTORCH_ROOT ${CMAKE_CURRENT_SOURCE_DIR}/../..)
endif()

# Apply pocketfft patch for ASAN-compatible aligned_alloc on POSIX systems. The
# portable aligned_alloc in pocketfft stores metadata at ptr[-1], which
# conflicts with ASAN's heap redzone and causes intermittent bus errors. This
# patch uses posix_memalign instead, which is ASAN-compatible.
set(POCKETFFT_DIR "${EXECUTORCH_ROOT}/third-party/pocketfft")
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should we land this upstream instead of having this patch, changing code on the fly?
cc @mergennachin

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, let's try upstreaming to https://github.com/mreineck/pocketfft

Copy link
Contributor Author

@metascroy metascroy Feb 24, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've put up a PR here: mreineck/pocketfft#28, but should it not land in pocketfft for some reason, should we not patch in ET given we're distributing the buggy software?

set(POCKETFFT_PATCH
"${CMAKE_CURRENT_SOURCE_DIR}/patches/pocketfft_aligned_alloc.patch"
)
if(EXISTS "${POCKETFFT_PATCH}" AND EXISTS "${POCKETFFT_DIR}")
execute_process(
COMMAND git apply --check ${POCKETFFT_PATCH}
WORKING_DIRECTORY ${POCKETFFT_DIR}
RESULT_VARIABLE PATCH_CHECK_RESULT
ERROR_QUIET
)
if(PATCH_CHECK_RESULT EQUAL 0)
message(STATUS "Applying pocketfft aligned_alloc patch...")
execute_process(
COMMAND git apply ${POCKETFFT_PATCH}
WORKING_DIRECTORY ${POCKETFFT_DIR}
RESULT_VARIABLE PATCH_RESULT
)
if(NOT PATCH_RESULT EQUAL 0)
message(WARNING "Failed to apply pocketfft patch")
endif()
else()
message(STATUS "pocketfft patch already applied or not applicable")
endif()
endif()

set(_common_compile_options
$<$<CXX_COMPILER_ID:MSVC>:/wd4996>
$<$<NOT:$<CXX_COMPILER_ID:MSVC>>:-Wno-deprecated-declarations>
Expand Down
36 changes: 36 additions & 0 deletions kernels/optimized/patches/pocketfft_aligned_alloc.patch
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
diff --git a/pocketfft_hdronly.h b/pocketfft_hdronly.h
index 57db7d7..e831ac0 100644
--- a/pocketfft_hdronly.h
+++ b/pocketfft_hdronly.h
@@ -162,8 +162,20 @@ template<> struct VLEN<double> { static constexpr size_t val=2; };
// std::aligned_alloc is a bit cursed ... it doesn't exist on MacOS < 10.15
// and in musl, and other OSes seem to have even more peculiarities.
// Let's unconditionally work around it for now.
-# if 0
-//#if (__cplusplus >= 201703L) && (!defined(__MINGW32__)) && (!defined(_MSC_VER)) && (__MAC_OS_X_VERSION_MIN_REQUIRED >= MAC_OS_X_VERSION_10_15)
+#if defined(__APPLE__) || defined(__unix__)
+// Use posix_memalign on POSIX systems - it is ASAN-compatible.
+// The portable aligned_alloc below stores metadata at ptr[-1], which conflicts
+// with ASAN's heap redzone and causes intermittent bus errors.
+inline void *aligned_alloc(size_t align, size_t size)
+ {
+ void *ptr = nullptr;
+ if (posix_memalign(&ptr, align, size) != 0)
+ throw std::bad_alloc();
+ return ptr;
+ }
+inline void aligned_dealloc(void *ptr)
+ { free(ptr); }
+#elif (__cplusplus >= 201703L) && (!defined(__MINGW32__)) && (!defined(_MSC_VER))
inline void *aligned_alloc(size_t align, size_t size)
{
// aligned_alloc() requires that the requested size is a multiple of "align"
@@ -173,7 +185,7 @@ inline void *aligned_alloc(size_t align, size_t size)
}
inline void aligned_dealloc(void *ptr)
{ free(ptr); }
-#else // portable emulation
+#else // portable emulation (NOT ASAN-compatible - stores metadata at ptr[-1])
inline void *aligned_alloc(size_t align, size_t size)
{
align = std::max(align, alignof(max_align_t));
Loading