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
7 changes: 5 additions & 2 deletions include/backend/bal_cpu.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,9 @@ extern "C"
{
#endif // __cplusplus

typedef uint64_t (*bal_cpu_guest_read_function_t)(void *context, uint64_t guest_address);
typedef uint64_t (*bal_cpu_guest_read_function_t)(void *context,
uint64_t guest_address,
uint64_t read_size);

BAL_ALIGNED(64) typedef struct
{
Expand All @@ -33,7 +35,8 @@ extern "C"
/// This only increments if the engine flag [`BAL_ENGINE_FLAG_INSTRUCTION_COUNTING`] is set.
uint64_t instruction_count;

/// Reads 8 bytes from guest memory.
/// Reads read_size bytes (1, 2, 4, or 8) from guest memory, zero-extending
/// to 64 bits. Faults only when the accessed bytes are unreadable.
bal_cpu_guest_read_function_t guest_read;

/// Opaque context passed as the first context to [`bal_cpu_guest_read_function_t`].
Expand Down
11 changes: 11 additions & 0 deletions src/backend/x86/bal_x86_tier1_compiler.c
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,13 @@

#define BAL_X86_ABI_ARG1 BAL_X86_RCX
#define BAL_X86_ABI_ARG2 BAL_X86_RDX
#define BAL_X86_ABI_ARG3 BAL_X86_R8

#else

#define BAL_X86_ABI_ARG1 BAL_X86_RDI
#define BAL_X86_ABI_ARG2 BAL_X86_RSI
#define BAL_X86_ABI_ARG3 BAL_X86_RDX

#endif

Expand Down Expand Up @@ -1659,6 +1661,15 @@ translate_ldr(bal_tier1_compiler_t *compiler,
};
bal_sliding_window_push(&compiler->window, mov_arg2_macro);

// Third argument: the access width in bytes, 1 << instruction bits 31-30. The engine
// faults on this width instead of always demanding 8 readable bytes, as the reference does.
const bal_x86_macro_t mov_arg3_macro = {
.opcode = BAL_X86_MACRO_MOV_REGISTER_IMMEDIATE,
.destination = BAL_X86_ABI_ARG3,
.immediate_or_offset = 1ULL << ((instruction >> 30) & 0x3U),
};
bal_sliding_window_push(&compiler->window, mov_arg3_macro);

const bal_x86_macro_t load_guest_read_function_pointer_macro = {
.opcode = BAL_X86_MACRO_LOAD,
.destination = ASSEMBLER_TEMPORARY_REGISTER,
Expand Down
21 changes: 15 additions & 6 deletions src/bal_engine.c
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ static void block_cache_insert(block_cache_set_t *BAL_RESTRICT cache,
void *host_code);

/// Helper function which gets called by LDR instructions.
static uint64_t guest_read(void *context, uint64_t guest_address);
static uint64_t guest_read(void *context, uint64_t guest_address, uint64_t read_size);

BAL_COLD bal_error_t
bal_engine_init(bal_engine_t *BAL_RESTRICT engine,
Expand Down Expand Up @@ -615,7 +615,7 @@ block_cache_insert(block_cache_set_t *BAL_RESTRICT cache,
}

static uint64_t
guest_read(void *context, const uint64_t guest_address)
guest_read(void *context, const uint64_t guest_address, const uint64_t read_size)
{
bal_engine_t *BAL_RESTRICT engine = (bal_engine_t *)context;

Expand All @@ -625,6 +625,15 @@ guest_read(void *context, const uint64_t guest_address)
return 0U;
}

if (BAL_UNLIKELY(0ULL == read_size || sizeof(uint64_t) < read_size))
{
BAL_LOG_ERROR(&bal_thread_logger,
"Aborting function: read size (%llu) is not within [1, 8].",
(unsigned long long)read_size);
engine->status = BAL_ERROR_INVALID_ARGUMENT;
return 0U;
}

const bal_memory_interface_t *BAL_RESTRICT memory_interface = engine->memory_interface;

if (BAL_UNLIKELY(NULL == memory_interface))
Expand Down Expand Up @@ -652,16 +661,16 @@ guest_read(void *context, const uint64_t guest_address)
return 0U;
}

if (BAL_UNLIKELY(max_readable_bytes < sizeof(uint64_t)))
if (BAL_UNLIKELY(max_readable_bytes < read_size))
{
BAL_LOG_ERROR(&bal_thread_logger,
"Aborting function: max_readable_bytes (%u) is less than sizeof(uint64_t)",
max_readable_bytes);
"Aborting function: max_readable_bytes (%llu) is less than the read size (%llu)",
(unsigned long long)max_readable_bytes, (unsigned long long)read_size);
engine->status = BAL_ERROR_MEMORY_FAULT;
return 0U;
}

uint64_t value = 0U;
(void)memcpy(&value, host_pointer, sizeof(value));
(void)memcpy(&value, host_pointer, (size_t)read_size);
return value;
}
66 changes: 63 additions & 3 deletions tests/unit/bal_tier1/test_bal_tier1_ldr.c
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,12 @@
#define LDRH_W1_X0_U1020 0x794FF801U
#define LDR_X1_X0_U8 0xF9400401U
#define LDR_W1_X0_U4 0xB9400401U
/* LDR X1, [X0] (64-bit, zero offset) */
#define LDR_X1_X0_U0 0xF9400001U
/* LDRB X1, [X0] (byte, zero offset) */
#define LDRB_X1_X0_U0 0x39400001U
/* LDRH X1, [X0] (halfword, zero offset) */
#define LDRH_X1_X0_U0 0x79400001U

static uint32_t bal_test_guest_memory[TEST_GUEST_MEMORY_SIZE / sizeof(uint32_t)];

Expand Down Expand Up @@ -53,15 +59,16 @@ setUp(void)
set_u64(0x500U, 0x0000000012345678ULL);
set_u64(0xBF8U, 0xF300F300F300F300ULL);
set_u32(0x8FCU, 0xAB112233U);
set_u64(0xFF8U, 0x1122334455667788ULL); // last 8 bytes of the region
}

