summaryrefslogtreecommitdiff
path: root/scope.c
Commit message (Collapse)AuthorAgeFilesLines
* fix NO_TAINT_SUPPORT on g++David Mitchell2012-11-051-1/+1
| | | | A '&' got lost in the conversion
* Add C define to remove taint support from perlSteffen Mueller2012-11-051-4/+5
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | By defining NO_TAINT_SUPPORT, all the various checks that perl does for tainting become no-ops. It's not an entirely complete change: it doesn't attempt to remove the taint-related interpreter variables, but instead virtually eliminates access to it. Why, you ask? Because it appears to speed up perl's run-time significantly by avoiding various "are we running under taint" checks and the like. This change is not in a state to go into blead yet. The actual way I implemented it might raise some (valid) objections. Basically, I replaced all uses of the global taint variables (but not PL_taint_warn!) with an extra layer of get/set macros (TAINT_get/TAINTING_get). Furthermore, the change is not complete: - PL_taint_warn would likely deserve the same treatment. - Obviously, tests fail. We have tests for -t/-T - Right now, I added a Perl warn() on startup when -t/-T are detected but the perl was not compiled support it. It might be argued that it should be silently ignored! Needs some thinking. - Code quality concerns - needs review. - Configure support required. - Needs thinking: How does this tie in with CPAN XS modules that use PL_taint and friends? It's easy to backport the new macros via PPPort, but that doesn't magically change all code out there. Might be harmless, though, because whenever you're running under NO_TAINT_SUPPORT, any check of PL_taint/etc is going to come up false. Thus, the only CPAN code that SHOULD be adversely affected is code that changes taint state.
* Revert "Set PL_comppad_name on sub entry"Father Chrysostomos2012-09-171-1/+0
| | | | This reverts commit d2c8bf052f5a8bb99050f6d2418d77151eb4b468.
* Make cx_dump() display the correct gimme descriptionVincent Pit2012-09-161-1/+16
| | | | Contexts are no longer what they used to be back in 1996.
* Fix perl with -DPERL_POISON after commit 22ade07Vincent Pit2012-09-161-5/+2
| | | | | The third argument to PoisonNew is the type, not the size. Also, poisoning the contents of the sv itself is incorrect.
* Save one NULL assignment per TMPSteffen Mueller2012-09-161-2/+7
| | | | | This assignment looks really rather like overzealous cleanliness. It's a hot path. Now it's death by 999 cuts instead of 1000.
* Move my sub prototype CVs to the pad namesFather Chrysostomos2012-09-151-14/+0
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | my subs are cloned on scope entry. To make closures work, a stub stored in the pad (and closed over elsewhere) is cloned into. But we need somewhere to store the prototype from which the clone is made. I was attaching the prototype via magic to the stub in the pad, since the pad is available at run time, but not the pad names. That leads to lots of little games all over the place to make sure the prototype isn’t lost when the pad is swiped on scope exit (SAVEt_CLEARSV in scope.c). We also run the risk of losing it if an XS module replaces the sub with another. Instead, we should be storing it with the pad name. The previous com- mit made the pad names available at run time, so we can move it there (still stuffed inside a magic box) and delete much code. This does mean that newMYSUB cannot rely on the behaviour of non-clon- able subs that close over variables (or subs) immediately. Previ- ously, we would dig through outer scopes to find the stub in cases like this: sub y { my sub foo; sub x { sub { sub foo { ... } } } } We would stop at x, which happens to have y’s stub in its pad, so that’s no problem. If we attach it to the pad name, we definitely have to dig past x to get to the pad name in y’s pad. Usually, immediate closures do not store the parent pad index, since it will never be used. But now we do need to use it, so we modify the code in pad.c:S_pad_findlex to set it always for my/state.
* Set PL_comppad_name on sub entryFather Chrysostomos2012-09-151-0/+1
|
* CvNAME_HEK_setFather Chrysostomos2012-09-151-5/+3
|
* Clone my subs on scope entryFather Chrysostomos2012-09-151-2/+41
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | The pad slot for a my sub now holds a stub with a prototype CV attached to it by proto magic. The prototype is cloned on scope entry. The stub in the pad is used when cloning, so any code that references the sub before scope entry will be able to see that stub become defined, making these behave similarly: our $x; BEGIN { $x = \&foo } sub foo { } our $x; my sub foo { } BEGIN { $x = \&foo } Constants are currently not cloned, but that may cause bugs in pad_push. I’ll have to look into that. On scope exit, lexical CVs go through leave_scope’s SAVEt_CLEARSV sec- tion, like lexical variables. If the sub is referenced elsewhere, it is abandoned, and its proto magic is stolen and attached to a new stub stored in the pad. If the sub is not referenced elsewhere, it is undefined via cv_undef. To clone my subs on scope entry, we create a sequence of introcv and clonecv ops. See the huge comment in block_end that explains why we need two separate ops for each CV. To allow my subs to be defined in inner subs (my sub foo; sub { sub foo {} }), pad_add_name_pvn and S_pad_findlex now upgrade the entry for a my sub to a CV to begin with, so that fake entries added to pads (fake entries are those that reference outer pads) can share the same CV. Otherwise newMYSUB would have to add the CV to every pad that closes over the ‘my sub’ declaration. newMYSUB no longer throws away the initial value replacing it with a new one. Prototypes are not currently visible to sub calls at compile time, because the lexer sees the empty stub. A future commit will solve that. When I added name heks to CV’s I made mistakes in a few places, by not turning on the CVf_NAMED flag, or by not clearing the field when free- ing the hek. Those code paths were not exercised enough by state subs, so the problems did not show up till now. So this commit fixes those, too. One of the tests in lexsub.t, involving foreach loops, was incorrect, and has been fixed. Another test has been added to the end for a par- ticular case of state subs closing over my subs that I broke when ini- tially trying to get sibling my subs to close over each other, before I had separate introcv and clonecv ops.
* Omnibus removal of register declarationsKarl Williamson2012-08-181-10/+10
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | This removes most register declarations in C code (and accompanying documentation) in the Perl core. Retained are those in the ext directory, Configure, and those that are associated with assembly language. See: http://stackoverflow.com/questions/314994/whats-a-good-example-of-register-variable-usage-in-c which says, in part: There is no good example of register usage when using modern compilers (read: last 10+ years) because it almost never does any good and can do some bad. When you use register, you are telling the compiler "I know how to optimize my code better than you do" which is almost never the case. One of three things can happen when you use register: The compiler ignores it, this is most likely. In this case the only harm is that you cannot take the address of the variable in the code. The compiler honors your request and as a result the code runs slower. The compiler honors your request and the code runs faster, this is the least likely scenario. Even if one compiler produces better code when you use register, there is no reason to believe another will do the same. If you have some critical code that the compiler is not optimizing well enough your best bet is probably to use assembler for that part anyway but of course do the appropriate profiling to verify the generated code is really a problem first.
* scope.c: Don’t stringify globs on scope exitFather Chrysostomos2012-07-301-1/+2
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | This is a waste: /* Can clear pad variable in place? */ if (SvREFCNT(sv) <= 1 && !SvOBJECT(sv)) { /* * if a my variable that was made readonly is going out of * scope, we want to remove the readonlyness so that it can * go out of scope quietly */ if (SvPADMY(sv) && !SvFAKE(sv)) SvREADONLY_off(sv); if (SvTHINKFIRST(sv)) sv_force_normal_flags(sv, SV_IMMEDIATE_UNREF); We can simply drop the globness in sv_force_normal instead of flatten- ing globs to strings. The same applies to COWs. The SV_COW_DROP_PV flag accomplishes both. Before and after: $ time ./miniperl -e 'for (1..1000000) { my $x = *foo }' real 0m2.324s user 0m2.316s sys 0m0.006s $ time ./miniperl -e 'for (1..1000000) { my $x = *foo }' real 0m0.848s user 0m0.840s sys 0m0.005s
* eliminate PL_reg_start_tmp, PL_reg_start_tmplDavid Mitchell2012-06-131-3/+0
| | | | | | | | | | | | | | | | | | PL_reg_start_tmp is a global array of temporary parentheses start positions. An element is set when a '(' is first encountered, while when a ')' is seen, the per-regex offs array is updated with the start and end position: the end derived from the position where the ')' was encountered, and the start position derived from PL_reg_start_tmp[n]. This allows us to differentiate between pending and fully-processed captures. Change it so that the tmp start value becomes a third field in the offs array (.start_tmp), along with the existing .start and .end fields. This makes the value now per regex rather than global. Although it uses a bit more memory (the start_tmp values aren't needed after the match has completed), it simplifies the code, and will make it easier to make a (??{}) switch to the new regex without having to dump everything on the save stack.
* update the editor hints for spaces, not tabsRicardo Signes2012-05-291-2/+2
| | | | | This updates the editor hints in our files for Emacs and vim to request that tabs be inserted as spaces.
* scope.c: Simplify and clarify commentFather Chrysostomos2012-05-211-15/+2
| | | | | | | | | | | | | | This comment seems to imply that this code is just working around a problem in gv.c, which we could simply correct there. I’ve already tried making the quoted code in gv.c handle *^H without a hash, but it doesn’t actually solve the problem. The real problem is that rv2hv could also add a hash to *^H, via GvHVn, and that is simply not set up to deal with autovivifying magic at all, and probably shouldn’t be. Also, quoting a piece of code that occurs elsewhere is just asking for it to drift apart. By this time, the code in gv.c that is quoted in scope.c is actually three times the length now, and looks completely different.
* [perl #112444] Don’t leak %^H autovivified by destructorFather Chrysostomos2012-04-161-1/+3
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | More precisely, don’t let a hint hash autovivified by a destructor during scope exit leak to outer scopes. GvHV(PL_hintgv) (aka *^H{HASH}) must be set to null before the hash in it is freed on scope exit. Otherwise destructors will see %^H with a refcount of zero, and might try to iterate over a hash that is in the process of being freed. Bad things then happen. Commit 2653c1e3b1 took care of this. Now, if GvHV(PL_hintgv) is null when destructors are called, those destructors might end up autovivifying it. The code in scope.c that handles hints when a scope is left (SAVEt_HINTS in Perl_leave_scope) would then end up leaving that new autovivified %^H in place when the scope exited, if the outer scope did not have HINT_LOCALIZE_HH set (meaning %^H was unused). That in itself would not be so much of a problem, if it were not for the fact that %^H is magicalised by the scope-handling code, not when it is autovivified (see also bug #112458). Hence, subsequent changes to %^H would not magically set the HINT_LOCALIZE_HH hint bit, which bit is checked all over the place to see whether %^H is in use. This, in turn, would cause hints subsequently added to %^H to leak to outer scopes. This commit fixes that by repeatedly freeing GvHV(PL_hintgv). If a destructor autovivifies it again, it just causes another iteration of the while loop. This does mean a destructor could autovivify %^H and cause the new %^H itself to trigger a destructor, resulting in infi- nite loops. But then that is that own code’s fault. This originally came up because commit 2653c1e3b1 also caused des- tructors that try to add new free magic to %^H to add it to a new autovivified %^H instead of the existing %^H that was being freed. This caused the nextgen module to fail its tests, because it uses B::Hooks::EndOfScope to register a sub to be called on scope exit, and it does this from a destructor itself called during scope exit. If the autovivified %^H leaks to an outer scope, the second destructor is not called.
* stop %^H pointing to being-freed hash; #112326David Mitchell2012-04-111-3/+4
| | | | | | | | | | | The leave_scope() action SAVEt_HINTS does the following to GvHV(PL_hintgv): first it SvREFCNT_dec()'s it, then sets it to NULL. If the current %^H contains a destructor, then that will be executed while %^H still points to the hash being freed. This can cause bad things to happen, like iterating over the hash being freed. Instead, setGvHV(PL_hintgv) to NULL first, *then* free the hash.
* Provide as much diagnostic information as possible in "panic: ..." messages.Nicholas Clark2012-01-161-2/+2
| | | | | | | | | | | | | | | The convention is that when the interpreter dies with an internal error, the message starts "panic: ". Historically, many panic messages had been terse fixed strings, which means that the out-of-range values that triggered the panic are lost. Now we try to report these values, as such panics may not be repeatable, and the original error message may be the only diagnostic we get when we try to find the cause. We can't report diagnostics when the panic message is generated by something other than croak(), as we don't have *printf-style format strings. Don't attempt to report values in panics related to *printf buffer overflows, as attempting to format the values to strings may repeat or compound the original error.
* Don’t double-free hint hash if copying diesFather Chrysostomos2011-12-231-2/+4
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | In this horrendous piece of code, the attempt to clone GvHV(PL_hintgv) in save_hints dies because the NEXTKEY method cannot be found. But that happens while GvHV(PL_hintgv) still points to the old value. So the old hash gets freed in the new scope (when it unwinds due to the error in trying to find NEXTKEY) and then gets freed in the outer scope, too, resulting in the dreaded ‘Attempt to free unrefer- enced scalar’. package namespace::clean::_TieHintHash; sub TIEHASH { bless[] } sub STORE { $_[0][0]{$_[1]} = $_[2] } sub FETCH { $_[0][0]{$_[1]} } sub FIRSTKEY { my $a = scalar keys %{$_[0][0]}; each %{$_[0][0]} } # Intentionally commented out: # sub NEXTKEY { each %{$_[0][0]} } package main; BEGIN { $^H{foo} = "bar"; # activate localisation magic tie( %^H, 'namespace::clean::_TieHintHash' ); # sabotage %^H $^H{foo} = "bar"; # create an element in the tied hash } { ; } # clone the tied hint hash The solution is to set GvHV(PL_hintgv) to NULL when copying it.
* scope.c: Allow successful saving of PL_taintedKarl Williamson2011-12-151-1/+10
| | | | | | | | leave_scope() saves and restores PL_tainted upon entry and exit. This means that any attempt to save this variable on the stack will fail, as its unstacked value will overwrite the popped one. To counteract this, we update our saved version with the popped value.
* Copy magic during localisation even for GVsFather Chrysostomos2011-11-181-1/+1
| | | | | The magic-copying is skipped for GVs. This logic goes back to perl 5.000 (patch a0d0e21e). I think it has always been wrong.
* Hide pad vars from magic methods on scope exitFather Chrysostomos2011-11-051-1/+3
| | | | | | | If, during scope exit, a pad var is being cleared for reuse, it needs to be hidden from magic methods that might reference it through weak references. Otherwise they can end up modifying the var that will be seen next time that scope is entered, by blessing it, etc.
* Weak refs to pad hvs should go staleFather Chrysostomos2011-11-051-0/+1
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | When a lexical variable goes out of scope, as in { my %lexical_variable; ... } # no longer in scope here it is supposed to disappear as far as Perl code can tell. That the same SV is reused the next time that scope is entered is an implement- ation detail. The move of hashes’ back-references from magic into the HvAUX struc- ture in 5.10 caused this implementation detail to leak through. Normally, weak references to pad variables going out of scope are killed off: { my $scalar; weaken ($global_scalar = \$scalar); } # here $global_scalar is undef When hashes’ back-references were moved, leave_scope was not updated to account. (For non-hash variables, it’s the mg_free call that takes care of it.) So in this case: { my %hash; weaken ($global_scalar = \%hash); } $global_scalar would still reference a hash, but one marked PADSTALE. Modifications to that hash through the reference would be visible the next time the scope was entered.
* make SVs_PADTMP and SVs_PADSTALE share a bitDavid Mitchell2011-10-071-2/+3
| | | | | | | | | | | SVs_PADSTALE is only meaningful with SVs_PADMY, while SVs_PADTMP is only meaningful with !SVs_PADMY, so let them share the same flag bit. Note that this doesn't yet free a bit in SvFLAGS, as the two bits are also used for SVpad_STATE, SVpad_TYPED. (This is is follow-on to 62bb6514085e5eddc42b4fdaf3713ccdb7f1da85.)
* remove index offsetting ($[)Zefram2011-09-091-5/+0
| | | | | | $[ remains as a variable. It no longer has compile-time magic. At runtime, it always reads as zero, accepts a write of zero, but dies on writing any other value.
* GVs of localised arrays and hashes should be refcountedFather Chrysostomos2011-08-261-2/+6
| | | | | | | | | | Otherwise the GV can be freed before the scope-popping code can put the old entry back in it: $ perl -le 'local @{"x"}; delete $::{x}' Bus error $ perl -le 'local %{"x"}; delete $::{x}' Bus error
* SvREFCNT_dec already checks if the SV is non-NULLVincent Pit2011-08-021-2/+1
|
* add GvCV_set() and GvGP_set() macros.David Mitchell2011-01-211-2/+2
| | | | | | | | and make GvCV() and GvGP() rvalue-only. This it to allow a future commit to eliminate some backref magic between GV and CVs, which will require complete control over assignment to the gp_cv slot.
* Silence some data truncation compiler warningsJan Dubois2010-12-161-1/+1
|
* Revert "Globs that are in the symbol table can be unglobbed"Father Chrysostomos2010-11-201-12/+3
| | | | | | | This reverts b9e00b79 except for the tests. This extra checking and saving of the FAKE flag is no longer necessary as of the previous commit.
* Switch the core MRO code over to HvENAMEFather Chrysostomos2010-10-291-1/+1
| | | | | | | | | | | | | | | | | | | | | | This has the side-effect of fixing these one-liners: $ perl5.13.5 -le' my $glob = \*foo::ISA; delete $::{"foo::"}; *$glob = *a' Bus error $ perl5.13.5 -le' my $glob = \*foo::ISA; delete $::{"foo::"}; *$glob = []' Bus error $ perl5.13.6 -le'sub baz; my $glob = \*foo::bar; delete $::{"foo::"}; *$glob = *baz;' Bus error $ perl5.13.6 -le'sub foo::bar; my $glob = \*foo::bar; delete $::{"foo::"}; *$glob = *baz;' Bus error In the first two cases the crash was inadvertently fixed (isn’t it nice when that happens?) in 5.13.6 (by 6f86b615fa7), but there was still a fatal error: Can't call mro_isa_changed_in() on anonymous symbol table at -e line 1. Because sv_clear calls ->DESTROY, if an object’s stash has been detached from the symbol table, mro_get_linear_isa can be called on a hash with no HvENAME. So HvNAME is used as a fallback for those cases.
* add SAVEFREECOPHH()Zefram2010-10-281-0/+4
| | | | | Add the facility for the save stack to free (decrement the refcount of) a COPHH*.
* full API for cop hint hashesZefram2010-10-211-10/+5
| | | | | | | | | | | | | Expose cop hint hashes as a type COPHH, with a cophh_* API which is a macro layer over the refcounted_he_* API. The documentation for cophh_* describes purely API-visible behaviour, whereas the refcounted_he_* documentation describes the functions mainly in terms of the implementation. Revise the cop_hints_* API, using the flags parameter consistently and reimplementing in terms of cophh_*. Use the cophh_* and cop_hints_* functions consistently where appropriate. [Modified by the committer to update two calls to Perl_refcounted_he_fetch recently added to newPMOP.]
* In S_save_scalar_at, remove oldtainted, unneeded since 0cbee0a449cc4e11.Nicholas Clark2010-10-131-2/+0
| | | | | 0cbee0a449cc4e11 removed the call to mg_get(), and hence any possibility of calling code with the side effect of changing PL_tainted.
* add hv_copy_hints_hv and save_hints to the APIZefram2010-09-161-1/+1
|
* bad things happened with for $x (...) { *x = *y }David Mitchell2010-09-081-0/+6
| | | | | | | | | | | | | | | | | | | | | | | | fix for [perl #21469]: since the GP may be pulled from under us and freed, coredumps and strange things can happen. Fix this by storing a pointer to the GV in the loop block, rather than a pointer to the GvSV slot. The ITHREADS variant already stores GV rather than than &GvSV; extend this to non-threaded builds too. Also, for both threaded and non-threaded, it used to push &GvSV on the save stack. Fix this by introducing a new save type, SAVEt_GVSV. This behaves similarly to SAVEt_SV, but without magic get/set. This means that for $package_var (...) is now close in behaviour to local $package_var = ... (except for the magic bit).
* eliminate next_op from struct block_loopDavid Mitchell2010-09-081-2/+0
| | | | | | This field is only used in non-threaded builds, and the comments imply that this is because in non-threaded builds this value may be modified. But nothing in core modifies it.
* remove misleading comment about CXINC; it's fineChip Salzenberg2010-07-271-1/+1
|
* Revert "Fix off-by-one: avoid allocating an extra context"Chip Salzenberg2010-07-271-2/+2
| | | | | This reverts commit 395b8e2d02eadc9b0639534410c39c530bc8a33d. The fencepost error is coming from inside the programmer!
* Fix off-by-one: avoid allocating an extra contextChip Salzenberg2010-07-271-2/+2
| | | | (patch req by Nicholas)
* When saving ints, if the value is small enough save it with the type.Nicholas Clark2010-05-051-1/+11
| | | | This uses a new type, SAVEt_INT_SMALL.
* When saving I32s, if the value is small enough save it with the type.Nicholas Clark2010-05-051-1/+11
| | | | This uses a new type, SAVEt_I32_SMALL.
* For SAVEt_I16, save the value with the type.Nicholas Clark2010-05-041-2/+4
|
* For SAVEt_I8, save the value with the type.Nicholas Clark2010-05-041-2/+4
|
* For SAVEt_BOOL, save the value with the type.Nicholas Clark2010-05-031-4/+3
|
* For SAVEt_ALLOC, store the number of save stack entries used with the type.Nicholas Clark2010-05-031-8/+9
|
* For SAVEt_REGCONTEXT, store the number of save stack entries used with the type.Nicholas Clark2010-05-021-0/+3
|
* Fix c6bf6a65 - 64 bit big endian builds were broken.Nicholas Clark2010-05-031-1/+1
|
* For SVt_CLEAR, store the pad offset with the type.Nicholas Clark2010-05-021-4/+9
| | | | This saves 1 slot on the save stack for each lexical encountered at run time.
* On the save stack, store the save type as the bottom 6 bits of a UV.Nicholas Clark2010-05-011-13/+15
| | | | This makes the other 26 (or 58) bits available for save data.