def run(self): while True: if self._close.is_set(): break request = None try: request = self._rx_queue.receive(0.5) except: continue req_struct = RemoteMemoryReq.from_buffer_copy(request[0]) if Operation(req_struct.operation) == Operation.READ: self.log.debug(("Received RemoteMemoryRequest." "Read from 0x%x at 0x%x") % (req_struct.address, req_struct.pc)) MemoryForwardMsg = RemoteMemoryReadMessage( self._origin, req_struct.id, req_struct.pc, req_struct.address, req_struct.size) elif Operation(req_struct.operation) == Operation.WRITE: self.log.debug(("Received RemoteMemoryRequest." "Write to 0x%x at 0x%x") % (req_struct.address, req_struct.pc)) MemoryForwardMsg = RemoteMemoryWriteMessage( self._origin, req_struct.id, req_struct.pc, req_struct.address, req_struct.value, req_struct.size) else: raise ValueError("Received Message with unkown operation %d" % req_struct.operation) self._avatar_queue.put(MemoryForwardMsg) self._closed.set()
def _forward_hook(self, uc, access, address, size, value, user_data): """Unicorn hook for memory forwarding.""" pc = self.read_register(self.arch.pc_name) if access == unicorn.UC_MEM_READ or access == unicorn.UC_MEM_FETCH: msg = RemoteMemoryReadMessage(self._origin, 0, pc, address, size) write_back = True elif access == unicorn.UC_MEM_WRITE: msg = RemoteMemoryWriteMessage(self._origin, 0, pc, address, value, size) write_back = False else: raise ValueError( 'Forward hook with unknown access {}'.format(access)) self._avatar_queue.put(msg) value, success = self._rmp_queue.get() if not success: self.log.debug( 'Remote memory request returned 0x{:x}'.format(value)) elif write_back and not self.write_memory(address, size, value): self.log.debug('Failed to write back remote memory')