From 45d39ca1cae74fcf58ec9911e771bc8baca63f66 Mon Sep 17 00:00:00 2001 From: Bruno Haible Date: Tue, 18 Jan 2011 20:11:59 +0100 Subject: [PATCH] *printf-posix: Avoid test failures. Make tests work on MacOS X, Cygwin. * tests/test-fprintf-posix3.c: Include "resource-ext.h". (main): Perform test also when getrlimit and setrlimit don't exist or when setrlimit of RLIMIT_DATA fails (like on Cygwin). Instead of limiting the address space size using setrlimit, compare the address space size before and after the the test. * tests/test-dprintf-posix2.c: Likewise. * tests/test-fprintf-posix3.sh: Update skip messages. * tests/test-dprintf-posix2.sh: Likewise. * modules/fprintf-posix-tests (Depends-on): Add get-rusage-as. * modules/dprintf-posix-tests (Depends-on): Likewise. Reported by Bruce Korb and Gary V. Vaughan . --- ChangeLog | 16 ++++++++ modules/dprintf-posix-tests | 1 + modules/fprintf-posix-tests | 1 + tests/test-dprintf-posix2.c | 91 ++++++++++++++++++++++---------------------- tests/test-dprintf-posix2.sh | 6 +-- tests/test-fprintf-posix3.c | 91 ++++++++++++++++++++++---------------------- tests/test-fprintf-posix3.sh | 6 +-- 7 files changed, 116 insertions(+), 96 deletions(-) diff --git a/ChangeLog b/ChangeLog index f99711917..00f9ebe8b 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,5 +1,21 @@ 2011-01-18 Bruno Haible + *printf-posix: Avoid test failures. Make tests work on MacOS X, Cygwin. + * tests/test-fprintf-posix3.c: Include "resource-ext.h". + (main): Perform test also when getrlimit and setrlimit don't exist or + when setrlimit of RLIMIT_DATA fails (like on Cygwin). Instead of + limiting the address space size using setrlimit, compare the address + space size before and after the the test. + * tests/test-dprintf-posix2.c: Likewise. + * tests/test-fprintf-posix3.sh: Update skip messages. + * tests/test-dprintf-posix2.sh: Likewise. + * modules/fprintf-posix-tests (Depends-on): Add get-rusage-as. + * modules/dprintf-posix-tests (Depends-on): Likewise. + Reported by Bruce Korb and + Gary V. Vaughan . + +2011-01-18 Bruno Haible + get-rusage-as: Improvement for Cygwin. * lib/get-rusage-as.c (get_rusage_as_via_iterator): On Windows, ignore areas that are merely reserved. diff --git a/modules/dprintf-posix-tests b/modules/dprintf-posix-tests index a60f8eda0..219761f77 100644 --- a/modules/dprintf-posix-tests +++ b/modules/dprintf-posix-tests @@ -9,6 +9,7 @@ tests/macros.h Depends-on: stdint +get-rusage-as configure.ac: AC_CHECK_FUNCS_ONCE([getrlimit setrlimit]) diff --git a/modules/fprintf-posix-tests b/modules/fprintf-posix-tests index 3eebaf9cb..d2058aeb2 100644 --- a/modules/fprintf-posix-tests +++ b/modules/fprintf-posix-tests @@ -12,6 +12,7 @@ tests/macros.h Depends-on: stdint +get-rusage-as configure.ac: AC_CHECK_FUNCS_ONCE([getrlimit setrlimit]) diff --git a/tests/test-dprintf-posix2.c b/tests/test-dprintf-posix2.c index 9fd869adc..047cc16fc 100644 --- a/tests/test-dprintf-posix2.c +++ b/tests/test-dprintf-posix2.c @@ -20,16 +20,19 @@ #include -#if HAVE_GETRLIMIT && HAVE_SETRLIMIT - #include -#include -#include -#include #include #include #include +#if HAVE_GETRLIMIT && HAVE_SETRLIMIT +# include +# include +# include +#endif + +#include "resource-ext.h" + /* Test against a memory leak in the fprintf replacement. */ /* Number of iterations across the loop. */ @@ -46,32 +49,32 @@ int main (int argc, char *argv[]) { - struct rlimit limit; + uintptr_t initial_rusage_as; int arg; - int repeat; + int result; /* Limit the amount of malloc()ed memory to MAX_ALLOC_TOTAL or less. */ /* On BSD systems, malloc() is limited by RLIMIT_DATA. */ -#ifdef RLIMIT_DATA - if (getrlimit (RLIMIT_DATA, &limit) < 0) - return 77; - if (limit.rlim_max == RLIM_INFINITY || limit.rlim_max > MAX_ALLOC_TOTAL) - limit.rlim_max = MAX_ALLOC_TOTAL; - limit.rlim_cur = limit.rlim_max; - if (setrlimit (RLIMIT_DATA, &limit) < 0) - return 77; +#if HAVE_GETRLIMIT && HAVE_SETRLIMIT && defined RLIMIT_DATA + { + struct rlimit limit; + + if (getrlimit (RLIMIT_DATA, &limit) >= 0) + { + if (limit.rlim_max == RLIM_INFINITY || limit.rlim_max > MAX_ALLOC_TOTAL) + limit.rlim_max = MAX_ALLOC_TOTAL; + limit.rlim_cur = limit.rlim_max; + (void) setrlimit (RLIMIT_DATA, &limit); + } + } #endif - /* On Linux systems, malloc() is limited by RLIMIT_AS. */ -#ifdef RLIMIT_AS - if (getrlimit (RLIMIT_AS, &limit) < 0) + /* On Linux systems, malloc() is limited by RLIMIT_AS. + On some systems, setrlimit of RLIMIT_AS doesn't work but get_rusage_as () + does. Allow the address space size to grow by at most MAX_ALLOC_TOTAL. */ + initial_rusage_as = get_rusage_as (); + if (initial_rusage_as == 0) return 77; - if (limit.rlim_max == RLIM_INFINITY || limit.rlim_max > MAX_ALLOC_TOTAL) - limit.rlim_max = MAX_ALLOC_TOTAL; - limit.rlim_cur = limit.rlim_max; - if (setrlimit (RLIMIT_AS, &limit) < 0) - return 77; -#endif arg = atoi (argv[1]); if (arg == 0) @@ -80,30 +83,28 @@ main (int argc, char *argv[]) if (memory == NULL) return 1; memset (memory, 17, MAX_ALLOC_TOTAL); - return 78; + result = 78; } - - /* Perform the test and test whether it triggers a permanent memory - allocation of more than MAX_ALLOC_TOTAL bytes. */ - - for (repeat = 0; repeat < NUM_ROUNDS; repeat++) + else { - /* This may produce a temporary memory allocation of 11000 bytes. - but should not result in a permanent memory allocation. */ - if (dprintf (STDOUT_FILENO, "%011000d\n", 17) == -1 - && errno == ENOMEM) - return 1; + /* Perform the test and test whether it triggers a permanent memory + allocation of more than MAX_ALLOC_TOTAL bytes. */ + int repeat; + + for (repeat = 0; repeat < NUM_ROUNDS; repeat++) + { + /* This may produce a temporary memory allocation of 11000 bytes. + but should not result in a permanent memory allocation. */ + if (dprintf (STDOUT_FILENO, "%011000d\n", 17) == -1 + && errno == ENOMEM) + return 1; + } + + result = 0; } - return 0; -} - -#else + if (get_rusage_as () > initial_rusage_as + MAX_ALLOC_TOTAL) + return 1; -int -main (int argc, char *argv[]) -{ - return 77; + return result; } - -#endif diff --git a/tests/test-dprintf-posix2.sh b/tests/test-dprintf-posix2.sh index f1aeacc71..fdaef9a80 100755 --- a/tests/test-dprintf-posix2.sh +++ b/tests/test-dprintf-posix2.sh @@ -9,14 +9,14 @@ ) 2>/dev/null malloc_result=$? if test $malloc_result = 77; then - echo "Skipping test: getrlimit and setrlimit don't work" + echo "Skipping test: no way to determine address space size" exit 77 fi ./test-dprintf-posix2${EXEEXT} 1 > /dev/null result=$? if test $result = 77; then - echo "Skipping test: getrlimit and setrlimit don't work" + echo "Skipping test: no way to determine address space size" exit 77 fi if test $result != 0; then @@ -24,7 +24,7 @@ if test $result != 0; then fi if test $malloc_result = 78; then - echo "Skipping test: getrlimit and setrlimit don't work" + echo "Skipping test: get_rusage_as() doesn't work" exit 77 fi diff --git a/tests/test-fprintf-posix3.c b/tests/test-fprintf-posix3.c index 90a2f7ce8..1f13fd484 100644 --- a/tests/test-fprintf-posix3.c +++ b/tests/test-fprintf-posix3.c @@ -20,15 +20,18 @@ #include -#if HAVE_GETRLIMIT && HAVE_SETRLIMIT - #include -#include -#include -#include #include #include +#if HAVE_GETRLIMIT && HAVE_SETRLIMIT +# include +# include +# include +#endif + +#include "resource-ext.h" + /* Test against a memory leak in the fprintf replacement. */ /* Number of iterations across the loop. */ @@ -45,32 +48,32 @@ int main (int argc, char *argv[]) { - struct rlimit limit; + uintptr_t initial_rusage_as; int arg; - int repeat; + int result; /* Limit the amount of malloc()ed memory to MAX_ALLOC_TOTAL or less. */ /* On BSD systems, malloc() is limited by RLIMIT_DATA. */ -#ifdef RLIMIT_DATA - if (getrlimit (RLIMIT_DATA, &limit) < 0) - return 77; - if (limit.rlim_max == RLIM_INFINITY || limit.rlim_max > MAX_ALLOC_TOTAL) - limit.rlim_max = MAX_ALLOC_TOTAL; - limit.rlim_cur = limit.rlim_max; - if (setrlimit (RLIMIT_DATA, &limit) < 0) - return 77; +#if HAVE_GETRLIMIT && HAVE_SETRLIMIT && defined RLIMIT_DATA + { + struct rlimit limit; + + if (getrlimit (RLIMIT_DATA, &limit) >= 0) + { + if (limit.rlim_max == RLIM_INFINITY || limit.rlim_max > MAX_ALLOC_TOTAL) + limit.rlim_max = MAX_ALLOC_TOTAL; + limit.rlim_cur = limit.rlim_max; + (void) setrlimit (RLIMIT_DATA, &limit); + } + } #endif - /* On Linux systems, malloc() is limited by RLIMIT_AS. */ -#ifdef RLIMIT_AS - if (getrlimit (RLIMIT_AS, &limit) < 0) + /* On Linux systems, malloc() is limited by RLIMIT_AS. + On some systems, setrlimit of RLIMIT_AS doesn't work but get_rusage_as () + does. Allow the address space size to grow by at most MAX_ALLOC_TOTAL. */ + initial_rusage_as = get_rusage_as (); + if (initial_rusage_as == 0) return 77; - if (limit.rlim_max == RLIM_INFINITY || limit.rlim_max > MAX_ALLOC_TOTAL) - limit.rlim_max = MAX_ALLOC_TOTAL; - limit.rlim_cur = limit.rlim_max; - if (setrlimit (RLIMIT_AS, &limit) < 0) - return 77; -#endif arg = atoi (argv[1]); if (arg == 0) @@ -79,30 +82,28 @@ main (int argc, char *argv[]) if (memory == NULL) return 1; memset (memory, 17, MAX_ALLOC_TOTAL); - return 78; + result = 78; } - - /* Perform the test and test whether it triggers a permanent memory - allocation of more than MAX_ALLOC_TOTAL bytes. */ - - for (repeat = 0; repeat < NUM_ROUNDS; repeat++) + else { - /* This may produce a temporary memory allocation of 11000 bytes. - but should not result in a permanent memory allocation. */ - if (fprintf (stdout, "%011000d\n", 17) == -1 - && errno == ENOMEM) - return 1; + /* Perform the test and test whether it triggers a permanent memory + allocation of more than MAX_ALLOC_TOTAL bytes. */ + int repeat; + + for (repeat = 0; repeat < NUM_ROUNDS; repeat++) + { + /* This may produce a temporary memory allocation of 11000 bytes. + but should not result in a permanent memory allocation. */ + if (fprintf (stdout, "%011000d\n", 17) == -1 + && errno == ENOMEM) + return 1; + } + + result = 0; } - return 0; -} - -#else + if (get_rusage_as () > initial_rusage_as + MAX_ALLOC_TOTAL) + return 1; -int -main (int argc, char *argv[]) -{ - return 77; + return result; } - -#endif diff --git a/tests/test-fprintf-posix3.sh b/tests/test-fprintf-posix3.sh index 6a6976d39..d9cbce963 100755 --- a/tests/test-fprintf-posix3.sh +++ b/tests/test-fprintf-posix3.sh @@ -9,14 +9,14 @@ ) 2>/dev/null malloc_result=$? if test $malloc_result = 77; then - echo "Skipping test: getrlimit and setrlimit don't work" + echo "Skipping test: no way to determine address space size" exit 77 fi ./test-fprintf-posix3${EXEEXT} 1 > /dev/null result=$? if test $result = 77; then - echo "Skipping test: getrlimit and setrlimit don't work" + echo "Skipping test: no way to determine address space size" exit 77 fi if test $result != 0; then @@ -24,7 +24,7 @@ if test $result != 0; then fi if test $malloc_result = 78; then - echo "Skipping test: getrlimit and setrlimit don't work" + echo "Skipping test: get_rusage_as() doesn't work" exit 77 fi -- 2.11.0