From 029881c8a7ace14d09d629bfd953870cbcaedb2c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=90=D1=80=D1=82=D1=91=D0=BC=20=D0=9F=D0=B0=D0=B2=D0=BB?= =?UTF-8?q?=D0=BE=D0=B2=20=5BArtyom=20Pavlov=5D?= Date: Mon, 14 Oct 2024 20:34:30 +0300 Subject: [PATCH] Add no-panic check for the custom backend --- .github/workflows/nopanic.yaml | 7 +++++++ nopanic_check/Cargo.toml | 6 ++++++ nopanic_check/src/lib.rs | 12 ++++++++++++ 3 files changed, 25 insertions(+) diff --git a/.github/workflows/nopanic.yaml b/.github/workflows/nopanic.yaml index a4c24173..a5bd6e02 100644 --- a/.github/workflows/nopanic.yaml +++ b/.github/workflows/nopanic.yaml @@ -47,6 +47,13 @@ jobs: - name: Check (rdrand.rs) run: ret=$(grep panic target/release/libgetrandom_wrapper.so; echo $?); [ $ret -eq 1 ] + - name: Build (custom.rs) + env: + RUSTFLAGS: -Dwarnings --cfg getrandom_backend="custom" + run: cargo build --release + - name: Check (custom.rs) + run: ret=$(grep panic target/release/libgetrandom_wrapper.so; echo $?); [ $ret -eq 1 ] + - name: Build (wasi.rs, preview 1) run: cargo build --release --target wasm32-wasip1 - name: Check (wasi.rs, preview 1) diff --git a/nopanic_check/Cargo.toml b/nopanic_check/Cargo.toml index 11b51024..f8c21c68 100644 --- a/nopanic_check/Cargo.toml +++ b/nopanic_check/Cargo.toml @@ -14,3 +14,9 @@ getrandom = { path = ".." } [profile.release] panic = "abort" + +[lints.rust.unexpected_cfgs] +level = "warn" +check-cfg = [ + 'cfg(getrandom_backend, values("custom"))', +] diff --git a/nopanic_check/src/lib.rs b/nopanic_check/src/lib.rs index 0c0a7a0d..af2c8ee3 100644 --- a/nopanic_check/src/lib.rs +++ b/nopanic_check/src/lib.rs @@ -16,3 +16,15 @@ pub extern "C" fn getrandom_wrapper(buf_ptr: *mut u8, buf_len: usize) -> u32 { let res = getrandom::getrandom_uninit(buf).map(|_| ()); unsafe { core::mem::transmute(res) } } + +#[cfg(getrandom_backend = "custom")] +#[no_mangle] +unsafe extern "Rust" fn __getrandom_custom( + dest: *mut u8, + len: usize, +) -> Result<(), getrandom::Error> { + for i in 0..len { + core::ptr::write(dest.add(i), i as u8); + } + Ok(()) +}