From ea6390b2f20a03b7d504bc68a1c95e645d271149 Mon Sep 17 00:00:00 2001 From: Pino Toscano Date: Wed, 2 Nov 2011 17:38:46 +0100 Subject: Remove all the values when deleting a key When deleting a key using `pthread_key_delete', delete all the values associated to that key in all the threads available. Otherwise, the key reuse in `pthread_key_create' can cause new keys to have thread specific data of the previously used key with the same index. Add a test for this case, which creates and deletes pairs of keys checking that they have a NULL thread specific data after creation. * sysdeps/hurd/pt-key-delete.c (pthread_key_delete): Remove all the values of the key in all the threads. * tests/Makefile (CHECK_SRC): Add test-17.c. * tests/test-17.c: New file. --- tests/Makefile | 2 +- tests/test-17.c | 57 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 58 insertions(+), 1 deletion(-) create mode 100644 tests/test-17.c (limited to 'tests') diff --git a/tests/Makefile b/tests/Makefile index 9509c95..4e2a4a8 100644 --- a/tests/Makefile +++ b/tests/Makefile @@ -4,7 +4,7 @@ LDLIBS = -lpthread CHECK_SRC := test-1.c test-2.c test-3.c test-6.c test-7.c test-8.c \ test-9.c test-10.c test-11.c test-12.c test-13.c test-14.c \ - test-15.c test-16.c + test-15.c test-16.c test-17.c CHECK_OBJS := $(addsuffix .o,$(basename $(notdir $(CHECK_SRC)))) CHECK_PROGS := $(basename $(notdir $(CHECK_SRC))) \ diff --git a/tests/test-17.c b/tests/test-17.c new file mode 100644 index 0000000..a8bd150 --- /dev/null +++ b/tests/test-17.c @@ -0,0 +1,57 @@ +/* Test that the key reuse inside libpthread does not cause thread + specific values to persist. */ + +#define _GNU_SOURCE 1 + +#include +#include +#include +#include + +void +work (int iter) +{ + error_t err; + pthread_key_t key1; + pthread_key_t key2; + void *value1; + void *value2; + + printf ("work/%d: start\n", iter); + err = pthread_key_create (&key1, NULL); + assert (err == 0); + err = pthread_key_create (&key2, NULL); + assert (err == 0); + + value1 = pthread_getspecific (key1); + value2 = pthread_getspecific (key2); + printf ("work/%d: pre-setspecific: %p,%p\n", iter, value1, value2); + assert (value1 == NULL); + assert (value2 == NULL); + err = pthread_setspecific (key1, (void *)(0x100 + iter)); + assert (err == 0); + err = pthread_setspecific (key2, (void *)(0x200 + iter)); + assert (err == 0); + + value1 = pthread_getspecific (key1); + value2 = pthread_getspecific (key2); + printf ("work/%d: post-setspecific: %p,%p\n", iter, value1, value2); + assert (value1 == (void *)(0x100 + iter)); + assert (value2 == (void *)(0x200 + iter)); + + err = pthread_key_delete (key1); + assert (err == 0); + err = pthread_key_delete (key2); + assert (err == 0); +} + +int +main (int argc, char *argv[]) +{ + int i; + + for (i = 0; i < 8; ++i) + work (i + 1); + + return 0; +} -- cgit v1.2.3 From 76af844177c2bacd7a39e865cf0bbe484a68ddb8 Mon Sep 17 00:00:00 2001 From: Thomas Schwinge Date: Sat, 5 Nov 2011 01:16:41 +0100 Subject: Correct logic for PTHREAD_KEY_INVALID slots. * sysdeps/hurd/pt-destroy-specific.c (__pthread_destroy_specific): Correct logic for PTHREAD_KEY_INVALID slots. * tests/test-__pthread_destroy_specific-skip.c: New file. * tests/Makefile (CHECK_SRC): Add test-__pthread_destroy_specific-skip.c. --- sysdeps/hurd/pt-destroy-specific.c | 2 +- tests/Makefile | 2 +- tests/test-__pthread_destroy_specific-skip.c | 83 ++++++++++++++++++++++++++++ 3 files changed, 85 insertions(+), 2 deletions(-) create mode 100644 tests/test-__pthread_destroy_specific-skip.c (limited to 'tests') diff --git a/sysdeps/hurd/pt-destroy-specific.c b/sysdeps/hurd/pt-destroy-specific.c index 23c7fbc..f7896e5 100644 --- a/sysdeps/hurd/pt-destroy-specific.c +++ b/sysdeps/hurd/pt-destroy-specific.c @@ -48,7 +48,7 @@ __pthread_destroy_specific (struct __pthread *thread) void *value; if (__pthread_key_destructors[i] == PTHREAD_KEY_INVALID) - break; + continue; value = hurd_ihash_find (thread->thread_specifics, i); if (value) diff --git a/tests/Makefile b/tests/Makefile index 9509c95..343a644 100644 --- a/tests/Makefile +++ b/tests/Makefile @@ -4,7 +4,7 @@ LDLIBS = -lpthread CHECK_SRC := test-1.c test-2.c test-3.c test-6.c test-7.c test-8.c \ test-9.c test-10.c test-11.c test-12.c test-13.c test-14.c \ - test-15.c test-16.c + test-15.c test-16.c test-__pthread_destroy_specific-skip.c CHECK_OBJS := $(addsuffix .o,$(basename $(notdir $(CHECK_SRC)))) CHECK_PROGS := $(basename $(notdir $(CHECK_SRC))) \ diff --git a/tests/test-__pthread_destroy_specific-skip.c b/tests/test-__pthread_destroy_specific-skip.c new file mode 100644 index 0000000..b2c4c0b --- /dev/null +++ b/tests/test-__pthread_destroy_specific-skip.c @@ -0,0 +1,83 @@ +/* Check that __pthread_destroy_specific works correctly if it has to skip + unused slots. */ + +#define _GNU_SOURCE + +#include +#include +#include + + +#define N_k 42 + +static volatile int v; + +static void +d (void *x) +{ + int *i = (int *) x; + + if (v != *i) + error (1, 0, "FAILED %d %d", v, *i); + v += 2; + + printf ("%s %d\n", __FUNCTION__, *i); + fflush (stdout); +} + +static void * +test (void *x) +{ + pthread_key_t k[N_k]; + static int k_v[N_k]; + + int err, i; + + for (i = 0; i < N_k; i += 1) + { + err = pthread_key_create (&k[i], &d); + if (err != 0) + error (1, err, "pthread_key_create %d", i); + } + + for (i = 0; i < N_k; i += 1) + { + k_v[i] = i; + err = pthread_setspecific (k[i], &k_v[i]); + if (err != 0) + error (1, err, "pthread_setspecific %d", i); + } + + /* Delete every even key. */ + for (i = 0; i < N_k; i += 2) + { + err = pthread_key_delete (k[i]); + if (err != 0) + error (1, err, "pthread_key_delete %d", i); + } + + v = 1; + pthread_exit (NULL); + + return NULL; +} + + +int main(void) +{ + pthread_t tid; + int err; + + err = pthread_create (&tid, 0, test, NULL); + if (err != 0) + error (1, err, "pthread_create"); + + err = pthread_join(tid, NULL); + if (err) + error (1, err, "pthread_join"); + + if (v != N_k + 1) + error (1, 0, "FAILED END %d %d", v, N_k + 1); + + return 0; +} -- cgit v1.2.3 From 56e25a91e9e25eba8da099efa89c3c487c3490cf Mon Sep 17 00:00:00 2001 From: Pino Toscano Date: Sun, 6 Nov 2011 12:39:04 +0100 Subject: pthread_getspecific, pthread_setspecific: check the key validity When getting a TSD, handle gracefully the case of an invalid key. When setting a TSD, check for the validity of the key as recommended (although not required) by POSIX. This also avoids potentially filling the `thread_specifics' hash of threads with TSD of invalid keys. Add two simple checks in test-7.c for the two situations above. * sysdeps/hurd/pt-getspecific.c (pthread_getspecific): Check the validity of the specified key. * sysdeps/hurd/pt-setspecific.c (pthread_setspecific): Likewise. * tests/test-7.c (main): Add two assertions. --- sysdeps/hurd/pt-getspecific.c | 4 +++- sysdeps/hurd/pt-setspecific.c | 4 ++++ tests/test-7.c | 3 +++ 3 files changed, 10 insertions(+), 1 deletion(-) (limited to 'tests') diff --git a/sysdeps/hurd/pt-getspecific.c b/sysdeps/hurd/pt-getspecific.c index 3060598..71ec63c 100644 --- a/sysdeps/hurd/pt-getspecific.c +++ b/sysdeps/hurd/pt-getspecific.c @@ -27,7 +27,9 @@ pthread_getspecific (pthread_key_t key) { struct __pthread *self; - assert (key < __pthread_key_count); + if (key < 0 || key >= __pthread_key_count + || __pthread_key_destructors[key] == PTHREAD_KEY_INVALID) + return NULL; self = _pthread_self (); if (! self->thread_specifics) diff --git a/sysdeps/hurd/pt-setspecific.c b/sysdeps/hurd/pt-setspecific.c index 89ca4d7..d0b7302 100644 --- a/sysdeps/hurd/pt-setspecific.c +++ b/sysdeps/hurd/pt-setspecific.c @@ -28,6 +28,10 @@ pthread_setspecific (pthread_key_t key, const void *value) error_t err; struct __pthread *self = _pthread_self (); + if (key < 0 || key >= __pthread_key_count + || __pthread_key_destructors[key] == PTHREAD_KEY_INVALID) + return EINVAL; + if (! self->thread_specifics) { err = hurd_ihash_create (&self->thread_specifics, HURD_IHASH_NO_LOCP); diff --git a/tests/test-7.c b/tests/test-7.c index 8159be3..22fb1ca 100644 --- a/tests/test-7.c +++ b/tests/test-7.c @@ -42,6 +42,9 @@ main (int argc, char **argv) assert ((pthread_t) val == pthread_self ()); } + assert (pthread_getspecific ((pthread_key_t) 0) == NULL); + assert (pthread_setspecific ((pthread_key_t) 0, (void *) 0x1) == EINVAL); + for (i = 0; i < KEYS; i ++) err = pthread_key_create (&key[i], des); -- cgit v1.2.3 From 640cfa0a4af3dc399bff496fd94081a99dbbcdd2 Mon Sep 17 00:00:00 2001 From: Thomas Schwinge Date: Tue, 7 Aug 2012 23:04:43 +0200 Subject: tests/Makefile: Support INSTALL_ROOT environment variable. * tests/Makefile [ifdef INSTALL_ROOT] (INSTALL_ROOT_CPPFLAGS) (INSTALL_ROOT_LDFLAGS): New variables. (%.o: %.c, %: %.o): New rules. (%-static: %.o): Update rule. * tests/README: Update. --- tests/Makefile | 13 ++++++++++++- tests/README | 6 ++++++ 2 files changed, 18 insertions(+), 1 deletion(-) create mode 100644 tests/README (limited to 'tests') diff --git a/tests/Makefile b/tests/Makefile index 5ebc01d..7177ad1 100644 --- a/tests/Makefile +++ b/tests/Makefile @@ -1,3 +1,8 @@ +ifdef INSTALL_ROOT +INSTALL_ROOT_CPPFLAGS = -isystem $(INSTALL_ROOT)/include +INSTALL_ROOT_LDFLAGS = -L$(INSTALL_ROOT)/lib -Wl,-rpath,$(INSTALL_ROOT)/lib +endif + CFLAGS=-Wall -g LDLIBS = -lpthread @@ -10,8 +15,14 @@ CHECK_OBJS := $(addsuffix .o,$(basename $(notdir $(CHECK_SRC)))) CHECK_PROGS := $(basename $(notdir $(CHECK_SRC))) \ $(addsuffix -static, $(basename $(CHECK_SRC))) +%.o: %.c + $(CC) $(INSTALL_ROOT_CPPFLAGS) $(CPPFLAGS) $(CFLAGS) $< -c -o $@ + +%: %.o + $(CC) $(INSTALL_ROOT_LDFLAGS) $(LDFLAGS) $< -o $@ $(LDLIBS) + %-static: %.o - $(CC) -static $(CFLAGS) $(LDFLAGS) $< -o $@ $(LDLIBS) + $(CC) -static $(INSTALL_ROOT_LDFLAGS) $(LDFLAGS) $< -o $@ $(LDLIBS) check: $(CHECK_OBJS) $(CHECK_PROGS) for i in $(CHECK_PROGS); do \ diff --git a/tests/README b/tests/README new file mode 100644 index 0000000..230f1b2 --- /dev/null +++ b/tests/README @@ -0,0 +1,6 @@ +Testing of installed package: + + $ [libpthread]/configure --prefix=[install_root] + $ make + $ make install + $ make -C [libpthread]/tests/ INSTALL_ROOT=[install_root] clean check -- cgit v1.2.3