This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
Remove the Rhapsody port.
[perl5.git] / t / op / svleak.t
CommitLineData
d97c33b5
DM
1#!./perl
2
3# A place to put some simple leak tests. Uses XS::APItest to make
64dc9714 4# PL_sv_count available, allowing us to run a bit of code multiple times and
d97c33b5
DM
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
ef09a500
DM
16use Config;
17
7f5d1fb2 18plan tests => 114;
d97c33b5
DM
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
1cac5c33
FC
35# Like leak, but run a string eval instead.
36# The code is used instead of the test name
2e880be7
FC
37# if the name is absent.
38sub eleak {
39 my ($n,$delta,$code,@rest) = @_;
1cac5c33 40 leak $n, $delta, sub { eval $code },
2e880be7
FC
41 @rest ? @rest : $code
42}
43
8bd05d90
DM
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
48#
49sub leak_expr {
50 my ($n, $delta, $expr, @rest) = @_;
51 my $sv0 = 0;
52 my $sv1 = 0;
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())";
57 if (eval $code) {
58 cmp_ok($sv1-$sv0, '<=', ($n-1)*$delta, @rest);
59 }
60 else {
61 fail("eval @rest: $@");
62 }
63}
64
65
d97c33b5
DM
66my @a;
67
68leak(5, 0, sub {}, "basic check 1 of leak test infrastructure");
69leak(5, 0, sub {push @a,1;pop @a}, "basic check 2 of leak test infrastructure");
70leak(5, 1, sub {push @a,1;}, "basic check 3 of leak test infrastructure");
459defa1 71
38b1111c
FC
72# Fatal warnings
73my $f = "use warnings FATAL =>";
74my $all = "$f 'all';";
38b1111c
FC
75eleak(2, 0, "$f 'deprecated'; qq|\\c\{|", 'qq|\c{| with fatal warnings');
76eleak(2, 0, "$f 'syntax'; qq|\\c`|", 'qq|\c`| with fatal warnings');
77eleak(2, 0, "$all /\$\\ /", '/$\ / with fatal warnings');
78eleak(2, 0, "$all s//\\1/", 's//\1/ with fatal warnings');
79eleak(2, 0, "$all qq|\\i|", 'qq|\i| with fatal warnings');
80eleak(2, 0, "$f 'digit'; qq|\\o{9}|", 'qq|\o{9}| with fatal warnings');
81eleak(2, 0, "$f 'misc'; sub foo{} sub foo:lvalue",
82 'ignored :lvalue with fatal warnings');
83eleak(2, 0, "no warnings; use feature ':all'; $f 'misc';
84 my sub foo{} sub foo:lvalue",
85 'ignored mysub :lvalue with fatal warnings');
86eleak(2, 0, "no warnings; use feature ':all'; $all
87 my sub foo{} sub foo:lvalue{}",
88 'fatal mysub redef warning');
89eleak(2, 0, "$all sub foo{} sub foo{}", 'fatal sub redef warning');
90eleak(2, 0, "$all *x=sub {}",
91 'fatal sub redef warning with sub-to-glob assignment');
92eleak(2, 0, "$all *x=sub() {1}",
93 'fatal const sub redef warning with sub-to-glob assignment');
94eleak(2, 0, "$all XS::APItest::newCONSTSUB(\\%main::=>name=>0=>1)",
95934569 95 'newCONSTSUB sub redefinition with fatal warnings');
38b1111c
FC
96eleak(2, 0, "$f 'misc'; my\$a,my\$a", 'double my with fatal warnings');
97eleak(2, 0, "$f 'misc'; our\$a,our\$a", 'double our with fatal warnings');
98eleak(2, 0, "$f 'closure';
99 sub foo { my \$x; format=\n\@\n\$x\n.\n} write; ",
af32a010 100 'format closing over unavailable var with fatal warnings');
38b1111c
FC
101eleak(2, 0, "$all /(?{})?/ ", '(?{})? with fatal warnings');
102eleak(2, 0, "$all /(?{})+/ ", '(?{})+ with fatal warnings');
103eleak(2, 0, "$all /[\\i]/ ", 'invalid charclass escape with fatal warns');
104eleak(2, 0, "$all /[:foo:]/ ", '/[:foo:]/ with fatal warnings');
105eleak(2, 0, "$all /[a-\\d]/ ", '[a-\d] char class with fatal warnings');
106eleak(2, 0, "$all v111111111111111111111111111111111111111111111111",
107 'vstring num overflow with fatal warnings');
38b1111c 108
e88567f2 109eleak(2, 0, 'sub{<*>}');
11ddfebc
FC
110# Use a random number of ops, so that the glob op does not reuse the same
111# address each time, giving us false passes.
a1ba7ec6 112leak(2, 0, sub { eval '$x+'x(1 + rand() * 100) . '<*>'; },
11ddfebc 113 'freeing partly iterated glob');
e88567f2 114
110af908
FC
115eleak(2, 0, 'goto sub {}', 'goto &sub in eval');
116eleak(2, 0, '() = sort { goto sub {} } 1,2', 'goto &sub in sort');
117eleak(2, 0, '/(?{ goto sub {} })/', 'goto &sub in regexp');
118
459defa1
DM
119sub TIEARRAY { bless [], $_[0] }
120sub FETCH { $_[0]->[$_[1]] }
121sub STORE { $_[0]->[$_[1]] = $_[2] }
122
123# local $tied_elem[..] leaks <20020502143736.N16831@dansat.data-plan.com>"
124{
125 tie my @a, 'main';
126 leak(5, 0, sub {local $a[0]}, "local \$tied[0]");
127}
128
5f7f7af5
FC
129# Overloading
130require overload;
131eleak(2, 0, "BEGIN{overload::constant integer=>sub{}} 1,1,1,1,1,1,1,1,1,1",
132 '"too many errors" from constant overloading returning undef');
133# getting this one to leak was complicated; we have to unset LOCALIZE_HH:
134eleak(2, 0, 'BEGIN{overload::constant integer=>sub{}; $^H &= ~ 0x00020000}
135 1,1,1,1,1,1,1,1,1,1',
136 '"too many errors" from constant overloading with $^H sabotaged');
137eleak(2, 0, "BEGIN{overload::constant integer=>sub{}; undef %^H}
138 1,1,1,1,1,1,1,1,1,1",
139 '"too many errors" from constant overloading with %^H undefined');
140
141
8bd05d90
DM
142# [perl #74484] repeated tries leaked SVs on the tmps stack
143
2e64971a 144leak_expr(5, 0, q{"YYYYYa" =~ /.+?(a(.+?)|b)/ }, "trie leak");
b2a2a901
DM
145
146# [perl #48004] map/grep didn't free tmps till the end
147
148{
149 # qr/1/ just creates tmps that are hopefully freed per iteration
150
151 my $s;
152 my @a;
153 my @count = (0) x 4; # pre-allocate
154
155 grep qr/1/ && ($count[$_] = sv_count()) && 99, 0..3;
156 is(@count[3] - @count[0], 0, "void grep expr: no new tmps per iter");
157 grep { qr/1/ && ($count[$_] = sv_count()) && 99 } 0..3;
158 is(@count[3] - @count[0], 0, "void grep block: no new tmps per iter");
159
160 $s = grep qr/1/ && ($count[$_] = sv_count()) && 99, 0..3;
161 is(@count[3] - @count[0], 0, "scalar grep expr: no new tmps per iter");
162 $s = grep { qr/1/ && ($count[$_] = sv_count()) && 99 } 0..3;
163 is(@count[3] - @count[0], 0, "scalar grep block: no new tmps per iter");
164
165 @a = grep qr/1/ && ($count[$_] = sv_count()) && 99, 0..3;
166 is(@count[3] - @count[0], 0, "list grep expr: no new tmps per iter");
167 @a = grep { qr/1/ && ($count[$_] = sv_count()) && 99 } 0..3;
168 is(@count[3] - @count[0], 0, "list grep block: no new tmps per iter");
169
170
171 map qr/1/ && ($count[$_] = sv_count()) && 99, 0..3;
172 is(@count[3] - @count[0], 0, "void map expr: no new tmps per iter");
173 map { qr/1/ && ($count[$_] = sv_count()) && 99 } 0..3;
174 is(@count[3] - @count[0], 0, "void map block: no new tmps per iter");
175
176 $s = map qr/1/ && ($count[$_] = sv_count()) && 99, 0..3;
177 is(@count[3] - @count[0], 0, "scalar map expr: no new tmps per iter");
178 $s = map { qr/1/ && ($count[$_] = sv_count()) && 99 } 0..3;
179 is(@count[3] - @count[0], 0, "scalar map block: no new tmps per iter");
180
181 @a = map qr/1/ && ($count[$_] = sv_count()) && 99, 0..3;
182 is(@count[3] - @count[0], 3, "list map expr: one new tmp per iter");
183 @a = map { qr/1/ && ($count[$_] = sv_count()) && 99 } 0..3;
184 is(@count[3] - @count[0], 3, "list map block: one new tmp per iter");
185
186}
a50d6ed0
TC
187
188SKIP:
189{ # broken by 304474c3, fixed by cefd5c7c, but didn't seem to cause
190 # any other test failures
191 # base test case from ribasushi (Peter Rabbitson)
192 eval { require Scalar::Util; Scalar::Util->import("weaken"); 1; }
193 or skip "no weaken", 1;
194 my $weak;
195 {
196 $weak = my $in = {};
197 weaken($weak);
198 my $out = { in => $in, in => undef }
199 }
200 ok(!$weak, "hash referenced weakened SV released");
201}
64dc9714 202
c9af70d5
FC
203# prototype() errors
204leak(2,0, sub { eval { prototype "CORE::fu" } }, 'prototype errors');
205
64dc9714
DM
206# RT #72246: rcatline memory leak on bad $/
207
208leak(2, 0,
209 sub {
210 my $f;
211 open CATLINE, '<', \$f;
212 local $/ = "\x{262E}";
213 my $str = "\x{2622}";
214 eval { $str .= <CATLINE> };
215 },
216 "rcatline leak"
217);
a17b8556
JP
218
219{
220 my $RE = qr/
221 (?:
222 <(?<tag>
223 \s*
224 [^>\s]+
225 )>
226 )??
227 /xis;
228
229 "<html><body></body></html>" =~ m/$RE/gcs;
230
231 leak(5, 0, sub {
232 my $tag = $+{tag};
233 }, "named regexp captures");
234}
bcb2959f 235
2e880be7
FC
236eleak(2,0,'/[:]/');
237eleak(2,0,'/[\xdf]/i');
f248b511 238eleak(2,0,'s![^/]!!');
3451022d
FC
239eleak(2,0,'/[pp]/');
240eleak(2,0,'/[[:ascii:]]/');
e5fcdda9
FC
241eleak(2,0,'/[[.zog.]]/');
242eleak(2,0,'/[.zog.]/');
7f5d1fb2
KW
243
244# These can generate one ref count, but just once.
245eleak(4,1,'chr(0x100) =~ /[[:punct:]]/');
246eleak(4,1,'chr(0x100) =~ /[[:^punct:]]/');
247eleak(4,1,'chr(0x100) =~ /[[:word:]]/');
248eleak(4,1,'chr(0x100) =~ /[[:^word:]]/');
249
4aca0fe6 250eleak(2,0,'chr(0x100) =~ /\P{Assigned}/');
e1ff3a88 251leak(2,0,sub { /(??{})/ }, '/(??{})/');
2e880be7 252
bcb2959f 253leak(2,0,sub { !$^V }, '[perl #109762] version object in boolean context');
d97935e0
DM
254
255
256# [perl #114356] run-time rexexp with unchanging pattern got
257# inflated refcounts
1cac5c33 258eleak(2, 0, q{ my $x = "x"; "abc" =~ /$x/ for 1..5 }, '#114356');
d97935e0 259
1cac5c33 260eleak(2, 0, 'sub', '"sub" with nothing following');
12d3c230 261eleak(2, 0, '+sub:a{}', 'anon subs with invalid attributes');
9ffcdca1 262eleak(2, 0, 'no warnings; sub a{1 1}', 'sub with syntax error');
4d2dfd15 263eleak(2, 0, 'no warnings; sub {1 1}', 'anon sub with syntax error');
8ca8859f
FC
264eleak(2, 0, 'no warnings; use feature ":all"; my sub a{1 1}',
265 'my sub with syntax error');
12d3c230 266
1cdc9186
FC
267# Reification (or lack thereof)
268leak(2, 0, sub { sub { local $_[0]; shift }->(1) },
269 'local $_[0] on surreal @_, followed by shift');
270leak(2, 0, sub { sub { local $_[0]; \@_ }->(1) },
271 'local $_[0] on surreal @_, followed by reification');
272
47c8e7fe
FC
273# Syntax errors
274eleak(2, 0, '"${<<END}"
275 ', 'unterminated here-doc in quotes in multiline eval');
276eleak(2, 0, '"${<<END
277 }"', 'unterminated here-doc in multiline quotes in eval');
1cac5c33 278leak(2, 0, sub { eval { do './op/svleak.pl' } },
2d85e411 279 'unterminated here-doc in file');
47c8e7fe
FC
280eleak(2, 0, 'tr/9-0//');
281eleak(2, 0, 'tr/a-z-0//');
282eleak(2, 0, 'no warnings; nonexistent_function 33838',
a577af66 283 'bareword followed by number');
10002bc1
FC
284eleak(2, 0, '//dd;'x20, '"too many errors" when parsing m// flags');
285eleak(2, 0, 's///dd;'x20, '"too many errors" when parsing s/// flags');
1cac5c33 286eleak(2, 0, 'no warnings; 2 2;BEGIN{}',
9eb48717 287 'BEGIN block after syntax error');
600beb2e
FC
288{
289 local %INC; # in case Errno is already loaded
290 eleak(2, 0, 'no warnings; 2@!{',
291 'implicit "use Errno" after syntax error');
292}
3ce3dcd9 293eleak(2, 0, "\"\$\0\356\"", 'qq containing $ <null> something');
3ac7ff8f 294eleak(2, 0, 'END OF TERMS AND CONDITIONS', 'END followed by words');
14ca8ff4 295eleak(2, 0, "+ + +;qq|\\N{a}|"x10,'qq"\N{a}" after errors');
b6407c49
FC
296eleak(2, 0, "qq|\\N{%}|", 'qq"\N{%}" (invalid charname)');
297eleak(2, 0, "qq|\\N{au}|;", 'qq"\N{invalid}"');
38b1111c 298eleak(2, 0, "qq|\\c|;"x10, '"too many errors" from qq"\c"');
38b1111c
FC
299eleak(2, 0, "qq|\\o|;"x10, '"too many errors" from qq"\o"');
300eleak(2, 0, "qq|\\x{|;"x10, '"too many errors" from qq"\x{"');
301eleak(2, 0, "qq|\\N|;"x10, '"too many errors" from qq"\N"');
302eleak(2, 0, "qq|\\N{|;"x10, '"too many errors" from qq"\N{"');
303eleak(2, 0, "qq|\\N{U+GETG}|;"x10,'"too many errors" from qq"\N{U+JUNK}"');
9e942bf5 304
9fa29fa7
FC
305
306# [perl #114764] Attributes leak scalars
307leak(2, 0, sub { eval 'my $x : shared' }, 'my $x :shared used to leak');
895cdc83 308
c25611d5
FC
309eleak(2, 0, 'ref: 1', 'labels');
310
895cdc83
FC
311# Tied hash iteration was leaking if the hash was freed before itera-
312# tion was over.
313package t {
314 sub TIEHASH { bless [] }
315 sub FIRSTKEY { 0 }
316}
317leak(2, 0, sub {
318 my $h = {};
319 tie %$h, t;
320 each %$h;
321 undef $h;
322}, 'tied hash iteration does not leak');
9c744f4f 323
33b889b0
RZ
324package explosive_scalar {
325 sub TIESCALAR { my $self = shift; bless [undef, {@_}], $self }
326 sub FETCH { die 'FETCH' if $_[0][1]{FETCH}; $_[0][0] }
327 sub STORE { die 'STORE' if $_[0][1]{STORE}; $_[0][0] = $_[1] }
328}
329tie my $die_on_fetch, 'explosive_scalar', FETCH => 1;
330
39984de3
FC
331# List assignment was leaking when assigning explosive scalars to
332# aggregates.
9c744f4f 333leak(2, 0, sub {
33b889b0
RZ
334 eval {%a = ($die_on_fetch, 0)}; # key
335 eval {%a = (0, $die_on_fetch)}; # value
336 eval {%a = ($die_on_fetch, $die_on_fetch)}; # both
499ace3b 337 eval {%a = ($die_on_fetch)}; # key, odd elements
9c744f4f 338}, 'hash assignment does not leak');
39984de3 339leak(2, 0, sub {
33b889b0
RZ
340 eval {@a = ($die_on_fetch)};
341 eval {($die_on_fetch, $b) = ($b, $die_on_fetch)};
342 # restore
343 tie $die_on_fetch, 'explosive_scalar', FETCH => 1;
39984de3 344}, 'array assignment does not leak');
9c744f4f 345
0db511c0
FC
346# [perl #107000]
347package hhtie {
348 sub TIEHASH { bless [] }
349 sub STORE { $_[0][0]{$_[1]} = $_[2] }
350 sub FETCH { die if $explosive; $_[0][0]{$_[1]} }
351 sub FIRSTKEY { keys %{$_[0][0]}; each %{$_[0][0]} }
352 sub NEXTKEY { each %{$_[0][0]} }
353}
1cac5c33 354leak(2, 0, sub {
0db511c0
FC
355 eval q`
356 BEGIN {
357 $hhtie::explosive = 0;
358 tie %^H, hhtie;
359 $^H{foo} = bar;
360 $hhtie::explosive = 1;
361 }
362 { 1; }
363 `;
364}, 'hint-hash copying does not leak');
33b889b0
RZ
365
366package explosive_array {
367 sub TIEARRAY { bless [[], {}], $_[0] }
368 sub FETCH { die if $_[0]->[1]{FETCH}; $_[0]->[0][$_[1]] }
369 sub FETCHSIZE { die if $_[0]->[1]{FETCHSIZE}; scalar @{ $_[0]->[0] } }
370 sub STORE { die if $_[0]->[1]{STORE}; $_[0]->[0][$_[1]] = $_[2] }
371 sub CLEAR { die if $_[0]->[1]{CLEAR}; @{$_[0]->[0]} = () }
372 sub EXTEND { die if $_[0]->[1]{EXTEND}; return }
373 sub explode { my $self = shift; $self->[1] = {@_} }
374}
375
376leak(2, 0, sub {
377 tie my @a, 'explosive_array';
378 tied(@a)->explode( STORE => 1 );
379 my $x = 0;
380 eval { @a = ($x) };
381}, 'explosive array assignment does not leak');
382
383leak(2, 0, sub {
384 my ($a, $b);
385 eval { warn $die_on_fetch };
386}, 'explosive warn argument');
387
388leak(2, 0, sub {
389 my $foo = sub { return $die_on_fetch };
390 my $res = eval { $foo->() };
391 my @res = eval { $foo->() };
392}, 'function returning explosive does not leak');
393
394leak(2, 0, sub {
395 my $res = eval { {$die_on_fetch, 0} };
396 $res = eval { {0, $die_on_fetch} };
397}, 'building anon hash with explosives does not leak');
398
399leak(2, 0, sub {
3ed356df
FC
400 my $res = eval { [$die_on_fetch] };
401}, 'building anon array with explosives does not leak');
402
403leak(2, 0, sub {
33b889b0
RZ
404 my @a;
405 eval { push @a, $die_on_fetch };
406}, 'pushing exploding scalar does not leak');
ddac780e 407
644ac3a8
FC
408leak(2, 0, sub {
409 eval { push @-, '' };
410}, 'pushing onto read-only array does not leak');
411
ddac780e
FC
412
413# Run-time regexp code blocks
414{
2ac13048
FC
415 use re 'eval';
416 my @tests = ('[(?{})]','(?{})');
ddac780e 417 for my $t (@tests) {
1cac5c33 418 leak(2, 0, sub {
ddac780e
FC
419 / $t/;
420 }, "/ \$x/ where \$x is $t does not leak");
1cac5c33 421 leak(2, 0, sub {
ddac780e
FC
422 /(?{})$t/;
423 }, "/(?{})\$x/ where \$x is $t does not leak");
424 }
425}
104c40b0
FC
426
427
428{
429 use warnings FATAL => 'all';
430 leak(2, 0, sub {
d861347e 431 no warnings 'once';
104c40b0
FC
432 eval { printf uNopened 42 };
433 }, 'printfing to bad handle under fatal warnings does not leak');
c7bd8b84
FC
434 open my $fh, ">", \my $buf;
435 leak(2, 0, sub {
436 eval { printf $fh chr 2455 };
437 }, 'wide fatal warning does not make printf leak');
438 close $fh or die $!;
104c40b0 439}
1e5f02b3
FC
440
441
442leak(2,0,sub{eval{require untohunothu}}, 'requiring nonexistent module');