-
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
Conversation
@LDong-Arm, thank you for your changes. |
c16cc35
to
bde716d
Compare
6dabc7f
to
25dd1c4
Compare
25dd1c4
to
fff4715
Compare
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
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
CI started |
Jenkins CI Test : ✔️ SUCCESSBuild Number: 1 | 🔒 Jenkins CI Job | 🌐 Logs & ArtifactsCLICK for Detailed Summary
|
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