From patchwork Mon May 22 14:28:08 2017 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Xi Ruoyao X-Patchwork-Id: 765441 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Received: from sourceware.org (server1.sourceware.org [209.132.180.131]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by ozlabs.org (Postfix) with ESMTPS id 3wWgxx5ZbGz9rxw for ; Tue, 23 May 2017 00:28:37 +1000 (AEST) Authentication-Results: ozlabs.org; dkim=pass (1024-bit key; unprotected) header.d=gcc.gnu.org header.i=@gcc.gnu.org header.b="k3UmYqHr"; dkim-atps=neutral DomainKey-Signature: a=rsa-sha1; c=nofws; d=gcc.gnu.org; h=list-id :list-unsubscribe:list-archive:list-post:list-help:sender :message-id:subject:from:to:cc:date:in-reply-to:references :content-type:mime-version; q=dns; s=default; b=HYoa1E1JEqP3TZ1X 9/sk/p31YG4gZ4Uzi7MHErlf2SqGNVc79+Yi9vPCXKKerd67eBsrzzKggmv2jDqB R4HhnazXPM6rxrid6wef1vAnyNKTypOJRXa8Q8l6hAbEBsPj4W9lBiwJLumPIpYZ Qg1xDYMD3YdBcUAFYRae4Sr7MHY= DKIM-Signature: v=1; a=rsa-sha1; c=relaxed; d=gcc.gnu.org; h=list-id :list-unsubscribe:list-archive:list-post:list-help:sender :message-id:subject:from:to:cc:date:in-reply-to:references :content-type:mime-version; s=default; bh=9TSKHVbO3Az4B0nd01y35r 5DwEQ=; b=k3UmYqHrDpqF6lPi03qUAHX8uYC/Rg0le+rkjJdmQVyT5P/RdGfFMJ dPj5UKBrzgcaq9UnvplofUH28bHnpZwIzBjSTEr/JWBnBCg7Cg+gS35DNTsWpbtT udo3tSmmnnuAC6ta68BEj5Q85P8xD4o6aH1j6xZF/pp8fv/duPxco= Received: (qmail 53491 invoked by alias); 22 May 2017 14:28:24 -0000 Mailing-List: contact gcc-patches-help@gcc.gnu.org; run by ezmlm Precedence: bulk List-Id: List-Unsubscribe: List-Archive: List-Post: List-Help: Sender: gcc-patches-owner@gcc.gnu.org Delivered-To: mailing list gcc-patches@gcc.gnu.org Received: (qmail 53469 invoked by uid 89); 22 May 2017 14:28:24 -0000 Authentication-Results: sourceware.org; auth=none X-Virus-Found: No X-Spam-SWARE-Status: No, score=-24.3 required=5.0 tests=BAYES_00, GIT_PATCH_0, GIT_PATCH_1, GIT_PATCH_2, GIT_PATCH_3, KAM_LAZY_DOMAIN_SECURITY, RP_MATCHES_RCVD, URIBL_SBL autolearn=ham version=3.3.2 spammy=H*Ad:D*edu.cn, H*Ad:D*cn X-HELO: stu.xidian.edu.cn Received: from stumail.xidian.edu.cn (HELO stu.xidian.edu.cn) (202.117.112.40) by sourceware.org (qpsmtpd/0.93/v0.84-503-g423c35a) with SMTP; Mon, 22 May 2017 14:28:19 +0000 Received: from xry111-laptop (xry111-laptop [10.177.176.78]) by stu.xidian.edu.cn (eYou MTA v8.1.0) with ESMTP id ae982119c327ce4c96baa4fe64c10539 for ; Mon, 22 May 2017 22:38:52 +0800 X-Eyou-Smtpauth: ryxi@stu.xidian.edu.cn Message-ID: <1495463288.29130.18.camel@stu.xidian.edu.cn> Subject: [PATCH v2] Implement non-trivial std::random_device::entropy (PR libstdc++/67578) From: Xi Ruoyao To: Jonathan Wakely Cc: ryxi@stu.xidian.edu.cn, gcc-patches@gcc.gnu.org, libstdc++@gcc.gnu.org Date: Mon, 22 May 2017 22:28:08 +0800 In-Reply-To: <20170522135002.GJ4527@redhat.com> References: <1495106850.1663.8.camel@stu.xidian.edu.cn> <20170522130058.GF4527@redhat.com> <1495460501.29130.6.camel@stu.xidian.edu.cn> <20170522135002.GJ4527@redhat.com> Mime-Version: 1.0 X-Eyou-Sender: 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) From dd343500100a32cca46fce8c698d34161ca25958 Mon Sep 17 00:00:00 2001 From: Xi Ruoyao 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 Jonathan Wakely 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 . * 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. GCC 5.1.0: libstdc++.so.6.0.21 GCC 6.1.0: libstdc++.so.6.0.22 GCC 7.1.0: libstdc++.so.6.0.23 + GCC 8.0.0: libstdc++.so.6.0.24 Note 1: Error should be libstdc++.so.3.0.3. @@ -334,6 +335,7 @@ compatible. GCC 5.1.0: GLIBCXX_3.4.21, CXXABI_1.3.9 GCC 6.1.0: GLIBCXX_3.4.22, CXXABI_1.3.10 GCC 7.1.0: GLIBCXX_3.4.23, CXXABI_1.3.11 + GCC 8.0.0: GLIBCXX_3.4.24, CXXABI_1.3.10 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 #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,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(_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, 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