mailing list of musl libc
 help / color / mirror / code / Atom feed
* PATCH: don't call cleanup handlers after a regular return from the thread start function
@ 2014-08-05 16:51 Jens Gustedt
  2014-08-05 17:09 ` Rich Felker
  0 siblings, 1 reply; 13+ messages in thread
From: Jens Gustedt @ 2014-08-05 16:51 UTC (permalink / raw)
  To: musl


[-- Attachment #1.1: Type: text/plain, Size: 1033 bytes --]

Don't call cleanup handlers after a regular return from the thread
start function

The chained list of cleanup handler function uses list items that are
local to the respective function of a cleanup block. In case of a
return out of the middle of a cleanup block, using these list items
can lead to UB.

POSIX lists three different cases in which a cleanup handler that is
established on the cleanup stack has to be executed. Regular return
from the thread start function is not among these cases.

Linux manpages are more explicit and state:

    Clean-up handlers are not called if the thread terminates by
    performing a return from the thread start function.

This patch aligns musl to that behavior.


-- 
:: INRIA Nancy Grand Est ::: AlGorille ::: ICube/ICPS :::
:: ::::::::::::::: office Strasbourg : +33 368854536   ::
:: :::::::::::::::::::::: gsm France : +33 651400183   ::
:: ::::::::::::::: gsm international : +49 15737185122 ::
:: http://icube-icps.unistra.fr/index.php/Jens_Gustedt ::



[-- Attachment #1.2: cleanup-push-fix.patch --]
[-- Type: text/x-patch, Size: 859 bytes --]

diff --git a/src/thread/pthread_create.c b/src/thread/pthread_create.c
index e77e54a..8441845 100644
--- a/src/thread/pthread_create.c
+++ b/src/thread/pthread_create.c
@@ -97,6 +97,7 @@ void __do_cleanup_pop(struct __ptcb *cb)
 
 static int start(void *p)
 {
+	void* ret;
 	pthread_t self = p;
 	if (self->startlock[0]) {
 		__wait(self->startlock, 0, 1, 1);
@@ -109,7 +110,12 @@ static int start(void *p)
 	if (self->unblock_cancel)
 		__syscall(SYS_rt_sigprocmask, SIG_UNBLOCK,
 			SIGPT_SET, 0, _NSIG/8);
-	pthread_exit(self->start(self->start_arg));
+	ret = self->start(self->start_arg);
+	/* POSIX states: The thread exits (that is, calls pthread_exit())
+	   According to the documentation on Linux a return from the
+	   function doesn't count as such an exit. */
+	self->cancelbuf = 0;
+	pthread_exit(ret);
 	return 0;
 }
 

[-- Attachment #2: This is a digitally signed message part --]
[-- Type: application/pgp-signature, Size: 198 bytes --]

^ permalink raw reply	[flat|nested] 13+ messages in thread

end of thread, other threads:[~2014-08-06 10:12 UTC | newest]

Thread overview: 13+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2014-08-05 16:51 PATCH: don't call cleanup handlers after a regular return from the thread start function Jens Gustedt
2014-08-05 17:09 ` Rich Felker
2014-08-05 19:06   ` Jens Gustedt
2014-08-05 19:41     ` Rich Felker
2014-08-05 20:29       ` Rich Felker
2014-08-05 21:05       ` Jens Gustedt
2014-08-05 21:48         ` Rich Felker
2014-08-05 23:19           ` Jens Gustedt
2014-08-06  2:02             ` Rich Felker
2014-08-06  7:15               ` Jens Gustedt
2014-08-06  9:35                 ` Rich Felker
2014-08-06 10:00                   ` Jens Gustedt
2014-08-06 10:12                     ` Rich Felker

Code repositories for project(s) associated with this public inbox

	https://git.vuxu.org/mirror/musl/

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).