2015-12-15 23:05:18 +00:00
|
|
|
|
|
|
|
import re
|
|
|
|
import sys
|
|
|
|
|
2015-12-16 23:06:26 +00:00
|
|
|
REGISTERS=["%rax", "%rbx", "%rcx", "%rdx", "%rsp", "%rbp", "%rsi", "%rdi",
|
2015-12-16 23:11:18 +00:00
|
|
|
"%r8", "%r9", "%r10", "%r11", "%r12", "%r13", "%r14", "%r15"]
|
2015-12-16 00:36:23 +00:00
|
|
|
REG_STATUS_TO_COLOR = {
|
|
|
|
"insert": "green",
|
|
|
|
"change": "yellow",
|
|
|
|
"remove": "red",
|
2015-12-16 01:02:19 +00:00
|
|
|
"none": "black",
|
|
|
|
"jump": "orange"
|
2015-12-16 00:36:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
class TikzPainter:
|
|
|
|
|
|
|
|
def __init__ (self, registers=None, max_stack_size=8):
|
|
|
|
self.registers = registers if registers else REGISTERS
|
|
|
|
self.text = []
|
|
|
|
self.pos_x = 0
|
|
|
|
self.max_stack_size = max_stack_size
|
|
|
|
|
|
|
|
def addText (self, str, *args):
|
|
|
|
self.text.append(str.format(*args))
|
|
|
|
|
|
|
|
def getRegColor (self, reg_state):
|
|
|
|
return REG_STATUS_TO_COLOR[reg_state]
|
|
|
|
|
|
|
|
def drawState (self, emu, line_nr=None):
|
|
|
|
x = (self.pos_x+1)*2.5-0.5
|
|
|
|
self.pos_x += 1
|
|
|
|
|
|
|
|
# Draw register cells
|
|
|
|
pos = 0.5
|
|
|
|
for reg_name in self.registers:
|
|
|
|
pos -= 0.5
|
|
|
|
if reg_name =="":
|
|
|
|
continue
|
|
|
|
reg_state = emu.regState(reg_name)
|
|
|
|
reg_dect = "register_node, fill="+self.getRegColor(reg_state)+"!10"
|
|
|
|
self.addText("\t\\node[{}]() at ({}, {}){{{}}};\n",
|
|
|
|
reg_dect, x, pos, emu.getVal(reg_name))
|
|
|
|
|
|
|
|
# Draw stack
|
|
|
|
pos -= 1.5
|
|
|
|
base_sp = max(emu.last_sp, emu.getVal('%rsp'))
|
|
|
|
if emu.last_sp - 1 == emu.getVal('%rsp'):
|
|
|
|
pos += 0.5
|
|
|
|
for index in range(base_sp, max(-1, base_sp-self.max_stack_size),-1):
|
|
|
|
reg_state = emu.regState("m"+str(index))
|
|
|
|
reg_dect = "register_node, fill="+self.getRegColor(reg_state)+"!10"
|
|
|
|
self.addText("\t\\node[{}]() at ({}, {}){{{}}};\n",
|
|
|
|
reg_dect, x, pos, emu.stack[index])
|
|
|
|
pos -= 0.5
|
|
|
|
|
|
|
|
# Draw line signature
|
|
|
|
if line_nr == None:
|
|
|
|
return
|
|
|
|
pos = 2
|
|
|
|
signature = emu.getLineSignature(line_nr)
|
|
|
|
for token in signature:
|
|
|
|
self.addText("\t\\node[text_node]() at ({}, {}){{{}}};\n", x-1.25, pos, token)
|
|
|
|
pos -= 0.5
|
|
|
|
|
|
|
|
def drawNames (self, emu):
|
2015-12-16 01:02:19 +00:00
|
|
|
x = 0
|
2015-12-16 00:36:23 +00:00
|
|
|
pos = 0
|
|
|
|
for reg_name in self.registers:
|
|
|
|
self.addText("\t\\node[text_node]() at ({}, {}){{{}}};\n", x, pos, reg_name[1:])
|
|
|
|
pos -= 0.5
|
|
|
|
|
|
|
|
self.addText("\t\\node[text_node]() at ({}, {}){{TOS+1}};\n", x, pos-0.5)
|
|
|
|
self.addText("\t\\node[text_node]() at ({}, {}){{TOS}};\n", x, pos-1.0)
|
|
|
|
pos -= 1
|
|
|
|
for index in range(1,self.max_stack_size):
|
|
|
|
pos -= 0.5
|
|
|
|
self.addText("\t\\node[text_node]() at ({}, {}){{TOS-{}}};\n", x, pos, index)
|
|
|
|
|
|
|
|
def __str__ (self):
|
|
|
|
return "".join(self.text)
|
|
|
|
|
2015-12-15 23:05:18 +00:00
|
|
|
|
|
|
|
class Emulator:
|
|
|
|
|
|
|
|
def __init__ (self, source_text):
|
|
|
|
self.registers = {}
|
|
|
|
for reg_name in REGISTERS:
|
2015-12-16 00:36:23 +00:00
|
|
|
self.registers[reg_name] = "Junk"
|
2015-12-15 23:43:30 +00:00
|
|
|
self.stack = {}
|
2015-12-15 23:05:18 +00:00
|
|
|
self.code = []
|
|
|
|
self.labels = {}
|
|
|
|
self.changes = {}
|
|
|
|
self.status = {}
|
|
|
|
self.last_sp = 0
|
|
|
|
self.max_stack_size = 0
|
|
|
|
index = 0
|
|
|
|
for line in iter(source_text.splitlines()):
|
|
|
|
index = self.processSourceLine(line, index)
|
|
|
|
|
2015-12-16 01:09:04 +00:00
|
|
|
def setStack (self, *stack_list, **kwargs):
|
|
|
|
"""
|
|
|
|
Sets various stack elements, starting from 0 and going up.
|
|
|
|
Automatically sets rsp. This can be disabled by passing set_rsp=False.
|
|
|
|
"""
|
|
|
|
i = -1
|
2015-12-15 23:43:30 +00:00
|
|
|
for element in stack_list:
|
|
|
|
i += 1
|
2015-12-16 01:09:04 +00:00
|
|
|
self.stack[i] = element
|
|
|
|
if (not 'set_rsp' in kwargs) or kwargs['set_rsp']:
|
|
|
|
self.setRegs(rsp=i)
|
2015-12-15 23:05:18 +00:00
|
|
|
|
2015-12-15 23:43:30 +00:00
|
|
|
def setRegs (self, **reg_dict):
|
2015-12-15 23:05:18 +00:00
|
|
|
for reg_name, reg_val in reg_dict.iteritems():
|
2015-12-15 23:43:30 +00:00
|
|
|
self.registers["%"+reg_name] = reg_val
|
2015-12-15 23:05:18 +00:00
|
|
|
|
2015-12-15 23:43:30 +00:00
|
|
|
def getVal (self, val_text):
|
|
|
|
if val_text[0] == "$":
|
|
|
|
return int(val_text[1:])
|
|
|
|
elif val_text[0] == "%":
|
|
|
|
return self.registers[val_text]
|
2015-12-15 23:05:18 +00:00
|
|
|
else:
|
|
|
|
raise "NOT A VALUE! UGHGUIHGUE!"
|
|
|
|
|
|
|
|
def compareVal (self, valT1, valT2):
|
|
|
|
val1 = self.getVal(valT2)
|
|
|
|
val2 = self.getVal(valT1)
|
2015-12-16 21:20:33 +00:00
|
|
|
self.status["g"] = val1>val2
|
|
|
|
self.status["l"] = val1<val2
|
|
|
|
self.status["e"] = val1==val2
|
2015-12-15 23:05:18 +00:00
|
|
|
def changedRegisters (self, *args):
|
|
|
|
for reg, val in args:
|
|
|
|
self.changes[reg] = val
|
|
|
|
|
|
|
|
def processSourceLine(self, line_text, index):
|
|
|
|
tokens = re.findall(r'[\w%:$#]+', line_text)
|
|
|
|
if len(tokens) <= 0:
|
|
|
|
return index
|
|
|
|
if tokens[0][-1] == ':':
|
|
|
|
self.labels[tokens[0][:-1]] = index
|
|
|
|
tokens = tokens[1:]
|
|
|
|
for i in range(len(tokens)):
|
|
|
|
if tokens[i][0] == "#":
|
|
|
|
tokens = tokens[:i]
|
|
|
|
break
|
|
|
|
self.code.append(tokens)
|
|
|
|
return index + 1
|
|
|
|
|
2015-12-16 00:36:23 +00:00
|
|
|
def regState (self, reg_name):
|
2015-12-15 23:05:18 +00:00
|
|
|
if reg_name in self.changes:
|
2015-12-16 00:36:23 +00:00
|
|
|
return self.changes[reg_name]
|
2015-12-15 23:05:18 +00:00
|
|
|
else:
|
2015-12-16 00:36:23 +00:00
|
|
|
return "none"
|
2015-12-15 23:05:18 +00:00
|
|
|
|
|
|
|
def getLineSignature (self, line_nr):
|
|
|
|
return [ "\\"+token if token[0]=="$" or token[0]=="%" else token for token in self.code[line_nr] ]
|
|
|
|
|
2015-12-16 01:02:19 +00:00
|
|
|
def pushToStack (self, new_element):
|
|
|
|
self.registers['%rsp'] = self.registers['%rsp'] + 1
|
|
|
|
self.stack[self.registers['%rsp']] = new_element
|
|
|
|
self.changedRegisters(('%rsp',"change"), ("m"+str(self.registers['%rsp']),"insert"))
|
|
|
|
self.max_stack_size = max(self.registers['%rsp'], self.max_stack_size)
|
|
|
|
|
|
|
|
def popFromStack (self):
|
|
|
|
temp = self.stack[self.registers['%rsp']]
|
|
|
|
self.registers['%rsp'] -= 1
|
|
|
|
self.changedRegisters(('%rsp',"change"),("m"+str(self.registers['%rsp']+1),"remove"))
|
|
|
|
return temp
|
|
|
|
|
2015-12-15 23:43:30 +00:00
|
|
|
def iterate (self):
|
|
|
|
old_rip = self.registers['%rip']
|
|
|
|
self.last_sp = self.registers['%rsp']
|
|
|
|
if self.registers['%rip'] >= len(self.code):
|
2015-12-16 00:36:23 +00:00
|
|
|
return None
|
2015-12-15 23:43:30 +00:00
|
|
|
instruct = self.code[self.registers['%rip']]
|
|
|
|
opcode = instruct[0]
|
|
|
|
self.changes = {}
|
2015-12-16 01:02:19 +00:00
|
|
|
if opcode[:4] == "push":
|
|
|
|
self.pushToStack(self.getVal(instruct[1]))
|
|
|
|
elif opcode[:3] == "pop":
|
|
|
|
self.registers[instruct[1]] = self.popFromStack()
|
|
|
|
self.changedRegisters((instruct[1],"change"))
|
|
|
|
|
|
|
|
elif opcode[:3] == "mov":
|
2015-12-15 23:43:30 +00:00
|
|
|
self.changedRegisters((instruct[2],"change"))
|
|
|
|
self.registers[instruct[2]] = self.getVal(instruct[1])
|
2015-12-16 01:02:19 +00:00
|
|
|
elif opcode[:3] == "add":
|
2015-12-15 23:43:30 +00:00
|
|
|
self.changedRegisters((instruct[2],"change"))
|
|
|
|
self.registers[instruct[2]] = self.getVal(instruct[2]) + self.getVal(instruct[1])
|
2015-12-16 01:02:19 +00:00
|
|
|
elif opcode[:3] == "sub":
|
2015-12-15 23:43:30 +00:00
|
|
|
self.changedRegisters((instruct[2],"change"))
|
|
|
|
self.registers[instruct[2]] = self.getVal(instruct[2]) - self.getVal(instruct[1])
|
2015-12-16 01:02:19 +00:00
|
|
|
|
|
|
|
elif opcode[:3] == "cmp":
|
|
|
|
self.compareVal(instruct[1],instruct[2])
|
2015-12-16 23:06:26 +00:00
|
|
|
|
2015-12-15 23:43:30 +00:00
|
|
|
elif opcode == "jg":
|
|
|
|
if self.status["g"]:
|
|
|
|
self.registers['%rip'] = self.labels[instruct[1]]
|
2015-12-16 21:20:33 +00:00
|
|
|
elif opcode == "jl":
|
|
|
|
if self.status["l"]:
|
|
|
|
self.registers['%rip'] = self.labels[instruct[1]]
|
|
|
|
elif opcode == "je":
|
|
|
|
if self.status["e"]:
|
|
|
|
self.registers['%rip'] = self.labels[instruct[1]]
|
|
|
|
elif opcode == "jge":
|
|
|
|
if (self.status["g"] or self.status["e"]):
|
|
|
|
self.registers['%rip'] = self.labels[instruct[1]]
|
|
|
|
elif opcode == "jle":
|
|
|
|
if (self.status["l"] or self.status["e"]):
|
|
|
|
self.registers['%rip'] = self.labels[instruct[1]]
|
|
|
|
elif opcode == "jne":
|
|
|
|
if not self.status["e"]:
|
|
|
|
self.registers['%rip'] = self.labels[instruct[1]]
|
2015-12-15 23:43:30 +00:00
|
|
|
elif opcode == "jmp":
|
|
|
|
self.registers['%rip'] = self.labels[instruct[1]]
|
2015-12-15 23:05:18 +00:00
|
|
|
|
2015-12-15 23:43:30 +00:00
|
|
|
elif opcode == "call":
|
2015-12-16 01:02:19 +00:00
|
|
|
self.pushToStack(self.registers['%rip']+1)
|
2015-12-15 23:43:30 +00:00
|
|
|
self.registers['%rip'] = self.labels[instruct[1]]
|
|
|
|
elif opcode == "leave":
|
|
|
|
self.registers["%rsp"] = self.registers["%rbp"]
|
2015-12-16 01:02:19 +00:00
|
|
|
self.registers["%rbp"] = self.popFromStack()
|
2015-12-15 23:43:30 +00:00
|
|
|
elif opcode == "ret":
|
2015-12-16 01:02:19 +00:00
|
|
|
self.registers['%rip'] = self.popFromStack()
|
2015-12-15 23:05:18 +00:00
|
|
|
|
2015-12-15 23:43:30 +00:00
|
|
|
if self.registers['%rip'] == old_rip:
|
|
|
|
self.registers['%rip'] += 1
|
|
|
|
else:
|
2015-12-16 01:02:19 +00:00
|
|
|
self.changedRegisters(('%rip',"jump"))
|
2015-12-15 23:05:18 +00:00
|
|
|
|
2015-12-16 00:36:23 +00:00
|
|
|
return old_rip
|
2015-12-15 23:05:18 +00:00
|
|
|
|
2015-12-15 23:43:30 +00:00
|
|
|
def __iter__(self):
|
|
|
|
return self
|
2015-12-15 23:05:18 +00:00
|
|
|
|
2015-12-15 23:43:30 +00:00
|
|
|
def next(self):
|
2015-12-16 00:36:23 +00:00
|
|
|
output = self.iterate()
|
|
|
|
if output is None:
|
2015-12-15 23:43:30 +00:00
|
|
|
raise StopIteration()
|
|
|
|
else:
|
2015-12-16 00:36:23 +00:00
|
|
|
return output
|