Skip to content
New issue

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

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

Already on GitHub? Sign in to your account

Reversing a default $_ lexicalized in an upper block fails #10426

Closed
p5pRT opened this issue Jun 7, 2010 · 19 comments
Closed

Reversing a default $_ lexicalized in an upper block fails #10426

p5pRT opened this issue Jun 7, 2010 · 19 comments

Comments

@p5pRT
Copy link

p5pRT commented Jun 7, 2010

Migrated from rt.perl.org#75598 (status was 'resolved')

Searchable as RT75598$

@p5pRT
Copy link
Author

p5pRT commented Jun 7, 2010

From perl@profvince.com

Created by perl@profvince.com

With the latest blead :

  $ ./perl -Ilib -E '{ my $_ = "foo"; sub z { scalar reverse } } say z'
  Bizarre copy of ARRAY in reverse at -e line 1.

It used to segfault :

  $ perl5.12.1-64 -M5.010 -E '{ my $_ = "foo"; sub z { scalar reverse } } say z'
  Segmentation fault.

At that time, the relevant issue was the one addressed in RT #75436. The crash is fixed, but there's still an underlying problem with change 789bd86.

For some reason Perl_pad_findlex() (thus find_rundefsv() as well) returns an AV instead of $_ in this case :

$ gdb --args ./perl -Ilib -M5.010 -E '{ my $_ = "foo"; sub z { scalar reverse } } say z'
GNU gdb (Gentoo 7.1 p1) 7.1
Copyright (C) 2010 Free Software Foundation, Inc.
License GPLv3+​: GNU GPL version 3 or later <http​://gnu.org/licenses/gpl.html>
This is free software​: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law. Type "show copying"
and "show warranty" for details.
This GDB was configured as "x86_64-pc-linux-gnu".
For bug reporting instructions, please see​:
<http​://bugs.gentoo.org/>...
Reading symbols from /tmp/git/perl...done.
(gdb) break Perl_croak
Breakpoint 1 at 0x524cf3​: file util.c, line 1609.
(gdb) r
Starting program​: /tmp/git/perl -Ilib -M5.010 -E \{\ my\ \$_\ =\ \"foo\"\;\ sub\ z\ \{\ scalar\ reverse\ \}\ \}\ say\ z
warning​: no loadable sections found in added symbol-file /usr/lib64/debug/lib64/ld-2.11.1.so.debug
[Thread debugging using libthread_db enabled]

Breakpoint 1, Perl_croak (my_perl=0xa08010,
  pat=0x7961dd "Bizarre copy of %s in %s") at util.c​:1609
1609 va_start(args, pat);
(gdb) bt
#0 Perl_croak (my_perl=0xa08010, pat=0x7961dd "Bizarre copy of %s in %s")
  at util.c​:1609
#1 0x00000000005a1879 in Perl_sv_setsv_flags (my_perl=0xa08010,
  dstr=0xa38060, sstr=0xa379d0, flags=1538) at sv.c​:3918
#2 0x000000000061f613 in Perl_pp_reverse (my_perl=0xa08010) at pp.c​:5497
#3 0x000000000051cd48 in Perl_runops_debug (my_perl=0xa08010) at dump.c​:2096
#4 0x00000000004557fa in S_run_body (my_perl=0xa08010, oldscope=1)
  at perl.c​:2309
#5 0x0000000000454a0a in perl_run (my_perl=0xa08010) at perl.c​:2233
#6 0x0000000000423aed in main (argc=5, argv=0x7fffffffdf08,
  env=0x7fffffffdf38) at perlmain.c​:117
(gdb) up 2
#2 0x000000000061f613 in Perl_pp_reverse (my_perl=0xa08010) at pp.c​:5497
5497 sv_setsv(TARG, SP > MARK ? *SP : find_rundefsv());
(gdb) do
#1 0x00000000005a1879 in Perl_sv_setsv_flags (my_perl=0xa08010,
  dstr=0xa38060, sstr=0xa379d0, flags=1538) at sv.c​:3918
