def local_max_thresholds_iterative(all_lines_list, lispnode_list, model, solver): define_funs_from_model(model, lispnode_list) thresholds_lookup = LispNode("") for lispnode in lispnode_list: for node in lispnode.find("thresholds-bv-version-lookup-table"): if node.parent.children[0] == "define-fun": thresholds_lookup = node.parent.children[4] if thresholds_lookup.token.startswith("#x"): thresholds_lookup.token = "#b" + hex_to_binary( thresholds_lookup.token[2:]) for bit in range(2, len(thresholds_lookup.token)): if thresholds_lookup.token[bit] == "1": print bit print thresholds_lookup.token thresholds_lookup.token = thresholds_lookup.token[ :bit]+"0"+\ thresholds_lookup.token[bit+1: ] #write file, use solver to determine if this is valid f_name = ("/tmp/iteration-"+\ str("-".join(str(datetime.datetime.now()).split()))+".smt2") f = open(f_name, "w") for x in map(lambda x: str(x).strip(), all_lines_list): if (len(x) > 0): if (x[0] == "("): x = LispNode(x).pretty_print() f.write(x) f.write("\n") f.flush() f.close() model_filename = "/tmp/iteration-model"+\ str("-".join(str(datetime.datetime.now()).split()))+".smt2" os.system(solver + " " + f_name + " > " + model_filename) m = open(model_filename, "r").read() m = m.strip() if m.startswith("sat"): return local_max_thresholds_iterative(all_lines_list, lispnode_list, LispNode(m[3:]), solver) elif m.startswith("unsat"): thresholds_lookup.token = thresholds_lookup.token[ :bit]+"1"+\ thresholds_lookup.token[bit+1: ] else: print "ITERATIVE SOLVER FAILED:\n" + m return thresholds_lookup.token return thresholds_lookup.token
def local_max_thresholds_iterative(all_lines_list, lispnode_list,model,solver): define_funs_from_model(model, lispnode_list) thresholds_lookup = LispNode("") for lispnode in lispnode_list: for node in lispnode.find("thresholds-bv-version-lookup-table"): if node.parent.children[0] == "define-fun": thresholds_lookup = node.parent.children[4] if thresholds_lookup.token.startswith("#x"): thresholds_lookup.token = "#b"+hex_to_binary(thresholds_lookup.token[2:]) for bit in range(2, len(thresholds_lookup.token)): if thresholds_lookup.token[bit] == "1": print bit print thresholds_lookup.token thresholds_lookup.token = thresholds_lookup.token[ :bit]+"0"+\ thresholds_lookup.token[bit+1: ] #write file, use solver to determine if this is valid f_name = ("/tmp/iteration-"+\ str("-".join(str(datetime.datetime.now()).split()))+".smt2") f = open(f_name,"w") for x in map(lambda x:str(x).strip(),all_lines_list): if (len(x) > 0): if (x[0] == "("): x = LispNode(x).pretty_print() f.write(x) f.write("\n") f.flush() f.close() model_filename = "/tmp/iteration-model"+\ str("-".join(str(datetime.datetime.now()).split()))+".smt2" os.system(solver+" "+f_name+" > "+ model_filename) m = open(model_filename,"r").read() m = m.strip() if m.startswith("sat"): return local_max_thresholds_iterative(all_lines_list, lispnode_list, LispNode(m[3:]), solver) elif m.startswith("unsat"): thresholds_lookup.token = thresholds_lookup.token[ :bit]+"1"+\ thresholds_lookup.token[bit+1: ] else: print "ITERATIVE SOLVER FAILED:\n"+m return thresholds_lookup.token return thresholds_lookup.token
def local_max_thresholds(all_lines_list, lispnode_list): """adds the requirement that flipping any 1 of thresholds to 0 fails assertions. Must be done after functions to tables but before exists and forall unrolling""" all_requirements = LispNode(children = [LispNode("and")] + map(lambda x:LispNode(x.children[1]), filter(lambda y: (len(y.children) > 1) and (y.children[0]=="assert"), lispnode_list))) # get the length of the lookup table, and thresholds functions len_lookup = 0 thresholds_all_index = 0 thresholds_index = 0 thresholds_bv = LispNode("") thresholds = LispNode("") for lispnode in lispnode_list: for node in lispnode.find("thresholds-bv-version-lookup-table"): if node.parent.children[0] == "declare-fun": len_lookup = int(str(node.parent.children[3].children[2])) for node in lispnode.find("thresholds-bv-version"): if node.parent.children[0] == "define-fun": thresholds_bv = LispNode(node.parent) for node in lispnode.find("thresholds"): if node.parent.children[0] == "define-fun": thresholds_all_index = all_lines_list.index(lispnode) thresholds_index = lispnode_list.index(lispnode) thresholds = LispNode(node.parent) # create a new function, threshholds-with-table, that is like thresholds but # takes as its last input a lookup table new_thresholds = LispNode(thresholds) new_thresholds.replace("thresholds","thresholds-with-table") new_thresholds.replace("thresholds-bv-version","thresholds-bv-version-with-table") new_thresholds.children[2].children.append(LispNode( "(lookup-table (_ BitVec "+str(len_lookup)+"))", parent = new_thresholds.children[2])) for ntbv in new_thresholds.find("thresholds-bv-version-with-table"): if ntbv.parent.children[0] == "thresholds-bv-version-with-table": ntbv.parent.children.append(LispNode("lookup-table", parent = ntbv)) # create a function thresholds-bv-version-with-table for use by # thresholds-with-table new_thresholds_bv = LispNode(thresholds_bv) new_thresholds_bv.replace("thresholds","thresholds-with-table") new_thresholds_bv.replace("thresholds-bv-version","thresholds-bv-version-with-table") new_thresholds_bv.children[2].children.append(LispNode( "(lookup-table (_ BitVec "+str(len_lookup)+"))", parent = new_thresholds_bv.children[2])) new_thresholds_bv.children[4].replace("thresholds-bv-version-lookup-table", "lookup-table") # modify the set of all requirements to take as input a table, and stipulate # that for no such table where you've changed one "1" bit to a "0" are all # the requirements met len_lookup_index = int(math.ceil(math.log(len_lookup, 2))) max_lookup_index = "#b"+binaries(len_lookup_index)[len_lookup-1] adapted_table = LispNode("(bvand thresholds-bv-version-lookup-table (bvnot "+\ "(bvshl "+str(bv_all_zero_1_in_ith_place(len_lookup, 0))+" (concat "+\ str(bv_all_zero_1_in_ith_place(len_lookup-len_lookup_index, len_lookup))+\ " thresholds-lookup-index))))") for thresholds in all_requirements.find("thresholds"): if thresholds.parent.children[0] == thresholds: thresholds.token="thresholds-with-table" thresholds.parent.children.append(LispNode(adapted_table, parent=thresholds.parent)) lispnode_list.insert(thresholds_index, LispNode(children= [LispNode("assert"), LispNode(children = [LispNode("not"), LispNode(children = [LispNode("exists"), LispNode("((thresholds-lookup-index (_ BitVec "+str(len_lookup_index)+\ ")))"), LispNode(children= [LispNode("and"), LispNode("(bvule thresholds-lookup-index "+max_lookup_index+")"), LispNode("(not (= thresholds-bv-version-lookup-table "+\ str(adapted_table)+"))"), all_requirements])])])])) # add our functions to the file all_lines_list.insert(thresholds_all_index, lispnode_list[thresholds_index]) lispnode_list.insert(thresholds_index, new_thresholds) all_lines_list.insert(thresholds_all_index, new_thresholds) lispnode_list.insert(thresholds_index, new_thresholds_bv) all_lines_list.insert(thresholds_all_index, new_thresholds_bv)
def local_max_thresholds(all_lines_list, lispnode_list): """adds the requirement that flipping any 1 of thresholds to 0 fails assertions. Must be done after functions to tables but before exists and forall unrolling""" all_requirements = LispNode(children=[LispNode("and")] + map( lambda x: LispNode(x.children[1]), filter(lambda y: (len(y.children) > 1) and (y.children[0] == "assert"), lispnode_list))) # get the length of the lookup table, and thresholds functions len_lookup = 0 thresholds_all_index = 0 thresholds_index = 0 thresholds_bv = LispNode("") thresholds = LispNode("") for lispnode in lispnode_list: for node in lispnode.find("thresholds-bv-version-lookup-table"): if node.parent.children[0] == "declare-fun": len_lookup = int(str(node.parent.children[3].children[2])) for node in lispnode.find("thresholds-bv-version"): if node.parent.children[0] == "define-fun": thresholds_bv = LispNode(node.parent) for node in lispnode.find("thresholds"): if node.parent.children[0] == "define-fun": thresholds_all_index = all_lines_list.index(lispnode) thresholds_index = lispnode_list.index(lispnode) thresholds = LispNode(node.parent) # create a new function, threshholds-with-table, that is like thresholds but # takes as its last input a lookup table new_thresholds = LispNode(thresholds) new_thresholds.replace("thresholds", "thresholds-with-table") new_thresholds.replace("thresholds-bv-version", "thresholds-bv-version-with-table") new_thresholds.children[2].children.append( LispNode("(lookup-table (_ BitVec " + str(len_lookup) + "))", parent=new_thresholds.children[2])) for ntbv in new_thresholds.find("thresholds-bv-version-with-table"): if ntbv.parent.children[0] == "thresholds-bv-version-with-table": ntbv.parent.children.append(LispNode("lookup-table", parent=ntbv)) # create a function thresholds-bv-version-with-table for use by # thresholds-with-table new_thresholds_bv = LispNode(thresholds_bv) new_thresholds_bv.replace("thresholds", "thresholds-with-table") new_thresholds_bv.replace("thresholds-bv-version", "thresholds-bv-version-with-table") new_thresholds_bv.children[2].children.append( LispNode("(lookup-table (_ BitVec " + str(len_lookup) + "))", parent=new_thresholds_bv.children[2])) new_thresholds_bv.children[4].replace("thresholds-bv-version-lookup-table", "lookup-table") # modify the set of all requirements to take as input a table, and stipulate # that for no such table where you've changed one "1" bit to a "0" are all # the requirements met len_lookup_index = int(math.ceil(math.log(len_lookup, 2))) max_lookup_index = "#b" + binaries(len_lookup_index)[len_lookup - 1] adapted_table = LispNode("(bvand thresholds-bv-version-lookup-table (bvnot "+\ "(bvshl "+str(bv_all_zero_1_in_ith_place(len_lookup, 0))+" (concat "+\ str(bv_all_zero_1_in_ith_place(len_lookup-len_lookup_index, len_lookup))+\ " thresholds-lookup-index))))") for thresholds in all_requirements.find("thresholds"): if thresholds.parent.children[0] == thresholds: thresholds.token = "thresholds-with-table" thresholds.parent.children.append( LispNode(adapted_table, parent=thresholds.parent)) lispnode_list.insert(thresholds_index, LispNode(children= [LispNode("assert"), LispNode(children = [LispNode("not"), LispNode(children = [LispNode("exists"), LispNode("((thresholds-lookup-index (_ BitVec "+str(len_lookup_index)+\ ")))"), LispNode(children= [LispNode("and"), LispNode("(bvule thresholds-lookup-index "+max_lookup_index+")"), LispNode("(not (= thresholds-bv-version-lookup-table "+\ str(adapted_table)+"))"), all_requirements])])])])) # add our functions to the file all_lines_list.insert(thresholds_all_index, lispnode_list[thresholds_index]) lispnode_list.insert(thresholds_index, new_thresholds) all_lines_list.insert(thresholds_all_index, new_thresholds) lispnode_list.insert(thresholds_index, new_thresholds_bv) all_lines_list.insert(thresholds_all_index, new_thresholds_bv)