use linux_api::errno::Errno;
use log::*;
use rand::RngCore;
use shadow_shim_helper_rs::syscall_types::ForeignPtr;
use crate::host::syscall::handler::{SyscallContext, SyscallHandler};
use crate::host::syscall::types::ForeignArrayPtr;
impl SyscallHandler {
log_syscall!(
getrandom,
isize,
*const std::ffi::c_void,
usize,
std::ffi::c_uint,
);
pub fn getrandom(
ctx: &mut SyscallContext,
buf_ptr: ForeignPtr<u8>,
count: usize,
_flags: std::ffi::c_uint,
) -> Result<isize, Errno> {
trace!("Trying to read {count} random bytes.");
let dst_ptr = ForeignArrayPtr::new(buf_ptr, count);
let mut memory = ctx.objs.process.memory_borrow_mut();
let mut mem_ref = match memory.memory_ref_mut_uninit(dst_ptr) {
Ok(m) => m,
Err(e) => {
warn!("Failed to get memory ref: {e:?}");
return Err(Errno::EFAULT);
}
};
let mut rng = ctx.objs.host.random_mut();
rng.fill_bytes(&mut mem_ref);
match mem_ref.flush() {
Ok(()) => Ok(isize::try_from(count).unwrap()),
Err(e) => {
warn!("Failed to flush writes: {e:?}");
Err(Errno::EFAULT)
}
}
}
}