3918 Perl_croak(aTHX_ "Bizarre copy of %s in %s", type, OP_DESC(PL_op));
(gdb) p sstr
$1 = (SV *) 0xa379d0
(gdb) call Perl_sv_dump(my_perl, sstr)
SV = PVAV(0xa0cff8) at 0xa379d0
  REFCNT = 2
  FLAGS = ()
  ARRAY = 0x0
  FILL = -1
  MAX = -1
  ARYLEN = 0x0
  FLAGS = (REIFY)

Perl Info

Flags:
    category=core
    severity=medium

Site configuration information for perl 5.10.1:

Configured by Gentoo at Sat Feb 13 19:44:25 CET 2010.

Summary of my perl5 (revision 5 version 10 subversion 1) configuration:
   
  Platform:
    osname=linux, osvers=2.6.32.7-fuuka.profvince.com, archname=x86_64-linux
    uname='linux fuuka 2.6.32.7-fuuka.profvince.com #1 smp sat jan 30 18:37:12 cet 2010 x86_64 intel(r) core(tm)2 duo cpu e6750 @ 2.66ghz genuineintel gnulinux '
    config_args='-des -Duseshrplib -Darchname=x86_64-linux -Dcc=x86_64-pc-linux-gnu-gcc -Doptimize=-march=core2 -O3 -fomit-frame-pointer -ftree-vectorize -ftree-vectorizer-verbose=1 -pipe -DNDEBUG -Dscriptdir=/usr/bin -Dprefix=/usr -Dvendorprefix=/usr -Dsiteprefix=/usr -Dprivlib=/usr/lib64/perl5/5.10.1 -Darchlib=/usr/lib64/perl5/5.10.1/x86_64-linux -Dvendorlib=/usr/lib64/perl5/vendor_perl/5.10.1 -Dvendorarch=/usr/lib64/perl5/vendor_perl/5.10.1/x86_64-linux -Dsitelib=/usr/lib64/perl5/site_perl/5.10.1 -Dsitearch=/usr/lib64/perl5/site_perl/5.10.1/x86_64-linux -Dman1dir=/usr/share/man/man1 -Dman3dir=/usr/share/man/man3 -Dinstallman1dir=/usr/share/man/man1 -Dinstallman3dir=/usr/share/man/man3 -Dman1ext=1 -Dman3ext=3pm -Dlibperl=libperl.so.5.10.1 -Dlocincpth=  -Duselargefiles -Dd_semctl_semun -Dinc_version_list=5.10.0 5.10.0/x86_64-linux -Dcf_by=Gentoo -Dmyhostname=localhost -Dperladmin=root@localhost -Dinstallusrbinperl=n -Ud_csh -Uusenm -Di_ndbm -Di_gdbm -Di_db -Dusrinc=/usr/include -Dlibpth=/usr/local/lib64 /lib64 /usr/lib64'
    hint=recommended, useposix=true, d_sigaction=define
    useithreads=undef, usemultiplicity=undef
    useperlio=define, d_sfio=undef, uselargefiles=define, usesocks=undef
    use64bitint=define, use64bitall=define, uselongdouble=undef
    usemymalloc=n, bincompat5005=undef
  Compiler:
    cc='x86_64-pc-linux-gnu-gcc', ccflags ='-fno-strict-aliasing -pipe -fstack-protector -D_LARGEFILE_SOURCE -D_FILE_OFFSET_BITS=64',
    optimize='-march=core2 -O3 -fomit-frame-pointer -ftree-vectorize -ftree-vectorizer-verbose=1 -pipe -DNDEBUG',
    cppflags='-fno-strict-aliasing -pipe -fstack-protector'
    ccversion='', gccversion='4.4.3', gccosandvers=''
    intsize=4, longsize=8, ptrsize=8, doublesize=8, byteorder=12345678
    d_longlong=define, longlongsize=8, d_longdbl=define, longdblsize=16
    ivtype='long', ivsize=8, nvtype='double', nvsize=8, Off_t='off_t', lseeksize=8
    alignbytes=8, prototype=define
  Linker and Libraries:
    ld='x86_64-pc-linux-gnu-gcc', ldflags =' -fstack-protector'
    libpth=/usr/local/lib64 /lib64 /usr/lib64
    libs=-lpthread -lnsl -lgdbm -ldb -ldl -lm -lcrypt -lutil -lc -lgdbm_compat
    perllibs=-lpthread -lnsl -ldl -lm -lcrypt -lutil -lc
    libc=/lib/libc-2.11.so, so=so, useshrplib=true, libperl=libperl.so.5.10.1
    gnulibc_version='2.11'
  Dynamic Linking:
    dlsrc=dl_dlopen.xs, dlext=so, d_dlsymun=undef, ccdlflags='-Wl,-E'
    cccdlflags='-fPIC', lddlflags='-shared -march=core2 -O3 -fomit-frame-pointer -ftree-vectorize -ftree-vectorizer-verbose=1 -pipe -DNDEBUG -fstack-protector'

