diff --git a/src/backend/x86/bal_x86_tier1_compiler.c b/src/backend/x86/bal_x86_tier1_compiler.c index d19d9e74..0cff8343 100644 --- a/src/backend/x86/bal_x86_tier1_compiler.c +++ b/src/backend/x86/bal_x86_tier1_compiler.c @@ -1533,21 +1533,86 @@ translate_ldr(bal_tier1_compiler_t *compiler, }; bal_sliding_window_push(&compiler->window, load_rn_macro); + uint64_t update_offset = 0ULL; + bool write_back = false; + if (true == is_immediate_offset) { - // imm12 is scaled by the access size (8 for 64-bit, 4 for 32-bit). const uint32_t raw_immediate = extract_operand_value(instruction, &operands[2]); - const uint32_t scale = is_64bit ? 8U : 4U; - const uint64_t byte_offset = raw_immediate * scale; - if (byte_offset != 0U) + if (12U == operands[2].bit_position && 9U == operands[2].bit_width) { - const bal_x86_macro_t add_offset_macro = { - .opcode = BAL_X86_MACRO_ADD_REGISTER_IMMEDIATE, - .destination = ASSEMBLER_TEMPORARY_REGISTER, - .immediate_or_offset = byte_offset, - }; - bal_sliding_window_push(&compiler->window, add_offset_macro); + const uint32_t index_bits = (instruction >> 10) & 0x3U; + const bool is_ldra + = 'L' == metadata->name[0] && 'D' == metadata->name[1] && 'R' == metadata->name[2] + && 'A' == metadata->name[3]; + + uint64_t byte_offset = 0ULL; + + if (true == is_ldra) + { + byte_offset = (uint64_t)raw_immediate * 8ULL; + + if (3U == index_bits) + { + update_offset = byte_offset; + write_back = true; + } + } + else + { + const uint64_t offset + = (uint64_t)(int64_t)(int32_t)((raw_immediate ^ 0x100U) - 0x100U); + + byte_offset = (3U == index_bits) ? offset : 0ULL; + + if (1U == index_bits || 3U == index_bits) + { + update_offset = offset; + write_back = true; + } + } + + if (byte_offset != 0ULL) + { + const bal_x86_macro_t add_offset_macro = { + .opcode = BAL_X86_MACRO_ADD_REGISTER_IMMEDIATE, + .destination = ASSEMBLER_TEMPORARY_REGISTER, + .immediate_or_offset = byte_offset, + }; + bal_sliding_window_push(&compiler->window, add_offset_macro); + } + } + else if (10U == operands[2].bit_position && 12U == operands[2].bit_width) + { + const uint32_t scale = 1U << ((instruction >> 30) & 0x3U); + + const uint64_t byte_offset = (uint64_t)raw_immediate * (uint64_t)scale; + + if (byte_offset != 0ULL) + { + const bal_x86_macro_t add_offset_macro = { + .opcode = BAL_X86_MACRO_ADD_REGISTER_IMMEDIATE, + .destination = ASSEMBLER_TEMPORARY_REGISTER, + .immediate_or_offset = byte_offset, + }; + bal_sliding_window_push(&compiler->window, add_offset_macro); + } + } + else + { + const uint32_t scale = true == is_64bit ? 8U : 4U; + const uint64_t byte_offset = (uint64_t)raw_immediate * (uint64_t)scale; + + if (byte_offset != 0ULL) + { + const bal_x86_macro_t add_offset_macro = { + .opcode = BAL_X86_MACRO_ADD_REGISTER_IMMEDIATE, + .destination = ASSEMBLER_TEMPORARY_REGISTER, + .immediate_or_offset = byte_offset, + }; + bal_sliding_window_push(&compiler->window, add_offset_macro); + } } } else @@ -1630,6 +1695,26 @@ translate_ldr(bal_tier1_compiler_t *compiler, #endif + if (true == write_back) + { + const bal_x86_macro_t reload_rn_macro = { + .opcode = BAL_X86_MACRO_LOAD, + .destination = BAL_X86_R10, + .immediate_or_offset = rn_offset, + }; + bal_sliding_window_push(&compiler->window, reload_rn_macro); + + if (update_offset != 0ULL) + { + const bal_x86_macro_t add_update_macro = { + .opcode = BAL_X86_MACRO_ADD_REGISTER_IMMEDIATE, + .destination = BAL_X86_R10, + .immediate_or_offset = update_offset, + }; + bal_sliding_window_push(&compiler->window, add_update_macro); + } + } + // Store the result RAX into x[Rt]. if (rt != 31U) @@ -1679,6 +1764,17 @@ translate_ldr(bal_tier1_compiler_t *compiler, }; bal_sliding_window_push(&compiler->window, store_rt_macro); } + + if (true == write_back) + { + const bal_x86_macro_t store_rn_macro = { + .opcode = BAL_X86_MACRO_STORE, + .source = BAL_X86_R10, + .immediate_or_offset = rn_offset, + }; + bal_sliding_window_push(&compiler->window, store_rn_macro); + } + reset_register_allocator(compiler); BAL_LOG_DEBUG(&bal_thread_logger, diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt index da4bab93..b1e09ea4 100644 --- a/tests/CMakeLists.txt +++ b/tests/CMakeLists.txt @@ -46,6 +46,8 @@ target_link_libraries(test_bal_assembler_sub PUBLIC BallisticTestAssemblerFixtur add_ballistic_test(test_bal_decoder_integrity unit/bal_decoder/test_bal_decoder_integrity.c) +add_ballistic_test(test_bal_tier1_ldr unit/bal_tier1/test_bal_tier1_ldr.c) + add_ballistic_test(test_bal_jit_debug_lifecycle unit/bal_jit_debug/test_bal_jit_debug_lifecycle.c) add_ballistic_test(test_bal_jit_debug_add_block unit/bal_jit_debug/test_bal_jit_debug_add_block.c) @@ -93,7 +95,6 @@ set_tests_properties(doctest PROPERTIES ENVIRONMENT "LD_LIBRARY_PATH=${LUA_LD_PATH};DYLD_LIBRARY_PATH=${LUA_DYLD_LIBRARY_PATH};DOCTEST_CC=${DOCTEST_CC};DOCTEST_CFLAGS=${DOCTEST_CFLAGS};DOCTEST_LTO=${DOCTEST_LTO};DOCTEST_LIB_DIR=${BALLISTIC_LIBRARIES_DIRECTORY}" ) - # TODO (GloriousTaco): Support IPC on Windows. if (UNIX) if (LINUX) diff --git a/tests/fuzzer/src/main.c b/tests/fuzzer/src/main.c index 8c832e5a..0369309d 100644 --- a/tests/fuzzer/src/main.c +++ b/tests/fuzzer/src/main.c @@ -148,18 +148,6 @@ main(const int argc, const char **argv) ++seeds_skipped; continue; } - if (ballistic_response.status != BAL_FUZZER_WORKER_OK) - { - BAL_LOG_ERROR(&bal_thread_logger, - "Ballistic worker reported execution error and " - "returned status code %d for seed 0x%08X.", - ballistic_response.status, - *seed_cursor); - ++errors; - ++seed_cursor; - continue; - } - if (unicorn_response.status != BAL_FUZZER_WORKER_OK) { BAL_LOG_WARN(&bal_thread_logger, @@ -171,6 +159,17 @@ main(const int argc, const char **argv) ++seed_cursor; continue; } + if (ballistic_response.status != BAL_FUZZER_WORKER_OK) + { + BAL_LOG_ERROR(&bal_thread_logger, + "Ballistic worker reported execution error and " + "returned status code %d for seed 0x%08X.", + ballistic_response.status, + *seed_cursor); + ++errors; + ++seed_cursor; + continue; + } const bal_fuzzer_comparison_result_t comparison_result = bal_fuzzer_state_compare( &unicorn_response.final_state, &ballistic_response.final_state); diff --git a/tests/unit/bal_tier1/test_bal_tier1_ldr.c b/tests/unit/bal_tier1/test_bal_tier1_ldr.c new file mode 100644 index 00000000..fcfb3725 --- /dev/null +++ b/tests/unit/bal_tier1/test_bal_tier1_ldr.c @@ -0,0 +1,318 @@ +#include "bal_engine.h" +#include "bal_engine_flags.h" +#include "bal_memory.h" +#include "unity.h" +#include + +#define ARM64_RET_ENCODING 0xD65F03C0U +#define TEST_GUEST_MEMORY_SIZE 4096U + +#define LDR_X1_X0_P255_PRE 0xF84FFC01U +#define LDR_X1_X0_P255_POST 0xF84FF401U +#define LDR_X1_X0_M8_PRE 0xF85F8C01U +#define LDR_W1_X0_M4_POST 0xB85FC401U +#define LDR_W1_X0_P252_PRE 0xB84FCC01U +#define LDRAA_X1_X0_PRE 0xF82FFC01U +#define LDRAB_X1_X0_PRE 0xF8AFFC01U +#define LDRAA_X1_X0_POST 0xF82FF401U +#define LDRAA_X1_X0_ZERO_PRE 0xF8200C01U +#define LDRB_W1_X0_P255_POST 0x384FF401U +#define LDRH_W1_X0_P248_PRE 0x784F8C01U +#define LDRSW_X1_X0_P255_POST 0xB88FF401U +#define LDR_X0_X0_P255_PRE 0xF84FFC00U +#define LDRB_W1_X0_U1023 0x395FFC01U +#define LDRH_W1_X0_U1020 0x794FF801U +#define LDR_X1_X0_U8 0xF9400401U +#define LDR_W1_X0_U4 0xB9400401U + +static uint32_t bal_test_guest_memory[TEST_GUEST_MEMORY_SIZE / sizeof(uint32_t)]; + +static void +set_u64(const uint32_t byte_address, const uint64_t value) +{ + bal_test_guest_memory[byte_address / 4U] = (uint32_t)value; + bal_test_guest_memory[byte_address / 4U + 1U] = (uint32_t)(value >> 32); +} + +static void +set_u32(const uint32_t byte_address, const uint32_t value) +{ + bal_test_guest_memory[byte_address / 4U] = value; +} + +void +setUp(void) +{ + (void)memset(bal_test_guest_memory, 0, sizeof(bal_test_guest_memory)); + + set_u64(0x3F8U, 0x3333333333333333ULL); + set_u64(0x400U, 0x8765432112345678ULL); + set_u64(0x408U, 0x5555555555555555ULL); + set_u64(0x4F8U, 0x6666666666666666ULL); + set_u32(0x4FCU, 0x2468ACE1U); + set_u64(0x500U, 0x0000000012345678ULL); + set_u64(0xBF8U, 0xF300F300F300F300ULL); + set_u32(0x8FCU, 0xAB112233U); +} + +void +tearDown(void) +{ +} + +static void +run_single_guest_instruction(const uint32_t instruction, bal_cpu_t *BAL_RESTRICT cpu) +{ + bal_test_guest_memory[0] = instruction; + bal_test_guest_memory[1] = ARM64_RET_ENCODING; + + bal_allocator_t allocator = {}; + bal_allocator_default_init(&allocator); + + bal_memory_interface_t memory_interface = {}; + TEST_ASSERT_EQUAL(BAL_SUCCESS, + bal_flat_translation_interface_init(&allocator, + &memory_interface, + bal_test_guest_memory, + sizeof(bal_test_guest_memory))); + + cpu->pc = 0ULL; + + bal_engine_t engine = {}; + TEST_ASSERT_EQUAL(BAL_SUCCESS, bal_engine_init(&engine, cpu, &allocator, &memory_interface)); + + engine.flags |= BAL_ENGINE_FLAG_SINGLE_STEP; + + const bal_error_t run_status = bal_engine_run_thread(&engine); + + bal_engine_destroy(&engine); + (void)bal_flat_translation_interface_destroy(&allocator, &memory_interface); + + TEST_ASSERT_EQUAL(BAL_SUCCESS, run_status); +} + +static void +test_BalTier1Ldr_PostIndexLoadsAtBaseThenUpdatesIt(void) +{ + bal_cpu_t cpu = {}; + cpu.x[0] = 0x400ULL; + cpu.x[31] = 0xCAFE000000000000ULL; + + run_single_guest_instruction(LDR_X1_X0_P255_POST, &cpu); + + TEST_ASSERT_EQUAL_HEX64(0x8765432112345678ULL, cpu.x[1]); + TEST_ASSERT_EQUAL_HEX64(0x4FFULL, cpu.x[0]); + TEST_ASSERT_EQUAL_HEX64(0xCAFE000000000000ULL, cpu.x[31]); +} + +static void +test_BalTier1Ldr_PreIndexUpdatesItAndLoadsAtNewAddress(void) +{ + bal_cpu_t cpu = {}; + cpu.x[0] = 0x400ULL; + + run_single_guest_instruction(LDR_X1_X0_P255_PRE, &cpu); + + TEST_ASSERT_EQUAL_HEX64(0x0000001234567824ULL, cpu.x[1]); + TEST_ASSERT_EQUAL_HEX64(0x4FFULL, cpu.x[0]); +} + +static void +test_BalTier1Ldr_PreIndexSignExtendsNegativeImmediate(void) +{ + bal_cpu_t cpu = {}; + cpu.x[0] = 0x400ULL; + + run_single_guest_instruction(LDR_X1_X0_M8_PRE, &cpu); + + TEST_ASSERT_EQUAL_HEX64(0x3333333333333333ULL, cpu.x[1]); + TEST_ASSERT_EQUAL_HEX64(0x3F8ULL, cpu.x[0]); +} + +static void +test_BalTier1Ldr_PostIndexNegativeImmediateStillWritesBackBaseMinusOffset(void) +{ + bal_cpu_t cpu = {}; + cpu.x[0] = 0x400ULL; + + run_single_guest_instruction(LDR_W1_X0_M4_POST, &cpu); + + TEST_ASSERT_EQUAL_HEX64(0x12345678ULL, cpu.x[1]); + TEST_ASSERT_EQUAL_HEX64(0x3FCULL, cpu.x[0]); +} + +static void +test_BalTier1Ldr_PreIndex32BitLoadsAtBasePlusOffset(void) +{ + bal_cpu_t cpu = {}; + cpu.x[0] = 0x400ULL; + + run_single_guest_instruction(LDR_W1_X0_P252_PRE, &cpu); + + TEST_ASSERT_EQUAL_HEX64(0x2468ACE1ULL, cpu.x[1]); + TEST_ASSERT_EQUAL_HEX64(0x4FCULL, cpu.x[0]); +} + +static void +test_BalTier1Ldr_LdraaPreIndexScalesByEightAndWritesBack(void) +{ + bal_cpu_t cpu = {}; + cpu.x[0] = 0x400ULL; + + run_single_guest_instruction(LDRAA_X1_X0_PRE, &cpu); + + TEST_ASSERT_EQUAL_HEX64(0xF300F300F300F300ULL, cpu.x[1]); + TEST_ASSERT_EQUAL_HEX64(0xBF8ULL, cpu.x[0]); +} + +static void +test_BalTier1Ldr_LdrabPreIndexScalesByEightAndWritesBack(void) +{ + bal_cpu_t cpu = {}; + cpu.x[0] = 0x400ULL; + + run_single_guest_instruction(LDRAB_X1_X0_PRE, &cpu); + + TEST_ASSERT_EQUAL_HEX64(0xF300F300F300F300ULL, cpu.x[1]); + TEST_ASSERT_EQUAL_HEX64(0xBF8ULL, cpu.x[0]); +} + +static void +test_BalTier1Ldr_LdraaPostIndexLeavesBaseRegisterUntouched(void) +{ + bal_cpu_t cpu = {}; + cpu.x[0] = 0x400ULL; + + run_single_guest_instruction(LDRAA_X1_X0_POST, &cpu); + + TEST_ASSERT_EQUAL_HEX64(0xF300F300F300F300ULL, cpu.x[1]); + TEST_ASSERT_EQUAL_HEX64(0x400ULL, cpu.x[0]); +} + +static void +test_BalTier1Ldr_LdraaPreIndexZeroImmediateWritesBackBase(void) +{ + bal_cpu_t cpu = {}; + cpu.x[0] = 0x400ULL; + + run_single_guest_instruction(LDRAA_X1_X0_ZERO_PRE, &cpu); + + TEST_ASSERT_EQUAL_HEX64(0x8765432112345678ULL, cpu.x[1]); + TEST_ASSERT_EQUAL_HEX64(0x400ULL, cpu.x[0]); +} + +static void +test_BalTier1Ldr_UnscaledBytePostIndexWritesBackSignedOffset(void) +{ + bal_cpu_t cpu = {}; + cpu.x[0] = 0x400ULL; + + run_single_guest_instruction(LDRB_W1_X0_P255_POST, &cpu); + + TEST_ASSERT_EQUAL_HEX64(0x78ULL, cpu.x[1]); + TEST_ASSERT_EQUAL_HEX64(0x4FFULL, cpu.x[0]); +} + +static void +test_BalTier1Ldr_UnscaledHalfwordPreIndexWritesBackSignedOffset(void) +{ + bal_cpu_t cpu = {}; + cpu.x[0] = 0x400ULL; + + run_single_guest_instruction(LDRH_W1_X0_P248_PRE, &cpu); + + TEST_ASSERT_EQUAL_HEX64(0x6666ULL, cpu.x[1]); + TEST_ASSERT_EQUAL_HEX64(0x4F8ULL, cpu.x[0]); +} + +static void +test_BalTier1Ldr_UnscaledSignedWordPostIndexWritesBackSignedOffset(void) +{ + bal_cpu_t cpu = {}; + cpu.x[0] = 0x500ULL; + + run_single_guest_instruction(LDRSW_X1_X0_P255_POST, &cpu); + + TEST_ASSERT_EQUAL_HEX64(0x0000000012345678ULL, cpu.x[1]); + TEST_ASSERT_EQUAL_HEX64(0x5FFULL, cpu.x[0]); +} + +static void +test_BalTier1Ldr_WriteBackWinsWhenDestinationMatchesBase(void) +{ + bal_cpu_t cpu = {}; + cpu.x[0] = 0x400ULL; + + run_single_guest_instruction(LDR_X0_X0_P255_PRE, &cpu); + + TEST_ASSERT_EQUAL_HEX64(0x4FFULL, cpu.x[0]); +} + +static void +test_BalTier1Ldr_ByteUnsignedOffsetScalesByOne(void) +{ + bal_cpu_t cpu = {}; + cpu.x[0] = 0x100ULL; + + run_single_guest_instruction(LDRB_W1_X0_U1023, &cpu); + + TEST_ASSERT_EQUAL_HEX64(0xABULL, cpu.x[1]); + TEST_ASSERT_EQUAL_HEX64(0x100ULL, cpu.x[0]); +} + +static void +test_BalTier1Ldr_HalfwordUnsignedOffsetScalesByTwo(void) +{ + bal_cpu_t cpu = {}; + cpu.x[0] = 0x100ULL; + + run_single_guest_instruction(LDRH_W1_X0_U1020, &cpu); + + TEST_ASSERT_EQUAL_HEX64(0x2233ULL, cpu.x[1]); + TEST_ASSERT_EQUAL_HEX64(0x100ULL, cpu.x[0]); +} + +static void +test_BalTier1Ldr_WordUnsignedOffsetScalesByAccessSize(void) +{ + bal_cpu_t cpu = {}; + cpu.x[0] = 0x400ULL; + + run_single_guest_instruction(LDR_X1_X0_U8, &cpu); + + TEST_ASSERT_EQUAL_HEX64(0x5555555555555555ULL, cpu.x[1]); + TEST_ASSERT_EQUAL_HEX64(0x400ULL, cpu.x[0]); + + bal_cpu_t cpu32 = {}; + cpu32.x[0] = 0x400ULL; + + run_single_guest_instruction(LDR_W1_X0_U4, &cpu32); + + TEST_ASSERT_EQUAL_HEX64(0x87654321ULL, cpu32.x[1]); + TEST_ASSERT_EQUAL_HEX64(0x400ULL, cpu32.x[0]); +} + +int +main(void) +{ + UNITY_BEGIN(); + + RUN_TEST(test_BalTier1Ldr_PostIndexLoadsAtBaseThenUpdatesIt); + RUN_TEST(test_BalTier1Ldr_PreIndexUpdatesItAndLoadsAtNewAddress); + RUN_TEST(test_BalTier1Ldr_PreIndexSignExtendsNegativeImmediate); + RUN_TEST(test_BalTier1Ldr_PostIndexNegativeImmediateStillWritesBackBaseMinusOffset); + RUN_TEST(test_BalTier1Ldr_PreIndex32BitLoadsAtBasePlusOffset); + RUN_TEST(test_BalTier1Ldr_LdraaPreIndexScalesByEightAndWritesBack); + RUN_TEST(test_BalTier1Ldr_LdrabPreIndexScalesByEightAndWritesBack); + RUN_TEST(test_BalTier1Ldr_LdraaPostIndexLeavesBaseRegisterUntouched); + RUN_TEST(test_BalTier1Ldr_LdraaPreIndexZeroImmediateWritesBackBase); + RUN_TEST(test_BalTier1Ldr_UnscaledBytePostIndexWritesBackSignedOffset); + RUN_TEST(test_BalTier1Ldr_UnscaledHalfwordPreIndexWritesBackSignedOffset); + RUN_TEST(test_BalTier1Ldr_UnscaledSignedWordPostIndexWritesBackSignedOffset); + RUN_TEST(test_BalTier1Ldr_WriteBackWinsWhenDestinationMatchesBase); + RUN_TEST(test_BalTier1Ldr_ByteUnsignedOffsetScalesByOne); + RUN_TEST(test_BalTier1Ldr_HalfwordUnsignedOffsetScalesByTwo); + RUN_TEST(test_BalTier1Ldr_WordUnsignedOffsetScalesByAccessSize); + + return UNITY_END(); +}