3 # A place to put some simple leak tests. Uses XS::APItest to make
4 # PL_sv_count available, allowing us to run a bit of code multiple times and
5 # see if the count increases.
12 eval { require XS::APItest; XS::APItest->import('sv_count'); 1 }
13 or skip_all("XS::APItest not available");
20 # run some code N times. If the number of SVs at the end of loop N is
21 # greater than (N-1)*delta at the end of loop 1, we've got a leak
24 my ($n, $delta, $code, @rest) = @_;
30 $sv0 = $sv1 if $i == 1;
32 cmp_ok($sv1-$sv0, '<=', ($n-1)*$delta, @rest);
35 # run some expression N times. The expr is concatenated N times and then
36 # evaled, ensuring that that there are no scope exits between executions.
37 # If the number of SVs at the end of expr N is greater than (N-1)*delta at
38 # the end of expr 1, we've got a leak
41 my ($n, $delta, $expr, @rest) = @_;
44 my $true = 1; # avoid stuff being optimised away
45 my $code1 = "($expr || \$true)";
46 my $code = "$code1 && (\$sv0 = sv_count())" . ("&& $code1" x 4)
47 . " && (\$sv1 = sv_count())";
49 cmp_ok($sv1-$sv0, '<=', ($n-1)*$delta, @rest);
52 fail("eval @rest: $@");
59 leak(5, 0, sub {}, "basic check 1 of leak test infrastructure");
60 leak(5, 0, sub {push @a,1;pop @a}, "basic check 2 of leak test infrastructure");
61 leak(5, 1, sub {push @a,1;}, "basic check 3 of leak test infrastructure");
63 sub TIEARRAY { bless [], $_[0] }
64 sub FETCH { $_[0]->[$_[1]] }
65 sub STORE { $_[0]->[$_[1]] = $_[2] }
67 # local $tied_elem[..] leaks <20020502143736.N16831@dansat.data-plan.com>"
70 leak(5, 0, sub {local $a[0]}, "local \$tied[0]");
73 # [perl #74484] repeated tries leaked SVs on the tmps stack
75 leak_expr(5, 0, q{"YYYYYa" =~ /.+?(a(.+?)|b)/ }, "trie leak");
77 # [perl #48004] map/grep didn't free tmps till the end
80 # qr/1/ just creates tmps that are hopefully freed per iteration
84 my @count = (0) x 4; # pre-allocate
86 grep qr/1/ && ($count[$_] = sv_count()) && 99, 0..3;
87 is(@count[3] - @count[0], 0, "void grep expr: no new tmps per iter");
88 grep { qr/1/ && ($count[$_] = sv_count()) && 99 } 0..3;
89 is(@count[3] - @count[0], 0, "void grep block: no new tmps per iter");
91 $s = grep qr/1/ && ($count[$_] = sv_count()) && 99, 0..3;
92 is(@count[3] - @count[0], 0, "scalar grep expr: no new tmps per iter");
93 $s = grep { qr/1/ && ($count[$_] = sv_count()) && 99 } 0..3;
94 is(@count[3] - @count[0], 0, "scalar grep block: no new tmps per iter");
96 @a = grep qr/1/ && ($count[$_] = sv_count()) && 99, 0..3;
97 is(@count[3] - @count[0], 0, "list grep expr: no new tmps per iter");
98 @a = grep { qr/1/ && ($count[$_] = sv_count()) && 99 } 0..3;
99 is(@count[3] - @count[0], 0, "list grep block: no new tmps per iter");
102 map qr/1/ && ($count[$_] = sv_count()) && 99, 0..3;
103 is(@count[3] - @count[0], 0, "void map expr: no new tmps per iter");
104 map { qr/1/ && ($count[$_] = sv_count()) && 99 } 0..3;
105 is(@count[3] - @count[0], 0, "void map block: no new tmps per iter");
107 $s = map qr/1/ && ($count[$_] = sv_count()) && 99, 0..3;
108 is(@count[3] - @count[0], 0, "scalar map expr: no new tmps per iter");
109 $s = map { qr/1/ && ($count[$_] = sv_count()) && 99 } 0..3;
110 is(@count[3] - @count[0], 0, "scalar map block: no new tmps per iter");
112 @a = map qr/1/ && ($count[$_] = sv_count()) && 99, 0..3;
113 is(@count[3] - @count[0], 3, "list map expr: one new tmp per iter");
114 @a = map { qr/1/ && ($count[$_] = sv_count()) && 99 } 0..3;
115 is(@count[3] - @count[0], 3, "list map block: one new tmp per iter");
120 { # broken by 304474c3, fixed by cefd5c7c, but didn't seem to cause
121 # any other test failures
122 # base test case from ribasushi (Peter Rabbitson)
123 eval { require Scalar::Util; Scalar::Util->import("weaken"); 1; }
124 or skip "no weaken", 1;
129 my $out = { in => $in, in => undef }
131 ok(!$weak, "hash referenced weakened SV released");
134 # RT #72246: rcatline memory leak on bad $/
139 open CATLINE, '<', \$f;
140 local $/ = "\x{262E}";
141 my $str = "\x{2622}";
142 eval { $str .= <CATLINE> };
157 "<html><body></body></html>" =~ m/$RE/gcs;
161 }, "named regexp captures");
164 leak(2,0,sub { !$^V }, '[perl #109762] version object in boolean context');
167 # [perl #114356] run-time rexexp with unchanging pattern got
171 skip "disabled under -Dmad (eval leaks)" if $Config{mad};
172 leak(2, 0, sub { eval q{ my $x = "x"; "abc" =~ /$x/ for 1..5 } }, '#114356');
176 skip "disabled under -Dmad (eval leaks)", 5 if $Config{mad};
177 leak(2, 0, sub { eval '"${<<END}"
178 ' }, 'unterminated here-doc in quotes in multiline eval');
179 leak(2, 0, sub { eval '"${<<END
180 }"' }, 'unterminated here-doc in multiline quotes in eval');
181 leak(2, 0, sub { eval { do './op/svleak.pl' } },
182 'unterminated here-doc in file');
183 leak(2, 0, sub { eval 'tr/9-0//' }, 'tr/9-0//');
184 leak(2, 0, sub { eval 'tr/a-z-0//' }, 'tr/a-z-0//');
187 # [perl #114764] Attributes leak scalars
188 leak(2, 0, sub { eval 'my $x : shared' }, 'my $x :shared used to leak');
190 # Tied hash iteration was leaking if the hash was freed before itera-
193 sub TIEHASH { bless [] }
201 }, 'tied hash iteration does not leak');
203 package explosive_scalar {
204 sub TIESCALAR { my $self = shift; bless [undef, {@_}], $self }
205 sub FETCH { die 'FETCH' if $_[0][1]{FETCH}; $_[0][0] }
206 sub STORE { die 'STORE' if $_[0][1]{STORE}; $_[0][0] = $_[1] }
208 tie my $die_on_fetch, 'explosive_scalar', FETCH => 1;
210 # List assignment was leaking when assigning explosive scalars to
213 eval {%a = ($die_on_fetch, 0)}; # key
214 eval {%a = (0, $die_on_fetch)}; # value
215 eval {%a = ($die_on_fetch, $die_on_fetch)}; # both
216 }, 'hash assignment does not leak');
218 eval {@a = ($die_on_fetch)};
219 eval {($die_on_fetch, $b) = ($b, $die_on_fetch)};
221 tie $die_on_fetch, 'explosive_scalar', FETCH => 1;
222 }, 'array assignment does not leak');
226 sub TIEHASH { bless [] }
227 sub STORE { $_[0][0]{$_[1]} = $_[2] }
228 sub FETCH { die if $explosive; $_[0][0]{$_[1]} }
229 sub FIRSTKEY { keys %{$_[0][0]}; each %{$_[0][0]} }
230 sub NEXTKEY { each %{$_[0][0]} }
232 leak(2,!!$Config{mad}, sub {
235 $hhtie::explosive = 0;
238 $hhtie::explosive = 1;
242 }, 'hint-hash copying does not leak');
244 package explosive_array {
245 sub TIEARRAY { bless [[], {}], $_[0] }
246 sub FETCH { die if $_[0]->[1]{FETCH}; $_[0]->[0][$_[1]] }
247 sub FETCHSIZE { die if $_[0]->[1]{FETCHSIZE}; scalar @{ $_[0]->[0] } }
248 sub STORE { die if $_[0]->[1]{STORE}; $_[0]->[0][$_[1]] = $_[2] }
249 sub CLEAR { die if $_[0]->[1]{CLEAR}; @{$_[0]->[0]} = () }
250 sub EXTEND { die if $_[0]->[1]{EXTEND}; return }
251 sub explode { my $self = shift; $self->[1] = {@_} }
255 tie my @a, 'explosive_array';
256 tied(@a)->explode( STORE => 1 );
259 }, 'explosive array assignment does not leak');
263 eval { warn $die_on_fetch };
264 }, 'explosive warn argument');
267 my $foo = sub { return $die_on_fetch };
268 my $res = eval { $foo->() };
269 my @res = eval { $foo->() };
270 }, 'function returning explosive does not leak');
273 my $res = eval { {$die_on_fetch, 0} };
274 $res = eval { {0, $die_on_fetch} };
275 }, 'building anon hash with explosives does not leak');
279 eval { push @a, $die_on_fetch };
280 }, 'pushing exploding scalar does not leak');