1 ------------------------------------------------------------------------------
3 -- GNAT RUN-TIME LIBRARY (GNARL) COMPONENTS --
5 -- S Y S T E M . S T A C K _ C H E C K I N G . O P E R A T I O N S --
9 -- Copyright (C) 1999-2009, Free Software Foundation, Inc. --
11 -- GNARL is free software; you can redistribute it and/or modify it under --
12 -- terms of the GNU General Public License as published by the Free Soft- --
13 -- ware Foundation; either version 3, or (at your option) any later ver- --
14 -- sion. GNAT is distributed in the hope that it will be useful, but WITH- --
15 -- OUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY --
16 -- or FITNESS FOR A PARTICULAR PURPOSE. --
18 -- As a special exception under Section 7 of GPL version 3, you are granted --
19 -- additional permissions described in the GCC Runtime Library Exception, --
20 -- version 3.1, as published by the Free Software Foundation. --
22 -- You should have received a copy of the GNU General Public License and --
23 -- a copy of the GCC Runtime Library Exception along with this program; --
24 -- see the files COPYING3 and COPYING.RUNTIME respectively. If not, see --
25 -- <http://www.gnu.org/licenses/>. --
27 -- GNARL was developed by the GNARL team at Florida State University. --
28 -- Extensive contributions were provided by Ada Core Technologies, Inc. --
30 ------------------------------------------------------------------------------
32 -- This is the general implementation of this package. There is a VxWorks
33 -- specific version of this package (s-stchop-vxworks.adb). This file should
34 -- be kept synchronized with it.
36 pragma Restrictions (No_Elaboration_Code);
37 -- We want to guarantee the absence of elaboration code because the
38 -- binder does not handle references to this package.
40 with System.Storage_Elements; use System.Storage_Elements;
41 with System.Parameters; use System.Parameters;
42 with System.Soft_Links;
45 package body System.Stack_Checking.Operations is
47 Kilobyte : constant := 1024;
49 function Set_Stack_Info
50 (Stack : not null access Stack_Access) return Stack_Access;
52 -- The function Set_Stack_Info is the actual function that updates the
53 -- cache containing a pointer to the Stack_Info. It may also be used for
54 -- detecting asynchronous abort in combination with Invalidate_Self_Cache.
56 -- Set_Stack_Info should do the following things in order:
57 -- 1) Get the Stack_Access value for the current task
58 -- 2) Set Stack.all to the value obtained in 1)
59 -- 3) Optionally Poll to check for asynchronous abort
61 -- This order is important because if at any time a write to the stack
62 -- cache is pending, that write should be followed by a Poll to prevent
65 -- Note: This function must be compiled with Polling turned off
67 -- Note: on systems like VxWorks and OS/2 with real thread-local storage,
68 -- Set_Stack_Info should return an access value for such local
69 -- storage. In those cases the cache will always be up-to-date.
71 -- The following constants should be imported from some system-specific
72 -- constants package. The constants must be static for performance reasons.
74 ----------------------------
75 -- Invalidate_Stack_Cache --
76 ----------------------------
78 procedure Invalidate_Stack_Cache (Any_Stack : Stack_Access) is
79 pragma Warnings (Off, Any_Stack);
82 end Invalidate_Stack_Cache;
84 -----------------------------
85 -- Notify_Stack_Attributes --
86 -----------------------------
88 procedure Notify_Stack_Attributes
89 (Initial_SP : System.Address;
90 Size : System.Storage_Elements.Storage_Offset)
92 My_Stack : constant Stack_Access := Soft_Links.Get_Stack_Info.all;
94 -- We piggyback on the 'Limit' field to store what will be used as the
95 -- 'Base' and leave the 'Size' alone to not interfere with the logic in
96 -- Set_Stack_Info below.
98 pragma Unreferenced (Size);
101 My_Stack.Limit := Initial_SP;
102 end Notify_Stack_Attributes;
108 function Set_Stack_Info
109 (Stack : not null access Stack_Access) return Stack_Access
111 type Frame_Mark is null record;
112 Frame_Location : Frame_Mark;
113 Frame_Address : constant Address := Frame_Location'Address;
115 My_Stack : Stack_Access;
116 Limit_Chars : System.Address;
120 -- The order of steps 1 .. 3 is important, see specification
122 -- 1) Get the Stack_Access value for the current task
124 My_Stack := Soft_Links.Get_Stack_Info.all;
126 if My_Stack.Base = Null_Address then
128 -- First invocation, initialize based on the assumption that there
129 -- are Environment_Stack_Size bytes available beyond the current
132 if My_Stack.Size = 0 then
133 My_Stack.Size := Storage_Offset (Default_Env_Stack_Size);
135 -- When the environment variable GNAT_STACK_LIMIT is set, set
136 -- Environment_Stack_Size to that number of kB.
138 Limit_Chars := System.CRTL.getenv ("GNAT_STACK_LIMIT" & ASCII.NUL);
140 if Limit_Chars /= Null_Address then
141 Limit := System.CRTL.atoi (Limit_Chars);
144 My_Stack.Size := Storage_Offset (Limit) * Kilobyte;
149 -- If a stack base address has been registered, honor it. Fallback to
150 -- the address of a local object otherwise.
152 if My_Stack.Limit /= System.Null_Address then
153 My_Stack.Base := My_Stack.Limit;
155 My_Stack.Base := Frame_Address;
158 if Stack_Grows_Down then
160 -- Prevent wrap-around on too big stack sizes
162 My_Stack.Limit := My_Stack.Base - My_Stack.Size;
164 if My_Stack.Limit > My_Stack.Base then
165 My_Stack.Limit := Address'First;
169 My_Stack.Limit := My_Stack.Base + My_Stack.Size;
171 -- Prevent wrap-around on too big stack sizes
173 if My_Stack.Limit < My_Stack.Base then
174 My_Stack.Limit := Address'Last;
179 -- 2) Set Stack.all to the value obtained in 1)
181 Stack.all := My_Stack;
183 -- 3) Optionally Poll to check for asynchronous abort
185 if Soft_Links.Check_Abort_Status.all /= 0 then
186 raise Standard'Abort_Signal;
189 -- Never trust the cached value, but return local copy!
199 (Stack_Address : System.Address) return Stack_Access
201 type Frame_Marker is null record;
202 Marker : Frame_Marker;
203 Cached_Stack : constant Stack_Access := Cache;
204 Frame_Address : constant System.Address := Marker'Address;
207 -- The parameter may have wrapped around in System.Address arithmetics.
208 -- In that case, we have no other choices than raising the exception.
210 if (Stack_Grows_Down and then
211 Stack_Address > Frame_Address)
213 (not Stack_Grows_Down and then
214 Stack_Address < Frame_Address)
216 raise Storage_Error with "stack overflow detected";
219 -- This function first does a "cheap" check which is correct if it
220 -- succeeds. In case of failure, the full check is done. Ideally the
221 -- cheap check should be done in an optimized manner, or be inlined.
223 if (Stack_Grows_Down and then
224 (Frame_Address <= Cached_Stack.Base
226 Stack_Address > Cached_Stack.Limit))
228 (not Stack_Grows_Down and then
229 (Frame_Address >= Cached_Stack.Base
231 Stack_Address < Cached_Stack.Limit))
233 -- Cached_Stack is valid as it passed the stack check
240 My_Stack : constant Stack_Access := Set_Stack_Info (Cache'Access);
241 -- At this point Stack.all might already be invalid, so
242 -- it is essential to use our local copy of Stack!
245 if (Stack_Grows_Down and then
246 (not (Frame_Address <= My_Stack.Base)))
248 (not Stack_Grows_Down and then
249 (not (Frame_Address >= My_Stack.Base)))
251 -- The returned Base is lower than the stored one, so assume that
252 -- the original one wasn't right and use the current Frame_Address
253 -- as new one. This allows Base to be initialized with the
254 -- Frame_Address as approximation. During initialization the
255 -- Frame_Address will be close to the stack base anyway: the
256 -- difference should be compensated for in the stack reserve.
258 My_Stack.Base := Frame_Address;
262 and then Stack_Address < My_Stack.Limit)
264 (not Stack_Grows_Down
265 and then Stack_Address > My_Stack.Limit)
267 raise Storage_Error with "stack overflow detected";
274 ------------------------
275 -- Update_Stack_Cache --
276 ------------------------
278 procedure Update_Stack_Cache (Stack : Stack_Access) is
280 if not Multi_Processor then
283 end Update_Stack_Cache;
285 end System.Stack_Checking.Operations;