2 -- { dg-options "-gnatws" }
4 with Ada
.Strings
.Unbounded
; use Ada
.Strings
.Unbounded
;
5 with Ada
.Text_IO
; use Ada
.Text_IO
;
6 with System
.Parameters
; use System
.Parameters
;
7 with System
.Secondary_Stack
; use System
.Secondary_Stack
;
9 procedure Sec_Stack2
is
10 procedure Overflow_SS_Index
;
11 -- Create a scenario where the frame index of the secondary stack overflows
12 -- while the stack itself uses little memory.
14 -----------------------
15 -- Overflow_SS_Index --
16 -----------------------
18 procedure Overflow_SS_Index
is
19 Max_Iterations
: constant := 20_000
;
20 -- The approximate number of iterations needed to overflow the frame
21 -- index type on a 64bit target.
23 Algn
: constant Positive := Positive (Standard
'Maximum_Alignment);
24 -- The maximum alignment of the target
26 Size
: constant Positive := Positive (Runtime_Default_Sec_Stack_Size
);
27 -- The default size of the secondary stack on the target
29 Base_Str
: constant String (1 .. Size
) := (others => 'a');
30 -- A string big enough to fill the static frame of the secondary stack
32 Small_Str
: constant String (1 .. Algn
) := (others => 'a');
33 -- A string small enough to cause a new round up to the nearest multiple
34 -- of the maximum alignment on the target at each new iteration of the
37 Base_US
: Unbounded_String
:= To_Unbounded_String
(Base_Str
);
38 -- Unbounded version of the base string
40 procedure SS_Print
is new SS_Info
(Put_Line
);
43 for Iteration
in 1 .. Max_Iterations
loop
45 -- Grow the base string by a small amount at each iteration of the
48 Append
(Base_US
, Small_Str
);
50 -- Convert the unbounded base into a new base. This causes routine
51 -- To_String to allocates the new base on the secondary stack. Since
52 -- the new base is slignly bigger than the previous base, the stack
53 -- would have to create a new frame.
55 -- Due to an issue with frame reclamation, the last frame (which is
56 -- also not big enough to fit the new base) is never reclaimed. This
57 -- causes the range of the new frame to shift toward the overflow
58 -- point of the frame index type.
62 New_Base_Str
: constant String := To_String
(Base_US
);
67 Put_Line
("ERROR: SS depleted");
68 Put_Line
("Iteration:" & Iteration
'Img);
69 Put_Line
("SS_Size :" & Size
'Img);
70 Put_Line
("SS_Algn :" & Algn
'Img);
76 Put_Line
("ERROR: unexpected exception");
80 end Overflow_SS_Index
;
82 -- Start of processing for SS_Depletion
85 -- This issue manifests only on targets with a dynamic secondary stack
87 if Sec_Stack_Dynamic
then