public inbox for gcc-patches@gcc.gnu.org
 help / color / mirror / Atom feed
* [PATCH] Implement non-trivial std::random_device::entropy (PR libstdc++/67578)
@ 2017-05-18 12:15 Xi Ruoyao
  2017-05-22 13:21 ` Jonathan Wakely
  0 siblings, 1 reply; 9+ messages in thread
From: Xi Ruoyao @ 2017-05-18 12:15 UTC (permalink / raw)
  To: gcc-patches, libstdc++; +Cc: ryxi

This patch use ioctl to get entropy of std::random_device using
/dev/random and /dev/urandom.

Regenerated files are mentioned in ChangeLog but not included
in the patch.

2017-03-12  Xi Ruoyao  <ryxi@stu.xidian.edu.cn>

	PR libstdc++/67578
	* config.h.in: Regenerate.
	* configure: Regenerate.
	* configure.ac: Check for linux/random.h.
	* config/abi/pre/gnu.ver: Add a new symbol.
	* include/bits/random.h (random_device::_M_getentropy):
	  Declare new function to get entropy using ioctl.
	  (random_device::entropy): Call it.
	* src/c++11/random.cc (random_device::_M_getentropy):
	  Implement new function.
	* config/abi/post/x86_64-linux-gnu/baseline_symbols.txt:
	  Regenerate.
	* config/abi/post/i386-linux-gnu/baseline_symbols.txt:
	  Regenerate.
	* config/abi/post/i486-linux-gnu/baseline_symbols.txt:
	  Regenerate.
---
 libstdc++-v3/config/abi/pre/gnu.ver                |  3 ++
 libstdc++-v3/configure.ac                          |  2 +-
 libstdc++-v3/include/bits/random.h                 |  9 +++++-
 libstdc++-v3/src/c++11/random.cc                   | 36 ++++++++++++++++++++++
 9 files changed, 55 insertions(+), 3 deletions(-)

diff --git a/libstdc++-v3/config/abi/pre/gnu.ver b/libstdc++-v3/config/abi/pre/gnu.ver
index 3e6e70b..39ad330 100644
--- a/libstdc++-v3/config/abi/pre/gnu.ver
+++ b/libstdc++-v3/config/abi/pre/gnu.ver
@@ -1963,6 +1963,9 @@ GLIBCXX_3.4.23 {
     _ZNSt13__future_base13_State_baseV211_Make_ready6_M_setEv;
 #endif
 
+    # std::random_device::_M_getentropy() const
+    _ZNKSt13random_device13_M_getentropyEv;
+
 } GLIBCXX_3.4.22;
 
 # Symbols in the support library (libsupc++) have their own tag.
diff --git a/libstdc++-v3/configure.ac b/libstdc++-v3/configure.ac
index 8e97350..270dcba 100644
--- a/libstdc++-v3/configure.ac
+++ b/libstdc++-v3/configure.ac
@@ -237,7 +237,7 @@ AC_CHECK_HEADERS([endian.h execinfo.h float.h fp.h ieeefp.h inttypes.h \
 locale.h machine/endian.h machine/param.h nan.h stdint.h stdlib.h string.h \
 strings.h sys/ipc.h sys/isa_defs.h sys/machine.h sys/param.h \
 sys/resource.h sys/sem.h sys/stat.h sys/time.h sys/types.h unistd.h \
-wchar.h wctype.h])
+wchar.h wctype.h linux/random.h])
 
 # Only do link tests if native. Else, hardcode.
 if $GLIBCXX_IS_NATIVE; then
diff --git a/libstdc++-v3/include/bits/random.h b/libstdc++-v3/include/bits/random.h
index d39cc3e..bb761ec 100644
--- a/libstdc++-v3/include/bits/random.h
+++ b/libstdc++-v3/include/bits/random.h
@@ -1603,7 +1603,13 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
 
     double
     entropy() const noexcept
-    { return 0.0; }
+    {
+#ifdef _GLIBCXX_USE_RANDOM_TR1
+      return this->_M_getentropy();
+#else
+      return 0.0;
+#endif
+    }
 
     result_type
     operator()()
@@ -1627,6 +1633,7 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
 
     result_type _M_getval();
     result_type _M_getval_pretr1();
+    double _M_getentropy() const noexcept;
 
     union
     {
diff --git a/libstdc++-v3/src/c++11/random.cc b/libstdc++-v3/src/c++11/random.cc
index df79874..414ddc3 100644
--- a/libstdc++-v3/src/c++11/random.cc
+++ b/libstdc++-v3/src/c++11/random.cc
@@ -38,6 +38,14 @@
 # include <unistd.h>
 #endif
 
+#ifdef _GLIBCXX_HAVE_SYS_IOCTL_H
+# include <sys/ioctl.h>
+#endif
+
+#ifdef _GLIBCXX_HAVE_LINUX_RANDOM_H
+# include <linux/random.h>
+#endif
+
 namespace std _GLIBCXX_VISIBILITY(default)
 {
   namespace
@@ -161,6 +169,34 @@ namespace std _GLIBCXX_VISIBILITY(default)
     return _M_mt();
   }
 
+  double
+  random_device::_M_getentropy() const noexcept
+  {
+#if defined _GLIBCXX_HAVE_SYS_IOCTL_H && \
+    defined _GLIBCXX_HAVE_LINUX_RANDOM_H
+    if (!_M_file)
+      return 0.0;
+
+    int fd = fileno(static_cast<FILE *>(_M_file));
+    if (fd < 0)
+      return 0.0;
+
+    int ent;
+    if (ioctl(fd, RNDGETENTCNT, &ent) < 0)
+      return 0.0;
+
+    if (ent < 0)
+      return 0.0;
+
+    if (static_cast<unsigned>(ent) > sizeof(result_type) * 8)
+      return static_cast<double>(sizeof(result_type) * 8);
+
+    return static_cast<double>(ent);
+#else
+    return 0.0;
+#endif
+  }
+
   template class mersenne_twister_engine<
     uint_fast32_t,
     32, 624, 397, 31,
-- 
2.7.1

-- 
Xi Ruoyao <ryxi@stu.xidian.edu.cn>
School of Aerospace Science and Technology, Xidian University

^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: [PATCH] Implement non-trivial std::random_device::entropy (PR libstdc++/67578)
  2017-05-18 12:15 [PATCH] Implement non-trivial std::random_device::entropy (PR libstdc++/67578) Xi Ruoyao
@ 2017-05-22 13:21 ` Jonathan Wakely
  2017-05-22 13:50   ` Xi Ruoyao
  0 siblings, 1 reply; 9+ messages in thread
From: Jonathan Wakely @ 2017-05-22 13:21 UTC (permalink / raw)
  To: Xi Ruoyao; +Cc: gcc-patches, libstdc++

[-- Attachment #1: Type: text/plain, Size: 1925 bytes --]

On 18/05/17 19:27 +0800, Xi Ruoyao wrote:
>This patch use ioctl to get entropy of std::random_device using
>/dev/random and /dev/urandom.

This is a nice addition, thanks.

N.B. I couldn't apply your patch, several lines had U+00A0 (i.e.
NO-BREAK SPACE) characters where normal spaces should be. I don't know
if this is something your mail client did, but maybe attaching the
patch as a multipart message rather than including it inline in the
body will prevent it in future.

>diff --git a/libstdc++-v3/config/abi/pre/gnu.ver b/libstdc++-v3/config/abi/pre/gnu.ver
>index 3e6e70b..39ad330 100644
>--- a/libstdc++-v3/config/abi/pre/gnu.ver
>+++ b/libstdc++-v3/config/abi/pre/gnu.ver
>@@ -1963,6 +1963,9 @@ GLIBCXX_3.4.23 {
>     _ZNSt13__future_base13_State_baseV211_Make_ready6_M_setEv;
> #endif
> 
>+    # std::random_device::_M_getentropy() const
>+    _ZNKSt13random_device13_M_getentropyEv;
>+
> } GLIBCXX_3.4.22;

The 3.4.23 version is now "closed" because it was used for the GCC 7.1
release, and so we can't add new symbols to it. That means we need to
bump the library version and create a new GLIBCXX_3.4.24 version node.
The required steps are documented in doc/xml/manual/build_hacking.xml
(and done by the attached patch which I'm testing).

>@@ -161,6 +169,34 @@ namespace std _GLIBCXX_VISIBILITY(default)
>     return _M_mt();
>   }
> 
>+  double
>+  random_device::_M_getentropy() const noexcept
>+  {
>+#if defined _GLIBCXX_HAVE_SYS_IOCTL_H && \
>+    defined _GLIBCXX_HAVE_LINUX_RANDOM_H

Is RNDGETENTCNT guaranteed to be defined by all versions of
<linux/random.h>?

Would it be better to check for it explicitly? i.e.

#if defined _GLIBCXX_HAVE_SYS_IOCTL_H && defined RNDGETENTCNT


It would also be nice to have a test for this functionality, but since
the available entropy might be zero sometimes we can't guarantee that
a test for a non-zero value will always pass.


[-- Attachment #2: patch.txt --]
[-- Type: text/x-patch, Size: 5948 bytes --]

commit 2e6f7939b6ab2ac0e25af6fc57085bcef8381d04
Author: Jonathan Wakely <jwakely@redhat.com>
Date:   Mon May 22 13:36:53 2017 +0100

    PR libstdc++/67578 Implement non-trivial std::random_device::entropy
    
    2017-05-22  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: Add GLIBCXX_3.4.23 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.

diff --git a/libstdc++-v3/acinclude.m4 b/libstdc++-v3/acinclude.m4
index 61c8cb2..baeea67 100644
--- a/libstdc++-v3/acinclude.m4
+++ b/libstdc++-v3/acinclude.m4
@@ -3750,7 +3750,7 @@ changequote([,])dnl
 fi
 
 # For libtool versioning info, format is CURRENT:REVISION:AGE
-libtool_VERSION=6:23:0
+libtool_VERSION=6:24:0
 
 # Everything parsed; figure out what files and settings to use.
 case $enable_symvers in
diff --git a/libstdc++-v3/config/abi/pre/gnu.ver b/libstdc++-v3/config/abi/pre/gnu.ver
index 268fb94..48e2ec8 100644
--- a/libstdc++-v3/config/abi/pre/gnu.ver
+++ b/libstdc++-v3/config/abi/pre/gnu.ver
@@ -1967,6 +1967,13 @@ GLIBCXX_3.4.23 {
 
 } GLIBCXX_3.4.22;
 
+GLIBCXX_3.4.24 {
+
+    # std::random_device::_M_getentropy() const
+    _ZNKSt13random_device13_M_getentropyEv;
+
+} GLIBCXX_3.4.23;
+
 # Symbols in the support library (libsupc++) have their own tag.
 CXXABI_1.3 {
 
diff --git a/libstdc++-v3/configure.ac b/libstdc++-v3/configure.ac
index 8e97350..270dcba 100644
--- a/libstdc++-v3/configure.ac
+++ b/libstdc++-v3/configure.ac
@@ -237,7 +237,7 @@ AC_CHECK_HEADERS([endian.h execinfo.h float.h fp.h ieeefp.h inttypes.h \
 locale.h machine/endian.h machine/param.h nan.h stdint.h stdlib.h string.h \
 strings.h sys/ipc.h sys/isa_defs.h sys/machine.h sys/param.h \
 sys/resource.h sys/sem.h sys/stat.h sys/time.h sys/types.h unistd.h \
-wchar.h wctype.h])
+wchar.h wctype.h linux/random.h])
 
 # Only do link tests if native. Else, hardcode.
 if $GLIBCXX_IS_NATIVE; then
diff --git a/libstdc++-v3/doc/xml/manual/abi.xml b/libstdc++-v3/doc/xml/manual/abi.xml
index d086d9e..de8e66c 100644
--- a/libstdc++-v3/doc/xml/manual/abi.xml
+++ b/libstdc++-v3/doc/xml/manual/abi.xml
@@ -266,6 +266,7 @@ compatible.
     <listitem><para>GCC 5.1.0: libstdc++.so.6.0.21</para></listitem>
     <listitem><para>GCC 6.1.0: libstdc++.so.6.0.22</para></listitem>
     <listitem><para>GCC 7.1.0: libstdc++.so.6.0.23</para></listitem>
+    <listitem><para>GCC 8.0.0: libstdc++.so.6.0.24</para></listitem>
     </itemizedlist>
     <para>
       Note 1: Error should be libstdc++.so.3.0.3.
@@ -334,6 +335,7 @@ compatible.
     <listitem><para>GCC 5.1.0: GLIBCXX_3.4.21, CXXABI_1.3.9</para></listitem>
     <listitem><para>GCC 6.1.0: GLIBCXX_3.4.22, CXXABI_1.3.10</para></listitem>
     <listitem><para>GCC 7.1.0: GLIBCXX_3.4.23, CXXABI_1.3.11</para></listitem>
+    <listitem><para>GCC 8.0.0: GLIBCXX_3.4.24, CXXABI_1.3.10</para></listitem>
     </itemizedlist>
     </listitem>
 
diff --git a/libstdc++-v3/include/bits/random.h b/libstdc++-v3/include/bits/random.h
index d39cc3e..bb761ec 100644
--- a/libstdc++-v3/include/bits/random.h
+++ b/libstdc++-v3/include/bits/random.h
@@ -1603,7 +1603,13 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
 
     double
     entropy() const noexcept
-    { return 0.0; }
+    {
+#ifdef _GLIBCXX_USE_RANDOM_TR1
+      return this->_M_getentropy();
+#else
+      return 0.0;
+#endif
+    }
 
     result_type
     operator()()
@@ -1627,6 +1633,7 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
 
     result_type _M_getval();
     result_type _M_getval_pretr1();
+    double _M_getentropy() const noexcept;
 
     union
     {
diff --git a/libstdc++-v3/src/c++11/random.cc b/libstdc++-v3/src/c++11/random.cc
index df79874..5011cf2 100644
--- a/libstdc++-v3/src/c++11/random.cc
+++ b/libstdc++-v3/src/c++11/random.cc
@@ -38,6 +38,14 @@
 # include <unistd.h>
 #endif
 
+#ifdef _GLIBCXX_HAVE_SYS_IOCTL_H
+# include <sys/ioctl.h>
+#endif
+
+#ifdef _GLIBCXX_HAVE_LINUX_RANDOM_H
+# include <linux/random.h>
+#endif
+
 namespace std _GLIBCXX_VISIBILITY(default)
 {
   namespace
@@ -161,6 +169,33 @@ namespace std _GLIBCXX_VISIBILITY(default)
     return _M_mt();
   }
 
+  double
+  random_device::_M_getentropy() const noexcept
+  {
+#if defined _GLIBCXX_HAVE_SYS_IOCTL_H && defined RNDGETENTCNT
+    if (!_M_file)
+      return 0.0;
+
+    const int fd = fileno(static_cast<FILE*>(_M_file));
+    if (fd < 0)
+      return 0.0;
+
+    int ent;
+    if (ioctl(fd, RNDGETENTCNT, &ent) < 0)
+      return 0.0;
+
+    if (ent < 0)
+      return 0.0;
+
+    if (static_cast<unsigned>(ent) > sizeof(result_type) * 8)
+      return static_cast<double>(sizeof(result_type) * 8);
+
+    return static_cast<double>(ent);
+#else
+    return 0.0;
+#endif
+  }
+
   template class mersenne_twister_engine<
     uint_fast32_t,
     32, 624, 397, 31,
diff --git a/libstdc++-v3/testsuite/util/testsuite_abi.cc b/libstdc++-v3/testsuite/util/testsuite_abi.cc
index f5fc594..953c907 100644
--- a/libstdc++-v3/testsuite/util/testsuite_abi.cc
+++ b/libstdc++-v3/testsuite/util/testsuite_abi.cc
@@ -204,6 +204,7 @@ check_version(symbol& test, bool added)
       known_versions.push_back("GLIBCXX_LDBL_3.4.21");
       known_versions.push_back("GLIBCXX_3.4.22");
       known_versions.push_back("GLIBCXX_3.4.23");
+      known_versions.push_back("GLIBCXX_3.4.24");
       known_versions.push_back("CXXABI_1.3");
       known_versions.push_back("CXXABI_LDBL_1.3");
       known_versions.push_back("CXXABI_1.3.1");

^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: [PATCH] Implement non-trivial std::random_device::entropy (PR libstdc++/67578)
  2017-05-22 13:21 ` Jonathan Wakely
