Skip to content

Commit 6885ddf

Browse files
authored
Merge branch 'main' into 24053-add-exit-constants
2 parents 5348353 + 2791c35 commit 6885ddf

53 files changed

Lines changed: 955 additions & 400 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

‎Doc/c-api/memory.rst‎

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -437,6 +437,8 @@ Release build ``"pymalloc"`` ``malloc``
437437
Debug build ``"pymalloc_debug"`` ``malloc`` + debug ``pymalloc`` + debug ``pymalloc`` + debug
438438
Release build, without pymalloc ``"malloc"`` ``malloc`` ``malloc`` ``malloc``
439439
Debug build, without pymalloc ``"malloc_debug"`` ``malloc`` + debug ``malloc`` + debug ``malloc`` + debug
440+
Release build, with ASan or MSan ``"malloc"`` ``malloc`` ``malloc`` ``malloc``
441+
Debug build, with ASan or MSan ``"malloc_debug"`` ``malloc`` + debug ``malloc`` + debug ``malloc`` + debug
440442
Free-threaded build ``"mimalloc"`` ``mimalloc`` ``mimalloc`` ``mimalloc``
441443
Free-threaded debug build ``"mimalloc_debug"`` ``mimalloc`` + debug ``mimalloc`` + debug ``mimalloc`` + debug
442444
=================================== ======================= ==================== ====================== ======================
@@ -451,6 +453,10 @@ Legend:
451453
* "+ debug": with :ref:`debug hooks on the Python memory allocators
452454
<pymem-debug-hooks>`.
453455
* "Debug build": :ref:`Python build in debug mode <debug-build>`.
456+
* "with ASan or MSan": sanitizer build as configured using the
457+
:option:`--with-address-sanitizer`,
458+
:option:`--with-hwaddress-sanitizer`, and/or
459+
:option:`--with-memory-sanitizer` option.
454460
455461
.. _customize-memory-allocators:
456462
@@ -705,9 +711,11 @@ This allocator is disabled if Python is configured with the
705711
:option:`--without-pymalloc` option. It can also be disabled at runtime using
706712
the :envvar:`PYTHONMALLOC` environment variable (ex: ``PYTHONMALLOC=malloc``).
707713
708-
Typically, it makes sense to disable the pymalloc allocator when building
709-
Python with AddressSanitizer (:option:`--with-address-sanitizer`) which helps
710-
uncover low level bugs within the C code.
714+
The pymalloc allocator is disabled by default when Python is built with
715+
a sanitizer which does not track pymalloc allocations
716+
(:option:`--with-address-sanitizer`, :option:`--with-hwaddress-sanitizer`,
717+
:option:`--with-memory-sanitizer`).
718+
Use :envvar:`PYTHONMALLOC=pymalloc <PYTHONMALLOC>` to enable pymalloc.
711719
712720
Customize pymalloc Arena Allocator
713721
----------------------------------

‎Doc/data/stable_abi.dat‎

Lines changed: 2 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎Doc/howto/abi3t-migration.rst‎

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -169,14 +169,22 @@ or :c:member:`PyTypeObject.tp_itemsize`), it cannot be ported to
169169
``abi3t`` 3.15.
170170

171171

172+
.. _abi3t-migration-build:
173+
172174
Setting up the build
173175
====================
174176

175-
If you use a build tool (such as setuptools, meson-python, scikit-build-core),
176-
search its documentation for a way to select ``abi3t``.
177-
At the time of writing, not all of them have this; but if your tool does,
178-
use it.
179-
You may want to verify that it set the right flag by temporarily adding the
177+
If you use a build tool, search its documentation for "``abi3t``", and follow
178+
any instructions to select the ABI.
179+
For reference, here are direct links for several popular build tools:
180+
181+
- `meson-python
182+
<https://mesonbuild.com/meson-python/how-to-guides/limited-api.html#the-abi3t-stable-abi>`__
183+
- `scikit-build-core
184+
<https://scikit-build-core.readthedocs.io/en/stable/configuration/#customizing-the-output-wheel>`__
185+
- `Maturin <https://www.maturin.rs/bindings#py_limited_apiabi3>`__
186+
187+
You may want to verify that the tool set the right flag by temporarily adding the
180188
following just after ``#include <Python.h>``::
181189

182190
#if Py_TARGET_ABI3T+0 <= 0x30f0000
@@ -185,6 +193,13 @@ following just after ``#include <Python.h>``::
185193

