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;
}
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;
}
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);
}
}
}
}
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() + ")");
}
});
}
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)));
}
}
}
}
Aggregations