
Nuxi CloudABI (https://github.com/NuxiNL/cloudlibc) does not allow processes to access the global filesystem namespace. This breaks random_device, as it attempts to use /dev/{u,}random. This change adds support for arc4random(), which is present on CloudABI. In my opinion it would also make sense to use arc4random() on other operating systems, such as *BSD and Mac OS X, but I'd rather leave that to the maintainers of the respective platforms. Switching to arc4random() does change the ABI. This change also attempts to make some cleanups to the code. It adds a single #define for every random interface, instead of testing against operating systems explicitly. As discussed, also validate the token argument to be equal to "/dev/urandom" on all systems that only provide pseudo-random numbers. This should cause little to no breakage, as "/dev/urandom" is also the default argument value. Reviewed by: jfb Differential Revision: http://reviews.llvm.org/D8134 git-svn-id: https://llvm.org/svn/llvm-project/libcxx/trunk@231764 91177308-0d34-0410-b5e6-96231b3b80d8
84 lines
2.0 KiB
C++
84 lines
2.0 KiB
C++
//===----------------------------------------------------------------------===//
|
|
//
|
|
// The LLVM Compiler Infrastructure
|
|
//
|
|
// This file is dual licensed under the MIT and the University of Illinois Open
|
|
// Source Licenses. See LICENSE.TXT for details.
|
|
//
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// <random>
|
|
|
|
// class random_device;
|
|
|
|
// explicit random_device(const string& token = implementation-defined);
|
|
|
|
// For the following ctors, the standard states: "The semantics and default
|
|
// value of the token parameter are implementation-defined". Implementations
|
|
// therefore aren't required to accept any string, but the default shouldn't
|
|
// throw.
|
|
|
|
#include <random>
|
|
#include <cassert>
|
|
#include <unistd.h>
|
|
|
|
bool is_valid_random_device(const std::string &token) {
|
|
#if defined(_LIBCPP_USING_DEV_RANDOM)
|
|
// Not an exhaustive list: they're the only tokens that are tested below.
|
|
return token == "/dev/urandom" || token == "/dev/random";
|
|
#else
|
|
return token == "/dev/urandom";
|
|
#endif
|
|
}
|
|
|
|
void check_random_device_valid(const std::string &token) {
|
|
std::random_device r(token);
|
|
}
|
|
|
|
void check_random_device_invalid(const std::string &token) {
|
|
try {
|
|
std::random_device r(token);
|
|
assert(false);
|
|
} catch (const std::system_error &e) {
|
|
}
|
|
}
|
|
|
|
int main() {
|
|
{ std::random_device r; }
|
|
|
|
{
|
|
int ec;
|
|
ec = close(STDIN_FILENO);
|
|
assert(!ec);
|
|
ec = close(STDOUT_FILENO);
|
|
assert(!ec);
|
|
ec = close(STDERR_FILENO);
|
|
assert(!ec);
|
|
std::random_device r;
|
|
}
|
|
|
|
{
|
|
std::string token = "wrong file";
|
|
if (is_valid_random_device(token))
|
|
check_random_device_valid(token);
|
|
else
|
|
check_random_device_invalid(token);
|
|
}
|
|
|
|
{
|
|
std::string token = "/dev/urandom";
|
|
if (is_valid_random_device(token))
|
|
check_random_device_valid(token);
|
|
else
|
|
check_random_device_invalid(token);
|
|
}
|
|
|
|
{
|
|
std::string token = "/dev/random";
|
|
if (is_valid_random_device(token))
|
|
check_random_device_valid(token);
|
|
else
|
|
check_random_device_invalid(token);
|
|
}
|
|
}
|