Search in sources :

Example 91 with BoolExpr

use of com.microsoft.z3.BoolExpr in project Dat3M by hernanponcedeleon.

the class Encodings method getInitialHigh.

public static BoolExpr getInitialHigh(Program p, Model model, Context ctx, boolean var1, boolean val1) {
    Set<Event> highInits = p.getEvents().stream().filter(e -> e instanceof Init).filter(e -> e.getLoc() instanceof HighLocation).collect(Collectors.toSet());
    BoolExpr reachedState = ctx.mkTrue();
    for (Event e : highInits) {
        IntExpr var = var1 ? initValue(e, ctx) : initValue2(e, ctx);
        IntExpr val = val1 ? initValue(e, ctx) : initValue2(e, ctx);
        if (e.getLoc().getIValue() == null) {
            reachedState = ctx.mkAnd(reachedState, ctx.mkEq(var, model.getConstInterp(val)));
        }
    }
    return reachedState;
}
Also used : HighLocation(dartagnan.program.HighLocation) Utils.lastValueReg(dartagnan.utils.Utils.lastValueReg) Utils.uniqueValue(dartagnan.utils.Utils.uniqueValue) com.microsoft.z3(com.microsoft.z3) Event(dartagnan.program.Event) Set(java.util.Set) If(dartagnan.program.If) Collectors(java.util.stream.Collectors) Utils.initValue(dartagnan.utils.Utils.initValue) Init(dartagnan.program.Init) Register(dartagnan.program.Register) Utils.initValue2(dartagnan.utils.Utils.initValue2) Local(dartagnan.program.Local) Load(dartagnan.program.Load) MemEvent(dartagnan.program.MemEvent) Utils.ssaReg(dartagnan.utils.Utils.ssaReg) Program(dartagnan.program.Program) Utils.edge(dartagnan.utils.Utils.edge) Utils.lastValueLoc(dartagnan.utils.Utils.lastValueLoc) Store(dartagnan.program.Store) MapSSA(dartagnan.utils.MapSSA) Location(dartagnan.program.Location) HighLocation(dartagnan.program.HighLocation) Init(dartagnan.program.Init) Event(dartagnan.program.Event) MemEvent(dartagnan.program.MemEvent)

Example 92 with BoolExpr

use of com.microsoft.z3.BoolExpr in project Dat3M by hernanponcedeleon.

the class Encodings method encodeReachedState.

public static BoolExpr encodeReachedState(Program p, Model model, Context ctx) {
    Set<Location> locs = p.getEvents().stream().filter(e -> e instanceof MemEvent).map(e -> e.getLoc()).collect(Collectors.toSet());
    BoolExpr reachedState = ctx.mkTrue();
    for (Location loc : locs) {
        reachedState = ctx.mkAnd(reachedState, ctx.mkEq(lastValueLoc(loc, ctx), model.getConstInterp(lastValueLoc(loc, ctx))));
    }
    Set<Event> executedEvents = p.getEvents().stream().filter(e -> model.getConstInterp(e.executes(ctx)).isTrue()).collect(Collectors.toSet());
    Set<Register> regs = executedEvents.stream().filter(e -> e instanceof Local | e instanceof Load).map(e -> e.getReg()).collect(Collectors.toSet());
    for (Register reg : regs) {
        reachedState = ctx.mkAnd(reachedState, ctx.mkEq(lastValueReg(reg, ctx), model.getConstInterp(lastValueReg(reg, ctx))));
    }
    return reachedState;
}
Also used : HighLocation(dartagnan.program.HighLocation) Utils.lastValueReg(dartagnan.utils.Utils.lastValueReg) Utils.uniqueValue(dartagnan.utils.Utils.uniqueValue) com.microsoft.z3(com.microsoft.z3) Event(dartagnan.program.Event) Set(java.util.Set) If(dartagnan.program.If) Collectors(java.util.stream.Collectors) Utils.initValue(dartagnan.utils.Utils.initValue) Init(dartagnan.program.Init) Register(dartagnan.program.Register) Utils.initValue2(dartagnan.utils.Utils.initValue2) Local(dartagnan.program.Local) Load(dartagnan.program.Load) MemEvent(dartagnan.program.MemEvent) Utils.ssaReg(dartagnan.utils.Utils.ssaReg) Program(dartagnan.program.Program) Utils.edge(dartagnan.utils.Utils.edge) Utils.lastValueLoc(dartagnan.utils.Utils.lastValueLoc) Store(dartagnan.program.Store) MapSSA(dartagnan.utils.MapSSA) Location(dartagnan.program.Location) Load(dartagnan.program.Load) Register(dartagnan.program.Register) MemEvent(dartagnan.program.MemEvent) Event(dartagnan.program.Event) MemEvent(dartagnan.program.MemEvent) Local(dartagnan.program.Local) HighLocation(dartagnan.program.HighLocation) Location(dartagnan.program.Location)

