This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
Don’t leak deleted iterator when tying hash
[perl5.git] / t / op / svleak.t
... / ...
CommitLineData
1#!./perl
2
3# A place to put some simple leak tests. Uses XS::APItest to make
4# PL_sv_count available, allowing us to run a bit of code multiple times and
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
16use Config;
17
18plan tests => 28;
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
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
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");
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
73# [perl #74484] repeated tries leaked SVs on the tmps stack
74
75leak_expr(5, 0, q{"YYYYYa" =~ /.+?(a(.+?)|b)/ }, "trie leak");
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}
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}
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);
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}
163
164leak(2,0,sub { !$^V }, '[perl #109762] version object in boolean context');
165
166
167# [perl #114356] run-time rexexp with unchanging pattern got
168# inflated refcounts
169
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
175SKIP: {
176 skip "disabled under -Dmad (eval leaks)", 5 if $Config{mad};
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');
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//');
185}
186
187# [perl #114764] Attributes leak scalars
188leak(2, 0, sub { eval 'my $x : shared' }, 'my $x :shared used to leak');