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-2007, 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 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, 51 Franklin Street, Fifth Floor, --
20 -- Boston, MA 02110-1301, USA. --
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. --
29 -- GNARL was developed by the GNARL team at Florida State University. --
30 -- Extensive contributions were provided by Ada Core Technologies, Inc. --
32 ------------------------------------------------------------------------------
34 -- This is the general implementation of this package. There is a VxWorks
35 -- specific version of this package (s-stchop-vxworks.adb). This file should
36 -- be kept synchronized with it.
38 pragma Restrictions
(No_Elaboration_Code
);
39 -- We want to guarantee the absence of elaboration code because the
40 -- binder does not handle references to this package.
44 with System
.Storage_Elements
; use System
.Storage_Elements
;
45 with System
.Parameters
; use System
.Parameters
;
46 with System
.Soft_Links
;
49 package body System
.Stack_Checking
.Operations
is
51 Kilobyte
: constant := 1024;
53 function Set_Stack_Info
54 (Stack
: not null 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
84 pragma Warnings
(Off
, Any_Stack
);
87 end Invalidate_Stack_Cache
;
89 -----------------------------
90 -- Notify_Stack_Attributes --
91 -----------------------------
93 procedure Notify_Stack_Attributes
94 (Initial_SP
: System
.Address
;
95 Size
: System
.Storage_Elements
.Storage_Offset
)
97 My_Stack
: constant Stack_Access
:= Soft_Links
.Get_Stack_Info
.all;
99 -- We piggyback on the 'Limit' field to store what will be used as the
100 -- 'Base' and leave the 'Size' alone to not interfere with the logic in
101 -- Set_Stack_Info below.
103 pragma Unreferenced
(Size
);
106 My_Stack
.Limit
:= Initial_SP
;
107 end Notify_Stack_Attributes
;
113 function Set_Stack_Info
114 (Stack
: not null access Stack_Access
) return Stack_Access
116 type Frame_Mark
is null record;
117 Frame_Location
: Frame_Mark
;
118 Frame_Address
: constant Address
:= Frame_Location
'Address;
120 My_Stack
: Stack_Access
;
121 Limit_Chars
: System
.Address
;
125 -- The order of steps 1 .. 3 is important, see specification
127 -- 1) Get the Stack_Access value for the current task
129 My_Stack
:= Soft_Links
.Get_Stack_Info
.all;
131 if My_Stack
.Base
= Null_Address
then
133 -- First invocation, initialize based on the assumption that
134 -- there are Environment_Stack_Size bytes available beyond
135 -- the current frame address.
137 if My_Stack
.Size
= 0 then
138 My_Stack
.Size
:= Storage_Offset
(Default_Env_Stack_Size
);
140 -- When the environment variable GNAT_STACK_LIMIT is set,
141 -- set Environment_Stack_Size to that number of kB.
143 Limit_Chars
:= System
.CRTL
.getenv
("GNAT_STACK_LIMIT" & ASCII
.NUL
);
145 if Limit_Chars
/= Null_Address
then
146 Limit
:= System
.CRTL
.atoi
(Limit_Chars
);
149 My_Stack
.Size
:= Storage_Offset
(Limit
) * Kilobyte
;
154 -- If a stack base address has been registered, honor it.
155 -- Fallback to the address of a local object otherwise.
157 if My_Stack
.Limit
/= System
.Null_Address
then
158 My_Stack
.Base
:= My_Stack
.Limit
;
160 My_Stack
.Base
:= Frame_Address
;
163 if Stack_Grows_Down
then
165 -- Prevent wrap-around on too big stack sizes
167 My_Stack
.Limit
:= My_Stack
.Base
- My_Stack
.Size
;
169 if My_Stack
.Limit
> My_Stack
.Base
then
170 My_Stack
.Limit
:= Address
'First;
174 My_Stack
.Limit
:= My_Stack
.Base
+ My_Stack
.Size
;
176 -- Prevent wrap-around on too big stack sizes
178 if My_Stack
.Limit
< My_Stack
.Base
then
179 My_Stack
.Limit
:= Address
'Last;
184 -- 2) Set Stack.all to the value obtained in 1)
186 Stack
.all := My_Stack
;
188 -- 3) Optionally Poll to check for asynchronous abort
190 if Soft_Links
.Check_Abort_Status
.all /= 0 then
191 raise Standard
'Abort_Signal;
194 return My_Stack
; -- Never trust the cached value, but return local copy!
202 (Stack_Address
: System
.Address
) return Stack_Access
204 type Frame_Marker
is null record;
205 Marker
: Frame_Marker
;
206 Cached_Stack
: constant Stack_Access
:= Cache
;
207 Frame_Address
: constant System
.Address
:= Marker
'Address;
210 -- The parameter may have wrapped around in System.Address arithmetics.
211 -- In that case, we have no other choices than raising the exception.
213 if (Stack_Grows_Down
and then
214 Stack_Address
> Frame_Address
)
216 (not Stack_Grows_Down
and then
217 Stack_Address
< Frame_Address
)
219 Ada
.Exceptions
.Raise_Exception
220 (E
=> Storage_Error
'Identity,
221 Message
=> "stack overflow detected");
224 -- This function first does a "cheap" check which is correct
225 -- if it succeeds. In case of failure, the full check is done.
226 -- Ideally the cheap check should be done in an optimized manner,
229 if (Stack_Grows_Down
and then
230 (Frame_Address
<= Cached_Stack
.Base
232 Stack_Address
> Cached_Stack
.Limit
))
234 (not Stack_Grows_Down
and then
235 (Frame_Address
>= Cached_Stack
.Base
237 Stack_Address
< Cached_Stack
.Limit
))
239 -- Cached_Stack is valid as it passed the stack check
245 My_Stack
: constant Stack_Access
:= Set_Stack_Info
(Cache
'Access);
246 -- At this point Stack.all might already be invalid, so
247 -- it is essential to use our local copy of Stack!
250 if (Stack_Grows_Down
and then
251 (not (Frame_Address
<= My_Stack
.Base
)))
253 (not Stack_Grows_Down
and then
254 (not (Frame_Address
>= My_Stack
.Base
)))
256 -- The returned Base is lower than the stored one,
257 -- so assume that the original one wasn't right and use the
258 -- current Frame_Address as new one. This allows initializing
259 -- Base with the Frame_Address as approximation.
260 -- During initialization the Frame_Address will be close to
261 -- the stack base anyway: the difference should be compensated
262 -- for in the stack reserve.
264 My_Stack
.Base
:= Frame_Address
;
267 if (Stack_Grows_Down
and then
268 Stack_Address
< My_Stack
.Limit
)
270 (not Stack_Grows_Down
and then
271 Stack_Address
> My_Stack
.Limit
)
273 Ada
.Exceptions
.Raise_Exception
274 (E
=> Storage_Error
'Identity,
275 Message
=> "stack overflow detected");
282 ------------------------
283 -- Update_Stack_Cache --
284 ------------------------
286 procedure Update_Stack_Cache
(Stack
: Stack_Access
) is
288 if not Multi_Processor
then
291 end Update_Stack_Cache
;
293 end System
.Stack_Checking
.Operations
;