def test_bv_zero_ule(self): x = Symbol("x", BVType(32)) f = BVULE(BVZero(32), x) self.check_equal_and_valid(f, Bool(True))
def walk_identity_bv(self, formula, args, **kwargs): #pylint: disable=unused-argument assert formula is not None assert len(args) == 0 return BVType(formula.bv_width())
def parse_string(self, strinput): hts = HTS() ts = TS() nodemap = {} node_covered = set([]) # list of tuples of var and cond_assign_list # cond_assign_list is tuples of (condition, value) # where everything is a pysmt FNode # for btor, the condition is always True ftrans = [] initlist = [] invarlist = [] invar_props = [] ltl_props = [] prop_count = 0 # clean string input, remove special characters from names for sc, rep in special_char_replacements.items(): strinput = strinput.replace(sc, rep) def getnode(nid): node_covered.add(nid) if int(nid) < 0: return Ite(BV2B(nodemap[str(-int(nid))]), BV(0,1), BV(1,1)) return nodemap[nid] def binary_op(bvop, bop, left, right): if (get_type(left) == BOOL) and (get_type(right) == BOOL): return bop(left, right) return bvop(B2BV(left), B2BV(right)) def unary_op(bvop, bop, left): if (get_type(left) == BOOL): return bop(left) return bvop(left) for line in strinput.split(NL): linetok = line.split() if len(linetok) == 0: continue if linetok[0] == COM: continue (nid, ntype, *nids) = linetok if ntype == SORT: (stype, *attr) = nids if stype == BITVEC: nodemap[nid] = BVType(int(attr[0])) node_covered.add(nid) if stype == ARRAY: nodemap[nid] = ArrayType(getnode(attr[0]), getnode(attr[1])) node_covered.add(nid) if ntype == WRITE: nodemap[nid] = Store(*[getnode(n) for n in nids[1:4]]) if ntype == READ: nodemap[nid] = Select(getnode(nids[1]), getnode(nids[2])) if ntype == ZERO: nodemap[nid] = BV(0, getnode(nids[0]).width) if ntype == ONE: nodemap[nid] = BV(1, getnode(nids[0]).width) if ntype == ONES: width = getnode(nids[0]).width nodemap[nid] = BV((2**width)-1, width) if ntype == REDOR: width = get_type(getnode(nids[1])).width zeros = BV(0, width) nodemap[nid] = BVNot(BVComp(getnode(nids[1]), zeros)) if ntype == REDXOR: width = get_type(getnode(nids[1])).width nodemap[nid] = BV(0, width) zeros = BV(0, width) for yx_i in range(width): tmp = BV(1 << yx_i, width) tmp_2 = BVAnd(tmp, B2BV(getnode(nids[1]))) tmp_3 = BVZExt(B2BV(BVComp(tmp_2, zeros)), int(width - 1)) nodemap[nid] = BVAdd(tmp_3, nodemap[nid]) nodemap[nid] = BVComp(BVAnd(BV(1, width), nodemap[nid]), BV(1, width)) if ntype == REDAND: width = get_type(getnode(nids[1])).width ones = BV((2**width)-1, width) nodemap[nid] = BVComp(getnode(nids[1]), ones) if ntype == CONSTD: width = getnode(nids[0]).width nodemap[nid] = BV(int(nids[1]), width) if ntype == CONST: width = getnode(nids[0]).width nodemap[nid] = BV(bin_to_dec(nids[1]), width) if ntype == STATE: if len(nids) > 1: nodemap[nid] = Symbol(nids[1], getnode(nids[0])) else: nodemap[nid] = Symbol((SN%nid), getnode(nids[0])) ts.add_state_var(nodemap[nid]) if ntype == INPUT: if len(nids) > 1: nodemap[nid] = Symbol(nids[1], getnode(nids[0])) else: nodemap[nid] = Symbol((SN%nid), getnode(nids[0])) ts.add_input_var(nodemap[nid]) if ntype == OUTPUT: # unfortunately we need to create an extra symbol just to have the output name # we could be smarter about this, but then this parser can't be greedy original_symbol = getnode(nids[0]) output_symbol = Symbol(nids[1], original_symbol.get_type()) nodemap[nid] = EqualsOrIff(output_symbol, original_symbol) invarlist.append(nodemap[nid]) node_covered.add(nid) ts.add_output_var(output_symbol) if ntype == AND: nodemap[nid] = binary_op(BVAnd, And, getnode(nids[1]), getnode(nids[2])) if ntype == CONCAT: nodemap[nid] = BVConcat(B2BV(getnode(nids[1])), B2BV(getnode(nids[2]))) if ntype == XOR: nodemap[nid] = binary_op(BVXor, Xor, getnode(nids[1]), getnode(nids[2])) if ntype == XNOR: nodemap[nid] = BVNot(binary_op(BVXor, Xor, getnode(nids[1]), getnode(nids[2]))) if ntype == NAND: bvop = lambda x,y: BVNot(BVAnd(x, y)) bop = lambda x,y: Not(And(x, y)) nodemap[nid] = binary_op(bvop, bop, getnode(nids[1]), getnode(nids[2])) if ntype == IMPLIES: nodemap[nid] = BVOr(BVNot(getnode(nids[1])), getnode(nids[2])) if ntype == NOT: nodemap[nid] = unary_op(BVNot, Not, getnode(nids[1])) if ntype == NEG: nodemap[nid] = unary_op(BVNeg, Not, getnode(nids[1])) if ntype == UEXT: nodemap[nid] = BVZExt(B2BV(getnode(nids[1])), int(nids[2])) if ntype == SEXT: nodemap[nid] = BVSExt(B2BV(getnode(nids[1])), int(nids[2])) if ntype == OR: nodemap[nid] = binary_op(BVOr, Or, getnode(nids[1]), getnode(nids[2])) if ntype == ADD: nodemap[nid] = BVAdd(B2BV(getnode(nids[1])), B2BV(getnode(nids[2]))) if ntype == SUB: nodemap[nid] = BVSub(B2BV(getnode(nids[1])), B2BV(getnode(nids[2]))) if ntype == UGT: nodemap[nid] = BVUGT(B2BV(getnode(nids[1])), B2BV(getnode(nids[2]))) if ntype == UGTE: nodemap[nid] = BVUGE(B2BV(getnode(nids[1])), B2BV(getnode(nids[2]))) if ntype == ULT: nodemap[nid] = BVULT(B2BV(getnode(nids[1])), B2BV(getnode(nids[2]))) if ntype == ULTE: nodemap[nid] = BVULE(B2BV(getnode(nids[1])), B2BV(getnode(nids[2]))) if ntype == SGT: nodemap[nid] = BVSGT(B2BV(getnode(nids[1])), B2BV(getnode(nids[2]))) if ntype == SGTE: nodemap[nid] = BVSGE(B2BV(getnode(nids[1])), B2BV(getnode(nids[2]))) if ntype == SLT: nodemap[nid] = BVSLT(B2BV(getnode(nids[1])), B2BV(getnode(nids[2]))) if ntype == SLTE: nodemap[nid] = BVSLE(B2BV(getnode(nids[1])), B2BV(getnode(nids[2]))) if ntype == EQ: nodemap[nid] = BVComp(B2BV(getnode(nids[1])), B2BV(getnode(nids[2]))) if ntype == NEQ: nodemap[nid] = BVNot(BVComp(getnode(nids[1]), getnode(nids[2]))) if ntype == MUL: nodemap[nid] = BVMul(B2BV(getnode(nids[1])), B2BV(getnode(nids[2]))) if ntype == SLICE: nodemap[nid] = BVExtract(B2BV(getnode(nids[1])), int(nids[3]), int(nids[2])) if ntype == SLL: nodemap[nid] = BVLShl(getnode(nids[1]), getnode(nids[2])) if ntype == SRA: nodemap[nid] = BVAShr(getnode(nids[1]), getnode(nids[2])) if ntype == SRL: nodemap[nid] = BVLShr(getnode(nids[1]), getnode(nids[2])) if ntype == ITE: if (get_type(getnode(nids[2])) == BOOL) or (get_type(getnode(nids[3])) == BOOL): nodemap[nid] = Ite(BV2B(getnode(nids[1])), B2BV(getnode(nids[2])), B2BV(getnode(nids[3]))) else: nodemap[nid] = Ite(BV2B(getnode(nids[1])), getnode(nids[2]), getnode(nids[3])) if ntype == NEXT: if (get_type(getnode(nids[1])) == BOOL) or (get_type(getnode(nids[2])) == BOOL): lval = TS.get_prime(getnode(nids[1])) rval = BV2B(getnode(nids[2])) else: lval = TS.get_prime(getnode(nids[1])) rval = getnode(nids[2]) nodemap[nid] = EqualsOrIff(lval, rval) ftrans.append( (lval, [(TRUE(), rval)]) ) if ntype == INIT: if (get_type(getnode(nids[1])) == BOOL) or (get_type(getnode(nids[2])) == BOOL): nodemap[nid] = EqualsOrIff(BV2B(getnode(nids[1])), BV2B(getnode(nids[2]))) else: nodemap[nid] = EqualsOrIff(getnode(nids[1]), getnode(nids[2])) initlist.append(getnode(nid)) if ntype == CONSTRAINT: nodemap[nid] = BV2B(getnode(nids[0])) invarlist.append(getnode(nid)) if ntype == BAD: nodemap[nid] = getnode(nids[0]) if ASSERTINFO in line: filename_lineno = os.path.basename(nids[3]) assert_name = 'embedded_assertion_%s'%filename_lineno description = "Embedded assertion at line {1} in {0}".format(*filename_lineno.split(COLON_REP)) else: assert_name = 'embedded_assertion_%i'%prop_count description = 'Embedded assertion number %i'%prop_count prop_count += 1 # Following problem format (name, description, strformula) invar_props.append((assert_name, description, Not(BV2B(getnode(nid))))) if nid not in nodemap: Logger.error("Unknown node type \"%s\""%ntype) # get wirename if it exists if ntype not in {STATE, INPUT, OUTPUT, BAD}: # check for wirename, if it's an integer, then it's a node ref try: a = int(nids[-1]) except: try: wire = Symbol(str(nids[-1]), getnode(nids[0])) invarlist.append(EqualsOrIff(wire, B2BV(nodemap[nid]))) ts.add_var(wire) except: pass if Logger.level(1): name = lambda x: str(nodemap[x]) if nodemap[x].is_symbol() else x uncovered = [name(x) for x in nodemap if x not in node_covered] uncovered.sort() if len(uncovered) > 0: Logger.warning("Unlinked nodes \"%s\""%",".join(uncovered)) if not self.symbolic_init: init = simplify(And(initlist)) else: init = TRUE() invar = simplify(And(invarlist)) # instead of trans, we're using the ftrans format -- see below ts.set_behavior(init, TRUE(), invar) # add ftrans for var, cond_assign_list in ftrans: ts.add_func_trans(var, cond_assign_list) hts.add_ts(ts) return (hts, invar_props, ltl_props)
def parse_string(self, strinput): hts = HTS() ts = TS() nodemap = {} node_covered = set([]) # list of tuples of var and cond_assign_list # cond_assign_list is tuples of (condition, value) # where everything is a pysmt FNode # for btor, the condition is always True ftrans = [] initlist = [] invarlist = [] invar_props = [] ltl_props = [] prop_count = 0 # clean string input, remove special characters from names for sc, rep in special_char_replacements.items(): strinput = strinput.replace(sc, rep) def getnode(nid): node_covered.add(nid) if int(nid) < 0: return Ite(BV2B(nodemap[str(-int(nid))]), BV(0, 1), BV(1, 1)) return nodemap[nid] def binary_op(bvop, bop, left, right): if (get_type(left) == BOOL) and (get_type(right) == BOOL): return bop(left, right) return bvop(B2BV(left), B2BV(right)) def unary_op(bvop, bop, left): if (get_type(left) == BOOL): return bop(left) return bvop(left) for line in strinput.split(NL): linetok = line.split() if len(linetok) == 0: continue if linetok[0] == COM: continue (nid, ntype, *nids) = linetok if ntype == SORT: (stype, *attr) = nids if stype == BITVEC: nodemap[nid] = BVType(int(attr[0])) node_covered.add(nid) if stype == ARRAY: nodemap[nid] = ArrayType(getnode(attr[0]), getnode(attr[1])) node_covered.add(nid) if ntype == WRITE: nodemap[nid] = Store(*[getnode(n) for n in nids[1:4]]) if ntype == READ: nodemap[nid] = Select(getnode(nids[1]), getnode(nids[2])) if ntype == ZERO: nodemap[nid] = BV(0, getnode(nids[0]).width) if ntype == ONE: nodemap[nid] = BV(1, getnode(nids[0]).width) if ntype == ONES: width = getnode(nids[0]).width nodemap[nid] = BV((2**width) - 1, width) if ntype == REDOR: width = get_type(getnode(nids[1])).width zeros = BV(0, width) nodemap[nid] = BVNot(BVComp(getnode(nids[1]), zeros)) if ntype == REDAND: width = get_type(getnode(nids[1])).width ones = BV((2**width) - 1, width) nodemap[nid] = BVComp(getnode(nids[1]), ones) if ntype == CONSTD: width = getnode(nids[0]).width nodemap[nid] = BV(int(nids[1]), width) if ntype == CONST: width = getnode(nids[0]).width try: nodemap[nid] = BV(bin_to_dec(nids[1]), width) except ValueError: if not all([i == 'x' or i == 'z' for i in nids[1]]): raise RuntimeError( "If not a valid number, only support " "all don't cares or high-impedance but got {}". format(nids[1])) # create a fresh variable for this non-deterministic constant nodemap[nid] = Symbol('const_' + nids[1], BVType(width)) ts.add_state_var(nodemap[nid]) Logger.warning( "Creating a fresh symbol for unsupported X/Z constant %s" % nids[1]) if ntype == STATE: if len(nids) > 1: nodemap[nid] = Symbol(nids[1], getnode(nids[0])) else: nodemap[nid] = Symbol((SN % nid), getnode(nids[0])) ts.add_state_var(nodemap[nid]) if ntype == INPUT: if len(nids) > 1: nodemap[nid] = Symbol(nids[1], getnode(nids[0])) else: nodemap[nid] = Symbol((SN % nid), getnode(nids[0])) ts.add_input_var(nodemap[nid]) if ntype == OUTPUT: # unfortunately we need to create an extra symbol just to have the output name # we could be smarter about this, but then this parser can't be greedy original_symbol = B2BV(getnode(nids[0])) output_symbol = Symbol(nids[1], original_symbol.get_type()) nodemap[nid] = EqualsOrIff(output_symbol, original_symbol) invarlist.append(nodemap[nid]) node_covered.add(nid) ts.add_output_var(output_symbol) if ntype == AND: nodemap[nid] = binary_op(BVAnd, And, getnode(nids[1]), getnode(nids[2])) if ntype == CONCAT: nodemap[nid] = BVConcat(B2BV(getnode(nids[1])), B2BV(getnode(nids[2]))) if ntype == XOR: nodemap[nid] = binary_op(BVXor, Xor, getnode(nids[1]), getnode(nids[2])) if ntype == XNOR: nodemap[nid] = BVNot( binary_op(BVXor, Xor, getnode(nids[1]), getnode(nids[2]))) if ntype == NAND: bvop = lambda x, y: BVNot(BVAnd(x, y)) bop = lambda x, y: Not(And(x, y)) nodemap[nid] = binary_op(bvop, bop, getnode(nids[1]), getnode(nids[2])) if ntype == IMPLIES: nodemap[nid] = BVOr(BVNot(getnode(nids[1])), getnode(nids[2])) if ntype == NOT: nodemap[nid] = unary_op(BVNot, Not, getnode(nids[1])) if ntype == NEG: nodemap[nid] = unary_op(BVNeg, Not, getnode(nids[1])) if ntype == UEXT: nodemap[nid] = BVZExt(B2BV(getnode(nids[1])), int(nids[2])) if ntype == SEXT: nodemap[nid] = BVSExt(B2BV(getnode(nids[1])), int(nids[2])) if ntype == OR: nodemap[nid] = binary_op(BVOr, Or, getnode(nids[1]), getnode(nids[2])) if ntype == ADD: nodemap[nid] = BVAdd(B2BV(getnode(nids[1])), B2BV(getnode(nids[2]))) if ntype == SUB: nodemap[nid] = BVSub(B2BV(getnode(nids[1])), B2BV(getnode(nids[2]))) if ntype == UGT: nodemap[nid] = BVUGT(B2BV(getnode(nids[1])), B2BV(getnode(nids[2]))) if ntype == UGTE: nodemap[nid] = BVUGE(B2BV(getnode(nids[1])), B2BV(getnode(nids[2]))) if ntype == ULT: nodemap[nid] = BVULT(B2BV(getnode(nids[1])), B2BV(getnode(nids[2]))) if ntype == ULTE: nodemap[nid] = BVULE(B2BV(getnode(nids[1])), B2BV(getnode(nids[2]))) if ntype == SGT: nodemap[nid] = BVSGT(B2BV(getnode(nids[1])), B2BV(getnode(nids[2]))) if ntype == SGTE: nodemap[nid] = BVSGE(B2BV(getnode(nids[1])), B2BV(getnode(nids[2]))) if ntype == SLT: nodemap[nid] = BVSLT(B2BV(getnode(nids[1])), B2BV(getnode(nids[2]))) if ntype == SLTE: nodemap[nid] = BVSLE(B2BV(getnode(nids[1])), B2BV(getnode(nids[2]))) if ntype == EQ: nodemap[nid] = BVComp(B2BV(getnode(nids[1])), B2BV(getnode(nids[2]))) if ntype == NEQ: nodemap[nid] = BVNot(BVComp(getnode(nids[1]), getnode(nids[2]))) if ntype == MUL: nodemap[nid] = BVMul(B2BV(getnode(nids[1])), B2BV(getnode(nids[2]))) if ntype == SLICE: nodemap[nid] = BVExtract(B2BV(getnode(nids[1])), int(nids[3]), int(nids[2])) if ntype == SLL: nodemap[nid] = BVLShl(getnode(nids[1]), getnode(nids[2])) if ntype == SRA: nodemap[nid] = BVAShr(getnode(nids[1]), getnode(nids[2])) if ntype == SRL: nodemap[nid] = BVLShr(getnode(nids[1]), getnode(nids[2])) if ntype == ITE: if (get_type(getnode(nids[2])) == BOOL) or (get_type( getnode(nids[3])) == BOOL): nodemap[nid] = Ite(BV2B(getnode(nids[1])), B2BV(getnode(nids[2])), B2BV(getnode(nids[3]))) else: nodemap[nid] = Ite(BV2B(getnode(nids[1])), getnode(nids[2]), getnode(nids[3])) if ntype == NEXT: if (get_type(getnode(nids[1])) == BOOL) or (get_type( getnode(nids[2])) == BOOL): lval = TS.get_prime(getnode(nids[1])) rval = B2BV(getnode(nids[2])) else: lval = TS.get_prime(getnode(nids[1])) rval = getnode(nids[2]) nodemap[nid] = EqualsOrIff(lval, rval) ftrans.append((lval, [(TRUE(), rval)])) if ntype == INIT: if (get_type(getnode(nids[1])) == BOOL) or (get_type( getnode(nids[2])) == BOOL): nodemap[nid] = EqualsOrIff(BV2B(getnode(nids[1])), BV2B(getnode(nids[2]))) elif get_type(getnode(nids[1])).is_array_type(): _type = get_type(getnode(nids[1])) nodemap[nid] = EqualsOrIff( getnode(nids[1]), Array(_type.index_type, default=getnode(nids[2]))) else: nodemap[nid] = EqualsOrIff(getnode(nids[1]), getnode(nids[2])) initlist.append(getnode(nid)) if ntype == CONSTRAINT: nodemap[nid] = BV2B(getnode(nids[0])) invarlist.append(getnode(nid)) if ntype == BAD: nodemap[nid] = getnode(nids[0]) if len(nids) > 1: assert_name = nids[1] description = "Embedded assertion: {}".format(assert_name) else: assert_name = 'embedded_assertion_%i' % prop_count description = 'Embedded assertion number %i' % prop_count prop_count += 1 # Following problem format (name, description, strformula) invar_props.append( (assert_name, description, Not(BV2B(getnode(nid))))) if nid not in nodemap: Logger.error("Unknown node type \"%s\"" % ntype) # get wirename if it exists if ntype not in {STATE, INPUT, OUTPUT, BAD}: # disregard comments at the end of the line try: symbol_idx = nids.index(';') symbol_idx -= 1 # the symbol should be before the comment except: # the symbol is just the end symbol_idx = -1 # check for wirename, if it's an integer, then it's a node ref try: a = int(nids[symbol_idx]) except: try: name = str(nids[symbol_idx]) # use the exact name, unless it has already been used wire = Symbol(name, getnode(nids[0])) if wire in ts.vars: wire = FreshSymbol(getnode(nids[0]), template=name + "%d") invarlist.append(EqualsOrIff(wire, B2BV(nodemap[nid]))) ts.add_var(wire) except: pass if Logger.level(1): name = lambda x: str(nodemap[x]) if nodemap[x].is_symbol() else x uncovered = [name(x) for x in nodemap if x not in node_covered] uncovered.sort() if len(uncovered) > 0: Logger.warning("Unlinked nodes \"%s\"" % ",".join(uncovered)) if not self.symbolic_init: init = simplify(And(initlist)) else: init = TRUE() invar = simplify(And(invarlist)) # instead of trans, we're using the ftrans format -- see below ts.set_behavior(init, TRUE(), invar) # add ftrans for var, cond_assign_list in ftrans: ts.add_func_trans(var, cond_assign_list) hts.add_ts(ts) return (hts, invar_props, ltl_props)
def walk_bv_comp(self, formula, args, **kwargs): # We check that all children are BV and the same size a, b = args if a != b or (not a.is_bv_type()): return None return BVType(1)
def compile_sts(self, name, params): sparser = StringParser() in_port, max_val, c_push, c_pop = list(params) max_val = int(max_val) if type(c_push) == str: c_push = sparser.parse_formula(c_push) if type(c_pop) == str: c_pop = sparser.parse_formula(c_pop) tracking = Symbol("%s.tracking" % name, BOOL) end = Symbol("%s.end" % name, BOOL) done = Symbol("%s.done" % name, BOOL) packet = Symbol("%s.packet" % name, BVType(in_port.symbol_type().width)) max_width = math.ceil(math.log(max_val) / math.log(2)) max_bvval = BV(max_val, max_width) zero = BV(0, max_width) one = BV(1, max_width) count = Symbol("%s.count" % name, BVType(max_width)) size = Symbol("%s.size" % name, BVType(max_width)) pos_c_push = BV2B(c_push) neg_c_push = Not(BV2B(c_push)) pos_c_pop = BV2B(c_pop) neg_c_pop = Not(BV2B(c_pop)) init = [] trans = [] invar = [] # INIT DEFINITION # # count = 0 init.append(EqualsOrIff(count, BV(0, max_width))) # tracking = False init.append(EqualsOrIff(tracking, FALSE())) # size = 0 init.append(EqualsOrIff(size, BV(0, max_width))) # end = false init.append(EqualsOrIff(end, FALSE())) # INVAR DEFINITION # # !done -> (end = (tracking & (size = count))) invar.append( Implies(Not(done), EqualsOrIff(end, And(tracking, EqualsOrIff(size, count))))) # count <= size invar.append(BVULE(count, size)) # count <= maxval invar.append(BVULE(count, max_bvval)) # size <= maxval invar.append(BVULE(size, max_bvval)) # done -> (end <-> False); invar.append(Implies(done, EqualsOrIff(end, FALSE()))) # done -> (count = 0_8); invar.append(Implies(done, EqualsOrIff(count, BV(0, max_width)))) # done -> (size = 0_8); invar.append(Implies(done, EqualsOrIff(size, BV(0, max_width)))) # done -> (packet = 0_8); invar.append( Implies(done, EqualsOrIff(packet, BV(0, in_port.symbol_type().width)))) # TRANS DEFINITION # # (!end & !done) -> next(!done); trans.append(Implies(And(Not(end), Not(done)), TS.to_next(Not(done)))) # end -> next(done); trans.append(Implies(end, TS.to_next(done))) # done -> next(done); trans.append(Implies(done, TS.to_next(done))) # tracking -> next(tracking); trans.append( Implies(Not(done), Implies(tracking, TS.to_next(tracking)))) # tracking -> (next(packet) = packet); trans.append( Implies(Not(done), Implies(tracking, EqualsOrIff(TS.to_next(packet), packet)))) # !tracking & next(tracking) -> c_push; trans.append( Implies( Not(done), Implies(And(Not(tracking), TS.to_next(tracking)), pos_c_push))) # (c_push & next(tracking)) -> ((packet = in) & (next(packet) = in); trans.append( Implies( Not(done), Implies( And(pos_c_push, TS.to_next(tracking)), And(EqualsOrIff(packet, in_port), EqualsOrIff(TS.to_next(packet), in_port))))) # (c_push & !c_pop & tracking) -> (next(count) = (count + 1_8)); trans.append( Implies( Not(done), Implies( And(pos_c_push, neg_c_pop, tracking), EqualsOrIff(TS.to_next(count), BVAdd(count, BV(1, max_width)))))) # (c_push & size < maxval) -> (next(size) = (size + 1_8)); trans.append( Implies( Not(done), Implies( And(pos_c_push, BVULT(size, max_bvval)), EqualsOrIff(TS.to_next(size), BVAdd(size, BV(1, max_width)))))) # (c_pop & size > 0) -> (next(size) = (size - 1_8)); trans.append( Implies( Not(done), Implies( And(pos_c_pop, BVUGT(size, zero)), EqualsOrIff(TS.to_next(size), BVSub(size, BV(1, max_width)))))) # (!(c_push | c_pop)) -> (next(count) = count); trans.append( Implies( Not(done), Implies(Not(Or(pos_c_push, pos_c_pop)), EqualsOrIff(count, TS.to_next(count))))) # ((c_push | c_pop) & !tracking) -> (next(count) = count); trans.append( Implies( Not(done), Implies(And(Or(pos_c_push, pos_c_pop), Not(tracking)), EqualsOrIff(count, TS.to_next(count))))) # (c_push & size = maxval) -> (next(size) = size); trans.append( Implies( Not(done), Implies(And(pos_c_push, EqualsOrIff(size, max_bvval)), EqualsOrIff(TS.to_next(size), size)))) # (!(c_push | c_pop)) -> (next(size) = size); trans.append( Implies( Not(done), Implies(Not(Or(pos_c_push, pos_c_pop)), EqualsOrIff(size, TS.to_next(size))))) # (!(c_push | c_pop)) -> (next(count) = count); trans.append( Implies( Not(done), Implies(Not(Or(pos_c_push, pos_c_pop)), EqualsOrIff(count, TS.to_next(count))))) # (c_pop & size = 0) -> (next(size) = 0); trans.append( Implies( Not(done), Implies(And(pos_c_pop, EqualsOrIff(size, zero)), EqualsOrIff(TS.to_next(size), zero)))) # (!c_push) -> (next(count) = count); trans.append( Implies(Not(done), Implies(neg_c_push, EqualsOrIff(TS.to_next(count), count)))) init = And(init) invar = And(invar) trans = And(trans) ts = TS() ts.vars, ts.init, ts.invar, ts.trans = set( [tracking, end, packet, count, size]), init, invar, trans return ts
VECT_WIDTH = 5 class Cell(Enum): s = BV(0, VECT_WIDTH) # space x = BV(1, VECT_WIDTH) # x - human player goes first o = BV(2, VECT_WIDTH) # o - cpu player x_turns = 0 o_turns = 0 x_val = Cell.x.value.constant_value() o_val = Cell.o.value.constant_value() board = [[FreshSymbol(BVType(VECT_WIDTH)) for _ in range(3)] for _ in range(3)] solver = Solver() # initialise board cells, each one has to be blank, x or o for row in board: for cell in row: solver.add_assertion(Or([Equals(cell, i.value) for i in Cell])) # load board test = 'tests/blank.txt' with open(test) as fh: for row, line in enumerate(fh.readlines()): for col, cell in enumerate(line.strip().split(' ')): if cell == Cell.x.name: solver.add_assertion(Equals(board[row][col], Cell.x.value))
def test_bv_zero_lshr(self): x = Symbol("x", BVType(32)) f = BVLShr(BVZero(32), x) self.check_equal_and_valid(f, BVZero(32))
def test_bv_lshr_underflow(self): x = Symbol("x", BVType(32)) f = BVLShr(x, BV(33, 32)) self.check_equal_and_valid(f, BVZero(32))
def test_bv_sub_eq(self): x, y = (Symbol(name, BVType(32)) for name in "xy") f = BVSub(BVMul(x, y), BVMul(x, y)) self.check_equal_and_valid(f, BVZero(32))
def test_bv_lshl_zero(self): x = Symbol("x", BVType(32)) f = BVLShl(x, BVZero(32)) self.check_equal_and_valid(f, x)
def test_bv_sub_zero(self): x = Symbol("x", BVType(32)) f = BVSub(x, BVZero(32)) self.check_equal_and_valid(f, x)
def test_bv_all_ones_or(self): x = Symbol("x", BVType(32)) f = BVOr(BV(2**32 - 1, 32), x) self.check_equal_and_valid(f, BV(2**32 - 1, 32))
def test_bv_and_all_ones(self): x = Symbol("x", BVType(32)) f = BVAnd(x, BV(2**32 - 1, 32)) self.check_equal_and_valid(f, x)
def generate_STS(self, lines): ts = TS("Additional system") init = TRUE() trans = TRUE() invar = TRUE() states = {} assigns = set([]) varsmap = {} def def_var(name, vtype): if name in varsmap: return varsmap[name] var = Symbol(name, vtype) ts.add_state_var(var) return var for line in lines: if line.comment: continue if line.init: if T_I not in states: states[T_I] = TRUE() if line.init.varname != "": (value, typev) = self.__get_value(line.init.value) ivar = def_var(line.init.varname, typev) state = EqualsOrIff(ivar, value) else: state = TRUE() if line.init.value == T_TRUE else FALSE() states[T_I] = And(states[T_I], state) # Optimization for the initial state assignment init = And(init, state) state = TRUE() if line.state: sname = T_S + line.state.id if (line.state.varname != ""): (value, typev) = self.__get_value(line.state.value) ivar = def_var(line.state.varname, typev) state = EqualsOrIff(ivar, value) assval = (sname, line.state.varname) if assval not in assigns: assigns.add(assval) else: Logger.error( "Double assignment for variable \"%s\" at state \"%s\"" % (line.state.varname, sname)) else: state = TRUE() if line.state.value == T_TRUE else FALSE() if sname not in states: states[sname] = TRUE() states[sname] = And(states[sname], state) stateid_width = math.ceil(math.log(len(states)) / math.log(2)) stateid_var = Symbol(self.new_state_id(), BVType(stateid_width)) init = And(init, EqualsOrIff(stateid_var, BV(0, stateid_width))) invar = And( invar, Implies(EqualsOrIff(stateid_var, BV(0, stateid_width)), states[T_I])) states[T_I] = EqualsOrIff(stateid_var, BV(0, stateid_width)) count = 1 state_items = list(states.keys()) state_items.sort() for state in state_items: if state == T_I: continue invar = And( invar, Implies(EqualsOrIff(stateid_var, BV(count, stateid_width)), states[state])) states[state] = EqualsOrIff(stateid_var, BV(count, stateid_width)) count += 1 transdic = {} for line in lines: if line.comment: continue if line.trans: if states[line.trans.start] not in transdic: transdic[states[line.trans.start]] = [] transdic[states[line.trans.start]].append( states[line.trans.end]) for transition in transdic: (start, end) = (transition, transdic[transition]) trans = And(trans, Implies(start, TS.to_next(Or(end)))) vars_ = [v for v in get_free_variables(trans) if not TS.is_prime(v)] vars_ += get_free_variables(init) vars_ += get_free_variables(invar) invar = And(invar, BVULE(stateid_var, BV(count - 1, stateid_width))) ts.set_behavior(init, trans, invar) ts.add_state_var(stateid_var) hts = HTS("ETS") hts.add_ts(ts) invar_props = [] ltl_props = [] return (hts, invar_props, ltl_props)
def test_bv_sle_eq(self): x, y = (Symbol(name, BVType(32)) for name in "xy") f = BVSLE(BVMul(x, y), BVMul(x, y)) self.check_equal_and_valid(f, Bool(True))
def get_full_example_formulae(environment=None): """Return a list of Examples using the given environment.""" if environment is None: environment = get_env() with environment: x = Symbol("x", BOOL) y = Symbol("y", BOOL) p = Symbol("p", INT) q = Symbol("q", INT) r = Symbol("r", REAL) s = Symbol("s", REAL) aii = Symbol("aii", ARRAY_INT_INT) ari = Symbol("ari", ArrayType(REAL, INT)) arb = Symbol("arb", ArrayType(REAL, BV8)) abb = Symbol("abb", ArrayType(BV8, BV8)) nested_a = Symbol("a_arb_aii", ArrayType(ArrayType(REAL, BV8), ARRAY_INT_INT)) rf = Symbol("rf", FunctionType(REAL, [REAL, REAL])) rg = Symbol("rg", FunctionType(REAL, [REAL])) ih = Symbol("ih", FunctionType(INT, [REAL, INT])) ig = Symbol("ig", FunctionType(INT, [INT])) bf = Symbol("bf", FunctionType(BOOL, [BOOL])) bg = Symbol("bg", FunctionType(BOOL, [BOOL])) bv3 = Symbol("bv3", BVType(3)) bv8 = Symbol("bv8", BV8) bv16 = Symbol("bv16", BV16) result = [ # Formula, is_valid, is_sat, is_qf Example(hr="(x & y)", expr=And(x, y), is_valid=False, is_sat=True, logic=pysmt.logics.QF_BOOL), Example(hr="(x <-> y)", expr=Iff(x, y), is_valid=False, is_sat=True, logic=pysmt.logics.QF_BOOL), Example(hr="((x | y) & (! (x | y)))", expr=And(Or(x, y), Not(Or(x, y))), is_valid=False, is_sat=False, logic=pysmt.logics.QF_BOOL), Example(hr="(x & (! y))", expr=And(x, Not(y)), is_valid=False, is_sat=True, logic=pysmt.logics.QF_BOOL), Example(hr="(False -> True)", expr=Implies(FALSE(), TRUE()), is_valid=True, is_sat=True, logic=pysmt.logics.QF_BOOL), Example(hr="((x | y) & (! (x | y)))", expr=And(Or(x, y), Not(Or(x, y))), is_valid=False, is_sat=False, logic=pysmt.logics.QF_BOOL), # # LIA # Example(hr="((q < p) & (x -> y))", expr=And(GT(p, q), Implies(x, y)), is_valid=False, is_sat=True, logic=pysmt.logics.QF_IDL), Example(hr="(((p + q) = 5) & (q < p))", expr=And(Equals(Plus(p, q), Int(5)), GT(p, q)), is_valid=False, is_sat=True, logic=pysmt.logics.QF_LIA), Example(hr="((q <= p) | (p <= q))", expr=Or(GE(p, q), LE(p, q)), is_valid=True, is_sat=True, logic=pysmt.logics.QF_IDL), Example(hr="(! (p < (q * 2)))", expr=Not(LT(p, Times(q, Int(2)))), is_valid=False, is_sat=True, logic=pysmt.logics.QF_LIA), Example(hr="(p < (p - (5 - 2)))", expr=GT(Minus(p, Minus(Int(5), Int(2))), p), is_valid=False, is_sat=False, logic=pysmt.logics.QF_IDL), Example(hr="((x ? 7 : ((p + -1) * 3)) = q)", expr=Equals( Ite(x, Int(7), Times(Plus(p, Int(-1)), Int(3))), q), is_valid=False, is_sat=True, logic=pysmt.logics.QF_LIA), Example(hr="(p < (q + 1))", expr=LT(p, Plus(q, Int(1))), is_valid=False, is_sat=True, logic=pysmt.logics.QF_LIA), # # LRA # Example(hr="((s < r) & (x -> y))", expr=And(GT(r, s), Implies(x, y)), is_valid=False, is_sat=True, logic=pysmt.logics.QF_RDL), Example(hr="(((r + s) = 28/5) & (s < r))", expr=And(Equals(Plus(r, s), Real(Fraction("5.6"))), GT(r, s)), is_valid=False, is_sat=True, logic=pysmt.logics.QF_LRA), Example(hr="((s <= r) | (r <= s))", expr=Or(GE(r, s), LE(r, s)), is_valid=True, is_sat=True, logic=pysmt.logics.QF_RDL), Example(hr="(! ((r * 2.0) < (s * 2.0)))", expr=Not(LT(Div(r, Real((1, 2))), Times(s, Real(2)))), is_valid=False, is_sat=True, logic=pysmt.logics.QF_LRA), Example(hr="(! (r < (r - (5.0 - 2.0))))", expr=Not(GT(Minus(r, Minus(Real(5), Real(2))), r)), is_valid=True, is_sat=True, logic=pysmt.logics.QF_RDL), Example(hr="((x ? 7.0 : ((s + -1.0) * 3.0)) = r)", expr=Equals( Ite(x, Real(7), Times(Plus(s, Real(-1)), Real(3))), r), is_valid=False, is_sat=True, logic=pysmt.logics.QF_LRA), # # EUF # Example(hr="(bf(x) <-> bg(x))", expr=Iff(Function(bf, (x, )), Function(bg, (x, ))), is_valid=False, is_sat=True, logic=pysmt.logics.QF_UF), Example(hr="(rf(5.0, rg(r)) = 0.0)", expr=Equals(Function(rf, (Real(5), Function(rg, (r, )))), Real(0)), is_valid=False, is_sat=True, logic=pysmt.logics.QF_UFLRA), Example(hr="((rg(r) = (5.0 + 2.0)) <-> (rg(r) = 7.0))", expr=Iff(Equals(Function(rg, [r]), Plus(Real(5), Real(2))), Equals(Function(rg, [r]), Real(7))), is_valid=True, is_sat=True, logic=pysmt.logics.QF_UFLRA), Example( hr="((r = (s + 1.0)) & (rg(s) = 5.0) & (rg((r - 1.0)) = 7.0))", expr=And([ Equals(r, Plus(s, Real(1))), Equals(Function(rg, [s]), Real(5)), Equals(Function(rg, [Minus(r, Real(1))]), Real(7)) ]), is_valid=False, is_sat=False, logic=pysmt.logics.QF_UFLRA), # # BV # Example(hr="((1_32 & 0_32) = 0_32)", expr=Equals(BVAnd(BVOne(32), BVZero(32)), BVZero(32)), is_valid=True, is_sat=True, logic=pysmt.logics.QF_BV), Example(hr="((! 2_3) = 5_3)", expr=Equals(BVNot(BV("010")), BV("101")), is_valid=True, is_sat=True, logic=pysmt.logics.QF_BV), Example(hr="((! bv3) = 5_3)", expr=Equals(BVNot(bv3), BV("101")), is_valid=False, is_sat=True, logic=pysmt.logics.QF_BV), Example(hr="((7_3 xor 0_3) = 0_3)", expr=Equals(BVXor(BV("111"), BV("000")), BV("000")), is_valid=False, is_sat=False, logic=pysmt.logics.QF_BV), Example(hr="((7_3 xor bv3) = (6_3 xor bv3))", expr=Equals(BVXor(BV("111"), bv3), BVXor(BV("110"), bv3)), is_valid=False, is_sat=False, logic=pysmt.logics.QF_BV), Example(hr="((bv8::bv8) u< 0_16)", expr=BVULT(BVConcat(bv8, bv8), BVZero(16)), is_valid=False, is_sat=False, logic=pysmt.logics.QF_BV), Example(hr="((bv8::bv8) u< (bv8::9_8))", expr=BVULT(BVConcat(bv8, bv8), BVConcat(bv8, BV(9, 8))), is_valid=False, is_sat=True, logic=pysmt.logics.QF_BV), Example(hr="(1_32[0:7] = 1_8)", expr=Equals(BVExtract(BVOne(32), end=7), BVOne(8)), is_valid=True, is_sat=True, logic=pysmt.logics.QF_BV), Example(hr="(0_8 u< (((bv8 + 1_8) * 5_8) u/ 5_8))", expr=BVUGT( BVUDiv(BVMul(BVAdd(bv8, BVOne(8)), BV(5, width=8)), BV(5, width=8)), BVZero(8)), is_valid=False, is_sat=True, logic=pysmt.logics.QF_BV), Example(hr="(0_16 u<= bv16)", expr=BVUGE(bv16, BVZero(16)), is_valid=True, is_sat=True, logic=pysmt.logics.QF_BV), Example(hr="(0_16 s<= bv16)", expr=BVSGE(bv16, BVZero(16)), is_valid=False, is_sat=True, logic=pysmt.logics.QF_BV), Example( hr="((0_32 u< (5_32 u% 2_32)) & ((5_32 u% 2_32) u<= 1_32))", expr=And( BVUGT(BVURem(BV(5, width=32), BV(2, width=32)), BVZero(32)), BVULE(BVURem(BV(5, width=32), BV(2, width=32)), BVOne(32))), is_valid=True, is_sat=True, logic=pysmt.logics.QF_BV), Example(hr="((((1_32 + (- 1_32)) << 1_32) >> 1_32) = 1_32)", expr=Equals( BVLShr(BVLShl(BVAdd(BVOne(32), BVNeg(BVOne(32))), 1), 1), BVOne(32)), is_valid=False, is_sat=False, logic=pysmt.logics.QF_BV), Example(hr="((1_32 - 1_32) = 0_32)", expr=Equals(BVSub(BVOne(32), BVOne(32)), BVZero(32)), is_valid=True, is_sat=True, logic=pysmt.logics.QF_BV), # Rotations Example(hr="(((1_32 ROL 1) ROR 1) = 1_32)", expr=Equals(BVRor(BVRol(BVOne(32), 1), 1), BVOne(32)), is_valid=True, is_sat=True, logic=pysmt.logics.QF_BV), Example(hr="((bv16 ROL 1) = (bv16 ROR 2))", expr=Equals(BVRol(bv16, 1), BVRor(bv16, 2)), is_valid=False, is_sat=True, logic=pysmt.logics.QF_BV), # Extensions Example(hr="((0_5 ZEXT 11) = (0_1 SEXT 15))", expr=Equals(BVZExt(BVZero(5), 11), BVSExt(BVZero(1), 15)), is_valid=True, is_sat=True, logic=pysmt.logics.QF_BV), Example(hr="((bv8 ZEXT 19) = (bv16 SEXT 11))", expr=Equals(BVZExt(bv8, 19), BVSExt(bv16, 11)), is_valid=False, is_sat=True, logic=pysmt.logics.QF_BV), Example(hr="((bv16 - bv16) = 0_16)", expr=Equals(BVSub(bv16, bv16), BVZero(16)), is_valid=True, is_sat=True, logic=pysmt.logics.QF_BV), Example(hr="((bv16 - bv16)[0:7] = bv8)", expr=Equals(BVExtract(BVSub(bv16, bv16), 0, 7), bv8), is_valid=False, is_sat=True, logic=pysmt.logics.QF_BV), Example(hr="((bv16[0:7] bvcomp bv8) = 1_1)", expr=Equals(BVComp(BVExtract(bv16, 0, 7), bv8), BVOne(1)), is_valid=False, is_sat=True, logic=pysmt.logics.QF_BV), Example(hr="((bv16 bvcomp bv16) = 0_1)", expr=Equals(BVComp(bv16, bv16), BVZero(1)), is_valid=False, is_sat=False, logic=pysmt.logics.QF_BV), Example(hr="(bv16 s< bv16)", expr=BVSLT(bv16, bv16), is_valid=False, is_sat=False, logic=pysmt.logics.QF_BV), Example(hr="(bv16 s< 0_16)", expr=BVSLT(bv16, BVZero(16)), is_valid=False, is_sat=True, logic=pysmt.logics.QF_BV), Example(hr="((bv16 s< 0_16) | (0_16 s<= bv16))", expr=Or(BVSGT(BVZero(16), bv16), BVSGE(bv16, BVZero(16))), is_valid=True, is_sat=True, logic=pysmt.logics.QF_BV), Example(hr="(bv16 u< bv16)", expr=BVULT(bv16, bv16), is_valid=False, is_sat=False, logic=pysmt.logics.QF_BV), Example(hr="(bv16 u< 0_16)", expr=BVULT(bv16, BVZero(16)), is_valid=False, is_sat=False, logic=pysmt.logics.QF_BV), Example(hr="((bv16 | 0_16) = bv16)", expr=Equals(BVOr(bv16, BVZero(16)), bv16), is_valid=True, is_sat=True, logic=pysmt.logics.QF_BV), Example(hr="((bv16 | 5_16) = bv16)", expr=Equals(BVOr(bv16, BV(5, 16)), bv16), is_valid=False, is_sat=True, logic=pysmt.logics.QF_BV), Example(hr="((bv16 & 0_16) = 0_16)", expr=Equals(BVAnd(bv16, BVZero(16)), BVZero(16)), is_valid=True, is_sat=True, logic=pysmt.logics.QF_BV), Example(hr="((bv16 & 7_16) = 0_16)", expr=Equals(BVAnd(bv16, BV(7, 16)), BVZero(16)), is_valid=False, is_sat=True, logic=pysmt.logics.QF_BV), Example(hr="((0_16 s< bv16) & ((bv16 s/ 65535_16) s< 0_16))", expr=And(BVSLT(BVZero(16), bv16), BVSLT(BVSDiv(bv16, SBV(-1, 16)), BVZero(16))), is_valid=False, is_sat=True, logic=pysmt.logics.QF_BV), Example(hr="((0_16 s< bv16) & ((bv16 s% 1_16) s< 0_16))", expr=And(BVSLT(BVZero(16), bv16), BVSLT(BVSRem(bv16, BVOne(16)), BVZero(16))), is_valid=False, is_sat=False, logic=pysmt.logics.QF_BV), Example(hr="((bv16 u% 1_16) = 0_16)", expr=Equals(BVURem(bv16, BVOne(16)), BVZero(16)), is_valid=True, is_sat=True, logic=pysmt.logics.QF_BV), Example(hr="((bv16 u% bv16) = 0_16)", expr=Equals(BVURem(bv16, bv16), BVZero(16)), is_valid=True, is_sat=True, logic=pysmt.logics.QF_BV), Example(hr="((bv16 s% 1_16) = 0_16)", expr=Equals(BVSRem(bv16, BVOne(16)), BVZero(16)), is_valid=True, is_sat=True, logic=pysmt.logics.QF_BV), Example(hr="((bv16 s% bv16) = 0_16)", expr=Equals(BVSRem(bv16, bv16), BVZero(16)), is_valid=True, is_sat=True, logic=pysmt.logics.QF_BV), Example(hr="((bv16 s% (- 1_16)) = 0_16)", expr=Equals(BVSRem(bv16, BVNeg(BVOne(16))), BVZero(16)), is_valid=True, is_sat=True, logic=pysmt.logics.QF_BV), Example(hr="(bv16 s< (- bv16))", expr=BVSGT(BVNeg(bv16), bv16), is_valid=False, is_sat=True, logic=pysmt.logics.QF_BV), Example(hr="((bv16 a>> 0_16) = bv16)", expr=Equals(BVAShr(bv16, BVZero(16)), bv16), is_valid=True, is_sat=True, logic=pysmt.logics.QF_BV), Example( hr="((0_16 s<= bv16) & ((bv16 a>> 1_16) = (bv16 >> 1_16)))", expr=And( BVSLE(BVZero(16), bv16), Equals(BVAShr(bv16, BVOne(16)), BVLShr(bv16, BVOne(16)))), is_valid=False, is_sat=True, logic=pysmt.logics.QF_BV), # # Quantification # Example(hr="(forall y . (x -> y))", expr=ForAll([y], Implies(x, y)), is_valid=False, is_sat=True, logic=pysmt.logics.BOOL), Example(hr="(forall p, q . ((p + q) = 0))", expr=ForAll([p, q], Equals(Plus(p, q), Int(0))), is_valid=False, is_sat=False, logic=pysmt.logics.LIA), Example( hr="(forall r, s . (((0.0 < r) & (0.0 < s)) -> ((r - s) < r)))", expr=ForAll([r, s], Implies(And(GT(r, Real(0)), GT(s, Real(0))), (LT(Minus(r, s), r)))), is_valid=True, is_sat=True, logic=pysmt.logics.LRA), Example(hr="(exists x, y . (x -> y))", expr=Exists([x, y], Implies(x, y)), is_valid=True, is_sat=True, logic=pysmt.logics.BOOL), Example(hr="(exists p, q . ((p + q) = 0))", expr=Exists([p, q], Equals(Plus(p, q), Int(0))), is_valid=True, is_sat=True, logic=pysmt.logics.LIA), Example(hr="(exists r . (forall s . (r < (r - s))))", expr=Exists([r], ForAll([s], GT(Minus(r, s), r))), is_valid=False, is_sat=False, logic=pysmt.logics.LRA), Example(hr="(forall r . (exists s . (r < (r - s))))", expr=ForAll([r], Exists([s], GT(Minus(r, s), r))), is_valid=True, is_sat=True, logic=pysmt.logics.LRA), Example(hr="(x & (forall r . ((r + s) = 5.0)))", expr=And(x, ForAll([r], Equals(Plus(r, s), Real(5)))), is_valid=False, is_sat=False, logic=pysmt.logics.LRA), Example(hr="(exists x . ((x <-> (5.0 < s)) & (s < 3.0)))", expr=Exists([x], (And(Iff(x, GT(s, Real(5))), LT(s, Real(3))))), is_valid=False, is_sat=True, logic=pysmt.logics.LRA), # # UFLIRA # Example(hr="((p < ih(r, q)) & (x -> y))", expr=And(GT(Function(ih, (r, q)), p), Implies(x, y)), is_valid=False, is_sat=True, logic=pysmt.logics.QF_UFLIRA), Example( hr= "(((p - 3) = q) -> ((p < ih(r, (q + 3))) | (ih(r, p) <= p)))", expr=Implies( Equals(Minus(p, Int(3)), q), Or(GT(Function(ih, (r, Plus(q, Int(3)))), p), LE(Function(ih, (r, p)), p))), is_valid=True, is_sat=True, logic=pysmt.logics.QF_UFLIRA), Example( hr= "(((ToReal((p - 3)) = r) & (ToReal(q) = r)) -> ((p < ih(ToReal((p - 3)), (q + 3))) | (ih(r, p) <= p)))", expr=Implies( And(Equals(ToReal(Minus(p, Int(3))), r), Equals(ToReal(q), r)), Or( GT( Function( ih, (ToReal(Minus(p, Int(3))), Plus(q, Int(3)))), p), LE(Function(ih, (r, p)), p))), is_valid=True, is_sat=True, logic=pysmt.logics.QF_UFLIRA), Example( hr= "(! (((ToReal((p - 3)) = r) & (ToReal(q) = r)) -> ((p < ih(ToReal((p - 3)), (q + 3))) | (ih(r, p) <= p))))", expr=Not( Implies( And(Equals(ToReal(Minus(p, Int(3))), r), Equals(ToReal(q), r)), Or( GT( Function(ih, (ToReal(Minus( p, Int(3))), Plus(q, Int(3)))), p), LE(Function(ih, (r, p)), p)))), is_valid=False, is_sat=False, logic=pysmt.logics.QF_UFLIRA), Example( hr= """("Did you know that any string works? #yolo" & "10" & "|#somesolverskeepthe||" & " ")""", expr=And(Symbol("Did you know that any string works? #yolo"), Symbol("10"), Symbol("|#somesolverskeepthe||"), Symbol(" ")), is_valid=False, is_sat=True, logic=pysmt.logics.QF_BOOL), # # Arrays # Example(hr="((q = 0) -> (aii[0 := 0] = aii[0 := q]))", expr=Implies( Equals(q, Int(0)), Equals(Store(aii, Int(0), Int(0)), Store(aii, Int(0), q))), is_valid=True, is_sat=True, logic=pysmt.logics.QF_ALIA), Example(hr="(aii[0 := 0][0] = 0)", expr=Equals(Select(Store(aii, Int(0), Int(0)), Int(0)), Int(0)), is_valid=True, is_sat=True, logic=pysmt.logics.QF_ALIA), Example(hr="((Array{Int, Int}(0)[1 := 1] = aii) & (aii[1] = 0))", expr=And(Equals(Array(INT, Int(0), {Int(1): Int(1)}), aii), Equals(Select(aii, Int(1)), Int(0))), is_valid=False, is_sat=False, logic=pysmt.logics.get_logic_by_name("QF_ALIA*")), Example(hr="((Array{Int, Int}(0)[1 := 3] = aii) & (aii[1] = 3))", expr=And(Equals(Array(INT, Int(0), {Int(1): Int(3)}), aii), Equals(Select(aii, Int(1)), Int(3))), is_valid=False, is_sat=True, logic=pysmt.logics.get_logic_by_name("QF_ALIA*")), Example(hr="((Array{Real, Int}(10) = ari) & (ari[6/5] = 0))", expr=And(Equals(Array(REAL, Int(10)), ari), Equals(Select(ari, Real((6, 5))), Int(0))), is_valid=False, is_sat=False, logic=pysmt.logics.get_logic_by_name("QF_AUFBVLIRA*")), Example( hr= "((Array{Real, Int}(0)[1.0 := 10][2.0 := 20][3.0 := 30][4.0 := 40] = ari) & (! ((ari[0.0] = 0) & (ari[1.0] = 10) & (ari[2.0] = 20) & (ari[3.0] = 30) & (ari[4.0] = 40))))", expr=And( Equals( Array( REAL, Int(0), { Real(1): Int(10), Real(2): Int(20), Real(3): Int(30), Real(4): Int(40) }), ari), Not( And(Equals(Select(ari, Real(0)), Int(0)), Equals(Select(ari, Real(1)), Int(10)), Equals(Select(ari, Real(2)), Int(20)), Equals(Select(ari, Real(3)), Int(30)), Equals(Select(ari, Real(4)), Int(40))))), is_valid=False, is_sat=False, logic=pysmt.logics.get_logic_by_name("QF_AUFBVLIRA*")), Example( hr= "((Array{Real, Int}(0)[1.0 := 10][2.0 := 20][3.0 := 30][4.0 := 40][5.0 := 50] = ari) & (! ((ari[0.0] = 0) & (ari[1.0] = 10) & (ari[2.0] = 20) & (ari[3.0] = 30) & (ari[4.0] = 40) & (ari[5.0] = 50))))", expr=And( Equals( Array( REAL, Int(0), { Real(1): Int(10), Real(2): Int(20), Real(3): Int(30), Real(4): Int(40), Real(5): Int(50) }), ari), Not( And(Equals(Select(ari, Real(0)), Int(0)), Equals(Select(ari, Real(1)), Int(10)), Equals(Select(ari, Real(2)), Int(20)), Equals(Select(ari, Real(3)), Int(30)), Equals(Select(ari, Real(4)), Int(40)), Equals(Select(ari, Real(5)), Int(50))))), is_valid=False, is_sat=False, logic=pysmt.logics.get_logic_by_name("QF_AUFBVLIRA*")), Example( hr= "((a_arb_aii = Array{Array{Real, BV{8}}, Array{Int, Int}}(Array{Int, Int}(7))) -> (a_arb_aii[arb][42] = 7))", expr=Implies( Equals(nested_a, Array(ArrayType(REAL, BV8), Array(INT, Int(7)))), Equals(Select(Select(nested_a, arb), Int(42)), Int(7))), is_valid=True, is_sat=True, logic=pysmt.logics.get_logic_by_name("QF_AUFBVLIRA*")), Example(hr="(abb[bv8 := y_][bv8 := z_] = abb[bv8 := z_])", expr=Equals( Store(Store(abb, bv8, Symbol("y_", BV8)), bv8, Symbol("z_", BV8)), Store(abb, bv8, Symbol("z_", BV8))), is_valid=True, is_sat=True, logic=pysmt.logics.QF_ABV), Example(hr="((r / s) = (r * s))", expr=Equals(Div(r, s), Times(r, s)), is_valid=False, is_sat=True, logic=pysmt.logics.QF_NRA), Example(hr="(2.0 = (r * r))", expr=Equals(Real(2), Times(r, r)), is_valid=False, is_sat=True, logic=pysmt.logics.QF_NRA), Example(hr="((p ^ 2) = 0)", expr=Equals(Pow(p, Int(2)), Int(0)), is_valid=False, is_sat=True, logic=pysmt.logics.QF_NIA), Example(hr="((r ^ 2.0) = 0.0)", expr=Equals(Pow(r, Real(2)), Real(0)), is_valid=False, is_sat=True, logic=pysmt.logics.QF_NRA), Example(hr="((r * r * r) = 25.0)", expr=Equals(Times(r, r, r), Real(25)), is_valid=False, is_sat=True, logic=pysmt.logics.QF_NRA), Example(hr="((5.0 * r * 5.0) = 25.0)", expr=Equals(Times(Real(5), r, Real(5)), Real(25)), is_valid=False, is_sat=True, logic=pysmt.logics.QF_LRA), Example(hr="((p * p * p) = 25)", expr=Equals(Times(p, p, p), Int(25)), is_valid=False, is_sat=False, logic=pysmt.logics.QF_NIA), Example(hr="((5 * p * 5) = 25)", expr=Equals(Times(Int(5), p, Int(5)), Int(25)), is_valid=False, is_sat=True, logic=pysmt.logics.QF_LIA), Example(hr="(((1 - 1) * p * 1) = 0)", expr=Equals(Times(Minus(Int(1), Int(1)), p, Int(1)), Int(0)), is_valid=True, is_sat=True, logic=pysmt.logics.QF_LIA), # Huge Fractions: Example( hr= "((r * 1606938044258990275541962092341162602522202993782792835301376/7) = -20480000000000000000000000.0)", expr=Equals(Times(r, Real(Fraction(2**200, 7))), Real(-200**11)), is_valid=False, is_sat=True, logic=pysmt.logics.QF_LRA), Example(hr="(((r + 5.0 + s) * (s + 2.0 + r)) = 0.0)", expr=Equals( Times(Plus(r, Real(5), s), Plus(s, Real(2), r)), Real(0)), is_valid=False, is_sat=True, logic=pysmt.logics.QF_NRA), Example( hr= "(((p + 5 + q) * (p - (q - 5))) = ((p * p) + (10 * p) + 25 + (-1 * q * q)))", expr=Equals( Times(Plus(p, Int(5), q), Minus(p, Minus(q, Int(5)))), Plus(Times(p, p), Times(Int(10), p), Int(25), Times(Int(-1), q, q))), is_valid=True, is_sat=True, logic=pysmt.logics.QF_NIA), ] return result
def test_bv_sle_symbols(self): x, y = (Symbol(name, BVType(32)) for name in "xy") f = BVSLE(x, y) self.check_equal_and_valid(f, BVSLE(x, y))
def test_bv(self): mgr = get_env().formula_manager BV = mgr.BV # Constants one = BV(1, 32) zero = BV(0, 32) big = BV(127, 128) binary = BV("111") binary2 = BV("#b111") binary3 = BV(0b111, 3) # In this case we need to explicit the width self.assertEqual(binary, binary2) self.assertEqual(binary2, binary3) self.assertEqual(one, mgr.BVOne(32)) self.assertEqual(zero, mgr.BVZero(32)) # Type Equality self.assertTrue(BV32 != BV128) self.assertFalse(BV32 != BV32) self.assertFalse(BV32 == BV128) self.assertTrue(BV32 == BV32) with self.assertRaises(ValueError): # Negative numbers are not supported BV(-1, 10) with self.assertRaises(ValueError): # Number should fit in the width BV(10, 2) # Variables b128 = Symbol("b", BV128) # BV1, BV8 etc. are defined in pysmt.typing b32 = Symbol("b32", BV32) hexample = BV(0x10, 32) #s_one = BV(-1, 32) bcustom = Symbol("bc", BVType(42)) self.assertIsNotNone(hexample) self.assertIsNotNone(bcustom) #self.assertIsNotNone(s_one) self.assertEqual(bcustom.bv_width(), 42) self.assertEqual(hexample.constant_value(), 16) #self.assertEqual(str(s_one), "-1_32") not_zero32 = mgr.BVNot(zero) not_b128 = mgr.BVNot(b128) f1 = Equals(not_zero32, b32) f2 = Equals(not_b128, big) #print(f1) #print(f2) self.assertTrue(is_sat(f1, logic=QF_BV)) self.assertTrue(is_sat(f2, logic=QF_BV)) zero_and_one = mgr.BVAnd(zero, one) zero_or_one = mgr.BVOr(zero, one) zero_xor_one = mgr.BVXor(zero, one) zero_xor_one.simplify() self.assertTrue(zero_xor_one.is_bv_op()) # print(zero_and_one) # print(zero_or_one) # print(zero_xor_one) f1 = Equals(zero_and_one, b32) f2 = Equals(zero_or_one, b32) f3 = Equals(zero_xor_one, b32) f4 = Equals(zero_xor_one, one) self.assertTrue(is_sat(f1, logic=QF_BV), f1) self.assertTrue(is_sat(f2, logic=QF_BV), f2) self.assertTrue(is_sat(f3, logic=QF_BV), f3) self.assertTrue(is_valid(f4, logic=QF_BV), f4) with self.assertRaises(TypeError): mgr.BVAnd(b128, zero) f = mgr.BVAnd(b32, zero) f = mgr.BVOr(f, b32) f = mgr.BVXor(f, b32) f = Equals(f, zero) self.assertTrue(is_sat(f, logic=QF_BV), f) zero_one_64 = mgr.BVConcat(zero, one) one_zero_64 = mgr.BVConcat(one, zero) one_one_64 = mgr.BVConcat(one, one) self.assertTrue(zero_one_64.bv_width() == 64) f1 = Equals(mgr.BVXor(one_zero_64, zero_one_64), one_one_64) self.assertTrue(is_sat(f1, logic=QF_BV), f1) # MG: BV indexes grow to the left. # This is confusing and we should address this. extraction = mgr.BVExtract(zero_one_64, 32, 63) self.assertTrue(is_valid(Equals(extraction, zero))) #print(extraction) ult = mgr.BVULT(zero, one) neg = mgr.BVNeg(one) self.assertTrue(is_valid(ult, logic=QF_BV), ult) test_eq = Equals(neg, one) self.assertTrue(is_unsat(test_eq, logic=QF_BV)) # print(ult) # print(neg) f = zero addition = mgr.BVAdd(f, one) multiplication = mgr.BVMul(f, one) udiv = mgr.BVUDiv(f, one) self.assertTrue(is_valid(Equals(addition, one), logic=QF_BV), addition) self.assertTrue(is_valid(Equals(multiplication, zero), logic=QF_BV), multiplication) self.assertTrue(is_valid(Equals(udiv, zero), logic=QF_BV), udiv) # print(addition) # print(multiplication) # print(udiv) three = mgr.BV(3, 32) two = mgr.BV(2, 32) reminder = mgr.BVURem(three, two) shift_l_a = mgr.BVLShl(one, one) shift_l_b = mgr.BVLShl(one, 1) self.assertTrue(is_valid(Equals(reminder, one)), reminder) self.assertEqual(shift_l_a, shift_l_b) self.assertTrue(is_valid(Equals(shift_l_a, two))) # print(reminder) # print(shift_l_a) # print(shift_l_b) shift_r_a = mgr.BVLShr(one, one) shift_r_b = mgr.BVLShr(one, 1) self.assertEqual(shift_r_a, shift_r_b) self.assertTrue(is_valid(Equals(shift_r_a, zero))) rotate_l = mgr.BVRol(one, 3) rotate_r = mgr.BVRor(rotate_l, 3) self.assertTrue(is_valid(Equals(one, rotate_r))) # print(rotate_l) # print(rotate_r) zero_ext = mgr.BVZExt(one, 64) signed_ext = mgr.BVSExt(one, 64) signed_ext2 = mgr.BVSExt(mgr.BVNeg(one), 64) self.assertNotEqual(signed_ext2, signed_ext) self.assertTrue(is_valid(Equals(zero_ext, signed_ext), logic=QF_BV)) # print(zero_ext) # print(signed_ext) x = Symbol("x") g = And(x, mgr.BVULT(zero, one)) res = is_sat(g, logic=QF_BV) self.assertTrue(res) model = get_model(g, logic=QF_BV) self.assertTrue(model[x] == TRUE()) gt_1 = mgr.BVUGT(zero, one) gt_2 = mgr.BVULT(one, zero) self.assertEqual(gt_1, gt_2) gte_1 = mgr.BVULE(zero, one) gte_2 = mgr.BVUGE(one, zero) self.assertEqual(gte_1, gte_2) self.assertTrue(is_valid(gte_2, logic=QF_BV)) ide = Equals(mgr.BVNeg(BV(10, 32)), mgr.SBV(-10, 32)) self.assertValid(ide, logic=QF_BV) # These should work without exceptions mgr.SBV(-2, 2) mgr.SBV(-1, 2) mgr.SBV(0, 2) mgr.SBV(1, 2) # Overflow and Underflow with self.assertRaises(ValueError): mgr.SBV(2, 2) with self.assertRaises(ValueError): mgr.SBV(-3, 2) # These should work without exceptions mgr.BV(0, 2) mgr.BV(1, 2) mgr.BV(2, 2) mgr.BV(3, 2) # Overflow with self.assertRaises(ValueError): mgr.BV(4, 2) # No negative number allowed with self.assertRaises(ValueError): mgr.BV(-1, 2) # SBV should behave as BV for positive numbers self.assertEqual(mgr.SBV(10, 16), mgr.BV(10, 16)) return
def get_sts(self, params): if len(params) != len(self.interface.split()): Logger.error("Invalid parameters for clock behavior \"%s\"" % (self.name)) clk = params[0] cyclestr = params[1] try: cycle = int(cyclestr) except: Logger.error( "Clock cycle should be an integer number instead of \"%s\"" % cyclestr) if (not type(clk) == FNode) or (not clk.is_symbol()): Logger.error("Clock symbol \"%s\" not found" % (str(clk))) init = [] invar = [] trans = [] vars = set([]) if clk.symbol_type().is_bv_type(): pos_clk = EqualsOrIff(clk, BV(1, 1)) neg_clk = EqualsOrIff(clk, BV(0, 1)) else: pos_clk = clk neg_clk = Not(clk) if cycle < 1: Logger.error( "Deterministic clock requires at least a cycle of size 1") if cycle == 1: init.append(neg_clk) trans.append(Iff(neg_clk, TS.to_next(pos_clk))) if cycle > 1: statesize = math.ceil(math.log(cycle) / math.log(2)) counter = Symbol("%s%s" % (clk.symbol_name(), CLOCK_COUNTER), BVType(statesize)) # 0 counts cycle -= 1 # counter = 0 & clk = 0 init.append(EqualsOrIff(counter, BV(0, statesize))) init.append(neg_clk) # counter <= cycle invar.append(BVULE(counter, BV(cycle, statesize))) # (counter < cycle) -> next(counter) = counter + 1 trans.append( Implies( BVULT(counter, BV(cycle, statesize)), EqualsOrIff(TS.to_next(counter), BVAdd(counter, BV(1, statesize))))) # (counter >= cycle) -> next(counter) = 0 trans.append( Implies(BVUGE(counter, BV(cycle, statesize)), EqualsOrIff(TS.to_next(counter), BV(0, statesize)))) # (!clk) & (counter < cycle) -> next(!clk) trans.append( Implies(And(neg_clk, BVULT(counter, BV(cycle, statesize))), TS.to_next(neg_clk))) # (!clk) & (counter >= cycle) -> next(clk) trans.append( Implies(And(neg_clk, BVUGE(counter, BV(cycle, statesize))), TS.to_next(pos_clk))) # (clk) & (counter < cycle) -> next(clk) trans.append( Implies(And(pos_clk, BVULT(counter, BV(cycle, statesize))), TS.to_next(pos_clk))) # (clk) & (counter >= cycle) -> next(!clk) trans.append( Implies(And(pos_clk, BVUGE(counter, BV(cycle, statesize))), TS.to_next(neg_clk))) vars.add(counter) ts = TS("Clock Behavior") ts.vars, ts.init, ts.invar, ts.trans = vars, And(init), And( invar), And(trans) Logger.log( "Adding clock behavior \"%s(%s)\"" % (self.name, ", ".join([str(p) for p in params])), 1) return ts
def test_infix_extended(self): p, r, x, y = self.p, self.r, self.x, self.y get_env().enable_infix_notation = True self.assertEqual(Plus(p, Int(1)), p + 1) self.assertEqual(Plus(r, Real(1)), r + 1) self.assertEqual(Times(r, Real(1)), r * 1) self.assertEqual(Minus(p, Int(1)), p - 1) self.assertEqual(Minus(r, Real(1)), r - 1) self.assertEqual(Times(r, Real(1)), r * 1) self.assertEqual(Plus(r, Real(1.5)), r + 1.5) self.assertEqual(Minus(r, Real(1.5)), r - 1.5) self.assertEqual(Times(r, Real(1.5)), r * 1.5) self.assertEqual(Plus(r, Real(1.5)), 1.5 + r) self.assertEqual(Times(r, Real(1.5)), 1.5 * r) with self.assertRaises(PysmtTypeError): foo = p + 1.5 self.assertEqual(Not(x), ~x) self.assertEqual(Times(r, Real(-1)), -r) self.assertEqual(Times(p, Int(-1)), -p) self.assertEqual(Xor(x, y), x ^ y) self.assertEqual(And(x, y), x & y) self.assertEqual(Or(x, y), x | y) self.assertEqual(Or(x, TRUE()), x | True) self.assertEqual(Or(x, TRUE()), True | x) self.assertEqual(And(x, TRUE()), x & True) self.assertEqual(And(x, TRUE()), True & x) self.assertEqual(Iff(x, y), x.Iff(y)) self.assertEqual(And(x, y), x.And(y)) self.assertEqual(Or(x, y), x.Or(y)) self.assertEqual(Ite(x, TRUE(), FALSE()), x.Ite(TRUE(), FALSE())) with self.assertRaises(Exception): x.Ite(1, 2) self.assertEqual(6 - r, Plus(Times(r, Real(-1)), Real(6))) # BVs # BV_CONSTANT: We use directly python numbers # # Note: In this case, the width is implicit (yikes!) The # width of the constant is inferred by the use of a # symbol or operator that enforces a bit_width. # # This works in very simple cases. For complex # expressions it is advisable to create a shortcut for # the given BVType. const1 = 3 const2 = 0b011 const3 = 0x3 # Since these are python numbers, the following holds self.assertEqual(const1, const2) self.assertEqual(const1, const3) # However, we cannot use infix pySMT Equals, since const1 is a # python int!!! with self.assertRaises(AttributeError): const1.Equals(const2) # We use the usual syntax to build a constant with a fixed width const1_8 = self.mgr.BV(3, width=8) # In actual code, one can simply create a macro for this: _8bv = lambda v: self.mgr.BV(v, width=8) const1_8b = _8bv(3) self.assertEqual(const1_8, const1_8b) # Equals forces constants to have the width of the operand self.assertEqual(const1_8.Equals(const1), self.mgr.Equals(const1_8, const1_8b)) # Symbols bv8 = self.mgr.FreshSymbol(BV8) bv7 = self.mgr.FreshSymbol(BVType(7)) self.assertEqual(bv8.Equals(const1), bv8.Equals(const1_8)) # BV_AND, self.assertEqual(bv8 & const1, self.mgr.BVAnd(bv8, const1_8)) self.assertEqual(const1 & bv8, self.mgr.BVAnd(bv8, const1_8)) self.assertEqual(const1 & bv8, self.mgr.BVAnd(bv8, const1_8)) # BV_XOR, self.assertEqual(bv8 ^ const1, self.mgr.BVXor(bv8, const1_8)) self.assertEqual(const1 ^ bv8, self.mgr.BVXor(bv8, const1_8)) # BV_OR, self.assertEqual(bv8 | const1, self.mgr.BVOr(bv8, const1_8)) self.assertEqual(const1 | bv8, self.mgr.BVOr(bv8, const1_8)) # BV_ADD, self.assertEqual(bv8 + const1, self.mgr.BVAdd(bv8, const1_8)) self.assertEqual(const1 + bv8, self.mgr.BVAdd(bv8, const1_8)) # BV_SUB, self.assertEqual(bv8 - const1, self.mgr.BVSub(bv8, const1_8)) self.assertEqual(const1 - bv8, self.mgr.BVSub(const1_8, bv8)) # BV_MUL, self.assertEqual(bv8 * const1, self.mgr.BVMul(bv8, const1_8)) self.assertEqual(const1 * bv8, self.mgr.BVMul(bv8, const1_8)) # BV_NOT: # !!!WARNING!!! Cannot be applied to python constants!! # This results in a negative number with self.assertRaises(PysmtValueError): _8bv(~const1) # For symbols and expressions this works as expected self.assertEqual(~bv8, self.mgr.BVNot(bv8)) # BV_NEG -- Cannot be applied to 'infix' constants self.assertEqual(-bv8, self.mgr.BVNeg(bv8)) # BV_EXTRACT -- Cannot be applied to 'infix' constants self.assertEqual(bv8[0:7], self.mgr.BVExtract(bv8, 0, 7)) self.assertEqual(bv8[:7], self.mgr.BVExtract(bv8, end=7)) self.assertEqual(bv8[0:], self.mgr.BVExtract(bv8, start=0)) self.assertEqual(bv8[7], self.mgr.BVExtract(bv8, start=7, end=7)) # BV_ULT, self.assertEqual(bv8 < const1, self.mgr.BVULT(bv8, const1_8)) # BV_ULE, self.assertEqual(bv8 <= const1, self.mgr.BVULE(bv8, const1_8)) # BV_UGT self.assertEqual(bv8 > const1, self.mgr.BVUGT(bv8, const1_8)) # BV_UGE self.assertEqual(bv8 >= const1, self.mgr.BVUGE(bv8, const1_8)) # BV_LSHL, self.assertEqual(bv8 << const1, self.mgr.BVLShl(bv8, const1_8)) # BV_LSHR, self.assertEqual(bv8 >> const1, self.mgr.BVLShr(bv8, const1_8)) # BV_UDIV, self.assertEqual(bv8 / const1, self.mgr.BVUDiv(bv8, const1_8)) # BV_UREM, self.assertEqual(bv8 % const1, self.mgr.BVURem(bv8, const1_8)) # The following operators use the infix syntax left.Operator.right # These includes all signed operators # BVSLT, self.assertEqual(self.mgr.BVSLT(bv8, const1_8), bv8.BVSLT(const1_8)) #BVSLE, self.assertEqual(self.mgr.BVSLE(bv8, const1_8), bv8.BVSLE(const1_8)) #BVComp self.assertEqual(self.mgr.BVComp(bv8, const1_8), bv8.BVComp(const1_8)) #BVSDiv self.assertEqual(self.mgr.BVSDiv(bv8, const1_8), bv8.BVSDiv(const1_8)) #BVSRem self.assertEqual(self.mgr.BVSRem(bv8, const1_8), bv8.BVSRem(const1_8)) #BVAShr self.assertEqual(self.mgr.BVAShr(bv8, const1_8), bv8.BVAShr(const1_8)) #BVNand self.assertEqual(self.mgr.BVNand(bv8, const1_8), bv8.BVNand(const1_8)) #BVNor self.assertEqual(self.mgr.BVNor(bv8, const1_8), bv8.BVNor(const1_8)) #BVXnor self.assertEqual(self.mgr.BVXnor(bv8, const1_8), bv8.BVXnor(const1_8)) #BVSGT self.assertEqual(self.mgr.BVSGT(bv8, const1_8), bv8.BVSGT(const1_8)) #BVSGE self.assertEqual(self.mgr.BVSGE(bv8, const1_8), bv8.BVSGE(const1_8)) #BVSMod self.assertEqual(self.mgr.BVSMod(bv8, const1_8), bv8.BVSMod(const1_8)) #BVRol, self.assertEqual(self.mgr.BVRol(bv8, steps=5), bv8.BVRol(5)) #BVRor, self.assertEqual(self.mgr.BVRor(bv8, steps=5), bv8.BVRor(5)) #BVZExt, self.assertEqual(self.mgr.BVZExt(bv8, increase=4), bv8.BVZExt(4)) #BVSExt, self.assertEqual(self.mgr.BVSExt(bv8, increase=4), bv8.BVSExt(4)) #BVRepeat, self.assertEqual(self.mgr.BVRepeat(bv8, count=5), bv8.BVRepeat(5))
def Reg(in_, clk, clr, rst, arst, out, initval, clk_posedge, arst_posedge): # INIT: out = initval # do_arst = Ite(arst_posedge, (!arst & arst'), (arst & !arst')) # do_clk = Ite(clk_posedge, (!clk & clk'), (clk & !clk')) # inr = Ite(clr, 0, Ite(rst, initval, in)) # if Modules.functional # TRANS: out' = Ite(do_clk, Ite(clr, 0, Ite(rst, initval, in)), Ite(rst, initval, in)) # INVAR: True # trans gives priority to clr signal over rst # else # act_trans = (out' = inr) # pas_trans = (out' = out) # TRANS: (!do_arst -> ((do_clk -> act_trans) & (!do_clk -> pas_trans))) & (do_arst -> (out' = initval)) # INVAR: True # trans gives priority to clr signal over rst vars_ = [in_, clk, clr, rst, arst, out] comment = "Reg (in, clk, clr, rst, arst, out) = (%s, %s, %s, %s, %s, %s)" % ( tuple([str(x) for x in vars_])) Logger.log(comment, 3) init = TRUE() trans = TRUE() invar = TRUE() initvar = None basename = SEP.join(out.symbol_name().split( SEP)[:-1]) if SEP in out.symbol_name() else out.symbol_name() initname = basename + SEP + INIT if initval is not None and not Modules.symbolic_init: if out.symbol_type() == BOOL: binitval = FALSE() if initval == 0 else TRUE() else: binitval = BV(initval, out.symbol_type().width) initvar = binitval else: if out.symbol_type() == BOOL: initvar = Symbol(initname, BOOL) else: initvar = Symbol(initname, BVType(out.symbol_type().width)) trans = And(trans, EqualsOrIff(initvar, TS.get_prime(initvar))) vars_.append(initvar) init = And(init, EqualsOrIff(out, initvar)) if arst_posedge is not None: arst_posedge0 = False if arst_posedge else True arst_posedge1 = True if arst_posedge else False else: arst_posedge0 = False arst_posedge1 = True if clk_posedge is not None: clk_posedge0 = False if clk_posedge else True clk_posedge1 = True if clk_posedge else False else: clk_posedge0 = False clk_posedge1 = True if clr is not None: if clr.symbol_type() == BOOL: clr0 = Not(clr) clr1 = clr else: clr0 = EqualsOrIff(clr, BV(0, 1)) clr1 = EqualsOrIff(clr, BV(1, 1)) else: clr0 = TRUE() clr1 = FALSE() if rst is not None: if rst.symbol_type() == BOOL: rst0 = Not(rst) rst1 = rst else: rst0 = EqualsOrIff(rst, BV(0, 1)) rst1 = EqualsOrIff(rst, BV(1, 1)) else: rst0 = TRUE() rst1 = FALSE() if arst is not None: if arst.symbol_type() == BOOL: arst0 = Not(arst) arst1 = arst else: arst0 = EqualsOrIff(arst, BV(0, 1)) arst1 = EqualsOrIff(arst, BV(1, 1)) else: arst0 = TRUE() arst1 = FALSE() if clk.symbol_type() == BOOL: clk0 = Not(clk) clk1 = clk else: clk0 = EqualsOrIff(clk, BV(0, 1)) clk1 = EqualsOrIff(clk, BV(1, 1)) if Modules.abstract_clock: do_clk = TRUE() else: do_clk = And(TS.to_next(clk1), clk0) if clk_posedge1 else And( TS.to_next(clk0), clk1) if out.symbol_type() == BOOL: out0 = FALSE() else: out0 = BV(0, out.symbol_type().width) inr = Ite(clr1, out0, Ite(rst1, initvar, in_)) do_arst = And(TS.to_next(arst1), arst0) if arst_posedge1 else And( TS.to_next(arst0), arst1) ndo_arst = Not(do_arst) ndo_clk = Not(do_clk) act_trans = EqualsOrIff(inr, TS.get_prime(out)) pas_trans = EqualsOrIff(out, TS.get_prime(out)) if Modules.functional: f_outr = Ite(rst1, initvar, out) f_inr = Ite(rst1, initvar, in_) f_clr_rst = Ite(clr1, out0, f_inr) trans = And( trans, EqualsOrIff(TS.get_prime(out), Ite(do_clk, f_clr_rst, f_outr))) else: trans = And(trans, And(Implies(ndo_arst, And(Implies(do_clk, act_trans), Implies(ndo_clk, pas_trans))), \ Implies(do_arst, EqualsOrIff(TS.get_prime(out), initvar)))) trans = simplify(trans) ts = TS(comment) ts.vars, ts.state_vars = set([v for v in vars_ if v is not None]), set([out]) ts.set_behavior(init, trans, invar) return ts
def walk_int_to_bv(self, formula, args, **kwargs): #pylint: disable=unused-argument bv_type = BVType(formula.bv_width()) return self.walk_type_to_type(formula, args, INT, bv_type)
def __init__(self, value=SMYBOLIC, *, name=AUTOMATIC, prefix=AUTOMATIC): if (name is not AUTOMATIC or prefix is not AUTOMATIC) and value is not SMYBOLIC: raise TypeError('Can only name symbolic variables') elif name is not AUTOMATIC and prefix is not AUTOMATIC: raise ValueError('Can only set either name or prefix not both') elif name is not AUTOMATIC: if not isinstance(name, str): raise TypeError('Name must be string') elif name in _name_table: raise ValueError(f'Name {name} already in use') elif _name_re.fullmatch(name): warnings.warn( 'Name looks like an auto generated name, this might break things' ) _name_table[name] = self elif prefix is not AUTOMATIC: name = _gen_name(prefix) _name_table[name] = self elif name is AUTOMATIC and value is SMYBOLIC: name = _gen_name() _name_table[name] = self self._name = name T = BVType(self.size) if value is SMYBOLIC: self._value = smt.Symbol(name, T) elif isinstance(value, pysmt.fnode.FNode): t = value.get_type() if t is T: self._value = value else: raise TypeError(f'Expected {T} not {t}') elif isinstance(value, SMTBitVector): if name is not AUTOMATIC and name != value.name: warnings.warn( 'Changing the name of a SMTBitVector does not cause a new underlying smt variable to be created' ) ext = self.size - value.size if ext < 0: warnings.warn('Truncating value from {} to {}'.format( type(value), type(self))) self._value = value[:self.size].value elif ext > 0: self._value = value.zext(ext).value else: self._value = value.value elif isinstance(value, SMTBit): self._value = smt.Ite(value.value, smt.BVOne(self.size), smt.BVZero(self.size)) elif isinstance(value, tp.Sequence): if len(value) != self.size: raise ValueError('Iterable is not the correct size') cls = type(self) B1 = cls.unsized_t[1] self._value = ft.reduce(lambda acc, elem: acc.concat(elem), map(B1, value)).value elif isinstance(value, int): self._value = smt.BV(value % (1 << self.size), self.size) elif hasattr(value, '__int__'): value = int(value) self._value = smt.BV(value, self.size) else: raise TypeError("Can't coerce {} to SMTBitVector".format( type(value))) self._value = smt.simplify(self._value) assert self._value.get_type() is T
def walk_bv_extend(self, formula, args, **kwargs): #pylint: disable=unused-argument target_width = formula.bv_width() if target_width < args[0].width or target_width < 0: return None return BVType(target_width)
def parse_string(self, strinput): hts = HTS() ts = TS() nodemap = {} node_covered = set([]) translist = [] initlist = [] invarlist = [] invar_props = [] ltl_props = [] def getnode(nid): node_covered.add(nid) if int(nid) < 0: return Ite(BV2B(nodemap[str(-int(nid))]), BV(0,1), BV(1,1)) return nodemap[nid] def binary_op(bvop, bop, left, right): if (get_type(left) == BOOL) and (get_type(right) == BOOL): return bop(left, right) return bvop(B2BV(left), B2BV(right)) def unary_op(bvop, bop, left): if (get_type(left) == BOOL): return bop(left) return bvop(left) for line in strinput.split(NL): linetok = line.split() if len(linetok) == 0: continue if linetok[0] == COM: continue (nid, ntype, *nids) = linetok if ntype == SORT: (stype, *attr) = nids if stype == BITVEC: nodemap[nid] = BVType(int(attr[0])) node_covered.add(nid) if stype == ARRAY: nodemap[nid] = ArrayType(getnode(attr[0]), getnode(attr[1])) node_covered.add(nid) if ntype == WRITE: nodemap[nid] = Store(*[getnode(n) for n in nids[1:4]]) if ntype == READ: nodemap[nid] = Select(getnode(nids[1]), getnode(nids[2])) if ntype == ZERO: nodemap[nid] = BV(0, getnode(nids[0]).width) if ntype == ONE: nodemap[nid] = BV(1, getnode(nids[0]).width) if ntype == ONES: width = getnode(nids[0]).width nodemap[nid] = BV((2**width)-1, width) if ntype == REDOR: width = get_type(getnode(nids[1])).width zeros = BV(0, width) nodemap[nid] = BVNot(BVComp(getnode(nids[1]), zeros)) if ntype == REDAND: width = get_type(getnode(nids[1])).width ones = BV((2**width)-1, width) nodemap[nid] = BVComp(getnode(nids[1]), ones) if ntype == CONSTD: width = getnode(nids[0]).width nodemap[nid] = BV(int(nids[1]), width) if ntype == CONST: width = getnode(nids[0]).width nodemap[nid] = BV(bin_to_dec(nids[1]), width) if ntype == STATE: if len(nids) > 1: nodemap[nid] = Symbol(nids[1], getnode(nids[0])) else: nodemap[nid] = Symbol((SN%nid), getnode(nids[0])) ts.add_state_var(nodemap[nid]) if ntype == INPUT: if len(nids) > 1: nodemap[nid] = Symbol(nids[1], getnode(nids[0])) else: nodemap[nid] = Symbol((SN%nid), getnode(nids[0])) ts.add_input_var(nodemap[nid]) if ntype == OUTPUT: if len(nids) > 2: symbol = Symbol(nids[2], getnode(nids[0])) else: symbol = Symbol((SN%nid), getnode(nids[0])) nodemap[nid] = EqualsOrIff(symbol, B2BV(getnode(nids[1]))) invarlist.append(nodemap[nid]) node_covered.add(nid) ts.add_output_var(symbol) if ntype == AND: nodemap[nid] = binary_op(BVAnd, And, getnode(nids[1]), getnode(nids[2])) if ntype == CONCAT: nodemap[nid] = BVConcat(B2BV(getnode(nids[1])), B2BV(getnode(nids[2]))) if ntype == XOR: nodemap[nid] = binary_op(BVXor, Xor, getnode(nids[1]), getnode(nids[2])) if ntype == NAND: bvop = lambda x,y: BVNot(BVAnd(x, y)) bop = lambda x,y: Not(And(x, y)) nodemap[nid] = binary_op(bvop, bop, getnode(nids[1]), getnode(nids[2])) if ntype == IMPLIES: nodemap[nid] = BVOr(BVNot(getnode(nids[1])), getnode(nids[2])) if ntype == NOT: nodemap[nid] = unary_op(BVNot, Not, getnode(nids[1])) if ntype == UEXT: nodemap[nid] = BVZExt(B2BV(getnode(nids[1])), int(nids[2])) if ntype == OR: nodemap[nid] = binary_op(BVOr, Or, getnode(nids[1]), getnode(nids[2])) if ntype == ADD: nodemap[nid] = BVAdd(B2BV(getnode(nids[1])), B2BV(getnode(nids[2]))) if ntype == SUB: nodemap[nid] = BVSub(B2BV(getnode(nids[1])), B2BV(getnode(nids[2]))) if ntype == UGT: nodemap[nid] = BVUGT(B2BV(getnode(nids[1])), B2BV(getnode(nids[2]))) if ntype == UGTE: nodemap[nid] = BVUGE(B2BV(getnode(nids[1])), B2BV(getnode(nids[2]))) if ntype == ULT: nodemap[nid] = BVULT(B2BV(getnode(nids[1])), B2BV(getnode(nids[2]))) if ntype == ULTE: nodemap[nid] = BVULE(B2BV(getnode(nids[1])), B2BV(getnode(nids[2]))) if ntype == EQ: nodemap[nid] = BVComp(getnode(nids[1]), getnode(nids[2])) if ntype == NE: nodemap[nid] = BVNot(BVComp(getnode(nids[1]), getnode(nids[2]))) if ntype == MUL: nodemap[nid] = BVMul(B2BV(getnode(nids[1])), B2BV(getnode(nids[2]))) if ntype == SLICE: nodemap[nid] = BVExtract(B2BV(getnode(nids[1])), int(nids[3]), int(nids[2])) if ntype == SLL: nodemap[nid] = BVLShl(getnode(nids[1]), getnode(nids[2])) if ntype == SRA: nodemap[nid] = BVAShr(getnode(nids[1]), getnode(nids[2])) if ntype == SRL: nodemap[nid] = BVLShr(getnode(nids[1]), getnode(nids[2])) if ntype == ITE: if (get_type(getnode(nids[2])) == BOOL) or (get_type(getnode(nids[3])) == BOOL): nodemap[nid] = Ite(BV2B(getnode(nids[1])), BV2B(getnode(nids[2])), BV2B(getnode(nids[3]))) else: nodemap[nid] = Ite(BV2B(getnode(nids[1])), getnode(nids[2]), getnode(nids[3])) if ntype == NEXT: if (get_type(getnode(nids[1])) == BOOL) or (get_type(getnode(nids[2])) == BOOL): nodemap[nid] = EqualsOrIff(BV2B(TS.get_prime(getnode(nids[1]))), BV2B(getnode(nids[2]))) else: nodemap[nid] = EqualsOrIff(TS.get_prime(getnode(nids[1])), getnode(nids[2])) translist.append(getnode(nid)) if ntype == INIT: if (get_type(getnode(nids[1])) == BOOL) or (get_type(getnode(nids[2])) == BOOL): nodemap[nid] = EqualsOrIff(BV2B(getnode(nids[1])), BV2B(getnode(nids[2]))) else: nodemap[nid] = EqualsOrIff(getnode(nids[1]), getnode(nids[2])) initlist.append(getnode(nid)) if ntype == CONSTRAINT: nodemap[nid] = BV2B(getnode(nids[0])) invarlist.append(getnode(nid)) if ntype == BAD: nodemap[nid] = getnode(nids[0]) invar_props.append(Not(BV2B(getnode(nid)))) if nid not in nodemap: Logger.error("Unknown node type \"%s\""%ntype) if Logger.level(1): name = lambda x: str(nodemap[x]) if nodemap[x].is_symbol() else x uncovered = [name(x) for x in nodemap if x not in node_covered] uncovered.sort() if len(uncovered) > 0: Logger.warning("Unlinked nodes \"%s\""%",".join(uncovered)) if not self.symbolic_init: init = simplify(And(initlist)) else: init = TRUE() trans = simplify(And(translist)) invar = simplify(And(invarlist)) ts.set_behavior(init, trans, invar) hts.add_ts(ts) return (hts, invar_props, ltl_props)
def add_free_var(self, free_var): """ Creates the encoding for free_var """ # We create a BV variable enc_val = FreshSymbol(BVType(SymbolicGrounding.MAX_BV)) self.fvars2encvars.add(free_var, enc_val) return enc_val
def get_sts(self, name, params): in_port, max_val, clk = list(params) max_val = int(max_val) zero = BV(0, 1) one = BV(1, 1) tracking = Symbol("%s.tracking" % name, BOOL) end = Symbol("%s.end" % name, BOOL) packet = Symbol("%s.packet" % name, BVType(in_port.symbol_type().width)) max_width = math.ceil(math.log(max_val) / math.log(2)) max_bvval = BV(max_val, max_width) count = Symbol("%s.count" % name, BVType(max_width)) pos_clk = And(EqualsOrIff(clk, zero), EqualsOrIff(TS.to_next(clk), one)) neg_clk = Not( And(EqualsOrIff(clk, zero), EqualsOrIff(TS.to_next(clk), one))) init = [] init.append(EqualsOrIff(count, BV(0, max_width))) init.append(EqualsOrIff(tracking, FALSE())) init = And(init) invar = EqualsOrIff(end, EqualsOrIff(max_bvval, count)) trans = [] # tracking -> next(tracking); trans.append(Implies(tracking, TS.to_next(tracking))) # tracking -> (next(packet) = packet); trans.append(Implies(tracking, EqualsOrIff(TS.to_next(packet), packet))) # !tracking & next(tracking) -> (next(clk) = 0_1); trans.append( Implies(And(Not(tracking), TS.to_next(tracking)), EqualsOrIff(TS.to_next(clk), zero))) # ((clk = 0_1) & (next(clk) = 1_1) & next(tracking)) -> (packet = in); trans.append( Implies(And(pos_clk, TS.to_next(tracking)), EqualsOrIff(packet, in_port))) # ((clk = 0_1) & (next(clk) = 1_1) & tracking) -> (next(count) = (count + 1_8)); trans.append( Implies( And(pos_clk, tracking), EqualsOrIff(TS.to_next(count), BVAdd(count, BV(1, max_width))))) # (!((clk = 0_1) & (next(clk) = 1_1))) -> (next(count) = count); trans.append(Implies(neg_clk, EqualsOrIff(count, TS.to_next(count)))) # ((clk = 0_1) & (next(clk) = 1_1) & !tracking) -> (next(count) = count); trans.append( Implies(And(pos_clk, Not(tracking)), EqualsOrIff(count, TS.to_next(count)))) trans = And(trans) ts = TS() ts.vars, ts.init, ts.invar, ts.trans = set( [tracking, end, packet, count]), init, invar, trans return ts
def test_bv_0_add(self): x = Symbol("x", BVType(32)) f = BVAdd(BVZero(32), x) self.check_equal_and_valid(f, x)
def test_bv_ult_zero(self): x = Symbol("x", BVType(32)) f = BVULT(x, BVZero(32)) self.check_equal_and_valid(f, Bool(False))