In perl.git, the branch blead has been updated <http://perl5.git.perl.org/perl.git/commitdiff/adf8f095c5881bcedf07b8e41072f8125e00b5a6?hp=f5d8aca1e51e677519775b879a4d41b6f78d07d2>
- Log ----------------------------------------------------------------- commit adf8f095c5881bcedf07b8e41072f8125e00b5a6 Author: Nicholas Clark <[email protected]> Date: Fri Feb 26 09:18:44 2010 +0000 Set PADSTALE on all lexicals at the end of sub creation. The PADSTALEness of lexicals between the 0th and 1st call to a subroutine is now consistent with the state between the nth and (n + 1)th call. This permits a work around in Perl_padlist_dup() to avoid leaking active pad data into a new thread, whilst still correctly bodging the external references needed by the current ?{} implementation. Fix that, and this can be removed. M pad.c M t/op/threads.t commit 05d04d9c74ee968bace5e063c9ded74f94b3df24 Author: Nicholas Clark <[email protected]> Date: Thu Feb 25 21:35:39 2010 +0000 Don't clone the contents of lexicals in pads. This stops the values of lexicals in active stack frames in the parent leaking into the lexicals in the child thread. With an exception for lexicals with a reference count of > 1, to cope with the implementation of ?{{ ... }} blocks in regexps. :-( M pad.c M t/op/threads.t commit 4cee4ca8b28e9dadc530df8ce100439bc4a78e73 Author: Nicholas Clark <[email protected]> Date: Thu Feb 25 16:56:53 2010 +0000 In Perl_pad_tidy(), merge the SvPADTMP_on() loops for padtidy_SUB and _FORMAT. M pad.c commit 6de654a5795b6f7915432ff16bcdac0688492a9b Author: Nicholas Clark <[email protected]> Date: Thu Feb 25 14:21:18 2010 +0000 In Perl_padlist_dup() don't duplicate @_ or pads caused by recursion. CvDEPTH() is 0 in a new thread, so duplicating pads beyond the always-present first level is a waste of effort and memory. M pad.c commit d5b1589c09b534ccfeb2eae26b3de9339c1bf22b Author: Nicholas Clark <[email protected]> Date: Wed Feb 10 09:57:23 2010 +0000 Convert PAD_DUP to a function Perl_padlist_dup(). assert() that pads are never AvREAL(). M embed.fnc M embed.h M pad.c M proto.h M sv.c commit e42956688f2e0df936f1a42811962946e4e185bf Author: Nicholas Clark <[email protected]> Date: Thu Feb 25 11:12:03 2010 +0000 When deleting CLONE_PARAMS, push any unreferenced SVs onto the temps stack. Effectively this leaves the cloned-into interpreter in a consistent state. In the cloned-from interpreter, the SV targets of non-reference owning pointers *are* referenced and managed by other pointers. SvREFCNT() == 0 SVs in the cloned-into interpreter result from the non-reference owning pointers being found and followed, but the reference owning and managing pointers not being part of the subsection of interpreter state cloned over. Hence, this change creates reference owning pointers to this SVs on the temps stack, which ensures that they are correctly cleaned up, and don't "leak" until interpreter destruction. (Which might be some time away, in a persistent process.) M embed.fnc M embed.h M proto.h M sv.c M t/op/threads.t commit d08d57ef17162c52e2024a3ba6755f778acbc697 Author: Nicholas Clark <[email protected]> Date: Wed Feb 24 17:15:41 2010 +0000 Better ithreads cloning - add all SVs with a 0 refcnt to the temps stack. Track all SVs created by sv_dup() that have a 0 reference count. If they still have a 0 reference count at the end of cloning, assign a reference to each to the temps stack. As the temps stack is cleared at thread exit, SVs book keeping will be correct and consistent before perl_destruct() makes its check for leaked scalars. Remove special case code for checking each @_ and the parent's temp stack. M embed.fnc M embed.h M proto.h M sv.c M sv.h M t/op/threads.t commit 1db366cc74404c47243e1d86efa59c6559db818e Author: Nicholas Clark <[email protected]> Date: Mon May 24 15:48:06 2010 +0100 Cleaner implementations for Perl_clone_params_{new,del} Not source or binary compatible with maint-5.12. M sv.c M sv.h commit f7abe70be985cb9179c2e728a593cb8a5c8e049d Author: Nicholas Clark <[email protected]> Date: Wed Feb 24 11:47:08 2010 +0000 Abstract *correct* initialisation of CLONE_PARAMS into Perl_clone_params_new(). As it allocates memory dynamically, add Perl_clone_params_del(). This will allow CLONE_PARAMS to be expand in future in a source and binary compatible fashion. These implementations of Perl_clone_params_new()/Perl_clone_params_del() jump through hoops to remain source and binary compatible, in particular, by not assuming that the structure member is present and correctly initialised. Hence they should be suitable for inclusion into Devel::PPPort. Convert threads.xs to use them, resolving RT #73046. M dist/threads/threads.xs M embed.fnc M embed.h M global.sym M proto.h M sv.c M t/op/threads.t commit a09252eb79f700c93c37322c1ad831cf3193571b Author: Nicholas Clark <[email protected]> Date: Tue Feb 23 14:48:17 2010 +0000 Convert Perl_sv_dup_inc() from a macro to a real function. M dist/threads/threads.xs M embed.fnc M embed.h M global.sym M hv.c M mro.c M proto.h M regcomp.c M sv.c commit 842c41230043ce99d4bf7b2c79aed85ce2908e89 Author: Nicholas Clark <[email protected]> Date: Wed Feb 24 15:07:51 2010 +0000 In perl_clone_using(), turn off AvREAL() on param->stashes. This is an optimisation, not a bug fix. M sv.c ----------------------------------------------------------------------- Summary of changes: dist/threads/threads.xs | 33 ++++----- embed.fnc | 19 +++++ embed.h | 36 ++++++++++ global.sym | 3 + hv.c | 4 +- mro.c | 9 +-- pad.c | 156 ++++++++++++++++++++++++++++++++++++++++--- proto.h | 45 +++++++++++++ regcomp.c | 5 +- sv.c | 167 ++++++++++++++++++++++++++++++++++++++--------- sv.h | 2 + t/op/threads.t | 77 +++++++++++++++++++--- 12 files changed, 474 insertions(+), 82 deletions(-) diff --git a/dist/threads/threads.xs b/dist/threads/threads.xs index a559595..f4e6946 100755 --- a/dist/threads/threads.xs +++ b/dist/threads/threads.xs @@ -774,7 +774,7 @@ S_ithread_create( * context for the duration of our work for new interpreter. */ { - CLONE_PARAMS clone_param; + CLONE_PARAMS *clone_param = Perl_clone_params_new(aTHX, thread->interp); dTHXa(thread->interp); @@ -786,13 +786,11 @@ S_ithread_create( SvREFCNT_dec(PL_endav); PL_endav = NULL; - clone_param.flags = 0; if (SvPOK(init_function)) { thread->init_function = newSV(0); sv_copypv(thread->init_function, init_function); } else { - thread->init_function = - SvREFCNT_inc(sv_dup(init_function, &clone_param)); + thread->init_function = sv_dup_inc(init_function, clone_param); } thread->params = params = newAV(); @@ -800,8 +798,9 @@ S_ithread_create( AvFILLp(params) = params_end - params_start - 1; array = AvARRAY(params); while (params_start < params_end) { - *array++ = SvREFCNT_inc(sv_dup(*params_start++, &clone_param)); + *array++ = SvREFCNT_inc(sv_dup(*params_start++, clone_param)); } + Perl_clone_params_del(clone_param); #if PERL_VERSION <= 8 && PERL_SUBVERSION <= 7 /* The code below checks that anything living on the tmps stack and @@ -1241,22 +1240,20 @@ ithread_join(...) /* Objects do not survive this process - FIXME */ if ((thread->gimme & G_WANT) != G_VOID) { AV *params_copy; - PerlInterpreter *other_perl; - CLONE_PARAMS clone_params; + PerlInterpreter *other_perl = thread->interp; + CLONE_PARAMS *clone_params = Perl_clone_params_new(other_perl, aTHX); params_copy = thread->params; - other_perl = thread->interp; - clone_params.stashes = newAV(); - clone_params.flags = CLONEf_JOIN_IN; + clone_params->flags |= CLONEf_JOIN_IN; PL_ptr_table = ptr_table_new(); S_ithread_set(aTHX_ thread); /* Ensure 'meaningful' addresses retain their meaning */ ptr_table_store(PL_ptr_table, &other_perl->Isv_undef, &PL_sv_undef); ptr_table_store(PL_ptr_table, &other_perl->Isv_no, &PL_sv_no); ptr_table_store(PL_ptr_table, &other_perl->Isv_yes, &PL_sv_yes); - params = (AV *)sv_dup((SV*)params_copy, &clone_params); + params = (AV *)sv_dup((SV*)params_copy, clone_params); S_ithread_set(aTHX_ current_thread); - SvREFCNT_dec(clone_params.stashes); + Perl_clone_params_del(clone_params); SvREFCNT_inc_void(params); ptr_table_free(PL_ptr_table); PL_ptr_table = NULL; @@ -1630,13 +1627,11 @@ ithread_error(...) /* If thread died, then clone the error into the calling thread */ if (thread->state & PERL_ITHR_DIED) { - PerlInterpreter *other_perl; - CLONE_PARAMS clone_params; + PerlInterpreter *other_perl = thread->interp; + CLONE_PARAMS *clone_params = Perl_clone_params_new(other_perl, aTHX); ithread *current_thread; - other_perl = thread->interp; - clone_params.stashes = newAV(); - clone_params.flags = CLONEf_JOIN_IN; + clone_params->flags |= CLONEf_JOIN_IN; PL_ptr_table = ptr_table_new(); current_thread = S_ithread_get(aTHX); S_ithread_set(aTHX_ thread); @@ -1644,9 +1639,9 @@ ithread_error(...) ptr_table_store(PL_ptr_table, &other_perl->Isv_undef, &PL_sv_undef); ptr_table_store(PL_ptr_table, &other_perl->Isv_no, &PL_sv_no); ptr_table_store(PL_ptr_table, &other_perl->Isv_yes, &PL_sv_yes); - err = sv_dup(thread->err, &clone_params); + err = sv_dup(thread->err, clone_params); S_ithread_set(aTHX_ current_thread); - SvREFCNT_dec(clone_params.stashes); + Perl_clone_params_del(clone_params); SvREFCNT_inc_void(err); /* If error was an object, bless it into the correct class */ if (thread->err_class) { diff --git a/embed.fnc b/embed.fnc index 1f2ecb9..8e463c1 100644 --- a/embed.fnc +++ b/embed.fnc @@ -1421,7 +1421,13 @@ ApR |MAGIC* |mg_dup |NULLOK MAGIC *mg|NN CLONE_PARAMS *const param s |SV ** |sv_dup_inc_multiple|NN SV *const *source|NN SV **dest \ |SSize_t items|NN CLONE_PARAMS *const param #endif +#if defined(PERL_IN_SV_C) || defined(PERL_DECL_PROT) +sR |SV* |sv_dup_common |NN const SV *const sstr \ + |NN CLONE_PARAMS *const param +#endif ApR |SV* |sv_dup |NULLOK const SV *const sstr|NN CLONE_PARAMS *const param +ApR |SV* |sv_dup_inc |NULLOK const SV *const sstr \ + |NN CLONE_PARAMS *const param Ap |void |rvpv_dup |NN SV *const dstr|NN const SV *const sstr|NN CLONE_PARAMS *const param Ap |yy_parser*|parser_dup |NULLOK const yy_parser *const proto|NN CLONE_PARAMS *const param #endif @@ -2073,6 +2079,11 @@ pd |void |pad_fixup_inner_anons|NN PADLIST *padlist|NN CV *old_cv|NN CV *new_cv pdX |void |pad_push |NN PADLIST *padlist|int depth : Only used in PAD_COMPNAME_TYPE() in op.c pR |HV* |pad_compname_type|const PADOFFSET po +: Used in sv.c +#if defined(USE_ITHREADS) +pR |AV* |padlist_dup |NULLOK AV *const srcpad \ + |NN CLONE_PARAMS *const param +#endif #if defined(PERL_IN_PAD_C) || defined(PERL_DECL_PROT) sd |PADOFFSET|pad_findlex |NN const char *name|NN const CV* cv|U32 seq|int warn \ @@ -2353,6 +2364,14 @@ xpoM |struct refcounted_he *|store_cop_label \ xpo |int |keyword_plugin_standard|NN char* keyword_ptr|STRLEN keyword_len|NN OP** op_ptr +#if defined(USE_ITHREADS) +# if defined(PERL_IN_SV_C) || defined(PERL_DECL_PROT) +s |void |unreferenced_to_tmp_stack|NN AV *const unreferenced +# endif +Aanop |CLONE_PARAMS *|clone_params_new|NN PerlInterpreter *const from \ + |NN PerlInterpreter *const to +Anop |void |clone_params_del|NN CLONE_PARAMS *param +#endif END_EXTERN_C /* * ex: set ts=8 sts=4 sw=4 noet: diff --git a/embed.h b/embed.h index 706bfe4..90e8045 100644 --- a/embed.h +++ b/embed.h @@ -1180,7 +1180,13 @@ #define sv_dup_inc_multiple S_sv_dup_inc_multiple #endif #endif +#if defined(PERL_IN_SV_C) || defined(PERL_DECL_PROT) +#ifdef PERL_CORE +#define sv_dup_common S_sv_dup_common +#endif +#endif #define sv_dup Perl_sv_dup +#define sv_dup_inc Perl_sv_dup_inc #define rvpv_dup Perl_rvpv_dup #define parser_dup Perl_parser_dup #endif @@ -1771,6 +1777,11 @@ #define pad_push Perl_pad_push #define pad_compname_type Perl_pad_compname_type #endif +#if defined(USE_ITHREADS) +#ifdef PERL_CORE +#define padlist_dup Perl_padlist_dup +#endif +#endif #if defined(PERL_IN_PAD_C) || defined(PERL_DECL_PROT) #ifdef PERL_CORE #define pad_findlex S_pad_findlex @@ -2042,6 +2053,13 @@ #ifdef PERL_CORE #define boot_core_mro Perl_boot_core_mro #endif +#if defined(USE_ITHREADS) +# if defined(PERL_IN_SV_C) || defined(PERL_DECL_PROT) +#ifdef PERL_CORE +#define unreferenced_to_tmp_stack S_unreferenced_to_tmp_stack +#endif +# endif +#endif #define ck_anoncode Perl_ck_anoncode #define ck_bitop Perl_ck_bitop #define ck_chdir Perl_ck_chdir @@ -3589,7 +3607,13 @@ #define sv_dup_inc_multiple(a,b,c,d) S_sv_dup_inc_multiple(aTHX_ a,b,c,d) #endif #endif +#if defined(PERL_IN_SV_C) || defined(PERL_DECL_PROT) +#ifdef PERL_CORE +#define sv_dup_common(a,b) S_sv_dup_common(aTHX_ a,b) +#endif +#endif #define sv_dup(a,b) Perl_sv_dup(aTHX_ a,b) +#define sv_dup_inc(a,b) Perl_sv_dup_inc(aTHX_ a,b) #define rvpv_dup(a,b,c) Perl_rvpv_dup(aTHX_ a,b,c) #define parser_dup(a,b) Perl_parser_dup(aTHX_ a,b) #endif @@ -4193,6 +4217,11 @@ #define pad_push(a,b) Perl_pad_push(aTHX_ a,b) #define pad_compname_type(a) Perl_pad_compname_type(aTHX_ a) #endif +#if defined(USE_ITHREADS) +#ifdef PERL_CORE +#define padlist_dup(a,b) Perl_padlist_dup(aTHX_ a,b) +#endif +#endif #if defined(PERL_IN_PAD_C) || defined(PERL_DECL_PROT) #ifdef PERL_CORE #define pad_findlex(a,b,c,d,e,f,g) S_pad_findlex(aTHX_ a,b,c,d,e,f,g) @@ -4476,6 +4505,13 @@ #endif #ifdef PERL_CORE #endif +#if defined(USE_ITHREADS) +# if defined(PERL_IN_SV_C) || defined(PERL_DECL_PROT) +#ifdef PERL_CORE +#define unreferenced_to_tmp_stack(a) S_unreferenced_to_tmp_stack(aTHX_ a) +#endif +# endif +#endif #define ck_anoncode(a) Perl_ck_anoncode(aTHX_ a) #define ck_bitop(a) Perl_ck_bitop(aTHX_ a) #define ck_chdir(a) Perl_ck_chdir(aTHX_ a) diff --git a/global.sym b/global.sym index 6cffeb8..8861fca 100644 --- a/global.sym +++ b/global.sym @@ -726,6 +726,7 @@ Perl_dirp_dup Perl_gp_dup Perl_mg_dup Perl_sv_dup +Perl_sv_dup_inc Perl_rvpv_dup Perl_parser_dup Perl_ptr_table_new @@ -822,4 +823,6 @@ Perl_sys_init Perl_sys_init3 Perl_sys_term Perl_fetch_cop_label +Perl_clone_params_new +Perl_clone_params_del # ex: set ro: diff --git a/hv.c b/hv.c index 1ff73b0..6d6edb2 100644 --- a/hv.c +++ b/hv.c @@ -179,7 +179,7 @@ Perl_he_dup(pTHX_ const HE *e, bool shared, CLONE_PARAMS* param) char *k; Newx(k, HEK_BASESIZE + sizeof(const SV *), char); HeKEY_hek(ret) = (HEK*)k; - HeKEY_sv(ret) = SvREFCNT_inc(sv_dup(HeKEY_sv(e), param)); + HeKEY_sv(ret) = sv_dup_inc(HeKEY_sv(e), param); } else if (shared) { /* This is hek_dup inlined, which seems to be important for speed @@ -202,7 +202,7 @@ Perl_he_dup(pTHX_ const HE *e, bool shared, CLONE_PARAMS* param) else HeKEY_hek(ret) = save_hek_flags(HeKEY(e), HeKLEN(e), HeHASH(e), HeKFLAGS(e)); - HeVAL(ret) = SvREFCNT_inc(sv_dup(HeVAL(e), param)); + HeVAL(ret) = sv_dup_inc(HeVAL(e), param); return ret; } #endif /* USE_ITHREADS */ diff --git a/mro.c b/mro.c index 2dca25c..488e564 100644 --- a/mro.c +++ b/mro.c @@ -162,23 +162,22 @@ Perl_mro_meta_dup(pTHX_ struct mro_meta* smeta, CLONE_PARAMS* param) if (newmeta->mro_linear_all) { newmeta->mro_linear_all - = MUTABLE_HV(SvREFCNT_inc(sv_dup((const SV *)newmeta->mro_linear_all, param))); + = MUTABLE_HV(sv_dup_inc((const SV *)newmeta->mro_linear_all, param)); /* This is just acting as a shortcut pointer, and will be automatically updated on the first get. */ newmeta->mro_linear_current = NULL; } else if (newmeta->mro_linear_current) { /* Only the current MRO is stored, so this owns the data. */ newmeta->mro_linear_current - = SvREFCNT_inc(sv_dup((const SV *)newmeta->mro_linear_current, - param)); + = sv_dup_inc((const SV *)newmeta->mro_linear_current, param); } if (newmeta->mro_nextmethod) newmeta->mro_nextmethod - = MUTABLE_HV(SvREFCNT_inc(sv_dup((const SV *)newmeta->mro_nextmethod, param))); + = MUTABLE_HV(sv_dup_inc((const SV *)newmeta->mro_nextmethod, param)); if (newmeta->isa) newmeta->isa - = MUTABLE_HV(SvREFCNT_inc(sv_dup((const SV *)newmeta->isa, param))); + = MUTABLE_HV(sv_dup_inc((const SV *)newmeta->isa, param)); return newmeta; } diff --git a/pad.c b/pad.c index f941252..8015154 100644 --- a/pad.c +++ b/pad.c @@ -1304,22 +1304,33 @@ Perl_pad_tidy(pTHX_ padtidy_type type) AvREIFY_only(av); } - /* XXX DAPM rationalise these two similar branches */ - - if (type == padtidy_SUB) { + if (type == padtidy_SUB || type == padtidy_FORMAT) { + SV * const * const namep = AvARRAY(PL_comppad_name); PADOFFSET ix; for (ix = AvFILLp(PL_comppad); ix > 0; ix--) { if (SvIMMORTAL(PL_curpad[ix]) || IS_PADGV(PL_curpad[ix]) || IS_PADCONST(PL_curpad[ix])) continue; - if (!SvPADMY(PL_curpad[ix])) - SvPADTMP_on(PL_curpad[ix]); - } - } - else if (type == padtidy_FORMAT) { - PADOFFSET ix; - for (ix = AvFILLp(PL_comppad); ix > 0; ix--) { - if (!SvPADMY(PL_curpad[ix]) && !SvIMMORTAL(PL_curpad[ix])) + if (!SvPADMY(PL_curpad[ix])) { SvPADTMP_on(PL_curpad[ix]); + } else if (!SvFAKE(namep[ix])) { + /* This is a work around for how the current implementation of + ?{ } blocks in regexps interacts with lexicals. + + One of our lexicals. + Can't do this on all lexicals, otherwise sub baz() won't + compile in + + my $foo; + + sub bar { ++$foo; } + + sub baz { ++$foo; } + + because completion of compiling &bar calling pad_tidy() + would cause (top level) $foo to be marked as stale, and + "no longer available". */ + SvPADSTALE_on(PL_curpad[ix]); + } } } PL_curpad = AvARRAY(PL_comppad); @@ -1752,6 +1763,129 @@ Perl_pad_compname_type(pTHX_ const PADOFFSET po) return NULL; } +#if defined(USE_ITHREADS) + +# define av_dup_inc(s,t) MUTABLE_AV(sv_dup_inc((const SV *)s,t)) + +AV * +Perl_padlist_dup(pTHX_ AV *const srcpad, CLONE_PARAMS *const param) +{ + AV *dstpad; + PERL_ARGS_ASSERT_PADLIST_DUP; + + if (!srcpad) + return NULL; + + assert(!AvREAL(srcpad)); + + if (param->flags & CLONEf_COPY_STACKS + || SvREFCNT(AvARRAY(srcpad)[1]) > 1) { + /* XXX padlists are real, but pretend to be not */ + AvREAL_on(srcpad); + dstpad = av_dup_inc(srcpad, param); + AvREAL_off(srcpad); + AvREAL_off(dstpad); + assert (SvREFCNT(AvARRAY(srcpad)[1]) == 1); + } else { + /* CvDEPTH() on our subroutine will be set to 0, so there's no need + to build anything other than the first level of pads. */ + + I32 ix = AvFILLp((const AV *)AvARRAY(srcpad)[1]); + AV *pad1; + const I32 names_fill = AvFILLp((const AV *)(AvARRAY(srcpad)[0])); + const AV *const srcpad1 = (const AV *) AvARRAY(srcpad)[1]; + SV **oldpad = AvARRAY(srcpad1); + SV **names; + SV **pad1a; + AV *args; + /* look for it in the table first. + I *think* that it shouldn't be possible to find it there. + Well, except for how Perl_sv_compile_2op() "works" :-( */ + dstpad = (AV*)ptr_table_fetch(PL_ptr_table, srcpad); + + if (dstpad) + return dstpad; + + dstpad = newAV(); + ptr_table_store(PL_ptr_table, srcpad, dstpad); + AvREAL_off(dstpad); + av_extend(dstpad, 1); + AvARRAY(dstpad)[0] = MUTABLE_SV(av_dup_inc(AvARRAY(srcpad)[0], param)); + names = AvARRAY(AvARRAY(dstpad)[0]); + + pad1 = newAV(); + + av_extend(pad1, ix); + AvARRAY(dstpad)[1] = MUTABLE_SV(pad1); + pad1a = AvARRAY(pad1); + AvFILLp(dstpad) = 1; + + if (ix > -1) { + AvFILLp(pad1) = ix; + + for ( ;ix > 0; ix--) { + if (!oldpad[ix]) { + pad1a[ix] = NULL; + } else if (names_fill >= ix && names[ix] != &PL_sv_undef) { + const char sigil = SvPVX_const(names[ix])[0]; + if ((SvFLAGS(names[ix]) & SVf_FAKE) + || (SvFLAGS(names[ix]) & SVpad_STATE) + || sigil == '&') + { + /* outer lexical or anon code */ + pad1a[ix] = sv_dup_inc(oldpad[ix], param); + } + else { /* our own lexical */ + if(SvPADSTALE(oldpad[ix]) && SvREFCNT(oldpad[ix]) > 1) { + /* This is a work around for how the current + implementation of ?{ } blocks in regexps + interacts with lexicals. */ + pad1a[ix] = sv_dup_inc(oldpad[ix], param); + } else { + SV *sv; + + if (sigil == '@') + sv = MUTABLE_SV(newAV()); + else if (sigil == '%') + sv = MUTABLE_SV(newHV()); + else + sv = newSV(0); + pad1a[ix] = sv; + SvPADMY_on(sv); + } + } + } + else if (IS_PADGV(oldpad[ix]) || IS_PADCONST(oldpad[ix])) { + pad1a[ix] = sv_dup_inc(oldpad[ix], param); + } + else { + /* save temporaries on recursion? */ + SV * const sv = newSV(0); + pad1a[ix] = sv; + + /* SvREFCNT(oldpad[ix]) != 1 for some code in threads.xs + FIXTHAT before merging this branch. + (And I know how to) */ + if (SvPADMY(oldpad[ix])) + SvPADMY_on(sv); + else + SvPADTMP_on(sv); + } + } + + if (oldpad[0]) { + args = newAV(); /* Will be @_ */ + AvREIFY_only(args); + pad1a[0] = (SV *)args; + } + } + } + + return dstpad; +} + +#endif + /* * Local variables: * c-indentation-style: bsd diff --git a/proto.h b/proto.h index 8d4f283..6ccf19c 100644 --- a/proto.h +++ b/proto.h @@ -4289,12 +4289,27 @@ STATIC SV ** S_sv_dup_inc_multiple(pTHX_ SV *const *source, SV **dest, SSize_t i assert(source); assert(dest); assert(param) #endif +#if defined(PERL_IN_SV_C) || defined(PERL_DECL_PROT) +STATIC SV* S_sv_dup_common(pTHX_ const SV *const sstr, CLONE_PARAMS *const param) + __attribute__warn_unused_result__ + __attribute__nonnull__(pTHX_1) + __attribute__nonnull__(pTHX_2); +#define PERL_ARGS_ASSERT_SV_DUP_COMMON \ + assert(sstr); assert(param) + +#endif PERL_CALLCONV SV* Perl_sv_dup(pTHX_ const SV *const sstr, CLONE_PARAMS *const param) __attribute__warn_unused_result__ __attribute__nonnull__(pTHX_2); #define PERL_ARGS_ASSERT_SV_DUP \ assert(param) +PERL_CALLCONV SV* Perl_sv_dup_inc(pTHX_ const SV *const sstr, CLONE_PARAMS *const param) + __attribute__warn_unused_result__ + __attribute__nonnull__(pTHX_2); +#define PERL_ARGS_ASSERT_SV_DUP_INC \ + assert(param) + PERL_CALLCONV void Perl_rvpv_dup(pTHX_ SV *const dstr, const SV *const sstr, CLONE_PARAMS *const param) __attribute__nonnull__(pTHX_1) __attribute__nonnull__(pTHX_2) @@ -6302,6 +6317,14 @@ PERL_CALLCONV void Perl_pad_push(pTHX_ PADLIST *padlist, int depth) PERL_CALLCONV HV* Perl_pad_compname_type(pTHX_ const PADOFFSET po) __attribute__warn_unused_result__; +#if defined(USE_ITHREADS) +PERL_CALLCONV AV* Perl_padlist_dup(pTHX_ AV *const srcpad, CLONE_PARAMS *const param) + __attribute__warn_unused_result__ + __attribute__nonnull__(pTHX_2); +#define PERL_ARGS_ASSERT_PADLIST_DUP \ + assert(param) + +#endif #if defined(PERL_IN_PAD_C) || defined(PERL_DECL_PROT) STATIC PADOFFSET S_pad_findlex(pTHX_ const char *name, const CV* cv, U32 seq, int warn, SV** out_capture, SV** out_name_sv, int *out_flags) @@ -6872,6 +6895,28 @@ PERL_CALLCONV int Perl_keyword_plugin_standard(pTHX_ char* keyword_ptr, STRLEN k assert(keyword_ptr); assert(op_ptr) +#if defined(USE_ITHREADS) +# if defined(PERL_IN_SV_C) || defined(PERL_DECL_PROT) +STATIC void S_unreferenced_to_tmp_stack(pTHX_ AV *const unreferenced) + __attribute__nonnull__(pTHX_1); +#define PERL_ARGS_ASSERT_UNREFERENCED_TO_TMP_STACK \ + assert(unreferenced) + +# endif +PERL_CALLCONV CLONE_PARAMS * Perl_clone_params_new(PerlInterpreter *const from, PerlInterpreter *const to) + __attribute__malloc__ + __attribute__warn_unused_result__ + __attribute__nonnull__(1) + __attribute__nonnull__(2); +#define PERL_ARGS_ASSERT_CLONE_PARAMS_NEW \ + assert(from); assert(to) + +PERL_CALLCONV void Perl_clone_params_del(CLONE_PARAMS *param) + __attribute__nonnull__(1); +#define PERL_ARGS_ASSERT_CLONE_PARAMS_DEL \ + assert(param) + +#endif END_EXTERN_C /* * ex: set ts=8 sts=4 sw=4 noet: diff --git a/regcomp.c b/regcomp.c index be5acdb..c8c47e9 100644 --- a/regcomp.c +++ b/regcomp.c @@ -9636,9 +9636,8 @@ Perl_regfree_internal(pTHX_ REGEXP * const rx) Safefree(ri); } -#define sv_dup_inc(s,t) SvREFCNT_inc(sv_dup(s,t)) -#define av_dup_inc(s,t) MUTABLE_AV(SvREFCNT_inc(sv_dup((const SV *)s,t))) -#define hv_dup_inc(s,t) MUTABLE_HV(SvREFCNT_inc(sv_dup((const SV *)s,t))) +#define av_dup_inc(s,t) MUTABLE_AV(sv_dup_inc((const SV *)s,t)) +#define hv_dup_inc(s,t) MUTABLE_HV(sv_dup_inc((const SV *)s,t)) #define SAVEPVN(p,n) ((p) ? savepvn(p,n) : NULL) /* diff --git a/sv.c b/sv.c index 7ec4242..0da4256 100644 --- a/sv.c +++ b/sv.c @@ -10491,18 +10491,17 @@ ptr_table_* functions. that currently av_dup, gv_dup and hv_dup are the same as sv_dup. If this changes, please unmerge ss_dup. Likewise, sv_dup_inc_multiple() relies on this fact. */ -#define sv_dup_inc(s,t) SvREFCNT_inc(sv_dup(s,t)) -#define sv_dup_inc_NN(s,t) SvREFCNT_inc_NN(sv_dup(s,t)) +#define sv_dup_inc_NN(s,t) SvREFCNT_inc_NN(sv_dup_inc(s,t)) #define av_dup(s,t) MUTABLE_AV(sv_dup((const SV *)s,t)) -#define av_dup_inc(s,t) MUTABLE_AV(SvREFCNT_inc(sv_dup((const SV *)s,t))) +#define av_dup_inc(s,t) MUTABLE_AV(sv_dup_inc((const SV *)s,t)) #define hv_dup(s,t) MUTABLE_HV(sv_dup((const SV *)s,t)) -#define hv_dup_inc(s,t) MUTABLE_HV(SvREFCNT_inc(sv_dup((const SV *)s,t))) +#define hv_dup_inc(s,t) MUTABLE_HV(sv_dup_inc((const SV *)s,t)) #define cv_dup(s,t) MUTABLE_CV(sv_dup((const SV *)s,t)) -#define cv_dup_inc(s,t) MUTABLE_CV(SvREFCNT_inc(sv_dup((const SV *)s,t))) +#define cv_dup_inc(s,t) MUTABLE_CV(sv_dup_inc((const SV *)s,t)) #define io_dup(s,t) MUTABLE_IO(sv_dup((const SV *)s,t)) -#define io_dup_inc(s,t) MUTABLE_IO(SvREFCNT_inc(sv_dup((const SV *)s,t))) +#define io_dup_inc(s,t) MUTABLE_IO(sv_dup_inc((const SV *)s,t)) #define gv_dup(s,t) MUTABLE_GV(sv_dup((const SV *)s,t)) -#define gv_dup_inc(s,t) MUTABLE_GV(SvREFCNT_inc(sv_dup((const SV *)s,t))) +#define gv_dup_inc(s,t) MUTABLE_GV(sv_dup_inc((const SV *)s,t)) #define SAVEPV(p) ((p) ? savepv(p) : NULL) #define SAVEPVN(p,n) ((p) ? savepvn(p,n) : NULL) @@ -11006,16 +11005,14 @@ S_sv_dup_inc_multiple(pTHX_ SV *const *source, SV **dest, /* duplicate an SV of any type (including AV, HV etc) */ -SV * -Perl_sv_dup(pTHX_ const SV *const sstr, CLONE_PARAMS *const param) +static SV * +S_sv_dup_common(pTHX_ const SV *const sstr, CLONE_PARAMS *const param) { dVAR; SV *dstr; - PERL_ARGS_ASSERT_SV_DUP; + PERL_ARGS_ASSERT_SV_DUP_COMMON; - if (!sstr) - return NULL; if (SvTYPE(sstr) == SVTYPEMASK) { #ifdef DEBUG_LEAKING_SCALARS_ABORT abort(); @@ -11246,11 +11243,6 @@ Perl_sv_dup(pTHX_ const SV *const sstr, CLONE_PARAMS *const param) else { while (items-- > 0) *dst_ary++ = sv_dup(*src_ary++, param); - if (!(param->flags & CLONEf_COPY_STACKS) - && AvREIFY(sstr)) - { - av_reify(MUTABLE_AV(dstr)); /* #41138 */ - } } items = AvMAX((const AV *)sstr) - AvFILLp((const AV *)sstr); while (items-- > 0) { @@ -11334,7 +11326,7 @@ Perl_sv_dup(pTHX_ const SV *const sstr, CLONE_PARAMS *const param) * duped GV may never be freed. A bit of a hack! DAPM */ CvGV(dstr) = (param->flags & CLONEf_JOIN_IN) ? NULL : gv_dup(CvGV(dstr), param) ; - PAD_DUP(CvPADLIST(dstr), CvPADLIST(sstr), param); + CvPADLIST(dstr) = padlist_dup(CvPADLIST(sstr), param); CvOUTSIDE(dstr) = CvWEAKOUTSIDE(sstr) ? cv_dup( CvOUTSIDE(dstr), param) @@ -11352,6 +11344,28 @@ Perl_sv_dup(pTHX_ const SV *const sstr, CLONE_PARAMS *const param) return dstr; } +SV * +Perl_sv_dup_inc(pTHX_ const SV *const sstr, CLONE_PARAMS *const param) +{ + PERL_ARGS_ASSERT_SV_DUP_INC; + return sstr ? SvREFCNT_inc(sv_dup_common(sstr, param)) : NULL; +} + +SV * +Perl_sv_dup(pTHX_ const SV *const sstr, CLONE_PARAMS *const param) +{ + SV *dstr = sstr ? sv_dup_common(sstr, param) : NULL; + PERL_ARGS_ASSERT_SV_DUP; + + /* Track every SV that (at least initially) had a reference count of 0. */ + if (dstr && !(param->flags & CLONEf_COPY_STACKS) && !SvREFCNT(dstr)) { + assert(param->unreferenced); + av_push(param->unreferenced, dstr); + } + + return dstr; +} + /* duplicate a context */ PERL_CONTEXT * @@ -12006,7 +12020,13 @@ perl_clone_using(PerlInterpreter *proto_perl, UV flags, #endif /* PERL_IMPLICIT_SYS */ param->flags = flags; + /* Nothing in the core code uses this, but we make it available to + extensions (using mg_dup). */ param->proto_perl = proto_perl; + /* Likely nothing will use this, but it is initialised to be consistent + with Perl_clone_params_new(). */ + param->proto_perl = my_perl; + param->unreferenced = NULL; INIT_TRACK_MEMPOOL(my_perl->Imemory_debug_header, my_perl); @@ -12100,6 +12120,18 @@ perl_clone_using(PerlInterpreter *proto_perl, UV flags, PL_origargv = proto_perl->Iorigargv; param->stashes = newAV(); /* Setup array of objects to call clone on */ + /* This makes no difference to the implementation, as it always pushes + and shifts pointers to other SVs without changing their reference + count, with the array becoming empty before it is freed. However, it + makes it conceptually clear what is going on, and will avoid some + work inside av.c, filling slots between AvFILL() and AvMAX() with + &PL_sv_undef, and SvREFCNT_dec()ing those. */ + AvREAL_off(param->stashes); + + if (!(flags & CLONEf_COPY_STACKS)) { + param->unreferenced = newAV(); + AvREAL_off(param->unreferenced); + } /* Set tainting stuff before PerlIO_debug can possibly get called */ PL_tainting = proto_perl->Itainting; @@ -12495,19 +12527,6 @@ perl_clone_using(PerlInterpreter *proto_perl, UV flags, else { init_stacks(); ENTER; /* perl_destruct() wants to LEAVE; */ - - /* although we're not duplicating the tmps stack, we should still - * add entries for any SVs on the tmps stack that got cloned by a - * non-refcount means (eg a temp in @_); otherwise they will be - * orphaned - */ - for (i = 0; i<= proto_perl->Itmps_ix; i++) { - SV * const nsv = MUTABLE_SV(ptr_table_fetch(PL_ptr_table, - proto_perl->Itmps_stack[i])); - if (nsv && !SvREFCNT(nsv)) { - PUSH_EXTEND_MORTAL__SV_C(SvREFCNT_inc_simple(nsv)); - } - } } PL_start_env = proto_perl->Istart_env; /* XXXXXX */ @@ -12614,6 +12633,9 @@ perl_clone_using(PerlInterpreter *proto_perl, UV flags, PL_ptr_table = NULL; } + if (!(flags & CLONEf_COPY_STACKS)) { + unreferenced_to_tmp_stack(param->unreferenced); + } SvREFCNT_dec(param->stashes); @@ -12626,6 +12648,89 @@ perl_clone_using(PerlInterpreter *proto_perl, UV flags, return my_perl; } +static void +S_unreferenced_to_tmp_stack(pTHX_ AV *const unreferenced) +{ + PERL_ARGS_ASSERT_UNREFERENCED_TO_TMP_STACK; + + if (AvFILLp(unreferenced) > -1) { + SV **svp = AvARRAY(unreferenced); + SV **const last = svp + AvFILLp(unreferenced); + SSize_t count = 0; + + do { + if (!SvREFCNT(*svp)) + ++count; + } while (++svp <= last); + + EXTEND_MORTAL(count); + svp = AvARRAY(unreferenced); + + do { + if (!SvREFCNT(*svp)) + PL_tmps_stack[++PL_tmps_ix] = SvREFCNT_inc_simple_NN(*svp); + } while (++svp <= last); + } + SvREFCNT_dec(unreferenced); +} + +void +Perl_clone_params_del(CLONE_PARAMS *param) +{ + PerlInterpreter *const was = PERL_GET_THX; + PerlInterpreter *const to = param->new_perl; + dTHXa(to); + + PERL_ARGS_ASSERT_CLONE_PARAMS_DEL; + + if (was != to) { + PERL_SET_THX(to); + } + + SvREFCNT_dec(param->stashes); + if (param->unreferenced) + unreferenced_to_tmp_stack(param->unreferenced); + + Safefree(param); + + if (was != to) { + PERL_SET_THX(was); + } +} + +CLONE_PARAMS * +Perl_clone_params_new(PerlInterpreter *const from, PerlInterpreter *const to) +{ + /* Need to play this game, as newAV() can call safesysmalloc(), and that + does a dTHX; to get the context from thread local storage. + FIXME - under PERL_CORE Newx(), Safefree() and friends should expand to + a version that passes in my_perl. */ + PerlInterpreter *const was = PERL_GET_THX; + CLONE_PARAMS *param; + + PERL_ARGS_ASSERT_CLONE_PARAMS_NEW; + + if (was != to) { + PERL_SET_THX(to); + } + + /* Given that we've set the context, we can do this unshared. */ + Newx(param, 1, CLONE_PARAMS); + + param->flags = 0; + param->proto_perl = from; + param->new_perl = to; + param->stashes = (AV *)Perl_newSV_type(to, SVt_PVAV); + AvREAL_off(param->stashes); + param->unreferenced = (AV *)Perl_newSV_type(to, SVt_PVAV); + AvREAL_off(param->unreferenced); + + if (was != to) { + PERL_SET_THX(was); + } + return param; +} + #endif /* USE_ITHREADS */ /* diff --git a/sv.h b/sv.h index cc7edb9..3f31920 100644 --- a/sv.h +++ b/sv.h @@ -1939,6 +1939,8 @@ struct clone_params { AV* stashes; UV flags; PerlInterpreter *proto_perl; + PerlInterpreter *new_perl; + AV *unreferenced; }; /* diff --git a/t/op/threads.t b/t/op/threads.t index 956102a..d8bab5b 100644 --- a/t/op/threads.t +++ b/t/op/threads.t @@ -16,7 +16,7 @@ BEGIN { exit 0; } - plan(18); + plan(22); } use strict; @@ -116,10 +116,6 @@ print do 'op/threads_create.pl' || die $@; EOI -TODO: { - no strict 'vars'; # Accessing $TODO from test.pl - local $TODO = 'refcount issues with threads'; - # Scalars leaked: 1 foreach my $BLOCK (qw(CHECK INIT)) { fresh_perl_is(<<EOI, 'ok', { }, "threads in $BLOCK block"); @@ -129,8 +125,6 @@ foreach my $BLOCK (qw(CHECK INIT)) { EOI } -} # TODO - # Scalars leaked: 1 fresh_perl_is(<<'EOI', 'ok', { }, 'Bug #41138'); use threads; @@ -206,8 +200,8 @@ print "ok"; EOI # Another, more reliable test for the same del_backref bug: -fresh_perl_like( - <<' EOJ', qr/ok/, {}, 'No del_backref panic [perl #70748] (2)' +fresh_perl_is( + <<' EOJ', 'ok', {}, 'No del_backref panic [perl #70748] (2)' use threads; push @bar, threads->create(sub{sub{}})->join() for 1...10; print "ok"; @@ -216,10 +210,10 @@ fresh_perl_like( # Simple closure-returning test: At least this case works (though it # leaks), and we don't want to break it. -fresh_perl_like(<<'EOJ', qr/^foo\n/, {}, 'returning a closure'); +fresh_perl_is(<<'EOJ', 'foo', {}, 'returning a closure'); use threads; print create threads sub { - my $x = "foo\n"; + my $x = 'foo'; sub{sub{$x}} }=>->join->()() //"undef" @@ -237,4 +231,65 @@ fresh_perl_is(<<'EOI', 'ok', { }, 'Test for 34394ecd06e704e9'); print 'ok'; EOI +# This will fail in "interesting" ways if stashes in clone_params is not +# initialised correctly. +fresh_perl_like(<<'EOI', qr/\AThread 1 terminated abnormally: Not a CODE reference/, { }, 'RT #73046'); + use strict; + use threads; + + sub foo::bar; + + my %h = (1, *{$::{'foo::'}}{HASH}); + *{$::{'foo::'}} = {}; + + threads->create({}, delete $h{1})->join(); + + print "end"; +EOI + +fresh_perl_is(<<'EOI', 'ok', { }, '0 refcnt neither on tmps stack nor in @_'); + use threads; + my %h = (1, []); + use Scalar::Util 'weaken'; + my $a = $h{1}; + weaken($a); + delete $h{1} && threads->create(sub {}, shift)->join(); + print 'ok'; +EOI + +{ + my $got; + sub stuff { + my $a; + if (@_) { + $a = "Leakage"; + threads->create(\&stuff)->join(); + } else { + is ($a, undef, 'RT #73086 - clone used to clone active pads'); + } + } + + stuff(1); + + curr_test(curr_test() + 1); +} + +{ + my $got; + sub more_stuff { + my $a; + $::b = \$a; + if (@_) { + $a = "More leakage"; + threads->create(\&more_stuff)->join(); + } else { + is ($a, undef, 'Just special casing lexicals in ?{ ... }'); + } + } + + more_stuff(1); + + curr_test(curr_test() + 1); +} + # EOF -- Perl5 Master Repository
