[CRIU] Re: [PATCH 6/6] zdtm: Rewrite pthread00 test to check blocked signals

Andrew Vagin avagin at parallels.com
Wed Oct 31 10:34:56 EDT 2012


Could you describe how this test checks blocked signals?

A patch is unreadable. I think in this case you can add a new test case
pthread01 and remove the old one.

Thanks.

On Wed, Oct 31, 2012 at 02:44:11PM +0400, Cyrill Gorcunov wrote:
> 
> Signed-off-by: Cyrill Gorcunov <gorcunov at openvz.org>
> ---
>  test/zdtm/live/static/pthread00.c |  331 +++++++++++++------------------------
>  1 files changed, 115 insertions(+), 216 deletions(-)
> 

> diff --git a/test/zdtm/live/static/pthread00.c b/test/zdtm/live/static/pthread00.c
> index 7b7c060..573d0eb 100644
> --- a/test/zdtm/live/static/pthread00.c
> +++ b/test/zdtm/live/static/pthread00.c
> @@ -7,6 +7,7 @@
>  #include <unistd.h>
>  #include <signal.h>
>  #include <string.h>
> +#include <signal.h>
>  
>  #include <sys/types.h>
>  #include <sys/wait.h>
> @@ -22,224 +23,133 @@
>  const char *test_doc	= "Create a few pthreads/forks and compare TLS and mmap data on restore\n";
>  const char *test_author	= "Cyrill Gorcunov <gorcunov at openvz.org";
>  
> -static pthread_mutex_t mtx = PTHREAD_MUTEX_INITIALIZER;
> -static __thread char tls_data[10];
> -
> -#define TRANSITION_CREATED	1
> -#define TRANSITION_STARTED	2
> -#define TRANSITION_PASSED	4
> -#define TRANSITION_FAILED	8
> -
> -#define MAP(map, i)		(((int *)map)[i])
> -
> -#define SET_CREATED(map, i)	MAP(map, i) |= TRANSITION_CREATED
> -#define SET_STARTED(map, i)	MAP(map, i) |= TRANSITION_STARTED
> -#define SET_PASSED(map, i)	MAP(map, i) |= TRANSITION_PASSED
> -#define SET_FAILED(map, i)	MAP(map, i) |= TRANSITION_FAILED
> +static __thread struct tls_data_s {
> +	char		*rand_string[10];
> +	sigset_t	blk_sigset;
> +} tls_data;
>  
> -#define IS_CREATED(map, i)	(MAP(map, i) & TRANSITION_CREATED)
> -#define IS_STARTED(map, i)	(MAP(map, i) & TRANSITION_STARTED)
> -#define IS_PASSED(map, i)	(MAP(map, i) & TRANSITION_PASSED)
> -#define IS_FAILED(map, i)	(MAP(map, i) & TRANSITION_FAILED)
> +static task_waiter_t t1;
> +static task_waiter_t t2;
>  
> -static void *ff1(void *map)
> +static void show_sigset(const sigset_t *s)
>  {
> -	char __tls_data[10] = "1122334455";
> -	pid_t pid;
> -	int status;
> -
> -	memcpy(tls_data, __tls_data, sizeof(tls_data));
> -
> -	pid = test_fork();
> -	if (pid < 0) {
> -		exit(1);
> -	} else if (pid == 0) {
> -		SET_CREATED(map, 4);
> -		while (1) {
> -			int ret = 0;
> -			pthread_mutex_lock(&mtx);
> -			if (memcmp(tls_data, __tls_data, sizeof(tls_data)))
> -				ret = 1;
> -			pthread_mutex_unlock(&mtx);
> -			if (ret) {
> -				if (IS_STARTED(map, 4)) {
> -					SET_FAILED(map, 4);
> -					exit(4);
> -				}
> -			} else {
> -				if (IS_STARTED(map, 4)) {
> -					SET_PASSED(map, 4);
> -					exit(4);
> -				}
> -			}
> -			sleep(1);
> -		}
> -	}
> +	const char *p = (void *)s;
> +	char buf[1024];
> +	size_t i;
>  
> -	SET_CREATED(map, 5);
> -	while (1) {
> -		int ret = 0;
> -		pthread_mutex_lock(&mtx);
> -		if (memcmp(tls_data, __tls_data, sizeof(tls_data)))
> -			ret = 1;
> -		pthread_mutex_unlock(&mtx);
> -		if (ret) {
> -			if (IS_STARTED(map, 5))
> -				SET_FAILED(map, 5);
> -		} else {
> -				SET_PASSED(map, 5);
> -		}
> -		if (IS_STARTED(map, 5))
> -			break;
> -		sleep(1);
> -	}
> -
> -	test_msg("Waiting for %d\n", pid);
> -	waitpid(pid, &status, P_ALL);
> -
> -	return NULL;
> +	for (i = 0; i < sizeof(s); i++)
> +		sprintf(&buf[i * 2], "%02x", p[i]);
> +	test_msg("sigset: %s\n", buf);
>  }
>  
> -static void *f1(void *map)
> +static void *ch_thread_2(void *arg)
>  {
> -	char __tls_data[10] = "3122131212";
> -	pthread_t th;
> -	pid_t pid;
> -	int status;
> -
> -	memcpy(tls_data, __tls_data, sizeof(tls_data));
> -
> -	if (pthread_create(&th, NULL, &ff1, map))
> -		perror("Cant create thread");
> -
> -	pid = test_fork();
> -	if (pid < 0) {
> -		fail("Failed to test_fork()\n");
> -		exit(1);
> -	} else if (pid == 0) {
> -		SET_CREATED(map, 2);
> -		while (1) {
> -			int ret = 0;
> -			pthread_mutex_lock(&mtx);
> -			if (memcmp(tls_data, __tls_data, sizeof(tls_data)))
> -				ret = 1;
> -			pthread_mutex_unlock(&mtx);
> -			if (ret) {
> -				if (IS_STARTED(map, 2)) {
> -					SET_FAILED(map, 2);
> -					exit(2);
> -				}
> -			} else {
> -				if (IS_STARTED(map, 2)) {
> -					SET_PASSED(map, 2);
> -					exit(2);
> -				}
> -			}
> -			sleep(1);
> -		}
> -	}
> -
> -	SET_CREATED(map, 3);
> -	while (1) {
> -		int ret = 0;
> -		pthread_mutex_lock(&mtx);
> -		if (memcmp(tls_data, __tls_data, sizeof(tls_data)))
> -			ret = 1;
> -		pthread_mutex_unlock(&mtx);
> -		if (ret) {
> -			if (IS_STARTED(map, 3))
> -				SET_FAILED(map, 3);
> -		} else {
> -				SET_PASSED(map, 3);
> -		}
> -		if (IS_STARTED(map, 3))
> -			break;
> -		sleep(1);
> -	}
> -
> -	pthread_join(th, NULL);
> -	test_msg("Waiting for %d\n", pid);
> -	waitpid(pid, &status, P_ALL);
> +	char __tls_data[sizeof(tls_data.rand_string)] = "XM5o:?B*[a";
> +	int *results_map = arg;
> +	sigset_t blk_sigset;
> +	sigset_t new;
> +
> +	memcpy(tls_data.rand_string, __tls_data, sizeof(tls_data.rand_string));
> +
> +	sigemptyset(&blk_sigset);
> +	sigprocmask(SIG_SETMASK, NULL, &blk_sigset);
> +	sigaddset(&blk_sigset, SIGFPE);
> +	sigprocmask(SIG_SETMASK, &blk_sigset, NULL);
> +	memcpy(&tls_data.blk_sigset, &blk_sigset, sizeof(tls_data.blk_sigset));
> +
> +	task_waiter_complete(&t2, 1);
> +	task_waiter_wait4(&t2, 2);
> +
> +	if (memcmp(tls_data.rand_string, __tls_data, sizeof(tls_data.rand_string))) {
> +		err("Failed to restore tls_data.rand_string in thread 2\n");
> +		results_map[2] = -1;
> +	} else
> +		results_map[2] = 1;
> +
> +	if (memcmp(&tls_data.blk_sigset, &blk_sigset, sizeof(tls_data.blk_sigset))) {
> +		err("Failed to restore tls_data.blk_sigset in thread 2\n");
> +		results_map[4] = -1;
> +	} else
> +		results_map[4] = 1;
> +
> +	sigprocmask(SIG_SETMASK, NULL, &new);
> +	if (memcmp(&tls_data.blk_sigset, &new, sizeof(tls_data.blk_sigset))) {
> +		err("Failed to restore blk_sigset in thread 2\n");
> +		results_map[6] = -1;
> +
> +		show_sigset(&tls_data.blk_sigset);
> +		show_sigset(&new);
> +	} else
> +		results_map[6] = 1;
>  
>  	return NULL;
>  }
>  
> -static void *f2(void *map)
> +static void *ch_thread_1(void *arg)
>  {
> -	char __tls_data[10] = "wasdfrdgdc";
> -	pid_t pid;
> -	int status;
> -
> -	memcpy(tls_data, __tls_data, sizeof(tls_data));
> -
> -	pid = test_fork();
> -	if (pid < 0) {
> -		fail("Failed to test_fork()\n");
> -		exit(1);
> -	} else if (pid == 0) {
> -		SET_CREATED(map, 0);
> -		while (1) {
> -			int ret = 0;
> -			pthread_mutex_lock(&mtx);
> -			if (memcmp(tls_data, __tls_data, sizeof(tls_data)))
> -				ret = 1;
> -			pthread_mutex_unlock(&mtx);
> -			if (ret) {
> -				if (IS_STARTED(map, 0)) {
> -					SET_FAILED(map, 0);
> -					exit(0);
> -				}
> -			} else {
> -				if (IS_STARTED(map, 0)) {
> -					SET_PASSED(map, 0);
> -					exit(0);
> -				}
> -			}
> -			sleep(1);
> -		}
> -	}
> -
> -	SET_CREATED(map, 1);
> -	while (1) {
> -		int ret = 0;
> -		pthread_mutex_lock(&mtx);
> -		if (memcmp(tls_data, __tls_data, sizeof(tls_data)))
> -			ret = 1;
> -		pthread_mutex_unlock(&mtx);
> -		if (ret) {
> -			if (IS_STARTED(map, 1))
> -				SET_FAILED(map, 1);
> -		} else {
> -				SET_PASSED(map, 1);
> -		}
> -		if (IS_STARTED(map, 1))
> -			break;
> -		sleep(1);
> -	}
> -
> -	test_msg("Waiting for %d\n", pid);
> -	waitpid(pid, &status, P_ALL);
> +	char __tls_data[sizeof(tls_data.rand_string)] = "pffYQSBo?6";
> +	int *results_map = arg;
> +	sigset_t blk_sigset;
> +	sigset_t new;
> +
> +	memcpy(tls_data.rand_string, __tls_data, sizeof(tls_data.rand_string));
> +
> +	sigemptyset(&blk_sigset);
> +	sigprocmask(SIG_SETMASK, NULL, &blk_sigset);
> +	sigaddset(&blk_sigset, SIGTRAP);
> +	sigprocmask(SIG_SETMASK, &blk_sigset, NULL);
> +	memcpy(&tls_data.blk_sigset, &blk_sigset, sizeof(tls_data.blk_sigset));
> +
> +	task_waiter_complete(&t1, 1);
> +	task_waiter_wait4(&t1, 2);
> +
> +	if (memcmp(tls_data.rand_string, __tls_data, sizeof(tls_data.rand_string))) {
> +		err("Failed to restore tls_data.rand_string in thread 1\n");
> +		results_map[1] = -1;
> +	} else
> +		results_map[1] = 1;
> +
> +	if (memcmp(&tls_data.blk_sigset, &blk_sigset, sizeof(tls_data.blk_sigset))) {
> +		err("Failed to restore tls_data.blk_sigset in thread 1\n");
> +		results_map[3] = -1;
> +	} else
> +		results_map[3] = 1;
> +
> +	sigemptyset(&new);
> +	sigprocmask(SIG_SETMASK, NULL, &new);
> +	if (memcmp(&tls_data.blk_sigset, &new, sizeof(tls_data.blk_sigset))) {
> +		err("Failed to restore blk_sigset in thread 1\n");
> +		results_map[5] = -1;
> +
> +		show_sigset(&tls_data.blk_sigset);
> +		show_sigset(&new);
> +	} else
> +		results_map[5] = 1;
>  
>  	return NULL;
>  }
>  
>  int main(int argc, char *argv[])
>  {
> -	pthread_t th1, th2;
> +	pthread_t thread_1, thread_2;
> +	int *results_map;
>  	int rc1, rc2;
> -	void *map;
>  
>  	test_init(argc, argv);
>  
> +	task_waiter_init(&t1);
> +	task_waiter_init(&t2);
> +
>  	test_msg("%s pid %d\n", argv[0], getpid());
> -	map = mmap(NULL, 1024, PROT_READ | PROT_WRITE, MAP_SHARED | MAP_ANONYMOUS, -1, 0);
> -	if (map == MAP_FAILED) {
> +
> +	results_map = mmap(NULL, 1024, PROT_READ | PROT_WRITE, MAP_SHARED | MAP_ANONYMOUS, -1, 0);
> +	if ((void *)results_map == MAP_FAILED) {
>  		fail("Can't map");
>  		exit(1);
>  	}
>  
> -	rc1 = pthread_create(&th1, NULL, &f1, map);
> -	rc2 = pthread_create(&th2, NULL, &f2, map);
> +	rc1 = pthread_create(&thread_1, NULL, &ch_thread_1, results_map);
> +	rc2 = pthread_create(&thread_2, NULL, &ch_thread_2, results_map);
>  
>  	if (rc1 | rc2) {
>  		fail("Can't pthread_create");
> @@ -247,37 +157,26 @@ int main(int argc, char *argv[])
>  	}
>  
>  	test_msg("Waiting until all threads are created\n");
> -	for (;;) {
> -		if (IS_CREATED(map, 0) &&
> -		    IS_CREATED(map, 1) &&
> -		    IS_CREATED(map, 2) &&
> -		    IS_CREATED(map, 3) &&
> -		    IS_CREATED(map, 4) &&
> -		    IS_CREATED(map, 5))
> -			break;
> -		sleep(1);
> -	}
>  
> -	SET_STARTED(map, 0);
> -	SET_STARTED(map, 1);
> -	SET_STARTED(map, 2);
> -	SET_STARTED(map, 3);
> -	SET_STARTED(map, 4);
> -	SET_STARTED(map, 5);
> +	task_waiter_wait4(&t1, 1);
> +	task_waiter_wait4(&t2, 1);
>  
>  	test_daemon();
>  	test_waitsig();
>  
> +	task_waiter_complete(&t1, 2);
> +	task_waiter_complete(&t2, 2);
> +
>  	test_msg("Waiting while all threads are joined\n");
> -	pthread_join(th1, NULL);
> -	pthread_join(th2, NULL);
> -
> -	if (IS_PASSED(map, 0) &&
> -	    IS_PASSED(map, 1) &&
> -	    IS_PASSED(map, 2) &&
> -	    IS_PASSED(map, 3) &&
> -	    IS_PASSED(map, 4) &&
> -	    IS_PASSED(map, 5))
> +	pthread_join(thread_1, NULL);
> +	pthread_join(thread_2, NULL);
> +
> +	if (results_map[1] == 1 &&
> +	    results_map[2] == 1 &&
> +	    results_map[3] == 1 &&
> +	    results_map[4] == 1 &&
> +	    results_map[5] == 1 &&
> +	    results_map[6] == 1)
>  		pass();
>  	else
>  		fail();



More information about the CRIU mailing list