def gen(self, depth=0): float_sort = SExpr("_", "FloatingPoint", self.eb, self.sb) exps = [] for i in range(settings.NumPrimaries): exps.append(self.gen_rand_sexpr(depth)) ret = Instance(exps) ret.set_logic("QF_FP") for i in range(settings.GeneratorNumConst): ret.declare_const("x" + str(i), float_sort) ret.mk_auxilary() for i in range(settings.NumPrimaries): ret.mk_assert(ret.primaries[i]) ret.check_sat() return ret
def bandit_mutate_core_bool_ops(self, primary, new_op): if primary.op == new_op: return primary, 0, True, True ret_form = SExpr() ret_form.op = new_op new_sig = self.funcs[ret_form.op] start = 0 while len(ret_form.args) < len(new_sig) - 1: if start < len(primary.args): ret_form.append(primary.args[start]) start += 1 else: ret_form.append(self.gen_rand_sexpr(1)) return ret_form, 0, True, False
def mk_and(left, right): return SExpr("and", left, right)
def mk_fp_isNormal(fp): return SExpr("fp.isNormal", fp)
def mk_fp_gt(fp1, fp2): return SExpr("fp.gt", fp1, fp2)
def mk_fp_max(fp1, fp2): return SExpr("fp.max", fp1, fp2)
def mk_fp_roundToIntegral(mode, fp): return SExpr("fp.roundToIntegral", mode, fp)
def mk_fp_rem(fp1, fp2): return SExpr("fp.rem", fp1, fp2)
def mk_fp_isNegative(fp): return SExpr("fp.isNegative", fp)
def mk_fp_isNaN(fp): return SExpr("fp.isNaN", fp)
def mk_fp_isInfinite(fp): return SExpr("fp.isInfinite", fp)
def mk_fp_isZero(fp): return SExpr("fp.isZero", fp)
from banditfuzz.interface.smtlib.script import SExpr QF_FP_ALIAS = { 'Float5' : SExpr("_", "FloatingPoint", 2, 3), 'Float16' : SExpr("_", "FloatingPoint", 5, 11), 'Float32' : SExpr("_", "FloatingPoint", 8, 24), 'Float64' : SExpr("_", "FloatingPoint", 11, 53), 'Float128': SExpr("_", "FloatingPoint", 15, 113) } QF_FP_SORTS = { 'Mode' : [], 'FloatingPoint' : ["Num", "Num"], } QF_FP_FUNCS = { 'fp.abs' : ['FloatingPoint', 'FloatingPoint'], 'fp.neg' : ['FloatingPoint', 'FloatingPoint'], 'fp.add' : ['Mode', 'FloatingPoint', 'FloatingPoint', 'FloatingPoint'], 'fp.sub' : ['Mode', 'FloatingPoint', 'FloatingPoint', 'FloatingPoint'], 'fp.mul' : ['Mode', 'FloatingPoint', 'FloatingPoint', 'FloatingPoint'], 'fp.div' : ['Mode', 'FloatingPoint', 'FloatingPoint', 'FloatingPoint'], 'fp.fma' : ['Mode', 'FloatingPoint', 'FloatingPoint', 'FloatingPoint', 'FloatingPoint'], 'fp.rem' : ['FloatingPoint', 'FloatingPoint', 'FloatingPoint'], 'fp.sqrt' : ['Mode', 'FloatingPoint', 'FloatingPoint'], 'fp.roundToIntegral' : ['Mode', 'FloatingPoint', 'FloatingPoint'], 'fp.min' : ['FloatingPoint', 'FloatingPoint', 'FloatingPoint'], 'fp.max' : ['FloatingPoint', 'FloatingPoint', 'FloatingPoint'], 'fp.leq' : ['FloatingPoint', 'FloatingPoint', 'Bool'], 'fp.lt' : ['FloatingPoint', 'FloatingPoint', 'Bool'], 'fp.geq' : ['FloatingPoint', 'FloatingPoint', 'Bool'],
def mutate(self, instance, action): float_sort = SExpr("_", "FloatingPoint", self.eb, self.sb) exp = None done = None fail = None new_exp = None m = -1 n = -1 if action in self.ops: m = randint(0, settings.NumPrimaries - 1) num_fp_ops = instance.num_float_ops(prim_indx=m) n = randint(0, num_fp_ops - 1) exp = deepcopy(instance.primaries[m]) new_exp, n, done, fail = self.bandit_mutate_core_fp_ops( exp, action, n) it = 0 while fail and it < 4: m = randint(0, settings.NumPrimaries - 1) exp = deepcopy(instance.primaries[m]) num_fp_ops = instance.num_float_ops(prim_indx=m) n = randint(0, num_fp_ops - 1) new_exp, n, done, fail = self.bandit_mutate_core_fp_ops( exp, action, n) it += 1 elif action in self.boolean_ops: m = randint(0, settings.NumPrimaries - 1) exp = deepcopy(instance.primaries[m]) new_exp, _, done, fail = self.bandit_mutate_core_bool_ops( exp, action) it = 0 while fail and it < 4: m = randint(0, settings.NumPrimaries - 1) exp = deepcopy(instance.primaries[m]) new_exp, _, done, fail = self.bandit_mutate_core_bool_ops( exp, action) it += 1 else: # action in self.rounding_modes: assert action in self.rounding_modes it = 0 m = randint(0, settings.NumPrimaries - 1) num_rounds = instance.num_rounding_modes(prim_indx=m) if num_rounds == 0: it += 1 fail = True else: exp = deepcopy(instance.primaries[m]) n = randint(0, num_rounds - 1) new_exp, _, done, fail = self.bandit_mutate_core_round_mode( exp, action, n) while fail and it < 4: m = randint(0, settings.NumPrimaries - 1) num_rounds = instance.num_rounding_modes(prim_indx=m) if num_rounds == 0: it += 1 continue n = randint(0, num_rounds - 1) exp = deepcopy(instance.primaries[m]) new_exp, _, done, fail = self.bandit_mutate_core_round_mode( exp, action, n) it += 1 if fail or it == 5: return None new_primaries = deepcopy(instance.primaries) # print(new_primaries[m]) # print(new_exp) # input(action+" " + str(n)) new_primaries[m] = new_exp ret = Instance(new_primaries) ret.set_logic("QF_FP") for i in range(settings.GeneratorNumConst): ret.declare_const("x" + str(i), float_sort) ret.mk_auxilary() for i in range(settings.NumPrimaries): ret.mk_assert(ret.primaries[i]) ret.check_sat() return ret
def mk_fp_div(mode, fp1, fp2): return SExpr("fp.div", mode, fp1, fp2)
def mk_fp_isPositive(fp): return SExpr("fp.isPositive", fp)
def mk_fp_fma(mode, fp1, fp2, fp3): return SExpr("fp.fma", mode, fp1, fp2, fp3)
def mk_literal(sign, exp, mant): return SExpr("fp", "#b%s"%sign, "#b%s"%exp, "#b%s"%mant)
def mk_fp_sqrt(mode, fp): return SExpr("fp.sqrt", mode, fp)
def mk_fp_abs(fp): return SExpr("fp.abs", fp)
def mk_fp_min(fp1, fp2): return SExpr("fp.min", fp1, fp2)
def mk_fp_neg(fp): return SExpr("fp.neg", fp)
def mk_fp_lt(fp1, fp2): return SExpr("fp.lt", fp1, fp2)
def mk_fp_add(mode, fp1, fp2): return SExpr("fp.add", mode, fp1, fp2)
def mk_fp_eq(fp1, fp2): return SExpr("fp.eq", fp1, fp2)
def mk_fp_sub(mode, fp1, fp2): return SExpr("fp.sub", mode, fp1, fp2)
def mk_not(expr): return SExpr("not", expr)
def mk_fp_mul(mode, fp1, fp2): return SExpr("fp.mul", mode, fp1, fp2)
def mk_or(left, right): return SExpr("or", left, right)
def bandit_mutate_core_fp_ops(self, primary, new_op, n, indx=0, depth=0): if isinstance(primary, str): return primary, indx, False, False op = primary.op sig = self.funcs[op] ret_form = SExpr() if sig[-1] != 'Bool' and indx == n: new_sig = self.funcs[new_op] if op == new_op: return primary, indx, True, True ret_form.op = new_op start = 0 if sig[0] == "Mode" and new_sig[0] == "Mode": ret_form.append(primary.args[0]) start = 1 elif sig[0] == "Mode": start = 1 elif new_sig[0] == "Mode": ret_form.append(choice(self.rounding_modes)) while len(ret_form.args) < len(new_sig) - 1: if start < len(sig) - 1: ret_form.append(primary.args[start]) start += 1 else: ret_form.append(self.gen_rand_sexpr(depth + 1)) return ret_form, indx, True, False elif depth != 0: indx += 1 start = 0 ret_form.op = op if sig[0] == "Mode": start = 1 ret_form.append(primary.args[0]) done = False fail = False for i in range(start, len(sig) - 1): if not done: arg, indx, done, fail = self.bandit_mutate_core_fp_ops( primary.args[i], new_op, n, indx, depth + 1) ret_form.append(arg) else: ret_form.append(primary.args[i]) return ret_form, indx, done, fail