Skip to content

Smoke me/davem/rc stack1 #20825

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 75 commits into from
Closed

Smoke me/davem/rc stack1 #20825

wants to merge 75 commits into from

Conversation

iabyn
Copy link
Contributor

@iabyn iabyn commented Feb 19, 2023

First cut of the first half of making the argument stack ref counted.
Functionally correct, all tests pass, Moose installs ok, but everything is very slow due to most ops still being wrapped.
The second half, to come later, will be to unwrap all the small ops (like pp_const()) and add other optimisations to make it run at blead speed.

By default the RC stack isn't enabled, and this branch works almost identically to blead. Build it with -Accflags=-DPERL_RC_STACK to get the RC stack. Then run something like perl -DstR -e'$x=1' to see the ref counting in action.

I intend for general discussions around this (such as when to merge into blead) to take place on p5p rather than here. I'll be starting a thread at some point soon.

iabyn added 30 commits February 19, 2023 12:38
The R modifier to the stack debugging switch -Ds already
indicates if an SV's refcount is > 1 or SvTEMP is set (T), or the
SV is on the temps stack without SvTEMP set (t), e.g.:

    =>  IV(101), <2>IV(102), <T>IV(103) <3t>IV(104)

With this commit, it displays SVs with the SvPADTMP flag set as <P>,
e.g.

    =>  <P>IV(101), <2P>IV(102), <PT>IV(103)
The tryAMAGICftest_MG() macro was doing two checks:

1) seeing whether the filetest operator's arg (*PL_stack_sp) looks
   to have magic or be a reference,
2) and if the op has children (which will have pushed an arg, unlike
   (-X _),
If both are true, then do full-on magic and/or overload processing.

The problem with this is that it checks the arg *before* checking whether
there's even an arg. Thus in the case of (-X _), it is actually examining
a random SV on the stack (or in the case of nothing on the stack,
examining the PL_sv_undef pointer always stored at PL_stack_base[0] as a
guard.)

It turns out this was harmless - the test for (1) will examine a random
(but real) SV and get garbage results, but then the 2nd test will fail
anyway, so overloading won't be called.

So the fix is to swap the (1) and (2) test order.

In addition, I changed the 'has an argument' test from OPf_KIDS to
!OPf_REF. These should be mutually exclusive, but the OPf_REF flag
formally indicates (-X _), i.e. that no arg has been pushed on the
stack. Whether the op has children or not could potentially change in
the future, independent of whether it's the (-X _) form.

So overall this commit makes no visible functional difference, but may
make the code more robust against future changes.
This field is used on DEBUG_LEAKING_SCALARS builds to track the file
where the SV was allocated. When freeing the SV, the string was
freed but the pointer was left pointing at the freed string.

So NULL it out.
A few places were calling pp_pushmark(), when they should have been just
directly doing a PUSHMARK()
Perl_leave_adjust_stacks(), which is called by all the scope-exiting ops
(pp_leave, pp_leavesub etc), handles scalar context by updating the list
of SVs on the stack to be returned to be just the one-item list at the
top of the stack (all other items on the stack being discarded).

For the special case of scalar context and no items on the return list,
it instead puts &PL_sv_undef at the lowest point on the stack and skips
most of the rest of the function. The rest of the function includes
things like shuffling down any args to be returned, which obliterates
any other stuff on that stack that needs discarding. For example in

    for (qw(a b c)) { ....; return qw(x y z); ... }

the stack contains a b c x y z; the a,b,c need discarding and the x,y,z
shifting down.

This commit removes the 'skip rest' special behaviour, and makes scalar
return of an empty list behave the same as a scalar return of a
non-empty list. So it pushes &PL_sv_undef at the top of the stack,
then goes through the normal "shift and copy the top arg down the stack"
code path.

This is slightly less efficient, but this is relatively rare condition,
and will make converting Perl_leave_adjust_stacks() to handle a
reference-counted stack easier.
In something like

    for $package_var (....) { ... }

or more experimentally,

    for \$lvref (....) { ... }

when entering the loop in pp_enteriter, perl would pop the GV/LVREF off
the stack, but didn't bump its refcount. Thus it was possible (if
unlikely) that it could be freed during the loop. In particular this
crashed:

    $f = "foo";
    for ${*$f} (1,2) {
        delete $main::{$f}; # delete the glob *foo
        ...;
    }

