1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
|
/*******************************************************************************
* Copyright (c) 2017 Politecnico di Torino and others.
*
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Apache License, Version 2.0
* which accompanies this distribution, and is available at
* http://www.apache.org/licenses/LICENSE-2.0
*******************************************************************************/
package mcnet.components;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import com.microsoft.z3.BoolExpr;
import com.microsoft.z3.Context;
import com.microsoft.z3.Expr;
import com.microsoft.z3.IntExpr;
import com.microsoft.z3.Model;
import com.microsoft.z3.Solver;
import com.microsoft.z3.Status;
/**Various checks for specific properties in the network.
*
*/
public class Checker {
Context ctx;
Network net;
NetContext nctx;
Solver solver;
ArrayList<BoolExpr> constraints;
public BoolExpr [] assertions;
public Status result;
public Model model;
public Checker(Context context,NetContext nctx,Network network){
this.ctx = context;
this.net = network;
this.nctx = nctx;
this.solver = ctx.mkSolver();
this.constraints = new ArrayList<BoolExpr>();
}
/**Resets the constraints
*
*/
public void clearState (){
this.solver.reset();
this.constraints = new ArrayList<BoolExpr>();
}
/**Checks whether the source provided can reach the destination
*
* @param src
* @param dest
* @return
*/
public IsolationResult checkIsolationProperty (NetworkObject src, NetworkObject dest){
assert(net.elements.contains(src));
assert(net.elements.contains(dest));
solver.push ();
addConstraints();
Expr p0 = ctx.mkConst("check_isolation_p0_"+src.getZ3Node()+"_"+dest.getZ3Node(), nctx.packet);
Expr p1 = ctx.mkConst("check_isolation_p1_"+src.getZ3Node()+"_"+dest.getZ3Node(), nctx.packet);
Expr n_0 =ctx.mkConst("check_isolation_n_0_"+src.getZ3Node()+"_"+dest.getZ3Node(), nctx.node);
Expr n_1 =ctx.mkConst("check_isolation_n_1_"+src.getZ3Node()+"_"+dest.getZ3Node(), nctx.node);
IntExpr t_0 = ctx.mkIntConst("check_isolation_t0_"+src.getZ3Node()+"_"+dest.getZ3Node());
IntExpr t_1 = ctx.mkIntConst("check_isolation_t1_"+src.getZ3Node()+"_"+dest.getZ3Node());
// Constraint1 recv(n_0,destNode,p0,t_0)
this.solver.add((BoolExpr)nctx.recv.apply(n_0, dest.getZ3Node(), p0, t_0));
// Constraint2 send(srcNode,n_1,p1,t_1)
this.solver.add((BoolExpr)nctx.send.apply(src.getZ3Node(), n_1, p1, t_1));
// Constraint3 nodeHasAddr(srcNode,p1.srcAddr)
this.solver.add((BoolExpr)nctx.nodeHasAddr.apply(src.getZ3Node(), nctx.pf.get("src").apply(p1)));
// Constraint4 p1.origin == srcNode
this.solver.add(ctx.mkEq(nctx.pf.get("origin").apply(p1), src.getZ3Node()));
// Constraint5 nodeHasAddr(destNode,p1.destAddr)
this.solver.add((BoolExpr)nctx.nodeHasAddr.apply(dest.getZ3Node(), nctx.pf.get("dest").apply(p1)));
//NON sembrano necessari
// this.solver.add(z3.Or(this.ctx.nodeHasAddr(src.getZ3Node(), this.ctx.packet.src(p0)),\
// this.ctx.nodeHasAddr(n_0, this.ctx.packet.src(p0)),\
// this.ctx.nodeHasAddr(n_1, this.ctx.packet.src(p0))))
//this.solver.add(this.ctx.packet.dest(p1) == this.ctx.packet.dest(p0))
// Constraint6 p1.origin == p0.origin
this.solver.add(ctx.mkEq(nctx.pf.get("origin").apply(p1),nctx.pf.get("origin").apply(p0)));
// Constraint7 nodeHasAddr(destNode, p0.destAddr)
this.solver.add((BoolExpr)nctx.nodeHasAddr.apply(dest.getZ3Node(), nctx.pf.get("dest").apply(p0)));
result = this.solver.check();
model = null;
assertions = this.solver.getAssertions();
if (result == Status.SATISFIABLE){
model = this.solver.getModel();
}
this.solver.pop();
return new IsolationResult(ctx,result, p0, n_0, t_1, t_0, nctx, assertions, model);
}
public IsolationResult CheckIsolationFlowProperty (NetworkObject src, NetworkObject dest){
assert(net.elements.contains(src));
assert(net.elements.contains(dest));
solver.push ();
addConstraints();
Expr p = ctx.mkConst("check_isolation_p_"+src.getZ3Node()+"_"+dest.getZ3Node(), nctx.packet);
Expr n_0 =ctx.mkConst("check_isolation_n_0_"+src.getZ3Node()+"_"+dest.getZ3Node(), nctx.node);
Expr n_1 =ctx.mkConst("check_isolation_n_1_"+src.getZ3Node()+"_"+dest.getZ3Node(), nctx.node);
Expr n_2 =ctx.mkConst("check_isolation_n_1_"+src.getZ3Node()+"_"+dest.getZ3Node(), nctx.node);
IntExpr t_0 = ctx.mkIntConst("check_isolation_t0_"+src.getZ3Node()+"_"+dest.getZ3Node());
IntExpr t_1 = ctx.mkIntConst("check_isolation_t1_"+src.getZ3Node()+"_"+dest.getZ3Node());
IntExpr t_2 = ctx.mkIntConst("check_isolation_t2_"+src.getZ3Node()+"_"+dest.getZ3Node());
// Constraint1 recv(n_0,destNode,p,t_0)
this.solver.add((BoolExpr)nctx.recv.apply(n_0, dest.getZ3Node(), p, t_0));
// Constraint2 send(srcNode,n_1,p1,t_1)
this.solver.add((BoolExpr)nctx.send.apply(src.getZ3Node(), n_1, p, t_1));
// Constraint3 nodeHasAddr(srcNode,p.srcAddr)
this.solver.add((BoolExpr)nctx.nodeHasAddr.apply(src.getZ3Node(), nctx.pf.get("src").apply(p)));
// Constraint4 p.origin == srcNode
this.solver.add(ctx.mkEq(nctx.pf.get("origin").apply(p), src.getZ3Node()));
Expr p_2 = ctx.mkConst("check_isolation_p_flow_"+src.getZ3Node()+"_"+dest.getZ3Node(), nctx.packet);
// Constraint5 there does not exist p_2, n_2, t_2 :
// send(destNode,n_2,p_2,t_2) &&
// p_2.srcAddr == p. destAddr &&
// p_2.srcPort == p.destPort &&
// p_2.destPort == p.srcPort &&
// p_2.destt == p.src &&
// t_2 < t_0
this.solver.add(ctx.mkNot(ctx.mkExists(new Expr[]{p_2,n_2,t_2},
ctx.mkAnd(
(BoolExpr)nctx.send.apply(dest.getZ3Node(), n_2, p_2, t_2),
ctx.mkEq(nctx.pf.get("src").apply(p_2), nctx.pf.get("dest").apply(p)),
ctx.mkEq(nctx.pf.get("src_port").apply(p_2), nctx.pf.get("dest_port").apply(p)),
ctx.mkEq(nctx.pf.get("dest_port").apply(p_2), nctx.pf.get("src_port").apply(p)),
ctx.mkEq(nctx.pf.get("dest").apply(p_2), nctx.pf.get("src").apply(p)),
ctx.mkLt(t_2,t_0)),1,null,null,null,null)));
result = this.solver.check();
model = null;
assertions = this.solver.getAssertions();
if (result == Status.SATISFIABLE){
model = this.solver.getModel();
}
this.solver.pop();
return new IsolationResult(ctx,result, p, n_0, t_1, t_0, nctx, assertions, model);
}
public IsolationResult CheckNodeTraversalProperty (NetworkObject src, NetworkObject dest, NetworkObject node){
assert(net.elements.contains(src));
assert(net.elements.contains(dest));
solver.push ();
addConstraints();
Expr p = ctx.mkConst("check_isolation_p_"+src.getZ3Node()+"_"+dest.getZ3Node(), nctx.packet);
Expr n_0 =ctx.mkConst("check_isolation_n_0_"+src.getZ3Node()+"_"+dest.getZ3Node(), nctx.node);
Expr n_1 =ctx.mkConst("check_isolation_n_1_"+src.getZ3Node()+"_"+dest.getZ3Node(), nctx.node);
Expr n_2 =ctx.mkConst("check_isolation_n_1_"+src.getZ3Node()+"_"+dest.getZ3Node(), nctx.node);
IntExpr t_0 = ctx.mkIntConst("check_isolation_t0_"+src.getZ3Node()+"_"+dest.getZ3Node());
IntExpr t_1 = ctx.mkIntConst("check_isolation_t1_"+src.getZ3Node()+"_"+dest.getZ3Node());
IntExpr t_2 = ctx.mkIntConst("check_isolation_t2_"+src.getZ3Node()+"_"+dest.getZ3Node());
// Constraint1 recv(n_0,destNode,p,t_0)
this.solver.add((BoolExpr)nctx.recv.apply(n_0, dest.getZ3Node(), p, t_0));
// Constraint2 send(srcNode,n_1,p1,t_1)
this.solver.add((BoolExpr)nctx.send.apply(src.getZ3Node(), n_1, p, t_1));
// Constraint3 nodeHasAddr(srcNode,p.srcAddr)
this.solver.add((BoolExpr)nctx.nodeHasAddr.apply(src.getZ3Node(), nctx.pf.get("src").apply(p)));
// Constraint4 p.origin == srcNode
this.solver.add(ctx.mkEq(nctx.pf.get("origin").apply(p), src.getZ3Node()));
// Constraint5 there does not exist n_2, t_2 : recv(n_2,node,p,t_2) && t_2 < t_0
this.solver.add(ctx.mkNot(ctx.mkExists(new Expr[]{n_2,t_2},
ctx.mkAnd(
(BoolExpr)nctx.recv.apply(n_2, node.getZ3Node(), p, t_2),
ctx.mkLt(t_2,t_0)),1,null,null,null,null)));
// Constraint 6 there does not exist n_2, t_2 : send(node,n_2,p,t_2) && t_2 < t_0
this.solver.add(ctx.mkNot(ctx.mkExists(new Expr[]{n_2,t_2},
ctx.mkAnd(
(BoolExpr)nctx.send.apply(node.getZ3Node(), n_2, p, t_2),
ctx.mkLt(t_2,t_0)),1,null,null,null,null)));
result = this.solver.check();
model = null;
assertions = this.solver.getAssertions();
if (result == Status.SATISFIABLE){
model = this.solver.getModel();
}
this.solver.pop();
return new IsolationResult(ctx,result, p, n_0, t_1, t_0, nctx, assertions, model);
}
public IsolationResult CheckLinkTraversalProperty (NetworkObject src, NetworkObject dest, NetworkObject le0, NetworkObject le1){
assert(net.elements.contains(src));
assert(net.elements.contains(dest));
solver.push ();
addConstraints();
Expr p = ctx.mkConst("check_isolation_p_"+src.getZ3Node()+"_"+dest.getZ3Node(), nctx.packet);
Expr n_0 =ctx.mkConst("check_isolation_n_0_"+src.getZ3Node()+"_"+dest.getZ3Node(), nctx.node);
Expr n_1 =ctx.mkConst("check_isolation_n_1_"+src.getZ3Node()+"_"+dest.getZ3Node(), nctx.node);
IntExpr t_0 = ctx.mkIntConst("check_isolation_t0_"+src.getZ3Node()+"_"+dest.getZ3Node());
IntExpr t_1 = ctx.mkIntConst("check_isolation_t1_"+src.getZ3Node()+"_"+dest.getZ3Node());
IntExpr t_2 = ctx.mkIntConst("check_isolation_t2_"+src.getZ3Node()+"_"+dest.getZ3Node());
// Constraint1 recv(n_0,destNode,p,t_0)
this.solver.add((BoolExpr)nctx.recv.apply(n_0, dest.getZ3Node(), p, t_0));
// Constraint2 send(srcNode,n_1,p1,t_1)
this.solver.add((BoolExpr)nctx.send.apply(src.getZ3Node(), n_1, p, t_1));
// Constraint3 nodeHasAddr(srcNode,p.srcAddr)
this.solver.add((BoolExpr)nctx.nodeHasAddr.apply(src.getZ3Node(), nctx.pf.get("src").apply(p)));
// Constraint4 p.origin == srcNode
this.solver.add(ctx.mkEq(nctx.pf.get("origin").apply(p), src.getZ3Node()));
// Constraint5 ∃ t_1, t_2 :
// send(linkNode0,linkNode1,p,t_1) &&
// recv(linkNode0,linkNode1,p,t_2) &&
// t_1 < t_0 &&
// t_2 < t_0
this.solver.add(ctx.mkExists(new Expr[]{t_1,t_2},
ctx.mkAnd(
(BoolExpr)nctx.send.apply(le0.getZ3Node(), le1.getZ3Node(), p, t_1),
(BoolExpr)nctx.recv.apply(le0.getZ3Node(), le1.getZ3Node(), p, t_2),
ctx.mkLt(t_1,t_0),
ctx.mkLt(t_2,t_0)),1,null,null,null,null));
result = this.solver.check();
model = null;
assertions = this.solver.getAssertions();
if (result == Status.SATISFIABLE){
model = this.solver.getModel();
}
this.solver.pop();
return new IsolationResult(ctx,result, p, n_0, t_1, t_0, nctx, assertions, model);
}
public Result CheckDataIsolationPropertyCore (NetworkObject src, NetworkObject dest){
assert(net.elements.contains(src));
assert(net.elements.contains(dest));
List<BoolExpr> constr = this.getConstraints();
Expr p = ctx.mkConst("check_isolation_p_"+src.getZ3Node()+"_"+dest.getZ3Node(), nctx.packet);
Expr n_0 =ctx.mkConst("check_isolation_n_0_"+src.getZ3Node()+"_"+dest.getZ3Node(), nctx.node);
IntExpr t = ctx.mkIntConst("check_isolation_t_"+src.getZ3Node()+"_"+dest.getZ3Node());
// Constraint1 recv(n_0,destNode,p,t)
constr.add((BoolExpr)nctx.recv.apply(n_0, dest.getZ3Node(), p, t));
// Constraint2 p.origin == srcNode
this.solver.add(ctx.mkEq(nctx.pf.get("origin").apply(p), src.getZ3Node()));
this.solver.push();
// Constraint3 for each constraint( n -> constraint)
ArrayList<BoolExpr> names =new ArrayList<BoolExpr>();
for(BoolExpr con : constr){
BoolExpr n = ctx.mkBoolConst(""+con);
names.add(n);
this.solver.add(ctx.mkImplies(n, con));
}
BoolExpr[] nam = new BoolExpr[names.size()];
result = this.solver.check(names.toArray(nam));
Result ret =null;
if (result == Status.SATISFIABLE){
System.out.println("SAT");
ret = new Result(ctx,this.solver.getModel());
}else if(result == Status.UNSATISFIABLE){
System.out.println("unsat");
ret = new Result(ctx,this.solver.getUnsatCore());
}
this.solver.pop();
return ret;
}
public DataIsolationResult CheckDataIsolationProperty(NetworkObject src, NetworkObject dest){
assert(net.elements.contains(src));
assert(net.elements.contains(dest));
solver.push ();
addConstraints();
Expr p = ctx.mkConst("check_isolation_p_"+src.getZ3Node()+"_"+dest.getZ3Node(), nctx.packet);
Expr n_0 =ctx.mkConst("check_isolation_n_0_"+src.getZ3Node()+"_"+dest.getZ3Node(), nctx.node);
IntExpr t = ctx.mkIntConst("check_isolation_t_"+src.getZ3Node()+"_"+dest.getZ3Node());
// Constraint1 recv(n_0,destNode,p,t)
this.solver.add((BoolExpr)nctx.recv.apply(n_0, dest.getZ3Node(), p, t));
// Constraint2 p.origin == srcNode
this.solver.add(ctx.mkEq(nctx.pf.get("origin").apply(p), src.getZ3Node()));
result = this.solver.check();
model = null;
assertions = this.solver.getAssertions();
if (result == Status.SATISFIABLE){
model = this.solver.getModel();
}
this.solver.pop();
return new DataIsolationResult(ctx,result, p, n_0, t, nctx, assertions, model);
}
public void addConstraints(){
nctx.addConstraints(solver);
net.addConstraints(solver);
for (NetworkObject el : net.elements)
el.addConstraints(solver);
}
public List<BoolExpr> getConstraints(){
Solver l = ctx.mkSolver();
nctx.addConstraints(l);
net.addConstraints(l);
for (NetworkObject el : net.elements)
el.addConstraints(l);
return Arrays.asList(l.getAssertions());
}
}
|