186194
This should result in a different error than "``abi3t`` define is not set".
187195

196+
.. seealso::
197+
198+
`Building and distributing abi3t extensions
199+
<https://py-free-threading.github.io/abi3t/>`__:
200+
Build configuration and wheel testing examples in the community-maintained
201+
Python Free-Threading Guide.
202+
188203
.. note::
189204

190205
If your build tool doesn't support ``abi3t`` yet, set the following macro

‎Doc/library/sys.rst‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2113,7 +2113,7 @@ always available. Unless explicitly noted otherwise, all variables are read-only
21132113
See :ref:`remote-debugging` for more information about the remote debugging
21142114
mechanism.
21152115

2116-
.. audit-event:: sys.remote_exec pid script_path
2116+
.. audit-event:: sys.remote_exec pid,script_path
21172117

21182118
When the code is executed in the remote process, an
21192119
:ref:`auditing event <auditing>` ``sys.remote_exec`` is raised with

‎Doc/using/configure.rst‎

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1016,12 +1016,28 @@ Debug options
10161016
.. option:: --with-address-sanitizer
10171017

10181018
Enable AddressSanitizer memory error detector, ``asan`` (default is no).
1019-
To improve ASan detection capabilities you may also want to combine this
1020-
with :option:`--without-pymalloc` to disable the specialized small-object
1021-
allocator whose allocations are not tracked by ASan.
1019+
1020+
When built with ``asan``, Python uses ``malloc`` instead of :ref:`pymalloc <pymalloc>` by default.
1021+
Set :envvar:`PYTHONMALLOC=pymalloc <PYTHONMALLOC>` to use pymalloc.
10221022

10231023
.. versionadded:: 3.6
10241024

1025+
.. option:: --with-hwaddress-sanitizer
1026+
1027+
Enable HWAddressSanitizer memory error detector, ``hwasan`` (default is no).
1028+
Note that on x86-64 this uses `page aliasing
1029+
<https://clang.llvm.org/docs/HardwareAssistedAddressSanitizerDesign.html#supported-architectures>`_,
1030+
which only tags heap allocations and is unsafe for programs that ``fork()``,
1031+
including much of the test suite.
1032+
See the `LLVM HWASan design documentation
1033+
<https://clang.llvm.org/docs/HardwareAssistedAddressSanitizerDesign.html>`_
1034+
for more information.
1035+
1036+
When built with ``hwasan``, Python uses ``malloc`` instead of :ref:`pymalloc <pymalloc>` by default.
1037+
Set :envvar:`PYTHONMALLOC=pymalloc <PYTHONMALLOC>` to use pymalloc.
1038+
1039+
.. versionadded:: next
1040+
10251041
.. option:: --with-memory-sanitizer
10261042

10271043
Enable MemorySanitizer allocation error detector, ``msan`` (default is no).
@@ -1030,6 +1046,9 @@ Debug options
10301046
not built with MSan, so either build all dependencies with MSan or disable
10311047
the extension modules that use them in :file:`Modules/Setup.local`.
10321048

1049+
When built with ``msan``, Python uses ``malloc`` instead of :ref:`pymalloc <pymalloc>` by default.
1050+
Set :envvar:`PYTHONMALLOC=pymalloc <PYTHONMALLOC>` to use pymalloc.
1051+
10331052
.. versionadded:: 3.6
10341053

10351054
.. option:: --with-undefined-behavior-sanitizer

‎Doc/whatsnew/3.15.rst‎

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -572,16 +572,19 @@ the existing Stable ABI (``abi3``) and the version-specific ABI for
572572
free-threading (``cp315t``) separately.
573573

574574
Stable ABI for Free-Threaded Builds should typically
575-
be selected in a build tool (such as, for example, Setuptools, meson-python,
576-
scikit-build-core, or Maturin).
577-
At the time of writing, these tools do **not** support ``abi3t``.
578-
If this is the case for your tool, compile for ``cp315t`` separately.
575+
be selected in a build tool. Tools with ``abi3t`` support include
576+
meson-python, scikit-build-core, and Maturin; see
577+
:ref:`abi3t-migration-build` for links to their documentation on ``abi3t`` builds.
578+
If your build tool does not support ``abi3t``, compile for ``cp315t`` separately.
579579
If not using a build tool -- or when writing such a tool -- you can select
580580
``abi3t`` by setting the macro :c:macro:`!Py_TARGET_ABI3T` as discussed
581581
in :ref:`abi3-compiling`.
582582

