@@ -11,6 +11,7 @@ Date: June 2017
11
11
#include " java_bytecode_instrument.h"
12
12
13
13
#include < util/arith_tools.h>
14
+ #include < util/assert_or_die.h>
14
15
#include < util/fresh_symbol.h>
15
16
#include < util/std_code.h>
16
17
#include < util/std_expr.h>
@@ -82,6 +83,21 @@ const std::vector<std::string> exception_needed_classes = {
82
83
" java.lang.NegativeArraySizeException" ,
83
84
" java.lang.NullPointerException" };
84
85
86
+ // / Get a copy of a source location with additional assertion information
87
+ // / \param loc: original source location
88
+ // / \param comment: human-readable assertion description
89
+ // / \param property_class: assertion property class
90
+ static source_locationt with_assertion_description (
91
+ const source_locationt &loc,
92
+ const irep_idt &comment,
93
+ const irep_idt &property_class)
94
+ {
95
+ source_locationt ret = loc;
96
+ ret.set_comment (comment);
97
+ ret.set_property_class (property_class);
98
+ return ret;
99
+ }
100
+
85
101
// / Creates a class stub for exc_name and generates a
86
102
// / conditional GOTO such that exc_name is thrown when
87
103
// / cond is met.
@@ -160,11 +176,14 @@ codet java_bytecode_instrumentt::check_arithmetic_exception(
160
176
original_loc,
161
177
" java.lang.ArithmeticException" );
162
178
163
- code_assertt ret (binary_relation_exprt (denominator, ID_notequal, zero));
164
- ret.add_source_location ()=original_loc;
165
- ret.add_source_location ().set_comment (" Denominator should be nonzero" );
166
- ret.add_source_location ().set_property_class (" integer-divide-by-zero" );
167
- return ret;
179
+ source_locationt assertion_loc =
180
+ with_assertion_description (
181
+ original_loc,
182
+ " Denominator should be nonzero" ,
183
+ " integer-divide-by-zero" );
184
+
185
+ return create_assert_or_die (
186
+ binary_relation_exprt (denominator, ID_notequal, zero), assertion_loc);
168
187
}
169
188
170
189
// / Checks whether the array access array_struct[idx] is out-of-bounds,
@@ -195,19 +214,21 @@ codet java_bytecode_instrumentt::check_array_access(
195
214
" java.lang.ArrayIndexOutOfBoundsException" );
196
215
197
216
code_blockt bounds_checks;
198
- bounds_checks.add (code_assertt (ge_zero));
199
- bounds_checks.operands ().back ().add_source_location ()=original_loc;
200
- bounds_checks.operands ().back ().add_source_location ()
201
- .set_comment (" Array index should be >= 0" );
202
- bounds_checks.operands ().back ().add_source_location ()
203
- .set_property_class (" array-index-out-of-bounds-low" );
204
-
205
- bounds_checks.add (code_assertt (lt_length));
206
- bounds_checks.operands ().back ().add_source_location ()=original_loc;
207
- bounds_checks.operands ().back ().add_source_location ()
208
- .set_comment (" Array index should be < length" );
209
- bounds_checks.operands ().back ().add_source_location ()
210
- .set_property_class (" array-index-out-of-bounds-high" );
217
+
218
+ bounds_checks.add (
219
+ create_assert_or_die (
220
+ ge_zero,
221
+ with_assertion_description (
222
+ original_loc,
223
+ " Array index should be >= 0" ,
224
+ " array-index-out-of-bounds-low" )));
225
+ bounds_checks.add (
226
+ create_assert_or_die (
227
+ lt_length,
228
+ with_assertion_description (
229
+ original_loc,
230
+ " Array index should be < length" ,
231
+ " array-index-out-of-bounds-high" )));
211
232
212
233
return bounds_checks;
213
234
}
@@ -246,11 +267,14 @@ codet java_bytecode_instrumentt::check_class_cast(
246
267
}
247
268
else
248
269
{
249
- code_assertt assert_class (class_cast_check);
250
- assert_class.add_source_location ().
251
- set_comment (" Dynamic cast check" );
252
- assert_class.add_source_location ().
253
- set_property_class (" bad-dynamic-cast" );
270
+ codet assert_class =
271
+ create_assert_or_die (
272
+ class_cast_check,
273
+ with_assertion_description (
274
+ original_loc,
275
+ " Dynamic cast check" ,
276
+ " bad-dynamic-cast" ));
277
+
254
278
check_code=std::move (assert_class);
255
279
}
256
280
@@ -283,12 +307,12 @@ codet java_bytecode_instrumentt::check_null_dereference(
283
307
equal_expr,
284
308
original_loc, " java.lang.NullPointerException" );
285
309
286
- code_assertt check (( not_exprt (equal_expr)));
287
- check. add_source_location ()
288
- . set_comment ( " Throw null " );
289
- check. add_source_location ()
290
- . set_property_class ( " null- pointer-exception " );
291
- return check ;
310
+ return create_assert_or_die (
311
+ not_exprt (equal_expr),
312
+ with_assertion_description (
313
+ original_loc,
314
+ " Null pointer check " ,
315
+ " null-pointer-exception " )) ;
292
316
}
293
317
294
318
// / Checks whether `length`>=0 and throws NegativeArraySizeException/
@@ -313,11 +337,12 @@ codet java_bytecode_instrumentt::check_array_length(
313
337
original_loc,
314
338
" java.lang.NegativeArraySizeException" );
315
339
316
- code_assertt check (ge_zero);
317
- check.add_source_location ().set_comment (" Array size should be >= 0" );
318
- check.add_source_location ()
319
- .set_property_class (" array-create-negative-size" );
320
- return check;
340
+ return create_assert_or_die (
341
+ ge_zero,
342
+ with_assertion_description (
343
+ original_loc,
344
+ " Array size should be >= 0" ,
345
+ " array-create-negative-size" ));
321
346
}
322
347
323
348
// / Checks whether `expr` requires instrumentation, and if so adds it
0 commit comments