-1

I've started investigating FreeRTOS and looked into the task context save routine. This routines stores the registers and the stack pointer. My question is about the stack in different threads. What if there is a thread which performs pushes and pops generated by the compiler. Wouldn't it be possible to overwrite the stack of a different thread?

Clifford
  • 88,407
  • 13
  • 85
  • 165
Gustavo
  • 919
  • 11
  • 34
  • If there's no memory protection you can do whatever. If there is, then no. But why do you think the stack could be overwritten? Every thread running there will have pushs and pops generated by a compiler so...? – Sami Kuhmonen Jan 06 '17 at 10:38
  • But what if there are some pushs and pops depending on a specific condition? Then the stack of another thread could be overwritten. – Gustavo Jan 06 '17 at 10:43
  • Still, explain how that would happen. Threads have separate stacks, why would they overwrite each other? And what would this specific condition be? – Sami Kuhmonen Jan 06 '17 at 10:43
  • On avr there is no memory protection. This could be a simple if conition – Gustavo Jan 06 '17 at 10:45
  • 2
    Yes, there are many pushes and pops with conditions and whatnot. Still they have separate stacks and the only way they can be overwritten is a stack overflow or intentionally changing the stack pointer. Compilers, ifs or buts don't change anything. – Sami Kuhmonen Jan 06 '17 at 10:47
  • Ok I think I begin to understand. Then there is a stackpointer for every thread? Let's say there are two threads. There must be a start point for the stack of the seconds thread and how does the manager know where to set this point and we still don't know how big the first thread's stack will grow. – Gustavo Jan 06 '17 at 10:55
  • @Gustavo while creating a thread, you typically specify the stack size to be allocated for that thread. This allows the RTOS to determine where the next thread stack should be set. – Rishikesh Raje Jan 06 '17 at 11:56
  • 1
    @Gustavo : The stack-pointer for each thread is part of the set stored and restored during the context switch. The context switch is effected on most targets by restoring the thread's stack-pointer and program-counter *after* other registers. – Clifford Jan 06 '17 at 12:37

1 Answers1

2

Each thread must be allocated sufficient stack for its own call-stack plus that required for context storage. The amount of additional stack space required for context storage will depend on the target, but in teh case of FreeRTOS specifically, the constant configMINIMAL_STACK_SIZE will be at least that size plus some margin.

On some targets where the thread stack is used in interrupt contexts, you will also need to account for stack usage by interrupts. If interrupts are nestable; the worst case condition will be when all interrupts become active in priority order before any have completed - perhaps an unlikely scenario, but one you should consider.

Advice on stack allocation for FreeRTOS is provided in the FAQ at http://www.freertos.org/FAQMem.html#StackSize

Clifford
  • 88,407
  • 13
  • 85
  • 165