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 # Like leak, but run a string eval instead.
36 # The code is used instead of the test name
37 # if the name is absent.
39 my ($n,$delta,$code,@rest) = @_;
40 leak $n, $delta, sub { eval $code },
44 # run some expression N times. The expr is concatenated N times and then
45 # evaled, ensuring that that there are no scope exits between executions.
46 # If the number of SVs at the end of expr N is greater than (N-1)*delta at
47 # the end of expr 1, we've got a leak
50 my ($n, $delta, $expr, @rest) = @_;
53 my $true = 1; # avoid stuff being optimised away
54 my $code1 = "($expr || \$true)";
55 my $code = "$code1 && (\$sv0 = sv_count())" . ("&& $code1" x 4)
56 . " && (\$sv1 = sv_count())";
58 cmp_ok($sv1-$sv0, '<=', ($n-1)*$delta, @rest);
61 fail("eval @rest: $@");
68 leak(5, 0, sub {}, "basic check 1 of leak test infrastructure");
69 leak(5, 0, sub {push @a,1;pop @a}, "basic check 2 of leak test infrastructure");
70 leak(5, 1, sub {push @a,1;}, "basic check 3 of leak test infrastructure");
73 my $f = "use warnings FATAL =>";
74 my $all = "$f 'all';";
75 eleak(2, 0, "$f 'deprecated'; qq|\\c\{|", 'qq|\c{| with fatal warnings');
76 eleak(2, 0, "$f 'syntax'; qq|\\c`|", 'qq|\c`| with fatal warnings');
77 eleak(2, 0, "$all /\$\\ /", '/$\ / with fatal warnings');
78 eleak(2, 0, "$all s//\\1/", 's//\1/ with fatal warnings');
79 eleak(2, 0, "$all qq|\\i|", 'qq|\i| with fatal warnings');
80 eleak(2, 0, "$f 'digit'; qq|\\o{9}|", 'qq|\o{9}| with fatal warnings');
81 eleak(2, 0, "$f 'misc'; sub foo{} sub foo:lvalue",
82 'ignored :lvalue with fatal warnings');
83 eleak(2, 0, "no warnings; use feature ':all'; $f 'misc';
84 my sub foo{} sub foo:lvalue",
85 'ignored mysub :lvalue with fatal warnings');
86 eleak(2, 0, "no warnings; use feature ':all'; $all
87 my sub foo{} sub foo:lvalue{}",
88 'fatal mysub redef warning');
89 eleak(2, 0, "$all sub foo{} sub foo{}", 'fatal sub redef warning');
90 eleak(2, 0, "$all *x=sub {}",
91 'fatal sub redef warning with sub-to-glob assignment');
92 eleak(2, 0, "$all *x=sub() {1}",
93 'fatal const sub redef warning with sub-to-glob assignment');
94 eleak(2, 0, "$all XS::APItest::newCONSTSUB(\\%main::=>name=>0=>1)",
95 'newCONSTSUB sub redefinition with fatal warnings');
96 eleak(2, 0, "$f 'misc'; my\$a,my\$a", 'double my with fatal warnings');
97 eleak(2, 0, "$f 'misc'; our\$a,our\$a", 'double our with fatal warnings');
98 eleak(2, 0, "$f 'closure';
99 sub foo { my \$x; format=\n\@\n\$x\n.\n} write; ",
100 'format closing over unavailable var with fatal warnings');
101 eleak(2, 0, "$all /(?{})?/ ", '(?{})? with fatal warnings');
102 eleak(2, 0, "$all /(?{})+/ ", '(?{})+ with fatal warnings');
103 eleak(2, 0, "$all /[\\i]/ ", 'invalid charclass escape with fatal warns');
104 eleak(2, 0, "$all /[:foo:]/ ", '/[:foo:]/ with fatal warnings');
105 eleak(2, 0, "$all /[a-\\d]/ ", '[a-\d] char class with fatal warnings');
106 eleak(2, 0, "$all v111111111111111111111111111111111111111111111111",
107 'vstring num overflow with fatal warnings');
109 eleak(2, 0, 'sub{<*>}');
111 eleak(2, 0, 'goto sub {}', 'goto &sub in eval');
112 eleak(2, 0, '() = sort { goto sub {} } 1,2', 'goto &sub in sort');
113 eleak(2, 0, '/(?{ goto sub {} })/', 'goto &sub in regexp');
115 sub TIEARRAY { bless [], $_[0] }
116 sub FETCH { $_[0]->[$_[1]] }
117 sub STORE { $_[0]->[$_[1]] = $_[2] }
119 # local $tied_elem[..] leaks <20020502143736.N16831@dansat.data-plan.com>"
122 leak(5, 0, sub {local $a[0]}, "local \$tied[0]");
127 eleak(2, 0, "BEGIN{overload::constant integer=>sub{}} 1,1,1,1,1,1,1,1,1,1",
128 '"too many errors" from constant overloading returning undef');
129 # getting this one to leak was complicated; we have to unset LOCALIZE_HH:
130 eleak(2, 0, 'BEGIN{overload::constant integer=>sub{}; $^H &= ~ 0x00020000}
131 1,1,1,1,1,1,1,1,1,1',
132 '"too many errors" from constant overloading with $^H sabotaged');
133 eleak(2, 0, "BEGIN{overload::constant integer=>sub{}; undef %^H}
134 1,1,1,1,1,1,1,1,1,1",
135 '"too many errors" from constant overloading with %^H undefined');
138 # [perl #74484] repeated tries leaked SVs on the tmps stack
140 leak_expr(5, 0, q{"YYYYYa" =~ /.+?(a(.+?)|b)/ }, "trie leak");
142 # [perl #48004] map/grep didn't free tmps till the end
145 # qr/1/ just creates tmps that are hopefully freed per iteration
149 my @count = (0) x 4; # pre-allocate
151 grep qr/1/ && ($count[$_] = sv_count()) && 99, 0..3;
152 is(@count[3] - @count[0], 0, "void grep expr: no new tmps per iter");
153 grep { qr/1/ && ($count[$_] = sv_count()) && 99 } 0..3;
154 is(@count[3] - @count[0], 0, "void grep block: no new tmps per iter");
156 $s = grep qr/1/ && ($count[$_] = sv_count()) && 99, 0..3;
157 is(@count[3] - @count[0], 0, "scalar grep expr: no new tmps per iter");
158 $s = grep { qr/1/ && ($count[$_] = sv_count()) && 99 } 0..3;
159 is(@count[3] - @count[0], 0, "scalar grep block: no new tmps per iter");
161 @a = grep qr/1/ && ($count[$_] = sv_count()) && 99, 0..3;
162 is(@count[3] - @count[0], 0, "list grep expr: no new tmps per iter");
163 @a = grep { qr/1/ && ($count[$_] = sv_count()) && 99 } 0..3;
164 is(@count[3] - @count[0], 0, "list grep block: no new tmps per iter");
167 map qr/1/ && ($count[$_] = sv_count()) && 99, 0..3;
168 is(@count[3] - @count[0], 0, "void map expr: no new tmps per iter");
169 map { qr/1/ && ($count[$_] = sv_count()) && 99 } 0..3;
170 is(@count[3] - @count[0], 0, "void map block: no new tmps per iter");
172 $s = map qr/1/ && ($count[$_] = sv_count()) && 99, 0..3;
173 is(@count[3] - @count[0], 0, "scalar map expr: no new tmps per iter");
174 $s = map { qr/1/ && ($count[$_] = sv_count()) && 99 } 0..3;
175 is(@count[3] - @count[0], 0, "scalar map block: no new tmps per iter");
177 @a = map qr/1/ && ($count[$_] = sv_count()) && 99, 0..3;
178 is(@count[3] - @count[0], 3, "list map expr: one new tmp per iter");
179 @a = map { qr/1/ && ($count[$_] = sv_count()) && 99 } 0..3;
180 is(@count[3] - @count[0], 3, "list map block: one new tmp per iter");
185 { # broken by 304474c3, fixed by cefd5c7c, but didn't seem to cause
186 # any other test failures
187 # base test case from ribasushi (Peter Rabbitson)
188 eval { require Scalar::Util; Scalar::Util->import("weaken"); 1; }
189 or skip "no weaken", 1;
194 my $out = { in => $in, in => undef }
196 ok(!$weak, "hash referenced weakened SV released");
200 leak(2,0, sub { eval { prototype "CORE::fu" } }, 'prototype errors');
202 # RT #72246: rcatline memory leak on bad $/
207 open CATLINE, '<', \$f;
208 local $/ = "\x{262E}";
209 my $str = "\x{2622}";
210 eval { $str .= <CATLINE> };
225 "<html><body></body></html>" =~ m/$RE/gcs;
229 }, "named regexp captures");
233 eleak(2,0,'/[\xdf]/i');
234 eleak(2,0,'s![^/]!!');
236 eleak(2,0,'/[[:ascii:]]/');
237 eleak(2,0,'/[[.zog.]]/');
238 eleak(2,0,'/[.zog.]/');
239 eleak(2,0,'chr(0x100) =~ /[[:punct:]]/');
240 eleak(2,0,'chr(0x100) =~ /[[:^punct:]]/');
241 eleak(2,0,'chr(0x100) =~ /\P{Assigned}/');
242 leak(2,0,sub { /(??{})/ }, '/(??{})/');
244 leak(2,0,sub { !$^V }, '[perl #109762] version object in boolean context');
247 # [perl #114356] run-time rexexp with unchanging pattern got
249 eleak(2, 0, q{ my $x = "x"; "abc" =~ /$x/ for 1..5 }, '#114356');
251 eleak(2, 0, 'sub', '"sub" with nothing following');
252 eleak(2, 0, '+sub:a{}', 'anon subs with invalid attributes');
253 eleak(2, 0, 'no warnings; sub a{1 1}', 'sub with syntax error');
254 eleak(2, 0, 'no warnings; sub {1 1}', 'anon sub with syntax error');
255 eleak(2, 0, 'no warnings; use feature ":all"; my sub a{1 1}',
256 'my sub with syntax error');
258 # Reification (or lack thereof)
259 leak(2, 0, sub { sub { local $_[0]; shift }->(1) },
260 'local $_[0] on surreal @_, followed by shift');
261 leak(2, 0, sub { sub { local $_[0]; \@_ }->(1) },
262 'local $_[0] on surreal @_, followed by reification');
265 eleak(2, 0, '"${<<END}"
266 ', 'unterminated here-doc in quotes in multiline eval');
267 eleak(2, 0, '"${<<END
268 }"', 'unterminated here-doc in multiline quotes in eval');
269 leak(2, 0, sub { eval { do './op/svleak.pl' } },
270 'unterminated here-doc in file');
271 eleak(2, 0, 'tr/9-0//');
272 eleak(2, 0, 'tr/a-z-0//');
273 eleak(2, 0, 'no warnings; nonexistent_function 33838',
274 'bareword followed by number');
275 eleak(2, 0, '//dd;'x20, '"too many errors" when parsing m// flags');
276 eleak(2, 0, 's///dd;'x20, '"too many errors" when parsing s/// flags');
277 eleak(2, 0, 'no warnings; 2 2;BEGIN{}',
278 'BEGIN block after syntax error');
280 local %INC; # in case Errno is already loaded
281 eleak(2, 0, 'no warnings; 2@!{',
282 'implicit "use Errno" after syntax error');
284 eleak(2, 0, "\"\$\0\356\"", 'qq containing $ <null> something');
285 eleak(2, 0, 'END OF TERMS AND CONDITIONS', 'END followed by words');
286 eleak(2, 0, "+ + +;qq|\\N{a}|"x10,'qq"\N{a}" after errors');
287 eleak(2, 0, "qq|\\c|;"x10, '"too many errors" from qq"\c"');
288 $::TODO = 'still leaks';
289 eleak(2, 0, "qq|\\N{%}|"x10, '"too many errors" from qq"\N{%}"');
291 eleak(2, 0, "qq|\\o|;"x10, '"too many errors" from qq"\o"');
292 eleak(2, 0, "qq|\\x{|;"x10, '"too many errors" from qq"\x{"');
293 eleak(2, 0, "qq|\\N|;"x10, '"too many errors" from qq"\N"');
294 eleak(2, 0, "qq|\\N{|;"x10, '"too many errors" from qq"\N{"');
295 eleak(2, 0, "qq|\\N{U+GETG}|;"x10,'"too many errors" from qq"\N{U+JUNK}"');
296 $::TODO = 'still leaks';
297 eleak(2, 0, "qq|\\N{au}|;"x10, '"too many errors" from qq"\N{invalid}"');
301 # [perl #114764] Attributes leak scalars
302 leak(2, 0, sub { eval 'my $x : shared' }, 'my $x :shared used to leak');
304 eleak(2, 0, 'ref: 1', 'labels');
306 # Tied hash iteration was leaking if the hash was freed before itera-
309 sub TIEHASH { bless [] }
317 }, 'tied hash iteration does not leak');
319 package explosive_scalar {
320 sub TIESCALAR { my $self = shift; bless [undef, {@_}], $self }
321 sub FETCH { die 'FETCH' if $_[0][1]{FETCH}; $_[0][0] }
322 sub STORE { die 'STORE' if $_[0][1]{STORE}; $_[0][0] = $_[1] }
324 tie my $die_on_fetch, 'explosive_scalar', FETCH => 1;
326 # List assignment was leaking when assigning explosive scalars to
329 eval {%a = ($die_on_fetch, 0)}; # key
330 eval {%a = (0, $die_on_fetch)}; # value
331 eval {%a = ($die_on_fetch, $die_on_fetch)}; # both
332 }, 'hash assignment does not leak');
334 eval {@a = ($die_on_fetch)};
335 eval {($die_on_fetch, $b) = ($b, $die_on_fetch)};
337 tie $die_on_fetch, 'explosive_scalar', FETCH => 1;
338 }, 'array assignment does not leak');
342 sub TIEHASH { bless [] }
343 sub STORE { $_[0][0]{$_[1]} = $_[2] }
344 sub FETCH { die if $explosive; $_[0][0]{$_[1]} }
345 sub FIRSTKEY { keys %{$_[0][0]}; each %{$_[0][0]} }
346 sub NEXTKEY { each %{$_[0][0]} }
351 $hhtie::explosive = 0;
354 $hhtie::explosive = 1;
358 }, 'hint-hash copying does not leak');
360 package explosive_array {
361 sub TIEARRAY { bless [[], {}], $_[0] }
362 sub FETCH { die if $_[0]->[1]{FETCH}; $_[0]->[0][$_[1]] }
363 sub FETCHSIZE { die if $_[0]->[1]{FETCHSIZE}; scalar @{ $_[0]->[0] } }
364 sub STORE { die if $_[0]->[1]{STORE}; $_[0]->[0][$_[1]] = $_[2] }
365 sub CLEAR { die if $_[0]->[1]{CLEAR}; @{$_[0]->[0]} = () }
366 sub EXTEND { die if $_[0]->[1]{EXTEND}; return }
367 sub explode { my $self = shift; $self->[1] = {@_} }
371 tie my @a, 'explosive_array';
372 tied(@a)->explode( STORE => 1 );
375 }, 'explosive array assignment does not leak');
379 eval { warn $die_on_fetch };
380 }, 'explosive warn argument');
383 my $foo = sub { return $die_on_fetch };
384 my $res = eval { $foo->() };
385 my @res = eval { $foo->() };
386 }, 'function returning explosive does not leak');
389 my $res = eval { {$die_on_fetch, 0} };
390 $res = eval { {0, $die_on_fetch} };
391 }, 'building anon hash with explosives does not leak');
394 my $res = eval { [$die_on_fetch] };
395 }, 'building anon array with explosives does not leak');
399 eval { push @a, $die_on_fetch };
400 }, 'pushing exploding scalar does not leak');
403 eval { push @-, '' };
404 }, 'pushing onto read-only array does not leak');
407 # Run-time regexp code blocks
410 my @tests = ('[(?{})]','(?{})');
414 }, "/ \$x/ where \$x is $t does not leak");
417 }, "/(?{})\$x/ where \$x is $t does not leak");
423 use warnings FATAL => 'all';
426 eval { printf uNopened 42 };
427 }, 'printfing to bad handle under fatal warnings does not leak');
428 open my $fh, ">", \my $buf;
430 eval { printf $fh chr 2455 };
431 }, 'wide fatal warning does not make printf leak');
436 leak(2,0,sub{eval{require untohunothu}}, 'requiring nonexistent module');