Skip to content

Commit cc61de2

Browse files
hanno-beckermkannwischer
authored andcommitted
Use posix_memalign in custom heap allocation config
Replace aligned_alloc + MLK_ALIGN_UP with posix_memalign in custom_heap_alloc_config.h. Unlike aligned_alloc, posix_memalign does not require the size to be a multiple of the alignment, removing the need for MLK_ALIGN_UP rounding. This ensures that allocations are exact-sized, allowing memory-safety tests like valgrind and ASan to detect overflows at precise buffer boundaries. On Windows, where posix_memalign is not available, we use _aligned_malloc instead. This, too, does not require the size to be a multiple of the alignment. Signed-off-by: Hanno Becker <beckphan@amazon.co.uk>
1 parent 13aa41a commit cc61de2

2 files changed

Lines changed: 39 additions & 5 deletions

File tree

test/configs/configs.yml

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -397,13 +397,29 @@ configs:
397397
defines:
398398
MLK_CONFIG_CUSTOM_ALLOC_FREE:
399399
content: |
400+
/* In practice, one could just use aligned_alloc here. However, this
401+
* requires aligning up the size to a multiple of the alignment, which
402+
* weakens some of the memory-safety tests we run using this config. */
400403
#define MLK_CONFIG_CUSTOM_ALLOC_FREE
401404
#if !defined(__ASSEMBLER__)
405+
#if defined(_WIN32)
406+
#include <malloc.h>
407+
#define MLK_CUSTOM_ALLOC(v, T, N) \
408+
T *v = (T *)_aligned_malloc(sizeof(T) * (N), MLK_DEFAULT_ALIGN)
409+
#define MLK_CUSTOM_FREE(v, T, N) _aligned_free(v)
410+
#else
402411
#include <stdlib.h>
403-
#define MLK_CUSTOM_ALLOC(v, T, N) \
404-
T* v = (T *)aligned_alloc(MLK_DEFAULT_ALIGN, \
405-
MLK_ALIGN_UP(sizeof(T) * (N)))
412+
static inline void *mlk_posix_memalign(size_t align, size_t sz)
413+
{
414+
void *ptr = NULL;
415+
if (posix_memalign(&ptr, align, sz) != 0)
416+
return NULL;
417+
return ptr;
418+
}
419+
#define MLK_CUSTOM_ALLOC(v, T, N) \
420+
T *v = (T *)mlk_posix_memalign(MLK_DEFAULT_ALIGN, sizeof(T) * (N))
406421
#define MLK_CUSTOM_FREE(v, T, N) free(v)
422+
#endif /* _WIN32 */
407423
#endif /* !__ASSEMBLER__ */
408424
409425
- path: examples/basic_deterministic/mlkem_native/mlkem_native_config.h
@@ -449,4 +465,3 @@ configs:
449465
#endif /* !__ASSEMBLER__ */
450466
MLK_CONFIG_FILE:
451467
comment: "/* No need to set this -- we _are_ already in a custom config */"
452-

test/configs/custom_heap_alloc_config.h

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -498,12 +498,31 @@
498498
* code will handle this case and invoke MLK_CUSTOM_FREE.
499499
*
500500
*****************************************************************************/
501+
/* In practice, one could just use aligned_alloc here. However, this
502+
* requires aligning up the size to a multiple of the alignment, which
503+
* weakens some of the memory-safety tests we run using this config. */
501504
#define MLK_CONFIG_CUSTOM_ALLOC_FREE
502505
#if !defined(__ASSEMBLER__)
506+
#if defined(_WIN32)
507+
#include <malloc.h>
508+
#define MLK_CUSTOM_ALLOC(v, T, N) \
509+
T *v = (T *)_aligned_malloc(sizeof(T) * (N), MLK_DEFAULT_ALIGN)
510+
#define MLK_CUSTOM_FREE(v, T, N) _aligned_free(v)
511+
#else /* _WIN32 */
503512
#include <stdlib.h>
513+
static inline void *mlk_posix_memalign(size_t align, size_t sz)
514+
{
515+
void *ptr = NULL;
516+
if (posix_memalign(&ptr, align, sz) != 0)
517+
{
518+
return NULL;
519+
}
520+
return ptr;
521+
}
504522
#define MLK_CUSTOM_ALLOC(v, T, N) \
505-
T *v = (T *)aligned_alloc(MLK_DEFAULT_ALIGN, MLK_ALIGN_UP(sizeof(T) * (N)))
523+
T *v = (T *)mlk_posix_memalign(MLK_DEFAULT_ALIGN, sizeof(T) * (N))
506524
#define MLK_CUSTOM_FREE(v, T, N) free(v)
525+
#endif /* !_WIN32 */
507526
#endif /* !__ASSEMBLER__ */
508527

509528

0 commit comments

Comments
 (0)