/* * 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 "include/binrec.h" #include "src/common.h" #include "src/guest-ppc.h" #include "src/rtl.h" #include "tests/common.h" #include "tests/execute.h" #include "tests/guest-ppc/insn/common.h" #include "tests/log-capture.h" static const uint8_t input[] = { 0x38,0x60,0x00,0x01, // li r3,1 0x38,0x60,0x00,0x00, // li r3,0 }; static const char expected[] = "[warning] Scanning terminated at 0x3 due to code range bounds\n" " 0: LOAD_ARG r1, 0\n" " 1: LOAD_ARG r2, 1\n" " 2: LOAD_IMM r3, 1\n" " 3: SET_ALIAS a2, r3\n" " 4: LOAD_IMM r4, 4\n" " 5: SET_ALIAS a1, r4\n" " 6: RETURN r1\n" "\n" "Alias 1: int32 @ 964(r1)\n" "Alias 2: int32 @ 268(r1)\n" "\n" "Block 0: --> [0,6] --> \n" ; int main(void) { void *aligned_input; ASSERT(aligned_input = malloc(sizeof(input))); memcpy(aligned_input, input, sizeof(input)); binrec_setup_t final_setup = setup; final_setup.guest_memory_base = aligned_input; final_setup.log = log_capture; binrec_t *handle; EXPECT(handle = binrec_create_handle(&final_setup)); /* Force code range end in the middle of the input. */ binrec_set_code_range(handle, 0, sizeof(input) - 5); RTLUnit *unit; EXPECT(unit = rtl_create_unit(handle)); if (!guest_ppc_translate(handle, 0, sizeof(input) - 1, unit)) { const char *log_messages = get_log_messages(); if (log_messages) { fputs(log_messages, stdout); } FAIL("guest_ppc_translate(handle, 0, 0x%X, unit) did not succeed as" " expected", (int)sizeof(input) - 1); } EXPECT(rtl_finalize_unit(unit)); const char *disassembly; EXPECT(disassembly = rtl_disassemble_unit(unit, false)); char *output; const char *log_messages = get_log_messages(); if (!log_messages) { log_messages = ""; } const int output_size = strlen(log_messages) + strlen(disassembly) + 1; ASSERT(output = malloc(output_size)); ASSERT(snprintf(output, output_size, "%s%s", log_messages, disassembly) == output_size - 1); EXPECT_STREQ(output, expected); free(output); rtl_destroy_unit(unit); binrec_destroy_handle(handle); free(aligned_input); return EXIT_SUCCESS; }