Skip to content
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 our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

gh-117657: TSAN Fix races in PyMember_Get and PyMember_Set, for C extensions #123211

Merged
merged 32 commits into from
Dec 3, 2024
Merged
Show file tree
Hide file tree
Changes from 23 commits
Commits
Show all changes
32 commits
Select commit Hold shift + click to select a range
d63eccb
gh-117657: TSAN Fix races in `PyMember_Get` and `PyMember_Set`, for C…
dpdani Aug 21, 2024
c2d3d41
exercise race in T_CHAR as well
dpdani Aug 21, 2024
f6ba4a3
signed char races
dpdani Aug 22, 2024
d9614b7
unsigned char race
dpdani Aug 22, 2024
1659849
unsigned char race 👀
dpdani Aug 22, 2024
4cfa619
unsigned char race 👀
dpdani Aug 22, 2024
107a9f5
signed short race
dpdani Aug 22, 2024
e20b9d1
unsigned short race
dpdani Aug 22, 2024
86a0945
signed int race
dpdani Aug 22, 2024
1a07031
who uses MSC anyways?
dpdani Aug 22, 2024
16f61d8
unsigned int races
dpdani Aug 22, 2024
cf25726
👀
dpdani Aug 22, 2024
d41132c
signed long race
dpdani Aug 22, 2024
2212605
unsigned long race
dpdani Aug 22, 2024
74616d6
ssizet race
dpdani Aug 22, 2024
23d10c6
float race
dpdani Aug 22, 2024
ae37a8e
double race
dpdani Aug 22, 2024
bce02d0
T_CHAR
dpdani Aug 22, 2024
afe2504
signed long long race
dpdani Aug 22, 2024
3dfaab8
unsigned long long race
dpdani Aug 22, 2024
50a7bb6
fix return type
dpdani Aug 22, 2024
416e0bb
arm64 does not have 128-bit integers, I suppose
dpdani Aug 22, 2024
4bd6927
at this point I'm just making guesses
dpdani Aug 22, 2024
a4c094b
scope
dpdani Aug 29, 2024
267f995
must 0-initialize
dpdani Aug 29, 2024
c006e1d
FT_ATOMIC_STORE_CHAR_RELEASE -> FT_ATOMIC_STORE_CHAR_RELAXED
dpdani Aug 29, 2024
488dea5
remaining release -> relaxed
dpdani Aug 29, 2024
b3b07d5
default build
dpdani Aug 29, 2024
761257e
Apply suggestions from code review
dpdani Sep 22, 2024
d045747
missing load
dpdani Sep 22, 2024
c1b4c35
fixes
dpdani Nov 2, 2024
6c5cec5
Merge branch 'main' into gh-117657-tsan-pymember-c-ext
colesbury Dec 3, 2024
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
52 changes: 52 additions & 0 deletions Include/cpython/pyatomic.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);
picnixz marked this conversation as resolved.
Show resolved Hide resolved

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);

Expand Down Expand Up @@ -481,6 +502,37 @@ _Py_atomic_store_ssize_release(Py_ssize_t *obj, Py_ssize_t value);
static inline void
_Py_atomic_store_int_release(int *obj, int value);

static inline void
_Py_atomic_store_char_release(char *obj, char value);

static inline void
_Py_atomic_store_uchar_release(unsigned char *obj, unsigned char value);

static inline void
_Py_atomic_store_short_release(short *obj, short value);

static inline void
_Py_atomic_store_ushort_release(unsigned short *obj, unsigned short value);

static inline void
_Py_atomic_store_uint_release(unsigned int *obj, unsigned int value);

static inline void
_Py_atomic_store_long_release(long *obj, long value);

static inline void
_Py_atomic_store_float_release(float *obj, float value);

static inline void
_Py_atomic_store_double_release(double *obj, double value);

static inline void
_Py_atomic_store_llong_release(long long *obj, long long value);

static inline void
_Py_atomic_store_ullong_release(unsigned long long *obj,
unsigned long long value);

static inline int
_Py_atomic_load_int_acquire(const int *obj);

Expand Down
73 changes: 73 additions & 0 deletions Include/cpython/pyatomic_gcc.h
Original file line number Diff line number Diff line change
Expand Up @@ -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); }
Expand Down Expand Up @@ -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 ------------------------------------------------------

Expand Down Expand Up @@ -508,6 +540,47 @@ static inline void
_Py_atomic_store_int_release(int *obj, int value)
{ __atomic_store_n(obj, value, __ATOMIC_RELEASE); }

static inline void
_Py_atomic_store_char_release(char *obj, char value)
{ __atomic_store_n(obj, value, __ATOMIC_RELEASE); }

static inline void
_Py_atomic_store_uchar_release(unsigned char *obj, unsigned char value)
{ __atomic_store_n(obj, value, __ATOMIC_RELEASE); }

static inline void
_Py_atomic_store_short_release(short *obj, short value)
{ __atomic_store_n(obj, value, __ATOMIC_RELEASE); }

static inline void
_Py_atomic_store_ushort_release(unsigned short *obj, unsigned short value)
{ __atomic_store_n(obj, value, __ATOMIC_RELEASE); }

static inline void
_Py_atomic_store_uint_release(unsigned int *obj, unsigned int value)
{ __atomic_store_n(obj, value, __ATOMIC_RELEASE); }

static inline void
_Py_atomic_store_long_release(long *obj, long value)
{ __atomic_store_n(obj, value, __ATOMIC_RELEASE); }

static inline void
_Py_atomic_store_float_release(float *obj, float value)
{ __atomic_store(obj, &value, __ATOMIC_RELEASE); }

static inline void
_Py_atomic_store_double_release(double *obj, double value)
{ __atomic_store(obj, &value, __ATOMIC_RELEASE); }

