506 Commits

Author SHA1 Message Date
Richard Leach
4342210875 Perl_leave_scope - sv_backoff shouldn't do an unnecessay string copy
When a `my` SV goes out of scope, any OOK hack on its string buffer is
undone by `Perl_sv_backoff`. If the SV is `SvOK`, a copy of the buffer
contents will occur, but since the contents are defunct at this point,
the copy is unnecessary.

See https://github.com/Perl/perl5/issues/23967 as an example of where
this unnecessary copy had a noticeable effect on performance.

This commit essentially inlines the necessary parts of `sv_backoff` to
avoid the copy, without excessive messing around with `sv`'s flags at
the call site in `Perl_leave_scope`.
2025-12-03 00:28:25 +00:00
Tony Cook
0c859ae7a5 regcomp: handle cloning the rexc cleanup in the scope stack
Previous on Win32 this could cause a double-free of the RExC state if
an emulated fork was done with the free of the state on the scope
stack.

Use a custom save type and prevent freeing in the cloned process to
prevent the double-free.

Fixes #23022
2025-04-19 17:25:53 +02:00
Leon Timmermans
410115a66c Dont call Perl_warn manually in core
Just call warn instead, we've been able to do that for vararg functions
since d933027ef0a56c99aee8cc3c88ff4f9981ac9fc2
2025-03-18 04:16:51 +01:00
Leon Timmermans
06c3a62f12 Dont call Perl_croak manually in core
Just call croak instead, we've been able to do that for vararg functions
since d933027ef0a56c99aee8cc3c88ff4f9981ac9fc2
2025-03-18 04:16:51 +01:00
Lukas Mai
a0f2cb392e fix various typos in C source comments
Fixes #22741, #22739, #22736.
2024-11-18 05:44:18 +01:00
Karl Williamson
acf02c631c Remove docs of broken SAVELONG and SAVEt_LONG
This macro can't work, because it calls a non-existent function:
save_long(); The accomanying SAVEt_LONG is useless as a result.
2024-08-28 12:30:40 -06:00
Tony Cook
a742fa0e61 allow building with high-water mark to be independent of -DDEBUGGING
This allows a debugging perl to be built with the high water mark
checks disabled, or a non-debugging perl to be built with the
high water marks enabled.

This should allow Debian, the reporter for #16607 to build both their
normal perl and debugperl with the same state of high water mark
checks and avoid the mismatch between a debugperl and non-debug
dynamic extension.

Fixes #16607
2024-04-15 09:54:26 +10:00
Karl Williamson
f4c40281d5 perlintern: Fix misspelling 2024-02-25 16:53:40 -07:00
Tony Cook
fea90cfbe1 grow the tmps (mortal) stack exponentially rather than linearly
As with the value stack and the save stack, this gives us constant
amortized growth per element.

After this patch the profiler shows the "SvPV_shrink_to_cur(sv)"
and "sv = sv_2mortal(newSV(80))" calls in do_readline as the
hotspots for the io unheated test case, using 55% of the measured
time in total.

Fixes #21654
2023-12-05 08:57:15 +11:00
Tony Cook
8f4707479f ensure we don't extend the stack beyond the limit of MARK
Previously this checked against a limit of SSize_t, but if MARK
is only 32-bits then over 2G stack entries will likely cause a
crash.
2023-10-11 09:44:20 +11:00
Tony Cook
a1ab4f2eff allow the markstack pointer type to be selected between I32 and SSize_t
By default it will use I32 for backward compatibility, but you
can select SSize_t during configuration with:

  -Accflags=-DPERL_STACK_OFFSET_SSIZET

which may one day become the default.

The actual offsets throughout the code continue to be SSize_t.
2023-10-11 09:44:20 +11:00
Tony Cook
c0588928a3 64-bit stack: first pass, marks are now 64-bits on 64-bit platforms
The basic test passes

Works towards #20917
2023-09-25 14:35:17 +10:00
David Mitchell
ba1af285ef Allow argument stacks to be reference-counted
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.
2023-08-16 17:17:00 +01:00
David Mitchell
026c6b9cdb allow argument stack to be AvREAL()
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.
2023-08-16 17:16:59 +01:00
David Mitchell
3da065caca pp_dbstate: skip SAVESTACK_POS()
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.
2023-08-16 17:16:58 +01:00
Yves Orton
2f920c2f73 scope.c - add mortal_destructor_sv() and mortal_svfunc_x()
The function SAVEDESTRUCTOR_X() (save_destructor_x) can be used to
execute a C function at the end of the current psuedo-block. Prior to
this patch there was no "mortal" equivalent that would execute at the
end of the current statement. We offer a collection of functions which
are intended to free SV's at either point in time, but only support
callbacks at the end of the current pseudo-block.

