Skip to content

Loop on Win32 to satisfy large random byte requests (as is done on Linux) #62364

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Dec 5, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 12 additions & 9 deletions stdlib/public/stubs/Random.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@

#if defined(_WIN32) && !defined(__CYGWIN__)
#define WIN32_LEAN_AND_MEAN
#define NOMINMAX
#include <Windows.h>
#include <Bcrypt.h>
#pragma comment(lib, "bcrypt.lib")
Expand Down Expand Up @@ -65,16 +66,18 @@ void swift_stdlib_random(void *buf, __swift_size_t nbytes) {

SWIFT_RUNTIME_STDLIB_API
void swift_stdlib_random(void *buf, __swift_size_t nbytes) {
if (nbytes > ULONG_MAX) {
fatalError(0, "Fatal error: %zd exceeds ULONG_MAX\n", nbytes);
}
while (nbytes > 0) {
auto actual_nbytes = std::min(nbytes, (__swift_size_t)ULONG_MAX);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It would be nice to use static_cast as this is C++.

Suggested change
auto actual_nbytes = std::min(nbytes, (__swift_size_t)ULONG_MAX);
auto actual_nbytes = std::min(nbytes, static_cast<__swift_size_t>(ULONG_MAX));

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@compnerd Can the #warning on line 64 be removed?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good question. AFAIK the unit tests for random number generation are not conditionalized by platform, are they?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

swift_stdlib_random is used in validation-test/stdlib/Random.swift, on all platforms AFAIK.

The #warning was originally an #error, because swift_stdlib_random was written without access to a compiler for Windows.

NTSTATUS status = BCryptGenRandom(nullptr,
static_cast<PUCHAR>(buf),
static_cast<ULONG>(actual_nbytes),
BCRYPT_USE_SYSTEM_PREFERRED_RNG);
if (!BCRYPT_SUCCESS(status)) {
fatalError(0, "Fatal error: 0x%lX in '%s'\n", status, __func__);
}

NTSTATUS status = BCryptGenRandom(nullptr,
static_cast<PUCHAR>(buf),
static_cast<ULONG>(nbytes),
BCRYPT_USE_SYSTEM_PREFERRED_RNG);
if (!BCRYPT_SUCCESS(status)) {
fatalError(0, "Fatal error: 0x%lX in '%s'\n", status, __func__);
buf = static_cast<uint8_t *>(buf) + actual_nbytes;
nbytes -= actual_nbytes;
}
}

Expand Down