thread tests

This commit is contained in:
Lysann Tranvouez 2025-10-03 00:04:52 +02:00
parent bf83e14d26
commit c87ad3c89a
4 changed files with 167 additions and 8 deletions

View file

@ -55,7 +55,7 @@ static mach_error_t internal_detour_writable_trampoline_regions()
DETOUR_BREAK();
result = error;
}
DETOUR_TRACE(("mach_vm_protect(%p, %" PRIu32 ", %d)\n", (void*)pRegion, DETOUR_REGION_SIZE, VM_PROT_READ | VM_PROT_WRITE));
DETOUR_TRACE(("mach_vm_protect(%p, %" PRIu32 ", %d) - internal_detour_writable_trampoline_regions\n", (void*)pRegion, DETOUR_REGION_SIZE, VM_PROT_READ | VM_PROT_WRITE));
}
return result;
}
@ -70,7 +70,7 @@ static void internal_detour_runnable_trampoline_regions()
if (error != err_none) {
DETOUR_BREAK();
}
DETOUR_TRACE(("mach_vm_protect(%p, %" PRIu32 ", %d)\n", (void*)pRegion, DETOUR_REGION_SIZE, VM_PROT_READ | VM_PROT_EXECUTE));
DETOUR_TRACE(("mach_vm_protect(%p, %" PRIu32 ", %d) - internal_detour_runnable_trampoline_regions\n", (void*)pRegion, DETOUR_REGION_SIZE, VM_PROT_READ | VM_PROT_EXECUTE));
}
}
@ -323,7 +323,7 @@ static detour_pending_thread* s_pending_threads_head = nullptr;
static mach_error_t s_pending_error = err_none;
static detour_func_t* s_pending_error_pointer = nullptr;
mach_error_t detour_transaction_begin()
static mach_error_t internal_detour_transaction_begin_managed_impl(bool manage_all_threads)
{
// Make sure only one thread can start a transaction.
thread_t expected = THREAD_NULL;
@ -336,12 +336,28 @@ mach_error_t detour_transaction_begin()
s_pending_threads_head = nullptr;
s_pending_error_pointer = nullptr;
if (manage_all_threads) {
s_pending_error = detour_manage_all_threads();
if (s_pending_error != err_none) {
return s_pending_error;
}
}
// Make sure the trampoline pages are writable.
s_pending_error = internal_detour_writable_trampoline_regions();
return s_pending_error;
}
mach_error_t detour_transaction_begin()
{
return internal_detour_transaction_begin_managed_impl(false);
}
mach_error_t detour_transaction_begin_managed()
{
return internal_detour_transaction_begin_managed_impl(true);
}
mach_error_t detour_transaction_abort()
{
if (s_transaction_thread == THREAD_NULL) {
@ -357,7 +373,7 @@ mach_error_t detour_transaction_abort()
DETOUR_CHECK(
mach_vm_protect(port, (mach_vm_address_t)operation->target, operation->trampoline->restore_code_size, false,
operation->perm));
DETOUR_TRACE(("mach_vm_protect(%p, %" PRIu8 ", %d)\n", (void*)operation->target, operation->trampoline->restore_code_size, operation->perm));
DETOUR_TRACE(("mach_vm_protect(%p, %" PRIu8 ", %d) - detour_transaction_abort\n", (void*)operation->target, operation->trampoline->restore_code_size, operation->perm));
if (operation->kind == detour_operation_kind_attach) {
if (operation->trampoline) {
@ -476,7 +492,7 @@ mach_error_t detour_transaction_commit_ex(detour_func_t** out_failed_target)
DETOUR_CHECK(
mach_vm_protect(port, (mach_vm_address_t)operation->target, operation->trampoline->restore_code_size, false,
operation->perm));
DETOUR_TRACE(("mach_vm_protect(%p, %" PRIu8 ", %d)\n", (void*)operation->target, operation->trampoline->restore_code_size, operation->perm));
DETOUR_TRACE(("mach_vm_protect(%p, %" PRIu8 ", %d) - detour_transaction_commit_ex\n", (void*)operation->target, operation->trampoline->restore_code_size, operation->perm));
if (operation->kind == detour_operation_kind_detach && operation->trampoline) {
internal_detour_free_trampoline(operation->trampoline);
@ -561,6 +577,23 @@ mach_error_t detour_manage_thread(thread_t thread)
return err_none;
}
mach_error_t detour_manage_all_threads()
{
const mach_port_t port = mach_task_self();
thread_act_array_t threads = nullptr;
mach_msg_type_number_t numThreads = 0;
const kern_return_t result = task_threads(port, &threads, &numThreads);
if (result != err_none) {
return result;
}
for (mach_msg_type_number_t i = 0; i < numThreads; i++) {
DETOUR_CHECK( detour_manage_thread(threads[i]) );
}
DETOUR_CHECK( vm_deallocate(port, (vm_address_t)threads, numThreads * sizeof(*threads)) );
return err_none;
}
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Attach/Detach
@ -794,7 +827,7 @@ mach_error_t detour_attach_ex(detour_func_t* inout_pointer, detour_func_t detour
DETOUR_BREAK();
goto fail;
}
DETOUR_TRACE(("mach_vm_protect(%p, %" PRIu32 ", %d)\n", (void*)target, target_override_len, VM_PROT_READ | VM_PROT_WRITE | VM_PROT_COPY));
DETOUR_TRACE(("mach_vm_protect(%p, %" PRIu32 ", %d) - detour_attach_ex\n", (void*)target, target_override_len, VM_PROT_READ | VM_PROT_WRITE | VM_PROT_COPY));
DETOUR_TRACE(("detours: target=%p: "
"%02x %02x %02x %02x "
@ -916,7 +949,7 @@ mach_error_t detour_detach(detour_func_t* inout_pointer, detour_func_t detour)
DETOUR_BREAK();
goto fail;
}
DETOUR_TRACE(("mach_vm_protect(%p, %" PRIu32 ", %d)\n", (void*)target, restore_code_size, VM_PROT_READ | VM_PROT_WRITE | VM_PROT_COPY));
DETOUR_TRACE(("mach_vm_protect(%p, %" PRIu32 ", %d) - detour_detach\n", (void*)target, restore_code_size, VM_PROT_READ | VM_PROT_WRITE | VM_PROT_COPY));
op->kind = detour_operation_kind_detach;
op->pointer = (uint8_t**)inout_pointer;