583583
A practical :ref:`migration guide <abi3t-migration-howto>` for switching to
584584
``abi3t`` is available.
585+
The community-maintained `Python Free-Threading Guide
586+
<https://py-free-threading.github.io/abi3t/>`__ provides examples of building
587+
and testing ``abi3t`` wheels.
585588

586589
.. seealso:: :pep:`803` for further details.
587590

‎Doc/whatsnew/3.16.rst‎

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1171,6 +1171,11 @@ Build changes
11711171

11721172
(Contributed by Stan Ulbrych in :gh:`139314`.)
11731173

1174+
* Add the :option:`--with-hwaddress-sanitizer` :program:`configure` option to
1175+
build with `HWAddressSanitizer <https://clang.llvm.org/docs/HardwareAssistedAddressSanitizerDesign.html>`_.
1176+
1177+
(Contributed by Stan Ulbrych, Florian Mayer and AnnaAr321 in :gh:`156049`.)
1178+
11741179

11751180
C API changes
11761181
=============
@@ -1181,6 +1186,10 @@ New features
11811186
* Add :c:func:`Py_HashBuffer` to the limited C API.
11821187
(Contributed by Victor Stinner in :gh:`148233`.)
11831188

1189+
* Add :c:macro:`Py_SETREF` and :c:macro:`Py_XSETREF` macros to the limited C
1190+
API.
1191+
(Contributed by Nathan Goldbaum and Victor Stinner in :gh:`158842`.)
1192+
11841193
Porting to Python 3.16
11851194
----------------------
11861195

‎Include/cpython/object.h‎

