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