@ 2017-05-22 13:50   ` Xi Ruoyao
  2017-05-22 14:10     ` Jonathan Wakely
  0 siblings, 1 reply; 9+ messages in thread
From: Xi Ruoyao @ 2017-05-22 13:50 UTC (permalink / raw)
  To: Jonathan Wakely; +Cc: ryxi, gcc-patches, libstdc++

On 2017-05-22 14:00 +0100, Jonathan Wakely wrote:
> On 18/05/17 19:27 +0800, Xi Ruoyao wrote:
> > This patch use ioctl to get entropy of std::random_device using
> > /dev/random and /dev/urandom.
> 
> This is a nice addition, thanks.
> 
> N.B. I couldn't apply your patch, several lines had U+00A0 (i.e.
> NO-BREAK SPACE) characters where normal spaces should be. I don't know
> if this is something your mail client did, but maybe attaching the
> patch as a multipart message rather than including it inline in the
> body will prevent it in future.

Oh no...  Seems my email client destroyed my patch. I'll attach the
patch next time.  Please wait for PATCH v2.

> > diff --git a/libstdc++-v3/config/abi/pre/gnu.ver b/libstdc++-v3/config/abi/pre/gnu.ver
> > index 3e6e70b..39ad330 100644
> > --- a/libstdc++-v3/config/abi/pre/gnu.ver
> > +++ b/libstdc++-v3/config/abi/pre/gnu.ver
> > @@ -1963,6 +1963,9 @@ GLIBCXX_3.4.23 {
> >      _ZNSt13__future_base13_State_baseV211_Make_ready6_M_setEv;
> >  #endif
> >  
> > +    # std::random_device::_M_getentropy() const
> > +    _ZNKSt13random_device13_M_getentropyEv;
> > +
> >  } GLIBCXX_3.4.22;
> 
> The 3.4.23 version is now "closed" because it was used for the GCC 7.1
> release, and so we can't add new symbols to it. That means we need to
> bump the library version and create a new GLIBCXX_3.4.24 version node.
> The required steps are documented in doc/xml/manual/build_hacking.xml
> (and done by the attached patch which I'm testing).

I'll do it.  Glad to add the first symbol in 3.4.24.

> > @@ -161,6 +169,34 @@ namespace std _GLIBCXX_VISIBILITY(default)
> >      return _M_mt();
> >    }
> >  
> > +  double
> > +  random_device::_M_getentropy() const noexcept
> > +  {
> > +#if defined _GLIBCXX_HAVE_SYS_IOCTL_H && \
> > +    defined _GLIBCXX_HAVE_LINUX_RANDOM_H
> 
> Is RNDGETENTCNT guaranteed to be defined by all versions of
> <linux/random.h>?
> 
> Would it be better to check for it explicitly? i.e.
> 
> #if defined _GLIBCXX_HAVE_SYS_IOCTL_H && defined RNDGETENTCNT

It has been there since the git repo of Linux kernel was created.
I have to check the ancient history of kernel code to know.

> 
> It would also be nice to have a test for this functionality, but since
> the available entropy might be zero sometimes we can't guarantee that
> a test for a non-zero value will always pass.

I've thought about it, but I couldn't find out how to do the test.
-- 
Xi Ruoyao <ryxi@stu.xidian.edu.cn>
School of Aerospace Science and Technology, Xidian University

^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: [PATCH] Implement non-trivial std::random_device::entropy (PR libstdc++/67578)
  2017-05-22 13:50   ` Xi Ruoyao
@ 2017-05-22 14:10     ` Jonathan Wakely
  2017-05-22 14:19       ` Xi Ruoyao
  2017-05-22 14:30       ` [PATCH v2] " Xi Ruoyao
  0 siblings, 2 replies; 9+ messages in thread
From: Jonathan Wakely @ 2017-05-22 14:10 UTC (permalink / raw)
  To: Xi Ruoyao; +Cc: gcc-patches, libstdc++

[-- Attachment #1: Type: text/plain, Size: 832 bytes --]

On 22/05/17 21:41 +0800, Xi Ruoyao wrote:
>On 2017-05-22 14:00 +0100, Jonathan Wakely wrote:
>> On 18/05/17 19:27 +0800, Xi Ruoyao wrote:
>> > This patch use ioctl to get entropy of std::random_device using
>> > /dev/random and /dev/urandom.
>>
>> This is a nice addition, thanks.
>>
>> N.B. I couldn't apply your patch, several lines had U+00A0 (i.e.
>> NO-BREAK SPACE) characters where normal spaces should be. I don't know
>> if this is something your mail client did, but maybe attaching the
>> patch as a multipart message rather than including it inline in the
>> body will prevent it in future.
>
>Oh no...  Seems my email client destroyed my patch. I'll attach the
>patch next time.  Please wait for PATCH v2.

OK, will do. I missed part of the required changes when adding a new
symbol version, see the attached patch.



[-- Attachment #2: patch.txt --]
[-- Type: text/x-patch, Size: 693 bytes --]

diff --git a/libstdc++-v3/testsuite/util/testsuite_abi.cc b/libstdc++-v3/testsuite/util/testsuite_abi.cc
index 953c907..ee7572e 100644
--- a/libstdc++-v3/testsuite/util/testsuite_abi.cc
+++ b/libstdc++-v3/testsuite/util/testsuite_abi.cc
@@ -235,7 +235,7 @@ check_version(symbol& test, bool added)
 	test.version_status = symbol::incompatible;
 
       // Check that added symbols are added in the latest pre-release version.
-      bool latestp = (test.version_name == "GLIBCXX_3.4.23"
+      bool latestp = (test.version_name == "GLIBCXX_3.4.24"
 		     || test.version_name == "CXXABI_1.3.11"
 		     || test.version_name == "CXXABI_FLOAT128"
 		     || test.version_name == "CXXABI_TM_1");

^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: [PATCH] Implement non-trivial std::random_device::entropy (PR libstdc++/67578)
  2017-05-22 14:10     ` Jonathan Wakely
@ 2017-05-22 14:19       ` Xi Ruoyao
  2017-05-23 12:11         ` Jonathan Wakely
  2017-05-22 14:30       ` [PATCH v2] " Xi Ruoyao
  1 sibling, 1 reply; 9+ messages in thread
From: Xi Ruoyao @ 2017-05-22 14:19 UTC (permalink / raw)
  To: Jonathan Wakely; +Cc: ryxi, gcc-patches, libstdc++

On 2017-05-22 14:50 +0100, Jonathan Wakely wrote:
> On 22/05/17 21:41 +0800, Xi Ruoyao wrote:
> > On 2017-05-22 14:00 +0100, Jonathan Wakely wrote:
> > > On 18/05/17 19:27 +0800, Xi Ruoyao wrote:
> > > > This patch use ioctl to get entropy of std::random_device using
> > > > /dev/random and /dev/urandom.
> > > 
> > > This is a nice addition, thanks.
> > > 
> > > N.B. I couldn't apply your patch, several lines had U+00A0 (i.e.
> > > NO-BREAK SPACE) characters where normal spaces should be. I don't know
> > > if this is something your mail client did, but maybe attaching the
> > > patch as a multipart message rather than including it inline in the
> > > body will prevent it in future.
> > 
> > Oh no...  Seems my email client destroyed my patch. I'll attach the
> > patch next time.  Please wait for PATCH v2.
> 
> OK, will do. I missed part of the required changes when adding a new
> symbol version, see the attached patch.
> 

In very old Linux kernel (1.3.x) there is random.h but not RNDGETENTCNT.
The random.h without RNDGETENTCNT was only used in the kernel internally.
But at that time Linux Makefile didn't have "headers_install" target
so user may copy all headers (including the internal ones) to /usr/include.
It seems we should check RNDGETENTCNT explicitly if we want to support
1.3.x.
-- 
Xi Ruoyao <ryxi@stu.xidian.edu.cn>
School of Aerospace Science and Technology, Xidian University

^ permalink raw reply	[flat|nested] 9+ messages in thread

* [PATCH v2] Implement non-trivial std::random_device::entropy (PR libstdc++/67578)
  2017-05-22 14:10     ` Jonathan Wakely
  2017-05-22 14:19       ` Xi Ruoyao
@ 2017-05-22 14:30       ` Xi Ruoyao
  2017-05-23 16:22         ` Jonathan Wakely
  1 sibling, 1 reply; 9+ messages in thread
From: Xi Ruoyao @ 2017-05-22 14:30 UTC (permalink / raw)
  To: Jonathan Wakely; +Cc: ryxi, gcc-patches, libstdc++

[-- Attachment #1: Type: text/plain, Size: 1268 bytes --]

On 2017-05-22Monçš„ 14:50 +0100, Jonathan Wakely wrote:
> On 22/05/17 21:41 +0800, Xi Ruoyao wrote:
> > On 2017-05-22 14:00 +0100, Jonathan Wakely wrote:
> > > On 18/05/17 19:27 +0800, Xi Ruoyao wrote:
> > > > This patch use ioctl to get entropy of std::random_device using
> > > > /dev/random and /dev/urandom.
> > > 
> > > This is a nice addition, thanks.
> > > 
> > > N.B. I couldn't apply your patch, several lines had U+00A0 (i.e.
> > > NO-BREAK SPACE) characters where normal spaces should be. I don't know
> > > if this is something your mail client did, but maybe attaching the
> > > patch as a multipart message rather than including it inline in the
> > > body will prevent it in future.
> > 
> > Oh no...  Seems my email client destroyed my patch. I'll attach the
> > patch next time.  Please wait for PATCH v2.
> 
> OK, will do. I missed part of the required changes when adding a new
> symbol version, see the attached patch.

The new patch is attached.  Just merged the patches you sent and fixed
the ChangeLog of gnu.ver and testsuite_abi.cc.

(For fun:  I had mistakenly attached the Vim .swp file of the patch
and almost sent it. :-p)
-- 
Xi Ruoyao <ryxi@stu.xidian.edu.cn>
School of Aerospace Science and Technology, Xidian University

[-- Attachment #2: 0001-PR-libstdc-67578-Implement-non-trivial-std-random_de.patch --]
[-- Type: text/x-patch, Size: 6942 bytes --]

From dd343500100a32cca46fce8c698d34161ca25958 Mon Sep 17 00:00:00 2001
From: Xi Ruoyao <ryxi@stu.xidian.edu.cn>
Date: Mon, 22 May 2017 22:19:18 +0800
Subject: [PATCH] PR libstdc++/67578 Implement non-trivial
 std::random_device::entropy

    2017-05-22  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.
---
 libstdc++-v3/acinclude.m4                    |  2 +-
 libstdc++-v3/config/abi/pre/gnu.ver          |  7 ++++++
 libstdc++-v3/configure.ac                    |  2 +-
 libstdc++-v3/doc/xml/manual/abi.xml          |  2 ++
 libstdc++-v3/include/bits/random.h           |  9 ++++++-
 libstdc++-v3/src/c++11/random.cc             | 35 ++++++++++++++++++++++++++++
 libstdc++-v3/testsuite/util/testsuite_abi.cc |  3 ++-
 7 files changed, 56 insertions(+), 4 deletions(-)

diff --git a/libstdc++-v3/acinclude.m4 b/libstdc++-v3/acinclude.m4
index 61c8cb2..baeea67 100644
--- a/libstdc++-v3/acinclude.m4
+++ b/libstdc++-v3/acinclude.m4
@@ -3750,7 +3750,7 @@ changequote([,])dnl
 fi
 
 # For libtool versioning info, format is CURRENT:REVISION:AGE
-libtool_VERSION=6:23:0
+libtool_VERSION=6:24:0
 
 # Everything parsed; figure out what files and settings to use.
 case $enable_symvers in
diff --git a/libstdc++-v3/config/abi/pre/gnu.ver b/libstdc++-v3/config/abi/pre/gnu.ver
index 268fb94..48e2ec8 100644
--- a/libstdc++-v3/config/abi/pre/gnu.ver
+++ b/libstdc++-v3/config/abi/pre/gnu.ver
@@ -1967,6 +1967,13 @@ GLIBCXX_3.4.23 {
 
 } GLIBCXX_3.4.22;
 
+GLIBCXX_3.4.24 {
+
+    # std::random_device::_M_getentropy() const
+    _ZNKSt13random_device13_M_getentropyEv;
+
+} GLIBCXX_3.4.23;
+
 # Symbols in the support library (libsupc++) have their own tag.
 CXXABI_1.3 {
 
diff --git a/libstdc++-v3/configure.ac b/libstdc++-v3/configure.ac
index 8e97350..270dcba 100644
--- a/libstdc++-v3/configure.ac
+++ b/libstdc++-v3/configure.ac
@@ -237,7 +237,7 @@ AC_CHECK_HEADERS([endian.h execinfo.h float.h fp.h ieeefp.h inttypes.h \
 locale.h machine/endian.h machine/param.h nan.h stdint.h stdlib.h string.h \
 strings.h sys/ipc.h sys/isa_defs.h sys/machine.h sys/param.h \
 sys/resource.h sys/sem.h sys/stat.h sys/time.h sys/types.h unistd.h \
-wchar.h wctype.h])
+wchar.h wctype.h linux/random.h])
 
 # Only do link tests if native. Else, hardcode.
 if $GLIBCXX_IS_NATIVE; then
diff --git a/libstdc++-v3/doc/xml/manual/abi.xml b/libstdc++-v3/doc/xml/manual/abi.xml
index d086d9e..de8e66c 100644
--- a/libstdc++-v3/doc/xml/manual/abi.xml
+++ b/libstdc++-v3/doc/xml/manual/abi.xml
@@ -266,6 +266,7 @@ compatible.
     <listitem><para>GCC 5.1.0: libstdc++.so.6.0.21</para></listitem>
     <listitem><para>GCC 6.1.0: libstdc++.so.6.0.22</para></listitem>
     <listitem><para>GCC 7.1.0: libstdc++.so.6.0.23</para></listitem>
+    <listitem><para>GCC 8.0.0: libstdc++.so.6.0.24</para></listitem>
     </itemizedlist>
     <para>
       Note 1: Error should be libstdc++.so.3.0.3.
@@ -334,6 +335,7 @@ compatible.
     <listitem><para>GCC 5.1.0: GLIBCXX_3.4.21, CXXABI_1.3.9</para></listitem>
     <listitem><para>GCC 6.1.0: GLIBCXX_3.4.22, CXXABI_1.3.10</para></listitem>
     <listitem><para>GCC 7.1.0: GLIBCXX_3.4.23, CXXABI_1.3.11</para></listitem>
+    <listitem><para>GCC 8.0.0: GLIBCXX_3.4.24, CXXABI_1.3.10</para></listitem>
     </itemizedlist>
     </listitem>
 
diff --git a/libstdc++-v3/include/bits/random.h b/libstdc++-v3/include/bits/random.h
index d39cc3e..bb761ec 100644
--- a/libstdc++-v3/include/bits/random.h
+++ b/libstdc++-v3/include/bits/random.h
@@ -1603,7 +1603,13 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
 
     double
     entropy() const noexcept
-    { return 0.0; }
+    {
+#ifdef _GLIBCXX_USE_RANDOM_TR1
+      return this->_M_getentropy();
+#else
+      return 0.0;
+#endif
+    }
 
     result_type
     operator()()
@@ -1627,6 +1633,7 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
 
     result_type _M_getval();
     result_type _M_getval_pretr1();
+    double _M_getentropy() const noexcept;
 
     union
     {
diff --git a/libstdc++-v3/src/c++11/random.cc b/libstdc++-v3/src/c++11/random.cc
index df79874..5011cf2 100644
--- a/libstdc++-v3/src/c++11/random.cc
+++ b/libstdc++-v3/src/c++11/random.cc
@@ -38,6 +38,14 @@
 # include <unistd.h>
 #endif
 
+#ifdef _GLIBCXX_HAVE_SYS_IOCTL_H
+# include <sys/ioctl.h>
+#endif
+
+#ifdef _GLIBCXX_HAVE_LINUX_RANDOM_H
+# include <linux/random.h>
+#endif
+
 namespace std _GLIBCXX_VISIBILITY(default)
 {
   namespace
@@ -161,6 +169,33 @@ namespace std _GLIBCXX_VISIBILITY(default)
     return _M_mt();
   }
 
+  double
+  random_device::_M_getentropy() const noexcept
+  {
+#if defined _GLIBCXX_HAVE_SYS_IOCTL_H && defined RNDGETENTCNT
+    if (!_M_file)
+      return 0.0;
+
+    const int fd = fileno(static_cast<FILE*>(_M_file));
+    if (fd < 0)
+      return 0.0;
+
+    int ent;
+    if (ioctl(fd, RNDGETENTCNT, &ent) < 0)
+      return 0.0;
+
+    if (ent < 0)
+      return 0.0;
+
+    if (static_cast<unsigned>(ent) > sizeof(result_type) * 8)
+      return static_cast<double>(sizeof(result_type) * 8);
+
+    return static_cast<double>(ent);
+#else
+    return 0.0;
+#endif
+  }
+
   template class mersenne_twister_engine<
     uint_fast32_t,
     32, 624, 397, 31,
diff --git a/libstdc++-v3/testsuite/util/testsuite_abi.cc b/libstdc++-v3/testsuite/util/testsuite_abi.cc
index f5fc594..ee7572e 100644
--- a/libstdc++-v3/testsuite/util/testsuite_abi.cc
+++ b/libstdc++-v3/testsuite/util/testsuite_abi.cc
@@ -204,6 +204,7 @@ check_version(symbol& test, bool added)
       known_versions.push_back("GLIBCXX_LDBL_3.4.21");
       known_versions.push_back("GLIBCXX_3.4.22");
       known_versions.push_back("GLIBCXX_3.4.23");
+      known_versions.push_back("GLIBCXX_3.4.24");
       known_versions.push_back("CXXABI_1.3");
       known_versions.push_back("CXXABI_LDBL_1.3");
       known_versions.push_back("CXXABI_1.3.1");
@@ -234,7 +235,7 @@ check_version(symbol& test, bool added)
 	test.version_status = symbol::incompatible;
 
       // Check that added symbols are added in the latest pre-release version.
-      bool latestp = (test.version_name == "GLIBCXX_3.4.23"
+      bool latestp = (test.version_name == "GLIBCXX_3.4.24"
 		     || test.version_name == "CXXABI_1.3.11"
 		     || test.version_name == "CXXABI_FLOAT128"
 		     || test.version_name == "CXXABI_TM_1");
-- 
2.7.1


^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: [PATCH] Implement non-trivial std::random_device::entropy (PR libstdc++/67578)
  2017-05-22 14:19       ` Xi Ruoyao
@ 2017-05-23 12:11         ` Jonathan Wakely
  0 siblings, 0 replies; 9+ messages in thread
From: Jonathan Wakely @ 2017-05-23 12:11 UTC (permalink / raw)
  To: Xi Ruoyao; +Cc: gcc-patches, libstdc++

On 22/05/17 22:10 +0800, Xi Ruoyao wrote:
>In very old Linux kernel (1.3.x) there is random.h but not RNDGETENTCNT.
>The random.h without RNDGETENTCNT was only used in the kernel internally.
>But at that time Linux Makefile didn't have "headers_install" target
>so user may copy all headers (including the internal ones) to /usr/include.
>It seems we should check RNDGETENTCNT explicitly if we want to support
>1.3.x.

OK, I think that's ancient enough that we don't need to care about it,
but on the other hand it's not impossible to believe that somebody
could be using a custom Linux kernel that removed it. So let's keep
the test for it.


^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: [PATCH v2] Implement non-trivial std::random_device::entropy (PR libstdc++/67578)
  2017-05-22 14:30       ` [PATCH v2] " Xi Ruoyao
@ 2017-05-23 16:22         ` Jonathan Wakely
  2017-05-24 19:28           ` Jonathan Wakely
  0 siblings, 1 reply; 9+ messages in thread
From: Jonathan Wakely @ 2017-05-23 16:22 UTC (permalink / raw)
  To: Xi Ruoyao; +Cc: gcc-patches, libstdc++

On 22/05/17 22:28 +0800, Xi Ruoyao wrote:
>The new patch is attached.  Just merged the patches you sent and fixed
>the ChangeLog of gnu.ver and testsuite_abi.cc.
>
>(For fun:  I had mistakenly attached the Vim .swp file of the patch
>and almost sent it. :-p)

Ha, that *definitely* wouldnt' have applied, but I think I'd have
noticed the reason sooner ;-)

I've committed the patch now - thanks.


>@@ -334,6 +335,7 @@ compatible.
>     <listitem><para>GCC 5.1.0: GLIBCXX_3.4.21, CXXABI_1.3.9</para></listitem>
>     <listitem><para>GCC 6.1.0: GLIBCXX_3.4.22, CXXABI_1.3.10</para></listitem>
>     <listitem><para>GCC 7.1.0: GLIBCXX_3.4.23, CXXABI_1.3.11</para></listitem>
>+    <listitem><para>GCC 8.0.0: GLIBCXX_3.4.24, CXXABI_1.3.10</para></listitem>

Oops, this should have been CXXABI_1.3.11, I fixed that before
committing it.

>+    if (static_cast<unsigned>(ent) > sizeof(result_type) * 8)
>+      return static_cast<double>(sizeof(result_type) * 8);

After committing it I realised this should use __CHAR_BIT__ instead of
hardcoding 8, which I'll fix shortly.


^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: [PATCH v2] Implement non-trivial std::random_device::entropy (PR libstdc++/67578)
  2017-05-23 16:22         ` Jonathan Wakely
@ 2017-05-24 19:28           ` Jonathan Wakely
  0 siblings, 0 replies; 9+ messages in thread
From: Jonathan Wakely @ 2017-05-24 19:28 UTC (permalink / raw)
  To: Xi Ruoyao; +Cc: gcc-patches, libstdc++

[-- Attachment #1: Type: text/plain, Size: 1214 bytes --]

On 23/05/17 17:15 +0100, Jonathan Wakely wrote:
>On 22/05/17 22:28 +0800, Xi Ruoyao wrote:
>>The new patch is attached.  Just merged the patches you sent and fixed
>>the ChangeLog of gnu.ver and testsuite_abi.cc.
>>
>>(For fun:  I had mistakenly attached the Vim .swp file of the patch
>>and almost sent it. :-p)
>
>Ha, that *definitely* wouldnt' have applied, but I think I'd have
>noticed the reason sooner ;-)
>
>I've committed the patch now - thanks.
>
>
>>@@ -334,6 +335,7 @@ compatible.
>>    <listitem><para>GCC 5.1.0: GLIBCXX_3.4.21, CXXABI_1.3.9</para></listitem>
>>    <listitem><para>GCC 6.1.0: GLIBCXX_3.4.22, CXXABI_1.3.10</para></listitem>
>>    <listitem><para>GCC 7.1.0: GLIBCXX_3.4.23, CXXABI_1.3.11</para></listitem>
>>+    <listitem><para>GCC 8.0.0: GLIBCXX_3.4.24, CXXABI_1.3.10</para></listitem>
>
>Oops, this should have been CXXABI_1.3.11, I fixed that before
>committing it.
>
>>+    if (static_cast<unsigned>(ent) > sizeof(result_type) * 8)
>>+      return static_cast<double>(sizeof(result_type) * 8);
>
>After committing it I realised this should use __CHAR_BIT__ instead of
>hardcoding 8, which I'll fix shortly.

Fixed with this patch. Tested powerpc64le-linux, committed to trunk.


[-- Attachment #2: patch.txt --]
[-- Type: text/x-patch, Size: 853 bytes --]

commit 26e91f1858aa054c28b521d86065966df4ba7099
Author: Jonathan Wakely <jwakely@redhat.com>
Date:   Wed May 24 17:41:34 2017 +0100

    Use CHAR_BIT instead of assuming 8 bits
    
    	* src/c++11/random.cc (random_device::_M_getentropy): Use __CHAR_BIT__
    	instead of fixed number of bits.

diff --git a/libstdc++-v3/src/c++11/random.cc b/libstdc++-v3/src/c++11/random.cc
index 5011cf2..ef17223 100644
--- a/libstdc++-v3/src/c++11/random.cc
+++ b/libstdc++-v3/src/c++11/random.cc
@@ -187,8 +187,9 @@ namespace std _GLIBCXX_VISIBILITY(default)
     if (ent < 0)
       return 0.0;
 
-    if (static_cast<unsigned>(ent) > sizeof(result_type) * 8)
-      return static_cast<double>(sizeof(result_type) * 8);
+    const int max = sizeof(result_type) * __CHAR_BIT__;
+    if (ent > max)
+      ent = max;
 
     return static_cast<double>(ent);
 #else

^ permalink raw reply	[flat|nested] 9+ messages in thread

end of thread, other threads:[~2017-05-24 19:28 UTC | newest]

Thread overview: 9+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2017-05-18 12:15 [PATCH] Implement non-trivial std::random_device::entropy (PR libstdc++/67578) Xi Ruoyao
2017-05-22 13:21 ` Jonathan Wakely
2017-05-22 13:50   ` Xi Ruoyao
2017-05-22 14:10     ` Jonathan Wakely
2017-05-22 14:19       ` Xi Ruoyao
2017-05-23 12:11         ` Jonathan Wakely
2017-05-22 14:30       ` [PATCH v2] " Xi Ruoyao
2017-05-23 16:22         ` Jonathan Wakely
2017-05-24 19:28           ` Jonathan Wakely

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).