Skip to content

Add custom-fallback Backend #684

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
wants to merge 7 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 20 additions & 0 deletions .github/workflows/tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -358,3 +358,23 @@ jobs:
run: cargo test --target wasm32-wasip1
- name: WASI 0.2 Test
run: cargo test --target wasm32-wasip2

fallback:
name: Custom Fallback
runs-on: ubuntu-24.04
steps:
- uses: actions/checkout@v4
- uses: dtolnay/rust-toolchain@master
with:
toolchain: 1.82
targets: thumbv6m-none-eabi # A target getrandom doesn't currently support
- uses: Swatinem/rust-cache@v2
- name: No Fallback (Should Fail)
run: $(cargo build --target thumbv6m-none-eabi; if [ $? -ne 0 ]; then exit 0; else exit 1; fi)
working-directory: fallback_test
- name: Fallback
run: cargo build --target thumbv6m-none-eabi --features fallback
working-directory: fallback_test
- name: Double Fallback (Should Fail)
run: $(cargo build --target thumbv6m-none-eabi --features fallback,fail-double-definition; if [ $? -ne 0 ]; then exit 0; else exit 1; fi)
working-directory: fallback_test
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,5 @@
**/*.rs.bk
nopanic_check/Cargo.lock
nopanic_check/target/
fallback_test/Cargo.lock
fallback_test/target/
7 changes: 7 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,12 @@ rustc-dep-of-std = ["dep:compiler_builtins", "dep:core"]
# i.e. avoid unconditionally enabling it in library crates.
wasm_js = ["dep:wasm-bindgen", "dep:js-sys"]

# Optional backend: custom-fallback
# This flag allows a custom fallback backend.
# This will be used as a last-resort instead of raising a compiler error when
# no other backend is available.
custom-fallback = []
Comment on lines +28 to +32
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Change the documentation to say that this feature should only be enabled by a crate which provides a custom fallback.


[dependencies]
cfg-if = "1"

Expand Down Expand Up @@ -90,6 +96,7 @@ check-cfg = [
'cfg(getrandom_test_linux_without_fallback)',
'cfg(getrandom_test_netbsd_fallback)',
'cfg(target_os, values("cygwin"))', # TODO(MSRV 1.86): Remove this.
'cfg(getrandom_no_custom_fallback)',
]

[package.metadata.docs.rs]
Expand Down
19 changes: 19 additions & 0 deletions fallback_test/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
[package]
name = "fallback_test"
description = "Test crate for assessing the custom-fallback feature"
version = "0.1.0"
edition = "2021"
publish = false

[dependencies]
getrandom = { path = "..", default-features = false }

[features]
fallback = ["getrandom/custom-fallback"]
fail-double-definition = ["fallback"]

[profile.release]
panic = "abort"

[profile.dev]
panic = "abort"
56 changes: 56 additions & 0 deletions fallback_test/src/main.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
#![no_std]
#![no_main]

#[panic_handler]
fn handle(_info: &core::panic::PanicInfo) -> ! {
loop {}
}

use getrandom::{Backend, Error};

/// This implementation fills using a constant value of 4.
///
/// WARNING: this custom implementation is for testing purposes ONLY!
struct ConstantBackend;

unsafe impl Backend for ConstantBackend {
#[inline]
fn fill_uninit(dest: &mut [core::mem::MaybeUninit<u8>]) -> Result<(), Error> {
for out in dest {
// Chosen by fair dice roll
out.write(4);
}

Ok(())
}
}

#[cfg(feature = "fallback")]
const _: () = {
getrandom::set_backend!(ConstantBackend);
};

// This second call will cause the following compile-time error:
/*
error: symbol `__getrandom_v03_fallback_fill_uninit` is already defined
--> src\main.rs:43:1
|
43 | getrandom::set_backend!(ConstantBackend);
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
= note: this error originates in the macro `getrandom::set_backend` (in Nightly builds, run with -Z macro-backtrace for more info)

error: could not compile `fallback_test` (bin "fallback_test") due to 1 previous error
*/
#[cfg(feature = "fail-double-definition")]
const _: () = {
getrandom::set_backend!(ConstantBackend);
};

#[no_mangle]
pub extern "C" fn _start() {
let mut dest = [0u8; 13];
let result = getrandom::fill(&mut dest);
assert!(result.is_ok());
assert_eq!(dest, [4; 13]);
}
Loading
Loading