Skip to content

Commit

Permalink
fix range op under aborted constant folding
Browse files Browse the repository at this point in the history
When constant-folding a range/flipflop construct, the op_next threading
of peephole optimisation caused multiple ops in the construct to have
a null op_next, because the final (and top-level) op in the construct
is a null op.  This meant that simple restoration of the top-level
op's op_next after execution wouldn't get it back into a fit state
to be composed with other ops.  In the event that the range construct
couldn't be constant-folded this made it compile to a broken optree.
If it couldn't be constant-folded but could actually be executed, for
example because it generated a warning, this meant the brokenness would
be encountered at runtime.  Execution would stop after the range op,
because of the null op_next.

To avoid this, temporarily mark the null op as a custom op during the
peephole optimisation that supports the execution for constant-folding.
This prevents it being op_next-threaded out, so simple op_next restoring
then works.  If the constant-folding is aborted, it compiles to an
operational optree.  However, the suppression of duplicate peephole
optimisation means that the null op is never ultimately threaded out
as it should be.  For the time being, this stands as a cost of failed
constant-folding of range constructs.

Fixes [perl #130639].
  • Loading branch information
Zefram committed Jan 27, 2017
1 parent 475b224 commit b369834
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 1 deletion.
6 changes: 6 additions & 0 deletions op.c
Original file line number Diff line number Diff line change
Expand Up @@ -4632,6 +4632,7 @@ S_gen_constant_list(pTHX_ OP *o)
COP not_compiling;
int ret = 0;
dJMPENV;
bool op_was_null;

list(o);
if (PL_parser && PL_parser->error_count)
Expand All @@ -4640,7 +4641,12 @@ S_gen_constant_list(pTHX_ OP *o)
curop = LINKLIST(o);
old_next = o->op_next;
o->op_next = 0;
op_was_null = o->op_type == OP_NULL;
if (op_was_null)
o->op_type = OP_CUSTOM;
CALL_PEEP(curop);
if (op_was_null)
o->op_type = OP_NULL;
S_prune_chain_head(&curop);
PL_op = curop;

Expand Down
5 changes: 4 additions & 1 deletion t/comp/fold.t
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
# we've not yet verified that use works.
# use strict;

print "1..34\n";
print "1..35\n";
my $test = 0;

# Historically constant folding was performed by evaluating the ops, and if
Expand Down Expand Up @@ -189,3 +189,6 @@ $b = 0;
$a = eval 'my @z; @z = 0..~0 if $b; 3';
is ($a, 3, "list constant folding doesn't signal compile-time error");
is ($@, '', 'no error');

$a = eval 'local $SIG{__WARN__} = sub {}; join("", ":".."~", "z")';
is ($a, ":z", "aborted list constant folding still executable");

0 comments on commit b369834

Please sign in to comment.