public inbox for libstdc++@gcc.gnu.org
 help / color / mirror / Atom feed
From: Jonathan Wakely <jwakely@redhat.com>
To: libstdc++@gcc.gnu.org, gcc-patches@gcc.gnu.org
Subject: [committed] libstdc++: Fix std::random_device::entropy() for non-posix targets
Date: Wed, 18 Jan 2023 21:49:22 +0000	[thread overview]
Message-ID: <20230118214922.440230-1-jwakely@redhat.com> (raw)

Tested x86_64-linux. Pushed to trunk.

-- >8 --

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


                 reply	other threads:[~2023-01-18 21:49 UTC|newest]

Thread overview: [no followups] expand[flat|nested]  mbox.gz  Atom feed

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20230118214922.440230-1-jwakely@redhat.com \
    --to=jwakely@redhat.com \
    --cc=gcc-patches@gcc.gnu.org \
    --cc=libstdc++@gcc.gnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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).