[Python-checkins] bpo-44363: Get test_capi passing with address sanitizer (GH-26639)

miss-islington webhook-mailer at python.org
Thu Jun 10 08:01:30 EDT 2021


https://github.com/python/cpython/commit/865269458fde83f58f056d02a777e4328c763880
commit: 865269458fde83f58f056d02a777e4328c763880
branch: 3.9
author: Miss Islington (bot) <31488909+miss-islington at users.noreply.github.com>
committer: miss-islington <31488909+miss-islington at users.noreply.github.com>
date: 2021-06-10T05:01:22-07:00
summary:

bpo-44363: Get test_capi passing with address sanitizer (GH-26639)

(cherry picked from commit 31aa0dbff4c1d39c9d77c6c8f4a61d0e46c1268b)

Co-authored-by: Mark Shannon <mark at hotpy.org>

files:
A Misc/NEWS.d/next/Tests/2021-06-10-11-19-43.bpo-44363.-K9jD0.rst
M Include/Python.h
M Modules/_testcapimodule.c

diff --git a/Include/Python.h b/Include/Python.h
index dcd0a57ac1f03f..acee38c41539d6 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
 
 /* Debug-mode build with pymalloc implies PYMALLOC_DEBUG.
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.
diff --git a/Modules/_testcapimodule.c b/Modules/_testcapimodule.c
index 4f6f193202b9b4..53a6752d5fb2b1 100644
--- a/Modules/_testcapimodule.c
+++ b/Modules/_testcapimodule.c
@@ -4688,6 +4688,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;
@@ -4697,6 +4701,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
 }
 
 



More information about the Python-checkins mailing list