Skip to content

Commit a0913b9

Browse files
c1728p9bulislaw
authored andcommitted
Fix intermittent crash on mutex test
The test tests-mbedmicro-rtos-mbed-mutex intermittently fails. This patch fixes the problem by initializing thread attributes. It also initializes attributes of other OS wrapper classes to prevent them from having undefined behavior. The test failure on tests-mbedmicro-rtos-mbed-mutex manifested as a crash after running the test case "Test multiple thread" on the ARMCC compiler. This is because one of the threads created for this test happened to have uninitialized attribute bits set to joinable. This caused a pointer to this task to be kept in the terminate_list even after the thread had been deleted and gone out of scope. Later when the RTX kernel accessed this deleted object a crash occurred.
1 parent 807aba6 commit a0913b9

File tree

5 files changed

+6
-0
lines changed

5 files changed

+6
-0
lines changed

rtos/Mutex.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ namespace rtos {
2929

3030
Mutex::Mutex() {
3131
memset(&_obj_mem, 0, sizeof(_obj_mem));
32+
memset(&_attr, 0, sizeof(_attr));
3233
_attr.cb_mem = &_obj_mem;
3334
_attr.cb_size = sizeof(_obj_mem);
3435
_attr.attr_bits = osMutexRecursive;

rtos/Queue.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ class Queue {
4646
/** Create and initialize a message Queue. */
4747
Queue() {
4848
memset(&_obj_mem, 0, sizeof(_obj_mem));
49+
memset(&_attr, 0, sizeof(_attr));
4950
_attr.mq_mem = _queue_mem;
5051
_attr.mq_size = sizeof(_queue_mem);
5152
_attr.cb_mem = &_obj_mem;

rtos/RtosTimer.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ namespace rtos {
3131
void RtosTimer::constructor(mbed::Callback<void()> func, os_timer_type type) {
3232
_function = func;
3333
memset(&_obj_mem, 0, sizeof(_obj_mem));
34+
memset(&_attr, 0, sizeof(_attr));
3435
_attr.cb_mem = &_obj_mem;
3536
_attr.cb_size = sizeof(_obj_mem);
3637
_id = osTimerNew((void (*)(void *))Callback<void()>::thunk, type, &_function, &_attr);

rtos/Semaphore.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ namespace rtos {
2828

2929
Semaphore::Semaphore(int32_t count, uint16_t max_count) {
3030
memset(&_obj_mem, 0, sizeof(_obj_mem));
31+
memset(&_attr, 0, sizeof(_attr));
3132
_attr.cb_mem = &_obj_mem;
3233
_attr.cb_size = sizeof(_obj_mem);
3334
_id = osSemaphoreNew(max_count, count, &_attr);

rtos/Thread.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,8 @@ void Thread::constructor(osPriority priority,
3939
_tid = 0;
4040
_dynamic_stack = (stack_mem == NULL);
4141
_finished = false;
42+
memset(&_obj_mem, 0, sizeof(_obj_mem));
43+
memset(&_attr, 0, sizeof(_attr));
4244
_attr.priority = priority;
4345
_attr.stack_size = stack_size;
4446
_attr.name = name;

0 commit comments

Comments
 (0)