def test_inst(code, inst): code.add(inst) code.cache_code() nasm_hex_str = get_nasm_output(code, inst) corepy_hex_str = get_corepy_output(code, inst) if nasm_hex_str == None: print "*************************** NASM ERROR" print "corepy output:", corepy_hex_str printer.PrintInstructionStream(code, printer.x86_Nasm(show_epilogue = False, show_prologue = False)) return 'nasm_fail' elif nasm_hex_str == corepy_hex_str: print "PASS" return 'pass' else: #nasm_rex = int(nasm_hex_str[0:2], 16) #corepy_rex = int(corepy_hex_str[0:2], 16) #if corepy_rex - nasm_rex == 8 and (nasm_rex & 0xF0 == 0x40): # print "WARNING CorePy is enabling 64bit for this inst, NASM is not" # print "nasm output: ", nasm_hex_str # print "corepy output:", corepy_hex_str # return 'rex_pass' #else: print "*************************** ERROR" print "nasm output: ", nasm_hex_str print "corepy output:", corepy_hex_str printer.PrintInstructionStream(code, printer.x86_Nasm(show_epilogue = False, show_prologue = False)) return 'fail' return
def get_nasm_output(code, inst): """Take an instruction, and return a hex string of its encoding, as encoded by GAS""" fd = open("x86_test.s", "w") printer.PrintInstructionStream(code, printer.x86_Nasm(function_name="_start"), fd = fd) fd.close() ret = subprocess.call(["nasm", "-Ox", "x86_test.s"]) if ret != 0: return output = subprocess.Popen(["xxd", "-ps", "x86_test"], stdout=subprocess.PIPE).communicate()[0] hex = ''.join(output.splitlines()) # If the prolog/epilog change, these need to be updated startstr = "5589e5575653" stopstr = "5b5e5fc9c3" startpos = hex.find(startstr) + len(startstr) stoppos = hex.find(stopstr) return hex[startpos:stoppos]