Example 93 with BoolExpr

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

the class Encoder method initSlices.

/*
   * Initialize each encoding slice.
   * For iBGP, we also add reachability information for each pair of neighbors,
   * to determine if messages sent to/from a neighbor will arrive.
   */
private void initSlices(HeaderSpace h, Graph g) {
    if (g.getIbgpNeighbors().isEmpty() || !_modelIgp) {
        _slices.put(MAIN_SLICE_NAME, new EncoderSlice(this, h, g, ""));
    } else {
        _slices.put(MAIN_SLICE_NAME, new EncoderSlice(this, h, g, MAIN_SLICE_NAME));
    }
    if (_modelIgp) {
        SortedSet<Pair<String, Ip>> ibgpRouters = new TreeSet<>();
        for (Entry<GraphEdge, BgpNeighbor> entry : g.getIbgpNeighbors().entrySet()) {
            GraphEdge ge = entry.getKey();
            BgpNeighbor n = entry.getValue();
            String router = ge.getRouter();
            Ip ip = n.getLocalIp();
            Pair<String, Ip> pair = new Pair<>(router, ip);
            // Add one slice per (router, source ip) pair
            if (!ibgpRouters.contains(pair)) {
                ibgpRouters.add(pair);
                // Create a control plane slice only for this ip
                HeaderSpace hs = new HeaderSpace();
                // Make sure messages are sent to this destination IP
                SortedSet<IpWildcard> ips = new TreeSet<>();
                ips.add(new IpWildcard(n.getLocalIp()));
                hs.setDstIps(ips);
                // Make sure messages use TCP port 179
                SortedSet<SubRange> dstPorts = new TreeSet<>();
                dstPorts.add(new SubRange(179, 179));
                hs.setDstPorts(dstPorts);
                // Make sure messages use the TCP protocol
                SortedSet<IpProtocol> protocols = new TreeSet<>();
                protocols.add(IpProtocol.TCP);
                hs.setIpProtocols(protocols);
                // TODO: create domains once
                Graph gNew = new Graph(g.getBatfish(), null, g.getDomain(router));
                String sliceName = "SLICE-" + router + "_";
                EncoderSlice slice = new EncoderSlice(this, hs, gNew, sliceName);
                _slices.put(sliceName, slice);
                PropertyAdder pa = new PropertyAdder(slice);
                Map<String, BoolExpr> reachVars = pa.instrumentReachability(router);
                _sliceReachability.put(router, reachVars);
            }
        }
    }
}
Also used : BoolExpr(com.microsoft.z3.BoolExpr) Ip(org.batfish.datamodel.Ip) HeaderSpace(org.batfish.datamodel.HeaderSpace) BgpNeighbor(org.batfish.datamodel.BgpNeighbor) IpWildcard(org.batfish.datamodel.IpWildcard) Graph(org.batfish.symbolic.Graph) TreeSet(java.util.TreeSet) IpProtocol(org.batfish.datamodel.IpProtocol) SubRange(org.batfish.datamodel.SubRange) GraphEdge(org.batfish.symbolic.GraphEdge) Pair(org.batfish.common.Pair)

Example 94 with BoolExpr

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

the class Encoder method buildCounterExample.

/*
   * Add the relevant variables in the counterexample to
   * display to the user in a human-readable fashion
   */
