Skip to content

Commit 8652694

Browse files
bpo-44363: Get test_capi passing with address sanitizer (GH-26639)
(cherry picked from commit 31aa0db) Co-authored-by: Mark Shannon <[email protected]>
1 parent d9f38d7 commit 8652694

File tree

3 files changed

+17
-1
lines changed

3 files changed

+17
-1
lines changed

Include/Python.h

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,13 +63,22 @@
6363
#include "pyport.h"
6464
#include "pymacro.h"
6565

66-
/* A convenient way for code to know if clang's memory sanitizer is enabled. */
66+
/* A convenient way for code to know if sanitizers are enabled. */
6767
#if defined(__has_feature)
6868
# if __has_feature(memory_sanitizer)
6969
# if !defined(_Py_MEMORY_SANITIZER)
7070
# define _Py_MEMORY_SANITIZER
7171
# endif
7272
# endif
73+
# if __has_feature(address_sanitizer)
74+
# if !defined(_Py_ADDRESS_SANITIZER)
75+
# define _Py_ADDRESS_SANITIZER
76+
# endif
77+
# endif
78+
#elif defined(__GNUC__)
79+
# if defined(__SANITIZE_ADDRESS__)
80+
# define _Py_ADDRESS_SANITIZER
81+
# endif
7382
#endif
7483

7584
/* Debug-mode build with pymalloc implies PYMALLOC_DEBUG.
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Account for address sanitizer in test_capi. test_capi now passes when run
2+
GCC address sanitizer.

Modules/_testcapimodule.c

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4688,6 +4688,10 @@ check_pyobject_forbidden_bytes_is_freed(PyObject *self, PyObject *Py_UNUSED(args
46884688
static PyObject*
46894689
check_pyobject_freed_is_freed(PyObject *self, PyObject *Py_UNUSED(args))
46904690
{
4691+
/* This test would fail if run with the address sanitizer */
4692+
#ifdef _Py_ADDRESS_SANITIZER
4693+
Py_RETURN_NONE;
4694+
#else
46914695
PyObject *op = _PyObject_CallNoArg((PyObject *)&PyBaseObject_Type);
46924696
if (op == NULL) {
46934697
return NULL;
@@ -4697,6 +4701,7 @@ check_pyobject_freed_is_freed(PyObject *self, PyObject *Py_UNUSED(args))
46974701
Py_SET_REFCNT(op, 1);
46984702
/* object memory is freed! */
46994703
return test_pyobject_is_freed("check_pyobject_freed_is_freed", op);
4704+
#endif
47004705
}
47014706

47024707

0 commit comments

Comments
 (0)