OSDN Git Service

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