This repository was archived by the owner on May 28, 2025. It is now read-only.
File tree Expand file tree Collapse file tree 1 file changed +41
-0
lines changed
src/tools/miri/tests/pass/shims Expand file tree Collapse file tree 1 file changed +41
-0
lines changed Original file line number Diff line number Diff line change
1
+ //@only-target-windows: this directly tests windows only random functions
2
+ use core:: ffi:: c_void;
3
+ use core:: mem:: size_of_val;
4
+ use core:: ptr:: null_mut;
5
+
6
+ // Windows API definitions.
7
+ type NTSTATUS = i32 ;
8
+ type BOOLEAN = u8 ;
9
+ const BCRYPT_USE_SYSTEM_PREFERRED_RNG : u32 = 0x00000002 ;
10
+ const BCRYPT_RNG_ALG_HANDLE : * mut c_void = 0x81 as * mut c_void ;
11
+ #[ link( name = "bcrypt" ) ]
12
+ extern "system" {
13
+ fn BCryptGenRandom (
14
+ halgorithm : * mut c_void ,
15
+ pbbuffer : * mut u8 ,
16
+ cbbuffer : u32 ,
17
+ dwflags : u32 ,
18
+ ) -> NTSTATUS ;
19
+ }
20
+ #[ link( name = "advapi32" ) ]
21
+ extern "system" {
22
+ #[ link_name = "SystemFunction036" ]
23
+ fn RtlGenRandom ( RandomBuffer : * mut u8 , RandomBufferLength : u32 ) -> BOOLEAN ;
24
+ }
25
+
26
+ fn main ( ) {
27
+ let mut key = [ 0u8 ; 24 ] ;
28
+ let len: u32 = size_of_val ( & key) . try_into ( ) . unwrap ( ) ;
29
+ let ret = unsafe {
30
+ BCryptGenRandom ( null_mut ( ) , key. as_mut_ptr ( ) , len, BCRYPT_USE_SYSTEM_PREFERRED_RNG )
31
+ } ;
32
+ // NTSTATUS codes use the high bit to indicate an error
33
+ assert ! ( ret >= 0 ) ;
34
+
35
+ let ret = unsafe { BCryptGenRandom ( BCRYPT_RNG_ALG_HANDLE , key. as_mut_ptr ( ) , len, 0 ) } ;
36
+ assert ! ( ret >= 0 ) ;
37
+
38
+ let ret = unsafe { RtlGenRandom ( key. as_mut_ptr ( ) , len) } ;
39
+ // RtlGenRandom returns a BOOLEAN where 0 indicates an error
40
+ assert_ne ! ( ret, 0 ) ;
41
+ }
You can’t perform that action at this time.
0 commit comments