Skip to content
Closed
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
21 changes: 21 additions & 0 deletions Lib/test/test_free_threading/test_bytes_object.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,27 @@ def reader():

threading_helper.run_concurrently([writer] + [reader] * 4)

def test_racing_bytearray_from_list_replace(self):
# gh-162822: bytearray(list) must not use a list item that another
# thread replaces (and frees) concurrently.
big = 1 << 1000
lst = [big] * 10
done = Event()

def writer():
try:
for n in range(100000):
lst[n % len(lst)] = big + n
finally:
done.set()

def reader():
while not done.is_set():
with self.assertRaises(ValueError):
bytearray(lst)

threading_helper.run_concurrently([writer] + [reader] * 4)


if __name__ == "__main__":
unittest.main()
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
Fix a crash in :class:`bytearray` construction from a :class:`list` in the
:term:`free-threaded build` when another thread concurrently replaces an item
of the list.
69 changes: 43 additions & 26 deletions Objects/bytearrayobject.c
Original file line number Diff line number Diff line change
Expand Up @@ -984,6 +984,42 @@ bytearray_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
return op;
}

/* Fill self from an exact list or tuple of exact ints. Return 0 on success,
-1 on error and 1 if an item is not an exact int, in which case self is left
empty and the caller falls back to the iterator. For a list, the caller
must hold its critical section so that no other thread can replace (and
free) an item while it is read. */
static int
bytearray_init_from_sequence_lock_held(PyByteArrayObject *self, PyObject *arg)
{
Py_ssize_t size = PySequence_Fast_GET_SIZE(arg);
if (PyByteArray_Resize((PyObject *)self, size) < 0) {
return -1;
}
PyObject **items = PySequence_Fast_ITEMS(arg);
char *s = PyByteArray_AS_STRING(self);
for (Py_ssize_t i = 0; i < size; i++) {
int value;
if (!PyLong_CheckExact(items[i])) {
/* Resize to 0 and go through the slow path */
if (Py_SIZE(self) != 0) {
if (PyByteArray_Resize((PyObject *)self, 0) < 0) {
return -1;
}
}
return 1;
}
int rc = _getbytevalue(items[i], &value);
if (!rc) {
/* Don't expose the uninitialized bytes: reset to empty */
(void)PyByteArray_Resize((PyObject *)self, 0);
return -1;
}
s[i] = value;
}
return 0;
}

/*[clinic input]
@vectorcall
bytearray.__init__
Expand Down Expand Up @@ -1114,34 +1150,15 @@ bytearray___init___impl(PyByteArrayObject *self, PyObject *arg,
}

if (PyList_CheckExact(arg) || PyTuple_CheckExact(arg)) {
Py_ssize_t size = PySequence_Fast_GET_SIZE(arg);
if (PyByteArray_Resize((PyObject *)self, size) < 0) {
return -1;
int rc;
Py_BEGIN_CRITICAL_SECTION_SEQUENCE_FAST(arg);
rc = bytearray_init_from_sequence_lock_held(self, arg);
Py_END_CRITICAL_SECTION_SEQUENCE_FAST();
if (rc <= 0) {
return rc;
}
PyObject **items = PySequence_Fast_ITEMS(arg);
char *s = PyByteArray_AS_STRING(self);
for (Py_ssize_t i = 0; i < size; i++) {
int value;
if (!PyLong_CheckExact(items[i])) {
/* Resize to 0 and go through slowpath */
if (Py_SIZE(self) != 0) {
if (PyByteArray_Resize((PyObject *)self, 0) < 0) {
return -1;
}
}
goto slowpath;
}
int rc = _getbytevalue(items[i], &value);
if (!rc) {
/* Don't expose the uninitialized bytes: reset to empty */
(void)PyByteArray_Resize((PyObject *)self, 0);
return -1;
}
s[i] = value;
}
return 0;
// This is not a sequence of exact ints, going to slow path
}
slowpath:
/* Get the iterator */
it = PyObject_GetIter(arg);
if (it == NULL) {
Expand Down
Loading