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 in 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');
124 { package Class; DESTROY { ++$destroyed; } }
130 $h{key} = bless({}, 'Class');
132 is($destroyed, 1, 'Timely hash destruction with lvalue keys');
135 # [perl #79178] Hash keys must not be stringified during compilation
136 # Run perl -MO=Concise -e '$a{\"foo"}' on a non-threaded pre-5.13.8 version
141 sub TIEHASH { bless {}, $_[0] }
142 sub FETCH { $key = $_[1] }
146 is ref $key, SCALAR =>
147 'hash keys are not stringified during compilation';
150 # Part of RT #85026: Deleting the current iterator in void context does not
155 local *::DESTROY = sub { ++$gone };
157 each %a; # make the entry with the obj the current iterator
159 ok $gone, 'deleting the current iterator in void context frees the val'
162 # [perl #99660] Deleted hash element visible to destructor
167 local *::DESTROY = sub { my $x = $h{k}; ++$normal_exit };
168 delete $h{k}; # must be in void context to trigger the bug
169 ok $normal_exit, 'freed hash elems are not visible to DESTROY';
172 # [perl #100340] Similar bug: freeing a hash elem during a delete
177 my $callback = shift;
178 return bless \$callback, "guard"
192 ok eval { delete $t{stash}{guard}; # must be in void context
194 'freeing a hash elem from destructor called by delete does not die';
195 diag $@ if $@; # panic: free from wrong pool
196 is $ok, 1, 'the destructor was called';
199 # Weak references to pad hashes
201 skip_if_miniperl("No Scalar::Util::weaken under miniperl", 1);
203 require Scalar::Util;
206 Scalar::Util::weaken($ref = \%hash);
207 1; # the previous statement must not be the last
209 is $ref, undef, 'weak refs to pad hashes go stale on scope exit';
213 sub A::DESTROY { $::ra = 0 }
214 $::ra = {a=>bless [], 'A'};
216 pass 'no crash when freeing hash that is being undeffed';
217 $::ra = {a=>bless [], 'A'};
219 pass 'no crash when freeing hash that is being exonerated, ahem, cleared';