1 |
12 |
jlechner |
------------------------------------------------------------------------------
|
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-2005 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, 51 Franklin Street, Fifth Floor, --
|
20 |
|
|
-- Boston, MA 02110-1301, 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 |
|
|
-- This is the VxWorks version of this package.
|
35 |
|
|
-- This file should be kept synchronized with the general implementation
|
36 |
|
|
-- provided by s-stchop.adb.
|
37 |
|
|
|
38 |
|
|
pragma Restrictions (No_Elaboration_Code);
|
39 |
|
|
-- We want to guarantee the absence of elaboration code because the
|
40 |
|
|
-- binder does not handle references to this package.
|
41 |
|
|
|
42 |
|
|
with Ada.Exceptions;
|
43 |
|
|
|
44 |
|
|
with System.Storage_Elements; use System.Storage_Elements;
|
45 |
|
|
with System.Parameters; use System.Parameters;
|
46 |
|
|
with System.Soft_Links;
|
47 |
|
|
with Interfaces.C;
|
48 |
|
|
with System.OS_Interface;
|
49 |
|
|
|
50 |
|
|
package body System.Stack_Checking.Operations is
|
51 |
|
|
|
52 |
|
|
-- In order to have stack checking working appropriately on
|
53 |
|
|
-- VxWorks we need to extract the stack size information from the
|
54 |
|
|
-- VxWorks kernel itself. It means that the library for showing
|
55 |
|
|
-- task-related information needs to be linked into the VxWorks
|
56 |
|
|
-- system, when using stack checking. The TaskShow library can be
|
57 |
|
|
-- linked into the VxWorks system by either:
|
58 |
|
|
-- * defining INCLUDE_SHOW_ROUTINES in config.h when using
|
59 |
|
|
-- configuration header files, or
|
60 |
|
|
-- * selecting INCLUDE_TASK_SHOW when using the Tornado project
|
61 |
|
|
-- facility.
|
62 |
|
|
|
63 |
|
|
function Set_Stack_Info (Stack : access Stack_Access) return Stack_Access;
|
64 |
|
|
|
65 |
|
|
-- The function Set_Stack_Info is the actual function that updates
|
66 |
|
|
-- the cache containing a pointer to the Stack_Info. It may also
|
67 |
|
|
-- be used for detecting asynchronous abort in combination with
|
68 |
|
|
-- Invalidate_Self_Cache.
|
69 |
|
|
|
70 |
|
|
-- Set_Stack_Info should do the following things in order:
|
71 |
|
|
-- 1) Get the Stack_Access value for the current task
|
72 |
|
|
-- 2) Set Stack.all to the value obtained in 1)
|
73 |
|
|
-- 3) Optionally Poll to check for asynchronous abort
|
74 |
|
|
|
75 |
|
|
-- This order is important because if at any time a write to
|
76 |
|
|
-- the stack cache is pending, that write should be followed
|
77 |
|
|
-- by a Poll to prevent loosing signals.
|
78 |
|
|
|
79 |
|
|
-- Note: This function must be compiled with Polling turned off
|
80 |
|
|
|
81 |
|
|
-- Note: on systems like VxWorks and OS/2 with real thread-local storage,
|
82 |
|
|
-- Set_Stack_Info should return an access value for such local
|
83 |
|
|
-- storage. In those cases the cache will always be up-to-date.
|
84 |
|
|
|
85 |
|
|
-- The following constants should be imported from some system-specific
|
86 |
|
|
-- constants package. The constants must be static for performance reasons.
|
87 |
|
|
|
88 |
|
|
----------------------------
|
89 |
|
|
-- Invalidate_Stack_Cache --
|
90 |
|
|
----------------------------
|
91 |
|
|
|
92 |
|
|
procedure Invalidate_Stack_Cache (Any_Stack : Stack_Access) is
|
93 |
|
|
pragma Warnings (Off, Any_Stack);
|
94 |
|
|
begin
|
95 |
|
|
Cache := Null_Stack;
|
96 |
|
|
end Invalidate_Stack_Cache;
|
97 |
|
|
|
98 |
|
|
--------------------
|
99 |
|
|
-- Set_Stack_Info --
|
100 |
|
|
--------------------
|
101 |
|
|
|
102 |
|
|
function Set_Stack_Info
|
103 |
|
|
(Stack : access Stack_Access) return Stack_Access
|
104 |
|
|
is
|
105 |
|
|
|
106 |
|
|
-- Task descriptor that is handled internally by the VxWorks kernel
|
107 |
|
|
type Task_Descriptor is record
|
108 |
|
|
T_Id : Interfaces.C.int; -- task identifier
|
109 |
|
|
Td_Name : System.Address; -- task name
|
110 |
|
|
Td_Priority : Interfaces.C.int; -- task priority
|
111 |
|
|
Td_Status : Interfaces.C.int; -- task status
|
112 |
|
|
Td_Options : Interfaces.C.int; -- task option bits (see below)
|
113 |
|
|
Td_Entry : System.Address; -- original entry point of task
|
114 |
|
|
Td_Sp : System.Address; -- saved stack pointer
|
115 |
|
|
Td_PStackBase : System.Address; -- the bottom of the stack
|
116 |
|
|
Td_PStackLimit : System.Address; -- the effective end of the stack
|
117 |
|
|
Td_PStackEnd : System.Address; -- the actual end of the stack
|
118 |
|
|
Td_StackSize : Interfaces.C.int; -- size of stack in bytes
|
119 |
|
|
Td_StackCurrent : Interfaces.C.int; -- current stack usage in bytes
|
120 |
|
|
Td_StackHigh : Interfaces.C.int; -- maximum stack usage in bytes
|
121 |
|
|
Td_StackMargin : Interfaces.C.int; -- current stack margin in bytes
|
122 |
|
|
Td_ErrorStatus : Interfaces.C.int; -- most recent task error status
|
123 |
|
|
Td_Delay : Interfaces.C.int; -- delay/timeout ticks
|
124 |
|
|
end record;
|
125 |
|
|
pragma Convention (C, Task_Descriptor);
|
126 |
|
|
|
127 |
|
|
-- This VxWorks procedure fills in a specified task descriptor
|
128 |
|
|
-- for a specified task.
|
129 |
|
|
procedure TaskInfoGet (T_Id : System.OS_Interface.t_id;
|
130 |
|
|
Task_Desc : access Task_Descriptor);
|
131 |
|
|
pragma Import (C, TaskInfoGet, "taskInfoGet");
|
132 |
|
|
|
133 |
|
|
My_Stack : Stack_Access;
|
134 |
|
|
Task_Desc : aliased Task_Descriptor;
|
135 |
|
|
|
136 |
|
|
begin
|
137 |
|
|
-- The order of steps 1 .. 3 is important, see specification.
|
138 |
|
|
|
139 |
|
|
-- 1) Get the Stack_Access value for the current task
|
140 |
|
|
|
141 |
|
|
My_Stack := Soft_Links.Get_Stack_Info.all;
|
142 |
|
|
|
143 |
|
|
if My_Stack.Base = Null_Address then
|
144 |
|
|
|
145 |
|
|
-- First invocation. Ask the VxWorks kernel about stack
|
146 |
|
|
-- values.
|
147 |
|
|
TaskInfoGet (System.OS_Interface.taskIdSelf, Task_Desc'Access);
|
148 |
|
|
|
149 |
|
|
My_Stack.Size := System.Storage_Elements.Storage_Offset
|
150 |
|
|
(Task_Desc.Td_StackSize);
|
151 |
|
|
My_Stack.Base := Task_Desc.Td_PStackBase;
|
152 |
|
|
My_Stack.Limit := Task_Desc.Td_PStackLimit;
|
153 |
|
|
|
154 |
|
|
end if;
|
155 |
|
|
|
156 |
|
|
-- 2) Set Stack.all to the value obtained in 1)
|
157 |
|
|
|
158 |
|
|
Stack.all := My_Stack;
|
159 |
|
|
|
160 |
|
|
-- 3) Optionally Poll to check for asynchronous abort
|
161 |
|
|
|
162 |
|
|
if Soft_Links.Check_Abort_Status.all /= 0 then
|
163 |
|
|
raise Standard'Abort_Signal;
|
164 |
|
|
end if;
|
165 |
|
|
|
166 |
|
|
return My_Stack; -- Never trust the cached value, but return local copy!
|
167 |
|
|
end Set_Stack_Info;
|
168 |
|
|
|
169 |
|
|
-----------------
|
170 |
|
|
-- Stack_Check --
|
171 |
|
|
-----------------
|
172 |
|
|
|
173 |
|
|
function Stack_Check
|
174 |
|
|
(Stack_Address : System.Address) return Stack_Access
|
175 |
|
|
is
|
176 |
|
|
type Frame_Marker is null record;
|
177 |
|
|
Marker : Frame_Marker;
|
178 |
|
|
Cached_Stack : constant Stack_Access := Cache;
|
179 |
|
|
Frame_Address : constant System.Address := Marker'Address;
|
180 |
|
|
|
181 |
|
|
begin
|
182 |
|
|
-- This function first does a "cheap" check which is correct
|
183 |
|
|
-- if it succeeds. In case of failure, the full check is done.
|
184 |
|
|
-- Ideally the cheap check should be done in an optimized manner,
|
185 |
|
|
-- or be inlined.
|
186 |
|
|
|
187 |
|
|
if (Stack_Grows_Down and then
|
188 |
|
|
(Frame_Address <= Cached_Stack.Base
|
189 |
|
|
and
|
190 |
|
|
Stack_Address > Cached_Stack.Limit))
|
191 |
|
|
or else
|
192 |
|
|
(not Stack_Grows_Down and then
|
193 |
|
|
(Frame_Address >= Cached_Stack.Base
|
194 |
|
|
and
|
195 |
|
|
Stack_Address < Cached_Stack.Limit))
|
196 |
|
|
then
|
197 |
|
|
-- Cached_Stack is valid as it passed the stack check
|
198 |
|
|
return Cached_Stack;
|
199 |
|
|
end if;
|
200 |
|
|
|
201 |
|
|
Full_Check :
|
202 |
|
|
declare
|
203 |
|
|
My_Stack : constant Stack_Access := Set_Stack_Info (Cache'Access);
|
204 |
|
|
-- At this point Stack.all might already be invalid, so
|
205 |
|
|
-- it is essential to use our local copy of Stack!
|
206 |
|
|
|
207 |
|
|
begin
|
208 |
|
|
if (Stack_Grows_Down and then
|
209 |
|
|
Stack_Address < My_Stack.Limit)
|
210 |
|
|
or else
|
211 |
|
|
(not Stack_Grows_Down and then
|
212 |
|
|
Stack_Address > My_Stack.Limit)
|
213 |
|
|
then
|
214 |
|
|
Ada.Exceptions.Raise_Exception
|
215 |
|
|
(E => Storage_Error'Identity,
|
216 |
|
|
Message => "stack overflow detected");
|
217 |
|
|
end if;
|
218 |
|
|
|
219 |
|
|
return My_Stack;
|
220 |
|
|
end Full_Check;
|
221 |
|
|
end Stack_Check;
|
222 |
|
|
|
223 |
|
|
------------------------
|
224 |
|
|
-- Update_Stack_Cache --
|
225 |
|
|
------------------------
|
226 |
|
|
|
227 |
|
|
procedure Update_Stack_Cache (Stack : Stack_Access) is
|
228 |
|
|
begin
|
229 |
|
|
if not Multi_Processor then
|
230 |
|
|
Cache := Stack;
|
231 |
|
|
end if;
|
232 |
|
|
end Update_Stack_Cache;
|
233 |
|
|
|
234 |
|
|
end System.Stack_Checking.Operations;
|