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
53 -- the cache containing a pointer to the Stack_Info. It may also
54 -- be used for detecting asynchronous abort in combination with
55 -- Invalidate_Self_Cache.
57 -- Set_Stack_Info should do the following things in order:
58 -- 1) Get the Stack_Access value for the current task
59 -- 2) Set Stack.all to the value obtained in 1)
60 -- 3) Optionally Poll to check for asynchronous abort
62 -- This order is important because if at any time a write to
63 -- the stack cache is pending, that write should be followed
64 -- by a Poll to prevent loosing signals.
66 -- Note: This function must be compiled with Polling turned off
68 -- Note: on systems like VxWorks and OS/2 with real thread-local storage,
69 -- Set_Stack_Info should return an access value for such local
70 -- storage. In those cases the cache will always be up-to-date.
72 -- The following constants should be imported from some system-specific
73 -- constants package. The constants must be static for performance reasons.
75 ----------------------------
76 -- Invalidate_Stack_Cache --
77 ----------------------------
79 procedure Invalidate_Stack_Cache (Any_Stack : Stack_Access) is
80 pragma Warnings (Off, Any_Stack);
83 end Invalidate_Stack_Cache;
85 -----------------------------
86 -- Notify_Stack_Attributes --
87 -----------------------------
89 procedure Notify_Stack_Attributes
90 (Initial_SP : System.Address;
91 Size : System.Storage_Elements.Storage_Offset)
93 My_Stack : constant Stack_Access := Soft_Links.Get_Stack_Info.all;
95 -- We piggyback on the 'Limit' field to store what will be used as the
96 -- 'Base' and leave the 'Size' alone to not interfere with the logic in
97 -- Set_Stack_Info below.
99 pragma Unreferenced (Size);
102 My_Stack.Limit := Initial_SP;
103 end Notify_Stack_Attributes;
109 function Set_Stack_Info
110 (Stack : not null access Stack_Access) return Stack_Access
112 type Frame_Mark is null record;
113 Frame_Location : Frame_Mark;
114 Frame_Address : constant Address := Frame_Location'Address;
116 My_Stack : Stack_Access;
117 Limit_Chars : System.Address;
121 -- The order of steps 1 .. 3 is important, see specification
123 -- 1) Get the Stack_Access value for the current task
125 My_Stack := Soft_Links.Get_Stack_Info.all;
127 if My_Stack.Base = Null_Address then
129 -- First invocation, initialize based on the assumption that
130 -- there are Environment_Stack_Size bytes available beyond
131 -- the current frame address.
133 if My_Stack.Size = 0 then
134 My_Stack.Size := Storage_Offset (Default_Env_Stack_Size);
136 -- When the environment variable GNAT_STACK_LIMIT is set,
137 -- set Environment_Stack_Size to that number of kB.
139 Limit_Chars := System.CRTL.getenv ("GNAT_STACK_LIMIT" & ASCII.NUL);
141 if Limit_Chars /= Null_Address then
142 Limit := System.CRTL.atoi (Limit_Chars);
145 My_Stack.Size := Storage_Offset (Limit) * Kilobyte;
150 -- If a stack base address has been registered, honor it.
151 -- Fallback to the address of a local object otherwise.
153 if My_Stack.Limit /= System.Null_Address then
154 My_Stack.Base := My_Stack.Limit;
156 My_Stack.Base := Frame_Address;
159 if Stack_Grows_Down then
161 -- Prevent wrap-around on too big stack sizes
163 My_Stack.Limit := My_Stack.Base - My_Stack.Size;
165 if My_Stack.Limit > My_Stack.Base then
166 My_Stack.Limit := Address'First;
170 My_Stack.Limit := My_Stack.Base + My_Stack.Size;
172 -- Prevent wrap-around on too big stack sizes
174 if My_Stack.Limit < My_Stack.Base then
175 My_Stack.Limit := Address'Last;
180 -- 2) Set Stack.all to the value obtained in 1)
182 Stack.all := My_Stack;
184 -- 3) Optionally Poll to check for asynchronous abort
186 if Soft_Links.Check_Abort_Status.all /= 0 then
187 raise Standard'Abort_Signal;
190 return My_Stack; -- Never trust the cached value, but return local copy!
198 (Stack_Address : System.Address) return Stack_Access
200 type Frame_Marker is null record;
201 Marker : Frame_Marker;
202 Cached_Stack : constant Stack_Access := Cache;
203 Frame_Address : constant System.Address := Marker'Address;
206 -- The parameter may have wrapped around in System.Address arithmetics.
207 -- In that case, we have no other choices than raising the exception.
209 if (Stack_Grows_Down and then
210 Stack_Address > Frame_Address)
212 (not Stack_Grows_Down and then
213 Stack_Address < Frame_Address)
215 raise Storage_Error with "stack overflow detected";
218 -- This function first does a "cheap" check which is correct
219 -- if it succeeds. In case of failure, the full check is done.
220 -- Ideally the cheap check should be done in an optimized manner,
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
239 My_Stack : constant Stack_Access := Set_Stack_Info (Cache'Access);
240 -- At this point Stack.all might already be invalid, so
241 -- it is essential to use our local copy of Stack!
244 if (Stack_Grows_Down and then
245 (not (Frame_Address <= My_Stack.Base)))
247 (not Stack_Grows_Down and then
248 (not (Frame_Address >= My_Stack.Base)))
250 -- The returned Base is lower than the stored one,
251 -- so assume that the original one wasn't right and use the
252 -- current Frame_Address as new one. This allows initializing
253 -- Base with the Frame_Address as approximation.
254 -- During initialization the Frame_Address will be close to
255 -- the stack base anyway: the difference should be compensated
256 -- for in the stack reserve.
258 My_Stack.Base := Frame_Address;
261 if (Stack_Grows_Down and then
262 Stack_Address < My_Stack.Limit)
264 (not Stack_Grows_Down and then
265 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;