Skip to content

Commit 95ce674

Browse files
committed
Merge remote-tracking branch 'origin/main' into gh-158973-reset-warnings-lock
2 parents 2f7a64c + e19dc47 commit 95ce674

226 files changed

Lines changed: 5819 additions & 4252 deletions

File tree

Some content is hidden

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

‎.github/workflows/build.yml‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -547,6 +547,9 @@ jobs:
547547
- check-name: Undefined behavior
548548
sanitizer: UBSan
549549
free-threading: false
550+
- check-name: Memory
551+
sanitizer: MSan
552+
free-threading: false
550553
uses: ./.github/workflows/reusable-san.yml
551554
with:
552555
sanitizer: ${{ matrix.sanitizer }}

‎.github/workflows/reusable-san.yml‎

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ jobs:
6060
|| ''
6161
}}
6262
- name: UBSan option setup
63-
if: inputs.sanitizer != 'TSan'
63+
if: inputs.sanitizer == 'UBSan'
6464
run: >-
6565
echo
6666
"UBSAN_OPTIONS=${SAN_LOG_OPTION}
@@ -69,6 +69,22 @@ jobs:
6969
>> "$GITHUB_ENV"
7070
env:
7171
SAN_LOG_OPTION: log_path=${{ github.workspace }}/san_log
72+
- name: MSan option setup
73+
if: inputs.sanitizer == 'MSan'
74+
run: |
75+
sudo sysctl -w vm.mmap_rnd_bits=28 # Reduce ASLR to avoid MSan re-executing
76+
77+
echo "MSAN_OPTIONS=${SAN_LOG_OPTION} allocator_may_return_null=1 handle_segv=0" >> "$GITHUB_ENV"
78+
# MSan reports false positives for memory initialized by libraries
79+
# that are not built with MSan, so disable modules that use them.
80+
# _remote_debugging links to libzstd directly, but we unpoision the memory.
81+
{
82+
echo '*disabled*'
83+
echo '_bz2 _ctypes _curses _curses_panel _dbm _decimal _gdbm _hashlib'
84+
echo '_lzma _sqlite3 _ssl _tkinter _uuid _zstd readline zlib'
85+
} > Modules/Setup.local
86+
env:
87+
SAN_LOG_OPTION: log_path=${{ github.workspace }}/san_log
7288
- name: Add ccache to PATH
7389
run: |
7490
echo "PATH=/usr/lib/ccache:$PATH" >> "$GITHUB_ENV"
@@ -93,6 +109,8 @@ jobs:
93109
# gh-157958: -O2 instead of the pydebug default -Og to avoid a clang 21
94110
# compile-time blowup on some interpreter files.
95111
# (https://github.com/llvm/llvm-project/issues/179695)
112+
# MSan uses --with-assertions instead of --with-pydebug because its
113+
# hooks on the Python memory allocators hide uninitialized reads.
96114
- name: Configure CPython
97115
run: >-
98116
./configure
@@ -101,9 +119,11 @@ jobs:
101119
${{
102120
inputs.sanitizer == 'TSan'
103121
&& '--with-thread-sanitizer'
122+
|| inputs.sanitizer == 'MSan'
123+
&& '--with-memory-sanitizer'
104124
|| '--with-undefined-behavior-sanitizer --with-strict-overflow'
105125
}}
106-
--with-pydebug
126+
${{ inputs.sanitizer == 'MSan' && '--with-assertions' || '--with-pydebug' }}
107127
${{ inputs.sanitizer == 'TSan' && '--with-openssl="$OPENSSL_DIR" --with-openssl-rpath=auto' || '' }}
108128
${{ inputs.free-threading && '--disable-gil' || '' }}
109129
- name: Build CPython

‎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/c-api/sys.rst‎

