diff options
Diffstat (limited to 'vendor/getrandom/src')
-rw-r--r-- | vendor/getrandom/src/bsd_arandom.rs | 1 | ||||
-rw-r--r-- | vendor/getrandom/src/dragonfly.rs | 1 | ||||
-rw-r--r-- | vendor/getrandom/src/error.rs | 4 | ||||
-rw-r--r-- | vendor/getrandom/src/lib.rs | 2 | ||||
-rw-r--r-- | vendor/getrandom/src/linux_android.rs | 1 | ||||
-rw-r--r-- | vendor/getrandom/src/macos.rs | 1 | ||||
-rw-r--r-- | vendor/getrandom/src/openbsd.rs | 1 | ||||
-rw-r--r-- | vendor/getrandom/src/solaris_illumos.rs | 1 | ||||
-rw-r--r-- | vendor/getrandom/src/util_libc.rs | 57 | ||||
-rw-r--r-- | vendor/getrandom/src/wasi.rs | 12 | ||||
-rw-r--r-- | vendor/getrandom/src/windows.rs | 1 |
11 files changed, 57 insertions, 25 deletions
diff --git a/vendor/getrandom/src/bsd_arandom.rs b/vendor/getrandom/src/bsd_arandom.rs index f26f2609c..d44121254 100644 --- a/vendor/getrandom/src/bsd_arandom.rs +++ b/vendor/getrandom/src/bsd_arandom.rs @@ -31,6 +31,7 @@ fn kern_arnd(buf: &mut [u8]) -> libc::ssize_t { } pub fn getrandom_inner(dest: &mut [u8]) -> Result<(), Error> { + // getrandom(2) was introduced in FreeBSD 12.0 and NetBSD 10.0 #[cfg(target_os = "freebsd")] { use crate::util_libc::Weak; diff --git a/vendor/getrandom/src/dragonfly.rs b/vendor/getrandom/src/dragonfly.rs index f27e90690..8daaa4048 100644 --- a/vendor/getrandom/src/dragonfly.rs +++ b/vendor/getrandom/src/dragonfly.rs @@ -17,6 +17,7 @@ pub fn getrandom_inner(dest: &mut [u8]) -> Result<(), Error> { static GETRANDOM: Weak = unsafe { Weak::new("getrandom\0") }; type GetRandomFn = unsafe extern "C" fn(*mut u8, libc::size_t, libc::c_uint) -> libc::ssize_t; + // getrandom(2) was introduced in DragonflyBSD 5.7 if let Some(fptr) = GETRANDOM.ptr() { let func: GetRandomFn = unsafe { core::mem::transmute(fptr) }; return sys_fill_exact(dest, |buf| unsafe { func(buf.as_mut_ptr(), buf.len(), 0) }); diff --git a/vendor/getrandom/src/error.rs b/vendor/getrandom/src/error.rs index 661575376..b5ab2bb18 100644 --- a/vendor/getrandom/src/error.rs +++ b/vendor/getrandom/src/error.rs @@ -109,10 +109,6 @@ cfg_if! { let idx = buf.iter().position(|&b| b == 0).unwrap_or(n); core::str::from_utf8(&buf[..idx]).ok() } - } else if #[cfg(target_os = "wasi")] { - fn os_err(errno: i32, _buf: &mut [u8]) -> Option<wasi::Error> { - wasi::Error::from_raw_error(errno as _) - } } else { fn os_err(_errno: i32, _buf: &mut [u8]) -> Option<&str> { None diff --git a/vendor/getrandom/src/lib.rs b/vendor/getrandom/src/lib.rs index 888b9a510..c62056e55 100644 --- a/vendor/getrandom/src/lib.rs +++ b/vendor/getrandom/src/lib.rs @@ -149,7 +149,7 @@ #![doc( html_logo_url = "https://www.rust-lang.org/logos/rust-logo-128x128-blk.png", html_favicon_url = "https://www.rust-lang.org/favicon.ico", - html_root_url = "https://docs.rs/getrandom/0.2.6" + html_root_url = "https://docs.rs/getrandom/0.2.7" )] #![no_std] #![warn(rust_2018_idioms, unused_lifetimes, missing_docs)] diff --git a/vendor/getrandom/src/linux_android.rs b/vendor/getrandom/src/linux_android.rs index 5508fdd06..4270b67c6 100644 --- a/vendor/getrandom/src/linux_android.rs +++ b/vendor/getrandom/src/linux_android.rs @@ -14,6 +14,7 @@ use crate::{ }; pub fn getrandom_inner(dest: &mut [u8]) -> Result<(), Error> { + // getrandom(2) was introduced in Linux 3.17 static HAS_GETRANDOM: LazyBool = LazyBool::new(); if HAS_GETRANDOM.unsync_init(is_getrandom_available) { sys_fill_exact(dest, |buf| unsafe { diff --git a/vendor/getrandom/src/macos.rs b/vendor/getrandom/src/macos.rs index 585a35abd..671a053bf 100644 --- a/vendor/getrandom/src/macos.rs +++ b/vendor/getrandom/src/macos.rs @@ -17,6 +17,7 @@ use core::mem; type GetEntropyFn = unsafe extern "C" fn(*mut u8, libc::size_t) -> libc::c_int; pub fn getrandom_inner(dest: &mut [u8]) -> Result<(), Error> { + // getentropy(2) was added in 10.12, Rust supports 10.7+ static GETENTROPY: Weak = unsafe { Weak::new("getentropy\0") }; if let Some(fptr) = GETENTROPY.ptr() { let func: GetEntropyFn = unsafe { mem::transmute(fptr) }; diff --git a/vendor/getrandom/src/openbsd.rs b/vendor/getrandom/src/openbsd.rs index c8d28b3d8..41371736f 100644 --- a/vendor/getrandom/src/openbsd.rs +++ b/vendor/getrandom/src/openbsd.rs @@ -10,6 +10,7 @@ use crate::{util_libc::last_os_error, Error}; pub fn getrandom_inner(dest: &mut [u8]) -> Result<(), Error> { + // getentropy(2) was added in OpenBSD 5.6, so we can use it unconditionally. for chunk in dest.chunks_mut(256) { let ret = unsafe { libc::getentropy(chunk.as_mut_ptr() as *mut libc::c_void, chunk.len()) }; if ret == -1 { diff --git a/vendor/getrandom/src/solaris_illumos.rs b/vendor/getrandom/src/solaris_illumos.rs index 2d1b767bb..cf3067d6d 100644 --- a/vendor/getrandom/src/solaris_illumos.rs +++ b/vendor/getrandom/src/solaris_illumos.rs @@ -30,6 +30,7 @@ type GetRandomFn = unsafe extern "C" fn(*mut u8, libc::size_t, libc::c_uint) -> type GetRandomFn = unsafe extern "C" fn(*mut u8, libc::size_t, libc::c_uint) -> libc::c_int; pub fn getrandom_inner(dest: &mut [u8]) -> Result<(), Error> { + // getrandom(2) was introduced in Solaris 11.3 for Illumos in 2015. static GETRANDOM: Weak = unsafe { Weak::new("getrandom\0") }; if let Some(fptr) = GETRANDOM.ptr() { let func: GetRandomFn = unsafe { mem::transmute(fptr) }; diff --git a/vendor/getrandom/src/util_libc.rs b/vendor/getrandom/src/util_libc.rs index 6df1cd7da..d057071a7 100644 --- a/vendor/getrandom/src/util_libc.rs +++ b/vendor/getrandom/src/util_libc.rs @@ -6,8 +6,13 @@ // option. This file may not be copied, modified, or distributed // except according to those terms. #![allow(dead_code)] -use crate::{util::LazyUsize, Error}; -use core::{num::NonZeroU32, ptr::NonNull}; +use crate::Error; +use core::{ + num::NonZeroU32, + ptr::NonNull, + sync::atomic::{fence, AtomicPtr, Ordering}, +}; +use libc::c_void; cfg_if! { if #[cfg(any(target_os = "netbsd", target_os = "openbsd", target_os = "android"))] { @@ -76,29 +81,57 @@ pub fn sys_fill_exact( // A "weak" binding to a C function that may or may not be present at runtime. // Used for supporting newer OS features while still building on older systems. -// F must be a function pointer of type `unsafe extern "C" fn`. Based off of the -// weak! macro in libstd. +// Based off of the DlsymWeak struct in libstd: +// https://github.com/rust-lang/rust/blob/1.61.0/library/std/src/sys/unix/weak.rs#L84 +// except that the caller must manually cast self.ptr() to a function pointer. pub struct Weak { name: &'static str, - addr: LazyUsize, + addr: AtomicPtr<c_void>, } impl Weak { + // A non-null pointer value which indicates we are uninitialized. This + // constant should ideally not be a valid address of a function pointer. + // However, if by chance libc::dlsym does return UNINIT, there will not + // be undefined behavior. libc::dlsym will just be called each time ptr() + // is called. This would be inefficient, but correct. + // TODO: Replace with core::ptr::invalid_mut(1) when that is stable. + const UNINIT: *mut c_void = 1 as *mut c_void; + // Construct a binding to a C function with a given name. This function is // unsafe because `name` _must_ be null terminated. pub const unsafe fn new(name: &'static str) -> Self { Self { name, - addr: LazyUsize::new(), + addr: AtomicPtr::new(Self::UNINIT), } } - // Return a function pointer if present at runtime. Otherwise, return null. - pub fn ptr(&self) -> Option<NonNull<libc::c_void>> { - let addr = self.addr.unsync_init(|| unsafe { - libc::dlsym(libc::RTLD_DEFAULT, self.name.as_ptr() as *const _) as usize - }); - NonNull::new(addr as *mut _) + // Return the address of a function if present at runtime. Otherwise, + // return None. Multiple callers can call ptr() concurrently. It will + // always return _some_ value returned by libc::dlsym. However, the + // dlsym function may be called multiple times. + pub fn ptr(&self) -> Option<NonNull<c_void>> { + // Despite having only a single atomic variable (self.addr), we still + // cannot always use Ordering::Relaxed, as we need to make sure a + // successful call to dlsym() is "ordered before" any data read through + // the returned pointer (which occurs when the function is called). + // Our implementation mirrors that of the one in libstd, meaning that + // the use of non-Relaxed operations is probably unnecessary. + match self.addr.load(Ordering::Relaxed) { + Self::UNINIT => { + let symbol = self.name.as_ptr() as *const _; + let addr = unsafe { libc::dlsym(libc::RTLD_DEFAULT, symbol) }; + // Synchronizes with the Acquire fence below + self.addr.store(addr, Ordering::Release); + NonNull::new(addr) + } + addr => { + let func = NonNull::new(addr)?; + fence(Ordering::Acquire); + Some(func) + } + } } } diff --git a/vendor/getrandom/src/wasi.rs b/vendor/getrandom/src/wasi.rs index 2d413e020..c5121824a 100644 --- a/vendor/getrandom/src/wasi.rs +++ b/vendor/getrandom/src/wasi.rs @@ -9,15 +9,11 @@ //! Implementation for WASI use crate::Error; use core::num::NonZeroU32; -use wasi::random_get; +use wasi::wasi_snapshot_preview1::random_get; pub fn getrandom_inner(dest: &mut [u8]) -> Result<(), Error> { - unsafe { - random_get(dest.as_mut_ptr(), dest.len()).map_err(|e: wasi::Error| { - // convert wasi's Error into getrandom's NonZeroU32 error - // SAFETY: `wasi::Error` is `NonZeroU16` internally, so `e.raw_error()` - // will never return 0 - NonZeroU32::new_unchecked(e.raw_error() as u32).into() - }) + match unsafe { random_get(dest.as_mut_ptr() as i32, dest.len() as i32) } { + 0 => Ok(()), + err => Err(unsafe { NonZeroU32::new_unchecked(err as u32) }.into()), } } diff --git a/vendor/getrandom/src/windows.rs b/vendor/getrandom/src/windows.rs index 643badd07..41dc37a5c 100644 --- a/vendor/getrandom/src/windows.rs +++ b/vendor/getrandom/src/windows.rs @@ -24,6 +24,7 @@ extern "system" { pub fn getrandom_inner(dest: &mut [u8]) -> Result<(), Error> { // Prevent overflow of u32 for chunk in dest.chunks_mut(u32::max_value() as usize) { + // BCryptGenRandom was introduced in Windows Vista let ret = unsafe { BCryptGenRandom( ptr::null_mut(), |