def gen_ast(expr,assign = False): #print "gen ast" ast_type, args = get_info(expr) #if(assign): # print "assign",ast_type , args #print "gen_ast type = ",ast_type, "args = ",args if (ast_type == "var"): return Z3VarTable.getvar(args[0],assign) elif (ast_type == "bool_expr"): return Z3VarTable.getvar(args[0],assign) elif (isAssign(ast_type)): return PredicateTable.insert(ast_type,gen_ast(args[0],True),gen_ast(args[1])) elif(isPredicate(ast_type)): #print "inserting predicate" return PredicateTable.insert(ast_type,gen_ast(args[0]),gen_ast(args[1])) #return Predicate.create(ast_type,gen_ast(args[0]),gen_ast(args[1])) elif(isBinary(ast_type)): return Bop.create(ast_type,gen_ast(args[0]),gen_ast(args[1])) elif(isUnary(ast_type)): return Uop.create(ast_type,gen_ast(args[0])) elif(isValue(ast_type)): return gen_const(ast_type,args[0]) else: raise Exception ("Parsing Ast, get %s"%ast_type)
def parse_declaration(filename): with open(filename) as f: lines = f.readlines() for line in lines: var_type, var_name = line.strip().split() if "=" in var_name: var_name, var_value = var_name.split("=") var_name = var_name.strip() var_value = var_value.strip() Z3VarTable.insert(var_name,var_type, is_input = False , init_value = var_value ) #print var_type, var_name , var_value else: #print var_type, var_name Z3VarTable.insert(var_name,var_type, is_input = False )
def getInitSolver(var): s = Solver() if (var.min_max is not None): print "adding min_max for ", var min_value, max_value = var.min_max z3var = Z3VarTable.get(var) s.add(min_value <= z3var) s.add(z3var <= max_value) return s
def gen_minterm(solver, var, predicates, depth, limit): if (solver.check() != sat): #print predicates return if (depth == limit): #print solver.model() #minterm = Minterm(predicates) minterm = MintermTable.insert(var, predicates, solver.model()[Z3VarTable.get(var)]) for predicate in predicates: PredicateMintermMap.insert_mapping(predicate, minterm) return pred = PredicateTable.table[var][depth] """ Case by Case analysis a). pred not duplicate: Label 1 and Label 3 get executuede b). pred in predicates -> not pred not in predicates => Label 2 and Label 3 get executed c). not pred in predicates -> pred not in predicates => # Label 1 and 4 get executed """ if (pred not in predicates): # Label 1 solver.push() solver.add(pred.toZ3()) gen_minterm(solver, var, predicates + [pred], depth + 1, limit) solver.pop() else: # Label 2 gen_minterm(solver, var, predicates, depth + 1, limit) if (pred.negate() not in predicates): # Label 3 solver.push() solver.add(pred.negate().toZ3()) gen_minterm(solver, var, predicates + [pred.negate()], depth + 1, limit) solver.pop() else: #Label 4 gen_minterm(solver, var, predicates, depth + 1, limit)
def setup_vartable(declare_in): for var in [ 'ethDst' , 'ethSrc', 'ethType', 'ipProto' ]: #print var Z3VarTable.insert(var,"int",True) Z3VarTable.insert(var+"_out","int") Z3VarTable.insert("ip4Src","ip",True) Z3VarTable.insert("ip4Src_out","ip") Z3VarTable.insert("ip4Dst","ip",True) Z3VarTable.insert("ip4Dst_out","ip") Z3VarTable.insert('port_id',"int",True) #Z3VarTable.insert("CONST_TRUE","bool",True) #Z3VarTable.insert("CONST_FALSE","bool",True) Z3VarTable.insert('Port',"int",is_input = False , min_max = (1,65536) ) parse_declaration(declare_in) variables = Z3VarTable.vartable
def gen_minterm2(solver, sofar, var, predicates, depth, limit): if (solver.check() != sat): #print predicates return if (depth == limit): #print solver.model() #minterm = Minterm(predicates) minterm = MintermTable.insert(var, predicates, solver.model()[Z3VarTable.get(var)]) for predicate in predicates: PredicateMintermMap.insert_mapping(predicate, minterm) return pred = PredicateTable.table[var][depth] predz3 = pred.toZ3() negpred = pred.negate() negpredz3 = negpred.toZ3() """ Case by Case analysis a). pred not duplicate: Label 1 and Label 3 get executuede b). pred in predicates -> not pred not in predicates => Label 2 and Label 3 get executed c). not pred in predicates -> pred not in predicates => # Label 1 and 4 get executed """ if (pred not in predicates): # Label 1 if (check_implies(sofar, predz3)): #print sofar, predz3 gen_minterm2(solver, sofar, var, predicates, depth + 1, limit) elif (check_implies(predz3, sofar)): print "swapping for ", var, predz3, sofar newsolver = getInitSolver(var) newsolver.add(predz3) gen_minterm2(newsolver, predz3, var, [pred], depth + 1, limit) else: solver.push() solver.add(predz3) gen_minterm2(solver, And(sofar, predz3), var, predicates + [pred], depth + 1, limit) solver.pop() else: # Label 2 gen_minterm2(solver, sofar, var, predicates, depth + 1, limit) if (negpred not in predicates): # Label 3 if (check_implies(sofar, negpredz3)): #print sofar, predz3 gen_minterm2(solver, sofar, var, predicates, depth + 1, limit) elif (check_implies(negpredz3, sofar)): print "swapping for ", var, predz3, sofar newsolver = getInitSolver(var) newsolver.add(negpredz3) gen_minterm2(newsolver, negpredz3, var, [negpred], depth + 1, limit) else: solver.push() solver.add(negpredz3) gen_minterm2(solver, And(sofar, negpredz3), var, predicates + [negpred], depth + 1, limit) solver.pop() else: #Label 4 gen_minterm2(solver, sofar, var, predicates, depth + 1, limit)