In perl.git, the branch blead has been updated <http://perl5.git.perl.org/perl.git/commitdiff/7ed1d857c7b7016b9bde564c6802a4721d903d95?hp=201e9e2aa1088e65b7160f94706673641f1e018a>
- Log ----------------------------------------------------------------- commit 7ed1d857c7b7016b9bde564c6802a4721d903d95 Author: Daniel Dragan <[email protected]> Date: Wed Jun 24 15:48:12 2015 -0400 fix #124181 double free/refcnt problems in IO types in typemap commit 50e5165b96 "stop T_IN/OUT/INOUT/STDIO typemaps leaking" changed newRV to newRV_noinc, but the GV * returned by newGVgen() is owned by the package tree, like the SV * returned by get_sv(). Now when the RV to GV is freed on mortal stack, the GV * in the package tree is freed, and now there is a freed GV * in the package tree, if you turn on "PERL_DESTRUCT_LEVEL=2" (and perhaps DEBUGGING is needed too), the package tree is destroyed SV * by SV *, and perl will eventually warn with "Attempt to free unreferenced scalar" which a very bad panic type warning. commit 50e5165b96 was reverted in commit bae466e878 "Revert "stop T_IN/OUT/INOUT/STDIO typemaps leaking" for 5.22's release to stop the panic, but reintroduced the SV/RV leak. So fix the RV leak (the val passed as source arg of sv_setsv) by freeing it after the copying. In a very unlikely scenario, the RV could still leak if sv_setsv dies. Also fix the problem, that if this OUTPUT: type is being used for an incoming arg, not the outgoing RETVAL arg, you can't assign a new SV* ontop of the old one, that only works for perl stack return args, so replace "$arg = &PL_sv_undef;" with "sv_setsv($arg, &PL_sv_undef);" if its not RETVAL, this way OUTPUT on incoming args also works if it goes down the error path. For efficiency, in a RETVAL siutation, let the undef original SV* in $arg which is typically obtained from sv_newmortal() by xsubpp pass through if we error out. Also for efficiency, if it is RETVAL (which is more common) dont do the sv_setsv/SvREFCNT_dec_NN stuff (2 function calls), just mortalize (1 function call) the ex-temp RV and arrange for the RV to wind up on perl stack. Also, the GV * already knows what HV * stash it belongs to, so avoid the stash lookup done by gv_stashpv() and just use GvSTASH which are simple pointer derefs. M lib/ExtUtils/typemap M pod/perldelta.pod commit c1b8440fca7358a5c52763ce726d40026870519c Author: Daniel Dragan <[email protected]> Date: Sun May 10 11:36:05 2015 -0400 add test that fails for #124181 to Typemap.t These tests will either fail with harness, and randomly SEGV for me, which is intentional since they are testing memory corruption. M ext/XS-Typemap/Typemap.pm M ext/XS-Typemap/Typemap.xs M ext/XS-Typemap/t/Typemap.t ----------------------------------------------------------------------- Summary of changes: ext/XS-Typemap/Typemap.pm | 4 ++-- ext/XS-Typemap/Typemap.xs | 9 +++++++++ ext/XS-Typemap/t/Typemap.t | 20 ++++++++++++++++++-- lib/ExtUtils/typemap | 40 ++++++++++++++++++++++++++++------------ pod/perldelta.pod | 5 ++++- 5 files changed, 61 insertions(+), 17 deletions(-) diff --git a/ext/XS-Typemap/Typemap.pm b/ext/XS-Typemap/Typemap.pm index de3319b..a1ae021 100644 --- a/ext/XS-Typemap/Typemap.pm +++ b/ext/XS-Typemap/Typemap.pm @@ -36,7 +36,7 @@ require XSLoader; use vars qw/ $VERSION @EXPORT /; -$VERSION = '0.13'; +$VERSION = '0.14'; @EXPORT = (qw/ T_SV @@ -76,7 +76,7 @@ $VERSION = '0.13'; T_OPAQUEPTR_IN T_OPAQUEPTR_OUT T_OPAQUEPTR_OUT_short T_OPAQUEPTR_IN_struct T_OPAQUEPTR_OUT_struct T_ARRAY - T_STDIO_open T_STDIO_close T_STDIO_print + T_STDIO_open T_STDIO_open_ret_in_arg T_STDIO_close T_STDIO_print T_PACKED_in T_PACKED_out T_PACKEDARRAY_in T_PACKEDARRAY_out T_INOUT T_IN T_OUT diff --git a/ext/XS-Typemap/Typemap.xs b/ext/XS-Typemap/Typemap.xs index 3fa0e74ab..8314cc2 100644 --- a/ext/XS-Typemap/Typemap.xs +++ b/ext/XS-Typemap/Typemap.xs @@ -906,6 +906,15 @@ T_STDIO_open( file ) OUTPUT: RETVAL +void +T_STDIO_open_ret_in_arg( file, io) + const char * file + FILE * io = NO_INIT + CODE: + io = xsfopen( file ); + OUTPUT: + io + SysRet T_STDIO_close( f ) PerlIO * f diff --git a/ext/XS-Typemap/t/Typemap.t b/ext/XS-Typemap/t/Typemap.t index 27b4086..49ac479 100644 --- a/ext/XS-Typemap/t/Typemap.t +++ b/ext/XS-Typemap/t/Typemap.t @@ -6,10 +6,11 @@ BEGIN { } } -use Test::More tests => 152; +use Test::More tests => 156; use strict; -use warnings; +#catch WARN_INTERNAL type errors, and anything else unexpected +use warnings FATAL => 'all'; use XS::Typemap; pass(); @@ -213,6 +214,7 @@ is( T_PV("a string"), "a string"); is( T_PV(52), 52); ok !defined T_PV_null, 'RETVAL = NULL returns undef for char*'; { + use warnings NONFATAL => 'all'; my $uninit; local $SIG{__WARN__} = sub { ++$uninit if shift =~ /uninit/ }; () = ''.T_PV_null; @@ -393,6 +395,16 @@ if (defined $fh) { } } +$fh = "FOO"; +T_STDIO_open_ret_in_arg( $testfile, $fh); +ok( $fh ne "FOO", 'return io in arg open succeeds'); +ok( print($fh "first line\n"), 'can print to return io in arg'); +ok( close($fh), 'can close return io in arg'); +$fh = "FOO"; +#now with a bad file name to make sure $fh is written to on failure +T_STDIO_open_ret_in_arg( "", $fh); +ok( !defined$fh, 'return io in arg open failed successfully'); + # T_INOUT note("T_INOUT"); SCOPE: { @@ -439,6 +451,10 @@ SCOPE: { ok(!close $fh2); } +# Perl RT #124181 SEGV due to double free in typemap +# "Attempt to free unreferenced scalar" +%{*{main::XS::}{HASH}} = (); + sub is_approx { my ($l, $r, $n) = @_; if (not defined $l or not defined $r) { diff --git a/lib/ExtUtils/typemap b/lib/ExtUtils/typemap index 0b09641..5f61527 100644 --- a/lib/ExtUtils/typemap +++ b/lib/ExtUtils/typemap @@ -388,32 +388,48 @@ T_STDIO { GV *gv = newGVgen("$Package"); PerlIO *fp = PerlIO_importFILE($var,0); - if ( fp && do_open(gv, "+<&", 3, FALSE, 0, 0, fp) ) - sv_setsv($arg, sv_bless(newRV((SV*)gv), gv_stashpv("$Package",1))); + if ( fp && do_open(gv, "+<&", 3, FALSE, 0, 0, fp) ) { + SV *rv = newRV_inc((SV*)gv); + rv = sv_bless(rv, GvSTASH(gv)); + ${"$var" eq "RETVAL" ? \"$arg = sv_2mortal(rv);" + : \"sv_setsv($arg, rv);\n\t\tSvREFCNT_dec_NN(rv);"} + }${"$var" ne "RETVAL" ? \" else - $arg = &PL_sv_undef; + sv_setsv($arg, &PL_sv_undef);\n" : \""} } T_IN { GV *gv = newGVgen("$Package"); - if ( do_open(gv, "<&", 2, FALSE, 0, 0, $var) ) - sv_setsv($arg, sv_bless(newRV((SV*)gv), gv_stashpv("$Package",1))); + if ( do_open(gv, "<&", 2, FALSE, 0, 0, $var) ) { + SV *rv = newRV_inc((SV*)gv); + rv = sv_bless(rv, GvSTASH(gv)); + ${"$var" eq "RETVAL" ? \"$arg = sv_2mortal(rv);" + : \"sv_setsv($arg, rv);\n\t\tSvREFCNT_dec_NN(rv);"} + }${"$var" ne "RETVAL" ? \" else - $arg = &PL_sv_undef; + sv_setsv($arg, &PL_sv_undef);\n" : \""} } T_INOUT { GV *gv = newGVgen("$Package"); - if ( do_open(gv, "+<&", 3, FALSE, 0, 0, $var) ) - sv_setsv($arg, sv_bless(newRV((SV*)gv), gv_stashpv("$Package",1))); + if ( do_open(gv, "+<&", 3, FALSE, 0, 0, $var) ) { + SV *rv = newRV_inc((SV*)gv); + rv = sv_bless(rv, GvSTASH(gv)); + ${"$var" eq "RETVAL" ? \"$arg = sv_2mortal(rv);" + : \"sv_setsv($arg, rv);\n\t\tSvREFCNT_dec_NN(rv);"} + }${"$var" ne "RETVAL" ? \" else - $arg = &PL_sv_undef; + sv_setsv($arg, &PL_sv_undef);\n" : \""} } T_OUT { GV *gv = newGVgen("$Package"); - if ( do_open(gv, "+>&", 3, FALSE, 0, 0, $var) ) - sv_setsv($arg, sv_bless(newRV((SV*)gv), gv_stashpv("$Package",1))); + if ( do_open(gv, "+>&", 3, FALSE, 0, 0, $var) ) { + SV *rv = newRV_inc((SV*)gv); + rv = sv_bless(rv, GvSTASH(gv)); + ${"$var" eq "RETVAL" ? \"$arg = sv_2mortal(rv);" + : \"sv_setsv($arg, rv);\n\t\tSvREFCNT_dec_NN(rv);"} + }${"$var" ne "RETVAL" ? \" else - $arg = &PL_sv_undef; + sv_setsv($arg, &PL_sv_undef);\n" : \""} } diff --git a/pod/perldelta.pod b/pod/perldelta.pod index d985359..68df77f 100644 --- a/pod/perldelta.pod +++ b/pod/perldelta.pod @@ -375,7 +375,10 @@ files in F<ext/> and F<lib/> are best summarized in L</Modules and Pragmata>. =item * -XXX +A leak in the XS typemap caused one scalar to be leaked each time a C<FILE *> +or a C<PerlIO *> was C<OUTPUT:>ed or imported to Perl, since perl 5.000. These +particular typemap entries are thought to be extremely rarely used by XS +modules. [perl #124181] =back -- Perl5 Master Repository
