1 ------------------------------------------------------------------------------
3 -- GNU ADA 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 --
11 -- Copyright (C) 1999-2001 Free Software Foundation, Inc. --
13 -- GNARL is free software; you can redistribute it and/or modify it under --
14 -- terms of the GNU General Public License as published by the Free Soft- --
15 -- ware Foundation; either version 2, or (at your option) any later ver- --
16 -- sion. GNARL is distributed in the hope that it will be useful, but WITH- --
17 -- OUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY --
18 -- or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License --
19 -- for more details. You should have received a copy of the GNU General --
20 -- Public License distributed with GNARL; see file COPYING. If not, write --
21 -- to the Free Software Foundation, 59 Temple Place - Suite 330, Boston, --
22 -- MA 02111-1307, USA. --
24 -- As a special exception, if other files instantiate generics from this --
25 -- unit, or you link this unit with other files to produce an executable, --
26 -- this unit does not by itself cause the resulting executable to be --
27 -- covered by the GNU General Public License. This exception does not --
28 -- however invalidate any other reasons why the executable file might be --
29 -- covered by the GNU Public License. --
31 -- GNARL was developed by the GNARL team at Florida State University. It is --
32 -- now maintained by Ada Core Technologies Inc. in cooperation with Florida --
33 -- State University (http://www.gnat.com). --
35 ------------------------------------------------------------------------------
39 with System
.Storage_Elements
; use System
.Storage_Elements
;
40 with System
.Parameters
; use System
.Parameters
;
41 with System
.Soft_Links
;
43 package body System
.Stack_Checking
is
45 Kilobyte
: constant Storage_Offset
:= 1024;
46 Default_Env_Stack_Size
: constant Storage_Offset
:= 8000 * Kilobyte
;
47 -- This size is assumed for the environment stack when no size has been
48 -- set by the runtime, and no GNAT_STACK_LIMIT environment variable was
49 -- present. The value is chosen to be just under 8 MB whic is the actual
50 -- default size on some systems including GNU/LinuxThreads, so we will get
51 -- correct storage errors on those systems without setting environment
54 function Set_Stack_Info
(Stack
: access Stack_Access
) return Stack_Access
;
56 -- The function Set_Stack_Info is the actual function that updates
57 -- the cache containing a pointer to the Stack_Info. It may also
58 -- be used for detecting asynchronous abort in combination with
59 -- Invalidate_Self_Cache.
61 -- Set_Stack_Info should do the following things in order:
62 -- 1) Get the Stack_Access value for the current task
63 -- 2) Set Stack.all to the value obtained in 1)
64 -- 3) Optionally Poll to check for asynchronous abort
66 -- This order is important because if at any time a write to
67 -- the stack cache is pending, that write should be followed
68 -- by a Poll to prevent loosing signals.
70 -- Note: This function must be compiled with Polling turned off
72 -- Note: on systems like VxWorks and OS/2 with real thread-local storage,
73 -- Set_Stack_Info should return an access value for such local
74 -- storage. In those cases the cache will always be up-to-date.
76 -- The following constants should be imported from some system-specific
77 -- constants package. The constants must be static for performance reasons.
79 ----------------------------
80 -- Invalidate_Stack_Cache --
81 ----------------------------
83 procedure Invalidate_Stack_Cache
(Any_Stack
: Stack_Access
) is
86 end Invalidate_Stack_Cache
;
92 function Set_Stack_Info
93 (Stack
: access Stack_Access
)
96 type Frame_Mark
is null record;
97 Frame_Location
: Frame_Mark
;
98 Frame_Address
: Address
:= Frame_Location
'Address;
100 My_Stack
: Stack_Access
;
101 Limit_Chars
: System
.Address
;
104 function getenv
(S
: String) return System
.Address
;
105 pragma Import
(C
, getenv
, External_Name
=> "getenv");
107 function atoi
(A
: System
.Address
) return Integer;
108 pragma Import
(C
, atoi
);
111 -- The order of steps 1 .. 3 is important, see specification.
113 -- 1) Get the Stack_Access value for the current task
115 My_Stack
:= Soft_Links
.Get_Stack_Info
.all;
117 if My_Stack
.Base
= Null_Address
then
119 -- First invocation, initialize based on the assumption that
120 -- there are Environment_Stack_Size bytes available beyond
121 -- the current frame address.
123 if My_Stack
.Size
= 0 then
125 My_Stack
.Size
:= Default_Env_Stack_Size
;
127 -- When the environment variable GNAT_STACK_LIMIT is set,
128 -- set Environment_Stack_Size to that number of kB.
130 Limit_Chars
:= getenv
("GNAT_STACK_LIMIT" & ASCII
.NUL
);
132 if Limit_Chars
/= Null_Address
then
133 Limit
:= atoi
(Limit_Chars
);
135 My_Stack
.Size
:= Storage_Offset
(Limit
) * Kilobyte
;
140 My_Stack
.Base
:= Frame_Address
;
142 if Stack_Grows_Down
then
144 -- Prevent wrap-around on too big stack sizes
146 My_Stack
.Limit
:= My_Stack
.Base
- My_Stack
.Size
;
148 if My_Stack
.Limit
> My_Stack
.Base
then
149 My_Stack
.Limit
:= Address
'First;
153 My_Stack
.Limit
:= My_Stack
.Base
+ My_Stack
.Size
;
155 -- Prevent wrap-around on too big stack sizes
157 if My_Stack
.Limit
< My_Stack
.Base
then
158 My_Stack
.Limit
:= Address
'Last;
163 -- 2) Set Stack.all to the value obtained in 1)
165 Stack
.all := My_Stack
;
167 -- 3) Optionally Poll to check for asynchronous abort
169 if Soft_Links
.Check_Abort_Status
.all /= 0 then
170 raise Standard
'Abort_Signal;
173 return My_Stack
; -- Never trust the cached value, but return local copy!
180 -- Specify the stack size for the current frame.
182 procedure Set_Stack_Size
183 (Stack_Size
: System
.Storage_Elements
.Storage_Offset
)
185 My_Stack
: Stack_Access
;
186 Frame_Address
: constant System
.Address
:= My_Stack
'Address;
189 My_Stack
:= Stack_Check
(Frame_Address
);
191 if Stack_Grows_Down
then
192 My_Stack
.Limit
:= My_Stack
.Base
- Stack_Size
;
194 My_Stack
.Limit
:= My_Stack
.Base
+ Stack_Size
;
203 (Stack_Address
: System
.Address
)
206 type Frame_Marker
is null record;
207 Marker
: Frame_Marker
;
208 Cached_Stack
: constant Stack_Access
:= Cache
;
209 Frame_Address
: constant System
.Address
:= Marker
'Address;
212 -- This function first does a "cheap" check which is correct
213 -- if it succeeds. In case of failure, the full check is done.
214 -- Ideally the cheap check should be done in an optimized manner,
217 if (Stack_Grows_Down
and then
218 (Frame_Address
<= Cached_Stack
.Base
220 Stack_Address
> Cached_Stack
.Limit
))
222 (not Stack_Grows_Down
and then
223 (Frame_Address
>= Cached_Stack
.Base
225 Stack_Address
< Cached_Stack
.Limit
))
227 -- Cached_Stack is valid as it passed the stack check
233 My_Stack
: Stack_Access
:= Set_Stack_Info
(Cache
'Access);
234 -- At this point Stack.all might already be invalid, so
235 -- it is essential to use our local copy of Stack!
239 if (Stack_Grows_Down
and then
240 (not (Frame_Address
<= My_Stack
.Base
)))
242 (not Stack_Grows_Down
and then
243 (not (Frame_Address
>= My_Stack
.Base
)))
245 -- The returned Base is lower than the stored one,
246 -- so assume that the original one wasn't right and use the
247 -- current Frame_Address as new one. This allows initializing
248 -- Base with the Frame_Address as approximation.
249 -- During initialization the Frame_Address will be close to
250 -- the stack base anyway: the difference should be compensated
251 -- for in the stack reserve.
253 My_Stack
.Base
:= Frame_Address
;
256 if (Stack_Grows_Down
and then
257 Stack_Address
< My_Stack
.Limit
)
259 (not Stack_Grows_Down
and then
260 Stack_Address
> My_Stack
.Limit
)
262 Ada
.Exceptions
.Raise_Exception
263 (E
=> Storage_Error
'Identity,
264 Message
=> "stack overflow detected");
271 ------------------------
272 -- Update_Stack_Cache --
273 ------------------------
275 procedure Update_Stack_Cache
(Stack
: Stack_Access
) is
277 if not Multi_Processor
then
280 end Update_Stack_Cache
;
282 end System
.Stack_Checking
;