From owner-freebsd-current@freebsd.org Thu Dec 3 07:34:01 2020 Return-Path: Delivered-To: freebsd-current@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 7BBDF47D23D for ; Thu, 3 Dec 2020 07:34:01 +0000 (UTC) (envelope-from avg@FreeBSD.org) Received: from relay3-d.mail.gandi.net (relay3-d.mail.gandi.net [217.70.183.195]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (Client did not present a certificate) by mx1.freebsd.org (Postfix) with ESMTPS id 4Cmndj0pZlz4vNr; Thu, 3 Dec 2020 07:34:00 +0000 (UTC) (envelope-from avg@FreeBSD.org) X-Originating-IP: 93.72.151.96 Received: from [192.168.0.88] (east.meadow.volia.net [93.72.151.96]) (Authenticated sender: andriy.gapon@uabsd.com) by relay3-d.mail.gandi.net (Postfix) with ESMTPSA id 946E360004; Thu, 3 Dec 2020 07:33:58 +0000 (UTC) Subject: Re: rand() is racy in multi-threaded programs? To: cem@freebsd.org Cc: FreeBSD Current References: <5b827768-eb46-07f3-5b44-49627779786e@FreeBSD.org> From: Andriy Gapon Openpgp: preference=signencrypt Autocrypt: addr=avg@FreeBSD.org; keydata= mDMEX1iFDhYJKwYBBAHaRw8BAQdAiu8JG/oLFkVkOAJqJc7Dx5KI/Q6C3SBI20EQm+DXnAu0 HkFuZHJpeSBHYXBvbiA8YXZnQEZyZWVCU0Qub3JnPoiWBBMWCAA+FiEEyCHHZM09l0OE3Ir/ 1A1+Gq8+L1EFAl9YhQ4CGwMFCQeEzgAFCwkIBwIGFQoJCAsCBBYCAwECHgECF4AACgkQ1A1+ Gq8+L1Fc0wD/ZjmhHfbCJywZU3aOxXIPjcz73FYEGMvqMCCLAWyLbSABALFL+1ZNrjV3BGjq 889cOYFuboA/Yn3eWezS+tfqYBsGuDgEX1iFDhIKKwYBBAGXVQEFAQEHQL6B20Xi600TrkpG P9fWjl7JtHNxqrHKhX6Kg7kgb4ILAwEIB4h+BBgWCAAmFiEEyCHHZM09l0OE3Ir/1A1+Gq8+ L1EFAl9YhQ4CGwwFCQeEzgAACgkQ1A1+Gq8+L1F3cgEAktp4h+IJUJxL1vn6zMOt//znni/J TanKfQuA8wGXcGkBAKpZJhqMkg+pKk7MGvJhgJ6nCpTZ+rMK6vZVZLUWc3QF Message-ID: <22f5dfc1-c709-6911-b8b0-121e4c40affc@FreeBSD.org> Date: Thu, 3 Dec 2020 09:33:57 +0200 User-Agent: Mozilla/5.0 (X11; FreeBSD amd64; rv:60.0) Gecko/20100101 Firefox/60.0 Thunderbird/60.9.0 MIME-Version: 1.0 In-Reply-To: Content-Type: text/plain; charset=utf-8 Content-Language: en-US Content-Transfer-Encoding: 8bit X-Rspamd-Queue-Id: 4Cmndj0pZlz4vNr X-Spamd-Bar: ---- X-Spamd-Result: default: False [-4.00 / 15.00]; REPLY(-4.00)[] X-BeenThere: freebsd-current@freebsd.org X-Mailman-Version: 2.1.34 Precedence: list List-Id: Discussions about the use of FreeBSD-current List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Thu, 03 Dec 2020 07:34:01 -0000 On 03/12/2020 01:20, Conrad Meyer wrote: > Hi Andriy, > > Rand(3) is explicitly unsafe to use from concurrent threads without some > external serialization, even after initialization. I’d suggest using a different > API. Conrad, thank you! Just want to check, unsafe in terms of bogus results (with respect to randomness) or unsafe as in may crash? > On Wed, Dec 2, 2020 at 13:53 Andriy Gapon > wrote: > > > Specifically, concurrent "first" calls to rand(). > There can be a moment when rand3_state is allocated but not completely set up > with initstate_r(). > Is this a known / documented issue? > Should we try to do better? > > P.S. > I am seeing this issue from time to time when running ztest program (from ZFS). > I guess that it uses rand() just because that's what OpenZFS did / does on > illumos and Linux. > > P.P.S. > Just realized that the problem can be relatively recent. > https://svnweb.freebsd.org/base?view=revision&revision=357382 > > -- > Andriy Gapon > -- Andriy Gapon