From 76cca67422a08ce1951c85d2dc57e12803e71a51 Mon Sep 17 00:00:00 2001 From: Luciano Passos Almeida Date: Mon, 6 Jul 2026 17:14:12 -0300 Subject: [PATCH] [NFC] Cargo fmt and fix clippy issues --- src/injector_core/internal.rs | 18 ++- src/injector_core/thread_local_registry.rs | 166 ++++++++++++--------- src/interface/injector.rs | 63 ++++---- tests/global.rs | 10 +- tests/hyper.rs | 3 +- tests/lifetime_safety.rs | 53 +++++-- tests/thread_safety.rs | 10 +- tests/will_execute.rs | 3 +- 8 files changed, 199 insertions(+), 127 deletions(-) diff --git a/src/injector_core/internal.rs b/src/injector_core/internal.rs index 360586c..2722da8 100644 --- a/src/injector_core/internal.rs +++ b/src/injector_core/internal.rs @@ -4,10 +4,10 @@ use super::patch_trait::PatchTrait; #[cfg(target_arch = "x86_64")] use super::patch_amd64::PatchAmd64; -#[cfg(target_arch = "aarch64")] -use super::patch_arm64::PatchArm64; #[cfg(target_arch = "arm")] use super::patch_arm::PatchArm; +#[cfg(target_arch = "aarch64")] +use super::patch_arm64::PatchArm64; #[cfg(any(target_arch = "x86_64", target_arch = "aarch64", target_arch = "arm"))] use super::thread_local_registry; @@ -49,10 +49,7 @@ impl WhenCalled { /// The original function is patched to a dispatcher that routes calls /// to per-thread replacement functions. #[cfg(any(target_arch = "x86_64", target_arch = "aarch64", target_arch = "arm"))] - pub(crate) fn will_execute_thread_local( - self, - target: FuncPtrInternal, - ) -> ThreadRegistration { + pub(crate) fn will_execute_thread_local(self, target: FuncPtrInternal) -> ThreadRegistration { let replacement_addr = target.as_ptr() as usize; thread_local_registry::register_replacement(&self.func_ptr, replacement_addr, None) } @@ -64,8 +61,13 @@ impl WhenCalled { #[cfg(target_arch = "x86_64")] let (jit_size, asm_code_vec) = { let code: [u8; 8] = [ - 0x48, 0xC7, 0xC0, // mov rax, imm32 - value as u8, 0x00, 0x00, 0x00, // imm32 + 0x48, + 0xC7, + 0xC0, // mov rax, imm32 + value as u8, + 0x00, + 0x00, + 0x00, // imm32 0xC3, // ret ]; (8usize, code.to_vec()) diff --git a/src/injector_core/thread_local_registry.rs b/src/injector_core/thread_local_registry.rs index f244f48..04e53ce 100644 --- a/src/injector_core/thread_local_registry.rs +++ b/src/injector_core/thread_local_registry.rs @@ -166,10 +166,7 @@ pub(crate) extern "C" fn get_thread_target(method_key: usize, default_target: us /// Inactive entries (ref_count == 0) are skipped — their dispatchers are permanent /// but their patches can safely be overwritten since no thread routes through them. #[cfg(target_arch = "arm")] -fn check_arm32_patch_overlap( - func_addr: *mut u8, - registry: &HashMap, -) { +fn check_arm32_patch_overlap(func_addr: *mut u8, registry: &HashMap) { let clean_addr = (func_addr as usize) & !1; let patch_size = 4; // minimum patch size on ARM32 @@ -364,8 +361,7 @@ fn install_dispatcher_aarch64(func_addr: *mut u8, method_key: usize) -> MethodEn // Step 4: Generate dispatcher code with the real trampoline address and write // to the pre-allocated buffer. - let dispatcher_code = - build_dispatcher_code_aarch64(method_key as u64, trampoline_addr as u64); + let dispatcher_code = build_dispatcher_code_aarch64(method_key as u64, trampoline_addr as u64); assert!( dispatcher_code.len() <= DISPATCHER_MAX_SIZE, "Dispatcher code ({} bytes) exceeds pre-allocated buffer ({} bytes)", @@ -442,17 +438,17 @@ fn build_dispatcher_code_aarch64(method_key_val: u64, trampoline_val: u64) -> Ve emit_sub_sp_imm(&mut code, 224); // Save integer registers: x0-x7, x8, x30 - emit_stp_x(&mut code, 0, 1, 0); // stp x0, x1, [sp, #0] - emit_stp_x(&mut code, 2, 3, 16); // stp x2, x3, [sp, #16] - emit_stp_x(&mut code, 4, 5, 32); // stp x4, x5, [sp, #32] - emit_stp_x(&mut code, 6, 7, 48); // stp x6, x7, [sp, #48] - emit_stp_x(&mut code, 8, 30, 64); // stp x8, x30, [sp, #64] + emit_stp_x(&mut code, 0, 1, 0); // stp x0, x1, [sp, #0] + emit_stp_x(&mut code, 2, 3, 16); // stp x2, x3, [sp, #16] + emit_stp_x(&mut code, 4, 5, 32); // stp x4, x5, [sp, #32] + emit_stp_x(&mut code, 6, 7, 48); // stp x6, x7, [sp, #48] + emit_stp_x(&mut code, 8, 30, 64); // stp x8, x30, [sp, #64] // Save SIMD/FP registers: q0-q7 - emit_stp_q(&mut code, 0, 1, 80); // stp q0, q1, [sp, #80] - emit_stp_q(&mut code, 2, 3, 112); // stp q2, q3, [sp, #112] - emit_stp_q(&mut code, 4, 5, 144); // stp q4, q5, [sp, #144] - emit_stp_q(&mut code, 6, 7, 176); // stp q6, q7, [sp, #176] + emit_stp_q(&mut code, 0, 1, 80); // stp q0, q1, [sp, #80] + emit_stp_q(&mut code, 2, 3, 112); // stp q2, q3, [sp, #112] + emit_stp_q(&mut code, 4, 5, 144); // stp q4, q5, [sp, #144] + emit_stp_q(&mut code, 6, 7, 176); // stp q6, q7, [sp, #176] // Load arguments for get_thread_target(method_key, trampoline_addr) // x0 = method_key, x1 = trampoline_addr @@ -496,10 +492,7 @@ fn build_dispatcher_code_aarch64(method_key_val: u64, trampoline_val: u64) -> Ve /// PC-relative instructions (ADRP, ADR, B/BL, LDR literal, etc.) are adjusted to /// account for the trampoline's different address. #[cfg(target_arch = "aarch64")] -fn create_trampoline_aarch64( - func_addr: *mut u8, - copy_size: usize, -) -> (*mut u8, usize) { +fn create_trampoline_aarch64(func_addr: *mut u8, copy_size: usize) -> (*mut u8, usize) { // The jump-back uses MOVZ + MOVK×3 + BR = 20 bytes (5 instructions) let jump_back_size = 20; let trampoline_total = copy_size + jump_back_size; @@ -595,7 +588,12 @@ fn fixup_aarch64_pc_relative_buf( for i in 0..num_insns { let offset = i * 4; - let insn = u32::from_le_bytes([buf[offset], buf[offset + 1], buf[offset + 2], buf[offset + 3]]); + let insn = u32::from_le_bytes([ + buf[offset], + buf[offset + 1], + buf[offset + 2], + buf[offset + 3], + ]); let orig_pc = original_addr as i64 + (i as i64) * 4; let tramp_pc = trampoline_addr as i64 + (i as i64) * 4; @@ -632,9 +630,8 @@ fn fixup_aarch64_pc_relative_buf( let new_u = (new_imm21 as u32) & 0x1FFFFF; let new_immhi = (new_u >> 2) & 0x7FFFF; let new_immlo = new_u & 0x3; - let new_insn = (insn & !(0x7FFFF << 5) & !(0x3 << 29)) - | (new_immhi << 5) - | (new_immlo << 29); + let new_insn = + (insn & !(0x7FFFF << 5) & !(0x3 << 29)) | (new_immhi << 5) | (new_immlo << 29); buf[offset..offset + 4].copy_from_slice(&new_insn.to_le_bytes()); } else { buf[offset..offset + 4].copy_from_slice(&nop.to_le_bytes()); @@ -652,8 +649,7 @@ fn fixup_aarch64_pc_relative_buf( }; let new_imm26 = (imm26_signed as i64) + delta_insns; if new_imm26 >= -(1 << 25) && new_imm26 < (1 << 25) { - let new_insn = - (insn & 0xFC000000) | ((new_imm26 as u32) & 0x03FFFFFF); + let new_insn = (insn & 0xFC000000) | ((new_imm26 as u32) & 0x03FFFFFF); buf[offset..offset + 4].copy_from_slice(&new_insn.to_le_bytes()); } else { buf[offset..offset + 4].copy_from_slice(&nop.to_le_bytes()); @@ -674,8 +670,7 @@ fn fixup_aarch64_pc_relative_buf( }; let new_imm19 = (imm19_signed as i64) + delta_insns; if new_imm19 >= -(1 << 18) && new_imm19 < (1 << 18) { - let new_insn = - (insn & !(0x7FFFF << 5)) | (((new_imm19 as u32) & 0x7FFFF) << 5); + let new_insn = (insn & !(0x7FFFF << 5)) | (((new_imm19 as u32) & 0x7FFFF) << 5); buf[offset..offset + 4].copy_from_slice(&new_insn.to_le_bytes()); } else { buf[offset..offset + 4].copy_from_slice(&nop.to_le_bytes()); @@ -693,8 +688,7 @@ fn fixup_aarch64_pc_relative_buf( }; let new_imm14 = (imm14_signed as i64) + delta_insns; if new_imm14 >= -(1 << 13) && new_imm14 < (1 << 13) { - let new_insn = - (insn & !(0x3FFF << 5)) | (((new_imm14 as u32) & 0x3FFF) << 5); + let new_insn = (insn & !(0x3FFF << 5)) | (((new_imm14 as u32) & 0x3FFF) << 5); buf[offset..offset + 4].copy_from_slice(&new_insn.to_le_bytes()); } else { buf[offset..offset + 4].copy_from_slice(&nop.to_le_bytes()); @@ -808,9 +802,7 @@ fn emit_mov_reg(code: &mut Vec, rd: u8, rn: u8) { #[cfg(target_arch = "arm")] fn calculate_thumb_copy_size(func_addr: *mut u8, min_bytes: usize) -> usize { // Check if the function starts with BX LR (0x4770) — a 2-byte function - let first_hw = unsafe { - u16::from_le_bytes([func_addr.read(), func_addr.add(1).read()]) - }; + let first_hw = unsafe { u16::from_le_bytes([func_addr.read(), func_addr.add(1).read()]) }; if first_hw == 0x4770 && min_bytes > 2 { panic!( "injectorpp: Function at {:#x} is only 2 bytes (BX LR), too small for \ @@ -845,8 +837,9 @@ fn install_dispatcher_arm32(func_addr: *mut u8, method_key: usize) -> MethodEntr // Clear the Thumb bit for actual memory operations let func_addr_clean = (func_addr as usize & !1) as *mut u8; - let near_src = - unsafe { FuncPtrInternal::new(std::ptr::NonNull::new(func_addr_clean as *mut ()).unwrap()) }; + let near_src = unsafe { + FuncPtrInternal::new(std::ptr::NonNull::new(func_addr_clean as *mut ()).unwrap()) + }; // Step 1: Pre-allocate dispatcher buffer to determine its address. // 64 bytes fits an optional 12-byte Thumb stub + 52-byte ARM dispatcher. @@ -857,7 +850,11 @@ fn install_dispatcher_arm32(func_addr: *mut u8, method_key: usize) -> MethodEntr // Step 2: Determine patch size based on distance to dispatcher. // B.W (Thumb) has ±16MB range, B (ARM) has ±32MB range. let distance = dispatcher_addr.abs_diff(func_addr_clean as usize); - let max_b_range = if is_thumb { 16 * 1024 * 1024 } else { 32 * 1024 * 1024 }; + let max_b_range = if is_thumb { + 16 * 1024 * 1024 + } else { + 32 * 1024 * 1024 + }; let patch_size = if distance < max_b_range { 4 } else { 12 }; // Step 3: Calculate the trampoline copy size. For Thumb, we must copy @@ -877,7 +874,8 @@ fn install_dispatcher_arm32(func_addr: *mut u8, method_key: usize) -> MethodEntr // For Thumb functions with a 4-byte patch, a Thumb-mode stub at the start // of the JIT buffer transitions to the ARM-mode dispatcher, because Thumb // B.W cannot switch processor mode. - let arm_dispatcher_code = build_dispatcher_code_arm32(method_key as u32, trampoline_addr as u32); + let arm_dispatcher_code = + build_dispatcher_code_arm32(method_key as u32, trampoline_addr as u32); if is_thumb && patch_size == 4 { let arm_code_addr = (dispatcher_addr + 12) as u32; @@ -888,17 +886,24 @@ fn install_dispatcher_arm32(func_addr: *mut u8, method_key: usize) -> MethodEntr let mut full_code = Vec::with_capacity(total_size); full_code.extend_from_slice(&stub); full_code.extend_from_slice(&arm_dispatcher_code); - unsafe { inject_asm_code(&full_code, dispatcher); } + unsafe { + inject_asm_code(&full_code, dispatcher); + } } else { assert!(arm_dispatcher_code.len() <= dispatcher_max_size); - unsafe { inject_asm_code(&arm_dispatcher_code, dispatcher); } + unsafe { + inject_asm_code(&arm_dispatcher_code, dispatcher); + } } // Step 5: Generate branch patch. // 4-byte Thumb B.W → Thumb stub at offset 0; 4-byte ARM B → dispatcher at offset 0. // 12-byte patches encode the dispatcher address directly. let patch = generate_branch_patch_arm32( - func_addr_clean as usize, dispatcher_addr, is_thumb, patch_size, + func_addr_clean as usize, + dispatcher_addr, + is_thumb, + patch_size, ); // Read original bytes before patching @@ -1072,8 +1077,12 @@ fn fixup_arm32_pc_relative( for i in 0..num_insns { let offset = i * 4; - let insn = - u32::from_le_bytes([buf[offset], buf[offset + 1], buf[offset + 2], buf[offset + 3]]); + let insn = u32::from_le_bytes([ + buf[offset], + buf[offset + 1], + buf[offset + 2], + buf[offset + 3], + ]); let orig_pc = original_addr as i64 + (i as i64) * 4 + 8; // ARM: PC = instruction + 8 let tramp_pc = trampoline_addr as i64 + (i as i64) * 4 + 8; @@ -1255,7 +1264,8 @@ fn generate_dispatcher_jit( #[cfg(not(target_os = "windows"))] let code = generate_dispatcher_sysv(method_key, trampoline_addr, fn_addr); - let near_src = unsafe { FuncPtrInternal::new(std::ptr::NonNull::new(near_addr as *mut ()).unwrap()) }; + let near_src = + unsafe { FuncPtrInternal::new(std::ptr::NonNull::new(near_addr as *mut ()).unwrap()) }; let jit_size = code.len(); let jit_mem = allocate_jit_memory(&near_src, jit_size); @@ -1335,11 +1345,7 @@ fn generate_dispatcher_windows( /// Integer args: rdi, rsi, rdx, rcx, r8, r9. Float args: xmm0-xmm7. #[cfg(target_arch = "x86_64")] #[cfg(not(target_os = "windows"))] -fn generate_dispatcher_sysv( - method_key: usize, - trampoline_addr: usize, - fn_addr: usize, -) -> Vec { +fn generate_dispatcher_sysv(method_key: usize, trampoline_addr: usize, fn_addr: usize) -> Vec { let mut code: Vec = Vec::with_capacity(200); // Save integer argument registers (6 registers) @@ -1484,11 +1490,7 @@ fn create_trampoline(func_addr: *mut u8, _method_key: usize) -> (*mut u8, usize, *jmp_ptr.add(4) = 0x00; *jmp_ptr.add(5) = 0x00; // 8-byte absolute target address - std::ptr::copy_nonoverlapping( - jump_back_addr.to_le_bytes().as_ptr(), - jmp_ptr.add(6), - 8, - ); + std::ptr::copy_nonoverlapping(jump_back_addr.to_le_bytes().as_ptr(), jmp_ptr.add(6), 8); // Flush instruction cache for the trampoline clear_cache_ptr(trampoline, trampoline_total); @@ -1569,10 +1571,8 @@ fn fixup_rip_relative_instructions( } else { // Overflow: emit an indirect stub and redirect the CALL/JMP. // Calculate the absolute target address from the original code. - let rip_after_insn = - func_addr as usize + offset + insn_len; - let absolute_target = - (rip_after_insn as i64 + old_rel as i64) as u64; + let rip_after_insn = func_addr as usize + offset + insn_len; + let absolute_target = (rip_after_insn as i64 + old_rel as i64) as u64; assert!( stub_cursor + 12 <= trampoline_alloc_size, @@ -1659,18 +1659,30 @@ fn find_rip_relative_disp_offset(insn: &[u8], _insn_len: usize) -> Option // Single-byte opcodes: check if they have a ModR/M byte match opcode { // Opcodes that do NOT have ModR/M — skip - 0x50..=0x5F | 0x90 | 0xC3 | 0xCC | 0xCB | 0xC9 | 0xF4 | 0xF5 | 0xF8 | 0xF9 - | 0xFC | 0xFD | 0x99 | 0x9E | 0x9F => return None, - 0x6A | 0x04 | 0x0C | 0x14 | 0x1C | 0x24 | 0x2C | 0x34 | 0x3C | 0xCD | 0xEB - | 0xA8 => return None, - 0x70..=0x7F => return None, // Jcc rel8 - 0x05 | 0x0D | 0x15 | 0x1D | 0x25 | 0x2D | 0x35 | 0x3D | 0x68 | 0xA9 => { + 0x50..=0x5F + | 0x90 + | 0xC3 + | 0xCC + | 0xCB + | 0xC9 + | 0xF4 + | 0xF5 + | 0xF8 + | 0xF9 + | 0xFC + | 0xFD + | 0x99 + | 0x9E + | 0x9F => return None, + 0x6A | 0x04 | 0x0C | 0x14 | 0x1C | 0x24 | 0x2C | 0x34 | 0x3C | 0xCD | 0xEB | 0xA8 => { return None } + 0x70..=0x7F => return None, // Jcc rel8 + 0x05 | 0x0D | 0x15 | 0x1D | 0x25 | 0x2D | 0x35 | 0x3D | 0x68 | 0xA9 => return None, 0xE8 | 0xE9 | 0xE3 => return None, // call/jmp rel32, JRCXZ - 0xA0..=0xA3 => return None, // MOV AL/AX moffs - 0xB0..=0xBF => return None, // MOV reg, imm - 0xC2 => return None, // RET imm16 + 0xA0..=0xA3 => return None, // MOV AL/AX moffs + 0xB0..=0xBF => return None, // MOV reg, imm + 0xC2 => return None, // RET imm16 _ => { // Assume has ModR/M — fall through } @@ -1701,9 +1713,7 @@ fn skip_prefixes(code: &[u8]) -> usize { // Skip legacy prefixes while pos < code.len() { match code[pos] { - 0x66 | 0x67 | 0xF0 | 0xF2 | 0xF3 | 0x26 | 0x2E | 0x36 | 0x3E | 0x64 | 0x65 => { - pos += 1 - } + 0x66 | 0x67 | 0xF0 | 0xF2 | 0xF3 | 0x26 | 0x2E | 0x36 | 0x3E | 0x64 | 0x65 => pos += 1, _ => break, } } @@ -1746,9 +1756,7 @@ fn x86_64_insn_len(code: &[u8]) -> usize { // Skip legacy prefixes while pos < code.len() { match code[pos] { - 0x66 | 0x67 | 0xF0 | 0xF2 | 0xF3 | 0x26 | 0x2E | 0x36 | 0x3E | 0x64 | 0x65 => { - pos += 1 - } + 0x66 | 0x67 | 0xF0 | 0xF2 | 0xF3 | 0x26 | 0x2E | 0x36 | 0x3E | 0x64 | 0x65 => pos += 1, _ => break, } } @@ -1775,8 +1783,20 @@ fn x86_64_insn_len(code: &[u8]) -> usize { match opcode { // Single byte, no operands - 0x50..=0x5F | 0x90 | 0xC3 | 0xCC | 0x99 | 0x9E | 0x9F | 0xCB | 0xF4 | 0xF5 | 0xF8 - | 0xF9 | 0xFC | 0xFD => pos, + 0x50..=0x5F + | 0x90 + | 0xC3 + | 0xCC + | 0x99 + | 0x9E + | 0x9F + | 0xCB + | 0xF4 + | 0xF5 + | 0xF8 + | 0xF9 + | 0xFC + | 0xFD => pos, // imm8 operand 0x6A | 0x04 | 0x0C | 0x14 | 0x1C | 0x24 | 0x2C | 0x34 | 0x3C | 0xCD | 0xEB | 0xA8 => { diff --git a/src/interface/injector.rs b/src/interface/injector.rs index e578701..95c4353 100644 --- a/src/interface/injector.rs +++ b/src/interface/injector.rs @@ -347,9 +347,10 @@ impl InjectorPP { F: Future, { let poll_fn: fn(Pin<&mut F>, &mut Context<'_>) -> Poll = ::poll; - let when = WhenCalled::new(unsafe { - FuncPtr::new(poll_fn as *const (), std::any::type_name_of_val(&poll_fn)) - }.func_ptr_internal); + let when = WhenCalled::new( + unsafe { FuncPtr::new(poll_fn as *const (), std::any::type_name_of_val(&poll_fn)) } + .func_ptr_internal, + ); let signature = fake_pair.1; WhenCalledBuilderAsync { @@ -407,9 +408,10 @@ impl InjectorPP { F: Future, { let poll_fn: fn(Pin<&mut F>, &mut Context<'_>) -> Poll = ::poll; - let when = WhenCalled::new(unsafe { - FuncPtr::new(poll_fn as *const (), std::any::type_name_of_val(&poll_fn)) - }.func_ptr_internal); + let when = WhenCalled::new( + unsafe { FuncPtr::new(poll_fn as *const (), std::any::type_name_of_val(&poll_fn)) } + .func_ptr_internal, + ); WhenCalledBuilderAsync { lib: self, @@ -506,16 +508,16 @@ impl WhenCalledBuilder<'_> { self.expected_signature, target.signature ); } - (None, _) | (_, None) => { + (None, _) | (_, None) if normalize_signature(target.signature) - != normalize_signature(self.expected_signature) - { - panic!( - "Signature mismatch: expected {:?} but got {:?}", - self.expected_signature, target.signature - ); - } + != normalize_signature(self.expected_signature) => + { + panic!( + "Signature mismatch: expected {:?} but got {:?}", + self.expected_signature, target.signature + ); } + _ => {} } @@ -525,7 +527,9 @@ impl WhenCalledBuilder<'_> { } else { #[cfg(any(target_arch = "x86_64", target_arch = "aarch64", target_arch = "arm"))] { - let reg = self.when.will_execute_thread_local(target.func_ptr_internal); + let reg = self + .when + .will_execute_thread_local(target.func_ptr_internal); self.lib.registrations.push(reg); } @@ -597,7 +601,9 @@ impl WhenCalledBuilder<'_> { } else { #[cfg(any(target_arch = "x86_64", target_arch = "aarch64", target_arch = "arm"))] { - let reg = self.when.will_execute_thread_local(target.func_ptr_internal); + let reg = self + .when + .will_execute_thread_local(target.func_ptr_internal); self.lib.registrations.push(reg); } @@ -737,16 +743,16 @@ impl WhenCalledBuilderAsync<'_> { self.expected_signature, target.signature ); } - (None, _) | (_, None) => { + (None, _) | (_, None) if normalize_signature(target.signature) - != normalize_signature(self.expected_signature) - { - panic!( - "Signature mismatch: expected {:?} but got {:?}", - self.expected_signature, target.signature - ); - } + != normalize_signature(self.expected_signature) => + { + panic!( + "Signature mismatch: expected {:?} but got {:?}", + self.expected_signature, target.signature + ); } + _ => {} } @@ -756,7 +762,9 @@ impl WhenCalledBuilderAsync<'_> { } else { #[cfg(any(target_arch = "x86_64", target_arch = "aarch64", target_arch = "arm"))] { - let reg = self.when.will_execute_thread_local(target.func_ptr_internal); + let reg = self + .when + .will_execute_thread_local(target.func_ptr_internal); self.lib.registrations.push(reg); } @@ -806,7 +814,9 @@ impl WhenCalledBuilderAsync<'_> { } else { #[cfg(any(target_arch = "x86_64", target_arch = "aarch64", target_arch = "arm"))] { - let reg = self.when.will_execute_thread_local(target.func_ptr_internal); + let reg = self + .when + .will_execute_thread_local(target.func_ptr_internal); self.lib.registrations.push(reg); } @@ -818,4 +828,3 @@ impl WhenCalledBuilderAsync<'_> { } } } - diff --git a/tests/global.rs b/tests/global.rs index efe9911..bdee8a5 100644 --- a/tests/global.rs +++ b/tests/global.rs @@ -86,7 +86,10 @@ fn test_global_fake_closure_cross_thread() { let mut injector = InjectorPP::new_global(); injector .when_called(injectorpp::func!(fn (global_multiply)(i32, i32) -> i32)) - .will_execute_raw(injectorpp::closure!(|_a: i32, _b: i32| -> i32 { 777 }, fn(i32, i32) -> i32)); + .will_execute_raw(injectorpp::closure!( + |_a: i32, _b: i32| -> i32 { 777 }, + fn(i32, i32) -> i32 + )); assert_eq!(global_multiply(3, 4), 777); @@ -193,7 +196,10 @@ fn test_thread_local_mode_not_visible_from_spawned_thread() { let mut injector = InjectorPP::new(); injector .when_called(injectorpp::func!(fn (global_add)(i32, i32) -> i32)) - .will_execute_raw(injectorpp::closure!(|_a: i32, _b: i32| -> i32 { 9999 }, fn(i32, i32) -> i32)); + .will_execute_raw(injectorpp::closure!( + |_a: i32, _b: i32| -> i32 { 9999 }, + fn(i32, i32) -> i32 + )); // Test thread sees the fake assert_eq!(global_add(1, 2), 9999); diff --git a/tests/hyper.rs b/tests/hyper.rs index 9af3c4d..b6cfb93 100644 --- a/tests/hyper.rs +++ b/tests/hyper.rs @@ -36,8 +36,7 @@ fn make_tcp_with_http_response() -> std::io::Result { } } - let body = - r#"{"status": "ok", "message": "mock response", "headers": {"User-Agent": "hyper-test/1.0"}}"#; + let body = r#"{"status": "ok", "message": "mock response", "headers": {"User-Agent": "hyper-test/1.0"}}"#; let response = format!( "HTTP/1.1 200 OK\r\n\ Content-Type: application/json\r\n\ diff --git a/tests/lifetime_safety.rs b/tests/lifetime_safety.rs index 0d64a43..0284aa5 100644 --- a/tests/lifetime_safety.rs +++ b/tests/lifetime_safety.rs @@ -14,18 +14,38 @@ use injectorpp::interface::injector::*; /// Helper: tries to compile a source file and returns whether it succeeded. /// Returns None if build artifacts can't be found (e.g. cross-compilation). fn try_compile(source_path: &str) -> Option { - let rlib = find_file(&["target/debug/deps", "target/debug"], "libinjectorpp", ".rlib")?; - let ext = if cfg!(windows) { ".dll" } else if cfg!(target_os = "macos") { ".dylib" } else { ".so" }; - let proc_dylib = find_file(&["target/debug/deps", "target/debug"], "injectorpp_macros", ext)?; + let rlib = find_file( + &["target/debug/deps", "target/debug"], + "libinjectorpp", + ".rlib", + )?; + let ext = if cfg!(windows) { + ".dll" + } else if cfg!(target_os = "macos") { + ".dylib" + } else { + ".so" + }; + let proc_dylib = find_file( + &["target/debug/deps", "target/debug"], + "injectorpp_macros", + ext, + )?; let output = std::process::Command::new("rustc") .args([ - "--edition", "2021", - "--crate-type", "bin", - "-L", "target/debug/deps", - "--extern", &format!("injectorpp={}", rlib), - "--extern", &format!("injectorpp_macros={}", proc_dylib), - "-o", if cfg!(windows) { "NUL" } else { "/dev/null" }, + "--edition", + "2021", + "--crate-type", + "bin", + "-L", + "target/debug/deps", + "--extern", + &format!("injectorpp={}", rlib), + "--extern", + &format!("injectorpp_macros={}", proc_dylib), + "-o", + if cfg!(windows) { "NUL" } else { "/dev/null" }, source_path, ]) .output() @@ -50,7 +70,10 @@ fn find_file(dirs: &[&str], prefix: &str, suffix: &str) -> Option { #[test] fn static_str_coerced_to_bare_ref_must_not_compile() { match try_compile("tests/compile_fail/static_str_coerced_to_bare_ref.rs") { - Some(compiled) => assert!(!compiled, "expected compile error: &'static str coerced to bare &str should be rejected"), + Some(compiled) => assert!( + !compiled, + "expected compile error: &'static str coerced to bare &str should be rejected" + ), None => eprintln!("skipped: build artifacts not found"), } } @@ -58,7 +81,10 @@ fn static_str_coerced_to_bare_ref_must_not_compile() { #[test] fn static_slice_coerced_to_bare_ref_must_not_compile() { match try_compile("tests/compile_fail/static_slice_coerced_to_bare_ref.rs") { - Some(compiled) => assert!(!compiled, "expected compile error: &'static [u8] coerced to bare &[u8] should be rejected"), + Some(compiled) => assert!( + !compiled, + "expected compile error: &'static [u8] coerced to bare &[u8] should be rejected" + ), None => eprintln!("skipped: build artifacts not found"), } } @@ -66,7 +92,10 @@ fn static_slice_coerced_to_bare_ref_must_not_compile() { #[test] fn func_info_prefix_lifetime_mismatch_must_not_compile() { match try_compile("tests/compile_fail/func_info_prefix_lifetime_mismatch.rs") { - Some(compiled) => assert!(!compiled, "expected compile error: lifetime mismatch with func_info: prefix should be rejected"), + Some(compiled) => assert!( + !compiled, + "expected compile error: lifetime mismatch with func_info: prefix should be rejected" + ), None => eprintln!("skipped: build artifacts not found"), } } diff --git a/tests/thread_safety.rs b/tests/thread_safety.rs index f7b24aa..d419ceb 100644 --- a/tests/thread_safety.rs +++ b/tests/thread_safety.rs @@ -859,7 +859,10 @@ fn test_string_return_thread_isolation() { let mut injector = InjectorPP::new(); injector .when_called(injectorpp::func!(fn(get_greeting)() -> String)) - .will_execute_raw(injectorpp::closure!(|| { "from_thread_1".to_string() }, fn() -> String)); + .will_execute_raw(injectorpp::closure!( + || { "from_thread_1".to_string() }, + fn() -> String + )); b1.wait(); if get_greeting() != "from_thread_1" { e1.fetch_add(1, Ordering::SeqCst); @@ -873,7 +876,10 @@ fn test_string_return_thread_isolation() { let mut injector = InjectorPP::new(); injector .when_called(injectorpp::func!(fn(get_greeting)() -> String)) - .will_execute_raw(injectorpp::closure!(|| { "from_thread_2".to_string() }, fn() -> String)); + .will_execute_raw(injectorpp::closure!( + || { "from_thread_2".to_string() }, + fn() -> String + )); b2.wait(); if get_greeting() != "from_thread_2" { e2.fetch_add(1, Ordering::SeqCst); diff --git a/tests/will_execute.rs b/tests/will_execute.rs index 5b87398..4b24657 100644 --- a/tests/will_execute.rs +++ b/tests/will_execute.rs @@ -160,7 +160,8 @@ fn test_will_execute_when_fake_no_return_function_over_called_should_panic() { let message = result.unwrap_err(); let message_str = message - .downcast_ref::<&str>().copied() + .downcast_ref::<&str>() + .copied() .or_else(|| message.downcast_ref::().map(|s| s.as_str())) .unwrap();