Skip to content
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

Using newlib and common malloc is not thread safe #78124

Open
talih0 opened this issue Sep 7, 2024 · 0 comments
Open

Using newlib and common malloc is not thread safe #78124

talih0 opened this issue Sep 7, 2024 · 0 comments
Assignees
Labels
area: C Library C Standard Library area: newlib Newlib C Standard Library bug The issue is a bug, or the PR is fixing a bug priority: medium Medium impact/importance bug

Comments

@talih0
Copy link
Contributor

talih0 commented Sep 7, 2024

Describe the bug

Using COMMON_LIBC_MALLOC and NEWLIB_LIBC is not thread safe.
Both implementations share the same heap, but each has their own locking mechanism.

This will cause memory corruptions when malloc() and newlib (i.e. printf()) are used in different threads.

To Reproduce
Steps to reproduce the behavior:

Checkout out my branch where I've modified samples/basic/threads to use printf() and malloc():
https://github.com/talih0/zephyr/tree/malloc_newlib_crash

I could reproduce the crash on two boards: xmc47_relax_kit and sam_v71_xult.
I did not get the crash when using qemu_86.

  1. west build -b sam_v71_xult samples/basic/threads
  2. west flash
  3. picocom -e b -b 115200 /dev/ttyACM0
%%%ѥUzMեrrjj:2*2RRRj
                    Count 1
Count 1
ASSERTION FAIL [chunk_used(h, c)] @ WEST_TOPDIR/zephyr/lib/heap/heap.c:175
        unexpected heap state (double-free?) for memory at 0x71c92764

Expected behavior
No crash.

Environment (please complete the following information):

@talih0 talih0 added bug The issue is a bug, or the PR is fixing a bug area: newlib Newlib C Standard Library area: C Library C Standard Library labels Sep 7, 2024
@mmahadevan108 mmahadevan108 added the priority: medium Medium impact/importance bug label Sep 10, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area: C Library C Standard Library area: newlib Newlib C Standard Library bug The issue is a bug, or the PR is fixing a bug priority: medium Medium impact/importance bug
Projects
None yet
Development

No branches or pull requests

3 participants