From: Tom de Vries Date: Fri, 22 Nov 2024 18:34:24 +0000 (+0100) Subject: [gdb/python] Fix abort on Py_Initialize X-Git-Tag: gdb-16-branchpoint~356 X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=9d3785a8ac15dcc369b8c0b1074c5ca7145cdda7;p=thirdparty%2Fbinutils-gdb.git [gdb/python] Fix abort on Py_Initialize I tried out making python initialization fail by passing an incorrect PYTHONHOME with python 3.6, and got: ... $ PYTHONHOME=foo gdb -q Fatal Python error: Py_Initialize: Unable to get the locale encoding ModuleNotFoundError: No module named 'encodings' Current thread 0x0000ffff89269c80 (most recent call first): Fatal signal: Aborted ... Aborted (core dumped) $ ... This is as per spec: when Py_Initialize () fails, a fatal error is raised using Py_FatalError. This can be worked around using: ... $ PYTHONHOME=foo gdb -q -eiex "set python ignore-environment on" (gdb) ... but it would be better if gdb didn't abort. I found an article [1] describing two solutions: - try out Py_Initialize in a separate process, and - catch the abort using a signal handler. This patch implements the latter solution. Obviously we cannot call into python anymore after the abort, so we avoid calling Py_IsInitialized (), and instead use a new variable py_isinitialized. This gets us instead: ... $ PYTHONHOME=foo gdb -q Fatal Python error: Py_Initialize: Unable to get the locale encoding ModuleNotFoundError: No module named 'encodings' Current thread 0x0000fffecfd49c80 (most recent call first): Python not initialized $ ... Tested on aarch64-linux. Approved-By: Tom Tromey PR python/32379 Bug: https://sourceware.org/bugzilla/show_bug.cgi?id=32379 [1] https://stackoverflow.com/questions/7688374/how-to-i-catch-and-handle-a-fatal-error-when-py-initialize-fails --- diff --git a/gdb/python/python.c b/gdb/python/python.c index 820fb585475..f9d8e8778d5 100644 --- a/gdb/python/python.c +++ b/gdb/python/python.c @@ -2297,6 +2297,42 @@ gdbpy_gdb_exiting (int exit_code) gdbpy_print_stack (); } +/* Signal handler to convert a SIGABRT into an exception. */ + +static void +catch_python_fatal (int signum) +{ + signal (SIGABRT, catch_python_fatal); + + throw_exception_sjlj (gdb_exception {RETURN_ERROR, GENERIC_ERROR}); +} + +/* Stand-in for Py_IsInitialized (). To be used because after a python fatal + error, no calls into Python are allowed. */ + +static bool py_isinitialized = false; + +/* Call Py_Initialize (), and return true if successful. */ + +static bool ATTRIBUTE_UNUSED +py_initialize () +{ + auto prev_handler = signal (SIGABRT, catch_python_fatal); + SCOPE_EXIT { signal (SIGABRT, prev_handler); }; + + TRY_SJLJ + { + Py_Initialize (); + py_isinitialized = true; + } + CATCH_SJLJ (e, RETURN_MASK_ERROR) + { + } + END_CATCH_SJLJ; + + return py_isinitialized; +} + static bool do_start_initialization () { @@ -2353,7 +2389,8 @@ do_start_initialization () remain alive for the duration of the program's execution, so it is not freed after this call. */ Py_SetProgramName (progname_copy); - Py_Initialize (); + if (!py_initialize ()) + return false; #else PyConfig config; @@ -2383,9 +2420,11 @@ init_done: status.exitcode); return false; } + py_isinitialized = true; #endif #else - Py_Initialize (); + if (!py_initialize ()) + return false; #endif #if PY_VERSION_HEX < 0x03090000 @@ -2727,7 +2766,7 @@ do_initialize (const struct extension_language_defn *extlang) static void gdbpy_initialize (const struct extension_language_defn *extlang) { - if (!do_start_initialization () && Py_IsInitialized () && PyErr_Occurred ()) + if (!do_start_initialization () && py_isinitialized && PyErr_Occurred ()) gdbpy_print_stack (); gdbpy_enter enter_py;