Blame nptl/tst-rwlock-pwn.c

Packit Service a21b76
/* Test rwlock with PREFER_WRITER_NONRECURSIVE_NP (bug 23861).
Packit Service a21b76
   Copyright (C) 2018 Free Software Foundation, Inc.
Packit Service a21b76
   This file is part of the GNU C Library.
Packit Service a21b76
Packit Service a21b76
   The GNU C Library is free software; you can redistribute it and/or
Packit Service a21b76
   modify it under the terms of the GNU Lesser General Public
Packit Service a21b76
   License as published by the Free Software Foundation; either
Packit Service a21b76
   version 2.1 of the License, or (at your option) any later version.
Packit Service a21b76
Packit Service a21b76
   The GNU C Library is distributed in the hope that it will be useful,
Packit Service a21b76
   but WITHOUT ANY WARRANTY; without even the implied warranty of
Packit Service a21b76
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
Packit Service a21b76
   Lesser General Public License for more details.
Packit Service a21b76
Packit Service a21b76
   You should have received a copy of the GNU Lesser General Public
Packit Service a21b76
   License along with the GNU C Library; if not, see
Packit Service a21b76
   <http://www.gnu.org/licenses/>.  */
Packit Service a21b76
Packit Service a21b76
#include <stdio.h>
Packit Service a21b76
#include <stdlib.h>
Packit Service a21b76
#include <unistd.h>
Packit Service a21b76
#include <pthread.h>
Packit Service a21b76
#include <support/xthread.h>
Packit Service a21b76
Packit Service a21b76
/* We choose 10 iterations because this happens to be able to trigger the
Packit Service a21b76
   stall on contemporary hardware.  */
Packit Service a21b76
#define LOOPS 10
Packit Service a21b76
/* We need 3 threads to trigger bug 23861.  One thread as a writer, and
Packit Service a21b76
   two reader threads.  The test verifies that the second-to-last reader
Packit Service a21b76
   is able to notify the *last* reader that it should be done waiting.
Packit Service a21b76
   If the second-to-last reader fails to notify the last reader or does
Packit Service a21b76
   so incorrectly then the last reader may stall indefinitely.  */
Packit Service a21b76
#define NTHREADS 3
Packit Service a21b76
Packit Service a21b76
_Atomic int do_exit;
Packit Service a21b76
pthread_rwlockattr_t mylock_attr;
Packit Service a21b76
pthread_rwlock_t mylock;
Packit Service a21b76
Packit Service a21b76
void *
Packit Service a21b76
run_loop (void *a)
Packit Service a21b76
{
Packit Service a21b76
  while (!do_exit)
Packit Service a21b76
    {
Packit Service a21b76
      if (random () & 1)
Packit Service a21b76
	{
Packit Service a21b76
	  xpthread_rwlock_wrlock (&mylock);
Packit Service a21b76
	  xpthread_rwlock_unlock (&mylock);
Packit Service a21b76
	}
Packit Service a21b76
      else
Packit Service a21b76
	{
Packit Service a21b76
	  xpthread_rwlock_rdlock (&mylock);
Packit Service a21b76
	  xpthread_rwlock_unlock (&mylock);
Packit Service a21b76
	}
Packit Service a21b76
    }
Packit Service a21b76
  return NULL;
Packit Service a21b76
}
Packit Service a21b76
Packit Service a21b76
int
Packit Service a21b76
do_test (void)
Packit Service a21b76
{
Packit Service a21b76
  xpthread_rwlockattr_init (&mylock_attr);
Packit Service a21b76
  xpthread_rwlockattr_setkind_np (&mylock_attr,
Packit Service a21b76
				  PTHREAD_RWLOCK_PREFER_WRITER_NONRECURSIVE_NP);
Packit Service a21b76
  xpthread_rwlock_init (&mylock, &mylock_attr);
Packit Service a21b76
Packit Service a21b76
  for (int n = 0; n < LOOPS; n++)
Packit Service a21b76
    {
Packit Service a21b76
      pthread_t tids[NTHREADS];
Packit Service a21b76
      do_exit = 0;
Packit Service a21b76
      for (int i = 0; i < NTHREADS; i++)
Packit Service a21b76
	tids[i] = xpthread_create (NULL, run_loop, NULL);
Packit Service a21b76
      /* Let the threads run for some time.  */
Packit Service a21b76
      sleep (1);
Packit Service a21b76
      printf ("Exiting...");
Packit Service a21b76
      fflush (stdout);
Packit Service a21b76
      do_exit = 1;
Packit Service a21b76
      for (int i = 0; i < NTHREADS; i++)
Packit Service a21b76
	xpthread_join (tids[i]);
Packit Service a21b76
      printf ("done.\n");
Packit Service a21b76
    }
Packit Service a21b76
  pthread_rwlock_destroy (&mylock);
Packit Service a21b76
  pthread_rwlockattr_destroy (&mylock_attr);
Packit Service a21b76
  return 0;
Packit Service a21b76
}
Packit Service a21b76
Packit Service a21b76
#define TIMEOUT (DEFAULT_TIMEOUT + 3 * LOOPS)
Packit Service a21b76
#include <support/test-driver.c>