|
| 1 | +/*******************************************************************\ |
| 2 | +
|
| 3 | +Module: Utilities for building havoc code for expressions. |
| 4 | +
|
| 5 | +Author: Saswat Padhi, [email protected] |
| 6 | +
|
| 7 | +Date: July 2021 |
| 8 | +
|
| 9 | +\*******************************************************************/ |
| 10 | + |
| 11 | +/// \file |
| 12 | +/// Utilities for building havoc code for expressions |
| 13 | + |
| 14 | +#include "havoc_utils.h" |
| 15 | + |
| 16 | +#include <util/arith_tools.h> |
| 17 | +#include <util/c_types.h> |
| 18 | +#include <util/pointer_expr.h> |
| 19 | + |
| 20 | +void append_safe_havoc_code_for_expr( |
| 21 | + const source_locationt source_location, |
| 22 | + const exprt &expr, |
| 23 | + goto_programt &dest, |
| 24 | + const std::function<void()> &havoc_code_impl) |
| 25 | +{ |
| 26 | + goto_programt skip_program; |
| 27 | + const auto skip_target = |
| 28 | + skip_program.add(goto_programt::make_skip(source_location)); |
| 29 | + |
| 30 | + // for deref expressions, check for validity of underlying pointer, |
| 31 | + // and skip havocing if invalid (to avoid spurious pointer deref errors) |
| 32 | + if(expr.id() == ID_dereference) |
| 33 | + { |
| 34 | + const auto condition = not_exprt(w_ok_exprt( |
| 35 | + to_dereference_expr(expr).pointer(), |
| 36 | + from_integer(0, unsigned_int_type()))); |
| 37 | + dest.add(goto_programt::make_goto(skip_target, condition, source_location)); |
| 38 | + } |
| 39 | + |
| 40 | + havoc_code_impl(); |
| 41 | + |
| 42 | + // for deref expressions, add the final skip target |
| 43 | + if(expr.id() == ID_dereference) |
| 44 | + dest.destructive_append(skip_program); |
| 45 | +} |
| 46 | + |
| 47 | +void append_object_havoc_code_for_expr( |
| 48 | + const source_locationt source_location, |
| 49 | + const exprt &expr, |
| 50 | + goto_programt &dest) |
| 51 | +{ |
| 52 | + append_safe_havoc_code_for_expr(source_location, expr, dest, [&]() { |
| 53 | + codet havoc(ID_havoc_object); |
| 54 | + havoc.add_source_location() = source_location; |
| 55 | + havoc.add_to_operands(expr); |
| 56 | + dest.add(goto_programt::make_other(havoc, source_location)); |
| 57 | + }); |
| 58 | +} |
| 59 | + |
| 60 | +void append_scalar_havoc_code_for_expr( |
| 61 | + const source_locationt source_location, |
| 62 | + const exprt &expr, |
| 63 | + goto_programt &dest) |
| 64 | +{ |
| 65 | + append_safe_havoc_code_for_expr(source_location, expr, dest, [&]() { |
| 66 | + side_effect_expr_nondett rhs(expr.type(), source_location); |
| 67 | + goto_programt::targett t = dest.add( |
| 68 | + goto_programt::make_assignment(expr, std::move(rhs), source_location)); |
| 69 | + t->code_nonconst().add_source_location() = source_location; |
| 70 | + }); |
| 71 | +} |
| 72 | + |
| 73 | +void append_havoc_code( |
| 74 | + const source_locationt source_location, |
| 75 | + const modifiest &modifies, |
| 76 | + goto_programt &dest) |
| 77 | +{ |
| 78 | + havoc_utils_is_constantt is_constant(modifies); |
| 79 | + for(const auto &expr : modifies) |
| 80 | + { |
| 81 | + if(expr.id() == ID_index || expr.id() == ID_dereference) |
| 82 | + { |
| 83 | + address_of_exprt address_of_expr(expr); |
| 84 | + if(!is_constant(address_of_expr)) |
| 85 | + { |
| 86 | + append_object_havoc_code_for_expr( |
| 87 | + source_location, address_of_expr, dest); |
| 88 | + continue; |
| 89 | + } |
| 90 | + } |
| 91 | + |
| 92 | + append_scalar_havoc_code_for_expr(source_location, expr, dest); |
| 93 | + } |
| 94 | +} |
0 commit comments