diff options
author | Nicolas Trangez <ikke@nicolast.be> | 2022-10-26 17:56:05 +0200 |
---|---|---|
committer | Marge Bot <ben+marge-bot@smart-cactus.org> | 2022-11-01 12:47:58 -0400 |
commit | 13b5f102b3049e177220bd6d867a33d5c8864b12 (patch) | |
tree | ef865f25f044395de350c5c07a7c600ca3279e09 /rts/posix | |
parent | 8ee8b41805a886cb839c02a5da23b9a8a404003a (diff) | |
download | haskell-13b5f102b3049e177220bd6d867a33d5c8864b12.tar.gz |
rts: fix lifetime of `start_thread`s `name` value
Since, unlike the code in ee0deb8054da2^, usage of the `name` value
passed to `createOSThread` now outlives said function's lifetime, and
could hence be released by the caller by the time the new thread runs
`start_thread`, it needs to be copied.
See: https://gitlab.haskell.org/ghc/ghc/-/commit/ee0deb8054da2a597fc5624469b4c44fd769ada2#note_460080
See: https://gitlab.haskell.org/ghc/ghc/-/merge_requests/9066
Diffstat (limited to 'rts/posix')
-rw-r--r-- | rts/posix/OSThreads.c | 22 |
1 files changed, 14 insertions, 8 deletions
diff --git a/rts/posix/OSThreads.c b/rts/posix/OSThreads.c index 535c79cac3..2987e8162b 100644 --- a/rts/posix/OSThreads.c +++ b/rts/posix/OSThreads.c @@ -198,20 +198,24 @@ struct ThreadDesc { static void * start_thread (void *param) { - struct ThreadDesc desc = *(struct ThreadDesc *) param; - stgFree(param); + struct ThreadDesc *desc = (struct ThreadDesc *) param; + OSThreadProc *startProc = desc->startProc; + void *startParam = desc->param; #if defined(HAVE_PTHREAD_SET_NAME_NP) - pthread_set_name_np(pthread_self(), desc.name); + pthread_set_name_np(pthread_self(), desc->name); #elif defined(HAVE_PTHREAD_SETNAME_NP) - pthread_setname_np(pthread_self(), desc.name); + pthread_setname_np(pthread_self(), desc->name); #elif defined(HAVE_PTHREAD_SETNAME_NP_DARWIN) - pthread_setname_np(desc.name); + pthread_setname_np(desc->name); #elif defined(HAVE_PTHREAD_SETNAME_NP_NETBSD) - pthread_setname_np(pthread_self(), "%s", desc.name); + pthread_setname_np(pthread_self(), "%s", desc->name); #endif - return desc.startProc(desc.param); + stgFree(desc->name); + stgFree(desc); + + return startProc(startParam); } int @@ -221,12 +225,14 @@ createOSThread (OSThreadId* pId, const char *name, struct ThreadDesc *desc = stgMallocBytes(sizeof(struct ThreadDesc), "createOSThread"); desc->startProc = startProc; desc->param = param; - desc->name = name; + desc->name = stgMallocBytes(strlen(name) + 1, "createOSThread"); + strcpy(desc->name, name); int result = pthread_create(pId, NULL, start_thread, desc); if (!result) { pthread_detach(*pId); } else { + stgFree(desc->name); stgFree(desc); } return result; |