1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160
|
/* Global test failure counter.
Copyright (C) 2016-2026 Free Software Foundation, Inc.
This file is part of the GNU C Library.
The GNU C Library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
The GNU C Library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with the GNU C Library; if not, see
<https://www.gnu.org/licenses/>. */
#include <support/check.h>
#include <support/support.h>
#include <support/test-driver.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <sys/mman.h>
#include <unistd.h>
/* This structure keeps track of test failures. The counter is
incremented on each failure. The failed member is set to true if a
failure is detected, so that even if the counter wraps around to
zero, the failure of a test can be detected.
If the accept_oom member is not zero, the supervisor process will
use heuristics to suppress process termination due to OOM
conditions.
The init constructor function below puts *state on a shared
anonymous mapping, so that failure reports from subprocesses
propagate to the parent process. */
struct test_failures
{
unsigned int counter;
unsigned int failed;
unsigned int accept_oom;
};
static struct test_failures *state;
static __attribute__ ((constructor)) void
init (void)
{
void *ptr = mmap (NULL, sizeof (*state), PROT_READ | PROT_WRITE,
MAP_ANONYMOUS | MAP_SHARED, -1, 0);
if (ptr == MAP_FAILED)
{
printf ("error: could not map %zu bytes: %m\n", sizeof (*state));
exit (1);
}
/* Zero-initialization of the struct is sufficient. */
state = ptr;
}
void
support_record_failure (void)
{
if (state == NULL)
{
write_message
("error: support_record_failure called without initialization\n");
_exit (1);
}
/* Relaxed MO is sufficient because we are only interested in the
values themselves, in isolation. */
__atomic_store_n (&state->failed, 1, __ATOMIC_RELEASE);
__atomic_add_fetch (&state->counter, 1, __ATOMIC_RELEASE);
}
int
support_report_failure (int status)
{
if (state == NULL)
{
write_message
("error: support_report_failure called without initialization\n");
return 1;
}
/* Relaxed MO is sufficient because acquire test result reporting
assumes that exiting from the main thread happens before the
error reporting via support_record_failure, which requires some
form of external synchronization. */
bool failed = __atomic_load_n (&state->failed, __ATOMIC_RELAXED);
if (failed)
printf ("error: %u test failures\n",
__atomic_load_n (&state->counter, __ATOMIC_RELAXED));
if ((status == 0 || status == EXIT_UNSUPPORTED) && failed)
/* If we have a recorded failure, it overrides a non-failure
report from the test function. */
status = 1;
return status;
}
void
support_record_failure_reset (void)
{
/* Only used for testing the test framework, with external
synchronization, but use release MO for consistency. */
__atomic_store_n (&state->failed, 0, __ATOMIC_RELAXED);
__atomic_add_fetch (&state->counter, 0, __ATOMIC_RELAXED);
}
int
support_record_failure_is_failed (void)
{
/* Relaxed MO is sufficient because we need (blocking) external
synchronization for reliable test error reporting anyway. */
return __atomic_load_n (&state->failed, __ATOMIC_RELAXED);
}
void
support_record_failure_barrier (void)
{
if (__atomic_load_n (&state->failed, __ATOMIC_RELAXED))
{
puts ("error: exiting due to previous errors");
exit (1);
}
}
void
support_accept_oom (bool onoff)
{
if (onoff)
{
/* One thread detects the overflow. */
if (__atomic_fetch_add (&state->accept_oom, 1, __ATOMIC_RELAXED)
== UINT_MAX)
{
puts ("error: OOM acceptance counter overflow");
exit (1);
}
}
else
{
/* One thread detects the underflow. */
if (__atomic_fetch_add (&state->accept_oom, -1, __ATOMIC_RELAXED)
== 0)
{
puts ("error: OOM acceptance counter underflow");
exit (1);
}
}
}
int
support_is_oom_accepted (void)
{
return __atomic_load_n (&state->accept_oom, __ATOMIC_RELAXED) != 0;
}
|