OSDN Git Service

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