Module Name: src Committed By: hannken Date: Sat Jun 8 08:01:49 UTC 2024
Modified Files: src/lib/libpthread: pthread.c Log Message: Fix resource leak in pthread_create(). Pthread field "pt_lwpctl" must not be accessed after _lwp_exit() as the kernel may free it, attach it to another thread and change its "lc_curcpu". The "pthread__deadqueue" will fill up with pthreads with an invalid "pt_lwpctl" and allocated stack that never get reused. Replace the test "lc_curcpu == LWPCTL_CPU_EXITED" with test "_lwp_kill(newthread->pt_lid, 0) == -1 && errno == ESRCH" to make sure this thread has finished its _lwp_exit(). PR lib/57831 "Memory leaks in libpthread/libc" To generate a diff of this commit: cvs rdiff -u -r1.184 -r1.185 src/lib/libpthread/pthread.c Please note that diffs are not public domain; they are subject to the copyright notices on the relevant files.
Modified files: Index: src/lib/libpthread/pthread.c diff -u src/lib/libpthread/pthread.c:1.184 src/lib/libpthread/pthread.c:1.185 --- src/lib/libpthread/pthread.c:1.184 Tue Nov 28 02:54:33 2023 +++ src/lib/libpthread/pthread.c Sat Jun 8 08:01:49 2024 @@ -1,4 +1,4 @@ -/* $NetBSD: pthread.c,v 1.184 2023/11/28 02:54:33 riastradh Exp $ */ +/* $NetBSD: pthread.c,v 1.185 2024/06/08 08:01:49 hannken Exp $ */ /*- * Copyright (c) 2001, 2002, 2003, 2006, 2007, 2008, 2020 @@ -31,7 +31,7 @@ */ #include <sys/cdefs.h> -__RCSID("$NetBSD: pthread.c,v 1.184 2023/11/28 02:54:33 riastradh Exp $"); +__RCSID("$NetBSD: pthread.c,v 1.185 2024/06/08 08:01:49 hannken Exp $"); #define __EXPOSE_STACK 1 @@ -462,9 +462,9 @@ pthread_create(pthread_t *thread, const if (!PTQ_EMPTY(&pthread__deadqueue)) { pthread_mutex_lock(&pthread__deadqueue_lock); PTQ_FOREACH(newthread, &pthread__deadqueue, pt_deadq) { - /* Still busily exiting, or finished? */ - if (newthread->pt_lwpctl->lc_curcpu == - LWPCTL_CPU_EXITED) + /* Still running? */ + if (_lwp_kill(newthread->pt_lid, 0) == -1 && + errno == ESRCH) break; } if (newthread)