-
Notifications
You must be signed in to change notification settings - Fork 187
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
16 additions
and
4 deletions.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -11,6 +11,9 @@ compile_error!( | |
or Rust version older than 1.80 was used" | ||
); | ||
|
||
#[cfg(not(target_arch = "wasm32"))] | ||
compile_error!("Currently only 32-bit WASI targets are supported"); | ||
|
||
#[cfg(target_env = "p1")] | ||
pub fn getrandom_inner(dest: &mut [MaybeUninit<u8>]) -> Result<(), Error> { | ||
// This linking is vendored from the wasi crate: | ||
|
@@ -34,7 +37,19 @@ pub fn getrandom_inner(dest: &mut [MaybeUninit<u8>]) -> Result<(), Error> { | |
#[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; | ||
|
||
// Based on the code from the wasi v0.13 crate: | ||
// https://docs.rs/wasi/0.13.2+wasi-0.2.1/src/wasi/bindings.rs.html#10845-10860 | ||
fn get_random_u64() -> u64 { | ||
unsafe { | ||
#[link(wasm_import_module = "wasi:random/[email protected]")] | ||
extern "C" { | ||
#[link_name = "get-random-u64"] | ||
fn wit_import() -> i64; | ||
} | ||
wit_import() as u64 | ||
} | ||
} | ||
|
||
let (prefix, chunks, suffix) = unsafe { dest.align_to_mut::<MaybeUninit<u64>>() }; | ||
|
||
|