Bug 67578

Summary: std::random_device::entropy() always returns 0
Product: gcc Reporter: Jonathan Wakely <redi>
Component: libstdc++Assignee: Not yet assigned to anyone <unassigned>
Status: RESOLVED FIXED    
Severity: normal CC: daniel.kruegler, webrown.cpp
Priority: P3    
Version: 5.2.1   
Target Milestone: 8.0   
Host: Target:
Build: Known to work:
Known to fail: Last reconfirmed: 2017-03-13 00:00:00
Attachments: patch

Description Jonathan Wakely 2015-09-14 17:25:13 UTC
When using /dev/random or /dev/urandom on GNU/Linux we can get the bits of available entropy using the RNDGETENTCNT ioctl.
Comment 1 Xi Ruoyao 2017-03-12 13:30:52 UTC
Created attachment 40951 [details]
patch

Is this OK?

Or maybe we should keep the ABI unchanged and put all the implementation
in the header random.h?
Comment 2 Xi Ruoyao 2017-03-13 14:07:51 UTC
(In reply to Xi Ruoyao from comment #1)

> Or maybe we should keep the ABI unchanged and put all the implementation
> in the header random.h?

No.  It would then pollute the global namespace with sys/ioctl.h and
linux/random.h.  Worse than an ABI minor ("acceptable" in libstdc++ ABI doc)
change.
Comment 3 Jonathan Wakely 2017-03-13 16:22:24 UTC
Right, it's better to add it to the DSO as your first patch does. This will have to wait for gcc8 though. In the meantime I'll email you separately about completing a copyright assignment for GCC.
Comment 4 Jonathan Wakely 2017-05-23 16:12:29 UTC
Author: redi
Date: Tue May 23 16:11:57 2017
New Revision: 248374

URL: https://gcc.gnu.org/viewcvs?rev=248374&root=gcc&view=rev
Log:
PR libstdc++/67578 Implement non-trivial std::random_device::entropy

2017-05-23  Xi Ruoyao  <ryxi@stu.xidian.edu.cn>
	    Jonathan Wakely  <jwakely@redhat.com>

	PR libstdc++/67578
	* acinclude.m4: Bump libtool_VERSION.
	* config/abi/pre/gnu.ver: Create GLIBCXX_3.4.24 with new symbol.
	* config.h.in: Regenerate.
	* configure: Regenerate.
	* configure.ac: Add test for <linux/random.h>.
	* doc/xml/manual/abi.xml: Document new library version.
	* include/bits/random.h (random_device::entropy)
	[_GLIBCXX_USE_RANDOM_TR1]: Add call to new _M_getentropy member.
	(random_device::_M_getentropy): Declare.
	* src/c++11/random.cc (random_device::_M_getentropy): Define.
	* testsuite/util/testsuite_abi.cc: Add GLIBCXX_3.4.24 to known
	versions, and make it the latest version.

Modified:
    trunk/libstdc++-v3/ChangeLog
    trunk/libstdc++-v3/acinclude.m4
    trunk/libstdc++-v3/config.h.in
    trunk/libstdc++-v3/config/abi/pre/gnu.ver
    trunk/libstdc++-v3/configure
    trunk/libstdc++-v3/configure.ac
    trunk/libstdc++-v3/doc/xml/manual/abi.xml
    trunk/libstdc++-v3/include/bits/random.h
    trunk/libstdc++-v3/src/c++11/random.cc
    trunk/libstdc++-v3/testsuite/util/testsuite_abi.cc
Comment 5 Jonathan Wakely 2017-05-23 16:34:58 UTC
Implemented for GCC 8.
Comment 6 Jonathan Wakely 2017-05-23 16:37:31 UTC
N.B. we still return zero for the default random_device if using the RDRAND instruction. I don't know what we could do there, except maybe trust Intel and return the maximum value.