usleep tests: Avoid failure due to known Cygwin 3.5.3 bug.
[gnulib.git] / tests / test-fprintf-posix3.c
blobf2d31e71ac6252060e0386f78f95787f37d85aec
1 /* Test of POSIX compatible fprintf() 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 10.13, this test fails, because the address space size increases
26 by 10 MB to 42 MB during the test's execution. But it's not a malloc
27 leak, as can be seen by running the 'leaks' program. And it does not fail
28 if the test's output is redirected to /dev/null. Probably piping a lot
29 of output to stdout, when not redirected to /dev/null, allocates intermediate
30 buffers in the virtual address space. */
32 int
33 main ()
35 fprintf (stderr, "Skipping test: cannot trust address space size on this platform\n");
36 return 78;
39 #else
41 /* Skip this test when an address sanitizer is in use, since it would fail. */
42 #ifndef __has_feature
43 # define __has_feature(a) 0
44 #endif
45 #if defined __SANITIZE_ADDRESS__ || __has_feature (address_sanitizer)
47 int
48 main ()
50 fprintf (stderr, "Skipping test: address sanitizer's malloc behaves differently\n");
51 return 80;
54 #else
56 #include <stdlib.h>
57 #include <string.h>
58 #include <errno.h>
60 #if HAVE_GETRLIMIT && HAVE_SETRLIMIT
61 # include <sys/types.h>
62 # include <sys/time.h>
63 # include <sys/resource.h>
64 #endif
66 #include "qemu.h"
67 #include "resource-ext.h"
69 /* Test against a memory leak in the fprintf replacement. */
71 /* Number of iterations across the loop. */
72 #define NUM_ROUNDS 1000
74 /* Number of bytes that are allowed to escape per round. */
75 #define MAX_ALLOC_ROUND 10000
77 /* Number of bytes that are allowed to escape in total.
78 This should be at least 10 MB, since it includes the normal memory
79 or address space of the test program. */
80 #define MAX_ALLOC_TOTAL (NUM_ROUNDS * MAX_ALLOC_ROUND)
82 int
83 main (int argc, char *argv[])
85 uintptr_t initial_rusage_as;
86 int arg;
87 int result;
89 if (is_running_under_qemu_user ())
91 fprintf (stderr, "Skipping test: cannot trust address space size when running under QEMU\n");
92 return 79;
95 /* Limit the amount of malloc()ed memory to MAX_ALLOC_TOTAL or less. */
97 /* On AIX systems, malloc() is limited by RLIMIT_DATA. */
98 #if HAVE_GETRLIMIT && HAVE_SETRLIMIT && defined RLIMIT_DATA
100 struct rlimit limit;
102 if (getrlimit (RLIMIT_DATA, &limit) >= 0)
104 if (limit.rlim_max == RLIM_INFINITY || limit.rlim_max > MAX_ALLOC_TOTAL)
105 limit.rlim_max = MAX_ALLOC_TOTAL;
106 limit.rlim_cur = limit.rlim_max;
107 (void) setrlimit (RLIMIT_DATA, &limit);
110 #endif
111 /* On all systems except AIX and OpenBSD, malloc() is limited by RLIMIT_AS.
112 On some systems, setrlimit of RLIMIT_AS doesn't work but get_rusage_as ()
113 does. Allow the address space size to grow by at most MAX_ALLOC_TOTAL. */
114 initial_rusage_as = get_rusage_as ();
115 #if !defined _AIX
116 if (initial_rusage_as == 0)
117 return 77;
118 #endif
120 arg = atoi (argv[1]);
121 if (arg == 0)
123 void *memory = malloc (MAX_ALLOC_TOTAL);
124 if (memory == NULL)
125 return 1;
126 memset (memory, 17, MAX_ALLOC_TOTAL);
127 result = 81;
129 else
131 /* Perform the test and test whether it triggers a permanent memory
132 allocation of more than MAX_ALLOC_TOTAL bytes. */
133 int repeat;
135 for (repeat = 0; repeat < NUM_ROUNDS; repeat++)
137 /* This may produce a temporary memory allocation of 11000 bytes.
138 but should not result in a permanent memory allocation. */
139 if (fprintf (stdout, "%011000d\n", 17) == -1
140 && errno == ENOMEM)
141 return 2;
144 result = 0;
147 if (get_rusage_as () > initial_rusage_as + MAX_ALLOC_TOTAL + 100000)
148 return 3;
150 return result;
153 #endif /* ! address sanitizer enabled */
154 #endif /* !macOS */