Use heap allocation + valgrind in backend unit test#1633
Draft
hanno-becker wants to merge 4 commits intomainfrom
Draft
Use heap allocation + valgrind in backend unit test#1633hanno-becker wants to merge 4 commits intomainfrom
hanno-becker wants to merge 4 commits intomainfrom
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 (187 proofs)
|
Contributor
CBMC Results (ML-KEM-1024)Full Results (187 proofs)
|
Contributor
CBMC Results (ML-KEM-768)Full Results (187 proofs)
|
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>
d97ce09 to
79a002b
Compare
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>
79a002b to
72e929a
Compare
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.