# How to protect from stack overflow on embedded targets?

**URL:** https://forum.ada-lang.io/t/how-to-protect-from-stack-overflow-on-embedded-targets/4331
**Category:** General
**Created:** [April 10, 2026, 4:39am UTC](https://forum.ada-lang.io/t/how-to-protect-from-stack-overflow-on-embedded-targets/4331 "2026-04-10T04:39:48Z")
**Posts on this page:** 14
**Page:** 1

<div class="post-metadata">

### Author: ![Max](https://forum.ada-lang.io/user_avatar/forum.ada-lang.io/max/32/21_2.png) [@Max](https://forum.ada-lang.io/u/Max)
#### Post date: [April 10, 2026, 4:39am UTC](https://forum.ada-lang.io/t/how-to-protect-from-stack-overflow-on-embedded-targets/4331/1 "2026-04-10T04:39:48Z")

</div>

How can I make `-fstack-check` work on stm32f4 or something like that?

Are there others ways to protect from overflows?

---

<div class="post-metadata">

### Author: ![liampwll](https://forum.ada-lang.io/letter_avatar_proxy/v4/letter/l/b19c9b/32.png) [@liampwll](https://forum.ada-lang.io/u/liampwll)
#### Post date: [April 10, 2026, 4:52am UTC](https://forum.ada-lang.io/t/how-to-protect-from-stack-overflow-on-embedded-targets/4331/2 "2026-04-10T04:52:43Z")

</div>

If I recall correctly it does work. I think it raises a Storage\_Error. It might go straight to the last chance handler instead of unwinding.

Another option is to statically check the stack size. AdaCore has a proprietary tool for this (GNATstack), but you can get the data from GCC and do something yourself. I have someone elses stack analysis tool here that I made a few tweaks to to get it working with the current GCC version: [prunt\_board\_3\_software/firmware at master · Prunt3D/prunt\_board\_3\_software · GitHub](https://github.com/Prunt3D/prunt_board_3_software/tree/master/firmware)

---

<div class="post-metadata">

### Author: ![Max](https://forum.ada-lang.io/user_avatar/forum.ada-lang.io/max/32/21_2.png) [@Max](https://forum.ada-lang.io/u/Max)
#### Post date: [April 10, 2026, 4:59am UTC](https://forum.ada-lang.io/t/how-to-protect-from-stack-overflow-on-embedded-targets/4331/3 "2026-04-10T04:59:41Z")

</div>

Cool! Can you rewrite it in Ada as standalone Alire crate?

---

<div class="post-metadata">

### Author: ![liampwll](https://forum.ada-lang.io/letter_avatar_proxy/v4/letter/l/b19c9b/32.png) [@liampwll](https://forum.ada-lang.io/u/liampwll)
#### Post date: [April 10, 2026, 5:15am UTC](https://forum.ada-lang.io/t/how-to-protect-from-stack-overflow-on-embedded-targets/4331/4 "2026-04-10T05:15:03Z")

</div>

A better starting point for that would be to pull mucheckstack out of Muen. I encountered some friction with getting it working, which is why I don’t use it in the above project.

---

<div class="post-metadata">

### Author: ![Max](https://forum.ada-lang.io/user_avatar/forum.ada-lang.io/max/32/21_2.png) [@Max](https://forum.ada-lang.io/u/Max)
#### Post date: [April 10, 2026, 6:54am UTC](https://forum.ada-lang.io/t/how-to-protect-from-stack-overflow-on-embedded-targets/4331/5 "2026-04-10T06:54:08Z")

</div>

Let me explain why `-fstack-check` doesn’t work for me. Event on tiny function it wants 4Kb stack, like this:

```ada
        @ args = 0, pretend = 0, frame = 40
        @ frame_needed = 0, uses_anonymous_args = 0
        push {r4, r5, r6, r7, r8, r9, lr}
        vpush.64 {d8}
        sub sp, sp, #44
        vmov.f32 s16, s0
        sub r0, sp, #4384 <---------
        subs r0, r0, #8
        bl __gnat_stack_check

```

> **Tested code**
>
> ```ada
> procedure Compute (Value : in out Float) is
> Data : array (1 .. 3) of Float := (Value, others => <>);
> begin
> for J in 2 .. Data'Last loop
> Data (J) := Data (J - 1) * Value;
> end loop;
> for Item of reverse Data loop
> Value := Value + Item;
> end loop;
> end Compute;
> 
> ```

Do I really need 4Kb spare space for 40 byte frame? 🙄

---

<div class="post-metadata">

### Author: ![liampwll](https://forum.ada-lang.io/letter_avatar_proxy/v4/letter/l/b19c9b/32.png) [@liampwll](https://forum.ada-lang.io/u/liampwll)
#### Post date: [April 10, 2026, 9:54am UTC](https://forum.ada-lang.io/t/how-to-protect-from-stack-overflow-on-embedded-targets/4331/6 "2026-04-10T09:54:54Z")

</div>

The extra 4kb is so that an exception can be raised with a full backtrace. It’s controlled by STACK\_CHECK\_PROTECT. Note that this does not accumulate for every frame, it’s just one extra bit of space on top of the stack.

---

<div class="post-metadata">

### Author: ![liampwll](https://forum.ada-lang.io/letter_avatar_proxy/v4/letter/l/b19c9b/32.png) [@liampwll](https://forum.ada-lang.io/u/liampwll)
#### Post date: [April 11, 2026, 6:53am UTC](https://forum.ada-lang.io/t/how-to-protect-from-stack-overflow-on-embedded-targets/4331/7 "2026-04-11T06:53:31Z")

</div>

I pulled mucheckstack out of Muen and packaged it with Alire. I think the issue with it for my project was that it didn’t look give separate numbers for each task: [GitHub - liampwll/mucheckstack · GitHub](https://github.com/liampwll/mucheckstack)

---

<div class="post-metadata">

### Author: ![liampwll](https://forum.ada-lang.io/letter_avatar_proxy/v4/letter/l/b19c9b/32.png) [@liampwll](https://forum.ada-lang.io/u/liampwll)
#### Post date: [April 11, 2026, 3:35pm UTC](https://forum.ada-lang.io/t/how-to-protect-from-stack-overflow-on-embedded-targets/4331/8 "2026-04-11T15:35:46Z")

</div>

It looks like it shouldn’t take much work to make it task-aware now that I’m looking closer at it. I’ll work on that at some point. Adding most of the other features from GNATstack should be trivial too.

---

<div class="post-metadata">

### Author: ![sidisyom](https://forum.ada-lang.io/letter_avatar_proxy/v4/letter/s/5fc32e/32.png) [@sidisyom](https://forum.ada-lang.io/u/sidisyom)
#### Post date: [April 12, 2026, 10:49am UTC](https://forum.ada-lang.io/t/how-to-protect-from-stack-overflow-on-embedded-targets/4331/9 "2026-04-12T10:49:57Z")

</div>

Probably largely missing something here but just curious, why can you not override the default stack size on the task that calls `Compute` if that’s too high?

---

<div class="post-metadata">

### Author: ![Max](https://forum.ada-lang.io/user_avatar/forum.ada-lang.io/max/32/21_2.png) [@Max](https://forum.ada-lang.io/u/Max)
#### Post date: [April 13, 2026, 3:44pm UTC](https://forum.ada-lang.io/t/how-to-protect-from-stack-overflow-on-embedded-targets/4331/10 "2026-04-13T15:44:33Z")

</div>

I could specify `task My_Task with Storage_Size => 4384;`, but I don’t want to spend memory on unused stack just to make `__gnat_stack_check` happy, if the task really needs just 1024 bytes or so. Having 10-20 tasks, for example, I will loose 30-60Kb of RAM for nothing. Total stm32f4 memory is 192Kb.

---

<div class="post-metadata">

### Author: ![Max](https://forum.ada-lang.io/user_avatar/forum.ada-lang.io/max/32/21_2.png) [@Max](https://forum.ada-lang.io/u/Max)
#### Post date: [April 13, 2026, 3:45pm UTC](https://forum.ada-lang.io/t/how-to-protect-from-stack-overflow-on-embedded-targets/4331/11 "2026-04-13T15:45:50Z")

</div>

Thank you! I’ve compiled it locally and tried with the example and it works.

---

<div class="post-metadata">

### Author: ![sidisyom](https://forum.ada-lang.io/letter_avatar_proxy/v4/letter/s/5fc32e/32.png) [@sidisyom](https://forum.ada-lang.io/u/sidisyom)
#### Post date: [April 13, 2026, 8:41pm UTC](https://forum.ada-lang.io/t/how-to-protect-from-stack-overflow-on-embedded-targets/4331/12 "2026-04-13T20:41:44Z")

</div>

Just curious, is `4384` a somehow hardcoded value for `__gnat_stack_check`?

If I am reading the above code right, initially the stack pointer gets bumped by `44` bytes:  
`sub sp, sp, #44`  
then `r0` is then set by the value of bumping `sp` a further `4384` bytes:  
`sub r0, sp, #4384`  
which is presumably then used by `__gnat_stack_check` to check for valid bounds? (i.e. its first argument)

Do you know if the source code for `__gnat_stack_check` can be accessed somewhere, by any chance?

Thanks!

---

<div class="post-metadata">

### Author: ![liampwll](https://forum.ada-lang.io/letter_avatar_proxy/v4/letter/l/b19c9b/32.png) [@liampwll](https://forum.ada-lang.io/u/liampwll)
#### Post date: [April 14, 2026, 5:51am UTC](https://forum.ada-lang.io/t/how-to-protect-from-stack-overflow-on-embedded-targets/4331/13 "2026-04-14T05:51:13Z")

</div>

> [@sidisyom](#):
>
> Just curious, is `4384` a somehow hardcoded value for `__gnat_stack_check`?

It is. It comes from the STACK\_CHECK\_PROTECT macro in GCC. Although I don’t know off the top of my head where the extra bit above 4096 is coming from. Refer to: [Stack Checking (GNU Compiler Collection (GCC) Internals)](https://gcc.gnu.org/onlinedocs/gccint/Stack-Checking.html)

> [@sidisyom](#):
>
> Do you know if the source code for `__gnat_stack_check` can be accessed somewhere, by any chance?

It’s in System.Stack\_Checking.Operations.

---

<div class="post-metadata">

### Author: ![sidisyom](https://forum.ada-lang.io/letter_avatar_proxy/v4/letter/s/5fc32e/32.png) [@sidisyom](https://forum.ada-lang.io/u/sidisyom)
#### Post date: [April 14, 2026, 8:56pm UTC](https://forum.ada-lang.io/t/how-to-protect-from-stack-overflow-on-embedded-targets/4331/14 "2026-04-14T20:56:39Z")

</div>

Thanks very much for that. 🙏
