Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
116 changes: 106 additions & 10 deletions src/backend/x86/bal_x86_tier1_compiler.c
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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,
Expand Down
3 changes: 2 additions & 1 deletion tests/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down Expand Up @@ -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)
Expand Down
23 changes: 11 additions & 12 deletions tests/fuzzer/src/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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);
Expand Down
Loading