How to kill perl?
robin
created: 2005-11-04 13:05:32

What’s your favourite way to make perl dump core or panic?

The Perl interpreter is quite good at doing the right thing when you give it sensible code, but it's easy to make it crash if you actually try to confuse it. That's a bit of a shame, but for the things that Perl tends to be used for it doesn't really matter that much.

Here are three of my favourites to get you started:

  perl -e 'undef %:: or die'
  perl -e '()= sort {*a=*b, 1} 2,3'
  perl -e '@a=(1..5); map {@a=() if $x++>2, $_} @a'

All these are still broken in bleadperl, though the last now panics instead of dumping core.

This isn’t wholly frivolous, because it would be useful to have a list of these bugs; though I don't want that to detract from the simple fun of trying to make something break.

Edit: [g0n] Moved to Meditations from SoPW

Re: How to kill perl?
created: 2005-11-04 13:21:25
unpack "P", ~0

Why doesn't unpack "P", 0 dump? I expected it to and this confused me.

Re^2: How to kill perl?
created: 2005-11-04 13:35:20
Why doesn't unpack "P", 0 dump? I expected it to and this confused me.

The reason (on some level) is that pp_unpack() calls newSVpvn(), which calls sv_setpvn(), which has this code:

    if (!ptr) {
        (void)SvOK_off(sv);
        return;
    }
In other words, unpack("P", 0) just returns an undefined SV. Since C code often uses null pointers in the same way that Perl code uses undef, I think this is more useful than crashing. :-)

Re: How to kill perl?
created: 2005-11-04 13:26:42
perl -e dump

-sam

Re^2: How to kill perl?
created: 2005-11-04 13:37:01
That's cheating! :-)

PS. diatolevi's answer is a cheat too of course, but not quite such a blatant one.

Re: How to kill perl?
created: 2005-11-04 14:13:54
My site has a nice one with reverse and sort perlbugs

perl -e '@a=(1..5); map {@a=() if $x++>2, $_} @a'
Can also be written as:
perl -e '@a=(1..5); map {@a=() if $x++, $_} @a'


Evan Carroll
www.EvanCarroll.com
Re^2: How to kill perl?
created: 2005-11-04 14:25:21

Good point. Or even

perl -e '@a=(0,1); map {@a=() if $_; $_} @a'

Re^2: How to kill perl?
created: 2005-11-04 17:57:59
My site has a nice one with reverse and sort perlbugs

That is nice. Do you have any idea why it happens? Is it in the bug database?

Update: Looks like you don't need the second reverse, or even the <>; the following crashes both bleadperl and 5.8.6 for me:

  perl -e 'map print(reverse), ("")x68'

Re^2: How to kill perl?
created: 2005-11-05 06:21:26
So, [http://www.xray.mpe.mpg.de/mailing-lists/perl5-porters/2005-11/msg00130.html|it turns out] that your map/reverse bug is just one of a family of crash bugs, e.g:
perl -e "map chomp(+()), ('')x10000;"
perl -e "map chown(+()), ('')x10000;"
perl -e "map warn(+()), ('')x100;"
Though they're all fixed now!
Re^3: How to kill perl?
created: 2005-11-06 17:50:27
Not all of them are fixed, the top two tested negative, with Perl 5.8.7; however, the bottom one produced this.

perl -e "map warn(+()), ('')x100;"
...
...
Warning: something's wrong at -e line 1.
Warning: something's wrong at -e line 1.
Warning: something's wrong at -e line 1.
Warning: something's wrong at -e line 1.
Warning: something's wrong at -e line 1.
Warning: something's wrong at -e line 1.
Warning: something's wrong at -e line 1.
Segmentation fault
UPDATE:
You are correct by increasing the number to 100_000, the top two crash as well.


Evan Carroll
www.EvanCarroll.com
Re^4: How to kill perl?
created: 2005-11-07 05:06:05
They're fixed in bleadperl, as of two days ago. The bug is still there in 5.8.7, and all three of these tests crash 5.8.7 for me. (Maybe you can see the crash too, if you increase the 10000 to something larger?)
Re: How to kill perl?
created: 2005-11-04 14:30:15
Currently
perl -MIO::Prompt -e'while(prompt){1}'
but that's using a module, so maybe it's cheating ;-)?

Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it. -- Brian W. Kernighan
Re: How to kill perl?
created: 2005-11-04 16:39:36

