Skip to content

Commit

Permalink
Auto merge of #3326 - rust-lang:rustup-2024-02-26, r=RalfJung
Browse files Browse the repository at this point in the history
Automatic Rustup

also fixes #3308
  • Loading branch information
bors committed Feb 26, 2024
2 parents bffd41b + aeab7ae commit 456c69e
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 1 deletion.
2 changes: 1 addition & 1 deletion rust-version
Original file line number Diff line number Diff line change
@@ -1 +1 @@
a2f3c0cf880ad819c4eab2b320525b6a31ac6513
0250ef2571185b05701ed9d74fc904c17508a397
8 changes: 8 additions & 0 deletions src/shims/windows/foreign_items.rs
Original file line number Diff line number Diff line change
Expand Up @@ -427,6 +427,14 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriInterpCxExt<'mir, 'tcx> {
this.gen_random(ptr, len.into())?;
this.write_scalar(Scalar::from_bool(true), dest)?;
}
"ProcessPrng" => {
let [ptr, len] =
this.check_shim(abi, Abi::System { unwind: false }, link_name, args)?;
let ptr = this.read_pointer(ptr)?;
let len = this.read_target_usize(len)?;
this.gen_random(ptr, len)?;
this.write_scalar(Scalar::from_i32(1), dest)?;
}
"BCryptGenRandom" => {
let [algorithm, ptr, len, flags] =
this.check_shim(abi, Abi::System { unwind: false }, link_name, args)?;
Expand Down
17 changes: 17 additions & 0 deletions tests/pass/shims/windows-rand.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ use core::ptr::null_mut;
// Windows API definitions.
type NTSTATUS = i32;
type BOOLEAN = u8;
type BOOL = i32; // yes, seriously, BOOL and BOOLEAN are very different...
const BCRYPT_USE_SYSTEM_PREFERRED_RNG: u32 = 0x00000002;
const BCRYPT_RNG_ALG_HANDLE: *mut c_void = 0x81 as *mut c_void;
#[link(name = "bcrypt")]
Expand All @@ -22,6 +23,16 @@ extern "system" {
#[link_name = "SystemFunction036"]
fn RtlGenRandom(RandomBuffer: *mut u8, RandomBufferLength: u32) -> BOOLEAN;
}
#[cfg(target_arch = "x86")]
#[link(name = "bcryptprimitives", kind = "raw-dylib", import_name_type = "undecorated")]
extern "system" {
pub fn ProcessPrng(pbdata: *mut u8, cbdata: usize) -> BOOL;
}
#[cfg(not(target_arch = "x86"))]
#[link(name = "bcryptprimitives", kind = "raw-dylib")]
extern "system" {
pub fn ProcessPrng(pbdata: *mut u8, cbdata: usize) -> BOOL;
}

fn main() {
let mut key = [0u8; 24];
Expand All @@ -38,4 +49,10 @@ fn main() {
let ret = unsafe { RtlGenRandom(key.as_mut_ptr(), len) };
// RtlGenRandom returns a BOOLEAN where 0 indicates an error
assert_ne!(ret, 0);

let len = key.len();
let ret = unsafe { ProcessPrng(key.as_mut_ptr(), len) };
// ProcessPrng is documented as always returning `TRUE`.
// https://learn.microsoft.com/en-us/windows/win32/seccng/processprng#return-value
assert_eq!(ret, 1);
}

0 comments on commit 456c69e

Please sign in to comment.