public inbox for gcc-bugs@sourceware.org help / color / mirror / Atom feed
From: "f.heckenbach@fh-soft.de" <gcc-bugzilla@gcc.gnu.org> To: gcc-bugs@gcc.gnu.org Subject: [Bug libstdc++/57920] New: [c++11] Linux: std::random_device reads too much from /dev/urandom Date: Wed, 17 Jul 2013 15:33:00 -0000 [thread overview] Message-ID: <bug-57920-4@http.gcc.gnu.org/bugzilla/> (raw) http://gcc.gnu.org/bugzilla/show_bug.cgi?id=57920 Bug ID: 57920 Summary: [c++11] Linux: std::random_device reads too much from /dev/urandom Product: gcc Version: 4.7.2 Status: UNCONFIRMED Severity: normal Priority: P3 Component: libstdc++ Assignee: unassigned at gcc dot gnu.org Reporter: f.heckenbach@fh-soft.de As the test below shows, a single invocation of std::random_device::operator() reads 4k from /dev/urandom, which is rather wasteful of the entropy collected in the random device pool. Of course, in theory, reading 4k and using just 4 bytes of it will only decrease the entropy by 4 bytes, not 4k, but the kernel can't know that. When you read 4k from /dev/urandom, it has to assume it will be used, so it will reduce the entropy by 4k (which is typically all it has). This means that a subsequent read from /dev/random (by the same or another process) will block, often unnecessarily because actually enough entropy was available. This is particularly annoying since std::random_device is often just used to seed a PRNG which needs just a few random bytes. So while buffered reading is almost always a good thing, I contend it's not in this case. I'd suggest to read unbuffered by default, which may entail using read() instead of fread(). % cat test.cpp #include <random> int main () { std::random_device rd; rd (); } % g++-4.7 -std=c++11 test.cpp % strace ./a.out [...] open("/dev/urandom", O_RDONLY) = 3 fstat64(3, {st_mode=S_IFCHR|0666, st_rdev=makedev(1, 9), ...}) = 0 ioctl(3, SNDCTL_TMR_TIMEBASE or TCGETS, 0xbfffec50) = -1 EINVAL (Invalid argument) mmap2(NULL, 4096, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0) = 0xb7fdf000 read(3, [...], 4096) = 4096 close(3) = 0
next reply other threads:[~2013-07-17 15:33 UTC|newest] Thread overview: 10+ messages / expand[flat|nested] mbox.gz Atom feed top 2013-07-17 15:33 f.heckenbach@fh-soft.de [this message] 2013-07-21 19:55 ` [Bug libstdc++/57920] " paolo.carlini at oracle dot com 2013-07-21 19:56 ` paolo.carlini at oracle dot com 2013-07-21 20:40 ` paolo.carlini at oracle dot com 2013-07-21 20:41 ` paolo.carlini at oracle dot com 2013-07-22 12:31 ` f.heckenbach@fh-soft.de 2013-07-22 12:33 ` f.heckenbach@fh-soft.de 2013-07-22 12:36 ` paolo.carlini at oracle dot com 2013-07-22 15:25 ` paolo.carlini at oracle dot com 2014-02-11 21:36 ` gnu at binarywings dot net
Reply instructions: You may reply publicly to this message via plain-text email using any one of the following methods: * Save the following mbox file, import it into your mail client, and reply-to-all from there: mbox Avoid top-posting and favor interleaved quoting: https://en.wikipedia.org/wiki/Posting_style#Interleaved_style * Reply using the --to, --cc, and --in-reply-to switches of git-send-email(1): git send-email \ --in-reply-to=bug-57920-4@http.gcc.gnu.org/bugzilla/ \ --to=gcc-bugzilla@gcc.gnu.org \ --cc=gcc-bugs@gcc.gnu.org \ /path/to/YOUR_REPLY https://kernel.org/pub/software/scm/git/docs/git-send-email.html * If your mail client supports setting the In-Reply-To header via mailto: links, try the mailto: linkBe sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions for how to clone and mirror all data and code used for this inbox; as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).