def test_bisz(self): mem = ReilMemoryEx(self.__address_size) cpu = ReilCpu(mem) instr = self.__parser.parse(["BISZ [DWORD t0, EMPTY, BIT t1]"])[0] instr.address = 0xcafecafe00 t0 = 0x12345678 cpu.registers['t0'] = t0 cpu.execute(instr) self.assertEquals(1 if t0 == 0 else 0, cpu.registers['t1'])
def test_sext(self): mem = ReilMemoryEx(self.__address_size) cpu = ReilCpu(mem) instr = self.__parser.parse(["sext [DWORD t0, EMPTY, QWORD t1]"])[0] instr.address = 0xcafecafe00 t0 = 0x12345678 cpu.registers['t0'] = -t0 & 2**32 - 1 cpu.execute(instr) self.assertEquals(-t0 & 2**64 - 1, cpu.registers['t1'])
def test_div(self): mem = ReilMemoryEx(self.__address_size) cpu = ReilCpu(mem) instr = self.__parser.parse(["div [DWORD t0, DWORD t1, DWORD t2]"])[0] instr.address = 0xcafecafe00 t0 = 0x12345678 t1 = 0x1234 cpu.registers['t0'] = t0 cpu.registers['t1'] = t1 cpu.execute(instr) self.assertEquals(t0 // t1, cpu.registers['t2'])
def test_jcc(self): mem = ReilMemoryEx(self.__address_size) cpu = ReilCpu(mem) instr = self.__parser.parse(["jcc [BIT t0, EMPTY, POINTER t1]"])[0] instr.address = 0xcafecafe00 t0 = 0x1 t1 = 0x1234567800 cpu.registers['t0'] = t0 cpu.registers['t1'] = t1 next_ip = cpu.execute(instr) self.assertEquals(t1, next_ip)
def test_stm(self): mem = ReilMemoryEx(self.__address_size) cpu = ReilCpu(mem) instr = self.__parser.parse(["stm [DWORD t0, EMPTY, DWORD t1]"])[0] instr.address = 0xcafecafe00 t0 = 0x12345678 t1 = 0x1234 cpu.registers['t0'] = t0 cpu.registers['t1'] = t1 cpu.execute(instr) self.assertEquals(t0, cpu.memory.read(t1, 4))
def test_bsh_left(self): mem = ReilMemoryEx(self.__address_size) cpu = ReilCpu(mem) instr = self.__parser.parse(["bsh [DWORD t0, DWORD t1, DWORD t2]"])[0] instr.address = 0xcafecafe00 t0 = 0x12345678 t1 = 0x8 cpu.registers['t0'] = t0 cpu.registers['t1'] = t1 cpu.execute(instr) self.assertEquals((t0 << t1) & 2**32 - 1, cpu.registers['t2'])
def test_write_read_byte_4(self): address_size = 32 memory = ReilMemoryEx(address_size) addr0 = 0x00001000 write_val = 0xdeadbeef memory.write(addr0, 32 / 8, write_val) read_val = memory.read(addr0, 32 / 8) self.assertEqual(write_val, read_val) addr1 = 0x00004000 write_val = 0xdeadbeef memory.write(addr1, 32 / 8, write_val) read_val = memory.read(addr1, 32 / 8) self.assertEqual(write_val, read_val) addrs = memory.read_inverse(0xdeadbeef, 32 / 8) self.assertEqual(addr0, addrs[0]) self.assertEqual(addr1, addrs[1])
def __init__(self, arch, cpu=None, memory=None): # Architecture information. self.__arch = arch # An instance of a ReilMemory. self.__mem = memory if memory else ReilMemoryEx( self.__arch.address_size) # An instance of a ReilCpu. self.__cpu = cpu if cpu else ReilCpu(self.__mem, arch=self.__arch) # An instance of a ReilTainter. self.__tainter = ReilEmulatorTainter(self, arch=self.__arch) # Instructions pre and post handlers. self.__instr_handler_pre = None, None self.__instr_handler_post = None, None self.__set_default_handlers()
def test_write_read_byte_4(self): address_size = 32 memory = ReilMemoryEx(address_size) addr0 = 0x00001000 write_val = 0xdeadbeef memory.write(addr0, 32 // 8, write_val) read_val = memory.read(addr0, 32 // 8) self.assertEqual(write_val, read_val) addr1 = 0x00004000 write_val = 0xdeadbeef memory.write(addr1, 32 // 8, write_val) read_val = memory.read(addr1, 32 // 8) self.assertEqual(write_val, read_val) addrs = memory.read_inverse(0xdeadbeef, 32 // 8) self.assertEqual(addr0, addrs[0]) self.assertEqual(addr1, addrs[1])