-
Notifications
You must be signed in to change notification settings - Fork 0
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
The getrandom
crate
#1
base: main
Are you sure you want to change the base?
Conversation
Crate implementation
Add getrandom wrapper func and documentation
8 Update Solaris getrandom
Some of these tests will fail for now!
Arguably the doc links and root URL are wrong, but they avoid broken links and will do for now.
Also new: impl From<NonZeroU32> for Error
- Cleanup `tests.yml` - Add better binary downloads - Add minimal dependancies check - Add tests for `custom` feature - Build/Link for iOS - Run cross tests on aarch64 linux and Android - Link on Solaris and Netbsd - Test wasm code on Node, Chrome, Firefox - Test WASI - No need for RDRAND feature on VxWorks Signed-off-by: Joe Richey <joerichey@google.com>
Most of the advantages from testing various Rust versions already come from running those tests on Linux and Windows. There's very little gain from also running these tests on macOS, while macOS jobs are the slowest to schedule. Signed-off-by: Joe Richey <joerichey@google.com>
Signed-off-by: Joe Richey <joerichey@google.com>
Update Github Actions CI for the master branch
Signed-off-by: Joe Richey <joerichey@google.com>
See rust-random#194 This installs (and caches) the emsdk toolchain at the last version compatible w/ stable rust. It also tests on both asmjs and wasm32, uses node by default, and works around an asm.js bug.
See rust-random#194 This uses the fact that `wasm32-unknown-unknown` is an "unsupported" target. This means we can use the `"custom"` feature to define a custom handler, and then write tests to make sure that function is called.
Signed-off-by: Joe Richey <joerichey@google.com>
Signed-off-by: Joe Richey <joerichey@google.com>
Add fixes for Caching and rustc-dep-of-std feature
Signed-off-by: Joe Richey <joerichey@google.com>
Signed-off-by: Joe Richey <joerichey@google.com>
Use doc_cfg to improve register_custom_getrandom docs
…m#209) Signed-off-by: Joe Richey <joerichey@google.com>
DragonFly BSD supports the getrandom system call since version 5.7 [1]. Use it if available, otherwise fall back to /dev/random. [1] https://leaf.dragonflybsd.org/cgi/web-man?command=getrandom Signed-off-by: Tobias Klauser <tklauser@distanz.ch>
#[link(name = "zircon")] | ||
extern "C" { | ||
fn zx_cprng_draw(buffer: *mut u8, length: usize); | ||
} | ||
|
||
pub fn getrandom_inner(dest: &mut [u8]) -> Result<(), Error> { | ||
unsafe { zx_cprng_draw(dest.as_mut_ptr(), dest.len()) } | ||
Ok(()) | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
✔️
#[link(name = "Security", kind = "framework")] | ||
extern "C" { | ||
fn SecRandomCopyBytes(rnd: *const c_void, count: usize, bytes: *mut u8) -> i32; | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
✔️
static RNG_SOURCE: Result<RngSource, Error> = getrandom_init(); | ||
); | ||
|
||
pub(crate) fn getrandom_inner(dest: &mut [u8]) -> Result<(), Error> { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not reviewed, since the "js"
feature will not be enabled when this is used in std
.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is correct, std
for wasm32-unknown-unknown
cannot assume JavaScript exists.
// Returns the file descriptor for the device file used to retrieve random | ||
// bytes. The file will be opened exactly once. All successful calls will | ||
// return the same file descriptor. This file descriptor is never closed. | ||
fn get_rng_fd() -> Result<libc::c_int, Error> { | ||
static FD: AtomicUsize = AtomicUsize::new(LazyUsize::UNINIT); | ||
fn get_fd() -> Option<libc::c_int> { | ||
match FD.load(Relaxed) { | ||
LazyUsize::UNINIT => None, | ||
val => Some(val as libc::c_int), | ||
} | ||
} | ||
|
||
// Use double-checked locking to avoid acquiring the lock if possible. | ||
if let Some(fd) = get_fd() { | ||
return Ok(fd); | ||
} | ||
|
||
// SAFETY: We use the mutex only in this method, and we always unlock it | ||
// before returning, making sure we don't violate the pthread_mutex_t API. | ||
static MUTEX: Mutex = Mutex::new(); | ||
unsafe { MUTEX.lock() }; | ||
let _guard = DropGuard(|| unsafe { MUTEX.unlock() }); | ||
|
||
if let Some(fd) = get_fd() { | ||
return Ok(fd); | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
✔️
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; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Might be good to add a note that getrandom
only appeared in DragonFly 5.7.
#[cfg(target_os = "freebsd")] | ||
{ | ||
use crate::util_libc::Weak; | ||
static GETRANDOM: Weak = unsafe { Weak::new("getrandom\0") }; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Might be good to add a note that getrandom
is only available since FreeBSD 12.0.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Agreed. Here and anywhere else we use a fallback implementation, we should note why we use that implementation.
fn kern_arnd(buf: &mut [u8]) -> libc::ssize_t { | ||
static MIB: [libc::c_int; 2] = [libc::CTL_KERN, libc::KERN_ARND]; | ||
let mut len = buf.len(); | ||
let ret = unsafe { | ||
libc::sysctl( | ||
MIB.as_ptr(), | ||
MIB.len() as libc::c_uint, | ||
buf.as_mut_ptr() as *mut _, | ||
&mut len, | ||
ptr::null(), | ||
0, | ||
) | ||
}; | ||
if ret == -1 { | ||
-1 | ||
} else { | ||
len as libc::ssize_t | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
✔️
static HAS_GETRANDOM: LazyBool = LazyBool::new(); | ||
if HAS_GETRANDOM.unsync_init(is_getrandom_available) { | ||
sys_fill_exact(dest, |buf| unsafe { | ||
getrandom(buf.as_mut_ptr() as *mut libc::c_void, buf.len(), 0) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
In std
, we call this with GRND_NONBLOCK
. See my comment in src/use_file.rs
.
… r=m-ou-se updating docs to mention usage of AtomicBool Mouse mentioned we should point out that atomic bool is used by the std lib these days. ( m-ou-se/getrandom#1 )
… r=m-ou-se updating docs to mention usage of AtomicBool Mouse mentioned we should point out that atomic bool is used by the std lib these days. ( m-ou-se/getrandom#1 )
… r=m-ou-se updating docs to mention usage of AtomicBool Mouse mentioned we should point out that atomic bool is used by the std lib these days. ( m-ou-se/getrandom#1 )
… r=m-ou-se updating docs to mention usage of AtomicBool Mouse mentioned we should point out that atomic bool is used by the std lib these days. ( m-ou-se/getrandom#1 )
No description provided.