From patchwork Fri Jun 30 14:09:23 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jonathan Wakely X-Patchwork-Id: 114732 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:994d:0:b0:3d9:f83d:47d9 with SMTP id k13csp10396665vqr; Fri, 30 Jun 2023 07:11:25 -0700 (PDT) X-Google-Smtp-Source: APBJJlGlHFa4Go+g1lu3zDOVtVstz6PNRmzBpUYtIQe0OHZZBAIKOYxjSTlP4XiQJqscDwduhHTL X-Received: by 2002:ac2:5b8b:0:b0:4f8:58f4:b96e with SMTP id o11-20020ac25b8b000000b004f858f4b96emr2108580lfn.37.1688134285061; Fri, 30 Jun 2023 07:11:25 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1688134285; cv=none; d=google.com; s=arc-20160816; b=tY1z9UBCmJnKLkdyTsOlXf7nPdoGTlQQaHHoKDovrBSGrWWP2I46pArK27EajpM7sg SXtNTXtfeuOif74sR7zW4s2BoIQIRxIWj+nsjUGX5cnP81avoHtfJmM+sBXP4dTtlBUG 14SosNqKr1bnY5+CO0T1vLylD1TJvqf4ZY+WBhnEAvX+hcYqqvsNJyS2WKresTw+m09S yolMmGbt7OXzKh7LX4Amx0JkGy2UdXMyOao3hxgMvJ6kA8DTfCy6s0eRpC2EOFaYKAd7 XIlL9MvLRtFcarWWmD3kbqZtvy16vObPiphqq8X973NkRRl/IBVkriRrNFli7Sii7vvs zBhQ== 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=HB86SeyRtOCyAueJ3PazwPVcBhNZxnWrohEFl1c4OG8=; fh=sJ+2/4g29YdyXkoRrFZSpsL2zxijepB7X/1rB0LDDh8=; b=bRRslrCSRsMzEPr2ITrtvXs/AhX3LC24KOOerATbEQxl/xw5wNcYWSOFGBMQbc/6tx MYlQnudl7GWIWGJhjDqn57jQeYZyiyfYzNYqYDo7TWz4U/p8iNMXaDip2kLqp9XDKRSu Nk8v7C+f4MV7BPejXNu/Q9P9nzvQjQo5dgylJqq3cddW+IhX+W64oo0t5kvr68W2uQCv nG51Lw7YR0MfKbWz3KsLkByTUBLKC9FaN3/C7RKMKFWgwmuhcjS4A97enN7pRi2qnCu4 y+vPWnpL4y6xQaFNpxn3q/5KtCKxYSGXxuf9KCNF/1qy8mCy0fIijmeMuDTM0ovXj0DN NYqw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gcc.gnu.org header.s=default header.b=hOyeXK6f; spf=pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 2620:52:3:1:0:246e:9693:128c 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. [2620:52:3:1:0:246e:9693:128c]) by mx.google.com with ESMTPS id w25-20020a056402129900b0051de3433c89si1702807edv.427.2023.06.30.07.11.24 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 30 Jun 2023 07:11:25 -0700 (PDT) Received-SPF: pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 2620:52:3:1:0:246e:9693:128c as permitted sender) client-ip=2620:52:3:1:0:246e:9693:128c; Authentication-Results: mx.google.com; dkim=pass header.i=@gcc.gnu.org header.s=default header.b=hOyeXK6f; spf=pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 2620:52:3:1:0:246e:9693:128c 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 4CF763858D38 for ; Fri, 30 Jun 2023 14:11:12 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 4CF763858D38 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1688134272; bh=HB86SeyRtOCyAueJ3PazwPVcBhNZxnWrohEFl1c4OG8=; h=To:Subject:Date:List-Id:List-Unsubscribe:List-Archive:List-Post: List-Help:List-Subscribe:From:Reply-To:From; b=hOyeXK6fWWI+9853YWas7gQRsdtnnKes/1V+Gia+wCv4W3f7adNn/OpoE+8VUIkOd G99dhLQ6Gtr5ri1CQoxm539h3DjHOy45pPHVFUajvFQbaMR2D/MafO0wIBqxeifjj7 fchpPCQ5an6nTX+lKfZ2iU+jCdrzSfKzEGXy1ShI= 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 6B1AF387093B for ; Fri, 30 Jun 2023 14:10:29 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 6B1AF387093B Received: from mimecast-mx02.redhat.com (mimecast-mx02.redhat.com [66.187.233.88]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-203-0_l4x8S2OQaVlsgR2G-ufA-1; Fri, 30 Jun 2023 10:10:25 -0400 X-MC-Unique: 0_l4x8S2OQaVlsgR2G-ufA-1 Received: from smtp.corp.redhat.com (int-mx08.intmail.prod.int.rdu2.redhat.com [10.11.54.8]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id 42D9885A58A; Fri, 30 Jun 2023 14:10:25 +0000 (UTC) Received: from localhost (unknown [10.42.28.54]) by smtp.corp.redhat.com (Postfix) with ESMTP id 0DF38C00049; Fri, 30 Jun 2023 14:10:24 +0000 (UTC) To: libstdc++@gcc.gnu.org, gcc-patches@gcc.gnu.org Subject: [committed] libstdc++: Make std::random_device throw more std::system_error [PR105081] Date: Fri, 30 Jun 2023 15:09:23 +0100 Message-ID: <20230630141024.716797-1-jwakely@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 3.1 on 10.11.54.8 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com X-Spam-Status: No, score=-11.8 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_H4, RCVD_IN_MSPIKE_WL, SPF_HELO_NONE, SPF_NONE, TXREP, T_SCC_BODY_TEXT_LINE 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?1770137096000377053?= X-GMAIL-MSGID: =?utf-8?q?1770137096000377053?= Tested x86_64-linux. Pushed to trunk. -- >8 -- In r14-289-gf9412cedd6c0e7 I made the std::random_device constructor throw std::system_error for unrecognized tokens. But it still throws std::runtime_error for a token such as "rdseed" that is recognized but not supported at runtime by the CPU the program is running on. With this change we throw std::system_error for those cases too. This fixes the following failures on Intel CPUs withour rdseed support: FAIL: 26_numerics/random/random_device/94087.cc execution test FAIL: 26_numerics/random/random_device/cons/token.cc execution test FAIL: 26_numerics/random/random_device/entropy.cc execution test libstdc++-v3/ChangeLog: PR libstdc++/105081 * src/c++11/random.cc (random_device::_M_init): Throw std::system_error when the requested device is a valid token but not available at runtime. --- libstdc++-v3/src/c++11/random.cc | 23 ++++++++++++++++++++--- 1 file changed, 20 insertions(+), 3 deletions(-) diff --git a/libstdc++-v3/src/c++11/random.cc b/libstdc++-v3/src/c++11/random.cc index 6ecdc7169ab..cece6edbfc7 100644 --- a/libstdc++-v3/src/c++11/random.cc +++ b/libstdc++-v3/src/c++11/random.cc @@ -373,6 +373,15 @@ namespace std _GLIBCXX_VISIBILITY(default) "(const std::string&):" " unsupported token")); +#if defined ENOSYS + const int unsupported = ENOSYS; +#elif defined ENOTSUP + const int unsupported = ENOTSUP; +#else + const int unsupported = 0; +#endif + int err = 0; + #ifdef _GLIBCXX_USE_CRT_RAND_S if (which & rand_s) { @@ -407,6 +416,7 @@ namespace std _GLIBCXX_VISIBILITY(default) return; } } + err = unsupported; } #endif // USE_RDSEED @@ -427,6 +437,7 @@ namespace std _GLIBCXX_VISIBILITY(default) return; } } + err = unsupported; } #endif // USE_RDRAND @@ -438,6 +449,7 @@ namespace std _GLIBCXX_VISIBILITY(default) _M_func = &__ppc_darn; return; } + err = unsupported; } #endif // USE_DARN @@ -458,6 +470,7 @@ namespace std _GLIBCXX_VISIBILITY(default) _M_func = &__libc_getentropy; return; } + err = unsupported; } #endif // _GLIBCXX_HAVE_GETENTROPY @@ -477,6 +490,7 @@ namespace std _GLIBCXX_VISIBILITY(default) if (_M_file) return; #endif // USE_POSIX_FILE_IO + err = errno; } #endif // _GLIBCXX_USE_DEV_RANDOM @@ -493,9 +507,12 @@ namespace std _GLIBCXX_VISIBILITY(default) } #endif - std::__throw_runtime_error( - __N("random_device::random_device(const std::string&):" - " device not available")); + auto msg = __N("random_device::random_device(const std::string&):" + " device not available"); + if (err) + std::__throw_syserr(err, msg); + else + std::__throw_runtime_error(msg); #endif // USE_MT19937 }