1#!./perl 2 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. 6 7BEGIN { 8 chdir 't'; 9 @INC = '../lib'; 10 require './test.pl'; 11 12 eval { require XS::APItest; XS::APItest->import('sv_count'); 1 } 13 or skip_all("XS::APItest not available"); 14} 15 16use Config; 17 18plan tests => 128; 19 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 22# 23sub leak { 24 my ($n, $delta, $code, @rest) = @_; 25 my $sv0 = 0; 26 my $sv1 = 0; 27 for my $i (1..$n) { 28 &$code(); 29 $sv1 = sv_count(); 30 $sv0 = $sv1 if $i == 1; 31 } 32 cmp_ok($sv1-$sv0, '<=', ($n-1)*$delta, @rest); 33} 34 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. 38sub eleak { 39 my ($n,$delta,$code,@rest) = @_; 40 no warnings 'deprecated'; # Silence the literal control character warning 41 leak $n, $delta, sub { eval $code }, 42 @rest ? @rest : $code 43} 44 45# run some expression N times. The expr is concatenated N times and then 46# evaled, ensuring that that there are no scope exits between executions. 47# If the number of SVs at the end of expr N is greater than (N-1)*delta at 48# the end of expr 1, we've got a leak 49# 50sub leak_expr { 51 my ($n, $delta, $expr, @rest) = @_; 52 my $sv0 = 0; 53 my $sv1 = 0; 54 my $true = 1; # avoid stuff being optimised away 55 my $code1 = "($expr || \$true)"; 56 my $code = "$code1 && (\$sv0 = sv_count())" . ("&& $code1" x 4) 57 . " && (\$sv1 = sv_count())"; 58 if (eval $code) { 59 cmp_ok($sv1-$sv0, '<=', ($n-1)*$delta, @rest); 60 } 61 else { 62 fail("eval @rest: $@"); 63 } 64} 65 66 67my @a; 68 69leak(5, 0, sub {}, "basic check 1 of leak test infrastructure"); 70leak(5, 0, sub {push @a,1;pop @a}, "basic check 2 of leak test infrastructure"); 71leak(5, 1, sub {push @a,1;}, "basic check 3 of leak test infrastructure"); 72 73# delete 74{ 75 my $key = "foo"; 76 $key++ while exists $ENV{$key}; 77 leak(2, 0, sub { delete local $ENV{$key} }, 78 'delete local on nonexistent env var'); 79} 80 81# Fatal warnings 82my $f = "use warnings FATAL =>"; 83my $all = "$f 'all';"; 84eleak(2, 0, "$f 'deprecated'; qq|\\c\{|", 'qq|\c{| with fatal warnings'); 85eleak(2, 0, "$f 'syntax'; qq|\\c`|", 'qq|\c`| with fatal warnings'); 86eleak(2, 0, "$all /\$\\ /", '/$\ / with fatal warnings'); 87eleak(2, 0, "$all s//\\1/", 's//\1/ with fatal warnings'); 88eleak(2, 0, "$all qq|\\i|", 'qq|\i| with fatal warnings'); 89eleak(2, 0, "$f 'digit'; qq|\\o{9}|", 'qq|\o{9}| with fatal warnings'); 90eleak(3, 1, "$f 'misc'; sub foo{} sub foo:lvalue", 91 'ignored :lvalue with fatal warnings'); 92eleak(2, 0, "no warnings; use feature ':all'; $f 'misc'; 93 my sub foo{} sub foo:lvalue", 94 'ignored mysub :lvalue with fatal warnings'); 95eleak(2, 0, "no warnings; use feature ':all'; $all 96 my sub foo{} sub foo:lvalue{}", 97 'fatal mysub redef warning'); 98eleak(2, 0, "$all sub foo{} sub foo{}", 'fatal sub redef warning'); 99eleak(2, 0, "$all *x=sub {}", 100 'fatal sub redef warning with sub-to-glob assignment'); 101eleak(2, 0, "$all *x=sub() {1}", 102 'fatal const sub redef warning with sub-to-glob assignment'); 103eleak(2, 0, "$all XS::APItest::newCONSTSUB(\\%main::=>name=>0=>1)", 104 'newCONSTSUB sub redefinition with fatal warnings'); 105eleak(2, 0, "$f 'misc'; my\$a,my\$a", 'double my with fatal warnings'); 106eleak(2, 0, "$f 'misc'; our\$a,our\$a", 'double our with fatal warnings'); 107eleak(2, 0, "$f 'closure'; 108 sub foo { my \$x; format=\n\@\n\$x\n.\n} write; ", 109 'format closing over unavailable var with fatal warnings'); 110eleak(2, 0, "$all /(?{})?/ ", '(?{})? with fatal warnings'); 111eleak(2, 0, "$all /(?{})+/ ", '(?{})+ with fatal warnings'); 112eleak(2, 0, "$all /[\\i]/ ", 'invalid charclass escape with fatal warns'); 113eleak(2, 0, "$all /[:foo:]/ ", '/[:foo:]/ with fatal warnings'); 114eleak(2, 0, "$all /[a-\\d]/ ", '[a-\d] char class with fatal warnings'); 115eleak(2, 0, "$all v111111111111111111111111111111111111111111111111", 116 'vstring num overflow with fatal warnings'); 117 118eleak(2, 0, 'sub{<*>}'); 119# Use a random number of ops, so that the glob op does not reuse the same 120# address each time, giving us false passes. 121leak(2, 0, sub { eval '$x+'x(1 + rand() * 100) . '<*>'; }, 122 'freeing partly iterated glob'); 123 124eleak(2, 0, 'goto sub {}', 'goto &sub in eval'); 125eleak(2, 0, '() = sort { goto sub {} } 1,2', 'goto &sub in sort'); 126eleak(2, 0, '/(?{ goto sub {} })/', 'goto &sub in regexp'); 127 128sub TIEARRAY { bless [], $_[0] } 129sub FETCH { $_[0]->[$_[1]] } 130sub STORE { $_[0]->[$_[1]] = $_[2] } 131 132# local $tied_elem[..] leaks <20020502143736.N16831@dansat.data-plan.com>" 133{ 134 tie my @a, 'main'; 135 leak(5, 0, sub {local $a[0]}, "local \$tied[0]"); 136} 137 138# Overloading 139require overload; 140eleak(2, 0, "BEGIN{overload::constant integer=>sub{}} 1,1,1,1,1,1,1,1,1,1", 141 '"too many errors" from constant overloading returning undef'); 142# getting this one to leak was complicated; we have to unset LOCALIZE_HH: 143eleak(2, 0, 'BEGIN{overload::constant integer=>sub{}; $^H &= ~ 0x00020000} 144 1,1,1,1,1,1,1,1,1,1', 145 '"too many errors" from constant overloading with $^H sabotaged'); 146eleak(2, 0, "BEGIN{overload::constant integer=>sub{}; undef %^H} 147 1,1,1,1,1,1,1,1,1,1", 148 '"too many errors" from constant overloading with %^H undefined'); 149 150 151# [perl #74484] repeated tries leaked SVs on the tmps stack 152 153leak_expr(5, 0, q{"YYYYYa" =~ /.+?(a(.+?)|b)/ }, "trie leak"); 154 155# [perl #48004] map/grep didn't free tmps till the end 156 157{ 158 # qr/1/ just creates tmps that are hopefully freed per iteration 159 160 my $s; 161 my @a; 162 my @count = (0) x 4; # pre-allocate 163 # Using 0..3 with constant endpoints will cause an erroneous test fail- 164 # ure, as the SV in the op tree needs to be copied (to protect it), 165 # but copying happens *during iteration*, causing the number of SVs to 166 # go up. Using a variable (0..$_3) will cause evaluation of the range 167 # operator at run time, not compile time, so the values will already be 168 # on the stack before grep starts. 169 my $_3 = 3; 170 171 grep qr/1/ && ($count[$_] = sv_count()) && 99, 0..$_3; 172 is(@count[3] - @count[0], 0, "void grep expr: no new tmps per iter"); 173 grep { qr/1/ && ($count[$_] = sv_count()) && 99 } 0..$_3; 174 is(@count[3] - @count[0], 0, "void grep block: no new tmps per iter"); 175 176 $s = grep qr/1/ && ($count[$_] = sv_count()) && 99, 0..$_3; 177 is(@count[3] - @count[0], 0, "scalar grep expr: no new tmps per iter"); 178 $s = grep { qr/1/ && ($count[$_] = sv_count()) && 99 } 0..$_3; 179 is(@count[3] - @count[0], 0, "scalar grep block: no new tmps per iter"); 180 181 @a = grep qr/1/ && ($count[$_] = sv_count()) && 99, 0..$_3; 182 is(@count[3] - @count[0], 0, "list grep expr: no new tmps per iter"); 183 @a = grep { qr/1/ && ($count[$_] = sv_count()) && 99 } 0..$_3; 184 is(@count[3] - @count[0], 0, "list grep block: no new tmps per iter"); 185 186 187 map qr/1/ && ($count[$_] = sv_count()) && 99, 0..$_3; 188 is(@count[3] - @count[0], 0, "void map expr: no new tmps per iter"); 189 map { qr/1/ && ($count[$_] = sv_count()) && 99 } 0..$_3; 190 is(@count[3] - @count[0], 0, "void map block: no new tmps per iter"); 191 192 $s = map qr/1/ && ($count[$_] = sv_count()) && 99, 0..$_3; 193 is(@count[3] - @count[0], 0, "scalar map expr: no new tmps per iter"); 194 $s = map { qr/1/ && ($count[$_] = sv_count()) && 99 } 0..$_3; 195 is(@count[3] - @count[0], 0, "scalar map block: no new tmps per iter"); 196 197 @a = map qr/1/ && ($count[$_] = sv_count()) && 99, 0..$_3; 198 is(@count[3] - @count[0], 3, "list map expr: one new tmp per iter"); 199 @a = map { qr/1/ && ($count[$_] = sv_count()) && 99 } 0..$_3; 200 is(@count[3] - @count[0], 3, "list map block: one new tmp per iter"); 201 202} 203 204SKIP: 205{ # broken by 304474c3, fixed by cefd5c7c, but didn't seem to cause 206 # any other test failures 207 # base test case from ribasushi (Peter Rabbitson) 208 eval { require Scalar::Util; Scalar::Util->import("weaken"); 1; } 209 or skip "no weaken", 1; 210 my $weak; 211 { 212 $weak = my $in = {}; 213 weaken($weak); 214 my $out = { in => $in, in => undef } 215 } 216 ok(!$weak, "hash referenced weakened SV released"); 217} 218 219# prototype() errors 220leak(2,0, sub { eval { prototype "CORE::fu" } }, 'prototype errors'); 221 222# RT #72246: rcatline memory leak on bad $/ 223 224leak(2, 0, 225 sub { 226 my $f; 227 open CATLINE, '<', \$f; 228 local $/ = "\x{262E}"; 229 my $str = "\x{2622}"; 230 eval { $str .= <CATLINE> }; 231 }, 232 "rcatline leak" 233); 234 235{ 236 my $RE = qr/ 237 (?: 238 <(?<tag> 239 \s* 240 [^>\s]+ 241 )> 242 )?? 243 /xis; 244 245 "<html><body></body></html>" =~ m/$RE/gcs; 246 247 leak(5, 0, sub { 248 my $tag = $+{tag}; 249 }, "named regexp captures"); 250} 251 252eleak(2,0,'/[:]/'); 253eleak(2,0,'/[\xdf]/i'); 254eleak(2,0,'s![^/]!!'); 255eleak(2,0,'/[pp]/'); 256eleak(2,0,'/[[:ascii:]]/'); 257eleak(2,0,'/[[.zog.]]/'); 258eleak(2,0,'/[.zog.]/'); 259eleak(2,0,'/|\W/', '/|\W/ [perl #123198]'); 260eleak(2,0,'no warnings; /(?[])/'); 261eleak(2,0,'no warnings; /(?[[a]+[b]])/'); 262eleak(2,0,'no warnings; /(?[[a]-[b]])/'); 263eleak(2,0,'no warnings; /(?[[a]&[b]])/'); 264eleak(2,0,'no warnings; /(?[[a]|[b]])/'); 265eleak(2,0,'no warnings; /(?[[a]^[b]])/'); 266eleak(2,0,'no warnings; /(?[![a]])/'); 267eleak(2,0,'no warnings; /(?[\p{Word}])/'); 268eleak(2,0,'no warnings; /(?[[a]+)])/'); 269eleak(2,0,'no warnings; /(?[\d\d)])/'); 270 271# These can generate one ref count, but just once. 272eleak(4,1,'chr(0x100) =~ /[[:punct:]]/'); 273eleak(4,1,'chr(0x100) =~ /[[:^punct:]]/'); 274eleak(4,1,'chr(0x100) =~ /[[:word:]]/'); 275eleak(4,1,'chr(0x100) =~ /[[:^word:]]/'); 276 277eleak(2,0,'chr(0x100) =~ /\P{Assigned}/'); 278leak(2,0,sub { /(??{})/ }, '/(??{})/'); 279 280leak(2,0,sub { !$^V }, '[perl #109762] version object in boolean context'); 281 282 283# [perl #114356] run-time rexexp with unchanging pattern got 284# inflated refcounts 285eleak(2, 0, q{ my $x = "x"; "abc" =~ /$x/ for 1..5 }, '#114356'); 286 287eleak(2, 0, 'sub', '"sub" with nothing following'); 288eleak(2, 0, '+sub:a{}', 'anon subs with invalid attributes'); 289eleak(2, 0, 'no warnings; sub a{1 1}', 'sub with syntax error'); 290eleak(2, 0, 'no warnings; sub {1 1}', 'anon sub with syntax error'); 291eleak(2, 0, 'no warnings; use feature ":all"; my sub a{1 1}', 292 'my sub with syntax error'); 293 294# Reification (or lack thereof) 295leak(2, 0, sub { sub { local $_[0]; shift }->(1) }, 296 'local $_[0] on surreal @_, followed by shift'); 297leak(2, 0, sub { sub { local $_[0]; \@_ }->(1) }, 298 'local $_[0] on surreal @_, followed by reification'); 299 300sub recredef {} 301sub Recursive::Redefinition::DESTROY { 302 *recredef = sub { CORE::state $x } # state makes it cloneable 303} 304leak(2, 0, sub { 305 bless \&recredef, "Recursive::Redefinition"; eval "sub recredef{}" 306}, 'recursive sub redefinition'); 307 308# Syntax errors 309eleak(2, 0, '"${<<END}" 310 ', 'unterminated here-doc in quotes in multiline eval'); 311eleak(2, 0, '"${<<END 312 }"', 'unterminated here-doc in multiline quotes in eval'); 313leak(2, 0, sub { eval { do './op/svleak.pl' } }, 314 'unterminated here-doc in file'); 315eleak(2, 0, 'tr/9-0//'); 316eleak(2, 0, 'tr/a-z-0//'); 317eleak(2, 0, 'no warnings; nonexistent_function 33838', 318 'bareword followed by number'); 319eleak(2, 0, '//dd;'x20, '"too many errors" when parsing m// flags'); 320eleak(2, 0, 's///dd;'x20, '"too many errors" when parsing s/// flags'); 321eleak(2, 0, 'no warnings; 2 2;BEGIN{}', 322 'BEGIN block after syntax error'); 323{ 324 local %INC; # in case Errno is already loaded 325 eleak(2, 0, 'no warnings; 2@!{', 326 'implicit "use Errno" after syntax error'); 327} 328eleak(2, 0, "\"\$\0\356\"", 'qq containing $ <null> something'); 329eleak(2, 0, 'END OF TERMS AND CONDITIONS', 'END followed by words'); 330eleak(2, 0, "+ + +;qq|\\N{a}|"x10,'qq"\N{a}" after errors'); 331eleak(2, 0, "qq|\\N{%}|", 'qq"\N{%}" (invalid charname)'); 332eleak(2, 0, "qq|\\N{au}|;", 'qq"\N{invalid}"'); 333eleak(2, 0, "qq|\\c|;"x10, '"too many errors" from qq"\c"'); 334eleak(2, 0, "qq|\\o|;"x10, '"too many errors" from qq"\o"'); 335eleak(2, 0, "qq|\\x{|;"x10, '"too many errors" from qq"\x{"'); 336eleak(2, 0, "qq|\\N|;"x10, '"too many errors" from qq"\N"'); 337eleak(2, 0, "qq|\\N{|;"x10, '"too many errors" from qq"\N{"'); 338eleak(2, 0, "qq|\\N{U+GETG}|;"x10,'"too many errors" from qq"\N{U+JUNK}"'); 339 340 341# [perl #114764] Attributes leak scalars 342leak(2, 0, sub { eval 'my $x : shared' }, 'my $x :shared used to leak'); 343 344eleak(2, 0, 'ref: 1', 'labels'); 345 346# Tied hash iteration was leaking if the hash was freed before itera- 347# tion was over. 348package t { 349 sub TIEHASH { bless [] } 350 sub FIRSTKEY { 0 } 351} 352leak(2, 0, sub { 353 my $h = {}; 354 tie %$h, t; 355 each %$h; 356 undef $h; 357}, 'tied hash iteration does not leak'); 358 359package explosive_scalar { 360 sub TIESCALAR { my $self = shift; bless [undef, {@_}], $self } 361 sub FETCH { die 'FETCH' if $_[0][1]{FETCH}; $_[0][0] } 362 sub STORE { die 'STORE' if $_[0][1]{STORE}; $_[0][0] = $_[1] } 363} 364tie my $die_on_fetch, 'explosive_scalar', FETCH => 1; 365 366# List assignment was leaking when assigning explosive scalars to 367# aggregates. 368leak(2, 0, sub { 369 eval {%a = ($die_on_fetch, 0)}; # key 370 eval {%a = (0, $die_on_fetch)}; # value 371 eval {%a = ($die_on_fetch, $die_on_fetch)}; # both 372 eval {%a = ($die_on_fetch)}; # key, odd elements 373}, 'hash assignment does not leak'); 374leak(2, 0, sub { 375 eval {@a = ($die_on_fetch)}; 376 eval {($die_on_fetch, $b) = ($b, $die_on_fetch)}; 377 # restore 378 tie $die_on_fetch, 'explosive_scalar', FETCH => 1; 379}, 'array assignment does not leak'); 380 381# [perl #107000] 382package hhtie { 383 sub TIEHASH { bless [] } 384 sub STORE { $_[0][0]{$_[1]} = $_[2] } 385 sub FETCH { die if $explosive; $_[0][0]{$_[1]} } 386 sub FIRSTKEY { keys %{$_[0][0]}; each %{$_[0][0]} } 387 sub NEXTKEY { each %{$_[0][0]} } 388} 389leak(2, 0, sub { 390 eval q` 391 BEGIN { 392 $hhtie::explosive = 0; 393 tie %^H, hhtie; 394 $^H{foo} = bar; 395 $hhtie::explosive = 1; 396 } 397 { 1; } 398 `; 399}, 'hint-hash copying does not leak'); 400 401package explosive_array { 402 sub TIEARRAY { bless [[], {}], $_[0] } 403 sub FETCH { die if $_[0]->[1]{FETCH}; $_[0]->[0][$_[1]] } 404 sub FETCHSIZE { die if $_[0]->[1]{FETCHSIZE}; scalar @{ $_[0]->[0] } } 405 sub STORE { die if $_[0]->[1]{STORE}; $_[0]->[0][$_[1]] = $_[2] } 406 sub CLEAR { die if $_[0]->[1]{CLEAR}; @{$_[0]->[0]} = () } 407 sub EXTEND { die if $_[0]->[1]{EXTEND}; return } 408 sub explode { my $self = shift; $self->[1] = {@_} } 409} 410 411leak(2, 0, sub { 412 tie my @a, 'explosive_array'; 413 tied(@a)->explode( STORE => 1 ); 414 my $x = 0; 415 eval { @a = ($x) }; 416}, 'explosive array assignment does not leak'); 417 418leak(2, 0, sub { 419 my ($a, $b); 420 eval { warn $die_on_fetch }; 421}, 'explosive warn argument'); 422 423leak(2, 0, sub { 424 my $foo = sub { return $die_on_fetch }; 425 my $res = eval { $foo->() }; 426 my @res = eval { $foo->() }; 427}, 'function returning explosive does not leak'); 428 429leak(2, 0, sub { 430 my $res = eval { {$die_on_fetch, 0} }; 431 $res = eval { {0, $die_on_fetch} }; 432}, 'building anon hash with explosives does not leak'); 433 434leak(2, 0, sub { 435 my $res = eval { [$die_on_fetch] }; 436}, 'building anon array with explosives does not leak'); 437 438leak(2, 0, sub { 439 my @a; 440 eval { push @a, $die_on_fetch }; 441}, 'pushing exploding scalar does not leak'); 442 443leak(2, 0, sub { 444 eval { push @-, '' }; 445}, 'pushing onto read-only array does not leak'); 446 447 448# Run-time regexp code blocks 449{ 450 use re 'eval'; 451 my @tests = ('[(?{})]','(?{})'); 452 for my $t (@tests) { 453 leak(2, 0, sub { 454 / $t/; 455 }, "/ \$x/ where \$x is $t does not leak"); 456 leak(2, 0, sub { 457 /(?{})$t/; 458 }, "/(?{})\$x/ where \$x is $t does not leak"); 459 } 460} 461 462 463{ 464 use warnings FATAL => 'all'; 465 leak(2, 0, sub { 466 no warnings 'once'; 467 eval { printf uNopened 42 }; 468 }, 'printfing to bad handle under fatal warnings does not leak'); 469 open my $fh, ">", \my $buf; 470 leak(2, 0, sub { 471 eval { printf $fh chr 2455 }; 472 }, 'wide fatal warning does not make printf leak'); 473 close $fh or die $!; 474} 475 476 477leak(2,0,sub{eval{require untohunothu}}, 'requiring nonexistent module'); 478 479# [perl #120939] 480use constant const_av_xsub_leaked => 1 .. 3; 481leak(5, 0, sub { scalar &const_av_xsub_leaked }, "const_av_sub in scalar context"); 482