-
Notifications
You must be signed in to change notification settings - Fork 778
Vendor getrandom 0.3
#2700
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
Open
hanyuone
wants to merge
6
commits into
briansmith:main
Choose a base branch
from
hanyuone:update-getrandom
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Vendor getrandom 0.3
#2700
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
61e398a
feat: place vendored version of getrandom into rand/getrandom
hanyuone 2482f4c
feat: only enable vendored version if wasm32-u-u and js feature is en…
hanyuone 74d8de8
feat: remove references to std feature
hanyuone d5f5af0
fix: remove dead code from error.rs
hanyuone bdec6dd
feat: set upstream to be getrandom v0.3.3
hanyuone b1b2aab
feat: rename getrandom.rs for easier upstream comparisons
hanyuone File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,11 @@ | ||
| //! System-specific implementations. | ||
| //! | ||
| //! This module should provide `fill_inner` with the signature | ||
| //! `fn fill_inner(dest: &mut [MaybeUninit<u8>]) -> Result<(), Error>`. | ||
| //! The function MUST fully initialize `dest` when `Ok(())` is returned; | ||
| //! the function may need to use `sanitizer::unpoison` as well. | ||
| //! The function MUST NOT ever write uninitialized bytes into `dest`, | ||
| //! regardless of what value it returns. | ||
|
|
||
| mod wasm_js; | ||
| pub use wasm_js::*; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,70 @@ | ||
| //! Implementation for WASM based on Web and Node.js | ||
| use crate::rand::getrandom::Error; | ||
| use core::mem::MaybeUninit; | ||
|
|
||
| #[cfg(not(all(target_arch = "wasm32", any(target_os = "unknown", target_os = "none"))))] | ||
| compile_error!("`wasm_js` backend can be enabled only for OS-less WASM targets!"); | ||
|
|
||
| use wasm_bindgen::{prelude::wasm_bindgen, JsValue}; | ||
|
|
||
| // Maximum buffer size allowed in `Crypto.getRandomValuesSize` is 65536 bytes. | ||
| // See https://developer.mozilla.org/en-US/docs/Web/API/Crypto/getRandomValues | ||
| const MAX_BUFFER_SIZE: usize = 65536; | ||
|
|
||
| #[cfg(not(target_feature = "atomics"))] | ||
| #[inline] | ||
| pub fn fill_inner(dest: &mut [MaybeUninit<u8>]) -> Result<(), Error> { | ||
| for chunk in dest.chunks_mut(MAX_BUFFER_SIZE) { | ||
| if get_random_values(chunk).is_err() { | ||
| return Err(Error::WEB_CRYPTO); | ||
| } | ||
| } | ||
| Ok(()) | ||
| } | ||
|
|
||
| #[cfg(target_feature = "atomics")] | ||
| pub fn fill_inner(dest: &mut [MaybeUninit<u8>]) -> Result<(), Error> { | ||
| // getRandomValues does not work with all types of WASM memory, | ||
| // so we initially write to browser memory to avoid exceptions. | ||
| let buf_len = usize::min(dest.len(), MAX_BUFFER_SIZE); | ||
| let buf_len_u32 = buf_len | ||
| .try_into() | ||
| .expect("buffer length is bounded by MAX_BUFFER_SIZE"); | ||
| let buf = js_sys::Uint8Array::new_with_length(buf_len_u32); | ||
| for chunk in dest.chunks_mut(buf_len) { | ||
| let chunk_len = chunk | ||
| .len() | ||
| .try_into() | ||
| .expect("chunk length is bounded by MAX_BUFFER_SIZE"); | ||
| // The chunk can be smaller than buf's length, so we call to | ||
| // JS to create a smaller view of buf without allocation. | ||
| let sub_buf = if chunk_len == buf_len_u32 { | ||
| &buf | ||
| } else { | ||
| &buf.subarray(0, chunk_len) | ||
| }; | ||
|
|
||
| if get_random_values(sub_buf).is_err() { | ||
| return Err(Error::WEB_CRYPTO); | ||
| } | ||
|
|
||
| sub_buf.copy_to_uninit(chunk); | ||
| } | ||
| Ok(()) | ||
| } | ||
|
|
||
| #[wasm_bindgen] | ||
| extern "C" { | ||
| // Crypto.getRandomValues() | ||
| #[cfg(not(target_feature = "atomics"))] | ||
| #[wasm_bindgen(js_namespace = ["globalThis", "crypto"], js_name = getRandomValues, catch)] | ||
| fn get_random_values(buf: &mut [MaybeUninit<u8>]) -> Result<(), JsValue>; | ||
| #[cfg(target_feature = "atomics")] | ||
| #[wasm_bindgen(js_namespace = ["globalThis", "crypto"], js_name = getRandomValues, catch)] | ||
| fn get_random_values(buf: &js_sys::Uint8Array) -> Result<(), JsValue>; | ||
| } | ||
|
|
||
| impl Error { | ||
| /// The environment does not support the Web Crypto API. | ||
| pub(crate) const WEB_CRYPTO: Error = Self::new_internal(10); | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,45 @@ | ||
| use core::fmt; | ||
|
|
||
| // This private alias mirrors `std::io::RawOsError`: | ||
| // https://doc.rust-lang.org/std/io/type.RawOsError.html) | ||
| cfg_if::cfg_if!( | ||
| if #[cfg(target_os = "uefi")] { | ||
| // See the UEFI spec for more information: | ||
| // https://uefi.org/specs/UEFI/2.10/Apx_D_Status_Codes.html | ||
| type RawOsError = usize; | ||
| type NonZeroRawOsError = core::num::NonZeroUsize; | ||
| const UEFI_ERROR_FLAG: RawOsError = 1 << (RawOsError::BITS - 1); | ||
| } else { | ||
| type RawOsError = i32; | ||
| type NonZeroRawOsError = core::num::NonZeroI32; | ||
| } | ||
| ); | ||
|
|
||
| /// A small and `no_std` compatible error type | ||
| /// | ||
| /// The [`Error::raw_os_error()`] will indicate if the error is from the OS, and | ||
| /// if so, which error code the OS gave the application. If such an error is | ||
| /// encountered, please consult with your system documentation. | ||
| /// | ||
| /// *If this crate's `"std"` Cargo feature is enabled*, then: | ||
| /// - [`getrandom::Error`][Error] implements | ||
| /// [`std::error::Error`](https://doc.rust-lang.org/std/error/trait.Error.html) | ||
| /// - [`std::io::Error`](https://doc.rust-lang.org/std/io/struct.Error.html) implements | ||
| /// [`From<getrandom::Error>`](https://doc.rust-lang.org/std/convert/trait.From.html). | ||
|
|
||
| // note: on non-UEFI targets OS errors are represented as negative integers, | ||
| // while on UEFI targets OS errors have the highest bit set to 1. | ||
| #[derive(Copy, Clone, Eq, PartialEq)] | ||
| pub struct Error(NonZeroRawOsError); | ||
|
|
||
| impl Error { | ||
| /// Internal errors can be in the range of 2^16..2^17 | ||
| const INTERNAL_START: RawOsError = 1 << 16; | ||
|
|
||
| /// Creates a new instance of an `Error` from a particular internal error code. | ||
| pub(crate) const fn new_internal(n: u16) -> Error { | ||
| // SAFETY: code > 0 as INTERNAL_START > 0 and adding `n` won't overflow `RawOsError`. | ||
| let code = Error::INTERNAL_START + (n as RawOsError); | ||
| Error(unsafe { NonZeroRawOsError::new_unchecked(code) }) | ||
| } | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.