This will become more serious when the stack becomes refcounted, as
popping something off the stack will trigger a refcount decrement on it
and thus a possible immediate free of the GV.

This commit future-proofs for loops against this by ensuring that
the refcount of the SV referred to by cx->blk_loop.itervar_u.svp is
appropriately bumped up / down on entering / exiting the loop.
This function is a bit of mess.

It gets the label string either from the op, or for OPf_STACKED, from
the SV at the top of the stack. This commit reduces the amount of
OPf_STACKED tests and repeated stack accesses for the label SV (even
after it's notionally been popped off the stack!)

By simplifying the code, it will also make it easier to make the changes
necessary for a reference-counted stack.
'av' is in scope both for the whole function, and for a small block
within that function. Rename the inner variable to av0 to avoid
confusion.
This test looks for a 'Use of freed value in iteration' warning, which
will soon disappear when this branch makes the stack reference counted.
Make the test more modifiable so that it can be made conditional on
build options.
These two functions do a slightly odd thing (which has been present
since 5.000) when calling out to a CV: they half fake up an OP_ENTERSUB,
then call pp_entersub() directly, and only then if it returns a non-null
PL_op value, execute the rest of the ops of the sub within a
CALLRUNOPS() loop.

I can't find any particular reason for this. I guess it might make
calling XS subs infinitesimally faster by not have to invoke the runops
loop when only a single op is executed (the entersub), but hardly seems
worth the effort.

Conversely, eliminating this special-case makes the code cleaner, and
it will allow the workarounds planned to be added shortly (to the runops
loops for reference-counted stacks) to work uniformly. Without this
commit, pp_entersub() would get called before runops() has had a chance
to fix up the stack if necessary.

So this commit *fully* populates the fake OP_ENTERSUB (including type
and pp address) then causes pp_entersub to be invoked implicitly from the
runops loop rather than explicitly.
Like the previous commit which did it for amagic_call() and call_sv(),
this commit makes executing the faked-up OP_ENTEREVAL be executed as
part of the runops loop rather than as a separate call. This is to allow
shortly fixing up for a reference-counted stack. (CALLRUNOPS() will
reify the stack if necessary, while the raw call to pp_entereval() won't
fix up the stack unless its part of the runops loop too.)

However, this is a bit more complex than call_sv() etc in that there is
a good reason for calling pp_entereval() separately. The faked up
OP_ENTEREVAL has its op_next set to NULL - this is the op which would
normally be returned on failure of the eval compilation.  By seeing
whether the retuned value from pp_entereval() is NULL or not, eval_sv()
can tell whether compilation failed.

On the other hand, if pp_entereval() was made to be called as part of
the runops loop, then the runops loop *always* finishes with PL_op set
to NULL. So we can no lo longer distinguish between compile-failed and
compile-succeeded-and-eval-ran-to-completion.

This commit moves the entereval into the runops loop, but restores the
ability to distinguish in a slightly hacky way. It adds a new private
flag for OP_ENTEREVAL - OPpEVAL_EVALSV - which indicates to
pp_entereval() that it was called from eval_sv(). And of course
eval_sv() sets this flag on the OPpEVAL_EVALSV op it fakes up.  If
pp_entereval() fails to compile, then if that flag is set, it pushes a
null pointer onto the argument stack before returning.

Thus by checking whether *PL_stack_sp is NULL or not on return from
CALLRUNOPS(), eval_sv() regains the ability to distinguish the two
cases.
This XS function is for testing the stack-extending EXTEND() macro.

This commit fixes two issues.

1) it uses a nested 'sp' variable declaration in addition to the one
declared implicitly, which is confusing. Use a separate variable called
new_sp instead. This changes the logic slightly, since the EXTEND()
macro messes implicitly with sp, including updating it after a realloc.
We have to do that manually now with new_sp.

