annotate gcc/ada/sem_spark.ads @ 138:fc828634a951

merge
author Shinji KONO <kono@ie.u-ryukyu.ac.jp>
date Thu, 08 Nov 2018 14:17:14 +0900
parents 84e7813d76e9
children
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 COMPILER COMPONENTS --
kono
parents:
diff changeset
4 -- --
kono
parents:
diff changeset
5 -- S E M _ S P A R K --
kono
parents:
diff changeset
6 -- --
kono
parents:
diff changeset
7 -- S p e c --
kono
parents:
diff changeset
8 -- --
131
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
9 -- Copyright (C) 2017-2018, Free Software Foundation, Inc. --
111
kono
parents:
diff changeset
10 -- --
kono
parents:
diff changeset
11 -- GNAT 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. See the GNU General Public License --
kono
parents:
diff changeset
17 -- for more details. You should have received a copy of the GNU General --
kono
parents:
diff changeset
18 -- Public License distributed with GNAT; see file COPYING3. If not, go to --
kono
parents:
diff changeset
19 -- http://www.gnu.org/licenses for a complete copy of the license. --
kono
parents:
diff changeset
20 -- --
kono
parents:
diff changeset
21 -- GNAT was originally developed by the GNAT team at New York University. --
kono
parents:
diff changeset
22 -- Extensive contributions were provided by Ada Core Technologies Inc. --
kono
parents:
diff changeset
23 -- --
kono
parents:
diff changeset
24 ------------------------------------------------------------------------------
kono
parents:
diff changeset
25
kono
parents:
diff changeset
26 -- This package implements an anti-aliasing analysis for access types. The
kono
parents:
diff changeset
27 -- rules that are enforced are defined in the anti-aliasing section of the
kono
parents:
diff changeset
28 -- SPARK RM 6.4.2
kono
parents:
diff changeset
29 --
131
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
30 -- Check_Safe_Pointers is called by Gnat1drv, when GNATprove mode is
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
31 -- activated. It does an analysis of the source code, looking for code that is
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
32 -- considered as SPARK and launches another function called Analyze_Node that
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
33 -- will do the whole analysis.
111
kono
parents:
diff changeset
34 --
kono
parents:
diff changeset
35 -- A path is an abstraction of a name, of which all indices, slices (for
kono
parents:
diff changeset
36 -- indexed components) and function calls have been abstracted and all
kono
parents:
diff changeset
37 -- dereferences are made explicit. A path is the atomic element viewed by the
kono
parents:
diff changeset
38 -- analysis, with the notion of prefixes and extensions of different paths.
kono
parents:
diff changeset
39 --
kono
parents:
diff changeset
40 -- The analysis explores the AST, and looks for different constructs
kono
parents:
diff changeset
41 -- that may involve aliasing. These main constructs are assignments
kono
parents:
diff changeset
42 -- (N_Assignment_Statement, N_Object_Declaration, ...), or calls
kono
parents:
diff changeset
43 -- (N_Procedure_Call_Statement, N_Entry_Call_Statement, N_Function_Call).
kono
parents:
diff changeset
44 -- The analysis checks the permissions of each construct and updates them
kono
parents:
diff changeset
45 -- according to the SPARK RM. This can follow three main different types
kono
parents:
diff changeset
46 -- of operations: move, borrow, and observe.
kono
parents:
diff changeset
47
kono
parents:
diff changeset
48 ----------------------------
kono
parents:
diff changeset
49 -- Deep and shallow types --
kono
parents:
diff changeset
50 ----------------------------
kono
parents:
diff changeset
51
kono
parents:
diff changeset
52 -- The analysis focuses on objects that can cause problems in terms of pointer
kono
parents:
diff changeset
53 -- aliasing. These objects have types that are called deep. Deep types are
kono
parents:
diff changeset
54 -- defined as being either types with an access part or class-wide types
kono
parents:
diff changeset
55 -- (which may have an access part in a derived type). Non-deep types are
kono
parents:
diff changeset
56 -- called shallow. Some objects of shallow type may cause pointer aliasing
kono
parents:
diff changeset
57 -- problems when they are explicitely marked as aliased (and then the aliasing
kono
parents:
diff changeset
58 -- occurs when we take the Access to this object and store it in a pointer).
kono
parents:
diff changeset
59
kono
parents:
diff changeset
60 ----------
kono
parents:
diff changeset
61 -- Move --
kono
parents:
diff changeset
62 ----------
kono
parents:
diff changeset
63
kono
parents:
diff changeset
64 -- Moves can happen at several points in the program: during assignment (and
kono
parents:
diff changeset
65 -- any similar statement such as object declaration with initial value), or
kono
parents:
diff changeset
66 -- during return statements.
kono
parents:
diff changeset
67 --
kono
parents:
diff changeset
68 -- The underlying concept consists of transferring the ownership of any path
kono
parents:
diff changeset
69 -- on the right-hand side to the left-hand side. There are some details that
kono
parents:
diff changeset
70 -- should be taken into account so as not to transfer paths that appear only
kono
parents:
diff changeset
71 -- as intermediate results of a more complex expression.
kono
parents:
diff changeset
72
kono
parents:
diff changeset
73 -- More specifically, the SPARK RM defines moved expressions, and any moved
kono
parents:
diff changeset
74 -- expression that points directly to a path is then checked and sees its
kono
parents:
diff changeset
75 -- permissions updated accordingly.
kono
parents:
diff changeset
76
kono
parents:
diff changeset
77 ------------
kono
parents:
diff changeset
78 -- Borrow --
kono
parents:
diff changeset
79 ------------
kono
parents:
diff changeset
80
kono
parents:
diff changeset
81 -- Borrows can happen in subprogram calls. They consist of a temporary
kono
parents:
diff changeset
82 -- transfer of ownership from a caller to a callee. Expressions that can be
kono
parents:
diff changeset
83 -- borrowed can be found in either procedure or entry actual parameters, and
kono
parents:
diff changeset
84 -- consist of parameters of mode either "out" or "in out", or parameters of
kono
parents:
diff changeset
85 -- mode "in" that are of type nonconstant access-to-variable. We consider
kono
parents:
diff changeset
86 -- global variables as implicit parameters to subprograms, with their mode
kono
parents:
diff changeset
87 -- given by the Global contract associated to the subprogram. Note that the
kono
parents:
diff changeset
88 -- analysis looks for such a Global contract mentioning any global variable
kono
parents:
diff changeset
89 -- of deep type accessed directly in the subprogram, and it raises an error if
kono
parents:
diff changeset
90 -- there is no Global contract, or if the Global contract does not mention the
kono
parents:
diff changeset
91 -- variable.
kono
parents:
diff changeset
92 --
kono
parents:
diff changeset
93 -- A borrow of a parameter X is equivalent in terms of aliasing to moving
kono
parents:
diff changeset
94 -- X'Access to the callee, and then assigning back X at the end of the call.
kono
parents:
diff changeset
95 --
kono
parents:
diff changeset
96 -- Borrowed parameters should have read-write permission (or write-only for
kono
parents:
diff changeset
97 -- "out" parameters), and should all have read-write permission at the end
kono
parents:
diff changeset
98 -- of the call (this guarantee is ensured by the callee).
kono
parents:
diff changeset
99
kono
parents:
diff changeset
100 -------------
kono
parents:
diff changeset
101 -- Observe --
kono
parents:
diff changeset
102 -------------
kono
parents:
diff changeset
103
kono
parents:
diff changeset
104 -- Observed parameters are all the other parameters that are not borrowed and
kono
parents:
diff changeset
105 -- that may cause problems with aliasing. They are considered as being sent to
kono
parents:
diff changeset
106 -- the callee with Read-Only permission, so that they can be aliased safely.
kono
parents:
diff changeset
107 -- This is the only construct that allows aliasing that does not prevent
kono
parents:
diff changeset
108 -- accessing the old path that is being aliased. However, this comes with
kono
parents:
diff changeset
109 -- the restriction that those aliased path cannot be written in the callee.
kono
parents:
diff changeset
110
kono
parents:
diff changeset
111 --------------------
kono
parents:
diff changeset
112 -- Implementation --
kono
parents:
diff changeset
113 --------------------
kono
parents:
diff changeset
114
kono
parents:
diff changeset
115 -- The implementation is based on trees that represent the possible paths
kono
parents:
diff changeset
116 -- in the source code. Those trees can be unbounded in depth, hence they are
kono
parents:
diff changeset
117 -- represented using lazy data structures, whose laziness is handled manually.
kono
parents:
diff changeset
118 -- Each time an identifier is declared, its path is added to the permission
kono
parents:
diff changeset
119 -- environment as a tree with only one node, the declared identifier. Each
kono
parents:
diff changeset
120 -- time a path is checked or updated, we look in the tree at the adequate
kono
parents:
diff changeset
121 -- node, unfolding the tree whenever needed.
kono
parents:
diff changeset
122
kono
parents:
diff changeset
123 -- For this, each node has several variables that indicate whether it is
kono
parents:
diff changeset
124 -- deep (Is_Node_Deep), what permission it has (Permission), and what is
kono
parents:
diff changeset
125 -- the lowest permission of all its descendants (Children_Permission). After
kono
parents:
diff changeset
126 -- unfolding the tree, we update the permissions of each node, deleting the
kono
parents:
diff changeset
127 -- Children_Permission, and specifying new ones for the leaves of the unfolded
kono
parents:
diff changeset
128 -- tree.
kono
parents:
diff changeset
129
kono
parents:
diff changeset
130 -- After assigning a path, the descendants of the assigned path are dumped
kono
parents:
diff changeset
131 -- (and hence the tree is folded back), given that all descendants directly
kono
parents:
diff changeset
132 -- get read-write permission, which can be specified using the node's
kono
parents:
diff changeset
133 -- Children_Permission field.
kono
parents:
diff changeset
134
kono
parents:
diff changeset
135 with Types; use Types;
kono
parents:
diff changeset
136
kono
parents:
diff changeset
137 package Sem_SPARK is
kono
parents:
diff changeset
138
kono
parents:
diff changeset
139 procedure Check_Safe_Pointers (N : Node_Id);
kono
parents:
diff changeset
140 -- The entry point of this package. It analyzes a node and reports errors
kono
parents:
diff changeset
141 -- when there are violations of aliasing rules.
kono
parents:
diff changeset
142
kono
parents:
diff changeset
143 end Sem_SPARK;