This is an archive of the discontinued LLVM Phabricator instance.

[libcxx] Support getentropy as a source of randomness for std::random_device
ClosedPublic

Authored by phosek on Nov 21 2017, 1:29 PM.

Details

Summary

Use this source use on Fuchsia where this is the officially way
to obtain randomness. This could be also used on other platforms
that already support getentropy such as *BSD or Linux.

Diff Detail

Repository
rL LLVM

Event Timeline

phosek created this revision.Nov 21 2017, 1:29 PM
This revision is now accepted and ready to land.Nov 21 2017, 1:34 PM
This revision was automatically updated to reflect the committed changes.
jwakely added inline comments.
libcxx/trunk/src/random.cpp
29 ↗(On Diff #125076)

musl only declares getentropy in <unistd.h> not <sys/random.h>. Glibc declares it in both. Should <unistd.h> also be included when _LIBCPP_USING_GETENTROPY is defined, so it can work more portably?

jwakely added inline comments.Nov 25 2021, 7:06 AM
libcxx/trunk/src/random.cpp
29 ↗(On Diff #125076)

I suppose it doesn't really matter, since _LIBCPP_USING_GETENTROPY is currently only defined for fuchsia and wasi, which both declare getentropy in <sys/random.h>.

mcgrathr added inline comments.Nov 29 2021, 11:14 AM
libcxx/trunk/src/random.cpp
29 ↗(On Diff #125076)

FWIW, if there is an emerging norm to declare getentropy in <unistd.h> then Fuchsia's libc can add it to our <unistd.h> and there's no real problem relying on that in libc++ sources within about a week of when we land that change in Fuchsia's trunk.

jwakely added inline comments.
libcxx/trunk/src/random.cpp
29 ↗(On Diff #125076)

@fweimer added it to glibc's unistd.h for compatibility with existing BSD code:
https://sourceware.org/bugzilla/show_bug.cgi?id=17252#c9

Maybe @dalias can confirm whether musl did it for the same reason?

Does it make sense to "standardize" on declaring getentropy in unistd.h?

getentropy comes from OpenBSD (among others), and it's in <unistd.h> there: https://man.openbsd.org/getentropy.2