diff options
| author | Chris Denton <chris@chrisdenton.dev> | 2024-02-21 08:40:02 -0300 |
|---|---|---|
| committer | Chris Denton <chris@chrisdenton.dev> | 2024-02-21 09:24:54 -0300 |
| commit | aa99d954faae0446664f82b0c3c597664845ca90 (patch) | |
| tree | 8c33fef28875c53abcb8f5d32fe9c478ed6d945b | |
| parent | 99df70896b0bce7ab9d4695419e2804d5ad6a10a (diff) | |
| download | rust-aa99d954faae0446664f82b0c3c597664845ca90.tar.gz rust-aa99d954faae0446664f82b0c3c597664845ca90.zip | |
Test windows random shims
| -rw-r--r-- | src/tools/miri/tests/pass/shims/windows-rand.rs | 41 |
1 files changed, 41 insertions, 0 deletions
diff --git a/src/tools/miri/tests/pass/shims/windows-rand.rs b/src/tools/miri/tests/pass/shims/windows-rand.rs new file mode 100644 index 00000000000..e2bcb7bd7cb --- /dev/null +++ b/src/tools/miri/tests/pass/shims/windows-rand.rs @@ -0,0 +1,41 @@ +//@only-target-windows: this directly tests windows only random functions +use core::ffi::c_void; +use core::mem::size_of_val; +use core::ptr::null_mut; + +// Windows API definitions. +type NTSTATUS = i32; +type BOOLEAN = u8; +const BCRYPT_USE_SYSTEM_PREFERRED_RNG: u32 = 0x00000002; +const BCRYPT_RNG_ALG_HANDLE: *mut c_void = 0x81 as *mut c_void; +#[link(name = "bcrypt")] +extern "system" { + fn BCryptGenRandom( + halgorithm: *mut c_void, + pbbuffer: *mut u8, + cbbuffer: u32, + dwflags: u32, + ) -> NTSTATUS; +} +#[link(name = "advapi32")] +extern "system" { + #[link_name = "SystemFunction036"] + fn RtlGenRandom(RandomBuffer: *mut u8, RandomBufferLength: u32) -> BOOLEAN; +} + +fn main() { + let mut key = [0u8; 24]; + let len: u32 = size_of_val(&key).try_into().unwrap(); + let ret = unsafe { + BCryptGenRandom(null_mut(), key.as_mut_ptr(), len, BCRYPT_USE_SYSTEM_PREFERRED_RNG) + }; + // NTSTATUS codes use the high bit to indicate an error + assert!(ret >= 0); + + let ret = unsafe { BCryptGenRandom(BCRYPT_RNG_ALG_HANDLE, key.as_mut_ptr(), len, 0) }; + assert!(ret >= 0); + + let ret = unsafe { RtlGenRandom(key.as_mut_ptr(), len) }; + // RtlGenRandom returns a BOOLEAN where 0 indicates an error + assert_ne!(ret, 0); +} |
