Causing PHP to crash

buffer-overflow, memory-leaks, php, stack-overflow

Solution

By causing some kinda infinite recursion, you can cause a PHP crash.

For example, a file that recursively requires itself should cause a stack overflow:

require __FILE__;  

Or a recursion in the magic `__sleep()` method that is supposed to unserialize an object, but calls `serialize()` instead:

class sleepCrasher
{
    public function __sleep()
    {
        serialize($this);
    }
}

serialize(new sleepCrasher());

Or a class destructor that creates new instances:

class destructorCrasher
{
    public function __destruct()
    {
        new destructorCrasher();
    }
}

// Calling __destruct() manually is just for the sake of example, 
// In real scenarios, PHP's garbage collector will crash PHP for you.
(new destructorCrasher())->__destruct();

As well as a recursive `__toString()`:

class toStringCrasher
{
    public function __tostring()
    {
        return strval($this);
    }
}

strval(new toStringCrasher());

There are other recursion scenarios that PHP is protected against. For example, calling a recursive function without an exit condition or a recursive self-yielding generator. These ones do not cause a crash, but a `Allowed memory size of ...` fatal error.

For more examples, you might want to see:

- PHP Crashers

- Top 10 ways to crash PHP

Problem

How can PHP cause memory leaks, buffer overflows, stack overflows and any other errors of such kind? Can PHP even cause such errors?

Original source

Related problems