Use __builtin_fma more in dbl-64 code.
[glibc.git] / test-skeleton.c
blobd53fe1c189933223419e1f403003cfb6751cfff8
1 /* Skeleton for test programs.
2 Copyright (C) 1998-2016 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
4 Contributed by Ulrich Drepper <drepper@cygnus.com>, 1998.
6 The GNU C Library is free software; you can redistribute it and/or
7 modify it under the terms of the GNU Lesser General Public
8 License as published by the Free Software Foundation; either
9 version 2.1 of the License, or (at your option) any later version.
11 The GNU C Library is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 Lesser General Public License for more details.
16 You should have received a copy of the GNU Lesser General Public
17 License along with the GNU C Library; if not, see
18 <http://www.gnu.org/licenses/>. */
20 #include <assert.h>
21 #include <errno.h>
22 #include <fcntl.h>
23 #include <getopt.h>
24 #include <malloc.h>
25 #include <paths.h>
26 #include <search.h>
27 #include <signal.h>
28 #include <stdio.h>
29 #include <stdlib.h>
30 #include <string.h>
31 #include <unistd.h>
32 #include <sys/resource.h>
33 #include <sys/wait.h>
34 #include <sys/param.h>
35 #include <time.h>
37 /* The test function is normally called `do_test' and it is called
38 with argc and argv as the arguments. We nevertheless provide the
39 possibility to overwrite this name. */
40 #ifndef TEST_FUNCTION
41 # define TEST_FUNCTION do_test (argc, argv)
42 #endif
44 #ifndef TEST_DATA_LIMIT
45 # define TEST_DATA_LIMIT (64 << 20) /* Data limit (bytes) to run with. */
46 #endif
48 #ifndef TIMEOUT
49 /* Default timeout is twenty seconds. Tests should normally complete faster
50 than this, but if they don't, that's abnormal (a bug) anyways. */
51 # define TIMEOUT 20
52 #endif
54 #define OPT_DIRECT 1000
55 #define OPT_TESTDIR 1001
57 static struct option options[] =
59 #ifdef CMDLINE_OPTIONS
60 CMDLINE_OPTIONS
61 #endif
62 { "direct", no_argument, NULL, OPT_DIRECT },
63 { "test-dir", required_argument, NULL, OPT_TESTDIR },
64 { NULL, 0, NULL, 0 }
67 /* PID of the test itself. */
68 static pid_t pid;
70 /* Directory to place temporary files in. */
71 static const char *test_dir;
73 static void
74 oom_error (const char *fn, size_t size)
76 printf ("%s: unable to allocate %zu bytes: %m\n", fn, size);
77 exit (1);
80 /* Allocate N bytes of memory dynamically, with error checking. */
81 __attribute__ ((unused))
82 static void *
83 xmalloc (size_t n)
85 void *p;
87 p = malloc (n);
88 if (p == NULL)
89 oom_error ("malloc", n);
90 return p;
93 /* Allocate memory for N elements of S bytes, with error checking. */
94 __attribute__ ((unused))
95 static void *
96 xcalloc (size_t n, size_t s)
98 void *p;
100 p = calloc (n, s);
101 if (p == NULL)
102 oom_error ("calloc", n * s);
103 return p;
106 /* Change the size of an allocated block of memory P to N bytes,
107 with error checking. */
108 __attribute__ ((unused))
109 static void *
110 xrealloc (void *p, size_t n)
112 void *result = realloc (p, n);
113 if (result == NULL && (n > 0 || p == NULL))
114 oom_error ("realloc", n);
115 return result;
118 /* Call asprintf with error checking. */
119 __attribute__ ((always_inline, format (printf, 1, 2)))
120 static __inline__ char *
121 xasprintf (const char *format, ...)
123 char *result;
124 if (asprintf (&result, format, __builtin_va_arg_pack ()) < 0)
126 printf ("error: asprintf: %m\n");
127 exit (1);
129 return result;
132 /* Write a message to standard output. Can be used in signal
133 handlers. */
134 static void
135 __attribute__ ((unused))
136 write_message (const char *message)
138 ssize_t unused __attribute__ ((unused));
139 unused = write (STDOUT_FILENO, message, strlen (message));
142 /* List of temporary files. */
143 struct temp_name_list
145 struct qelem q;
146 char *name;
147 } *temp_name_list;
149 /* Add temporary files in list. */
150 static void
151 __attribute__ ((unused))
152 add_temp_file (const char *name)
154 struct temp_name_list *newp
155 = (struct temp_name_list *) xcalloc (sizeof (*newp), 1);
156 char *newname = strdup (name);
157 if (newname != NULL)
159 newp->name = newname;
160 if (temp_name_list == NULL)
161 temp_name_list = (struct temp_name_list *) &newp->q;
162 else
163 insque (newp, temp_name_list);
165 else
166 free (newp);
169 /* Delete all temporary files. */
170 static void
171 delete_temp_files (void)
173 while (temp_name_list != NULL)
175 remove (temp_name_list->name);
176 free (temp_name_list->name);
178 struct temp_name_list *next
179 = (struct temp_name_list *) temp_name_list->q.q_forw;
180 free (temp_name_list);
181 temp_name_list = next;
185 /* Create a temporary file. Return the opened file descriptor on
186 success, or -1 on failure. Write the file name to *FILENAME if
187 FILENAME is not NULL. In this case, the caller is expected to free
188 *FILENAME. */
189 static int
190 __attribute__ ((unused))
191 create_temp_file (const char *base, char **filename)
193 char *fname;
194 int fd;
196 fname = (char *) xmalloc (strlen (test_dir) + 1 + strlen (base)
197 + sizeof ("XXXXXX"));
198 strcpy (stpcpy (stpcpy (stpcpy (fname, test_dir), "/"), base), "XXXXXX");
200 fd = mkstemp (fname);
201 if (fd == -1)
203 printf ("cannot open temporary file '%s': %m\n", fname);
204 free (fname);
205 return -1;
208 add_temp_file (fname);
209 if (filename != NULL)
210 *filename = fname;
211 else
212 free (fname);
214 return fd;
217 /* Timeout handler. We kill the child and exit with an error. */
218 static void
219 __attribute__ ((noreturn))
220 signal_handler (int sig __attribute__ ((unused)))
222 int killed;
223 int status;
225 assert (pid > 1);
226 /* Kill the whole process group. */
227 kill (-pid, SIGKILL);
228 /* In case setpgid failed in the child, kill it individually too. */
229 kill (pid, SIGKILL);
231 /* Wait for it to terminate. */
232 int i;
233 for (i = 0; i < 5; ++i)
235 killed = waitpid (pid, &status, WNOHANG|WUNTRACED);
236 if (killed != 0)
237 break;
239 /* Delay, give the system time to process the kill. If the
240 nanosleep() call return prematurely, all the better. We
241 won't restart it since this probably means the child process
242 finally died. */
243 struct timespec ts;
244 ts.tv_sec = 0;
245 ts.tv_nsec = 100000000;
246 nanosleep (&ts, NULL);
248 if (killed != 0 && killed != pid)
250 printf ("Failed to kill test process: %m\n");
251 exit (1);
254 #ifdef CLEANUP_HANDLER
255 CLEANUP_HANDLER;
256 #endif
258 if (sig == SIGINT)
260 signal (sig, SIG_DFL);
261 raise (sig);
264 /* If we expected this signal: good! */
265 #ifdef EXPECTED_SIGNAL
266 if (EXPECTED_SIGNAL == SIGALRM)
267 exit (0);
268 #endif
270 if (killed == 0 || (WIFSIGNALED (status) && WTERMSIG (status) == SIGKILL))
271 puts ("Timed out: killed the child process");
272 else if (WIFSTOPPED (status))
273 printf ("Timed out: the child process was %s\n",
274 strsignal (WSTOPSIG (status)));
275 else if (WIFSIGNALED (status))
276 printf ("Timed out: the child process got signal %s\n",
277 strsignal (WTERMSIG (status)));
278 else
279 printf ("Timed out: killed the child process but it exited %d\n",
280 WEXITSTATUS (status));
282 /* Exit with an error. */
283 exit (1);
286 /* Avoid all the buffer overflow messages on stderr. */
287 static void
288 __attribute__ ((unused))
289 ignore_stderr (void)
291 int fd = open (_PATH_DEVNULL, O_WRONLY);
292 if (fd == -1)
293 close (STDERR_FILENO);
294 else
296 dup2 (fd, STDERR_FILENO);
297 close (fd);
299 setenv ("LIBC_FATAL_STDERR_", "1", 1);
302 /* Set fortification error handler. Used when tests want to verify that bad
303 code is caught by the library. */
304 static void
305 __attribute__ ((unused))
306 set_fortify_handler (void (*handler) (int sig))
308 struct sigaction sa;
310 sa.sa_handler = handler;
311 sa.sa_flags = 0;
312 sigemptyset (&sa.sa_mask);
314 sigaction (SIGABRT, &sa, NULL);
315 ignore_stderr ();
318 /* Show people how to run the program. */
319 static void
320 usage (void)
322 size_t i;
324 printf ("Usage: %s [options]\n"
325 "\n"
326 "Environment Variables:\n"
327 " TIMEOUTFACTOR An integer used to scale the timeout\n"
328 " TMPDIR Where to place temporary files\n"
329 "\n",
330 program_invocation_short_name);
331 printf ("Options:\n");
332 for (i = 0; options[i].name; ++i)
334 int indent;
336 indent = printf (" --%s", options[i].name);
337 if (options[i].has_arg == required_argument)
338 indent += printf (" <arg>");
339 printf ("%*s", 25 - indent, "");
340 switch (options[i].val)
342 case OPT_DIRECT:
343 printf ("Run the test directly (instead of forking & monitoring)");
344 break;
345 case OPT_TESTDIR:
346 printf ("Override the TMPDIR env var");
347 break;
349 printf ("\n");
353 /* We provide the entry point here. */
355 main (int argc, char *argv[])
357 int direct = 0; /* Directly call the test function? */
358 int status;
359 int opt;
360 unsigned int timeoutfactor = 1;
361 pid_t termpid;
363 #ifndef TEST_NO_MALLOPT
364 /* Make uses of freed and uninitialized memory known. */
365 mallopt (M_PERTURB, 42);
366 #endif
368 #ifdef STDOUT_UNBUFFERED
369 setbuf (stdout, NULL);
370 #endif
372 while ((opt = getopt_long (argc, argv, "+", options, NULL)) != -1)
373 switch (opt)
375 case '?':
376 usage ();
377 exit (1);
378 case OPT_DIRECT:
379 direct = 1;
380 break;
381 case OPT_TESTDIR:
382 test_dir = optarg;
383 break;
384 #ifdef CMDLINE_PROCESS
385 CMDLINE_PROCESS
386 #endif
389 /* If set, read the test TIMEOUTFACTOR value from the environment.
390 This value is used to scale the default test timeout values. */
391 char *envstr_timeoutfactor = getenv ("TIMEOUTFACTOR");
392 if (envstr_timeoutfactor != NULL)
394 char *envstr_conv = envstr_timeoutfactor;
395 unsigned long int env_fact;
397 env_fact = strtoul (envstr_timeoutfactor, &envstr_conv, 0);
398 if (*envstr_conv == '\0' && envstr_conv != envstr_timeoutfactor)
399 timeoutfactor = MAX (env_fact, 1);
402 /* Set TMPDIR to specified test directory. */
403 if (test_dir != NULL)
405 setenv ("TMPDIR", test_dir, 1);
407 if (chdir (test_dir) < 0)
409 printf ("chdir: %m\n");
410 exit (1);
413 else
415 test_dir = getenv ("TMPDIR");
416 if (test_dir == NULL || test_dir[0] == '\0')
417 test_dir = "/tmp";
420 /* Make sure we see all message, even those on stdout. */
421 setvbuf (stdout, NULL, _IONBF, 0);
423 /* Make sure temporary files are deleted. */
424 atexit (delete_temp_files);
426 /* Correct for the possible parameters. */
427 argv[optind - 1] = argv[0];
428 argv += optind - 1;
429 argc -= optind - 1;
431 /* Call the initializing function, if one is available. */
432 #ifdef PREPARE
433 PREPARE (argc, argv);
434 #endif
436 const char *envstr_direct = getenv ("TEST_DIRECT");
437 if (envstr_direct != NULL)
439 FILE *f = fopen (envstr_direct, "w");
440 if (f == NULL)
442 printf ("cannot open TEST_DIRECT output file '%s': %m\n",
443 envstr_direct);
444 exit (1);
447 fprintf (f, "timeout=%u\ntimeoutfactor=%u\n", TIMEOUT, timeoutfactor);
448 #ifdef EXPECTED_STATUS
449 fprintf (f, "exit=%u\n", EXPECTED_STATUS);
450 #endif
451 #ifdef EXPECTED_SIGNAL
452 switch (EXPECTED_SIGNAL)
454 default: abort ();
455 # define init_sig(signo, name, text) \
456 case signo: fprintf (f, "signal=%s\n", name); break;
457 # include <siglist.h>
458 # undef init_sig
460 #endif
462 if (temp_name_list != NULL)
464 struct temp_name_list *n;
465 fprintf (f, "temp_files=(\n");
466 for (n = temp_name_list;
467 n != NULL;
468 n = (struct temp_name_list *) n->q.q_forw)
469 fprintf (f, " '%s'\n", n->name);
470 fprintf (f, ")\n");
473 fclose (f);
474 direct = 1;
477 /* If we are not expected to fork run the function immediately. */
478 if (direct)
479 return TEST_FUNCTION;
481 /* Set up the test environment:
482 - prevent core dumps
483 - set up the timer
484 - fork and execute the function. */
486 pid = fork ();
487 if (pid == 0)
489 /* This is the child. */
490 #ifdef RLIMIT_CORE
491 /* Try to avoid dumping core. */
492 struct rlimit core_limit;
493 core_limit.rlim_cur = 0;
494 core_limit.rlim_max = 0;
495 setrlimit (RLIMIT_CORE, &core_limit);
496 #endif
498 /* We put the test process in its own pgrp so that if it bogusly
499 generates any job control signals, they won't hit the whole build. */
500 if (setpgid (0, 0) != 0)
501 printf ("Failed to set the process group ID: %m\n");
503 /* Execute the test function and exit with the return value. */
504 exit (TEST_FUNCTION);
506 else if (pid < 0)
508 printf ("Cannot fork test program: %m\n");
509 exit (1);
512 /* Set timeout. */
513 signal (SIGALRM, signal_handler);
514 alarm (TIMEOUT * timeoutfactor);
516 /* Make sure we clean up if the wrapper gets interrupted. */
517 signal (SIGINT, signal_handler);
519 /* Wait for the regular termination. */
520 termpid = TEMP_FAILURE_RETRY (waitpid (pid, &status, 0));
521 if (termpid == -1)
523 printf ("Waiting for test program failed: %m\n");
524 exit (1);
526 if (termpid != pid)
528 printf ("Oops, wrong test program terminated: expected %ld, got %ld\n",
529 (long int) pid, (long int) termpid);
530 exit (1);
533 /* Process terminated normaly without timeout etc. */
534 if (WIFEXITED (status))
536 #ifndef EXPECTED_STATUS
537 # ifndef EXPECTED_SIGNAL
538 /* Simply exit with the return value of the test. */
539 return WEXITSTATUS (status);
540 # else
541 printf ("Expected signal '%s' from child, got none\n",
542 strsignal (EXPECTED_SIGNAL));
543 exit (1);
544 # endif
545 #else
546 if (WEXITSTATUS (status) != EXPECTED_STATUS)
548 printf ("Expected status %d, got %d\n",
549 EXPECTED_STATUS, WEXITSTATUS (status));
550 exit (1);
553 return 0;
554 #endif
556 /* Process was killed by timer or other signal. */
557 else
559 #ifndef EXPECTED_SIGNAL
560 printf ("Didn't expect signal from child: got `%s'\n",
561 strsignal (WTERMSIG (status)));
562 exit (1);
563 #else
564 if (WTERMSIG (status) != EXPECTED_SIGNAL)
566 printf ("Incorrect signal from child: got `%s', need `%s'\n",
567 strsignal (WTERMSIG (status)),
568 strsignal (EXPECTED_SIGNAL));
569 exit (1);
572 return 0;
573 #endif
577 /* The following functionality is only available if <pthread.h> was
578 included before this file. */
579 #ifdef _PTHREAD_H
581 /* Call pthread_sigmask with error checking. */
582 static void
583 xpthread_sigmask (int how, const sigset_t *set, sigset_t *oldset)
585 if (pthread_sigmask (how, set, oldset) != 0)
587 write_message ("error: pthread_setmask failed\n");
588 _exit (1);
592 /* Call pthread_mutex_lock with error checking. */
593 __attribute__ ((unused))
594 static void
595 xpthread_mutex_lock (pthread_mutex_t *mutex)
597 int ret = pthread_mutex_lock (mutex);
598 if (ret != 0)
600 errno = ret;
601 printf ("error: pthread_mutex_lock: %m\n");
602 exit (1);
606 /* Call pthread_spin_lock with error checking. */
607 __attribute__ ((unused))
608 static void
609 xpthread_spin_lock (pthread_spinlock_t *lock)
611 int ret = pthread_spin_lock (lock);
612 if (ret != 0)
614 errno = ret;
615 printf ("error: pthread_spin_lock: %m\n");
616 exit (1);
620 /* Call pthread_cond_wait with error checking. */
621 __attribute__ ((unused))
622 static void
623 xpthread_cond_wait (pthread_cond_t * cond,
624 pthread_mutex_t * mutex)
626 int ret = pthread_cond_wait (cond, mutex);
627 if (ret != 0)
629 errno = ret;
630 printf ("error: pthread_cond_wait: %m\n");
631 exit (1);
635 /* Call pthread_barrier_wait with error checking. */
636 __attribute__ ((unused))
637 static int
638 xpthread_barrier_wait (pthread_barrier_t *barrier)
640 int ret = pthread_barrier_wait (barrier);
641 if (ret != 0 && ret != PTHREAD_BARRIER_SERIAL_THREAD)
643 errno = ret;
644 printf ("error: pthread_barrier_wait: %m\n");
645 exit (1);
647 return ret;
650 /* Call pthread_create with error checking. */
651 static pthread_t
652 xpthread_create (pthread_attr_t *attr,
653 void *(*thread_func) (void *), void *closure)
655 pthread_t thr;
656 int ret = pthread_create (&thr, attr, thread_func, closure);
657 if (ret != 0)
659 errno = ret;
660 printf ("error: pthread_create: %m\n");
661 exit (1);
663 return thr;
666 /* Call pthread_detach with error checking. */
667 static void
668 xpthread_detach (pthread_t thr)
670 int ret = pthread_detach (thr);
671 if (ret != 0)
673 errno = ret;
674 printf ("error: pthread_detach: %m\n");
675 exit (1);
679 /* Call pthread_join with error checking. */
680 __attribute__ ((unused))
681 static void *
682 xpthread_join (pthread_t thr)
684 void *result;
685 int ret = pthread_join (thr, &result);
686 if (ret != 0)
688 errno = ret;
689 printf ("error: pthread_join: %m\n");
690 exit (1);
692 return result;
695 /* Used to implement the delayed_exit function defined below. */
696 static void *
697 delayed_exit_thread (void *seconds_as_ptr)
699 int seconds = (uintptr_t) seconds_as_ptr;
700 struct timespec delay = { seconds, 0 };
701 struct timespec remaining = { 0 };
702 if (nanosleep (&delay, &remaining) != 0)
704 printf ("error: nanosleep: %m\n");
705 _exit (1);
707 /* Exit the process sucessfully. */
708 exit (0);
709 return NULL;
712 /* Exit (with status 0) after SECONDS have elapsed, from a helper
713 thread. The process is terminated with the exit function, so
714 atexit handlers are executed. */
715 __attribute__ ((unused))
716 static void
717 delayed_exit (int seconds)
719 /* Create the new thread with all signals blocked. */
720 sigset_t all_blocked;
721 sigfillset (&all_blocked);
722 sigset_t old_set;
723 xpthread_sigmask (SIG_SETMASK, &all_blocked, &old_set);
724 /* Create a detached thread. */
725 pthread_t thr = xpthread_create
726 (NULL, delayed_exit_thread, (void *) (uintptr_t) seconds);
727 xpthread_detach (thr);
728 /* Restore the original signal mask. */
729 xpthread_sigmask (SIG_SETMASK, &old_set, NULL);
732 #endif /* _PTHREAD_H */