Branch: refs/heads/yves/redo_curlyx_curlym
  Home:   https://github.com/Perl/perl5
  Commit: 805273d95f901aa8a85f46fca5a7286218ce53e2
      
https://github.com/Perl/perl5/commit/805273d95f901aa8a85f46fca5a7286218ce53e2
  Author: Yves Orton <[email protected]>
  Date:   2023-03-13 (Mon, 13 Mar 2023)

  Changed paths:
    M regexec.c
    M regexp.h
    M t/re/re_tests

  Log Message:
  -----------
  regexec.c - incredibly inefficient solution to backref problem

Backrefs to unclosed parens inside of a quantified group were not being
properly handled, which revealed we are not unrolling the paren state properly
on failure and backtracking.

Much of the code assumes that when we execute a "conditional" operation (where
more than one thing could match) that we need not concern ourself with the
paren state unless the conditional operation itself represents a paren, and
that generally opcodes only needed to concern themselves with parens to their
right. When you exclude backrefs from the equation this is broadly reasonable
(i think), as on failure we typically dont care about the state of the paren
buffers. They either get reset as we find a new different accepting pathway,
or their state is irrelevant if the overal match is rejected (eg it fails).

However backreferences are different. Consider the following pattern
from the tests

    "xa=xaaa" =~ /^(xa|=?\1a){2}\z/

in the first iteration through this the first branch matches, and in fact
because the \1 is in the second branch it can't match on the first iteration
at all. After this $1 = "xa". We then perform the second iteration. "xa" does
not match "=xaaa" so we fall to the second branch. The '=?' matches, but sets
up a backtracking action to not match if the rest of the pattern does not
match. \1 matches 'xa', and then the 'a' matches, leaving an unmatched 'a' in
the string, we exit the quantifier loop with $1 = "=xaa" and match \z against
the remaining "a" in the pattern, and fail.

Here is where things go wrong in the old code, we unwind to the outer loop,
but we do not unwind the paren state. We then unwind further into the 2nds
iteration of the loop, to the '=?' where we then try to match the tail with
the quantifier matching the empty string. We then match the old $1 (which was
not unwound) as "=xaa", and then the "a" matches, and we are the end of the
string and we have incorrectly accpeted this string as matching the pattern.

What should have happend was when the \1 was resolved the second time it
should have returned the same string as it did when the =? matched '=', which
then would have resulted in the tail matching again, and etc, eventually
unwinding the entire pattern when the second iteration failed entirely.

This patch is very crude. It simply pushes the state of the parens and creates
an unwind point for every case where we do a transition to a B or _next
operation, and we make the corresponding _next_fail do the appropriate
unwinding. The objective was to achieve correctness and then work towards
making it more efficient. We almost certainly overstore items on the stack.

In the next patch we will keep track of the unclosed parens before the
relevant operators and make sure that they are properly pushed and
unwound at the correct times. In other words this is a first step patch
to make sure things are correct, the next patch will change it so we do
it quickly.


  Commit: 9b7643d29d70c8b1bda8bc6f67dfb64461a67ae1
      
https://github.com/Perl/perl5/commit/9b7643d29d70c8b1bda8bc6f67dfb64461a67ae1
  Author: Yves Orton <[email protected]>
  Date:   2023-03-13 (Mon, 13 Mar 2023)

  Changed paths:
    M regcomp.c
    M regcomp.h
    M regcomp.sym
    M regcomp_internal.h
    M regexec.c
    M regexp.h
    M regnodes.h

  Log Message:
  -----------
  regexec.c - make REF into a backtracking state

This way we can do the required paren restoration only when it is in use. When
we match a REF type node which is potentially a reference to an unclosed paren
we push the match context information, currently for "everything", but in a
future patch we can teach it to be more efficient by adding a new parameter to
the REF regop to track which parens it should save.

This converts the backtracking changes from the previous commit, so that it is
run only when specifically enabled via the define RE_PESSIMISTIC_PARENS which
is by default 0. We don't make the new fields in the struct conditional as the
stack frames are large and our changes don't make any real difference and it
keeps things simpler to not have conditional members, especially since some of
the structures have to line up with each other.

If enabling RE_PESSIMISTIC_PARENS fixes a backtracking bug then it means
something is sensitive to us not necessarily restoring the parens properly on
failure. We make some assumptions that the paren state after a failing state
will be corrected by a future successful state, or that the state of the
parens is irrelevant as we will fail anyway. This can be made not true by
EVAL, backrefs, and potentially some other scenarios. Thus I have left this
inefficient logic in place but guarded by the flag.


  Commit: 9939750da0b445ba493a506c923ed1e9af286481
      
