mailing list of musl libc
 help / color / mirror / code / Atom feed
* [PATCH v3] move the definition of __pthread_tsd_main to the only compilation unit that references it
@ 2013-02-11 10:44 Jens Gustedt
  2013-02-11 11:14 ` Szabolcs Nagy
  0 siblings, 1 reply; 4+ messages in thread
From: Jens Gustedt @ 2013-02-11 10:44 UTC (permalink / raw)
  To: musl

 - Avoids a const cast that doesn't seem to be very useful.
 - Remove unused __pthread_tsd_size

0	3	src/thread/pthread_key_create.c
2	4	src/thread/pthread_self.c

diff --git a/src/thread/pthread_key_create.c b/src/thread/pthread_key_create.c
index e51cb02..084cbaa 100644
--- a/src/thread/pthread_key_create.c
+++ b/src/thread/pthread_key_create.c
@@ -1,8 +1,5 @@
 #include "pthread_impl.h"
 
-const size_t __pthread_tsd_size = sizeof(void *) * PTHREAD_KEYS_MAX;
-void *__pthread_tsd_main[PTHREAD_KEYS_MAX] = { 0 };
-
 static void (*keys[PTHREAD_KEYS_MAX])(void *);
 
 static void nodtor(void *dummy)
diff --git a/src/thread/pthread_self.c b/src/thread/pthread_self.c
index 23dbaa5..af4998d 100644
--- a/src/thread/pthread_self.c
+++ b/src/thread/pthread_self.c
@@ -2,9 +2,7 @@
 
 static struct pthread *main_thread = &(struct pthread){0};
 
-/* pthread_key_create.c overrides this */
-static const void *dummy[1] = { 0 };
-weak_alias(dummy, __pthread_tsd_main);
+static void *__pthread_tsd_main[PTHREAD_KEYS_MAX];
 
 static int init_main_thread()
 {
@@ -12,7 +10,7 @@ static int init_main_thread()
 		SIGPT_SET, 0, __SYSCALL_SSLEN);
 	if (__set_thread_area(TP_ADJ(main_thread)) < 0) return -1;
 	main_thread->canceldisable = libc.canceldisable;
-	main_thread->tsd = (void **)__pthread_tsd_main;
+	main_thread->tsd = __pthread_tsd_main;
 	main_thread->errno_ptr = __errno_location();
 	main_thread->self = main_thread;
 	main_thread->tid = main_thread->pid =
-- 
1.7.9.5



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

* Re: [PATCH v3] move the definition of __pthread_tsd_main to the only compilation unit that references it
  2013-02-11 10:44 [PATCH v3] move the definition of __pthread_tsd_main to the only compilation unit that references it Jens Gustedt
@ 2013-02-11 11:14 ` Szabolcs Nagy
  2013-02-11 12:24   ` Jens Gustedt
  0 siblings, 1 reply; 4+ messages in thread
From: Szabolcs Nagy @ 2013-02-11 11:14 UTC (permalink / raw)
  To: musl

* Jens Gustedt <Jens.Gustedt@inria.fr> [2013-02-11 11:44:21 +0100]:
>  - Avoids a const cast that doesn't seem to be very useful.
>  - Remove unused __pthread_tsd_size
> 

sorry i was wrong, __pthread_tsd_size is used in pthread_create
(i haven't read all the mails yet)

the logic is that it is 0 when pthread_key_create is not
linked to your code so you avoid all the tsd stack overhead
(which is reasonable: in small tools where size and memory
usage matters most you don't often use thread specific data..
especially now that c11 has tls)

same for pthread_self: you only want non-dummy tsd if
pthread_key_create is used

with your change you break pthread_create (now __pthread_tsd_size
is always 0 there) and pthread_create always pulls in the tsd buf

the weak_alias is there for a reason..


> 0	3	src/thread/pthread_key_create.c
> 2	4	src/thread/pthread_self.c
> 
> diff --git a/src/thread/pthread_key_create.c b/src/thread/pthread_key_create.c
> index e51cb02..084cbaa 100644
> --- a/src/thread/pthread_key_create.c
> +++ b/src/thread/pthread_key_create.c
> @@ -1,8 +1,5 @@
>  #include "pthread_impl.h"
>  
> -const size_t __pthread_tsd_size = sizeof(void *) * PTHREAD_KEYS_MAX;
> -void *__pthread_tsd_main[PTHREAD_KEYS_MAX] = { 0 };
> -
>  static void (*keys[PTHREAD_KEYS_MAX])(void *);
>  
>  static void nodtor(void *dummy)
> diff --git a/src/thread/pthread_self.c b/src/thread/pthread_self.c
> index 23dbaa5..af4998d 100644
> --- a/src/thread/pthread_self.c
> +++ b/src/thread/pthread_self.c
> @@ -2,9 +2,7 @@
>  
>  static struct pthread *main_thread = &(struct pthread){0};
>  
> -/* pthread_key_create.c overrides this */
> -static const void *dummy[1] = { 0 };
> -weak_alias(dummy, __pthread_tsd_main);
> +static void *__pthread_tsd_main[PTHREAD_KEYS_MAX];
>  
>  static int init_main_thread()
>  {
> @@ -12,7 +10,7 @@ static int init_main_thread()
>  		SIGPT_SET, 0, __SYSCALL_SSLEN);
>  	if (__set_thread_area(TP_ADJ(main_thread)) < 0) return -1;
>  	main_thread->canceldisable = libc.canceldisable;
> -	main_thread->tsd = (void **)__pthread_tsd_main;
> +	main_thread->tsd = __pthread_tsd_main;
>  	main_thread->errno_ptr = __errno_location();
>  	main_thread->self = main_thread;
>  	main_thread->tid = main_thread->pid =
> -- 
> 1.7.9.5


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

* Re: [PATCH v3] move the definition of __pthread_tsd_main to the only compilation unit that references it
  2013-02-11 11:14 ` Szabolcs Nagy
