/* * libbinrec: a recompiling translator for machine code * Copyright (c) 2016 Andrew Church * * This software may be copied and redistributed under certain conditions; * see the file "COPYING" in the source code distribution for details. * NO WARRANTY is provided with this software. */ #include "src/endian.h" #include "src/rtl-internal.h" #include "tests/common.h" static const binrec_setup_t setup = { .host = BINREC_ARCH_X86_64_SYSV, }; static const unsigned int host_opt = 0; static int add_rtl(RTLUnit *unit) { static uint32_t value_buf[1]; value_buf[0] = bswap_le32(0x3F800000); unit->handle->setup.guest_memory_base = value_buf; binrec_add_readonly_region(unit->handle, 0, sizeof(value_buf)); unit->handle->common_opt = BINREC_OPT_FOLD_CONSTANTS | BINREC_OPT_DSE; int reg1, reg2, reg3, reg4, reg5; EXPECT(reg1 = rtl_alloc_register(unit, RTLTYPE_ADDRESS)); EXPECT(rtl_add_insn(unit, RTLOP_LOAD_IMM, reg1, 0, 0, 0)); rtl_set_membase_pointer(unit, reg1); EXPECT(reg2 = rtl_alloc_register(unit, RTLTYPE_ADDRESS)); EXPECT(rtl_add_insn(unit, RTLOP_ADDI, reg2, reg1, 0, 0)); EXPECT(reg3 = rtl_alloc_register(unit, RTLTYPE_FLOAT32)); EXPECT(rtl_add_insn(unit, RTLOP_LOAD, reg3, reg2, 0, 0)); EXPECT(reg4 = rtl_alloc_register(unit, RTLTYPE_FLOAT32)); EXPECT(rtl_add_insn(unit, RTLOP_LOAD, reg4, reg2, 0, 0)); /* If the optimizer fails to clear reg2 from the LOAD instructions' * src1 fields, the reg4 optimization will only roll its death back * to the reg3 LOAD, and the x86 register allocator will see it as a * possible reuse target and attempt to assign a GPR to reg3 (a float * value). This MOVE will raise an assertion failure if that happens. */ EXPECT(reg5 = rtl_alloc_register(unit, RTLTYPE_FLOAT32)); EXPECT(rtl_add_insn(unit, RTLOP_MOVE, reg5, reg3, 0, 0)); EXPECT(rtl_add_insn(unit, RTLOP_NOP, 0, reg3, reg4, 0)); EXPECT(rtl_add_insn(unit, RTLOP_NOP, 0, reg5, 0, 0)); return EXIT_SUCCESS; } static const uint8_t expected_code[] = { 0x48,0x83,0xEC,0x08, // sub $8,%rsp 0xB8,0x00,0x00,0x80,0x3F, // mov $0x3F800000,%eax 0x66,0x0F,0x6E,0xC0, // movd %eax,%xmm0 0xB8,0x00,0x00,0x80,0x3F, // mov $0x3F800000,%eax 0x66,0x0F,0x6E,0xC8, // movd %eax,%xmm1 0x0F,0x28,0xD0, // movaps %xmm0,%xmm2 0x48,0x83,0xC4,0x08, // add $8,%rsp 0xC3, // ret }; static const char expected_log[] = #ifdef RTL_DEBUG_OPTIMIZE "[info] r3 loads constant value 0x3F800000 from 0x0 at 2\n" "[info] r4 loads constant value 0x3F800000 from 0x0 at 3\n" "[info] r2 no longer used, setting death = birth\n" "[info] Dropping dead store to r2 at 1\n" "[info] Killing instruction 1\n" "[info] r1 no longer used, setting death = birth\n" "[info] Killing instruction 0\n" #endif ""; /* Modified version of rtl-translate-test.i to do an RTL optimization pass * on the unit before translating it. */ #include "src/common.h" #include "src/host-x86.h" #include "src/rtl.h" #include "tests/common.h" #include "tests/log-capture.h" int main(void) { binrec_setup_t final_setup = setup; final_setup.log = log_capture; binrec_t *handle; EXPECT(handle = binrec_create_handle(&final_setup)); binrec_set_optimization_flags(handle, 0, 0, host_opt); RTLUnit *unit; EXPECT(unit = rtl_create_unit(handle)); if (add_rtl(unit) != EXIT_SUCCESS) { const int line = __LINE__ - 1; const char *log_messages = get_log_messages(); printf("%s:%d: add_rtl(unit) failed (%s)\n%s", __FILE__, line, log_messages ? "log follows" : "no errors logged", log_messages ? log_messages : ""); return EXIT_FAILURE; } if (!rtl_finalize_unit(unit)) { const char *log_messages = get_log_messages(); if (log_messages) { fputs(log_messages, stdout); } FAIL("rtl_finalize_unit(unit) was not true as expected"); } EXPECT(rtl_optimize_unit(unit, unit->handle->common_opt)); #ifdef EXPECT_TRANSLATE_FAILURE handle->code_buffer_size = 1; #else handle->code_buffer_size = sizeof(expected_code); #endif handle->code_alignment = 16; EXPECT(handle->code_buffer = binrec_code_malloc( handle, handle->code_buffer_size, handle->code_alignment)); #ifndef EXPECT_TRANSLATE_FAILURE EXPECT(host_x86_translate(handle, unit)); if (!(handle->code_len == sizeof(expected_code) && memcmp(handle->code_buffer, expected_code, sizeof(expected_code)) == 0)) { const char *log_messages = get_log_messages(); if (log_messages) { fputs(log_messages, stdout); } EXPECT_MEMEQ(handle->code_buffer, expected_code, sizeof(expected_code)); EXPECT_EQ(handle->code_len, sizeof(expected_code)); } #else EXPECT_FALSE(host_x86_translate(handle, unit)); #endif const char *log_messages = get_log_messages(); EXPECT_STREQ(log_messages, *expected_log ? expected_log : NULL); binrec_code_free(handle, handle->code_buffer); handle->code_buffer = NULL; rtl_destroy_unit(unit); binrec_destroy_handle(handle); return EXIT_SUCCESS; }