void
tearDown(void)
{
}

static void
run_single_guest_instruction(const uint32_t instruction, bal_cpu_t *BAL_RESTRICT cpu)
static bal_error_t
run_single_guest_instruction_status(const uint32_t instruction, bal_cpu_t *BAL_RESTRICT cpu)
{
bal_test_guest_memory[0] = instruction;
bal_test_guest_memory[1] = ARM64_RET_ENCODING;
Expand All @@ -88,7 +95,13 @@ run_single_guest_instruction(const uint32_t instruction, bal_cpu_t *BAL_RESTRICT
bal_engine_destroy(&engine);
(void)bal_flat_translation_interface_destroy(&allocator, &memory_interface);

TEST_ASSERT_EQUAL(BAL_SUCCESS, run_status);
return run_status;
}

static void
run_single_guest_instruction(const uint32_t instruction, bal_cpu_t *BAL_RESTRICT cpu)
{
TEST_ASSERT_EQUAL(BAL_SUCCESS, run_single_guest_instruction_status(instruction, cpu));
}

static void
Expand Down Expand Up @@ -292,6 +305,49 @@ test_BalTier1Ldr_WordUnsignedOffsetScalesByAccessSize(void)
TEST_ASSERT_EQUAL_HEX64(0x400ULL, cpu32.x[0]);
}

static void
test_BalTier1Ldr_ReadEndingExactlyAtRegionEndSucceeds(void)
{
bal_cpu_t cpu = {};
cpu.x[0] = 0xFF8ULL; // 8 readable bytes ending flush with the region end

run_single_guest_instruction(LDR_X1_X0_U0, &cpu);

TEST_ASSERT_EQUAL_HEX64(0x1122334455667788ULL, cpu.x[1]);
}

static void
test_BalTier1Ldr_ByteAtLastByteOfRegionSucceedsWhereWiderReadWouldNot(void)
{
bal_cpu_t cpu = {};
cpu.x[0] = 0xFFFULL; // only 1 byte readable

run_single_guest_instruction(LDRB_X1_X0_U0, &cpu);

TEST_ASSERT_EQUAL_HEX64(0x11ULL, cpu.x[1]); // byte 0xFFF holds the most significant byte
TEST_ASSERT_EQUAL_HEX64(0xFFFULL, cpu.x[0]);
}

static void
test_BalTier1Ldr_HalfwordReadCrossingRegionEndFaults(void)
{
bal_cpu_t cpu = {};
cpu.x[0] = 0xFFFULL; // 1 byte readable, 2 requested

TEST_ASSERT_EQUAL(BAL_ERROR_MEMORY_FAULT,
run_single_guest_instruction_status(LDRH_X1_X0_U0, &cpu));
}

static void
test_BalTier1Ldr_ReadCrossingRegionEndStillFaults(void)
{
bal_cpu_t cpu = {};
cpu.x[0] = 0xFFAULL; // 6 bytes readable, 8 requested

TEST_ASSERT_EQUAL(BAL_ERROR_MEMORY_FAULT,
run_single_guest_instruction_status(LDR_X1_X0_U0, &cpu));
}

int
main(void)
{
Expand All @@ -313,6 +369,10 @@ main(void)
RUN_TEST(test_BalTier1Ldr_ByteUnsignedOffsetScalesByOne);
RUN_TEST(test_BalTier1Ldr_HalfwordUnsignedOffsetScalesByTwo);
RUN_TEST(test_BalTier1Ldr_WordUnsignedOffsetScalesByAccessSize);
RUN_TEST(test_BalTier1Ldr_ReadEndingExactlyAtRegionEndSucceeds);
RUN_TEST(test_BalTier1Ldr_ByteAtLastByteOfRegionSucceedsWhereWiderReadWouldNot);
RUN_TEST(test_BalTier1Ldr_HalfwordReadCrossingRegionEndFaults);
RUN_TEST(test_BalTier1Ldr_ReadCrossingRegionEndStillFaults);

return UNITY_END();
}