-
Notifications
You must be signed in to change notification settings - Fork 182
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
Add support for wasm32-wasip1
and wasm32-wasip2
, remove support for wasm32-wasi
#499
Merged
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
10f59f6
Add support for `wasm32-wasip1` and `wasm32-wasip2`, remove support f…
newpavlov 927f14a
Apply yoshuawuyts' suggestion
newpavlov 12809c4
Tweak CI config
newpavlov c00eb60
Use Nightly for wasip2 tests
newpavlov 4c62ab5
Use pre-built std for wasip2 tests
newpavlov c1c63c0
Merge WASI jobs
newpavlov 9b56e5c
Fix CI config
newpavlov 2f3c164
Add comment about `get_random_u64`
newpavlov 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
This file contains 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 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 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 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 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 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 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 |
---|---|---|
@@ -1,9 +1,53 @@ | ||
//! Implementation for WASI | ||
//! Implementation for WASI (preview 1 and 2) | ||
//! | ||
//! `target_env = "p1"` was introduced only in Rust 1.80, so on earlier compiler versions this | ||
//! code will result in a compilation error. | ||
use crate::Error; | ||
use core::mem::MaybeUninit; | ||
use wasi::random_get; | ||
|
||
#[cfg(not(any(target_env = "p1", target_env = "p2")))] | ||
compile_error!( | ||
"Unknown version of WASI (only previews 1 and 2 are supported) \ | ||
or Rust version older than 1.80 was used" | ||
); | ||
|
||
#[cfg(target_env = "p1")] | ||
pub fn getrandom_inner(dest: &mut [MaybeUninit<u8>]) -> Result<(), Error> { | ||
unsafe { random_get(dest.as_mut_ptr().cast::<u8>(), dest.len()) } | ||
unsafe { wasi::random_get(dest.as_mut_ptr().cast::<u8>(), dest.len()) } | ||
.map_err(|e| Error::from_os_error(e.raw().into())) | ||
} | ||
|
||
#[cfg(target_env = "p2")] | ||
pub fn getrandom_inner(dest: &mut [MaybeUninit<u8>]) -> Result<(), Error> { | ||
use core::ptr::copy_nonoverlapping; | ||
use wasi::random::random::get_random_u64; | ||
|
||
let (prefix, chunks, suffix) = unsafe { dest.align_to_mut::<MaybeUninit<u64>>() }; | ||
|
||
// We use `get_random_u64` instead of `get_random_bytes` because the latter creates | ||
// an allocation due to the Wit IDL [restrictions][0]. This should be fine since | ||
// the main use case of `getrandom` is seed generation. | ||
// | ||
// [0]: https://github.com/WebAssembly/wasi-random/issues/27 | ||
if !prefix.is_empty() { | ||
let val = get_random_u64(); | ||
let src = (&val as *const u64).cast(); | ||
unsafe { | ||
copy_nonoverlapping(src, prefix.as_mut_ptr(), prefix.len()); | ||
} | ||
} | ||
|
||
for dst in chunks { | ||
dst.write(get_random_u64()); | ||
} | ||
|
||
if !suffix.is_empty() { | ||
let val = get_random_u64(); | ||
let src = (&val as *const u64).cast(); | ||
unsafe { | ||
copy_nonoverlapping(src, suffix.as_mut_ptr(), suffix.len()); | ||
} | ||
} | ||
|
||
Ok(()) | ||
} |
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.
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.
get_random_u64
is used instead ofget_random_bytes
because the latter uses an allocation due to the Wit IDL restrictions. This should be fine since the main use case ofgetrandom
is seed generation.