2) The test function NULLs a couple of items near the top of the
(potentially just extended) stack. Where the extend size is zero, it
could be NULLing out one or two of the passed arguments on the stack.
At the moment these values aren't used any more and are discarded on
return; but it will get messy once the stack becomes reference-counted,
so only NULL addresses if they're above PL_stack_sp.
A test recently added to check reference count of a stored AV element
had to account for extra reference counts from the temporary refs
generated by

    if (\$a[0] == \$j) { ... }

Instead, calculate this boolean value in a separate statement so the ref
counts are easier to understand.
This function handles perl -Dsv, producing output like

STACK 0: MAIN
  CX 0: BLOCK  =>
  CX 1: SUB    => UNDEF  PV("main"\0)
  retop=leave
STACK 1: MAGIC
  CX 0: SUB    => IV(1)

When a CX stack had zero contexts pushed (like can sometimes happen
when something has just done a PUSHSTACKi() and no op has pushed a BLOCK
or SUB or whatever yet), then the code for determining where the next
markstack pointer is (by peeking ahead into the first CX of the next SI)
was accessing random garbage at cx[0]. This commit fixes that.
This macro turns on stack reference counting.
Add the macro, but commented out for now.

In addition, temporarily define and enable PERL_XXX_TMP_NORC.

This is a temporary transition measure: even in the presence of
PERL_RC_STACK, do not actually modify reference counts yet. So
PERL_RC_STACK will still define all the new static functions and code
behaviour, except for the bits that actually modify reference counts in
ways that assume the stack is reference counted. It allows this branch
to have many commits which incrementally deal with adding support for
PERL_RC_STACK, with each individual commit still working while half of
the core is still not yet "ref count aware".
This indicates whether perl has been built with PERL_RC_STACK.
For now this should only be used by core; It's not yet decided whether
CPAN modules have a use case too.
There are a few 'expected' Dump() output patterns which expect the
refcount of the top-most SV in the tree to be fixed number like

    REFCNT = 1

Change all those occurrences to

    REFCNT = \d+

or similar, since a reference-counted stack will cause higher values
On PERL_RC_STACK builds, skip the call to SAVESTACK_POS().
I suspect that saving the stack position is no longer
required. It was added in 5.001 by:

    NETaa13155: &DB::DB left trash on the stack.
    From: Thomas Koenig
    Files patched: lib/perl5db.pl pp_ctl.c
     The call by pp_dbstate() to &DB::DB left trash on the
     stack.  It now calls DB in list context, and DB returns
     ().

but the details of what bug it fixed are long lost to history.
SAVESTACK_POS() doesn't work well with stacks which may be split into
partly reference-counted and partly not halves, so skip it and hope it
doesn't cause any problems.

Also, assert that SAVEt_STACK_POS isn't used anywhere any more.
From time to time, pp_mapwhile() shifts up any remaining args on the
stack, in order to leave enough space to add the current results to any
accumulated results. This leaves a hole in the stack, which might not be
fully filled until the last iteration. As well as making the output of
perl -Ds confusing, any garbage left in the hole will double-count
towards the reference count of each SV once the stack becomes
reference-counted.

So fill the newly-created hole in the stack with NULL pointers.
This function abstracts away removing any AvARRAY(av) - AvALLOC(av)
offset from an array (previously added by av_shift()).

Also use Zero() rather than a loop to NULL out any elements in the
offset area.
Rather than always using PL_stack_sp to pass to cx_pushblock(),
allow the caller of create_eval_scope() to specify what stack position
is to be saved.

This will be used shortly to fix a bug.
call_sv(cv, G_EVAL) pushes a CXt_EVAL context, fakes up an entersub op,
then runs it, letting entersub consume the arguments pushed onto the current
stack frame by call_sv()'s caller.

So the C-level

    PUSHMARK():
    PUSHs(arg1);
    ...
    PUSHs(argN);
    call_sv(cv, G_EVAL);

is about equivalent to the perl-level:

    eval { cv($arg1,..$argN) }

Except that in the latter, when the CXt_EVAL is pushed, it is done
*before* any of the arguments have been pushed, so the eval context's
blk_oldsp points to just before the arguments. Conversely in the case of
call_sv(), blk_oldsp gets set to just *after* the arguments.

During an exception, the context stack gets unwound until the CXt_EVAL
is reached, then PL_stack_sp is restored to the blk_oldsp value.