private void buildCounterExample(Encoder enc, Model m, SortedMap<String, String> model, SortedMap<String, String> packetModel, SortedSet<String> fwdModel, SortedMap<String, SortedMap<String, String>> envModel, SortedSet<String> failures) {
    SortedMap<Expr, String> valuation = new TreeMap<>();
    // If user asks for the full model
    for (Entry<String, Expr> entry : _allVariables.entrySet()) {
        String name = entry.getKey();
        Expr e = entry.getValue();
        Expr val = m.evaluate(e, true);
        if (!val.equals(e)) {
            String s = val.toString();
            if (_question.getFullModel()) {
                model.put(name, s);
            }
            valuation.put(e, s);
        }
    }
    // Packet model
    SymbolicPacket p = enc.getMainSlice().getSymbolicPacket();
    String dstIp = valuation.get(p.getDstIp());
    String srcIp = valuation.get(p.getSrcIp());
    String dstPt = valuation.get(p.getDstPort());
    String srcPt = valuation.get(p.getSrcPort());
    String icmpCode = valuation.get(p.getIcmpCode());
    String icmpType = valuation.get(p.getIcmpType());
    String ipProtocol = valuation.get(p.getIpProtocol());
    String tcpAck = valuation.get(p.getTcpAck());
    String tcpCwr = valuation.get(p.getTcpCwr());
    String tcpEce = valuation.get(p.getTcpEce());
    String tcpFin = valuation.get(p.getTcpFin());
    String tcpPsh = valuation.get(p.getTcpPsh());
    String tcpRst = valuation.get(p.getTcpRst());
    String tcpSyn = valuation.get(p.getTcpSyn());
    String tcpUrg = valuation.get(p.getTcpUrg());
    Ip dip = new Ip(Long.parseLong(dstIp));
    Ip sip = new Ip(Long.parseLong(srcIp));
    packetModel.put("dstIp", dip.toString());
    if (sip.asLong() != 0) {
        packetModel.put("srcIp", sip.toString());
    }
    if (dstPt != null && !dstPt.equals("0")) {
        packetModel.put("dstPort", dstPt);
    }
    if (srcPt != null && !srcPt.equals("0")) {
        packetModel.put("srcPort", srcPt);
    }
    if (icmpCode != null && !icmpCode.equals("0")) {
        packetModel.put("icmpCode", icmpCode);
    }
    if (icmpType != null && !icmpType.equals("0")) {
        packetModel.put("icmpType", icmpType);
    }
    if (ipProtocol != null && !ipProtocol.equals("0")) {
        Integer number = Integer.parseInt(ipProtocol);
        IpProtocol proto = IpProtocol.fromNumber(number);
        packetModel.put("protocol", proto.toString());
    }
    if ("true".equals(tcpAck)) {
        packetModel.put("tcpAck", "set");
    }
    if ("true".equals(tcpCwr)) {
        packetModel.put("tcpCwr", "set");
    }
    if ("true".equals(tcpEce)) {
        packetModel.put("tcpEce", "set");
    }
    if ("true".equals(tcpFin)) {
        packetModel.put("tcpFin", "set");
    }
    if ("true".equals(tcpPsh)) {
        packetModel.put("tcpPsh", "set");
    }
    if ("true".equals(tcpRst)) {
        packetModel.put("tcpRst", "set");
    }
    if ("true".equals(tcpSyn)) {
        packetModel.put("tcpSyn", "set");
    }
    if ("true".equals(tcpUrg)) {
        packetModel.put("tcpUrg", "set");
    }
    for (EncoderSlice slice : enc.getSlices().values()) {
        for (Entry<LogicalEdge, SymbolicRoute> entry2 : slice.getLogicalGraph().getEnvironmentVars().entrySet()) {
            LogicalEdge lge = entry2.getKey();
            SymbolicRoute r = entry2.getValue();
            if ("true".equals(valuation.get(r.getPermitted()))) {
                SortedMap<String, String> recordMap = new TreeMap<>();
                GraphEdge ge = lge.getEdge();
                String nodeIface = ge.getRouter() + "," + ge.getStart().getName() + " (BGP)";
                envModel.put(nodeIface, recordMap);
                if (r.getPrefixLength() != null) {
                    String x = valuation.get(r.getPrefixLength());
                    if (x != null) {
                        int len = Integer.parseInt(x);
                        Prefix p1 = new Prefix(dip, len);
                        recordMap.put("prefix", p1.toString());
                    }
                }
                if (r.getAdminDist() != null) {
                    String x = valuation.get(r.getAdminDist());
                    if (x != null) {
                        recordMap.put("admin distance", x);
                    }
                }
                if (r.getLocalPref() != null) {
                    String x = valuation.get(r.getLocalPref());
                    if (x != null) {
                        recordMap.put("local preference", x);
                    }
                }
                if (r.getMetric() != null) {
                    String x = valuation.get(r.getMetric());
                    if (x != null) {
                        recordMap.put("protocol metric", x);
                    }
                }
                if (r.getMed() != null) {
                    String x = valuation.get(r.getMed());
                    if (x != null) {
                        recordMap.put("multi-exit disc.", valuation.get(r.getMed()));
                    }
                }
                if (r.getOspfArea() != null && r.getOspfArea().getBitVec() != null) {
                    String x = valuation.get(r.getOspfArea().getBitVec());
                    if (x != null) {
                        Integer i = Integer.parseInt(x);
                        Long area = r.getOspfArea().value(i);
                        recordMap.put("OSPF Area", area.toString());
                    }
                }
                if (r.getOspfType() != null && r.getOspfType().getBitVec() != null) {
                    String x = valuation.get(r.getOspfType().getBitVec());
                    if (x != null) {
                        Integer i = Integer.parseInt(x);
                        OspfType type = r.getOspfType().value(i);
                        recordMap.put("OSPF Type", type.toString());
                    }
                }
                for (Entry<CommunityVar, BoolExpr> entry3 : r.getCommunities().entrySet()) {
                    CommunityVar cvar = entry3.getKey();
                    BoolExpr e = entry3.getValue();
                    String c = valuation.get(e);
                    // TODO: what about OTHER type?
                    if ("true".equals(c) && displayCommunity(cvar)) {
                        String s = cvar.getValue();
                        String t = slice.getNamedCommunities().get(cvar.getValue());
                        s = (t == null ? s : t);
                        recordMap.put("community " + s, "");
                    }
                }
            }
        }
    }
    // Forwarding Model
    enc.getMainSlice().getSymbolicDecisions().getDataForwarding().forEach((router, edge, e) -> {
        String s = valuation.get(e);
        if ("true".equals(s)) {
            SymbolicRoute r = enc.getMainSlice().getSymbolicDecisions().getBestNeighbor().get(router);
            if (r.getProtocolHistory() != null) {
                Protocol proto;
                List<Protocol> allProtocols = enc.getMainSlice().getProtocols().get(router);
                if (allProtocols.size() == 1) {
                    proto = allProtocols.get(0);
                } else {
                    s = valuation.get(r.getProtocolHistory().getBitVec());
                    int i = Integer.parseInt(s);
                    proto = r.getProtocolHistory().value(i);
                }
                fwdModel.add(edge + " (" + proto.name() + ")");
            } else {
                fwdModel.add(edge.toString());
            }
        }
    });
    _symbolicFailures.getFailedInternalLinks().forEach((x, y, e) -> {
        String s = valuation.get(e);
        if ("1".equals(s)) {
            String pair = (x.compareTo(y) < 0 ? x + "," + y : y + "," + x);
            failures.add("link(" + pair + ")");
        }
    });
    _symbolicFailures.getFailedEdgeLinks().forEach((ge, e) -> {
        String s = valuation.get(e);
        if ("1".equals(s)) {
            failures.add("link(" + ge.getRouter() + "," + ge.getStart().getName() + ")");
        }
    });
}
Also used : BoolExpr(com.microsoft.z3.BoolExpr) Ip(org.batfish.datamodel.Ip) Prefix(org.batfish.datamodel.Prefix) TreeMap(java.util.TreeMap) CommunityVar(org.batfish.symbolic.CommunityVar) BoolExpr(com.microsoft.z3.BoolExpr) ArithExpr(com.microsoft.z3.ArithExpr) BitVecExpr(com.microsoft.z3.BitVecExpr) Expr(com.microsoft.z3.Expr) IpProtocol(org.batfish.datamodel.IpProtocol) OspfType(org.batfish.symbolic.OspfType) IpProtocol(org.batfish.datamodel.IpProtocol) Protocol(org.batfish.symbolic.Protocol) GraphEdge(org.batfish.symbolic.GraphEdge)

