|
| 1 | +/*******************************************************************\ |
| 2 | +
|
| 3 | +Module: Fatal Assertions |
| 4 | +
|
| 5 | +Author: Daniel Kroening, [email protected] |
| 6 | +
|
| 7 | +\*******************************************************************/ |
| 8 | + |
| 9 | +/// \file |
| 10 | +/// Fatal Assertions |
| 11 | + |
| 12 | +#include "fatal_assertions.h" |
| 13 | + |
| 14 | +#include <util/irep_hash.h> |
| 15 | + |
| 16 | +#include <goto-programs/goto_functions.h> |
| 17 | + |
| 18 | +#include <stack> |
| 19 | +#include <unordered_set> |
| 20 | + |
| 21 | +struct function_loc_pairt |
| 22 | +{ |
| 23 | + using function_itt = goto_functionst::function_mapt::const_iterator; |
| 24 | + function_loc_pairt( |
| 25 | + function_itt __function_it, |
| 26 | + goto_programt::const_targett __target) |
| 27 | + : function_it(__function_it), target(__target) |
| 28 | + { |
| 29 | + } |
| 30 | + function_itt function_it; |
| 31 | + goto_programt::const_targett target; |
| 32 | + bool operator==(const function_loc_pairt &other) const |
| 33 | + { |
| 34 | + return function_it->first == other.function_it->first && |
| 35 | + target == other.target; |
| 36 | + } |
| 37 | +}; |
| 38 | + |
| 39 | +struct function_loc_pair_hasht |
| 40 | +{ |
| 41 | + std::size_t operator()(const function_loc_pairt &p) const |
| 42 | + { |
| 43 | + auto h1 = p.function_it->first.hash(); |
| 44 | + auto h2 = const_target_hash{}(p.target); |
| 45 | + return hash_combine(h1, h2); |
| 46 | + } |
| 47 | +}; |
| 48 | + |
| 49 | +using loc_sett = |
| 50 | + std::unordered_set<function_loc_pairt, function_loc_pair_hasht>; |
| 51 | + |
| 52 | +static void |
| 53 | +reachable_fixpoint(loc_sett &locs, const goto_functionst &goto_functions) |
| 54 | +{ |
| 55 | + std::stack<function_loc_pairt> working; |
| 56 | + |
| 57 | + for(auto loc : locs) |
| 58 | + working.push(loc); |
| 59 | + |
| 60 | + while(!working.empty()) |
| 61 | + { |
| 62 | + auto loc = working.top(); |
| 63 | + working.pop(); |
| 64 | + locs.insert(loc); |
| 65 | + |
| 66 | + if(loc.target->is_function_call()) |
| 67 | + { |
| 68 | + } |
| 69 | + else |
| 70 | + { |
| 71 | + auto &body = loc.function_it->second.body; |
| 72 | + |
| 73 | + for(auto successor : body.get_successors(loc.target)) |
| 74 | + working.emplace(loc.function_it, successor); |
| 75 | + } |
| 76 | + } |
| 77 | +} |
| 78 | + |
| 79 | +void propagate_fatal_assertions( |
| 80 | + propertiest &properties, |
| 81 | + const goto_functionst &goto_functions) |
| 82 | +{ |
| 83 | + // Iterate to find refuted fatal assertions. Anything reachalble |
| 84 | + // from there is a 'fatal loc'. |
| 85 | + loc_sett fatal_locs; |
| 86 | + |
| 87 | + for(auto function_it = goto_functions.function_map.begin(); |
| 88 | + function_it != goto_functions.function_map.end(); |
| 89 | + function_it++) |
| 90 | + { |
| 91 | + auto &body = function_it->second.body; |
| 92 | + for(auto target = body.instructions.begin(); |
| 93 | + target != body.instructions.end(); |
| 94 | + target++) |
| 95 | + { |
| 96 | + if(target->is_assert() && target->source_location().property_fatal()) |
| 97 | + { |
| 98 | + auto id = target->source_location().get_property_id(); |
| 99 | + auto property = properties.find(id); |
| 100 | + CHECK_RETURN(property != properties.end()); |
| 101 | + |
| 102 | + // Status? |
| 103 | + if(property->second.status == property_statust::FAIL) |
| 104 | + { |
| 105 | + fatal_locs.emplace(function_it, target); |
| 106 | + } |
| 107 | + } |
| 108 | + } |
| 109 | + } |
| 110 | + |
| 111 | + // Saturate fixpoint. |
| 112 | + reachable_fixpoint(fatal_locs, goto_functions); |
| 113 | + |
| 114 | + // Now mark PASS assertions as UNKNOWN. |
| 115 | + for(auto &loc : fatal_locs) |
| 116 | + { |
| 117 | + if(loc.target->is_assert()) |
| 118 | + { |
| 119 | + auto id = loc.target->source_location().get_property_id(); |
| 120 | + auto property = properties.find(id); |
| 121 | + CHECK_RETURN(property != properties.end()); |
| 122 | + |
| 123 | + // Status? |
| 124 | + if(property->second.status == property_statust::PASS) |
| 125 | + { |
| 126 | + property->second.status = property_statust::UNKNOWN; |
| 127 | + } |
| 128 | + } |
| 129 | + } |
| 130 | +} |
0 commit comments