code cleanup

const etc
This commit is contained in:
Lysann Tranvouez 2025-09-29 01:19:23 +02:00
parent d8d3d85444
commit dbdfa84fab
3 changed files with 17 additions and 10 deletions

View file

@ -56,12 +56,12 @@ static_assert(sizeof(detour_trampoline) == 192);
typedef uint32_t detours_arm64_opcode_t; typedef uint32_t detours_arm64_opcode_t;
static inline detours_arm64_opcode_t fetch_opcode(uint8_t* code) static inline detours_arm64_opcode_t fetch_opcode(const uint8_t* code)
{ {
return *(detours_arm64_opcode_t*)code; return *(detours_arm64_opcode_t*)code;
} }
static inline void write_opcode(uint8_t** int_out_code, detours_arm64_opcode_t opcode) static inline void write_opcode(uint8_t** int_out_code, const detours_arm64_opcode_t opcode)
{ {
uint8_t* code = *int_out_code; uint8_t* code = *int_out_code;
*(detours_arm64_opcode_t*)code = opcode; *(detours_arm64_opcode_t*)code = opcode;
@ -98,10 +98,10 @@ union detours_arm64_indirect_imm {
uint64_t adrp_immhi : 19; uint64_t adrp_immhi : 19;
}; };
int64_t value; uint64_t value;
}; };
static inline uint8_t* internal_detour_gen_jmp_indirect(uint8_t* code, uint64_t* jump_val) static inline uint8_t* internal_detour_gen_jmp_indirect(uint8_t* code, const uint64_t* jump_val)
{ {
// adrp x17, [jmpval] // adrp x17, [jmpval]
// ldr x17, [x17, jmpval] // ldr x17, [x17, jmpval]
@ -135,7 +135,7 @@ static inline uint8_t* internal_detour_gen_jmp_indirect(uint8_t* code, uint64_t*
return code; return code;
} }
static inline uint8_t* internal_detour_gen_brk(uint8_t* code, uint8_t* limit) static inline uint8_t* internal_detour_gen_brk(uint8_t* code, const uint8_t* limit)
{ {
while (code < limit) { while (code < limit) {
write_opcode(&code, 0xd4100000 | (0xf000 << 5)); write_opcode(&code, 0xd4100000 | (0xf000 << 5));

View file

@ -26,7 +26,11 @@
#endif #endif
#ifndef ARRAYSIZE #ifndef ARRAYSIZE
#define ARRAYSIZE(x) (sizeof(x)/sizeof(x[0])) #define ARRAYSIZE(x) (sizeof(x)/sizeof(x[0]))
#endif
#ifndef UNUSED_VARIABLE
#define UNUSED_VARIABLE(x) (void)(x)
#endif #endif
#endif //MACH_DETOURS_INTERNAL_H #endif //MACH_DETOURS_INTERNAL_H

View file

@ -30,6 +30,7 @@ typedef struct detour_region
detour_trampoline* free_list_head; // List of free trampolines in this region. detour_trampoline* free_list_head; // List of free trampolines in this region.
} detour_region; } detour_region;
// ReSharper disable once CppMultiCharacterLiteral
static const uint32_t DETOUR_REGION_SIGNATURE = 'Rrtd'; static const uint32_t DETOUR_REGION_SIGNATURE = 'Rrtd';
static const uint32_t DETOUR_REGION_SIZE = 0x10000; static const uint32_t DETOUR_REGION_SIZE = 0x10000;
static const uint32_t DETOUR_TRAMPOLINES_PER_REGION = (DETOUR_REGION_SIZE / sizeof(detour_trampoline)) - 1; static const uint32_t DETOUR_TRAMPOLINES_PER_REGION = (DETOUR_REGION_SIZE / sizeof(detour_trampoline)) - 1;
@ -119,7 +120,8 @@ static void internal_detour_free_unused_trampoline_regions()
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Trampoline Helpers // Trampoline Helpers
static uint8_t internal_detour_align_from_trampoline(detour_trampoline* trampoline, uint8_t offset_trampoline) static uint8_t internal_detour_align_from_trampoline(const detour_trampoline* trampoline,
const uint8_t offset_trampoline)
{ {
for (int32_t n = 0; n < ARRAYSIZE(trampoline->align); n++) { for (int32_t n = 0; n < ARRAYSIZE(trampoline->align); n++) {
if (trampoline->align[n].offset_trampoline == offset_trampoline) { if (trampoline->align[n].offset_trampoline == offset_trampoline) {
@ -129,7 +131,7 @@ static uint8_t internal_detour_align_from_trampoline(detour_trampoline* trampoli
return 0; return 0;
} }
static uint8_t internal_detour_align_from_target(detour_trampoline* trampoline, uint8_t offset_target) static uint8_t internal_detour_align_from_target(const detour_trampoline* trampoline, const uint8_t offset_target)
{ {
for (int32_t n = 0; n < ARRAYSIZE(trampoline->align); n++) { for (int32_t n = 0; n < ARRAYSIZE(trampoline->align); n++) {
if (trampoline->align[n].offset_target == offset_target) { if (trampoline->align[n].offset_target == offset_target) {
@ -285,6 +287,7 @@ mach_error_t detour_transaction_commit_ex(detour_func_t** out_failed_target)
uint8_t* code = internal_detour_gen_jmp_indirect(operation->target, uint8_t* code = internal_detour_gen_jmp_indirect(operation->target,
(uint64_t*)&(operation->trampoline->ptr_detour)); (uint64_t*)&(operation->trampoline->ptr_detour));
code = internal_detour_gen_brk(code, operation->trampoline->ptr_remain); code = internal_detour_gen_brk(code, operation->trampoline->ptr_remain);
UNUSED_VARIABLE(code);
*operation->pointer = operation->trampoline->code; *operation->pointer = operation->trampoline->code;
#endif // DETOURS_ARM64 #endif // DETOURS_ARM64
@ -315,8 +318,8 @@ mach_error_t detour_transaction_commit_ex(detour_func_t** out_failed_target)
for (detour_pending_thread* thread = s_pending_threads_head; thread != nullptr; thread = thread->next) { for (detour_pending_thread* thread = s_pending_threads_head; thread != nullptr; thread = thread->next) {
arm_thread_state64_t threadState; arm_thread_state64_t threadState;
mach_msg_type_number_t threadStateCnt = ARM_THREAD_STATE64_COUNT; mach_msg_type_number_t threadStateCnt = ARM_THREAD_STATE64_COUNT;
kern_return_t error = thread_get_state(thread->thread, ARM_THREAD_STATE64, (thread_state_t)&threadState, const kern_return_t error = thread_get_state(thread->thread, ARM_THREAD_STATE64, (thread_state_t)&threadState,
&threadStateCnt); &threadStateCnt);
if (error != err_none) { if (error != err_none) {
DETOUR_BREAK(); DETOUR_BREAK();
continue; continue;