The difference currently makes no real functional difference, as
call_sv() resets PL_stack_sp immediately after an exception anyway, so
the bad just-restored value is discarded. But it does mean that
PL_stack_sp briefly points to junk on the stack. When the stack becomes
reference-counted, this will start to become important. So fix now.

This commit changes it so that call_sv()'s blk_oldsp gets set to just
before the arguments too. It also asserts that the stack was cleared
back to blk_oldsp.
The saved stack positions

    cxstack[0].oldsp
    cxstack[1].oldsp
    cxstack[2].oldsp
    ...

effectively divide the argument stack into a series of stack frames.
This unravels if an earlier oldsp is actually greater than a later one.
It didn't really matter before, but for a reference-counted stack it
becomes a mess when trying to unwind the stack after an exception.  So
assert that oldsp never decreases.

This assert found one failure in core, which was fixed by the previous
commit.
The various new inline functions like rpp_push_1() are intended to be
(approximate) replacements for the traditional PUSHs(), POPs() etc
macros, except that when compiled under PERL_RC_STACK, they understand
a reference-counted stack.

Except currently they assume the stack is still not reference-counted in
the presence of PERL_XXX_TMP_NORC. This will be turned off in a few
commits' time.
This inline function checks for whether an SV is only referenced
from the stack or temps stack. (Well, it only checks TEMPs currently, but
will shortly handle a ref-counted stack too). In particular it tests for:

    SvTEMP(sv) && && SvREFCNT(sv) == 1

This commit uses it in various places to replace where the conditions
are currently written out in full. This will allow those places to still
work correctly when the stack becomes reference-counted.
In the presence of PERL_RC_STACK, wrap most pp functions with a function
that calls the real pp function with a (non-reference-counted) copy of
the stack arguments.  On return, the wrapper bumps up the reference
count of the returned list and, as appropriate, shifts them down the
stack.

This allows a pp function which is not aware of a reference-counted
stack to be called in a reference-counted stack environment.

About 250 such functions are wrappable; the remaining 50 or so need
individual handling and will be dealt with in the following few commits.

This is a temporary band-aid which slows down calls to pp() functions.
It is expected that over time these functions will be unwrapped and
updated to to understand a reference-counted stack. But by temporarily
wrapping them now, it allows perl to be (experimentally) shifted over to
a reference-counted stack without having to review and modify all 30K
lines of pp code first.