Locally applied patches:
    


@INC for perl 5.10.1:
    /usr/lib64/perl5/site_perl/5.10.1/x86_64-linux
    /usr/lib64/perl5/site_perl/5.10.1
    /usr/lib64/perl5/site_perl
    /usr/lib64/perl5/vendor_perl/5.10.1/x86_64-linux
    /usr/lib64/perl5/vendor_perl/5.10.1
    /usr/lib64/perl5/vendor_perl
    /usr/lib64/perl5/5.10.1/x86_64-linux
    /usr/lib64/perl5/5.10.1
    .


Environment for perl 5.10.1:
    HOME=/home/vince
    LANG=fr_FR.UTF-8
    LANGUAGE (unset)
    LC_ALL=fr_FR.UTF-8
    LD_LIBRARY_PATH (unset)
    LOGDIR (unset)
    PATH=/home/vince/bin:/home/vince/perl/builds/bin:/usr/local/bin:/usr/bin:/bin:/opt/bin:/usr/x86_64-pc-linux-gnu/gcc-bin/4.4.3:/opt/intel/cce/10.1.018/bin:/usr/games/bin
    PERL_BADLANG (unset)
    SHELL=/bin/bash



@p5pRT
Copy link
Author

p5pRT commented Nov 30, 2012

From @cpansprout

On Mon Jun 07 14​:35​:34 2010, perl@​profvince.com wrote​:

This is a bug report for perl from perl@​profvince.com,
generated with the help of perlbug 1.39 running under perl 5.10.1.

-----------------------------------------------------------------
[Please describe your issue here]

With the latest blead :

$ \./perl \-Ilib \-E '\{ my $\_ = "foo"; sub z \{ scalar reverse \} \} say

z'
Bizarre copy of ARRAY in reverse at -e line 1.

It used to segfault :

$ perl5\.12\.1\-64 \-M5\.010 \-E '\{ my $\_ = "foo"; sub z \{ scalar

reverse } } say z'
Segmentation fault.

At that time, the relevant issue was the one addressed in RT #75436.
The crash is fixed, but there's still an underlying problem with
change 789bd86.

For some reason Perl_pad_findlex() (thus find_rundefsv() as well)
returns an AV instead of $_ in this case :

S_pad_findlex has its own expectations about how its pointer arguments
are used. find_rundefsv breaks those expectations.

One fix would be to make every sub implicitly close over a lexical $_
that is in scope. Then ‘my $_’ would automatically make all functional
code slower.

I still think we should just deprecate lexical $_.

--

Father Chrysostomos

@p5pRT
Copy link
Author

p5pRT commented Nov 30, 2012

The RT System itself - Status changed from 'new' to 'open'

@p5pRT
Copy link
Author

p5pRT commented Nov 30, 2012

From @doy

On Thu, Nov 29, 2012 at 05​:58​:52PM -0800, Father Chrysostomos via RT wrote​:

On Mon Jun 07 14​:35​:34 2010, perl@​profvince.com wrote​:

This is a bug report for perl from perl@​profvince.com,
generated with the help of perlbug 1.39 running under perl 5.10.1.

-----------------------------------------------------------------
[Please describe your issue here]

With the latest blead :

$ \./perl \-Ilib \-E '\{ my $\_ = "foo"; sub z \{ scalar reverse \} \} say

z'
Bizarre copy of ARRAY in reverse at -e line 1.

It used to segfault :

$ perl5\.12\.1\-64 \-M5\.010 \-E '\{ my $\_ = "foo"; sub z \{ scalar

reverse } } say z'
Segmentation fault.

