Esempio n. 1
0
 def __rpow__(self,
              value,
              mod=None,
              /):  # <slot wrapper '__rpow__' of 'int' objects> TODO
     """Return pow(value, self, mod)."""
     log.debug("ConInt, __rpow__ is called")
     return ConcolicObject(super().__rpow__(unwrap(value), unwrap(mod)))
Esempio n. 2
0
def _is(obj1, obj2):
    from libct.concolic import Concolic
    from libct.utils import unwrap
    if obj1 is obj2: return True
    if isinstance(obj1, Concolic): obj1 = unwrap(obj1)
    if isinstance(obj2, Concolic): obj2 = unwrap(obj2)
    return obj1 is obj2
Esempio n. 3
0
 def __reversed__(self, *args,
                  **kwargs):  # <method '__reversed__' of 'range' objects>
     """Return a reverse iterator."""
     log.debug("ConRange, __reversed__ is called")
     args = [unwrap(arg) for arg in args]
     kwargs = {k: unwrap(v)
               for (k, v) in kwargs.items()}
     return ConcolicObject(self.super.__reversed__(*args, **kwargs))
Esempio n. 4
0
 def __round__(self, *args,
               **kwargs):  # <method '__round__' of 'int' objects>
     """Rounding an Integral returns itself.\nRounding with an ndigits argument also returns an integer."""
     log.debug("ConInt, __round__ is called")
     args = [unwrap(arg) for arg in args]
     kwargs = {k: unwrap(v)
               for (k, v) in kwargs.items()}
     value = super().__round__(*args, **kwargs)
     return ConcolicObject(value, self)
Esempio n. 5
0
 def conjugate(self, *args,
               **kwargs):  # <method 'conjugate' of 'int' objects>
     """Returns self, the complex conjugate of any int."""
     log.debug("ConInt, conjugate is called")
     args = [unwrap(arg) for arg in args]
     kwargs = {k: unwrap(v)
               for (k, v) in kwargs.items()}
     value = super().conjugate(*args, **kwargs)
     return ConcolicObject(value, self)
Esempio n. 6
0
 def __floor__(self, *args,
               **kwargs):  # <method '__floor__' of 'int' objects>
     """Flooring an Integral returns itself."""
     log.debug("ConInt, __floor__ is called")
     args = [unwrap(arg) for arg in args]
     kwargs = {k: unwrap(v)
               for (k, v) in kwargs.items()}
     value = super().__floor__(*args, **kwargs)
     return ConcolicObject(value, self)
Esempio n. 7
0
 def _get_concolic_arguments(self, func, prim_args):
     ccc_args = []
     ccc_kwargs = {}
     for v in inspect.signature(func).parameters.values():
         if v.kind in (inspect.Parameter.VAR_POSITIONAL,
                       inspect.Parameter.VAR_KEYWORD):
             prim_args.pop(v.name, None)
             continue  # do not support *args, **kwargs currently
         if v.name in prim_args:
             value = prim_args[v.name]
         else:
             has_value = False
             if (t := v.annotation) is not inspect._empty:
                 try:
                     value = t()
                     has_value = True  # may raise TypeError: Cannot instantiate ...
                 except:
                     pass
             if not has_value:
                 if (t := v.default) is not inspect._empty:
                     value = unwrap(t)  # default values may also be wrapped
                 else:
                     value = ''
             prim_args[v.name] = value if type(value) in (
                 bool, float, int, str) else self.LazyLoading
Esempio n. 8
0
 def add_branch(self, conbool):
     p = Predicate(conbool.expr, unwrap(conbool))
     c = self.current_constraint.find_child(p)
     pneg = Predicate(conbool.expr, not unwrap(conbool))
     cneg = self.current_constraint.find_child(pneg)
     if c is None and cneg is None:
         c = self.current_constraint.add_child(p)
         c.processed = True  # for debugging purposes
         cneg = self.current_constraint.add_child(pneg)
         conbool.engine.constraints_to_solve.append(
             cneg
         )  # add the negated constraint to the queue for later traversal
         log.smtlib2(f"Now constraint: {c}")
         log.smtlib2(f"Add constraint: {cneg}")
     else:
         assert c is not None and cneg is not None
     self.current_constraint = c  # move the current constraint to the child we want now
