-
Notifications
You must be signed in to change notification settings - Fork 3k
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
Fix Thread::start() and general_block_device test's thread allocation/deallocation #15059
Merged
Conversation
This file contains 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
@LDong-Arm, thank you for your changes. |
c16cc35
to
bde716d
Compare
6dabc7f
to
25dd1c4
Compare
Patater
previously requested changes
Sep 8, 2021
25dd1c4
to
fff4715
Compare
LDong-Arm
commented
Sep 8, 2021
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.
@Patater Thanks, good catches. Now fixed.
Pull request has been modified.
When a Thread object's stack memory is not provided, its `start()` member function dynamically allocates its stack from the heap. If allocation fails, there is no way to catch it because * `std::nothrow` is missing after the `new` keyword. As Mbed OS is built with `-fno-exceptions` (C++ exceptions disabled), failed allocation results in an unrecoverable fault. * The attempted `nullptr` check, which doesn't work anyway due to the first point, is an assertion instead of error returning. Assertions should be used as a development tool to ensure code behaves correctly. But out-of-memory is a completely valid runtime situation. This commit adds the missing `std::nothrow`, and makes `Thread::start()` return `osErrorNoMemory` if allocation fails so the caller can handle it. Note: A case when a thread should never fail due to lack of memory is the main thread. But the main thread's stack is a pre-allocated array in the static memory, passed to the `Thread()` constructor during thread creation, so it's not impacted by this change.
The test case for multithreaded erase/program/read allocates a few Thread objects from the heap and starts them. It has a few problems: * To check that there will be enough heap to start a new thread, the test case tries to allocate a dummy buffer of the thread's heap size and then frees it, before starting the thread. Then the thread will allocate its own stack. Such check is not reliable, because threads that are already running also perform additional allocation (when running `test_thread_job()`) and may take away the memory we just checked. * When deleting all threads in a loop, the loop boundary misses the last thread if the last thread object was allocated but not started (i.e. due to failed thread stack allocation check). To fix the issues * Start a thread without any allocation test. Following the preceding commit "rtos: Thread: Make stack allocation failure runtime catchable", `Thread::start()` now returns `osErrorNoMemory` if stack allocation fails which we can handle. * Store pointers to all threads in a zero-initialized array, and free all elements at the end of the test.
fff4715
to
0b868d5
Compare
Patater
approved these changes
Sep 8, 2021
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.
LGTM
Mbed Core
automation
moved this from Review in progress (4)
to Reviewer approved & awaiting CI & merge (3)
Sep 8, 2021
CI started |
0xc0170
approved these changes
Sep 8, 2021
Jenkins CI Test : ✔️ SUCCESSBuild Number: 1 | 🔒 Jenkins CI Job | 🌐 Logs & ArtifactsCLICK for Detailed Summary
|
Mbed Core
automation
moved this from Reviewer approved & awaiting CI & merge (3)
to Done
Sep 8, 2021
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Summary of changes
This issues were identified while working on QSPI flash support for CYW9P62S1_43012EVB_01 (#14989).
rtos::Thread
class: If a thread's stack memory is not preallocated,Thread::start()
allocates its stack from the heap. Allocation is asserted to succeed and not runtime-catchable, and this assertion doesn't even work due to missingstd::nothrow
. The system crashes in this case.test_thread_job()
) and may take away the memory we just checked.To fix the issues above
rtos::Thread
: Add missingstd::nothrow
to stack allocation. If allocation returnsnullptr
, makestart()
returnosErrorNoMemory
osErrorNoMemory
return value fromThread::start()
.Impact of changes
Migration actions required
Documentation
None
Pull request type
Test results
Reviewers