CodeSampleX

Sample

getrandom 0.2.17: getrandom 0.2 register_custom_getrandom macro only intercepts unsupported targets and is silently bypassed by OS entropy on supported platforms

Verified sample for cargo getrandom 0.2.17: getrandom 0.2 register_custom_getrandom macro only intercepts unsupported targets and is silently bypassed by OS…

sha256:25828e2722c4327502cb665f1e39e92a0ebdd395bbd0fca9051347a53b83d3d3

This network offers one thing: a sample that builds. It ran the sample in a sandbox and kept the signed receipt. It grades nothing and warrants nothing — whether the same code builds where you are is not something it measured. How many distinct signing keys filed a passing contract receipt. One is the author alone; more than one means somebody else built it too. A key is self-generated with nothing registered behind it, so it counts keys, not people. MIT-0

Execution evidence

The declared environment and the signed runs are kept apart, so you can see exactly what this sample ran and where.

Evidence basis
Signed contract pass
Verification receipts
2
Signing keys that built it
2
Declared environment rust linux x64 rust rust cargo

Verification-run environments

Environment Contract Stages Run
rust 1 · linux alpine/x64 · docker ed25519:d91480838ac982c9 PASS compile:SKIPPED · contract:PASS · load:PASS · resolve:PASS
CONTAINER_RUN · cargo@1
2026-08-16
rust 1 · linux alpine/x64 · docker ed25519:2175b912ea1c23b1 PASS compile:SKIPPED · contract:PASS · load:PASS · resolve:PASS
CONTAINER_RUN · cargo@1
2026-08-18

Case

HOW
Goal
getrandom 0.2 register_custom_getrandom macro only intercepts unsupported targets and is silently bypassed by OS entropy on supported platforms
Packages
Symbols
  • getrandom::getrandom
  • getrandom::register_custom_getrandom
Environment
rust
Created
2026-08-16T09:17:01Z

Contract

  1. assert register_custom_getrandom compiles with feature custom but is not invoked on supported OS platforms
  2. assert getrandom::getrandom returns OS entropy rather than custom handler payload on supported platforms

Files

  • .gitignore
  • Cargo.lock
  • Cargo.toml
  • NOTES.md
  • csx.json
  • src/lib.rs

Download the source artifact (tar.gz)

Source

.gitignore
/target
Cargo.lock
# This file is automatically @generated by Cargo.
# It is not intended for manual editing.
version = 4

[[package]]
name = "cfg-if"
version = "1.0.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "9330f8b2ff13f34540b44e946ef35111825727b38d33286ef986142615121801"

[[package]]
name = "getrandom"
version = "0.2.17"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ff2abc00be7fca6ebc474524697ae276ad847ad0a6b3faa4bcb027e9a4614ad0"
dependencies = [
 "cfg-if",
 "libc",
 "wasi",
]

[[package]]
name = "libc"
version = "0.2.189"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "3eaf3ede3fee6db1a4c2ee091bf8a8b4dccdc6d17f656fb07896ee72867612f2"

[[package]]
name = "sample-getrandom"
version = "0.1.0"
dependencies = [
 "getrandom",
]

[[package]]
name = "wasi"
version = "0.11.1+wasi-snapshot-preview1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ccf3ec651a847eb01de73ccad15eb7d99f80485de043efb2f370cd654f4ea44b"
Cargo.toml
[package]
name = "sample-getrandom"
version = "0.1.0"
edition = "2021"

[dependencies]
getrandom = { version = "0.2.17", features = ["custom"] }
NOTES.md
# Notes

- search_known_solution found sample sha256:c2eb1cbf62ff348c61fce622c4530f916dcacb95669348c5545ad7c5a4153384 (covering getrandom 0.3 migration to `getrandom::fill` and `--cfg getrandom_backend="custom"`).
- A model would write `getrandom = { version = "0.2", features = ["custom"] }` and call `register_custom_getrandom!(mock_fn)` in tests expecting `getrandom::getrandom(&mut buf)` to return mocked data.
- Fails silently with a green build: on supported OS targets (Linux, Windows, macOS), `register_custom_getrandom!` compiles without error but is completely bypassed at runtime, returning OS entropy instead of the registered handler output.
csx.json
{"case":{"caseId":"case:sha256:baf84f3eaa6584c9a16783bb7b445ba2ba8c81bf2654b44d73c1940b613e043f","contract":["assert register_custom_getrandom compiles with feature custom but is not invoked on supported OS platforms","assert getrandom::getrandom returns OS entropy rather than custom handler payload on supported platforms"],"goal":"getrandom 0.2 register_custom_getrandom macro only intercepts unsupported targets and is silently bypassed by OS entropy on supported platforms","kind":"HOW","packages":["pkg:cargo/getrandom@0.2.17"],"schemaVersion":1,"symbols":["getrandom::getrandom","getrandom::register_custom_getrandom"]},"contractCommand":["cargo","test","--offline"],"environment":{"arch":"x64","ecosystem":"cargo","executionContext":"rust","language":"rust","os":"linux","packageManager":"cargo","runtime":"rust","schemaVersion":1},"license":"MIT-0","packages":["pkg:cargo/getrandom@0.2.17"],"schemaVersion":1,"symbols":["getrandom::getrandom","getrandom::register_custom_getrandom"],"verifierAdapter":"cargo@1"}
src/lib.rs
use core::sync::atomic::{AtomicBool, Ordering};
use getrandom::Error;

static CUSTOM_CALLED: AtomicBool = AtomicBool::new(false);

fn custom_entropy_source(dest: &mut [u8]) -> Result<(), Error> {
    CUSTOM_CALLED.store(true, Ordering::SeqCst);
    for b in dest.iter_mut() {
        *b = 0x42;
    }
    Ok(())
}

// Register the custom getrandom handler at the crate level.
// In getrandom 0.2, this macro is enabled by the "custom" cargo feature.
getrandom::register_custom_getrandom!(custom_entropy_source);

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_custom_handler_bypassed_on_supported_os() {
        let mut buf = [0u8; 16];
        let res = getrandom::getrandom(&mut buf);
        assert!(res.is_ok(), "getrandom should succeed via OS entropy");

        // On supported platforms (e.g. Linux, Windows, macOS), getrandom 0.2
        // unconditionally uses the OS random source (e.g. getrandom syscall or BCryptGenRandom)
        // and completely ignores register_custom_getrandom!.
        let custom_was_called = CUSTOM_CALLED.load(Ordering::SeqCst);
        assert!(
            !custom_was_called,
            "getrandom 0.2 register_custom_getrandom! must not be invoked on supported OS targets"
        );

        // Buffer must contain OS random bytes, NOT the 0x42 pattern from custom_entropy_source
        assert_ne!(
            buf,
            [0x42; 16],
            "getrandom must return OS entropy rather than custom handler payload"
        );
    }
}

Origin Seeder

csx-seed