Skip to content

Commit

Permalink
Fix x64 constant immediate size calculation failing for slightly more…
Browse files Browse the repository at this point in the history
… complex immediates and displacements, due to changes needed for #95
  • Loading branch information
CensoredUsername committed Oct 29, 2024
1 parent e7290dd commit 73395f8
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 14 deletions.
31 changes: 18 additions & 13 deletions plugin/src/arch/x64/compiler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -753,31 +753,36 @@ fn sanitize_indirects_and_sizes(ctx: &Context, args: &mut [CleanArg]) -> Result<

// Tries to find the maximum size necessary to hold the result of an expression.
fn derive_size(expr: &syn::Expr) -> Option<Size> {
// strip any wrapping Group nodes due to delimiting
// strip any wrapping Group or Paren nodes due to delimiting
let mut inner = expr;
while let syn::Expr::Group(syn::ExprGroup { expr, .. }) = inner {
inner = expr;
loop {
inner = match inner {
syn::Expr::Group(syn::ExprGroup { expr, ..}) => expr,
syn::Expr::Paren(syn::ExprParen { expr, .. }) => expr,
_ => break
}
}

match inner {
syn::Expr::Lit(syn::ExprLit { ref lit, .. } ) => match lit {
syn::Lit::Byte(_) => Some(Size::BYTE),
syn::Lit::Int(i) => match i.base10_parse::<u32>() {
Ok(x) if x < 0x80 => Some(Size::BYTE),
Ok(x) if x < 0x8000 => Some(Size::B_2),
Ok(x) if x < 0x8000_0000 => Some(Size::B_4),
_ => Some(Size::B_8),
syn::Lit::Int(i) => match i.base10_parse::<i32>() {
Err(_) => Some(Size::B_8),
Ok(x) if x > 0x7FFF || x < -0x8000 => Some(Size::B_4),
Ok(x) if x > 0x7F || x < -0x80 => Some(Size::B_2),
Ok(_) => Some(Size::BYTE),
},
_ => None
},
syn::Expr::Unary(syn::ExprUnary { op: syn::UnOp::Neg(_), ref expr, .. } ) => match &**expr {
syn::Expr::Lit(syn::ExprLit { ref lit, .. } ) => match lit {
syn::Lit::Byte(_) => Some(Size::BYTE),
syn::Lit::Int(i) => match i.base10_parse::<u32>() {
Ok(x) if x <= 0x80 => Some(Size::BYTE),
Ok(x) if x <= 0x8000 => Some(Size::B_2),
Ok(x) if x <= 0x8000_0000 => Some(Size::B_4),
_ => Some(Size::B_8),
syn::Lit::Int(i) => match i.base10_parse::<i64>() {
Err(_) => Some(Size::B_8),
Ok(x) if x > 0x8000_0000 || x < -0x7FFF_FFFF => Some(Size::B_4),
Ok(x) if x > 0x8000 || x < -0x7FFF => Some(Size::B_4),
Ok(x) if x > 0x80 || x < -0x7F => Some(Size::B_2),
Ok(_) => Some(Size::BYTE),
},
_ => None
},
Expand Down
20 changes: 19 additions & 1 deletion testing/tests/bugreports.rs
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,6 @@ fn bugreport_4() {
// Precedence issue around typemapped operands due to proc_macro2::Delimiter::None being broken.
#[test]
fn bugreport_5() {
#![allow(unused_parens)]
#[allow(dead_code)]
struct Test {
a: u32,
Expand All @@ -100,3 +99,22 @@ fn bugreport_5() {
let hex: String = hex.join(", ");
assert_eq!(hex, "0x48, 0x89, 0x83, 0x1C, 0x00, 0x00, 0x00", "bugreport_5");
}

// Bad sizing of constant immediates in x64 mode
#[test]
fn bugreport_6() {
let mut ops = dynasmrt::x64::Assembler::new().unwrap();
dynasm!(ops
; .arch x64
; lea rax, [rbx + 1]
; lea rax, [rbx + 0x80]
; lea rax, [rbx - 1]
; lea rax, [rbx + -1]
; lea rax, [rbx - 0x81]
);

let buf = ops.finalize().unwrap();
let hex: Vec<String> = buf.iter().map(|x| format!("0x{:02X}", *x)).collect();
let hex: String = hex.join(", ");
assert_eq!(hex, "0x48, 0x8D, 0x43, 0x01, 0x48, 0x8D, 0x83, 0x80, 0x00, 0x00, 0x00, 0x48, 0x8D, 0x43, 0xFF, 0x48, 0x8D, 0x43, 0xFF, 0x48, 0x8D, 0x83, 0x7F, 0xFF, 0xFF, 0xFF", "bugreport_6");
}

0 comments on commit 73395f8

Please sign in to comment.