From 3055dbaa2b4348fcec031e603f1b59e45ceedff5 Mon Sep 17 00:00:00 2001 From: Mark Drobnak Date: Sun, 20 Feb 2022 20:50:54 -0800 Subject: [PATCH 1/4] Add support for the Nintendo 3DS (armv6k-nintendo-3ds) --- src/3ds.rs | 17 +++++++++++++++++ src/lib.rs | 6 ++++++ src/util_libc.rs | 6 ++++++ 3 files changed, 29 insertions(+) create mode 100644 src/3ds.rs diff --git a/src/3ds.rs b/src/3ds.rs new file mode 100644 index 000000000..60305127e --- /dev/null +++ b/src/3ds.rs @@ -0,0 +1,17 @@ +// Copyright 2021 Developers of the Rand project. +// +// Licensed under the Apache License, Version 2.0 or the MIT license +// , at your +// option. This file may not be copied, modified, or distributed +// except according to those terms. + +//! Implementation for Nintendo 3DS +use crate::util_libc::sys_fill_exact; +use crate::Error; + +pub fn getrandom_inner(dest: &mut [u8]) -> Result<(), Error> { + sys_fill_exact(dest, |buf| unsafe { + libc::getrandom(buf.as_mut_ptr() as *mut libc::c_void, buf.len(), 0) + }) +} diff --git a/src/lib.rs b/src/lib.rs index 336c8ae19..b0ab00651 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -33,6 +33,7 @@ //! | Web Browser | `wasm32‑*‑unknown` | [`Crypto.getRandomValues()`][14], see [WebAssembly support][16] //! | Node.js | `wasm32‑*‑unknown` | [`crypto.randomBytes`][15], see [WebAssembly support][16] //! | SOLID | `*-kmc-solid_*` | `SOLID_RNG_SampleRandomBytes` +//! | Nintendo 3DS | `armv6k-nintendo-3ds` | [`getrandom`][1] //! //! There is no blanket implementation on `unix` targets that reads from //! `/dev/urandom`. This ensures all supported targets are using the recommended @@ -223,6 +224,11 @@ cfg_if! { } else if #[cfg(all(feature = "js", target_arch = "wasm32", target_os = "unknown"))] { #[path = "js.rs"] mod imp; + } else if #[cfg(all(target_os = "horizon", target_arch = "arm"))] { + // We check for target_arch = "arm" because the Nintendo Switch also + // uses Horizon OS (it is aarch64). + mod util_libc; + #[path = "3ds.rs"] mod imp; } else if #[cfg(feature = "custom")] { use custom as imp; } else if #[cfg(all(target_arch = "wasm32", target_os = "unknown"))] { diff --git a/src/util_libc.rs b/src/util_libc.rs index 682360972..f14a34a81 100644 --- a/src/util_libc.rs +++ b/src/util_libc.rs @@ -20,6 +20,12 @@ cfg_if! { use libc::__error as errno_location; } else if #[cfg(target_os = "haiku")] { use libc::_errnop as errno_location; + } else if #[cfg(target_os = "horizon")] { + extern "C" { + // Not provided by libc: https://github.com/rust-lang/libc/issues/1995 + fn __errno() -> *mut libc::c_int; + } + use __errno as errno_location; } } From 4376014284401877e9e1c48bcdc8a2d1e92c5f02 Mon Sep 17 00:00:00 2001 From: Mark Drobnak Date: Mon, 14 Mar 2022 19:25:21 -0700 Subject: [PATCH 2/4] Bump version of libc to 0.2.120 Needed for armv6k-nintendo-3ds support. --- Cargo.toml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Cargo.toml b/Cargo.toml index 3b42b7606..b09a27d9a 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -18,7 +18,7 @@ compiler_builtins = { version = "0.1", optional = true } core = { version = "1.0", optional = true, package = "rustc-std-workspace-core" } [target.'cfg(unix)'.dependencies] -libc = { version = "0.2.64", default-features = false } +libc = { version = "0.2.120", default-features = false } [target.'cfg(target_os = "wasi")'.dependencies] wasi = "0.10" From 0321d49bff8f3cc29a6c5170b705561aab837f11 Mon Sep 17 00:00:00 2001 From: Mark Drobnak Date: Tue, 15 Mar 2022 17:20:21 -0700 Subject: [PATCH 3/4] Use the same cfg condition in util_libc as lib.rs for the 3DS --- src/util_libc.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/util_libc.rs b/src/util_libc.rs index f14a34a81..7e955b94e 100644 --- a/src/util_libc.rs +++ b/src/util_libc.rs @@ -20,7 +20,7 @@ cfg_if! { use libc::__error as errno_location; } else if #[cfg(target_os = "haiku")] { use libc::_errnop as errno_location; - } else if #[cfg(target_os = "horizon")] { + } else if #[cfg(all(target_os = "horizon", target_arch = "arm"))] { extern "C" { // Not provided by libc: https://github.com/rust-lang/libc/issues/1995 fn __errno() -> *mut libc::c_int; From b56d8c390ab5b647c352955b50f99779a0dd1576 Mon Sep 17 00:00:00 2001 From: Mark Drobnak Date: Sun, 20 Mar 2022 15:49:23 -0700 Subject: [PATCH 4/4] Add a build test for armv6k-nintendo-3ds --- .github/workflows/tests.yml | 2 ++ 1 file changed, 2 insertions(+) diff --git a/.github/workflows/tests.yml b/.github/workflows/tests.yml index d4965a49d..b4e937e0e 100644 --- a/.github/workflows/tests.yml +++ b/.github/workflows/tests.yml @@ -316,6 +316,8 @@ jobs: run: cargo build -Z build-std=core --target=x86_64-wrs-vxworks - name: SOLID run: cargo build -Z build-std=core --target=aarch64-kmc-solid_asp3 + - name: Nintendo 3DS + run: cargo build -Z build-std=core --target=armv6k-nintendo-3ds clippy-fmt: name: Clippy + rustfmt