Esempio n. 9
0
 def __ge__(self, other, /):
     try:
         if (value := super().__ge__(unwrap(other))) is NotImplemented:
             raise NotImplementedError
     except:
         value = unwrap(other).__le__(unwrap(self))
     if isinstance(other, Concolic):
         if hasattr(other, 'isBool'): other = other.__float2__()
         assert not (hasattr(other, 'super')
                     and isinstance(other.super, range))
         assert not isinstance(other, str)
         # other cannot be of type "range" and "str" here, since Exception should be thrown at the statement: value = unwrap(other).__ge__(unwrap(self))
     else:
         if type(other) is bool: other = float(other)
         if type(other) is not int and type(other) is not float:
             return ConcolicObject(
                 value
             )  # discard the symbolic expression if it cannot match the concrete value
         other = ConcolicObject(other)
     expr = ['>=', self, other]
     return ConcolicObject(value, expr)
Esempio n. 10
0
 def __truediv__(self,
                 other):  # <slot wrapper '__truediv__' of 'float' objects>
     log.debug("ConFloat, __truediv__ is called")
     value = super().__truediv__(unwrap(other))
     if isinstance(other, Concolic):
         if hasattr(other, '__float2__'): other = other.__float2__()
     else:
         try:
             other = float(other)
         except:
             other = 1.0
         other = self.__class__(other)
     expr = ['/', self, other]
     return ConcolicObject(value, expr)
Esempio n. 11
0
 def __getitem__(self, key,
                 /):  # <slot wrapper '__getitem__' of 'range' objects>
     value = self.value.__getitem__(unwrap(key))
     if isinstance(
             key, slice
     ) and key.start is None and key.stop is None and key.step == -1:
         if self.step > 0:
             k = (self.stop - self.start) // self.step - int(
                 (self.stop - self.start) % self.step == 0)
             start = self.start + k * self.step
             stop = self.start - self.step
             step = -self.step
             if start == value.start and stop == value.stop and step == value.step:
                 return self.__class__(start, stop, step)
     return ConcolicObject(value)  # TODO
