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:
Ronald Caesar
2026-01-24 20:48:49 -04:00
parent 57db0cea96
commit ad0f881e1b
9 changed files with 151 additions and 201 deletions

View File

@@ -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

View File

@@ -47,7 +47,8 @@ extern "C"
} 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
}

View File

@@ -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
@@ -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,8 +108,7 @@ 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_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);
@@ -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 ***/

View 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,8 +41,7 @@ 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,
typedef const uint8_t *(*bal_translate_function_t)(void *context,
bal_guest_address_t guest_address,
size_t *max_readable_size);
@@ -115,8 +110,7 @@ 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_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);

View File

@@ -1,7 +1,6 @@
#ifndef BALLISTIC_PLATFORM_H
#define BALLISTIC_PLATFORM_H
#if defined(_WIN32) || defined(_WIN64)
#define BAL_PLATFORM_WINDOWS 1

View File

@@ -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;

View File

@@ -27,13 +27,9 @@
#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)
@@ -43,9 +39,7 @@ bal_engine_init (bal_allocator_t *allocator, bal_engine_t *engine)
return BAL_ERROR_INVALID_ARGUMENT;
}
size_t source_variables_size
= MAX_GUEST_REGISTERS * sizeof(bal_source_variable_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);
@@ -53,21 +47,19 @@ bal_engine_init (bal_allocator_t *allocator, bal_engine_t *engine)
// Calculate amount of memory needed for all arrays in engine.
//
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)
{
@@ -79,8 +71,7 @@ bal_engine_init (bal_allocator_t *allocator, bal_engine_t *engine)
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->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;
@@ -88,19 +79,10 @@ bal_engine_init (bal_allocator_t *allocator, bal_engine_t *engine)
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;
}
@@ -124,8 +106,7 @@ 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;
@@ -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,57 +158,12 @@ 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)
= (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);
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;
++bit_width_cursor;
++instruction_count;
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);
}
else
{
uint32_t constant_index
= intern_constant((uint32_t)value,
uint32_t mask_index = intern_constant((uint32_t)clear_mask,
constants,
&constant_count,
engine->constants_size,
@@ -241,10 +175,44 @@ bal_engine_translate (bal_engine_t *BAL_RESTRICT engine,
}
*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_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);
// 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);
}
else
{
uint32_t constant_index = intern_constant((uint32_t)value,
constants,
&constant_count,
engine->constants_size,
&status);
if (BAL_UNLIKELY(status != BAL_SUCCESS))
{
break;
}
*ir_instruction_cursor
= ((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.
@@ -279,12 +247,10 @@ 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;
}
@@ -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)
{

View File

@@ -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
{
@@ -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,8 +63,7 @@ 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
@@ -121,15 +117,13 @@ _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.
//