OSDN Git Service

2008-03-26 Robert Dewar <dewar@adacore.com>
[pf3gnuchains/gcc-fork.git] / gcc / ada / s-stchop.adb
1 ------------------------------------------------------------------------------
2 --                                                                          --
3 --                 GNAT 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 . O P E R A T I O N S      --
6 --                                                                          --
7 --                                  B o d y                                 --
8 --                                                                          --
9 --          Copyright (C) 1999-2007, 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,  51  Franklin  Street,  Fifth  Floor, --
20 -- Boston, MA 02110-1301, 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 ------------------------------------------------------------------------------
33
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.
37
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.
41
42 with System.Storage_Elements; use System.Storage_Elements;
43 with System.Parameters; use System.Parameters;
44 with System.Soft_Links;
45 with System.CRTL;
46
47 package body System.Stack_Checking.Operations is
48
49    Kilobyte : constant := 1024;
50
51    function Set_Stack_Info
52      (Stack : not null access Stack_Access) return Stack_Access;
53
54    --  The function Set_Stack_Info is the actual function that updates
55    --  the cache containing a pointer to the Stack_Info. It may also
56    --  be used for detecting asynchronous abort in combination with
57    --  Invalidate_Self_Cache.
58
59    --  Set_Stack_Info should do the following things in order:
60    --     1) Get the Stack_Access value for the current task
61    --     2) Set Stack.all to the value obtained in 1)
62    --     3) Optionally Poll to check for asynchronous abort
63
64    --  This order is important because if at any time a write to
65    --  the stack cache is pending, that write should be followed
66    --  by a Poll to prevent loosing signals.
67
68    --  Note: This function must be compiled with Polling turned off
69
70    --  Note: on systems like VxWorks and OS/2 with real thread-local storage,
71    --        Set_Stack_Info should return an access value for such local
72    --        storage. In those cases the cache will always be up-to-date.
73
74    --  The following constants should be imported from some system-specific
75    --  constants package. The constants must be static for performance reasons.
76
77    ----------------------------
78    -- Invalidate_Stack_Cache --
79    ----------------------------
80
81    procedure Invalidate_Stack_Cache (Any_Stack : Stack_Access) is
82       pragma Warnings (Off, Any_Stack);
83    begin
84       Cache := Null_Stack;
85    end Invalidate_Stack_Cache;
86
87    -----------------------------
88    -- Notify_Stack_Attributes --
89    -----------------------------
90
91    procedure Notify_Stack_Attributes
92      (Initial_SP : System.Address;
93       Size       : System.Storage_Elements.Storage_Offset)
94    is
95       My_Stack : constant Stack_Access := Soft_Links.Get_Stack_Info.all;
96
97       --  We piggyback on the 'Limit' field to store what will be used as the
98       --  'Base' and leave the 'Size' alone to not interfere with the logic in
99       --  Set_Stack_Info below.
100
101       pragma Unreferenced (Size);
102
103    begin
104       My_Stack.Limit := Initial_SP;
105    end Notify_Stack_Attributes;
106
107    --------------------
108    -- Set_Stack_Info --
109    --------------------
110
111    function Set_Stack_Info
112      (Stack : not null access Stack_Access) return Stack_Access
113    is
114       type Frame_Mark is null record;
115       Frame_Location : Frame_Mark;
116       Frame_Address  : constant Address := Frame_Location'Address;
117
118       My_Stack    : Stack_Access;
119       Limit_Chars : System.Address;
120       Limit       : Integer;
121
122    begin
123       --  The order of steps 1 .. 3 is important, see specification
124
125       --  1) Get the Stack_Access value for the current task
126
127       My_Stack := Soft_Links.Get_Stack_Info.all;
128
129       if My_Stack.Base = Null_Address then
130
131          --  First invocation, initialize based on the assumption that
132          --  there are Environment_Stack_Size bytes available beyond
133          --  the current frame address.
134
135          if My_Stack.Size = 0 then
136             My_Stack.Size := Storage_Offset (Default_Env_Stack_Size);
137
138             --  When the environment variable GNAT_STACK_LIMIT is set,
139             --  set Environment_Stack_Size to that number of kB.
140
141             Limit_Chars := System.CRTL.getenv ("GNAT_STACK_LIMIT" & ASCII.NUL);
142
143             if Limit_Chars /= Null_Address then
144                Limit := System.CRTL.atoi (Limit_Chars);
145
146                if Limit >= 0 then
147                   My_Stack.Size := Storage_Offset (Limit) * Kilobyte;
148                end if;
149             end if;
150          end if;
151
152          --  If a stack base address has been registered, honor it.
153          --  Fallback to the address of a local object otherwise.
154
155          if My_Stack.Limit /= System.Null_Address then
156             My_Stack.Base := My_Stack.Limit;
157          else
158             My_Stack.Base := Frame_Address;
159          end if;
160
161          if Stack_Grows_Down then
162
163             --  Prevent wrap-around on too big stack sizes
164
165             My_Stack.Limit := My_Stack.Base - My_Stack.Size;
166
167             if My_Stack.Limit > My_Stack.Base then
168                My_Stack.Limit := Address'First;
169             end if;
170
171          else
172             My_Stack.Limit := My_Stack.Base + My_Stack.Size;
173
174             --  Prevent wrap-around on too big stack sizes
175
176             if My_Stack.Limit < My_Stack.Base then
177                My_Stack.Limit := Address'Last;
178             end if;
179          end if;
180       end if;
181
182       --  2) Set Stack.all to the value obtained in 1)
183
184       Stack.all := My_Stack;
185
186       --  3) Optionally Poll to check for asynchronous abort
187
188       if Soft_Links.Check_Abort_Status.all /= 0 then
189          raise Standard'Abort_Signal;
190       end if;
191
192       return My_Stack; -- Never trust the cached value, but return local copy!
193    end Set_Stack_Info;
194
195    -----------------
196    -- Stack_Check --
197    -----------------
198
199    function Stack_Check
200      (Stack_Address : System.Address) return Stack_Access
201    is
202       type Frame_Marker is null record;
203       Marker        : Frame_Marker;
204       Cached_Stack  : constant Stack_Access := Cache;
205       Frame_Address : constant System.Address := Marker'Address;
206
207    begin
208       --  The parameter may have wrapped around in System.Address arithmetics.
209       --  In that case, we have no other choices than raising the exception.
210
211       if (Stack_Grows_Down and then
212             Stack_Address > Frame_Address)
213         or else
214          (not Stack_Grows_Down and then
215             Stack_Address < Frame_Address)
216       then
217          raise Storage_Error with "stack overflow detected";
218       end if;
219
220       --  This function first does a "cheap" check which is correct
221       --  if it succeeds. In case of failure, the full check is done.
222       --  Ideally the cheap check should be done in an optimized manner,
223       --  or be inlined.
224
225       if (Stack_Grows_Down and then
226             (Frame_Address <= Cached_Stack.Base
227                and
228              Stack_Address > Cached_Stack.Limit))
229         or else
230          (not Stack_Grows_Down and then
231             (Frame_Address >= Cached_Stack.Base
232                and
233              Stack_Address < Cached_Stack.Limit))
234       then
235          --  Cached_Stack is valid as it passed the stack check
236          return Cached_Stack;
237       end if;
238
239       Full_Check :
240       declare
241          My_Stack : constant Stack_Access := Set_Stack_Info (Cache'Access);
242          --  At this point Stack.all might already be invalid, so
243          --  it is essential to use our local copy of Stack!
244
245       begin
246          if (Stack_Grows_Down and then
247                (not (Frame_Address <= My_Stack.Base)))
248            or else
249             (not Stack_Grows_Down and then
250                (not (Frame_Address >= My_Stack.Base)))
251          then
252             --  The returned Base is lower than the stored one,
253             --  so assume that the original one wasn't right and use the
254             --  current Frame_Address as new one. This allows initializing
255             --  Base with the Frame_Address as approximation.
256             --  During initialization the Frame_Address will be close to
257             --  the stack base anyway: the difference should be compensated
258             --  for in the stack reserve.
259
260             My_Stack.Base := Frame_Address;
261          end if;
262
263          if (Stack_Grows_Down and then
264                   Stack_Address < My_Stack.Limit)
265            or else
266             (not Stack_Grows_Down and then
267                   Stack_Address > My_Stack.Limit)
268          then
269             raise Storage_Error with "stack overflow detected";
270          end if;
271
272          return My_Stack;
273       end Full_Check;
274    end Stack_Check;
275
276    ------------------------
277    -- Update_Stack_Cache --
278    ------------------------
279
280    procedure Update_Stack_Cache (Stack : Stack_Access) is
281    begin
282       if not Multi_Processor then
283          Cache := Stack;
284       end if;
285    end Update_Stack_Cache;
286
287 end System.Stack_Checking.Operations;