Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
global type_cache
if type_cache is None:
type_cache = parse_defns(open(os.path.join(location, 'tests_src', 'manyfloatsum.c')).read())
p = angr.Project(os.path.join(location, 'tests', arch, 'manyfloatsum'))
function = 'sum_doubles'
cc = p.factory.cc(func_ty=type_cache[function])
args = [claripy.FPS('arg_%d' % i, claripy.FSORT_DOUBLE) for i in range(len(type_cache[function].args))]
addr = p.loader.main_object.get_symbol(function).rebased_addr
my_callable = p.factory.callable(addr, cc=cc)
result = my_callable(*args)
nose.tools.assert_true(result.symbolic)
s = claripy.Solver()
for arg in args:
s.add(arg > claripy.FPV(1.0, claripy.FSORT_DOUBLE))
s.add(result == claripy.FPV(27.7, claripy.FSORT_DOUBLE))
args_conc = s.batch_eval(args, 1)[0]
nose.tools.assert_equal(s.eval(result, 1)[0], 27.7)
# not almost equal!! totally equal!!! z3 is magic, if kinda slow!!!!!
for arg_conc in args_conc:
nose.tools.assert_greater(arg_conc, 1.0)
nose.tools.assert_equal(sum(args_conc), 27.7)
val = val.reversed
return val
elif isinstance(arg, float):
sort = claripy.FSORT_FLOAT
if check:
if isinstance(ty, SimTypeDouble):
sort = claripy.FSORT_DOUBLE
elif isinstance(ty, SimTypeFloat):
pass
else:
raise TypeError("Type mismatch: expectd %s, got float" % ty.name)
else:
sort = claripy.FSORT_DOUBLE if state.arch.bits == 64 else claripy.FSORT_FLOAT
val = claripy.fpToIEEEBV(claripy.FPV(arg, sort))
if state.arch.memory_endness == 'Iend_LE':
val = val.reversed # pylint: disable=no-member
return val
elif isinstance(arg, claripy.ast.FP):
val = claripy.fpToIEEEBV(arg)
if state.arch.memory_endness == 'Iend_LE':
val = val.reversed # pylint: disable=no-member
return val
elif isinstance(arg, claripy.ast.Base):
endswap = False
bypass_sizecheck = False
if check:
if isinstance(ty, SimTypePointer):
# we have been passed an AST as a pointer argument. is this supposed to be the pointer or the
def make_default_value(ty):
if 'F' in ty:
if '32' in ty:
return claripy.FPV(0.0, claripy.fp.FSORT_FLOAT)
elif '64' in ty:
return claripy.FPV(0.0, claripy.fp.FSORT_DOUBLE)
else:
raise ValueError("Unknown float type %s" % ty)
else:
return claripy.BVV(0, extract_int(ty))
def default(ty):
size = int(''.join(c for c in ty if c in '0123456789'))
fp = ty.startswith('Ity_F')
if not fp:
return BiHead(claripy.BVV(0, size), claripy.BVV(0, size))
else:
if size == 32:
sort = claripy.fp.FSORT_FLOAT
elif size == 64:
sort = claripy.fp.FSORT_DOUBLE
else:
raise ValueError("Bad float size: %d" % size)
return BiHead(claripy.FPV(0.0, sort), claripy.FPV(0.0, sort))
def pow(rm, arg, n):
out = claripy.FPV(1.0, arg.sort)
for _ in range(n):
out = claripy.fpMul(rm, arg, out)
return out
val = val.reversed
return val
elif isinstance(arg, float):
sort = claripy.FSORT_FLOAT
if check:
if isinstance(ty, s_type.SimTypeDouble):
sort = claripy.FSORT_DOUBLE
elif isinstance(ty, s_type.SimTypeFloat):
pass
else:
raise TypeError("Type mismatch: expectd %s, got float" % ty.name)
else:
sort = claripy.FSORT_DOUBLE if state.arch.bits == 64 else claripy.FSORT_FLOAT
val = claripy.fpToIEEEBV(claripy.FPV(arg, sort))
if state.arch.memory_endness == 'Iend_LE':
val = val.reversed # pylint: disable=no-member
return val
elif isinstance(arg, claripy.ast.Base):
# yikes
if state.arch.memory_endness == 'Iend_LE' and arg.length == state.arch.bits:
arg = arg.reversed
return arg
else:
raise TypeError("I don't know how to serialize %s." % repr(arg))
:param e: The expression `value` is a solution for
:param value: The solution to be cast
:param cast_to: The type `value` should be cast to. Must be one of the currently supported types (bytes|int)
:raise ValueError: If cast_to is a currently unsupported cast target.
:return: The value of `solution` cast to type `cast_to`
"""
if cast_to is None:
return solution
if type(solution) is bool:
if cast_to is bytes:
return bytes([int(solution)])
elif cast_to is int:
return int(solution)
elif type(solution) is float:
solution = _concrete_value(claripy.FPV(solution, claripy.fp.FSort.from_size(len(e))).raw_to_bv())
if cast_to is bytes:
if len(e) == 0:
return b""
return binascii.unhexlify('{:x}'.format(solution).zfill(len(e)//4))
if cast_to is not int:
raise ValueError("cast_to parameter {!r} is not a valid cast target, currently supported are only int and bytes!".format(cast_to))
return solution