Sha256: c9d9d5c78e0e435c2678ef43d1296aef885fd62957d6b454d758ca475cd4e544

Contents?: true

Size: 1.02 KB

Versions: 8

Compression:

Stored size: 1.02 KB

Contents

//! Implementation for Hermit
use crate::Error;
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;
}

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) };
        // Positive `isize`s can be safely casted to `usize`
        if res > 0 && (res as usize) <= dest.len() {
            dest = &mut dest[res as usize..];
        } else {
            let err = match res {
                MIN_RET_CODE..=-1 => NonZeroU32::new(-res as u32).unwrap().into(),
                _ => Error::UNEXPECTED,
            };
            return Err(err);
        }
    }
    Ok(())
}

Version data entries

8 entries across 8 versions & 1 rubygems

Version Path
wasmtime-29.0.0 ./ext/cargo-vendor/getrandom-0.2.15/src/hermit.rs
wasmtime-28.0.0 ./ext/cargo-vendor/getrandom-0.2.15/src/hermit.rs
wasmtime-27.0.0 ./ext/cargo-vendor/getrandom-0.2.15/src/hermit.rs
wasmtime-26.0.0 ./ext/cargo-vendor/getrandom-0.2.15/src/hermit.rs
wasmtime-25.0.2 ./ext/cargo-vendor/getrandom-0.2.15/src/hermit.rs
wasmtime-25.0.1 ./ext/cargo-vendor/getrandom-0.2.15/src/hermit.rs
wasmtime-25.0.0 ./ext/cargo-vendor/getrandom-0.2.15/src/hermit.rs
wasmtime-24.0.0 ./ext/cargo-vendor/getrandom-0.2.15/src/hermit.rs