From patchwork Mon Jan 20 20:06:43 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Florian Weimer X-Patchwork-Id: 105138 Return-Path: X-Original-To: patchwork@sourceware.org Delivered-To: patchwork@sourceware.org Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 398563858CD1 for ; Mon, 20 Jan 2025 20:08:31 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 398563858CD1 Authentication-Results: sourceware.org; dkim=pass (1024-bit key, unprotected) header.d=redhat.com header.i=@redhat.com header.a=rsa-sha256 header.s=mimecast20190719 header.b=RC2CBrn7 X-Original-To: libc-alpha@sourceware.org Delivered-To: libc-alpha@sourceware.org Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) by sourceware.org (Postfix) with ESMTP id 3F2A63858D21 for ; Mon, 20 Jan 2025 20:06:50 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 3F2A63858D21 Authentication-Results: sourceware.org; dmarc=pass (p=none dis=none) header.from=redhat.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=redhat.com ARC-Filter: OpenARC Filter v1.0.0 sourceware.org 3F2A63858D21 Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=170.10.133.124 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1737403610; cv=none; b=NHvB4ISFK1EMOH3sjyh5B76tbGRK2pMZUXryMLlN/713qpgAxj5zLIUiPgqZtHwtnQx48CSoIPk2I5rcuh5llYLWSwycxuOp2iil3dR1BJ0Qh+x4sRpa8Vpur9jg5Sy1IuPSgRiHEd8cc3qk3hVn1GIomSqmXTIoPR0HH+iPeKQ= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1737403610; c=relaxed/simple; bh=Ctz11CdFBQu7uPuK/8gm6pVvHffDfN86+oMhcl9PWK4=; h=DKIM-Signature:From:To:Subject:Message-ID:Date:MIME-Version; b=tDpq5sJjGtZBu/u0aLf33KerIF65g2poZ4/rySBJNwvftFGMgqtvPaBmdYQtiEeoKuEZ3X+PmDwAevYGvgwNH9hws+UlASgRAjLHOWLUdfB7gg8Lg3tD7e9JJwu8Gmr+SSZ/oqojIwtGgkkgu0SY+hliet2O7yL+7JnxXtnuW3g= ARC-Authentication-Results: i=1; server2.sourceware.org DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 3F2A63858D21 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1737403610; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:mime-version:mime-version:content-type:content-type; bh=q1lDU2vjpBrKCxdBjACaGO/ugBXYAkbEdRaECMqiJMM=; b=RC2CBrn7/U1Mr9bwNTlHcir0pF+XrVvePMFGUE0i4oaDjwX9RkZDWUq9ao3OJhhwi0hKO2 +8tLR4TZ/VJVrvK4yXLq4Y6uCsMsIamA542OGxZl+N95Wsy+Kx91cXpjJ++GAnoj6IVOx/ rQaKaWHIL7Bp8tNaMFhQOSZvgkysdn4= Received: from mx-prod-mc-02.mail-002.prod.us-west-2.aws.redhat.com (ec2-54-186-198-63.us-west-2.compute.amazonaws.com [54.186.198.63]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-568-WTCBleJyN6Ck8md9wBKgsQ-1; Mon, 20 Jan 2025 15:06:48 -0500 X-MC-Unique: WTCBleJyN6Ck8md9wBKgsQ-1 X-Mimecast-MFC-AGG-ID: WTCBleJyN6Ck8md9wBKgsQ Received: from mx-prod-int-01.mail-002.prod.us-west-2.aws.redhat.com (mx-prod-int-01.mail-002.prod.us-west-2.aws.redhat.com [10.30.177.4]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by mx-prod-mc-02.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTPS id 9800A1955D8D for ; Mon, 20 Jan 2025 20:06:47 +0000 (UTC) Received: from oldenburg.str.redhat.com (unknown [10.2.16.54]) by mx-prod-int-01.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTPS id C01BE3003FD2 for ; Mon, 20 Jan 2025 20:06:46 +0000 (UTC) From: Florian Weimer To: libc-alpha@sourceware.org Subject: [PATCH 1/2] stdlib: Fix unintended change to the random_r implementation Message-ID: <76b56012252d98de3ffacb5f8e3b2adfd6ee6ede.1737403568.git.fweimer@redhat.com> X-From-Line: 76b56012252d98de3ffacb5f8e3b2adfd6ee6ede Mon Sep 17 00:00:00 2001 Date: Mon, 20 Jan 2025 21:06:43 +0100 User-Agent: Gnus/5.13 (Gnus v5.13) MIME-Version: 1.0 X-Scanned-By: MIMEDefang 3.4.1 on 10.30.177.4 X-Mimecast-Spam-Score: 0 X-Mimecast-MFC-PROC-ID: AVTbYgEIZsFFyLlF0g3DGjfd-Av3m7WhGCeiXoAAlSo_1737403607 X-Mimecast-Originator: redhat.com X-Spam-Status: No, score=-12.3 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=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on server2.sourceware.org X-BeenThere: libc-alpha@sourceware.org X-Mailman-Version: 2.1.30 Precedence: list List-Id: Libc-alpha mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: libc-alpha-bounces~patchwork=sourceware.org@sourceware.org Commit d5bceac99d24af1131b90027dab267e437b65cd1 changed the sequence of random numbers. This was completely unintended. The statistical properties of the new sequences are unclear, so restore the old behavior. Fixes commit d5bceac99d24af1131b90027dab267e437b65cd1 ("stdlib: random_r: fix unaligned access in initstate and initstate_r [BZ #30584]"). Reviewed-by: Noah Goldstein --- stdlib/random_r.c | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) base-commit: a3131e71cf65c0c28869824de9b1b1fab363882c diff --git a/stdlib/random_r.c b/stdlib/random_r.c index 605e96983c..b49f03f5be 100644 --- a/stdlib/random_r.c +++ b/stdlib/random_r.c @@ -390,9 +390,10 @@ __random_r (struct random_data *buf, int32_t *result) int32_t *end_ptr = buf->end_ptr; uint32_t val; - val = read_state (rptr, 0); - int32_t t = read_state (fptr, 0); - write_state (fptr, 0, t + val); + /* Avoid integer overflow with uint32_t arihmetic. */ + val = read_state (fptr, 0); + val += read_state (rptr, 0); + write_state (fptr, 0, val); /* Chucking least random bit. */ *result = val >> 1; ++fptr; From patchwork Mon Jan 20 20:06:59 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Florian Weimer X-Patchwork-Id: 105139 Return-Path: X-Original-To: patchwork@sourceware.org Delivered-To: patchwork@sourceware.org Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id AD47C3858C52 for ; Mon, 20 Jan 2025 20:08:46 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org AD47C3858C52 Authentication-Results: sourceware.org; dkim=pass (1024-bit key, unprotected) header.d=redhat.com header.i=@redhat.com header.a=rsa-sha256 header.s=mimecast20190719 header.b=e4//QljV X-Original-To: libc-alpha@sourceware.org Delivered-To: libc-alpha@sourceware.org Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.129.124]) by sourceware.org (Postfix) with ESMTP id C8DB53858417 for ; Mon, 20 Jan 2025 20:07:04 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org C8DB53858417 Authentication-Results: sourceware.org; dmarc=pass (p=none dis=none) header.from=redhat.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=redhat.com ARC-Filter: OpenARC Filter v1.0.0 sourceware.org C8DB53858417 Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=170.10.129.124 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1737403624; cv=none; b=lZoiz4HhWq9IVYL8URQwyqWL0n1RJRZiktYTTbnJt6f0wR8RjQbfQr/+d3RPuUgqeflQbLQN0fTGIBjcvowHSfxWAL2pqPWmcc2oRM8VkZ8FP0FSZB68VfQ4RAeE/ofywazKqjfp7ShcZjuVMBt+113C1u2EBYL7NIAKdRaPqPE= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1737403624; c=relaxed/simple; bh=x00tF5XV1UKVMTC8LYEjBfmVsdkemjpWdOrvxveT898=; h=DKIM-Signature:From:To:Subject:Message-ID:Date:MIME-Version; b=NSOKv8Y2nljE++zQzMfbKC01ajomOx5XKt/rbQpRdNpYG32ybHMhlSal2GWCLE4Ii0z2he1cP85KxppcUQoGeFOKKQEseDRUw3AKSvPCulrgHHaMFoZ7alx3vrra9Qi6L/y8FLrCbQw/peV1fspBfTi7R0o4UzMY23pMQTotw9I= ARC-Authentication-Results: i=1; server2.sourceware.org DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org C8DB53858417 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1737403624; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:mime-version:mime-version:content-type:content-type: in-reply-to:in-reply-to:references:references; bh=o9QNx0JQWyQYrKAAgkhBe+F8/dNsyyKjE73tE2OCPDw=; b=e4//QljV9gK3WvcVEOXxTxhRR+cYIxKXsuqrRAoD+3oAshk+ywt9Q8s6uURVIRk0uJbfPK T6fEj8iIXtteRICQTp+gtpgPirLgTa5u1tq5wbSYsTDPQMXhqhSGYrfZFgaqMmRad5ndU6 9iVb0byEp+yDppnZKaS9X4TTJdhfXsg= Received: from mx-prod-mc-01.mail-002.prod.us-west-2.aws.redhat.com (ec2-54-186-198-63.us-west-2.compute.amazonaws.com [54.186.198.63]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-607-0ZXxObKLPYiDR4f37khF2g-1; Mon, 20 Jan 2025 15:07:03 -0500 X-MC-Unique: 0ZXxObKLPYiDR4f37khF2g-1 X-Mimecast-MFC-AGG-ID: 0ZXxObKLPYiDR4f37khF2g Received: from mx-prod-int-05.mail-002.prod.us-west-2.aws.redhat.com (mx-prod-int-05.mail-002.prod.us-west-2.aws.redhat.com [10.30.177.17]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by mx-prod-mc-01.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTPS id 8C12C1955F34 for ; Mon, 20 Jan 2025 20:07:02 +0000 (UTC) Received: from oldenburg.str.redhat.com (unknown [10.2.16.54]) by mx-prod-int-05.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTPS id B1D1319560A3 for ; Mon, 20 Jan 2025 20:07:01 +0000 (UTC) From: Florian Weimer To: libc-alpha@sourceware.org Subject: [PATCH 2/2] stdlib: Test for expected sequence of random numbers from rand In-Reply-To: <76b56012252d98de3ffacb5f8e3b2adfd6ee6ede.1737403568.git.fweimer@redhat.com> Message-ID: <42535323bc7a2024fd577240dd254c51e0a08ee2.1737403568.git.fweimer@redhat.com> References: <76b56012252d98de3ffacb5f8e3b2adfd6ee6ede.1737403568.git.fweimer@redhat.com> X-From-Line: 42535323bc7a2024fd577240dd254c51e0a08ee2 Mon Sep 17 00:00:00 2001 Date: Mon, 20 Jan 2025 21:06:59 +0100 User-Agent: Gnus/5.13 (Gnus v5.13) MIME-Version: 1.0 X-Scanned-By: MIMEDefang 3.0 on 10.30.177.17 X-Mimecast-Spam-Score: 0 X-Mimecast-MFC-PROC-ID: Aa6NDsjYfobCinEjMjliJD0qWeeY8TvNFkTIB3yuCC0_1737403622 X-Mimecast-Originator: redhat.com X-Spam-Status: No, score=-12.3 required=5.0 tests=BAYES_00, DKIMWL_WL_HIGH, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, GIT_PATCH_0, KAM_SHORT, RCVD_IN_DNSWL_NONE, RCVD_IN_MSPIKE_H5, RCVD_IN_MSPIKE_WL, SPF_HELO_NONE, SPF_NONE, TXREP autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on server2.sourceware.org X-BeenThere: libc-alpha@sourceware.org X-Mailman-Version: 2.1.30 Precedence: list List-Id: Libc-alpha mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: libc-alpha-bounces~patchwork=sourceware.org@sourceware.org As the test comment explains, this test is not quite valid, but preserving the exact sequences helps distributions to port to newer glibc versions. We can remove this test if we ever switch to a different implementation. Reviewed-by: Noah Goldstein --- stdlib/Makefile | 1 + stdlib/tst-rand-sequence.c | 57 ++++++++++++++++++++++++++++++++++++++ 2 files changed, 58 insertions(+) create mode 100644 stdlib/tst-rand-sequence.c diff --git a/stdlib/Makefile b/stdlib/Makefile index 18a19f46eb..a5fbc1a27e 100644 --- a/stdlib/Makefile +++ b/stdlib/Makefile @@ -296,6 +296,7 @@ tests := \ tst-qsort3 \ tst-qsort6 \ tst-quick_exit \ + tst-rand-sequence \ tst-rand48 \ tst-rand48-2 \ tst-random \ diff --git a/stdlib/tst-rand-sequence.c b/stdlib/tst-rand-sequence.c new file mode 100644 index 0000000000..4691dfd1e7 --- /dev/null +++ b/stdlib/tst-rand-sequence.c @@ -0,0 +1,57 @@ +/* Test that rand () evaluates to a special expected sequence. + Copyright (C) 2025 Free Software Foundation, Inc. + This file is part of the GNU C Library. + + The GNU C Library is free software; you can redistribute it and/or + modify it under the terms of the GNU Lesser General Public + License as published by the Free Software Foundation; either + version 2.1 of the License, or (at your option) any later version. + + The GNU C Library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public + License along with the GNU C Library; if not, see + . */ + +/* This test is borderline invalid because the rand function is not + guaranteed to produce the same sequence of numbers on every + architecture or glibc build. However, some software packages have + test suites that depend on the specific sequence of numbers return + here, so this test verifies that they do not change unexpectedly. */ + +#include +#include + +static int +do_test (void) +{ + TEST_COMPARE (rand (), 1804289383); + TEST_COMPARE (rand (), 846930886); + TEST_COMPARE (rand (), 1681692777); + TEST_COMPARE (rand (), 1714636915); + TEST_COMPARE (rand (), 1957747793); + TEST_COMPARE (rand (), 424238335); + TEST_COMPARE (rand (), 719885386); + TEST_COMPARE (rand (), 1649760492); + TEST_COMPARE (rand (), 596516649); + TEST_COMPARE (rand (), 1189641421); + + srand (1009); + TEST_COMPARE (rand (), 176208083); + TEST_COMPARE (rand (), 1650100842); + TEST_COMPARE (rand (), 1813188575); + TEST_COMPARE (rand (), 2064804480); + TEST_COMPARE (rand (), 1944264725); + TEST_COMPARE (rand (), 1673642853); + TEST_COMPARE (rand (), 1582759448); + TEST_COMPARE (rand (), 309901569); + TEST_COMPARE (rand (), 444031692); + TEST_COMPARE (rand (), 1926035991); + + return 0; +} + +#include