-
Notifications
You must be signed in to change notification settings - Fork 7.3k
newlib: first malloc call may fail on Xtensa depending on image size #38258
New issue
Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? # to your account
Comments
NOTE: This issue exists to keep track of this rather serious bug with the newlib on Xtensa. It needs to be fixed in the Zephyr SDK, and also in the upstream newlib if it has not already been patched in the latest releases. |
For the Xtensa platforms (e.g. qemu_xtensa), the first `malloc` call may fail if the newlib heap base address is such that the first `sbrk` call returns a 4096-byte aligned address. Here we add a workaround for Xtensa that allocates and immediately frees a 16-byte memory block during initialisation so that all subsequent `malloc` calls succeed. This commit needs to be reverted once the issue zephyrproject-rtos#38258 is fixed. Signed-off-by: Stephanos Ioannidis <root@stephanos.io>
This commit adds test code to reproduce the Xtensa malloc failure issue reported in zephyrproject-rtos#38258. Run the `hello_world` sample in `qemu_xtensa` to see the first malloc fail as described in the aforementioned issue. Tested with Zephyr SDK 0.13.0, which uses the newlib 3.3.0.
For the Xtensa platforms (e.g. qemu_xtensa), the first `malloc` call may fail if the newlib heap base address is such that the first `sbrk` call returns a 4096-byte aligned address. Here we add a workaround for Xtensa that allocates and immediately frees a 16-byte memory block during initialisation so that all subsequent `malloc` calls succeed. This commit needs to be reverted once the issue #38258 is fixed. Signed-off-by: Stephanos Ioannidis <root@stephanos.io>
@sylvioalves do you know if you guys newlib version has this issue or a fix for it? |
To all Xtensa maintainers: how important is it that we enforce My hunch is that this issue will go away if we just remove that line. Of course, the newlib bug persists, but at least Xtensa should no longer be affected by it. |
Also cc @jcmvbkbc, if you are around. |
I believe that 16 is not required for the base xtensa architecture with or without standard FP coprocessors. But 16 or even higher alignment may be required in the presence of TIE extensions, so it should really not be hardcoded but taken from the xtensa core configuration. |
@jcmvbkbc Thanks for clarifying. |
zephyrproject-rtos/newlib-cygwin@b67ccc2#diff-f056d0212a247fc9bf5137a683fa14d9f1f80f23f76eedb68cf81710207fdb27R198 On
I will post a Zephyr SDK patch for this. |
The current malloc implementation has a bug in which the first malloc call may fail if MALLOC_ALIGNMENT is 16 and the requested block size is such that malloc invokes sbrk with the heap expansion size that causes the resulting heap address to fall on a page boundary. sys/config.h was defining the minimum MALLOC_ALIGNMENT for the Xtensa architecture as 16, and this caused the first malloc call to fail on the Xtensa platforms in some rare cases that satisfy the conditions described above (see zephyrproject-rtos/zephyr#38258). Since the Xtensa architecture in itself does not require the malloc alignment of 16, remove MALLOC_ALIGNMENT definition and use the default alignment of 8 for all Xtensa platforms. Signed-off-by: Stephanos Ioannidis <root@stephanos.io>
Pull in the newlib workaround for the Xtensa malloc failure issue reported in zephyrproject-rtos/zephyr#38258. Signed-off-by: Stephanos Ioannidis <root@stephanos.io>
Pull in the newlib workaround for the Xtensa malloc failure issue reported in zephyrproject-rtos/zephyr#38258. Signed-off-by: Stephanos Ioannidis <root@stephanos.io>
zephyrproject-rtos/sdk-ng#394 will fix this issue. |
I'll check and get back soon. |
Pull in the newlib workaround for the Xtensa malloc failure issue reported in zephyrproject-rtos/zephyr#38258. Signed-off-by: Stephanos Ioannidis <root@stephanos.io>
zephyrproject-rtos/sdk-ng#394 has been merged. Waiting for the Zephyr SDK 0.13.1 to be released and mainlined in the CI. |
Not sure about this. Need to check. |
#38261 is a workaround |
As per the discussion in the bug triage meeting today: We will be keeping this issue open until the new SDK is released so that we can keep track of this problem, and also that we do not forget to remove the workaround added in #38261. SDK 0.13.1 will not be released for and used by the Zephyr 2.7.0 release because it contains other unrelated fixes that can potentially break other things and we cannot afford that at this stage. |
As per the Toolchain WG meeting today: SDK 0.13.1 will be released for and used by the Zephyr 2.7.0 release since it contains a critical ARM-related security fix. |
The commit 9bd1483 was added as a workaround for the Xtensa initial malloc failure bug. This bug has been fixed in the Zephyr SDK 0.13.1 release and therefore this workaround is no longer needed. Signed-off-by: Stephanos Ioannidis <root@stephanos.io>
This commit sets the minimum required Zephyr SDK version to 0.13.1, which fixes the following critical issues: 1. Xtensa initial malloc failure (GitHub issue zephyrproject-rtos#38258) 2. ARMv8-M security extension vulnerability (CVE-2021-35465) Signed-off-by: Stephanos Ioannidis <root@stephanos.io>
Bumping to high as we released SDK 0.13.1 with fix, and we should make sure the PR to revert workaround gets merged. |
This will be merged on Friday AM EST, just so that the message on the devel mailing list has time to be circulated, and so that there are enough cycles for CI prior to cutting rc3. |
The commit 9bd1483 was added as a workaround for the Xtensa initial malloc failure bug. This bug has been fixed in the Zephyr SDK 0.13.1 release and therefore this workaround is no longer needed. Signed-off-by: Stephanos Ioannidis <root@stephanos.io>
This commit sets the minimum required Zephyr SDK version to 0.13.1, which fixes the following critical issues: 1. Xtensa initial malloc failure (GitHub issue #38258) 2. ARMv8-M security extension vulnerability (CVE-2021-35465) Signed-off-by: Stephanos Ioannidis <root@stephanos.io>
The commit 9bd1483 was added as a workaround for the Xtensa initial malloc failure bug. This bug has been fixed in the Zephyr SDK 0.13.1 release and therefore this workaround is no longer needed. Signed-off-by: Stephanos Ioannidis <root@stephanos.io>
This commit sets the minimum required Zephyr SDK version to 0.13.1, which fixes the following critical issues: 1. Xtensa initial malloc failure (GitHub issue zephyrproject-rtos#38258) 2. ARMv8-M security extension vulnerability (CVE-2021-35465) Signed-off-by: Stephanos Ioannidis <root@stephanos.io>
The current malloc implementation has a bug in which the first malloc call may fail if MALLOC_ALIGNMENT is 16 and the requested block size is such that malloc invokes sbrk with the heap expansion size that causes the resulting heap address to fall on a page boundary. sys/config.h was defining the minimum MALLOC_ALIGNMENT for the Xtensa architecture as 16, and this caused the first malloc call to fail on the Xtensa platforms in some rare cases that satisfy the conditions described above (see zephyrproject-rtos/zephyr#38258). Since the Xtensa architecture in itself does not require the malloc alignment of 16, remove MALLOC_ALIGNMENT definition and use the default alignment of 8 for all Xtensa platforms. Signed-off-by: Stephanos Ioannidis <root@stephanos.io>
The current malloc implementation has a bug in which the first malloc call may fail if MALLOC_ALIGNMENT is 16 and the requested block size is such that malloc invokes sbrk with the heap expansion size that causes the resulting heap address to fall on a page boundary. sys/config.h was defining the minimum MALLOC_ALIGNMENT for the Xtensa architecture as 16, and this caused the first malloc call to fail on the Xtensa platforms in some rare cases that satisfy the conditions described above (see zephyrproject-rtos/zephyr#38258). Since the Xtensa architecture in itself does not require the malloc alignment of 16, remove MALLOC_ALIGNMENT definition and use the default alignment of 8 for all Xtensa platforms. Signed-off-by: Stephanos Ioannidis <root@stephanos.io>
Describe the bug
The first
malloc
call may fail on the Xtensa platforms (e.g.qemu_xtensa
) if the requested block size is such that themalloc
function invokes thesbrk
function with the heap expansion size that causes the resulting heap address to fall on the 0x1000 boundary (what newlib thinks is "page boundary").For example, if the initial heap address is
0x60009fd0
andmalloc
is invoked to allocate a 16-byte block, themalloc
attempts to increase the heap size by 48 by callingsbrk
, and this causessbrk
to return0x6000a000
, which is a 0x1000-aligned address.To Reproduce
samples/hello_world
onqemu_xtensa
and observe the first malloc fail.Expected behavior
malloc
always succeeds.Logs and console output
Environment (please complete the following information):
Additional context
This only happens on Xtensa -- I have reproduced a similar case on other archs, but they all work as expected:
Xtensa (qemu_xtensa)
ARM (qemu_cortex_m3)
SPARC (qemu_leon3)
This is likely a newlib bug.
What is special about Xtensa, compared to other archs, is that its
MALLOC_ALIGNMENT
is 16 rather than 8, and this is causing the newlib malloc algorithm to produce a mishap not observed on the archs with theMALLOC_ALIGNMENT
of 8.https://github.com/zephyrproject-rtos/newlib-cygwin/blob/4f5997d3c0f9011135e9627dad700c9d64be4a4b/newlib/libc/stdlib/mallocr.c#L1399-L1401
https://github.com/zephyrproject-rtos/newlib-cygwin/blob/b1fe4401fdbd0860e0b91227219b15d2e0142b78/newlib/libc/include/sys/config.h#L198
The text was updated successfully, but these errors were encountered: