OSDN Git Service

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