Skip to content

Commit

Permalink
Tweak Hermit code
Browse files Browse the repository at this point in the history
  • Loading branch information
newpavlov authored Dec 2, 2023
1 parent d6e8464 commit cfdcfc4
Showing 1 changed file with 13 additions and 11 deletions.
24 changes: 13 additions & 11 deletions src/hermit.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
use crate::Error;
use core::{convert::TryInto, mem::MaybeUninit, num::NonZeroU32};
use core::{mem::MaybeUninit, num::NonZeroU32};

/// Minimum return value which we should get from syscalls in practice,
/// because Hermit uses positive `i32`s for error codes:
/// https://github.com/hermitcore/libhermit-rs/blob/main/src/errno.rs
const MIN_RET_CODE: isize = -(i32::MAX as isize);

extern "C" {
fn sys_read_entropy(buffer: *mut u8, length: usize, flags: u32) -> isize;
Expand All @@ -8,17 +13,14 @@ extern "C" {
pub fn getrandom_inner(mut dest: &mut [MaybeUninit<u8>]) -> Result<(), Error> {
while !dest.is_empty() {
let res = unsafe { sys_read_entropy(dest.as_mut_ptr() as *mut u8, dest.len(), 0) };
if res > 0 {
dest = dest.get_mut(res as usize..).ok_or(Error::UNEXPECTED)?;
// Positive `isize`s can be safely casted to `usize`
if res > 0 && (res as usize) <= dest.len() {
dest = &mut dest[res as usize..];
} else {
// We should not get `res` equal to zero or smaller than `-i32::MAX`.
// If we get such unexpected value after all, we will return `Error::UNEXPECTED`.
let err = res
.checked_neg()
.and_then(|val| val.try_into().ok())
.and_then(NonZeroU32::new)
.map(Into::into)
.unwrap_or(Error::UNEXPECTED);
let err = match res {
MIN_RET_CODE..=-1 => NonZeroU32::new(-res as u32).unwrap().into(),
_ => Error::UNEXPECTED,
};
return Err(err);
}
}
Expand Down

0 comments on commit cfdcfc4

Please sign in to comment.