|
| 1 | +/*******************************************************************\ |
| 2 | +
|
| 3 | +Module: SVA Monitor |
| 4 | +
|
| 5 | +Author: Daniel Kroening, [email protected] |
| 6 | +
|
| 7 | +\*******************************************************************/ |
| 8 | + |
| 9 | +#include "sva_monitor.h" |
| 10 | + |
| 11 | +#include <temporal-logic/ltl.h> |
| 12 | +#include <temporal-logic/temporal_logic.h> |
| 13 | +#include <verilog/sva_expr.h> |
| 14 | + |
| 15 | +#include "ebmc_properties.h" |
| 16 | +#include "transition_system.h" |
| 17 | + |
| 18 | +struct monitor_resultt |
| 19 | +{ |
| 20 | + explicit monitor_resultt(exprt _safety) : safety(std::move(_safety)) |
| 21 | + { |
| 22 | + } |
| 23 | + |
| 24 | + exprt safety = true_exprt{}; |
| 25 | + exprt liveness = true_exprt{}; |
| 26 | + exprt as_LTL() const; |
| 27 | +}; |
| 28 | + |
| 29 | +exprt monitor_resultt::as_LTL() const |
| 30 | +{ |
| 31 | + exprt::operandst conjuncts; |
| 32 | + |
| 33 | + if(!safety.is_true()) |
| 34 | + conjuncts.push_back(G_exprt{safety}); |
| 35 | + |
| 36 | + if(!liveness.is_true()) |
| 37 | + conjuncts.push_back(G_exprt{F_exprt{liveness}}); |
| 38 | + |
| 39 | + return conjunction(conjuncts); |
| 40 | +} |
| 41 | + |
| 42 | +/// Recursion for creating a safety automaton for a safety property. |
| 43 | +/// * the property is assumed to be in NNF. |
| 44 | +/// * the monitor starts with the given "start" signal |
| 45 | +/// * the return value is the "safety" signal, or {} if not supported |
| 46 | +std::optional<exprt> create_sva_safety_monitor_rec( |
| 47 | + transition_systemt &transition_system, |
| 48 | + const exprt &start, |
| 49 | + const exprt &property_expr) |
| 50 | +{ |
| 51 | + if(!has_temporal_operator(property_expr)) |
| 52 | + { |
| 53 | + // A state formula only. Needs to hold in "start" states only. |
| 54 | + return and_exprt{start, property_expr}; |
| 55 | + } |
| 56 | + else if( |
| 57 | + property_expr.id() == ID_not || property_expr.id() == ID_implies || |
| 58 | + property_expr.id() == ID_nor || property_expr.id() == ID_nand || |
| 59 | + property_expr.id() == ID_xor || property_expr.id() == ID_xnor) |
| 60 | + { |
| 61 | + // We rely on NNF. |
| 62 | + return {}; |
| 63 | + } |
| 64 | + else if(property_expr.id() == ID_and) |
| 65 | + { |
| 66 | + // The conjunction of safety automata is built by conjoining the |
| 67 | + // safety signal. |
| 68 | + |
| 69 | + exprt::operandst conjuncts; |
| 70 | + |
| 71 | + for(auto &op : property_expr.operands()) |
| 72 | + { |
| 73 | + auto rec = create_sva_safety_monitor_rec(transition_system, start, op); |
| 74 | + if(!rec.has_value()) |
| 75 | + return {}; |
| 76 | + conjuncts.push_back(rec.value()); |
| 77 | + } |
| 78 | + |
| 79 | + return conjunction(conjuncts); |
| 80 | + } |
| 81 | + else if(property_expr.id() == ID_or) |
| 82 | + { |
| 83 | + // Build automaton per disjunct. |
| 84 | + // Keep a state bit per automaton to record an unsafe state. |
| 85 | + // Signal an unsafe state when all subautomata signalled an unsafe state. |
| 86 | + return {}; |
| 87 | + } |
| 88 | + else if(property_expr.id() == ID_sva_always) |
| 89 | + { |
| 90 | + // Nondeterministically guess when to start monitoring. |
| 91 | + //auto &op = to_sva_always_expr(expr).op(); |
| 92 | + //return create_sva_safety_monitor_rec(transition_system, op); |
| 93 | + return {}; |
| 94 | + } |
| 95 | + else if(property_expr.id() == ID_sva_s_nexttime) |
| 96 | + { |
| 97 | + // Simply wait one time frame. |
| 98 | + return {}; |
| 99 | + } |
| 100 | + else |
| 101 | + return {}; // not supported |
| 102 | +} |
| 103 | + |
| 104 | +exprt sva_monitor_initial(transition_systemt &transition_system) |
| 105 | +{ |
| 106 | + return nil_exprt{}; |
| 107 | +} |
| 108 | + |
| 109 | +/// top-level formula |
| 110 | +std::optional<monitor_resultt> |
| 111 | +create_sva_monitor(transition_systemt &transition_system, exprt property_expr) |
| 112 | +{ |
| 113 | + if(property_expr.id() == ID_sva_always) |
| 114 | + { |
| 115 | + auto &op = to_sva_always_expr(property_expr); |
| 116 | + |
| 117 | + // Special-case "always p". |
| 118 | + if(!has_temporal_operator(op)) |
| 119 | + return monitor_resultt{op}; |
| 120 | + |
| 121 | + // Create the top-level "start" signal -- on in the initial states exactly. |
| 122 | + auto start = sva_monitor_initial(transition_system); |
| 123 | + |
| 124 | + // always X |
| 125 | + auto result_rec = |
| 126 | + create_sva_safety_monitor_rec(transition_system, start, op); |
| 127 | + if(result_rec.has_value()) |
| 128 | + return monitor_resultt{result_rec.value()}; |
| 129 | + } |
| 130 | + |
| 131 | + return {}; // not supported |
| 132 | +} |
| 133 | + |
| 134 | +void create_sva_monitor( |
| 135 | + transition_systemt &transition_system, |
| 136 | + ebmc_propertiest::propertyt &property) |
| 137 | +{ |
| 138 | + auto result = create_sva_monitor(transition_system, property.normalized_expr); |
| 139 | + |
| 140 | + if(result.has_value()) |
| 141 | + property.normalized_expr = result.value().as_LTL(); |
| 142 | +} |
| 143 | + |
| 144 | +void sva_monitor( |
| 145 | + transition_systemt &transition_system, |
| 146 | + ebmc_propertiest &properties) |
| 147 | +{ |
| 148 | + for(auto &property : properties.properties) |
| 149 | + { |
| 150 | + if(has_SVA_operator(property.normalized_expr)) |
| 151 | + create_sva_monitor(transition_system, property); |
| 152 | + } |
| 153 | +} |
0 commit comments