Skip to content

Commit

Permalink
Changed: Cleaned up some x86 code
Browse files Browse the repository at this point in the history
  • Loading branch information
Sewer56 committed Dec 30, 2023
1 parent df1ac47 commit b72dc25
Show file tree
Hide file tree
Showing 23 changed files with 164 additions and 194 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -206,15 +206,15 @@ pub(crate) fn decompose_pop_operations_ex<TRegister: Copy + Clone + RegisterInfo

fn do_decompose_pops<TRegister: Clone + Copy>(
operations: &mut Vec<Operation<TRegister>>,
mut ops: &mut SmallVec<[Operation<TRegister>; 16]>,
ops: &mut SmallVec<[Operation<TRegister>; 16]>,
stack_size: &mut isize,
last_stackalloc_val: i32,
last_stackalloc_idx: usize,
first_pop_idx: usize,
) {
let is_stackalloc = last_stackalloc_idx == first_pop_idx - 1;
let stackalloc_offset = is_stackalloc as i32 * last_stackalloc_val;
update_mov_from_stack_offsets(&mut ops, last_stackalloc_val, is_stackalloc);
update_mov_from_stack_offsets(ops, last_stackalloc_val, is_stackalloc);

// Emit stackalloc for the writes we just made.
let stackalloc_ofs = -*stack_size as i32 + stackalloc_offset;
Expand Down
63 changes: 42 additions & 21 deletions projects/reloaded-hooks-x86-sys/src/common/jit_common.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
extern crate alloc;

use crate::all_registers::AllRegisters;
use crate::common::jit_common::alloc::string::ToString;
use crate::instructions::{
call_absolute::encode_call_absolute, call_relative::encode_call_relative,
jump_absolute::encode_jump_absolute, jump_absolute_indirect::encode_jump_absolute_indirect,
Expand All @@ -10,6 +8,7 @@ use crate::instructions::{
push_const::encode_push_constant, push_stack::encode_push_stack, ret::encode_return,
stack_alloc::encode_stack_alloc, xchg::encode_xchg,
};
use alloc::string::ToString;

#[cfg(target_feature = "multipushpop")]
use crate::instructions::multi_pop::encode_multi_pop;
Expand All @@ -34,29 +33,29 @@ pub(crate) fn encode_instruction(
address: usize,
) -> Result<(), JitError<AllRegisters>> {
match operation {
Operation::Mov(x) => encode_mov(assembler, x),
Operation::MovFromStack(x) => encode_mov_from_stack(assembler, x),
Operation::Push(x) => encode_push(assembler, x),
Operation::PushStack(x) => encode_push_stack(assembler, x),
Operation::StackAlloc(x) => encode_stack_alloc(assembler, x),
Operation::Pop(x) => encode_pop(assembler, x),
Operation::Xchg(x) => encode_xchg(assembler, x),
Operation::CallRelative(x) => encode_call_relative(assembler, x),
Operation::CallAbsolute(x) => encode_call_absolute(assembler, x),
Operation::JumpRelative(x) => encode_jump_relative(assembler, x),
Operation::JumpAbsolute(x) => encode_jump_absolute(assembler, x),
Operation::JumpAbsoluteIndirect(x) => encode_jump_absolute_indirect(assembler, x),
Operation::MovToStack(x) => encode_mov_to_stack(assembler, x),
Operation::Mov(x) => Ok(encode_mov(assembler, x)?),
Operation::MovFromStack(x) => Ok(encode_mov_from_stack(assembler, x)?),
Operation::Push(x) => Ok(encode_push(assembler, x)?),
Operation::PushStack(x) => Ok(encode_push_stack(assembler, x)?),
Operation::StackAlloc(x) => Ok(encode_stack_alloc(assembler, x)?),
Operation::Pop(x) => Ok(encode_pop(assembler, x)?),
Operation::Xchg(x) => Ok(encode_xchg(assembler, x)?),
Operation::CallRelative(x) => Ok(encode_call_relative(assembler, x)?),
Operation::CallAbsolute(x) => Ok(encode_call_absolute(assembler, x)?),
Operation::JumpRelative(x) => Ok(encode_jump_relative(assembler, x)?),
Operation::JumpAbsolute(x) => Ok(encode_jump_absolute(assembler, x)?),
Operation::JumpAbsoluteIndirect(x) => Ok(encode_jump_absolute_indirect(assembler, x)?),
Operation::MovToStack(x) => Ok(encode_mov_to_stack(assembler, x)?),

// x64 only
#[cfg(feature = "x64")]
Operation::CallIpRelative(x) => encode_call_ip_relative(assembler, x, address),
Operation::CallIpRelative(x) => Ok(encode_call_ip_relative(assembler, x, address)?),
#[cfg(feature = "x64")]
Operation::JumpIpRelative(x) => encode_jump_ip_relative(assembler, x, address),
Operation::JumpIpRelative(x) => Ok(encode_jump_ip_relative(assembler, x, address)?),

// Optimised Functions
Operation::PushConst(x) => encode_push_constant(assembler, x),
Operation::Return(x) => encode_return(assembler, x),
Operation::PushConst(x) => Ok(encode_push_constant(assembler, x)?),
Operation::Return(x) => Ok(encode_return(assembler, x)?),

// Deprecated
#[cfg(target_feature = "multipushpop")]
Expand All @@ -68,6 +67,28 @@ pub(crate) fn encode_instruction(
}
}

pub(crate) fn convert_error(e: IcedError) -> JitError<AllRegisters> {
JitError::ThirdPartyAssemblerError(e.to_string())
pub enum X86jitError<T> {
IcedError(IcedError),
JitError(JitError<T>),
}

impl<T> From<IcedError> for X86jitError<T> {
fn from(e: IcedError) -> Self {
Self::IcedError(e)
}
}

impl<T> From<JitError<T>> for X86jitError<T> {
fn from(e: JitError<T>) -> Self {
Self::JitError(e)
}
}

impl<T> From<X86jitError<T>> for JitError<T> {
fn from(val: X86jitError<T>) -> Self {
match val {
X86jitError::JitError(e) => e,
X86jitError::IcedError(e) => JitError::ThirdPartyAssemblerError(e.to_string()),
}
}
}
18 changes: 7 additions & 11 deletions projects/reloaded-hooks-x86-sys/src/instructions/call_absolute.rs
Original file line number Diff line number Diff line change
@@ -1,32 +1,28 @@
extern crate alloc;

use crate::all_registers::AllRegisters;
use crate::common::jit_common::{convert_error, ARCH_NOT_SUPPORTED};
use crate::common::jit_common::{X86jitError, ARCH_NOT_SUPPORTED};
use alloc::string::ToString;
use iced_x86::code_asm::CodeAssembler;
use reloaded_hooks_portable::api::jit::{compiler::JitError, operation_aliases::CallAbs};

pub(crate) fn encode_call_absolute(
a: &mut CodeAssembler,
x: &CallAbs<AllRegisters>,
) -> Result<(), JitError<AllRegisters>> {
) -> Result<(), X86jitError<AllRegisters>> {
if a.bitness() == 64 && cfg!(feature = "x64") {
#[cfg(feature = "x64")]
{
let target_reg = x.scratch_register.as_iced_64()?;
a.mov(target_reg, x.target_address as u64)
.map_err(convert_error)?;
a.call(target_reg).map_err(convert_error)?;
a.mov(target_reg, x.target_address as u64)?;
a.call(target_reg)?;
}
} else if a.bitness() == 32 && cfg!(feature = "x86") {
let target_reg = x.scratch_register.as_iced_32()?;
a.mov(target_reg, x.target_address as u32)
.map_err(convert_error)?;
a.call(target_reg).map_err(convert_error)?;
a.mov(target_reg, x.target_address as u32)?;
a.call(target_reg)?;
} else {
return Err(JitError::ThirdPartyAssemblerError(
ARCH_NOT_SUPPORTED.to_string(),
));
return Err(JitError::ThirdPartyAssemblerError(ARCH_NOT_SUPPORTED.to_string()).into());
}

Ok(())
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
extern crate alloc;

use crate::all_registers::AllRegisters;
use crate::common::jit_common::convert_error;
use crate::common::jit_common::X86jitError;
use alloc::string::ToString;
use iced_x86::code_asm::{qword_ptr, CodeAssembler};
use reloaded_hooks_portable::api::jit::{compiler::JitError, operation_aliases::CallIpRel};
Expand All @@ -11,11 +11,12 @@ pub(crate) fn encode_call_ip_relative(
a: &mut CodeAssembler,
x: &CallIpRel<AllRegisters>,
address: usize,
) -> Result<(), JitError<AllRegisters>> {
) -> Result<(), X86jitError<AllRegisters>> {
if a.bitness() == 32 {
return Err(JitError::ThirdPartyAssemblerError(
"Call IP Relative is only Supported on 64-bit!".to_string(),
));
)
.into());
}

let isns = a.instructions();
Expand All @@ -26,8 +27,7 @@ pub(crate) fn encode_call_ip_relative(
};

let relative_offset = x.target_address.wrapping_sub(current_ip as usize);
a.call(qword_ptr(iced_x86::Register::RIP) + relative_offset as i32)
.map_err(convert_error)?;
a.call(qword_ptr(iced_x86::Register::RIP) + relative_offset as i32)?;
Ok(())
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
use crate::all_registers::AllRegisters;
use crate::common::jit_common::convert_error;
use crate::{all_registers::AllRegisters, common::jit_common::X86jitError};
use iced_x86::code_asm::CodeAssembler;
use reloaded_hooks_portable::api::jit::{compiler::JitError, operation_aliases::CallRel};
use reloaded_hooks_portable::api::jit::operation_aliases::CallRel;

pub(crate) fn encode_call_relative(
a: &mut CodeAssembler,
x: &CallRel,
) -> Result<(), JitError<AllRegisters>> {
a.call(x.target_address as u64).map_err(convert_error)?;
) -> Result<(), X86jitError<AllRegisters>> {
a.call(x.target_address as u64)?;
Ok(())
}

Expand Down
20 changes: 8 additions & 12 deletions projects/reloaded-hooks-x86-sys/src/instructions/jump_absolute.rs
Original file line number Diff line number Diff line change
@@ -1,32 +1,28 @@
extern crate alloc;

use crate::all_registers::AllRegisters;
use crate::common::jit_common::{convert_error, ARCH_NOT_SUPPORTED};
use crate::common::jit_common::ARCH_NOT_SUPPORTED;
use crate::{all_registers::AllRegisters, common::jit_common::X86jitError};
use alloc::string::ToString;
use iced_x86::code_asm::CodeAssembler;
use reloaded_hooks_portable::api::jit::{compiler::JitError, operation_aliases::JumpAbs};

pub(crate) fn encode_jump_absolute(
a: &mut CodeAssembler,
x: &JumpAbs<AllRegisters>,
) -> Result<(), JitError<AllRegisters>> {
) -> Result<(), X86jitError<AllRegisters>> {
if a.bitness() == 64 && cfg!(feature = "x64") {
#[cfg(feature = "x64")]
{
let target_reg = x.scratch_register.as_iced_64()?;
a.mov(target_reg, x.target_address as u64)
.map_err(convert_error)?;
a.jmp(target_reg).map_err(convert_error)?;
a.mov(target_reg, x.target_address as u64)?;
a.jmp(target_reg)?;
}
} else if a.bitness() == 32 && cfg!(feature = "x86") {
let target_reg = x.scratch_register.as_iced_32()?;
a.mov(target_reg, x.target_address as u32)
.map_err(convert_error)?;
a.jmp(target_reg).map_err(convert_error)?;
a.mov(target_reg, x.target_address as u32)?;
a.jmp(target_reg)?;
} else {
return Err(JitError::ThirdPartyAssemblerError(
ARCH_NOT_SUPPORTED.to_string(),
));
return Err(JitError::ThirdPartyAssemblerError(ARCH_NOT_SUPPORTED.to_string()).into());
}

Ok(())
Expand Down
Original file line number Diff line number Diff line change
@@ -1,26 +1,26 @@
extern crate alloc;

use crate::all_registers::AllRegisters;
use crate::common::jit_common::convert_error;
use crate::{all_registers::AllRegisters, common::jit_common::X86jitError};
use alloc::string::ToString;
use iced_x86::code_asm::{dword_ptr, qword_ptr, CodeAssembler};
use reloaded_hooks_portable::api::jit::{compiler::JitError, operation_aliases::JumpAbsInd};

pub(crate) fn encode_jump_absolute_indirect(
a: &mut CodeAssembler,
x: &JumpAbsInd<AllRegisters>,
) -> Result<(), JitError<AllRegisters>> {
) -> Result<(), X86jitError<AllRegisters>> {
let mem_op = if a.bitness() == 64 && cfg!(feature = "x64") {
qword_ptr(x.pointer_address)
} else if cfg!(feature = "x86") {
dword_ptr(x.pointer_address)
} else {
return Err(JitError::ThirdPartyAssemblerError(
"Please use 'x86' or 'x64' library feature".to_string(),
));
)
.into());
};

a.jmp(mem_op).map_err(convert_error)?;
a.jmp(mem_op)?;
Ok(())
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
extern crate alloc;

use crate::all_registers::AllRegisters;
use crate::common::jit_common::convert_error;
use crate::common::jit_common::X86jitError;
use alloc::string::ToString;
use iced_x86::code_asm::{qword_ptr, CodeAssembler};
use reloaded_hooks_portable::api::jit::compiler::JitError;
Expand All @@ -12,11 +12,12 @@ pub(crate) fn encode_jump_ip_relative(
a: &mut CodeAssembler,
x: &JumpIpRel<AllRegisters>,
address: usize,
) -> Result<(), JitError<AllRegisters>> {
) -> Result<(), X86jitError<AllRegisters>> {
if a.bitness() == 32 {
return Err(JitError::ThirdPartyAssemblerError(
"Jump IP Relative is only Supported on 64-bit!".to_string(),
));
)
.into());
}

let isns = a.instructions();
Expand All @@ -27,8 +28,7 @@ pub(crate) fn encode_jump_ip_relative(
};

let relative_offset = x.target_address.wrapping_sub(current_ip as usize);
a.jmp(qword_ptr(iced_x86::Register::RIP) + relative_offset as i32)
.map_err(convert_error)?;
a.jmp(qword_ptr(iced_x86::Register::RIP) + relative_offset as i32)?;
Ok(())
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
use crate::all_registers::AllRegisters;
use crate::common::jit_common::convert_error;
use crate::{all_registers::AllRegisters, common::jit_common::X86jitError};
use iced_x86::code_asm::CodeAssembler;
use reloaded_hooks_portable::api::jit::{compiler::JitError, operation_aliases::JumpRel};
use reloaded_hooks_portable::api::jit::operation_aliases::JumpRel;

pub(crate) fn encode_jump_relative(
a: &mut CodeAssembler,
x: &JumpRel<AllRegisters>,
) -> Result<(), JitError<AllRegisters>> {
a.jmp(x.target_address as u64).map_err(convert_error)?;
) -> Result<(), X86jitError<AllRegisters>> {
a.jmp(x.target_address as u64)?;
Ok(())
}

Expand Down
9 changes: 4 additions & 5 deletions projects/reloaded-hooks-x86-sys/src/instructions/macros.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,21 +8,20 @@ macro_rules! mov_item_to_stack {
$a.$op(
iced_x86::code_asm::dword_ptr(iced_x86::code_asm::registers::esp) + $offset,
$reg.$convert_method()?,
)
.map_err($crate::common::jit_common::convert_error)?;
)?;
}
#[cfg(feature = "x64")]
64 => {
$a.$op(
iced_x86::code_asm::qword_ptr(iced_x86::code_asm::registers::rsp) + $offset,
$reg.$convert_method()?,
)
.map_err($crate::common::jit_common::convert_error)?;
)?;
}
_ => {
return Err(JitError::ThirdPartyAssemblerError(
$crate::common::jit_common::ARCH_NOT_SUPPORTED.to_string(),
));
)
.into());
}
}
};
Expand Down
10 changes: 4 additions & 6 deletions projects/reloaded-hooks-x86-sys/src/instructions/mov.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
use crate::all_registers::AllRegisters;
use crate::common::jit_common::convert_error;
use crate::{all_registers::AllRegisters, common::jit_common::X86jitError};
use iced_x86::code_asm::CodeAssembler;
use reloaded_hooks_portable::api::jit::{compiler::JitError, operation_aliases::Mov};

pub(crate) fn encode_mov(
a: &mut CodeAssembler,
mov: &Mov<AllRegisters>,
) -> Result<(), JitError<AllRegisters>> {
) -> Result<(), X86jitError<AllRegisters>> {
if mov.target.is_32() && mov.source.is_32() {
a.mov(mov.target.as_iced_32()?, mov.source.as_iced_32()?)
} else if mov.target.is_64() && mov.source.is_64() && cfg!(feature = "x64") {
Expand All @@ -25,9 +24,8 @@ pub(crate) fn encode_mov(
} else if mov.target.is_zmm() && mov.source.is_zmm() {
a.vmovaps(mov.target.as_iced_zmm()?, mov.source.as_iced_zmm()?)
} else {
return Err(JitError::InvalidRegisterCombination(mov.source, mov.target));
}
.map_err(convert_error)?;
return Err(JitError::InvalidRegisterCombination(mov.source, mov.target).into());
}?;

Ok(())
}
Expand Down
Loading

0 comments on commit b72dc25

Please sign in to comment.