From 7a101bc1a087c234c50ea39641913144312d6a63 Mon Sep 17 00:00:00 2001 From: Mark Shannon Date: Thu, 10 Jun 2021 11:16:28 +0100 Subject: [PATCH 1/2] Get test_capi passing when run with address sanitizer. --- Include/Python.h | 11 ++++++++++- Modules/_testcapimodule.c | 5 +++++ 2 files changed, 15 insertions(+), 1 deletion(-) diff --git a/Include/Python.h b/Include/Python.h index 4d0335d3c52c36..04858f281cab8d 100644 --- a/Include/Python.h +++ b/Include/Python.h @@ -63,13 +63,22 @@ #include "pyport.h" #include "pymacro.h" -/* A convenient way for code to know if clang's memory sanitizer is enabled. */ +/* A convenient way for code to know if sanitizers are enabled. */ #if defined(__has_feature) # if __has_feature(memory_sanitizer) # if !defined(_Py_MEMORY_SANITIZER) # define _Py_MEMORY_SANITIZER # endif # endif +# if __has_feature(address_sanitizer) +# if !defined(_Py_ADDRESS_SANITIZER) +# define _Py_ADDRESS_SANITIZER +# endif +# endif +#elif defined(__GNUC__) +# if defined(__SANITIZE_ADDRESS__) +# define _Py_ADDRESS_SANITIZER +# endif #endif #include "pymath.h" diff --git a/Modules/_testcapimodule.c b/Modules/_testcapimodule.c index ab47949d89e635..b983deeac6aafb 100644 --- a/Modules/_testcapimodule.c +++ b/Modules/_testcapimodule.c @@ -4784,6 +4784,10 @@ check_pyobject_forbidden_bytes_is_freed(PyObject *self, PyObject *Py_UNUSED(args static PyObject* check_pyobject_freed_is_freed(PyObject *self, PyObject *Py_UNUSED(args)) { + /* This test would fail if run with the address sanitizer */ +#ifdef _Py_ADDRESS_SANITIZER + Py_RETURN_NONE; +#else PyObject *op = _PyObject_CallNoArg((PyObject *)&PyBaseObject_Type); if (op == NULL) { return NULL; @@ -4793,6 +4797,7 @@ check_pyobject_freed_is_freed(PyObject *self, PyObject *Py_UNUSED(args)) Py_SET_REFCNT(op, 1); /* object memory is freed! */ return test_pyobject_is_freed("check_pyobject_freed_is_freed", op); +#endif } From d78d27eab3f309b77b533a3fc33e140e0de1e20d Mon Sep 17 00:00:00 2001 From: Mark Shannon Date: Thu, 10 Jun 2021 11:19:51 +0100 Subject: [PATCH 2/2] Add NEWS --- Misc/NEWS.d/next/Tests/2021-06-10-11-19-43.bpo-44363.-K9jD0.rst | 2 ++ 1 file changed, 2 insertions(+) create mode 100644 Misc/NEWS.d/next/Tests/2021-06-10-11-19-43.bpo-44363.-K9jD0.rst diff --git a/Misc/NEWS.d/next/Tests/2021-06-10-11-19-43.bpo-44363.-K9jD0.rst b/Misc/NEWS.d/next/Tests/2021-06-10-11-19-43.bpo-44363.-K9jD0.rst new file mode 100644 index 00000000000000..28468cbd2b682b --- /dev/null +++ b/Misc/NEWS.d/next/Tests/2021-06-10-11-19-43.bpo-44363.-K9jD0.rst @@ -0,0 +1,2 @@ +Account for address sanitizer in test_capi. test_capi now passes when run +GCC address sanitizer.