Sun Oct 13 09:42:15 2019 UTC ()
Fix race in t_ptrace_wait* LWP tests

Increment the done variable under a mutex. This variable was updated
non-atomically and sometimes not reaching the expected treshold.


(kamil)
diff -r1.136 -r1.137 src/tests/lib/libc/sys/t_ptrace_wait.c

cvs diff -r1.136 -r1.137 src/tests/lib/libc/sys/t_ptrace_wait.c (expand / switch to unified diff)

--- src/tests/lib/libc/sys/t_ptrace_wait.c 2019/10/13 04:05:39 1.136
+++ src/tests/lib/libc/sys/t_ptrace_wait.c 2019/10/13 09:42:15 1.137
@@ -1,14 +1,14 @@ @@ -1,14 +1,14 @@
1/* $NetBSD: t_ptrace_wait.c,v 1.136 2019/10/13 04:05:39 kamil Exp $ */ 1/* $NetBSD: t_ptrace_wait.c,v 1.137 2019/10/13 09:42:15 kamil Exp $ */
2 2
3/*- 3/*-
4 * Copyright (c) 2016, 2017, 2018, 2019 The NetBSD Foundation, Inc. 4 * Copyright (c) 2016, 2017, 2018, 2019 The NetBSD Foundation, Inc.
5 * All rights reserved. 5 * All rights reserved.
6 * 6 *
7 * Redistribution and use in source and binary forms, with or without 7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions 8 * modification, are permitted provided that the following conditions
9 * are met: 9 * are met:
10 * 1. Redistributions of source code must retain the above copyright 10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer. 11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright 12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the 13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution. 14 * documentation and/or other materials provided with the distribution.
@@ -17,27 +17,27 @@ @@ -17,27 +17,27 @@
17 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 17 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
18 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 18 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
19 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 19 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
20 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 20 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 21 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 22 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 23 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 24 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 25 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26 * POSSIBILITY OF SUCH DAMAGE. 26 * POSSIBILITY OF SUCH DAMAGE.
27 */ 27 */
28 28
29#include <sys/cdefs.h> 29#include <sys/cdefs.h>
30__RCSID("$NetBSD: t_ptrace_wait.c,v 1.136 2019/10/13 04:05:39 kamil Exp $"); 30__RCSID("$NetBSD: t_ptrace_wait.c,v 1.137 2019/10/13 09:42:15 kamil Exp $");
31 31
32#include <sys/param.h> 32#include <sys/param.h>
33#include <sys/types.h> 33#include <sys/types.h>
34#include <sys/exec_elf.h> 34#include <sys/exec_elf.h>
35#include <sys/mman.h> 35#include <sys/mman.h>
36#include <sys/ptrace.h> 36#include <sys/ptrace.h>
37#include <sys/resource.h> 37#include <sys/resource.h>
38#include <sys/stat.h> 38#include <sys/stat.h>
39#include <sys/syscall.h> 39#include <sys/syscall.h>
40#include <sys/sysctl.h> 40#include <sys/sysctl.h>
41#include <sys/uio.h> 41#include <sys/uio.h>
42#include <sys/wait.h> 42#include <sys/wait.h>
43#include <machine/reg.h> 43#include <machine/reg.h>
@@ -5448,32 +5448,35 @@ ATF_TC_BODY(test, tc) \ @@ -5448,32 +5448,35 @@ ATF_TC_BODY(test, tc) \
5448 \ 5448 \
5449 traceme_exec(masked, ignored); \ 5449 traceme_exec(masked, ignored); \
5450} 5450}
5451 5451
5452TRACEME_EXEC(traceme_exec, false, false) 5452TRACEME_EXEC(traceme_exec, false, false)
5453TRACEME_EXEC(traceme_signalmasked_exec, true, false) 5453TRACEME_EXEC(traceme_signalmasked_exec, true, false)
5454TRACEME_EXEC(traceme_signalignored_exec, false, true) 5454TRACEME_EXEC(traceme_signalignored_exec, false, true)
5455 5455
5456/// ---------------------------------------------------------------------------- 5456/// ----------------------------------------------------------------------------
5457 5457
5458#define TRACE_THREADS_NUM 100 5458#define TRACE_THREADS_NUM 100
5459 5459
5460static volatile int done; 5460static volatile int done;
 5461pthread_mutex_t trace_threads_mtx = PTHREAD_MUTEX_INITIALIZER;
5461 5462
5462static void * 5463static void *
5463trace_threads_cb(void *arg __unused) 5464trace_threads_cb(void *arg __unused)
5464{ 5465{
5465 5466
 5467 pthread_mutex_lock(&trace_threads_mtx);
5466 done++; 5468 done++;
 5469 pthread_mutex_unlock(&trace_threads_mtx);
5467 5470
5468 while (done < TRACE_THREADS_NUM) 5471 while (done < TRACE_THREADS_NUM)
5469 sched_yield(); 5472 sched_yield();
5470 5473
5471 return NULL; 5474 return NULL;
5472} 5475}
5473 5476
5474static void 5477static void
5475trace_threads(bool trace_create, bool trace_exit) 5478trace_threads(bool trace_create, bool trace_exit)
5476{ 5479{
5477 const int sigval = SIGSTOP; 5480 const int sigval = SIGSTOP;
5478 pid_t child, wpid; 5481 pid_t child, wpid;
5479#if defined(TWAIT_HAVE_STATUS) 5482#if defined(TWAIT_HAVE_STATUS)