Skip to content

Commit 19f4319

Browse files
committed
librasan: Simplify patch for ARM
1 parent 19f24cb commit 19f4319

File tree

1 file changed

+35
-52
lines changed
  • libafl_qemu/librasan/asan/src/patch

1 file changed

+35
-52
lines changed

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

Lines changed: 35 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -14,35 +14,18 @@ pub struct RawPatch;
1414

1515
impl Patch for RawPatch {
1616
type Error = RawPatchError;
17-
18-
#[cfg(not(target_arch = "arm"))]
1917
fn patch(target: GuestAddr, destination: GuestAddr) -> Result<(), Self::Error> {
2018
debug!("patch - addr: {:#x}, target: {:#x}", target, destination);
2119
if target == destination {
2220
Err(RawPatchError::IdentityPatch(target))?;
2321
}
24-
let patch = Self::get_patch(destination)?;
25-
trace!("patch: {:02x?}", patch);
26-
let dest = unsafe { from_raw_parts_mut(target as *mut u8, patch.len()) };
27-
dest.copy_from_slice(&patch);
28-
Ok(())
29-
}
22+
let patch = Self::get_patch(target, destination)?;
3023

31-
#[cfg(target_arch = "arm")]
32-
fn patch(target: GuestAddr, destination: GuestAddr) -> Result<(), Self::Error> {
33-
debug!("patch - addr: {:#x}, target: {:#x}", target, destination);
34-
if target == destination {
35-
Err(RawPatchError::IdentityPatch(target))?;
36-
}
37-
38-
let patch = if target & 1 == 1 {
39-
Self::get_patch_thumb(destination)?
40-
} else {
41-
Self::get_patch_arm(destination)?
42-
};
24+
// Mask the thumb mode indicator bit
25+
#[cfg(target_arch = "arm")]
26+
let target = target & !1;
4327

4428
trace!("patch: {:02x?}", patch);
45-
let target = target & !1;
4629
let dest = unsafe { from_raw_parts_mut(target as *mut u8, patch.len()) };
4730
dest.copy_from_slice(&patch);
4831
Ok(())
@@ -51,7 +34,7 @@ impl Patch for RawPatch {
5134

5235
impl RawPatch {
5336
#[cfg(target_arch = "x86_64")]
54-
fn get_patch(destination: GuestAddr) -> Result<Vec<u8>, RawPatchError> {
37+
fn get_patch(_target: GuestAddr, destination: GuestAddr) -> Result<Vec<u8>, RawPatchError> {
5538
// mov rax, 0xdeadfacef00dd00d
5639
// jmp rax
5740
let insns = [
@@ -77,7 +60,7 @@ impl RawPatch {
7760
}
7861

7962
#[cfg(target_arch = "x86")]
80-
fn get_patch(destination: GuestAddr) -> Result<Vec<u8>, RawPatchError> {
63+
fn get_patch(_target: GuestAddr, destination: GuestAddr) -> Result<Vec<u8>, RawPatchError> {
8164
// mov eax, 0xdeadface
8265
// jmp eax
8366
let insns = [
@@ -91,37 +74,37 @@ impl RawPatch {
9174
}
9275

9376
#[cfg(target_arch = "arm")]
94-
fn get_patch_arm(destination: GuestAddr) -> Result<Vec<u8>, RawPatchError> {
95-
// ldr ip, [pc]
96-
// bx ip
97-
// .long 0xdeadface
98-
let insns = [
99-
[0x00, 0xc0, 0x9f, 0xe5].to_vec(),
100-
[0x1c, 0xff, 0x2f, 0xe1].to_vec(),
101-
[0xce, 0xfa, 0xad, 0xde].to_vec(),
102-
];
103-
let addr = destination.to_ne_bytes().to_vec();
104-
let insns_mod = [&insns[0], &insns[1], &addr];
105-
Ok(insns_mod.into_iter().flatten().cloned().collect())
106-
}
107-
108-
#[cfg(target_arch = "arm")]
109-
fn get_patch_thumb(destination: GuestAddr) -> Result<Vec<u8>, RawPatchError> {
110-
// ldr ip, [pc, #2]
111-
// bx ip
112-
// .long 0xdeadface
113-
let insns = [
114-
[0xdf, 0xf8, 0x02, 0xc0].to_vec(),
115-
[0x60, 0x47].to_vec(),
116-
[0xce, 0xfa, 0xad, 0xde].to_vec(),
117-
];
118-
let addr = destination.to_ne_bytes().to_vec();
119-
let insns_mod = [&insns[0], &insns[1], &addr];
120-
Ok(insns_mod.into_iter().flatten().cloned().collect())
77+
fn get_patch(target: GuestAddr, destination: GuestAddr) -> Result<Vec<u8>, RawPatchError> {
78+
// If our target is in thumb mode
79+
if target & 1 == 1 {
80+
// ldr ip, [pc, #2]
81+
// bx ip
82+
// .long 0xdeadface
83+
let insns = [
84+
[0xdf, 0xf8, 0x02, 0xc0].to_vec(),
85+
[0x60, 0x47].to_vec(),
86+
[0xce, 0xfa, 0xad, 0xde].to_vec(),
87+
];
88+
let addr = destination.to_ne_bytes().to_vec();
89+
let insns_mod = [&insns[0], &insns[1], &addr];
90+
Ok(insns_mod.into_iter().flatten().cloned().collect())
91+
} else {
92+
// ldr ip, [pc]
93+
// bx ip
94+
// .long 0xdeadface
95+
let insns = [
96+
[0x00, 0xc0, 0x9f, 0xe5].to_vec(),
97+
[0x1c, 0xff, 0x2f, 0xe1].to_vec(),
98+
[0xce, 0xfa, 0xad, 0xde].to_vec(),
99+
];
100+
let addr = destination.to_ne_bytes().to_vec();
101+
let insns_mod = [&insns[0], &insns[1], &addr];
102+
Ok(insns_mod.into_iter().flatten().cloned().collect())
103+
}
121104
}
122105

123106
#[cfg(target_arch = "aarch64")]
124-
fn get_patch(destination: GuestAddr) -> Result<Vec<u8>, RawPatchError> {
107+
fn get_patch(_target: GuestAddr, destination: GuestAddr) -> Result<Vec<u8>, RawPatchError> {
125108
// ldr x16, #8
126109
// br x16
127110
// .quad 0xdeadfacef00dd00d
@@ -137,7 +120,7 @@ impl RawPatch {
137120
}
138121

139122
#[cfg(target_arch = "powerpc")]
140-
fn get_patch(destination: GuestAddr) -> Result<Vec<u8>, RawPatchError> {
123+
fn get_patch(_target: GuestAddr, destination: GuestAddr) -> Result<Vec<u8>, RawPatchError> {
141124
// lis 12, 0xdead
142125
// ori 12, 12, 0xface
143126
// mtctr 12

0 commit comments

Comments
 (0)