Blame support/support_record_failure.c

Packit 6c4009
/* Global test failure counter.
Packit 6c4009
   Copyright (C) 2016-2018 Free Software Foundation, Inc.
Packit 6c4009
   This file is part of the GNU C Library.
Packit 6c4009
Packit 6c4009
   The GNU C Library is free software; you can redistribute it and/or
Packit 6c4009
   modify it under the terms of the GNU Lesser General Public
Packit 6c4009
   License as published by the Free Software Foundation; either
Packit 6c4009
   version 2.1 of the License, or (at your option) any later version.
Packit 6c4009
Packit 6c4009
   The GNU C Library is distributed in the hope that it will be useful,
Packit 6c4009
   but WITHOUT ANY WARRANTY; without even the implied warranty of
Packit 6c4009
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
Packit 6c4009
   Lesser General Public License for more details.
Packit 6c4009
Packit 6c4009
   You should have received a copy of the GNU Lesser General Public
Packit 6c4009
   License along with the GNU C Library; if not, see
Packit 6c4009
   <http://www.gnu.org/licenses/>.  */
Packit 6c4009
Packit 6c4009
#include <support/check.h>
Packit 6c4009
#include <support/support.h>
Packit 6c4009
#include <support/test-driver.h>
Packit 6c4009
Packit 6c4009
#include <stdbool.h>
Packit 6c4009
#include <stdio.h>
Packit 6c4009
#include <stdlib.h>
Packit 6c4009
#include <sys/mman.h>
Packit 6c4009
#include <unistd.h>
Packit 6c4009
Packit 6c4009
/* This structure keeps track of test failures.  The counter is
Packit 6c4009
   incremented on each failure.  The failed member is set to true if a
Packit 6c4009
   failure is detected, so that even if the counter wraps around to
Packit 6c4009
   zero, the failure of a test can be detected.
Packit 6c4009
Packit 6c4009
   The init constructor function below puts *state on a shared
Packit 6c4009
   annonymous mapping, so that failure reports from subprocesses
Packit 6c4009
   propagate to the parent process.  */
Packit 6c4009
struct test_failures
Packit 6c4009
{
Packit 6c4009
  unsigned int counter;
Packit 6c4009
  unsigned int failed;
Packit 6c4009
};
Packit 6c4009
static struct test_failures *state;
Packit 6c4009
Packit 6c4009
static __attribute__ ((constructor)) void
Packit 6c4009
init (void)
Packit 6c4009
{
Packit 6c4009
  void *ptr = mmap (NULL, sizeof (*state), PROT_READ | PROT_WRITE,
Packit 6c4009
                    MAP_ANONYMOUS | MAP_SHARED, -1, 0);
Packit 6c4009
  if (ptr == MAP_FAILED)
Packit 6c4009
    {
Packit 6c4009
      printf ("error: could not map %zu bytes: %m\n", sizeof (*state));
Packit 6c4009
      exit (1);
Packit 6c4009
    }
Packit 6c4009
  /* Zero-initialization of the struct is sufficient.  */
Packit 6c4009
  state = ptr;
Packit 6c4009
}
Packit 6c4009
Packit 6c4009
void
Packit 6c4009
support_record_failure (void)
Packit 6c4009
{
Packit 6c4009
  if (state == NULL)
Packit 6c4009
    {
Packit 6c4009
      write_message
Packit 6c4009
        ("error: support_record_failure called without initialization\n");
Packit 6c4009
      _exit (1);
Packit 6c4009
    }
Packit 6c4009
  /* Relaxed MO is sufficient because we are only interested in the
Packit 6c4009
     values themselves, in isolation.  */
Packit 6c4009
  __atomic_store_n (&state->failed, 1, __ATOMIC_RELEASE);
Packit 6c4009
  __atomic_add_fetch (&state->counter, 1, __ATOMIC_RELEASE);
Packit 6c4009
}
Packit 6c4009
Packit 6c4009
int
Packit 6c4009
support_report_failure (int status)
Packit 6c4009
{
Packit 6c4009
  if (state == NULL)
Packit 6c4009
    {
Packit 6c4009
      write_message
Packit 6c4009
        ("error: support_report_failure called without initialization\n");
Packit 6c4009
      return 1;
Packit 6c4009
    }
Packit 6c4009
Packit 6c4009
  /* Relaxed MO is sufficient because acquire test result reporting
Packit 6c4009
     assumes that exiting from the main thread happens before the
Packit 6c4009
     error reporting via support_record_failure, which requires some
Packit 6c4009
     form of external synchronization.  */
Packit 6c4009
  bool failed = __atomic_load_n (&state->failed, __ATOMIC_RELAXED);
Packit 6c4009
  if (failed)
Packit 6c4009
    printf ("error: %u test failures\n",
Packit 6c4009
            __atomic_load_n (&state->counter, __ATOMIC_RELAXED));
Packit 6c4009
Packit 6c4009
  if ((status == 0 || status == EXIT_UNSUPPORTED) && failed)
Packit 6c4009
    /* If we have a recorded failure, it overrides a non-failure
Packit 6c4009
       report from the test function.  */
Packit 6c4009
    status = 1;
Packit 6c4009
  return status;
Packit 6c4009
}
Packit 6c4009
Packit 6c4009
void
Packit 6c4009
support_record_failure_reset (void)
Packit 6c4009
{
Packit 6c4009
  /* Only used for testing the test framework, with external
Packit 6c4009
     synchronization, but use release MO for consistency.  */
Packit 6c4009
  __atomic_store_n (&state->failed, 0, __ATOMIC_RELAXED);
Packit 6c4009
  __atomic_add_fetch (&state->counter, 0, __ATOMIC_RELAXED);
Packit 6c4009
}
Packit Service 864d16
Packit Service 864d16
int
Packit Service 864d16
support_record_failure_is_failed (void)
Packit Service 864d16
{
Packit Service 864d16
  /* Relaxed MO is sufficient because we need (blocking) external
Packit Service 864d16
     synchronization for reliable test error reporting anyway.  */
Packit Service 864d16
  return __atomic_load_n (&state->failed, __ATOMIC_RELAXED);
Packit Service 864d16
}