Wow, There are some really interesting ways to acomplish this! Any one have any others?

Re^2: How to kill perl?
created: 2005-11-04 18:30:01
Any one have any others?

I just fixed a whole bunch of sort-related ones, which is what got me thinking about this. If your perl is less more than a week old, these should kill it:

  perl -e'sub f{undef &f} ()=sort f 1,2'
  perl -e'sub f{goto sub{1}} ()=sort f 1,2'
  perl -e'@a=(1..10);@a=sort{@a=(1);0}@a'

Re^3: How to kill perl?
created: 2005-11-04 18:52:48
perl -e'@a=(1..10);@a=sort{@a=(1);0}@a'

That works fine for me on 5.8.0 buts segfaults on 5.8.7.

-sauoq
"My two cents aren't worth a dime.";
Re^4: How to kill perl?
created: 2005-11-04 19:50:42
Since 5.8.4, @foo = sort @foo is optimised to be an in-place sort of @foo, which is what makes it die so messily.
Re: How to kill perl?
created: 2005-11-04 16:44:27

See my anonymous reply to the poll node 293886. The second one worked only in older perls.

(Update: fixed typo)

Update: perl -we 'kill 11,$$' is cheating, isn't it? You can also use lots of other numbers instead of 11 of course.

Re: How to kill perl?
created: 2005-11-04 16:48:16
How to kill perl? die perl, die ;-)
Re^2: How to kill perl?
created: 2005-11-05 06:05:38

OB Simpsons

That's not perl, that's german ;)

--
if ( 1 ) { $postman->ring() for (1..2); }
Re: How to kill perl?
created: 2005-11-04 16:59:23
$ perl -le'/(??{s!!!})/'
Segmentation fault

(I like the way it kind of looks like someone’s yelling “what the hell?!” at perl.)

Makeshifts last the longest.

Re^2: How to kill perl?
created: 2005-11-04 17:34:29
That's beautiful!
Re: How to kill perl?
created: 2005-11-04 18:34:23

Similar to your second one...

$ echo -e 'foo\n' | perl -e'print sort{*a=$b,0} <>'
*** glibc detected *** malloc(): memory corruption: 0x0812dcdc ***
Aborted

-sauoq
"My two cents aren't worth a dime.";
Re^2: How to kill perl?
created: 2005-11-06 21:54:03
I could claim that gnu malloc(3) was able to prevent the segfault. Here it is choosing a graceful abort to the crash.
$ export MALLOC_CHECK_=2
$ perl -e 'print sort{*a=$b,0} <>'
a
b
Aborted
Re: How to kill perl?
created: 2005-11-04 18:56:16

Okay, here's one I just found...

$ perl -e'@a=(1,1);map{sort{@a=()}@a}@a'
Segmentation fault
Update: Err... but that's on 5.8.0. It seems to be okay on 5.8.7. Darn.

-sauoq
"My two cents aren't worth a dime.";
Re: How to kill perl?
created: 2005-11-04 21:40:28

I closed a few perlbugs opened by people that ran something that amounted to perl -u, which is supposed to dump core on purpose.

Re: How to kill perl?
created: 2005-11-05 14:25:02
perl -e 'undef %:: or die'

I'm testing a fix to this in bleadperl and it appears to have it fixed. Should be available later today. By the way...

perl -e 'undef %:: or warn'

was equally broken.

Re^2: How to kill perl?
created: 2005-11-05 15:54:58
Ugh! Spoke too soon. My fix kills some of the more exotic functionality of die(). Well, at least I know I'm on the right track.
Re^2: How to kill perl?
created: 2005-11-05 17:15:48

