Debian Codegraph @

gcc/ada/libgnat/s-stchop.adb

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