https://github.com/Perl/perl5/commit/9939750da0b445ba493a506c923ed1e9af286481
  Author: Yves Orton <[email protected]>
  Date:   2023-03-13 (Mon, 13 Mar 2023)

  Changed paths:
    M embed.fnc
    M embed.h
    M pod/perldebguts.pod
    M proto.h
    M regcomp.c
    M regcomp.h
    M regcomp.sym
    M regcomp_debug.c
    M regcomp_study.c
    M regcomp_trie.c
    M regexec.c
    M reginline.h
    M regnodes.h

  Log Message:
  -----------
  regex engine - simplify regnode structures and make them consistent

This eliminates the regnode_2L data structure, and merges it with the older
regnode_2 data structure. At the same time it makes each "arg" property of the
various regnode types that have one be consistently structured as an anonymous
union like this:

    union {
        U32 arg1u;
        I32 arg2i;
        struct {
            U16 arg1a;
            U16 arg1b;
        };
    };

We then expose four macros for accessing each slot: ARG1u() ARG1i() and
ARG1a() and ARG1b(). Code then explicitly designates which they want. The old
logic used ARG() to access an U32 arg1, and ARG1() to access an I32 arg1,
which was confusing to say the least. The regnode_2L structure had a U32 arg1,
and I32 arg2, and the regnode_2 data strucutre had two I32 args. With the new
set of macros we use the regnode_2 for both, and use the appropriate macros to
show whether we want to signed or unsigned values.

This also renames the regnode_4 to regnode_3. The 3 stands for "three 32-bit
args". However as each slot can also store two U16s, a regnode_3 can hold up
to 6 U16s, or as 3 I32's, or a combination. For instance the CURLY style nodes
use regnode_3 to store 4 values, ARG1i() for min count, ARG2i() for max count
and ARG3a() and ARG3b() for parens before and inside the quantifier.

It also changes the functions reganode() to reg1node() and changes reg2Lanode()
to reg2node(). The 2L thing was just confusing.


  Commit: c41e6a07f6f749cf46cc4b5f1a40eb1ed7dfef5d
      
https://github.com/Perl/perl5/commit/c41e6a07f6f749cf46cc4b5f1a40eb1ed7dfef5d
  Author: Yves Orton <[email protected]>
  Date:   2023-03-13 (Mon, 13 Mar 2023)

  Changed paths:
    M pod/perldebguts.pod
    M regcomp.c
    M regcomp.sym
    M regcomp_debug.c
    M regexec.c
    M regnodes.h
    M t/re/pat_advanced.t

  Log Message:
  -----------
  regcomp.c - extend REF to hold the paren it needs to regcppush

this way we can avoid pushing every buffer, we only need to push
the nestroot of the ref.


  Commit: 4e4700298dff167b7e68b811436679b680d99c99
      
https://github.com/Perl/perl5/commit/4e4700298dff167b7e68b811436679b680d99c99
  Author: Yves Orton <[email protected]>
  Date:   2023-03-13 (Mon, 13 Mar 2023)

  Changed paths:
    M dump.c
    M regcomp.c
    M regexec.c
    M regexp.h

  Log Message:
  -----------
  regcomp.c - Use RXp_OFFSp() to access offset data

This insulates access to the regexp match offset data so we can
fix the define later and move the offset structure into a new struct.

The RXp_OFFSp() was introduced in a recent commit to deliberately
break anything using RXp_OFFS() directly. It is hard to type
deliberately, nothing but the internals should use it. Everything
else should use one of the wrappers around it.


  Commit: b82f4ce36cab0442e0f3ef1f5fc34d7480c7dd42
      
https://github.com/Perl/perl5/commit/b82f4ce36cab0442e0f3ef1f5fc34d7480c7dd42
  Author: Yves Orton <[email protected]>
  Date:   2023-03-13 (Mon, 13 Mar 2023)

  Changed paths:
    M regexp.h

  Log Message:
  -----------
  regexp.h - standardize macros, and parenthesize parameters

Obviously this isn't required as we build fine. But doing this
future proofs us to other changes.


  Commit: 10656703178f6986e57e1bf91eef61b718e12d4f
      
https://github.com/Perl/perl5/commit/10656703178f6986e57e1bf91eef61b718e12d4f
  Author: Yves Orton <[email protected]>
  Date:   2023-03-13 (Mon, 13 Mar 2023)

  Changed paths:
    M dump.c
    M regcomp_debug.c
    M regexec.c

  Log Message:
  -----------
  regexec.c - use RXp_LASTPAREN(rex) to access rex->lastparen

This field will be moving to a new struct. Converting this to a macro
will make that move easier.


  Commit: 7568ad613c3e7a0dee3283d70069e9199c7bad0c
      
https://github.com/Perl/perl5/commit/7568ad613c3e7a0dee3283d70069e9199c7bad0c
  Author: Yves Orton <[email protected]>
  Date:   2023-03-13 (Mon, 13 Mar 2023)

  Changed paths:
    M regexp.h

  Log Message:
  -----------
  regexp.h - add missing defines

