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