2.8. Checking for Stack Overflow

In Version 1.7, stack checks are included by default. To suppress all checks use the compiler option -gnatp. The stack limit, which is the lowest address in the stack, is held in global _stack_limit. Here is an example that overflows the 8K byte main program stack:

Example 2-9. Stack Overflow Check

$ more biggy.adb
procedure Biggy is
   S : String (1 .. 10_000);
begin
   null;
end Biggy;
$ m1750-coff-gnatmake -g biggy
biggy.adb:2:04: warning: "S" is never assigned a value
m1750-coff-gnatbind -x biggy.ali
m1750-coff-gnatlink -g biggy.ali
$ m1750-coff-run biggy
Unhandled exception at AS=0 IC=02E2 (000005c4): Storage_Error
_ada_biggy():
.../examples/biggy.adb:2

The global variable _stack_limit is initialized in the run-time system module art0.S. The value is computed from the stack bounds declared in the linker script file, and stored with the most significant bit inverted. This is to save instructions when making an unsigned comparison between the limit and the stack pointer.

Note that _stack_limit is set correctly for the main program, for interrupt handlers which use the interrupt stack, and for any Ada tasks, which have their own stacks.