Skip to content
Draft
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
44 changes: 44 additions & 0 deletions Include/internal/pycore_bytesobject.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,50 @@ extern PyObject* _PyBytes_FormatEx(
* reference rather than modifying its first argument in place. */
extern PyObject* _PyBytes_Concat(PyObject *a, PyObject *b);

/* Copy a bytearray while holding its lock. */
static inline PyObject *
_PyBytes_FromByteArray(PyObject *op)
{
PyObject *bytes;
Py_BEGIN_CRITICAL_SECTION(op);
bytes = PyBytes_FromStringAndSize(PyByteArray_AS_STRING(op),
PyByteArray_GET_SIZE(op));
Py_END_CRITICAL_SECTION();
return bytes;
}

/* If the bytearray has exactly one byte, store it in *p. Return the size
* of the bytearray. */
static inline Py_ssize_t
_PyByteArray_GetByte(PyObject *op, char *p)
{
Py_ssize_t size;
Py_BEGIN_CRITICAL_SECTION(op);
size = PyByteArray_GET_SIZE(op);
if (size == 1) {
*p = PyByteArray_AS_STRING(op)[0];
}
Py_END_CRITICAL_SECTION();
return size;
}

/* Copy at most maxlen bytes of the bytearray into dest, which may overlap
* the bytearray's own storage. Return the size of the bytearray, which can
* be larger than maxlen. */
static inline Py_ssize_t
_PyByteArray_CopyTo(PyObject *op, char *dest, Py_ssize_t maxlen)
{
Py_ssize_t size;
Py_BEGIN_CRITICAL_SECTION(op);
size = PyByteArray_GET_SIZE(op);
Py_ssize_t n = Py_MIN(size, maxlen);
if (n > 0) {
memmove(dest, PyByteArray_AS_STRING(op), n);
}
Py_END_CRITICAL_SECTION();
return size;
}

extern PyObject* _PyBytes_FromHex(
PyObject *string,
int use_bytearray);
Expand Down
1 change: 1 addition & 0 deletions Include/internal/pycore_long.h
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,7 @@ static inline PyObject* _PyLong_FromUnsignedChar(unsigned char i)
PyAPI_DATA(double) _PyLong_Frexp(PyLongObject *a, int64_t *e);

extern PyObject* _PyLong_FromBytes(const char *, Py_ssize_t, int);
extern PyObject* _PyLong_FromByteArrayString(PyObject *, int);

// _PyLong_DivmodNear. Given integers a and b, compute the nearest
// integer q to the exact quotient a / b, rounding to the nearest even integer
Expand Down
100 changes: 43 additions & 57 deletions Lib/test/clinic.test.c
Original file line number Diff line number Diff line change
Expand Up @@ -561,14 +561,13 @@ test_char_converter(PyObject *module, PyObject *const *args, Py_ssize_t nargs)
a = PyBytes_AS_STRING(args[0])[0];
}
else if (PyByteArray_Check(args[0])) {
if (PyByteArray_GET_SIZE(args[0]) != 1) {
Py_ssize_t a_size = _PyByteArray_GetByte(args[0], &a);
if (a_size != 1) {
PyErr_Format(PyExc_TypeError,
"test_char_converter(): argument 1 must be a byte string of length 1, "
"not a bytearray object of length %zd",
PyByteArray_GET_SIZE(args[0]));
"not a bytearray object of length %zd", a_size);
goto exit;
}
a = PyByteArray_AS_STRING(args[0])[0];
}
else {
_PyArg_BadArgument("test_char_converter", "argument 1", "a byte string of length 1", args[0]);
Expand All @@ -588,14 +587,13 @@ test_char_converter(PyObject *module, PyObject *const *args, Py_ssize_t nargs)
b = PyBytes_AS_STRING(args[1])[0];
}
else if (PyByteArray_Check(args[1])) {
if (PyByteArray_GET_SIZE(args[1]) != 1) {
Py_ssize_t b_size = _PyByteArray_GetByte(args[1], &b);
if (b_size != 1) {
PyErr_Format(PyExc_TypeError,
"test_char_converter(): argument 2 must be a byte string of length 1, "
"not a bytearray object of length %zd",
PyByteArray_GET_SIZE(args[1]));
"not a bytearray object of length %zd", b_size);
goto exit;
}
b = PyByteArray_AS_STRING(args[1])[0];
}
else {
_PyArg_BadArgument("test_char_converter", "argument 2", "a byte string of length 1", args[1]);
Expand All @@ -615,14 +613,13 @@ test_char_converter(PyObject *module, PyObject *const *args, Py_ssize_t nargs)
c = PyBytes_AS_STRING(args[2])[0];
}
else if (PyByteArray_Check(args[2])) {
if (PyByteArray_GET_SIZE(args[2]) != 1) {
Py_ssize_t c_size = _PyByteArray_GetByte(args[2], &c);
if (c_size != 1) {
PyErr_Format(PyExc_TypeError,
"test_char_converter(): argument 3 must be a byte string of length 1, "
"not a bytearray object of length %zd",
PyByteArray_GET_SIZE(args[2]));
"not a bytearray object of length %zd", c_size);
goto exit;
}
c = PyByteArray_AS_STRING(args[2])[0];
}
else {
_PyArg_BadArgument("test_char_converter", "argument 3", "a byte string of length 1", args[2]);
Expand All @@ -642,14 +639,13 @@ test_char_converter(PyObject *module, PyObject *const *args, Py_ssize_t nargs)
d = PyBytes_AS_STRING(args[3])[0];
}
else if (PyByteArray_Check(args[3])) {
if (PyByteArray_GET_SIZE(args[3]) != 1) {
Py_ssize_t d_size = _PyByteArray_GetByte(args[3], &d);
if (d_size != 1) {
PyErr_Format(PyExc_TypeError,
"test_char_converter(): argument 4 must be a byte string of length 1, "
"not a bytearray object of length %zd",
PyByteArray_GET_SIZE(args[3]));
"not a bytearray object of length %zd", d_size);
goto exit;
}
d = PyByteArray_AS_STRING(args[3])[0];
}
else {
_PyArg_BadArgument("test_char_converter", "argument 4", "a byte string of length 1", args[3]);
Expand All @@ -669,14 +665,13 @@ test_char_converter(PyObject *module, PyObject *const *args, Py_ssize_t nargs)
e = PyBytes_AS_STRING(args[4])[0];
}
else if (PyByteArray_Check(args[4])) {
if (PyByteArray_GET_SIZE(args[4]) != 1) {
Py_ssize_t e_size = _PyByteArray_GetByte(args[4], &e);
if (e_size != 1) {
PyErr_Format(PyExc_TypeError,
"test_char_converter(): argument 5 must be a byte string of length 1, "
"not a bytearray object of length %zd",
PyByteArray_GET_SIZE(args[4]));
"not a bytearray object of length %zd", e_size);
goto exit;
}
e = PyByteArray_AS_STRING(args[4])[0];
}
else {
_PyArg_BadArgument("test_char_converter", "argument 5", "a byte string of length 1", args[4]);
Expand All @@ -696,14 +691,13 @@ test_char_converter(PyObject *module, PyObject *const *args, Py_ssize_t nargs)
f = PyBytes_AS_STRING(args[5])[0];
}
else if (PyByteArray_Check(args[5])) {
if (PyByteArray_GET_SIZE(args[5]) != 1) {
Py_ssize_t f_size = _PyByteArray_GetByte(args[5], &f);
if (f_size != 1) {
PyErr_Format(PyExc_TypeError,
"test_char_converter(): argument 6 must be a byte string of length 1, "
"not a bytearray object of length %zd",
PyByteArray_GET_SIZE(args[5]));
"not a bytearray object of length %zd", f_size);
goto exit;
}
f = PyByteArray_AS_STRING(args[5])[0];
}
else {
_PyArg_BadArgument("test_char_converter", "argument 6", "a byte string of length 1", args[5]);
Expand All @@ -723,14 +717,13 @@ test_char_converter(PyObject *module, PyObject *const *args, Py_ssize_t nargs)
g = PyBytes_AS_STRING(args[6])[0];
}
else if (PyByteArray_Check(args[6])) {
if (PyByteArray_GET_SIZE(args[6]) != 1) {
Py_ssize_t g_size = _PyByteArray_GetByte(args[6], &g);
if (g_size != 1) {
PyErr_Format(PyExc_TypeError,
"test_char_converter(): argument 7 must be a byte string of length 1, "
"not a bytearray object of length %zd",
PyByteArray_GET_SIZE(args[6]));
"not a bytearray object of length %zd", g_size);
goto exit;
}
g = PyByteArray_AS_STRING(args[6])[0];
}
else {
_PyArg_BadArgument("test_char_converter", "argument 7", "a byte string of length 1", args[6]);
Expand All @@ -750,14 +743,13 @@ test_char_converter(PyObject *module, PyObject *const *args, Py_ssize_t nargs)
h = PyBytes_AS_STRING(args[7])[0];
}
else if (PyByteArray_Check(args[7])) {
if (PyByteArray_GET_SIZE(args[7]) != 1) {
Py_ssize_t h_size = _PyByteArray_GetByte(args[7], &h);
if (h_size != 1) {
PyErr_Format(PyExc_TypeError,
"test_char_converter(): argument 8 must be a byte string of length 1, "
"not a bytearray object of length %zd",
PyByteArray_GET_SIZE(args[7]));
"not a bytearray object of length %zd", h_size);
goto exit;
}
h = PyByteArray_AS_STRING(args[7])[0];
}
else {
_PyArg_BadArgument("test_char_converter", "argument 8", "a byte string of length 1", args[7]);
Expand All @@ -777,14 +769,13 @@ test_char_converter(PyObject *module, PyObject *const *args, Py_ssize_t nargs)
i = PyBytes_AS_STRING(args[8])[0];
}
else if (PyByteArray_Check(args[8])) {
if (PyByteArray_GET_SIZE(args[8]) != 1) {
Py_ssize_t i_size = _PyByteArray_GetByte(args[8], &i);
if (i_size != 1) {
PyErr_Format(PyExc_TypeError,
"test_char_converter(): argument 9 must be a byte string of length 1, "
"not a bytearray object of length %zd",
PyByteArray_GET_SIZE(args[8]));
"not a bytearray object of length %zd", i_size);
goto exit;
}
i = PyByteArray_AS_STRING(args[8])[0];
}
else {
_PyArg_BadArgument("test_char_converter", "argument 9", "a byte string of length 1", args[8]);
Expand All @@ -804,14 +795,13 @@ test_char_converter(PyObject *module, PyObject *const *args, Py_ssize_t nargs)
j = PyBytes_AS_STRING(args[9])[0];
}
else if (PyByteArray_Check(args[9])) {
if (PyByteArray_GET_SIZE(args[9]) != 1) {
Py_ssize_t j_size = _PyByteArray_GetByte(args[9], &j);
if (j_size != 1) {
PyErr_Format(PyExc_TypeError,
"test_char_converter(): argument 10 must be a byte string of length 1, "
"not a bytearray object of length %zd",
PyByteArray_GET_SIZE(args[9]));
"not a bytearray object of length %zd", j_size);
goto exit;
}
j = PyByteArray_AS_STRING(args[9])[0];
}
else {
_PyArg_BadArgument("test_char_converter", "argument 10", "a byte string of length 1", args[9]);
Expand All @@ -831,14 +821,13 @@ test_char_converter(PyObject *module, PyObject *const *args, Py_ssize_t nargs)
k = PyBytes_AS_STRING(args[10])[0];
}
else if (PyByteArray_Check(args[10])) {
if (PyByteArray_GET_SIZE(args[10]) != 1) {
Py_ssize_t k_size = _PyByteArray_GetByte(args[10], &k);
if (k_size != 1) {
PyErr_Format(PyExc_TypeError,
"test_char_converter(): argument 11 must be a byte string of length 1, "
"not a bytearray object of length %zd",
PyByteArray_GET_SIZE(args[10]));
"not a bytearray object of length %zd", k_size);
goto exit;
}
k = PyByteArray_AS_STRING(args[10])[0];
}
else {
_PyArg_BadArgument("test_char_converter", "argument 11", "a byte string of length 1", args[10]);
Expand All @@ -858,14 +847,13 @@ test_char_converter(PyObject *module, PyObject *const *args, Py_ssize_t nargs)
l = PyBytes_AS_STRING(args[11])[0];
}
else if (PyByteArray_Check(args[11])) {
if (PyByteArray_GET_SIZE(args[11]) != 1) {
Py_ssize_t l_size = _PyByteArray_GetByte(args[11], &l);
if (l_size != 1) {
PyErr_Format(PyExc_TypeError,
"test_char_converter(): argument 12 must be a byte string of length 1, "
"not a bytearray object of length %zd",
PyByteArray_GET_SIZE(args[11]));
"not a bytearray object of length %zd", l_size);
goto exit;
}
l = PyByteArray_AS_STRING(args[11])[0];
}
else {
_PyArg_BadArgument("test_char_converter", "argument 12", "a byte string of length 1", args[11]);
Expand All @@ -885,14 +873,13 @@ test_char_converter(PyObject *module, PyObject *const *args, Py_ssize_t nargs)
m = PyBytes_AS_STRING(args[12])[0];
}
else if (PyByteArray_Check(args[12])) {
if (PyByteArray_GET_SIZE(args[12]) != 1) {
Py_ssize_t m_size = _PyByteArray_GetByte(args[12], &m);
if (m_size != 1) {
PyErr_Format(PyExc_TypeError,
"test_char_converter(): argument 13 must be a byte string of length 1, "
"not a bytearray object of length %zd",
PyByteArray_GET_SIZE(args[12]));
"not a bytearray object of length %zd", m_size);
goto exit;
}
m = PyByteArray_AS_STRING(args[12])[0];
}
else {
_PyArg_BadArgument("test_char_converter", "argument 13", "a byte string of length 1", args[12]);
Expand All @@ -912,14 +899,13 @@ test_char_converter(PyObject *module, PyObject *const *args, Py_ssize_t nargs)
n = PyBytes_AS_STRING(args[13])[0];
}
else if (PyByteArray_Check(args[13])) {
if (PyByteArray_GET_SIZE(args[13]) != 1) {
Py_ssize_t n_size = _PyByteArray_GetByte(args[13], &n);
if (n_size != 1) {
PyErr_Format(PyExc_TypeError,
"test_char_converter(): argument 14 must be a byte string of length 1, "
"not a bytearray object of length %zd",
PyByteArray_GET_SIZE(args[13]));
"not a bytearray object of length %zd", n_size);
goto exit;
}
n = PyByteArray_AS_STRING(args[13])[0];
}
else {
_PyArg_BadArgument("test_char_converter", "argument 14", "a byte string of length 1", args[13]);
Expand All @@ -936,7 +922,7 @@ static PyObject *
test_char_converter_impl(PyObject *module, char a, char b, char c, char d,
char e, char f, char g, char h, char i, char j,
char k, char l, char m, char n)
/*[clinic end generated code: output=6503d15448e1d4c4 input=e42330417a44feac]*/
/*[clinic end generated code: output=804e25f65fb146d8 input=e42330417a44feac]*/


/*[clinic input]
Expand Down
3 changes: 3 additions & 0 deletions Lib/test/test_clinic.py
Original file line number Diff line number Diff line change
Expand Up @@ -4736,6 +4736,9 @@ def test_char_converter(self):
chars = [b'1', b'2', b'3', b'4', b'5', b'6', b'7', b'8', b'9', b'0', b'a', b'b', b'c', b'd']
expected = tuple(ord(c) for c in chars)
self.assertEqual(ac_tester.char_converter(*chars), expected)
self.assertEqual(ac_tester.char_converter(*map(bytearray, chars)), expected)
with self.assertRaisesRegex(TypeError, 'bytearray object of length 2'):
ac_tester.char_converter(bytearray(b'ab'))

def test_unsigned_char_converter(self):
from _testcapi import UCHAR_MAX
Expand Down
2 changes: 2 additions & 0 deletions Lib/test/test_format.py
Original file line number Diff line number Diff line change
Expand Up @@ -451,6 +451,8 @@ def __bytes__(self):
fb = FakeBytes()
testcommon(b"%b", b"abc", b"abc")
testcommon(b"%b", bytearray(b"def"), b"def")
testcommon(b"%4.2b", bytearray(b"def"), b" de")
testcommon(b"%-4.0b", bytearray(b"def"), b" ")
testcommon(b"%b", fb, b"123")
testcommon(b"%b", memoryview(b"abc"), b"abc")
# # %s is an alias for %b -- should only be used for Py2/3 code
Expand Down
28 changes: 28 additions & 0 deletions Lib/test/test_free_threading/test_bytes_object.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import struct
import unittest
from threading import Thread, Barrier, Event
from test.support import threading_helper
Expand Down Expand Up @@ -54,6 +55,33 @@ def reader():

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

def test_read_bytearray_during_resize(self):
# Each read must see one complete slice assignment, even if the
# writer replaces the backing storage.
short = b'1'
long = b'2' * 512 # Below the minimum configurable int digit limit.
data = bytearray(short)
pack_string = struct.Struct('1024s').pack
pack_pascal = struct.Struct('1024p').pack
strings = (pack_string(short), pack_string(long))
pascals = (pack_pascal(short), pack_pascal(long))
numbers = (int(short), int(long))

def writer():
for _ in range(2000):
data[:] = long
data[:] = short

def reader():
for _ in range(2000):
self.assertIn(b'%b' % data, (short, long))
self.assertIn(pack_string(data), strings)
self.assertIn(pack_pascal(data), pascals)
self.assertIn(int(data), numbers)
self.assertIn(int(data, 10), numbers)

threading_helper.run_concurrently([writer, reader, reader])


if __name__ == "__main__":
unittest.main()
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
Fix unsafe reads of :class:`bytearray` storage during concurrent mutation
in free-threaded builds. Also prevent use-after-free when :func:`compile`
or :class:`int` calls Python code that resizes its :class:`bytearray` input.
Loading
Loading