Mailing List Archive

parse_block() and unbalanced scope stack
If parse_block() succeeds, all is fine.

Sometimes it fails though. If the parser finds some errors in a block,
then parse_block() still returns anyway, and the caller is supposed to
just know to check if(PL_parser->error_count > 0). OK, fine...

But if this happens then sometimes the scopestack is unbalanced. I know
because I wrote this:

U32 was_scopestack_ix = PL_scopestack_ix;
OP *block = parse_block(0);
assert(PL_scopestack_ix == was_scopestack_ix);

that assertion fails if a compile error happens.

This stack unbalance seems to cause upsets far away - in particular,
S_aassign_scan() is very fragile to this sort of thing, and will
outright SIGSEGV:

Program received signal SIGSEGV, Segmentation fault.
#0 0x00005555555a1e40 in S_aassign_scan.constprop.0 ()

It turns out I can fix this by the horrible hack of:

/* REALLY??! Do I really have to do this?? */
while(PL_scopestack_ix > was_scopestack_ix)
LEAVE;

But (as the comment says) - really, am I supposed to be doing this? It
feels like parse_block() is being rude in not doing that for me.

Also for that matter - why does parse_block() even bother returning in
failure... Why doesn't it just croak up to the parser?

--
Paul "LeoNerd" Evans

leonerd@leonerd.org.uk | https://metacpan.org/author/PEVANS
http://www.leonerd.org.uk/ | https://www.tindie.com/stores/leonerd/
Re: parse_block() and unbalanced scope stack [ In reply to ]
On Mon, 21 Jun 2021 17:07:46 +0100
"Paul \"LeoNerd\" Evans" <leonerd@leonerd.org.uk> wrote:

> It turns out I can fix this by the horrible hack of:
>
> /* REALLY??! Do I really have to do this?? */
> while(PL_scopestack_ix > was_scopestack_ix)
> LEAVE;
>
> But (as the comment says) - really, am I supposed to be doing this? It
> feels like parse_block() is being rude in not doing that for me.

The source code, for reference:
https://metacpan.org/release/PEVANS/XS-Parse-Sublike-0.12/source/lib/XS/Parse/Sublike.xs#L219-224

--
Paul "LeoNerd" Evans

leonerd@leonerd.org.uk | https://metacpan.org/author/PEVANS
http://www.leonerd.org.uk/ | https://www.tindie.com/stores/leonerd/
Re: parse_block() and unbalanced scope stack [ In reply to ]
??????? Original Message ???????

On Monday, June 21st, 2021 at 2:56 PM, Paul "LeoNerd" Evans <leonerd@leonerd.org.uk> wrote:

> On Mon, 21 Jun 2021 17:07:46 +0100
>
> "Paul \"LeoNerd\" Evans" leonerd@leonerd.org.uk wrote:
>
> > It turns out I can fix this by the horrible hack of:
> >
> > /* REALLY??! Do I really have to do this?? */
> > while(PL_scopestack_ix > was_scopestack_ix)
> > LEAVE;

For my own edification, why is this not an "if" - or is this a busy wait or something? Seems to imply a race condition (and that doesn't make any sense to my current understanding of the sequential nature of the runtime) - but I also admittedly don't know what I am looking at.

Cheers,
Brett

> >
> >
> > But (as the comment says) - really, am I supposed to be doing this? It
> >
> > feels like parse_block() is being rude in not doing that for me.
>
> The source code, for reference:
>
> https://metacpan.org/release/PEVANS/XS-Parse-Sublike-0.12/source/lib/XS/Parse/Sublike.xs#L219-224
>
> -------------------------------------------------------------------------------------------------------------------------------------
>
> Paul "LeoNerd" Evans
>
> leonerd@leonerd.org.uk | https://metacpan.org/author/PEVANS
>
> http://www.leonerd.org.uk/ | https://www.tindie.com/stores/leonerd/
Re: parse_block() and unbalanced scope stack [ In reply to ]
On Mon, 21 Jun 2021 20:07:43 +0000
"mah.kitteh via perl5-porters" <perl5-porters@perl.org> wrote:

> ??????? Original Message ???????
>
> On Monday, June 21st, 2021 at 2:56 PM, Paul "LeoNerd" Evans
> <leonerd@leonerd.org.uk> wrote:
>
> > On Mon, 21 Jun 2021 17:07:46 +0100
> >
> > "Paul \"LeoNerd\" Evans" leonerd@leonerd.org.uk wrote:
> >
> > > It turns out I can fix this by the horrible hack of:
> > >
> > > /* REALLY??! Do I really have to do this?? */
> > > while(PL_scopestack_ix > was_scopestack_ix)
> > > LEAVE;
>
> For my own edification, why is this not an "if" - or is this a busy
> wait or something? Seems to imply a race condition (and that doesn't
> make any sense to my current understanding of the sequential nature
> of the runtime) - but I also admittedly don't know what I am looking
> at.

Each LEAVE call will reduce the scopestack height by 1. So if it's out
of sync by more than a difference of 1, we'll have to call it twice, or
three times, etc until it's level again.

--
Paul "LeoNerd" Evans

leonerd@leonerd.org.uk | https://metacpan.org/author/PEVANS
http://www.leonerd.org.uk/ | https://www.tindie.com/stores/leonerd/
Re: parse_block() and unbalanced scope stack [ In reply to ]
Thank you.
??????? Original Message ???????

On Monday, June 21st, 2021 at 3:30 PM, Paul "LeoNerd" Evans <leonerd@leonerd.org.uk> wrote:

> On Mon, 21 Jun 2021 20:07:43 +0000
>
> "mah.kitteh via perl5-porters" perl5-porters@perl.org wrote:
>
> > ??????? Original Message ???????
> >
> > On Monday, June 21st, 2021 at 2:56 PM, Paul "LeoNerd" Evans
> >
> > leonerd@leonerd.org.uk wrote:
> >
> > > On Mon, 21 Jun 2021 17:07:46 +0100
> > >
> > > "Paul \"LeoNerd\" Evans" leonerd@leonerd.org.uk wrote:
> > >
> > > > It turns out I can fix this by the horrible hack of:
> > > >
> > > > /* REALLY??! Do I really have to do this?? */
> > > > while(PL_scopestack_ix > was_scopestack_ix)
> > > > LEAVE;
> > > >
> >
> > For my own edification, why is this not an "if" - or is this a busy
> >
> > wait or something? Seems to imply a race condition (and that doesn't
> >
> > make any sense to my current understanding of the sequential nature
> >
> > of the runtime) - but I also admittedly don't know what I am looking
> >
> > at.
>
> Each LEAVE call will reduce the scopestack height by 1. So if it's out
>
> of sync by more than a difference of 1, we'll have to call it twice, or
>
> three times, etc until it's level again.
>
> --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
>
> Paul "LeoNerd" Evans
>
> leonerd@leonerd.org.uk | https://metacpan.org/author/PEVANS
>
> http://www.leonerd.org.uk/ | https://www.tindie.com/stores/leonerd/
Re: parse_block() and unbalanced scope stack [ In reply to ]
??, 21 ???. 2021 ?. ? 19:08, Paul "LeoNerd" Evans <leonerd@leonerd.org.uk>:
> Also for that matter - why does parse_block() even bother returning in
> failure... Why doesn't it just croak up to the parser?

IIRC, so that perl could return more than one parse error.

Best regards,
Sergey Aleynikov