annotate gcc/ada/libgnat/s-stchop.adb @ 111:04ced10e8804

gcc 7
author kono
date Fri, 27 Oct 2017 22:46:09 +0900
parents
children 84e7813d76e9
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
111
kono
parents:
diff changeset
1 ------------------------------------------------------------------------------
kono
parents:
diff changeset
2 -- --
kono
parents:
diff changeset
3 -- GNAT RUN-TIME LIBRARY (GNARL) COMPONENTS --
kono
parents:
diff changeset
4 -- --
kono
parents:
diff changeset
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 --
kono
parents:
diff changeset
6 -- --
kono
parents:
diff changeset
7 -- B o d y --
kono
parents:
diff changeset
8 -- --
kono
parents:
diff changeset
9 -- Copyright (C) 1999-2017, Free Software Foundation, Inc. --
kono
parents:
diff changeset
10 -- --
kono
parents:
diff changeset
11 -- GNARL is free software; you can redistribute it and/or modify it under --
kono
parents:
diff changeset
12 -- terms of the GNU General Public License as published by the Free Soft- --
kono
parents:
diff changeset
13 -- ware Foundation; either version 3, or (at your option) any later ver- --
kono
parents:
diff changeset
14 -- sion. GNAT is distributed in the hope that it will be useful, but WITH- --
kono
parents:
diff changeset
15 -- OUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY --
kono
parents:
diff changeset
16 -- or FITNESS FOR A PARTICULAR PURPOSE. --
kono
parents:
diff changeset
17 -- --
kono
parents:
diff changeset
18 -- As a special exception under Section 7 of GPL version 3, you are granted --
kono
parents:
diff changeset
19 -- additional permissions described in the GCC Runtime Library Exception, --
kono
parents:
diff changeset
20 -- version 3.1, as published by the Free Software Foundation. --
kono
parents:
diff changeset
21 -- --
kono
parents:
diff changeset
22 -- You should have received a copy of the GNU General Public License and --
kono
parents:
diff changeset
23 -- a copy of the GCC Runtime Library Exception along with this program; --
kono
parents:
diff changeset
24 -- see the files COPYING3 and COPYING.RUNTIME respectively. If not, see --
kono
parents:
diff changeset
25 -- <http://www.gnu.org/licenses/>. --
kono
parents:
diff changeset
26 -- --
kono
parents:
diff changeset
27 -- GNARL was developed by the GNARL team at Florida State University. --
kono
parents:
diff changeset
28 -- Extensive contributions were provided by Ada Core Technologies, Inc. --
kono
parents:
diff changeset
29 -- --
kono
parents:
diff changeset
30 ------------------------------------------------------------------------------
kono
parents:
diff changeset
31
kono
parents:
diff changeset
32 -- This is the general implementation of this package. There is a VxWorks
kono
parents:
diff changeset
33 -- specific version of this package (s-stchop-vxworks.adb). This file should
kono
parents:
diff changeset
34 -- be kept synchronized with it.
kono
parents:
diff changeset
35
kono
parents:
diff changeset
36 pragma Restrictions (No_Elaboration_Code);
kono
parents:
diff changeset
37 -- We want to guarantee the absence of elaboration code because the
kono
parents:
diff changeset
38 -- binder does not handle references to this package.
kono
parents:
diff changeset
39
kono
parents:
diff changeset
40 with System.Storage_Elements; use System.Storage_Elements;
kono
parents:
diff changeset
41 with System.Parameters; use System.Parameters;
kono
parents:
diff changeset
42 with System.Soft_Links;
kono
parents:
diff changeset
43 with System.CRTL;
kono
parents:
diff changeset
44
kono
parents:
diff changeset
45 package body System.Stack_Checking.Operations is
kono
parents:
diff changeset
46
kono
parents:
diff changeset
47 Kilobyte : constant := 1024;
kono
parents:
diff changeset
48
kono
parents:
diff changeset
49 function Set_Stack_Info
kono
parents:
diff changeset
50 (Stack : not null access Stack_Access) return Stack_Access;
kono
parents:
diff changeset
51 -- The function Set_Stack_Info is the actual function that updates the
kono
parents:
diff changeset
52 -- cache containing a pointer to the Stack_Info. It may also be used for
kono
parents:
diff changeset
53 -- detecting asynchronous abort in combination with Invalidate_Self_Cache.
kono
parents:
diff changeset
54 --
kono
parents:
diff changeset
55 -- Set_Stack_Info should do the following things in order:
kono
parents:
diff changeset
56 -- 1) Get the Stack_Access value for the current task
kono
parents:
diff changeset
57 -- 2) Set Stack.all to the value obtained in 1)
kono
parents:
diff changeset
58 -- 3) Optionally Poll to check for asynchronous abort
kono
parents:
diff changeset
59 --
kono
parents:
diff changeset
60 -- This order is important because if at any time a write to the stack
kono
parents:
diff changeset
61 -- cache is pending, that write should be followed by a Poll to prevent
kono
parents:
diff changeset
62 -- losing signals.
kono
parents:
diff changeset
63 --
kono
parents:
diff changeset
64 -- Note: This function must be compiled with Polling turned off
kono
parents:
diff changeset
65 --
kono
parents:
diff changeset
66 -- Note: on systems with real thread-local storage, Set_Stack_Info should
kono
parents:
diff changeset
67 -- return an access value for such local storage. In those cases the cache
kono
parents:
diff changeset
68 -- will always be up-to-date.
kono
parents:
diff changeset
69
kono
parents:
diff changeset
70 ----------------------------
kono
parents:
diff changeset
71 -- Invalidate_Stack_Cache --
kono
parents:
diff changeset
72 ----------------------------
kono
parents:
diff changeset
73
kono
parents:
diff changeset
74 procedure Invalidate_Stack_Cache (Any_Stack : Stack_Access) is
kono
parents:
diff changeset
75 pragma Warnings (Off, Any_Stack);
kono
parents:
diff changeset
76 begin
kono
parents:
diff changeset
77 Cache := Null_Stack;
kono
parents:
diff changeset
78 end Invalidate_Stack_Cache;
kono
parents:
diff changeset
79
kono
parents:
diff changeset
80 -----------------------------
kono
parents:
diff changeset
81 -- Notify_Stack_Attributes --
kono
parents:
diff changeset
82 -----------------------------
kono
parents:
diff changeset
83
kono
parents:
diff changeset
84 procedure Notify_Stack_Attributes
kono
parents:
diff changeset
85 (Initial_SP : System.Address;
kono
parents:
diff changeset
86 Size : System.Storage_Elements.Storage_Offset)
kono
parents:
diff changeset
87 is
kono
parents:
diff changeset
88 My_Stack : constant Stack_Access := Soft_Links.Get_Stack_Info.all;
kono
parents:
diff changeset
89
kono
parents:
diff changeset
90 -- We piggyback on the 'Limit' field to store what will be used as the
kono
parents:
diff changeset
91 -- 'Base' and leave the 'Size' alone to not interfere with the logic in
kono
parents:
diff changeset
92 -- Set_Stack_Info below.
kono
parents:
diff changeset
93
kono
parents:
diff changeset
94 pragma Unreferenced (Size);
kono
parents:
diff changeset
95
kono
parents:
diff changeset
96 begin
kono
parents:
diff changeset
97 My_Stack.Limit := Initial_SP;
kono
parents:
diff changeset
98 end Notify_Stack_Attributes;
kono
parents:
diff changeset
99
kono
parents:
diff changeset
100 --------------------
kono
parents:
diff changeset
101 -- Set_Stack_Info --
kono
parents:
diff changeset
102 --------------------
kono
parents:
diff changeset
103
kono
parents:
diff changeset
104 function Set_Stack_Info
kono
parents:
diff changeset
105 (Stack : not null access Stack_Access) return Stack_Access
kono
parents:
diff changeset
106 is
kono
parents:
diff changeset
107 type Frame_Mark is null record;
kono
parents:
diff changeset
108 Frame_Location : Frame_Mark;
kono
parents:
diff changeset
109 Frame_Address : constant Address := Frame_Location'Address;
kono
parents:
diff changeset
110
kono
parents:
diff changeset
111 My_Stack : Stack_Access;
kono
parents:
diff changeset
112 Limit_Chars : System.Address;
kono
parents:
diff changeset
113 Limit : Integer;
kono
parents:
diff changeset
114
kono
parents:
diff changeset
115 begin
kono
parents:
diff changeset
116 -- The order of steps 1 .. 3 is important, see specification
kono
parents:
diff changeset
117
kono
parents:
diff changeset
118 -- 1) Get the Stack_Access value for the current task
kono
parents:
diff changeset
119
kono
parents:
diff changeset
120 My_Stack := Soft_Links.Get_Stack_Info.all;
kono
parents:
diff changeset
121
kono
parents:
diff changeset
122 if My_Stack.Base = Null_Address then
kono
parents:
diff changeset
123
kono
parents:
diff changeset
124 -- First invocation, initialize based on the assumption that there
kono
parents:
diff changeset
125 -- are Environment_Stack_Size bytes available beyond the current
kono
parents:
diff changeset
126 -- frame address.
kono
parents:
diff changeset
127
kono
parents:
diff changeset
128 if My_Stack.Size = 0 then
kono
parents:
diff changeset
129 My_Stack.Size := Storage_Offset (Default_Env_Stack_Size);
kono
parents:
diff changeset
130
kono
parents:
diff changeset
131 -- When the environment variable GNAT_STACK_LIMIT is set, set
kono
parents:
diff changeset
132 -- Environment_Stack_Size to that number of kB.
kono
parents:
diff changeset
133
kono
parents:
diff changeset
134 Limit_Chars := System.CRTL.getenv ("GNAT_STACK_LIMIT" & ASCII.NUL);
kono
parents:
diff changeset
135
kono
parents:
diff changeset
136 if Limit_Chars /= Null_Address then
kono
parents:
diff changeset
137 Limit := System.CRTL.atoi (Limit_Chars);
kono
parents:
diff changeset
138
kono
parents:
diff changeset
139 if Limit >= 0 then
kono
parents:
diff changeset
140 My_Stack.Size := Storage_Offset (Limit) * Kilobyte;
kono
parents:
diff changeset
141 end if;
kono
parents:
diff changeset
142 end if;
kono
parents:
diff changeset
143 end if;
kono
parents:
diff changeset
144
kono
parents:
diff changeset
145 -- If a stack base address has been registered, honor it. Fallback to
kono
parents:
diff changeset
146 -- the address of a local object otherwise.
kono
parents:
diff changeset
147
kono
parents:
diff changeset
148 My_Stack.Base :=
kono
parents:
diff changeset
149 (if My_Stack.Limit /= System.Null_Address
kono
parents:
diff changeset
150 then My_Stack.Limit else Frame_Address);
kono
parents:
diff changeset
151
kono
parents:
diff changeset
152 if Stack_Grows_Down then
kono
parents:
diff changeset
153
kono
parents:
diff changeset
154 -- Prevent wrap-around on too big stack sizes
kono
parents:
diff changeset
155
kono
parents:
diff changeset
156 My_Stack.Limit := My_Stack.Base - My_Stack.Size;
kono
parents:
diff changeset
157
kono
parents:
diff changeset
158 if My_Stack.Limit > My_Stack.Base then
kono
parents:
diff changeset
159 My_Stack.Limit := Address'First;
kono
parents:
diff changeset
160 end if;
kono
parents:
diff changeset
161
kono
parents:
diff changeset
162 else
kono
parents:
diff changeset
163 My_Stack.Limit := My_Stack.Base + My_Stack.Size;
kono
parents:
diff changeset
164
kono
parents:
diff changeset
165 -- Prevent wrap-around on too big stack sizes
kono
parents:
diff changeset
166
kono
parents:
diff changeset
167 if My_Stack.Limit < My_Stack.Base then
kono
parents:
diff changeset
168 My_Stack.Limit := Address'Last;
kono
parents:
diff changeset
169 end if;
kono
parents:
diff changeset
170 end if;
kono
parents:
diff changeset
171 end if;
kono
parents:
diff changeset
172
kono
parents:
diff changeset
173 -- 2) Set Stack.all to the value obtained in 1)
kono
parents:
diff changeset
174
kono
parents:
diff changeset
175 Stack.all := My_Stack;
kono
parents:
diff changeset
176
kono
parents:
diff changeset
177 -- 3) Optionally Poll to check for asynchronous abort
kono
parents:
diff changeset
178
kono
parents:
diff changeset
179 if Soft_Links.Check_Abort_Status.all /= 0 then
kono
parents:
diff changeset
180 raise Standard'Abort_Signal;
kono
parents:
diff changeset
181 end if;
kono
parents:
diff changeset
182
kono
parents:
diff changeset
183 -- Never trust the cached value, but return local copy
kono
parents:
diff changeset
184
kono
parents:
diff changeset
185 return My_Stack;
kono
parents:
diff changeset
186 end Set_Stack_Info;
kono
parents:
diff changeset
187
kono
parents:
diff changeset
188 -----------------
kono
parents:
diff changeset
189 -- Stack_Check --
kono
parents:
diff changeset
190 -----------------
kono
parents:
diff changeset
191
kono
parents:
diff changeset
192 function Stack_Check
kono
parents:
diff changeset
193 (Stack_Address : System.Address) return Stack_Access
kono
parents:
diff changeset
194 is
kono
parents:
diff changeset
195 type Frame_Marker is null record;
kono
parents:
diff changeset
196 Marker : Frame_Marker;
kono
parents:
diff changeset
197 Cached_Stack : constant Stack_Access := Cache;
kono
parents:
diff changeset
198 Frame_Address : constant System.Address := Marker'Address;
kono
parents:
diff changeset
199
kono
parents:
diff changeset
200 begin
kono
parents:
diff changeset
201 -- The parameter may have wrapped around in System.Address arithmetics.
kono
parents:
diff changeset
202 -- In that case, we have no other choices than raising the exception.
kono
parents:
diff changeset
203
kono
parents:
diff changeset
204 if (Stack_Grows_Down and then
kono
parents:
diff changeset
205 Stack_Address > Frame_Address)
kono
parents:
diff changeset
206 or else
kono
parents:
diff changeset
207 (not Stack_Grows_Down and then
kono
parents:
diff changeset
208 Stack_Address < Frame_Address)
kono
parents:
diff changeset
209 then
kono
parents:
diff changeset
210 raise Storage_Error with "stack overflow detected";
kono
parents:
diff changeset
211 end if;
kono
parents:
diff changeset
212
kono
parents:
diff changeset
213 -- This function first does a "cheap" check which is correct if it
kono
parents:
diff changeset
214 -- succeeds. In case of failure, the full check is done. Ideally the
kono
parents:
diff changeset
215 -- cheap check should be done in an optimized manner, or be inlined.
kono
parents:
diff changeset
216
kono
parents:
diff changeset
217 if (Stack_Grows_Down and then
kono
parents:
diff changeset
218 (Frame_Address <= Cached_Stack.Base
kono
parents:
diff changeset
219 and then
kono
parents:
diff changeset
220 Stack_Address > Cached_Stack.Limit))
kono
parents:
diff changeset
221 or else
kono
parents:
diff changeset
222 (not Stack_Grows_Down and then
kono
parents:
diff changeset
223 (Frame_Address >= Cached_Stack.Base
kono
parents:
diff changeset
224 and then
kono
parents:
diff changeset
225 Stack_Address < Cached_Stack.Limit))
kono
parents:
diff changeset
226 then
kono
parents:
diff changeset
227 -- Cached_Stack is valid as it passed the stack check
kono
parents:
diff changeset
228
kono
parents:
diff changeset
229 return Cached_Stack;
kono
parents:
diff changeset
230 end if;
kono
parents:
diff changeset
231
kono
parents:
diff changeset
232 Full_Check :
kono
parents:
diff changeset
233 declare
kono
parents:
diff changeset
234 My_Stack : constant Stack_Access := Set_Stack_Info (Cache'Access);
kono
parents:
diff changeset
235 -- At this point Stack.all might already be invalid, so
kono
parents:
diff changeset
236 -- it is essential to use our local copy of Stack.
kono
parents:
diff changeset
237
kono
parents:
diff changeset
238 begin
kono
parents:
diff changeset
239 if (Stack_Grows_Down and then
kono
parents:
diff changeset
240 (not (Frame_Address <= My_Stack.Base)))
kono
parents:
diff changeset
241 or else
kono
parents:
diff changeset
242 (not Stack_Grows_Down and then
kono
parents:
diff changeset
243 (not (Frame_Address >= My_Stack.Base)))
kono
parents:
diff changeset
244 then
kono
parents:
diff changeset
245 -- The returned Base is lower than the stored one, so assume that
kono
parents:
diff changeset
246 -- the original one wasn't right and use the current Frame_Address
kono
parents:
diff changeset
247 -- as new one. This allows Base to be initialized with the
kono
parents:
diff changeset
248 -- Frame_Address as approximation. During initialization the
kono
parents:
diff changeset
249 -- Frame_Address will be close to the stack base anyway: the
kono
parents:
diff changeset
250 -- difference should be compensated for in the stack reserve.
kono
parents:
diff changeset
251
kono
parents:
diff changeset
252 My_Stack.Base := Frame_Address;
kono
parents:
diff changeset
253 end if;
kono
parents:
diff changeset
254
kono
parents:
diff changeset
255 if (Stack_Grows_Down
kono
parents:
diff changeset
256 and then Stack_Address < My_Stack.Limit)
kono
parents:
diff changeset
257 or else
kono
parents:
diff changeset
258 (not Stack_Grows_Down
kono
parents:
diff changeset
259 and then Stack_Address > My_Stack.Limit)
kono
parents:
diff changeset
260 then
kono
parents:
diff changeset
261 raise Storage_Error with "stack overflow detected";
kono
parents:
diff changeset
262 end if;
kono
parents:
diff changeset
263
kono
parents:
diff changeset
264 return My_Stack;
kono
parents:
diff changeset
265 end Full_Check;
kono
parents:
diff changeset
266 end Stack_Check;
kono
parents:
diff changeset
267
kono
parents:
diff changeset
268 ------------------------
kono
parents:
diff changeset
269 -- Update_Stack_Cache --
kono
parents:
diff changeset
270 ------------------------
kono
parents:
diff changeset
271
kono
parents:
diff changeset
272 procedure Update_Stack_Cache (Stack : Stack_Access) is
kono
parents:
diff changeset
273 begin
kono
parents:
diff changeset
274 if not Multi_Processor then
kono
parents:
diff changeset
275 Cache := Stack;
kono
parents:
diff changeset
276 end if;
kono
parents:
diff changeset
277 end Update_Stack_Cache;
kono
parents:
diff changeset
278
kono
parents:
diff changeset
279 end System.Stack_Checking.Operations;