-
Notifications
You must be signed in to change notification settings - Fork 15.2k
[libc] implement getrandom for windows #119438
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
Closed
Closed
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,4 +6,5 @@ set(TARGET_PUBLIC_HEADERS | |
libc.include.errno | ||
libc.include.fenv | ||
libc.include.math | ||
libc.include.sys_random | ||
) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
//===-- Definition of macros from sys/auxv.h ------------------------------===// | ||
// | ||
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. | ||
// See https://llvm.org/LICENSE.txt for license information. | ||
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception | ||
// | ||
//===----------------------------------------------------------------------===// | ||
|
||
#ifndef LLVM_LIBC_HDR_SYS_RANDOM_MACROS_H | ||
#define LLVM_LIBC_HDR_SYS_RANDOM_MACROS_H | ||
|
||
#if defined(LIBC_FULL_BUILD) || defined(_WIN32) | ||
|
||
#include "include/llvm-libc-macros/sys-random-macros.h" | ||
|
||
#else // Overlay mode | ||
|
||
#include <sys/random.h> | ||
|
||
#endif // LLVM_LIBC_FULL_BUILD | ||
|
||
#endif // LLVM_LIBC_HDR_SYS_RANDOM_MACROS_H |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -9,6 +9,12 @@ | |
#ifndef LLVM_LIBC_TYPES_SSIZE_T_H | ||
#define LLVM_LIBC_TYPES_SSIZE_T_H | ||
|
||
// https://learn.microsoft.com/en-us/windows/win32/winprog/windows-data-types | ||
#if __has_include(<BaseTsd.h>) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
#include <BaseTsd.h> | ||
typedef SSIZE_T ssize_t; | ||
#else | ||
typedef __INT64_TYPE__ ssize_t; | ||
#endif | ||
|
||
#endif // LLVM_LIBC_TYPES_SSIZE_T_H |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
add_entrypoint_object( | ||
getrandom | ||
SRCS | ||
getrandom.cpp | ||
HDRS | ||
../getrandom.h | ||
DEPENDS | ||
# Maybe we should include the following | ||
# but we don't really care the flags on Windows | ||
# libc.hdr.sys_random_macros | ||
libc.hdr.types.ssize_t | ||
libc.src.errno.errno | ||
libc.src.__support.CPP.limits | ||
) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
//===-- Windows implementation of getrandom -------------------------------===// | ||
// | ||
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. | ||
// See https://llvm.org/LICENSE.txt for license information. | ||
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception | ||
// | ||
//===----------------------------------------------------------------------===// | ||
|
||
#include "src/sys/random/getrandom.h" | ||
#include "src/__support/CPP/bit.h" | ||
#include "src/__support/CPP/limits.h" | ||
#include "src/__support/common.h" | ||
#include "src/__support/macros/config.h" | ||
#include "src/__support/macros/optimization.h" | ||
#include "src/errno/libc_errno.h" | ||
|
||
#define WIN32_LEAN_AND_MEAN | ||
#define NOMINMAX | ||
#include <Windows.h> | ||
#include <bcrypt.h> | ||
#include <ntstatus.h> | ||
#pragma comment(lib, "bcrypt.lib") | ||
|
||
namespace LIBC_NAMESPACE_DECL { | ||
|
||
LLVM_LIBC_FUNCTION(ssize_t, getrandom, | ||
(void *buf, size_t buflen, | ||
[[maybe_unused]] unsigned int flags)) { | ||
// https://learn.microsoft.com/en-us/windows/win32/api/bcrypt/nf-bcrypt-bcryptgenrandom | ||
// BCRYPT_USE_SYSTEM_PREFERRED_RNG | ||
// Use the system-preferred random number generator algorithm. The hAlgorithm | ||
// parameter must be NULL. | ||
|
||
// flags are ignored as Windows does not distinguish between urandom/random. | ||
// size_t is larger than ULONG. Linux API allows getrandom to return fewer | ||
// bytes than required. Hence, we trancate the size_t to ULONG. If user really | ||
// needs huge amount of bytes (which is highly unlikely), they can call | ||
// getrandom multiple times in a loop. This is also the common pattern in | ||
// Linux. | ||
|
||
// https://learn.microsoft.com/en-us/windows-hardware/drivers/gettingstarted/virtual-address-spaces | ||
// A 64-bit process on 64-bit Windows has a virtual address space within the | ||
// 128-terabyte range 0x000'00000000 through 0x7FFF'FFFFFFFF. | ||
if (buf == nullptr || cpp::bit_cast<INT_PTR>(buf) < 0) { | ||
libc_errno = EFAULT; | ||
return -1; | ||
} | ||
|
||
constexpr size_t PARAM_LIMIT = | ||
static_cast<size_t>(cpp::numeric_limits<ULONG>::max()); | ||
constexpr size_t RETURN_LIMIT = | ||
static_cast<size_t>(cpp::numeric_limits<ssize_t>::max()); | ||
buflen = buflen > PARAM_LIMIT ? PARAM_LIMIT : buflen; | ||
buflen = buflen > RETURN_LIMIT ? RETURN_LIMIT : buflen; | ||
NTSTATUS result = ::BCryptGenRandom(nullptr, static_cast<PUCHAR>(buf), | ||
static_cast<ULONG>(buflen), | ||
BCRYPT_USE_SYSTEM_PREFERRED_RNG); | ||
|
||
// not possible to overflow as we have truncated the limit. | ||
if (LIBC_LIKELY(result == STATUS_SUCCESS)) | ||
return static_cast<ssize_t>(buflen); | ||
|
||
libc_errno = EINVAL; | ||
return -1; | ||
} | ||
|
||
} // namespace LIBC_NAMESPACE_DECL |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,15 @@ | ||
if(EXISTS ${CMAKE_CURRENT_SOURCE_DIR}/${LIBC_TARGET_OS}) | ||
add_subdirectory(${LIBC_TARGET_OS}) | ||
endif() | ||
add_custom_target(libc_sys_random_unittests) | ||
|
||
add_libc_unittest( | ||
getrandom_test | ||
SUITE | ||
libc_sys_random_unittests | ||
SRCS | ||
getrandom_test.cpp | ||
DEPENDS | ||
libc.hdr.sys_random_macros | ||
libc.src.errno.errno | ||
libc.src.math.fabs | ||
libc.src.sys.random.getrandom | ||
libc.test.UnitTest.ErrnoSetterMatcher | ||
) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this is important, but should probably be in a separate patch.