usleep tests: Avoid failure due to known Cygwin 3.5.3 bug.
[gnulib.git] / tests / test-dprintf-posix2.c
blob6e01d7e782ea1012128e709e1b632ec86217a413
1 /* Test of POSIX compatible dprintf() function.
2 Copyright (C) 2009-2024 Free Software Foundation, Inc.
4 This program is free software: you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation, either version 3 of the License, or
7 (at your option) any later version.
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
14 You should have received a copy of the GNU General Public License
15 along with this program. If not, see <https://www.gnu.org/licenses/>. */
17 /* Written by Bruno Haible <bruno@clisp.org>, 2009. */
19 #include <config.h>
21 #include <stdio.h>
23 #if defined __APPLE__ && defined __MACH__ /* macOS */
25 /* On macOS 11, 12, 13, 14, this test sometimes fails. Probably for the same
26 reason as mentioned in the comment in test-fprintf-posix3.c. */
28 int
29 main ()
31 fprintf (stderr, "Skipping test: cannot trust address space size on this platform\n");
32 return 78;
35 #else
37 /* Skip this test when an address sanitizer is in use, since it would fail. */
38 #ifndef __has_feature
39 # define __has_feature(a) 0
40 #endif
41 #if defined __SANITIZE_ADDRESS__ || __has_feature (address_sanitizer)
43 int
44 main ()
46 fprintf (stderr, "Skipping test: address sanitizer's malloc behaves differently\n");
47 return 80;
50 #else
52 #include <stdlib.h>
53 #include <string.h>
54 #include <unistd.h>
55 #include <errno.h>
57 #if HAVE_GETRLIMIT && HAVE_SETRLIMIT
58 # include <sys/types.h>
59 # include <sys/time.h>
60 # include <sys/resource.h>
61 #endif
63 #include "qemu.h"
64 #include "resource-ext.h"
66 /* Test against a memory leak in the fprintf replacement. */
68 /* Number of iterations across the loop. */
69 #define NUM_ROUNDS 1000
71 /* Number of bytes that are allowed to escape per round. */
72 #define MAX_ALLOC_ROUND 10000
74 /* Number of bytes that are allowed to escape in total.
75 This should be at least 10 MB, since it includes the normal memory
76 or address space of the test program. */
77 #define MAX_ALLOC_TOTAL (NUM_ROUNDS * MAX_ALLOC_ROUND)
79 int
80 main (int argc, char *argv[])
82 uintptr_t initial_rusage_as;
83 int arg;
84 int result;
86 if (is_running_under_qemu_user ())
88 fprintf (stderr, "Skipping test: cannot trust address space size when running under QEMU\n");
89 return 79;
92 /* Limit the amount of malloc()ed memory to MAX_ALLOC_TOTAL or less. */
94 /* On AIX systems, malloc() is limited by RLIMIT_DATA. */
95 #if HAVE_GETRLIMIT && HAVE_SETRLIMIT && defined RLIMIT_DATA
97 struct rlimit limit;
99 if (getrlimit (RLIMIT_DATA, &limit) >= 0)
101 if (limit.rlim_max == RLIM_INFINITY || limit.rlim_max > MAX_ALLOC_TOTAL)
102 limit.rlim_max = MAX_ALLOC_TOTAL;
103 limit.rlim_cur = limit.rlim_max;
104 (void) setrlimit (RLIMIT_DATA, &limit);
107 #endif
108 /* On all systems except AIX and OpenBSD, malloc() is limited by RLIMIT_AS.
109 On some systems, setrlimit of RLIMIT_AS doesn't work but get_rusage_as ()
110 does. Allow the address space size to grow by at most MAX_ALLOC_TOTAL. */
111 initial_rusage_as = get_rusage_as ();
112 #if !defined _AIX
113 if (initial_rusage_as == 0)
114 return 77;
115 #endif
117 arg = atoi (argv[1]);
118 if (arg == 0)
120 void *memory = malloc (MAX_ALLOC_TOTAL);
121 if (memory == NULL)
122 return 1;
123 memset (memory, 17, MAX_ALLOC_TOTAL);
124 result = 81;
126 else
128 /* Perform the test and test whether it triggers a permanent memory
129 allocation of more than MAX_ALLOC_TOTAL bytes. */
130 int repeat;
132 for (repeat = 0; repeat < NUM_ROUNDS; repeat++)
134 /* This may produce a temporary memory allocation of 11000 bytes.
135 but should not result in a permanent memory allocation. */
136 if (dprintf (STDOUT_FILENO, "%011000d\n", 17) == -1
137 && errno == ENOMEM)
138 return 2;
141 result = 0;
144 if (get_rusage_as () > initial_rusage_as + MAX_ALLOC_TOTAL + 100000)
145 return 3;
147 return result;
150 #endif /* ! address sanitizer enabled */
151 #endif /* !macOS */