Add support for arc4random() to random_device.

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
This commit is contained in:
Ed Schouten
2015-03-10 07:46:06 +00:00
parent 9f4f2215b7
commit 63e70b67eb
4 changed files with 86 additions and 53 deletions

View File

@@ -23,14 +23,12 @@
#include <unistd.h>
bool is_valid_random_device(const std::string &token) {
#if defined(_WIN32)
return true;
#elif defined(_LIBCPP_USING_NACL_RANDOM)
return token == "/dev/urandom";
#else // !defined(_WIN32) && !defined(_LIBCPP_USING_NACL_RANDOM)
#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";
#endif // defined(_WIN32) || defined(_LIBCPP_USING_NACL_RANDOM)
#else
return token == "/dev/urandom";
#endif
}
void check_random_device_valid(const std::string &token) {