summaryrefslogtreecommitdiff
path: root/gv.c
Commit message (Collapse)AuthorAgeFilesLines
* [perl #123103] Just set SVf_READONLY on magic varsFather Chrysostomos2014-11-031-0/+10
| | | | | | a623f8939 was arguably a little too eager. It’s purpose is to protect vars whose modification can causes hangs and crashes. I don’t believe that is the case for any magic vars.
* [perl #57512] Warnings for implicitly closed handlesFather Chrysostomos2014-11-021-0/+10
| | | | | | | | | | | | | | | | | | If the implicit close() fails, warn about it, mentioning $! in the message. This is a default warning in the io category. We do this in two spots, sv_clear and gp_free. While sv_clear would be sufficient to get the warning emitted, the warning won’t contain the name of the handle when called from there, because lone IO thing- ies are nameless. Doing it also when a GV’s glob pointer is freed--as long as the IO thingy in there has a reference count of 1--allows the name to be included in the message, because we still have the glob, which is where the name is stored. The result: $ ./miniperl -Ilib -e 'open fh, ">/Volumes/Disk Image/foo"; print fh "x"x1000, "\n" for 1..50; undef *fh' Warning: unable to close filehandle fh properly: No space left on device at -e line 1.
* free up CvPADLIST slot for XSUBs for future useDaniel Dragan2014-10-311-0/+1
| | | | | | | | | | | | | | | | | | | CvRESERVED is a placeholder, it will be replaced with a sentinal value from future revised BOOTCHECK API. CvPADLIST_set was helpful during development of this patch, so keep it around for now. PoisonPADLIST's magic value is from PERL_POISON 0xEF pattern. Some PoisonPADLIST locations will get code from future BOOTCHECK API. Make padlist_dup a NN function to avoid overhead of calling it for XSUBs during closing. Perl_cv_undef_flags's else if (CvISXSUB(&cvbody)) is to avoid whitespace changes. Filed as perl [#123059].
* [perl #122799] Always turn off CvNAMED in cvgv-setFather Chrysostomos2014-09-201-1/+1
| | | | | | | | | | | | Instead of turning off the flag only when we need to turn it off (when there is a hek, which is the only time it should be on), just turn it off unconditionally. This gets Scope::Upper working once more. While it is arguably the module’s fault, it’s still a good idea to make cvgv_set robust. CvNAMED should never be on after calling it, regardless of the previous state of the CV.
* gv.c: For ‘Global symbol’ msg, don’t check utf8ness twiceFather Chrysostomos2014-09-181-6/+3
| | | | | | We already take care of possible utf8ness in the format string. There is no need to turn on the flag afterwards. This double utf8 check has been present since ecad31f018.
* [perl #121638] Make ‘Global symbol’ message more newbie-friendlyFather Chrysostomos2014-09-181-1/+6
| | | | | | by suggesting the declaration of a lexical variable, but without removing the ‘Global symbol’ part, since much code no doubt depends on its presence.
* Avoid reifing GVs in gv.c:gv_try_downgradeFather Chrysostomos2014-09-151-1/+1
| | | | | I’m not sure that it’s even possible for a CV with a name hek to reach this code path, but if that happens this avoids reifying the GV.
* Avoid reifying GVs when lex subs are used for overloadFather Chrysostomos2014-09-151-6/+8
| | | | Takes more code, but I think the new code is clearer....
* Avoid reifying GV when lex stub is used as methodFather Chrysostomos2014-09-151-1/+1
|
* Avoid creating GVs when subs are declaredFather Chrysostomos2014-09-151-5/+26
| | | | | | | | | | | | | | | | | | | | | | | | | | | This patch changes ‘sub foo {...}’ declarations to store subroutine references in the stash, to save memory. Typeglobs still notionally exist. Accessing CvGV(cv) will reify them. Hence, currently the savings are lost when a sub call is compiled. $ ./miniperl -e 'sub foo{} BEGIN { warn $::{foo} } foo(); BEGIN { warn $::{foo} }' CODE(0x7f8ef082ad98) at -e line 1. *main::foo at -e line 1. This optimisation is skipped if the subroutine declaration contains a package separator. Concerning the changes in caller.t, this code: sub foo { print +(caller(0))[3],"\n" } my $fooref = delete $::{foo}; $fooref -> (); used to crash in 5.7.3 or thereabouts. It was fixed by 16658 (aka 07b8c804e8) to produce ‘(unknown)’ instead. Then in 5.13.3 it was changed (by 803f274) to produce ‘main::__ANON__’ instead. So the tests are really checking that we don’t get a crash. I think it is acceptable that it has now changed to ‘main::foo’.
* For lexical subs, reify CvGV from CvSTASH and CvNAME_HEKFather Chrysostomos2014-09-151-1/+24
| | | | | From now on, the presence of a name hek implies a GV. Any access to CvGV will cause that implicit GV to be reified.
* Turn on CVf_LEXICAL for lexical subsFather Chrysostomos2014-09-151-0/+1
| | | | | This flag will signify that lexical subs should not have package names associated with them in error messages, etc.
* introduce gv_stashsvpvn_cached()syber2014-09-021-9/+50
| | | | | | | | | | | | | Wrap gv_stashpvn_internal() with a routine which caches what it does, and rework gv_stashsv() and gv_stashpvn() to use the cached codepath. Also rework the documentation of gv_stashsv() and gv_stashpvn() that the gv_stashsv() is prefered as there is a mechanism to cache the hash value associated with the name which requires an SV to passed in as an argument for caching purposes. Note this is a reworked version of sybers original patch.
* Rename S_stashpvn to S_gv_stashpvn_internal and add to embed.fncYves Orton2014-09-021-4/+13
| | | | | | | S_stashpvn was not added to embed.fnc properly, and is named contrary to general expectations of the Perl internals. This fixes that, there should be no other functional differences.
* Fix crash in leave_scope when my sub has CvGVFather Chrysostomos2014-08-281-1/+4
| | | | | | | Sub declaration can reuse an existing stub. So it is possible to define a package sub using a stub that was originally lexical. Hence, leave_scope needs to take into account that a my-sub may not have a name hek any more.
* Fix crash when lex subs are used for overloadFather Chrysostomos2014-08-281-1/+1
|
* Fix crash when lex subs are used for AUTOLOADFather Chrysostomos2014-08-281-1/+1
|
* Make S_method_common use gv_stashpvn instead of copypasted cache usagesyber2014-08-201-0/+1
| | | | | | The previous commit copied the PL_stashcache handling code from S_method_common() to gv_stashpvn(), so now we can call that function directly rather than doing it ourselves.
* Make gv_stashpvn() use PL_stashcachesyber2014-08-201-2/+21
| | | | | | | | | | | | | perl has a stash name lookup cache, which is currently just used for looking up up class names in Some::Class->method calls. This means that a lookup of the class name 'A::B::C' gets reduced from doing several stash lookups (e.g. $::{'A::'}{'B::'}{'C::'}) to a single cache lookup (e.g. $PL_stashcache{'A::B::C::'}, so to speak). Make gv_stashpvn() use this cache too, which means that all package name lookups benefit, not just class method calls. Among other things, this also indirectly affects bless operations both from Perl (OP_BLESS) and XS.
* Use grok_atou instead of strtoul (no explicit strtol uses).Jarkko Hietaniemi2014-07-221-1/+1
|
* Remove or downgrade unnecessary dVAR.Jarkko Hietaniemi2014-06-251-15/+0
| | | | | | | | You need to configure with g++ *and* -Accflags=-DPERL_GLOBAL_STRUCT or -Accflags=-DPERL_GLOBAL_STRUCT_PRIVATE to see any difference. (g++ does not do the "post-annotation" form of "unused".) The version code has some of these issues, reported upstream.
* Unused contexts found under PERL_GLOBAL_STRUCT.Jarkko Hietaniemi2014-06-241-0/+1
|
* Some low-hanging -Wunreachable-code fruits.Jarkko Hietaniemi2014-06-151-4/+3
| | | | | | | | | | | | | | | | | | | | | | | | | | - after return/croak/die/exit, return/break are pointless (break is not a terminator/separator, it's a goto) - after goto, another goto (!) is pointless - in some cases (usually function ends) introduce explicit NOT_REACHED to make the noreturn nature clearer (do not do this everywhere, though, since that would mean adding NOT_REACHED after every croak) - for the added NOT_REACHED also add /* NOTREACHED */ since NOT_REACHED is for gcc (and VC), while the comment is for linters - declaring variables in switch blocks is just too fragile: it kind of works for narrowing the scope (which is nice), but breaks the moment there are initializations for the variables (the initializations will be skipped since the flow will bypass the start of the block); in some easy cases simply hoist the declarations out of the block and move them earlier Note 1: Since after this patch the core is not yet -Wunreachable-code clean, not enabling that via cflags.SH, one needs to -Accflags=... it. Note 2: At least with the older gcc 4.4.7 there are far too many "unreachable code" warnings, which seem to go away with gcc 4.8, maybe better flow control analysis. Therefore, the warning should eventually be enabled only for modernish gccs (what about clang and Intel cc?)
* Revert "Some low-hanging -Wunreachable-code fruits."Jarkko Hietaniemi2014-06-131-3/+4
| | | | | | | This reverts commit 8c2b19724d117cecfa186d044abdbf766372c679. I don't understand - smoke-me came back happy with three separate reports... oh well, some other time.
* Some low-hanging -Wunreachable-code fruits.Jarkko Hietaniemi2014-06-131-4/+3
| | | | | | | | | | | | | | | | | | - after croak/die/exit (or return), break (or return!) are pointless (break is not a terminator/separator, it's a promise of a jump) - after goto, another goto (!) is pointless - in some cases (usually function ends) introduce explicit NOT_REACHED to make the noreturn nature clearer (do not do this everywhere, though, since that would mean adding NOT_REACHED after every croak) - for the added NOT_REACHED also add /* NOTREACHED */ since NOT_REACHED is for gcc (and VC), while the comment is for linters - declaring variables in switch blocks is just too fragile: it kind of works for narrowing the scope (which is nice), but breaks the moment there are initializations for the variables (they will be skipped!); in some easy cases simply hoist the declarations out of the block and move them earlier There are still a few places left.
* Adding missing HEKfARG() invocationsBrian Fraser2014-06-131-1/+1
| | | | This silences a chunk of warnings under -Wformat
* perlapi: Include general informationKarl Williamson2014-06-051-1/+0
| | | | | | | | | | | Unlike other pod handling routines, autodoc requires the line following an =head1 to be non-empty for its text to be included in the paragraph started by the heading. If you fail to do this, silently the text will be omitted from perlapi. I went through the source code, and where it was apparent that the text was supposed to be in perlapi, deleted the empty line so it would be, with some revisions to make more sense. I added =cuts where I thought it best for the text to not be included.
* Unify the "fall-through" lint annotation.Jarkko Hietaniemi2014-05-291-2/+2
| | | | | | | Used by linters (static checkers), and also good for human readers. Even though "FALL THROUGH" seems to be the most common, e.g BSD lint manual only knows "FALLTHROUGH" (or "FALLTHRU").
* Annotate intentional case fallthrough, or add breaks.Jarkko Hietaniemi2014-05-281-0/+1
| | | | | | | | | | | | | | Fix suspicious (*) switch cases found by Coverity by annotating with either "/* FALLTHROUGH */" or adding a break; The FALLTHROUGH was much more common, but the break turned out to be the right choice in three spots. All changes tested to work. (*) suspicious = case1 + code + case2, and neither flow control (like break) nor fallthrough annotation between code and case2. Fix for Coverity perl5 CIDs 28977..28989, 45353.
* make core safe against HvAUX() reallocDavid Mitchell2014-03-071-7/+3
| | | | | | | | | | | | | | | | | Since the HvAUX structure is just tacked onto the end of the HvARRAY() struct, code like this can do bad things: aux = HvAUX(); ... something that might split hv ... aux->foo = ...; /* SEGV! */ So I've visually audited core for places where HbAUX() is saved and then re-used, and re-initialised the var if it looks like HvARRAY() could have changed in the meantime. I've been very conservative about what might be unsafe. For example, destructors or __WARN__ handlers could call perl code that modifies the hash.
* [perl #121362] overload optimisation added a SEGVDavid Mitchell2014-03-041-2/+4
| | | | | | | | | | | My recent commit 3d147ac29d12abdb to "speed up (non)overloaded derefs" introduced a potential SEGV. In Perl_Gv_AMupdate(), the 'aux' variable is set to HvAUX(hv). My patch used the value of the variable later on in the function, but it turns out that by then, S_hsplit() may have been called, and thus HvARRAY (and HvAUX()) may have been reallocated. Issue first spotted by Andreas' awesome BBC service, and diagnosed by Nicholas Clark.
* speed up (non)overloaded derefsDavid Mitchell2014-02-281-2/+37
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | Consider a class that has some minimal overloading added - e.g. to give pretty stringification of objects - but which *doesn't* overload dereference methods such as '@[]'. '%[]' etc. In this case, simple dereferencing, such as $obj->[0] or $obj->{foo} becomes much slower than if the object was blessed into a non-overloaded class. This is because every time a dereferencing is performed in pp_rv2av for example, the "normal" code path has to go through the full checking of: * is the stash into which the referent is blessed overloaded? If so, * retrieve the overload magic from the stash; * check whether the overload method cache has been invalidated and if so rebuild it; * check whether we are in the scope of 'no overloading', and if so is the current method disabled in this scope? * Is there a '@{}' or whatever (or 'nomethod') method in the cache? If not, then process the ref as normal. That's a lot of extra overhead to decide that an overloaded method doesn't in fact need to be called. This commit adds a new flag to the newish xhv_aux_flags field, HvAUXf_NO_DEREF, which signals that the overloading of this stash contains no deref (nor 'nomethod') overloaded methods. Thus a quick check for this flag in the common case allows us to short-circuit all the above checks except the first one. Before this commit, a simple $obj->[0] was about 40-50% slower if the class it was blessed into was overloaded (but didn't have deref methods); after the commit, the slowdown is 0-10%. (These timings are very approximate, given the vagaries of nano benchmarks.)
* Document what the gv_check() function doesDavid Mitchell2014-02-281-0/+5
|
* gv_check(): use aux flag rather than IsCOWDavid Mitchell2014-02-281-5/+12
| | | | | | | | | | Currently the SVf_IsCOW flag doesn't have any meaning for HVs, except that it is used in the specific case of gv_check() to temporarily mark a stash as being scanned. Since stashes will have the HV_AUX fields, we can use a flags bit in the new xhv_aux_flags field instead. This then potentially frees up the SVf_IsCOW for use as a new general flag bit for *all* HVs (including non-stash ones).
* Silence some VC++ compiler warningsSteve Hay2014-02-251-3/+3
| | | | | | | | | | gv.c(1455) : warning C4146: unary minus operator applied to unsigned type, result still unsigned gv.c(1586) : warning C4146: unary minus operator applied to unsigned type, result still unsigned gv.c(2122) : warning C4146: unary minus operator applied to unsigned type, result still unsigned toke.c(12423) : warning C4244: '=' : conversion from 'I32' to 'char', possible loss of data win32.c(3017) : warning C4022: 'SetHandleInformation' : pointer mismatch for actual parameter 1 win32.c(2971) : warning C4101: 'old_h' : unreferenced local variable win32.c(2967) : warning C4101: 'oldfd' : unreferenced local variable
* [perl #115736] fix undocumented param from newATTRSUB_flagsDaniel Dragan2013-12-231-2/+2
| | | | | flags param was poorly designed and didn't have a formal api. Replace it with the bool it really is. See #115736 for details.
* [perl #120694] Fix ->SUPER::foo and AUTOLOADFather Chrysostomos2013-12-041-1/+2
| | | | | | | | | | | | | Commit aae438050a20 (5.17.4) broke ->SUPER::foo with AUTOLOAD by look- ing up AUTOLOAD from the current package, rather than the current package’s superclass. Instead of keeping track of whether it was doing a SUPER lookup via a ::SUPER prefix on the package name, that commit changed method lookup to pass a GV_SUPER flag around (to fix another bug) and to pass the current stash, rather than __PACKAGE__::SUPER. But it did not update gv_autoload_pvn to pass that flag through to gv_fetchmeth_pvn when actually looking up the method.
* Perl_load_module() no longer moves the current stack, so no need to save it.Nicholas Clark2013-11-221-2/+3
|
* Remove redundant SPAGAIN & PUTBACK after PUSHSTACKi().Nicholas Clark2013-11-221-1/+0
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | PUSHSTACKi() calls SWITCHSTACK(), which sets PL_stack_sp and sp like this: sp = PL_stack_sp = PL_stack_base + AvFILLp(t) Hence after PUSHSTACKi() both are identical, so use of SPAGAIN or PUTBACK to assign one to the other is redundant. The use of SPAGAIN in encoding.xs and via.xs was added with commit 24f59afc531955e5 (April 2002) which added the use of PUSHSTACKi(). It feels like cargo-cult. The use of PUTBACK in Perl_amagic_call() predates the introduction of nested stacks and PUSHSTACKi() in commit e336de0d01f30cc4 (April 1998). It dates from perl 5.000, but it's not clear that it was ever needed, as the code in question looked like this, and nothing could have moved the stack between the dSP and PUTBACK: dSP; BINOP myop; SV* res; Zero(&myop, 1, BINOP); myop.op_last = (OP *) &myop; myop.op_next = Nullop; myop.op_flags = OPf_KNOW|OPf_STACKED; ENTER; SAVESPTR(op); op = (OP *) &myop; PUTBACK; The PUTBACK and SPAGAIN in Perl_require_pv() were added by commit d3acc0f7e5197310 (June 1998) which also added the PUSHSTACKi(). They have both been redundant since they were added.
* gv.c: Remove redundant mro_method_changed_in()Father Chrysostomos2013-11-161-1/+0
| | | | | | Core subs are already accounted for by method lookup, so vivifying them does not change what gets inherited. Hence, there is no need to flush caches here.
* Fix bad read in gp_free introduced by 4571f4a72Father Chrysostomos2013-11-141-1/+6
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | 4571f4a72 attempted to fix this: $ ./perl -Ilib -e 'sub foo{} bless \&foo; DESTROY { undef *foo } undef *foo' Attempt to free unreferenced glob pointers, Perl interpreter: 0x7fd6a3803200 at -e line 1. by lowering the refcount of the gp only after freeing the contents. But what was missing was a check at the end to make sure the refcount was not already 0. In fact, that can’t work either, as, if the refcount has gone down to 0 while freeing the contents of the gp, we will be reading freed memory. In an attempt to implement what my half-baked idea was meant to do, I thought of incrementing the reference count before freeing the entries, and then checking it afterwards, but that would cause an ‘undef *foo’ during that to null the gp slot of the gv and replace it with a new one. In the mean time, gp_free is only freeing the old gp and the new gp will leak when it sets GvGP to 0. I thought of detaching the gp from the gv completely before freeing its entries, but most code doesn’t expect to see typeglobs with no gp. GvSV will crash. I nearly concluded that the only approach I could use was to revert 4571f4a72 and simply extirpate that warning. As far as I can tell, it only comes up with recursive gp_free calls. However, in those cases, simply returning will cause a memory leak, as pp_undef will give the gv a new gp, and the outer gp_free call, when it finishes, will set the gv’s gp slot to null, overwriting what pp_undef put there (a vari- ation of the leak above). So, the final solution is for gp_free to re-read the GvGP value each time it loops, freeing entries. The reference count will continue to be decremented at the end of the function, as in 4571f4a72. That will prevent any bad reads. If pp_undef has reallocated the gp in the mean time, we will simply start using the new one. We need an extra refer- ence count check at the end, in case a destructor does glob assignment and causes the gp to be shared. Ideally this should fix the recent build errors. (See <20131113045538.GA18816@mars.tony.develop-help.com>.) I tried it under valgrind on dromedary both before and after the patch, and the bad reads went away.
* Undeffing a gv in DESTROY triggered by undeffing the same gvFather Chrysostomos2013-11-111-1/+3
| | | | | | | | $ ./perl -Ilib -e 'sub foo{} bless \&foo; DESTROY { undef *foo } undef *foo' Attempt to free unreferenced glob pointers, Perl interpreter: 0x7fd6a3803200 at -e line 1. Lowering the reference count on the glob pointer only after freeing the contents fixes this.
* Stop lexical CORE sub from interfering with CORE::Father Chrysostomos2013-11-081-1/+1
| | | | | | | | | | | | | | | | | | | | The way CORE:: was handled in the lexer was convoluted. CORE was treated initially as a keyword, with exceptions in the lexer to make it behave correctly. If it turned out not to be followed by ::, then the lexer would fall back to treating it as a bareword or sub name. Before even checking for a keyword, the lexer looks for :: and goes to the bareword/sub code. But it made a special exception there for CORE::. In the end, treating CORE as a keyword recognized by the keyword() function requires more special cases than simply special-casing CORE:: in toke.c. This fixes the lexical CORE sub bug, while reducing the total num- ber of lines.
* gv.c:gv_try_downgrade: Use hv_fetchhekFather Chrysostomos2013-11-061-2/+1
| | | | | Unlike hv_fetch, this also passes the precomputed hash, saving hv_common from having to recalculate it.
* Put common override code into gv_overrideFather Chrysostomos2013-11-061-0/+17
| | | | | | | | | | | | When I moved the three occurrences of this code in op.c into a static function, I did not realise at the time that it also occurred thre etimes in toke.c. So now it is in a new non-static function in gv.c. Only two of the instances in toke.c could be changed to use this func- tion, as the otherwise is a little different. I couldn’t see a simple way of factoring its requirements in.
* Stop gv_try_downgrade from anonymising referenced CVsFather Chrysostomos2013-11-051-1/+1
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | I keep discovering ways in which gv_try_downgrade, which is supposed to be an optimisation, changes observable behaviour even without look- ing at the stash. This one had me confused at first: $ ./perl -Ilib -e 'use constant foo=>1; BEGIN { $x = \&foo } undef &$x; $x->()' Undefined subroutine called at -e line 1. $ ./perl -Ilib -e 'use constant foo=>1; BEGIN { $x = \&{"foo"} } undef &$x; $x->()' Undefined subroutine &main::foo called at -e line 1. Notice how the first example (where gv_try_downgrade kicks in) shows no name in the error message. This only happens on non- threaded builds. What’s happening is that, when the BEGIN block is freed, the GV op corresponding to &foo get freed, triggering gv_try_downgrade, which checks to see whether it can downgrade the GV to a simple reference to a constant (the way constants are stored by default). It then pro- ceeds to do that, so the GV qua GV ceases to exist, and the CV gets automatically anonymised as a result (the same thing happens with ‘$x = \&{"foo"}; Dump $x; delete $::{foo}’, but legitimately in that case). The solution here is to check the reference count on the CV before downgrading the GV. If the CV’s reference count > 1, then we should leave it alone.
* gv.c: Removed redundant len==1 checkFather Chrysostomos2013-11-051-1/+1
| | | | | | | When I added this in ea238638, I put the same code in the branches for the main stash and other stashes. In the main stash branch, this occurs in a switch that is solely for one-character names, so checking the length again is superfluous.
* [perl #120462] Exempt $a and $b from ‘used once’ warningsFather Chrysostomos2013-11-041-2/+11
|
* gv.c: Tweak API docsFather Chrysostomos2013-11-041-7/+7
| | | | | | | Consistent spaces after dots Some grammar corrections (by no means sufficient, though) Remove a sentence about ‘name’ being writable. It has been a const char * since these docs were added in 954c1994.
* Undefined lex sub used as inherited method crashesFather Chrysostomos2013-11-021-1/+1
| | | | | | | | | | Thanks to misuse of CvGV. $ perl5.19.5 -XMfeature=:all -e 'my sub foo {} undef &foo; *UNIVERSAL::bar = \&foo; main->bar()' Segmentation fault: 11 When deciding under which name to autoload the sub, we can’t assume CvGV(that_sub) is a GV, as it may be null.