From 74c346621d3bfc0c38a75351261571947fff8482 Mon Sep 17 00:00:00 2001 From: Steve Dower Date: Fri, 2 Jun 2017 14:11:17 -0700 Subject: [PATCH 1/3] bpo-30557: faulthandler now correctly filters and displays exception codes on Windows --- Misc/NEWS | 3 +++ Modules/faulthandler.c | 6 +++--- 2 files changed, 6 insertions(+), 3 deletions(-) diff --git a/Misc/NEWS b/Misc/NEWS index 902b102ac72d5c..bb8485edd0ebcf 100644 --- a/Misc/NEWS +++ b/Misc/NEWS @@ -345,6 +345,9 @@ Extension Modules Library ------- +- bpo-30557: faulthandler now correctly filters and displays exception codes + on Windows + - bpo-30245: Fix possible overflow when organize struct.pack_into error message. Patch by Yuan Liu. diff --git a/Modules/faulthandler.c b/Modules/faulthandler.c index dcfebf2781f6c9..d2e0fa38680a84 100644 --- a/Modules/faulthandler.c +++ b/Modules/faulthandler.c @@ -374,7 +374,7 @@ faulthandler_exc_handler(struct _EXCEPTION_POINTERS *exc_info) DWORD flags = exc_info->ExceptionRecord->ExceptionFlags; /* only log fatal exceptions */ - if (flags & EXCEPTION_NONCONTINUABLE) { + if (!(code & 0x80000000)) { /* call the next exception handler */ return EXCEPTION_CONTINUE_SEARCH; } @@ -391,8 +391,8 @@ faulthandler_exc_handler(struct _EXCEPTION_POINTERS *exc_info) case EXCEPTION_IN_PAGE_ERROR: PUTS(fd, "page error"); break; case EXCEPTION_STACK_OVERFLOW: PUTS(fd, "stack overflow"); break; default: - PUTS(fd, "code "); - _Py_DumpDecimal(fd, code); + PUTS(fd, "code 0x"); + _Py_DumpHexadecimal(fd, code, 8); } PUTS(fd, "\n\n"); From 6d665b259e9e7fefdd94d9e5f45d1c75ab30bd5e Mon Sep 17 00:00:00 2001 From: Steve Dower Date: Mon, 5 Jun 2017 12:59:06 -0700 Subject: [PATCH 2/3] Adds test for non-fatal exceptions. --- Lib/test/test_faulthandler.py | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) diff --git a/Lib/test/test_faulthandler.py b/Lib/test/test_faulthandler.py index 626e2458dcd9a6..c965d67db3437d 100644 --- a/Lib/test/test_faulthandler.py +++ b/Lib/test/test_faulthandler.py @@ -754,6 +754,32 @@ def test_raise_exception(self): 3, name) + @unittest.skipUnless(MS_WINDOWS, 'specific to Windows') + def test_raise_nonfatal_exception(self): + # These exceptions are not strictly errors. Letting + # faulthandler display the traceback when they are + # raised is likely to result in noise. However, they + # may still terminate the process if there is no + # handler installed for them (which there typically + # is, e.g. for debug messages). + for exc in ( + 0x00000000, + 0x34567890, + 0x40000000, + 0x40001000, + 0x70000000, + 0x7FFFFFFF, + ): + output, exitcode = self.get_output(f""" + import faulthandler + faulthandler.enable() + faulthandler._raise_exception(0x{exc:x}) + """ + ) + self.assertEqual(output, []) + # Actual exception code has bit 4 cleared + self.assertEqual(exitcode, exc & ~0x10000000) + @unittest.skipUnless(MS_WINDOWS, 'specific to Windows') def test_disable_windows_exc_handler(self): code = dedent(""" From 513c34a5545da8a2baa351a5c7096c2078cd895f Mon Sep 17 00:00:00 2001 From: Steve Dower Date: Mon, 5 Jun 2017 13:01:22 -0700 Subject: [PATCH 3/3] Adds bpo number to comment. --- Modules/faulthandler.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Modules/faulthandler.c b/Modules/faulthandler.c index d2e0fa38680a84..39b70bcf3a10b4 100644 --- a/Modules/faulthandler.c +++ b/Modules/faulthandler.c @@ -373,7 +373,7 @@ faulthandler_exc_handler(struct _EXCEPTION_POINTERS *exc_info) DWORD code = exc_info->ExceptionRecord->ExceptionCode; DWORD flags = exc_info->ExceptionRecord->ExceptionFlags; - /* only log fatal exceptions */ + /* bpo-30557: only log fatal exceptions */ if (!(code & 0x80000000)) { /* call the next exception handler */ return EXCEPTION_CONTINUE_SEARCH;