(Except currently, the wrapper assumes the stack is still not
reference-counted in the presence of PERL_XXX_TMP_NORC. This will be
turned off in a few commits' time.)

Note also that pp_wrap() is not yet complete. It simulates wrapping by
making a copy of the args on entry to the wrapped function and shifting
any returned values back down. But as well as being temporarily
inhibited from actually modifying reference counts by PERL_XXX_TMP_NORC,
there are extra considerations which will be taken account of by the
"Allow stacks to be reference-counted" commit coming later.
The various method ops are sometimes naughty and substitute a different
object at the base of the current stack frame. This is "below"
the area of the stack normally processed by pp_wrap(), so the reference
count adjusting must be done manually.
Update pp_regcomp() to handle a reference-counted stack environment in
the presence of PERL_RC_STACK.

But in the presence of PERL_XXX_TMP_NORC, don't actually manipulate
reference counts yet. This will be turned off in a few commits' time.
iabyn added 25 commits February 19, 2023 13:38
This function is implemented as a custom op rather than as a function
call, so it wasn't getting wrapped for reference-count purposes by
either pp_wrap() nor xs_wrap().

This commit makes it directly handle a reference-counted stack.

This was a particularly confusing one, as a failing test in t/op/eval.t
was reduced to

    use Devel::Peek;
    Dump($@);
    eval { die "boo\n"; };
    Dump($@);

which showed the ref count of $@ increasing after the eval {}. But it
was actually Dump(), not eval{} which was messing with the ref count.
It has some code that calls out to PACKAGE::CLONE(). Depending on how
the interpreter is being cloned, the code which assembles the args on
the stack for the CLONE() call may be operating on either a
reference-counted stack, or a non-RC one. So handle both cases
Note that these two functions may be called both from PP functions aware
of a reference-counted stack and from wrapped (non-aware) functions,
hence the RC manipulation is runtime conditional.
S_ithread_run() starts running a new top-level perl interpreter, which
will have a ref-counted stack. So treat the stack a ref-counted.
A test that expects to fail on a non reference-counted stack should no
longer do so if the stack is fully reference-counted
Various places in core call DB::postponed(). This all happens in
places where the stack will be reference-counted, so fix the callers.
APItest.xs mainly consists of XS functions - which are not aware of a
ref counted stack - but such functions are called via xs_wrap(), so
that's ok.

However, THX_pp_establish_cleanup() is actually a PP function attached
to an op, rather than an XS function - so it doesn't get automatically
wrapped. Update it to handle a reference-counted stack.

Also, use the standard CALLRUNOPS() macro to call a runos loop rather
than invoking directly; the former will allow a wrapper to be inserted.

Finally, parameterise the expected ref count in t/magic.t, as it will
change if the stack is ref counted.
Generally shifting is done by adjusting the start position of the
array, so that AvALLOC() and AvARRAY() no longer align. Previously if
the array was AvREAL(), perl NULL-ed out the old pointer at the same
time, i.e. doing the equivalent of AvARRAY(av)[-1] = NULL.

This commit changes it so that on PERL_RC_STACK builds, perl instead
keeps the old pointer there on AvREAL() arrays too, even if it now
points to a freed SV; and instead makes av_clear() and av_unshift()
responsible for NULL-ing out the slot only when actually reclaiming any
unused slots between AvALLOC() and AvARRAY().

The reason for this is because of the mechanism whereby @db::args is
sometimes populated by caller(), as used by Carp.pm to display function
arguments in stack traces etc. For the common idiom of

    sub f {
        my $self = shift;
        ....;
    }

then previously, since @_ wasn't AvREAL(), the old pointer in @_ to
argument 0 was kept around and thus the hidden pointer value was still
available for caller() to add to @db::args.

But since the next commit will change @_ to be AvREAL(), this will no
longer work.  Thus to keep the ability to debug stack traces etc (via
the awful @db::args hack), we make shift keep the pointer even for the
AvREAL() case.
Traditionally, the @_ AV has been marked as AvREAL_off(), but AvREIFY_on().

This commit changes it so that on PERL_RC_STACK builds, @_ is AvREAL().
This will mean that when the stack is ref-counted, the stack frame can
be directly moved to @_ while maintaining the correct ref count for each
argument (in the same way that !AvREAL() worked well with a
non-ref-counted stack).

Note that the stack will not actually be reference-counted for a few
more commits yet. In the meantime, this commit allows for that by
bumping up the reference count of SVs being transferred from the stack
to @_. This extra step will soon be skipped.
These new inline functions are supposed to be near-identical
replacements for these macros:

    PUSHSTACKi(type)     push_stackinfo(type)
    POPSTACK()           pop_stackinfo()
    SWITCHSTACK(from,to) switch_argstack(to) // assumes (from == PL_curstack)

except that they don't require dSP to be in scope (they operate on
PL_stack_sp rather than sp) and their names more clearly specify what
sort of stack they manipulate.

The macros are now thin wrappers around the functions. I've kept most of
their uses in core for now as they are still used in places with dSP
present.
Add a _flags() variant of new_stackinfo() which indicates whether the
new stack AV should be created real or not.

Modify the new push_stackinfo() function to have a similar flag.

Then make the backcompat macros like PUSHSTACKi() still push a non-real
stack, while functions which have been updated to use the new
push_stackinfo() etc will be able get a real AV. The next commit makes
use of that.

This means that existing code (core and XS) which hasn't been updated to
the new ref-counted stack regime can do stuff like:

    PUSHSTACKi(FOO);
    PUSHMARK(sp)
    XPUSHs(sv);
    call_sv();

where call_sv() (or rather, the runops loop it invokes) will be able to
determine that it's been called from a non-RC environment and that the
args on the stack aren't reference-counted.

The next commit will update the runops loops etc to do exactly that.
When built with PERL_RC_STACK, this substantial commit:

* makes stacks AvREAL() by default.
* adds the si_stack_nonrc_base field to the stackinfo structure.
* adds the runops_wrap() wrapper

