gcc optimisation freertos and all that

kurator wrote on Thursday, January 15, 2009:

I got hit by a very strange GCC/FreeRTOS problem.
In a task I check the return value of a function like this:

int ret;

if( xQueueReceive(QueueMain, &Event, portMAX_DELAY) == pdTRUE )
     case EV_KEY_MENU_OK:
       ret = menu_descend();
          if( ret != 0 )

Using this code the system hangs after returning from menu_descend()!
The program gets stuck in xQueueReceive() in an endless loop of xTaskResumeAll().
There is a while loop:
while( ( pxTCB = ( tskTCB * ) listGET_OWNER_OF_HEAD_ENTRY(  ( ( xList * ) &xPendingReadyList ) ) ) != NULL )

As there is an portENTER_CRITICAL() in the beginning of that routine there
interrupts are disabled, the os tick is no longer called.

But, when I declare ret as volatile
volatile int ret;
the program runs as expected!

Looking at the assembler code shows:
Without volatile:

bl    menu_descend
cmp    r0, #0
beq    .L123

With volatile:

bl    menu_descend
str    r0, [sp, #84]
ldr    r3, [sp, #84]
cmp    r3, #0
beq    .L123

So it looks like that storing the return value on the stack
and using r3 for comparison against zero saves my program
from getting stuck.

From here I only can guess.
Maybe gcc tricks itself by the assumption that there lies a return value
of the function on stack, but optimisation uses r0 for handover directly instead,
and from there on the stack pointer is one position to low?

Or has it to do with register treatment of freertos, especially register r0?

I use freertos version 3.2  ( because thats what the project started with
and I’m not allowed to upgrade :wink:
It’s a port for the STR7 ARM7 from ST.
gcc is arm-elf-gcc 4.2.2
optimization level is 2

All that really frightens me!
I must use optimization, at least for size, as the code got to big.

What goes wrong here?

davedoors wrote on Thursday, January 15, 2009:

Wow, that is an old version.

Could you post more of the function?  The snippet you posted has brackets missing, I’m guessing this is just a typo on your part.

Getting stuck in loops like that is normally a sign of memory corruption occurring somewhere. The most likely cause of which is a stack overflow. Unfortunately the version you are using does not have the stack traps in it.

kurator wrote on Thursday, January 15, 2009:

the missing bracket is just a typo
I sampled the code together, there is much more between the queuereceive  and the switch
I forgot that spaces and tabs got eaten up here
the stack looks good to me
everything works fine when I change to volatile, so…

kurator wrote on Monday, January 19, 2009:


no ideas around?
I know its an old FreeRTOS version but maybe
someone stumbled over something similar?

bittendorf wrote on Friday, January 13, 2012:

Hi kurator,

I have a problem similar to yours. I am using gcc 4.3.3 and FreeRTOS 6.0.4 on Atmel AT91SAM7S. Everythins seems to work fine but from time to time the return value of ‘cComReceiveChar ()’ is lost and ‘vReadLine ()’ receives ‘\0’ instead. The functions are located in different modules. The problem is dependent on optimization level: With -O3 and -O2 it occurs, with -O1 or without optimization it doesn’t. I checked stack sizes and found that there’s space left on every stack I use. With better optimizations I found that variables are placed in registers - so memory location overwrites are improbable. The return value is returned in r0 (as in your code).

portCHAR cComReceiveChar (portBASE_TYPE  xComIndex)
  portCHAR  cReceived;

  switch (xComIndex)
    case  0: xQueueReceive (xCom0Receive, & cReceived, portMAX_DELAY);
    case  1: xQueueReceive (xCom1Receive, & cReceived, portMAX_DELAY);
    default: cReceived = ‘\0’;
  return (cReceived);
} /* cComReceiveChar () */

static void vReadLine (portCHAR *pcLine, portBASE_TYPE xMaxChars, portBASE_TYPE xComIndex, portCHAR  cTerm)
  portBASE_TYPE  xIndex = 0;
  portCHAR       cCom;

    cCom = cComReceiveChar (xComIndex);
    pcLine  = cCom;
  } while ((pcLine  != cTerm) && (xIndex < xMaxChars));


} /* vReadLine () */