mirror of
https://github.com/pound-emu/ballistic.git
synced 2026-01-31 01:15:21 +01:00
engine: udpate clang-format rules
Makes Ballistic more inlime with BARR-C's style guide. Signed-off-by: Ronald Caesar <github43132@proton.me>
This commit is contained in:
@@ -48,7 +48,7 @@ BreakConstructorInitializersBeforeComma: false
|
||||
BreakConstructorInitializers: BeforeComma
|
||||
BreakAfterJavaFieldAnnotations: true
|
||||
BreakStringLiterals: true
|
||||
ColumnLimit: 80
|
||||
ColumnLimit: 100
|
||||
CommentPragmas: '^ IWYU pragma:'
|
||||
CompactNamespaces: false
|
||||
ConstructorInitializerAllOnOneLineOrOnePerLine: false
|
||||
@@ -104,7 +104,7 @@ PenaltyExcessCharacter: 1000000
|
||||
PenaltyReturnTypeOnItsOwnLine: 200
|
||||
PointerAlignment: Right
|
||||
ReflowComments: true
|
||||
SortIncludes: false
|
||||
SortIncludes: true
|
||||
SortUsingDeclarations: false
|
||||
SpaceAfterCStyleCast: false
|
||||
SpaceAfterLogicalNot: false
|
||||
@@ -116,7 +116,8 @@ SpaceBeforeInheritanceColon: true
|
||||
SpaceBeforeParens: Custom
|
||||
SpaceBeforeParensOptions:
|
||||
AfterControlStatements: true
|
||||
AfterFunctionDefinitionName: true
|
||||
AfterFunctionDefinitionName: false
|
||||
AfterFunctionDeclarationName: false
|
||||
SpaceBeforeRangeBasedForLoopColon: true
|
||||
SpaceInEmptyBlock: false
|
||||
SpaceInEmptyParentheses: false
|
||||
|
||||
@@ -19,35 +19,36 @@ extern "C"
|
||||
{
|
||||
#endif
|
||||
|
||||
#define BAL_OPERANDS_SIZE 4
|
||||
#define BAL_OPERAND_BIT_WIDTH 5
|
||||
#define BAL_OPERANDS_SIZE 4
|
||||
#define BAL_OPERAND_BIT_WIDTH 5
|
||||
|
||||
/// The type of an instruction operand.
|
||||
typedef enum
|
||||
{
|
||||
BAL_OPERAND_TYPE_NONE = 0,
|
||||
BAL_OPERAND_TYPE_NONE = 0,
|
||||
BAL_OPERAND_TYPE_REGISTER_32 = 1,
|
||||
BAL_OPERAND_TYPE_REGISTER_64 = 2,
|
||||
BAL_OPERAND_TYPE_REGISTER_64 = 2,
|
||||
BAL_OPERAND_TYPE_REGISTER_128 = 3,
|
||||
BAL_OPERAND_TYPE_IMMEDIATE = 4,
|
||||
BAL_OPERAND_TYPE_CONDITION = 5,
|
||||
BAL_OPERAND_TYPE_IMMEDIATE = 4,
|
||||
BAL_OPERAND_TYPE_CONDITION = 5,
|
||||
} bal_decoder_operand_type_t;
|
||||
|
||||
/// Descriptor for a single operand.
|
||||
typedef struct
|
||||
{
|
||||
/// Operand type. See [`bal_decoder_operand_type_t`].
|
||||
uint16_t type : 5;
|
||||
uint16_t type : 5;
|
||||
|
||||
/// Bit position in the instruction.
|
||||
uint16_t bit_position : 6;
|
||||
|
||||
/// Bit width of the field.
|
||||
uint16_t bit_width : BAL_OPERAND_BIT_WIDTH;
|
||||
uint16_t bit_width : BAL_OPERAND_BIT_WIDTH;
|
||||
} bal_decoder_operand_t;
|
||||
|
||||
static_assert(2 == sizeof(bal_decoder_operand_t), "Expected operand struct to be 2 bytes.");
|
||||
static_assert(5 == BAL_OPERAND_BIT_WIDTH, "Operand bit width must be less than 32 to prevent shift overflow.");
|
||||
static_assert(5 == BAL_OPERAND_BIT_WIDTH,
|
||||
"Operand bit width must be less than 32 to prevent shift overflow.");
|
||||
|
||||
/// Represents static metadata aasociated with a specific ARM instruction.
|
||||
BAL_ALIGNED(32) typedef struct
|
||||
@@ -73,7 +74,8 @@ extern "C"
|
||||
bal_decoder_operand_t operands[BAL_OPERANDS_SIZE];
|
||||
} bal_decoder_instruction_metadata_t;
|
||||
|
||||
static_assert(32 == sizeof(bal_decoder_instruction_metadata_t), "Expected decoder metadata struct to be 32 bytes.");
|
||||
static_assert(32 == sizeof(bal_decoder_instruction_metadata_t),
|
||||
"Expected decoder metadata struct to be 32 bytes.");
|
||||
|
||||
/// Decodes a raw ARM64 instruction.
|
||||
///
|
||||
@@ -85,8 +87,7 @@ extern "C"
|
||||
///
|
||||
/// The pointer refers to static readonly memory. It is valid for the
|
||||
/// lifetime of the program and must not be freed.
|
||||
BAL_HOT const bal_decoder_instruction_metadata_t *bal_decode_arm64(
|
||||
const uint32_t instruction);
|
||||
BAL_HOT const bal_decoder_instruction_metadata_t *bal_decode_arm64(const uint32_t instruction);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
|
||||
@@ -2,9 +2,9 @@
|
||||
#define BALLISTIC_ENGINE_H
|
||||
|
||||
#include "bal_attributes.h"
|
||||
#include "bal_types.h"
|
||||
#include "bal_memory.h"
|
||||
#include "bal_errors.h"
|
||||
#include "bal_memory.h"
|
||||
#include "bal_types.h"
|
||||
#include <stdint.h>
|
||||
|
||||
/// A byte pattern written to memory during initialization, poisoning allocated
|
||||
@@ -75,7 +75,7 @@ BAL_ALIGNED(64) typedef struct
|
||||
|
||||
/// The base pointer returned during the underlying heap allocation. This
|
||||
/// is required to correctly free the engine's internal arrays.
|
||||
void *arena_base;
|
||||
void *arena_base;
|
||||
|
||||
/// The total size of the allocated arena.
|
||||
size_t arena_size;
|
||||
@@ -96,8 +96,7 @@ BAL_ALIGNED(64) typedef struct
|
||||
///
|
||||
/// Returns [`BAL_ERROR_ALLOCATION_FAILED`] if the allocator cannot fulfill the
|
||||
/// request.
|
||||
BAL_COLD bal_error_t bal_engine_init(bal_allocator_t *allocator,
|
||||
bal_engine_t *engine);
|
||||
BAL_COLD bal_error_t bal_engine_init(bal_allocator_t *allocator, bal_engine_t *engine);
|
||||
|
||||
/// Translates machine code starting at `arm_entry_point` into the engine's
|
||||
/// internal IR. `interface` provides memory access handling (like instruction
|
||||
@@ -109,10 +108,9 @@ BAL_COLD bal_error_t bal_engine_init(bal_allocator_t *allocator,
|
||||
///
|
||||
/// Returns [`BAL_ERROR_ENGINE_STATE_INVALID`] if `engine` is not initialized
|
||||
/// or `engine->status != BAL_SUCCESS`.
|
||||
BAL_HOT bal_error_t
|
||||
bal_engine_translate(bal_engine_t *BAL_RESTRICT engine,
|
||||
bal_memory_interface_t *BAL_RESTRICT interface,
|
||||
const uint32_t *BAL_RESTRICT arm_entry_point);
|
||||
BAL_HOT bal_error_t bal_engine_translate(bal_engine_t *BAL_RESTRICT engine,
|
||||
bal_memory_interface_t *BAL_RESTRICT interface,
|
||||
const uint32_t *BAL_RESTRICT arm_entry_point);
|
||||
|
||||
/// Resets `engine` for the next compilation unit. This is a low cost memory
|
||||
/// operation designed to be called between translation units.
|
||||
@@ -130,8 +128,7 @@ BAL_HOT bal_error_t bal_engine_reset(bal_engine_t *engine);
|
||||
///
|
||||
/// This function does not free the [`bal_engine_t`] struct itself, as the
|
||||
/// caller may have allocated it on the stack.
|
||||
BAL_COLD void bal_engine_destroy(bal_allocator_t *allocator,
|
||||
bal_engine_t *engine);
|
||||
BAL_COLD void bal_engine_destroy(bal_allocator_t *allocator, bal_engine_t *engine);
|
||||
#endif /* BALLISTIC_ENGINE_H */
|
||||
|
||||
/*** end of file ***/
|
||||
|
||||
@@ -2,10 +2,10 @@
|
||||
#define BALLISTIC_MEMORY_H
|
||||
|
||||
#include "bal_attributes.h"
|
||||
#include "bal_types.h"
|
||||
#include "bal_errors.h"
|
||||
#include <stdint.h>
|
||||
#include "bal_types.h"
|
||||
#include <stddef.h>
|
||||
#include <stdint.h>
|
||||
|
||||
/// A function signature for allocating aligned memory.
|
||||
///
|
||||
@@ -16,9 +16,7 @@
|
||||
///
|
||||
/// Returns a pointer to the allocated memory, or `NULL` if the request could
|
||||
/// not be fulfilled.
|
||||
typedef void *(*bal_allocate_function_t)(void *allocator,
|
||||
size_t alignment,
|
||||
size_t size);
|
||||
typedef void *(*bal_allocate_function_t)(void *allocator, size_t alignment, size_t size);
|
||||
|
||||
/// A function signature for releasing memory.
|
||||
///
|
||||
@@ -26,9 +24,7 @@ typedef void *(*bal_allocate_function_t)(void *allocator,
|
||||
/// previously allocated by the corresponding allocate function. The `size`
|
||||
/// parameter indicates the size of the allocation being freed. Access to the
|
||||
/// heap state is provided via `allocator`.
|
||||
typedef void (*bal_free_function_t)(void *allocator,
|
||||
void *pointer,
|
||||
size_t size);
|
||||
typedef void (*bal_free_function_t)(void *allocator, void *pointer, size_t size);
|
||||
|
||||
/// Translates a Guest Virtual Address (GVA) to a Host Virtual Address (HVA).
|
||||
///
|
||||
@@ -45,10 +41,9 @@ typedef void (*bal_free_function_t)(void *allocator,
|
||||
/// The implementation must write the number of contiguous, readable bytes
|
||||
/// available at the returned pointer into `max_readable_size`. This prevents
|
||||
/// Ballistic from reading beyond the end of a mapped page or buffer.
|
||||
typedef const uint8_t *(*bal_translate_function_t)(
|
||||
void *context,
|
||||
bal_guest_address_t guest_address,
|
||||
size_t *max_readable_size);
|
||||
typedef const uint8_t *(*bal_translate_function_t)(void *context,
|
||||
bal_guest_address_t guest_address,
|
||||
size_t *max_readable_size);
|
||||
|
||||
/// The host application is responsible for providing an allocator capable of
|
||||
/// handling aligned memory requests.
|
||||
@@ -76,7 +71,7 @@ typedef struct
|
||||
{
|
||||
/// An opaque pointer to the context required for address translation
|
||||
/// (e.g, a page walker or a buffer descriptor.).
|
||||
void *context;
|
||||
void *context;
|
||||
|
||||
/// The callback invoked to perform address translation.
|
||||
bal_translate_function_t translate;
|
||||
@@ -115,11 +110,10 @@ BAL_COLD void bal_get_default_allocator(bal_allocator_t *out_allocator);
|
||||
///
|
||||
/// The caller retains ownership of `buffer`. It must remain valid and
|
||||
/// unmodified for the lifetime of the created interface.
|
||||
BAL_COLD bal_error_t
|
||||
bal_memory_init_flat(bal_allocator_t *BAL_RESTRICT allocator,
|
||||
bal_memory_interface_t *BAL_RESTRICT interface,
|
||||
void *BAL_RESTRICT buffer,
|
||||
size_t size);
|
||||
BAL_COLD bal_error_t bal_memory_init_flat(bal_allocator_t *BAL_RESTRICT allocator,
|
||||
bal_memory_interface_t *BAL_RESTRICT interface,
|
||||
void *BAL_RESTRICT buffer,
|
||||
size_t size);
|
||||
|
||||
/// Frees the internal sttae allocated within `interface` using the provided
|
||||
/// `allocator`.
|
||||
|
||||
@@ -1,7 +1,6 @@
|
||||
#ifndef BALLISTIC_PLATFORM_H
|
||||
#define BALLISTIC_PLATFORM_H
|
||||
|
||||
|
||||
#if defined(_WIN32) || defined(_WIN64)
|
||||
|
||||
#define BAL_PLATFORM_WINDOWS 1
|
||||
|
||||
@@ -8,7 +8,6 @@
|
||||
|
||||
#include <stdint.h>
|
||||
|
||||
|
||||
typedef uint64_t bal_guest_address_t;
|
||||
typedef uint64_t bal_instruction_t;
|
||||
typedef uint32_t bal_constant_t;
|
||||
|
||||
205
src/bal_engine.c
205
src/bal_engine.c
@@ -27,47 +27,39 @@
|
||||
#define BAL_ALIGN_UP(x, memory_alignment) \
|
||||
(((x) + ((memory_alignment) - 1)) & ~((memory_alignment) - 1))
|
||||
|
||||
static uint32_t extract_operand_value(const uint32_t,
|
||||
const bal_decoder_operand_t *);
|
||||
static uint32_t intern_constant(bal_constant_t,
|
||||
bal_constant_t *,
|
||||
bal_constant_count_t *,
|
||||
size_t,
|
||||
bal_error_t *);
|
||||
static uint32_t extract_operand_value(const uint32_t, const bal_decoder_operand_t *);
|
||||
static uint32_t intern_constant(
|
||||
bal_constant_t, bal_constant_t *, bal_constant_count_t *, size_t, bal_error_t *);
|
||||
|
||||
bal_error_t
|
||||
bal_engine_init (bal_allocator_t *allocator, bal_engine_t *engine)
|
||||
bal_engine_init(bal_allocator_t *allocator, bal_engine_t *engine)
|
||||
{
|
||||
if (NULL == allocator || NULL == engine)
|
||||
{
|
||||
return BAL_ERROR_INVALID_ARGUMENT;
|
||||
}
|
||||
|
||||
size_t source_variables_size
|
||||
= MAX_GUEST_REGISTERS * sizeof(bal_source_variable_t);
|
||||
|
||||
size_t ssa_bit_widths_size = MAX_INSTRUCTIONS * sizeof(bal_bit_width_t);
|
||||
size_t instructions_size = MAX_INSTRUCTIONS * sizeof(bal_instruction_t);
|
||||
size_t constants_size = MAX_INSTRUCTIONS * sizeof(bal_instruction_t);
|
||||
size_t source_variables_size = MAX_GUEST_REGISTERS * sizeof(bal_source_variable_t);
|
||||
size_t ssa_bit_widths_size = MAX_INSTRUCTIONS * sizeof(bal_bit_width_t);
|
||||
size_t instructions_size = MAX_INSTRUCTIONS * sizeof(bal_instruction_t);
|
||||
size_t constants_size = MAX_INSTRUCTIONS * sizeof(bal_instruction_t);
|
||||
|
||||
// Calculate amount of memory needed for all arrays in engine.
|
||||
//
|
||||
size_t memory_alignment = 64U;
|
||||
size_t memory_alignment = 64U;
|
||||
size_t offset_instructions = BAL_ALIGN_UP(source_variables_size, memory_alignment);
|
||||
|
||||
size_t offset_instructions
|
||||
= BAL_ALIGN_UP(source_variables_size, memory_alignment);
|
||||
size_t offset_ssa_bit_widths
|
||||
= BAL_ALIGN_UP((offset_instructions + instructions_size), memory_alignment);
|
||||
|
||||
size_t offset_ssa_bit_widths = BAL_ALIGN_UP(
|
||||
(offset_instructions + instructions_size), memory_alignment);
|
||||
|
||||
size_t offset_constants = BAL_ALIGN_UP(
|
||||
(offset_ssa_bit_widths + ssa_bit_widths_size), memory_alignment);
|
||||
size_t offset_constants
|
||||
= BAL_ALIGN_UP((offset_ssa_bit_widths + ssa_bit_widths_size), memory_alignment);
|
||||
|
||||
size_t total_size_with_padding
|
||||
= BAL_ALIGN_UP((offset_constants + constants_size), memory_alignment);
|
||||
|
||||
uint8_t *data = (uint8_t *)allocator->allocate(
|
||||
allocator, memory_alignment, total_size_with_padding);
|
||||
uint8_t *data
|
||||
= (uint8_t *)allocator->allocate(allocator, memory_alignment, total_size_with_padding);
|
||||
|
||||
if (NULL == data)
|
||||
{
|
||||
@@ -75,40 +67,30 @@ bal_engine_init (bal_allocator_t *allocator, bal_engine_t *engine)
|
||||
return engine->status;
|
||||
}
|
||||
|
||||
engine->source_variables = (bal_source_variable_t *)data;
|
||||
engine->instructions = (bal_instruction_t *)(data + offset_instructions);
|
||||
engine->ssa_bit_widths = (bal_bit_width_t *)(data + offset_ssa_bit_widths);
|
||||
engine->constants = (bal_constant_t *)(data + offset_constants);
|
||||
engine->source_variables_size
|
||||
= source_variables_size / sizeof(bal_source_variable_t);
|
||||
engine->instructions_size = instructions_size / sizeof(bal_instruction_t);
|
||||
engine->constants_size = constants_size / sizeof(bal_constant_t);
|
||||
engine->instruction_count = 0;
|
||||
engine->status = BAL_SUCCESS;
|
||||
engine->arena_base = (void *)data;
|
||||
engine->arena_size = total_size_with_padding;
|
||||
engine->source_variables = (bal_source_variable_t *)data;
|
||||
engine->instructions = (bal_instruction_t *)(data + offset_instructions);
|
||||
engine->ssa_bit_widths = (bal_bit_width_t *)(data + offset_ssa_bit_widths);
|
||||
engine->constants = (bal_constant_t *)(data + offset_constants);
|
||||
engine->source_variables_size = source_variables_size / sizeof(bal_source_variable_t);
|
||||
engine->instructions_size = instructions_size / sizeof(bal_instruction_t);
|
||||
engine->constants_size = constants_size / sizeof(bal_constant_t);
|
||||
engine->instruction_count = 0;
|
||||
engine->status = BAL_SUCCESS;
|
||||
engine->arena_base = (void *)data;
|
||||
engine->arena_size = total_size_with_padding;
|
||||
|
||||
(void)memset(engine->source_variables,
|
||||
POISON_UNINITIALIZED_MEMORY,
|
||||
source_variables_size);
|
||||
|
||||
(void)memset(
|
||||
engine->instructions, POISON_UNINITIALIZED_MEMORY, instructions_size);
|
||||
|
||||
(void)memset(engine->ssa_bit_widths,
|
||||
POISON_UNINITIALIZED_MEMORY,
|
||||
ssa_bit_widths_size);
|
||||
|
||||
(void)memset(
|
||||
engine->constants, POISON_UNINITIALIZED_MEMORY, constants_size);
|
||||
(void)memset(engine->source_variables, POISON_UNINITIALIZED_MEMORY, source_variables_size);
|
||||
(void)memset(engine->instructions, POISON_UNINITIALIZED_MEMORY, instructions_size);
|
||||
(void)memset(engine->ssa_bit_widths, POISON_UNINITIALIZED_MEMORY, ssa_bit_widths_size);
|
||||
(void)memset(engine->constants, POISON_UNINITIALIZED_MEMORY, constants_size);
|
||||
|
||||
return engine->status;
|
||||
}
|
||||
|
||||
bal_error_t
|
||||
bal_engine_translate (bal_engine_t *BAL_RESTRICT engine,
|
||||
bal_memory_interface_t *BAL_RESTRICT interface,
|
||||
const uint32_t *BAL_RESTRICT arm_entry_point)
|
||||
bal_engine_translate(bal_engine_t *BAL_RESTRICT engine,
|
||||
bal_memory_interface_t *BAL_RESTRICT interface,
|
||||
const uint32_t *BAL_RESTRICT arm_entry_point)
|
||||
{
|
||||
if (BAL_UNLIKELY(NULL == engine || NULL == arm_entry_point))
|
||||
{
|
||||
@@ -124,14 +106,13 @@ bal_engine_translate (bal_engine_t *BAL_RESTRICT engine,
|
||||
bal_bit_width_t *BAL_RESTRICT bit_width_cursor
|
||||
= engine->ssa_bit_widths + engine->instruction_count;
|
||||
|
||||
bal_source_variable_t *BAL_RESTRICT source_variables
|
||||
= engine->source_variables;
|
||||
bal_source_variable_t *BAL_RESTRICT source_variables = engine->source_variables;
|
||||
|
||||
bal_constant_t *BAL_RESTRICT constants = engine->constants;
|
||||
bal_constant_count_t constant_count = engine->constant_count;
|
||||
bal_instruction_count_t instruction_count = engine->instruction_count;
|
||||
bal_error_t status = engine->status;
|
||||
const uint32_t *BAL_RESTRICT arm_instruction_cursor = arm_entry_point;
|
||||
bal_constant_t *BAL_RESTRICT constants = engine->constants;
|
||||
bal_constant_count_t constant_count = engine->constant_count;
|
||||
bal_instruction_count_t instruction_count = engine->instruction_count;
|
||||
bal_error_t status = engine->status;
|
||||
const uint32_t *BAL_RESTRICT arm_instruction_cursor = arm_entry_point;
|
||||
uint32_t arm_registers[BAL_OPERANDS_SIZE] = { 0 };
|
||||
|
||||
while (ir_instruction_cursor < ir_instruction_end)
|
||||
@@ -142,8 +123,7 @@ bal_engine_translate (bal_engine_t *BAL_RESTRICT engine,
|
||||
|
||||
for (size_t i = 0; i < BAL_OPERANDS_SIZE; ++i)
|
||||
{
|
||||
arm_registers[i]
|
||||
= extract_operand_value(*arm_instruction_cursor, operands);
|
||||
arm_registers[i] = extract_operand_value(*arm_instruction_cursor, operands);
|
||||
++operands;
|
||||
}
|
||||
|
||||
@@ -155,9 +135,8 @@ bal_engine_translate (bal_engine_t *BAL_RESTRICT engine,
|
||||
uint32_t hw = arm_registers[2];
|
||||
uint32_t shift = hw * 16;
|
||||
|
||||
uint64_t mask = (32 == operands[0].bit_width)
|
||||
? 0xFFFFFFFFULL
|
||||
: 0xFFFFFFFFFFFFFFFFULL;
|
||||
uint64_t mask
|
||||
= (32 == operands[0].bit_width) ? 0xFFFFFFFFULL : 0xFFFFFFFFFFFFFFFFULL;
|
||||
// Calculate the shifted immediate value.
|
||||
//
|
||||
uint64_t value = (imm16 << shift) & mask;
|
||||
@@ -179,72 +158,61 @@ bal_engine_translate (bal_engine_t *BAL_RESTRICT engine,
|
||||
// new_val = cleared_val + (imm << shift)
|
||||
|
||||
uint32_t old_ssa
|
||||
= (31 == rd) ? intern_constant(0,
|
||||
constants,
|
||||
&constant_count,
|
||||
engine->constants_size,
|
||||
&status)
|
||||
: source_variables[rd].current_ssa_index;
|
||||
= (31 == rd)
|
||||
? intern_constant(
|
||||
0, constants, &constant_count, engine->constants_size, &status)
|
||||
: source_variables[rd].current_ssa_index;
|
||||
uint64_t clear_mask = (~(0xFFFFULL << shift)) & mask;
|
||||
uint32_t mask_index
|
||||
= intern_constant((uint32_t)clear_mask,
|
||||
constants,
|
||||
&constant_count,
|
||||
engine->constants_size,
|
||||
&status);
|
||||
uint32_t mask_index = intern_constant((uint32_t)clear_mask,
|
||||
constants,
|
||||
&constant_count,
|
||||
engine->constants_size,
|
||||
&status);
|
||||
|
||||
if (BAL_UNLIKELY(status != BAL_SUCCESS))
|
||||
{
|
||||
break;
|
||||
}
|
||||
|
||||
*ir_instruction_cursor = ((bal_instruction_t)OPCODE_AND
|
||||
<< BAL_OPCODE_SHIFT_POSITION)
|
||||
| ((bal_instruction_t)old_ssa
|
||||
<< BAL_SOURCE1_SHIFT_POSITION)
|
||||
| ((bal_instruction_t)mask_index
|
||||
<< BAL_SOURCE2_SHIFT_POSITION);
|
||||
*ir_instruction_cursor
|
||||
= ((bal_instruction_t)OPCODE_AND << BAL_OPCODE_SHIFT_POSITION)
|
||||
| ((bal_instruction_t)old_ssa << BAL_SOURCE1_SHIFT_POSITION)
|
||||
| ((bal_instruction_t)mask_index << BAL_SOURCE2_SHIFT_POSITION);
|
||||
|
||||
++ir_instruction_cursor;
|
||||
++bit_width_cursor;
|
||||
++instruction_count;
|
||||
|
||||
uint32_t value_index
|
||||
= intern_constant((uint32_t)value,
|
||||
constants,
|
||||
&constant_count,
|
||||
engine->constants_size,
|
||||
&status);
|
||||
uint32_t value_index = intern_constant((uint32_t)value,
|
||||
constants,
|
||||
&constant_count,
|
||||
engine->constants_size,
|
||||
&status);
|
||||
|
||||
// Source 1 is the result of the AND instruction.
|
||||
//
|
||||
uint32_t masked_ssa = instruction_count - 1;
|
||||
*ir_instruction_cursor = ((bal_instruction_t)OPCODE_ADD
|
||||
<< BAL_OPCODE_SHIFT_POSITION)
|
||||
| ((bal_instruction_t)masked_ssa
|
||||
<< BAL_SOURCE1_SHIFT_POSITION)
|
||||
| ((bal_instruction_t)value_index
|
||||
<< BAL_SOURCE2_SHIFT_POSITION);
|
||||
uint32_t masked_ssa = instruction_count - 1;
|
||||
*ir_instruction_cursor
|
||||
= ((bal_instruction_t)OPCODE_ADD << BAL_OPCODE_SHIFT_POSITION)
|
||||
| ((bal_instruction_t)masked_ssa << BAL_SOURCE1_SHIFT_POSITION)
|
||||
| ((bal_instruction_t)value_index << BAL_SOURCE2_SHIFT_POSITION);
|
||||
}
|
||||
else
|
||||
{
|
||||
uint32_t constant_index
|
||||
= intern_constant((uint32_t)value,
|
||||
constants,
|
||||
&constant_count,
|
||||
engine->constants_size,
|
||||
&status);
|
||||
uint32_t constant_index = intern_constant((uint32_t)value,
|
||||
constants,
|
||||
&constant_count,
|
||||
engine->constants_size,
|
||||
&status);
|
||||
|
||||
if (BAL_UNLIKELY(status != BAL_SUCCESS))
|
||||
{
|
||||
break;
|
||||
break;
|
||||
}
|
||||
|
||||
*ir_instruction_cursor
|
||||
= ((bal_instruction_t)OPCODE_CONST
|
||||
<< BAL_OPCODE_SHIFT_POSITION)
|
||||
| ((bal_instruction_t)constant_index
|
||||
<< BAL_SOURCE1_SHIFT_POSITION);
|
||||
= ((bal_instruction_t)OPCODE_CONST << BAL_OPCODE_SHIFT_POSITION)
|
||||
| ((bal_instruction_t)constant_index << BAL_SOURCE1_SHIFT_POSITION);
|
||||
}
|
||||
|
||||
// Only update the SSA map is not writing to XZR/WZR.
|
||||
@@ -269,7 +237,7 @@ bal_engine_translate (bal_engine_t *BAL_RESTRICT engine,
|
||||
}
|
||||
|
||||
bal_error_t
|
||||
bal_engine_reset (bal_engine_t *engine)
|
||||
bal_engine_reset(bal_engine_t *engine)
|
||||
{
|
||||
if (BAL_UNLIKELY(NULL == engine))
|
||||
{
|
||||
@@ -279,18 +247,16 @@ bal_engine_reset (bal_engine_t *engine)
|
||||
engine->instruction_count = 0;
|
||||
engine->status = BAL_SUCCESS;
|
||||
|
||||
(void)memset(engine->source_variables,
|
||||
POISON_UNINITIALIZED_MEMORY,
|
||||
engine->source_variables_size);
|
||||
|
||||
(void)memset(
|
||||
engine->constants, POISON_UNINITIALIZED_MEMORY, engine->constants_size);
|
||||
engine->source_variables, POISON_UNINITIALIZED_MEMORY, engine->source_variables_size);
|
||||
|
||||
(void)memset(engine->constants, POISON_UNINITIALIZED_MEMORY, engine->constants_size);
|
||||
|
||||
return engine->status;
|
||||
}
|
||||
|
||||
void
|
||||
bal_engine_destroy (bal_allocator_t *allocator, bal_engine_t *engine)
|
||||
bal_engine_destroy(bal_allocator_t *allocator, bal_engine_t *engine)
|
||||
{
|
||||
// No argument error handling. Segfault if user passes NULL.
|
||||
|
||||
@@ -302,8 +268,7 @@ bal_engine_destroy (bal_allocator_t *allocator, bal_engine_t *engine)
|
||||
}
|
||||
|
||||
BAL_HOT static uint32_t
|
||||
extract_operand_value (const uint32_t instruction,
|
||||
const bal_decoder_operand_t *operand)
|
||||
extract_operand_value(const uint32_t instruction, const bal_decoder_operand_t *operand)
|
||||
{
|
||||
if (BAL_OPERAND_TYPE_NONE == operand->type)
|
||||
{
|
||||
@@ -316,11 +281,11 @@ extract_operand_value (const uint32_t instruction,
|
||||
}
|
||||
|
||||
BAL_HOT static uint32_t
|
||||
intern_constant (bal_constant_t constant,
|
||||
bal_constant_t *BAL_RESTRICT constants,
|
||||
bal_constant_count_t *BAL_RESTRICT count,
|
||||
size_t constants_max_size,
|
||||
bal_error_t *BAL_RESTRICT status)
|
||||
intern_constant(bal_constant_t constant,
|
||||
bal_constant_t *BAL_RESTRICT constants,
|
||||
bal_constant_count_t *BAL_RESTRICT count,
|
||||
size_t constants_max_size,
|
||||
bal_error_t *BAL_RESTRICT status)
|
||||
{
|
||||
if (BAL_UNLIKELY(*status != BAL_SUCCESS))
|
||||
{
|
||||
|
||||
@@ -1,12 +1,10 @@
|
||||
#include "bal_memory.h"
|
||||
#include <stdlib.h>
|
||||
#include <stddef.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
static void *default_allocate(void *, size_t, size_t);
|
||||
static void default_free(void *, void *, size_t);
|
||||
BAL_HOT static const uint8_t *_bal_translate_flat(void *,
|
||||
bal_guest_address_t,
|
||||
size_t *);
|
||||
BAL_HOT static const uint8_t *_bal_translate_flat(void *, bal_guest_address_t, size_t *);
|
||||
|
||||
typedef struct
|
||||
{
|
||||
@@ -15,7 +13,7 @@ typedef struct
|
||||
} flat_translation_interface_t;
|
||||
|
||||
void
|
||||
bal_get_default_allocator (bal_allocator_t *out_allocator)
|
||||
bal_get_default_allocator(bal_allocator_t *out_allocator)
|
||||
{
|
||||
out_allocator->allocator = NULL;
|
||||
out_allocator->allocate = default_allocate;
|
||||
@@ -23,10 +21,10 @@ bal_get_default_allocator (bal_allocator_t *out_allocator)
|
||||
}
|
||||
|
||||
bal_error_t
|
||||
bal_memory_init_flat (bal_allocator_t *BAL_RESTRICT allocator,
|
||||
bal_memory_interface_t *BAL_RESTRICT interface,
|
||||
void *BAL_RESTRICT buffer,
|
||||
size_t size)
|
||||
bal_memory_init_flat(bal_allocator_t *BAL_RESTRICT allocator,
|
||||
bal_memory_interface_t *BAL_RESTRICT interface,
|
||||
void *BAL_RESTRICT buffer,
|
||||
size_t size)
|
||||
{
|
||||
if (NULL == allocator || NULL == interface || NULL == buffer || 0 == size)
|
||||
{
|
||||
@@ -53,8 +51,7 @@ bal_memory_init_flat (bal_allocator_t *BAL_RESTRICT allocator,
|
||||
}
|
||||
|
||||
void
|
||||
bal_memory_destroy_flat (bal_allocator_t *allocator,
|
||||
bal_memory_interface_t *interface)
|
||||
bal_memory_destroy_flat(bal_allocator_t *allocator, bal_memory_interface_t *interface)
|
||||
{
|
||||
if (NULL == allocator || NULL == interface)
|
||||
{
|
||||
@@ -66,14 +63,13 @@ bal_memory_destroy_flat (bal_allocator_t *allocator,
|
||||
return;
|
||||
}
|
||||
|
||||
allocator->free(
|
||||
allocator, interface->context, sizeof(flat_translation_interface_t));
|
||||
allocator->free(allocator, interface->context, sizeof(flat_translation_interface_t));
|
||||
}
|
||||
|
||||
#if BAL_PLATFORM_POSIX
|
||||
|
||||
static void *
|
||||
default_allocate (void *allocator, size_t alignment, size_t size)
|
||||
default_allocate(void *allocator, size_t alignment, size_t size)
|
||||
{
|
||||
if (0 == size)
|
||||
{
|
||||
@@ -85,7 +81,7 @@ default_allocate (void *allocator, size_t alignment, size_t size)
|
||||
}
|
||||
|
||||
static void
|
||||
default_free (void *allocator, void *pointer, size_t size)
|
||||
default_free(void *allocator, void *pointer, size_t size)
|
||||
{
|
||||
free(pointer);
|
||||
}
|
||||
@@ -97,7 +93,7 @@ default_free (void *allocator, void *pointer, size_t size)
|
||||
#include <malloc.h>
|
||||
|
||||
static void *
|
||||
default_allocate (void *allocator, size_t alignment, size_t size)
|
||||
default_allocate(void *allocator, size_t alignment, size_t size)
|
||||
{
|
||||
if (0 == size)
|
||||
{
|
||||
@@ -109,7 +105,7 @@ default_allocate (void *allocator, size_t alignment, size_t size)
|
||||
}
|
||||
|
||||
static void
|
||||
default_free (void *allocator, void *pointer, size_t size)
|
||||
default_free(void *allocator, void *pointer, size_t size)
|
||||
{
|
||||
_aligned_free(pointer);
|
||||
}
|
||||
@@ -117,19 +113,17 @@ default_free (void *allocator, void *pointer, size_t size)
|
||||
#endif /* BAL_PLATFORM_WINDOWS */
|
||||
|
||||
static const uint8_t *
|
||||
_bal_translate_flat (void *BAL_RESTRICT interface,
|
||||
bal_guest_address_t guest_address,
|
||||
size_t *BAL_RESTRICT max_readable_size)
|
||||
_bal_translate_flat(void *BAL_RESTRICT interface,
|
||||
bal_guest_address_t guest_address,
|
||||
size_t *BAL_RESTRICT max_readable_size)
|
||||
{
|
||||
if (BAL_UNLIKELY(NULL == interface || 0 == guest_address
|
||||
|| NULL == max_readable_size))
|
||||
if (BAL_UNLIKELY(NULL == interface || 0 == guest_address || NULL == max_readable_size))
|
||||
{
|
||||
return NULL;
|
||||
}
|
||||
|
||||
flat_translation_interface_t *BAL_RESTRICT context
|
||||
= (flat_translation_interface_t *)((bal_memory_interface_t *)interface)
|
||||
->context;
|
||||
= (flat_translation_interface_t *)((bal_memory_interface_t *)interface)->context;
|
||||
|
||||
// Is address out of bounds.
|
||||
//
|
||||
|
||||
@@ -12,7 +12,7 @@ bal_decode_arm64(const uint32_t instruction)
|
||||
const decoder_bucket_t *bucket = &g_decoder_lookup_table[index];
|
||||
for (size_t i = 0; i < bucket->count; ++i)
|
||||
{
|
||||
const bal_decoder_instruction_metadata_t* metadata = bucket->instructions[i];
|
||||
const bal_decoder_instruction_metadata_t *metadata = bucket->instructions[i];
|
||||
|
||||
if ((instruction & metadata->mask) == metadata->expected)
|
||||
{
|
||||
|
||||
Reference in New Issue
Block a user