def test_disassembler_bytes(self): evmcode = EvmBytecode(self.testcode_bytes) self.assertEqual(self.testcode, evmcode.bytecode) self.assertIsInstance(evmcode.disassemble(), EvmInstructions) self.assertIsInstance(evmcode.disassemble().assemble(), EvmBytecode) self.assertEqual(self.testcode, evmcode.disassemble().assemble().as_hexstring)
def test_disassembler_0xhexstr(self): evmcode = EvmBytecode("0x%s" % self.testcode) self.assertEqual(evmcode.bytecode, self.testcode) self.assertIsInstance(evmcode.disassemble(), EvmInstructions) self.assertIsInstance(evmcode.disassemble().assemble(), EvmBytecode) self.assertEqual(utils.strip_0x_prefix(self.testcode), evmcode.disassemble().assemble().as_hexstring)
def test_instructions_string(self): evmcode = EvmBytecode(self.testcode_bytes) evmcode_listing = evmcode.disassemble().as_string self.assertIn("PUSH1 60\n", evmcode_listing) self.assertIn("\nUNKNOWN_0x29", evmcode_listing)
def test_disassembler_noerror(self): evmcode = EvmBytecode(self.testcode) self.assertFalse(evmcode.disassemble().errors) self.assertFalse(evmcode.disassemble().assemble().errors)
def setUp(self): self.testcode = '606060405260043610610062576000357c0100000000000000000000000000000000000000000000000000000000900463ffffffff16806319e30bc7146100775780635bd74afe146101055780639df211541461017f578063b1d131bf146101ad575b6000366001919061007492919061042d565b50005b341561008257600080fd5b61008a610202565b6040518080602001828103825283818151815260200191508051906020019080838360005b838110156100ca5780820151818401526020810190506100af565b50505050905090810190601f1680156100f75780820380516001836020036101000a031916815260200191505b509250505060405180910390f35b61017d600480803573ffffffffffffffffffffffffffffffffffffffff1690602001909190803590602001908201803590602001908080601f016020809104026020016040519081016040528093929190818152602001838380828437820191505050505050919080359060200190919050506102a0565b005b6101ab600480803573ffffffffffffffffffffffffffffffffffffffff1690602001909190505061038d565b005b34156101b857600080fd5b6101c0610408565b604051808273ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff16815260200191505060405180910390f35b60018054600181600116156101000203166002900480601f0160208091040260200160405190810160405280929190818152602001828054600181600116156101000203166002900480156102985780601f1061026d57610100808354040283529160200191610298565b820191906000526020600020905b81548152906001019060200180831161027b57829003601f168201915b505050505081565b3373ffffffffffffffffffffffffffffffffffffffff166000809054906101000a900473ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff161415156102fb57600080fd5b8273ffffffffffffffffffffffffffffffffffffffff16818360405180828051906020019080838360005b83811015610341578082015181840152602081019050610326565b50505050905090810190601f16801561036e5780820380516001836020036101000a031916815260200191505b5091505060006040518083038185876187965a03f19250505050505050565b6000809054906101000a900473ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff166108fc3073ffffffffffffffffffffffffffffffffffffffff16319081150290604051600060405180830381858888f19350505050151561040557600080fd5b50565b6000809054906101000a900473ffffffffffffffffffffffffffffffffffffffff1681565b828054600181600116156101000203166002900490600052602060002090601f016020900481019282601f1061046e57803560ff191683800117855561049c565b8280016001018555821561049c579182015b8281111561049b578235825591602001919060010190610480565b5b5090506104a991906104ad565b5090565b6104cf91905b808211156104cb5760008160009055506001016104b3565b5090565b905600a165627a7a723058202592c848fd2bdbf19b6558815a8c0a67519b4ad552eb001c92109a188ef215950029' self.evmcode = EvmBytecode("0x%s" % self.testcode)
class EvmInstructionTest(unittest.TestCase): def setUp(self): self.testcode = '606060405260043610610062576000357c0100000000000000000000000000000000000000000000000000000000900463ffffffff16806319e30bc7146100775780635bd74afe146101055780639df211541461017f578063b1d131bf146101ad575b6000366001919061007492919061042d565b50005b341561008257600080fd5b61008a610202565b6040518080602001828103825283818151815260200191508051906020019080838360005b838110156100ca5780820151818401526020810190506100af565b50505050905090810190601f1680156100f75780820380516001836020036101000a031916815260200191505b509250505060405180910390f35b61017d600480803573ffffffffffffffffffffffffffffffffffffffff1690602001909190803590602001908201803590602001908080601f016020809104026020016040519081016040528093929190818152602001838380828437820191505050505050919080359060200190919050506102a0565b005b6101ab600480803573ffffffffffffffffffffffffffffffffffffffff1690602001909190505061038d565b005b34156101b857600080fd5b6101c0610408565b604051808273ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff16815260200191505060405180910390f35b60018054600181600116156101000203166002900480601f0160208091040260200160405190810160405280929190818152602001828054600181600116156101000203166002900480156102985780601f1061026d57610100808354040283529160200191610298565b820191906000526020600020905b81548152906001019060200180831161027b57829003601f168201915b505050505081565b3373ffffffffffffffffffffffffffffffffffffffff166000809054906101000a900473ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff161415156102fb57600080fd5b8273ffffffffffffffffffffffffffffffffffffffff16818360405180828051906020019080838360005b83811015610341578082015181840152602081019050610326565b50505050905090810190601f16801561036e5780820380516001836020036101000a031916815260200191505b5091505060006040518083038185876187965a03f19250505050505050565b6000809054906101000a900473ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff166108fc3073ffffffffffffffffffffffffffffffffffffffff16319081150290604051600060405180830381858888f19350505050151561040557600080fd5b50565b6000809054906101000a900473ffffffffffffffffffffffffffffffffffffffff1681565b828054600181600116156101000203166002900490600052602060002090601f016020900481019282601f1061046e57803560ff191683800117855561049c565b8280016001018555821561049c579182015b8281111561049b578235825591602001919060010190610480565b5b5090506104a991906104ad565b5090565b6104cf91905b808211156104cb5760008160009055506001016104b3565b5090565b905600a165627a7a723058202592c848fd2bdbf19b6558815a8c0a67519b4ad552eb001c92109a188ef215950029' self.evmcode = EvmBytecode("0x%s" % self.testcode) def _check_address_linear(self, instructions): assert (len(instructions) > 0) pc = instructions[0].address for instr in instructions: self.assertEqual(instr.address, pc) pc += len(instr) def _check_address_linear_getitem(self, instructions): assert (len(instructions) > 0) pc = instructions[0].address for idx in range(len(instructions)): instr = instructions[idx] self.assertEqual(instr.address, pc) pc += len(instr) def test_address_linear(self): self._check_address_linear(self.evmcode.disassemble()) def test_append(self): instructions = self.evmcode.disassemble() for _ in range(4000): instructions.append(registry.create_instruction(name="JUMPDEST")) self._check_address_linear(instructions) def test_append_check_end(self): instructions = self.evmcode.disassemble() for _ in range(4000): instructions.append(registry.create_instruction(name="JUMPDEST")) self._check_address_linear(instructions) self._check_address_linear(instructions) def test_append_check_end_getitem(self): instructions = self.evmcode.disassemble() for _ in range(4000): instructions.append(registry.create_instruction(name="JUMPDEST")) self._check_address_linear_getitem(instructions) self._check_address_linear_getitem(instructions) def test_append_check_end_no_fix_address(self): instructions = self.evmcode.disassemble() instructions._fix_addresses = False for _ in range(4000): instructions.append(registry.create_instruction(name="JUMPDEST")) error = False pc = instructions[0].address for instr in instructions: if not instr.address == pc: error = True break pc += len(instr) self.assertTrue(error) def test_insert(self): instructions = self.evmcode.disassemble() instructions.insert(0, registry.create_instruction(name="JUMPDEST")) self._check_address_linear(instructions) for idx in range(len(instructions)): instructions.insert(-idx, registry.create_instruction(name="JUMPDEST")) self._check_address_linear(instructions) for _ in range(len(instructions)): idx = random.randint(0, len(instructions) - 1) instructions.insert(idx, registry.create_instruction(name="JUMPDEST")) self._check_address_linear(instructions) def test_insert_check_end(self): instructions = self.evmcode.disassemble() instructions.insert(0, registry.create_instruction(name="JUMPDEST")) for idx in range(len(instructions)): instructions.insert(-idx, registry.create_instruction(name="JUMPDEST")) self._check_address_linear(instructions) for _ in range(len(instructions)): idx = random.randint(0, len(instructions) - 1) instructions.insert(idx, registry.create_instruction(name="JUMPDEST")) self._check_address_linear(instructions) def test_del(self): instructions = self.evmcode.disassemble() while True: length = len(instructions) del_idx = random.randint(0, length - 1) del (instructions[del_idx]) if len(instructions) <= 0: break self._check_address_linear(instructions) assert (len(instructions) == 0) def test_del_check_every_10_items(self): instructions = self.evmcode.disassemble() while True: length = len(instructions) del_idx = random.randint(0, length - 1) del (instructions[del_idx]) if length % 10 == 0: self._check_address_linear(instructions) if length <= 1: break assert (len(instructions) == 0)
def byte2opcode(self, evm_bytecode): evmcode = EvmBytecode(evm_bytecode) # can be hexstr, 0xhexstr or bytes evminstructions = evmcode.disassemble() opcode = " ".join([str(ei) for ei in evminstructions]) return opcode