Lines changed: 23 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -152,28 +152,29 @@ Operating System Utilities
152152
<c-preinit>` and so that the LC_CTYPE locale is properly configured: see
153153
the :c:func:`Py_PreInitialize` function.
154154
155-
Decode a byte string from the :term:`filesystem encoding and error handler`.
156-
If the error handler is :ref:`surrogateescape error handler
157-
<surrogateescape>`, undecodable bytes are decoded as characters in range
158-
U+DC80..U+DCFF; and if a byte sequence can be decoded as a surrogate
159-
character, the bytes are escaped using the surrogateescape error handler
160-
instead of decoding them.
155+
Decode a byte string from the :term:`filesystem encoding <filesystem
156+
encoding and error handler>` with the :ref:`surrogateescape error handler
157+
<surrogateescape>`.
158+
159+
Undecodable bytes are decoded as characters in range U+DC80..U+DCFF. If a
160+
byte sequence can be decoded as a surrogate character, escape the bytes
161+
using the surrogateescape error handler instead of decoding them.
161162
162163
Return a pointer to a newly allocated wide character string, use
163164
:c:func:`PyMem_RawFree` to free the memory. If size is not ``NULL``, write
164165
the number of wide characters excluding the null character into ``*size``
165166
166-
Return ``NULL`` on decoding error or memory allocation error. If *size* is
167-
not ``NULL``, ``*size`` is set to ``(size_t)-1`` on memory error or set to
168-
``(size_t)-2`` on decoding error.
167+
On memory allocation failure, set *\*size* to ``(size_t)-1`` and return
168+
``NULL``.
169+
170+
On decode error, set *\*size* to ``(size_t)-2`` and return ``NULL``.
171+
Decoding errors should never happen, unless there is a bug in the C
172+
library.
169173
170174
The :term:`filesystem encoding and error handler` are selected by
171175
:c:func:`PyConfig_Read`: see :c:member:`~PyConfig.filesystem_encoding` and
172176
:c:member:`~PyConfig.filesystem_errors` members of :c:type:`PyConfig`.
173177
174-
Decoding errors should never happen, unless there is a bug in the C
175-
library.
176-
177178
Use the :c:func:`Py_EncodeLocale` function to encode the character string
178179
back to a byte string.
179180
@@ -195,17 +196,19 @@ Operating System Utilities
195196
196197
.. c:function:: char* Py_EncodeLocale(const wchar_t *text, size_t *error_pos)
197198
198-
Encode a wide character string to the :term:`filesystem encoding and error
199-
handler`. If the error handler is :ref:`surrogateescape error handler
200-
<surrogateescape>`, surrogate characters in the range U+DC80..U+DCFF are
201-
converted to bytes 0x80..0xFF.
199+
Encode a wide character string to the :term:`filesystem encoding <filesystem
200+
encoding and error handler>` with the :ref:`surrogateescape error handler
201+
<surrogateescape>`. Surrogate characters in the range U+DC80..U+DCFF are
202+
encoded to bytes 0x80..0xFF.
202203
203204
Return a pointer to a newly allocated byte string, use :c:func:`PyMem_Free`
204-
to free the memory. Return ``NULL`` on encoding error or memory allocation
205-
error.
205+
to free the memory.
206+
207+
On memory allocation failure, set *\*error_pos* to ``(size_t)-1`` and return
208+
``NULL``.
206209
207-
If error_pos is not ``NULL``, ``*error_pos`` is set to ``(size_t)-1`` on
208-
success, or set to the index of the invalid character on encoding error.
210+
On encoding error, set *\*error_pos* to the index of the first unencodable
211+
character and return ``NULL``.
209212
210213
The :term:`filesystem encoding and error handler` are selected by
211214
:c:func:`PyConfig_Read`: see :c:member:`~PyConfig.filesystem_encoding` and

‎Doc/c-api/unicode.rst‎

Lines changed: 40 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -168,8 +168,14 @@ access to internal read-only data of Unicode objects:
168168
The function performs no checks for any of its requirements,
169169
and is intended for usage in loops.
170170
171+
While :class:`str` objects are usually immutable in Python, this special C API allows
172+
mutating a fresh :class:`str` object if the string has not been "used" yet.
173+
171174
.. versionadded:: 3.3
172175
176+
.. soft-deprecated:: next
177+
Use the :c:type:`PyUnicodeWriter` API instead.
178+
173179
174180
.. c:function:: Py_UCS4 PyUnicode_READ(int kind, void *data, Py_ssize_t index)
175181
@@ -407,9 +413,15 @@ APIs:
407413
using the :c:type:`PyUnicodeWriter` API, or one of the ``PyUnicode_From*``
408414
functions below.
409415
416+
While :class:`str` objects are usually immutable in Python, this special C API
417+
returns a :class:`str` object that can be mutated, except if *size* is zero, in which
418+
case it returns the immutable empty string constant.
410419
411420
.. versionadded:: 3.3
412421
422+
.. soft-deprecated:: next
423+
Use the :c:type:`PyUnicodeWriter` API instead.
424+
413425
414426
.. c:function:: PyObject* PyUnicode_FromKindAndData(int kind, const void *buffer, \
415427
Py_ssize_t size)
@@ -754,11 +766,16 @@ APIs:
754766
possible. Returns ``-1`` and sets an exception on error, otherwise returns
755767
the number of copied characters.
756768
757-
The string must not have been “used” yet.
769+
While :class:`str` objects are usually immutable in Python, this special C API allows
770+
mutating a fresh :class:`str` object if the string has not been "used" yet.
771+
758772
See :c:func:`PyUnicode_New` for details.
759773
760774
.. versionadded:: 3.3
761775
776+
.. soft-deprecated:: next
777+
Use the :c:type:`PyUnicodeWriter` API instead.
778+
762779
763780
.. c:function:: int PyUnicode_Resize(PyObject **unicode, Py_ssize_t length);
764781
@@ -774,6 +791,14 @@ APIs:
774791
The function doesn't check string content, the result may not be a
775792
string in canonical representation.
776793
794+
While :class:`str` objects are usually immutable in Python, this special C API
795+
can resize a :class:`str` object in-place if the string has not been "used" yet.
796+
It returns a :class:`str` object which can be mutated, except if *size* is zero, in
797+
which case it returns the immutable empty string constant.
798+
799+
.. soft-deprecated:: next
800+
Use the :c:type:`PyUnicodeWriter` API instead.
801+
777802
778803
.. c:function:: Py_ssize_t PyUnicode_Fill(PyObject *unicode, Py_ssize_t start, \
779804
Py_ssize_t length, Py_UCS4 fill_char)
@@ -784,14 +809,19 @@ APIs:
784809
Fail if *fill_char* is bigger than the string maximum character, or if the
785810
string has more than 1 reference.
786811
787-
The string must not have been “used” yet.
788-
See :c:func:`PyUnicode_New` for details.
789-
790812
Return the number of written characters, or return ``-1`` and raise an
791813
exception on error.
792814
815+
While :class:`str` objects are usually immutable in Python, this special C API allows
816+
mutating a fresh :class:`str` object if the string has not been "used" yet.
817+
818+
See :c:func:`PyUnicode_New` for details.
819+
793820
.. versionadded:: 3.3
794821
822+
.. soft-deprecated:: next
823+
Use the :c:type:`PyUnicodeWriter` API instead.
824+
795825
796826
.. c:function:: int PyUnicode_WriteChar(PyObject *unicode, Py_ssize_t index, \
797827
Py_UCS4 character)
@@ -804,11 +834,16 @@ APIs:
804834
See :c:func:`PyUnicode_WRITE` for a version that skips these checks,
805835
making them your responsibility.
806836
807-
The string must not have been “used” yet.
837+
While :class:`str` objects are usually immutable in Python, this special C API allows
838+
mutating a fresh :class:`str` object if the string has not been "used" yet.
839+
808840
See :c:func:`PyUnicode_New` for details.
809841
810842
.. versionadded:: 3.3
811843
844+
.. soft-deprecated:: next
845+
Use the :c:type:`PyUnicodeWriter` API instead.
846+
812847
813848
.. c:function:: Py_UCS4 PyUnicode_ReadChar(PyObject *unicode, Py_ssize_t index)
814849

‎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/ctypes.rst‎

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1755,8 +1755,9 @@ These prefabricated library loaders are available:
17551755
:c:expr:`int`, which is of course not always the truth, so you have to assign
17561756
the correct :attr:`!restype` attribute to use these functions.
17571757

1758-
Note that if the Python interpreter is statically linked, this will be
1759-
``None``, as ``dlopen`` is not possible in this case.
1758+
.. note::
1759+
1760+
If the Python interpreter is statically linked, this may be ``None``.
17601761

17611762
.. audit-event:: ctypes.dlopen name ctypes.LibraryLoader
17621763

‎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: 26 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1016,16 +1016,39 @@ 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).
10281044

1045+
MSan reports false positives for memory initialized by libraries that are
1046+
not built with MSan, so either build all dependencies with MSan or disable
1047+
the extension modules that use them in :file:`Modules/Setup.local`.
1048+
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+
10291052
.. versionadded:: 3.6
10301053

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

0 commit comments

Comments
 (0)