summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorVictor Stinner <vstinner@python.org>2022-03-03 20:45:01 (GMT)
committerGitHub <noreply@github.com>2022-03-03 20:45:01 (GMT)
commit4173d677a1d7c72bb32d292fbff1b4cf073d615c (patch)
tree09f1e461fadd304b59ece2382f6f0659d8253017
parent127797f572cc7374192e415c44ea2e95b009d5ab (diff)
downloadcpython-4173d677a1d7c72bb32d292fbff1b4cf073d615c.zip
cpython-4173d677a1d7c72bb32d292fbff1b4cf073d615c.tar.gz
cpython-4173d677a1d7c72bb32d292fbff1b4cf073d615c.tar.bz2
bpo-46913: Fix test_faulthandler.test_sigfpe() on UBSAN (GH-31662)
Disable undefined behavior sanitizer (UBSAN) on faulthandler_sigfpe().
-rw-r--r--Misc/NEWS.d/next/Tests/2022-03-03-17-36-24.bpo-46913.vxETIE.rst3
-rw-r--r--Modules/faulthandler.c22
2 files changed, 23 insertions, 2 deletions
diff --git a/Misc/NEWS.d/next/Tests/2022-03-03-17-36-24.bpo-46913.vxETIE.rst b/Misc/NEWS.d/next/Tests/2022-03-03-17-36-24.bpo-46913.vxETIE.rst
new file mode 100644
index 0000000..65fed1c
--- /dev/null
+++ b/Misc/NEWS.d/next/Tests/2022-03-03-17-36-24.bpo-46913.vxETIE.rst
@@ -0,0 +1,3 @@
+Fix test_faulthandler.test_sigfpe() if Python is built with undefined
+behavior sanitizer (UBSAN): disable UBSAN on the faulthandler_sigfpe()
+function. Patch by Victor Stinner.
diff --git a/Modules/faulthandler.c b/Modules/faulthandler.c
index 46e18c1..4f9f661 100644
--- a/Modules/faulthandler.c
+++ b/Modules/faulthandler.c
@@ -1102,17 +1102,35 @@ faulthandler_fatal_error_c_thread(PyObject *self, PyObject *args)
Py_RETURN_NONE;
}
-static PyObject *
+// clang uses __attribute__((no_sanitize("undefined")))
+// GCC 4.9+ uses __attribute__((no_sanitize_undefined))
+#if defined(__has_feature) // Clang
+# if __has_feature(undefined_behavior_sanitizer)
+# define _Py_NO_SANITIZE_UNDEFINED __attribute__((no_sanitize("undefined")))
+# endif
+#endif
+#if defined(__GNUC__) \
+ && ((__GNUC__ >= 5) || (__GNUC__ == 4) && (__GNUC_MINOR__ >= 9))
+# define _Py_NO_SANITIZE_UNDEFINED __attribute__((no_sanitize_undefined))
+#endif
+#ifndef _Py_NO_SANITIZE_UNDEFINED
+# define _Py_NO_SANITIZE_UNDEFINED
+#endif
+
+static PyObject* _Py_NO_SANITIZE_UNDEFINED
faulthandler_sigfpe(PyObject *self, PyObject *args)
{
+ faulthandler_suppress_crash_report();
+
/* Do an integer division by zero: raise a SIGFPE on Intel CPU, but not on
PowerPC. Use volatile to disable compile-time optimizations. */
volatile int x = 1, y = 0, z;
- faulthandler_suppress_crash_report();
z = x / y;
+
/* If the division by zero didn't raise a SIGFPE (e.g. on PowerPC),
raise it manually. */
raise(SIGFPE);
+
/* This line is never reached, but we pretend to make something with z
to silence a compiler warning. */
return PyLong_FromLong(z);