This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
Don’t leak subs containing syntax errors
[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
9ffcdca1 18plan tests => 65;
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
2e880be7 35# Like leak, but run a string eval instead; takes into account existing
9eb48717
FC
36# string eval leaks under -Dmad (except when -Dmad leaks two or
37# more SVs). The code is used instead of the test name
2e880be7
FC
38# if the name is absent.
39sub eleak {
40 my ($n,$delta,$code,@rest) = @_;
41 leak $n, $delta + !!$Config{mad}, sub { eval $code },
42 @rest ? @rest : $code
43}
44
8bd05d90
DM
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
d97c33b5
DM
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");
459defa1 72
e88567f2
FC
73eleak(2, 0, 'sub{<*>}');
74
110af908
FC
75eleak(2, 0, 'goto sub {}', 'goto &sub in eval');
76eleak(2, 0, '() = sort { goto sub {} } 1,2', 'goto &sub in sort');
77eleak(2, 0, '/(?{ goto sub {} })/', 'goto &sub in regexp');
78
459defa1
DM
79sub TIEARRAY { bless [], $_[0] }
80sub FETCH { $_[0]->[$_[1]] }
81sub STORE { $_[0]->[$_[1]] = $_[2] }
82
83# local $tied_elem[..] leaks <20020502143736.N16831@dansat.data-plan.com>"
84{
85 tie my @a, 'main';
86 leak(5, 0, sub {local $a[0]}, "local \$tied[0]");
87}
88
8bd05d90
DM
89# [perl #74484] repeated tries leaked SVs on the tmps stack
90
2e64971a 91leak_expr(5, 0, q{"YYYYYa" =~ /.+?(a(.+?)|b)/ }, "trie leak");
b2a2a901
DM
92
93# [perl #48004] map/grep didn't free tmps till the end
94
95{
96 # qr/1/ just creates tmps that are hopefully freed per iteration
97
98 my $s;
99 my @a;
100 my @count = (0) x 4; # pre-allocate
101
102 grep qr/1/ && ($count[$_] = sv_count()) && 99, 0..3;
103 is(@count[3] - @count[0], 0, "void grep expr: no new tmps per iter");
104 grep { qr/1/ && ($count[$_] = sv_count()) && 99 } 0..3;
105 is(@count[3] - @count[0], 0, "void grep block: no new tmps per iter");
106
107 $s = grep qr/1/ && ($count[$_] = sv_count()) && 99, 0..3;
108 is(@count[3] - @count[0], 0, "scalar grep expr: no new tmps per iter");
109 $s = grep { qr/1/ && ($count[$_] = sv_count()) && 99 } 0..3;
110 is(@count[3] - @count[0], 0, "scalar grep block: no new tmps per iter");
111
112 @a = grep qr/1/ && ($count[$_] = sv_count()) && 99, 0..3;
113 is(@count[3] - @count[0], 0, "list grep expr: no new tmps per iter");
114 @a = grep { qr/1/ && ($count[$_] = sv_count()) && 99 } 0..3;
115 is(@count[3] - @count[0], 0, "list grep block: no new tmps per iter");
116
117
118 map qr/1/ && ($count[$_] = sv_count()) && 99, 0..3;
119 is(@count[3] - @count[0], 0, "void map expr: no new tmps per iter");
120 map { qr/1/ && ($count[$_] = sv_count()) && 99 } 0..3;
121 is(@count[3] - @count[0], 0, "void map block: no new tmps per iter");
122
123 $s = map qr/1/ && ($count[$_] = sv_count()) && 99, 0..3;
124 is(@count[3] - @count[0], 0, "scalar map expr: no new tmps per iter");
125 $s = map { qr/1/ && ($count[$_] = sv_count()) && 99 } 0..3;
126 is(@count[3] - @count[0], 0, "scalar map block: no new tmps per iter");
127
128 @a = map qr/1/ && ($count[$_] = sv_count()) && 99, 0..3;
129 is(@count[3] - @count[0], 3, "list map expr: one new tmp per iter");
130 @a = map { qr/1/ && ($count[$_] = sv_count()) && 99 } 0..3;
131 is(@count[3] - @count[0], 3, "list map block: one new tmp per iter");
132
133}
a50d6ed0
TC
134
135SKIP:
136{ # broken by 304474c3, fixed by cefd5c7c, but didn't seem to cause
137 # any other test failures
138 # base test case from ribasushi (Peter Rabbitson)
139 eval { require Scalar::Util; Scalar::Util->import("weaken"); 1; }
140 or skip "no weaken", 1;
141 my $weak;
142 {
143 $weak = my $in = {};
144 weaken($weak);
145 my $out = { in => $in, in => undef }
146 }
147 ok(!$weak, "hash referenced weakened SV released");
148}
64dc9714
DM
149
150# RT #72246: rcatline memory leak on bad $/
151
152leak(2, 0,
153 sub {
154 my $f;
155 open CATLINE, '<', \$f;
156 local $/ = "\x{262E}";
157 my $str = "\x{2622}";
158 eval { $str .= <CATLINE> };
159 },
160 "rcatline leak"
161);
a17b8556
JP
162
163{
164 my $RE = qr/
165 (?:
166 <(?<tag>
167 \s*
168 [^>\s]+
169 )>
170 )??
171 /xis;
172
173 "<html><body></body></html>" =~ m/$RE/gcs;
174
175 leak(5, 0, sub {
176 my $tag = $+{tag};
177 }, "named regexp captures");
178}
bcb2959f 179
2e880be7
FC
180eleak(2,0,'/[:]/');
181eleak(2,0,'/[\xdf]/i');
f248b511 182eleak(2,0,'s![^/]!!');
3451022d
FC
183eleak(2,0,'/[pp]/');
184eleak(2,0,'/[[:ascii:]]/');
2e880be7 185
bcb2959f 186leak(2,0,sub { !$^V }, '[perl #109762] version object in boolean context');
d97935e0
DM
187
188
189# [perl #114356] run-time rexexp with unchanging pattern got
190# inflated refcounts
191
ef09a500
DM
192SKIP: {
193 skip "disabled under -Dmad (eval leaks)" if $Config{mad};
194 leak(2, 0, sub { eval q{ my $x = "x"; "abc" =~ /$x/ for 1..5 } }, '#114356');
195}
196
12d3c230 197eleak(2, 0, '+sub:a{}', 'anon subs with invalid attributes');
9ffcdca1 198eleak(2, 0, 'no warnings; sub a{1 1}', 'sub with syntax error');
12d3c230 199
47c8e7fe
FC
200# Syntax errors
201eleak(2, 0, '"${<<END}"
202 ', 'unterminated here-doc in quotes in multiline eval');
203eleak(2, 0, '"${<<END
204 }"', 'unterminated here-doc in multiline quotes in eval');
205leak(2, !!$Config{mad}, sub { eval { do './op/svleak.pl' } },
2d85e411 206 'unterminated here-doc in file');
47c8e7fe
FC
207eleak(2, 0, 'tr/9-0//');
208eleak(2, 0, 'tr/a-z-0//');
209eleak(2, 0, 'no warnings; nonexistent_function 33838',
a577af66 210 'bareword followed by number');
10002bc1
FC
211eleak(2, 0, '//dd;'x20, '"too many errors" when parsing m// flags');
212eleak(2, 0, 's///dd;'x20, '"too many errors" when parsing s/// flags');
9eb48717
FC
213eleak(2, !!$Config{mad}, 'no warnings; 2 2;BEGIN{}',
214 'BEGIN block after syntax error');
600beb2e
FC
215{
216 local %INC; # in case Errno is already loaded
217 eleak(2, 0, 'no warnings; 2@!{',
218 'implicit "use Errno" after syntax error');
219}
3ce3dcd9 220eleak(2, 0, "\"\$\0\356\"", 'qq containing $ <null> something');
9e942bf5
FC
221{
222 local $::TODO = 'eval "END blah blah" still leaks';
223 eleak(2, 0, 'END OF TERMS AND CONDITIONS', 'END followed by words');
224}
225
9fa29fa7
FC
226
227# [perl #114764] Attributes leak scalars
228leak(2, 0, sub { eval 'my $x : shared' }, 'my $x :shared used to leak');
895cdc83 229
c25611d5
FC
230eleak(2, 0, 'ref: 1', 'labels');
231
895cdc83
FC
232# Tied hash iteration was leaking if the hash was freed before itera-
233# tion was over.
234package t {
235 sub TIEHASH { bless [] }
236 sub FIRSTKEY { 0 }
237}
238leak(2, 0, sub {
239 my $h = {};
240 tie %$h, t;
241 each %$h;
242 undef $h;
243}, 'tied hash iteration does not leak');
9c744f4f 244
33b889b0
RZ
245package explosive_scalar {
246 sub TIESCALAR { my $self = shift; bless [undef, {@_}], $self }
247 sub FETCH { die 'FETCH' if $_[0][1]{FETCH}; $_[0][0] }
248 sub STORE { die 'STORE' if $_[0][1]{STORE}; $_[0][0] = $_[1] }
249}
250tie my $die_on_fetch, 'explosive_scalar', FETCH => 1;
251
39984de3
FC
252# List assignment was leaking when assigning explosive scalars to
253# aggregates.
9c744f4f 254leak(2, 0, sub {
33b889b0
RZ
255 eval {%a = ($die_on_fetch, 0)}; # key
256 eval {%a = (0, $die_on_fetch)}; # value
257 eval {%a = ($die_on_fetch, $die_on_fetch)}; # both
9c744f4f 258}, 'hash assignment does not leak');
39984de3 259leak(2, 0, sub {
33b889b0
RZ
260 eval {@a = ($die_on_fetch)};
261 eval {($die_on_fetch, $b) = ($b, $die_on_fetch)};
262 # restore
263 tie $die_on_fetch, 'explosive_scalar', FETCH => 1;
39984de3 264}, 'array assignment does not leak');
9c744f4f 265
0db511c0
FC
266# [perl #107000]
267package hhtie {
268 sub TIEHASH { bless [] }
269 sub STORE { $_[0][0]{$_[1]} = $_[2] }
270 sub FETCH { die if $explosive; $_[0][0]{$_[1]} }
271 sub FIRSTKEY { keys %{$_[0][0]}; each %{$_[0][0]} }
272 sub NEXTKEY { each %{$_[0][0]} }
273}
274leak(2,!!$Config{mad}, sub {
275 eval q`
276 BEGIN {
277 $hhtie::explosive = 0;
278 tie %^H, hhtie;
279 $^H{foo} = bar;
280 $hhtie::explosive = 1;
281 }
282 { 1; }
283 `;
284}, 'hint-hash copying does not leak');
33b889b0
RZ
285
286package explosive_array {
287 sub TIEARRAY { bless [[], {}], $_[0] }
288 sub FETCH { die if $_[0]->[1]{FETCH}; $_[0]->[0][$_[1]] }
289 sub FETCHSIZE { die if $_[0]->[1]{FETCHSIZE}; scalar @{ $_[0]->[0] } }
290 sub STORE { die if $_[0]->[1]{STORE}; $_[0]->[0][$_[1]] = $_[2] }
291 sub CLEAR { die if $_[0]->[1]{CLEAR}; @{$_[0]->[0]} = () }
292 sub EXTEND { die if $_[0]->[1]{EXTEND}; return }
293 sub explode { my $self = shift; $self->[1] = {@_} }
294}
295
296leak(2, 0, sub {
297 tie my @a, 'explosive_array';
298 tied(@a)->explode( STORE => 1 );
299 my $x = 0;
300 eval { @a = ($x) };
301}, 'explosive array assignment does not leak');
302
303leak(2, 0, sub {
304 my ($a, $b);
305 eval { warn $die_on_fetch };
306}, 'explosive warn argument');
307
308leak(2, 0, sub {
309 my $foo = sub { return $die_on_fetch };
310 my $res = eval { $foo->() };
311 my @res = eval { $foo->() };
312}, 'function returning explosive does not leak');
313
314leak(2, 0, sub {
315 my $res = eval { {$die_on_fetch, 0} };
316 $res = eval { {0, $die_on_fetch} };
317}, 'building anon hash with explosives does not leak');
318
319leak(2, 0, sub {
3ed356df
FC
320 my $res = eval { [$die_on_fetch] };
321}, 'building anon array with explosives does not leak');
322
323leak(2, 0, sub {
33b889b0
RZ
324 my @a;
325 eval { push @a, $die_on_fetch };
326}, 'pushing exploding scalar does not leak');
ddac780e 327
644ac3a8
FC
328leak(2, 0, sub {
329 eval { push @-, '' };
330}, 'pushing onto read-only array does not leak');
331
ddac780e
FC
332
333# Run-time regexp code blocks
334{
2ac13048 335 use re 'eval';
9f81bf16 336 my $madness = !!$Config{mad};
2ac13048 337 my @tests = ('[(?{})]','(?{})');
ddac780e 338 for my $t (@tests) {
9f81bf16 339 leak(2, $madness, sub {
ddac780e
FC
340 / $t/;
341 }, "/ \$x/ where \$x is $t does not leak");
9f81bf16 342 leak(2, $madness, sub {
ddac780e
FC
343 /(?{})$t/;
344 }, "/(?{})\$x/ where \$x is $t does not leak");
345 }
346}
104c40b0
FC
347
348
349{
350 use warnings FATAL => 'all';
351 leak(2, 0, sub {
d861347e 352 no warnings 'once';
104c40b0
FC
353 eval { printf uNopened 42 };
354 }, 'printfing to bad handle under fatal warnings does not leak');
c7bd8b84
FC
355 open my $fh, ">", \my $buf;
356 leak(2, 0, sub {
357 eval { printf $fh chr 2455 };
358 }, 'wide fatal warning does not make printf leak');
359 close $fh or die $!;
104c40b0 360}
1e5f02b3
FC
361
362
363leak(2,0,sub{eval{require untohunothu}}, 'requiring nonexistent module');