15 ok (!Internals::HvREHASH(%h), "hash doesn't start with rehash flag on");
21 ok (!Internals::HvREHASH(%h), "10 entries doesn't trigger rehash");
27 ok (Internals::HvREHASH(%h), "20 entries triggers rehash");
32 # second part using an emulation of the PERL_HASH in perl, mounting an
33 # attack on a pre-populated hash. This is also useful if you need normal
34 # keys which don't contain \0 -- suitable for stashes
36 use constant MASK_U32 => 2**32;
37 use constant HASH_SEED => 0;
38 use constant THRESHOLD => 14;
39 use constant START => "a";
41 # some initial hash data
42 my %h2 = map {$_ => 1} 'a'..'cc';
44 ok (!Internals::HvREHASH(%h2),
45 "starting with pre-populated non-pathological hash (rehash flag if off)");
47 my @keys = get_keys(\%h2);
49 ok (Internals::HvREHASH(%h2),
50 scalar(@keys) . " colliding into the same bucket keys are triggering rehash");
55 # the minimum of bits required to mount the attack on a hash
56 my $min_bits = log(THRESHOLD)/log(2);
58 # if the hash has already been populated with a significant amount
59 # of entries the number of mask bits can be higher
60 my $keys = scalar keys %$hr;
61 my $bits = $keys ? log($keys)/log(2) : 0;
62 $bits = $min_bits if $min_bits > $bits;
64 $bits = int($bits) < $bits ? int($bits) + 1 : int($bits);
65 # need to add 2 bits to cover the internal split cases
67 my $mask = 2**$bits-1;
68 print "# using mask: $mask ($bits)\n";
73 # get 2 keys on top of the THRESHOLD
75 while (@keys < THRESHOLD+2) {
76 # next if exists $hash->{$s};
78 next unless ($hash & $mask) == 0;
80 printf "# %2d: %5s, %10s\n", $c, $s, $hash;
90 # trying to provide the fastest equivalent of C macro's PERL_HASH in
91 # Perl - the main complication is that it uses U32 integer, which we
92 # can't do it perl, without doing some tricks
98 # (A % M) + (B % M) == (A + B) % M
99 # This works because '+' produces a NV, which is big enough to hold
100 # the intermediate result. We only need the % before any "^" and "&"
101 # to get the result in the range for an I32.
102 # and << doesn't work on NV, so using 1 << 10
104 $u += $u * (1 << 10); $u %= MASK_U32;
107 $u += $u << 3; $u %= MASK_U32;
108 $u ^= $u >> 11; $u %= MASK_U32;
109 $u += $u << 15; $u %= MASK_U32;
113 # This will crash perl if it fails
115 use constant PVBM => 'foo';
117 my $dummy = index 'foo', PVBM;
118 eval { my %h = (a => PVBM); 1 };
120 ok (!$@, 'fbm scalar can be inserted into a hash');