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