-
Notifications
You must be signed in to change notification settings - Fork 6.5k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
kernel: k_heap failures on small heaps #33009
Labels
Milestone
Comments
just a quick update @andyross: tried running the test with a small heap size of 40 and tests passed on both Cortex_M3 and native_posix_64. |
On the latest master with the following diff, I can still reproduce the fatal errors
|
andyross
pushed a commit
to andyross/zephyr
that referenced
this issue
May 20, 2021
The K_HEAP_DEFINE macro would allow users to specify heaps that are too small, leading to potential corruption events (though at least there were __ASSERTs that would catch this at runtime if enabled). It would be nice to put the logic to compute this value into the heap code, but that isn't available in kernel.h (and we don't want to pull it in as this header is already WAY to thick). So instead we just hand-compute and document the choice. We can address bitrot problems with a test. (Tweaks to heap size asserts and correct size bounds from Nicolas Pitre) Fixes zephyrproject-rtos#33009 Signed-off-by: Andy Ross <[email protected]>
nashif
pushed a commit
that referenced
this issue
May 20, 2021
The K_HEAP_DEFINE macro would allow users to specify heaps that are too small, leading to potential corruption events (though at least there were __ASSERTs that would catch this at runtime if enabled). It would be nice to put the logic to compute this value into the heap code, but that isn't available in kernel.h (and we don't want to pull it in as this header is already WAY to thick). So instead we just hand-compute and document the choice. We can address bitrot problems with a test. (Tweaks to heap size asserts and correct size bounds from Nicolas Pitre) Fixes #33009 Signed-off-by: Andy Ross <[email protected]>
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Labels
Describe the bug
k_heap_alloc
fails when attempting to allocate memory from small heaps.The following code will fail:
On a Cortex M4F board, this will just return NULL, but on
native_posix_64
aK_ERR_KERNEL_PANIC
is triggered.Based on the documentation (https://docs.zephyrproject.org/latest/reference/kernel/memory/heap.html), it seems clear that there is simply not enough space at the start of the buffer to hold the bucket list for allocations.
The bug is not that the k_heap cannot support tiny heaps, but that fatal errors can occur.
It also not obvious from the documentation how large the heap needs to be before it will start working.
64 bytes is sufficient to allocate 8 bytes on a Cortex M4F , but not
native_posix_64
.Expected behavior
Allocating from small heap should just return NULL, not hard fault.
Ideally some form of validation would be done on the values provided to
K_HEAP_DEFINE
to ensure that it is large enough to return a valid 8 byte (smallest allocation unit) memory chunk at least once.Something similar to:
Environment (please complete the following information):
The text was updated successfully, but these errors were encountered: