1 ------------------------------------------------------------------------------
3 -- GNU ADA RUN-TIME LIBRARY (GNARL) COMPONENTS --
5 -- S Y S T E M . S T A C K _ C H E C K I N G --
10 -- Copyright (C) 1999-2001 Free Software Foundation, Inc. --
12 -- GNARL is free software; you can redistribute it and/or modify it under --
13 -- terms of the GNU General Public License as published by the Free Soft- --
14 -- ware Foundation; either version 2, or (at your option) any later ver- --
15 -- sion. GNARL is distributed in the hope that it will be useful, but WITH- --
16 -- OUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY --
17 -- or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License --
18 -- for more details. You should have received a copy of the GNU General --
19 -- Public License distributed with GNARL; see file COPYING. If not, write --
20 -- to the Free Software Foundation, 59 Temple Place - Suite 330, Boston, --
21 -- MA 02111-1307, USA. --
23 -- As a special exception, if other files instantiate generics from this --
24 -- unit, or you link this unit with other files to produce an executable, --
25 -- this unit does not by itself cause the resulting executable to be --
26 -- covered by the GNU General Public License. This exception does not --
27 -- however invalidate any other reasons why the executable file might be --
28 -- covered by the GNU Public License. --
30 -- GNARL was developed by the GNARL team at Florida State University. --
31 -- Extensive contributions were provided by Ada Core Technologies Inc. --
33 ------------------------------------------------------------------------------
35 -- This package provides a system-independent implementation of stack
36 -- checking using comparison with stack base and limit.
38 with System
.Storage_Elements
;
41 -- Turn off polling, we do not want polling to take place during stack
42 -- checking operations. It causes infinite loops and other problems.
44 package System
.Stack_Checking
is
45 type Stack_Info
is record
46 Limit
: System
.Address
:= System
.Null_Address
;
47 Base
: System
.Address
:= System
.Null_Address
;
48 Size
: System
.Storage_Elements
.Storage_Offset
:= 0;
50 -- This record may be part of a larger data structure like the
51 -- task control block in the tasking case.
52 -- This specific layout has the advantage of being compatible with the
53 -- Intel x86 BOUNDS instruction.
55 type Stack_Access
is access all Stack_Info
;
56 -- Unique local storage associated with a specific task. This storage is
57 -- used for the stack base and limit, and is returned by Checked_Self.
58 -- Only self may write this information, it may be read by any task.
59 -- At no time the address range Limit .. Base (or Base .. Limit for
60 -- upgrowing stack) may contain any address that is part of another stack.
61 -- The Stack_Access may be part of a larger data structure.
63 Multi_Processor
: constant Boolean := False; -- Not supported yet
65 ----------------------
66 -- Client Interface --
67 ----------------------
69 procedure Set_Stack_Size
70 (Stack_Size
: System
.Storage_Elements
.Storage_Offset
);
71 -- Specify the stack size for the current task.
73 procedure Update_Stack_Cache
(Stack
: Stack_Access
);
74 -- Set the stack cache for the current task. Note that this is only
75 -- for optimization purposes, nothing can be assumed about the
76 -- contents of the cache at any time, see Set_Stack_Info.
78 procedure Invalidate_Stack_Cache
(Any_Stack
: Stack_Access
);
79 -- Invalidate cache entries for the task T that owns Any_Stack.
80 -- This causes the Set_Stack_Info function to be called during
81 -- the next stack check done by T. This can be used to interrupt
82 -- task T asynchronously.
83 -- Stack_Check should be called in loops for this to work reliably.
85 function Stack_Check
(Stack_Address
: System
.Address
) return Stack_Access
;
86 -- This version of Stack_Check should not be inlined.
90 Null_Stack_Info
: aliased Stack_Info
:=
91 (Limit
=> System
.Null_Address
,
92 Base
=> System
.Null_Address
,
94 -- Use explicit assignment to avoid elaboration code (call to _init_proc).
96 Null_Stack
: constant Stack_Access
:= Null_Stack_Info
'Access;
97 -- Stack_Access value that will return a Stack_Base and Stack_Limit
98 -- that fail any stack check.
100 Cache
: aliased Stack_Access
:= Null_Stack
;
102 pragma Export
(C
, Cache
, "_gnat_stack_cache");
103 pragma Export
(C
, Stack_Check
, "_gnat_stack_check");
105 end System
.Stack_Checking
;