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 | ||
2ac13048 | 18 | plan tests => 42; |
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 | ||
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 | # | |
40 | sub 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 |
57 | my @a; |
58 | ||
59 | leak(5, 0, sub {}, "basic check 1 of leak test infrastructure"); | |
60 | leak(5, 0, sub {push @a,1;pop @a}, "basic check 2 of leak test infrastructure"); | |
61 | leak(5, 1, sub {push @a,1;}, "basic check 3 of leak test infrastructure"); | |
459defa1 DM |
62 | |
63 | sub TIEARRAY { bless [], $_[0] } | |
64 | sub FETCH { $_[0]->[$_[1]] } | |
65 | sub 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 | 75 | leak_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 | |
119 | SKIP: | |
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 | ||
136 | leak(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 | |
164 | leak(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 |
170 | SKIP: { |
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 | 175 | SKIP: { |
a121486f | 176 | skip "disabled under -Dmad (eval leaks)", 5 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//'); | |
2d85e411 | 185 | } |
9fa29fa7 FC |
186 | |
187 | # [perl #114764] Attributes leak scalars | |
188 | leak(2, 0, sub { eval 'my $x : shared' }, 'my $x :shared used to leak'); | |
895cdc83 FC |
189 | |
190 | # Tied hash iteration was leaking if the hash was freed before itera- | |
191 | # tion was over. | |
192 | package t { | |
193 | sub TIEHASH { bless [] } | |
194 | sub FIRSTKEY { 0 } | |
195 | } | |
196 | leak(2, 0, sub { | |
197 | my $h = {}; | |
198 | tie %$h, t; | |
199 | each %$h; | |
200 | undef $h; | |
201 | }, 'tied hash iteration does not leak'); | |
9c744f4f | 202 | |
33b889b0 RZ |
203 | package explosive_scalar { |
204 | sub TIESCALAR { my $self = shift; bless [undef, {@_}], $self } | |
205 | sub FETCH { die 'FETCH' if $_[0][1]{FETCH}; $_[0][0] } | |
206 | sub STORE { die 'STORE' if $_[0][1]{STORE}; $_[0][0] = $_[1] } | |
207 | } | |
208 | tie my $die_on_fetch, 'explosive_scalar', FETCH => 1; | |
209 | ||
39984de3 FC |
210 | # List assignment was leaking when assigning explosive scalars to |
211 | # aggregates. | |
9c744f4f | 212 | leak(2, 0, sub { |
33b889b0 RZ |
213 | eval {%a = ($die_on_fetch, 0)}; # key |
214 | eval {%a = (0, $die_on_fetch)}; # value | |
215 | eval {%a = ($die_on_fetch, $die_on_fetch)}; # both | |
9c744f4f | 216 | }, 'hash assignment does not leak'); |
39984de3 | 217 | leak(2, 0, sub { |
33b889b0 RZ |
218 | eval {@a = ($die_on_fetch)}; |
219 | eval {($die_on_fetch, $b) = ($b, $die_on_fetch)}; | |
220 | # restore | |
221 | tie $die_on_fetch, 'explosive_scalar', FETCH => 1; | |
39984de3 | 222 | }, 'array assignment does not leak'); |
9c744f4f | 223 | |
0db511c0 FC |
224 | # [perl #107000] |
225 | package hhtie { | |
226 | sub TIEHASH { bless [] } | |
227 | sub STORE { $_[0][0]{$_[1]} = $_[2] } | |
228 | sub FETCH { die if $explosive; $_[0][0]{$_[1]} } | |
229 | sub FIRSTKEY { keys %{$_[0][0]}; each %{$_[0][0]} } | |
230 | sub NEXTKEY { each %{$_[0][0]} } | |
231 | } | |
232 | leak(2,!!$Config{mad}, sub { | |
233 | eval q` | |
234 | BEGIN { | |
235 | $hhtie::explosive = 0; | |
236 | tie %^H, hhtie; | |
237 | $^H{foo} = bar; | |
238 | $hhtie::explosive = 1; | |
239 | } | |
240 | { 1; } | |
241 | `; | |
242 | }, 'hint-hash copying does not leak'); | |
33b889b0 RZ |
243 | |
244 | package explosive_array { | |
245 | sub TIEARRAY { bless [[], {}], $_[0] } | |
246 | sub FETCH { die if $_[0]->[1]{FETCH}; $_[0]->[0][$_[1]] } | |
247 | sub FETCHSIZE { die if $_[0]->[1]{FETCHSIZE}; scalar @{ $_[0]->[0] } } | |
248 | sub STORE { die if $_[0]->[1]{STORE}; $_[0]->[0][$_[1]] = $_[2] } | |
249 | sub CLEAR { die if $_[0]->[1]{CLEAR}; @{$_[0]->[0]} = () } | |
250 | sub EXTEND { die if $_[0]->[1]{EXTEND}; return } | |
251 | sub explode { my $self = shift; $self->[1] = {@_} } | |
252 | } | |
253 | ||
254 | leak(2, 0, sub { | |
255 | tie my @a, 'explosive_array'; | |
256 | tied(@a)->explode( STORE => 1 ); | |
257 | my $x = 0; | |
258 | eval { @a = ($x) }; | |
259 | }, 'explosive array assignment does not leak'); | |
260 | ||
261 | leak(2, 0, sub { | |
262 | my ($a, $b); | |
263 | eval { warn $die_on_fetch }; | |
264 | }, 'explosive warn argument'); | |
265 | ||
266 | leak(2, 0, sub { | |
267 | my $foo = sub { return $die_on_fetch }; | |
268 | my $res = eval { $foo->() }; | |
269 | my @res = eval { $foo->() }; | |
270 | }, 'function returning explosive does not leak'); | |
271 | ||
272 | leak(2, 0, sub { | |
273 | my $res = eval { {$die_on_fetch, 0} }; | |
274 | $res = eval { {0, $die_on_fetch} }; | |
275 | }, 'building anon hash with explosives does not leak'); | |
276 | ||
277 | leak(2, 0, sub { | |
3ed356df FC |
278 | my $res = eval { [$die_on_fetch] }; |
279 | }, 'building anon array with explosives does not leak'); | |
280 | ||
281 | leak(2, 0, sub { | |
33b889b0 RZ |
282 | my @a; |
283 | eval { push @a, $die_on_fetch }; | |
284 | }, 'pushing exploding scalar does not leak'); | |
ddac780e FC |
285 | |
286 | ||
287 | # Run-time regexp code blocks | |
288 | { | |
2ac13048 FC |
289 | use re 'eval'; |
290 | my @tests = ('[(?{})]','(?{})'); | |
ddac780e FC |
291 | for my $t (@tests) { |
292 | leak(2, 0, sub { | |
293 | / $t/; | |
294 | }, "/ \$x/ where \$x is $t does not leak"); | |
295 | leak(2, 0, sub { | |
296 | /(?{})$t/; | |
297 | }, "/(?{})\$x/ where \$x is $t does not leak"); | |
298 | } | |
299 | } |