Malloc hooks

classic Classic list List threaded Threaded
1 message Options
Reply | Threaded
Open this post in threaded view
|

Malloc hooks

Taavo Raykoff
The avr-libc malloc and friends such as realloc and free are implemented in a way that is not re-entrant.

When using FreeRTOS, any application code can be pre-empted and needs to be thread-safe.  FreeRTOS does have its own allocators independent of avr-libc, and one allocator which wraps avr-libc in what is effectively a critical section.

However this does not solve the problem of legacy library code which use avr-libc directly, such as
Arduino String library
avr-lib’s strdup() and possibly others
Other libraries

When pre-empted, the allocator structures (freelist, etc) can be corrupted resulting in hard-to-find bugs.

I think a good solution to this would be to provide entry/exit hooks on the malloc and friends implementations, that can optionally be implemented to ensure that the avr-libc implementation is in a section that will prevent pre-emption.  This can easily be done using weak linker symbols, that can be ignored by existing code, but provide a hook for the entry/exit handlers.

This is one approach:  https://www.gnu.org/software/libc/manual/html_node/Hooks-for-Malloc.html <https://www.gnu.org/software/libc/manual/html_node/Hooks-for-Malloc.html>

I’d like to submit a patch for this and get included upstream…

Taavo.



_______________________________________________
AVR-libc-dev mailing list
[hidden email]
https://lists.nongnu.org/mailman/listinfo/avr-libc-dev