This patch adds two such functions, "mortal_destructor_sv" which can be
used to trigger a perl code reference to execute at the end of the
current statement, and "mortal_svfunc_x" which can be used to trigger an
SVFUNC_t C function at the end of the current statement.

Both functions differ from save_destructor_x() in that instead of
supporting a void pointer argument they both require their argument to
be some sort of SV pointer. The Perl callback function triggered by
"mortal_destructor_sv" may be provided no arguments, a single argument
or a list of arguments, depending on the type of argument provided to
mortal_destructor_sv(): when the argument is a raw AV (with no SV ref
wrapping it), then the contents of the AV are passed in as a list of
arguments. When the argument is anything else but NULL, the argument is
provided as a single argument, and when it is NULL the perl function is
called with no arguments.

Both functions are implemented on top of a mortal SV (unseen by the
user) which has PERL_MAGIC_destruct magic associated with it, which
triggers the destructor behavior when the SV is freed.

Both functions are provided with macros to match the normal SAVExx()
API, with MORTALDESTRUCTOR_SV() wrapping mortal_destructor_sv() and
MORTALSVFUNC_X() wrapping mortal_svfunc_x().

The heart of this logic cribbed from Leon Timmermans' Variable-OnDestruct.
See the code at:

https://metacpan.org/dist/Variable-OnDestruct/source/lib/Variable/OnDestruct.xs#L6-17

I am very grateful to him for his help on this. Any errors or omissions
in this code are my fault, not his.
2023-03-18 20:57:59 +08:00
Yves Orton
624f6f53b1 scope.c - improved RCPV support, SAVERCPV SAVEFREERCPV
I misnamed some of the RCPV stuff when it was introduced. The macro
which I called SAVERCPVFREE should have been called SAVERCPV,
and there should also have been a SAVEFREERCPV macro.

