1
1
import cpp
2
2
3
+ /**
4
+ * Holds if `val` is an access to the variable `v`, or if `val`
5
+ * is an assignment with an access to `v` on the left-hand side.
6
+ */
3
7
predicate valueOfVar ( Variable v , Expr val ) {
4
8
val = v .getAnAccess ( ) or
5
9
val .( AssignExpr ) .getLValue ( ) = v .getAnAccess ( )
6
10
}
7
11
12
+ /**
13
+ * Holds if either:
14
+ * - `cond` is an (in)equality expression that compares the variable `v` to the value `-1`, or
15
+ * - `cond` is a relational expression that compares the variable `v` to a constant.
16
+ */
8
17
predicate boundsCheckExpr ( Variable v , Expr cond ) {
9
18
exists ( EQExpr eq |
10
19
cond = eq and
@@ -43,6 +52,18 @@ predicate boundsCheckExpr(Variable v, Expr cond) {
43
52
)
44
53
}
45
54
55
+ /**
56
+ * Holds if `node` is an expression in a conditional statement and `succ` is an
57
+ * immediate successor of `node` that may be reached after evaluating `node`.
58
+ * For example, given
59
+ * ```
60
+ * if (a < 10 && b) func1();
61
+ * else func2();
62
+ * ```
63
+ * this predicate holds when either:
64
+ * - `node` is `a < 10` and `succ` is `func2()` or `b`, or
65
+ * - `node` is `b` and `succ` is `func1()` or `func2()`
66
+ */
46
67
predicate conditionalSuccessor ( ControlFlowNode node , ControlFlowNode succ ) {
47
68
if node .isCondition ( )
48
69
then succ = node .getATrueSuccessor ( ) or succ = node .getAFalseSuccessor ( )
@@ -52,6 +73,12 @@ predicate conditionalSuccessor(ControlFlowNode node, ControlFlowNode succ) {
52
73
)
53
74
}
54
75
76
+ /**
77
+ * Holds if the current value of the variable `v` at control-flow
78
+ * node `n` has been used either in:
79
+ * - an (in)equality comparison with the value `-1`, or
80
+ * - a relational comparison that compares `v` to a constant.
81
+ */
55
82
predicate boundsChecked ( Variable v , ControlFlowNode node ) {
56
83
exists ( Expr test |
57
84
boundsCheckExpr ( v , test ) and
@@ -63,6 +90,14 @@ predicate boundsChecked(Variable v, ControlFlowNode node) {
63
90
)
64
91
}
65
92
93
+ /**
94
+ * Holds if `cond` compares `v` to some common error values. Specifically, this
95
+ * predicate holds when:
96
+ * - `cond` checks that `v` is equal to `-1`, or
97
+ * - `cond` checks that `v` is less than `0`, or
98
+ * - `cond` checks that `v` is less than or equal to `-1`, or
99
+ * - `cond` checks that `v` is not some common success value (see `successCondition`).
100
+ */
66
101
predicate errorCondition ( Variable v , Expr cond ) {
67
102
exists ( EQExpr eq |
68
103
cond = eq and
@@ -88,6 +123,14 @@ predicate errorCondition(Variable v, Expr cond) {
88
123
)
89
124
}
90
125
126
+ /**
127
+ * Holds if `cond` compares `v` to some common success values. Specifically, this
128
+ * predicate holds when:
129
+ * - `cond` checks that `v` is not equal to `-1`, or
130
+ * - `cond` checks that `v` is greater than or equal than `0`, or
131
+ * - `cond` checks that `v` is greater than `-1`, or
132
+ * - `cond` checks that `v` is not some common error value (see `errorCondition`).
133
+ */
91
134
predicate successCondition ( Variable v , Expr cond ) {
92
135
exists ( NEExpr ne |
93
136
cond = ne and
@@ -113,6 +156,11 @@ predicate successCondition(Variable v, Expr cond) {
113
156
)
114
157
}
115
158
159
+ /**
160
+ * Holds if there exists a comparison operation that checks whether `v`
161
+ * represents some common *error* values, and `n` may be reached
162
+ * immediately following the comparison operation.
163
+ */
116
164
predicate errorSuccessor ( Variable v , ControlFlowNode n ) {
117
165
exists ( Expr cond |
118
166
errorCondition ( v , cond ) and n = cond .getATrueSuccessor ( )
@@ -121,6 +169,11 @@ predicate errorSuccessor(Variable v, ControlFlowNode n) {
121
169
)
122
170
}
123
171
172
+ /**
173
+ * Holds if there exists a comparison operation that checks whether `v`
174
+ * represents some common *success* values, and `n` may be reached
175
+ * immediately following the comparison operation.
176
+ */
124
177
predicate successSuccessor ( Variable v , ControlFlowNode n ) {
125
178
exists ( Expr cond |
126
179
successCondition ( v , cond ) and n = cond .getATrueSuccessor ( )
@@ -129,6 +182,10 @@ predicate successSuccessor(Variable v, ControlFlowNode n) {
129
182
)
130
183
}
131
184
185
+ /**
186
+ * Holds if the current value of the variable `v` at control-flow node
187
+ * `n` may have been checked against a common set of *error* values.
188
+ */
132
189
predicate checkedError ( Variable v , ControlFlowNode n ) {
133
190
errorSuccessor ( v , n )
134
191
or
@@ -139,6 +196,10 @@ predicate checkedError(Variable v, ControlFlowNode n) {
139
196
)
140
197
}
141
198
199
+ /**
200
+ * Holds if the current value of the variable `v` at control-flow node
201
+ * `n` may have been checked against a common set of *success* values.
202
+ */
142
203
predicate checkedSuccess ( Variable v , ControlFlowNode n ) {
143
204
successSuccessor ( v , n )
144
205
or
0 commit comments