root/maint/gnulib/tests/test-fprintf-posix3.c

/* [previous][next][first][last][top][bottom][index][help] */

DEFINITIONS

This source file includes following definitions.
  1. main
  2. main

   1 /* Test of POSIX compatible fprintf() function.
   2    Copyright (C) 2009-2021 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 <bruno@clisp.org>, 2009.  */
  18 
  19 #include <config.h>
  20 
  21 #include <stdio.h>
  22 
  23 #if defined __APPLE__ && defined __MACH__                            /* macOS */
  24 
  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.  */
  31 
  32 int
  33 main ()
     /* [previous][next][first][last][top][bottom][index][help] */
  34 {
  35   fprintf (stderr, "Skipping test: cannot trust address space size on this platform\n");
  36   return 78;
  37 }
  38 
  39 #else
  40 
  41 #include <stdlib.h>
  42 #include <string.h>
  43 #include <errno.h>
  44 
  45 #if HAVE_GETRLIMIT && HAVE_SETRLIMIT
  46 # include <sys/types.h>
  47 # include <sys/time.h>
  48 # include <sys/resource.h>
  49 #endif
  50 
  51 #include "qemu.h"
  52 #include "resource-ext.h"
  53 
  54 /* Test against a memory leak in the fprintf replacement.  */
  55 
  56 /* Number of iterations across the loop.  */
  57 #define NUM_ROUNDS 1000
  58 
  59 /* Number of bytes that are allowed to escape per round.  */
  60 #define MAX_ALLOC_ROUND 10000
  61 
  62 /* Number of bytes that are allowed to escape in total.
  63    This should be at least 10 MB, since it includes the normal memory
  64    or address space of the test program.  */
  65 #define MAX_ALLOC_TOTAL (NUM_ROUNDS * MAX_ALLOC_ROUND)
  66 
  67 int
  68 main (int argc, char *argv[])
     /* [previous][next][first][last][top][bottom][index][help] */
  69 {
  70   uintptr_t initial_rusage_as;
  71   int arg;
  72   int result;
  73 
  74   if (is_running_under_qemu_user ())
  75     {
  76       fprintf (stderr, "Skipping test: cannot trust address space size when running under QEMU\n");
  77       return 79;
  78     }
  79 
  80   /* Limit the amount of malloc()ed memory to MAX_ALLOC_TOTAL or less.  */
  81 
  82   /* On AIX systems, malloc() is limited by RLIMIT_DATA.  */
  83 #if HAVE_GETRLIMIT && HAVE_SETRLIMIT && defined RLIMIT_DATA
  84   {
  85     struct rlimit limit;
  86 
  87     if (getrlimit (RLIMIT_DATA, &limit) >= 0)
  88       {
  89         if (limit.rlim_max == RLIM_INFINITY || limit.rlim_max > MAX_ALLOC_TOTAL)
  90           limit.rlim_max = MAX_ALLOC_TOTAL;
  91         limit.rlim_cur = limit.rlim_max;
  92         (void) setrlimit (RLIMIT_DATA, &limit);
  93       }
  94   }
  95 #endif
  96   /* On all systems except AIX and OpenBSD, malloc() is limited by RLIMIT_AS.
  97      On some systems, setrlimit of RLIMIT_AS doesn't work but get_rusage_as ()
  98      does.  Allow the address space size to grow by at most MAX_ALLOC_TOTAL.  */
  99   initial_rusage_as = get_rusage_as ();
 100 #if !defined _AIX
 101   if (initial_rusage_as == 0)
 102     return 77;
 103 #endif
 104 
 105   arg = atoi (argv[1]);
 106   if (arg == 0)
 107     {
 108       void *memory = malloc (MAX_ALLOC_TOTAL);
 109       if (memory == NULL)
 110         return 1;
 111       memset (memory, 17, MAX_ALLOC_TOTAL);
 112       result = 80;
 113     }
 114   else
 115     {
 116       /* Perform the test and test whether it triggers a permanent memory
 117          allocation of more than MAX_ALLOC_TOTAL bytes.  */
 118       int repeat;
 119 
 120       for (repeat = 0; repeat < NUM_ROUNDS; repeat++)
 121         {
 122           /* This may produce a temporary memory allocation of 11000 bytes.
 123              but should not result in a permanent memory allocation.  */
 124           if (fprintf (stdout, "%011000d\n", 17) == -1
 125               && errno == ENOMEM)
 126             return 2;
 127         }
 128 
 129       result = 0;
 130     }
 131 
 132   if (get_rusage_as () > initial_rusage_as + MAX_ALLOC_TOTAL + 100000)
 133     return 3;
 134 
 135   return result;
 136 }
 137 
 138 #endif /* !macOS */

/* [previous][next][first][last][top][bottom][index][help] */