Note the naming system for these functions is a bit confusing.
SAVEFREERCPV /just/ frees. SAVERCPV saves and restores (eg, its like
local). SAVEFREESV is an exception. :-( and it behaves like SAVERCPV.
See for instance SAVEPV or similar macros.

There also should have been RCPV_REFCNT_dec() and RCPV_REFCNT_inc()
macros.

There was also missing documentation. This patch should fix all of these
issues. Since this functionality has never been released in a production
perl it should be fine to do these renames, nothing out there should use
these macros yet.

I noticed these oversights while fixing Scope::Upper, see also:
https://github.com/Perl/perl5/issues/20861
https://rt.cpan.org/Ticket/Display.html?id=146897
2023-03-06 14:43:25 +08:00
Yves Orton
ad2d7a7b84 scope.c - fix typo 2023-03-06 14:43:25 +08:00
Yves Orton
fc11df3e7c scope.c - rework SSGROW() and SSCHECK() macros and undelying functions
Prior to this patch SSCHECK() took a "needs" parameter, but did not
actually guarantee that the stack would be sufficiently large to
accomodate that many elements after the call. This was quite misleading.
Especially as SSGROW() would not do geometric preallocation, but
SSCHECK() would, so much of the time SSCHECK() would appear to be a
better choice, but not always.

This patch makes it so SSCHECK() is an alias for SSGROW(), and it makes
it so that SSGROW() also geometrically overallocates. The underlying
function that used to implement SSCHECK() savestack_grow() now calls the
savestack_grow_cnt() which has always implemented SSGROW(). Anything
in the internals that used to call SSCHECK() now calls SSGROW() instead.

At the same time the preallocation has been made a little bit more
aggressive, ensuring that we always allocate at least SS_MAXPUSH
elements on top of what was requested as part of the "known" size of the
stack, and additional SS_MAXPUSH elements which are not part of the
"known" size of the stack. This "hidden extra" is used to simply some of
the other macros which are used a lot internally. (I have beefed up the
comment explaining this as well.)

This fixes GH Issue #20826
2023-02-20 16:17:07 +08:00
Tony Cook
2e4090b824 only fully calculate the stash (effective) name where needed
gcc 12 was complaining that evaluating (somehekptr)->hek_key
was always true in many places where HvNAME() or HvENAME() was
being called in boolean context.

Add new macros to check whether the names should be available and
use those instead.
2022-11-18 18:12:45 -05:00
Yves Orton
fdec6615b5 scope.* - revert and rework SAVECOPWARNINGS change
We can't put PL_compiling or PL_curcop on the save stack as we don't
have a way to ensure they cross threads properly. This showed up as a
win32 t/op/fork.t failure in the thread based fork emulation layer.

This adds a new save type SAVEt_CURCOP_WARNINGS and macro
SAVECURCOPWARNINGS() to complement SAVEt_COMPILER_WARNINGS and
SAVECOMPILEWARNINGS(). By simply hard coding where the pointers should
be restored to we side step the issue of which thread we are in.

Thanks to Graham Knop for help identifying that one of my commits was
responsible.
2022-11-04 10:35:40 +01:00
Tony Cook
1a2b24d857 change the return value of SSNEW to SSize_t
The normal savestack index is an I32, but that counts in ANY
(which are typically the larger of pointer or IV sizes), this
meant is the save stack was large, but still nowhere need it's
limit, the result of SSNEW() could overflow.

So make the result SSize_t and adjust SSPTR() to match.

SSPTR() asserts to ensure the supplied type is the same size as
SSize_t to ensure callers are updated to handle the new limit.
2022-11-03 09:48:23 +11:00
Yves Orton
f8552c1a7e cop.h - get rid of the STRLEN* stuff from cop_warnings
With RCPV strings we can use the RCPV_LEN() macro, and
make this logic a little less weird.
2022-11-02 08:49:32 +01:00
Yves Orton
39cf776065 scope.* - more flexible ways to save warning bits 2022-11-02 08:49:32 +01:00
Yves Orton
721bab5938 scope_types.h - new header file for scope type data
Next patch this data will be autogenerated.
2022-11-01 11:57:31 +01:00
Yves Orton
6760f691a9 cop.h - add support for refcounted filenames in cops under threads
We have a weird bifurcation of the cop logic around threads. With
threads we use a char * cop_file member, without it we use a GV * and
replace cop_file with cop_filegv.

The GV * code refcounts filenames and more or less efficiently shares
the filename amongst many opcodes. However under threads we were
simplify copying the filenames into each opcode. This is because in
theory opcodes created in one thread can be destroyed in another. I say
in theory because as far as I know the core code does not actually do
this. But we have tests that you can construct a perl, clone it, and
then destroy the original, and have the copy work just fine, this means
that opcodes constructed in the main thread will be destroyed in the
cloned thread. This in turn means that you can't put SV derived
structures into the op-tree under threads. Which is why we can not use
the GV * stategy under threads.

As such this code adds a new struct/type RCPV, which is a refcounted
string using shared memory. This is implemented in such a way that code
that previously used a char * can continue to do so, as the refcounting
data is located a specific offset before the char * pointer itself.
This also allows the len data to embedded "into" the PV, which allows
us to expose macros to acces the length of what is in theory a null
terminated string.

    struct rcpv {
        UV      refcount;
        STRLEN  len;
        char    pv[1];
    };
    typedef struct rcpv RCPV;

The struct is sized appropriately on creation in rcpv_new() so that the
pv member contains the full string plus a null byte. It then returns a
pointer to the pv member of the struct. Thus the refcount and length and
embedded at a predictable offset in front of the char *, which means we
do not have to change any types for members using this.

We provide three operations: rcpv_new(), rcpv_copy() and rcpv_free(),
which roughly correspond with newSVpv(), SvREFCNT_inc(), SvREFCNT_dec(),
and a handful of macros as well. We also expose SAVERCPVFREE which is
similar to SAVEGENERICSV but operates on pv's constructed with
rcpv_new().

Currently I have not restricted use of this logic to threaded perls. We
simply do not use it in unthreaded perls, but I see no reason we
couldn't normalize the code to use this in both cases, except possibly
that actually the GV case is more efficient.

Note that rcpv_new() does NOT use a hash table to dedup strings. Two
calls to rcpv_new() with the same arguments will produce two distinct
pointers with their own refcount data.

Refcounting the cop_file data was Tony Cook's idea.
2022-11-01 11:57:31 +01:00
Yves Orton
e6421d31ff mg.c/perl.c/scope.c - fixup mangled indentation and whitespace
Various code related to set_and_free_cop_warnings was terribly mangled
as far as whitespace goes. This patch cleans it up so it is readable and
correctly indented. Whitespace only changes.
2022-11-01 11:57:31 +01:00
Yves Orton
3fe8433633 scope.c - sanity check a var before we use it 2022-10-24 14:33:55 +02:00
Paul "LeoNerd" Evans
53083cad3a Use HvHasAUX() rather than SvOOK() when operating on HVs 2022-07-02 23:00:28 +01:00
Karl Williamson
82943faa9f Convert '!!' to cBOOL()
I believe the '!!' is somewhat obscure; I for one didn't know about it
for years of programming C, and it was buggy in one compiler, which is why
cBOOL was created, I believe.  And it is graphically dense, and
generally harder to read than the cBOOL() construct.

This commit dates from before we moved to C99 where we can simply cast
to (bool), and cBOOL() has been rewritten to do that.  But the vast
majority of code uses cBOOL(), and this commit brings the remainder of
the core .[ch] files into uniformity.
2022-06-14 07:38:34 -06:00
Karl Williamson
4601e42512 perlapi: Document save_pushptr 2022-05-31 05:30:17 -06:00
Karl Williamson
d4192ed96f perlapi: Document save_[ah]elem(_flags)? 2022-05-27 21:24:39 -06:00
Karl Williamson
077e4a84c6 perlintern: Document save_scalar_at 2022-05-27 21:24:39 -06:00
Karl Williamson
1f3d7af79a perlapi: Document save_alloc; mark as internal
This implements SSNEW and kin, which should be used instead of this.
2022-05-18 04:40:51 -06:00
Karl Williamson
780fa0f328 perlapi: fix typo 2022-05-15 09:59:56 -06:00
Karl Williamson
06ecdb436a perlapi: Add markup for save_gp 2022-05-14 20:29:03 -06:00
Karl Williamson
aaa067eafc perlintern: Document save_[ah]delete; mark internal
These implement SAVE[AH]HDELETE which are the API interfaces for this
functionality.
2022-05-11 15:02:44 -06:00
Karl Williamson
a8ed036f55 Mark internal and document leave_scope 2022-05-07 14:00:24 -06:00
Karl Williamson
e446bf9324 perlapi: Mark internal and document some save_FOO fcns
These implement uppercase-named macros.
2022-05-07 11:17:13 -06:00
Karl Williamson
f58e1f6ccb perlapi: Mark internal, document (push|pop)_scope 2022-05-07 11:17:13 -06:00
Richard Leach
8fcb24256a Inlined newSV_type(SVt_NULL) leaner than non-inlined newSV(0)
When a function outside of sv.c creates a SV via newSV(0):
 * There is a call to Perl_newSV
 * A SV head is uprooted and its flags set
 * A runtime check is made to effectively see if 0 > 0
 * The new SV* is returned

Replacing newSV(0) with newSV_type(SVt_NULL) should be more efficient,
because (assuming there are SV heads to uproot), the only step is:
 * A SV head is uprooted and its flags set
2022-03-07 01:08:53 +01:00
Paul "LeoNerd" Evans
78efaf0398 Add a PL_prevailing_version interpreter var
Save/restore PL_prevailing_version at SAVEHINTS time

Have PL_prevailing_version track the applied use VERSION currently in scope
2022-02-13 18:46:05 +00:00
Dagfinn Ilmari Mannsåker
2eb109a4d3 Remove NetWare support
The build has been broken since 2009.
2021-10-08 19:21:33 +01:00
Paul "LeoNerd" Evans
f79e2ff95f Create defer syntax and OP_PUSHDEFER opcode
Adds syntax `defer { BLOCK }` to create a deferred block; code that is
deferred until the scope exits. This syntax is guarded by

  use feature 'defer';

Adds a new opcode, `OP_PUSHDEFER`, which is a LOGOP whose `op_other` field
gives the start of an optree to be deferred until scope exit. That op
pointer will be stored on the save stack and invoked as part of scope
unwind.

Included is support for `B::Deparse` to deparse the optree back into
syntax.
2021-08-25 13:52:09 +01:00
Paul "LeoNerd" Evans
eb7e169eaa Rename G_ARRAY to G_LIST; provide back-compat when not(PERL_CORE) 2021-06-02 00:29:54 +01:00
Michael G. Schwern
1604cfb027 style: Detabify indentation of the C code maintained by the core.
This just detabifies to get rid of the mixed tab/space indentation.

Applying consistent indentation and dealing with other tabs are another issue.

Done with `expand -i`.

* vutil.* left alone, it's part of version.
* Left regen managed files alone for now.
2021-01-17 09:18:15 -07:00
Paul "LeoNerd" Evans
8462890b43 Implement SAVEt_STRLEN_SMALL
Most uses of SAVEt_STRLEN actually store small values; often zero.
Rather than using an entire U64-sized element for these values, it saves
space to use the same "SMALL" mechanism as other numerical values, like
SAVEt_INT_SMALL.
2020-12-09 12:00:56 +11:00
Karl Williamson
3f6206216e autodoc.pl: Enhance apidoc_section feature
This feature allows documentation destined for perlapi or perlintern to
be split into sections of related functions, no matter where the
documentation source is.  Prior to this commit the line had to contain
the exact text of the title of the section.  Now it can be a $variable
name that autodoc.pl expands to the title. It still has to be an exact
match for the variable in autodoc, but now, the expanded text can be
changed in autodoc alone, without other files needing to be updated at
the same time.
2020-11-06 06:16:04 -07:00
Karl Williamson
51b56f5c7c Reorganize perlapi
This uses a new organization of sections that I came up with.  I asked
for comments on p5p, but there were none.
2020-09-04 16:13:25 -06:00
Paul "LeoNerd" Evans
6c148c868e Define a new SAVEt_HINT_HH type
Rather than possibly push an extra HV* to the save stack if the right
bit is set in the (saved) hints flags, better just to define a different
SAVEt type. Having done this, the stack layout is now constant per type
value.

This fixes

  https://github.com/Perl/perl5/issues/17895
2020-07-30 15:05:00 -06:00