Esempio n. 12
0
 def index(self, key):  # <method 'index' of 'range' objects>
     """rangeobject.index(value) -> integer -- return index of value.\nRaise ValueError if the value is not present."""
     log.debug(
         "ConRange, index is called"
     )  #; args = [unwrap(arg) for arg in args]; kwargs = {k: unwrap(v) for (k, v) in kwargs.items()}
     value = self.super.index(unwrap(
         key))  # raise Exception when "key" is not in the range object
     if isinstance(key, Concolic) and hasattr(key, '__int2__'):
         key = key.__int2__()
     else:
         try:
             key = int(key)
         except:
             key = 0
         key = ConcolicObject(key)
     return ConcolicObject(value, (key - self.start) // self.step)
Esempio n. 13
0
 def __init__(self, *args):
     self.super = range(*[unwrap(arg)
                          for arg in args])  # emulate inheritance
     if len(args) < 2:
         self.start = 0
         self.stop = args[0]
     else:
         self.start = args[0]
         self.stop = args[1]
     if len(args) < 3:
         self.step = 1
     else:
         self.step = args[2]
     # We must ensure that all attributes are concolic integers in order to
     # make the overridden __getitem__ and __iter__ work!
     if not (isinstance(self.start, int)
             and isinstance(self.start, Concolic)):  # not ConcolicInt
         if isinstance(self.start, Concolic) and hasattr(
                 self.start, '__int2__'):
             self.start = self.start.__int2__()
             if self.start != self.super.start:
                 self.start = ConcolicObject(
                     self.super.start)  # ConcolicInt
         else:
             self.start = ConcolicObject(self.super.start)  # ConcolicInt
     if not (isinstance(self.stop, int)
             and isinstance(self.stop, Concolic)):  # not ConcolicInt
         if isinstance(self.stop, Concolic) and hasattr(
                 self.stop, '__int2__'):
             self.stop = self.stop.__int2__()
             if self.stop != self.super.stop:
                 self.stop = ConcolicObject(self.super.stop)  # ConcolicInt
         else:
             self.stop = ConcolicObject(self.super.stop)  # ConcolicInt
     if not (isinstance(self.step, int)
             and isinstance(self.step, Concolic)):  # not ConcolicInt
         if isinstance(self.step, Concolic) and hasattr(
                 self.step, '__int2__'):
             self.step = self.step.__int2__()
             if self.step != self.super.step:
                 self.step = ConcolicObject(self.super.step)  # ConcolicInt
         else:
             self.step = ConcolicObject(self.super.step)  # ConcolicInt
Esempio n. 14
0
 def __contains__(self, key,
                  /):  # <slot wrapper '__contains__' of 'range' objects>
     """Return key in self."""
     log.debug("ConRange, __contains__ is called")
     value = self.super.__contains__(unwrap(key))
     if isinstance(key, Concolic) and hasattr(key, '__int2__'):
         key = key.__int2__()
     else:
         try:
             key = int(key)
         except:
             key = 0
         key = ConcolicObject(key)
     if self.start < self.stop:
         return ConcolicObject(value, self.start <= key < self.stop and \
                                      (key - self.start) % self.step == 0)
     if self.start > self.stop:
         return ConcolicObject(value, self.start >= key > self.stop and \
                                      (key - self.start) % self.step == 0)
     return ConcolicObject(value)
Esempio n. 15
0
 def count(self, key):  # <method 'count' of 'range' objects>
     """rangeobject.count(value) -> integer -- return number of occurrences of value"""
     log.debug(
         "ConRange, count is called"
     )  #; args = [unwrap(arg) for arg in args]; kwargs = {k: unwrap(v) for (k, v) in kwargs.items()}
     value = self.super.count(unwrap(key))
     if isinstance(key, Concolic) and hasattr(key, '__int2__'):
         key = key.__int2__()
     else:
         try:
             key = int(key)
         except:
             key = 0
         key = ConcolicObject(key)
     if self.start < self.stop:
         return ConcolicObject(value, (self.start <= key < self.stop and \
                                      (key - self.start) % self.step == 0).__int2__())
     if self.start > self.stop:
         return ConcolicObject(value, (self.start >= key > self.stop and \
                                      (key - self.start) % self.step == 0).__int2__())
     return ConcolicObject(value)
Esempio n. 16
0
 def __format__(self, format_spec,
                /):  # <method '__format__' of 'int' objects> TODO
     """Default object formatter."""
     log.debug("ConInt, __format__ is called")
     return ConcolicObject(super().__format__(unwrap(format_spec)))
Esempio n. 17
0
 def socket_getaddrinfo(*args, **kwargs):
     return _socket_getaddrinfo(
         *map(unwrap, args), **{k: unwrap(v)
                                for (k, v) in kwargs.items()})
Esempio n. 18
0
 def __divmod__(self, value,
                /):  # <slot wrapper '__divmod__' of 'int' objects> TODO
     """Return divmod(self, value)."""
     log.debug("ConInt, __divmod__ is called")
     return ConcolicObject(super().__divmod__(unwrap(value)))
Esempio n. 19
0
 def _bin_op(self, op, other):
     if op == '__add__':
         try:
             if (value := super().__add__(unwrap(other))) is NotImplemented:
                 raise NotImplementedError
         except:
             value = unwrap(other).__radd__(unwrap(self))
         if isinstance(other, Concolic):
             if hasattr(other, 'isBool'): other = other.__int2__()
             # Please note that int + float -> float instead of int, so we cannot convert float to int here!
             assert not (hasattr(other, 'super')
                         and isinstance(other.super, range))
             assert not isinstance(other, str)
             # other cannot be of type "range" and "str" here, since Exception should be thrown at the statement: value = unwrap(other).__radd__(unwrap(self))
         else:
             if type(other) is bool: other = int(other)
             if type(other) is not int and type(other) is not float:
                 return ConcolicObject(
                     value
                 )  # discard the symbolic expression if it cannot match the concrete value
             other = ConcolicObject(other)
         expr = ['+', self, other]
         return ConcolicObject(value, expr)
     if op == '__eq__':
         try:
             if (value := super().__eq__(unwrap(other))) is NotImplemented:
                 raise NotImplementedError
         except:
             value = unwrap(other).__eq__(unwrap(self))
         if isinstance(other, Concolic):
             if hasattr(other, 'isBool'):
                 other = other.__int2__()
                 # Please note that (int = float) will convert int to float, so we cannot convert float to int here!
             elif (hasattr(other, 'super')
                   and isinstance(other.super, range)) or isinstance(
                       other, str):
                 return ConcolicObject(
                     value
                 )  # smtlib2 cannot compare int with range and str.
         else:
             if type(other) is bool: other = int(other)
             if type(other) is not int and type(other) is not float:
                 return ConcolicObject(
                     value
                 )  # discard the symbolic expression if it cannot match the concrete value
             other = ConcolicObject(other)
         expr = ['=', self, other]
         return ConcolicObject(value, expr)
     if op == '__floordiv__':
         # TODO: not sure in the following statement what if "other" does not support the "!=" operator.
         try:
             (other != 0).__bool__(
             )  # insert a handmade branch since a number cannot be divided by zero.
         except:
             pass
         try:
             if (value := super().__floordiv__(
                     unwrap(other))) is NotImplemented:
                 raise NotImplementedError
         except:
             value = unwrap(other).__rfloordiv__(unwrap(self))
         if isinstance(other, Concolic):
             if hasattr(other, 'isBool'):
                 other = other.__int2__()
                 # Please note that int.__floordiv__(float) will be changed to float.__rfloordiv__(int) in Python!
             elif isinstance(other, float):
                 return ConcolicObject(
                     value)  # TODO: smtlib2 cannot perform int // float
             elif isinstance(other, int) and unwrap(other) < 0:
                 return ConcolicObject(
                     value
                 )  # TODO: Currently not support the case when "other" is negative.
             assert not (hasattr(other, 'super')
                         and isinstance(other.super, range))
             assert not isinstance(other, str)
             # other cannot be of type "range" and "str" here, since Exception should be thrown at the statement: value = unwrap(other).__rfloordiv__(unwrap(self))
         else:
             if type(other) is bool: other = int(other)
             if not (type(other) is int and other > 0):
                 return ConcolicObject(
                     value
                 )  # discard the symbolic expression if it cannot match the concrete value
             other = self.__class__(other)
         expr = ['div', self, other]
         return ConcolicObject(value, expr)
     if op == '__ge__':
         try:
             if (value := super().__ge__(unwrap(other))) is NotImplemented:
                 raise NotImplementedError
         except:
             value = unwrap(other).__le__(unwrap(self))
         if isinstance(other, Concolic):
             if hasattr(other, 'isBool'): other = other.__int2__()
             # Please note that (int >= float) will convert int to float, so we cannot convert float to int here!
             assert not (hasattr(other, 'super')
                         and isinstance(other.super, range))
             assert not isinstance(other, str)
             # other cannot be of type "range" and "str" here, since Exception should be thrown at the statement: value = unwrap(other).__le__(unwrap(self))
         else:
             if type(other) is bool: other = int(other)
             if type(other) is not int and type(other) is not float:
                 return ConcolicObject(
                     value
                 )  # discard the symbolic expression if it cannot match the concrete value
             other = ConcolicObject(other)
         expr = ['>=', self, other]
         return ConcolicObject(value, expr)
     if op == '__gt__':
         try:
             if (value := super().__gt__(unwrap(other))) is NotImplemented:
                 raise NotImplementedError
         except:
             value = unwrap(other).__lt__(unwrap(self))
         if isinstance(other, Concolic):
             if hasattr(other, 'isBool'): other = other.__int2__()
             # Please note that (int > float) will convert int to float, so we cannot convert float to int here!
             assert not (hasattr(other, 'super')
                         and isinstance(other.super, range))
             assert not isinstance(other, str)
             # other cannot be of type "range" and "str" here, since Exception should be thrown at the statement: value = unwrap(other).__lt__(unwrap(self))
         else:
             if type(other) is bool: other = int(other)
             if type(other) is not int and type(other) is not float:
                 return ConcolicObject(
                     value
                 )  # discard the symbolic expression if it cannot match the concrete value
             other = ConcolicObject(other)
         expr = ['>', self, other]
         return ConcolicObject(value, expr)
     if op == '__le__':
         try:
             if (value := super().__le__(unwrap(other))) is NotImplemented:
                 raise NotImplementedError
         except:
             value = unwrap(other).__ge__(unwrap(self))
         if isinstance(other, Concolic):
             if hasattr(other, 'isBool'): other = other.__int2__()
             # Please note that (int <= float) will convert int to float, so we cannot convert float to int here!
             assert not (hasattr(other, 'super')
                         and isinstance(other.super, range))
             assert not isinstance(other, str)
             # other cannot be of type "range" and "str" here, since Exception should be thrown at the statement: value = unwrap(other).__ge__(unwrap(self))
         else:
             if type(other) is bool: other = int(other)
             if type(other) is not int and type(other) is not float:
                 return ConcolicObject(
                     value
                 )  # discard the symbolic expression if it cannot match the concrete value
             other = ConcolicObject(other)
         expr = ['<=', self, other]
         return ConcolicObject(value, expr)
     if op == '__lt__':
         try:
             if (value := super().__lt__(unwrap(other))) is NotImplemented:
                 raise NotImplementedError
         except:
             value = unwrap(other).__gt__(unwrap(self))
         if isinstance(other, Concolic):
             if hasattr(other, 'isBool'): other = other.__int2__()
             # Please note that (int < float) will convert int to float, so we cannot convert float to int here!
             assert not (hasattr(other, 'super')
                         and isinstance(other.super, range))
             assert not isinstance(other, str)
             # other cannot be of type "range" and "str" here, since Exception should be thrown at the statement: value = unwrap(other).__gt__(unwrap(self))
         else:
             if type(other) is bool: other = int(other)
             if type(other) is not int and type(other) is not float:
                 return ConcolicObject(
                     value
                 )  # discard the symbolic expression if it cannot match the concrete value
             other = ConcolicObject(other)
         expr = ['<', self, other]
         return ConcolicObject(value, expr)
     if op == '__mod__':
         # TODO: not sure in the following statement what if "other" does not support the "!=" operator.
         try:
             (other != 0).__bool__(
             )  # insert a handmade branch since a number cannot be divided by zero.
         except:
             pass
         try:
             if (value := super().__mod__(unwrap(other))) is NotImplemented:
                 raise NotImplementedError
         except:
             value = unwrap(other).__rmod__(unwrap(self))
         if isinstance(other, Concolic):
             if hasattr(other, 'isBool'):
                 other = other.__int2__()
                 # Please note that int.__mod__(float) will be changed to float.__rmod__(int) in Python!
             elif isinstance(other, float):
                 return ConcolicObject(
                     value)  # TODO: smtlib2 cannot perform int // float
             elif isinstance(other, int) and unwrap(other) < 0:
                 return ConcolicObject(
                     value
                 )  # TODO: Currently not support the case when "other" is negative.
             assert not (hasattr(other, 'super')
                         and isinstance(other.super, range))
             assert not isinstance(other, str)
             # other cannot be of type "range" and "str" here, since Exception should be thrown at the statement: value = unwrap(other).__rmod__(unwrap(self))
         else:
             if type(other) is bool: other = int(other)
             if not (type(other) is int and other > 0):
                 return ConcolicObject(
                     value
                 )  # discard the symbolic expression if it cannot match the concrete value
             other = self.__class__(other)
         expr = ['mod', self, other]
         return ConcolicObject(value, expr)
     if op == '__mul__':
         try:
             if (value := super().__mul__(unwrap(other))) is NotImplemented:
                 raise NotImplementedError
         except:
             value = unwrap(other).__rmul__(unwrap(self))
         if isinstance(other, Concolic):
             if hasattr(other, 'isBool'): other = other.__int2__()
             # Please note that int * float -> float instead of int, so we cannot convert float to int here!
             if isinstance(other, str): return other.__mul__(self)
             assert not (hasattr(other, 'super')
                         and isinstance(other.super, range))
             # other cannot be of type "range" here, since Exception should be thrown at the statement: value = unwrap(other).__rmul__(unwrap(self))
         else:
             if type(other) is str:
                 return ConcolicObject(other).__mul__(self)
             if type(other) is bool: other = int(other)
             if type(other) is not int and type(other) is not float:
                 return ConcolicObject(
                     value
                 )  # discard the symbolic expression if it cannot match the concrete value
             other = ConcolicObject(other)
         expr = ['*', self, other]
         return ConcolicObject(value, expr)
     if op == '__ne__':
         try:
             if (value := super().__ne__(unwrap(other))) is NotImplemented:
                 raise NotImplementedError
         except:
             value = unwrap(other).__ne__(unwrap(self))
         if isinstance(other, Concolic):
             if hasattr(other, 'isBool'):
                 other = other.__int2__()
                 # Please note that (int != float) will convert int to float, so we cannot convert float to int here!
             elif (hasattr(other, 'super')
                   and isinstance(other.super, range)) or isinstance(
                       other, str):
                 return ConcolicObject(
                     value
                 )  # smtlib2 cannot compare int with range and str.
         else:
             if type(other) is bool: other = int(other)
             if type(other) is not int and type(other) is not float:
                 return ConcolicObject(
                     value
                 )  # discard the symbolic expression if it cannot match the concrete value
             other = ConcolicObject(other)
         expr = ['not', ['=', self, other]]
         return ConcolicObject(value, expr)
     if op == '__radd__':
         value = super().__radd__(unwrap(other))
         if isinstance(other, Concolic):
             if hasattr(other, 'isBool'): other = other.__int2__()
             # Please note that float + int -> float instead of int, so we cannot convert float to int here!
             assert not (hasattr(other, 'super')
                         and isinstance(other.super, range))
             assert not isinstance(other, str)
             # other cannot be of type "range" and "str" here, since Exception should be thrown at the statement: value = unwrap(other).__radd__(unwrap(self))
         else:
             if type(other) is bool: other = int(other)
             if type(other) is not int and type(other) is not float:
                 return ConcolicObject(
                     value
                 )  # discard the symbolic expression if it cannot match the concrete value
             other = ConcolicObject(other)
         expr = ['+', other, self]
         return ConcolicObject(value, expr)
     if op == '__rfloordiv__':
         # TODO: not sure in the following statement what if "other" does not support the "!=" operator.
         try:
             (self != 0).__bool__(
             )  # insert a handmade branch since a number cannot be divided by zero.
         except:
             pass
         value = super().__rfloordiv__(unwrap(other))
         if isinstance(other, Concolic):
             if hasattr(other, 'isBool'): other = other.__int2__()
             elif isinstance(other, float):
                 return ConcolicObject(
                     value)  # TODO: smtlib2 cannot perform float // int
             elif isinstance(other, int) and unwrap(other) < 0:
                 return ConcolicObject(
                     value
                 )  # TODO: Currently not support the case when "other" is negative.
             assert not (hasattr(other, 'super')
                         and isinstance(other.super, range))
             assert not isinstance(other, str)
             # other cannot be of type "range" and "str" here, since Exception should be thrown at the statement: value = super().__rfloordiv__(unwrap(other))
         else:
             if type(other) is bool: other = int(other)
             if not (type(other) is int and other > 0):
                 return ConcolicObject(
                     value
                 )  # discard the symbolic expression if it cannot match the concrete value
             other = self.__class__(other)
         expr = ['div', other, self]
         return ConcolicObject(value, expr)
     if op == '__rmod__':
         # TODO: not sure in the following statement what if "other" does not support the "!=" operator.
         try:
             (self != 0).__bool__(
             )  # insert a handmade branch since a number cannot be divided by zero.
         except:
             pass
         value = super().__rmod__(unwrap(other))
         if isinstance(other, Concolic):
             if hasattr(other, 'isBool'): other = other.__int2__()
             elif isinstance(other, float):
                 return ConcolicObject(
                     value)  # TODO: smtlib2 cannot perform float // int
             elif isinstance(other, int) and unwrap(other) < 0:
                 return ConcolicObject(
                     value
                 )  # TODO: Currently not support the case when "other" is negative.
             assert not (hasattr(other, 'super')
                         and isinstance(other.super, range))
             assert not isinstance(other, str)
             # other cannot be of type "range" and "str" here, since Exception should be thrown at the statement: value = super().__rmod__(unwrap(other))
         else:
             if type(other) is bool: other = int(other)
             if not (type(other) is int and other > 0):
                 return ConcolicObject(
                     value
                 )  # discard the symbolic expression if it cannot match the concrete value
             other = self.__class__(other)
         expr = ['mod', other, self]
         return ConcolicObject(value, expr)
     if op == '__rmul__':
         value = super().__rmul__(unwrap(other))
         if isinstance(other, Concolic):
             if hasattr(other, 'isBool'): other = other.__int2__()
             # Please note that float * int -> float instead of int, so we cannot convert float to int here!
             if isinstance(other, str): return other.__mul__(self)
             assert not (hasattr(other, 'super')
                         and isinstance(other.super, range))
             # other cannot be of type "range" here, since Exception should be thrown at the statement: value = unwrap(other).__rmul__(unwrap(self))
         else:
             if type(other) is str:
                 return ConcolicObject(other).__mul__(self)
             if type(other) is bool: other = int(other)
             if type(other) is not int and type(other) is not float:
                 return ConcolicObject(
                     value
                 )  # discard the symbolic expression if it cannot match the concrete value
             other = ConcolicObject(other)
         expr = ['*', other, self]
         return ConcolicObject(value, expr)
     if op == '__rsub__':
         value = super().__rsub__(unwrap(other))
         if isinstance(other, Concolic):
             if hasattr(other, 'isBool'): other = other.__int2__()
             # Please note that float - int -> float instead of int, so we cannot convert float to int here!
             assert not (hasattr(other, 'super')
                         and isinstance(other.super, range))
             assert not isinstance(other, str)
             # other cannot be of type "range" and "str" here, since Exception should be thrown at the statement: value = super().__rsub__(unwrap(other))
         else:
             if type(other) is bool: other = int(other)
             if type(other) is not int and type(other) is not float:
                 return ConcolicObject(
                     value
                 )  # discard the symbolic expression if it cannot match the concrete value
             other = ConcolicObject(other)
         expr = ['-', other, self]
         return ConcolicObject(value, expr)
     if op == '__rtruediv__':
         # TODO: not sure in the following statement what if "other" does not support the "!=" operator.
         try:
             (self != 0).__bool__(
             )  # insert a handmade branch since a number cannot be divided by zero.
         except:
             pass
         value = super().__rtruediv__(unwrap(other))
         if isinstance(other, Concolic):
             if hasattr(other, 'isBool'): other = other.__int2__()
             # Please note that int / float -> float instead of int, so we cannot convert float to int here!
             assert not (hasattr(other, 'super')
                         and isinstance(other.super, range))
             assert not isinstance(other, str)
             # other cannot be of type "range" and "str" here, since Exception should be thrown at the statement: super().__rtruediv__(unwrap(other))
         else:
             if type(other) is bool: other = int(other)
             if type(other) is not int and type(other) is not float:
                 return ConcolicObject(
                     value
                 )  # discard the symbolic expression if it cannot match the concrete value
             other = ConcolicObject(other)
         expr = ['/', other, self]
         return ConcolicObject(value, expr)
     if op == '__sub__':
         try:
             if (value := super().__sub__(unwrap(other))) is NotImplemented:
                 raise NotImplementedError
         except:
             value = unwrap(other).__rsub__(unwrap(self))
         if isinstance(other, Concolic):
             if hasattr(other, 'isBool'): other = other.__int2__()
             # Please note that int - float -> float instead of int, so we cannot convert float to int here!
             assert not (hasattr(other, 'super')
                         and isinstance(other.super, range))
             assert not isinstance(other, str)
             # other cannot be of type "range" and "str" here, since Exception should be thrown at the statement: value = unwrap(other).__rsub__(unwrap(self))
         else:
             if type(other) is bool: other = int(other)
             if type(other) is not int and type(other) is not float:
                 return ConcolicObject(
                     value
                 )  # discard the symbolic expression if it cannot match the concrete value
             other = ConcolicObject(other)
         expr = ['-', self, other]
         return ConcolicObject(value, expr)
     if op == '__truediv__':
         # TODO: not sure in the following statement what if "other" does not support the "!=" operator.
         try:
             (other != 0).__bool__(
             )  # insert a handmade branch since a number cannot be divided by zero.
         except:
             pass
         try:
             if (value := super().__truediv__(
                     unwrap(other))) is NotImplemented:
                 raise NotImplementedError
         except:
             value = unwrap(other).__rtruediv__(unwrap(self))
         if isinstance(other, Concolic):
             if hasattr(other, 'isBool'): other = other.__int2__()
             # Please note that int / float -> float instead of int, so we cannot convert float to int here!
             assert not (hasattr(other, 'super')
                         and isinstance(other.super, range))
             assert not isinstance(other, str)
             # other cannot be of type "range" and "str" here, since Exception should be thrown at the statement: value = unwrap(other).__rtruediv__(unwrap(self))
         else:
             if type(other) is bool: other = int(other)
             if type(other) is not int and type(other) is not float:
                 return ConcolicObject(
                     value
                 )  # discard the symbolic expression if it cannot match the concrete value
             other = ConcolicObject(other)
         expr = ['/', self, other]
         return ConcolicObject(value, expr)
     raise NotImplementedError
Esempio n. 20
0
 def __rshift__(self, value,
                /):  # <slot wrapper '__rshift__' of 'int' objects>
     """Return self>>value."""
     log.debug("ConInt, __rshift__ is called")
     return ConcolicObject(super().__rshift__(unwrap(value)))
Esempio n. 21
0
    @property
    def real(self):  # <attribute 'real' of 'int' objects>
        """the real part of a complex number"""
        log.debug("ConInt, real is called")
        value = super().real
        return ConcolicObject(value, self)

    def to_bytes(self,
                 /,
                 length,
                 byteorder,
                 *,
                 signed=False):  # <method 'to_bytes' of 'int' objects> TODO
        """Return an array of bytes representing an integer."""
        log.debug("ConInt, to_bytes is called")
        return ConcolicObject(super().to_bytes(unwrap(length),
                                               unwrap(byteorder),
                                               signed=unwrap(signed)))

    ################################################################
    # Other helper methods are implemented in the following section.
    ################################################################

    def __bool2__(self):
        log.debug("ConInt, __bool2__ is called")
        value = super().__bool__()
        expr = ["not", ["=", self, "0"]]
        return ConcolicObject(value, expr)

    def __float2__(self, /):  # our version of "def __float__(self, /):"
        log.debug("ConInt, __float2__ is called")
Esempio n. 22
0
 def __ge__(self, value, /):  # <slot wrapper '__ge__' of 'range' objects>
     """Return self>=value."""
     log.debug("ConRange, __ge__ is called")
     return ConcolicObject((self.super.__ge__(unwrap(value))))