Skip to content

Commit 9bd1483

Browse files
committed
lib: newlib: Add workaround for #38258
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>
1 parent 4ec03e0 commit 9bd1483

File tree

1 file changed

+14
-0
lines changed

1 file changed

+14
-0
lines changed

lib/libc/newlib/libc-hooks.c

+14
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,20 @@ static int malloc_prepare(const struct device *unused)
130130
"minimum required size specified by "
131131
"CONFIG_NEWLIB_LIBC_MIN_REQUIRED_HEAP_SIZE");
132132

133+
#ifdef CONFIG_XTENSA
134+
/*
135+
* FIXME: For Xtensa, the first `malloc` call may fail if the HEAP_BASE
136+
* is such that the first `sbrk` call returns a 4096-byte
137+
* aligned address.
138+
*
139+
* This is a very ugly workaround for the issue #38258 and must
140+
* be removed once it is fixed.
141+
*/
142+
void *ptr = malloc(16);
143+
144+
free(ptr);
145+
#endif /* CONFIG_XTENSA */
146+
133147
return 0;
134148
}
135149

0 commit comments

Comments
 (0)