-
Notifications
You must be signed in to change notification settings - Fork 22
/
rewriter.py
110 lines (110 loc) · 3.42 KB
/
rewriter.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
# from optimizer import Optimizer
# from blockstate import MemState
# from instructions import SHA3Operation, MoveInstruction
#
# from opcodes import log_ops
#
# import sys
#
#
# def rewrite_sha3_operation(block):
# local_memory = MemState()
# instructions = block.get_instructions()
# changed = False
# for index, instruction in enumerate(instructions):
# if instruction.opcode == "SHA3":
# begin, end = instruction.reads
# if begin == 0 and not isinstance(end, str):
# addresses = range(begin, end, 32)
# items = local_memory.lookup_mapping(addresses)
# if len(items) != 0:
# changed = True
# values, indices = zip(*items)
# for i in indices:
# block.set_nop_instruction(i)
# operation = SHA3Operation(values, instruction.writes, instruction.address)
# block.set_instruction(index, operation)
# # local_memory.debug_state()
# # print(instruction)
# local_memory.add_mapping(index, instruction)
# # block.debug_block()
# return changed
#
#
# # def rewrite_log_operation(block):
# # instructions = block.get_instructions()
# # print(block.get_str_id())
# # for index, instruction in enumerate(instructions):
# # # if instruction.opcode in log_ops:
# # print(str(instruction).lower())
# # print("")
#
# # def rewrite_free_ptr(block):
# # for i, instruction in enumerate(block.get_instructions()):
# # if instruction.opcode == "MLOAD" and \
# # instruction.reads[0] == 64:
# # new_instruction = MoveInstruction("MOVE", ["$m"], instruction.writes, instruction.address)
# # block.set_instruction(i, new_instruction)
# # elif instruction.opcode == "MSTORE" and \
# # instruction.reads[0] == 64:
# # new_instruction = MoveInstruction("MOVE", [instruction.reads[1]], ["$m"], instruction.address)
# # block.set_instruction(i, new_instruction)
#
#
# def remove_nops(block):
# new_instructions = list()
# for instruction in block:
# if instruction.opcode != "NOP":
# new_instructions.append(instruction)
# block.set_instructions(new_instructions)
#
#
# class Rewriter(Optimizer):
# def __init__(self, binary):
# Optimizer.__init__(self, binary)
# for func in self.get_all_functions():
# self.__rewrite_function(func)
#
# def __rewrite_function(self, func):
# for block in func.graph.get_basic_blocks():
# self.__rewrite_instruction_block(block)
# # self.__rewrite_assert_operation(func, block)
# pass
#
# # @staticmethod
# # def __rewrite_assert_operation(func, block):
# # graph = func.graph
# # block_id = block.get_str_id()
# # suc_ids = graph.get_successor_ids(block_id)
# # if len(suc_ids) != 2 or not block.is_condition_block():
# # return
# # suc_id_0, suc_id_1 = suc_ids
# # suc_0, suc_1 = graph[suc_id_0], graph[suc_id_1]
# # if suc_1.is_abort_block():
# # suc_id_0, suc_id_1 = suc_id_1, suc_id_0
# # suc_0, suc_1 = suc_1, suc_0
# # if not suc_0.is_abort_block() or \
# # len(graph.get_predecessor_ids(suc_id_1)) != 1:
# # return
# #
# # block.insert_assert_operation()
# # graph.remove_block(suc_id_0)
#
# @staticmethod
# def __rewrite_instruction_block(block):
# rewrite_sha3_operation(block)
# remove_nops(block)
# # rewrite_free_ptr(block)
# # rewrite_log_operation(block)
#
# # for instruction in block:
#
#
# if __name__ == "__main__":
# input_file = open(sys.argv[1])
# line = input_file.readline().strip()
# if " " in line:
# line = line.split(" ")[1]
# input_file.close()
# a = Rewriter(line)
# a.debug_functions()