Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

gh-129069: make list ass_slice and memory_repeat safe in free-threading#131882

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to ourterms of service andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

Open
tom-pytel wants to merge13 commits intopython:main
base:main
Choose a base branch
Loading
fromtom-pytel:fix-issue-129069
Open
Show file tree
Hide file tree
Changes fromall commits
Commits
Show all changes
13 commits
Select commitHold shift + click to select a range
0c8dcfc
gh-129069: make list ass_slice and memory_repeat safe
tom-pytelMar 29, 2025
67c9459
📜🤖 Added by blurb_it.
blurb-it[bot]Mar 29, 2025
e460b5a
include atomic wrappers in pycore_list.h
tom-pytelMar 29, 2025
fcdb634
provisional fix to compile on windows
tom-pytelMar 29, 2025
b1e9216
requested changes so far
tom-pytelApr 1, 2025
944abf0
Merge branch 'main' into fix-issue-129069
tom-pytelApr 1, 2025
3a9e249
Merge branch 'main' into fix-issue-129069
tom-pytelApr 4, 2025
0f96d58
remove atomic load and rename
tom-pytelApr 8, 2025
47f3d81
Merge branch 'main' into fix-issue-129069
tom-pytelApr 8, 2025
adf2c15
Merge branch 'main' into fix-issue-129069
tom-pytelApr 14, 2025
b569865
Merge branch 'main' into fix-issue-129069
tom-pytelApr 23, 2025
87d48ff
Merge branch 'main' into fix-issue-129069
tom-pytelMay 18, 2025
26ee081
requested variable name changes
tom-pytelMay 18, 2025
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
55 changes: 55 additions & 0 deletionsInclude/cpython/pyatomic.h
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -545,6 +545,61 @@ static inline Py_ssize_t
_Py_atomic_load_ssize_acquire(const Py_ssize_t *obj);


// --- _Py_atomic_memcpy / _Py_atomic_memmove ------------

static inline void *
_Py_atomic_memcpy_ptr_store_relaxed(void *dest, void *src, Py_ssize_t n)
{
size_t size = (size_t)n;
assert(((uintptr_t)dest & (sizeof (void *) - 1)) == 0);
assert(((uintptr_t)src & (sizeof (void *) - 1)) == 0);
assert(size % sizeof(void *) == 0);

if (dest != src) {
void **dest_ = (void **)dest;
void **src_ = (void **)src;
void **end = dest_ + size / sizeof(void *);

for (; dest_ != end; dest_++, src_++) {
_Py_atomic_store_ptr_relaxed(dest_, *src_);
}
}

return dest;
}

static inline void *
_Py_atomic_memmove_ptr_store_relaxed(void *dest, void *src, Py_ssize_t n)
{
size_t size = (size_t)n;
assert(((uintptr_t)dest & (sizeof (void *) - 1)) == 0);
assert(((uintptr_t)src & (sizeof (void *) - 1)) == 0);
assert(size % sizeof(void *) == 0);

if (dest < src || dest >= (void *)((char *)src + size)) {
void **dest_ = (void **)dest;
void **src_ = (void **)src;
void **end = dest_ + size / sizeof(void *);

for (; dest_ != end; dest_++, src_++) {
_Py_atomic_store_ptr_relaxed(dest_, *src_);
}
}
else if (dest > src) {
size = size / sizeof(void *) - 1;
void **dest_ = (void **)dest + size;
void **src_ = (void **)src + size;
void **end = (void **)dest - 1;

for (; dest_ != end; dest_--, src_--) {
_Py_atomic_store_ptr_relaxed(dest_, *src_);
}
}

return dest;
}




