Skip to content

Commit 3d82c5d

Browse files
Merge branch 'main' into stack_overflow_check
2 parents 53eb756 + ad4e723 commit 3d82c5d

File tree

4 files changed

+132
-37
lines changed

4 files changed

+132
-37
lines changed

MISRA.md

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,25 @@ _Ref 11.5.5_
115115
because data storage buffers are implemented as uint8_t arrays for the
116116
ease of sizing, alignment and access.
117117

118+
#### Rule 14.3
119+
120+
MISRA C-2012 Rule 14.3: Controlling expressions shall not be invariant.
121+
122+
_Ref 14.3_
123+
- The `configMAX_TASK_NAME_LEN` and `taskRESERVED_TASK_NAME_LENGTH` are
124+
evaluated to constants at compile time and may vary based on the build
125+
configuration.
126+
127+
#### Rule 18.1
128+
129+
MISRA C-2012 Rule 18.1: A pointer resulting from arithmetic on a pointer operand
130+
shall address an element of the same array as that pointer operand.
131+
132+
_Ref 18.1_
133+
- Array access remains within bounds since either the null terminator in
134+
the IDLE task name will break the loop, or the loop will break normally
135+
if the array size is smaller than the IDLE task name length.
136+
118137
#### Rule 21.6
119138

120139
MISRA C-2012 Rule 21.6: The Standard Library input/output functions shall not

portable/ThirdParty/GCC/Posix/port.c

Lines changed: 44 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -119,14 +119,20 @@ static void prvResumeThread( Thread_t * xThreadId );
119119
static void vPortSystemTickHandler( int sig );
120120
static void vPortStartFirstTask( void );
121121
static void prvPortYieldFromISR( void );
122+
static void prvThreadKeyDestructor( void * pvData );
123+
static void prvInitThreadKey( void );
124+
static void prvMarkAsFreeRTOSThread( void );
125+
static BaseType_t prvIsFreeRTOSThread( void );
126+
static void prvDestroyThreadKey( void );
122127
/*-----------------------------------------------------------*/
123128

124-
void prvThreadKeyDestructor( void * data )
129+
static void prvThreadKeyDestructor( void * pvData )
125130
{
126-
free( data );
131+
free( pvData );
127132
}
133+
/*-----------------------------------------------------------*/
128134

129-
static void prvInitThreadKey()
135+
static void prvInitThreadKey( void )
130136
{
131137
pthread_mutex_lock( &xThreadMutex );
132138

@@ -137,24 +143,39 @@ static void prvInitThreadKey()
137143

138144
pthread_mutex_unlock( &xThreadMutex );
139145
}
146+
/*-----------------------------------------------------------*/
140147

141-
static void prvMarkAsFreeRTOSThread( pthread_t thread )
148+
static void prvMarkAsFreeRTOSThread( void )
142149
{
150+
uint8_t * pucThreadData = NULL;
151+
143152
prvInitThreadKey();
144-
uint8_t * thread_data = malloc( 1 );
145-
configASSERT( thread_data != NULL );
146-
*thread_data = 1;
147-
pthread_setspecific( xThreadKey, thread_data );
153+
154+
pucThreadData = malloc( 1 );
155+
configASSERT( pucThreadData != NULL );
156+
157+
*pucThreadData = 1;
158+
159+
pthread_setspecific( xThreadKey, pucThreadData );
148160
}
161+
/*-----------------------------------------------------------*/
149162

150-
static BaseType_t prvIsFreeRTOSThread( pthread_t thread )
163+
static BaseType_t prvIsFreeRTOSThread( void )
151164
{
152-
uint8_t * thread_data = ( uint8_t * ) pthread_getspecific( xThreadKey );
165+
uint8_t * pucThreadData = NULL;
166+
BaseType_t xRet = pdFALSE;
153167

154-
return thread_data != NULL && *thread_data == 1;
168+
pucThreadData = ( uint8_t * ) pthread_getspecific( xThreadKey );
169+
if( ( pucThreadData != NULL ) && ( *pucThreadData == 1 ) )
170+
{
171+
xRet = pdTRUE;
172+
}
173+
174+
return xRet;
155175
}
176+
/*-----------------------------------------------------------*/
156177

