From bf2a3dae9f4f828fd1f2f8aaf4769f96520c9552 Mon Sep 17 00:00:00 2001 From: Yves Orton Date: Fri, 5 Aug 2022 13:18:02 +0200 Subject: 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 with no arguments, either explicitly or implicitly via use of C prior to calling srand(). This is implemented in such a way that calling C 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 env var. --- pp_sys.c | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) (limited to 'pp_sys.c') diff --git a/pp_sys.c b/pp_sys.c index 729371813e..48ad17df80 100644 --- a/pp_sys.c +++ b/pp_sys.c @@ -4232,6 +4232,7 @@ PP(pp_fork) sigset_t oldmask, newmask; #endif + EXTEND(SP, 1); PERL_FLUSHALL_FOR_CHILD; #ifdef HAS_SIGPROCMASK @@ -4259,6 +4260,9 @@ PP(pp_fork) #ifdef PERL_USES_PL_PIDSTATUS hv_clear(PL_pidstatus); /* no kids, so don't wait for 'em */ #endif + PERL_SRAND_OVERRIDE_NEXT_CHILD(); + } else { + PERL_SRAND_OVERRIDE_NEXT_PARENT(); } PUSHi(childpid); RETURN; @@ -4271,6 +4275,19 @@ PP(pp_fork) childpid = PerlProc_fork(); if (childpid == -1) RETPUSHUNDEF; + else if (childpid) { + /* we are in the parent */ + PERL_SRAND_OVERRIDE_NEXT_PARENT(); + } + else { + /* This is part of the logic supporting the env var + * PERL_RAND_SEED which causes use of rand() without an + * explicit srand() to use a deterministic seed. This logic is + * intended to give most forked children of a process a + * deterministic but different srand seed. + */ + PERL_SRAND_OVERRIDE_NEXT_CHILD(); + } PUSHi(childpid); RETURN; #else -- cgit v1.2.1