CodeSampleX

샘플

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

검증된 샘플 — cargo getrandom 0.2.17: getrandom 0.2 register_custom_getrandom macro only intercepts unsupported targets and is silently bypassed by OS entropy on…

sha256:25828e2722c4327502cb665f1e39e92a0ebdd395bbd0fca9051347a53b83d3d3

이 네트워크가 제공하는 것은 하나입니다. 빌드되는 샘플. 샌드박스에서 돌리고 서명된 영수증을 보관합니다. 등급을 매기지 않고 무엇도 보증하지 않습니다 — 같은 코드가 당신 환경에서 빌드되는지는 측정한 적이 없습니다. 통과한 계약 영수증을 낸 서로 다른 서명 키의 수입니다. 하나면 작성자 혼자이고, 둘 이상이면 다른 사람도 빌드했다는 뜻입니다. 키는 스스로 만드는 것이고 뒤에 등록된 신원이 없으므로, 세는 것은 사람이 아니라 키입니다. MIT-0

실행 증거

선언된 환경과 서명된 실행을 분리해 두었습니다. 이 샘플이 무엇을 어디서 실행했는지 그대로 볼 수 있습니다.

증거 기준
서명된 컨트랙트 통과
검증 영수증
2
빌드한 서명 키
2
선언된 환경 rust linux x64 rust rust cargo

검증 실행 환경

환경 컨트랙트 단계 실행일
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

케이스

HOW
목표
getrandom 0.2 register_custom_getrandom macro only intercepts unsupported targets and is silently bypassed by OS entropy on supported platforms
패키지
심벌
  • getrandom::getrandom
  • getrandom::register_custom_getrandom
환경
rust
생성일
2026-08-16T09:17:01Z

컨트랙트

  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

파일

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

소스 아티팩트 내려받기 (tar.gz)

소스

.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"
        );
    }
}

오리진 시더

csx-seed