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. */
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. */
35 fprintf (stderr
, "Skipping test: cannot trust address space size on this platform\n");
41 /* Skip this test when an address sanitizer is in use, since it would fail. */
43 # define __has_feature(a) 0
45 #if defined __SANITIZE_ADDRESS__ || __has_feature (address_sanitizer)
50 fprintf (stderr
, "Skipping test: address sanitizer's malloc behaves differently\n");
60 #if HAVE_GETRLIMIT && HAVE_SETRLIMIT
61 # include <sys/types.h>
62 # include <sys/time.h>
63 # include <sys/resource.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)
83 main (int argc
, char *argv
[])
85 uintptr_t initial_rusage_as
;
89 if (is_running_under_qemu_user ())
91 fprintf (stderr
, "Skipping test: cannot trust address space size when running under QEMU\n");
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
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
);
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 ();
116 if (initial_rusage_as
== 0)
120 arg
= atoi (argv
[1]);
123 void *memory
= malloc (MAX_ALLOC_TOTAL
);
126 memset (memory
, 17, MAX_ALLOC_TOTAL
);
131 /* Perform the test and test whether it triggers a permanent memory
132 allocation of more than MAX_ALLOC_TOTAL bytes. */
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
147 if (get_rusage_as () > initial_rusage_as
+ MAX_ALLOC_TOTAL
+ 100000)
153 #endif /* ! address sanitizer enabled */