Skip to content

Commit

Permalink
(perl #127663) create a separate random souce for internal use
Browse files Browse the repository at this point in the history
and use it to initialize hash randomization and to innoculate against
quadratic behaviour in pp_sort
  • Loading branch information
tonycoz committed Dec 6, 2016
1 parent 5aa240e commit ecdd0c8
Show file tree
Hide file tree
Showing 6 changed files with 19 additions and 4 deletions.
1 change: 1 addition & 0 deletions embedvar.h
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,7 @@
#define PL_incgv (vTHX->Iincgv)
#define PL_initav (vTHX->Iinitav)
#define PL_inplace (vTHX->Iinplace)
#define PL_internal_random_state (vTHX->Iinternal_random_state)
#define PL_isarev (vTHX->Iisarev)
#define PL_known_layers (vTHX->Iknown_layers)
#define PL_last_in_gv (vTHX->Ilast_in_gv)
Expand Down
8 changes: 8 additions & 0 deletions intrpvar.h
Original file line number Diff line number Diff line change
Expand Up @@ -810,6 +810,14 @@ PERLVAR(I, random_state, PL_RANDOM_STATE_TYPE)

PERLVARI(I, dump_re_max_len, STRLEN, 0)

/* For internal uses of randomness, this ensures the sequence of
* random numbers returned by rand() isn't modified by perl's internal
* use of randomness.
* This is important if the user has called srand() with a seed.
*/

PERLVAR(I, internal_random_state, PL_RANDOM_STATE_TYPE)

/* If you are adding a U8 or U16, check to see if there are 'Space' comments
* above on where there are gaps which currently will be structure padding. */

Expand Down
2 changes: 2 additions & 0 deletions perl.c
Original file line number Diff line number Diff line change
Expand Up @@ -261,6 +261,8 @@ perl_construct(pTHXx)

init_constants();

Perl_drand48_init_r(&PL_internal_random_state, seed());

SvREADONLY_on(&PL_sv_placeholder);
SvREFCNT(&PL_sv_placeholder) = SvREFCNT_IMMORTAL;

Expand Down
2 changes: 1 addition & 1 deletion pp_sort.c
Original file line number Diff line number Diff line change
Expand Up @@ -787,7 +787,7 @@ S_qsortsvu(pTHX_ SV ** array, size_t num_elts, SVCOMPARE_t compare)
size_t n;
SV ** const q = array;
for (n = num_elts; n > 1; ) {
const size_t j = (size_t)(n-- * Drand01());
const size_t j = (size_t)(n-- * Perl_internal_drand48());
temp = q[j];
q[j] = q[n];
q[n] = temp;
Expand Down
4 changes: 1 addition & 3 deletions util.c
Original file line number Diff line number Diff line change
Expand Up @@ -4757,10 +4757,8 @@ Perl_get_hash_seed(pTHX_ unsigned char * const seed_buffer)
else
#endif
{
(void)seedDrand01((Rand_seed_t)seed());

for( i = 0; i < PERL_HASH_SEED_BYTES; i++ ) {
seed_buffer[i] = (unsigned char)(Drand01() * (U8_MAX+1));
seed_buffer[i] = (unsigned char)(Perl_internal_drand48() * (U8_MAX+1));
}
}
#ifdef USE_PERL_PERTURB_KEYS
Expand Down
6 changes: 6 additions & 0 deletions util.h
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,12 @@ typedef struct PERL_DRAND48_T perl_drand48_t;
#define Perl_drand48_init(seed) (Perl_drand48_init_r(&PL_random_state, (seed)))
#define Perl_drand48() (Perl_drand48_r(&PL_random_state))

#ifdef PERL_CORE
/* uses a different source of randomness to avoid interfering with the results
* of rand() */
#define Perl_internal_drand48() (Perl_drand48_r(&PL_internal_random_state))
#endif

#ifdef USE_C_BACKTRACE

typedef struct {
Expand Down

0 comments on commit ecdd0c8

Please sign in to comment.