We were missing various RXp_XXXX() and RX_XXXX() macros. This adds
them so we can use them in places where we are unreasonable intimate
with the regexp struct internals.


  Commit: 4a948609dce8f481ac3a1017fb6f6bb89893575b
      
https://github.com/Perl/perl5/commit/4a948609dce8f481ac3a1017fb6f6bb89893575b
  Author: Yves Orton <[email protected]>
  Date:   2023-03-13 (Mon, 13 Mar 2023)

  Changed paths:
    M dump.c

  Log Message:
  -----------
  dump.c - use RXp_ macros to access regexp struct members

We will move some of these members out of the regexp structure
into a new sub structucture. This isolates those changes to the
macro definitions


  Commit: e8a55238f0e0812cc709c515309e71803203b68d
      
https://github.com/Perl/perl5/commit/e8a55238f0e0812cc709c515309e71803203b68d
  Author: Yves Orton <[email protected]>
  Date:   2023-03-13 (Mon, 13 Mar 2023)

  Changed paths:
    M regexec.c

  Log Message:
  -----------
  regexec.c - use RXp_LASTCLOSEPAREN(r) to access r->lastcloseparen

We will move this struct member into a new struct in a future patch,
and using the macros means we can reduce the number of places that
needs to be explcitly aware of the new structure.


  Commit: 3697eb9574b1a11b74af72399b84087ecd088aff
      
https://github.com/Perl/perl5/commit/3697eb9574b1a11b74af72399b84087ecd088aff
  Author: Yves Orton <[email protected]>
  Date:   2023-03-13 (Mon, 13 Mar 2023)

  Changed paths:
    M regexec.c

  Log Message:
  -----------
  regexec.c - use macro to access rex->subbeg

We will move this member to a new struct in the near future,
converting all uses to a macro isolates that change.


  Commit: 0bce65ad0f488b3103d4d93fc9a5e75dfe3023a0
      
https://github.com/Perl/perl5/commit/0bce65ad0f488b3103d4d93fc9a5e75dfe3023a0
  Author: Yves Orton <[email protected]>
  Date:   2023-03-13 (Mon, 13 Mar 2023)

  Changed paths:
    M regexec.c

  Log Message:
  -----------
  regexec.c - use RXp_SUBLEN(ret) for ret->sublen

This member of the regexp structure will be moved to a new
structure in the near future. Converting to use the macro
will make this change easier to manage.


  Commit: 3a05036043880f2ac7dbb1da0e1983708b32b85f
      
https://github.com/Perl/perl5/commit/3a05036043880f2ac7dbb1da0e1983708b32b85f
  Author: Yves Orton <[email protected]>
  Date:   2023-03-13 (Mon, 13 Mar 2023)

  Changed paths:
    M regexec.c

  Log Message:
  -----------
  regexec.c - use RXp_SUBOFFSET(rx) instead of rx->suboffset

We will migrate this struct member to a new struct in the near future
this change will make that patch more minimal and hide the gory details.


  Commit: eb0573857506ddae338d8d6530c17781a584b5fc
      
https://github.com/Perl/perl5/commit/eb0573857506ddae338d8d6530c17781a584b5fc
  Author: Yves Orton <[email protected]>
  Date:   2023-03-13 (Mon, 13 Mar 2023)

  Changed paths:
    M regexec.c

  Log Message:
  -----------
  regexec.c - use RXp_SUBCOFFSET instead of rx->subcoffset

This member of the regexp struct will soon be migrated to a new
independent structure. This change ensure that when we do the migration
the changes are restricted to the least code possible.


  Commit: 04d422e35882096214bd8a3cc60c6e648a083171
      
https://github.com/Perl/perl5/commit/04d422e35882096214bd8a3cc60c6e648a083171
  Author: Yves Orton <[email protected]>
  Date:   2023-03-13 (Mon, 13 Mar 2023)

  Changed paths:
    M regexec.c

  Log Message:
  -----------
  regexec.c - use RXp_SAVED_COPY(rex) instead of rex->saved_copy

We will migrate this member to a new structure in the near future,
wrapping with a macro makes that migration simpler and less invasive.


  Commit: e9a6ab15f011929bcf61636908d50dbe46c065bc
      
https://github.com/Perl/perl5/commit/e9a6ab15f011929bcf61636908d50dbe46c065bc
  Author: Yves Orton <[email protected]>
  Date:   2023-03-13 (Mon, 13 Mar 2023)

  Changed paths:
    M regcomp.c

  Log Message:
  -----------
  regcomp.c - use macro wrappers to minimize impact of struct split

We will move various members of the regexp structure to a new
structure which just contains information about the match. Wrapping
the members in the standard macros means that change can be made
less invasive. We already did all of this in regexec.c


Compare: https://github.com/Perl/perl5/compare/98a9b349f010...e9a6ab15f011

Reply via email to