Patchwork [testsuite,racy] Fix race condition in check-libthread-db

login
register
mail settings
Submitter Alan Hayward
Date Sept. 26, 2018, 3 p.m.
Message ID <20180926150035.96813-1-alan.hayward@arm.com>
Download mbox | patch
Permalink /patch/29552/
State New
Headers show

Comments

Alan Hayward - Sept. 26, 2018, 3 p.m.
It is possible for the created thread to reach the breakpoint before
the main thread has set errno to 23.

Prevent this using a pthread barrier.

gdb/testsuite/ChangeLog:

2018-09-26  Alan Hayward  <alan.hayward@arm.com>

	* gdb.threads/check-libthread-db.c (thread_routine): Use a
	pthread barrier.
	(main): Likewise.
---
 gdb/testsuite/gdb.threads/check-libthread-db.c | 8 ++++++++
 1 file changed, 8 insertions(+)
Simon Marchi - Sept. 27, 2018, 3:10 a.m.
On 2018-09-26 11:00, Alan Hayward wrote:
> It is possible for the created thread to reach the breakpoint before
> the main thread has set errno to 23.
> 
> Prevent this using a pthread barrier.
> 
> gdb/testsuite/ChangeLog:
> 
> 2018-09-26  Alan Hayward  <alan.hayward@arm.com>
> 
> 	* gdb.threads/check-libthread-db.c (thread_routine): Use a
> 	pthread barrier.
> 	(main): Likewise.
> ---
>  gdb/testsuite/gdb.threads/check-libthread-db.c | 8 ++++++++
>  1 file changed, 8 insertions(+)
> 
> diff --git a/gdb/testsuite/gdb.threads/check-libthread-db.c
> b/gdb/testsuite/gdb.threads/check-libthread-db.c
> index 85a97a93af..39d39694ce 100644
> --- a/gdb/testsuite/gdb.threads/check-libthread-db.c
> +++ b/gdb/testsuite/gdb.threads/check-libthread-db.c
> @@ -23,6 +23,10 @@
>  #include <pthread.h>
>  #include <errno.h>
> 
> +/* This barrier ensures we only reach the initial breakpoint both 
> threads have
> +   set errno.  */

I think you are missing a word here.  Otherwise, good catch, LGTM.

Simon

Patch

diff --git a/gdb/testsuite/gdb.threads/check-libthread-db.c b/gdb/testsuite/gdb.threads/check-libthread-db.c
index 85a97a93af..39d39694ce 100644
--- a/gdb/testsuite/gdb.threads/check-libthread-db.c
+++ b/gdb/testsuite/gdb.threads/check-libthread-db.c
@@ -23,6 +23,10 @@ 
 #include <pthread.h>
 #include <errno.h>
 
+/* This barrier ensures we only reach the initial breakpoint both threads have
+   set errno.  */
+pthread_barrier_t start_threads_barrier;
+
 static void
 break_here (void)
 {
@@ -32,6 +36,7 @@  static void *
 thread_routine (void *arg)
 {
   errno = 42;
+  pthread_barrier_wait (&start_threads_barrier);
 
   break_here ();
 
@@ -47,6 +52,8 @@  main (int argc, char *argv)
   pthread_t the_thread;
   int err;
 
+  pthread_barrier_init (&start_threads_barrier, NULL, 2);
+
   err = pthread_create (&the_thread, NULL, thread_routine, NULL);
   if (err != 0)
     {
@@ -55,6 +62,7 @@  main (int argc, char *argv)
     }
 
   errno = 23;
+  pthread_barrier_wait (&start_threads_barrier);
 
   err = pthread_join (the_thread, NULL);
   if (err != 0)