I don't think this really has to do with die specifically. For example,

  perl -e 'undef %::; sub{}->()'
crashes too. Lots of thing crash when you've totalled the defstash! But perhaps that just indicates a pervasive assumption, which really needs to be fixed in lots of different places.

See also [http://guest:guest@rt.perl.org:80/rt3/Ticket/Display.html?id=32401|bug 32401].

PS. Getting a bit more targeted:

perl -e 'delete $::{"@"}; warn'      ;# crashes
perl -e 'delete $::{"@"}; sub{}->()' ;# doesn't crash

perl -e 'delete $::{"_"}; warn'      ;# doesn't crash
perl -e 'delete $::{"_"}; sub{}->()' ;# crashes

I suppose that wiping out important global variables oughtn't to be allowed, really.

PPS. [http://www.xray.mpe.mpg.de/mailing-lists/perl5-porters/2005-11/msg00148.html|Let's see what the p5p hive mind thinks].

Re: How to kill perl?
created: 2005-11-06 18:34:25
Maybe this isn't what you wanted, but my favorite is kill 6 => $$.

Re: How to kill perl?
created: 2005-11-06 20:05:26
Whilst it doesn't cause a core dump, another method for bailing out of perl quickly, is to use the [cpan://POSIX] module..
use POSIX qw(_exit);

POSIX::_exit( 0 ); # abort ungracefully (and quickly!)

When perl dies it usually appears to spend lots of time closing file handles. For a forked server that has lots of sockets open this could take some time, so why not let the OS deal with the messy issues and use POSIX::_exit().

Re: How to kill perl?
created: 2005-11-07 05:16:15
Kill Perl Golf:
$ perl -e '/(?{<})/'
panic: top_env
If you swap the less then sign, it's just a syntax error:
$ perl -e '/(?{>})/'
syntax error at (re_eval 1) line 1, near ">"
Compilation failed in regexp at -e line 1.
(Reason: anything that might be the start of a quoted construct inside (?{}), but isn't followed by one panics perl. (?{'}), (?{"}), (?{`}), (?{<}), (?{/}), (?{?}), (?{q}), (?{m}) and (?{s}) all panic perl. Other single charactes seem to do ok (although I didn't check all of them).
Perl --((8:>*
Re^2: How to kill perl?
created: 2005-11-07 05:58:50
Wow! I didn't know about this one, though I see it was reported quite a while ago.
Re^3: How to kill perl?
QM
created: 2005-12-10 10:54:05
I was searching on "panic top_env" and came across this thread...

Did you quote the right bug? Or did it's link change?

The link you gave (now) points to a bug about array slice problems.

-QM
--
Quantum Mechanics: The dreams stuff is made of

Re: How to kill perl?
created: 2005-11-07 19:28:04
perl -e 'END{%::=()}'
Re^2: How to kill perl?
created: 2005-11-08 13:44:03

These stash-nuking bugs are now fixed in blead. Yay!

(The fix was originally written in April 2001 by Benjamin Sugars, but was only applied today.)

Re: How to kill perl?
created: 2006-02-13 23:43:47
$#='%n' and print;


Evan Carroll
www.EvanCarroll.com
Re^2: How to kill perl?
created: 2006-02-17 08:00:36
What version of perl does that kill? On what platform? (I can't get it to kill 5.8.6 or 5.9.3, on the Mac.)
Re: How to kill perl?
created: 2006-05-07 00:16:56
perl -we s//*_=0/e
perl -e  s//*_=0/e
This one is interesting mostly because $^W has an unexpected effect. (some versions of perl don't crash if -w is used.)

perlmonks.org content © perlmonks.org and ambrus, Anonymous Monk, Aristotle, diotalevi, Dominus, duckyd, EvanCarroll, gargle, ioannis, mikeock, monarch, Perl Mouse, QM, robin, samtregar, sauoq, Steve_p, tirwhan, whio

prlmnks.org © 2006 edmund von der burg (eccles & toad)

v 0.03