From dda8cb8e0e2c5615617ca6d78ab7a74561da3035 Mon Sep 17 00:00:00 2001 From: bluetoe-99 <102478736+bluetoe-99@users.noreply.github.com> Date: Thu, 29 Oct 2026 00:47:30 -0400 Subject: [PATCH 1/2] jit: translate ADD/SUB (shifted register) second operand The add/subtract (shifted register) encodings were rejected whenever the shift amount was non-zero, so ADD Xd, Xn, Xm, LSL #s - one of the most common address-arithmetic forms in real AArch64 code - never compiled, and shifted SUB, CMP and CMN failed the whole block too. Fold the shifted operand into a scratch copy of Rm (the idiom translate_and_reg uses) so the guest Rm mapping survives. For the rd == 31 flags-only case the arithmetic folds into Rn's host copy and its mapping is invalidated, the same clobber idiom the existing rd == rm subtraction collision uses. 32-bit W shifted operands (the shift macros do not perform 32-bit windowing) and ROR/reserved shift types stay cleanly rejected. Signed-off-by: bluetoe-99 <102478736+bluetoe-99@users.noreply.github.com> --- src/backend/x86/bal_x86_tier1_compiler.c | 61 ++++++++++++++++++++---- 1 file changed, 52 insertions(+), 9 deletions(-) diff --git a/src/backend/x86/bal_x86_tier1_compiler.c b/src/backend/x86/bal_x86_tier1_compiler.c index d19d9e74..2bcb27ee 100644 --- a/src/backend/x86/bal_x86_tier1_compiler.c +++ b/src/backend/x86/bal_x86_tier1_compiler.c @@ -1045,10 +1045,12 @@ translate_add_sub_reg(bal_tier1_compiler_t *BAL_RESTRICT com const uint8_t rm = (uint8_t)extract_operand_value(instruction, &operand_cursor[3]); const uint32_t shift_type = extract_operand_value(instruction, &operand_cursor[4]); - if (BAL_UNLIKELY(shift_amount != 0)) + const bool is_32bit = BAL_OPERAND_TYPE_REGISTER_32 == operand_cursor[0].type; + + if (BAL_UNLIKELY(is_32bit && 0 != shift_amount)) { BAL_LOG_ERROR(&bal_thread_logger, - "Aborting function: Tier 1 does not support shift amounts != 0 yet: %s", + "Aborting function: Tier 1 does not support 32-bit shifted operands: %s", metadata->name); compiler->status = BAL_ERROR_UNKNOWN_INSTRUCTION; return; @@ -1072,15 +1074,51 @@ translate_add_sub_reg(bal_tier1_compiler_t *BAL_RESTRICT com bal_x86_register_t x86_rm = allocate_x86_register(compiler, rm, skip_load_rm); bal_x86_register_t x86_rd; - if (true == is_discarded_result) + if (0 != shift_amount) { - x86_rd = BAL_X86_REGISTER_DISCARD_RESULT; - const bal_x86_macro_t mov_macro = { + const bal_x86_macro_t copy_macro = { .opcode = BAL_X86_MACRO_MOV_REGISTER_REGISTER, - .destination = x86_rd, - .source = x86_rn, + .destination = BAL_X86_REGISTER_DISCARD_RESULT, + .source = x86_rm, }; - bal_sliding_window_push(&compiler->window, mov_macro); + bal_sliding_window_push(&compiler->window, copy_macro); + + bal_x86_macro_opcode_t shift_opcode = BAL_X86_MACRO_SHL_REGISTER_IMMEDIATE; + + if (0b01 == shift_type) + { + shift_opcode = BAL_X86_MACRO_SHR_REGISTER_IMMEDIATE; + } + else if (0b10 == shift_type) + { + shift_opcode = BAL_X86_MACRO_SAR_REGISTER_IMMEDIATE; + } + + const bal_x86_macro_t shift_macro = { + .opcode = shift_opcode, + .destination = BAL_X86_REGISTER_DISCARD_RESULT, + .immediate_or_offset = shift_amount, + }; + bal_sliding_window_push(&compiler->window, shift_macro); + x86_rm = BAL_X86_REGISTER_DISCARD_RESULT; + } + + if (true == is_discarded_result) + { + if (0 != shift_amount) + { + x86_rd = x86_rn; + } + else + { + x86_rd = BAL_X86_REGISTER_DISCARD_RESULT; + const bal_x86_macro_t mov_macro = { + .opcode = BAL_X86_MACRO_MOV_REGISTER_REGISTER, + .destination = x86_rd, + .source = x86_rn, + }; + bal_sliding_window_push(&compiler->window, mov_macro); + } } else { @@ -1131,7 +1169,6 @@ translate_add_sub_reg(bal_tier1_compiler_t *BAL_RESTRICT com } } - const bool is_32bit = BAL_OPERAND_TYPE_REGISTER_32 == operand_cursor[0].type; bal_x86_macro_opcode_t arithmetic_opcode; if (true == is_sub) @@ -1152,6 +1189,12 @@ translate_add_sub_reg(bal_tier1_compiler_t *BAL_RESTRICT com }; bal_sliding_window_push(&compiler->window, arithmetic_macro); + if (true == is_discarded_result && 0 != shift_amount) + { + compiler->arm_to_x86[rn] = -1; + compiler->x86_to_arm[x86_rn] = -1; + } + if (true == is_setting_flags) { const bal_x86_macro_t set_n_macro = { From 5f93db48923a8c384265aef66879378e2f77fcd9 Mon Sep 17 00:00:00 2001 From: bluetoe-99 <102478736+bluetoe-99@users.noreply.github.com> Date: Thu, 29 Oct 2026 00:47:30 -0400 Subject: [PATCH 2/2] tests: add Tier 1 ADD/SUB (shifted register) translation tests Twelve engine-level tests: LSL/LSR/ASR shifted operands including LSL #63, the rd == rm and rd == rn aliases, shifted CMP/CMN flag state, no-clobber sentinels proving the scratch folding leaves Rd, Rn and the stack pointer untouched, and two unshifted regressions. Signed-off-by: bluetoe-99 <102478736+bluetoe-99@users.noreply.github.com> --- tests/CMakeLists.txt | 3 +- tests/unit/bal_tier1/test_bal_tier1_add_sub.c | 261 ++++++++++++++++++ 2 files changed, 263 insertions(+), 1 deletion(-) create mode 100644 tests/unit/bal_tier1/test_bal_tier1_add_sub.c diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt index da4bab93..11f2b324 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_add_sub unit/bal_tier1/test_bal_tier1_add_sub.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/unit/bal_tier1/test_bal_tier1_add_sub.c b/tests/unit/bal_tier1/test_bal_tier1_add_sub.c new file mode 100644 index 00000000..009fb154 --- /dev/null +++ b/tests/unit/bal_tier1/test_bal_tier1_add_sub.c @@ -0,0 +1,261 @@ +#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 ADD_X8_X9_X10_LSL4 0x8B0A1128U +#define ADD_X8_X9_X10_LSR4 0x8B4A1128U +#define ADD_X8_X9_X10_ASR4 0x8B8A1128U +#define ADD_X8_X9_X10_LSL63 0x8B0AFD28U +#define ADD_X8_X9_X8_LSL4 0x8B081128U +#define ADD_X8_X8_X10_LSL4 0x8B0A1108U +#define ADD_X8_X9_X10 0x8B0A0128U +#define SUB_X8_X9_X10_LSL4 0xCB0A1128U +#define SUB_X8_X9_X8_LSL4 0xCB081128U +#define CMP_X8_X9_LSL4 0xEB0A113FU +#define CMP_X8_X9 0xEB0A013FU +#define CMN_X8_X9_ASR4 0xAB8A113FU + +static uint32_t bal_test_guest_memory[TEST_GUEST_MEMORY_SIZE / sizeof(uint32_t)]; + +void +setUp(void) +{ + (void)memset(bal_test_guest_memory, 0, sizeof(bal_test_guest_memory)); +} + +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_BalTier1AddSub_AddsLeftShiftedSecondSource(void) +{ + bal_cpu_t cpu = {}; + cpu.x[8] = 0x0000000000000000ULL; + cpu.x[9] = 0x0000000000001000ULL; + cpu.x[10] = 0x00000000100000FFULL; + + run_single_guest_instruction(ADD_X8_X9_X10_LSL4, &cpu); + + TEST_ASSERT_EQUAL_HEX64(0x0000000100001FF0ULL, cpu.x[8]); + TEST_ASSERT_EQUAL_HEX64(0x00000000100000FFULL, cpu.x[10]); +} + +static void +test_BalTier1AddSub_AddsLogicalRightShiftedSecondSource(void) +{ + bal_cpu_t cpu = {}; + cpu.x[9] = 0x0000000000000001ULL; + cpu.x[10] = 0x0000000000000FF0ULL; + + run_single_guest_instruction(ADD_X8_X9_X10_LSR4, &cpu); + + TEST_ASSERT_EQUAL_HEX64(0x0000000000000100ULL, cpu.x[8]); +} + +static void +test_BalTier1AddSub_AddsArithmeticShiftedSecondSource(void) +{ + bal_cpu_t cpu = {}; + cpu.x[9] = 0x0000000000000100ULL; + cpu.x[10] = 0xFFFFFFFFFFFFFFF0ULL; + + run_single_guest_instruction(ADD_X8_X9_X10_ASR4, &cpu); + + TEST_ASSERT_EQUAL_HEX64(0x00000000000000FFULL, cpu.x[8]); +} + +static void +test_BalTier1AddSub_AddsLeftShiftedBySixtyThree(void) +{ + bal_cpu_t cpu = {}; + cpu.x[9] = 0x0000000000000001ULL; + cpu.x[10] = 0x0000000000000001ULL; + + run_single_guest_instruction(ADD_X8_X9_X10_LSL63, &cpu); + + TEST_ASSERT_EQUAL_HEX64(0x8000000000000001ULL, cpu.x[8]); +} + +static void +test_BalTier1AddSub_DestinationMatchingSecondSourceUsesOldValue(void) +{ + bal_cpu_t cpu = {}; + cpu.x[8] = 0x0000000000000001ULL; + cpu.x[9] = 0x0000000000000010ULL; + + run_single_guest_instruction(ADD_X8_X9_X8_LSL4, &cpu); + + TEST_ASSERT_EQUAL_HEX64(0x0000000000000020ULL, cpu.x[8]); +} + +static void +test_BalTier1AddSub_DestinationMatchingFirstSource(void) +{ + bal_cpu_t cpu = {}; + cpu.x[8] = 0x0000000000000010ULL; + cpu.x[10] = 0x0000000000000001ULL; + + run_single_guest_instruction(ADD_X8_X8_X10_LSL4, &cpu); + + TEST_ASSERT_EQUAL_HEX64(0x0000000000000020ULL, cpu.x[8]); +} + +static void +test_BalTier1AddSub_SubtractsLeftShiftedSecondSource(void) +{ + bal_cpu_t cpu = {}; + cpu.x[9] = 0x0000000000001000ULL; + cpu.x[10] = 0x00000000000000FFULL; + + run_single_guest_instruction(SUB_X8_X9_X10_LSL4, &cpu); + + TEST_ASSERT_EQUAL_HEX64(0x0000000000000010ULL, cpu.x[8]); +} + +static void +test_BalTier1AddSub_SubtractionDestinationMatchingSecondSourceUsesOldValue(void) +{ + bal_cpu_t cpu = {}; + cpu.x[8] = 0x0000000000000010ULL; + cpu.x[9] = 0x0000000000000120ULL; + + run_single_guest_instruction(SUB_X8_X9_X8_LSL4, &cpu); + + TEST_ASSERT_EQUAL_HEX64(0x0000000000000020ULL, cpu.x[8]); +} + +static void +test_BalTier1AddSub_CompareSetsFlagsFromShiftedOperand(void) +{ + bal_cpu_t cpu = {}; + cpu.x[8] = 0xDEADBEEFDEADBEEFULL; + cpu.x[9] = 0x0000000000000010ULL; + cpu.x[10] = 0x0000000000000001ULL; + cpu.x[31] = 0xCAFE000000000000ULL; + + run_single_guest_instruction(CMP_X8_X9_LSL4, &cpu); + + TEST_ASSERT_EQUAL_UINT8(0U, cpu.flag_n); + TEST_ASSERT_EQUAL_UINT8(1U, cpu.flag_z); + TEST_ASSERT_EQUAL_UINT8(1U, cpu.flag_c); + TEST_ASSERT_EQUAL_UINT8(0U, cpu.flag_v); + + TEST_ASSERT_EQUAL_HEX64(0xDEADBEEFDEADBEEFULL, cpu.x[8]); + TEST_ASSERT_EQUAL_HEX64(0x0000000000000010ULL, cpu.x[9]); + TEST_ASSERT_EQUAL_HEX64(0x0000000000000001ULL, cpu.x[10]); + TEST_ASSERT_EQUAL_HEX64(0xCAFE000000000000ULL, cpu.x[31]); +} + +static void +test_BalTier1AddSub_CompareNegateSetsFlagsFromShiftedOperand(void) +{ + bal_cpu_t cpu = {}; + cpu.x[8] = 0xDEADBEEFDEADBEEFULL; + cpu.x[9] = 0xFFFFFFFFFFFFFFF0ULL; + cpu.x[10] = 0xFFFFFFFFFFFFFFF0ULL; + cpu.x[31] = 0xCAFE000000000000ULL; + + run_single_guest_instruction(CMN_X8_X9_ASR4, &cpu); + + TEST_ASSERT_EQUAL_UINT8(1U, cpu.flag_n); + TEST_ASSERT_EQUAL_UINT8(0U, cpu.flag_z); + TEST_ASSERT_EQUAL_UINT8(1U, cpu.flag_c); + TEST_ASSERT_EQUAL_UINT8(0U, cpu.flag_v); + + TEST_ASSERT_EQUAL_HEX64(0xDEADBEEFDEADBEEFULL, cpu.x[8]); + TEST_ASSERT_EQUAL_HEX64(0xFFFFFFFFFFFFFFF0ULL, cpu.x[9]); + TEST_ASSERT_EQUAL_HEX64(0xFFFFFFFFFFFFFFF0ULL, cpu.x[10]); + TEST_ASSERT_EQUAL_HEX64(0xCAFE000000000000ULL, cpu.x[31]); +} + +static void +test_BalTier1AddSub_UnshiftedAddStillWorks(void) +{ + bal_cpu_t cpu = {}; + cpu.x[9] = 0x0000000000000010ULL; + cpu.x[10] = 0x0000000000000002ULL; + + run_single_guest_instruction(ADD_X8_X9_X10, &cpu); + + TEST_ASSERT_EQUAL_HEX64(0x0000000000000012ULL, cpu.x[8]); +} + +static void +test_BalTier1AddSub_UnshiftedCompareStillWorks(void) +{ + bal_cpu_t cpu = {}; + cpu.x[8] = 0xDEADBEEFDEADBEEFULL; + cpu.x[9] = 0x0000000000000010ULL; + cpu.x[10] = 0x0000000000000020ULL; + cpu.x[31] = 0xCAFE000000000000ULL; + + run_single_guest_instruction(CMP_X8_X9, &cpu); + + TEST_ASSERT_EQUAL_UINT8(1U, cpu.flag_n); + TEST_ASSERT_EQUAL_UINT8(0U, cpu.flag_z); + TEST_ASSERT_EQUAL_UINT8(0U, cpu.flag_c); + TEST_ASSERT_EQUAL_UINT8(0U, cpu.flag_v); + + TEST_ASSERT_EQUAL_HEX64(0xDEADBEEFDEADBEEFULL, cpu.x[8]); + TEST_ASSERT_EQUAL_HEX64(0x0000000000000010ULL, cpu.x[9]); + TEST_ASSERT_EQUAL_HEX64(0xCAFE000000000000ULL, cpu.x[31]); +} + +int +main(void) +{ + UNITY_BEGIN(); + + RUN_TEST(test_BalTier1AddSub_AddsLeftShiftedSecondSource); + RUN_TEST(test_BalTier1AddSub_AddsLogicalRightShiftedSecondSource); + RUN_TEST(test_BalTier1AddSub_AddsArithmeticShiftedSecondSource); + RUN_TEST(test_BalTier1AddSub_AddsLeftShiftedBySixtyThree); + RUN_TEST(test_BalTier1AddSub_DestinationMatchingSecondSourceUsesOldValue); + RUN_TEST(test_BalTier1AddSub_DestinationMatchingFirstSource); + RUN_TEST(test_BalTier1AddSub_SubtractsLeftShiftedSecondSource); + RUN_TEST(test_BalTier1AddSub_SubtractionDestinationMatchingSecondSourceUsesOldValue); + RUN_TEST(test_BalTier1AddSub_CompareSetsFlagsFromShiftedOperand); + RUN_TEST(test_BalTier1AddSub_CompareNegateSetsFlagsFromShiftedOperand); + RUN_TEST(test_BalTier1AddSub_UnshiftedAddStillWorks); + RUN_TEST(test_BalTier1AddSub_UnshiftedCompareStillWorks); + + return UNITY_END(); +}