At that time, the relevant issue was the one addressed in RT #75436.
The crash is fixed, but there's still an underlying problem with
change 789bd86.

For some reason Perl_pad_findlex() (thus find_rundefsv() as well)
returns an AV instead of $_ in this case :

S_pad_findlex has its own expectations about how its pointer arguments
are used. find_rundefsv breaks those expectations.

One fix would be to make every sub implicitly close over a lexical $_
that is in scope. Then ‘my $_’ would automatically make all functional
code slower.

I still think we should just deprecate lexical $_.

+1, again.

-doy

@p5pRT
Copy link
Author

p5pRT commented Nov 30, 2012

From @bulk88

On Thu Nov 29 17​:58​:52 2012, sprout wrote​:

I still think we should just deprecate lexical $_.

I like lexical $_. It was added in 5.10. Adding a my is the instinctive
thing to do to stop something from being a global for a *beginner*. I
dont have enough background to comment on the internals side of this.

--
bulk88 ~ bulk88 at hotmail.com

@p5pRT
Copy link
Author

p5pRT commented Nov 30, 2012

From @ap

* bulk88 via RT <perlbug-followup@​perl.org> [2012-11-30 08​:45]​:

I like lexical $_. It was added in 5.10. Adding a my is the
instinctive thing to do to stop something from being a global for
a *beginner*. I dont have enough background to comment on the
internals side of this.

But it’s the *only* punctuation variable for which you can do that. You
cannot say `my $/` and the like.

And it is precisely beginners who are ill equipped to understand why
code using closures suddenly breaks when they add `my $_`, how to fix
that, and why the fix works…

… and in the event that they then refuse to use the solution because it
is such a bleeding eyesore, and they instead opt to avoid the problem in
the first place, then I will not be in a position to begrudge them the
stance​:

So do I.

Regards,
--
Aristotle Pagaltzis // <http​://plasmasturm.org/>

@p5pRT
Copy link
Author

p5pRT commented Dec 1, 2012

From aaron@priven.com

Let's step back a minute.

The point of having $_ is to avoid extra typing. The difference between using $_, and a regular variable like $n or $x, is to allow certain operations to proceed without needing to specify a variable at all. So C<for (…)> is the same as C<for $_ (…)>, C<s/x/y/> is the same as C<$_ =~ s/x/y/>, and so forth.

