Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 7 additions & 3 deletions Doc/c-api/memory.rst
Original file line number Diff line number Diff line change
Expand Up @@ -437,6 +437,8 @@ Release build ``"pymalloc"`` ``malloc``
Debug build ``"pymalloc_debug"`` ``malloc`` + debug ``pymalloc`` + debug ``pymalloc`` + debug
Release build, without pymalloc ``"malloc"`` ``malloc`` ``malloc`` ``malloc``
Debug build, without pymalloc ``"malloc_debug"`` ``malloc`` + debug ``malloc`` + debug ``malloc`` + debug
Release build, with ASan or MSan ``"malloc"`` ``malloc`` ``malloc`` ``malloc``
Debug build, with ASan or MSan ``"malloc_debug"`` ``malloc`` + debug ``malloc`` + debug ``malloc`` + debug
Free-threaded build ``"mimalloc"`` ``mimalloc`` ``mimalloc`` ``mimalloc``
Free-threaded debug build ``"mimalloc_debug"`` ``mimalloc`` + debug ``mimalloc`` + debug ``mimalloc`` + debug
=================================== ======================= ==================== ====================== ======================
Expand Down Expand Up @@ -705,9 +707,11 @@ This allocator is disabled if Python is configured with the
:option:`--without-pymalloc` option. It can also be disabled at runtime using
the :envvar:`PYTHONMALLOC` environment variable (ex: ``PYTHONMALLOC=malloc``).

Typically, it makes sense to disable the pymalloc allocator when building
Python with AddressSanitizer (:option:`--with-address-sanitizer`) which helps
uncover low level bugs within the C code.
The pymalloc allocator is disabled by default when Python is built with
AddressSanitizer (:option:`--with-address-sanitizer`) or MemorySanitizer
(:option:`--with-memory-sanitizer`), since these sanitizers do not track
allocations made by pymalloc. Use
:envvar:`PYTHONMALLOC=pymalloc <PYTHONMALLOC>` to enable it.

Customize pymalloc Arena Allocator
----------------------------------
Expand Down
7 changes: 4 additions & 3 deletions Doc/using/configure.rst
Original file line number Diff line number Diff line change
Expand Up @@ -1009,15 +1009,16 @@ Debug options
.. option:: --with-address-sanitizer

Enable AddressSanitizer memory error detector, ``asan`` (default is no).
Comment thread
vasiliyk marked this conversation as resolved.
To improve ASan detection capabilities you may also want to combine this
with :option:`--without-pymalloc` to disable the specialized small-object
allocator whose allocations are not tracked by ASan.
Python uses ``malloc`` instead of :ref:`pymalloc <pymalloc>` by default.
Set :envvar:`PYTHONMALLOC=pymalloc <PYTHONMALLOC>` to use pymalloc.

.. versionadded:: 3.6

.. option:: --with-memory-sanitizer

Enable MemorySanitizer allocation error detector, ``msan`` (default is no).
Python uses ``malloc`` instead of :ref:`pymalloc <pymalloc>` by default.
Set :envvar:`PYTHONMALLOC=pymalloc <PYTHONMALLOC>` to use pymalloc.

.. versionadded:: 3.6

