From patchwork Wed Jan 18 21:49:22 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jonathan Wakely X-Patchwork-Id: 45520 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:adf:eb09:0:0:0:0:0 with SMTP id s9csp2580146wrn; Wed, 18 Jan 2023 13:51:30 -0800 (PST) X-Google-Smtp-Source: AMrXdXu1eepYnPalBBY+rsuKh+6620VdUojk3sfav3gY13epNxsMEz3guI79AFQ2Bx0fU+CJ6YIR X-Received: by 2002:a17:906:eb05:b0:84c:7974:8a73 with SMTP id mb5-20020a170906eb0500b0084c79748a73mr8211634ejb.57.1674078690249; Wed, 18 Jan 2023 13:51:30 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1674078690; cv=none; d=google.com; s=arc-20160816; b=XC6yUAe0D8ROXHyrTuaif2+FeKHfjaVHfIGAJUCY2n6maNNaV/lC5BnCRFe8b+k/FY MKevoW5nRFp5h1TLPJaYIxwk4C8ZrQ5xcIchtmgNE4kbhzcAkJTCpcRYhAQT4CEquGhI kTwDV0MluH4ay5GhHnHWi+9oEn4oPkhDLHsphZh2cGQKosPNwQyugL/KI04JvZoVjnD7 uDDG6pTqCZ0ThjtlPQ4gRRAoBNBCyb0uSQIfD98vcnHf1JMhtyEw8c+yTnD6mKxwGoyH iZs+5uPijkAQH1YHrhvo/l8rTtzyQUOVvZgMCnT3Z1nosyCu9yshyVTEdEq1Jai7XCn8 0VIQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=sender:errors-to:reply-to:from:list-subscribe:list-help:list-post :list-archive:list-unsubscribe:list-id:precedence :content-transfer-encoding:mime-version:message-id:date:subject:to :dmarc-filter:delivered-to:dkim-signature:dkim-filter; bh=CQAit+Nad+K+oIqwIIST/8r55dDY6qRwBaXASq2yRQ8=; b=YDQk9e1G4/ObjhIK++MdWX5g0Ftr3QxlwEdDlGUsfviDieIexcKpmRElMtmAl4cif9 egcoWqFP5wHOWWWzkdEoj1cuDkEgQ0odX4t1TQc+hMTQ1n5IzvRqaAjpFU5Jxul+nMTo 49RIdPYHV2ThT4DTRtKDkA/wBoxDjBI5JzoD+ElokO/DUpfJwgAeeovpfqvJBwew9nku Kueq0tu2+CaLLKtgeLrJz1MLycg4eKDzuGFv9iZUNIxM69Uda4/FRTPU9WYmeAILxkPm Il2n/EeGiNraSYr6oL4qtzmeDJBnsfKYhdsVhiEORvSDs7R2eKqxpT9GweOiw/lXNkj7 1bCQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gcc.gnu.org header.s=default header.b=VStm1UdE; spf=pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 8.43.85.97 as permitted sender) smtp.mailfrom="gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=gnu.org Received: from sourceware.org (server2.sourceware.org. [8.43.85.97]) by mx.google.com with ESMTPS id wv4-20020a170907080400b007c10bb5b4b3si39129453ejb.681.2023.01.18.13.51.30 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 18 Jan 2023 13:51:30 -0800 (PST) Received-SPF: pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 8.43.85.97 as permitted sender) client-ip=8.43.85.97; Authentication-Results: mx.google.com; dkim=pass header.i=@gcc.gnu.org header.s=default header.b=VStm1UdE; spf=pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 8.43.85.97 as permitted sender) smtp.mailfrom="gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=gnu.org Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 8510D385B513 for ; Wed, 18 Jan 2023 21:50:45 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 8510D385B513 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1674078645; bh=CQAit+Nad+K+oIqwIIST/8r55dDY6qRwBaXASq2yRQ8=; h=To:Subject:Date:List-Id:List-Unsubscribe:List-Archive:List-Post: List-Help:List-Subscribe:From:Reply-To:From; b=VStm1UdEJ2YSHFqaJhLIYmhbBThsmkvUNR1wLfJK8L0I11Zt1/G9A5ak2LxmwFg41 DjPH5faQVqoQOgJ/tFzHXy0M+W/VmKqq++LJ2U5k4u6ijmMqh/aKLF5Qy1ILq6cf85 LEyq8shFNMxztb9mmMY6sTKXsugjEpAKSHOwGw7A= X-Original-To: gcc-patches@gcc.gnu.org Delivered-To: gcc-patches@gcc.gnu.org Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.129.124]) by sourceware.org (Postfix) with ESMTPS id 748DC3858D35 for ; Wed, 18 Jan 2023 21:49:27 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 748DC3858D35 Received: from mimecast-mx02.redhat.com (mx3-rdu2.redhat.com [66.187.233.73]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-231-lsUL64-fPjKtmPSR5L_P4w-1; Wed, 18 Jan 2023 16:49:23 -0500 X-MC-Unique: lsUL64-fPjKtmPSR5L_P4w-1 Received: from smtp.corp.redhat.com (int-mx04.intmail.prod.int.rdu2.redhat.com [10.11.54.4]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id 8B01B1C0513C; Wed, 18 Jan 2023 21:49:23 +0000 (UTC) Received: from localhost (unknown [10.33.36.201]) by smtp.corp.redhat.com (Postfix) with ESMTP id 53E402026D4B; Wed, 18 Jan 2023 21:49:23 +0000 (UTC) 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 Message-Id: <20230118214922.440230-1-jwakely@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 3.1 on 10.11.54.4 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com X-Spam-Status: No, score=-12.2 required=5.0 tests=BAYES_00, DKIMWL_WL_HIGH, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, GIT_PATCH_0, RCVD_IN_DNSWL_NONE, RCVD_IN_MSPIKE_H2, SPF_HELO_NONE, SPF_NONE, TXREP autolearn=unavailable autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on server2.sourceware.org X-BeenThere: gcc-patches@gcc.gnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Gcc-patches mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-Patchwork-Original-From: Jonathan Wakely via Gcc-patches From: Jonathan Wakely Reply-To: Jonathan Wakely Errors-To: gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org Sender: "Gcc-patches" X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1755398736573103786?= X-GMAIL-MSGID: =?utf-8?q?1755398736573103786?= 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()()