# What is this ? Stack Overflow ?

**URL:** <https://forums.freertos.org/t/what-is-this-stack-overflow/4018>\
**Category:** Kernel\
**Created:** [May 18, 2009, 7:20am UTC](https://forums.freertos.org/t/what-is-this-stack-overflow/4018 "2009-05-18T07:20:12Z")\
**Posts on this page:** 13\
**Page:** 1

<div class="post-metadata">

**Author:** ![system](https://d2v6vdsk2p900z.cloudfront.net/original/1X/7fc8fdb729c645aab5b20282bbe34e57cb407be6.jpeg) [@system](https://forums.freertos.org/u/system)\
**Post date:** [May 18, 2009, 7:20am UTC](https://forums.freertos.org/t/what-is-this-stack-overflow/4018/1 "2009-05-18T07:20:12Z")

</div>

**[enginayd](https://sourceforge.net/u/enginayd/)** wrote on [Monday, May 18, 2009](https://sourceforge.net/p/freertos/discussion/382005/thread/b3e87e6a/#e227):

Hello there,

My application started to act crazy.&nbsp; I find the device is working (i.e. interrupt handlers are being called, my status led keeps on blinking) but tasks are stopped.

I halt the system via a debugger and find that uxMissedTicks is 4 billion sometehing. The memory layout is something like this;

.bss.pxCurrentTCB  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 0x20009d58&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 0x4 …/Source/tasks.o  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 0x20009d58&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; pxCurrentTCB  
.bss.xDelayedTaskList1  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 0x20009d5c&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 0x14 …/Source/tasks.o  
.bss.xDelayedTaskList2  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 0x20009d70&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 0x14 …/Source/tasks.o  
.bss.uxSchedulerSuspended  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 0x20009d84&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 0x4 …/Source/tasks.o  
.bss.uxMissedTicks  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 0x20009d88&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 0x4 …/Source/tasks.o  
.bss.pcTraceBufferEnd  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 0x20009d8c&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 0x4 …/Source/tasks.o  
.bss.xNumOfOverflows  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 0x20009d90&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 0x4 …/Source/tasks.o  
.bss.xTickCount  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 0x20009d94&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 0x4 …/Source/tasks.o  
.bss.uxTopUsedPriority  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 0x20009d98&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 0x4 …/Source/tasks.o  
.bss.xHeap&nbsp;&nbsp;&nbsp;&nbsp; 0x20009d9c&nbsp;&nbsp;&nbsp;&nbsp; 0x2804 …/Source/portable/MemMang/heap\_2.o  
.bss.xStart&nbsp;&nbsp;&nbsp; 0x2000c5a0&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 0x8 …/Source/portable/MemMang/heap\_2.o  
.bss.xEnd&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 0x2000c5a8&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 0x8 …/Source/portable/MemMang/heap\_2.o  
.bss.xHeapHasBeenInitialised.1635

This is ARM Cortex-M3, SRAM starts at 0x20000000 and the SRAM size is 64k. So it looks like growing down xHeap stack could do this kind of damage. But the variables in between looks intact (as indeed the top used priority is 4).

uxMissedTicks&nbsp;&nbsp;&nbsp; 4289657151&nbsp;&nbsp;&nbsp;  
pcTraceBufferEnd&nbsp;&nbsp;&nbsp; 0x00000000&nbsp;&nbsp;&nbsp;  
xNumOfOverflows&nbsp;&nbsp;&nbsp; 0&nbsp;&nbsp;&nbsp;  
xTickCount&nbsp;&nbsp;&nbsp; 5339088&nbsp;&nbsp;&nbsp;  
uxTopUsedPriority&nbsp;&nbsp;&nbsp; 4&nbsp;&nbsp;&nbsp;

When I set uxMissedTicks to, say, 10 via debugger the system continiues to operate correctly (at least apparently, it answers serial port queries and all)… but soon after uxMissedTicks is again some 4-billion value.

Any idea how to hunt this ?

Kind regards

---

<div class="post-metadata">

**Author:** ![system](https://d2v6vdsk2p900z.cloudfront.net/original/1X/7fc8fdb729c645aab5b20282bbe34e57cb407be6.jpeg) [@system](https://forums.freertos.org/u/system)\
**Post date:** [May 18, 2009, 8:05am UTC](https://forums.freertos.org/t/what-is-this-stack-overflow/4018/2 "2009-05-18T08:05:45Z")

</div>

**[davedoors](https://sourceforge.net/u/davedoors/)** wrote on [Monday, May 18, 2009](https://sourceforge.net/p/freertos/discussion/382005/thread/b3e87e6a/#e227/07be):

What is configKERNEL\_INTERRUPT\_PRIORITY set to?  
What is configMAX\_SYSCALL\_INTERRUPT\_PRIORITY set to?  
What priority are all your other interrupts running at?

On Cortex problems like this are normally bad interrupt priority settings, or calling an API function from inside a critical section.

---

<div class="post-metadata">

**Author:** ![system](https://d2v6vdsk2p900z.cloudfront.net/original/1X/7fc8fdb729c645aab5b20282bbe34e57cb407be6.jpeg) [@system](https://forums.freertos.org/u/system)\
**Post date:** [May 18, 2009, 8:10am UTC](https://forums.freertos.org/t/what-is-this-stack-overflow/4018/3 "2009-05-18T08:10:51Z")

</div>

**[enginayd](https://sourceforge.net/u/enginayd/)** wrote on [Monday, May 18, 2009](https://sourceforge.net/p/freertos/discussion/382005/thread/b3e87e6a/#e227/a3b2):

I have the following macros in my config header.

#define configMAX\_SYSCALL\_INTERRUPT\_PRIORITY&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 191  
#define configKERNEL\_INTERRUPT\_PRIORITY 255

But…

As far as I remember these #defines introduced in 5.2.0 ? And I have reverted to using 5.0.0. Do these still apply ?

---

<div class="post-metadata">

**Author:** ![rtel](https://d3iykfbgfh8zhg.cloudfront.net/user_avatar/forums.freertos.org/rtel/32/31_2.png) [@rtel](https://forums.freertos.org/u/rtel)\
**Post date:** [May 18, 2009, 8:19am UTC](https://forums.freertos.org/t/what-is-this-stack-overflow/4018/4 "2009-05-18T08:19:48Z")

</div>

**[rtel](https://sourceforge.net/u/rtel/)** wrote on [Monday, May 18, 2009](https://sourceforge.net/p/freertos/discussion/382005/thread/b3e87e6a/#e227/a3b2/0623):

See [http://www.freertos.org/History.txt](http://www.freertos.org/History.txt) , it was introduced in V5.0.2.

Anybody else having to log in twice to be able to post at the moment?

Regards.

---

<div class="post-metadata">

**Author:** ![system](https://d2v6vdsk2p900z.cloudfront.net/original/1X/7fc8fdb729c645aab5b20282bbe34e57cb407be6.jpeg) [@system](https://forums.freertos.org/u/system)\
**Post date:** [May 18, 2009, 12:21pm UTC](https://forums.freertos.org/t/what-is-this-stack-overflow/4018/5 "2009-05-18T12:21:51Z")

</div>

**[enginayd](https://sourceforge.net/u/enginayd/)** wrote on [Monday, May 18, 2009](https://sourceforge.net/p/freertos/discussion/382005/thread/b3e87e6a/#e227/51ea):

This case so many undefined behavior, I’m really stuck with this problem for the past 2 weeks 😕

In essence this is the story, there are 5 runnings tasks (uIP, MAC, Idle, Modbus, UserTask).

uIP and MAC are two tasks which are being used directly from the Richard’s examples. And Modbus task is mine which is very similar to the MAC task… Both are “interrupt handler tasks” woken by real ISRs. In this case Modbus task is woken by UART handler.

Now, I’m upgraded to FreeRTOS 5.2.0.

5 tasks still runs… as soon as I plug the serial port cable (so my device is started to be queried like 30 times/sec) it immediately freezes.

This time I find FreeRTOS looping in vListInsert()'s&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;

for( pxIterator = ( xListItem \* ) &( pxList-\>xListEnd ); pxIterator-\>pxNext-\>xItemValue \<= xValueOfInsertion; pxIterator = pxIterator-\>pxNext )

Any ideas ?

---

<div class="post-metadata">

**Author:** ![system](https://d2v6vdsk2p900z.cloudfront.net/original/1X/7fc8fdb729c645aab5b20282bbe34e57cb407be6.jpeg) [@system](https://forums.freertos.org/u/system)\
**Post date:** [May 18, 2009, 12:30pm UTC](https://forums.freertos.org/t/what-is-this-stack-overflow/4018/6 "2009-05-18T12:30:56Z")

</div>

**[davedoors](https://sourceforge.net/u/davedoors/)** wrote on [Monday, May 18, 2009](https://sourceforge.net/p/freertos/discussion/382005/thread/b3e87e6a/#e227/51ea/6674):

What is configKERNEL\_INTERRUPT\_PRIORITY set to?  
What is configMAX\_SYSCALL\_INTERRUPT\_PRIORITY set to?  
What priority are all your other interrupts running at?

On Cortex problems like this are normally bad interrupt priority settings, or calling an API function from inside a critical section.

---

<div class="post-metadata">

**Author:** ![system](https://d2v6vdsk2p900z.cloudfront.net/original/1X/7fc8fdb729c645aab5b20282bbe34e57cb407be6.jpeg) [@system](https://forums.freertos.org/u/system)\
**Post date:** [May 18, 2009, 12:41pm UTC](https://forums.freertos.org/t/what-is-this-stack-overflow/4018/7 "2009-05-18T12:41:40Z")

</div>

**[enginayd](https://sourceforge.net/u/enginayd/)** wrote on [Monday, May 18, 2009](https://sourceforge.net/p/freertos/discussion/382005/thread/b3e87e6a/#e227/07be/ccf9):

#define configMAX\_SYSCALL\_INTERRUPT\_PRIORITY&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 191  
#define configKERNEL\_INTERRUPT\_PRIORITY 255

I’m not configuring any priority level for my interrupts, so they should have the default value. Is this a problem ?

---

<div class="post-metadata">

**Author:** ![system](https://d2v6vdsk2p900z.cloudfront.net/original/1X/7fc8fdb729c645aab5b20282bbe34e57cb407be6.jpeg) [@system](https://forums.freertos.org/u/system)\
**Post date:** [May 18, 2009, 12:45pm UTC](https://forums.freertos.org/t/what-is-this-stack-overflow/4018/8 "2009-05-18T12:45:28Z")

</div>

**anonymous** wrote on [Monday, May 18, 2009](https://sourceforge.net/p/freertos/discussion/382005/thread/b3e87e6a/#e227/51ea/156e):

I haven’t read the whole thread, but given what you describe and the fact that you’re on a Cortex-M3, I would bet that your interrupt handlers using FreeRTOS API functions (ending in \_FromISR) have too low a priority (as configured in the NVIC), meaning that they run even when FreeRTOS tries to prevent those interrupt handlers from running.

What priority did you set for those ISR? Once you group the preemption priority and the subpriority into a 8 bits value, each of them must be between configMAX\_SYSCALL\_INTERRUPT\_PRIORITY (exclusive, default 191) and configKERNEL\_INTERRUPT\_PRIORITY (inclusive, default 255).

---

<div class="post-metadata">

**Author:** ![system](https://d2v6vdsk2p900z.cloudfront.net/original/1X/7fc8fdb729c645aab5b20282bbe34e57cb407be6.jpeg) [@system](https://forums.freertos.org/u/system)\
**Post date:** [May 18, 2009, 12:48pm UTC](https://forums.freertos.org/t/what-is-this-stack-overflow/4018/9 "2009-05-18T12:48:23Z")

</div>

**anonymous** wrote on [Monday, May 18, 2009](https://sourceforge.net/p/freertos/discussion/382005/thread/b3e87e6a/#e227/07be/ccf9/2e61):

\> I’m not configuring any priority level for my interrupts, so they should have the default value. Is this a problem ?

Absolutely! Default ISR priorities on the Cortex-M3 are \*not\* suitable for ISR using the FreeRTOS API. Those priorities are much too low (meaning too high a priority in the Cortex-M3 world).

---

<div class="post-metadata">

**Author:** ![system](https://d2v6vdsk2p900z.cloudfront.net/original/1X/7fc8fdb729c645aab5b20282bbe34e57cb407be6.jpeg) [@system](https://forums.freertos.org/u/system)\
**Post date:** [May 18, 2009, 1:01pm UTC](https://forums.freertos.org/t/what-is-this-stack-overflow/4018/10 "2009-05-18T13:01:11Z")

</div>

**[enginayd](https://sourceforge.net/u/enginayd/)** wrote on [Monday, May 18, 2009](https://sourceforge.net/p/freertos/discussion/382005/thread/b3e87e6a/#e227/07be/ccf9/2e61/1cc6):

Oh. I checked with the documentation and indeed the default value is 0, the highest.

The line that I have missed from Richards example is

IntPrioritySet( INT\_ETH, configKERNEL\_INTERRUPT\_PRIORITY );

I’ve added similar line for UART and now it looks like working really well!

Now, this means that previously, my UART interrupt could have preempted the kernel. And call API functions. What would be an example scenario that could corrupt the system ? (I’m trying to grasp what is happening here)

And why isn’t kernel not with the highest priority then ?

Kind regards

---

<div class="post-metadata">

**Author:** ![system](https://d2v6vdsk2p900z.cloudfront.net/original/1X/7fc8fdb729c645aab5b20282bbe34e57cb407be6.jpeg) [@system](https://forums.freertos.org/u/system)\
**Post date:** [May 18, 2009, 1:13pm UTC](https://forums.freertos.org/t/what-is-this-stack-overflow/4018/11 "2009-05-18T13:13:29Z")

</div>

**anonymous** wrote on [Monday, May 18, 2009](https://sourceforge.net/p/freertos/discussion/382005/thread/b3e87e6a/#e227/07be/ccf9/2e61/1cc6/13da):

FreeRTOS needs to run as the lowest possible priority (in the regular meaning, not the Cortex-M3 meaning) to make sure that the context it saves and restores is the interrupted task context. If it had a high priority, it could by accident manipulate the context of another ISR that would have been preempted.

Also, why would the FreeRTOS kernel need to run at a higher priority? The less intrusive the better. If it had a high priority, sure, it could switch tasks while other ISRs are currently running or pending, but anyway those tasks would never execute immediately because of the pending ISR.

---

<div class="post-metadata">

**Author:** ![system](https://d2v6vdsk2p900z.cloudfront.net/original/1X/7fc8fdb729c645aab5b20282bbe34e57cb407be6.jpeg) [@system](https://forums.freertos.org/u/system)\
**Post date:** [May 18, 2009, 1:35pm UTC](https://forums.freertos.org/t/what-is-this-stack-overflow/4018/12 "2009-05-18T13:35:20Z")

</div>

**[enginayd](https://sourceforge.net/u/enginayd/)** wrote on [Monday, May 18, 2009](https://sourceforge.net/p/freertos/discussion/382005/thread/b3e87e6a/#e227/07be/ccf9/2e61/1cc6/13da/fbfc):

\>FreeRTOS needs to run as the lowest possible priority (in the regular meaning, not the Cortex-M3 meaning) to make sure that the context it saves and restores is the interrupted task context. If it had a high priority, it could by accident manipulate the context of another ISR that would have been preempted

So the idea is to never preempt an interrupt service routine but only the user space tasks ? I don’t see how it would hurt the ISR context though.

By the way, Dave and Samuel, I can’t put in words how much I appreciate your help. Thank you very much.

---

<div class="post-metadata">

**Author:** ![system](https://d2v6vdsk2p900z.cloudfront.net/original/1X/7fc8fdb729c645aab5b20282bbe34e57cb407be6.jpeg) [@system](https://forums.freertos.org/u/system)\
**Post date:** [September 2, 2009, 11:37pm UTC](https://forums.freertos.org/t/what-is-this-stack-overflow/4018/13 "2009-09-02T23:37:43Z")

</div>

**[utezduyar](https://sourceforge.net/u/utezduyar/)** wrote on [Wednesday, September 02, 2009](https://sourceforge.net/p/freertos/discussion/382005/thread/b3e87e6a/#e227/ce59):

Hi Engin,

The problem is you are facing a race condition where the suspended task was trying to add the missed ticks to the system tick count.

–uxMissedTicks is getting called by 2 different tasks. One of them drains it, loops until it becomes 0. Then at some point it is swapped out and other task decrements it which overflows the unsigned integer.

Obviously this shouldn’t happen. It looks like you are using CortexM3 core and I would guess STM32. I had stumbled on a similar problem with STM32. Even though CortexM3 can have 256 priority levels, STM32 set this to 16. They can be 0x00, 0x10… 0xF0.

Basically when you call the disable interrupt function, if it is setting the BASEPRI register to a high value, any interrupt with priority lower than it will not be masked.

Probably for your case, when you are about to decrement the missed tick count, a PENDSV interrupt swaps the context and another task claims these missed ticks.

I have resolved the problem by setting #define configMAX\_SYSCALL\_INTERRUPT\_PRIORITY 0x10  
#define configKERNEL\_INTERRUPT\_PRIORITY 0xF0.

But even with these settings, interrupts with priority 0x00 will not be masked. So, make sure your SYSTICK and PENDSV interrupts have lower priority.

Thanks a lot
