How to use the pyvex.ffi function in pyvex

To help you get started, we’ve selected a few pyvex examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github angr / angr / angr / vexer.py View on Github external
try:
            buff, size = self.mem.read_bytes_c(addr)

        except KeyError:
            if backup_state:
                buff, size = self._bytes_from_state(backup_state, addr, max_size)
                if not size:
                    raise AngrMemoryError("No bytes in memory for block starting at 0x%x." % addr)
                buff = pyvex.ffi.new("char [%d]" % size, buff)

        if size >= 0 and size < max_size and backup_state:
            # Try to read data from backup_state
            to_append, to_append_size = self._bytes_from_state(backup_state, addr + size, max_size - size)
            if to_append_size > 0:
                buff = str(pyvex.ffi.buffer(buff, size)) + to_append
                buff = pyvex.ffi.new("char [%d]" % len(buff), buff)
                size += to_append_size

        if not buff or size == 0:
            raise AngrMemoryError("No bytes in memory for block starting at 0x%x." % addr)

        # deal with thumb mode in ARM, sending an odd address and an offset
        # into the string
        byte_offset = 0
        if thumb:
            byte_offset = 1
            addr += 1

        l.debug("Creating pyvex.IRSB of arch %s at 0x%x", self.arch.name, addr)

        if self.use_cache:
github angr / angr / angr / analyses / girlscout.py View on Github external
def _calc_entropy(self, data, size=None):
        if not data:
            return 0
        entropy = 0
        if size is None: size = len(data)
        data = str(pyvex.ffi.buffer(data, size))
        for x in range(0, 256):
            p_x = float(data.count(chr(x)))/size
            if p_x > 0:
                entropy += - p_x * math.log(p_x, 2)
        return entropy
github angr / angr / angr / block.py View on Github external
self._vex = vex
        self._vex_nostmt = None
        self._capstone = None
        self.size = size
        self._collect_data_refs = collect_data_refs

        self._instructions = num_inst
        self._instruction_addrs = []

        self._parse_vex_info()

        if byte_string is None:
            if backup_state is not None:
                self._bytes = self._vex_engine._load_bytes(addr - thumb, size, state=backup_state)[0]
                if type(self._bytes) is not bytes:
                    self._bytes = bytes(pyvex.ffi.buffer(self._bytes, size))
            else:
                self._bytes = None
        elif type(byte_string) is bytes:
            if self.size is not None:
                self._bytes = byte_string[:self.size]
            else:
                self._bytes = byte_string
        else:
            # Convert bytestring to a str
            # size will ALWAYS be known at this point
            self._bytes = str(pyvex.ffi.buffer(byte_string, self.size))
github angr / angr / angr / artifacts / snippet.py View on Github external
    @property
    def bytes(self):
        bytestring = self._bytes
        if not isinstance(bytestring, str):
            bytestring = str(pyvex.ffi.buffer(bytestring, self.size))
        return bytestring
github angr / angr / angr / analyses / cfg_fast.py View on Github external
def _calc_entropy(data, size=None):
        """
        Calculate the entropy of a piece of data

        :param data: The target data to calculate entropy on
        :param size: Size of the data, Optional.
        :return: A float
        """

        if not data:
            return 0
        entropy = 0
        if size is None:
            size = len(data)

        data = str(pyvex.ffi.buffer(data, size))
        for x in xrange(0, 256):
            p_x = float(data.count(chr(x))) / size
            if p_x > 0:
                entropy += - p_x * math.log(p_x, 2)
        return entropy
github angr / angr / angr / engines / vex / lifter.py View on Github external
if p.symbolic:
                    smc = True
                else:
                    smc = claripy.is_true(p & 2 != 0)
            except: # pylint: disable=bare-except
                smc = True # I don't know why this would ever happen, we checked this right?

        if (not smc or not state) and isinstance(clemory, cle.Clemory):
            try:
                start, backer = next(clemory.backers(addr))
            except StopIteration:
                pass
            else:
                if start <= addr:
                    offset = addr - start
                    buff = pyvex.ffi.from_buffer(backer) + offset
                    size = len(backer) - offset

        # If that didn't work, try to load from the state
        if size == 0 and state:
            fallback = True
            if addr in state.memory and addr + max_size - 1 in state.memory:
                try:
                    buff = state.solver.eval(state.memory.load(addr, max_size, inspect=False), cast_to=bytes)
                    size = max_size
                    fallback = False
                except SimError:
                    l.warning("Cannot load bytes at %#x. Fallback to the slow path.", addr)

            if fallback:
                buff_lst = [ ]
                symbolic_warned = False
github angr / angr / angr / engines / vex / engine.py View on Github external
if p.symbolic:
                    smc = True
                else:
                    smc = claripy.is_true(p & 2 != 0)
            except: # pylint: disable=bare-except
                smc = True # I don't know why this would ever happen, we checked this right?

        if (not smc or not state) and isinstance(clemory, cle.Clemory):
            try:
                start, backer = next(clemory.backers(addr))
            except StopIteration:
                pass
            else:
                if start <= addr:
                    offset = addr - start
                    buff = pyvex.ffi.from_buffer(backer) + offset
                    size = len(backer) - offset

        # If that didn't work, try to load from the state
        if size == 0 and state:
            fallback = True
            if addr in state.memory and addr + max_size - 1 in state.memory:
                try:
                    buff = state.solver.eval(state.memory.load(addr, max_size, inspect=False), cast_to=bytes)
                    size = max_size
                    fallback = False
                except SimError:
                    l.warning("Cannot load bytes at %#x. Fallback to the slow path.", addr)

            if fallback:
                buff_lst = [ ]
                symbolic_warned = False
github angr / angr / angr / engines / vex / engine.py View on Github external
stop_point = self._first_stoppoint(irsb, extra_stop_points)
                    if stop_point is not None:
                        size = stop_point - addr
                        continue

                if use_cache:
                    self._block_cache[cache_key] = irsb
                return irsb

        # phase x: error handling
        except pyvex.PyVEXError as e:
            l.debug("VEX translation error at %#x", addr)
            if isinstance(buff, bytes):
                l.debug('Using bytes: %r', buff)
            else:
                l.debug("Using bytes: %r", pyvex.ffi.buffer(buff, size))
            raise SimTranslationError("Unable to translate bytecode") from e
github angr / angr / angr / vexer.py View on Github external
addr &= ~1

        # TODO: FIXME: figure out what to do if we're about to exhaust the memory
        # (we can probably figure out how many instructions we have left by talking to IDA)

        buff, size = "", 0

        try:
            buff, size = self.mem.read_bytes_c(addr)

        except KeyError:
            if backup_state:
                buff, size = self._bytes_from_state(backup_state, addr, max_size)
                if not size:
                    raise AngrMemoryError("No bytes in memory for block starting at 0x%x." % addr)
                buff = pyvex.ffi.new("char [%d]" % size, buff)

        if size >= 0 and size < max_size and backup_state:
            # Try to read data from backup_state
            to_append, to_append_size = self._bytes_from_state(backup_state, addr + size, max_size - size)
            if to_append_size > 0:
                buff = str(pyvex.ffi.buffer(buff, size)) + to_append
                buff = pyvex.ffi.new("char [%d]" % len(buff), buff)
                size += to_append_size

        if not buff or size == 0:
            raise AngrMemoryError("No bytes in memory for block starting at 0x%x." % addr)

        # deal with thumb mode in ARM, sending an odd address and an offset
        # into the string
        byte_offset = 0
        if thumb: