471 Commits

Author SHA1 Message Date
Paul "LeoNerd" Evans
05fb84472b Two bugfixes to field handling during thread cloning
This fixes two separate but related bugs.

 * Thread clone crashed if any class existed with no fields in it. This
   is fixed by permitting a NULL PADNAMELIST parameter to
   padnamelist_dup(). [GH23771]

 * Thread cloning would unreliably segfault due to missing
   PadnameFIELDINFO() of an outer closure capture, depending on the
   exact order of CV discovery. This is fixed by correct usage of the
   PL_ptr_table to store details of cloned `struct padname_fieldinfo`
   structures, and careful ordering of assignments and recursive clone
   calls.
2025-09-26 19:16:46 +01:00
Paul "LeoNerd" Evans
162b6fd168 Assert on args to padnamelist_dup() before trying to use them 2025-09-26 19:16:35 +01:00
Karl Williamson
4913086621 perlapi: Add detail to pad_findmy_pv entry 2025-09-19 07:59:35 -06:00
Karl Williamson
505e049e46 perlapi: Use correct name for pad_findmy_pvn parameter 2025-09-09 10:50:08 -06:00
Karl Williamson
fec05dbb71 pad.c: Remove trailing spaces 2025-08-22 10:51:55 -06:00
Lukas Mai
27ea2e4033 don't use Perl_suspend_compcv as destructor directly
SAVEDESTRUCTOR_X needs a function whose type is 'void (pTHX_ void *)'
exactly, so introduce a little shim.

Should fix this ASan error:

    scope.c:1543:13: runtime error: call to function Perl_suspend_compcv through pointer to incorrect function type 'void (*)(struct interpreter *, void *)'
2025-08-16 19:27:41 +02:00
Karl Williamson
801f8b8959 Add some ARGS_ASSERT calls
An ARGS_ASSERT macro is always generated for every function listed in
embed.fnc, unless possibly suppressed with the G flag for that entry.
The generated macro is empty if there is nothing to assert.

It is mandatory (enforced through a porting test) to call that macro
when non-empty.  (Hopefully the call occurs in the function the macro is
designed for, but the porting test is currently simplistic and doesn't
check for that; often compilation would fail anyway if it did get placed
in the wrong function, as the parameter names the macro expects and the
ones in the function could easily not match).

It is optional (but a good idea) to call the macro even when empty.
That way this commit would not have been necessary.  From time to time,
an empty macro becomes non-empty as we figure out more things to check
for.  When that happens, the porting test fails for the newly-non-empty
macros that aren't called.  If the function had originally called the
empty-one, its source wouldn't have to change at all.

Several commits from now will make some ARGS_ASSERT macros non-empty;
this commit adds calls to the ones that weren't already called.
2025-08-02 15:02:34 -06:00
Dagfinn Ilmari Mannsåker
30aeaad51e Stop calling Perl_*warn*() manually in core
Except reg*.[ch], which are also compiled "outside" core for re.pm
2025-03-19 06:37:08 +01: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
dbd0f2f14f Avoid calling Perl_croak_nocontext from core
In core we almost always have a context, or we can easily get one.
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
Tony Cook
90595091f2 Revert "Revert "[perl #89544] Non-eval closures don’t need CvOUTSIDE""
This reverts commit 386907f061c1812ecaa5f3c88d9f729828408097.

Reinstates the behaviour of CV outside references from 5.38, fixing #22547

Breaks #19370
2025-01-08 10:35:39 +11:00
Paul "LeoNerd" Evans
8e916e2da4 pad.c: Ensure SvTYPE(cvbody) == SVt_PVCV during destruction to keep asserts happy 2024-07-24 17:03:29 +01:00
Paul "LeoNerd" Evans
de762f9311 Define and use macro families for typed refcount increment
Create specific macros for incrementing the refcount of an AV, CV, GV or
HV, returning a typed pointer to avoid the caller needing to cast the
result. This yields neater easier-to-read code.

While not implemented here, this gives an opportunity to add an
`assert()` check on the SvTYPE of the structure being adjusted, for
extra debug checking during debugging builds.
2024-07-22 16:51:49 +01:00
Karl Williamson
b00fe9dd8f perlapi: Combine all forms of pad_findmy() 2024-06-30 20:23:34 -06:00
Karl Williamson
fe64bd37d6 perlapi: Combine all forms of pad_add_name()
Having one group containing the descriptions of all closely related
functions makes the pod more compact and makes maintenance easier; fixes
only need to be applied in one place.  And it encourages the
documentation authors to compare and contrast the variants, paying
closer attention to the subtle differences between them.

And it is easier for the reader to choose the variant that is best for
their current purpose, rather than hopping around the file, unsure if
the current text is identical to that found elsewhere, or if there is a
subtle nuance (or three).
2024-06-25 09:27:07 -06:00
David Mitchell
d06d7106c2 add CvEVAL_COMPILED() flag and fix closure bug.
EVAL CVs are treated a bit weirdly: their CvROOT() and CvSTART() fields
don't get populated; instead the current values are stored in the
PL_eval_root and PL_eval_start variables while they are being executed.

This caused a bug in closures and nested evals when an inner eval was
repeated twice. The first inner eval accessed an outer lexical, which
caused a fake cache entry to be added to the outer eval's pad. The
second inner eval finds this cached entry, but incorrectly concludes
that the outer eval is in fact an anon sub prototype and issues a
'variable is not available' warning. This is due to this simplistic
definition in pad.c:

    #define CvCOMPILED(cv) CvROOT(cv)

This commit adds a new flag, CvEVAL_COMPILED(), to indicate a
fully-compiled EVAL CV. This allows us to work around the limitation.

In an ideal world this would have been fixed instead by making EVAL CVs
first-class citizens with CvROOT() etc, but plenty of stuff seems to
assume otherwise. So I took the path of least resistance.

See https://www.perlmonks.org/?node_id=11158351
2024-06-17 10:48:33 +01:00
David Mitchell
17535c984a fix refcount on cloned constant state subs
Perl was doing this:

    $ perl -e'sub { CORE::state sub FOO () { 42 } }'
    Attempt to free unreferenced scalar: ...
    $

This warning was in particular now showing up on stderr on bleed builds:
ever since the recent addition of similar code to Deparse.t with
v5.39.9-33-g4a55343c55.

When a sub is made constant, it is converted into an XS sub, and the
IV(42) SV is stored in the CV's CvXSUBANY(cv).any_sv field.

But state subs (even const ones) get cloned if wrapped within an outer
anon sub and then that outer sub gets cloned. And it turns out that when
a const state sub is cloned, the ref count of that const SV wasn't being
incremented.

The fix is trivial. But there were two possible ways to fix it. The
approach I chose was to fix the cloning code so that it increments on
CvCONST(cv) being true in addition to on CvREFCOUNTED_ANYSV(cv) being
true.

The other approach (and arguably more logically correct) would be to set
the CVf_REFCOUNTED_ANYSV flag on const subs too, but this involves
modifying the code in multiple places, e.g.  newMYSUB(), newATTRSUB_x(),
newCONSTSUB_flags(), and makes it more likely that CPAN XS code out
there which cargo-cults similar code would also need fixing. So my fix
is simpler, more robust, but less satisfying.

Note that before 5.36.0, the failing code example above would segfault
rather than warn.
2024-04-22 08:58:58 +01:00
Paul "LeoNerd" Evans
743293ba24 Revert "Warn about shadowing names when importing a lexical sub whose name matches a package one"
This partly reverts commit 31273fbcec76b8eef204e7d7c8dde2b78d0e0ce5
which was committed in https://github.com/Perl/perl5/pull/21915

It has been found that this warning creates too many warnings from
otherwise-harmless situations, and additionally acts inconsistently with
other behaviours such as lexical variables. In any case it only warns in
the less likely package-then-lexical order, and not in the more likely
lexical-then-package order which was the original intent of the
discussion leading up to its invention.

We decided to remove it https://github.com/Perl/perl5/pull/21915#issuecomment-1998021478

I have left the changes to t/op/attrproto.t and t/op/lexsub.t in place
because those were just renames for something that made debugging the
tests confusing. They're not directly related to this warning, so should
remain.
2024-03-19 14:33:59 +00:00
Paul "LeoNerd" Evans
4870fd2e1a Remove the tombstone-related code from pad.h + pad.c
This was a short-lived experimental feature intended to implement
removal of lexical symbols, in order to provide syntax like
`no builtin ...` or to remove implied imported builtins on a change of
prevailing `use VERSION`. The model of removing a lexical has proven to
be too subtle and complex to implement as well as raising various
awkward questions about the semantics, so we decided to remove it again
in Perl 5.39.8. There is now no longer any code that uses
PADNAMEf_TOMBSTONE.

As PADNAMEf_TOMBSTONE was only added in earlier in the 5.39.x developent
series, there is no need to retain the constants in .h files for
compatibility or to reserve the numbers for them. They have never
appeared in a stable release of Perl.
2024-03-06 00:21:58 +00:00
Tony Cook
4fc2379a01 make a new stub to clone into when pushing a new pad
This previously put the same CV into the inner pad, so on a
recursive call into the owning sub, from this sub, this CV would
still be active, and the attempt to clone into the still busy CV
would throw an error.

Fixes #18606
2024-02-20 14:38:56 +11:00
Paul "LeoNerd" Evans
e6b735631f Warn about shadowing names when importing a lexical sub whose name matches a package one 2024-02-14 22:51:57 +00:00
Lukas Mai
a9ab5eec7b pad.h: remove pad_peg (was part of MAD)
This was missed in b5bbe64ad2ec5141 when most of MAD was removed.
pad_peg() has been a no-op macro ever since.
2024-02-14 17:53:43 +01:00
Tony Cook
6ea84ede02 pad_add_name_pvn(): the name is in UTF-8
This came up in discussion for #21927
2024-02-14 09:57:48 +11:00
Paul "LeoNerd" Evans
e25b066d3a pad.c: NULL out the pad entry corresponding to a tombstone
When adding a tombstone entry to the pad, there's no need to use a blank
SV for its pad entry, as it will never be used for anything.

This does now mean that pad slots whose names start '&' could now become
NULL, so downstream modules that try to inspect the pad should be a bit
more careful when looking, to see if it is a tombstone. If this commit
proves problematic for CPAN modules, we could undo it and leave all the
pad slots as blank SVt_NULL instead, at a slight increase in memory
usage.
2024-01-24 23:16:50 +00:00
Tony Cook
386907f061 Revert "[perl #89544] Non-eval closures don’t need CvOUTSIDE"
But they do need CvOUTSIDE() for eval-in-package-DB's scope
magic to work correctly.

This also incidentally fixes a TODO Deparse, since the CvOUTSIDE
is now available

Fixes #19370 aka rt89544

This breaks #11286, but I don't think that's fixable without breaking
eval-in-DB's special scope behaviour.

This reverts (most of) commit a0d2bbd5c47035a4f7369e4fddd46b502764d86e.
2023-09-25 10:24:07 +10:00
Paul "LeoNerd" Evans
5e7a38e197 Define a PADNAMEf_TOMBSTONE for lexically deleting pad entries
A pad entry with this flag means that we should consider the name does
not exist. If we find that name on lookup, return NOT_IN_PAD instead.

This will be used for implementing the unexport version of lexical
imports used by `use builtin`, allowing `no builtin ...` to remove pad
entries again.
2023-09-08 12:48:25 +01:00
David Mitchell
2c204b7a22 make @_ AvREAL() on RC-stack
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.
2023-08-16 17:16:59 +01:00
Tony Cook
59b7881009 cv_undef_flags: there's nothing to clean up for our subs in cloned pads
Fixes #21067

When an instance of a closure is created, the pad entries for the
cloned sub are copied (S_clone_sv_pad()) from either the prototype
sub (for lexicals belonging to the sub) or from the context (for
lexicals from outside
the sub).

This doesn't happen for our variables, instead the cloned sub has
these pad entries set to NULL.

This isn't an issue at runtime since references to lexical our
sub/variables are compiled into GV or GVSV ops.

cv_undef_flags() assumed this these entries were non-NULL for all
lexical subs.

Fix by checking if the entry is an our sub and the CV being freed is
a clone, if so, skip the accesses to the NULL pad entry.

Added an assert to the new branch to ensure any changes in the above
results in an early failure here and hopefully a fix.
2023-07-25 15:49:15 +10:00
Karl Williamson
fccb25289c perlapi: 'resume_compcv' doesn't exist
I presume what was meant here is 'resume_compcv_final'
2023-07-06 07:52:50 -06:00
Paul "LeoNerd" Evans
725e4a6205 Fix typo of 'compilation' in docs in pad.c 2023-02-19 20:33:13 +00:00
Paul "LeoNerd" Evans
04c0207ebb Fix a bunch of memory leaks in feature 'class'
* Free the attrlist OP fragment when applying class or field attribute
 * Free the OP_PADxV ops we only use to get the pad index out for
   fieldvar declarations
 * Add a refcount to the `struct padname_fieldinfo` to keep track of its
   capture in inner closures so it can be freed at the right time
 * Free the class-related fields out of HvAUX
 * Free the actual ObjectFIELDS() array when destroying an object instance
 * Dup fieldinfo->paramname at sv_dup() time / free it at free time
2023-02-17 21:06:16 +00:00
Paul "LeoNerd" Evans
39e1ad85b8 Ensure that sv_dup() handles new class structures
It needs to:
 * clone SVt_PVOBJ instances
 * clone the xhv_class_* fields of an SVt_PVHV
 * clone the PadnameFIELDINFO structure of a padname
2023-02-13 17:41:49 +00:00
Paul "LeoNerd" Evans
e7faea5438 Provide padname_dup_inc() and padnamelist_dup_inc() 2023-02-13 16:01:46 +00:00
Paul "LeoNerd" Evans
45826d9c09 Move the macros which wrap sv_dup_inc() into sv.h 2023-02-13 16:01:46 +00:00
Paul "LeoNerd" Evans
99b497aa90 Initial attack at basic 'class' feature
Adds a new experimental warning, feature, keywords and enough parsing to
implement basic classes with an empty `new` constructor method.

Inject a $self lexical into method bodies; populate it with the object instance, suitably shifted
Creates a new OP_METHSTART opcode to perform method setup
Define an aux flag to remark which stashes are classes

Basic implementation of fields.

Basic anonymous methods.
2023-02-10 12:07:02 +00:00
Paul "LeoNerd" Evans
b40895ae55 Define the concept of a suspended compcv 2023-02-10 12:07:02 +00:00
James E Keenan
0c6362adf0 Correct typos as per GH 20435
In GH 20435 many typos in our C code were corrected.  However, this pull
request was not applied to blead and developed merge conflicts.  I
extracted diffs for the individual modified files and applied them with
'git apply', excepting four files where patch conflicts were reported.
Those files were:

        handy.h
        locale.c
        regcomp.c
        toke.c

We can handle these in a subsequent commit. Also, had to run these two
programs to keep 'make test_porting' happy:

        $ ./perl -Ilib regen/uconfig_h.pl
        $ ./perl -Ilib regen/regcomp.pl regnodes.h
2022-12-29 09:39:58 -05:00
Paul "LeoNerd" Evans
79277e97f5 Add a PadnameREFCNT_inc() macro
Implemented as a static inline function call, so that it can return the
padname pointer itself.  This would allow use in expressions such as

  ptr->field = PadnameREFCNT_inc(pn);

That makes it similar to the familiar SvREFCNT_inc() macro.
2022-08-17 15:59:15 +01:00
Paul "LeoNerd" Evans
b234f9dcb6 Define a CvREFCOUNTED_ANYSV flag
If this flag is set, then the CvXSUBANY(cv).any_sv pointer will have its
reference count decremented when the CV itself is freed. This is useful
for XS extensions that wish to store extra data in here. Without this
flag, such extensions have to resort to using magic with a 'free'
function to perform this work.
2022-08-16 17:28:08 +01:00
Paul "LeoNerd" Evans
ec11e33811 Rename CVf_METHOD to CVf_NOWARN_AMBIGUOUS
Also renames the CvMETHOD* macro family to CvNOWARN_AMBIGUOUS*
2022-07-26 11:32:40 +01:00
Paul "LeoNerd" Evans
494412901f Guard the older SvPAD* wrappers with #ifndef PERL_CORE 2022-07-05 13:57:12 +01:00
Paul "LeoNerd" Evans
792bf4bab0 Neaten the PADNAME flag constants
Rename the `PADNAMEt_*` constants to `PADNAMEf_*`, as they're supposed
to represent bitflags, not a type enumeration.

Also updated the `B` and `B::Deparse` modules to make use of the new modern
names (and avoid the old `SVpad_*` flags).

Also added `PADNAMEt_*` back-compat defines, guarded by `#ifndef PERL_CORE`
so as not to permit their use accidentally within perl core.
2022-07-05 13:57:12 +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
789738ed9c Remove pod for non-existent functions
7008caa915ad99e650acf2aea40612b5e48b7ba2 removed several deprecated
functions, but did not remove all the pods thereof.
2022-06-10 11:02:05 -06:00
Richard Leach
7ea8b04b5a Perl_newSV_type_mortal - new inline function introduced and used
There's no efficient way to create a mortal SV of any type other than
SVt_NULL (via sv_newmortal). The options are either to do:

* SV* sv = sv_newmortal; sv_upgrade(sv, SVt_SOMETYPE);
  but sv_upgrade is needlessly inefficient on new SVs.

* SV* sv = sv_2mortal(newSV_type(SVt_SOMETYPE)
  but this will perform runtime checks to see if (sv) and if (SvIMMORTAL(sv),
  and for a new SV we know that those answers will always be yes and no.

This commit adds a new inline function which is basically a mortalizing
wrapper around the now-inlined newSV_type.
2022-03-07 01:08:53 +01: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
Yves Orton
277d63d68e pad.c: Fix GH Issue #19463, -DXv fails assert when dumping anonymous constant sub
Anonymous constant subs were changed to be implemented internally as
XSUBs in 5.21.6, commit 1567c65ac069266bfe65959430c185babd476538.
This broke DEBUGGING perls running under -DXv which weren't taught
about the new implementation. In ed958fa3156084f3cf4d8c4768716d9e1a11ce91
strict.pm also was changed to use such subs, which then breaks many
uses of -DXv.

See t/run/switchDx.t for an example of code that would trigger this that
does not depend on strict.pm

This fixes the problem and adds a test for -Dx.
2022-03-02 08:46:28 -07:00
Paul "LeoNerd" Evans
6a2e756f86 Add a builtin:: namespace, with true/false/isbool
This finishes the perl-visible API required for RFC 0008
  https://github.com/Perl/RFCs/blob/master/rfcs/rfc0008.md

It also begins the "builtin::" namespace of RFC 0009
  https://github.com/Perl/RFCs/blob/master/rfcs/rfc0009.md
2021-11-29 10:35:46 +00:00
Paul "LeoNerd" Evans
58541fd098 Expose a public SAVESTRLEN() macro
This seems to have been forgotten in the public API, perhaps because it
was added later.

Fixes #19165
2021-10-05 12:34:30 +01:00