Lines changed: 0 additions & 81 deletions
Original file line numberDiff line numberDiff line change
@@ -326,87 +326,6 @@ _PyObject_GenericSetAttrWithDict(PyObject *, PyObject *,
326326

327327
PyAPI_FUNC(PyObject *) _PyObject_FunctionStr(PyObject *);
328328

329-
/* Safely decref `dst` and set `dst` to `src`.
330-
*
331-
* As in case of Py_CLEAR "the obvious" code can be deadly:
332-
*
333-
* Py_DECREF(dst);
334-
* dst = src;
335-
*
336-
* The safe way is:
337-
*
338-
* Py_SETREF(dst, src);
339-
*
340-
* That arranges to set `dst` to `src` _before_ decref'ing, so that any code
341-
* triggered as a side-effect of `dst` getting torn down no longer believes
342-
* `dst` points to a valid object.
343-
*
344-
* Temporary variables are used to only evaluate macro arguments once and so
345-
* avoid the duplication of side effects. _Py_TYPEOF(), C++ auto, or memcpy()
346-
* is used to avoid a miscompilation caused by type punning. See Py_CLEAR()
347-
* comment for implementation details about type punning.
348-
*
349-
* The memcpy() implementation does not emit a compiler warning if 'src' has
350-
* not the same type than 'src': any pointer type is accepted for 'src'.
351-
*/
352-
#ifdef _Py_TYPEOF
353-
#define Py_SETREF(dst, src) \
354-
do { \
355-
_Py_TYPEOF(&(dst)) _tmp_dst_ptr = &(dst); \
356-
_Py_TYPEOF(dst) _tmp_old_dst = (*_tmp_dst_ptr); \
357-
*_tmp_dst_ptr = (src); \
358-
Py_DECREF(_tmp_old_dst); \
359-
} while (0)
360-
#elif defined(__cplusplus) && (__cplusplus >= 201103L || _MSVC_LANG >= 201103L)
361-
#define Py_SETREF(dst, src) \
362-
do { \
363-
auto _tmp_dst_ptr = &(dst); \
364-
auto _tmp_old_dst = (*_tmp_dst_ptr); \
365-
*_tmp_dst_ptr = (src); \
366-
Py_DECREF(_tmp_old_dst); \
367-
} while (0)
368-
#else
369-
#define Py_SETREF(dst, src) \
370-
do { \
371-
PyObject **_tmp_dst_ptr = _Py_CAST(PyObject**, &(dst)); \
372-
PyObject *_tmp_old_dst = (*_tmp_dst_ptr); \
373-
PyObject *_tmp_src = _PyObject_CAST(src); \
374-
memcpy(_tmp_dst_ptr, &_tmp_src, sizeof(PyObject*)); \
375-
Py_DECREF(_tmp_old_dst); \
376-
} while (0)
377-
#endif
378-
379-
/* Py_XSETREF() is a variant of Py_SETREF() that uses Py_XDECREF() instead of
380-
* Py_DECREF().
381-
*/
382-
#ifdef _Py_TYPEOF
383-
#define Py_XSETREF(dst, src) \
384-
do { \
385-
_Py_TYPEOF(&(dst)) _tmp_dst_ptr = &(dst); \
386-
_Py_TYPEOF(dst) _tmp_old_dst = (*_tmp_dst_ptr); \
387-
*_tmp_dst_ptr = (src); \
388-
Py_XDECREF(_tmp_old_dst); \
389-
} while (0)
390-
#elif defined(__cplusplus) && (__cplusplus >= 201103L || _MSVC_LANG >= 201103L)
391-
#define Py_XSETREF(dst, src) \
392-
do { \
393-
auto _tmp_dst_ptr = &(dst); \
394-
auto _tmp_old_dst = (*_tmp_dst_ptr); \
395-
*_tmp_dst_ptr = (src); \
396-
Py_XDECREF(_tmp_old_dst); \
397-
} while (0)
398-
#else
399-
#define Py_XSETREF(dst, src) \
400-
do { \
401-
PyObject **_tmp_dst_ptr = _Py_CAST(PyObject**, &(dst)); \
402-
PyObject *_tmp_old_dst = (*_tmp_dst_ptr); \
403-
PyObject *_tmp_src = _PyObject_CAST(src); \
404-
memcpy(_tmp_dst_ptr, &_tmp_src, sizeof(PyObject*)); \
405-
Py_XDECREF(_tmp_old_dst); \
406-
} while (0)
407-
#endif
408-
409-
410329
/* Define a pair of assertion macros:
411330
_PyObject_ASSERT_FROM(), _PyObject_ASSERT_WITH_MSG() and _PyObject_ASSERT().
412331

‎Include/internal/pycore_pymem_init.h‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,10 @@ extern void _PyMem_MiRawFree(void *, void *);
3636
extern void* _PyMem_MiRawRealloc(void *, void *, size_t);
3737
# undef PYRAW_ALLOC
3838
# define PYRAW_ALLOC {NULL, _PyMem_MiRawMalloc, _PyMem_MiRawCalloc, _PyMem_MiRawRealloc, _PyMem_MiRawFree}
39+
#elif defined(_Py_ADDRESS_SANITIZER) || defined(_Py_MEMORY_SANITIZER)
40+
// Keep in sync with the default allocators in Objects/obmalloc.c.
41+
# define PYOBJ_ALLOC PYRAW_ALLOC
42+
# define PYMEM_ALLOC PYOBJ_ALLOC
3943
#elif defined(WITH_PYMALLOC)
4044
extern void* _PyObject_Malloc(void *, size_t);
4145
extern void* _PyObject_Calloc(void *, size_t, size_t);

‎Include/pyport.h‎

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -567,6 +567,12 @@ extern "C" {
567567
# define _Py_NO_SANITIZE_ADDRESS __attribute__((no_sanitize_address))
568568
# endif
569569
# endif
570+
# if __has_feature(hwaddress_sanitizer)
571+
# if !defined(_Py_ADDRESS_SANITIZER)
572+
# define _Py_ADDRESS_SANITIZER
573+
# define _Py_NO_SANITIZE_ADDRESS __attribute__((no_sanitize("hwaddress")))
574+
# endif
575+
# endif
570576
# if __has_feature(thread_sanitizer)
571577
# if !defined(_Py_THREAD_SANITIZER)
572578
# define _Py_THREAD_SANITIZER
@@ -577,6 +583,9 @@ extern "C" {
577583
# if defined(__SANITIZE_ADDRESS__)
578584
# define _Py_ADDRESS_SANITIZER
579585
# define _Py_NO_SANITIZE_ADDRESS __attribute__((no_sanitize_address))
586+
# elif defined(__SANITIZE_HWADDRESS__)
587+
# define _Py_ADDRESS_SANITIZER
588+
# define _Py_NO_SANITIZE_ADDRESS __attribute__((no_sanitize("hwaddress")))
580589
# endif
581590
# if defined(__SANITIZE_THREAD__)
582591
# define _Py_THREAD_SANITIZER

0 commit comments

Comments
 (0)