Corruption of Mutex struct when using FreeRTOS + POSIX port for RISC V Architecture

I am trying to run FreeRTOS + POSIX demo code on Spike - RISCV simulator

FreeRTOS Source from

I see that the pxDispatcher and pxWorkers[] are created and messages do get passed between the threads via Messages Q. The problem is with Mutex - pxThread->xJoinMutex , structure getting corrupted.

Issue :
Memory corruption leads to Assert condition in configASSERT at
configASSERT( !( ( pvItemToQueue == NULL ) && ( pxQueue->uxItemSize != ( UBaseType_t ) 0U ) ) ); via xQueueGenericSend - xSemaphoreGive( ( SemaphoreHandle_t ) &pxThread->xJoinMutex );

back trace from gdb
#1 0x0000000080004990 in Sleep (count=count@entry=100) at main.c:102
#2 0x0000000080004a0a in vAssertCalled (pcFile=pcFile@entry=0x80050210 “…/Source/queue.c”, ulLine=ulLine@entry=788) at main.c:171
#3 0x0000000080000aec in xQueueGenericSend (xQueue=xQueue@entry=0x80050f68 <ucHeap+1168>, pvItemToQueue=pvItemToQueue@entry=0x0, xTicksToWait=, xTicksToWait@entry=0, xCopyPosition=xCopyPosition@entry=0) at …/Source/queue.c:788
#4 0x00000000800057aa in pthread_join (pthread=0x80050eb8 <ucHeap+992>, retval=retval@entry=0x0) at …/Source/lib/FreeRTOS-Plus-POSIX/source/FreeRTOS_POSIX_pthread.c:462
#5 0x000000008000492e in vStartPOSIXDemo (pvParameters=) at posix_demo.c:374
#6 0x00000000800004e0 in prvSetNextTimerInterrupt () at …/Source/portable/GCC/RISC-V/port.c:139
Backtrace stopped: frame did not save the PC

pxQueue->uxItemSize and pxQueue->uxMessagesWaiting variables of pxThread->xJoinMutex have 0x80000fb4 (vPortYield+) and 0x80000466 (xQueueReceive +) addresses, when expected value for both should be 0.

Looks like xSemaphoreCreateMutexStatic( &pxThread->xJoinMutex ); --> Queue structure’s on heap is getting corrupted.

I am running the Demo code with MQUEUE_NUMBER_OF_WORKERS set 1.
I have tried to use the default FreeRTOS_POSIX_portable.h , _default.h FreeRTOSConfig.h, not sure if i am missing any specific define ?

I have tried adding watch *(int *) 0x80050fd8, but no luck. Dont hit the break point.

Pls let me know how to resolve this issue, Thanks

Thanks for providing this information, we will look into it. Can you please let me know which version of FreeRTOS you are using (the version numbers will in the comments at the top of the file), which compiler you are using (assuming GCC, but which?), and also that you are using our official RISC-V port (did the port come from the official FreeRTOS distribution, and if so, which directory did the port.c file come from). That will be enough to get us started, but no doubt we will have more questions regarding setup etc.

Below is what you requested for :
compiler version :
riscv64-unknown-elf-gcc (GCC) 9.2.0

FreeRTOS POSIC source

from posix_demo.c

  • FreeRTOS POSIX Demo V1.0.0
  • Copyright © 2017, Inc. or its affiliates. All Rights Reserved.

FreeRTOS spike source

I moved the Spike specifc port files into 2. and built the FreeRTOS + POSIX lib for Spike

Let me know, if you need any more info.

Can you point me to a FreeRTOS + POSIX port which can be used on Qemu ? any architecture is fine, would help to know the flow.


To partially answer your questions –

Can you point me to a FreeRTOS + POSIX port which can be used on Qemu ? any architecture is fine, would help to know the flow.

Please give a try. I put something together as proof of concept, and the setup instruction can be found in The demo is modified from an existing FreeRTOS port — LM3S6965, and compiles with Keil MDK. (Could check the commit history and see what’s been changed.) I case you don’t have Keil license could simple play with precompiled images.

Some gotcha I ran into yesterday, in case any could be helpful –

  • Since pthread_create() wraps thread creation with vTaskCreate(), depending on what you put into thread context may need to tune stack size. (See PTHREAD_STACK_MIN. In my commit history, you could see I added local char array to format log message to be written to UART which requires more stack space.)

  • There are multiple edits to FreeRTOSConfig.h, namely enabling dynamic allocation and setting configUSE_APPLICATION_TASK_TAG.

I plan to look into Spike. Will follow up if any progress.

1 Like

What is the configMINIMAL_STACK_SIZE ? in FreeRTOS Config.h This page would best answer your question. (May need to search for configMINIMAL_STACK_SIZE.)