Skip site navigation (1)Skip section navigation (2)
Date:      Wed, 2 Dec 2020 15:20:28 -0800
From:      Conrad Meyer <cem@freebsd.org>
To:        Andriy Gapon <avg@freebsd.org>
Cc:        FreeBSD Current <freebsd-current@freebsd.org>
Subject:   Re: rand() is racy in multi-threaded programs?
Message-ID:  <CAG6CVpXdrivsAk45dDKtk_0M3wuZ-WkdBz1HWHtr2GwRa6v78Q@mail.gmail.com>
In-Reply-To: <5b827768-eb46-07f3-5b44-49627779786e@FreeBSD.org>
References:  <5b827768-eb46-07f3-5b44-49627779786e@FreeBSD.org>

next in thread | previous in thread | raw e-mail | index | archive | help
Hi Andriy,

Rand(3) is explicitly unsafe to use from concurrent threads without some
external serialization, even after initialization. I=E2=80=99d suggest usin=
g a
different API.

Best,
Conrad

On Wed, Dec 2, 2020 at 13:53 Andriy Gapon <avg@freebsd.org> wrote:

>
> Specifically, concurrent "first" calls to rand().
> There can be a moment when rand3_state is allocated but not completely se=
t
> 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 o=
n
> illumos and Linux.
>
> P.P.S.
> Just realized that the problem can be relatively recent.
> https://svnweb.freebsd.org/base?view=3Drevision&revision=3D357382
>
> --
> Andriy Gapon
>



Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?CAG6CVpXdrivsAk45dDKtk_0M3wuZ-WkdBz1HWHtr2GwRa6v78Q>