feat: add support for libc::memset
Signed-off-by: vishruth-thimmaiah <vishruththimmaiah@gmail.com>
This commit is contained in:
parent
91a9b1fe4d
commit
27afeb0085
2 changed files with 58 additions and 0 deletions
|
|
@ -827,6 +827,22 @@ trait EvalContextExtPriv<'tcx>: crate::MiriInterpCxExt<'tcx> {
|
|||
this.mem_copy(ptr_src, ptr_dest, Size::from_bytes(n), true)?;
|
||||
this.write_pointer(ptr_dest, dest)?;
|
||||
}
|
||||
"memset" => {
|
||||
let [ptr_dest, val, n] =
|
||||
this.check_shim_sig_lenient(abi, CanonAbi::C, link_name, args)?;
|
||||
let ptr_dest = this.read_pointer(ptr_dest)?;
|
||||
let val = this.read_scalar(val)?.to_i32()?;
|
||||
let n = this.read_target_usize(n)?;
|
||||
// The docs say val is "interpreted as unsigned char".
|
||||
#[expect(clippy::as_conversions)]
|
||||
let val = val as u8;
|
||||
|
||||
this.ptr_get_alloc_id(ptr_dest, 0)?;
|
||||
|
||||
let bytes = std::iter::repeat_n(val, n.try_into().unwrap());
|
||||
this.write_bytes_ptr(ptr_dest, bytes)?;
|
||||
this.write_pointer(ptr_dest, dest)?;
|
||||
}
|
||||
|
||||
// LLVM intrinsics
|
||||
"llvm.prefetch" => {
|
||||
|
|
|
|||
|
|
@ -78,6 +78,47 @@ fn test_strcpy() {
|
|||
}
|
||||
}
|
||||
|
||||
fn test_memset() {
|
||||
unsafe {
|
||||
let val = 1;
|
||||
let dest = libc::calloc(3, 1);
|
||||
libc::memset(dest, val, 3);
|
||||
let slc = std::slice::from_raw_parts(dest as *const i8, 3);
|
||||
assert_eq!(*slc, [1i8, 1, 1]);
|
||||
libc::free(dest);
|
||||
}
|
||||
|
||||
unsafe {
|
||||
let val = 1;
|
||||
let dest = libc::calloc(4, 1);
|
||||
libc::memset(dest, val, 3);
|
||||
let slc = std::slice::from_raw_parts(dest as *const i8, 4);
|
||||
assert_eq!(*slc, [1i8, 1, 1, 0]);
|
||||
libc::free(dest);
|
||||
}
|
||||
|
||||
unsafe {
|
||||
let val = 1;
|
||||
let mut dest = 0_i8;
|
||||
libc::memset(&mut dest as *mut i8 as *mut libc::c_void, val, mem::size_of::<i8>());
|
||||
assert_eq!(dest, val as i8);
|
||||
}
|
||||
|
||||
unsafe {
|
||||
let val = 1;
|
||||
let mut dest = 0_i16;
|
||||
libc::memset(&mut dest as *mut i16 as *mut libc::c_void, val, mem::size_of::<i16>());
|
||||
assert_eq!(dest, 257);
|
||||
}
|
||||
|
||||
unsafe {
|
||||
let val = 257;
|
||||
let mut dest = 0_i16;
|
||||
libc::memset(&mut dest as *mut i16 as *mut libc::c_void, val, mem::size_of::<i16>());
|
||||
assert_eq!(dest, 257);
|
||||
}
|
||||
}
|
||||
|
||||
fn test_malloc() {
|
||||
// Test that small allocations sometimes *are* not very aligned.
|
||||
let saw_unaligned = (0..64).any(|_| unsafe {
|
||||
|
|
@ -310,4 +351,5 @@ fn main() {
|
|||
|
||||
test_memcpy();
|
||||
test_strcpy();
|
||||
test_memset();
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue