You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
goto refcount increased by one when using goto #16749
While developing Test::MockFile across perls betwen 5.10.1 and 5.28, I've
discovered a discrepancy on how perl sometimes bumps refcount for of args
passed into CORE::GLOBAL:: subroutines.
This showed up primarily when overriding CORE::GLOBAL::open and discovering
that the file handle wasn't autoclosing when the file handle went out of
scope.
The simplest example of this problem is:
---
#!perl
use B ();
BEGIN {
*CORE::GLOBAL::open = sub (*;$@) {
goto \&CORE::open;
};
}
When the compiler (op.c) can’t figure out the name of a vivified file-
handle based on the variable name, then pp.c:S_rv2gv (which vivifies
the handle at run time) calls newGVgen, which generates something
named _GEN_0 or suchlike.
When it does that, the reference counting is wrong, because the stash
gets a *_GEN_0 typeglob and the reference stored in open’s argument
points to it, too; but the reference count is nevertheless 1. So
if both sources shed their pointers to the GV, then you get a
double free.
Because usually the typeglob sits in the stash until program exit,
this bug has gone unnoticed for a long time.
This bug appears to have been present ever since rv2gv started call-
ing newGVgen, in 2c8ac47.
On Thu, Nov 15, 2018 at 10:38:36AM -0800, Todd Rinaldo (via RT) wrote:
While developing Test::MockFile across perls betwen 5.10.1 and 5.28, I've
discovered a discrepancy on how perl sometimes bumps refcount for of args
passed into CORE::GLOBAL:: subroutines.
This showed up primarily when overriding CORE::GLOBAL::open and discovering
that the file handle wasn't autoclosing when the file handle went out of
scope.
The simplest example of this problem is:
---
#!perl
use B ();
BEGIN {
*CORE::GLOBAL::open = sub (*;$@) {
goto \&CORE::open;
};
}
--
31 December 1661: "I have newly taken a solemne oath about abstaining from
plays". 1 January 1662: "And after ... we went by coach to the play".
-- The Diary of Samuel Pepys
commit db9848c (refs/bisect/bad)
Author: Zefram <zefram@fysh.org>
Date: Sun Dec 10 01:34:04 2017 +0000
stop gensyming when vivifying IO handles
When open() is given as a handle a scalar with undef value, the rv2gv
op creates a new glob for the I/O handle, and mutates the scalar to
contain an RV referencing the glob. This is documented behaviour.
The question arises of what GvNAME the glob should have. There's some
compile-time logic that spots that this might happen, and if the handle
expression is simple enough it stores in the pad a name representing
that expression, and rv2gv uses that for the GvNAME. But if no name
was supplied by that route then rv2gv was using newGVgen() to generate
the glob. That succeeds in giving it some kind of name, but has the
unwanted side effect of interning the glob in the stash under that name.
From the user's point of view, that creates a phantom reference to the
glob, which means that killing the RV doesn't remove the last reference
to the glob and so doesn't close the handle.
Instead of gensyming, just create an uninterned glob and give it a
fixed GvNAME. Fixes [perl #115814].
The only other action I can see here is to make sure we have a test for this. I need to look into the tests some more. The tests added in that commit are a skip and I'm not clear if we'll detect the refcount issue the next time this happens.
On Tue, 20 Nov 2018 08:14:16 -0800, todd.e.rinaldo@gmail.com wrote:
The only other action I can see here is to make sure we have a test
for this. I need to look into the tests some more. The tests added in
that commit are a skip and I'm not clear if we'll detect the refcount
issue the next time this happens.
From 88f17ff8910eefba68388fbf055d9d31f822c641 Mon Sep 17 00:00:00 2001
From: Tony Cook <tony@develop-help.com>
Date: Wed, 6 Feb 2019 15:42:10 +1100
Subject: (perl #133660) add test for goto &sub in overload leaking
The bug in this case was fixed in db9848c8d.
---
t/op/svleak.t | 22 +++++++++++++++++++++-
1 file changed, 21 insertions(+), 1 deletion(-)
diff --git a/t/op/svleak.t b/t/op/svleak.t
index 3283c95cbf..bfa6747a02 100644
--- a/t/op/svleak.t+++ b/t/op/svleak.t@@ -15,7 +15,7 @@ BEGIN {
use Config;
-plan tests => 149;+plan tests => 150;
# run some code N times. If the number of SVs at the end of loop N is
# greater than (N-1)*delta at the end of loop 1, we've got a leak
@@ -625,3 +625,23 @@ SKIP: {
sub Regex_Key_Leak { my ($r)= keys %rh; "foo"=~$r; }
leak 2, 0, \&Regex_Key_Leak,"RT #132892 - regex patterns should not leak";
}
++{+ # perl #133660+ fresh_perl_is(<<'PERL', "ok", {}, "check goto core sub doesn't leak");+# done this way to avoid overloads for all of svleak.t+use B;+BEGIN {+ *CORE::GLOBAL::open = sub (*;$@) {+ goto \&CORE::open;+ };+}++my $refcount;+{+ open(my $fh, '<', 'TEST');+ my $sv = B::svref_2object($fh);+ print $sv->REFCNT == 1 ? "ok" : "not ok";+}+PERL+}--
2.11.0
On Tue, 05 Feb 2019 20:42:46 -0800, tonyc wrote:
On Tue, 20 Nov 2018 08:14:16 -0800, todd.e.rinaldo@gmail.com wrote:
The only other action I can see here is to make sure we have a test
for this. I need to look into the tests some more. The tests added in
that commit are a skip and I'm not clear if we'll detect the refcount
issue the next time this happens.
I'll try to check them later this week.
Something like the attached?
That's it!
Fails on maint-5.26 as expected:
# Failed test 142 - check goto core sub doesn't leak at ./test.pl line 1059
# got "not ok"
# expected "ok"
# PROG:
#
# # done this way to avoid overloads for all of svleak.t
# use B;
# BEGIN {
# *CORE::GLOBAL::open = sub (*;$@) {
# goto \&CORE::open;
# };
# }
#
# my $refcount;
# {
# open(my $fh, '<', 'TEST');
# my $sv = B::svref_2object($fh);
# print $sv->REFCNT == 1 ? "ok" : "not ok";
# }
# STATUS: 0
On Thu, 07 Feb 2019 06:46:10 -0800, todd.e.rinaldo@gmail.com wrote:
On Tue, 05 Feb 2019 20:42:46 -0800, tonyc wrote:
On Tue, 20 Nov 2018 08:14:16 -0800, todd.e.rinaldo@gmail.com wrote:
The only other action I can see here is to make sure we have a test
for this. I need to look into the tests some more. The tests added in
that commit are a skip and I'm not clear if we'll detect the refcount
issue the next time this happens.
I'll try to check them later this week.
Something like the attached?
That's it!
Fails on maint-5.26 as expected:
# Failed test 142 - check goto core sub doesn't leak at ./test.pl line 1059
# got "not ok"
# expected "ok"
# PROG:
#
# # done this way to avoid overloads for all of svleak.t
# use B;
# BEGIN {
# *CORE::GLOBAL::open = sub (*;$@) {
# goto \&CORE::open;
# };
# }
#
# my $refcount;
# {
# open(my $fh, '<', 'TEST');
# my $sv = B::svref_2object($fh);
# print $sv->REFCNT == 1 ? "ok" : "not ok";
# }
# STATUS: 0
Activity
p5pRT commentedon Nov 15, 2018
From @toddr
Created by @toddr
While developing Test::MockFile across perls betwen 5.10.1 and 5.28, I've
discovered a discrepancy on how perl sometimes bumps refcount for of args
passed into CORE::GLOBAL:: subroutines.
This showed up primarily when overriding CORE::GLOBAL::open and discovering
that the file handle wasn't autoclosing when the file handle went out of
scope.
The simplest example of this problem is:
---
#!perl
use B ();
BEGIN {
*CORE::GLOBAL::open = sub (*;$@) {
goto \&CORE::open;
};
}
my $refcount;
{
open(my $fh, '<', '/etc/passwd');
my $sv = B::svref_2object($fh);
$refcount = $sv->REFCNT;
}
print "REF=$refcount\n";
exit($refcount == 1 ? 0 : $refcount);
---
On 5.28, the refcount for $fh is 2. But in 5.20, the refcount is 1.
I can make the problem go away by doing this instead of goto: return
CORE::open($_[0], ...);
I also notice the problem doesn't manifest on 5.28 if I do this instead:
---
#!/usr/local/cpanel/3rdparty/bin/perl
use B ();
BEGIN {
*CORE::GLOBAL::open = sub (*;$@) {
goto \&CORE::open;
};
}
my $count = 1;
trynow();
trynow();
trynow();
trynow();
sub trynow {
open(my $fh, '<', '/etc/passwd');
my $sv = B::svref_2object($fh);
$refcount = $sv->REFCNT;
print "REFCNT for run " . $count++ . " is $refcount\n";
}
---
So this doesn't seem to be a global problem.
Perl Info
p5pRT commentedon Nov 15, 2018
From @toddr
I was able to automatically bisect this to 7bdb4ff which first showed up in v5.21.10.
commit 7bdb4ff (refs/bisect/bad)
Author: Father Chrysostomos <sprout@cpan.org>
Date: Tue Sep 2 22:11:08 2014 -0700
Fix refcounting in rv2gv when it calls newGVgen
When the compiler (op.c) can’t figure out the name of a vivified file-
handle based on the variable name, then pp.c:S_rv2gv (which vivifies
the handle at run time) calls newGVgen, which generates something
named _GEN_0 or suchlike.
When it does that, the reference counting is wrong, because the stash
gets a *_GEN_0 typeglob and the reference stored in open’s argument
points to it, too; but the reference count is nevertheless 1. So
if both sources shed their pointers to the GV, then you get a
double free.
Because usually the typeglob sits in the stash until program exit,
this bug has gone unnoticed for a long time.
This bug appears to have been present ever since rv2gv started call-
ing newGVgen, in 2c8ac47.
pp.c | 1 +
t/op/gv.t | 14 +++++++++++++-
2 files changed, 14 insertions(+), 1 deletion(-)
p5pRT commentedon Nov 15, 2018
The RT System itself - Status changed from 'new' to 'open'
p5pRT commentedon Nov 19, 2018
From @iabyn
On Thu, Nov 15, 2018 at 10:38:36AM -0800, Todd Rinaldo (via RT) wrote:
Running the above I don't see that in 5.27.7 and above:
$ perl5200 ~/tmp/p
REF=1
$ perl5220 ~/tmp/p
REF=2
$ perl5240 ~/tmp/p
REF=2
$ perl5260 ~/tmp/p
REF=2
$ perl5275 ~/tmp/p
REF=2
$ perl5276 ~/tmp/p
REF=2
$ perl5277 ~/tmp/p
REF=1
$ perl5278 ~/tmp/p
REF=1
$ perl5280 ~/tmp/p
REF=1
$ perl5294 ~/tmp/p
REF=1
--
31 December 1661: "I have newly taken a solemne oath about abstaining from
plays". 1 January 1662: "And after ... we went by coach to the play".
-- The Diary of Samuel Pepys
p5pRT commentedon Nov 20, 2018
From @toddr
Which means it was fixed in:
commit db9848c (refs/bisect/bad)
Author: Zefram <zefram@fysh.org>
Date: Sun Dec 10 01:34:04 2017 +0000
stop gensyming when vivifying IO handles
When open() is given as a handle a scalar with undef value, the rv2gv
op creates a new glob for the I/O handle, and mutates the scalar to
contain an RV referencing the glob. This is documented behaviour.
The question arises of what GvNAME the glob should have. There's some
compile-time logic that spots that this might happen, and if the handle
expression is simple enough it stores in the pad a name representing
that expression, and rv2gv uses that for the GvNAME. But if no name
was supplied by that route then rv2gv was using newGVgen() to generate
the glob. That succeeds in giving it some kind of name, but has the
unwanted side effect of interning the glob in the stash under that name.
From the user's point of view, that creates a phantom reference to the
glob, which means that killing the RV doesn't remove the last reference
to the glob and so doesn't close the handle.
Instead of gensyming, just create an uninterned glob and give it a
fixed GvNAME. Fixes [perl #115814].
pp.c | 12 +++++-------
t/io/open.t | 29 ++++++++++++++++++++++++++---
t/op/coreamp.t | 2 +-
3 files changed, 32 insertions(+), 11 deletions(-)
p5pRT commentedon Nov 20, 2018
From @toddr
The only other action I can see here is to make sure we have a test for this. I need to look into the tests some more. The tests added in that commit are a skip and I'm not clear if we'll detect the refcount issue the next time this happens.
I'll try to check them later this week.
p5pRT commentedon Feb 6, 2019
From @tonycoz
On Tue, 20 Nov 2018 08:14:16 -0800, todd.e.rinaldo@gmail.com wrote:
Something like the attached?
Tony
p5pRT commentedon Feb 6, 2019
From @tonycoz
0001-perl-133660-add-test-for-goto-sub-in-overload-leakin.patch
p5pRT commentedon Feb 7, 2019
From @toddr
On Tue, 05 Feb 2019 20:42:46 -0800, tonyc wrote:
That's it!
Fails on maint-5.26 as expected:
# Failed test 142 - check goto core sub doesn't leak at ./test.pl line 1059
# got "not ok"
# expected "ok"
# PROG:
#
# # done this way to avoid overloads for all of svleak.t
# use B;
# BEGIN {
# *CORE::GLOBAL::open = sub (*;$@) {
# goto \&CORE::open;
# };
# }
#
# my $refcount;
# {
# open(my $fh, '<', 'TEST');
# my $sv = B::svref_2object($fh);
# print $sv->REFCNT == 1 ? "ok" : "not ok";
# }
# STATUS: 0
And passes on maint-5.28
ok 150 - check goto core sub doesn't leak
I'll let you do the honors.
Thanks,
Todd
p5pRT commentedon Feb 13, 2019
From @tonycoz
On Thu, 07 Feb 2019 06:46:10 -0800, todd.e.rinaldo@gmail.com wrote:
Applied as ac6d259.
Tony
p5pRT commentedon Feb 13, 2019
@tonycoz - Status changed from 'open' to 'pending release'
p5pRT commentedon May 22, 2019
From @khwilliamson
Thank you for filing this report. You have helped make Perl better.
With the release today of Perl 5.30.0, this and 160 other issues have been
resolved.
Perl 5.30.0 may be downloaded via:
https://metacpan.org/release/XSAWYERX/perl-5.30.0
If you find that the problem persists, feel free to reopen this ticket.
p5pRT commentedon May 22, 2019
@khwilliamson - Status changed from 'pending release' to 'resolved'