Commit Graph

326 Commits

Author SHA1 Message Date
Joseph Richey 8e44d13ebf Merge pull request #317 from thomcc/tvos-watchos
Add tvOS and watchOS support, identical to iOS support
2022-10-25 02:50:29 -07:00
Thom Chiovoloni e32ea822c3 Add tvOS and watchOS to CI 2022-10-24 23:24:56 -07:00
Thom Chiovoloni e6ed8e1a55 Add tvOS and watchOS support, identical to iOS support 2022-10-24 18:21:50 -07:00
Joseph Richey 2aabb63d2c Merge pull request #316 from rust-random/ci
CI: Run iOS targets and other improvements
2022-10-24 12:22:23 -07:00
Joe Richey 4917673cd2 Break up Tier 3 builds
Signed-off-by: Joe Richey <joerichey@google.com>
2022-10-24 01:56:17 -07:00
Joe Richey b35add2bb7 Uptade to Swatinem/rust-cache@v2
Also move the cache step to be right before we start running cargo
commands.

Signed-off-by: Joe Richey <joerichey@google.com>
2022-10-24 01:56:17 -07:00
Joe Richey a91ad48d8c Update to actions/checkout@v3
Signed-off-by: Joe Richey <joerichey@google.com>
2022-10-24 01:56:17 -07:00
Joe Richey b1da11b850 Run iOS Simulator Tests
This builds, links, and runs the tests on `x86_64-apple-ios` using
[`dinghy`](https://github.com/sonos/dinghy). It moves the targets
which only Build/Link to their own job.

Signed-off-by: Joe Richey <joerichey@google.com>
2022-10-24 01:56:10 -07:00
Joe Richey 14b461fa39 Make job names consistent
This makes it easier to tell on which targets we:
  - Build
  - Link
  - Run

Signed-off-by: Joe Richey <joerichey@google.com>
2022-10-24 01:50:03 -07:00
Joe Richey 762c95dd7a Move from actions-rs/toolchain to dtolnay/rust-toolchain
The current action we are using is unmantained:
https://github.com/actions-rs/toolchain/issues/216

This replacement is faster, smaller, and more cachable.

We can also completely remove the toolchain step when testing with
cross, as cross handles configuration of its own toolchain.

Signed-off-by: Joe Richey <joerichey@google.com>
2022-10-23 21:32:21 -07:00
Joseph Richey 52a4c3cbad Remove buffer zeroing from Node.js implementation (#315)
We can use
[`Uint8Array::view_mut_raw`](https://rustwasm.github.io/wasm-bindgen/api/js_sys/struct.Uint8Array.html#method.view_mut_raw)
which was
[added](https://github.com/rustwasm/wasm-bindgen/pull/1850) in version
`0.2.54` of `wasm-bindgen`. This method was introduced to deal with
uninitialized memory, see the Safety comment for more info.

Signed-off-by: Joe Richey <joerichey@google.com>
2022-10-23 16:22:28 -07:00
Joseph Richey 710b24dd27 Use getentropy on Emscripten (#307)
Signed-off-by: Joe Richey <joerichey@google.com>

Signed-off-by: Joe Richey <joerichey@google.com>
2022-10-23 15:09:31 -07:00
Joseph Richey 68b4d7a8fc Merge pull request #303 from rust-random/wasm64
Add wasm64-unknown-unknown support
2022-10-23 14:57:21 -07:00
Joe Richey 3fa761b949 Add chunking for Node.js implementation
Signed-off-by: Joe Richey <joerichey@google.com>
2022-10-23 13:47:00 -07:00
Joe Richey 1d371ef5e2 Add wasm64-unknown-unknown support
We can build and link just fine, but we cannot actually run the tests as
`wasm-bindgen-test-runner` hasn't yet added support.

Signed-off-by: Joe Richey <joerichey@google.com>
2022-10-23 13:47:00 -07:00
Joseph Richey f2d76625d5 Solaris: consistantly use /dev/random source (#310)
On Solaris, we opt to use /dev/random source instead of /dev/urandom due
to reasons explained in the comments and
[in this Solaris blog post](https://blogs.oracle.com/solaris/post/solaris-new-system-calls-getentropy2-and-getrandom2).

However, we haven't been making the same choice when getting randomness
via the `getrandom(2)` function, as we just pass `0` for the flags. We
[used to](https://github.com/rust-random/rand/pull/730/files#diff-694d4302a3ff2a976f2fbd34bc05ada22ee61a4e21d2d985beab27f7a809268fR151)
always set `GRND_RANDOM`, but that was removed in the move from `OsRng`
to this crate.

For context, https://github.com/rust-random/rand/pull/730,
https://github.com/rust-random/getrandom/pull/9, and
https://github.com/rust-random/getrandom/pull/51 are the major changes
to the Solaris/Illumos implementation over the years.

See the solaris documentation for:
- [`getrandom(2)`](https://docs.oracle.com/cd/E88353_01/html/E37841/getrandom-2.html)
- [`urandom(4)`](https://docs.oracle.com/cd/E88353_01/html/E37851/urandom-4d.html)

I also updated the doucmentation to better reflect when
[Illumos added the `getrandom(2)` function](https://www.illumos.org/issues/9971#change-23483).

Signed-off-by: Joe Richey <joerichey@google.com>
2022-10-23 13:46:01 -07:00
Joseph Richey ad08dd9e72 Cleanup wasm32-wasi target (#306)
* Cleanup wasm32-wasi target

This change ensures that we only compile our WASI implementation for
32-bit targets. The interaction between the WASI proposal and the
memory64 proposal is not yet clear, [wasmtime does not yet support](
https://github.com/bytecodealliance/wasmtime/issues/3594#issuecomment-992590383)
using WASI with memory64, and many of the interfaces use 32-bit values
for pointers.

This change also reduces the use of `unsafe` from the wasi
implementation. As noted in #253, changes to `Errno` mean that we can't
get the error message from the raw error code, but we can avoid using
unsafe when converting this code to a NonZeroU32. This handling also
makes WASI behave more like our other targets, which also manually check
that errno is non-zero.

Signed-off-by: Joe Richey <joerichey@google.com>

* Disable default features for WASI crate

Similar to this crate, the `wasi` crate just uses a `std` feature to
implement `std::error::Errno`, which we don't use.

Signed-off-by: Joe Richey <joerichey@google.com>

Signed-off-by: Joe Richey <joerichey@google.com>
2022-10-23 13:29:09 -07:00
Joseph Richey d87d3399bb Document use of /dev/urandom vs /dev/random (#311)
Also, on platforms where the devices are the same, prefer using
/dev/urandom.

Documentation of the devices being the same:
- macOS [`random(4)`](https://www.unix.com/man-page/mojave/4/urandom/)
- DragonFly [`getrandom(2)`](https://leaf.dragonflybsd.org/cgi/web-man?command=getrandom)
- Haiku [implementation](https://github.com/haiku/haiku/blob/f40bacae87f37276be7d107a6b3d41ca97b4d82c/src/add-ons/kernel/bus_managers/random/driver.cpp#L217-L222)

Signed-off-by: Joe Richey <joerichey@google.com>

Signed-off-by: Joe Richey <joerichey@google.com>
2022-10-23 13:26:32 -07:00
Joseph Richey 88d0d31a2c Move 3ds selection above rdrand/js/custom fallback (#312)
This makes the big cfg_if easier to read.

Signed-off-by: Joe Richey <joerichey@google.com>
2022-10-23 12:59:43 -07:00
Joseph Richey 412e0d5ff5 Merge pull request #309 from briansmith/b/test-benches
CI: On Nightly toolchains, test the benchmarks instead of just building them.
2022-10-23 00:26:23 -07:00
Brian Smith 84883207b9 CI: On Nightly toolchains, test the benchmarks instead of just building them.
`cargo test` will run one iteration of each benchmark to provide evidence
that they don't fail in an obvious way (e.g. unintended panics).
2022-10-22 23:16:50 -07:00
Joseph Richey 2ec38ad931 Merge pull request #304 from rust-random/rng_tests
This PR adds some tests which make sure calls to getrandom (for both small and large buffers) "look" random.

While we could certainly add more complicated randomness tests, these simple tests are:
  - Very easy to understand
  - Don't require any external crates
  - Makes sure we aren't doing something obviously stupid like
    - forgetting [these lines](https://github.com/rust-random/getrandom/blob/bd0654fe70980583e51573e755bafa3b2f8342d9/src/rdrand.rs#L91-L95)
    - failing to initialize every other byte
    - initializing some significant fraction of bytes with a constant

As this tests all buffer sizes from 1 to 64, it also fixes #290.
2022-10-22 14:21:31 -07:00
Joe Richey b893cb9c77 Use iterators for num_diff_bits
Signed-off-by: Joe Richey <joerichey@google.com>
2022-10-22 13:52:42 -07:00
Joe Richey c83edb650b Add quality tests for calling getrandom on small buffers
Signed-off-by: Joe Richey <joerichey@google.com>
2022-10-22 13:37:23 -07:00
Joe Richey c12f70c20a Sharpen bounds in test_diff
We can be much stricter without risking inadvertant failure.

Signed-off-by: Joe Richey <joerichey@google.com>
2022-10-22 13:37:21 -07:00
Joseph Richey 97c17892e7 Merge pull request #300 from rust-random/custom_test
Tests: Use custom tests to verify operations on empty slices are no-ops.

Reworking of #299 to keep our custom tests in a single file (and keep things better organized in general).
2022-10-22 13:32:14 -07:00
Brian Smith cf85546863 Tests: Use custom tests to verify operations on empty slices are no-ops.
Modify the custom tests so that they would have detected and prevented
the issue fixed in https://github.com/rust-random/getrandom/pull/298.
2022-10-22 13:25:44 -07:00
Joe Richey ee0ca47aa2 Run (most) common tests against the custom RNG
Signed-off-by: Joe Richey <joerichey@google.com>
2022-10-22 13:25:44 -07:00
Joe Richey 92af82ab9a Remove state from Custom RNG
This makes it much easier to run this RNG alongside other tests.

Signed-off-by: Joe Richey <joerichey@google.com>
2022-10-22 13:25:44 -07:00
Joseph Richey ce5c2d96ac Test emscripten via Cross (#302)
Part of addressing #275 

Signed-off-by: Joe Richey <joerichey@google.com>
2022-10-22 13:23:49 -07:00
Joseph Richey d2ca7695e3 Fix Solaris/OpenBSD/Dragonfly build and re-enable CI (#301)
Fixes #216 

This also adds two minor CI improvements:
  - Do a full link on `freebsd`
  - Build, Link, but don't run on `illumos`
  - Use the stable toolchain for our Tier 2 Build-only targets
  - Build (via `build-std`) for Tier 3 targets: `openbsd`, `dragonfly`, `haiku`

Signed-off-by: Joe Richey <joerichey@google.com>
2022-10-22 13:22:38 -07:00
Joseph Richey f7b733042c Update MSRV in .clippy.toml (#305)
This reflects the changes in #291
2022-10-22 11:20:18 +00:00
Joseph Richey bd0654fe70 Rework benchmarks to make it easier to get assembly. (#297)
* Rename benches/mod.rs to benches/buffer.rs

This naming makes more sense, especially if we add more benchmark
files.

Signed-off-by: Joe Richey <joerichey@google.com>

* Rework benchmarks to make it easier to get assembly.

This change:
  - Move the benchmarks from mod.rs to buffer.rs
  - Move the inner loop we benchmark into an `#[inline(never)]` function
  - Includes instructions for getting the ASM for a specific benchmark

This should hopefully reduce the variance of these benchmarks and make
it easier to figure out if we are emitting the assembly or IR we expect
for a particular implementation.

Signed-off-by: Joe Richey <joerichey@google.com>

Signed-off-by: Joe Richey <joerichey@google.com>
2022-10-21 14:10:44 +00:00
Joseph Richey 55ad4c41ba Add back empty slice check (#298)
https://github.com/rust-random/getrandom/pull/291 inadvertantly removed
this check

See https://github.com/rust-random/getrandom/pull/104 for why this was
added in the first place. Also, per our docs:

> If `dest` is empty, `getrandom` immediately returns success, making
> no calls to the underlying operating system.

Signed-off-by: Joe Richey <joerichey@google.com>
2022-10-21 14:02:34 +00:00
Brian Smith 47a59dda25 Add getrandom_uninit_slice(dest: &mut [MaybeUninit<u8>]) -> .... (#291)
* Add `getrandom_uninit(dest: &mut [MaybeUninit<u8>]) -> ...`.

Add a public API for filling an `&mut [MaybeUninit<u8>]`. This will primarily
serve as the building block for more typeful APIs for constructing random
arrays.

Increase the MSRV to 1.36, as `MaybeUninit` was added in that release.

Fixes #226.

* Revert testing changes

Signed-off-by: Joe Richey <joerichey@google.com>

* Allow rdrand tests to work with new implementation

Signed-off-by: Joe Richey <joerichey@google.com>

* Add Additional benchmarks and buffer size

Signed-off-by: Joe Richey <joerichey@google.com>

* Use pointer casts instead of transmute

Signed-off-by: Joe Richey <joerichey@google.com>

* Avoid initializing the buffer in `getrandom_uninit` benchmarks.

* Benchmarks: Consume the result in `black_box`.

Signed-off-by: Joe Richey <joerichey@google.com>
Co-authored-by: Joe Richey <joerichey@google.com>
2022-10-20 19:09:20 -07:00
Joseph Richey 5c1bb00b74 Release v0.2.8 (#294)
Also updates the changelog

Signed-off-by: Joe Richey <joerichey@google.com>
2022-10-20 13:53:06 -07:00
Joseph Richey 353d0ca157 Update docs for wasm32-unknown-unknown implementation (#295)
Signed-off-by: Joe Richey <joerichey@google.com>
2022-10-20 02:33:05 -07:00
Joseph Richey cfdad53dce Merge pull request #284 from rust-random/webcrypto
Rework JS feature detection
2022-10-06 15:06:39 -07:00
Joe Richey 9962c706c9 Update Module::require internal comments
Signed-off-by: Joe Richey <joerichey@google.com>
2022-10-06 14:52:39 -07:00
Joe Richey e0c93b10d5 Catch call to module.require
This call throws an exception if module isn't defined.

Signed-off-by: Joe Richey <joerichey@google.com>
2022-10-06 14:52:07 -07:00
Joe Richey 0503000381 Fix link typo
Signed-off-by: Joe Richey <joerichey@google.com>
2022-10-06 14:51:49 -07:00
Joe Richey 0579fe3014 Update documentation and error messages
This allows users to get an actionable error message about this
particular problem. We also add detection for this problem.

Signed-off-by: Joe Richey <joerichey@google.com>
2022-09-13 02:56:51 -07:00
Joe Richey d69e8e003b Rework JS feature detection
Now we look for the standard Web Cryptography API before attempting to
check for Node.js support. This allows Node.js ES6 module users to add
a polyfill like:
```js
import {webcrypto} from 'crypto'
globalThis.crypto = webcrypto
```
as described in https://github.com/rust-random/getrandom/issues/256#issuecomment-1161028902

Signed-off-by: Joe Richey <joerichey@google.com>
2022-09-13 00:37:43 -07:00
Mauri Mustonen d3aa089bbd Add description about Cargo js feature for WebAssembly section (#280)
* add description about Cargo js feature
* add note about different crate types
* clean up
* Fix wording/grammer
* Remove "Indirect Depenencies" section
* Note that libraries shouldn't add their own JS feature

Signed-off-by: Joe Richey <joerichey@google.com>
Co-authored-by: Mauri Mustonen <mauri.mustonen@hotmail.com>
Co-authored-by: Joe Richey <joerichey@google.com>
2022-08-30 01:06:17 -07:00
Artyom Pavlov 0b71d50b49 Fix link to wasm-bindgen (#278) 2022-08-18 18:58:34 -07:00
Autumn 9a64857ae6 document the variation in underlying details of abstracted randomness sources (#276) 2022-08-18 01:53:05 +00:00
Joseph Richey 7089766df0 Add/Rework benchmarks to track initialization cost (#272)
This PR adds more benchmarks so we can get and accurate idea about two
things:

  - What is the cost of having to zero the buffer before calling
    `getrandom`?
  - What is the performance on aligned, 32-byte buffers?
    - This is by far the most common use, as its used to seed
      usersapce CSPRNGs.

I ran the benchmarks on my system:
  - CPU: AMD Ryzen 7 5700G
  - OS: Linux 5.15.52-1-lts
  - Rust Version: 1.62.0-nightly (ea92b0838 2022-05-07)

I got the following results:
```
test bench_large      ... bench:   3,759,323 ns/iter (+/- 177,100) = 557 MB/s
test bench_large_init ... bench:   3,821,229 ns/iter (+/- 39,132) = 548 MB/s
test bench_page       ... bench:       7,281 ns/iter (+/- 59) = 562 MB/s
test bench_page_init  ... bench:       7,290 ns/iter (+/- 69) = 561 MB/s
test bench_seed       ... bench:         206 ns/iter (+/- 3) = 155 MB/s
test bench_seed_init  ... bench:         206 ns/iter (+/- 1) = 155 MB/s
```

These results were very consistent across multiple runs, and roughtly
behave as we would expect:
  - The thoughput is highest with a buffer large enough to amoritize the
    syscall overhead, but small enough to stay in the L1D cache.
  - There is a _very_ small cost to zeroing the buffer beforehand.
  - This cost is imperceptible in the common 32-byte usecase, where the
    syscall overhead dominates.
  - The cost is slightly higher (1%) with multi-megabyte buffers as the
    data gets evicted from the L1 cache between the `memset` and the
    call to `getrandom`.

I would love to see results for other platforms. Could we get someone to
run this on an M1 Mac?

Signed-off-by: Joe Richey <joerichey@google.com>
2022-07-13 06:04:41 -07:00
Leonardo Yvens 3d818a6a0a use $crate in macro (#270)
This is the recommended way of referring to the current crate in a macro.
2022-07-07 23:48:06 -07:00
Joseph Richey 63f861c684 Release v0.2.7 (#264)
Signed-off-by: Joe Richey <joerichey@google.com>
2022-06-13 14:31:59 -07:00
Joseph Richey c82a522713 Use AtomicPtr instead of AtomicUsize for Weak (#263)
This allows Strict Provenance to work properly, fixing #262. It also
now matches what `libstd` does:
https://github.com/rust-lang/rust/blob/9f7e997c8bc3cacd2ab4eb75e63cb5fa9279c7b0/library/std/src/sys/unix/weak.rs#L85-L141

Also, while reading the `libstd` code, I noticed that they use an
`Acquire` fence and `Release` store as the returned pointer should
have "consume" semantics. As this doesn't yet exist in Rust, we
instead do exactly what `libstd` does, which means:
  - Relaxed Load
  - Release Store
  - Acquire fence when returning pointer

Signed-off-by: Joe Richey <joerichey@google.com>

Co-authored-by: Joe ST <joe@fbstj.net>
2022-06-13 12:10:19 -07:00