gh-117657: TSAN Fix races in `PyMember_Get` and `PyMember_Set` for C extensions (GH-123211)

This commit is contained in:
Daniele Parmeggiani 2024-12-03 15:41:53 +01:00 committed by GitHub
parent 84ff1313d0
commit 979bf2489d
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
10 changed files with 735 additions and 50 deletions

View File

@ -321,6 +321,27 @@ _Py_atomic_load_ptr(const void *obj);
static inline int
_Py_atomic_load_int_relaxed(const int *obj);
static inline char
_Py_atomic_load_char_relaxed(const char *obj);
static inline unsigned char
_Py_atomic_load_uchar_relaxed(const unsigned char *obj);
static inline short
_Py_atomic_load_short_relaxed(const short *obj);
static inline unsigned short
_Py_atomic_load_ushort_relaxed(const unsigned short *obj);
static inline long
_Py_atomic_load_long_relaxed(const long *obj);
static inline double
_Py_atomic_load_double_relaxed(const double *obj);
static inline long long
_Py_atomic_load_llong_relaxed(const long long *obj);
static inline int8_t
_Py_atomic_load_int8_relaxed(const int8_t *obj);
@ -458,6 +479,30 @@ static inline void
_Py_atomic_store_ullong_relaxed(unsigned long long *obj,
unsigned long long value);
static inline void
_Py_atomic_store_char_relaxed(char *obj, char value);
static inline void
_Py_atomic_store_uchar_relaxed(unsigned char *obj, unsigned char value);
static inline void
_Py_atomic_store_short_relaxed(short *obj, short value);
static inline void
_Py_atomic_store_ushort_relaxed(unsigned short *obj, unsigned short value);
static inline void
_Py_atomic_store_long_relaxed(long *obj, long value);
static inline void
_Py_atomic_store_float_relaxed(float *obj, float value);
static inline void
_Py_atomic_store_double_relaxed(double *obj, double value);
static inline void
_Py_atomic_store_llong_relaxed(long long *obj, long long value);
// --- _Py_atomic_load_ptr_acquire / _Py_atomic_store_ptr_release ------------

View File

@ -306,6 +306,34 @@ static inline int
_Py_atomic_load_int_relaxed(const int *obj)
{ return __atomic_load_n(obj, __ATOMIC_RELAXED); }
static inline char
_Py_atomic_load_char_relaxed(const char *obj)
{ return __atomic_load_n(obj, __ATOMIC_RELAXED); }
static inline unsigned char
_Py_atomic_load_uchar_relaxed(const unsigned char *obj)
{ return __atomic_load_n(obj, __ATOMIC_RELAXED); }
static inline short
_Py_atomic_load_short_relaxed(const short *obj)
{ return __atomic_load_n(obj, __ATOMIC_RELAXED); }
static inline unsigned short
_Py_atomic_load_ushort_relaxed(const unsigned short *obj)
{ return __atomic_load_n(obj, __ATOMIC_RELAXED); }
static inline long
_Py_atomic_load_long_relaxed(const long *obj)
{ return __atomic_load_n(obj, __ATOMIC_RELAXED); }
static inline float
_Py_atomic_load_float_relaxed(const float *obj)
{ float ret; __atomic_load(obj, &ret, __ATOMIC_RELAXED); return ret; }
static inline double
_Py_atomic_load_double_relaxed(const double *obj)
{ double ret; __atomic_load(obj, &ret, __ATOMIC_RELAXED); return ret; }
static inline int8_t
_Py_atomic_load_int8_relaxed(const int8_t *obj)
{ return __atomic_load_n(obj, __ATOMIC_RELAXED); }
@ -362,6 +390,10 @@ static inline unsigned long long
_Py_atomic_load_ullong_relaxed(const unsigned long long *obj)
{ return __atomic_load_n(obj, __ATOMIC_RELAXED); }
static inline long long
_Py_atomic_load_llong_relaxed(const long long *obj)
{ return __atomic_load_n(obj, __ATOMIC_RELAXED); }
// --- _Py_atomic_store ------------------------------------------------------
@ -485,6 +517,38 @@ _Py_atomic_store_ullong_relaxed(unsigned long long *obj,
unsigned long long value)
{ __atomic_store_n(obj, value, __ATOMIC_RELAXED); }
static inline void
_Py_atomic_store_char_relaxed(char *obj, char value)
{ __atomic_store_n(obj, value, __ATOMIC_RELEASE); }
static inline void
_Py_atomic_store_uchar_relaxed(unsigned char *obj, unsigned char value)
{ __atomic_store_n(obj, value, __ATOMIC_RELAXED); }
static inline void
_Py_atomic_store_short_relaxed(short *obj, short value)
{ __atomic_store_n(obj, value, __ATOMIC_RELAXED); }
static inline void
_Py_atomic_store_ushort_relaxed(unsigned short *obj, unsigned short value)
{ __atomic_store_n(obj, value, __ATOMIC_RELAXED); }
static inline void
_Py_atomic_store_long_relaxed(long *obj, long value)
{ __atomic_store_n(obj, value, __ATOMIC_RELAXED); }
static inline void
_Py_atomic_store_float_relaxed(float *obj, float value)
{ __atomic_store(obj, &value, __ATOMIC_RELAXED); }
static inline void
_Py_atomic_store_double_relaxed(double *obj, double value)
{ __atomic_store(obj, &value, __ATOMIC_RELAXED); }
static inline void
_Py_atomic_store_llong_relaxed(long long *obj, long long value)
{ __atomic_store_n(obj, value, __ATOMIC_RELAXED); }
// --- _Py_atomic_load_ptr_acquire / _Py_atomic_store_ptr_release ------------

