Skip to content

Commit 7212306

Browse files
authored
gh-130115: fix thread identifiers for 32-bit musl (#130391)
CPython's pthread-based thread identifier relies on pthread_t being able to be represented as an unsigned integer type. This is true in most Linux libc implementations where it's defined as an unsigned long, however musl typedefs it as a struct *. If the pointer has the high bit set and is cast to PyThread_ident_t, the resultant value can be sign-extended [0]. This can cause issues when comparing against threading._MainThread's identifier. The main thread's identifier value is retrieved via _get_main_thread_ident which is backed by an unsigned long which truncates sign extended bits. >>> hex(threading.main_thread().ident) '0xb6f33f3c' >>> hex(threading.current_thread().ident) '0xffffffffb6f33f3c' Work around this by conditionally compiling in some code for non-glibc based Linux platforms that are at risk of sign-extension to return a PyLong based on the main thread's unsigned long thread identifier if the current thread is the main thread. [0]: https://gcc.gnu.org/onlinedocs/gcc-14.2.0/gcc/Arrays-and-pointers-implementation.html --------- Signed-off-by: Vincent Fazio <[email protected]>
1 parent 345baa7 commit 7212306

File tree

2 files changed

+22
-9
lines changed

2 files changed

+22
-9
lines changed
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Fix an issue with thread identifiers being sign-extended on some platforms.

Python/thread_pthread.h

Lines changed: 21 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -306,16 +306,33 @@ do_start_joinable_thread(void (*func)(void *), void *arg, pthread_t* out_id)
306306
return 0;
307307
}
308308

309+
/* Helper to convert pthread_t to PyThread_ident_t. POSIX allows pthread_t to be
310+
non-arithmetic, e.g., musl typedefs it as a pointer. */
311+
static PyThread_ident_t
312+
_pthread_t_to_ident(pthread_t value) {
313+
// Cast through an integer type of the same size to avoid sign-extension.
314+
#if SIZEOF_PTHREAD_T == SIZEOF_VOID_P
315+
return (uintptr_t) value;
316+
#elif SIZEOF_PTHREAD_T == SIZEOF_LONG
317+
return (unsigned long) value;
318+
#elif SIZEOF_PTHREAD_T == SIZEOF_INT
319+
return (unsigned int) value;
320+
#elif SIZEOF_PTHREAD_T == SIZEOF_LONG_LONG
321+
return (unsigned long long) value;
322+
#else
323+
#error "Unsupported SIZEOF_PTHREAD_T value"
324+
#endif
325+
}
326+
309327
int
310328
PyThread_start_joinable_thread(void (*func)(void *), void *arg,
311329
PyThread_ident_t* ident, PyThread_handle_t* handle) {
312330
pthread_t th = (pthread_t) 0;
313331
if (do_start_joinable_thread(func, arg, &th)) {
314332
return -1;
315333
}
316-
*ident = (PyThread_ident_t) th;
334+
*ident = _pthread_t_to_ident(th);
317335
*handle = (PyThread_handle_t) th;
318-
assert(th == (pthread_t) *ident);
319336
assert(th == (pthread_t) *handle);
320337
return 0;
321338
}
@@ -328,11 +345,7 @@ PyThread_start_new_thread(void (*func)(void *), void *arg)
328345
return PYTHREAD_INVALID_THREAD_ID;
329346
}
330347
pthread_detach(th);
331-
#if SIZEOF_PTHREAD_T <= SIZEOF_LONG
332-
return (unsigned long) th;
333-
#else
334-
return (unsigned long) *(unsigned long *) &th;
335-
#endif
348+
return (unsigned long) _pthread_t_to_ident(th);;
336349
}
337350

338351
int
@@ -357,8 +370,7 @@ PyThread_get_thread_ident_ex(void) {
357370
if (!initialized)
358371
PyThread_init_thread();
359372
threadid = pthread_self();
360-
assert(threadid == (pthread_t) (PyThread_ident_t) threadid);
361-
return (PyThread_ident_t) threadid;
373+
return _pthread_t_to_ident(threadid);
362374
}
363375

364376
unsigned long

0 commit comments

Comments
 (0)