diff options
author | Yves Orton <demerphq@gmail.com> | 2022-08-05 13:18:02 +0200 |
---|---|---|
committer | Yves Orton <demerphq@gmail.com> | 2022-08-12 22:29:05 +0200 |
commit | bf2a3dae9f4f828fd1f2f8aaf4769f96520c9552 (patch) | |
tree | b58ad50f0d8d828bb5a890686e0ce7e82ae529ae /pp.c | |
parent | 08da5deb5d0c842dab3fe5f4f5a450972a0eb67c (diff) | |
download | perl-bf2a3dae9f4f828fd1f2f8aaf4769f96520c9552.tar.gz |
Add a new env var PERL_RAND_SEED
This env var can be used to trigger a repeatable run of a script which
calls C<srand()> with no arguments, either explicitly or implicitly
via use of C<rand()> prior to calling srand(). This is implemented in
such a way that calling C<srand()> with no arguments in forks or
subthreads (again explicitly or implicitly) will receive their own seed
but the seeds they receive will be repeatable.
This is intended for debugging and perl development performance testing,
and for running the test suite consistently. It is documented that the
exact seeds used to initialize the random state are unspecified, and
that they may change between releases or even builds. The only guarantee
provided is that the same perl executable will produce the same results
twice all other things being equal. In practice and in core testing we
do expect consistency, but adding the tightest set of restrictions on
our commitments seemed sensible.
The env var is ignored when perl is run setuid or setgid similarly to
the C<PERL_INTERNAL_RAND_SEED> env var.
Diffstat (limited to 'pp.c')
-rw-r--r-- | pp.c | 20 |
1 files changed, 18 insertions, 2 deletions
@@ -2920,7 +2920,17 @@ PP(pp_sin) PP(pp_rand) { if (!PL_srand_called) { - (void)seedDrand01((Rand_seed_t)seed()); + Rand_seed_t s; + if (PL_srand_override) { + /* env var PERL_RAND_SEED has been set so the user wants + * consistent srand() initialization. */ + PERL_SRAND_OVERRIDE_GET(s); + } else { + /* Pseudo random initialization from context state and possible + * random devices */ + s= (Rand_seed_t)seed(); + } + (void)seedDrand01(s); PL_srand_called = TRUE; } { @@ -2979,7 +2989,13 @@ PP(pp_srand) } } else { - anum = seed(); + if (PL_srand_override) { + /* env var PERL_RAND_SEED has been set so the user wants + * consistent srand() initialization. */ + PERL_SRAND_OVERRIDE_GET(anum); + } else { + anum = seed(); + } } (void)seedDrand01((Rand_seed_t)anum); |