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.
153 (if My_Stack.Limit /= System.Null_Address
154 then My_Stack.Limit else Frame_Address);
156 if Stack_Grows_Down then
158 -- Prevent wrap-around on too big stack sizes
160 My_Stack.Limit := My_Stack.Base - My_Stack.Size;
162 if My_Stack.Limit > My_Stack.Base then
163 My_Stack.Limit := Address'First;
167 My_Stack.Limit := My_Stack.Base + My_Stack.Size;
169 -- Prevent wrap-around on too big stack sizes
171 if My_Stack.Limit < My_Stack.Base then
172 My_Stack.Limit := Address'Last;
177 -- 2) Set Stack.all to the value obtained in 1)
179 Stack.all := My_Stack;
181 -- 3) Optionally Poll to check for asynchronous abort
183 if Soft_Links.Check_Abort_Status.all /= 0 then
184 raise Standard'Abort_Signal;
187 -- Never trust the cached value, but return local copy!
197 (Stack_Address : System.Address) return Stack_Access
199 type Frame_Marker is null record;
200 Marker : Frame_Marker;
201 Cached_Stack : constant Stack_Access := Cache;
202 Frame_Address : constant System.Address := Marker'Address;
205 -- The parameter may have wrapped around in System.Address arithmetics.
206 -- In that case, we have no other choices than raising the exception.
208 if (Stack_Grows_Down and then
209 Stack_Address > Frame_Address)
211 (not Stack_Grows_Down and then
212 Stack_Address < Frame_Address)
214 raise Storage_Error with "stack overflow detected";
217 -- This function first does a "cheap" check which is correct if it
218 -- succeeds. In case of failure, the full check is done. Ideally the
219 -- cheap check should be done in an optimized manner, or be inlined.
221 if (Stack_Grows_Down and then
222 (Frame_Address <= Cached_Stack.Base
224 Stack_Address > Cached_Stack.Limit))
226 (not Stack_Grows_Down and then
227 (Frame_Address >= Cached_Stack.Base
229 Stack_Address < Cached_Stack.Limit))
231 -- Cached_Stack is valid as it passed the stack check
238 My_Stack : constant Stack_Access := Set_Stack_Info (Cache'Access);
239 -- At this point Stack.all might already be invalid, so
240 -- it is essential to use our local copy of Stack!
243 if (Stack_Grows_Down and then
244 (not (Frame_Address <= My_Stack.Base)))
246 (not Stack_Grows_Down and then
247 (not (Frame_Address >= My_Stack.Base)))
249 -- The returned Base is lower than the stored one, so assume that
250 -- the original one wasn't right and use the current Frame_Address
251 -- as new one. This allows Base to be initialized with the
252 -- Frame_Address as approximation. During initialization the
253 -- Frame_Address will be close to the stack base anyway: the
254 -- difference should be compensated for in the stack reserve.
256 My_Stack.Base := Frame_Address;
260 and then Stack_Address < My_Stack.Limit)
262 (not Stack_Grows_Down
263 and then Stack_Address > My_Stack.Limit)
265 raise Storage_Error with "stack overflow detected";
272 ------------------------
273 -- Update_Stack_Cache --
274 ------------------------
276 procedure Update_Stack_Cache (Stack : Stack_Access) is
278 if not Multi_Processor then
281 end Update_Stack_Cache;
283 end System.Stack_Checking.Operations;