This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
leakfinder.pl
[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
a577af66 18plan tests => 46;
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
8bd05d90
DM
35# run some expression N times. The expr is concatenated N times and then
36# evaled, ensuring that that there are no scope exits between executions.
37# If the number of SVs at the end of expr N is greater than (N-1)*delta at
38# the end of expr 1, we've got a leak
39#
40sub leak_expr {
41 my ($n, $delta, $expr, @rest) = @_;
42 my $sv0 = 0;
43 my $sv1 = 0;
44 my $true = 1; # avoid stuff being optimised away
45 my $code1 = "($expr || \$true)";
46 my $code = "$code1 && (\$sv0 = sv_count())" . ("&& $code1" x 4)
47 . " && (\$sv1 = sv_count())";
48 if (eval $code) {
49 cmp_ok($sv1-$sv0, '<=', ($n-1)*$delta, @rest);
50 }
51 else {
52 fail("eval @rest: $@");
53 }
54}
55
56
d97c33b5
DM
57my @a;
58
59leak(5, 0, sub {}, "basic check 1 of leak test infrastructure");
60leak(5, 0, sub {push @a,1;pop @a}, "basic check 2 of leak test infrastructure");
61leak(5, 1, sub {push @a,1;}, "basic check 3 of leak test infrastructure");
459defa1
DM
62
63sub TIEARRAY { bless [], $_[0] }
64sub FETCH { $_[0]->[$_[1]] }
65sub STORE { $_[0]->[$_[1]] = $_[2] }
66
67# local $tied_elem[..] leaks <20020502143736.N16831@dansat.data-plan.com>"
68{
69 tie my @a, 'main';
70 leak(5, 0, sub {local $a[0]}, "local \$tied[0]");
71}
72
8bd05d90
DM
73# [perl #74484] repeated tries leaked SVs on the tmps stack
74
2e64971a 75leak_expr(5, 0, q{"YYYYYa" =~ /.+?(a(.+?)|b)/ }, "trie leak");
b2a2a901
DM
76
77# [perl #48004] map/grep didn't free tmps till the end
78
79{
80 # qr/1/ just creates tmps that are hopefully freed per iteration
81
82 my $s;
83 my @a;
84 my @count = (0) x 4; # pre-allocate
85
86 grep qr/1/ && ($count[$_] = sv_count()) && 99, 0..3;
87 is(@count[3] - @count[0], 0, "void grep expr: no new tmps per iter");
88 grep { qr/1/ && ($count[$_] = sv_count()) && 99 } 0..3;
89 is(@count[3] - @count[0], 0, "void grep block: no new tmps per iter");
90
91 $s = grep qr/1/ && ($count[$_] = sv_count()) && 99, 0..3;
92 is(@count[3] - @count[0], 0, "scalar grep expr: no new tmps per iter");
93 $s = grep { qr/1/ && ($count[$_] = sv_count()) && 99 } 0..3;
94 is(@count[3] - @count[0], 0, "scalar grep block: no new tmps per iter");
95
96 @a = grep qr/1/ && ($count[$_] = sv_count()) && 99, 0..3;
97 is(@count[3] - @count[0], 0, "list grep expr: no new tmps per iter");
98 @a = grep { qr/1/ && ($count[$_] = sv_count()) && 99 } 0..3;
99 is(@count[3] - @count[0], 0, "list grep block: no new tmps per iter");
100
101
102 map qr/1/ && ($count[$_] = sv_count()) && 99, 0..3;
103 is(@count[3] - @count[0], 0, "void map expr: no new tmps per iter");
104 map { qr/1/ && ($count[$_] = sv_count()) && 99 } 0..3;
105 is(@count[3] - @count[0], 0, "void map block: no new tmps per iter");
106
107 $s = map qr/1/ && ($count[$_] = sv_count()) && 99, 0..3;
108 is(@count[3] - @count[0], 0, "scalar map expr: no new tmps per iter");
109 $s = map { qr/1/ && ($count[$_] = sv_count()) && 99 } 0..3;
110 is(@count[3] - @count[0], 0, "scalar map block: no new tmps per iter");
111
112 @a = map qr/1/ && ($count[$_] = sv_count()) && 99, 0..3;
113 is(@count[3] - @count[0], 3, "list map expr: one new tmp per iter");
114 @a = map { qr/1/ && ($count[$_] = sv_count()) && 99 } 0..3;
115 is(@count[3] - @count[0], 3, "list map block: one new tmp per iter");
116
117}
a50d6ed0
TC
118
119SKIP:
120{ # broken by 304474c3, fixed by cefd5c7c, but didn't seem to cause
121 # any other test failures
122 # base test case from ribasushi (Peter Rabbitson)
123 eval { require Scalar::Util; Scalar::Util->import("weaken"); 1; }
124 or skip "no weaken", 1;
125 my $weak;
126 {
127 $weak = my $in = {};
128 weaken($weak);
129 my $out = { in => $in, in => undef }
130 }
131 ok(!$weak, "hash referenced weakened SV released");
132}
64dc9714
DM
133
134# RT #72246: rcatline memory leak on bad $/
135
136leak(2, 0,
137 sub {
138 my $f;
139 open CATLINE, '<', \$f;
140 local $/ = "\x{262E}";
141 my $str = "\x{2622}";
142 eval { $str .= <CATLINE> };
143 },
144 "rcatline leak"
145);
a17b8556
JP
146
147{
148 my $RE = qr/
149 (?:
150 <(?<tag>
151 \s*
152 [^>\s]+
153 )>
154 )??
155 /xis;
156
157 "<html><body></body></html>" =~ m/$RE/gcs;
158
159 leak(5, 0, sub {
160 my $tag = $+{tag};
161 }, "named regexp captures");
162}
bcb2959f
FC
163
164leak(2,0,sub { !$^V }, '[perl #109762] version object in boolean context');
d97935e0
DM
165
166
167# [perl #114356] run-time rexexp with unchanging pattern got
168# inflated refcounts
169
ef09a500
DM
170SKIP: {
171 skip "disabled under -Dmad (eval leaks)" if $Config{mad};
172 leak(2, 0, sub { eval q{ my $x = "x"; "abc" =~ /$x/ for 1..5 } }, '#114356');
173}
174
2d85e411 175SKIP: {
a577af66 176 skip "disabled under -Dmad (eval leaks)", 6 if $Config{mad};
2d85e411
FC
177 leak(2, 0, sub { eval '"${<<END}"
178 ' }, 'unterminated here-doc in quotes in multiline eval');
179 leak(2, 0, sub { eval '"${<<END
180 }"' }, 'unterminated here-doc in multiline quotes in eval');
181 leak(2, 0, sub { eval { do './op/svleak.pl' } },
182 'unterminated here-doc in file');
4dc843bc
FC
183 leak(2, 0, sub { eval 'tr/9-0//' }, 'tr/9-0//');
184 leak(2, 0, sub { eval 'tr/a-z-0//' }, 'tr/a-z-0//');
a577af66
FC
185 leak(2, 0, sub { eval 'no warnings; nonexistent_function 33838' },
186 'bareword followed by number');
2d85e411 187}
9fa29fa7
FC
188
189# [perl #114764] Attributes leak scalars
190leak(2, 0, sub { eval 'my $x : shared' }, 'my $x :shared used to leak');
895cdc83
FC
191
192# Tied hash iteration was leaking if the hash was freed before itera-
193# tion was over.
194package t {
195 sub TIEHASH { bless [] }
196 sub FIRSTKEY { 0 }
197}
198leak(2, 0, sub {
199 my $h = {};
200 tie %$h, t;
201 each %$h;
202 undef $h;
203}, 'tied hash iteration does not leak');
9c744f4f 204
33b889b0
RZ
205package explosive_scalar {
206 sub TIESCALAR { my $self = shift; bless [undef, {@_}], $self }
207 sub FETCH { die 'FETCH' if $_[0][1]{FETCH}; $_[0][0] }
208 sub STORE { die 'STORE' if $_[0][1]{STORE}; $_[0][0] = $_[1] }
209}
210tie my $die_on_fetch, 'explosive_scalar', FETCH => 1;
211
39984de3
FC
212# List assignment was leaking when assigning explosive scalars to
213# aggregates.
9c744f4f 214leak(2, 0, sub {
33b889b0
RZ
215 eval {%a = ($die_on_fetch, 0)}; # key
216 eval {%a = (0, $die_on_fetch)}; # value
217 eval {%a = ($die_on_fetch, $die_on_fetch)}; # both
9c744f4f 218}, 'hash assignment does not leak');
39984de3 219leak(2, 0, sub {
33b889b0
RZ
220 eval {@a = ($die_on_fetch)};
221 eval {($die_on_fetch, $b) = ($b, $die_on_fetch)};
222 # restore
223 tie $die_on_fetch, 'explosive_scalar', FETCH => 1;
39984de3 224}, 'array assignment does not leak');
9c744f4f 225
0db511c0
FC
226# [perl #107000]
227package hhtie {
228 sub TIEHASH { bless [] }
229 sub STORE { $_[0][0]{$_[1]} = $_[2] }
230 sub FETCH { die if $explosive; $_[0][0]{$_[1]} }
231 sub FIRSTKEY { keys %{$_[0][0]}; each %{$_[0][0]} }
232 sub NEXTKEY { each %{$_[0][0]} }
233}
234leak(2,!!$Config{mad}, sub {
235 eval q`
236 BEGIN {
237 $hhtie::explosive = 0;
238 tie %^H, hhtie;
239 $^H{foo} = bar;
240 $hhtie::explosive = 1;
241 }
242 { 1; }
243 `;
244}, 'hint-hash copying does not leak');
33b889b0
RZ
245
246package explosive_array {
247 sub TIEARRAY { bless [[], {}], $_[0] }
248 sub FETCH { die if $_[0]->[1]{FETCH}; $_[0]->[0][$_[1]] }
249 sub FETCHSIZE { die if $_[0]->[1]{FETCHSIZE}; scalar @{ $_[0]->[0] } }
250 sub STORE { die if $_[0]->[1]{STORE}; $_[0]->[0][$_[1]] = $_[2] }
251 sub CLEAR { die if $_[0]->[1]{CLEAR}; @{$_[0]->[0]} = () }
252 sub EXTEND { die if $_[0]->[1]{EXTEND}; return }
253 sub explode { my $self = shift; $self->[1] = {@_} }
254}
255
256leak(2, 0, sub {
257 tie my @a, 'explosive_array';
258 tied(@a)->explode( STORE => 1 );
259 my $x = 0;
260 eval { @a = ($x) };
261}, 'explosive array assignment does not leak');
262
263leak(2, 0, sub {
264 my ($a, $b);
265 eval { warn $die_on_fetch };
266}, 'explosive warn argument');
267
268leak(2, 0, sub {
269 my $foo = sub { return $die_on_fetch };
270 my $res = eval { $foo->() };
271 my @res = eval { $foo->() };
272}, 'function returning explosive does not leak');
273
274leak(2, 0, sub {
275 my $res = eval { {$die_on_fetch, 0} };
276 $res = eval { {0, $die_on_fetch} };
277}, 'building anon hash with explosives does not leak');
278
279leak(2, 0, sub {
3ed356df
FC
280 my $res = eval { [$die_on_fetch] };
281}, 'building anon array with explosives does not leak');
282
283leak(2, 0, sub {
33b889b0
RZ
284 my @a;
285 eval { push @a, $die_on_fetch };
286}, 'pushing exploding scalar does not leak');
ddac780e 287
644ac3a8
FC
288leak(2, 0, sub {
289 eval { push @-, '' };
290}, 'pushing onto read-only array does not leak');
291
ddac780e
FC
292
293# Run-time regexp code blocks
294{
2ac13048
FC
295 use re 'eval';
296 my @tests = ('[(?{})]','(?{})');
ddac780e
FC
297 for my $t (@tests) {
298 leak(2, 0, sub {
299 / $t/;
300 }, "/ \$x/ where \$x is $t does not leak");
301 leak(2, 0, sub {
302 /(?{})$t/;
303 }, "/(?{})\$x/ where \$x is $t does not leak");
304 }
305}
104c40b0
FC
306
307
308{
309 use warnings FATAL => 'all';
310 leak(2, 0, sub {
d861347e 311 no warnings 'once';
104c40b0
FC
312 eval { printf uNopened 42 };
313 }, 'printfing to bad handle under fatal warnings does not leak');
c7bd8b84
FC
314 open my $fh, ">", \my $buf;
315 leak(2, 0, sub {
316 eval { printf $fh chr 2455 };
317 }, 'wide fatal warning does not make printf leak');
318 close $fh or die $!;
104c40b0 319}