Blame nptl/tst-rwlock-pwn.c

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