This is an archive of the discontinued LLVM Phabricator instance.

libc++: Add support for arc4random() to random_device.
ClosedPublic

Authored by ed on Mar 7 2015, 12:12 AM.

Details

Summary

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.

Diff Detail

Repository
rL LLVM

Event Timeline

ed updated this revision to Diff 21416.Mar 7 2015, 12:12 AM
ed retitled this revision from to libc++: Add support for arc4random() to random_device..
ed updated this object.
ed edited the test plan for this revision. (Show Details)
ed added reviewers: jfb, majnemer.
ed added a subscriber: Unknown Object (MLST).
ed updated this object.Mar 7 2015, 12:13 AM
ed set the repository for this revision to rL LLVM.Mar 7 2015, 12:22 AM
jfb edited edge metadata.Mar 7 2015, 9:10 AM

arc4random was broken for a short while on FreeBSD: http://lwn.net/Articles/633805/rss
Not much can be done from libc++, but I'd avoid using it for a while on that platform so we can be reasonably certain they're fixed.

Another idea is having arc4random as a fallback on platforms where opening /dev/urandom fails and arc4random is available. The rationale is that the platforms offers a POSIX-y API but the /dev/ access was blocked by a sandbox. I'm not sure I like that approach, but I think it's worth mentioning.

test/std/numerics/rand/rand.device/ctor.pass.cpp
32 ↗(On Diff #21416)

We had a discussion about allowing any string on Windows: I think it's a bad idea. I realize it's implementation dependent, but I'd only allow /dev/urandom on new platforms if you don't offer the semantics of /dev/random.

ed updated this revision to Diff 21480.Mar 9 2015, 5:32 AM
ed edited edge metadata.

Hi JF,

In D8134#136002, @jfb wrote:

arc4random was broken for a short while on FreeBSD: http://lwn.net/Articles/633805/rss
Not much can be done from libc++, but I'd avoid using it for a while on that platform so we can be reasonably certain they're fixed.

Just for the record, as Ed Maste (emaste@FreeBSD.org) reported, arc4random() was only broken in the development track of FreeBSD. There shouldn't be any reason to not use it on supported versions of FreeBSD. For now I'll only enable this on CloudABI (because it's needed to make libc++ build).

I've updated the patch to check for /dev/urandom on all architectures now. Be sure to let me know what you think.

Thanks,
Ed

jfb added a comment.Mar 9 2015, 8:21 PM

The changes LGTM with the Windows implementation fix.

Please update the commit message to indicate that this is an API break on Windows but it's probably inconsequential: any string used to be accepted, and now only the default string in the random_device ctor is accepted.

src/random.cpp
122 ↗(On Diff #21480)

__token?

This revision was automatically updated to reflect the committed changes.