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
Perl lexer (???) has a deep recursion bug in Perl_scalarvoid when given a very large Perl subroutine to run. Perl_scalarvoid calls itself over and over, for what I blindly guess is every opcode in the sub. If there are too many opcodes, C stack overflows.
I could not replicate this with any stock ActivePerl (AP 5.10.0, AP 5.12.3). While the test script is running, ActivePerls peak at 8 MB on the C stack. I checked with VMMAP. If I lower the stack reserve from 16 MB to 256KB in the PE header on perl.exe from ActivePerls, I will get the same exact Perl_scalarvoid stack overflow. There is no reason for the interpreter to peak at many MBs of C stack, which is irreversibly expanded (there are OS specific ways to shrink C stacks.... I know it can be done with VirtualFree and VirtualAlloc on Windows, but that is the wrong way to fix C stack recursion) and wasted, never to be used again after BEGIN blocks run. I have not tested doing the "require" at runtime. On other perl scripts and apps I run, all the interpreter (APs and my compiled Perl) peak at 20-50KB of C stack which is fine. I assume that the reason that my self-compiled Perl will stack overflow at 16MB and ActivePerls do not is because of high C stack overhead of each recursive Perl_scalarvoid when Perl is compiled with -Od (no registers) meanwhile ActivePerls are compiled with -O1 (small code).
I wrote this test script to test how efficient Perl is at returning memory to the OS after a Perl Module is "use"d and if, and how well, memory behind the BEGIN blocks (SVs, CVs, Opcodes, pads, raw malloced blocks) is returned to the OS. If I lower the 100000 to 50000 in the test script, the script will run without a stack overflow. On sucessful runs, malloced memory went down from ~60MB inside the module BEGIN block (at "print "after branches\n";") to ~3MB at runtime ("print "in main\n";" ) according to VMMAP (private/yellow memory). C Stack remained at many MBs (7 to 8MB at 50000 in test script).
Perl_scalarvoid, whatever it does to opcodes, should run in a loop down the optree, not recursively on every opcode and thus eating away the C stack. I request that the above-reported bug be fixed.
included pics of VS debugger and opcode structs, the pp_cond_expr and
pp_null only 2 things inthe recursive pattern, also added full C stack
trace, and
added the test Perl script that generates these stack overflows
Perl lexer (???) has a deep recursion bug in Perl_scalarvoid when
given a very large Perl subroutine to run.� Perl_scalarvoid calls
itself over and over, for what I blindly guess is every opcode in
the sub.� If there are too many opcodes, C stack overflows.
I could not replicate this with any stock ActivePerl (AP 5.10.0, AP
5.12.3).� While the test script is running, ActivePerls peak at 8
MB on the C stack.� I checked with VMMAP.� If I lower the stack
reserve from 16 MB to 256KB in the PE header on perl.exe from
ActivePerls, I will get the same exact Perl_scalarvoid stack
overflow.� There is no reason for the interpreter to peak at many
MBs of C stack, which is irreversibly expanded (there are OS
specific ways to shrink C stacks.... I know it can be done with
VirtualFree and VirtualAlloc on Windows, but that is the wrong way
to fix C stack recursion) and wasted, never to be used again after
BEGIN blocks run.� I have not tested doing the "require" at
runtime.� On other perl scripts and apps I run, all the interpreter
(APs and my compiled Perl) peak at 20-50KB of C stack which is
fine.� I assume that the reason that my self-compiled Perl will
stack overflow at 16MB and ActivePerls do not is because of high C
stack overhead of each recursive Perl_scalarvoid when Perl is
compiled with -Od (no registers) meanwhile ActivePerls are compiled
with -O1 (small code).
I wrote this test script to test how efficient Perl is at returning
memory to the OS after a Perl Module is "use"d and if, and how
well, memory behind the BEGIN blocks (SVs, CVs, Opcodes, pads, raw
malloced blocks) is returned to the OS.� If I lower the 100000 to
50000 in the test script, the script will run without a stack
overflow.� On sucessful runs, malloced memory went down from ~60MB
inside the module BEGIN block (at "print "after branches\n";") to
~3MB at runtime ("print "in main\n";" ) according to VMMAP
(private/yellow memory).� C Stack remained at many MBs (7 to 8MB at
50000 in test script).
Perl_scalarvoid, whatever it does to opcodes, should run in a loop
down the optree, not recursively on every opcode and thus eating
away the C stack.� I request that the above-reported bug be fixed.
1. I modified the test program you wrote to take the number of
iterations from the command-line rather than having it hard-coded. I
then ran the program on the two OSes to which I have access, Darwin and
Linux. In both cases, the program failed.
$ perl 108276_stk.pl 10
number: 10
before require
Can't exec "pause": No such file or directory at 108276_stk.pl line 23.
10after branches
Can't exec "pause": No such file or directory at testmod1326659374.pm
line 14.
in main
Can't exec "pause": No such file or directory at 108276_stk.pl line 27.
'pause' is not an installed executable on those platforms (though it has
a man page as a C library function).
Could you modify your program to avoid the use of 'pause'? That would
enable more people to run it.
2. While the screenshots you posted via the RT GUI are displaying, I
suspect they won't show up on the perl5-porters mailing list or newsgroup.
Perl lexer (???) has a deep recursion bug in Perl_scalarvoid when
given a very large Perl subroutine to run.� Perl_scalarvoid calls
itself over and over, for what I blindly guess is every opcode in
the sub.� If there are too many opcodes, C stack overflows.
I could not replicate this with any stock ActivePerl (AP 5.10.0, AP
5.12.3).� While the test script is running, ActivePerls peak at 8
MB on the C stack.� I checked with VMMAP.� If I lower the stack
reserve from 16 MB to 256KB in the PE header on perl.exe from
ActivePerls, I will get the same exact Perl_scalarvoid stack
overflow.� There is no reason for the interpreter to peak at many
MBs of C stack, which is irreversibly expanded (there are OS
specific ways to shrink C stacks.... I know it can be done with
VirtualFree and VirtualAlloc on Windows, but that is the wrong way
to fix C stack recursion) and wasted, never to be used again after
BEGIN blocks run.� I have not tested doing the "require" at
runtime.� On other perl scripts and apps I run, all the interpreter
(APs and my compiled Perl) peak at 20-50KB of C stack which is
fine.� I assume that the reason that my self-compiled Perl will
stack overflow at 16MB and ActivePerls do not is because of high C
stack overhead of each recursive Perl_scalarvoid when Perl is
compiled with -Od (no registers) meanwhile ActivePerls are compiled
with -O1 (small code).
I wrote this test script to test how efficient Perl is at returning
memory to the OS after a Perl Module is "use"d and if, and how
well, memory behind the BEGIN blocks (SVs, CVs, Opcodes, pads, raw
malloced blocks) is returned to the OS.� If I lower the 100000 to
50000 in the test script, the script will run without a stack
overflow.� On sucessful runs, malloced memory went down from ~60MB
inside the module BEGIN block (at "print "after branches\n";") to
~3MB at runtime ("print "in main\n";" ) according to VMMAP
(private/yellow memory).� C Stack remained at many MBs (7 to 8MB at
50000 in test script).
Perl_scalarvoid, whatever it does to opcodes, should run in a loop
down the optree, not recursively on every opcode and thus eating
away the C stack.� I request that the above-reported bug be fixed.
1. I modified the test program you wrote to take the number of
iterations from the command-line rather than having it hard-coded. I
then ran the program on the two OSes to which I have access, Darwin and
Linux. In both cases, the program failed.
$ perl 108276_stk.pl 10
number: 10
before require
Can't exec "pause": No such file or directory at 108276_stk.pl line 23.
10after branches
Can't exec "pause": No such file or directory at testmod1326659374.pm
line 14.
in main
Can't exec "pause": No such file or directory at 108276_stk.pl line 27.
'pause' is not an installed executable on those platforms (though it has
a man page as a C library function).
Could you modify your program to avoid the use of 'pause'? That would
enable more people to run it.
I haven’t used Windows for a long, long time (except IE for web
testing), but if I remember correctly the pause command prints ‘Press
the "any" key to continue...’ and waits for a keystroke before exiting.
Changing system("pause") to:
system("perl -e '$|=1; print q|Press return to continue... |; <>'")
I haven’t used Windows for a long, long time (except IE for web
testing), but if I remember correctly the pause command prints ‘Press
the "any" key to continue...’ and waits for a keystroke before exiting.
Changing system("pause") to:
system\("perl \-e '$|=1; print q|Press return to continue\.\.\. |; \<>'"\)
should work.
Google says "pause" equivalent on POSIX is "echo -n "Press any key to
continue . . ." && read -n 1". The system("pause") lines are the points
I suggest to look at memory usage of the Perl process. Not sure if there
are any tools that can show reserved size of stack vs allocated amount
of stack in virtual memory system on Linux/Unix. I used VMMAP for
Windows to get the memory counts.
I haven’t used Windows for a long, long time (except IE for web
testing), but if I remember correctly the pause command prints ‘Press
the "any" key to continue...’ and waits for a keystroke before exiting.
Changing system("pause") to:
system\("perl \-e '$|=1; print q|Press return to continue\.\.\. |; \<>'"\)
should work.
Google says "pause" equivalent on POSIX is "echo -n "Press any key to
continue . . ." && read -n 1". The system("pause") lines are the points
I suggest to look at memory usage of the Perl process. Not sure if there
are any tools that can show reserved size of stack vs allocated amount
of stack in virtual memory system on Linux/Unix. I used VMMAP for
Windows to get the memory counts.
On Sun, Jan 15, 2012 at 10:35:14AM -0800, bulk 88 wrote:
The code can be simplified to the following:
my $n = $ARGV[0];
die unless $n =~ /^\d+$/;
my $code = 'my $i = 0; if ($i) { print }' . "\n";
$code .= 'elsif ($i) { print }' . "\n" for 1..$n;
eval $code;
segfaults for me on blead with n >= about 5000.
In blead, it's recursing in S_finalize_op rather than scalarvoid; not sure
whether this is instead of or in addition to a scalarvoid problem.
--
"But Sidley Park is already a picture, and a most amiable picture too.
The slopes are green and gentle. The trees are companionably grouped at
intervals that show them to advantage. The rill is a serpentine ribbon
unwound from the lake peaceably contained by meadows on which the right
amount of sheep are tastefully arranged." -- Lady Croom, "Arcadia"
So it looks as though at least S_finalize_op needs to be made iterative,
and probably scalarvoid, too.
And this results in a crash in op_free:
perl -e'eval "sub{".q"$a+"x shift . "}"' 500000
What else needs to be made iterative?
I decided to run Perl's test suite on my c stack reduced perl. First one
found is on parset.t, there are many more on my list to analyze, I see
one in comp/proto.t that I am working on getting a stack trace of right now.
Perl_peep, when running
cop_line 111 unsigned long
+ cop_file 0x018332bc "C:\perl-5.12.2\t\comp\parser.t"
From f003f14c35078fdf95a51ec8b7bd2d85fc92ded0 Mon Sep 17 00:00:00 2001
From: Tony Cook <tony@develop-help.com>
Date: Tue, 29 Jan 2019 13:57:51 +1100
Subject: (perl #108276) eliminate recursion from finalize_op()
whitespace in next commit
---
embed.fnc | 1 +
embed.h | 1 +
op.c | 48 +++++++++++++++++++++++++++++++++++++++++++-----
proto.h | 3 +++
4 files changed, 48 insertions(+), 5 deletions(-)
diff --git a/embed.fnc b/embed.fnc
index bdb29f7216..d311ca7f51 100644
--- a/embed.fnc+++ b/embed.fnc@@ -563,6 +563,7 @@ i |OP* |newMETHOP_internal |I32 type|I32 flags|NULLOK OP* dynamic_meth \
|NULLOK SV* const_meth
: FIXME
s |OP* |fold_constants |NN OP * const o
+s |OP* |traverse_op_tree|NN OP* top|NN OP* o
#endif
Afpd |char* |form |NN const char* pat|...
Ap |char* |vform |NN const char* pat|NULLOK va_list* args
diff --git a/embed.h b/embed.h
index a94583870a..f3b95eadbd 100644
--- a/embed.h+++ b/embed.h@@ -1886,6 +1886,7 @@
#define simplify_sort(a) S_simplify_sort(aTHX_ a)
#define too_few_arguments_pv(a,b,c) S_too_few_arguments_pv(aTHX_ a,b,c)
#define too_many_arguments_pv(a,b,c) S_too_many_arguments_pv(aTHX_ a,b,c)
+#define traverse_op_tree(a,b) S_traverse_op_tree(aTHX_ a,b)
# if defined(USE_ITHREADS)
#define op_relocate_sv(a,b) S_op_relocate_sv(aTHX_ a,b)
# endif
diff --git a/op.c b/op.c
index d966848055..41067e7ff4 100644
--- a/op.c+++ b/op.c@@ -3537,12 +3537,52 @@ S_op_relocate_sv(pTHX_ SV** svp, PADOFFSET* targp)
}
#endif
+/*+=for apidoc s|OP*|traverse_op_tree|OP* top|OP* o++Return the next op in a depth-first traversal of the op tree,+returning NULL when the traversal is complete.++The initial call must supply the root of the tree as both top and o.++For now it's static, but it may be exposed to the API in the future.++=cut+*/++STATIC OP*+S_traverse_op_tree(OP *top, OP *o) {+ OP *sib;++ PERL_ARGS_ASSERT_TRAVERSE_OP_TREE;++ if ((o->op_flags & OPf_KIDS) && cUNOPo->op_first) {+ return cUNOPo->op_first;+ }+ else if ((sib = OpSIBLING(o))) {+ return sib;+ }+ else {+ OP *parent = o->op_sibparent;+ assert(!(o->op_moresib));+ while (parent && parent != top) {+ OP *sib = OpSIBLING(parent);+ if (sib)+ return sib;+ parent = parent->op_sibparent;+ }++ return NULL;+ }+}
STATIC void
S_finalize_op(pTHX_ OP* o)
{
+ OP * const top = o;
PERL_ARGS_ASSERT_FINALIZE_OP;
+ do {
assert(o->op_type != OP_FREED);
switch (o->op_type) {
@@ -3659,10 +3699,10 @@ S_finalize_op(pTHX_ OP* o)
break;
}
+#ifdef DEBUGGING
if (o->op_flags & OPf_KIDS) {
OP *kid;
-#ifdef DEBUGGING
/* check that op_last points to the last sibling, and that
* the last op_sibling/op_sibparent field points back to the
* parent, and that the only ops with KIDS are those which are
@@ -3705,11 +3745,9 @@ S_finalize_op(pTHX_ OP* o)
assert(kid->op_sibparent == o);
}
}
-#endif-- for (kid = cUNOPo->op_first; kid; kid = OpSIBLING(kid))- finalize_op(kid);
}
+#endif+ } while (( o = traverse_op_tree(top, o)) != NULL);
}
/*
diff --git a/proto.h b/proto.h
index 36a61db05d..daf338707b 100644
--- a/proto.h+++ b/proto.h@@ -5115,6 +5115,9 @@ STATIC OP* S_too_few_arguments_pv(pTHX_ OP *o, const char* name, U32 flags)
STATIC OP* S_too_many_arguments_pv(pTHX_ OP *o, const char* name, U32 flags);
#define PERL_ARGS_ASSERT_TOO_MANY_ARGUMENTS_PV \
assert(o); assert(name)
+STATIC OP* S_traverse_op_tree(pTHX_ OP* top, OP* o);+#define PERL_ARGS_ASSERT_TRAVERSE_OP_TREE \+ assert(top); assert(o)
# if defined(USE_ITHREADS)
#ifndef PERL_NO_INLINE_FUNCTIONS
PERL_STATIC_INLINE void S_op_relocate_sv(pTHX_ SV** svp, PADOFFSET* targp);
--
2.11.0
From 6bf66a2cd50fc888294f11b2c4c9a69dc8113c31 Mon Sep 17 00:00:00 2001
From: Tony Cook <tony@develop-help.com>
Date: Tue, 29 Jan 2019 14:22:02 +1100
Subject: (perl #108276) indent body of new finalize_op() loop
---
op.c | 260 +++++++++++++++++++++++++++++++++----------------------------------
1 file changed, 130 insertions(+), 130 deletions(-)
diff --git a/op.c b/op.c
index 41067e7ff4..befedb7c3c 100644
--- a/op.c+++ b/op.c@@ -3583,20 +3583,20 @@ S_finalize_op(pTHX_ OP* o)
PERL_ARGS_ASSERT_FINALIZE_OP;
do {
- assert(o->op_type != OP_FREED);+ assert(o->op_type != OP_FREED);- switch (o->op_type) {- case OP_NEXTSTATE:- case OP_DBSTATE:- PL_curcop = ((COP*)o); /* for warnings */- break;- case OP_EXEC:- if (OpHAS_SIBLING(o)) {- OP *sib = OpSIBLING(o);- if (( sib->op_type == OP_NEXTSTATE || sib->op_type == OP_DBSTATE)- && ckWARN(WARN_EXEC)- && OpHAS_SIBLING(sib))- {+ switch (o->op_type) {+ case OP_NEXTSTATE:+ case OP_DBSTATE:+ PL_curcop = ((COP*)o); /* for warnings */+ break;+ case OP_EXEC:+ if (OpHAS_SIBLING(o)) {+ OP *sib = OpSIBLING(o);+ if (( sib->op_type == OP_NEXTSTATE || sib->op_type == OP_DBSTATE)+ && ckWARN(WARN_EXEC)+ && OpHAS_SIBLING(sib))+ {
const OPCODE type = OpSIBLING(sib)->op_type;
if (type != OP_EXIT && type != OP_WARN && type != OP_DIE) {
const line_t oldline = CopLINE(PL_curcop);
@@ -3607,145 +3607,145 @@ S_finalize_op(pTHX_ OP* o)
"\t(Maybe you meant system() when you said exec()?)\n");
CopLINE_set(PL_curcop, oldline);
}
- }- }- break;+ }+ }+ break;- case OP_GV:- if ((o->op_private & OPpEARLY_CV) && ckWARN(WARN_PROTOTYPE)) {- GV * const gv = cGVOPo_gv;- if (SvTYPE(gv) == SVt_PVGV && GvCV(gv) && SvPVX_const(GvCV(gv))) {- /* XXX could check prototype here instead of just carping */- SV * const sv = sv_newmortal();- gv_efullname3(sv, gv, NULL);- Perl_warner(aTHX_ packWARN(WARN_PROTOTYPE),- "%" SVf "() called too early to check prototype",- SVfARG(sv));- }- }- break;+ case OP_GV:+ if ((o->op_private & OPpEARLY_CV) && ckWARN(WARN_PROTOTYPE)) {+ GV * const gv = cGVOPo_gv;+ if (SvTYPE(gv) == SVt_PVGV && GvCV(gv) && SvPVX_const(GvCV(gv))) {+ /* XXX could check prototype here instead of just carping */+ SV * const sv = sv_newmortal();+ gv_efullname3(sv, gv, NULL);+ Perl_warner(aTHX_ packWARN(WARN_PROTOTYPE),+ "%" SVf "() called too early to check prototype",+ SVfARG(sv));+ }+ }+ break;- case OP_CONST:- if (cSVOPo->op_private & OPpCONST_STRICT)- no_bareword_allowed(o);+ case OP_CONST:+ if (cSVOPo->op_private & OPpCONST_STRICT)+ no_bareword_allowed(o);
#ifdef USE_ITHREADS
- /* FALLTHROUGH */- case OP_HINTSEVAL:- op_relocate_sv(&cSVOPo->op_sv, &o->op_targ);+ /* FALLTHROUGH */+ case OP_HINTSEVAL:+ op_relocate_sv(&cSVOPo->op_sv, &o->op_targ);
#endif
- break;+ break;
#ifdef USE_ITHREADS
- /* Relocate all the METHOP's SVs to the pad for thread safety. */- case OP_METHOD_NAMED:- case OP_METHOD_SUPER:- case OP_METHOD_REDIR:- case OP_METHOD_REDIR_SUPER:- op_relocate_sv(&cMETHOPx(o)->op_u.op_meth_sv, &o->op_targ);- break;+ /* Relocate all the METHOP's SVs to the pad for thread safety. */+ case OP_METHOD_NAMED:+ case OP_METHOD_SUPER:+ case OP_METHOD_REDIR:+ case OP_METHOD_REDIR_SUPER:+ op_relocate_sv(&cMETHOPx(o)->op_u.op_meth_sv, &o->op_targ);+ break;
#endif
- case OP_HELEM: {- UNOP *rop;- SVOP *key_op;- OP *kid;+ case OP_HELEM: {+ UNOP *rop;+ SVOP *key_op;+ OP *kid;- if ((key_op = cSVOPx(((BINOP*)o)->op_last))->op_type != OP_CONST)- break;+ if ((key_op = cSVOPx(((BINOP*)o)->op_last))->op_type != OP_CONST)+ break;- rop = (UNOP*)((BINOP*)o)->op_first;+ rop = (UNOP*)((BINOP*)o)->op_first;- goto check_keys;+ goto check_keys;- case OP_HSLICE:- S_scalar_slice_warning(aTHX_ o);- /* FALLTHROUGH */+ case OP_HSLICE:+ S_scalar_slice_warning(aTHX_ o);+ /* FALLTHROUGH */- case OP_KVHSLICE:- kid = OpSIBLING(cLISTOPo->op_first);- if (/* I bet there's always a pushmark... */- OP_TYPE_ISNT_AND_WASNT_NN(kid, OP_LIST)- && OP_TYPE_ISNT_NN(kid, OP_CONST))- {- break;- }+ case OP_KVHSLICE:+ kid = OpSIBLING(cLISTOPo->op_first);+ if (/* I bet there's always a pushmark... */+ OP_TYPE_ISNT_AND_WASNT_NN(kid, OP_LIST)+ && OP_TYPE_ISNT_NN(kid, OP_CONST))+ {+ break;+ }- key_op = (SVOP*)(kid->op_type == OP_CONST- ? kid- : OpSIBLING(kLISTOP->op_first));+ key_op = (SVOP*)(kid->op_type == OP_CONST+ ? kid+ : OpSIBLING(kLISTOP->op_first));- rop = (UNOP*)((LISTOP*)o)->op_last;+ rop = (UNOP*)((LISTOP*)o)->op_last;- check_keys: - if (o->op_private & OPpLVAL_INTRO || rop->op_type != OP_RV2HV)- rop = NULL;- S_check_hash_fields_and_hekify(aTHX_ rop, key_op);- break;- }- case OP_NULL:- if (o->op_targ != OP_HSLICE && o->op_targ != OP_ASLICE)- break;- /* FALLTHROUGH */- case OP_ASLICE:- S_scalar_slice_warning(aTHX_ o);- break;+ check_keys:+ if (o->op_private & OPpLVAL_INTRO || rop->op_type != OP_RV2HV)+ rop = NULL;+ S_check_hash_fields_and_hekify(aTHX_ rop, key_op);+ break;+ }+ case OP_NULL:+ if (o->op_targ != OP_HSLICE && o->op_targ != OP_ASLICE)+ break;+ /* FALLTHROUGH */+ case OP_ASLICE:+ S_scalar_slice_warning(aTHX_ o);+ break;- case OP_SUBST: {- if (cPMOPo->op_pmreplrootu.op_pmreplroot)- finalize_op(cPMOPo->op_pmreplrootu.op_pmreplroot);- break;- }- default:- break;- }+ case OP_SUBST: {+ if (cPMOPo->op_pmreplrootu.op_pmreplroot)+ finalize_op(cPMOPo->op_pmreplrootu.op_pmreplroot);+ break;+ }+ default:+ break;+ }
#ifdef DEBUGGING
- if (o->op_flags & OPf_KIDS) {- OP *kid;+ if (o->op_flags & OPf_KIDS) {+ OP *kid;- /* check that op_last points to the last sibling, and that- * the last op_sibling/op_sibparent field points back to the- * parent, and that the only ops with KIDS are those which are- * entitled to them */- U32 type = o->op_type;- U32 family;- bool has_last;-- if (type == OP_NULL) {- type = o->op_targ;- /* ck_glob creates a null UNOP with ex-type GLOB- * (which is a list op. So pretend it wasn't a listop */- if (type == OP_GLOB)- type = OP_NULL;- }- family = PL_opargs[type] & OA_CLASS_MASK;-- has_last = ( family == OA_BINOP- || family == OA_LISTOP- || family == OA_PMOP- || family == OA_LOOP- );- assert( has_last /* has op_first and op_last, or ...- ... has (or may have) op_first: */- || family == OA_UNOP- || family == OA_UNOP_AUX- || family == OA_LOGOP- || family == OA_BASEOP_OR_UNOP- || family == OA_FILESTATOP- || family == OA_LOOPEXOP- || family == OA_METHOP- || type == OP_CUSTOM- || type == OP_NULL /* new_logop does this */- );-- for (kid = cUNOPo->op_first; kid; kid = OpSIBLING(kid)) {- if (!OpHAS_SIBLING(kid)) {- if (has_last)- assert(kid == cLISTOPo->op_last);- assert(kid->op_sibparent == o);+ /* check that op_last points to the last sibling, and that+ * the last op_sibling/op_sibparent field points back to the+ * parent, and that the only ops with KIDS are those which are+ * entitled to them */+ U32 type = o->op_type;+ U32 family;+ bool has_last;++ if (type == OP_NULL) {+ type = o->op_targ;+ /* ck_glob creates a null UNOP with ex-type GLOB+ * (which is a list op. So pretend it wasn't a listop */+ if (type == OP_GLOB)+ type = OP_NULL;+ }+ family = PL_opargs[type] & OA_CLASS_MASK;++ has_last = ( family == OA_BINOP+ || family == OA_LISTOP+ || family == OA_PMOP+ || family == OA_LOOP+ );+ assert( has_last /* has op_first and op_last, or ...+ ... has (or may have) op_first: */+ || family == OA_UNOP+ || family == OA_UNOP_AUX+ || family == OA_LOGOP+ || family == OA_BASEOP_OR_UNOP+ || family == OA_FILESTATOP+ || family == OA_LOOPEXOP+ || family == OA_METHOP+ || type == OP_CUSTOM+ || type == OP_NULL /* new_logop does this */+ );++ for (kid = cUNOPo->op_first; kid; kid = OpSIBLING(kid)) {+ if (!OpHAS_SIBLING(kid)) {+ if (has_last)+ assert(kid == cLISTOPo->op_last);+ assert(kid->op_sibparent == o);+ }
}
}
- }
#endif
} while (( o = traverse_op_tree(top, o)) != NULL);
}
--
2.11.0
From c13919ff3cfb99b5660bd0e20021c92cffb594cd Mon Sep 17 00:00:00 2001
From: Tony Cook <tony@develop-help.com>
Date: Tue, 29 Jan 2019 15:03:43 +1100
Subject: (perl #108276) add wrappers for deferred op processing
To avoid duplication of the declarations.
---
op.c | 17 +++++++++--------
1 file changed, 9 insertions(+), 8 deletions(-)
diff --git a/op.c b/op.c
index befedb7c3c..8ed48b293b 100644
--- a/op.c+++ b/op.c@@ -175,6 +175,11 @@ static const char array_passed_to_stat[] = "Array passed to stat will be coerced
op_free()
*/
+#define dDEFER_OP \+ SSize_t defer_stack_alloc = 0; \+ SSize_t defer_ix = -1; \+ OP **defer_stack = NULL;+#define DEFER_OP_CLEANUP Safefree(defer_stack)
#define DEFERRED_OP_STEP 100
#define DEFER_OP(o) \
STMT_START { \
@@ -770,9 +775,7 @@ Perl_op_free(pTHX_ OP *o)
{
dVAR;
OPCODE type;
- SSize_t defer_ix = -1;- SSize_t defer_stack_alloc = 0;- OP **defer_stack = NULL;+ dDEFER_OP;
do {
@@ -870,7 +873,7 @@ Perl_op_free(pTHX_ OP *o)
PL_op = NULL;
} while ( (o = POP_DEFERRED_OP()) );
- Safefree(defer_stack);+ DEFER_OP_CLEANUP;
}
/* S_op_clear_gv(): free a GV attached to an OP */
@@ -1892,10 +1895,8 @@ Perl_scalarvoid(pTHX_ OP *arg)
dVAR;
OP *kid;
SV* sv;
- SSize_t defer_stack_alloc = 0;- SSize_t defer_ix = -1;- OP **defer_stack = NULL;
OP *o = arg;
+ dDEFER_OP;
PERL_ARGS_ASSERT_SCALARVOID;
@@ -2256,7 +2257,7 @@ Perl_scalarvoid(pTHX_ OP *arg)
}
} while ( (o = POP_DEFERRED_OP()) );
- Safefree(defer_stack);+ DEFER_OP_CLEANUP;
return arg;
}
--
2.11.0
From 1a0de2e84c170d316c95b4323525eac85bef5802 Mon Sep 17 00:00:00 2001
From: Tony Cook <tony@develop-help.com>
Date: Tue, 29 Jan 2019 16:29:04 +1100
Subject: (perl #108276) remove recursion from optimize_op()
The prevented code like:
./miniperl -e 'my $line = "\$cond ? \$a : \n"; my $code = ($line x 100000) . "\$b;\n"; eval $code;'
from crashing due to stack overflow.
It does however take a long time to compile.
Because it doesn't strictly recurse through the op tree (due
to OP_SUBST), I couldn't use traverse_op_tree().
I considered wrapping a traverse_op_tree() loop inside a defer op
loop, so OP_SUBST would defer its op, but processing order is
somewhat important from setting PL_curcop.
This also processes the child ops in reverse order, I'm not sure if
that's a real problem (no tests failed), but the next commit fixes
that order.
---
op.c | 16 ++++++++++------
1 file changed, 10 insertions(+), 6 deletions(-)
diff --git a/op.c b/op.c
index 8ed48b293b..0db0128500 100644
--- a/op.c+++ b/op.c@@ -3459,9 +3459,10 @@ Perl_optimize_optree(pTHX_ OP* o)
STATIC void
S_optimize_op(pTHX_ OP* o)
{
- OP *kid;+ dDEFER_OP;
PERL_ARGS_ASSERT_OPTIMIZE_OP;
+ do {
assert(o->op_type != OP_FREED);
switch (o->op_type) {
@@ -3480,18 +3481,21 @@ S_optimize_op(pTHX_ OP* o)
case OP_SUBST:
if (cPMOPo->op_pmreplrootu.op_pmreplroot)
- optimize_op(cPMOPo->op_pmreplrootu.op_pmreplroot);+ DEFER_OP(cPMOPo->op_pmreplrootu.op_pmreplroot);
break;
default:
break;
}
- if (!(o->op_flags & OPf_KIDS))- return;+ if (o->op_flags & OPf_KIDS) {+ OP *kid;+ for (kid = cUNOPo->op_first; kid; kid = OpSIBLING(kid))+ DEFER_OP(kid);+ }+ } while ( ( o = POP_DEFERRED_OP() ) );- for (kid = cUNOPo->op_first; kid; kid = OpSIBLING(kid))- optimize_op(kid);+ DEFER_OP_CLEANUP;
}
--
2.11.0
From e95b861e282cb218336589bb74790651183e1d88 Mon Sep 17 00:00:00 2001
From: Tony Cook <tony@develop-help.com>
Date: Wed, 30 Jan 2019 11:06:52 +1100
Subject: (perl #108276) indent optimize_op() loop body
---
op.c | 48 ++++++++++++++++++++++++------------------------
1 file changed, 24 insertions(+), 24 deletions(-)
diff --git a/op.c b/op.c
index 0db0128500..35414bef16 100644
--- a/op.c+++ b/op.c@@ -3463,36 +3463,36 @@ S_optimize_op(pTHX_ OP* o)
PERL_ARGS_ASSERT_OPTIMIZE_OP;
do {
- assert(o->op_type != OP_FREED);+ assert(o->op_type != OP_FREED);- switch (o->op_type) {- case OP_NEXTSTATE:- case OP_DBSTATE:- PL_curcop = ((COP*)o); /* for warnings */- break;+ switch (o->op_type) {+ case OP_NEXTSTATE:+ case OP_DBSTATE:+ PL_curcop = ((COP*)o); /* for warnings */+ break;- case OP_CONCAT:- case OP_SASSIGN:- case OP_STRINGIFY:- case OP_SPRINTF:- S_maybe_multiconcat(aTHX_ o);- break;+ case OP_CONCAT:+ case OP_SASSIGN:+ case OP_STRINGIFY:+ case OP_SPRINTF:+ S_maybe_multiconcat(aTHX_ o);+ break;- case OP_SUBST:- if (cPMOPo->op_pmreplrootu.op_pmreplroot)- DEFER_OP(cPMOPo->op_pmreplrootu.op_pmreplroot);- break;+ case OP_SUBST:+ if (cPMOPo->op_pmreplrootu.op_pmreplroot)+ DEFER_OP(cPMOPo->op_pmreplrootu.op_pmreplroot);+ break;- default:- break;- }+ default:+ break;+ }- if (o->op_flags & OPf_KIDS) {- OP *kid;- for (kid = cUNOPo->op_first; kid; kid = OpSIBLING(kid))- DEFER_OP(kid);- }+ if (o->op_flags & OPf_KIDS) {+ OP *kid;+ for (kid = cUNOPo->op_first; kid; kid = OpSIBLING(kid))+ DEFER_OP(kid);+ }
} while ( ( o = POP_DEFERRED_OP() ) );
DEFER_OP_CLEANUP;
--
2.11.0
From 4a1bb46bb07cba6823f9a55c5cb343f26b7d55f2 Mon Sep 17 00:00:00 2001
From: Tony Cook <tony@develop-help.com>
Date: Tue, 29 Jan 2019 16:29:38 +1100
Subject: (perl #108276) optimize child ops in sibling order
---
op.c | 22 +++++++++++++++++++++-
1 file changed, 21 insertions(+), 1 deletion(-)
diff --git a/op.c b/op.c
index 35414bef16..8a61b8b616 100644
--- a/op.c+++ b/op.c@@ -190,6 +190,22 @@ static const char array_passed_to_stat[] = "Array passed to stat will be coerced
} \
defer_stack[++defer_ix] = o; \
} STMT_END
+#define DEFER_REVERSE(count) \+ STMT_START { \+ UV cnt = (count); \+ if (cnt > 1) { \+ OP **top = defer_stack + defer_ix; \+ /* top - (cnt) + 1 isn't safe here */ \+ OP **bottom = top - (cnt - 1); \+ OP *tmp; \+ assert(bottom >= defer_stack); \+ while (top > bottom) { \+ tmp = *top; \+ *top-- = *bottom; \+ *bottom++ = tmp; \+ } \+ } \+ } STMT_END;
#define POP_DEFERRED_OP() (defer_ix >= 0 ? defer_stack[defer_ix--] : (OP *)NULL)
@@ -3490,8 +3506,12 @@ S_optimize_op(pTHX_ OP* o)
if (o->op_flags & OPf_KIDS) {
OP *kid;
- for (kid = cUNOPo->op_first; kid; kid = OpSIBLING(kid))+ IV child_count = 0;+ for (kid = cUNOPo->op_first; kid; kid = OpSIBLING(kid)) {
DEFER_OP(kid);
+ ++child_count;+ }+ DEFER_REVERSE(child_count);
}
} while ( ( o = POP_DEFERRED_OP() ) );
--
2.11.0
Activity
p5pRT commentedon Jan 15, 2012
From @bulk88
Created by @bulk88
Perl lexer (???) has a deep recursion bug in Perl_scalarvoid when given a very large Perl subroutine to run. Perl_scalarvoid calls itself over and over, for what I blindly guess is every opcode in the sub. If there are too many opcodes, C stack overflows.
I could not replicate this with any stock ActivePerl (AP 5.10.0, AP 5.12.3). While the test script is running, ActivePerls peak at 8 MB on the C stack. I checked with VMMAP. If I lower the stack reserve from 16 MB to 256KB in the PE header on perl.exe from ActivePerls, I will get the same exact Perl_scalarvoid stack overflow. There is no reason for the interpreter to peak at many MBs of C stack, which is irreversibly expanded (there are OS specific ways to shrink C stacks.... I know it can be done with VirtualFree and VirtualAlloc on Windows, but that is the wrong way to fix C stack recursion) and wasted, never to be used again after BEGIN blocks run. I have not tested doing the "require" at runtime. On other perl scripts and apps I run, all the interpreter (APs and my compiled Perl) peak at 20-50KB of C stack which is fine. I assume that the reason that my self-compiled Perl will stack overflow at 16MB and ActivePerls do not is because of high C stack overhead of each recursive Perl_scalarvoid when Perl is compiled with -Od (no registers) meanwhile ActivePerls are compiled with -O1 (small code).
I wrote this test script to test how efficient Perl is at returning memory to the OS after a Perl Module is "use"d and if, and how well, memory behind the BEGIN blocks (SVs, CVs, Opcodes, pads, raw malloced blocks) is returned to the OS. If I lower the 100000 to 50000 in the test script, the script will run without a stack overflow. On sucessful runs, malloced memory went down from ~60MB inside the module BEGIN block (at "print "after branches\n";") to ~3MB at runtime ("print "in main\n";" ) according to VMMAP (private/yellow memory). C Stack remained at many MBs (7 to 8MB at 50000 in test script).
Perl_scalarvoid, whatever it does to opcodes, should run in a loop down the optree, not recursively on every opcode and thus eating away the C stack. I request that the above-reported bug be fixed.
Perl Info
p5pRT commentedon Jan 15, 2012
From @bulk88
included pics of VS debugger and opcode structs, the pp_cond_expr and
pp_null only 2 things inthe recursive pattern, also added full C stack
trace, and
added the test Perl script that generates these stack overflows
p5pRT commentedon Jan 15, 2012
From @bulk88
stk.pl
p5pRT commentedon Jan 15, 2012
From @bulk88
perlstk1.PNG
p5pRT commentedon Jan 15, 2012
From @bulk88
perlstk2.png
p5pRT commentedon Jan 15, 2012
From @bulk88
perlstackcrash.txt
p5pRT commentedon Jan 15, 2012
@bulk88 - Status changed from 'new' to 'open'
p5pRT commentedon Jan 15, 2012
From @jkeenan
On Sun Jan 15 10:35:13 2012, bulk88 wrote:
1. I modified the test program you wrote to take the number of
iterations from the command-line rather than having it hard-coded. I
then ran the program on the two OSes to which I have access, Darwin and
Linux. In both cases, the program failed.
$ perl 108276_stk.pl 10
number: 10
before require
Can't exec "pause": No such file or directory at 108276_stk.pl line 23.
10after branches
Can't exec "pause": No such file or directory at testmod1326659374.pm
line 14.
in main
Can't exec "pause": No such file or directory at 108276_stk.pl line 27.
'pause' is not an installed executable on those platforms (though it has
a man page as a C library function).
Could you modify your program to avoid the use of 'pause'? That would
enable more people to run it.
2. While the screenshots you posted via the RT GUI are displaying, I
suspect they won't show up on the perl5-porters mailing list or newsgroup.
Thank you for your report.
Jim Keenan
p5pRT commentedon Jan 15, 2012
From @jkeenan
108276_stk.pl
p5pRT commentedon Jan 15, 2012
From @cpansprout
On Sun Jan 15 12:58:36 2012, jkeenan wrote:
I haven’t used Windows for a long, long time (except IE for web
testing), but if I remember correctly the pause command prints ‘Press
the "any" key to continue...’ and waits for a keystroke before exiting.
Changing system("pause") to:
system("perl -e '$|=1; print q|Press return to continue... |; <>'")
should work.
--
Father Chrysostomos
p5pRT commentedon Jan 15, 2012
From @bulk88
On Sun Jan 15 13:16:04 2012, sprout wrote:
Google says "pause" equivalent on POSIX is "echo -n "Press any key to
continue . . ." && read -n 1". The system("pause") lines are the points
I suggest to look at memory usage of the Perl process. Not sure if there
are any tools that can show reserved size of stack vs allocated amount
of stack in virtual memory system on Linux/Unix. I used VMMAP for
Windows to get the memory counts.
p5pRT commentedon Jan 15, 2012
From [Unknown Contact. See original ticket]
On Sun Jan 15 13:16:04 2012, sprout wrote:
Google says "pause" equivalent on POSIX is "echo -n "Press any key to
continue . . ." && read -n 1". The system("pause") lines are the points
I suggest to look at memory usage of the Perl process. Not sure if there
are any tools that can show reserved size of stack vs allocated amount
of stack in virtual memory system on Linux/Unix. I used VMMAP for
Windows to get the memory counts.
p5pRT commentedon Jan 16, 2012
From @iabyn
On Sun, Jan 15, 2012 at 10:35:14AM -0800, bulk 88 wrote:
The code can be simplified to the following:
my $n = $ARGV[0];$n =~ /^\d+$ /;
die unless
my $code = 'my $i = 0; if ($i) { print }' . "\n";
$code .= 'elsif ($i) { print }' . "\n" for 1..$n;
eval $code;
segfaults for me on blead with n >= about 5000.
In blead, it's recursing in S_finalize_op rather than scalarvoid; not sure
whether this is instead of or in addition to a scalarvoid problem.
--
"But Sidley Park is already a picture, and a most amiable picture too.
The slopes are green and gentle. The trees are companionably grouped at
intervals that show them to advantage. The rill is a serpentine ribbon
unwound from the lake peaceably contained by meadows on which the right
amount of sheep are tastefully arranged." -- Lady Croom, "Arcadia"
p5pRT commentedon Jan 20, 2012
From @cpansprout
On Mon Jan 16 03:51:55 2012, davem wrote:
Without actually looking, I suspect it is in addition.
As I’m sure you already know, each elsif is nested inside the previous
one in the op tree.
So it looks as though at least S_finalize_op needs to be made iterative,
and probably scalarvoid, too.
And this results in a crash in op_free:
perl -e'eval "sub{".q"$a+"x shift . "}"' 500000
What else needs to be made iterative?
--
Father Chrysostomos
p5pRT commentedon Jan 21, 2012
From @bulk88
On Fri Jan 20 09:38:48 2012, sprout wrote:
I decided to run Perl's test suite on my c stack reduced perl. First one
found is on parset.t, there are many more on my list to analyze, I see
one in comp/proto.t that I am working on getting a stack trace of right now.
Perl_peep, when running
cop_line 111 unsigned long
+ cop_file 0x018332bc "C:\perl-5.12.2\t\comp\parser.t"
55 remaining items
p5pRT commentedon Jan 30, 2019
From @tonycoz
On Tue, 18 Nov 2014 19:19:28 -0800, tonyc wrote:
A new set of patches:
- standalone finalize_op() fix, which requires PERL_OP_PARENT
- DEFER_OP() fix for optimize_op()
The final commit might not be needed.
Tony
p5pRT commentedon Jan 30, 2019
From @tonycoz
op-recursion.patch
p5pRT commentedon Jan 30, 2019
From @iabyn
On Tue, Jan 29, 2019 at 04:47:14PM -0800, Tony Cook via RT wrote:
They look plausible
Dunno.
--
Dave's first rule of Opera:
If something needs saying, say it: don't warble it.
p5pRT commentedon Feb 4, 2019
From @tonycoz
On Tue, 29 Jan 2019 16:47:14 -0800, tonyc wrote:
Applied as 7f8280c through ee367d4.
Tony
hvds commentedon Jan 23, 2020
@tonycoz is there anything left to do for this ticket?
jkeenan commentedon Jan 28, 2020
@bulk88, we believe this (8-year-old !) ticket is closable ... and will close it in 7 days unless we hear from you otherwise. Thanks.
bulk88 commentedon Feb 2, 2020
Its closeable.
jkeenan commentedon Feb 2, 2020
Thanks!