Example 95 with BoolExpr

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

the class EncoderSlice method addChoicePerProtocolConstraints.

/*
   * Constraints that define a choice for a given protocol
   * to be when a particular import is equal to the best choice.
   * For example:
   *
   * choice_bgp_Serial0 = (import_Serial0 = best_bgp)
   */
private void addChoicePerProtocolConstraints() {
    for (Entry<String, Configuration> entry : getGraph().getConfigurations().entrySet()) {
        String router = entry.getKey();
        Configuration conf = entry.getValue();
        for (Protocol proto : getProtocols().get(router)) {
            SymbolicRoute bestVars = _symbolicDecisions.getBestVars(_optimizations, router, proto);
            assert (bestVars != null);
            for (LogicalEdge e : collectAllImportLogicalEdges(router, conf, proto)) {
                SymbolicRoute vars = correctVars(e);
                BoolExpr choice = _symbolicDecisions.getChoiceVariables().get(router, proto, e);
                assert (choice != null);
                BoolExpr isBest = equal(conf, proto, bestVars, vars, e, false);
                add(mkEq(choice, mkAnd(vars.getPermitted(), isBest)));
            }
        }
    }
}
Also used : BoolExpr(com.microsoft.z3.BoolExpr) Configuration(org.batfish.datamodel.Configuration) IpProtocol(org.batfish.datamodel.IpProtocol) RoutingProtocol(org.batfish.datamodel.RoutingProtocol) Protocol(org.batfish.symbolic.Protocol) MatchProtocol(org.batfish.datamodel.routing_policy.expr.MatchProtocol)

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