// --- _Py_atomic_fence ------------------------------------------------------
Expand Down
10 changes: 7 additions & 3 deletionsInclude/internal/pycore_list.h
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -12,6 +12,8 @@ extern "C" {
#include "pycore_stackref.h"
#endif

#include "pycore_pyatomic_ft_wrappers.h"

PyAPI_FUNC(PyObject*) _PyList_Extend(PyListObject *, PyObject *);
PyAPI_FUNC(PyObject) *_PyList_SliceSubscript(PyObject*, PyObject*);
extern void _PyList_DebugMallocStats(FILE *out);
Expand DownExpand Up@@ -51,15 +53,17 @@ _PyList_AppendTakeRef(PyListObject *self, PyObject *newitem)
return _PyList_AppendTakeRefListResize(self, newitem);
}

// Repeat the bytes of a buffer in place
// Repeat the bytes of a bufferof pointersin place
static inline void
_Py_memory_repeat(char* dest, Py_ssize_t len_dest, Py_ssize_t len_src)
_Py_memory_ptrs_repeat(char* dest, Py_ssize_t len_dest, Py_ssize_t len_src)
{
assert(len_src > 0);
assert(len_src % sizeof(void *) == 0);
assert(((uintptr_t)dest & (sizeof (void *) - 1)) == 0);
Py_ssize_t copied = len_src;
while (copied < len_dest) {
Py_ssize_t bytes_to_copy = Py_MIN(copied, len_dest - copied);
memcpy(dest + copied, dest, (size_t)bytes_to_copy);
FT_ATOMIC_MEMCPY_PTR_STORE_RELAXED(dest + copied, dest, (size_t)bytes_to_copy);
copied += bytes_to_copy;
}
}
Expand Down
9 changes: 9 additions & 0 deletionsInclude/internal/pycore_pyatomic_ft_wrappers.h
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -112,6 +112,12 @@ extern "C" {
#define FT_ATOMIC_ADD_SSIZE(value, new_value) \
(void)_Py_atomic_add_ssize(&value, new_value)

#define FT_ATOMIC_MEMCPY_PTR_STORE_RELAXED(dest, src, n) \
_Py_atomic_memcpy_ptr_store_relaxed(dest, src, (Py_ssize_t)(n))
#define FT_ATOMIC_MEMMOVE_PTR_STORE_RELAXED(dest, src, n) \
_Py_atomic_memmove_ptr_store_relaxed(dest, src, (Py_ssize_t)(n))


#else
#define FT_ATOMIC_LOAD_PTR(value) value
#define FT_ATOMIC_STORE_PTR(value, new_value) value = new_value
Expand DownExpand Up@@ -160,6 +166,9 @@ extern "C" {
#define FT_ATOMIC_STORE_ULLONG_RELAXED(value, new_value) value = new_value
#define FT_ATOMIC_ADD_SSIZE(value, new_value) (void)(value += new_value)

#define FT_ATOMIC_MEMCPY_PTR_STORE_RELAXED(dest, src, n) memcpy(dest, src, n)
#define FT_ATOMIC_MEMMOVE_PTR_STORE_RELAXED(dest, src, n) memmove(dest, src, n)

#endif

#ifdef __cplusplus
Expand Down
View file
Open in desktop
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
Fix data race and avoid jagged writes in ``list.list_ass_slice``.
26 changes: 9 additions & 17 deletionsObjects/listobject.c
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -817,10 +817,8 @@ list_repeat_lock_held(PyListObject *a, Py_ssize_t n)
_Py_RefcntAdd(*src, n);
*dest++ = *src++;
}
// TODO: _Py_memory_repeat calls are not safe for shared lists in
// GIL_DISABLED builds. (See issue #129069)
_Py_memory_repeat((char *)np->ob_item, sizeof(PyObject *)*output_size,
sizeof(PyObject *)*input_size);
_Py_memory_ptrs_repeat((char *)np->ob_item, sizeof(PyObject *)*output_size,
sizeof(PyObject *)*input_size);
}

