casaroli opened a new pull request, #21961:
URL: https://github.com/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)`. On arm64, 
x86_64 and 64-bit RISC-V, `alignof(max_align_t)` is 16 (`long double`). 
`CONFIG_MM_DEFAULT_ALIGNMENT` has the default 8, so about half of the heap 
blocks are 8 modulo 16. On x86_64, an aligned SSE access (`movaps`, `movdqa`) 
to such a block gives a general protection fault. GCC also assumes the 
alignment of 16 for `malloc()` on these targets: at -O2 it folds 
`(uintptr_t)malloc(n) & 15` to 0, so code that checks the alignment gets a 
wrong answer.
   
   This PR makes the default 16 when `CONFIG_ARCH_64BIT` is set (arm64, x86_64, 
RV64, and sim on a 64-bit host). The other architectures keep the default 8. A 
configuration that sets the value keeps it. The second commit updates 
Documentation/os/memory/index.rst, which said that all allocations are aligned 
to 8 or 4 bytes.
   
   ## Impact
   
   64-bit configurations that do not set `CONFIG_MM_DEFAULT_ALIGNMENT` get 
16-byte heap blocks. The code does not change (the same text size). The heap 
uses a few bytes more: at boot, +144 bytes for 58 allocations on 
qemu-armv8a:nsh and +232 bytes on rv-virt:nsh64; an allocation uses at most 8 
bytes more. The API does not change, in flat, protected and kernel builds. No 
defconfig changes: no 64-bit defconfig sets the value to 8 or 16, and 
qemu-intel64:nxterm keeps its 64. `savedefconfig` gives the in-tree defconfig 
for qemu-armv8a nsh/knsh, rv-virt nsh64/knsh64/nsh, qemu-intel64 nsh/nxterm, 
mps2-an500:nsh, esp32s3-devkit:nsh and linum-stm32h753bi:littlefs. The TLSF 
manager (`CONFIG_MM_TLSF_MANAGER`) uses its own alignment for `malloc()` and 
does not change.
   
   ## Testing
   
   A new case of the mm cmocka suite checks the alignment of `malloc()`, 
`calloc()` and `realloc()` memory against `alignof(max_align_t)`: 
apache/nuttx-apps#NNNN (`test_nuttx_mm09`).
   
   | configuration | master | this PR |
   |---|---|---|
   | qemu-armv8a:nsh (flat, arm64, QEMU HVF) | `test_nuttx_mm09` FAIL: 1023 
pointers not aligned | 9 of 9 PASS |
   | rv-virt:nsh64 (flat, RV64, QEMU TCG) | `test_nuttx_mm09` FAIL: 997 
pointers not aligned | 9 of 9 PASS |
   
   The user heap of the kernel build (qemu-armv8a:knsh) has the same problem on 
master: two `malloc(45)` blocks in a row are at 0xc00077e0 and 0xc0007818. 
ostest passes (exit status 0) with this PR on qemu-armv8a:nsh and 
qemu-armv8a:knsh. x86_64 (qemu-intel64) was not built.
   
   tools/checkpatch.sh passes. The Documentation build (Sphinx) gives no 
warning for the changed page.
   


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to