Vendor asic-rs 0.8.1 (280 crates, offline builds) + wxWidgets 3.3.3 (pristine + patch queue); UI spec + ADR-0003; M1 on asic-rs
This commit is contained in:
+29
@@ -0,0 +1,29 @@
|
||||
cfg_if! {
|
||||
if #[cfg(any(target_os = "netbsd", target_os = "openbsd", target_os = "android", target_os = "cygwin"))] {
|
||||
use libc::__errno as errno_location;
|
||||
} else if #[cfg(any(target_os = "linux", target_os = "emscripten", target_os = "hurd", target_os = "redox", target_os = "dragonfly"))] {
|
||||
use libc::__errno_location as errno_location;
|
||||
} else if #[cfg(target_os = "illumos")] {
|
||||
use libc::___errno as errno_location;
|
||||
} else if #[cfg(any(target_os = "macos", target_os = "freebsd"))] {
|
||||
use libc::__error as errno_location;
|
||||
} else if #[cfg(target_os = "haiku")] {
|
||||
use libc::_errnop as errno_location;
|
||||
} else if #[cfg(target_os = "nto")] {
|
||||
use libc::__get_errno_ptr as errno_location;
|
||||
} else if #[cfg(any(all(target_os = "horizon", target_arch = "arm"), target_os = "vita"))] {
|
||||
unsafe extern "C" {
|
||||
// Not provided by libc: https://github.com/rust-lang/libc/issues/1995
|
||||
fn __errno() -> *mut libc::c_int;
|
||||
}
|
||||
use __errno as errno_location;
|
||||
} else if #[cfg(target_os = "aix")] {
|
||||
use libc::_Errno as errno_location;
|
||||
} else {
|
||||
compile_error!("errno_location is not provided for the target");
|
||||
}
|
||||
}
|
||||
|
||||
pub(crate) fn get_errno() -> libc::c_int {
|
||||
unsafe { core::ptr::read(errno_location()) }
|
||||
}
|
||||
+39
@@ -0,0 +1,39 @@
|
||||
use core::sync::atomic::{AtomicU8, Ordering::Relaxed};
|
||||
|
||||
/// Lazily caches a `bool` in an `AtomicU8`.
|
||||
///
|
||||
/// Initialization is intentionally unsynchronized: concurrent callers may race
|
||||
/// and run `init` more than once. Once a value is produced, it is cached and
|
||||
/// reused by subsequent calls.
|
||||
///
|
||||
/// Uses `Relaxed` ordering because this helper only publishes the cached
|
||||
/// value itself.
|
||||
pub(crate) struct LazyBool(AtomicU8);
|
||||
|
||||
impl LazyBool {
|
||||
const UNINIT: u8 = u8::MAX;
|
||||
|
||||
/// Create new `LazyBool`.
|
||||
pub const fn new() -> Self {
|
||||
Self(AtomicU8::new(Self::UNINIT))
|
||||
}
|
||||
|
||||
/// Call the `init` closure and return the result after caching it.
|
||||
#[cold]
|
||||
fn cold_init(&self, init: impl FnOnce() -> bool) -> bool {
|
||||
let val = u8::from(init());
|
||||
self.0.store(val, Relaxed);
|
||||
val != 0
|
||||
}
|
||||
|
||||
/// Retrieve the cached value if it was already initialized or call the `init` closure
|
||||
/// and return the result after caching it.
|
||||
#[inline]
|
||||
pub fn unsync_init(&self, init: impl FnOnce() -> bool) -> bool {
|
||||
let val = self.0.load(Relaxed);
|
||||
if val == Self::UNINIT {
|
||||
return self.cold_init(init);
|
||||
}
|
||||
val != 0
|
||||
}
|
||||
}
|
||||
+57
@@ -0,0 +1,57 @@
|
||||
use core::{
|
||||
convert::Infallible,
|
||||
ptr::{self, NonNull},
|
||||
sync::atomic::{AtomicPtr, Ordering::Relaxed},
|
||||
};
|
||||
|
||||
/// Lazily caches a non-null pointer in an `AtomicPtr`.
|
||||
///
|
||||
/// Initialization is intentionally unsynchronized: concurrent callers may race
|
||||
/// and run `init` more than once. Once a value is produced, it is cached and
|
||||
/// reused by subsequent calls.
|
||||
///
|
||||
/// For fallible initialization (`try_unsync_init`), only successful values are
|
||||
/// cached; errors are returned to the caller and are not cached.
|
||||
///
|
||||
/// Uses `Ordering::Relaxed` because this helper only publishes the cached
|
||||
/// pointer value. Callers must not rely on this mechanism to synchronize
|
||||
/// unrelated memory side effects performed by `init`.
|
||||
pub(crate) struct LazyPtr<T>(AtomicPtr<T>);
|
||||
|
||||
impl<T> LazyPtr<T> {
|
||||
/// Create new `LazyPtr`.
|
||||
pub const fn new() -> Self {
|
||||
Self(AtomicPtr::new(ptr::null_mut()))
|
||||
}
|
||||
|
||||
/// Call the `init` closure and return the result after caching it in the case of success.
|
||||
#[cold]
|
||||
fn cold_init<E>(&self, init: impl FnOnce() -> Result<NonNull<T>, E>) -> Result<NonNull<T>, E> {
|
||||
let val = init()?;
|
||||
self.0.store(val.as_ptr(), Relaxed);
|
||||
Ok(val)
|
||||
}
|
||||
|
||||
/// Retrieve the cached value if it was already initialized or call the potentially fallible
|
||||
/// `init` closure and return the result after caching it in the case of success.
|
||||
#[inline]
|
||||
pub fn try_unsync_init<E>(
|
||||
&self,
|
||||
init: impl FnOnce() -> Result<NonNull<T>, E>,
|
||||
) -> Result<NonNull<T>, E> {
|
||||
let p = self.0.load(Relaxed);
|
||||
match NonNull::new(p) {
|
||||
Some(val) => Ok(val),
|
||||
None => self.cold_init(init),
|
||||
}
|
||||
}
|
||||
|
||||
/// Retrieve the cached value if it was already initialized or call the `init` closure
|
||||
/// and return the result after caching it.
|
||||
#[inline]
|
||||
#[allow(dead_code, reason = "Some modules use only `try_unsync_init`")]
|
||||
pub fn unsync_init(&self, init: impl FnOnce() -> NonNull<T>) -> NonNull<T> {
|
||||
let Ok(p): Result<_, Infallible> = self.try_unsync_init(|| Ok(init()));
|
||||
p
|
||||
}
|
||||
}
|
||||
+28
@@ -0,0 +1,28 @@
|
||||
use core::mem::MaybeUninit;
|
||||
|
||||
/// Unpoisons `buf` if MSAN support is enabled.
|
||||
///
|
||||
/// Most backends do not need to unpoison their output. Rust language- and
|
||||
/// library- provided functionality unpoisons automatically. Similarly, libc
|
||||
/// either natively supports MSAN and/or MSAN hooks libc-provided functions
|
||||
/// to unpoison outputs on success. Only when all of these things are
|
||||
/// bypassed do we need to do it ourselves.
|
||||
///
|
||||
/// The call to unpoison should be done as close to the write as possible.
|
||||
/// For example, if the backend partially fills the output buffer in chunks,
|
||||
/// each chunk should be unpoisoned individually. This way, the correctness of
|
||||
/// the chunking logic can be validated (in part) using MSAN.
|
||||
pub unsafe fn unpoison(buf: &mut [MaybeUninit<u8>]) {
|
||||
cfg_if! {
|
||||
if #[cfg(getrandom_msan)] {
|
||||
unsafe extern "C" {
|
||||
fn __msan_unpoison(a: *mut core::ffi::c_void, size: usize);
|
||||
}
|
||||
let a = buf.as_mut_ptr().cast();
|
||||
let size = buf.len();
|
||||
unsafe { __msan_unpoison(a, size) };
|
||||
} else {
|
||||
let _ = buf;
|
||||
}
|
||||
}
|
||||
}
|
||||
+41
@@ -0,0 +1,41 @@
|
||||
use crate::Error;
|
||||
use core::mem::MaybeUninit;
|
||||
|
||||
mod get_errno;
|
||||
mod sanitizer;
|
||||
|
||||
pub(crate) use get_errno::get_errno;
|
||||
|
||||
/// Fill a buffer by repeatedly invoking `sys_fill`.
|
||||
///
|
||||
/// The `sys_fill` function:
|
||||
/// - should return -1 and set errno on failure
|
||||
/// - should return the number of bytes written on success
|
||||
pub(crate) fn sys_fill_exact(
|
||||
mut buf: &mut [MaybeUninit<u8>],
|
||||
sys_fill: impl Fn(&mut [MaybeUninit<u8>]) -> libc::ssize_t,
|
||||
) -> Result<(), Error> {
|
||||
while !buf.is_empty() {
|
||||
let res = sys_fill(buf);
|
||||
match res {
|
||||
res if res > 0 => {
|
||||
let len = usize::try_from(res).map_err(|_| Error::UNEXPECTED)?;
|
||||
let (l, r) = buf.split_at_mut_checked(len).ok_or(Error::UNEXPECTED)?;
|
||||
unsafe { sanitizer::unpoison(l) };
|
||||
buf = r;
|
||||
}
|
||||
-1 => {
|
||||
let errno = get_errno();
|
||||
// We should try again if the call was interrupted.
|
||||
if errno != libc::EINTR {
|
||||
return Err(Error::from_errno(errno));
|
||||
}
|
||||
}
|
||||
// Negative return codes not equal to -1 should be impossible.
|
||||
// EOF (ret = 0) should be impossible, as the data we are reading
|
||||
// should be an infinite stream of random bytes.
|
||||
_ => return Err(Error::UNEXPECTED),
|
||||
}
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
Reference in New Issue
Block a user