Use heap allocation + valgrind in backend unit test#1633
Merged
mkannwischer merged 4 commits intomainfrom Apr 19, 2026
Merged
Conversation
Contributor
hanno-becker
commented
Mar 19, 2026
- Resolves Use heap allocation + valgrind in backend unit test #1364
88001da to
d97ce09
Compare
Contributor
CBMC Results (ML-KEM-512)Full Results (191 proofs)
|
Contributor
CBMC Results (ML-KEM-1024)Full Results (191 proofs)
|
Contributor
CBMC Results (ML-KEM-768)Full Results (191 proofs)
|
79a002b to
72e929a
Compare
6ffbf41 to
15fb7d5
Compare
15fb7d5 to
b1dc236
Compare
34c12f9 to
3401242
Compare
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>
Replace all stack-allocated buffers in test_unit.c with heap allocations via MLK_ALLOC/MLK_FREE, using the custom_heap_alloc_config. This enables valgrind to detect buffer overflows in assembly backends, which operate on these buffers. Build the unit test objects with custom_heap_alloc_config.h by adding the appropriate -DMLK_CONFIG_FILE, -std=c11, and -D_GNU_SOURCE flags in components.mk. Signed-off-by: Hanno Becker <beckphan@amazon.co.uk>
Add native-vs-C consistency tests for previously untested backends:
- mlk_rej_uniform_native: compare against mlk_rej_uniform_c
- mlk_poly_compress_d{4,5,10,11}_native: compare against C reference
- mlk_poly_decompress_d{4,5,10,11}_native: compare against C reference
These tests call the assembly backends directly with heap-allocated
buffers, enabling valgrind to detect buffer overflows. In particular,
the rej_uniform test would have caught the 4-byte overread in the
AVX2 rejection sampling fixed in commit f10b801.
Previous ad-hoc tests for detecting overflow in (de)compression
routines are now subsumed by the unit tests, and removed.
Signed-off-by: Hanno Becker <beckphan@amazon.co.uk>
Add unit_valgrind job to ci.yml that runs the unit tests under valgrind on x86_64 and aarch64 runners. This catches buffer overflows in hand-written assembly that ASan cannot detect, since ASan only instruments compiler-generated code. Signed-off-by: Hanno Becker <beckphan@amazon.co.uk>
3401242 to
a74b3c4
Compare
mkannwischer
approved these changes
Apr 19, 2026
Contributor
mkannwischer
left a comment
There was a problem hiding this comment.
Thanks @hanno-becker!
For future reference, this is how it looks like when a memory-overread is detected:
INFO > Unit Test ML-KEM-512 (native opt): EXEC_WRAPPER=valgrind --error-exitcode=1 make run_unit_512 -j4
ERROR > Unit Test ML-KEM-512 (native opt): 'EXEC_WRAPPER=valgrind --error-exitcode=1 make run_unit_512 -j4' failed with with 2
ERROR > Unit Test ML-KEM-512 (native opt): ==25679== Memcheck, a memory error detector
==25679== Copyright (C) 2002-2024, and GNU GPL'd, by Julian Seward et al.
==25679== Using Valgrind-3.26.0 and LibVEX; rerun with -h for copyright info
==25679== Command: test/build/mlkem512/bin/test_unit512
==25679==
==25679== Invalid read of size 16
==25679== at 0x40088FB: ??? (in /home/runner/work/mlkem-native/mlkem-native/test/build/mlkem512/bin/test_unit512)
==25679== by 0x4001649: main (in /home/runner/work/mlkem-native/mlkem-native/test/build/mlkem512/bin/test_unit512)
==25679== Address 0x59000cc is 492 bytes inside a block of size 504 alloc'd
==25679== at 0x486035B: posix_memalign (in /nix/store/9i082ffnccn11i398mfcpmgwg2hxzl1m-valgrind-3.26.0/libexec/valgrind/vgpreload_memcheck-amd64-linux.so)
==25679== by 0x40021CB: mlk_posix_memalign.constprop.0 (in /home/runner/work/mlkem-native/mlkem-native/test/build/mlkem512/bin/test_unit512)
==25679== by 0x4001601: main (in /home/runner/work/mlkem-native/mlkem-native/test/build/mlkem512/bin/test_unit512)
==25679==
==25679==
==25679== HEAP SUMMARY:
==25679== in use at exit: 0 bytes in 0 blocks
==25679== total heap usage: 307,223 allocs, 307,223 frees, 171,631,592 bytes allocated
==25679==
==25679== All heap blocks were freed -- no leaks are possible
==25679==
==25679== For lists of detected and suppressed errors, rerun with: -s
==25679== ERROR SUMMARY: 509 errors from 1 contexts (suppressed: 0 from 0)
make: *** [Makefile:75: run_unit_512] Error 1
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.