http://www.perlmonks.org?node_id=505817

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

Replies are listed 'Best First'.
Re: How to kill perl?
by Aristotle (Chancellor) on Nov 04, 2005 at 21:59 UTC
    $ 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.

      That's beautiful!
Re: How to kill perl?
by diotalevi (Canon) on Nov 04, 2005 at 18:21 UTC
    unpack "P", ~0

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

      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?
by samtregar (Abbot) on Nov 04, 2005 at 18:26 UTC
    perl -e dump

    -sam

      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?
by EvanCarroll (Chaplain) on Nov 04, 2005 at 19:13 UTC
    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
      So, 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!
        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
      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'

      Good point. Or even

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

Re: How to kill perl?
by ambrus (Abbot) on Nov 04, 2005 at 21:44 UTC
Re: How to kill perl?
by tirwhan (Abbot) on Nov 04, 2005 at 19:30 UTC
    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?
by Perl Mouse (Chaplain) on Nov 07, 2005 at 10:16 UTC
    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:>*
        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?
by sauoq (Abbot) on Nov 04, 2005 at 23:34 UTC

    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.";
    
      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?
by sauoq (Abbot) on Nov 04, 2005 at 23:56 UTC

    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?
by Steve_p (Priest) on Nov 05, 2005 at 19:25 UTC
    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.

      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 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. Let's see what the p5p hive mind thinks.

      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: How to kill perl?
by Steve_p (Priest) on Nov 05, 2005 at 02:40 UTC

    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?
by duckyd (Hermit) on Nov 08, 2005 at 00:28 UTC

      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?
by mikeock (Hermit) on Nov 04, 2005 at 21:39 UTC

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

      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'

        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: How to kill perl?
by Dominus (Parson) on Nov 06, 2005 at 23:34 UTC
Re: How to kill perl?
by monarch (Priest) on Nov 07, 2005 at 01:05 UTC
    Whilst it doesn't cause a core dump, another method for bailing out of perl quickly, is to use the 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?
by Anonymous Monk on Nov 04, 2005 at 21:48 UTC
    How to kill perl? die perl, die ;-)

      OB Simpsons

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

      --
      if ( 1 ) { $postman->ring() for (1..2); }
Re: How to kill perl?
by whio (Beadle) on May 07, 2006 at 04:16 UTC
    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.)
Re: How to kill perl?
by Dominus (Parson) on Aug 20, 2007 at 14:21 UTC
    A few years ago someone—I think maybe Ilya—was looking for core dumps by generating random inputs to Perl, and found quite a few, mostly in the parser.

    I also recall being able to get Perl to dump core by feeding it the output of uuencode.

Re: How to kill perl?
by poolpi (Hermit) on Jan 11, 2008 at 09:33 UTC
    # This is perl, v5.8.8 built for i486-linux-gnu-thread-multi
    # Linux 2.6.18-4-686
    #!/usr/bin/perl use strict; use warnings; my $c; my $x =~ /(?{local $c})/;

    Die with: panic: top_env

    PooLpi
Re: How to kill perl?
by EvanCarroll (Chaplain) on Feb 14, 2006 at 04:43 UTC
    $#='%n' and print;


    Evan Carroll
    www.EvanCarroll.com
      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.)