Py_SET_SIZE(np, output_size);
Expand DownExpand Up@@ -953,12 +951,10 @@ list_ass_slice_lock_held(PyListObject *a, Py_ssize_t ilow, Py_ssize_t ihigh, PyO
if (d < 0) { /* Delete -d items */
Py_ssize_t tail;
tail = (Py_SIZE(a) - ihigh) * sizeof(PyObject *);
// TODO: these memmove/memcpy calls are not safe for shared lists in
// GIL_DISABLED builds. (See issue #129069)
memmove(&item[ihigh+d], &item[ihigh], tail);
FT_ATOMIC_MEMMOVE_PTR_STORE_RELAXED(&item[ihigh+d], &item[ihigh], tail);
if (list_resize(a, Py_SIZE(a) + d) < 0) {
memmove(&item[ihigh], &item[ihigh+d], tail);
memcpy(&item[ilow], recycle, s);
FT_ATOMIC_MEMMOVE_PTR_STORE_RELAXED(&item[ihigh], &item[ihigh+d], tail);
FT_ATOMIC_MEMCPY_PTR_STORE_RELAXED(&item[ilow], recycle, s);
goto Error;
}
item = a->ob_item;
Expand All@@ -968,10 +964,8 @@ list_ass_slice_lock_held(PyListObject *a, Py_ssize_t ilow, Py_ssize_t ihigh, PyO
if (list_resize(a, k+d) < 0)
goto Error;
item = a->ob_item;
// TODO: these memmove/memcpy calls are not safe for shared lists in
// GIL_DISABLED builds. (See issue #129069)
memmove(&item[ihigh+d], &item[ihigh],
(k - ihigh)*sizeof(PyObject *));
FT_ATOMIC_MEMMOVE_PTR_STORE_RELAXED(&item[ihigh+d], &item[ihigh],
(k - ihigh)*sizeof(PyObject *));
}
for (k = 0; k < n; k++, ilow++) {
PyObject *w = vitem[k];
Expand DownExpand Up@@ -1055,10 +1049,8 @@ list_inplace_repeat_lock_held(PyListObject *self, Py_ssize_t n)
for (Py_ssize_t j = 0; j < input_size; j++) {
_Py_RefcntAdd(items[j], n-1);
}
// TODO: _Py_memory_repeat calls are not safe for shared lists in
// GIL_DISABLED builds. (See issue #129069)
_Py_memory_repeat((char *)items, sizeof(PyObject *)*output_size,
sizeof(PyObject *)*input_size);
_Py_memory_ptrs_repeat((char *)items, sizeof(PyObject *)*output_size,
sizeof(PyObject *)*input_size);
return 0;
}

Expand Down
6 changes: 3 additions & 3 deletionsObjects/tupleobject.c
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -5,7 +5,7 @@
#include "pycore_ceval.h" // _PyEval_GetBuiltin()
#include "pycore_freelist.h" // _Py_FREELIST_PUSH()
#include "pycore_gc.h" // _PyObject_GC_IS_TRACKED()
#include "pycore_list.h" //_Py_memory_repeat()
#include "pycore_list.h" //_Py_memory_ptrs_repeat()
#include "pycore_modsupport.h" // _PyArg_NoKwnames()
#include "pycore_object.h" // _PyObject_GC_TRACK()
#include "pycore_stackref.h" // PyStackRef_AsPyObjectSteal()
Expand DownExpand Up@@ -540,8 +540,8 @@ tuple_repeat(PyObject *self, Py_ssize_t n)
*dest++ = *src++;
}

_Py_memory_repeat((char *)np->ob_item, sizeof(PyObject *)*output_size,
sizeof(PyObject *)*input_size);
_Py_memory_ptrs_repeat((char *)np->ob_item, sizeof(PyObject *)*output_size,
sizeof(PyObject *)*input_size);
}
_PyObject_GC_TRACK(np);
return (PyObject *) np;
Expand Down
Loading

[8]ページ先頭

©2009-2025 Movatter.jp