12

We are using embedded cpython as a scripting language in our app. We are modifying our sys.path at startup to make sure that we don't execute code from outside our app, but a user with a sitecustomize.py in their PYTHONPATH is causing code to execute before we have a chance to fix sys.path, and we believe their code has a hard crash (not an exception, which site.py will catch and handle gracefully).

I believe the correct fix is to simply clear the PYTHONPATH variable from the environment before we initialize python, but I can't test it properly because I can't recreate the problem.

The simplest way I've found is using ctypes to write to memory, such as:

import ctypes
p = (ctypes.c_char).from_address(0)
while True:
  p[0] = 0
  p = p + 1

But in Python 3.5, it doesn't allow me to write to c_char types, giving the error "TypeError: 'c_char' object does not support item assignment".

I've tried a few of the methods available in https://wiki.python.org/moin/CrashingPython to no avail.

Is there a reliable way to crash python 3.5 from pure Python code?

3

7 Answers 7

8

Here is a simple way to crash python:

def crash():
    try:
        crash()
    except:
        crash()

crash()
1
  • 1
    In python 3.11.3 this appears to cause python to hang, not crash.
    – spelufo
    Jul 25 at 15:21
7

There are plenty of ways through ctypes. For example, a corrected version of your code:

p = ctypes.pointer(ctypes.c_char.from_address(5))
p[0] = b'x'

If you don't want to use ctypes, you could trigger a C stack overflow in the dict.__repr__ implementation:

x = {}
for i in range(1000000):
    x = {1: x}
repr(x)

This might get patched in a future Python release, but for now, it should produce a hard crash.

There are also ways to do it by constructing your own bytecode objects, since Python does almost nothing to make sure that the bytecode it's executing makes sense.

0
4

You can use the fallowing:

exec(type((lambda: 0).__code__)(0, 0, 0, 0, 0, 0, b'\x053', (), (), (), '', '', 0, b''))

This will create bytecode that starts a finally statement that is never ended, thus crashing the interpreter.

1

Found this one-liner:

exec(type((lambda:0).__code__)(0,1,0,0,0,b'',(),(),(),'','',1,b''))

which was snagged from this code-golf question:

1
  • This looks good to me, although I didn't test it. user2357112's answer worked for me, and the code is easier to understand. Thank you for your answer.
    – alvion
    Mar 6, 2017 at 7:19
1

Simply put 0 / 0 on the line of code you want to trigger the crash. You will get a ZeroDivisionError

I think this is the easiest way to force a crash

1
  • An exception is not a crash. Nov 3 at 9:07
0

Fastest crash in the west(at least worked on 3.10 and 3.11):

__import__("sys").setrecursionlimit(500000000)
def i():i()
i()

Like waits a second and boom, window closed.

It increases the recursion limit to 500000000, defines a recursive function, and runs it. It would take a lot of time if it really worked, but it's just way too much, so window closes.

1
  • This simply causes a RecursionError: maximum recursion depth exceeded. That is not a crash. And you could do it quicker with a raise RecursionError("maximum recursion depth exceeded"). Nov 3 at 9:06
-3

I usually only use Python 2.7 but I believe the same still applies here as long as I understand your question correctly. If you would like to forcibly exit a Python program then the easiest way I know is by raising a SystemExit. Here is a fairly simple example:

for i in range (0, 10):
    if (i == 6): #Stop the program at 6 just.. because
        raise SystemExit
    print(i)
1
  • 5
    This does not cause python to crash. It is no different than simply calling sys.exit().
    – ekhumoro
    Mar 2, 2017 at 0:38

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.