157-
static void prvDestroyThreadKey()
178+
static void prvDestroyThreadKey( void )
158179
{
159180
pthread_key_delete( xThreadKey );
160181
}
@@ -309,7 +330,7 @@ void vPortEndScheduler( void )
309330
( void ) pthread_kill( hMainThread, SIG_RESUME );
310331

311332
/* Waiting to be deleted here. */
312-
if( prvIsFreeRTOSThread( pthread_self() ) == pdTRUE )
333+
if( prvIsFreeRTOSThread() == pdTRUE )
313334
{
314335
pxCurrentThread = prvGetThreadFromTask( xTaskGetCurrentTaskHandle() );
315336
event_wait( pxCurrentThread->ev );
@@ -369,7 +390,7 @@ void vPortYield( void )
369390

370391
void vPortDisableInterrupts( void )
371392
{
372-
if( prvIsFreeRTOSThread( pthread_self() ) == pdTRUE )
393+
if( prvIsFreeRTOSThread() == pdTRUE )
373394
{
374395
pthread_sigmask(SIG_BLOCK, &xAllSignals, NULL);
375396
}
@@ -378,9 +399,9 @@ void vPortDisableInterrupts( void )
378399

379400
void vPortEnableInterrupts( void )
380401
{
381-
if( prvIsFreeRTOSThread( pthread_self() ) == pdTRUE )
402+
if( prvIsFreeRTOSThread() == pdTRUE )
382403
{
383-
pthread_sigmask(SIG_UNBLOCK, &xAllSignals, NULL);
404+
pthread_sigmask( SIG_UNBLOCK, &xAllSignals, NULL );
384405
}
385406
}
386407
/*-----------------------------------------------------------*/
@@ -417,9 +438,9 @@ static void * prvTimerTickHandler( void * arg )
417438
{
418439
( void ) arg;
419440

420-
prvMarkAsFreeRTOSThread( pthread_self() );
441+
prvMarkAsFreeRTOSThread();
421442

422-
prvPortSetCurrentThreadName("Scheduler timer");
443+
prvPortSetCurrentThreadName( "Scheduler timer" );
423444

424445
while( xTimerTickThreadShouldRun )
425446
{
@@ -451,7 +472,7 @@ void prvSetupTimerInterrupt( void )
451472

452473
static void vPortSystemTickHandler( int sig )
453474
{
454-
if( prvIsFreeRTOSThread( pthread_self() ) == pdTRUE )
475+
if( prvIsFreeRTOSThread() == pdTRUE )
455476
{
456477
Thread_t * pxThreadToSuspend;
457478
Thread_t * pxThreadToResume;
@@ -473,7 +494,9 @@ static void vPortSystemTickHandler( int sig )
473494
}
474495

475496
uxCriticalNesting--;
476-
} else {
497+
}
498+
else
499+
{
477500
fprintf( stderr, "vPortSystemTickHandler called from non-FreeRTOS thread\n" );
478501
}
479502
}
@@ -508,7 +531,7 @@ static void * prvWaitForStart( void * pvParams )
508531
{
509532
Thread_t * pxThread = pvParams;
510533

511-
prvMarkAsFreeRTOSThread( pthread_self() );
534+
prvMarkAsFreeRTOSThread();
512535

513536
prvSuspendSelf( pxThread );
514537

portable/ThirdParty/GCC/Posix/utils/wait_for_event.c

Lines changed: 35 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -35,9 +35,11 @@
3535
struct event
3636
{
3737
pthread_mutex_t mutex;
38+
pthread_mutexattr_t mutexattr;
3839
pthread_cond_t cond;
3940
bool event_triggered;
4041
};
42+
/*-----------------------------------------------------------*/
4143

4244
struct event * event_create( void )
4345
{
@@ -46,23 +48,36 @@ struct event * event_create( void )
4648
if( ev != NULL )
4749
{
4850
ev->event_triggered = false;
49-
pthread_mutex_init( &ev->mutex, NULL );
51+
pthread_mutexattr_init( &ev->mutexattr );
52+
#ifndef __APPLE__
53+
pthread_mutexattr_setrobust( &ev->mutexattr, PTHREAD_MUTEX_ROBUST );
54+
#endif
55+
pthread_mutex_init( &ev->mutex, &ev->mutexattr );
5056
pthread_cond_init( &ev->cond, NULL );
5157
}
5258

5359
return ev;
5460
}
61+
/*-----------------------------------------------------------*/
5562

5663
void event_delete( struct event * ev )
5764
{
5865
pthread_mutex_destroy( &ev->mutex );
66+
pthread_mutexattr_destroy( &ev->mutexattr );
5967
pthread_cond_destroy( &ev->cond );
6068
free( ev );
6169
}
70+
/*-----------------------------------------------------------*/
6271

6372
bool event_wait( struct event * ev )
6473
{
65-
pthread_mutex_lock( &ev->mutex );
74+
if( pthread_mutex_lock( &ev->mutex ) == EOWNERDEAD )
75+
{
76+
#ifndef __APPLE__
77+
/* If the thread owning the mutex died, make the mutex consistent. */
78+
pthread_mutex_consistent( &ev->mutex );
79+
#endif
80+
}
6681

6782
while( ev->event_triggered == false )
6883
{
@@ -73,6 +88,8 @@ bool event_wait( struct event * ev )
7388
pthread_mutex_unlock( &ev->mutex );
7489
return true;
7590
}
91+
/*-----------------------------------------------------------*/
92+
7693
bool event_wait_timed( struct event * ev,
7794
time_t ms )
7895
{
@@ -82,7 +99,13 @@ bool event_wait_timed( struct event * ev,
8299
clock_gettime( CLOCK_REALTIME, &ts );
83100
ts.tv_sec += ms / 1000;
84101
ts.tv_nsec += ( ( ms % 1000 ) * 1000000 );
85-
pthread_mutex_lock( &ev->mutex );
102+
if( pthread_mutex_lock( &ev->mutex ) == EOWNERDEAD )
103+
{
104+
#ifndef __APPLE__
105+
/* If the thread owning the mutex died, make the mutex consistent. */
106+
pthread_mutex_consistent( &ev->mutex );
107+
#endif
108+
}
86109

87110
while( ( ev->event_triggered == false ) && ( ret == 0 ) )
88111
{
@@ -98,11 +121,19 @@ bool event_wait_timed( struct event * ev,
98121
pthread_mutex_unlock( &ev->mutex );
99122
return true;
100123
}
124+
/*-----------------------------------------------------------*/
101125

102126
void event_signal( struct event * ev )
103127
{
104-
pthread_mutex_lock( &ev->mutex );
128+
if( pthread_mutex_lock( &ev->mutex ) == EOWNERDEAD )
129+
{
130+
#ifndef __APPLE__
131+
/* If the thread owning the mutex died, make the mutex consistent. */
132+
pthread_mutex_consistent( &ev->mutex );
133+
#endif
134+
}
105135
ev->event_triggered = true;
106136
pthread_cond_signal( &ev->cond );
107137
pthread_mutex_unlock( &ev->mutex );
108138
}
139+
/*-----------------------------------------------------------*/

tasks.c

Lines changed: 34 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -156,6 +156,23 @@
156156
#define configIDLE_TASK_NAME "IDLE"
157157
#endif
158158

159+
#if ( configNUMBER_OF_CORES > 1 )
160+
/* Reserve space for Core ID and null termination. */
161+
#if ( configMAX_TASK_NAME_LEN < 2U )
162+
#error Minimum required task name length is 2. Please increase configMAX_TASK_NAME_LEN.
163+
#endif
164+
#define taskRESERVED_TASK_NAME_LENGTH 2U
165+
166+
#elif ( configNUMBER_OF_CORES > 9 )
167+
#warning Please increase taskRESERVED_TASK_NAME_LENGTH. 1 character is insufficient to store the core ID.
168+
#else
169+
/* Reserve space for null termination. */
170+
#if ( configMAX_TASK_NAME_LEN < 1U )
171+
#error Minimum required task name length is 1. Please increase configMAX_TASK_NAME_LEN.
172+
#endif
173+
#define taskRESERVED_TASK_NAME_LENGTH 1U
174+
#endif /* if ( ( configNUMBER_OF_CORES > 1 ) */
175+
159176
#if ( configUSE_PORT_OPTIMISED_TASK_SELECTION == 0 )
160177

161178
/* If configUSE_PORT_OPTIMISED_TASK_SELECTION is 0 then task selection is
@@ -3527,21 +3544,26 @@ static BaseType_t prvCreateIdleTasks( void )
35273544
BaseType_t xCoreID;
35283545
char cIdleName[ configMAX_TASK_NAME_LEN ] = { 0 };
35293546
TaskFunction_t pxIdleTaskFunction = NULL;
3530-
BaseType_t xIdleTaskNameIndex;
3531-
BaseType_t xIdleNameLen;
3532-
BaseType_t xCopyLen;
3533-
3534-
configASSERT( ( configIDLE_TASK_NAME != NULL ) && ( configMAX_TASK_NAME_LEN > 3 ) );
3535-
3536-
/* The length of the idle task name is limited to the minimum of the length
3537-
* of configIDLE_TASK_NAME and configMAX_TASK_NAME_LEN - 2, keeping space
3538-
* for the core ID suffix and the null-terminator. */
3539-
xIdleNameLen = strlen( configIDLE_TASK_NAME );
3540-
xCopyLen = xIdleNameLen < ( configMAX_TASK_NAME_LEN - 2 ) ? xIdleNameLen : ( configMAX_TASK_NAME_LEN - 2 );
3547+
UBaseType_t xIdleTaskNameIndex;
35413548

3542-
for( xIdleTaskNameIndex = ( BaseType_t ) 0; xIdleTaskNameIndex < xCopyLen; xIdleTaskNameIndex++ )
3549+
/* MISRA Ref 14.3.1 [Configuration dependent invariant] */
3550+
/* More details at: https://github.com/FreeRTOS/FreeRTOS-Kernel/blob/main/MISRA.md#rule-143. */
3551+
/* coverity[misra_c_2012_rule_14_3_violation] */
3552+
for( xIdleTaskNameIndex = 0U; xIdleTaskNameIndex < ( configMAX_TASK_NAME_LEN - taskRESERVED_TASK_NAME_LENGTH ); xIdleTaskNameIndex++ )
35433553
{
3554+
/* MISRA Ref 18.1.1 [Configuration dependent bounds checking] */
3555+
/* More details at: https://github.com/FreeRTOS/FreeRTOS-Kernel/blob/main/MISRA.md#rule-181. */
3556+
/* coverity[misra_c_2012_rule_18_1_violation] */
35443557
cIdleName[ xIdleTaskNameIndex ] = configIDLE_TASK_NAME[ xIdleTaskNameIndex ];
3558+
3559+
if( cIdleName[ xIdleTaskNameIndex ] == ( char ) 0x00 )
3560+
{
3561+
break;
3562+
}
3563+
else
3564+
{
3565+
mtCOVERAGE_TEST_MARKER();
3566+
}
35453567
}
35463568

35473569
/* Ensure null termination. */

0 commit comments

Comments
 (0)