2003-11-27 Guilhem Lavaux <guilhem@kaffe.org>
[official-gcc.git] / gcc / ada / s-stache.adb
blob65e816b654f50a726f398015d7ebab06567d3cc8
1 ------------------------------------------------------------------------------
2 -- --
3 -- GNU ADA RUN-TIME LIBRARY (GNARL) COMPONENTS --
4 -- --
5 -- S Y S T E M . S T A C K _ C H E C K I N G --
6 -- --
7 -- B o d y --
8 -- --
9 -- Copyright (C) 1999-2002 Free Software Foundation, Inc. --
10 -- --
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 2, or (at your option) any later ver- --
14 -- sion. GNARL 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. See the GNU General Public License --
17 -- for more details. You should have received a copy of the GNU General --
18 -- Public License distributed with GNARL; see file COPYING. If not, write --
19 -- to the Free Software Foundation, 59 Temple Place - Suite 330, Boston, --
20 -- MA 02111-1307, USA. --
21 -- --
22 -- As a special exception, if other files instantiate generics from this --
23 -- unit, or you link this unit with other files to produce an executable, --
24 -- this unit does not by itself cause the resulting executable to be --
25 -- covered by the GNU General Public License. This exception does not --
26 -- however invalidate any other reasons why the executable file might be --
27 -- covered by the GNU Public License. --
28 -- --
29 -- GNARL was developed by the GNARL team at Florida State University. --
30 -- Extensive contributions were provided by Ada Core Technologies, Inc. --
31 -- --
32 ------------------------------------------------------------------------------
34 with Ada.Exceptions;
36 with System.Storage_Elements; use System.Storage_Elements;
37 with System.Parameters; use System.Parameters;
38 with System.Soft_Links;
40 package body System.Stack_Checking is
42 Kilobyte : constant := 1024;
44 function Set_Stack_Info (Stack : access Stack_Access) return Stack_Access;
46 -- The function Set_Stack_Info is the actual function that updates
47 -- the cache containing a pointer to the Stack_Info. It may also
48 -- be used for detecting asynchronous abort in combination with
49 -- Invalidate_Self_Cache.
51 -- Set_Stack_Info should do the following things in order:
52 -- 1) Get the Stack_Access value for the current task
53 -- 2) Set Stack.all to the value obtained in 1)
54 -- 3) Optionally Poll to check for asynchronous abort
56 -- This order is important because if at any time a write to
57 -- the stack cache is pending, that write should be followed
58 -- by a Poll to prevent loosing signals.
60 -- Note: This function must be compiled with Polling turned off
62 -- Note: on systems like VxWorks and OS/2 with real thread-local storage,
63 -- Set_Stack_Info should return an access value for such local
64 -- storage. In those cases the cache will always be up-to-date.
66 -- The following constants should be imported from some system-specific
67 -- constants package. The constants must be static for performance reasons.
69 ----------------------------
70 -- Invalidate_Stack_Cache --
71 ----------------------------
73 procedure Invalidate_Stack_Cache (Any_Stack : Stack_Access) is
74 pragma Warnings (Off, Any_Stack);
76 begin
77 Cache := Null_Stack;
78 end Invalidate_Stack_Cache;
80 --------------------
81 -- Set_Stack_Info --
82 --------------------
84 function Set_Stack_Info
85 (Stack : access Stack_Access)
86 return Stack_Access
88 type Frame_Mark is null record;
89 Frame_Location : Frame_Mark;
90 Frame_Address : constant Address := Frame_Location'Address;
92 My_Stack : Stack_Access;
93 Limit_Chars : System.Address;
94 Limit : Integer;
96 function getenv (S : String) return System.Address;
97 pragma Import (C, getenv, External_Name => "getenv");
99 function atoi (A : System.Address) return Integer;
100 pragma Import (C, atoi);
102 begin
103 -- The order of steps 1 .. 3 is important, see specification.
105 -- 1) Get the Stack_Access value for the current task
107 My_Stack := Soft_Links.Get_Stack_Info.all;
109 if My_Stack.Base = Null_Address then
111 -- First invocation, initialize based on the assumption that
112 -- there are Environment_Stack_Size bytes available beyond
113 -- the current frame address.
115 if My_Stack.Size = 0 then
117 My_Stack.Size := Storage_Offset (Default_Env_Stack_Size);
119 -- When the environment variable GNAT_STACK_LIMIT is set,
120 -- set Environment_Stack_Size to that number of kB.
122 Limit_Chars := getenv ("GNAT_STACK_LIMIT" & ASCII.NUL);
124 if Limit_Chars /= Null_Address then
125 Limit := atoi (Limit_Chars);
126 if Limit >= 0 then
127 My_Stack.Size := Storage_Offset (Limit) * Kilobyte;
128 end if;
129 end if;
130 end if;
132 My_Stack.Base := Frame_Address;
134 if Stack_Grows_Down then
136 -- Prevent wrap-around on too big stack sizes
138 My_Stack.Limit := My_Stack.Base - My_Stack.Size;
140 if My_Stack.Limit > My_Stack.Base then
141 My_Stack.Limit := Address'First;
142 end if;
144 else
145 My_Stack.Limit := My_Stack.Base + My_Stack.Size;
147 -- Prevent wrap-around on too big stack sizes
149 if My_Stack.Limit < My_Stack.Base then
150 My_Stack.Limit := Address'Last;
151 end if;
152 end if;
153 end if;
155 -- 2) Set Stack.all to the value obtained in 1)
157 Stack.all := My_Stack;
159 -- 3) Optionally Poll to check for asynchronous abort
161 if Soft_Links.Check_Abort_Status.all /= 0 then
162 raise Standard'Abort_Signal;
163 end if;
165 return My_Stack; -- Never trust the cached value, but return local copy!
166 end Set_Stack_Info;
168 --------------------
169 -- Set_Stack_Size --
170 --------------------
172 -- Specify the stack size for the current frame.
174 procedure Set_Stack_Size
175 (Stack_Size : System.Storage_Elements.Storage_Offset)
177 My_Stack : Stack_Access;
178 Frame_Address : constant System.Address := My_Stack'Address;
180 begin
181 My_Stack := Stack_Check (Frame_Address);
183 if Stack_Grows_Down then
184 My_Stack.Limit := My_Stack.Base - Stack_Size;
185 else
186 My_Stack.Limit := My_Stack.Base + Stack_Size;
187 end if;
188 end Set_Stack_Size;
190 -----------------
191 -- Stack_Check --
192 -----------------
194 function Stack_Check
195 (Stack_Address : System.Address)
196 return Stack_Access
198 type Frame_Marker is null record;
199 Marker : Frame_Marker;
200 Cached_Stack : constant Stack_Access := Cache;
201 Frame_Address : constant System.Address := Marker'Address;
203 begin
204 -- This function first does a "cheap" check which is correct
205 -- if it succeeds. In case of failure, the full check is done.
206 -- Ideally the cheap check should be done in an optimized manner,
207 -- or be inlined.
209 if (Stack_Grows_Down and then
210 (Frame_Address <= Cached_Stack.Base
212 Stack_Address > Cached_Stack.Limit))
213 or else
214 (not Stack_Grows_Down and then
215 (Frame_Address >= Cached_Stack.Base
217 Stack_Address < Cached_Stack.Limit))
218 then
219 -- Cached_Stack is valid as it passed the stack check
220 return Cached_Stack;
221 end if;
223 Full_Check :
224 declare
225 My_Stack : Stack_Access := Set_Stack_Info (Cache'Access);
226 -- At this point Stack.all might already be invalid, so
227 -- it is essential to use our local copy of Stack!
229 begin
231 if (Stack_Grows_Down and then
232 (not (Frame_Address <= My_Stack.Base)))
233 or else
234 (not Stack_Grows_Down and then
235 (not (Frame_Address >= My_Stack.Base)))
236 then
237 -- The returned Base is lower than the stored one,
238 -- so assume that the original one wasn't right and use the
239 -- current Frame_Address as new one. This allows initializing
240 -- Base with the Frame_Address as approximation.
241 -- During initialization the Frame_Address will be close to
242 -- the stack base anyway: the difference should be compensated
243 -- for in the stack reserve.
245 My_Stack.Base := Frame_Address;
246 end if;
248 if (Stack_Grows_Down and then
249 Stack_Address < My_Stack.Limit)
250 or else
251 (not Stack_Grows_Down and then
252 Stack_Address > My_Stack.Limit)
253 then
254 Ada.Exceptions.Raise_Exception
255 (E => Storage_Error'Identity,
256 Message => "stack overflow detected");
257 end if;
259 return My_Stack;
260 end Full_Check;
261 end Stack_Check;
263 ------------------------
264 -- Update_Stack_Cache --
265 ------------------------
267 procedure Update_Stack_Cache (Stack : Stack_Access) is
268 begin
269 if not Multi_Processor then
270 Cache := Stack;
271 end if;
272 end Update_Stack_Cache;
274 end System.Stack_Checking;