From b65235d0df618d16b57d5f97dd8aa05e90aa5b83 Mon Sep 17 00:00:00 2001 From: Xi Ruoyao Date: Sun, 12 Mar 2017 21:23:38 +0800 Subject: [PATCH] Implement non-trivial std::random_device::entropy (PR libstdc++/67578) 2017-03-12 Xi Ruoyao 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.h.in | 3 ++ .../abi/post/i386-linux-gnu/baseline_symbols.txt | 1 + .../abi/post/i486-linux-gnu/baseline_symbols.txt | 1 + .../abi/post/x86_64-linux-gnu/baseline_symbols.txt | 1 + libstdc++-v3/config/abi/pre/gnu.ver | 3 ++ libstdc++-v3/configure | 4 +-- libstdc++-v3/configure.ac | 2 +- libstdc++-v3/include/bits/random.h | 9 +++++- libstdc++-v3/src/c++11/random.cc | 36 ++++++++++++++++++++++ 9 files changed, 56 insertions(+), 4 deletions(-) diff --git a/libstdc++-v3/config.h.in b/libstdc++-v3/config.h.in index 51b770d..6465ebd 100644 --- a/libstdc++-v3/config.h.in +++ b/libstdc++-v3/config.h.in @@ -267,6 +267,9 @@ /* Define if futex syscall is available. */ #undef HAVE_LINUX_FUTEX +/* Define to 1 if you have the header file. */ +#undef HAVE_LINUX_RANDOM_H + /* Define to 1 if you have the header file. */ #undef HAVE_LOCALE_H diff --git a/libstdc++-v3/config/abi/post/i386-linux-gnu/baseline_symbols.txt b/libstdc++-v3/config/abi/post/i386-linux-gnu/baseline_symbols.txt index f9eab290..55439c5 100644 --- a/libstdc++-v3/config/abi/post/i386-linux-gnu/baseline_symbols.txt +++ b/libstdc++-v3/config/abi/post/i386-linux-gnu/baseline_symbols.txt @@ -444,6 +444,7 @@ FUNC:_ZNKSt13basic_fstreamIwSt11char_traitsIwEE7is_openEv@GLIBCXX_3.4 FUNC:_ZNKSt13basic_istreamIwSt11char_traitsIwEE6gcountEv@@GLIBCXX_3.4 FUNC:_ZNKSt13basic_istreamIwSt11char_traitsIwEE6sentrycvbEv@@GLIBCXX_3.4 FUNC:_ZNKSt13basic_ostreamIwSt11char_traitsIwEE6sentrycvbEv@@GLIBCXX_3.4 +FUNC:_ZNKSt13random_device13_M_getentropyEv@@GLIBCXX_3.4.23 FUNC:_ZNKSt13runtime_error4whatEv@@GLIBCXX_3.4 FUNC:_ZNKSt14basic_ifstreamIcSt11char_traitsIcEE5rdbufEv@@GLIBCXX_3.4 FUNC:_ZNKSt14basic_ifstreamIcSt11char_traitsIcEE7is_openEv@@GLIBCXX_3.4.5 diff --git a/libstdc++-v3/config/abi/post/i486-linux-gnu/baseline_symbols.txt b/libstdc++-v3/config/abi/post/i486-linux-gnu/baseline_symbols.txt index f9eab290..55439c5 100644 --- a/libstdc++-v3/config/abi/post/i486-linux-gnu/baseline_symbols.txt +++ b/libstdc++-v3/config/abi/post/i486-linux-gnu/baseline_symbols.txt @@ -444,6 +444,7 @@ FUNC:_ZNKSt13basic_fstreamIwSt11char_traitsIwEE7is_openEv@GLIBCXX_3.4 FUNC:_ZNKSt13basic_istreamIwSt11char_traitsIwEE6gcountEv@@GLIBCXX_3.4 FUNC:_ZNKSt13basic_istreamIwSt11char_traitsIwEE6sentrycvbEv@@GLIBCXX_3.4 FUNC:_ZNKSt13basic_ostreamIwSt11char_traitsIwEE6sentrycvbEv@@GLIBCXX_3.4 +FUNC:_ZNKSt13random_device13_M_getentropyEv@@GLIBCXX_3.4.23 FUNC:_ZNKSt13runtime_error4whatEv@@GLIBCXX_3.4 FUNC:_ZNKSt14basic_ifstreamIcSt11char_traitsIcEE5rdbufEv@@GLIBCXX_3.4 FUNC:_ZNKSt14basic_ifstreamIcSt11char_traitsIcEE7is_openEv@@GLIBCXX_3.4.5 diff --git a/libstdc++-v3/config/abi/post/x86_64-linux-gnu/baseline_symbols.txt b/libstdc++-v3/config/abi/post/x86_64-linux-gnu/baseline_symbols.txt index a31597e..6ba742c 100644 --- a/libstdc++-v3/config/abi/post/x86_64-linux-gnu/baseline_symbols.txt +++ b/libstdc++-v3/config/abi/post/x86_64-linux-gnu/baseline_symbols.txt @@ -444,6 +444,7 @@ FUNC:_ZNKSt13basic_fstreamIwSt11char_traitsIwEE7is_openEv@GLIBCXX_3.4 FUNC:_ZNKSt13basic_istreamIwSt11char_traitsIwEE6gcountEv@@GLIBCXX_3.4 FUNC:_ZNKSt13basic_istreamIwSt11char_traitsIwEE6sentrycvbEv@@GLIBCXX_3.4 FUNC:_ZNKSt13basic_ostreamIwSt11char_traitsIwEE6sentrycvbEv@@GLIBCXX_3.4 +FUNC:_ZNKSt13random_device13_M_getentropyEv@@GLIBCXX_3.4.23 FUNC:_ZNKSt13runtime_error4whatEv@@GLIBCXX_3.4 FUNC:_ZNKSt14basic_ifstreamIcSt11char_traitsIcEE5rdbufEv@@GLIBCXX_3.4 FUNC:_ZNKSt14basic_ifstreamIcSt11char_traitsIcEE7is_openEv@@GLIBCXX_3.4.5 diff --git a/libstdc++-v3/config/abi/pre/gnu.ver b/libstdc++-v3/config/abi/pre/gnu.ver index 268fb94..b0c51d9 100644 --- a/libstdc++-v3/config/abi/pre/gnu.ver +++ b/libstdc++-v3/config/abi/pre/gnu.ver @@ -1965,6 +1965,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 b/libstdc++-v3/configure index 29456c4..0494bcf 100755 --- a/libstdc++-v3/configure +++ b/libstdc++-v3/configure @@ -18391,7 +18391,7 @@ else cat confdefs.h - <<_ACEOF >conftest.$ac_ext /* end confdefs.h. */ #define _GLIBCXX_INCLUDE_NEXT_C_HEADERS -#include + #include #undef isnan namespace std { using ::isnan; @@ -21795,7 +21795,7 @@ for ac_header in 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 do : as_ac_Header=`$as_echo "ac_cv_header_$ac_header" | $as_tr_sh` ac_fn_c_check_header_mongrel "$LINENO" "$ac_header" "$as_ac_Header" "$ac_includes_default" 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 #endif +#ifdef _GLIBCXX_HAVE_SYS_IOCTL_H +# include +#endif + +#ifdef _GLIBCXX_HAVE_LINUX_RANDOM_H +# include +#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(_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(ent) > sizeof(result_type) * 8) + return static_cast(sizeof(result_type) * 8); + + return static_cast(ent); +#else + return 0.0; +#endif + } + template class mersenne_twister_engine< uint_fast32_t, 32, 624, 397, 31, -- 2.7.1