It also adds rpp_obliterate_stack_to() for clearing the stack on
exceptions

Collectively, this allows each stack to be in one of three states:

1) AvREAL(PL_curstack) && PL_curstackinfo->si_stack_nonrc_base == 0

(the new default) all the SVs pointed to from the stack are
reference-counted.

2) AvREAL(PL_curstack) && PL_curstackinfo->si_stack_nonrc_base > 0

items on the stack are reference-counted only below si_stack_nonrc_base.

3) !AvREAL(PL_curstack)

(existing behaviour) no items on the stack are reference-counted.

The way it generally works is that runops loops assume that all the PP
functions they call out to are reference-count-aware. Where this isn't
yet the case, the recently-added pp_wrap() and xs_wrap() functions
assume a reference-counted stack, but use the si_stack_nonrc_base
mechanism to call out to PP or XS functions which aren't aware of a
reference-counted stack.

Conversely, the runops_wrap() function added by this commit wraps calls
to runops loops, since such loops work only with an RC stack. So if
called with a non-RC or partially-RC stack, the wrapper temporarily
fixes up the stack to be fully-RC, calls the real runops loop, then on
return reverts the stack back to it's non-RC ways, mortalising any
return values if necessary.

This downgrading and upgrading by pp_wrap()/xs_wrap() and
runops_wrap() allows handling of multiple nesting of when, for
example, perl calls pp_entersub(), which calls XS code, which calls
call_sv(), which calls a runops loop, which calls pp_entersub(), which
calls XS code, and so on.

For si_stack_nonrc_base, this index value marks the lowest position on
the argument stack which is not reference-counted. The special (and
normal) value of 0 indicates that *all* items on the stack are
reference-counted.

The new function rpp_obliterate_stack_to() is a bit like
rpp_popfree_to(), except that it can handle stacks in all three of the
states listed above. It's intended to be used when dying, throwing
exceptions or exiting, since the stack could be in any state when that
happens.

Note that as of this commit, PERL_XXX_TMP_NORC is still defined, which
means that even in the presence of AvREAL(PL_curstack) etc, the stack
isn't yet actually reference counted. So with this commit, perl goes
through all the motions, (calling wrappers like runops_wrap()
etc), but skips actually manipulating any reference counts. There will
be a few more commits, fixing up a few more things, before
PERL_XXX_TMP_NORC will be removed.
When invoked via a scalar-context eval, die_unwind() was pushing
&PL_sv_undef without extending the stack first. It's been this way for
many years. Spotted by visual inspection while adding rpp_() code.

I could get a SEGV with the following code, but I haven't added it as a
test as it's highly sensitive to exactly what size stack is initially
allocated and the OS's malloc().

    my $x;
    my @A =
        ($x,$x,$x,$x,$x,$x,$x,$x,$x,$x,$x,$x,$x,$x,$x,$x,
         $x,$x,$x,$x,$x,$x,$x,$x,$x,$x,$x,$x,$x,$x,$x,$x,
         $x,$x,$x,$x,$x,$x,$x,$x,$x,$x,$x,$x,$x,$x,$x,$x,
         $x,$x,$x,$x,$x,$x,$x,$x,$x,$x,$x,$x,$x,$x,$x,$x,
         $x,$x,$x,$x,$x,$x,$x,$x,$x,$x,$x,$x,$x,$x,$x,$x,
         $x,$x,$x,$x,$x,$x,$x,$x,$x,$x,$x,$x,$x,$x,$x,$x,
         $x,$x,$x,$x,$x,$x,$x,$x,$x,$x,$x,$x,$x,$x,$x,$x,
         $x,$x,$x,$x,$x,$x,$x,$x,$x,$x,$x,$x,$x,$x,$x,
         scalar eval { die },
    );
This function in toke.c calls out to a random C-level filter function
for each source input chunk. Because that function may in turn invoke
a Perl function via call_sv() -  and may itself push stuff onto or off the
stack for that call -  present that function with a
non-reference-counted stack environment for backwards compatibility.
This function indicates whether an SV on the stack is kept alive only by
a single ref from the temps stack - which is often the case with stuff
pushed onto the stack by PP functions. Often this means that the SV can
be passed through rather then copied in places like returning from a
subroutine.

