Skip to content

testing/testsuites: Check the alignment of malloc() memory. - #4079

Draft
casaroli wants to merge 1 commit into
apache:masterfrom
casaroli:malloc-align-test-up
Draft

casaroli wants to merge 1 commit into
apache:masterfrom
casaroli:malloc-align-test-up

Conversation

@casaroli

Copy link
Copy Markdown
Contributor

depends-on: apache/nuttx/pull/21961

Summary

malloc(), calloc() and realloc() must return memory that is aligned for every type (C11 7.22.3), that is to alignof(max_align_t). No test checks this today. This PR adds case 9 to the mm cmocka suite (cmocka_mm_test, CONFIG_CM_MM_TEST): it allocates 1024 blocks of many sizes, frees one in three, reallocates the others and fills the holes with calloc(), and counts the pointers that are not aligned to alignof(max_align_t). The case gets the alignment from offsetof() of a max_align_t member, so it needs no C11 keyword. It reads each address back through a volatile: GCC assumes that malloc() memory is aligned to 16 on 64-bit targets, and at -O2 it removes a check such as (uintptr_t)p % 16.

Depends on apache/nuttx#21961, which makes the default heap alignment 16 on 64-bit architectures. With the old default 8, this case fails there.

Impact

Test only. The new file is in the existing kernel/mm/cases/ directory, which the Make and CMake builds take with a wildcard.

Testing

cmocka_mm_test with CONFIG_CM_MM_TEST (and cmocka: ALLOW_MIT_COMPONENTS, LIBC_REGEX, ARCH_SETJMP_H):

configuration nuttx master with apache/nuttx#21961
qemu-armv8a:nsh (flat, arm64, QEMU HVF) test_nuttx_mm09 FAIL: 1023 pointers not aligned; cases 1-8 PASS 9 of 9 PASS
rv-virt:nsh64 (flat, RV64, QEMU TCG) test_nuttx_mm09 FAIL: 997 pointers not aligned; cases 1-8 PASS 9 of 9 PASS

ostest passes (exit status 0) in the same qemu-armv8a:nsh images. tools/checkpatch.sh passes.

malloc(), calloc() and realloc() must return memory that is aligned
for every type (C11 7.22.3), that is to alignof(max_align_t).  Add
mm test case 9: it allocates, frees and reallocates blocks of many
sizes and checks each pointer.

Assisted-by: Claude Code:claude-opus-5-5
Signed-off-by: Marco Casaroli <marco.casaroli@gmail.com>
@casaroli casaroli changed the title testing/testsuites: Check the alignment of heap memory. testing/testsuites: Check the alignment of malloc() memory. Nov 17, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants