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. */
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. */
31 fprintf (stderr
, "Skipping test: cannot trust address space size on this platform\n");
37 /* Skip this test when an address sanitizer is in use, since it would fail. */
39 # define __has_feature(a) 0
41 #if defined __SANITIZE_ADDRESS__ || __has_feature (address_sanitizer)
46 fprintf (stderr
, "Skipping test: address sanitizer's malloc behaves differently\n");
57 #if HAVE_GETRLIMIT && HAVE_SETRLIMIT
58 # include <sys/types.h>
59 # include <sys/time.h>
60 # include <sys/resource.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)
80 main (int argc
, char *argv
[])
82 uintptr_t initial_rusage_as
;
86 if (is_running_under_qemu_user ())
88 fprintf (stderr
, "Skipping test: cannot trust address space size when running under QEMU\n");
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
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
);
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 ();
113 if (initial_rusage_as
== 0)
117 arg
= atoi (argv
[1]);
120 void *memory
= malloc (MAX_ALLOC_TOTAL
);
123 memset (memory
, 17, MAX_ALLOC_TOTAL
);
128 /* Perform the test and test whether it triggers a permanent memory
129 allocation of more than MAX_ALLOC_TOTAL bytes. */
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
144 if (get_rusage_as () > initial_rusage_as
+ MAX_ALLOC_TOTAL
+ 100000)
150 #endif /* ! address sanitizer enabled */