Skip to content

Commit c68b30a

Browse files
WorksButNotTestedYour Name
and
Your Name
authored
Add iniitialize feature to librasan (#3113)
* Optimize memset * Make the initialization of memory buffers an optional feature --------- Co-authored-by: Your Name <[email protected]>
1 parent 36c7481 commit c68b30a

File tree

3 files changed

+34
-7
lines changed

3 files changed

+34
-7
lines changed

libafl_qemu/librasan/asan/Cargo.toml

+2
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,8 @@ guest = []
3434
hooks = []
3535
## Enable support for shadow memory and tracking in the host
3636
host = ["dep:syscalls"]
37+
## Intialize all allocations with 0xff
38+
initialize = []
3739
## Enable use of the `libc` library to support creation of mappings, read/write, logging etc (more OS agnostic)
3840
libc = ["dep:libc"]
3941
## Enable the use of direct syscalls (supported by `rustix`) to interact with the operating system (Linux specific).

libafl_qemu/librasan/asan/src/allocator/frontend/default.rs

+6-3
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,8 @@ use alloc::{
1313
collections::{BTreeMap, VecDeque},
1414
fmt::Debug,
1515
};
16-
use core::slice::from_raw_parts_mut;
16+
#[cfg(feature = "initialize")]
17+
use core::ptr::write_bytes;
1718

1819
use log::debug;
1920
use thiserror::Error;
@@ -107,8 +108,10 @@ impl<B: GlobalAlloc + Send, S: Shadow, T: Tracking> AllocatorFrontend for Defaul
107108
.poison(data + len, poison_len, PoisonType::AsanStackRightRz)
108109
.map_err(|e| DefaultFrontendError::ShadowError(e))?;
109110

110-
let buffer = unsafe { from_raw_parts_mut(data as *mut u8, len) };
111-
buffer.iter_mut().for_each(|b| *b = 0xff);
111+
#[cfg(feature = "initialize")]
112+
unsafe {
113+
write_bytes(data as *mut u8, 0xff, len)
114+
};
112115
Ok(data)
113116
}
114117

libafl_qemu/librasan/asan/src/mem.rs

+26-4
Original file line numberDiff line numberDiff line change
@@ -65,10 +65,32 @@ pub unsafe extern "C" fn memcpy(dest: *mut u8, src: *const u8, count: usize) {
6565

6666
#[unsafe(no_mangle)]
6767
pub unsafe extern "C" fn memset(dest: *mut u8, value: u8, count: usize) {
68-
let dest_slice = unsafe { from_raw_parts_mut(dest, count) };
69-
#[allow(clippy::needless_range_loop)]
70-
for i in 0..count {
71-
dest_slice[i] = value;
68+
unsafe {
69+
let mut cursor = dest;
70+
let word_value = match value {
71+
u8::MIN => Some(usize::MIN),
72+
u8::MAX => Some(usize::MAX),
73+
_ => None,
74+
};
75+
76+
if let Some(word_value) = word_value {
77+
let num_words = count / size_of::<usize>();
78+
for _ in 0..num_words {
79+
*(cursor as *mut usize) = word_value;
80+
cursor = cursor.wrapping_add(size_of::<usize>());
81+
}
82+
83+
let num_bytes = count % size_of::<usize>();
84+
for _ in 0..num_bytes {
85+
*cursor = value;
86+
cursor = cursor.wrapping_add(1);
87+
}
88+
} else {
89+
for _ in 0..count {
90+
*cursor = value;
91+
cursor = cursor.wrapping_add(1);
92+
}
93+
}
7294
}
7395
}
7496

0 commit comments

Comments
 (0)