diff --git a/Cargo.toml b/Cargo.toml index 07f637eabb..fcaaff7783 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -307,7 +307,7 @@ untrusted = { version = "0.7.0" } spin = { version = "0.5.2", default-features = false } -[target.'cfg(any(target_os = "android", target_os = "linux"))'.dependencies] +[target.'cfg(any(target_os = "android", target_os = "linux", target_os = "vxworks"))'.dependencies] libc = { version = "0.2.48", default-features = false } [target.'cfg(any(target_os = "android", target_os = "freebsd", target_os = "linux", target_os = "netbsd", target_os = "openbsd", target_os = "solaris"))'.dependencies] diff --git a/build.rs b/build.rs index c6cb3a7009..f1355d8bdf 100644 --- a/build.rs +++ b/build.rs @@ -549,7 +549,7 @@ fn cc( for f in cpp_flags(target) { let _ = c.flag(&f); } - if &target.os != "none" && &target.os != "redox" && &target.os != "windows" { + if &target.os != "none" && &target.os != "redox" && &target.os != "windows" && &target.os != "vxworks" { let _ = c.flag("-fstack-protector"); } diff --git a/src/rand.rs b/src/rand.rs index cb24defd7c..c69845010f 100644 --- a/src/rand.rs +++ b/src/rand.rs @@ -193,6 +193,9 @@ use self::darwin::fill as fill_impl; #[cfg(any(target_os = "fuchsia"))] use self::fuchsia::fill as fill_impl; +#[cfg(target_os = "vxworks")] +use self::vxworks::fill as fill_impl; + #[cfg(any(target_os = "android", target_os = "linux"))] mod sysrand_chunk { use crate::{c, error}; @@ -432,3 +435,32 @@ mod fuchsia { fn zx_cprng_draw(buffer: *mut u8, length: usize); } } + +#[cfg(target_os = "vxworks")] +mod vxworks { + use crate::error; + use core::sync::atomic::{AtomicBool, Ordering::Relaxed}; + + pub fn fill(dest: &mut [u8]) -> Result<(), error::Unspecified> { + static RNG_INIT: AtomicBool = AtomicBool::new(false); + while !RNG_INIT.load(Relaxed) { + let ret = unsafe { libc::randSecure() }; + if ret < 0 { + return Err(error::Unspecified); + } else if ret > 0 { + RNG_INIT.store(true, Relaxed); + break; + } + let _ = unsafe { libc::usleep(10) }; + } + + // Prevent overflow of i32 + for chunk in dest.chunks_mut(i32::max_value() as usize) { + let ret = unsafe { libc::randABytes(chunk.as_mut_ptr(), chunk.len() as i32) }; + if ret != 0 { + return Err(error::Unspecified); + } + } + Ok(()) + } +}