Search in sources :

Example 41 with BoolExpr

use of com.microsoft.z3.BoolExpr in project batfish by batfish.

the class Encoder method environmentBlockingClause.

/*
   * Generate a blocking clause for the encoding that says that one
   * of the environments that was true before must now be false.
   */
private BoolExpr environmentBlockingClause(Model m) {
    BoolExpr acc1 = mkFalse();
    BoolExpr acc2 = mkTrue();
    // Disable an environment edge if possible
    Map<LogicalEdge, SymbolicRoute> map = getMainSlice().getLogicalGraph().getEnvironmentVars();
    for (Map.Entry<LogicalEdge, SymbolicRoute> entry : map.entrySet()) {
        SymbolicRoute record = entry.getValue();
        BoolExpr per = record.getPermitted();
        Expr x = m.evaluate(per, false);
        if (x.toString().equals("true")) {
            acc1 = mkOr(acc1, mkNot(per));
        } else {
            acc2 = mkAnd(acc2, mkNot(per));
        }
    }
    // Disable a community value if possible
    for (Map.Entry<LogicalEdge, SymbolicRoute> entry : map.entrySet()) {
        SymbolicRoute record = entry.getValue();
        for (Map.Entry<CommunityVar, BoolExpr> centry : record.getCommunities().entrySet()) {
            BoolExpr comm = centry.getValue();
            Expr x = m.evaluate(comm, false);
            if (x.toString().equals("true")) {
                acc1 = mkOr(acc1, mkNot(comm));
            } else {
                acc2 = mkAnd(acc2, mkNot(comm));
            }
        }
    }
    return mkAnd(acc1, acc2);
}
Also used : CommunityVar(org.batfish.symbolic.CommunityVar) BoolExpr(com.microsoft.z3.BoolExpr) BoolExpr(com.microsoft.z3.BoolExpr) ArithExpr(com.microsoft.z3.ArithExpr) BitVecExpr(com.microsoft.z3.BitVecExpr) Expr(com.microsoft.z3.Expr) HashMap(java.util.HashMap) Map(java.util.Map) TreeMap(java.util.TreeMap) SortedMap(java.util.SortedMap)

Example 42 with BoolExpr

use of com.microsoft.z3.BoolExpr in project batfish by batfish.

the class Encoder method verify.

/**
 * Checks that a property is always true by seeing if the encoding is unsatisfiable. mkIf the
 * model is satisfiable, then there is a counter example to the property.
 *
 * @return A VerificationResult indicating the status of the check.
 */
public Tuple<VerificationResult, Model> verify() {
    EncoderSlice mainSlice = _slices.get(MAIN_SLICE_NAME);
    int numVariables = _allVariables.size();
    int numConstraints = _solver.getAssertions().length;
    int numNodes = mainSlice.getGraph().getConfigurations().size();
    int numEdges = 0;
    for (Map.Entry<String, Set<String>> e : mainSlice.getGraph().getNeighbors().entrySet()) {
        numEdges += e.getValue().size();
    }
    long start = System.currentTimeMillis();
    Status status = _solver.check();
    long time = System.currentTimeMillis() - start;
    VerificationStats stats = null;
    if (_question.getBenchmark()) {
        stats = new VerificationStats();
        stats.setAvgNumNodes(numNodes);
        stats.setMaxNumNodes(numNodes);
        stats.setMinNumNodes(numNodes);
        stats.setAvgNumEdges(numEdges);
        stats.setMaxNumEdges(numEdges);
        stats.setMinNumEdges(numEdges);
        stats.setAvgNumVariables(numVariables);
        stats.setMaxNumVariables(numVariables);
        stats.setMinNumVariables(numVariables);
        stats.setAvgNumConstraints(numConstraints);
        stats.setMaxNumConstraints(numConstraints);
        stats.setMinNumConstraints(numConstraints);
        stats.setAvgSolverTime(time);
        stats.setMaxSolverTime(time);
        stats.setMinSolverTime(time);
    }
    if (status == Status.UNSATISFIABLE) {
        VerificationResult res = new VerificationResult(true, null, null, null, null, null, stats);
        return new Tuple<>(res, null);
    } else if (status == Status.UNKNOWN) {
        throw new BatfishException("ERROR: satisfiability unknown");
    } else {
        VerificationResult result;
        Model m;
        while (true) {
            m = _solver.getModel();
            SortedMap<String, String> model = new TreeMap<>();
            SortedMap<String, String> packetModel = new TreeMap<>();
            SortedSet<String> fwdModel = new TreeSet<>();
            SortedMap<String, SortedMap<String, String>> envModel = new TreeMap<>();
            SortedSet<String> failures = new TreeSet<>();
            buildCounterExample(this, m, model, packetModel, fwdModel, envModel, failures);
            if (_previousEncoder != null) {
                buildCounterExample(_previousEncoder, m, model, packetModel, fwdModel, envModel, failures);
            }
            result = new VerificationResult(false, model, packetModel, envModel, fwdModel, failures, stats);
            if (!_question.getMinimize()) {
                break;
            }
            BoolExpr blocking = environmentBlockingClause(m);
            add(blocking);
            Status s = _solver.check();
            if (s == Status.UNSATISFIABLE) {
                break;
            }
            if (s == Status.UNKNOWN) {
                throw new BatfishException("ERROR: satisfiability unknown");
            }
        }
        return new Tuple<>(result, m);
    }
}
Also used : Status(com.microsoft.z3.Status) BatfishException(org.batfish.common.BatfishException) BoolExpr(com.microsoft.z3.BoolExpr) SortedSet(java.util.SortedSet) TreeSet(java.util.TreeSet) HashSet(java.util.HashSet) Set(java.util.Set) SortedSet(java.util.SortedSet) SortedMap(java.util.SortedMap) Model(com.microsoft.z3.Model) HashMap(java.util.HashMap) Map(java.util.Map) TreeMap(java.util.TreeMap) SortedMap(java.util.SortedMap) Tuple(org.batfish.symbolic.utils.Tuple)

