-
Notifications
You must be signed in to change notification settings - Fork 8.2k
Fix newlib malloc thread safety issue #35227
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
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,8 @@ | ||
| # SPDX-License-Identifier: Apache-2.0 | ||
|
|
||
| cmake_minimum_required(VERSION 3.13.1) | ||
| find_package(Zephyr REQUIRED HINTS $ENV{ZEPHYR_BASE}) | ||
| project(newlib_thread_safety) | ||
|
|
||
| FILE(GLOB app_sources src/*.c) | ||
| target_sources(app PRIVATE ${app_sources}) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,3 @@ | ||
| CONFIG_ZTEST=y | ||
| CONFIG_NEWLIB_LIBC=y | ||
| CONFIG_TIMESLICE_SIZE=1 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,100 @@ | ||
| /* | ||
| * Copyright (c) 2021 Stephanos Ioannidis <[email protected]> | ||
| * | ||
| * SPDX-License-Identifier: Apache-2.0 | ||
| */ | ||
|
|
||
| /* | ||
| * @file Newlib thread safety test | ||
| * | ||
| * This file contains a set of tests to verify that the C standard functions | ||
| * provided by newlib are thread safe (i.e. synchronised) and that the thread- | ||
| * specific contexts are properly handled (i.e. re-entrant). | ||
| */ | ||
|
|
||
| #include <zephyr.h> | ||
| #include <ztest.h> | ||
|
|
||
| #include <stdio.h> | ||
| #include <stdlib.h> | ||
|
|
||
| #define THREAD_COUNT (64) | ||
| #define STACK_SIZE (512 + CONFIG_TEST_EXTRA_STACKSIZE) | ||
| #define TEST_INTERVAL (30) /* seconds */ | ||
|
|
||
| static struct k_thread tdata[THREAD_COUNT]; | ||
| static K_THREAD_STACK_ARRAY_DEFINE(tstack, THREAD_COUNT, STACK_SIZE); | ||
|
|
||
| void malloc_thread(void *p1, void *p2, void *p3) | ||
| { | ||
| static atomic_t count; | ||
| bool *aborted = p1; | ||
| int *volatile ptr; | ||
| int val; | ||
|
|
||
| while (*aborted == false) { | ||
| /* Compute unique value specific to this iteration. */ | ||
| val = atomic_inc(&count); | ||
|
|
||
| /* Allocate memory block and write a unique value to it. */ | ||
| ptr = malloc(sizeof(int)); | ||
| zassert_not_null(ptr, "Out of memory"); | ||
| *ptr = val; | ||
|
|
||
| /* Busy wait to increase the likelihood of preemption. */ | ||
| k_busy_wait(10); | ||
|
|
||
| /* | ||
| * Verify that the unique value previously written to the | ||
| * memory block is valid. This value will become corrupted if | ||
| * the newlib heap is not properly synchronised. | ||
| */ | ||
| zassert_equal(*ptr, val, "Corrupted memory block"); | ||
|
|
||
| /* Free memory block. */ | ||
| free(ptr); | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * @brief Test thread safety of newlib memory management functions | ||
| * | ||
| * This test calls the malloc() and free() functions from multiple threads to | ||
| * verify that no corruption occurs in the newlib memory heap. | ||
| */ | ||
| void test_malloc_thread_safety(void) | ||
| { | ||
| int i; | ||
| k_tid_t tid[THREAD_COUNT]; | ||
| bool aborted = false; | ||
|
|
||
| /* Create worker threads. */ | ||
| for (i = 0; i < ARRAY_SIZE(tid); i++) { | ||
| tid[i] = k_thread_create(&tdata[i], tstack[i], STACK_SIZE, | ||
| malloc_thread, &aborted, NULL, NULL, | ||
| K_PRIO_PREEMPT(0), 0, K_NO_WAIT); | ||
| } | ||
|
|
||
| TC_PRINT("Created %d worker threads.\n", THREAD_COUNT); | ||
|
|
||
| /* Wait and see if any failures occur. */ | ||
| TC_PRINT("Waiting %d seconds to see if any failures occur ...\n", | ||
| TEST_INTERVAL); | ||
|
|
||
| k_sleep(K_SECONDS(TEST_INTERVAL)); | ||
|
|
||
| /* Abort all worker threads. */ | ||
| aborted = true; | ||
|
|
||
| for (i = 0; i < ARRAY_SIZE(tid); i++) { | ||
| k_thread_join(tid[i], K_FOREVER); | ||
| } | ||
| } | ||
|
|
||
| void test_main(void) | ||
| { | ||
| ztest_test_suite(newlib_thread_safety, | ||
| ztest_unit_test(test_malloc_thread_safety)); | ||
|
|
||
| ztest_run_test_suite(newlib_thread_safety); | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,13 @@ | ||
| common: | ||
| filter: TOOLCHAIN_HAS_NEWLIB == 1 | ||
| min_ram: 64 | ||
| tags: clib newlib | ||
|
|
||
| tests: | ||
| libraries.libc.newlib.thread_safety: | ||
| slow: true | ||
| libraries.libc.newlib_nano.thread_safety: | ||
| slow: true | ||
| filter: CONFIG_HAS_NEWLIB_LIBC_NANO | ||
| extra_configs: | ||
| - CONFIG_NEWLIB_LIBC_NANO=y |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
When CONFIG_USERSPACE=n, making this a spinlock would be much smaller/faster. But they I guess someday we'll get around to implementing a futex backend for sys_mutex which would have the same property.