diff mbox series

[v1,2/2] Add gettid02 test

Message ID 20230905114157.4190-3-andrea.cervesato@suse.de
State Superseded
Headers show
Series Rewrite the gettid() testing suite | expand

Commit Message

Andrea Cervesato Sept. 5, 2023, 11:41 a.m. UTC
From: Andrea Cervesato <andrea.cervesato@suse.com>

This new test is checking if gettid() is properly handling a
multi-threaded application, by assigning a different TID for each thread
which differs from the parent ID.

Signed-off-by: Andrea Cervesato <andrea.cervesato@suse.com>
---
 runtest/syscalls                            |  1 +
 testcases/kernel/syscalls/gettid/.gitignore |  1 +
 testcases/kernel/syscalls/gettid/Makefile   |  4 +-
 testcases/kernel/syscalls/gettid/gettid02.c | 68 +++++++++++++++++++++
 4 files changed, 73 insertions(+), 1 deletion(-)
 create mode 100644 testcases/kernel/syscalls/gettid/gettid02.c

Comments

Richard Palethorpe Oct. 5, 2023, 9:18 a.m. UTC | #1
Hello,

Andrea Cervesato <andrea.cervesato@suse.de> writes:

> From: Andrea Cervesato <andrea.cervesato@suse.com>
>
> This new test is checking if gettid() is properly handling a
> multi-threaded application, by assigning a different TID for each thread
> which differs from the parent ID.
>
> Signed-off-by: Andrea Cervesato <andrea.cervesato@suse.com>
> ---
>  runtest/syscalls                            |  1 +
>  testcases/kernel/syscalls/gettid/.gitignore |  1 +
>  testcases/kernel/syscalls/gettid/Makefile   |  4 +-
>  testcases/kernel/syscalls/gettid/gettid02.c | 68 +++++++++++++++++++++
>  4 files changed, 73 insertions(+), 1 deletion(-)
>  create mode 100644 testcases/kernel/syscalls/gettid/gettid02.c
>
> diff --git a/runtest/syscalls b/runtest/syscalls
> index 4fb76584f..d50f5a3e9 100644
> --- a/runtest/syscalls
> +++ b/runtest/syscalls
> @@ -537,6 +537,7 @@ getsockopt01 getsockopt01
>  getsockopt02 getsockopt02
>  
>  gettid01 gettid01
> +gettid02 gettid02
>  
>  gettimeofday01 gettimeofday01
>  gettimeofday02 gettimeofday02
> diff --git a/testcases/kernel/syscalls/gettid/.gitignore b/testcases/kernel/syscalls/gettid/.gitignore
> index 78dce3499..9014f7c3a 100644
> --- a/testcases/kernel/syscalls/gettid/.gitignore
> +++ b/testcases/kernel/syscalls/gettid/.gitignore
> @@ -1 +1,2 @@
>  /gettid01
> +/gettid02
> diff --git a/testcases/kernel/syscalls/gettid/Makefile b/testcases/kernel/syscalls/gettid/Makefile
> index 4e9982f76..5345eb0f5 100644
> --- a/testcases/kernel/syscalls/gettid/Makefile
> +++ b/testcases/kernel/syscalls/gettid/Makefile
> @@ -10,7 +10,9 @@ top_srcdir		?= ../../../..
>  include $(top_srcdir)/include/mk/testcases.mk
>  
>  ifeq ($(ANDROID), 1)
> -FILTER_OUT_MAKE_TARGETS	+= gettid01
> +FILTER_OUT_MAKE_TARGETS	+= gettid01 gettid02
>  endif
>  
> +gettid02: LDLIBS += -lpthread
> +
>  include $(top_srcdir)/include/mk/generic_leaf_target.mk
> diff --git a/testcases/kernel/syscalls/gettid/gettid02.c b/testcases/kernel/syscalls/gettid/gettid02.c
> new file mode 100644
> index 000000000..c981d2b79
> --- /dev/null
> +++ b/testcases/kernel/syscalls/gettid/gettid02.c
> @@ -0,0 +1,68 @@
> +// SPDX-License-Identifier: GPL-2.0-or-later
> +/*
> + * Copyright (C) 2023 SUSE LLC Andrea Cervesato <andrea.cervesato@suse.com>
> + */
> +/*\
> + * [Description]
> + *
> + * This test spawns multiple threads, then check for each one of them if the
> + * parent ID is different AND if the thread ID is different from all the other
> + * spwaned threads.
> + */
> +
> +#include "tst_test.h"
> +#include "lapi/syscalls.h"
> +#include "tst_safe_pthread.h"
> +
> +#define THREADS_NUM 10
> +
> +static pid_t tids[THREADS_NUM];
> +
> +static void *threaded(void *arg)
> +{
> +	int i = *(int *)arg;
> +	pid_t pid, tid;
> +
> +	pid = getpid();

Maybe we could call __NR_getpid directly as well and compare it?

Same reason as with other test; libc's like to be clever and cache this
value. So maybe we are not testing the kernel here.

Otherwise LGTM.
diff mbox series

Patch

diff --git a/runtest/syscalls b/runtest/syscalls
index 4fb76584f..d50f5a3e9 100644
--- a/runtest/syscalls
+++ b/runtest/syscalls
@@ -537,6 +537,7 @@  getsockopt01 getsockopt01
 getsockopt02 getsockopt02
 
 gettid01 gettid01
+gettid02 gettid02
 
 gettimeofday01 gettimeofday01
 gettimeofday02 gettimeofday02
diff --git a/testcases/kernel/syscalls/gettid/.gitignore b/testcases/kernel/syscalls/gettid/.gitignore
index 78dce3499..9014f7c3a 100644
--- a/testcases/kernel/syscalls/gettid/.gitignore
+++ b/testcases/kernel/syscalls/gettid/.gitignore
@@ -1 +1,2 @@ 
 /gettid01
+/gettid02
diff --git a/testcases/kernel/syscalls/gettid/Makefile b/testcases/kernel/syscalls/gettid/Makefile
index 4e9982f76..5345eb0f5 100644
--- a/testcases/kernel/syscalls/gettid/Makefile
+++ b/testcases/kernel/syscalls/gettid/Makefile
@@ -10,7 +10,9 @@  top_srcdir		?= ../../../..
 include $(top_srcdir)/include/mk/testcases.mk
 
 ifeq ($(ANDROID), 1)
-FILTER_OUT_MAKE_TARGETS	+= gettid01
+FILTER_OUT_MAKE_TARGETS	+= gettid01 gettid02
 endif
 
+gettid02: LDLIBS += -lpthread
+
 include $(top_srcdir)/include/mk/generic_leaf_target.mk
diff --git a/testcases/kernel/syscalls/gettid/gettid02.c b/testcases/kernel/syscalls/gettid/gettid02.c
new file mode 100644
index 000000000..c981d2b79
--- /dev/null
+++ b/testcases/kernel/syscalls/gettid/gettid02.c
@@ -0,0 +1,68 @@ 
+// SPDX-License-Identifier: GPL-2.0-or-later
+/*
+ * Copyright (C) 2023 SUSE LLC Andrea Cervesato <andrea.cervesato@suse.com>
+ */
+/*\
+ * [Description]
+ *
+ * This test spawns multiple threads, then check for each one of them if the
+ * parent ID is different AND if the thread ID is different from all the other
+ * spwaned threads.
+ */
+
+#include "tst_test.h"
+#include "lapi/syscalls.h"
+#include "tst_safe_pthread.h"
+
+#define THREADS_NUM 10
+
+static pid_t tids[THREADS_NUM];
+
+static void *threaded(void *arg)
+{
+	int i = *(int *)arg;
+	pid_t pid, tid;
+
+	pid = getpid();
+	tid = tst_syscall(__NR_gettid);
+
+	TST_EXP_EXPR(pid != tid,
+		"parent ID (%d) differs from thread[%d] ID (%d)",
+		pid, i, tid);
+
+	return (void *)tst_syscall(__NR_gettid);
+}
+
+static void run(void)
+{
+	pthread_t thread;
+	int error = 0;
+
+	for (int i = 0; i < THREADS_NUM; i++) {
+		SAFE_PTHREAD_CREATE(&thread, NULL, threaded, &i);
+		SAFE_PTHREAD_JOIN(thread, (void **)&tids[i]);
+	}
+
+	for (int i = 0; i < THREADS_NUM; i++) {
+		for (int j = 0; j < THREADS_NUM; j++) {
+			if (i == j)
+				continue;
+
+			if (tids[i] == tids[j]) {
+				tst_res(TINFO, "thread[%d] and thread[%d] have the same ID", i, j);
+				error = 1;
+				goto end;
+			}
+		}
+	}
+
+end:
+	if (error)
+		tst_res(TFAIL, "Some threads have the same TID");
+	else
+		tst_res(TPASS, "All threads have a different TID");
+}
+
+static struct tst_test test = {
+	.test_all = run,
+};