View File

@ -634,6 +634,48 @@ _Py_atomic_load_int_relaxed(const int *obj)
return *(volatile int *)obj;
}
static inline char
_Py_atomic_load_char_relaxed(const char *obj)
{
return *(volatile char *)obj;
}
static inline unsigned char
_Py_atomic_load_uchar_relaxed(const unsigned char *obj)
{
return *(volatile unsigned char *)obj;
}
static inline short
_Py_atomic_load_short_relaxed(const short *obj)
{
return *(volatile short *)obj;
}
static inline unsigned short
_Py_atomic_load_ushort_relaxed(const unsigned short *obj)
{
return *(volatile unsigned short *)obj;
}
static inline long
_Py_atomic_load_long_relaxed(const long *obj)
{
return *(volatile long *)obj;
}
static inline float
_Py_atomic_load_float_relaxed(const float *obj)
{
return *(volatile float *)obj;
}
static inline double
_Py_atomic_load_double_relaxed(const double *obj)
{
return *(volatile double *)obj;
}
static inline int8_t
_Py_atomic_load_int8_relaxed(const int8_t *obj)
{
@ -718,6 +760,12 @@ _Py_atomic_load_ullong_relaxed(const unsigned long long *obj)
return *(volatile unsigned long long *)obj;
}
static inline long long
_Py_atomic_load_llong_relaxed(const long long *obj)
{
return *(volatile long long *)obj;
}
// --- _Py_atomic_store ------------------------------------------------------
@ -899,6 +947,60 @@ _Py_atomic_store_ullong_relaxed(unsigned long long *obj,
*(volatile unsigned long long *)obj = value;
}
static inline void
_Py_atomic_store_char_relaxed(char *obj, char value)
{
*(volatile char *)obj = value;
}
static inline void
_Py_atomic_store_uchar_relaxed(unsigned char *obj, unsigned char value)
{
*(volatile unsigned char *)obj = value;
}
static inline void
_Py_atomic_store_short_relaxed(short *obj, short value)
{
*(volatile short *)obj = value;
}
static inline void
_Py_atomic_store_ushort_relaxed(unsigned short *obj, unsigned short value)
{
*(volatile unsigned short *)obj = value;
}
static inline void
_Py_atomic_store_uint_release(unsigned int *obj, unsigned int value)
{
*(volatile unsigned int *)obj = value;
}
static inline void
_Py_atomic_store_long_relaxed(long *obj, long value)
{
*(volatile long *)obj = value;
}
static inline void
_Py_atomic_store_float_relaxed(float *obj, float value)
{
*(volatile float *)obj = value;
}
static inline void
_Py_atomic_store_double_relaxed(double *obj, double value)
{
*(volatile double *)obj = value;
}
static inline void
_Py_atomic_store_llong_relaxed(long long *obj, long long value)
{
*(volatile long long *)obj = value;
}
// --- _Py_atomic_load_ptr_acquire / _Py_atomic_store_ptr_release ------------

View File

@ -515,6 +515,62 @@ _Py_atomic_load_int_relaxed(const int *obj)
memory_order_relaxed);
}
static inline char
_Py_atomic_load_char_relaxed(const char *obj)
{
_Py_USING_STD;
return atomic_load_explicit((const _Atomic(char)*)obj,
memory_order_relaxed);
}
static inline unsigned char
_Py_atomic_load_uchar_relaxed(const unsigned char *obj)
{
_Py_USING_STD;
return atomic_load_explicit((const _Atomic(unsigned char)*)obj,
memory_order_relaxed);
}
static inline short
_Py_atomic_load_short_relaxed(const short *obj)
{
_Py_USING_STD;
return atomic_load_explicit((const _Atomic(short)*)obj,
memory_order_relaxed);
}
static inline unsigned short
_Py_atomic_load_ushort_relaxed(const unsigned short *obj)
{
_Py_USING_STD;
return atomic_load_explicit((const _Atomic(unsigned short)*)obj,
memory_order_relaxed);
}
static inline long
_Py_atomic_load_long_relaxed(const long *obj)
{
_Py_USING_STD;
return atomic_load_explicit((const _Atomic(long)*)obj,
memory_order_relaxed);
}
static inline float
_Py_atomic_load_float_relaxed(const float *obj)
{
_Py_USING_STD;
return atomic_load_explicit((const _Atomic(float)*)obj,
memory_order_relaxed);
}
static inline double
_Py_atomic_load_double_relaxed(const double *obj)
{
_Py_USING_STD;
return atomic_load_explicit((const _Atomic(double)*)obj,
memory_order_relaxed);
}
static inline int8_t
_Py_atomic_load_int8_relaxed(const int8_t *obj)
{
@ -627,6 +683,14 @@ _Py_atomic_load_ullong_relaxed(const unsigned long long *obj)
memory_order_relaxed);
}
static inline long long
_Py_atomic_load_llong_relaxed(const long long *obj)
{
_Py_USING_STD;
return atomic_load_explicit((const _Atomic(long long)*)obj,
memory_order_relaxed);
}
// --- _Py_atomic_store ------------------------------------------------------
@ -852,6 +916,78 @@ _Py_atomic_store_ullong_relaxed(unsigned long long *obj,
memory_order_relaxed);
}
static inline void
_Py_atomic_store_char_relaxed(char *obj, char value)
{
_Py_USING_STD;
atomic_store_explicit((_Atomic(char)*)obj, value,
memory_order_relaxed);
}
static inline void
_Py_atomic_store_uchar_relaxed(unsigned char *obj, unsigned char value)
{
_Py_USING_STD;
atomic_store_explicit((_Atomic(unsigned char)*)obj, value,
memory_order_relaxed);
}
static inline void
_Py_atomic_store_short_relaxed(short *obj, short value)
{
_Py_USING_STD;
atomic_store_explicit((_Atomic(short)*)obj, value,
memory_order_relaxed);
}
static inline void
_Py_atomic_store_ushort_relaxed(unsigned short *obj, unsigned short value)
{
_Py_USING_STD;
atomic_store_explicit((_Atomic(unsigned short)*)obj, value,
memory_order_relaxed);
}
static inline void
_Py_atomic_store_uint_release(unsigned int *obj, unsigned int value)
{
_Py_USING_STD;
atomic_store_explicit((_Atomic(unsigned int)*)obj, value,
memory_order_relaxed);
}
static inline void
_Py_atomic_store_long_relaxed(long *obj, long value)
{
_Py_USING_STD;
atomic_store_explicit((_Atomic(long)*)obj, value,
memory_order_relaxed);
}
static inline void
_Py_atomic_store_float_relaxed(float *obj, float value)
{
_Py_USING_STD;
atomic_store_explicit((_Atomic(float)*)obj, value,
memory_order_relaxed);
}
static inline void
_Py_atomic_store_double_relaxed(double *obj, double value)
{
_Py_USING_STD;
atomic_store_explicit((_Atomic(double)*)obj, value,
memory_order_relaxed);
}
static inline void
_Py_atomic_store_llong_relaxed(long long *obj, long long value)
{
_Py_USING_STD;
atomic_store_explicit((_Atomic(long long)*)obj, value,
memory_order_relaxed);
}
// --- _Py_atomic_load_ptr_acquire / _Py_atomic_store_ptr_release ------------

