From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: by sourceware.org (Postfix, from userid 2181) id DD3663858D28; Wed, 18 Jan 2023 21:49:06 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org DD3663858D28 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1674078546; bh=vLD3RlIvj//YX9dEynpFS0fS9/paqhGtcTcGpp3GJL0=; h=From:To:Subject:Date:From; b=g92nGjW4vBnt6/V7X+abieCgEyjbfbWaiEW8s9yeu/Ew3/Cif1/DsXPHLuevcyFuK 9Aubawn+fKeIzipvpyGuXFa9sUCOn4nWzgUQCG50ZOGwf9GvBbt1mlwppzuWxOWyMg lEW8IUliLshS0E5Us4EppB05YKPFe6H0dJUcdEks= MIME-Version: 1.0 Content-Transfer-Encoding: 7bit Content-Type: text/plain; charset="utf-8" From: Jonathan Wakely To: gcc-cvs@gcc.gnu.org, libstdc++-cvs@gcc.gnu.org Subject: [gcc r13-5247] libstdc++: Fix std::random_device::entropy() for non-posix targets X-Act-Checkin: gcc X-Git-Author: Jonathan Wakely X-Git-Refname: refs/heads/master X-Git-Oldrev: af7881e07631fc1c61deb307119f7cabdd4094a1 X-Git-Newrev: 26c68b8c31f637cc01f4bf511f9a0ca714231161 Message-Id: <20230118214906.DD3663858D28@sourceware.org> Date: Wed, 18 Jan 2023 21:49:06 +0000 (GMT) List-Id: https://gcc.gnu.org/g:26c68b8c31f637cc01f4bf511f9a0ca714231161 commit r13-5247-g26c68b8c31f637cc01f4bf511f9a0ca714231161 Author: Jonathan Wakely Date: Wed Jan 18 13:09:10 2023 +0000 libstdc++: Fix std::random_device::entropy() for non-posix targets Since the r12-4515-g58f339fc5eaae7 change std::random_device::entropy() returns non-zero for hardware sources such as RDRAND. However, the call to the underlying _M_getentropy function is conditionally compiled according to #if _GLIBCXX_USE_DEV_RANDOM which means it only happens for targets that support /dev/random and /dev/urandom. This means entropy() always returns zero for x86 Windows, even though the RDRAND and RDSEED sources work there. The _M_getentropy() function is always compiled into the library, it just doesn't get called for targets without /dev/random. We can change that just by removing the #if conditional. This is not an ABI change, because new code will just start calling the existing _M_getentropy function, old code that has inlined entropy() will not call it. Similarly, the std::random_device destructor doesn't call the underlying _M_fini function unless _GLIBCXX_USE_DEV_RANDOM is defined. That's less of a problem because it's still true that the only resources that need to be freed are when one of /dev/random or /dev/urandom has been opened for reading, which is only possible when _GLIBCXX_USE_DEV_RANDOM is defined. The _M_fini function does also destroy a random engine object if a std::linear_congruential_engine object is used, but that destructor is trivial and so no resources are leaked if it's not called. Remove the preprocessor condition in the destructor too, so that we always call the _M_fini function even if it doesn't have side effects. This makes the destructor non-trivial for Windows and bare metal targets, but as the class is non-copyable that shouldn't cause any ABI change in practice. libstdc++-v3/ChangeLog: * include/bits/random.h (random_device) [!_GLIBCXX_USE_DEV_RANDOM]: Always call _M_fini and _M_getentropy. Diff: --- libstdc++-v3/include/bits/random.h | 10 +--------- 1 file changed, 1 insertion(+), 9 deletions(-) diff --git a/libstdc++-v3/include/bits/random.h b/libstdc++-v3/include/bits/random.h index e2b9bdf568c..42f37c1e77e 100644 --- a/libstdc++-v3/include/bits/random.h +++ b/libstdc++-v3/include/bits/random.h @@ -1639,10 +1639,8 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION explicit random_device(const std::string& __token) { _M_init(__token); } -#if defined _GLIBCXX_USE_DEV_RANDOM ~random_device() { _M_fini(); } -#endif static constexpr result_type min() @@ -1654,13 +1652,7 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION double entropy() const noexcept - { -#ifdef _GLIBCXX_USE_DEV_RANDOM - return this->_M_getentropy(); -#else - return 0.0; -#endif - } + { return this->_M_getentropy(); } result_type operator()()