Skip to content

Commit 4173d67

Browse files
authored
bpo-46913: Fix test_faulthandler.test_sigfpe() on UBSAN (GH-31662)
Disable undefined behavior sanitizer (UBSAN) on faulthandler_sigfpe().
1 parent 127797f commit 4173d67

File tree

2 files changed

+23
-2
lines changed

2 files changed

+23
-2
lines changed
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
Fix test_faulthandler.test_sigfpe() if Python is built with undefined
2+
behavior sanitizer (UBSAN): disable UBSAN on the faulthandler_sigfpe()
3+
function. Patch by Victor Stinner.

Modules/faulthandler.c

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1102,17 +1102,35 @@ faulthandler_fatal_error_c_thread(PyObject *self, PyObject *args)
11021102
Py_RETURN_NONE;
11031103
}
11041104

1105-
static PyObject *
1105+
// clang uses __attribute__((no_sanitize("undefined")))
1106+
// GCC 4.9+ uses __attribute__((no_sanitize_undefined))
1107+
#if defined(__has_feature) // Clang
1108+
# if __has_feature(undefined_behavior_sanitizer)
1109+
# define _Py_NO_SANITIZE_UNDEFINED __attribute__((no_sanitize("undefined")))
1110+
# endif
1111+
#endif
1112+
#if defined(__GNUC__) \
1113+
&& ((__GNUC__ >= 5) || (__GNUC__ == 4) && (__GNUC_MINOR__ >= 9))
1114+
# define _Py_NO_SANITIZE_UNDEFINED __attribute__((no_sanitize_undefined))
1115+
#endif
1116+
#ifndef _Py_NO_SANITIZE_UNDEFINED
1117+
# define _Py_NO_SANITIZE_UNDEFINED
1118+
#endif
1119+
1120+
static PyObject* _Py_NO_SANITIZE_UNDEFINED
11061121
faulthandler_sigfpe(PyObject *self, PyObject *args)
11071122
{
1123+
faulthandler_suppress_crash_report();
1124+
11081125
/* Do an integer division by zero: raise a SIGFPE on Intel CPU, but not on
11091126
PowerPC. Use volatile to disable compile-time optimizations. */
11101127
volatile int x = 1, y = 0, z;
1111-
faulthandler_suppress_crash_report();
11121128
z = x / y;
1129+
11131130
/* If the division by zero didn't raise a SIGFPE (e.g. on PowerPC),
11141131
raise it manually. */
11151132
raise(SIGFPE);
1133+
11161134
/* This line is never reached, but we pretend to make something with z
11171135
to silence a compiler warning. */
11181136
return PyLong_FromLong(z);

0 commit comments

Comments
 (0)