From 2754826ab67149addc44bc16868d42d982d9deba Mon Sep 17 00:00:00 2001 From: sewer56 Date: Sat, 4 Nov 2023 00:29:55 +0000 Subject: [PATCH 01/27] Changed: Moved Internal common API to `common` --- projects/reloaded-hooks-x86-sys/Cargo.toml | 2 +- .../benches/assembler_bench_64.rs | 12 +++---- .../benches/my_benchmark.rs | 1 - .../src/{ => common}/jit_common.rs | 33 +++++++------------ .../{ => common}/jit_conversions_common.rs | 0 .../src/instructions/call_absolute.rs | 8 ++--- .../src/instructions/call_ip_relative.rs | 8 ++--- .../src/instructions/call_relative.rs | 4 +-- .../src/instructions/jump_absolute.rs | 8 ++--- .../instructions/jump_absolute_indirect.rs | 5 +-- .../src/instructions/jump_ip_relative.rs | 8 ++--- .../src/instructions/jump_relative.rs | 4 +-- .../src/instructions/mov.rs | 4 +-- .../src/instructions/mov_from_stack.rs | 9 ++--- .../src/instructions/multi_pop.rs | 11 +++---- .../src/instructions/multi_push.rs | 11 +++---- .../src/instructions/pop.rs | 12 +++---- .../src/instructions/push.rs | 11 +++---- .../src/instructions/push_const.rs | 9 ++--- .../src/instructions/push_stack.rs | 9 ++--- .../src/instructions/ret.rs | 4 +-- .../src/instructions/stack_alloc.rs | 8 ++--- .../src/instructions/xchg.rs | 3 +- projects/reloaded-hooks-x86-sys/src/lib.rs | 13 +++++--- .../reloaded-hooks-x86-sys/src/x64/jit.rs | 8 ++--- .../reloaded-hooks-x86-sys/src/x86/jit.rs | 8 ++--- 26 files changed, 90 insertions(+), 123 deletions(-) rename projects/reloaded-hooks-x86-sys/src/{ => common}/jit_common.rs (63%) rename projects/reloaded-hooks-x86-sys/src/{ => common}/jit_conversions_common.rs (100%) diff --git a/projects/reloaded-hooks-x86-sys/Cargo.toml b/projects/reloaded-hooks-x86-sys/Cargo.toml index 4f30c8a..91ac591 100644 --- a/projects/reloaded-hooks-x86-sys/Cargo.toml +++ b/projects/reloaded-hooks-x86-sys/Cargo.toml @@ -19,7 +19,7 @@ reloaded-hooks-portable = { version = "0.1.0", path = "../reloaded-hooks-portabl version = "1.20.0" default-features = false # See below for all features -features = ["no_std", "code_asm"] +features = ["no_std", "code_asm", "decoder", "block_encoder", "op_code_info", "instr_info"] [dev-dependencies] criterion = "0.5.1" diff --git a/projects/reloaded-hooks-x86-sys/benches/assembler_bench_64.rs b/projects/reloaded-hooks-x86-sys/benches/assembler_bench_64.rs index a7bf6b2..3d0ca60 100644 --- a/projects/reloaded-hooks-x86-sys/benches/assembler_bench_64.rs +++ b/projects/reloaded-hooks-x86-sys/benches/assembler_bench_64.rs @@ -1,11 +1,11 @@ use std::rc::Rc; -use reloaded_hooks_portable::api::jit::compiler::Jit; -use reloaded_hooks_portable::api::jit::operation_aliases::*; -use reloaded_hooks_portable::api::jit::{compiler::JitError, operation::Operation}; -use reloaded_hooks_x86_sys::x64; -use reloaded_hooks_x86_sys::x64::jit::JitX64; -use reloaded_hooks_x86_sys::x64::Register; +use reloaded_hooks_portable::api::jit::{ + compiler::{Jit, JitError}, + operation::Operation, + operation_aliases::*, +}; +use reloaded_hooks_x86_sys::x64::{self, jit::JitX64, Register}; // Separate function for the code to be benchmarked. #[allow(dead_code)] diff --git a/projects/reloaded-hooks-x86-sys/benches/my_benchmark.rs b/projects/reloaded-hooks-x86-sys/benches/my_benchmark.rs index b11de7d..5a1ec3e 100644 --- a/projects/reloaded-hooks-x86-sys/benches/my_benchmark.rs +++ b/projects/reloaded-hooks-x86-sys/benches/my_benchmark.rs @@ -1,5 +1,4 @@ mod assembler_bench_64; - use assembler_bench_64::{ compile_instructions_64, create_and_assemble_instructions_64, create_operations_64, }; diff --git a/projects/reloaded-hooks-x86-sys/src/jit_common.rs b/projects/reloaded-hooks-x86-sys/src/common/jit_common.rs similarity index 63% rename from projects/reloaded-hooks-x86-sys/src/jit_common.rs rename to projects/reloaded-hooks-x86-sys/src/common/jit_common.rs index 574125c..0d6c1e1 100644 --- a/projects/reloaded-hooks-x86-sys/src/jit_common.rs +++ b/projects/reloaded-hooks-x86-sys/src/common/jit_common.rs @@ -1,31 +1,20 @@ extern crate alloc; -use crate::instructions::call_ip_relative::encode_call_ip_relative; -use crate::instructions::call_relative::encode_call_relative; -use crate::instructions::jump_absolute::encode_jump_absolute; -use crate::instructions::jump_ip_relative::encode_jump_ip_relative; -use crate::instructions::jump_relative::encode_jump_relative; -use crate::instructions::mov::encode_mov; -use crate::instructions::mov_from_stack::encode_mov_from_stack; -use crate::instructions::multi_pop::encode_multi_pop; -use crate::instructions::multi_push::encode_multi_push; -use crate::instructions::pop::encode_pop; -use crate::instructions::push::encode_push; -use crate::instructions::push_const::encode_push_constant; -use crate::instructions::push_stack::encode_push_stack; -use crate::instructions::ret::encode_return; -use crate::instructions::stack_alloc::encode_stack_alloc; -use crate::instructions::xchg::encode_xchg; +use crate::all_registers::AllRegisters; +use crate::common::jit_common::alloc::string::ToString; use crate::instructions::{ - call_absolute::encode_call_absolute, jump_absolute_indirect::encode_jump_absolute_indirect, + call_absolute::encode_call_absolute, call_ip_relative::encode_call_ip_relative, + call_relative::encode_call_relative, jump_absolute::encode_jump_absolute, + jump_absolute_indirect::encode_jump_absolute_indirect, + jump_ip_relative::encode_jump_ip_relative, jump_relative::encode_jump_relative, + mov::encode_mov, mov_from_stack::encode_mov_from_stack, multi_pop::encode_multi_pop, + multi_push::encode_multi_push, pop::encode_pop, push::encode_push, + push_const::encode_push_constant, push_stack::encode_push_stack, ret::encode_return, + stack_alloc::encode_stack_alloc, xchg::encode_xchg, }; -use crate::jit_common::alloc::string::ToString; -use iced_x86::code_asm::CodeAssembler; -use iced_x86::IcedError; +use iced_x86::{code_asm::CodeAssembler, IcedError}; use reloaded_hooks_portable::api::jit::{compiler::JitError, operation::Operation}; -use crate::all_registers::AllRegisters; - pub const ARCH_NOT_SUPPORTED: &str = "Non 32/64bit architectures are not supported"; pub(crate) fn encode_instruction( diff --git a/projects/reloaded-hooks-x86-sys/src/jit_conversions_common.rs b/projects/reloaded-hooks-x86-sys/src/common/jit_conversions_common.rs similarity index 100% rename from projects/reloaded-hooks-x86-sys/src/jit_conversions_common.rs rename to projects/reloaded-hooks-x86-sys/src/common/jit_conversions_common.rs diff --git a/projects/reloaded-hooks-x86-sys/src/instructions/call_absolute.rs b/projects/reloaded-hooks-x86-sys/src/instructions/call_absolute.rs index 3ce1b20..2c012d1 100644 --- a/projects/reloaded-hooks-x86-sys/src/instructions/call_absolute.rs +++ b/projects/reloaded-hooks-x86-sys/src/instructions/call_absolute.rs @@ -1,13 +1,11 @@ extern crate alloc; + +use crate::all_registers::AllRegisters; +use crate::common::jit_common::{convert_error, ARCH_NOT_SUPPORTED}; use alloc::string::ToString; use iced_x86::code_asm::CodeAssembler; use reloaded_hooks_portable::api::jit::{compiler::JitError, operation_aliases::CallAbs}; -use crate::{ - all_registers::AllRegisters, - jit_common::{convert_error, ARCH_NOT_SUPPORTED}, -}; - pub(crate) fn encode_call_absolute( a: &mut CodeAssembler, x: &CallAbs, diff --git a/projects/reloaded-hooks-x86-sys/src/instructions/call_ip_relative.rs b/projects/reloaded-hooks-x86-sys/src/instructions/call_ip_relative.rs index b5fcb9d..cab2890 100644 --- a/projects/reloaded-hooks-x86-sys/src/instructions/call_ip_relative.rs +++ b/projects/reloaded-hooks-x86-sys/src/instructions/call_ip_relative.rs @@ -1,11 +1,11 @@ extern crate alloc; + +use crate::all_registers::AllRegisters; +use crate::common::jit_common::convert_error; use alloc::string::ToString; -use iced_x86::code_asm::CodeAssembler; +use iced_x86::code_asm::{qword_ptr, CodeAssembler}; use reloaded_hooks_portable::api::jit::{compiler::JitError, operation_aliases::CallIpRel}; -use crate::{all_registers::AllRegisters, jit_common::convert_error}; -use iced_x86::code_asm::qword_ptr; - pub(crate) fn encode_call_ip_relative( a: &mut CodeAssembler, x: &CallIpRel, diff --git a/projects/reloaded-hooks-x86-sys/src/instructions/call_relative.rs b/projects/reloaded-hooks-x86-sys/src/instructions/call_relative.rs index 9be8e7f..67f3306 100644 --- a/projects/reloaded-hooks-x86-sys/src/instructions/call_relative.rs +++ b/projects/reloaded-hooks-x86-sys/src/instructions/call_relative.rs @@ -1,8 +1,8 @@ +use crate::all_registers::AllRegisters; +use crate::common::jit_common::convert_error; use iced_x86::code_asm::CodeAssembler; use reloaded_hooks_portable::api::jit::{compiler::JitError, operation_aliases::CallRel}; -use crate::{all_registers::AllRegisters, jit_common::convert_error}; - pub(crate) fn encode_call_relative( a: &mut CodeAssembler, x: &CallRel, diff --git a/projects/reloaded-hooks-x86-sys/src/instructions/jump_absolute.rs b/projects/reloaded-hooks-x86-sys/src/instructions/jump_absolute.rs index 0b75587..40a15ef 100644 --- a/projects/reloaded-hooks-x86-sys/src/instructions/jump_absolute.rs +++ b/projects/reloaded-hooks-x86-sys/src/instructions/jump_absolute.rs @@ -1,13 +1,11 @@ extern crate alloc; + +use crate::all_registers::AllRegisters; +use crate::common::jit_common::{convert_error, ARCH_NOT_SUPPORTED}; use alloc::string::ToString; use iced_x86::code_asm::CodeAssembler; use reloaded_hooks_portable::api::jit::{compiler::JitError, operation_aliases::JumpAbs}; -use crate::{ - all_registers::AllRegisters, - jit_common::{convert_error, ARCH_NOT_SUPPORTED}, -}; - pub(crate) fn encode_jump_absolute( a: &mut CodeAssembler, x: &JumpAbs, diff --git a/projects/reloaded-hooks-x86-sys/src/instructions/jump_absolute_indirect.rs b/projects/reloaded-hooks-x86-sys/src/instructions/jump_absolute_indirect.rs index b3dfa35..df9f17b 100644 --- a/projects/reloaded-hooks-x86-sys/src/instructions/jump_absolute_indirect.rs +++ b/projects/reloaded-hooks-x86-sys/src/instructions/jump_absolute_indirect.rs @@ -1,9 +1,10 @@ extern crate alloc; + +use crate::all_registers::AllRegisters; +use crate::common::jit_common::convert_error; use iced_x86::code_asm::{dword_ptr, qword_ptr, CodeAssembler}; use reloaded_hooks_portable::api::jit::{compiler::JitError, operation_aliases::JumpAbsInd}; -use crate::{all_registers::AllRegisters, jit_common::convert_error}; - pub(crate) fn encode_jump_absolute_indirect( a: &mut CodeAssembler, x: &JumpAbsInd, diff --git a/projects/reloaded-hooks-x86-sys/src/instructions/jump_ip_relative.rs b/projects/reloaded-hooks-x86-sys/src/instructions/jump_ip_relative.rs index 0796a66..a889a37 100644 --- a/projects/reloaded-hooks-x86-sys/src/instructions/jump_ip_relative.rs +++ b/projects/reloaded-hooks-x86-sys/src/instructions/jump_ip_relative.rs @@ -1,12 +1,12 @@ extern crate alloc; + +use crate::all_registers::AllRegisters; +use crate::common::jit_common::convert_error; use alloc::string::ToString; -use iced_x86::code_asm::CodeAssembler; +use iced_x86::code_asm::{qword_ptr, CodeAssembler}; use reloaded_hooks_portable::api::jit::compiler::JitError; use reloaded_hooks_portable::api::jit::operation_aliases::JumpIpRel; -use crate::{all_registers::AllRegisters, jit_common::convert_error}; -use iced_x86::code_asm::qword_ptr; - pub(crate) fn encode_jump_ip_relative( a: &mut CodeAssembler, x: &JumpIpRel, diff --git a/projects/reloaded-hooks-x86-sys/src/instructions/jump_relative.rs b/projects/reloaded-hooks-x86-sys/src/instructions/jump_relative.rs index a886737..1d12fca 100644 --- a/projects/reloaded-hooks-x86-sys/src/instructions/jump_relative.rs +++ b/projects/reloaded-hooks-x86-sys/src/instructions/jump_relative.rs @@ -1,8 +1,8 @@ +use crate::all_registers::AllRegisters; +use crate::common::jit_common::convert_error; use iced_x86::code_asm::CodeAssembler; use reloaded_hooks_portable::api::jit::{compiler::JitError, operation_aliases::JumpRel}; -use crate::{all_registers::AllRegisters, jit_common::convert_error}; - pub(crate) fn encode_jump_relative( a: &mut CodeAssembler, x: &JumpRel, diff --git a/projects/reloaded-hooks-x86-sys/src/instructions/mov.rs b/projects/reloaded-hooks-x86-sys/src/instructions/mov.rs index f309c9c..ac916dd 100644 --- a/projects/reloaded-hooks-x86-sys/src/instructions/mov.rs +++ b/projects/reloaded-hooks-x86-sys/src/instructions/mov.rs @@ -1,8 +1,8 @@ +use crate::all_registers::AllRegisters; +use crate::common::jit_common::convert_error; use iced_x86::code_asm::CodeAssembler; use reloaded_hooks_portable::api::jit::{compiler::JitError, operation_aliases::Mov}; -use crate::{all_registers::AllRegisters, jit_common::convert_error}; - pub(crate) fn encode_mov( a: &mut CodeAssembler, mov: &Mov, diff --git a/projects/reloaded-hooks-x86-sys/src/instructions/mov_from_stack.rs b/projects/reloaded-hooks-x86-sys/src/instructions/mov_from_stack.rs index 9118758..00aa49f 100644 --- a/projects/reloaded-hooks-x86-sys/src/instructions/mov_from_stack.rs +++ b/projects/reloaded-hooks-x86-sys/src/instructions/mov_from_stack.rs @@ -1,14 +1,11 @@ extern crate alloc; + +use crate::all_registers::AllRegisters; +use crate::common::jit_common::{convert_error, ARCH_NOT_SUPPORTED}; use alloc::string::ToString; use iced_x86::code_asm::{dword_ptr, qword_ptr, CodeAssembler}; - use reloaded_hooks_portable::api::jit::{compiler::JitError, operation_aliases::MovFromStack}; -use crate::{ - all_registers::AllRegisters, - jit_common::{convert_error, ARCH_NOT_SUPPORTED}, -}; - pub(crate) fn encode_mov_from_stack( a: &mut CodeAssembler, x: &MovFromStack, diff --git a/projects/reloaded-hooks-x86-sys/src/instructions/multi_pop.rs b/projects/reloaded-hooks-x86-sys/src/instructions/multi_pop.rs index 4f0be4d..f41ed56 100644 --- a/projects/reloaded-hooks-x86-sys/src/instructions/multi_pop.rs +++ b/projects/reloaded-hooks-x86-sys/src/instructions/multi_pop.rs @@ -1,14 +1,11 @@ extern crate alloc; + +use crate::all_registers::AllRegisters; +use crate::common::jit_common::{convert_error, ARCH_NOT_SUPPORTED}; use alloc::string::ToString; -use iced_x86::code_asm::CodeAssembler; +use iced_x86::code_asm::{dword_ptr, qword_ptr, registers as iced_regs, CodeAssembler}; use reloaded_hooks_portable::api::jit::{compiler::JitError, operation_aliases::Pop}; -use crate::jit_common::ARCH_NOT_SUPPORTED; -use crate::{all_registers::AllRegisters, jit_common::convert_error}; -use iced_x86::code_asm::dword_ptr; -use iced_x86::code_asm::qword_ptr; -use iced_x86::code_asm::registers as iced_regs; - macro_rules! multi_pop_item { ($a:expr, $reg:expr, $offset:expr, $convert_method:ident, $op:ident) => { match $a.bitness() { diff --git a/projects/reloaded-hooks-x86-sys/src/instructions/multi_push.rs b/projects/reloaded-hooks-x86-sys/src/instructions/multi_push.rs index 90af0b4..262d8b7 100644 --- a/projects/reloaded-hooks-x86-sys/src/instructions/multi_push.rs +++ b/projects/reloaded-hooks-x86-sys/src/instructions/multi_push.rs @@ -1,15 +1,12 @@ extern crate alloc; + +use crate::all_registers::AllRegisters; +use crate::common::jit_common::{convert_error, ARCH_NOT_SUPPORTED}; use alloc::string::ToString; -use iced_x86::code_asm::CodeAssembler; +use iced_x86::code_asm::{dword_ptr, qword_ptr, registers as iced_regs, CodeAssembler}; use reloaded_hooks_portable::api::jit::compiler::JitError; use reloaded_hooks_portable::api::jit::operation_aliases::Push; -use crate::jit_common::ARCH_NOT_SUPPORTED; -use crate::{all_registers::AllRegisters, jit_common::convert_error}; -use iced_x86::code_asm::dword_ptr; -use iced_x86::code_asm::qword_ptr; -use iced_x86::code_asm::registers as iced_regs; - macro_rules! multi_push_item { ($a:expr, $reg:expr, $offset:expr, $convert_method:ident, $op:ident) => { match $a.bitness() { diff --git a/projects/reloaded-hooks-x86-sys/src/instructions/pop.rs b/projects/reloaded-hooks-x86-sys/src/instructions/pop.rs index 3e445ed..c2cec37 100644 --- a/projects/reloaded-hooks-x86-sys/src/instructions/pop.rs +++ b/projects/reloaded-hooks-x86-sys/src/instructions/pop.rs @@ -1,15 +1,11 @@ extern crate alloc; + +use crate::all_registers::AllRegisters; +use crate::common::jit_common::{convert_error, ARCH_NOT_SUPPORTED}; use alloc::string::ToString; -use iced_x86::code_asm::{dword_ptr, qword_ptr, CodeAssembler}; +use iced_x86::code_asm::{dword_ptr, qword_ptr, registers as iced_regs, CodeAssembler}; use reloaded_hooks_portable::api::jit::{compiler::JitError, operation_aliases::Pop}; -use crate::{ - all_registers::AllRegisters, - jit_common::{convert_error, ARCH_NOT_SUPPORTED}, -}; - -use iced_x86::code_asm::registers as iced_regs; - macro_rules! encode_xmm_pop { ($a:expr, $reg:expr, $reg_type:ident, $op:ident) => { if $a.bitness() == 32 { diff --git a/projects/reloaded-hooks-x86-sys/src/instructions/push.rs b/projects/reloaded-hooks-x86-sys/src/instructions/push.rs index 05fb417..e911cc7 100644 --- a/projects/reloaded-hooks-x86-sys/src/instructions/push.rs +++ b/projects/reloaded-hooks-x86-sys/src/instructions/push.rs @@ -1,15 +1,12 @@ extern crate alloc; + +use crate::all_registers::AllRegisters; +use crate::common::jit_common::{convert_error, ARCH_NOT_SUPPORTED}; use alloc::string::ToString; -use iced_x86::code_asm::CodeAssembler; +use iced_x86::code_asm::{dword_ptr, qword_ptr, registers as iced_regs, CodeAssembler}; use reloaded_hooks_portable::api::jit::compiler::JitError; use reloaded_hooks_portable::api::jit::operation_aliases::Push; -use crate::jit_common::ARCH_NOT_SUPPORTED; -use crate::{all_registers::AllRegisters, jit_common::convert_error}; -use iced_x86::code_asm::dword_ptr; -use iced_x86::code_asm::qword_ptr; -use iced_x86::code_asm::registers as iced_regs; - macro_rules! encode_xmm_push { ($a:expr, $reg:expr, $reg_type:ident, $op:ident) => { if $a.bitness() == 32 { diff --git a/projects/reloaded-hooks-x86-sys/src/instructions/push_const.rs b/projects/reloaded-hooks-x86-sys/src/instructions/push_const.rs index 8c358e1..c13ec6b 100644 --- a/projects/reloaded-hooks-x86-sys/src/instructions/push_const.rs +++ b/projects/reloaded-hooks-x86-sys/src/instructions/push_const.rs @@ -1,14 +1,11 @@ extern crate alloc; + +use crate::all_registers::AllRegisters; +use crate::common::jit_common::{convert_error, ARCH_NOT_SUPPORTED}; use alloc::string::ToString; use iced_x86::code_asm::CodeAssembler; - use reloaded_hooks_portable::api::jit::{compiler::JitError, operation_aliases::PushConst}; -use crate::{ - all_registers::AllRegisters, - jit_common::{convert_error, ARCH_NOT_SUPPORTED}, -}; - pub(crate) fn encode_push_constant( a: &mut CodeAssembler, x: &PushConst, diff --git a/projects/reloaded-hooks-x86-sys/src/instructions/push_stack.rs b/projects/reloaded-hooks-x86-sys/src/instructions/push_stack.rs index 3fd211f..8be8d3f 100644 --- a/projects/reloaded-hooks-x86-sys/src/instructions/push_stack.rs +++ b/projects/reloaded-hooks-x86-sys/src/instructions/push_stack.rs @@ -1,14 +1,11 @@ extern crate alloc; + +use crate::all_registers::AllRegisters; +use crate::common::jit_common::{convert_error, ARCH_NOT_SUPPORTED}; use alloc::string::ToString; use iced_x86::code_asm::{dword_ptr, qword_ptr, CodeAssembler}; - use reloaded_hooks_portable::api::jit::{compiler::JitError, operation_aliases::PushStack}; -use crate::{ - all_registers::AllRegisters, - jit_common::{convert_error, ARCH_NOT_SUPPORTED}, -}; - macro_rules! encode_push_stack_impl { ($a:expr, $push:expr, $reg:expr, $size:expr, $ptr_type:ident, $error_msg:expr) => { if $push.item_size != $size { diff --git a/projects/reloaded-hooks-x86-sys/src/instructions/ret.rs b/projects/reloaded-hooks-x86-sys/src/instructions/ret.rs index ab20417..2702762 100644 --- a/projects/reloaded-hooks-x86-sys/src/instructions/ret.rs +++ b/projects/reloaded-hooks-x86-sys/src/instructions/ret.rs @@ -1,8 +1,8 @@ +use crate::all_registers::AllRegisters; +use crate::common::jit_common::convert_error; use iced_x86::code_asm::CodeAssembler; use reloaded_hooks_portable::api::jit::{compiler::JitError, operation_aliases::Return}; -use crate::{all_registers::AllRegisters, jit_common::convert_error}; - pub(crate) fn encode_return( a: &mut CodeAssembler, x: &Return, diff --git a/projects/reloaded-hooks-x86-sys/src/instructions/stack_alloc.rs b/projects/reloaded-hooks-x86-sys/src/instructions/stack_alloc.rs index ccda7a3..524b3d5 100644 --- a/projects/reloaded-hooks-x86-sys/src/instructions/stack_alloc.rs +++ b/projects/reloaded-hooks-x86-sys/src/instructions/stack_alloc.rs @@ -1,13 +1,13 @@ extern crate alloc; +use crate::all_registers::AllRegisters; +use crate::common::jit_common::convert_error; +use crate::common::jit_common::ARCH_NOT_SUPPORTED; use alloc::string::ToString; +use iced_x86::code_asm::registers as iced_regs; use iced_x86::code_asm::CodeAssembler; use reloaded_hooks_portable::api::jit::compiler::JitError; use reloaded_hooks_portable::api::jit::operation_aliases::StackAlloc; -use crate::jit_common::ARCH_NOT_SUPPORTED; -use crate::{all_registers::AllRegisters, jit_common::convert_error}; -use iced_x86::code_asm::registers as iced_regs; - pub(crate) fn encode_stack_alloc( a: &mut CodeAssembler, sub: &StackAlloc, diff --git a/projects/reloaded-hooks-x86-sys/src/instructions/xchg.rs b/projects/reloaded-hooks-x86-sys/src/instructions/xchg.rs index 1e62982..0196cbd 100644 --- a/projects/reloaded-hooks-x86-sys/src/instructions/xchg.rs +++ b/projects/reloaded-hooks-x86-sys/src/instructions/xchg.rs @@ -3,7 +3,8 @@ use alloc::string::ToString; use iced_x86::code_asm::CodeAssembler; use reloaded_hooks_portable::api::jit::{compiler::JitError, operation_aliases::XChg}; -use crate::{all_registers::AllRegisters, jit_common::convert_error}; +use crate::all_registers::AllRegisters; +use crate::common::jit_common::convert_error; macro_rules! encode_xchg_vector { ($fn_name:ident, $reg_type:ident, $mov_instr:ident) => { diff --git a/projects/reloaded-hooks-x86-sys/src/lib.rs b/projects/reloaded-hooks-x86-sys/src/lib.rs index f8f2929..712ea21 100644 --- a/projects/reloaded-hooks-x86-sys/src/lib.rs +++ b/projects/reloaded-hooks-x86-sys/src/lib.rs @@ -4,14 +4,17 @@ #[cfg(not(tarpaulin_include))] pub mod all_registers; -pub mod jit_common; - -#[allow(dead_code)] -#[cfg(not(tarpaulin_include))] -pub mod jit_conversions_common; pub mod preset_calling_convention; +pub(crate) mod common { + pub mod jit_common; + + #[allow(dead_code)] + #[cfg(not(tarpaulin_include))] + pub mod jit_conversions_common; +} + /// Contains the public namespaces for x86 pub mod x86 { pub(crate) mod code_rewriter { diff --git a/projects/reloaded-hooks-x86-sys/src/x64/jit.rs b/projects/reloaded-hooks-x86-sys/src/x64/jit.rs index 768f330..26b9a15 100644 --- a/projects/reloaded-hooks-x86-sys/src/x64/jit.rs +++ b/projects/reloaded-hooks-x86-sys/src/x64/jit.rs @@ -1,11 +1,11 @@ // JIT for x64 extern crate alloc; -use crate::{ - jit_common::encode_instruction, - jit_conversions_common::{map_allregisters_to_x64, map_register_x64_to_allregisters}, - x64::register::Register, +use crate::common::jit_common::encode_instruction; +use crate::common::jit_conversions_common::{ + map_allregisters_to_x64, map_register_x64_to_allregisters, }; +use crate::x64::register::Register; use alloc::{rc::Rc, string::ToString}; use iced_x86::code_asm::CodeAssembler; use reloaded_hooks_portable::api::jit::{ diff --git a/projects/reloaded-hooks-x86-sys/src/x86/jit.rs b/projects/reloaded-hooks-x86-sys/src/x86/jit.rs index f5280e5..1d39813 100644 --- a/projects/reloaded-hooks-x86-sys/src/x86/jit.rs +++ b/projects/reloaded-hooks-x86-sys/src/x86/jit.rs @@ -1,11 +1,11 @@ // JIT for x86 extern crate alloc; -use crate::{ - jit_common::encode_instruction, - jit_conversions_common::{map_allregisters_to_x86, map_register_x86_to_allregisters}, - x86::register::Register, +use crate::common::jit_common::encode_instruction; +use crate::common::jit_conversions_common::{ + map_allregisters_to_x86, map_register_x86_to_allregisters, }; +use crate::x86::register::Register; use alloc::{rc::Rc, string::ToString}; use iced_x86::code_asm::CodeAssembler; use reloaded_hooks_portable::api::jit::{ From 8529e94f10e6463cfef0debb96ee0142a9c67671 Mon Sep 17 00:00:00 2001 From: sewer56 Date: Sat, 4 Nov 2023 00:42:30 +0000 Subject: [PATCH 02/27] Moved: Preset calling conventions to X86 --- projects/reloaded-hooks-x86-sys/src/lib.rs | 3 +-- .../src/{ => x86}/preset_calling_convention.rs | 0 2 files changed, 1 insertion(+), 2 deletions(-) rename projects/reloaded-hooks-x86-sys/src/{ => x86}/preset_calling_convention.rs (100%) diff --git a/projects/reloaded-hooks-x86-sys/src/lib.rs b/projects/reloaded-hooks-x86-sys/src/lib.rs index 712ea21..819dfa8 100644 --- a/projects/reloaded-hooks-x86-sys/src/lib.rs +++ b/projects/reloaded-hooks-x86-sys/src/lib.rs @@ -5,8 +5,6 @@ #[cfg(not(tarpaulin_include))] pub mod all_registers; -pub mod preset_calling_convention; - pub(crate) mod common { pub mod jit_common; @@ -21,6 +19,7 @@ pub mod x86 { pub mod x86_rewriter; } + pub mod preset_calling_convention; pub mod register; pub use register::Register; pub mod jit; diff --git a/projects/reloaded-hooks-x86-sys/src/preset_calling_convention.rs b/projects/reloaded-hooks-x86-sys/src/x86/preset_calling_convention.rs similarity index 100% rename from projects/reloaded-hooks-x86-sys/src/preset_calling_convention.rs rename to projects/reloaded-hooks-x86-sys/src/x86/preset_calling_convention.rs From b71c896977e95c1694e5bc22df629360885fbb22 Mon Sep 17 00:00:00 2001 From: sewer56 Date: Sun, 5 Nov 2023 02:08:03 +0000 Subject: [PATCH 03/27] Added: x86 >2GiB relocations for conditional jumps & regular branches --- .idea/workspace.xml | 63 ++++ docs/dev/arch/arm64/code_relocation.md | 12 +- .../src/api/rewriter/code_rewriter.rs | 11 + .../.idea/projectSettingsUpdater.xml | 6 + .../.idea/workspace.xml | 97 ++++++ .../reloaded-hooks-x86-sys/.idea/discord.xml | 7 + projects/reloaded-hooks-x86-sys/.idea/vcs.xml | 6 + .../.idea/workspace.xml | 207 +++++++++++++ projects/reloaded-hooks-x86-sys/Cargo.toml | 2 +- .../src/all_registers.rs | 98 ++++++ .../src/common/disasm.rs | 292 ++++++++++++++++++ .../common/util/get_stolen_instructions.rs | 81 +++++ .../common/util/invert_branch_condition.rs | 132 ++++++++ projects/reloaded-hooks-x86-sys/src/lib.rs | 7 + .../src/x64/code_rewriter/x64_rewriter.rs | 42 ++- .../src/x86/code_rewriter/x86_rewriter.rs | 41 ++- 16 files changed, 1083 insertions(+), 21 deletions(-) create mode 100644 .idea/workspace.xml create mode 100644 projects/reloaded-hooks-x86-sys/.idea/.idea.reloaded-hooks-x86-sys.dir/.idea/projectSettingsUpdater.xml create mode 100644 projects/reloaded-hooks-x86-sys/.idea/.idea.reloaded-hooks-x86-sys.dir/.idea/workspace.xml create mode 100644 projects/reloaded-hooks-x86-sys/.idea/discord.xml create mode 100644 projects/reloaded-hooks-x86-sys/.idea/vcs.xml create mode 100644 projects/reloaded-hooks-x86-sys/.idea/workspace.xml create mode 100644 projects/reloaded-hooks-x86-sys/src/common/disasm.rs create mode 100644 projects/reloaded-hooks-x86-sys/src/common/util/get_stolen_instructions.rs create mode 100644 projects/reloaded-hooks-x86-sys/src/common/util/invert_branch_condition.rs diff --git a/.idea/workspace.xml b/.idea/workspace.xml new file mode 100644 index 0000000..3d0196e --- /dev/null +++ b/.idea/workspace.xml @@ -0,0 +1,63 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + 1699067202240 + + + + + + \ No newline at end of file diff --git a/docs/dev/arch/arm64/code_relocation.md b/docs/dev/arch/arm64/code_relocation.md index f09b5b3..2515f84 100644 --- a/docs/dev/arch/arm64/code_relocation.md +++ b/docs/dev/arch/arm64/code_relocation.md @@ -304,12 +304,12 @@ If the instruction is Prefetch `PRFM`, it is discarded if it can't be re-encoded The `TBZ` instruction in ARM architectures tests a specified bit in a register and performs a conditional branch if the bit is zero. If the tested bit is not zero, the next sequential instruction is executed. **Behaviour**: -The `TBZ` instruction is rewritten based on the distance to the new branch target. It is transformed into one of the following patterns: -- TBZ -- TBZ + B -- TBZ + ADRP + BR -- TBZ + ADRP + ADD + BR -- TBZ + MOV to Register + Branch Register +The `TBZ` instruction is rewritten based on the distance to the new branch target. It is transformed into one of the following patterns: +- TBZ +- TBZ + B +- TBZ + ADRP + BR +- TBZ + ADRP + ADD + BR +- TBZ + MOV to Register + Branch Register Here, `` is used to indicate a conditional skip over a set of instructions if the tested bit is not zero. The specific transformation depends on the offset between the current position and the new branch target. diff --git a/projects/reloaded-hooks-portable/src/api/rewriter/code_rewriter.rs b/projects/reloaded-hooks-portable/src/api/rewriter/code_rewriter.rs index 9d13dd6..d5cd20d 100644 --- a/projects/reloaded-hooks-portable/src/api/rewriter/code_rewriter.rs +++ b/projects/reloaded-hooks-portable/src/api/rewriter/code_rewriter.rs @@ -49,7 +49,18 @@ pub enum CodeRewriterError { )] OutOfRange(isize, String), + /// Failed to disassemble instruction. Unknown or invalid. + #[error("Failed to Disasemble Instruction. Instruction offset: {0:?}, Remaining Bytes (Starting with Instruction): {1:?}")] + FailedToDisasm(String, String), + + /// Insufficient bytes to disassemble a single instruction. + #[error("Insufficient bytes to disassemble a single instruction.")] + InsufficientBytes, + /// Missing a scratch register. #[error("Missing scratch register, required by function: {0:?}")] NoScratchRegister(String), + + #[error("Third party assembler error: {0:?}")] + ThirdPartyAssemblerError(String), } diff --git a/projects/reloaded-hooks-x86-sys/.idea/.idea.reloaded-hooks-x86-sys.dir/.idea/projectSettingsUpdater.xml b/projects/reloaded-hooks-x86-sys/.idea/.idea.reloaded-hooks-x86-sys.dir/.idea/projectSettingsUpdater.xml new file mode 100644 index 0000000..4bb9f4d --- /dev/null +++ b/projects/reloaded-hooks-x86-sys/.idea/.idea.reloaded-hooks-x86-sys.dir/.idea/projectSettingsUpdater.xml @@ -0,0 +1,6 @@ + + + + + \ No newline at end of file diff --git a/projects/reloaded-hooks-x86-sys/.idea/.idea.reloaded-hooks-x86-sys.dir/.idea/workspace.xml b/projects/reloaded-hooks-x86-sys/.idea/.idea.reloaded-hooks-x86-sys.dir/.idea/workspace.xml new file mode 100644 index 0000000..dc905ab --- /dev/null +++ b/projects/reloaded-hooks-x86-sys/.idea/.idea.reloaded-hooks-x86-sys.dir/.idea/workspace.xml @@ -0,0 +1,97 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + 1699068938898 + + + + + + + + \ No newline at end of file diff --git a/projects/reloaded-hooks-x86-sys/.idea/discord.xml b/projects/reloaded-hooks-x86-sys/.idea/discord.xml new file mode 100644 index 0000000..d8e9561 --- /dev/null +++ b/projects/reloaded-hooks-x86-sys/.idea/discord.xml @@ -0,0 +1,7 @@ + + + + + \ No newline at end of file diff --git a/projects/reloaded-hooks-x86-sys/.idea/vcs.xml b/projects/reloaded-hooks-x86-sys/.idea/vcs.xml new file mode 100644 index 0000000..b2bdec2 --- /dev/null +++ b/projects/reloaded-hooks-x86-sys/.idea/vcs.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/projects/reloaded-hooks-x86-sys/.idea/workspace.xml b/projects/reloaded-hooks-x86-sys/.idea/workspace.xml new file mode 100644 index 0000000..3ecb8d6 --- /dev/null +++ b/projects/reloaded-hooks-x86-sys/.idea/workspace.xml @@ -0,0 +1,207 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + { + "associatedIndex": 7 +} + + + + { + "keyToString": { + "Cargo.Build reloaded-hooks-x86-sys.executor": "Run", + "Cargo.Test call_absolute::tests (1).executor": "Run", + "Cargo.Test call_absolute::tests.executor": "Run", + "Cargo.Test common::disasm::tests::relocate_64b.executor": "Run", + "Cargo.Test common::disasm::tests::relocate_beyond_2gib_64b.executor": "Run", + "Cargo.Test disasm::tests (1).executor": "Run", + "Cargo.Test disasm::tests.executor": "Run", + "Cargo.Test instructions::call_absolute::tests::call_absolute_x64.executor": "Run", + "Cargo.Test instructions::call_absolute::tests::call_absolute_x86.executor": "Run", + "Cargo.test.executor": "Run", + "RunOnceActivity.OpenProjectViewOnStart": "true", + "RunOnceActivity.ShowReadmeOnStart": "true", + "code.cleanup.on.save": "true", + "deletionFromPopupRequiresConfirmation": "false", + "git-widget-placeholder": "code-rewriter-x86", + "last_opened_file_path": "/mnt/BTRFSSharedData/Projects/Reloaded.Hooks-rs/projects/reloaded-hooks-x86-sys", + "node.js.detected.package.eslint": "true", + "node.js.detected.package.tslint": "true", + "node.js.selected.package.eslint": "(autodetect)", + "node.js.selected.package.tslint": "(autodetect)", + "nodejs_package_manager_path": "npm", + "org.rust.cargo.project.model.PROJECT_DISCOVERY": "true", + "settings.editor.selected.configurable": "preferences.pluginManager", + "vue.rearranger.settings.migration": "true" + } +} + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + 1699067374185 + + + + + + + + + + file://$PROJECT_DIR$/src/common/disasm.rs + 259 + + + file://$PROJECT_DIR$/src/common/disasm.rs + 257 + + + file://$PROJECT_DIR$/src/common/disasm.rs + 188 + + + + + + +