With a ref-counted stack, it's possible for a *non* SvTEMP SV to also be
stealable, if it has a refcount of 1, since that SV is being kept alive
purely by its (now refcounted) link from the stack.

So this commit adds that condition too (and also simplifies the
condition for efficiency).

In summary: originally perl (and then this function) did:

    if (SvTEMP(sv) && SvREFCNT(sv) == 1) { ... steal it ... }

Now this function's condition is the equivalent of

    #ifdef PERL_RC_STACK
        (    (SvREFCNT(sv) == 1)
        || (SvTEMP(sv) && SvREFCNT(sv) == 2))
    #else
        (SvTEMP(sv) && SvREFCNT(sv) == 1)
    #endif
When displaying the stack with perl -Ds or -Dsv, put a vertical bar (|)
at the boundary between the reference-counted part of the stack and the
non-reference-counted part, if any.

In addition with -Dsv, for each stack show its status;

    (real)         all items on the stack are reference-counted
    (partial real) items below si->si_stack_nonrc_base are RCed
    blank          no items are RCed
This macro was just a temporary crutch that disabled actually adjusting
the reference count of things being pushed and popped off the stack,
even on PERL_RC_STACK builds. It allowed the core to be converted to
support a reference-counted stack as a bunch of incremental changes.

This commit turns it off, enabling the stack to become actually
reference-counted on PERL_RC_STACK builds.

In a few commits' time, this macro will be completely eliminated from
the source code.
On perls built with both -DPERL_RC_STACK and -DDEBUG_LEAKING_SCALARS,
add asserts to the various old-style PUSHs() etc macros and new-style
rpp_push_1() etc functions that they're operating on the right sort of
stack. In particular:

PUSHs() and POPs() should only be used on stacks where rpp_stack_is_rc()
is false, since they don't modify the reference count of the SVs they
are pushing or popping.

Conversely, rpp_push_1(), rpp_popfree_1() etc should only be used on
stacks where rpp_stack_is_rc() is true, since they modify the reference
counts of the SVs they push and pop.

On perls not compiled with PERL_RC_STACK, the rpp_ functions don't
modify the reference counts, but on such builds the rpp_stack_is_rc()
assertions in the rpp_ functions are disabled, so it all works out
still.
on PERL_RC_STACK builds,
1) assert that the stack is reference-counted (rpp_stack_is_rc()).
2) assert the any split point (si_stack_nonrc_base) isn't above the
    top of the stack.
and put it in a static function, S_pp_xs_wrap_return().
After bumping up the reference counts of the returned values,
reset si_stack_nonrc_base earlier, so that if perl dies while
decrementing the original args, all reference counts will be properly
accounted for.

Also in xs_wrap(), only bother doing

    PL_markstack_ptr[0]  += nargs;

in the branch where nargs > 0.
This new macro allows an old-style non-reference-counted PP-style
function to be used on a PERL_RC_STACK builds. It's intended for
non-core PP functions which are typically in an XS file and are used to
replace the PP function of an existing op, or to be attached to a custom
op.

It's virtually identical to the PP_wrapped() macro, except that it
uses the name as-is rather than prepending Perl_ to the name
In this test function, push a couple of stackinfos, one with a
ref-counted stack, and one without.
After cloning, pop the cloned stackinfos and assert that the cloned
argument stacks are also appropriately RC or non-RC.

This should probably have been done properly as some new tests rather
than a couple of asserts, but that would have involved a lot more
effort.
This macro was disabled a few commits ago. Now remove all use of it
from the core.

It was just a temporary crutch that disabled actually adjusting the
reference count of things being pushed and popped off the stack, even on
PERL_RC_STACK builds. It allowed the core to be converted to support a
reference-counted stack as a bunch of incremental changes.
Explain what all the new rpp_foo() functions are for.
@khwilliamson
Copy link
Contributor

khwilliamson commented Feb 20, 2023 via email

@iabyn iabyn closed this Feb 26, 2023
@iabyn iabyn deleted the smoke-me/davem/rc_stack1 branch February 26, 2023 14:53
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants