Use Darling reserved key definitions for TLS keys

Our previous choices for per-thread-WD keys were conflicting with the
keys reserved for the Swift runtime and causing crashes for Swift apps.

Instead, let's use some keys from the newly-reserved Darling range for
pthread keys (200-209). This range seems to be otherwise unused, so it
should be fine for us to use it.
This commit is contained in:
Ariel Abreu 2023-10-04 23:30:57 -04:00
parent b2bd57725b
commit 99020685f1
No known key found for this signature in database
GPG Key ID: 5B88AAAF4280706F
2 changed files with 20 additions and 9 deletions

View File

@ -1,29 +1,29 @@
#ifndef _PER_THREAD_WD_H
#define _PER_THREAD_WD_H
#include <pthread/tsd_private.h>
#include "../common_at.h"
#include "../tsd.h"
// We use the following static TSD keys (see tsd_private.h):
// 100 = is 101 set? (Because 0 could be a valid FD, theoretically.)
// 101 = per thread wd file descriptor
// We use the following static TSD keys (see `../tsd.h` and `pthread/tsd_private.h`):
// __PTK_DARLING_PERTHREAD_WD_IS_SET = is __PTK_DARLING_PERTHREAD_WD_FD set? (Because 0 could be a valid FD, theoretically.)
// __PTK_DARLING_PERTHREAD_WD_FD = per thread wd file descriptor
//
// NOTE: The file descriptor in 101 will get leaked unless __pthread_fchdir(-1)
// NOTE: The file descriptor in __PTK_DARLING_PERTHREAD_WD_FD will get leaked unless __pthread_fchdir(-1)
// is called, but this is such a rare scenario that we disregard it.
// It is be too hard to fix, because static TSD keys (such as the ones we use)
// don't have destructors.
static inline int get_perthread_wd(void)
{
if (_pthread_getspecific_direct(100))
return _pthread_getspecific_direct(101);
if (_pthread_getspecific_direct(__PTK_DARLING_PERTHREAD_WD_IS_SET))
return _pthread_getspecific_direct(__PTK_DARLING_PERTHREAD_WD_FD);
else
return LINUX_AT_FDCWD;
}
static inline void set_perthread_wd(int fd)
{
_pthread_setspecific_direct(100, 1);
_pthread_setspecific_direct(101, fd);
_pthread_setspecific_direct(__PTK_DARLING_PERTHREAD_WD_IS_SET, 1);
_pthread_setspecific_direct(__PTK_DARLING_PERTHREAD_WD_FD, fd);
}
#endif

View File

@ -0,0 +1,11 @@
#ifndef _DARLING_EMULATION_TSD_H_
#define _DARLING_EMULATION_TSD_H_
#include <pthread/tsd_private.h>
#define __PTK_DARLING_XTRACE_TLS __PTK_DARLING_KEY0
#define __PTK_DARLING_PERTHREAD_WD_IS_SET __PTK_DARLING_KEY1
#define __PTK_DARLING_PERTHREAD_WD_FD __PTK_DARLING_KEY2
// other __PTK_DARLING_KEYs are currently unused
#endif // _DARLING_EMULATION_TSD_H_