View File

@ -61,6 +61,54 @@ extern "C" {
_Py_atomic_store_uint16_relaxed(&value, new_value)
#define FT_ATOMIC_STORE_UINT32_RELAXED(value, new_value) \
_Py_atomic_store_uint32_relaxed(&value, new_value)
#define FT_ATOMIC_STORE_CHAR_RELAXED(value, new_value) \
_Py_atomic_store_char_relaxed(&value, new_value)
#define FT_ATOMIC_LOAD_CHAR_RELAXED(value) \
_Py_atomic_load_char_relaxed(&value)
#define FT_ATOMIC_STORE_UCHAR_RELAXED(value, new_value) \
_Py_atomic_store_uchar_relaxed(&value, new_value)
#define FT_ATOMIC_LOAD_UCHAR_RELAXED(value) \
_Py_atomic_load_uchar_relaxed(&value)
#define FT_ATOMIC_STORE_SHORT_RELAXED(value, new_value) \
_Py_atomic_store_short_relaxed(&value, new_value)
#define FT_ATOMIC_LOAD_SHORT_RELAXED(value) \
_Py_atomic_load_short_relaxed(&value)
#define FT_ATOMIC_STORE_USHORT_RELAXED(value, new_value) \
_Py_atomic_store_ushort_relaxed(&value, new_value)
#define FT_ATOMIC_LOAD_USHORT_RELAXED(value) \
_Py_atomic_load_ushort_relaxed(&value)
#define FT_ATOMIC_STORE_INT_RELAXED(value, new_value) \
_Py_atomic_store_int_relaxed(&value, new_value)
#define FT_ATOMIC_LOAD_INT_RELAXED(value) \
_Py_atomic_load_int_relaxed(&value)
#define FT_ATOMIC_STORE_UINT_RELAXED(value, new_value) \
_Py_atomic_store_uint_relaxed(&value, new_value)
#define FT_ATOMIC_LOAD_UINT_RELAXED(value) \
_Py_atomic_load_uint_relaxed(&value)
#define FT_ATOMIC_STORE_LONG_RELAXED(value, new_value) \
_Py_atomic_store_long_relaxed(&value, new_value)
#define FT_ATOMIC_LOAD_LONG_RELAXED(value) \
_Py_atomic_load_long_relaxed(&value)
#define FT_ATOMIC_STORE_ULONG_RELAXED(value, new_value) \
_Py_atomic_store_ulong_relaxed(&value, new_value)
#define FT_ATOMIC_STORE_SSIZE_RELAXED(value, new_value) \
_Py_atomic_store_ssize_relaxed(&value, new_value)
#define FT_ATOMIC_STORE_FLOAT_RELAXED(value, new_value) \
_Py_atomic_store_float_relaxed(&value, new_value)
#define FT_ATOMIC_LOAD_FLOAT_RELAXED(value) \
_Py_atomic_load_float_relaxed(&value)
#define FT_ATOMIC_STORE_DOUBLE_RELAXED(value, new_value) \
_Py_atomic_store_double_relaxed(&value, new_value)
#define FT_ATOMIC_LOAD_DOUBLE_RELAXED(value) \
_Py_atomic_load_double_relaxed(&value)
#define FT_ATOMIC_STORE_LLONG_RELAXED(value, new_value) \
_Py_atomic_store_llong_relaxed(&value, new_value)
#define FT_ATOMIC_LOAD_LLONG_RELAXED(value) \
_Py_atomic_load_llong_relaxed(&value)
#define FT_ATOMIC_STORE_ULLONG_RELAXED(value, new_value) \
_Py_atomic_store_ullong_relaxed(&value, new_value)
#define FT_ATOMIC_LOAD_ULLONG_RELAXED(value) \
_Py_atomic_load_ullong_relaxed(&value)
#else
#define FT_ATOMIC_LOAD_PTR(value) value
@ -68,7 +116,6 @@ extern "C" {
#define FT_ATOMIC_LOAD_SSIZE(value) value
#define FT_ATOMIC_LOAD_SSIZE_ACQUIRE(value) value
#define FT_ATOMIC_LOAD_SSIZE_RELAXED(value) value
#define FT_ATOMIC_STORE_PTR(value, new_value) value = new_value
#define FT_ATOMIC_LOAD_PTR_ACQUIRE(value) value
#define FT_ATOMIC_LOAD_UINTPTR_ACQUIRE(value) value
#define FT_ATOMIC_LOAD_PTR_RELAXED(value) value
@ -85,6 +132,30 @@ extern "C" {
#define FT_ATOMIC_STORE_UINT8_RELAXED(value, new_value) value = new_value
#define FT_ATOMIC_STORE_UINT16_RELAXED(value, new_value) value = new_value
#define FT_ATOMIC_STORE_UINT32_RELAXED(value, new_value) value = new_value
#define FT_ATOMIC_LOAD_CHAR_RELAXED(value) value
#define FT_ATOMIC_STORE_CHAR_RELAXED(value, new_value) value = new_value
#define FT_ATOMIC_LOAD_UCHAR_RELAXED(value) value
#define FT_ATOMIC_STORE_UCHAR_RELAXED(value, new_value) value = new_value
#define FT_ATOMIC_LOAD_SHORT_RELAXED(value) value
#define FT_ATOMIC_STORE_SHORT_RELAXED(value, new_value) value = new_value
#define FT_ATOMIC_LOAD_USHORT_RELAXED(value) value
#define FT_ATOMIC_STORE_USHORT_RELAXED(value, new_value) value = new_value
#define FT_ATOMIC_LOAD_INT_RELAXED(value) value
#define FT_ATOMIC_STORE_INT_RELAXED(value, new_value) value = new_value
#define FT_ATOMIC_LOAD_UINT_RELAXED(value) value
#define FT_ATOMIC_STORE_UINT_RELAXED(value, new_value) value = new_value
#define FT_ATOMIC_LOAD_LONG_RELAXED(value) value
#define FT_ATOMIC_STORE_LONG_RELAXED(value, new_value) value = new_value
#define FT_ATOMIC_STORE_ULONG_RELAXED(value, new_value) value = new_value
#define FT_ATOMIC_STORE_SSIZE_RELAXED(value, new_value) value = new_value
#define FT_ATOMIC_LOAD_FLOAT_RELAXED(value) value
#define FT_ATOMIC_STORE_FLOAT_RELAXED(value, new_value) value = new_value
#define FT_ATOMIC_LOAD_DOUBLE_RELAXED(value) value
#define FT_ATOMIC_STORE_DOUBLE_RELAXED(value, new_value) value = new_value
#define FT_ATOMIC_LOAD_LLONG_RELAXED(value) value
#define FT_ATOMIC_STORE_LLONG_RELAXED(value, new_value) value = new_value
#define FT_ATOMIC_LOAD_ULLONG_RELAXED(value) value
#define FT_ATOMIC_STORE_ULLONG_RELAXED(value, new_value) value = new_value
#endif

View File

@ -25,6 +25,7 @@
'test_threading_local',
'test_threadsignals',
'test_weakref',
'test_free_threading.test_slots',
]

View File

@ -37,6 +37,9 @@ def _make_test_object(cls):
9.99999,# T_FLOAT
10.1010101010, # T_DOUBLE
"hi", # T_STRING_INPLACE
12, # T_LONGLONG
13, # T_ULONGLONG
"c", # T_CHAR
)
@ -162,6 +165,16 @@ def test_inplace_string(self):
self.assertRaises(TypeError, setattr, ts, "T_STRING_INPLACE", "s")
self.assertRaises(TypeError, delattr, ts, "T_STRING_INPLACE")
def test_char(self):
ts = self.ts
self.assertEqual(ts.T_CHAR, "c")
ts.T_CHAR = "z"
self.assertEqual(ts.T_CHAR, "z")
self.assertRaises(TypeError, setattr, ts, "T_CHAR", "")
self.assertRaises(TypeError, setattr, ts, "T_CHAR", b"a")
self.assertRaises(TypeError, setattr, ts, "T_CHAR", bytearray(b"b"))
self.assertRaises(TypeError, delattr, ts, "T_STRING_INPLACE")
class ReadWriteTests_OldAPI(ReadWriteTests, unittest.TestCase):
cls = _test_structmembersType_OldAPI

View File

@ -1,3 +1,4 @@
import _testcapi
import threading
from test.support import threading_helper
from unittest import TestCase
@ -41,3 +42,246 @@ def reader():
assert 0 <= eggs <= iters
run_in_threads([writer, reader, reader, reader])
def test_T_BOOL(self):
spam_old = _testcapi._test_structmembersType_OldAPI()
spam_new = _testcapi._test_structmembersType_NewAPI()
def writer():
for _ in range(1_000):
# different code paths for True and False
spam_old.T_BOOL = True
spam_new.T_BOOL = True
spam_old.T_BOOL = False
spam_new.T_BOOL = False
def reader():
for _ in range(1_000):
spam_old.T_BOOL
spam_new.T_BOOL
run_in_threads([writer, reader])
def test_T_BYTE(self):
spam_old = _testcapi._test_structmembersType_OldAPI()
spam_new = _testcapi._test_structmembersType_NewAPI()
def writer():
for _ in range(1_000):
spam_old.T_BYTE = 0
spam_new.T_BYTE = 0
def reader():
for _ in range(1_000):
spam_old.T_BYTE
spam_new.T_BYTE
run_in_threads([writer, reader])
def test_T_UBYTE(self):
spam_old = _testcapi._test_structmembersType_OldAPI()
spam_new = _testcapi._test_structmembersType_NewAPI()
def writer():
for _ in range(1_000):
spam_old.T_UBYTE = 0
spam_new.T_UBYTE = 0
def reader():
for _ in range(1_000):
spam_old.T_UBYTE
spam_new.T_UBYTE
run_in_threads([writer, reader])
def test_T_SHORT(self):
spam_old = _testcapi._test_structmembersType_OldAPI()
spam_new = _testcapi._test_structmembersType_NewAPI()
def writer():
for _ in range(1_000):
spam_old.T_SHORT = 0
spam_new.T_SHORT = 0
def reader():
for _ in range(1_000):
spam_old.T_SHORT
spam_new.T_SHORT
run_in_threads([writer, reader])
def test_T_USHORT(self):
spam_old = _testcapi._test_structmembersType_OldAPI()
spam_new = _testcapi._test_structmembersType_NewAPI()
def writer():
for _ in range(1_000):
spam_old.T_USHORT = 0
spam_new.T_USHORT = 0
def reader():
for _ in range(1_000):
spam_old.T_USHORT
spam_new.T_USHORT
run_in_threads([writer, reader])
def test_T_INT(self):
spam_old = _testcapi._test_structmembersType_OldAPI()
spam_new = _testcapi._test_structmembersType_NewAPI()
def writer():
for _ in range(1_000):
spam_old.T_INT = 0
spam_new.T_INT = 0
def reader():
for _ in range(1_000):
spam_old.T_INT
spam_new.T_INT
run_in_threads([writer, reader])
def test_T_UINT(self):
spam_old = _testcapi._test_structmembersType_OldAPI()
spam_new = _testcapi._test_structmembersType_NewAPI()
def writer():
for _ in range(1_000):
spam_old.T_UINT = 0
spam_new.T_UINT = 0
def reader():
for _ in range(1_000):
spam_old.T_UINT
spam_new.T_UINT
run_in_threads([writer, reader])
def test_T_LONG(self):
spam_old = _testcapi._test_structmembersType_OldAPI()
spam_new = _testcapi._test_structmembersType_NewAPI()
def writer():
for _ in range(1_000):
spam_old.T_LONG = 0
spam_new.T_LONG = 0
def reader():
for _ in range(1_000):
spam_old.T_LONG
spam_new.T_LONG
run_in_threads([writer, reader])
def test_T_ULONG(self):
spam_old = _testcapi._test_structmembersType_OldAPI()
spam_new = _testcapi._test_structmembersType_NewAPI()
def writer():
for _ in range(1_000):
spam_old.T_ULONG = 0
spam_new.T_ULONG = 0
def reader():
for _ in range(1_000):
spam_old.T_ULONG
spam_new.T_ULONG
run_in_threads([writer, reader])
def test_T_PYSSIZET(self):
spam_old = _testcapi._test_structmembersType_OldAPI()
spam_new = _testcapi._test_structmembersType_NewAPI()
def writer():
for _ in range(1_000):
spam_old.T_PYSSIZET = 0
spam_new.T_PYSSIZET = 0
def reader():
for _ in range(1_000):
spam_old.T_PYSSIZET
spam_new.T_PYSSIZET
run_in_threads([writer, reader])
def test_T_FLOAT(self):
spam_old = _testcapi._test_structmembersType_OldAPI()
spam_new = _testcapi._test_structmembersType_NewAPI()
def writer():
for _ in range(1_000):
spam_old.T_FLOAT = 0.0
spam_new.T_FLOAT = 0.0
def reader():
for _ in range(1_000):
spam_old.T_FLOAT
spam_new.T_FLOAT
run_in_threads([writer, reader])
def test_T_DOUBLE(self):
spam_old = _testcapi._test_structmembersType_OldAPI()
spam_new = _testcapi._test_structmembersType_NewAPI()
def writer():
for _ in range(1_000):
spam_old.T_DOUBLE = 0.0
spam_new.T_DOUBLE = 0.0
def reader():
for _ in range(1_000):
spam_old.T_DOUBLE
spam_new.T_DOUBLE
run_in_threads([writer, reader])
def test_T_LONGLONG(self):
spam_old = _testcapi._test_structmembersType_OldAPI()
spam_new = _testcapi._test_structmembersType_NewAPI()
def writer():
for _ in range(1_000):
spam_old.T_LONGLONG = 0
spam_new.T_LONGLONG = 0
def reader():
for _ in range(1_000):
spam_old.T_LONGLONG
spam_new.T_LONGLONG
run_in_threads([writer, reader])
def test_T_ULONGLONG(self):
spam_old = _testcapi._test_structmembersType_OldAPI()
spam_new = _testcapi._test_structmembersType_NewAPI()
def writer():
for _ in range(1_000):
spam_old.T_ULONGLONG = 0
spam_new.T_ULONGLONG = 0
def reader():
for _ in range(1_000):
spam_old.T_ULONGLONG
spam_new.T_ULONGLONG
run_in_threads([writer, reader])
def test_T_CHAR(self):
spam_old = _testcapi._test_structmembersType_OldAPI()
spam_new = _testcapi._test_structmembersType_NewAPI()
def writer():
for _ in range(1_000):
spam_old.T_CHAR = "c"
spam_new.T_CHAR = "c"
def reader():
for _ in range(1_000):
spam_old.T_CHAR
spam_new.T_CHAR
run_in_threads([writer, reader])

View File

@ -22,6 +22,7 @@ typedef struct {
char inplace_member[6];
long long longlong_member;
unsigned long long ulonglong_member;
char char_member;
} all_structmembers;
typedef struct {
@ -46,6 +47,7 @@ static struct PyMemberDef test_members_newapi[] = {
{"T_STRING_INPLACE", Py_T_STRING_INPLACE, offsetof(test_structmembers, structmembers.inplace_member), 0, NULL},
{"T_LONGLONG", Py_T_LONGLONG, offsetof(test_structmembers, structmembers.longlong_member), 0, NULL},
{"T_ULONGLONG", Py_T_ULONGLONG, offsetof(test_structmembers, structmembers.ulonglong_member), 0, NULL},
{"T_CHAR", Py_T_CHAR, offsetof(test_structmembers, structmembers.char_member), 0, NULL},
{NULL}
};
@ -56,9 +58,9 @@ test_structmembers_new(PyTypeObject *type, PyObject *args, PyObject *kwargs)
"T_BOOL", "T_BYTE", "T_UBYTE", "T_SHORT", "T_USHORT",
"T_INT", "T_UINT", "T_LONG", "T_ULONG", "T_PYSSIZET",
"T_FLOAT", "T_DOUBLE", "T_STRING_INPLACE",
"T_LONGLONG", "T_ULONGLONG",
"T_LONGLONG", "T_ULONGLONG", "T_CHAR",
NULL};
static const char fmt[] = "|bbBhHiIlknfds#LK";
static const char fmt[] = "|bbBhHiIlknfds#LKC";
test_structmembers *ob;
const char *s = NULL;
Py_ssize_t string_len = 0;
@ -82,7 +84,8 @@ test_structmembers_new(PyTypeObject *type, PyObject *args, PyObject *kwargs)
&ob->structmembers.double_member,
&s, &string_len,
&ob->structmembers.longlong_member,
&ob->structmembers.ulonglong_member))
&ob->structmembers.ulonglong_member,
&ob->structmembers.char_member))
{
Py_DECREF(ob);
return NULL;
@ -132,6 +135,7 @@ static struct PyMemberDef test_members[] = {
{"T_STRING_INPLACE", T_STRING_INPLACE, offsetof(test_structmembers, structmembers.inplace_member), 0, NULL},
{"T_LONGLONG", T_LONGLONG, offsetof(test_structmembers, structmembers.longlong_member), 0, NULL},
{"T_ULONGLONG", T_ULONGLONG, offsetof(test_structmembers, structmembers.ulonglong_member), 0, NULL},
{"T_CHAR", T_CHAR, offsetof(test_structmembers, structmembers.char_member), 0, NULL},
{NULL}
};

View File

@ -34,40 +34,40 @@ PyMember_GetOne(const char *obj_addr, PyMemberDef *l)
const char* addr = obj_addr + l->offset;
switch (l->type) {
case Py_T_BOOL:
v = PyBool_FromLong(*(char*)addr);
v = PyBool_FromLong(FT_ATOMIC_LOAD_CHAR_RELAXED(*(char*)addr));
break;
case Py_T_BYTE:
v = PyLong_FromLong(*(char*)addr);
v = PyLong_FromLong(FT_ATOMIC_LOAD_CHAR_RELAXED(*(char*)addr));
break;
case Py_T_UBYTE:
v = PyLong_FromUnsignedLong(*(unsigned char*)addr);
v = PyLong_FromUnsignedLong(FT_ATOMIC_LOAD_UCHAR_RELAXED(*(unsigned char*)addr));
break;
case Py_T_SHORT:
v = PyLong_FromLong(*(short*)addr);
v = PyLong_FromLong(FT_ATOMIC_LOAD_SHORT_RELAXED(*(short*)addr));
break;
case Py_T_USHORT:
v = PyLong_FromUnsignedLong(*(unsigned short*)addr);
v = PyLong_FromUnsignedLong(FT_ATOMIC_LOAD_USHORT_RELAXED(*(unsigned short*)addr));
break;
case Py_T_INT:
v = PyLong_FromLong(*(int*)addr);
v = PyLong_FromLong(FT_ATOMIC_LOAD_INT_RELAXED(*(int*)addr));
break;
case Py_T_UINT:
v = PyLong_FromUnsignedLong(*(unsigned int*)addr);
v = PyLong_FromUnsignedLong(FT_ATOMIC_LOAD_UINT_RELAXED(*(unsigned int*)addr));
break;
case Py_T_LONG:
v = PyLong_FromLong(*(long*)addr);
v = PyLong_FromLong(FT_ATOMIC_LOAD_LONG_RELAXED(*(long*)addr));
break;
case Py_T_ULONG:
v = PyLong_FromUnsignedLong(*(unsigned long*)addr);
v = PyLong_FromUnsignedLong(FT_ATOMIC_LOAD_ULONG_RELAXED(*(unsigned long*)addr));
break;
case Py_T_PYSSIZET:
v = PyLong_FromSsize_t(*(Py_ssize_t*)addr);
v = PyLong_FromSsize_t(FT_ATOMIC_LOAD_SSIZE_RELAXED(*(Py_ssize_t*)addr));
break;
case Py_T_FLOAT:
v = PyFloat_FromDouble((double)*(float*)addr);
v = PyFloat_FromDouble((double)FT_ATOMIC_LOAD_FLOAT_RELAXED(*(float*)addr));
break;
case Py_T_DOUBLE:
v = PyFloat_FromDouble(*(double*)addr);
v = PyFloat_FromDouble(FT_ATOMIC_LOAD_DOUBLE_RELAXED(*(double*)addr));
break;
case Py_T_STRING:
if (*(char**)addr == NULL) {
@ -79,9 +79,11 @@ PyMember_GetOne(const char *obj_addr, PyMemberDef *l)
case Py_T_STRING_INPLACE:
v = PyUnicode_FromString((char*)addr);
break;
case Py_T_CHAR:
v = PyUnicode_FromStringAndSize((char*)addr, 1);
case Py_T_CHAR: {
char char_val = FT_ATOMIC_LOAD_CHAR_RELAXED(*addr);
v = PyUnicode_FromStringAndSize(&char_val, 1);
break;
}
case _Py_T_OBJECT:
v = *(PyObject **)addr;
if (v == NULL)
@ -104,10 +106,10 @@ PyMember_GetOne(const char *obj_addr, PyMemberDef *l)
#endif
break;
case Py_T_LONGLONG:
v = PyLong_FromLongLong(*(long long *)addr);
v = PyLong_FromLongLong(FT_ATOMIC_LOAD_LLONG_RELAXED(*(long long *)addr));
break;
case Py_T_ULONGLONG:
v = PyLong_FromUnsignedLongLong(*(unsigned long long *)addr);
v = PyLong_FromUnsignedLongLong(FT_ATOMIC_LOAD_ULLONG_RELAXED(*(unsigned long long *)addr));
break;
case _Py_T_NONE:
// doesn't require free-threading code path
@ -169,16 +171,16 @@ PyMember_SetOne(char *addr, PyMemberDef *l, PyObject *v)
return -1;
}
if (v == Py_True)
*(char*)addr = (char) 1;
FT_ATOMIC_STORE_CHAR_RELAXED(*(char*)addr, 1);
else
*(char*)addr = (char) 0;
FT_ATOMIC_STORE_CHAR_RELAXED(*(char*)addr, 0);
break;
}
case Py_T_BYTE:{
long long_val = PyLong_AsLong(v);
if ((long_val == -1) && PyErr_Occurred())
return -1;
*(char*)addr = (char)long_val;
FT_ATOMIC_STORE_CHAR_RELAXED(*(char*)addr, (char)long_val);
/* XXX: For compatibility, only warn about truncations
for now. */
if ((long_val > CHAR_MAX) || (long_val < CHAR_MIN))
@ -189,7 +191,7 @@ PyMember_SetOne(char *addr, PyMemberDef *l, PyObject *v)
long long_val = PyLong_AsLong(v);
if ((long_val == -1) && PyErr_Occurred())
return -1;
*(unsigned char*)addr = (unsigned char)long_val;
FT_ATOMIC_STORE_UCHAR_RELAXED(*(unsigned char*)addr, (unsigned char)long_val);
if ((long_val > UCHAR_MAX) || (long_val < 0))
WARN("Truncation of value to unsigned char");
break;
@ -198,7 +200,7 @@ PyMember_SetOne(char *addr, PyMemberDef *l, PyObject *v)
long long_val = PyLong_AsLong(v);
if ((long_val == -1) && PyErr_Occurred())
return -1;
*(short*)addr = (short)long_val;
FT_ATOMIC_STORE_SHORT_RELAXED(*(short*)addr, (short)long_val);
if ((long_val > SHRT_MAX) || (long_val < SHRT_MIN))
WARN("Truncation of value to short");
break;
@ -207,7 +209,7 @@ PyMember_SetOne(char *addr, PyMemberDef *l, PyObject *v)
long long_val = PyLong_AsLong(v);
if ((long_val == -1) && PyErr_Occurred())
return -1;
*(unsigned short*)addr = (unsigned short)long_val;
FT_ATOMIC_STORE_USHORT_RELAXED(*(unsigned short*)addr, (unsigned short)long_val);
if ((long_val > USHRT_MAX) || (long_val < 0))
WARN("Truncation of value to unsigned short");
break;
@ -216,7 +218,7 @@ PyMember_SetOne(char *addr, PyMemberDef *l, PyObject *v)
long long_val = PyLong_AsLong(v);
if ((long_val == -1) && PyErr_Occurred())
return -1;
*(int *)addr = (int)long_val;
FT_ATOMIC_STORE_INT_RELAXED(*(int *)addr, (int)long_val);
if ((long_val > INT_MAX) || (long_val < INT_MIN))
WARN("Truncation of value to int");
break;
@ -234,7 +236,7 @@ PyMember_SetOne(char *addr, PyMemberDef *l, PyObject *v)
if (long_val == -1 && PyErr_Occurred()) {
return -1;
}
*(unsigned int *)addr = (unsigned int)(unsigned long)long_val;
FT_ATOMIC_STORE_UINT_RELAXED(*(unsigned int *)addr, (unsigned int)(unsigned long)long_val);
WARN("Writing negative value into unsigned field");
}
else {
@ -243,19 +245,20 @@ PyMember_SetOne(char *addr, PyMemberDef *l, PyObject *v)
if (ulong_val == (unsigned long)-1 && PyErr_Occurred()) {
return -1;
}
*(unsigned int*)addr = (unsigned int)ulong_val;
FT_ATOMIC_STORE_UINT_RELAXED(*(unsigned int *)addr, (unsigned int)ulong_val);
if (ulong_val > UINT_MAX) {
WARN("Truncation of value to unsigned int");
}
}
break;
}
case Py_T_LONG:{
*(long*)addr = PyLong_AsLong(v);
if ((*(long*)addr == -1) && PyErr_Occurred())
case Py_T_LONG: {
const long long_val = PyLong_AsLong(v);
if ((long_val == -1) && PyErr_Occurred())
return -1;
FT_ATOMIC_STORE_LONG_RELAXED(*(long*)addr, long_val);
break;
}
}
case Py_T_ULONG: {
/* XXX: For compatibility, accept negative int values
as well. */
@ -269,7 +272,7 @@ PyMember_SetOne(char *addr, PyMemberDef *l, PyObject *v)
if (long_val == -1 && PyErr_Occurred()) {
return -1;
}
*(unsigned long *)addr = (unsigned long)long_val;
FT_ATOMIC_STORE_ULONG_RELAXED(*(unsigned long *)addr, (unsigned long)long_val);
WARN("Writing negative value into unsigned field");
}
else {
@ -278,29 +281,31 @@ PyMember_SetOne(char *addr, PyMemberDef *l, PyObject *v)
if (ulong_val == (unsigned long)-1 && PyErr_Occurred()) {
return -1;
}
*(unsigned long*)addr = ulong_val;
FT_ATOMIC_STORE_ULONG_RELAXED(*(unsigned long *)addr, ulong_val);
}
break;
}
case Py_T_PYSSIZET:{
*(Py_ssize_t*)addr = PyLong_AsSsize_t(v);
if ((*(Py_ssize_t*)addr == (Py_ssize_t)-1)
&& PyErr_Occurred())
return -1;
case Py_T_PYSSIZET: {
const Py_ssize_t ssize_val = PyLong_AsSsize_t(v);
if ((ssize_val == (Py_ssize_t)-1) && PyErr_Occurred())
return -1;
FT_ATOMIC_STORE_SSIZE_RELAXED(*(Py_ssize_t*)addr, ssize_val);
break;
}
}
case Py_T_FLOAT:{
double double_val = PyFloat_AsDouble(v);
if ((double_val == -1) && PyErr_Occurred())
return -1;
*(float*)addr = (float)double_val;
FT_ATOMIC_STORE_FLOAT_RELAXED(*(float*)addr, (float)double_val);
break;
}
case Py_T_DOUBLE:
*(double*)addr = PyFloat_AsDouble(v);
if ((*(double*)addr == -1) && PyErr_Occurred())
case Py_T_DOUBLE: {
const double double_val = PyFloat_AsDouble(v);
if ((double_val == -1) && PyErr_Occurred())
return -1;
FT_ATOMIC_STORE_DOUBLE_RELAXED(*(double *) addr, double_val);
break;
}
case _Py_T_OBJECT:
case Py_T_OBJECT_EX:
Py_BEGIN_CRITICAL_SECTION(obj);
@ -318,7 +323,7 @@ PyMember_SetOne(char *addr, PyMemberDef *l, PyObject *v)
PyErr_BadArgument();
return -1;
}
*(char*)addr = string[0];
FT_ATOMIC_STORE_CHAR_RELAXED(*(char*)addr, string[0]);
break;
}
case Py_T_STRING:
@ -326,10 +331,10 @@ PyMember_SetOne(char *addr, PyMemberDef *l, PyObject *v)
PyErr_SetString(PyExc_TypeError, "readonly attribute");
return -1;
case Py_T_LONGLONG:{
long long value;
*(long long*)addr = value = PyLong_AsLongLong(v);
long long value = PyLong_AsLongLong(v);
if ((value == -1) && PyErr_Occurred())
return -1;
FT_ATOMIC_STORE_LLONG_RELAXED(*(long long*)addr, value);
break;
}
case Py_T_ULONGLONG: {
@ -343,7 +348,7 @@ PyMember_SetOne(char *addr, PyMemberDef *l, PyObject *v)
if (long_val == -1 && PyErr_Occurred()) {
return -1;
}
*(unsigned long long *)addr = (unsigned long long)(long long)long_val;
FT_ATOMIC_STORE_ULLONG_RELAXED(*(unsigned long long *)addr, (unsigned long long)(long long)long_val);
WARN("Writing negative value into unsigned field");
}
else {
@ -352,7 +357,7 @@ PyMember_SetOne(char *addr, PyMemberDef *l, PyObject *v)
if (ulonglong_val == (unsigned long long)-1 && PyErr_Occurred()) {
return -1;
}
*(unsigned long long*)addr = ulonglong_val;
FT_ATOMIC_STORE_ULLONG_RELAXED(*(unsigned long long *)addr, ulonglong_val);
}
break;
}