| 1 | /* Test of POSIX compatible fprintf() function. |
| 2 | Copyright (C) 2009-2020 Free Software Foundation, Inc. |
| 3 | |
| 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. |
| 8 | |
| 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. |
| 13 | |
| 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/>. */ |
| 16 | |
| 17 | /* Written by Bruno Haible <[email protected]>, 2009. */ |
| 18 | |
| 19 | #include <config.h> |
| 20 | |
| 21 | #include <stdio.h> |
| 22 | |
| 23 | #include <stdlib.h> |
| 24 | #include <string.h> |
| 25 | #include <errno.h> |
| 26 | |
| 27 | #if HAVE_GETRLIMIT && HAVE_SETRLIMIT |
| 28 | # include <sys/types.h> |
| 29 | # include <sys/time.h> |
| 30 | # include <sys/resource.h> |
| 31 | #endif |
| 32 | |
| 33 | #include "resource-ext.h" |
| 34 | |
| 35 | /* Test against a memory leak in the fprintf replacement. */ |
| 36 | |
| 37 | /* Number of iterations across the loop. */ |
| 38 | #define NUM_ROUNDS 1000 |
| 39 | |
| 40 | /* Number of bytes that are allowed to escape per round. */ |
| 41 | #define MAX_ALLOC_ROUND 10000 |
| 42 | |
| 43 | /* Number of bytes that are allowed to escape in total. |
| 44 | This should be at least 10 MB, since it includes the normal memory |
| 45 | or address space of the test program. */ |
| 46 | #define MAX_ALLOC_TOTAL (NUM_ROUNDS * MAX_ALLOC_ROUND) |
| 47 | |
| 48 | int |
| 49 | main (int argc, char *argv[]) |
| 50 | { |
| 51 | uintptr_t initial_rusage_as; |
| 52 | int arg; |
| 53 | int result; |
| 54 | |
| 55 | /* Limit the amount of malloc()ed memory to MAX_ALLOC_TOTAL or less. */ |
| 56 | |
| 57 | /* On AIX systems, malloc() is limited by RLIMIT_DATA. */ |
| 58 | #if HAVE_GETRLIMIT && HAVE_SETRLIMIT && defined RLIMIT_DATA |
| 59 | { |
| 60 | struct rlimit limit; |
| 61 | |
| 62 | if (getrlimit (RLIMIT_DATA, &limit) >= 0) |
| 63 | { |
| 64 | if (limit.rlim_max == RLIM_INFINITY || limit.rlim_max > MAX_ALLOC_TOTAL) |
| 65 | limit.rlim_max = MAX_ALLOC_TOTAL; |
| 66 | limit.rlim_cur = limit.rlim_max; |
| 67 | (void) setrlimit (RLIMIT_DATA, &limit); |
| 68 | } |
| 69 | } |
| 70 | #endif |
| 71 | /* On all systems except AIX and OpenBSD, malloc() is limited by RLIMIT_AS. |
| 72 | On some systems, setrlimit of RLIMIT_AS doesn't work but get_rusage_as () |
| 73 | does. Allow the address space size to grow by at most MAX_ALLOC_TOTAL. */ |
| 74 | initial_rusage_as = get_rusage_as (); |
| 75 | #if !defined _AIX |
| 76 | if (initial_rusage_as == 0) |
| 77 | return 77; |
| 78 | #endif |
| 79 | |
| 80 | arg = atoi (argv[1]); |
| 81 | if (arg == 0) |
| 82 | { |
| 83 | void *memory = malloc (MAX_ALLOC_TOTAL); |
| 84 | if (memory == NULL) |
| 85 | return 1; |
| 86 | memset (memory, 17, MAX_ALLOC_TOTAL); |
| 87 | result = 78; |
| 88 | } |
| 89 | else |
| 90 | { |
| 91 | /* Perform the test and test whether it triggers a permanent memory |
| 92 | allocation of more than MAX_ALLOC_TOTAL bytes. */ |
| 93 | int repeat; |
| 94 | |
| 95 | for (repeat = 0; repeat < NUM_ROUNDS; repeat++) |
| 96 | { |
| 97 | /* This may produce a temporary memory allocation of 11000 bytes. |
| 98 | but should not result in a permanent memory allocation. */ |
| 99 | if (fprintf (stdout, "%011000d\n", 17) == -1 |
| 100 | && errno == ENOMEM) |
| 101 | return 1; |
| 102 | } |
| 103 | |
| 104 | result = 0; |
| 105 | } |
| 106 | |
| 107 | if (get_rusage_as () > initial_rusage_as + MAX_ALLOC_TOTAL) |
| 108 | return 1; |
| 109 | |
| 110 | return result; |
| 111 | } |