Example 43 with BoolExpr

use of com.microsoft.z3.BoolExpr in project batfish by batfish.

the class EncoderSlice method initForwardingAcross.

/*
   * Initialize boolean expressions to represent that traffic sent along
   * and edge will reach the other side of the edge.
   */
private void initForwardingAcross() {
    _symbolicDecisions.getDataForwarding().forEach((router, edge, var) -> {
        BoolExpr inAcl;
        if (edge.getEnd() == null) {
            inAcl = mkTrue();
        } else {
            GraphEdge ge = getGraph().getOtherEnd().get(edge);
            inAcl = _inboundAcls.get(ge);
            if (inAcl == null) {
                inAcl = mkTrue();
            }
        }
        _forwardsAcross.put(router, edge, mkAnd(var, inAcl));
    });
}
Also used : BoolExpr(com.microsoft.z3.BoolExpr) GraphEdge(org.batfish.symbolic.GraphEdge)

Example 44 with BoolExpr

use of com.microsoft.z3.BoolExpr in project batfish by batfish.

the class EncoderSlice method equalCommunities.

private BoolExpr equalCommunities(SymbolicRoute best, SymbolicRoute vars) {
    BoolExpr acc = mkTrue();
    for (Map.Entry<CommunityVar, BoolExpr> entry : best.getCommunities().entrySet()) {
        CommunityVar cvar = entry.getKey();
        BoolExpr var = entry.getValue();
        BoolExpr other = vars.getCommunities().get(cvar);
        if (other == null) {
            acc = mkAnd(acc, mkNot(var));
        } else {
            acc = mkAnd(acc, mkEq(var, other));
        }
    }
    return acc;
}
Also used : CommunityVar(org.batfish.symbolic.CommunityVar) BoolExpr(com.microsoft.z3.BoolExpr) HashMap(java.util.HashMap) Map(java.util.Map)

Example 45 with BoolExpr

use of com.microsoft.z3.BoolExpr in project batfish by batfish.

the class EncoderSlice method computeIpProtocols.

/*
   * Convert a set of ip protocols to a boolean expression on the symbolic packet
   */
private BoolExpr computeIpProtocols(Set<IpProtocol> ipProtos) {
    BoolExpr acc = mkFalse();
    for (IpProtocol proto : ipProtos) {
        ArithExpr protoNum = mkInt(proto.number());
        acc = mkOr(acc, mkEq(protoNum, _symbolicPacket.getIpProtocol()));
    }
    return (BoolExpr) acc.simplify();
}
Also used : ArithExpr(com.microsoft.z3.ArithExpr) BoolExpr(com.microsoft.z3.BoolExpr) IpProtocol(org.batfish.datamodel.IpProtocol)

Aggregations

BoolExpr (com.microsoft.z3.BoolExpr)141 Status (com.microsoft.z3.Status)55 Test (org.junit.Test)51 ArithExpr (com.microsoft.z3.ArithExpr)27 GraphEdge (org.batfish.symbolic.GraphEdge)25 Context (com.microsoft.z3.Context)24 HashMap (java.util.HashMap)22 Expr (com.microsoft.z3.Expr)21 Set (java.util.Set)20 Collectors (java.util.stream.Collectors)18 BitVecExpr (com.microsoft.z3.BitVecExpr)17 Event (dartagnan.program.Event)17 MemEvent (dartagnan.program.MemEvent)17 Program (dartagnan.program.Program)16 Map (java.util.Map)16 Local (dartagnan.program.Local)15 ArrayList (java.util.ArrayList)15 BatfishException (org.batfish.common.BatfishException)14 Graph (org.batfish.symbolic.Graph)14 com.microsoft.z3 (com.microsoft.z3)12