@ 2013-02-11 12:24   ` Jens Gustedt
  2013-02-11 12:51     ` Szabolcs Nagy
  0 siblings, 1 reply; 4+ messages in thread
From: Jens Gustedt @ 2013-02-11 12:24 UTC (permalink / raw)
  To: musl

[-- Attachment #1: Type: text/plain, Size: 1764 bytes --]

Am Montag, den 11.02.2013, 12:14 +0100 schrieb Szabolcs Nagy:
> * Jens Gustedt <Jens.Gustedt@inria.fr> [2013-02-11 11:44:21 +0100]:
> >  - Avoids a const cast that doesn't seem to be very useful.
> >  - Remove unused __pthread_tsd_size
> > 
> 
> sorry i was wrong, __pthread_tsd_size is used in pthread_create
> (i haven't read all the mails yet)

oops, somehow I must have got my grep for it wrong then, sorry for the
noise.


> the logic is that it is 0 when pthread_key_create is not
> linked to your code so you avoid all the tsd stack overhead
> (which is reasonable: in small tools where size and memory
> usage matters most you don't often use thread specific data..
> especially now that c11 has tls)
> 
> same for pthread_self: you only want non-dummy tsd if
> pthread_key_create is used
> 
> with your change you break pthread_create (now __pthread_tsd_size
> is always 0 there) and pthread_create always pulls in the tsd buf

Ok, now I see the idea.

In case of dynamic linking, this sounds a bit dangerous to me. Suppose
I have a program that runs fine, without linking to the key stuff. Say
it launches a new thread which then has the key table invalid.

Then I dynamically load a module that uses keys. The thread that has
an invalid table jumps into a function of that module that calles
pthread_key_get() ?

In such a scenario everything would simply crash, wouldn't it?

Jens

-- 
:: INRIA Nancy Grand Est :: http://www.loria.fr/~gustedt/   ::
:: AlGorille ::::::::::::::: office Nancy : +33 383593090   ::
:: ICube :::::::::::::: office Strasbourg : +33 368854536   ::
:: ::::::::::::::::::::::::::: gsm France : +33 651400183   ::
:: :::::::::::::::::::: gsm international : +49 15737185122 ::




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

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

* Re: [PATCH v3] move the definition of __pthread_tsd_main to the only compilation unit that references it
  2013-02-11 12:24   ` Jens Gustedt
@ 2013-02-11 12:51     ` Szabolcs Nagy
  0 siblings, 0 replies; 4+ messages in thread
From: Szabolcs Nagy @ 2013-02-11 12:51 UTC (permalink / raw)
  To: musl

* Jens Gustedt <jens.gustedt@inria.fr> [2013-02-11 13:24:07 +0100]:
> 
> In case of dynamic linking, this sounds a bit dangerous to me. Suppose
> I have a program that runs fine, without linking to the key stuff. Say
> it launches a new thread which then has the key table invalid.
> 
> Then I dynamically load a module that uses keys. The thread that has
> an invalid table jumps into a function of that module that calles
> pthread_key_get() ?
> 
> In such a scenario everything would simply crash, wouldn't it?
> 

this weak alias magic only works for static linking
since the .so has non-weak version of the symbols

(the non-weak symbols will take precedence and only
those will be added to the .so, but in .a there will
be objects with weak symbols and objects with non-weak
ones and linker will do the right thing)


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

end of thread, other threads:[~2013-02-11 12:51 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2013-02-11 10:44 [PATCH v3] move the definition of __pthread_tsd_main to the only compilation unit that references it Jens Gustedt
2013-02-11 11:14 ` Szabolcs Nagy
2013-02-11 12:24   ` Jens Gustedt
2013-02-11 12:51     ` Szabolcs Nagy

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).