And, of course, it also works with a number of core functions, such as alarm (where C<alarm()> is the same as C<alarm($_)>, etc.) It varies, by function. So alarm and chomp, among many others, default to $_, but other functions such as kill, localtime, rand, or sleep do not. User functions with the underscore prototype can do this too (although I think, even with changes in 5.16, it still doesn't really work properly​: see http​://perlmonks.org/?node_id=958820 )

If you think about it, it is actually extremely weird that a function can, at its own choosing (so to speak), reach up back into the caller's space and take the value of one of the caller's variables.

And in some sense, that means that lexical $_ isn't really lexical at all -- at least not in the same sense as lexical $x. "my $x" means that $x is completely hidden from all other scopes unless it is explicitly passed, where "my $_" does not. And this using $_ as the default is the entire purpose of $_ existing in the first place (outside map and grep blocks).

So $_ doesn't act like a lexical even if it's declared as one. "my $_" adds a third kind of variable -- global, lexical, and lexical-but-the-calling-function-can-request-its-value. It would be clearer, and more consistent, to have $_ always be global.

On Nov 29, 2012, at 11​:43 PM, bulk88 via RT <perlbug-followup@​perl.org> wrote​:

On Thu Nov 29 17​:58​:52 2012, sprout wrote​:

I still think we should just deprecate lexical $_.

I like lexical $_. It was added in 5.10. Adding a my is the instinctive
thing to do to stop something from being a global for a *beginner*. I
dont have enough background to comment on the internals side of this.

--
bulk88 ~ bulk88 at hotmail.com

---
via perlbug​: queue​: perl5 status​: open
https://rt-archive.perl.org/perl5/Ticket/Display.html?id=75598

@p5pRT
Copy link
Author

p5pRT commented Dec 1, 2012

From @doy

On Fri, Nov 30, 2012 at 04​:25​:53PM -0800, Aaron Priven wrote​:

Let's step back a minute.

The point of having $_ is to avoid extra typing. The difference
between using $_, and a regular variable like $n or $x, is to allow
certain operations to proceed without needing to specify a variable at
all. So C<for (…)> is the same as C<for $_ (…)>, C<s/x/y/> is the same
as C<$_ =~ s/x/y/>, and so forth.

And, of course, it also works with a number of core functions, such as
alarm (where C<alarm()> is the same as C<alarm($_)>, etc.) It varies,
by function. So alarm and chomp, among many others, default to $_, but
other functions such as kill, localtime, rand, or sleep do not. User
functions with the underscore prototype can do this too (although I
think, even with changes in 5.16, it still doesn't really work
properly​: see http​://perlmonks.org/?node_id=958820 )

If you think about it, it is actually extremely weird that a function
can, at its own choosing (so to speak), reach up back into the
caller's space and take the value of one of the caller's variables.

And in some sense, that means that lexical $_ isn't really lexical at
all -- at least not in the same sense as lexical $x. "my $x" means
that $x is completely hidden from all other scopes unless it is
explicitly passed, where "my $_" does not. And this using $_ as the
default is the entire purpose of $_ existing in the first place
(outside map and grep blocks).

So $_ doesn't act like a lexical even if it's declared as one. "my $_"
adds a third kind of variable -- global, lexical, and
lexical-but-the-calling-function-can-request-its-value. It would be
clearer, and more consistent, to have $_ always be global.

Exactly.

-doy

@p5pRT
Copy link
Author

p5pRT commented Dec 2, 2012

From @rjbs

* Father Chrysostomos via RT <perlbug-followup@​perl.org> [2012-11-29T20​:58​:52]

I still think we should just deprecate lexical $_.

Let's do it.

--
rjbs

@p5pRT
Copy link
Author

p5pRT commented Dec 4, 2012

From @cpansprout

On Sat Dec 01 20​:59​:32 2012, perl.p5p@​rjbs.manxome.org wrote​:

* Father Chrysostomos via RT <perlbug-followup@​perl.org> [2012-11-
29T20​:58​:52]

I still think we should just deprecate lexical $_.

Let's do it.

Done with commit 90b58ec.

--

Father Chrysostomos

@p5pRT
Copy link
Author

p5pRT commented Dec 4, 2012

From [Unknown Contact. See original ticket]

On Sat Dec 01 20​:59​:32 2012, perl.p5p@​rjbs.manxome.org wrote​:

* Father Chrysostomos via RT <perlbug-followup@​perl.org> [2012-11-
29T20​:58​:52]

I still think we should just deprecate lexical $_.

Let's do it.

Done with commit 90b58ec.

--

Father Chrysostomos

@p5pRT
Copy link
Author

p5pRT commented Dec 4, 2012

From @cpansprout

On Tue Dec 04 10​:54​:52 2012, sprout wrote​:

On Sat Dec 01 20​:59​:32 2012, perl.p5p@​rjbs.manxome.org wrote​:

* Father Chrysostomos via RT <perlbug-followup@​perl.org> [2012-11-
29T20​:58​:52]

I still think we should just deprecate lexical $_.

Let's do it.

Done with commit 90b58ec.

BTW, what happened to smartmatch?

--

Father Chrysostomos

@p5pRT
Copy link
Author

p5pRT commented Dec 4, 2012

From [Unknown Contact. See original ticket]

On Tue Dec 04 10​:54​:52 2012, sprout wrote​:

On Sat Dec 01 20​:59​:32 2012, perl.p5p@​rjbs.manxome.org wrote​:

* Father Chrysostomos via RT <perlbug-followup@​perl.org> [2012-11-
29T20​:58​:52]

I still think we should just deprecate lexical $_.

Let's do it.

Done with commit 90b58ec.

BTW, what happened to smartmatch?

--

Father Chrysostomos

@p5pRT
Copy link
Author

p5pRT commented Dec 4, 2012

From @rjbs

* Father Chrysostomos via RT <perlbug-comment@​perl.org> [2012-12-04T13​:55​:10]

BTW, what happened to smartmatch?

Short answer​:

It seemed to me like interest in creating a version matching the semantics I
described fell off. I know you had one that was close, but I think there were
also some significant differences, at least regarding the behavior of when's
implicit flow control.

I think I thought you were not interested in continuing work on that branch,
but perhaps I am misremembering, or am remembering correctly but mistaken in my
thinking.

I can review my old mail and try to give a longer answer later.

--
rjbs

@p5pRT
Copy link
Author

p5pRT commented Dec 4, 2012

From @cpansprout

On Tue Dec 04 11​:00​:21 2012, perl.p5p@​rjbs.manxome.org wrote​:

* Father Chrysostomos via RT <perlbug-comment@​perl.org> [2012-12-
04T13​:55​:10]

BTW, what happened to smartmatch?

Short answer​:

It seemed to me like interest in creating a version matching the
semantics I
described fell off. I know you had one that was close, but I think
there were
also some significant differences, at least regarding the behavior of
when's
implicit flow control.

I think I thought you were not interested in continuing work on that
branch,
but perhaps I am misremembering, or am remembering correctly but
mistaken in my
thinking.

In <DFE2CD25-99AA-4FE0-B561-BB8501354BC4@​cpan.org> I wrote​:

So now we have three variations. I’m fine with any of them, and they
are *really* easy to implement. So which will it be?

I don’t remember getting a conclusive response from you.

Regarding flow control, I have yet to hear a sound proposal that
maintains the current behaviour of skipping some loops and not others.
I think I asked you somewhere in that thread about while(<>), but I
don’t remember your answering it satisfactorily.

--

Father Chrysostomos

@p5pRT
Copy link
Author

p5pRT commented Dec 4, 2012

From [Unknown Contact. See original ticket]

On Tue Dec 04 11​:00​:21 2012, perl.p5p@​rjbs.manxome.org wrote​:

* Father Chrysostomos via RT <perlbug-comment@​perl.org> [2012-12-
04T13​:55​:10]

BTW, what happened to smartmatch?

Short answer​:

It seemed to me like interest in creating a version matching the
semantics I
described fell off. I know you had one that was close, but I think
there were
also some significant differences, at least regarding the behavior of
when's
implicit flow control.

I think I thought you were not interested in continuing work on that
branch,
but perhaps I am misremembering, or am remembering correctly but
mistaken in my
thinking.

In <DFE2CD25-99AA-4FE0-B561-BB8501354BC4@​cpan.org> I wrote​:

So now we have three variations. I’m fine with any of them, and they
are *really* easy to implement. So which will it be?

I don’t remember getting a conclusive response from you.

Regarding flow control, I have yet to hear a sound proposal that
maintains the current behaviour of skipping some loops and not others.
I think I asked you somewhere in that thread about while(<>), but I
don’t remember your answering it satisfactorily.

--

Father Chrysostomos

@p5pRT
Copy link
Author

p5pRT commented Jul 26, 2016

From @dcollinsn

I'd like to close this ticket, because it was fixed by deprecating and removing lexical $_. (The testcase now reports q{Can't use global $_ in "my"})

However, there's some more recent conversation about "what happened to smartmatch". Since I can't see how this ticket is related to smartmatch, I'm concerned that I'm missing something.

Is there still a bug where Perl crashes in this ticket? Any objections to marking this "resolved"? (I'm taking for followup, if you see a problem and intend to fix it, please feel free to assign the ticket to yourself)

--
Respectfully,
Dan Collins

@p5pRT
Copy link
Author

p5pRT commented Jul 26, 2016

From @cpansprout

On Tue Jul 26 12​:42​:11 2016, dcollinsn@​gmail.com wrote​:

I'd like to close this ticket, because it was fixed by deprecating and
removing lexical $_. (The testcase now reports q{Can't use global $_
in "my"})

However, there's some more recent conversation about "what happened to
smartmatch". Since I can't see how this ticket is related to
smartmatch, I'm concerned that I'm missing something.

That was unrelated.

Is there still a bug where Perl crashes in this ticket? Any objections
to marking this "resolved"?

I think you can go ahead and do that.

--

Father Chrysostomos

@p5pRT p5pRT closed this as completed Jul 27, 2016
@p5pRT
Copy link
Author

p5pRT commented Jul 27, 2016

@dcollinsn - Status changed from 'open' to 'resolved'

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant