From owner-dev-commits-src-branches@freebsd.org  Thu Aug 19 01:12:02 2021
Return-Path: <owner-dev-commits-src-branches@freebsd.org>
Delivered-To: dev-commits-src-branches@mailman.nyi.freebsd.org
Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2610:1c1:1:606c::19:1])
 by mailman.nyi.freebsd.org (Postfix) with ESMTP id 76CA765A152;
 Thu, 19 Aug 2021 01:12:02 +0000 (UTC) (envelope-from git@FreeBSD.org)
Received: from mxrelay.nyi.freebsd.org (mxrelay.nyi.freebsd.org
 [IPv6:2610:1c1:1:606c::19:3])
 (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits)
 key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256
 client-signature RSA-PSS (4096 bits) client-digest SHA256)
 (Client CN "mxrelay.nyi.freebsd.org", Issuer "R3" (verified OK))
 by mx1.freebsd.org (Postfix) with ESMTPS id 4GqmvQ2nNHz3pjl;
 Thu, 19 Aug 2021 01:12:02 +0000 (UTC) (envelope-from git@FreeBSD.org)
Received: from gitrepo.freebsd.org (gitrepo.freebsd.org
 [IPv6:2610:1c1:1:6068::e6a:5])
 (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits)
 key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256)
 (Client did not present a certificate)
 by mxrelay.nyi.freebsd.org (Postfix) with ESMTPS id 47C661BF00;
 Thu, 19 Aug 2021 01:12:02 +0000 (UTC) (envelope-from git@FreeBSD.org)
Received: from gitrepo.freebsd.org ([127.0.1.44])
 by gitrepo.freebsd.org (8.16.1/8.16.1) with ESMTP id 17J1C2DS017435;
 Thu, 19 Aug 2021 01:12:02 GMT (envelope-from git@gitrepo.freebsd.org)
Received: (from git@localhost)
 by gitrepo.freebsd.org (8.16.1/8.16.1/Submit) id 17J1C2Ea017434;
 Thu, 19 Aug 2021 01:12:02 GMT (envelope-from git)
Date: Thu, 19 Aug 2021 01:12:02 GMT
Message-Id: <202108190112.17J1C2Ea017434@gitrepo.freebsd.org>
To: src-committers@FreeBSD.org, dev-commits-src-all@FreeBSD.org,
 dev-commits-src-branches@FreeBSD.org
From: Alexander Motin <mav@FreeBSD.org>
Subject: git: 09d4fe40e039 - stable/13 - Fix race between first rand(3) calls
 with _once().
MIME-Version: 1.0
Content-Type: text/plain; charset=utf-8
Content-Transfer-Encoding: 8bit
X-Git-Committer: mav
X-Git-Repository: src
X-Git-Refname: refs/heads/stable/13
X-Git-Reftype: branch
X-Git-Commit: 09d4fe40e039d4b08a034454c36aed86571725b0
Auto-Submitted: auto-generated
X-BeenThere: dev-commits-src-branches@freebsd.org
X-Mailman-Version: 2.1.34
Precedence: list
List-Id: Commits to the stable branches of the FreeBSD src repository
 <dev-commits-src-branches.freebsd.org>
List-Unsubscribe: <https://lists.freebsd.org/mailman/options/dev-commits-src-branches>, 
 <mailto:dev-commits-src-branches-request@freebsd.org?subject=unsubscribe>
List-Archive: <http://lists.freebsd.org/pipermail/dev-commits-src-branches/>
List-Post: <mailto:dev-commits-src-branches@freebsd.org>
List-Help: <mailto:dev-commits-src-branches-request@freebsd.org?subject=help>
List-Subscribe: <https://lists.freebsd.org/mailman/listinfo/dev-commits-src-branches>, 
 <mailto:dev-commits-src-branches-request@freebsd.org?subject=subscribe>
X-List-Received-Date: Thu, 19 Aug 2021 01:12:02 -0000

The branch stable/13 has been updated by mav:

URL: https://cgit.FreeBSD.org/src/commit/?id=09d4fe40e039d4b08a034454c36aed86571725b0

commit 09d4fe40e039d4b08a034454c36aed86571725b0
Author:     Alexander Motin <mav@FreeBSD.org>
AuthorDate: 2021-07-21 15:25:46 +0000
Commit:     Alexander Motin <mav@FreeBSD.org>
CommitDate: 2021-08-19 01:11:51 +0000

    Fix race between first rand(3) calls with _once().
    
    Before this patch there was a chance for thread that called rand(3)
    slightly later to see rand3_state already allocated, but not yet
    initialized.  While this API is not expected to be thread-safe, it
    is not expected to crash.  ztest on 64-thread system reproduced it
    reliably for me.
    
    Submitted by:   avg@
    MFC after:      1 month
    
    (cherry picked from commit 3a57f08b5042f15bb8ffb2fcce99d082d225d4cf)
---
 lib/libc/stdlib/rand.c | 9 +++++----
 1 file changed, 5 insertions(+), 4 deletions(-)

diff --git a/lib/libc/stdlib/rand.c b/lib/libc/stdlib/rand.c
index bddb0f040302..e448d1b1fd14 100644
--- a/lib/libc/stdlib/rand.c
+++ b/lib/libc/stdlib/rand.c
@@ -41,11 +41,13 @@ __FBSDID("$FreeBSD$");
 #include <sys/param.h>
 #include <sys/sysctl.h>
 #include <assert.h>
+#include <pthread.h>
 #include <stdbool.h>
 #include <stdlib.h>
 #include <syslog.h>
 #include "un-namespace.h"
 
+#include "libc_private.h"
 #include "random.h"
 
 /*
@@ -64,6 +66,7 @@ __FBSDID("$FreeBSD$");
  * the advantage of being the one already in the tree.
  */
 static struct __random_state *rand3_state;
+static pthread_once_t rand3_state_once = PTHREAD_ONCE_INIT;
 
 static void
 initialize_rand3(void)
@@ -78,16 +81,14 @@ initialize_rand3(void)
 int
 rand(void)
 {
-	if (rand3_state == NULL)
-		initialize_rand3();
+	_once(&rand3_state_once, initialize_rand3);
 	return ((int)random_r(rand3_state));
 }
 
 void
 srand(unsigned seed)
 {
-	if (rand3_state == NULL)
-		initialize_rand3();
+	_once(&rand3_state_once, initialize_rand3);
 	srandom_r(rand3_state, seed);
 }