static inline void
_Py_atomic_store_llong_release(long long *obj, long long value)
{ __atomic_store_n(obj, value, __ATOMIC_RELEASE); }

static inline void
_Py_atomic_store_ullong_release(unsigned long long *obj,
unsigned long long value)
{ __atomic_store_n(obj, value, __ATOMIC_RELEASE); }

static inline void
_Py_atomic_store_ssize_release(Py_ssize_t *obj, Py_ssize_t value)
{ __atomic_store_n(obj, value, __ATOMIC_RELEASE); }
Expand Down
178 changes: 178 additions & 0 deletions Include/cpython/pyatomic_msc.h
Original file line number Diff line number Diff line change
Expand Up @@ -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)
{
Expand Down Expand Up @@ -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 ------------------------------------------------------

Expand Down Expand Up @@ -964,6 +1012,136 @@ _Py_atomic_store_int_release(int *obj, int value)
#endif
}

static inline void
_Py_atomic_store_char_release(char *obj, char value)
{
#if defined(_M_X64) || defined(_M_IX86)
*(char volatile *)obj = value;
#elif defined(_M_ARM64)
_Py_atomic_ASSERT_ARG_TYPE(unsigned __int8);
__stlr8((unsigned __int8 volatile *)obj, (unsigned __int8)value);
#else
# error "no implementation of _Py_atomic_store_char_release"
#endif
}

static inline void
_Py_atomic_store_uchar_release(unsigned char *obj, unsigned char value)
{
#if defined(_M_X64) || defined(_M_IX86)
*(unsigned char volatile *)obj = value;
#elif defined(_M_ARM64)
_Py_atomic_ASSERT_ARG_TYPE(unsigned __int8);
__stlr8((unsigned __int8 volatile *)obj, (unsigned __int8)value);
#else
# error "no implementation of _Py_atomic_store_uchar_release"
#endif
}

static inline void
_Py_atomic_store_short_release(short *obj, short value)
{
#if defined(_M_X64) || defined(_M_IX86)
*(short volatile *)obj = value;
#elif defined(_M_ARM64)
_Py_atomic_ASSERT_ARG_TYPE(unsigned __int16);
__stlr16((unsigned __int16 volatile *)obj, (unsigned __int16)value);
#else
# error "no implementation of _Py_atomic_store_short_release"
#endif
}

static inline void
_Py_atomic_store_ushort_release(unsigned short *obj, unsigned short value)
{
#if defined(_M_X64) || defined(_M_IX86)
*(unsigned short volatile *)obj = value;
#elif defined(_M_ARM64)
_Py_atomic_ASSERT_ARG_TYPE(unsigned __int16);
__stlr16((unsigned __int16 volatile *)obj, (unsigned __int16)value);
#else
# error "no implementation of _Py_atomic_store_ushort_release"
#endif
}

static inline void
_Py_atomic_store_uint_release(unsigned int *obj, unsigned int value)
{
#if defined(_M_X64) || defined(_M_IX86)
*(unsigned int volatile *)obj = value;
#elif defined(_M_ARM64)
_Py_atomic_ASSERT_ARG_TYPE(unsigned __int32);
__stlr32((unsigned __int32 volatile *)obj, (unsigned __int32)value);
#else
# error "no implementation of _Py_atomic_store_uint_release"
#endif
}

static inline void
_Py_atomic_store_long_release(long *obj, long value)
{
#if defined(_M_X64) || defined(_M_IX86)
*(long volatile *)obj = value;
#elif defined(_M_ARM64)
_Py_atomic_ASSERT_ARG_TYPE(unsigned __int32);
__stlr32((unsigned __int32 volatile *)obj, (unsigned __int32)value);
#else
# error "no implementation of _Py_atomic_store_long_release"
#endif
}

static inline void
_Py_atomic_store_float_release(float *obj, float value)
{
#if defined(_M_X64) || defined(_M_IX86)
*(float volatile *)obj = value;
#elif defined(_M_ARM64)
_Py_atomic_ASSERT_ARG_TYPE(unsigned __int32);
__stlr32((unsigned __int32 volatile *)obj, (unsigned __int32)value);
#else
# error "no implementation of _Py_atomic_store_float_release"
#endif
}

static inline void
_Py_atomic_store_double_release(double *obj, double value)
{
#if defined(_M_X64) || defined(_M_IX86)
*(double volatile *)obj = value;
#elif defined(_M_ARM64)
_Py_atomic_ASSERT_ARG_TYPE(unsigned __int64);
__stlr64((unsigned __int64 volatile *)obj, (unsigned __int64)value);
#else
# error "no implementation of _Py_atomic_store_double_release"
#endif
}

static inline void
_Py_atomic_store_llong_release(long long *obj, long long value)
{
#if defined(_M_X64) || defined(_M_IX86)
*(long long volatile *)obj = value;
#elif defined(_M_ARM64)
_Py_atomic_ASSERT_ARG_TYPE(unsigned __int64);
__stlr64((unsigned __int64 volatile *)obj, (unsigned __int64)value);
#else
# error "no implementation of _Py_atomic_store_llong_release"
#endif
}

static inline void
_Py_atomic_store_ullong_release(unsigned long long *obj, unsigned long long value)
{
#if defined(_M_X64) || defined(_M_IX86)
*(unsigned long long volatile *)obj = value;
#elif defined(_M_ARM64)
_Py_atomic_ASSERT_ARG_TYPE(__int64);
__stlr64((unsigned __int64 volatile *)obj, (unsigned __int64)value);
#else
# error "no implementation of _Py_atomic_store_ullong_release"
#endif
}

static inline void
_Py_atomic_store_ssize_release(Py_ssize_t *obj, Py_ssize_t value)
{
Expand Down
Loading
Loading