Skip to content

Commit 38544bc

Browse files
committed
librasan: Use dynasm for patching supported architectures
1 parent 71cf4b6 commit 38544bc

File tree

2 files changed

+30
-29
lines changed

2 files changed

+30
-29
lines changed

libafl_qemu/librasan/asan/Cargo.toml

+1
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ baby-mimalloc = { version = "0.2.1", default-features = false, features = [
5454
bitflags = { version = "2.8.0", default-features = false }
5555
document-features = { version = "0.2.11", optional = true }
5656
dlmalloc = { version = "0.2.7", default-features = false, optional = true }
57+
dynasmrt = "3.2.0"
5758
itertools = { version = "0.14.0", default-features = false }
5859
log = { version = "0.4.22", default-features = false, features = [
5960
"release_max_level_info",

libafl_qemu/librasan/asan/src/patch/raw.rs

+29-29
Original file line numberDiff line numberDiff line change
@@ -35,28 +35,28 @@ impl Patch for RawPatch {
3535
impl RawPatch {
3636
#[cfg(target_arch = "x86_64")]
3737
fn get_patch(_target: GuestAddr, destination: GuestAddr) -> Vec<u8> {
38-
// mov rax, 0xdeadfacef00dd00d
39-
// jmp rax
40-
let addr = destination.to_ne_bytes();
41-
#[rustfmt::skip]
42-
let insns: &[&[u8]] = &[
43-
&[0x48, 0xb8], &addr,
44-
&[0xff, 0xe0],
45-
];
46-
insns.concat()
38+
use dynasmrt::{DynasmApi, VecAssembler, dynasm, x64::X64Relocation};
39+
let mut assembler = VecAssembler::<X64Relocation>::new(0);
40+
41+
dynasm!(assembler
42+
; .arch x64
43+
; mov rax, QWORD destination as _
44+
; jmp rax
45+
);
46+
assembler.finalize().expect("valid static assembly")
4747
}
4848

4949
#[cfg(target_arch = "x86")]
5050
fn get_patch(_target: GuestAddr, destination: GuestAddr) -> Vec<u8> {
51-
// mov eax, 0xdeadface
52-
// jmp eax
53-
let addr = destination.to_ne_bytes();
54-
#[rustfmt::skip]
55-
let insns: &[&[u8]] = &[
56-
&[0xb8], &addr,
57-
&[0xff, 0xe0],
58-
];
59-
insns.concat()
51+
use dynasmrt::{DynasmApi, VecAssembler, dynasm, x86::X86Relocation};
52+
let mut assembler = VecAssembler::<X86Relocation>::new(0);
53+
54+
dynasm!(assembler
55+
; .arch x86
56+
; mov eax, DWORD destination as _
57+
; jmp eax
58+
);
59+
assembler.finalize().expect("valid static assembly")
6060
}
6161

6262
#[cfg(target_arch = "arm")]
@@ -88,17 +88,17 @@ impl RawPatch {
8888

8989
#[cfg(target_arch = "aarch64")]
9090
fn get_patch(_target: GuestAddr, destination: GuestAddr) -> Vec<u8> {
91-
// ldr x16, #8
92-
// br x16
93-
// .quad 0xdeadfacef00dd00d
94-
let addr = destination.to_ne_bytes();
95-
#[rustfmt::skip]
96-
let insns: &[&[u8]] = &[
97-
&[0x50, 0x00, 0x00, 0x58],
98-
&[0x00, 0x02, 0x1f, 0xd6],
99-
&addr
100-
];
101-
insns.concat()
91+
use dynasmrt::{DynasmApi, VecAssembler, aarch64::Aarch64Relocation, dynasm};
92+
let mut assembler = VecAssembler::<Aarch64Relocation>::new(0);
93+
94+
dynasm!(assembler
95+
; .arch aarch64
96+
; ldr x16, #8
97+
; br x16
98+
; .i64 destination as _
99+
);
100+
101+
assembler.finalize().expect("valid static assembly")
102102
}
103103

104104
#[cfg(target_arch = "powerpc")]

0 commit comments

Comments
 (0)