Add iniitialize feature to librasan (#3113)
* Optimize memset * Make the initialization of memory buffers an optional feature --------- Co-authored-by: Your Name <you@example.com>
This commit is contained in:
parent
36c748100e
commit
c68b30ae2a
@ -34,6 +34,8 @@ guest = []
|
|||||||
hooks = []
|
hooks = []
|
||||||
## Enable support for shadow memory and tracking in the host
|
## Enable support for shadow memory and tracking in the host
|
||||||
host = ["dep:syscalls"]
|
host = ["dep:syscalls"]
|
||||||
|
## Intialize all allocations with 0xff
|
||||||
|
initialize = []
|
||||||
## Enable use of the `libc` library to support creation of mappings, read/write, logging etc (more OS agnostic)
|
## Enable use of the `libc` library to support creation of mappings, read/write, logging etc (more OS agnostic)
|
||||||
libc = ["dep:libc"]
|
libc = ["dep:libc"]
|
||||||
## Enable the use of direct syscalls (supported by `rustix`) to interact with the operating system (Linux specific).
|
## Enable the use of direct syscalls (supported by `rustix`) to interact with the operating system (Linux specific).
|
||||||
|
@ -13,7 +13,8 @@ use alloc::{
|
|||||||
collections::{BTreeMap, VecDeque},
|
collections::{BTreeMap, VecDeque},
|
||||||
fmt::Debug,
|
fmt::Debug,
|
||||||
};
|
};
|
||||||
use core::slice::from_raw_parts_mut;
|
#[cfg(feature = "initialize")]
|
||||||
|
use core::ptr::write_bytes;
|
||||||
|
|
||||||
use log::debug;
|
use log::debug;
|
||||||
use thiserror::Error;
|
use thiserror::Error;
|
||||||
@ -107,8 +108,10 @@ impl<B: GlobalAlloc + Send, S: Shadow, T: Tracking> AllocatorFrontend for Defaul
|
|||||||
.poison(data + len, poison_len, PoisonType::AsanStackRightRz)
|
.poison(data + len, poison_len, PoisonType::AsanStackRightRz)
|
||||||
.map_err(|e| DefaultFrontendError::ShadowError(e))?;
|
.map_err(|e| DefaultFrontendError::ShadowError(e))?;
|
||||||
|
|
||||||
let buffer = unsafe { from_raw_parts_mut(data as *mut u8, len) };
|
#[cfg(feature = "initialize")]
|
||||||
buffer.iter_mut().for_each(|b| *b = 0xff);
|
unsafe {
|
||||||
|
write_bytes(data as *mut u8, 0xff, len)
|
||||||
|
};
|
||||||
Ok(data)
|
Ok(data)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -65,10 +65,32 @@ pub unsafe extern "C" fn memcpy(dest: *mut u8, src: *const u8, count: usize) {
|
|||||||
|
|
||||||
#[unsafe(no_mangle)]
|
#[unsafe(no_mangle)]
|
||||||
pub unsafe extern "C" fn memset(dest: *mut u8, value: u8, count: usize) {
|
pub unsafe extern "C" fn memset(dest: *mut u8, value: u8, count: usize) {
|
||||||
let dest_slice = unsafe { from_raw_parts_mut(dest, count) };
|
unsafe {
|
||||||
#[allow(clippy::needless_range_loop)]
|
let mut cursor = dest;
|
||||||
for i in 0..count {
|
let word_value = match value {
|
||||||
dest_slice[i] = value;
|
u8::MIN => Some(usize::MIN),
|
||||||
|
u8::MAX => Some(usize::MAX),
|
||||||
|
_ => None,
|
||||||
|
};
|
||||||
|
|
||||||
|
if let Some(word_value) = word_value {
|
||||||
|
let num_words = count / size_of::<usize>();
|
||||||
|
for _ in 0..num_words {
|
||||||
|
*(cursor as *mut usize) = word_value;
|
||||||
|
cursor = cursor.wrapping_add(size_of::<usize>());
|
||||||
|
}
|
||||||
|
|
||||||
|
let num_bytes = count % size_of::<usize>();
|
||||||
|
for _ in 0..num_bytes {
|
||||||
|
*cursor = value;
|
||||||
|
cursor = cursor.wrapping_add(1);
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
for _ in 0..count {
|
||||||
|
*cursor = value;
|
||||||
|
cursor = cursor.wrapping_add(1);
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user