Expand Down
4 changes: 4 additions & 0 deletions Include/internal/pycore_pymem_init.h
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,10 @@ extern void _PyMem_MiRawFree(void *, void *);
extern void* _PyMem_MiRawRealloc(void *, void *, size_t);
# undef PYRAW_ALLOC
# define PYRAW_ALLOC {NULL, _PyMem_MiRawMalloc, _PyMem_MiRawCalloc, _PyMem_MiRawRealloc, _PyMem_MiRawFree}
#elif defined(_Py_ADDRESS_SANITIZER) || defined(_Py_MEMORY_SANITIZER)
// Keep in sync with the default allocators in Objects/obmalloc.c.
# define PYOBJ_ALLOC PYRAW_ALLOC
# define PYMEM_ALLOC PYOBJ_ALLOC
#elif defined(WITH_PYMALLOC)
extern void* _PyObject_Malloc(void *, size_t);
extern void* _PyObject_Calloc(void *, size_t, size_t);
Expand Down
14 changes: 11 additions & 3 deletions Lib/test/test_cmd_line.py
Original file line number Diff line number Diff line change
Expand Up @@ -830,10 +830,13 @@ def test_xdev(self):
code = "import _testinternalcapi; print(_testinternalcapi.pymem_getallocatorsname())"
with support.SuppressCrashReport():
out = self.run_xdev("-c", code, check_exitcode=False)
if support.with_pymalloc():
alloc_name = "pymalloc_debug"
elif support.Py_GIL_DISABLED:
if support.Py_GIL_DISABLED:
alloc_name = "mimalloc_debug"
elif support.check_sanitizer(address=True, memory=True):
# ASan and MSan builds default to malloc, even with pymalloc.
alloc_name = "malloc_debug"
elif support.with_pymalloc():
alloc_name = "pymalloc_debug"
else:
alloc_name = "malloc_debug"
self.assertEqual(out, alloc_name)
Expand Down Expand Up @@ -912,10 +915,15 @@ def test_pythonmalloc(self):
# Test the PYTHONMALLOC environment variable
malloc = not support.Py_GIL_DISABLED
pymalloc = support.with_pymalloc()
sanitizer = support.check_sanitizer(address=True, memory=True)
mimalloc = support.with_mimalloc()
if support.Py_GIL_DISABLED:
default_name = 'mimalloc_debug' if support.Py_DEBUG else 'mimalloc'
default_name_debug = 'mimalloc_debug'
elif sanitizer:
# ASan and MSan builds default to malloc, even with pymalloc.
default_name = 'malloc_debug' if support.Py_DEBUG else 'malloc'
default_name_debug = 'malloc_debug'
elif pymalloc:
default_name = 'pymalloc_debug' if support.Py_DEBUG else 'pymalloc'
default_name_debug = 'pymalloc_debug'
Expand Down
4 changes: 3 additions & 1 deletion Lib/test/test_sys.py
Original file line number Diff line number Diff line change
Expand Up @@ -1129,8 +1129,10 @@ def test_debugmallocstats(self):
# The sysconfig vars are not available on Windows.
if sys.platform != "win32":
with_pymalloc = sysconfig.get_config_var("WITH_PYMALLOC")
with_sanitizer = support.check_sanitizer(address=True, memory=True)
self.assertIn(b"free PyDictObjects", err)
if with_pymalloc:
# ASan and MSan builds default to malloc, even with pymalloc.
if with_pymalloc and not with_sanitizer:
self.assertIn(b'Small block threshold', err)

# The function has no parameter
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
Python built with :option:`--with-address-sanitizer` or
:option:`--with-memory-sanitizer` now includes pymalloc, but uses ``malloc``
as the default memory allocator. Set
:envvar:`PYTHONMALLOC=pymalloc <PYTHONMALLOC>` to use pymalloc.
6 changes: 6 additions & 0 deletions Objects/obmalloc.c
Original file line number Diff line number Diff line change
Expand Up @@ -433,6 +433,12 @@ void* _PyObject_Realloc(void *ctx, void *ptr, size_t size);
# define PYRAW_ALLOC MIMALLOC_RAWALLOC
# define PYMEM_ALLOC MIMALLOC_ALLOC
# define PYOBJ_ALLOC MIMALLOC_OBJALLOC
#elif defined(_Py_ADDRESS_SANITIZER) || defined(_Py_MEMORY_SANITIZER)
// ASan and MSan do not track pymalloc blocks, so use malloc by default.
// pymalloc can still be selected at runtime.
# define PYRAW_ALLOC MALLOC_ALLOC
# define PYMEM_ALLOC MALLOC_ALLOC
# define PYOBJ_ALLOC MALLOC_ALLOC
#elif defined(WITH_PYMALLOC)
# define PYRAW_ALLOC MALLOC_ALLOC
# define PYMEM_ALLOC PYMALLOC_ALLOC
Expand Down
8 changes: 4 additions & 4 deletions configure

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 4 additions & 4 deletions configure.ac
Original file line number Diff line number Diff line change
Expand Up @@ -3523,8 +3523,8 @@ AC_ARG_WITH([address_sanitizer],
AC_MSG_RESULT([$withval])
BASECFLAGS="-fsanitize=address -fno-omit-frame-pointer $BASECFLAGS"
LDFLAGS="-fsanitize=address $LDFLAGS"
# ASan works by controlling memory allocation, our own malloc interferes.
with_pymalloc="no"
# ASan works by controlling memory allocation, our own malloc interferes,
# so pymalloc is still built, but malloc is the default allocator at runtime.
],
[AC_MSG_RESULT([no])])

Expand All @@ -3541,8 +3541,8 @@ AX_CHECK_COMPILE_FLAG([-fsanitize=memory],[
BASECFLAGS="-fsanitize=memory -fsanitize-memory-track-origins=2 -fno-omit-frame-pointer $BASECFLAGS"
LDFLAGS="-fsanitize=memory -fsanitize-memory-track-origins=2 $LDFLAGS"
],[AC_MSG_ERROR([The selected compiler doesn't support memory sanitizer])])
# MSan works by controlling memory allocation, our own malloc interferes.
with_pymalloc="no"
# MSan works by controlling memory allocation, our own malloc interferes,
# so pymalloc is still built, but malloc is the default allocator at runtime.
],
[AC_MSG_RESULT([no])])

Expand Down
Loading