aboutsummaryrefslogtreecommitdiff
path: root/std/os
diff options
context:
space:
mode:
authorAndrew Kelley <superjoe30@gmail.com>2018-08-02 17:29:31 -0400
committerAndrew Kelley <superjoe30@gmail.com>2018-08-02 17:29:31 -0400
commit65140b2fba4e55d713de506f2bed259ca9410cbf (patch)
treee52660c1726caabc730c8869499e5801f027f1b1 /std/os
parent951124e1772c7013c2b1a674cf98a0b638c36262 (diff)
parentfb05b96492f4fb1476106bf735788ac16f69c7ef (diff)
downloadzig-65140b2fba4e55d713de506f2bed259ca9410cbf.tar.gz
zig-65140b2fba4e55d713de506f2bed259ca9410cbf.zip
Merge remote-tracking branch 'origin/master' into async-fs
Diffstat (limited to 'std/os')
-rw-r--r--std/os/index.zig26
-rw-r--r--std/os/windows/advapi32.zig5
-rw-r--r--std/os/windows/util.zig2
3 files changed, 19 insertions, 14 deletions
diff --git a/std/os/index.zig b/std/os/index.zig
index 943e1259b3..0205f3f0ae 100644
--- a/std/os/index.zig
+++ b/std/os/index.zig
@@ -120,16 +120,10 @@ pub fn getRandomBytes(buf: []u8) !void {
try posixRead(fd, buf);
},
Os.windows => {
- var hCryptProv: windows.HCRYPTPROV = undefined;
- if (windows.CryptAcquireContextA(&hCryptProv, null, null, windows.PROV_RSA_FULL, 0) == 0) {
- const err = windows.GetLastError();
- return switch (err) {
- else => unexpectedErrorWindows(err),
- };
- }
- defer _ = windows.CryptReleaseContext(hCryptProv, 0);
-
- if (windows.CryptGenRandom(hCryptProv, @intCast(windows.DWORD, buf.len), buf.ptr) == 0) {
+ // Call RtlGenRandom() instead of CryptGetRandom() on Windows
+ // https://github.com/rust-lang-nursery/rand/issues/111
+ // https://bugzilla.mozilla.org/show_bug.cgi?id=504270
+ if (windows.RtlGenRandom(buf.ptr, buf.len) == 0) {
const err = windows.GetLastError();
return switch (err) {
else => unexpectedErrorWindows(err),
@@ -149,8 +143,14 @@ pub fn getRandomBytes(buf: []u8) !void {
}
test "os.getRandomBytes" {
- var buf: [50]u8 = undefined;
- try getRandomBytes(buf[0..]);
+ var buf_a: [50]u8 = undefined;
+ var buf_b: [50]u8 = undefined;
+ // Call Twice
+ try getRandomBytes(buf_a[0..]);
+ try getRandomBytes(buf_b[0..]);
+
+ // Check if random (not 100% conclusive)
+ assert( !mem.eql(u8, buf_a, buf_b) );
}
/// Raises a signal in the current kernel thread, ending its execution.
@@ -2823,7 +2823,7 @@ pub fn cpuCount(fallback_allocator: *mem.Allocator) CpuCountError!usize {
builtin.Os.macosx => {
var count: c_int = undefined;
var count_len: usize = @sizeOf(c_int);
- const rc = posix.sysctlbyname(c"hw.ncpu", @ptrCast(*c_void, &count), &count_len, null, 0);
+ const rc = posix.sysctlbyname(c"hw.logicalcpu", @ptrCast(*c_void, &count), &count_len, null, 0);
const err = posix.getErrno(rc);
switch (err) {
0 => return @intCast(usize, count),
diff --git a/std/os/windows/advapi32.zig b/std/os/windows/advapi32.zig
index dcb5a636ea..2f3195475c 100644
--- a/std/os/windows/advapi32.zig
+++ b/std/os/windows/advapi32.zig
@@ -28,3 +28,8 @@ pub extern "advapi32" stdcallcc fn RegOpenKeyExW(hKey: HKEY, lpSubKey: LPCWSTR,
pub extern "advapi32" stdcallcc fn RegQueryValueExW(hKey: HKEY, lpValueName: LPCWSTR, lpReserved: LPDWORD,
lpType: LPDWORD, lpData: LPBYTE, lpcbData: LPDWORD,) LSTATUS;
+
+// RtlGenRandom is known as SystemFunction036 under advapi32
+// http://msdn.microsoft.com/en-us/library/windows/desktop/aa387694.aspx */
+pub extern "advapi32" stdcallcc fn SystemFunction036(output: [*]u8, length: usize) BOOL;
+pub const RtlGenRandom = SystemFunction036;
diff --git a/std/os/windows/util.zig b/std/os/windows/util.zig
index dda9ce7a8b..c9d2c3c3e6 100644
--- a/std/os/windows/util.zig
+++ b/std/os/windows/util.zig
@@ -166,7 +166,7 @@ pub fn windowsUnloadDll(hModule: windows.HMODULE) void {
}
test "InvalidDll" {
- if (builtin.os != builtin.Os.windows) return;
+ if (builtin.os != builtin.Os.windows) return error.SkipZigTest;
const DllName = "asdf.dll";
const allocator = std.debug.global_allocator;