INN commit: trunk (4 files)
INN Commit
rra at isc.org
Thu Aug 18 13:52:54 UTC 2011
Date: Thursday, August 18, 2011 @ 06:52:54
Author: iulius
Revision: 9346
update xmalloc librairies from rra-c-util 3.7
* Switch the xmalloc_fail prototype from lib/xmalloc.c to include/inn/libinn.h.
* Update tests/lib/xmalloc.t to the new shell syntax with libtest.sh
for the test suite.
* Disable the check for RRA_MAINTAINER_TESTS. We'll see whether we should
keep it later.
* Use 3.5MB instead of 96KB for the tests/lib/xmalloc.t failing test.
* Use RLIMIT_AS instead of RLIMIT_DATA in tests/lib/xmalloc.c.
Modified:
trunk/include/inn/libinn.h
trunk/lib/xmalloc.c
trunk/tests/lib/xmalloc.c
trunk/tests/lib/xmalloc.t
----------------------+
include/inn/libinn.h | 3
lib/xmalloc.c | 1
tests/lib/xmalloc.c | 195 +++++++++++++++++++++++++++++++-------------
tests/lib/xmalloc.t | 213 ++++++++++++++++++++++++++++---------------------
4 files changed, 265 insertions(+), 147 deletions(-)
Modified: include/inn/libinn.h
===================================================================
--- include/inn/libinn.h 2011-08-18 13:38:03 UTC (rev 9345)
+++ include/inn/libinn.h 2011-08-18 13:52:54 UTC (rev 9346)
@@ -84,7 +84,8 @@
typedef void (*xmalloc_handler_type)(const char *, size_t, const char *, int);
/* The default error handler. */
-void xmalloc_fail(const char *, size_t, const char *, int);
+void xmalloc_fail(const char *, size_t, const char *, int)
+ __attribute__((__noreturn__));
/* Assign to this variable to choose a handler other than the default, which
just calls sysdie. */
Modified: lib/xmalloc.c
===================================================================
--- lib/xmalloc.c 2011-08-18 13:38:03 UTC (rev 9345)
+++ lib/xmalloc.c 2011-08-18 13:52:54 UTC (rev 9346)
@@ -65,7 +65,6 @@
#include "inn/libinn.h"
/* The default error handler. */
-void xmalloc_fail(const char *, size_t, const char *, int) __attribute__((__noreturn__));
void
xmalloc_fail(const char *function, size_t size, const char *file, int line)
{
Modified: tests/lib/xmalloc.c
===================================================================
--- tests/lib/xmalloc.c 2011-08-18 13:38:03 UTC (rev 9345)
+++ tests/lib/xmalloc.c 2011-08-18 13:52:54 UTC (rev 9346)
@@ -1,6 +1,36 @@
-/* $Id$ */
-/* Test suite for xmalloc and family. */
+/*
+ * Test suite for xmalloc and family.
+ *
+ * $Id$
+ *
+ * The canonical version of this file is maintained in the rra-c-util package,
+ * which can be found at <http://www.eyrie.org/~eagle/software/rra-c-util/>.
+ *
+ * Copyright 2000, 2001, 2006 Russ Allbery <rra at stanford.edu>
+ * Copyright 2008
+ * The Board of Trustees of the Leland Stanford Junior University
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a
+ * copy of this software and associated documentation files (the "Software"),
+ * to deal in the Software without restriction, including without limitation
+ * the rights to use, copy, modify, merge, publish, distribute, sublicense,
+ * and/or sell copies of the Software, and to permit persons to whom the
+ * Software is furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in
+ * all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
+ * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
+ * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
+ * DEALINGS IN THE SOFTWARE.
+ */
+#define LIBTEST_NEW_FORMAT 1
+
#include "config.h"
#include "clibrary.h"
#include <ctype.h>
@@ -16,17 +46,25 @@
#include "inn/messages.h"
#include "inn/libinn.h"
-/* A customized error handler for checking xmalloc's support of them.
- Prints out the error message and exits with status 1. */
+/* Adjust the beginning of the test file. */
+#line 15 "xmalloc.c"
+
+/*
+ * A customized error handler for checking xmalloc's support of them. Prints
+ * out the error message and exits with status 1.
+ */
static void
test_handler(const char *function, size_t size, const char *file, int line)
{
die("%s %lu %s %d", function, (unsigned long) size, file, line);
}
-/* Allocate the amount of memory given and write to all of it to make sure
- we can, returning true if that succeeded and false on any sort of
- detectable error. */
+
+/*
+ * Allocate the amount of memory given and write to all of it to make sure we
+ * can, returning true if that succeeded and false on any sort of detectable
+ * error.
+ */
static int
test_malloc(size_t size)
{
@@ -45,38 +83,42 @@
return 1;
}
-/* Allocate half the memory given, write to it, then reallocate to the
- desired size, writing to the rest and then checking it all. Returns true
- on success, false on any failure. */
+
+/*
+ * Allocate 10 bytes, write to it, then reallocate to the desired
+ * size, writing to the rest and then checking it all. Returns true on
+ * success, false on any failure.
+ */
static int
test_realloc(size_t size)
{
char *buffer;
size_t i;
- buffer = xmalloc(size / 2);
+ buffer = xmalloc(10);
if (buffer == NULL)
return 0;
- if (size / 2 > 0)
- memset(buffer, 1, size / 2);
+ memset(buffer, 1, 10);
buffer = xrealloc(buffer, size);
if (buffer == NULL)
return 0;
if (size > 0)
- memset(buffer + size / 2, 2, size - size / 2);
- for (i = 0; i < size / 2; i++)
+ memset(buffer + 10, 2, size - 10);
+ for (i = 0; i < 10; i++)
if (buffer[i] != 1)
return 0;
- for (i = size / 2; i < size; i++)
+ for (i = 10; i < size; i++)
if (buffer[i] != 2)
return 0;
free(buffer);
return 1;
}
-/* Generate a string of the size indicated, call xstrdup on it, and then
- ensure the result matches. Returns true on success, false on any
- failure. */
+
+/*
+ * Generate a string of the size indicated, call xstrdup on it, and then
+ * ensure the result matches. Returns true on success, false on any failure.
+ */
static int
test_strdup(size_t size)
{
@@ -97,9 +139,12 @@
return (match == 0);
}
-/* Generate a string of the size indicated plus some, call xstrndup on it, and
- then ensure the result matches. Returns true on success, false on any
- failure. */
+
+/*
+ * Generate a string of the size indicated plus some, call xstrndup on it, and
+ * then ensure the result matches. Returns true on success, false on any
+ * failure.
+ */
static int
test_strndup(size_t size)
{
@@ -122,9 +167,11 @@
return (match == 0 && toomuch != 0);
}
-/* Allocate the amount of memory given and check that it's all zeroed,
- returning true if that succeeded and false on any sort of detectable
- error. */
+
+/*
+ * Allocate the amount of memory given and check that it's all zeroed,
+ * returning true if that succeeded and false on any sort of detectable error.
+ */
static int
test_calloc(size_t size)
{
@@ -144,19 +191,25 @@
return 1;
}
-/* Test asprintf with a large string (essentially using it as strdup).
- Returns true if successful, false otherwise. */
+
+/*
+ * Test asprintf with a large string (essentially using it as strdup).
+ * Returns true if successful, false otherwise.
+ */
static int
test_asprintf(size_t size)
{
char *copy, *string;
+ int status;
size_t i;
string = xmalloc(size);
memset(string, 42, size - 1);
string[size - 1] = '\0';
- xasprintf(©, "%s", string);
+ status = xasprintf(©, "%s", string);
free(string);
+ if (status < 0)
+ return 0;
for (i = 0; i < size - 1; i++)
if (copy[i] != 42)
return 0;
@@ -166,6 +219,7 @@
return 1;
}
+
/* Wrapper around vasprintf to do the va_list stuff. */
static int
xvasprintf_wrapper(char **strp, const char *format, ...)
@@ -179,19 +233,25 @@
return status;
}
-/* Test vasprintf with a large string (essentially using it as strdup).
- Returns true if successful, false otherwise. */
+
+/*
+ * Test vasprintf with a large string (essentially using it as strdup).
+ * Returns true if successful, false otherwise.
+ */
static int
test_vasprintf(size_t size)
{
char *copy, *string;
+ int status;
size_t i;
string = xmalloc(size);
memset(string, 42, size - 1);
string[size - 1] = '\0';
- xvasprintf_wrapper(©, "%s", string);
+ status = xvasprintf_wrapper(©, "%s", string);
free(string);
+ if (status < 0)
+ return 0;
for (i = 0; i < size - 1; i++)
if (copy[i] != 42)
return 0;
@@ -201,8 +261,11 @@
return 1;
}
-/* Take the amount of memory to allocate in bytes as a command-line argument
- and call test_malloc with that amount of memory. */
+
+/*
+ * Take the amount of memory to allocate in bytes as a command-line argument
+ * and call test_malloc with that amount of memory.
+ */
int
main(int argc, char *argv[])
{
@@ -210,7 +273,6 @@
size_t limit = 0;
int willfail = 0;
unsigned char code;
- struct rlimit rl;
if (argc < 3)
die("Usage error. Type, size, and limit must be given.");
@@ -223,41 +285,60 @@
if (limit == 0 && errno != 0)
sysdie("Invalid limit");
- /* If a memory limit was given and we can set memory limits, set it.
- Otherwise, exit 2, signalling to the driver that the test should be
- skipped. We do this here rather than in the driver due to some
- pathological problems with Linux (setting ulimit in the shell caused
- the shell to die). */
+ /* If the code is capitalized, install our customized error handler. */
+ code = argv[1][0];
+ if (isupper(code)) {
+ xmalloc_error_handler = test_handler;
+ code = tolower(code);
+ }
+
+ /*
+ * Decide if the allocation should fail. If it should, set willfail to 2,
+ * so that if it unexpectedly succeeds, we exit with a status indicating
+ * that the test should be skipped.
+ */
+ max = size;
+ if (code == 's' || code == 'n' || code == 'a' || code == 'v') {
+ max += size;
+ if (limit > 0)
+ limit += size;
+ }
+ if (limit > 0 && max > limit)
+ willfail = 2;
+
+ /*
+ * If a memory limit was given and we can set memory limits, set it.
+ * Otherwise, exit 2, signalling to the driver that the test should be
+ * skipped. We do this here rather than in the driver due to some
+ * pathological problems with Linux (setting ulimit in the shell caused
+ * the shell to die).
+ */
if (limit > 0) {
-#if HAVE_SETRLIMIT && defined(RLIMIT_DATA)
+#if HAVE_SETRLIMIT && defined(RLIMIT_AS)
+ struct rlimit rl;
+ void *tmp;
+
rl.rlim_cur = limit;
rl.rlim_max = limit;
- if (setrlimit(RLIMIT_DATA, &rl) < 0) {
+ if (setrlimit(RLIMIT_AS, &rl) < 0) {
syswarn("Can't set data limit to %lu", (unsigned long) limit);
exit(2);
}
+ if (size < limit || code == 'r') {
+ tmp = malloc(code == 'r' ? 10 : size);
+ if (tmp == NULL) {
+ syswarn("Can't allocate initial memory of %lu",
+ (unsigned long) size);
+ exit(2);
+ }
+ free(tmp);
+ }
#else
warn("Data limits aren't supported.");
exit(2);
#endif
}
- /* If the code is capitalized, install our customized error handler. */
- code = argv[1][0];
- if (isupper((unsigned char) code)) {
- xmalloc_error_handler = test_handler;
- code = tolower(code);
- }
-
- /* Decide if the allocation should fail. If it should, set willfail to
- 2, so that if it unexpectedly succeeds, we exit with a status
- indicating that the test should be skipped. */
- max = size;
- if (code == 's' || code == 'n' || code == 'a' || code == 'v')
- max *= 2;
- if (limit > 0 && max > limit)
- willfail = 2;
-
switch (code) {
case 'c': exit(test_calloc(size) ? willfail : 1);
case 'm': exit(test_malloc(size) ? willfail : 1);
Modified: tests/lib/xmalloc.t
===================================================================
--- tests/lib/xmalloc.t 2011-08-18 13:38:03 UTC (rev 9345)
+++ tests/lib/xmalloc.t 2011-08-18 13:52:54 UTC (rev 9346)
@@ -1,109 +1,146 @@
#! /bin/sh
+#
+# Test suite for xmalloc and friends.
+#
# $Id$
#
-# Test suite for xmalloc and friends.
+# The canonical version of this file is maintained in the rra-c-util package,
+# which can be found at <http://www.eyrie.org/~eagle/software/rra-c-util/>.
+#
+# Written by Russ Allbery <rra at stanford.edu>
+# Copyright 2000, 2001, 2006 Russ Allbery <rra at stanford.edu>
+# Copyright 2008, 2009, 2010
+# The Board of Trustees of the Leland Stanford Junior University
+#
+# Permission is hereby granted, free of charge, to any person obtaining a
+# copy of this software and associated documentation files (the "Software"),
+# to deal in the Software without restriction, including without limitation
+# the rights to use, copy, modify, merge, publish, distribute, sublicense,
+# and/or sell copies of the Software, and to permit persons to whom the
+# Software is furnished to do so, subject to the following conditions:
+#
+# The above copyright notice and this permission notice shall be included in
+# all copies or substantial portions of the Software.
+#
+# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
+# THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
+# FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
+# DEALINGS IN THE SOFTWARE.
-# The count starts at 1 and is updated each time ok is printed. printcount
-# takes "ok" or "not ok".
-count=1
-printcount () {
- echo "$1 $count $2"
- count=`expr $count + 1`
-}
+. "$SOURCE/libtest.sh"
+cd "$BUILD/lib"
-# Run a program expected to succeed, and print ok if it does.
-runsuccess () {
- output=`$xmalloc "$1" "$2" "$3" 2>&1 >/dev/null`
+# Run an xmalloc test. Takes the description, the expectd exit status, the
+# output, and the arguments.
+ok_xmalloc () {
+ local desc w_status w_output output status
+ desc="$1"
+ shift
+ w_status="$1"
+ shift
+ w_output="$1"
+ shift
+ output=`./xmalloc "$@" 2>&1`
status=$?
- if test $status = 0 && test -z "$output" ; then
- printcount "ok"
- else
- if test $status = 2 ; then
- printcount "ok" "# skip - no data limit support"
- else
- printcount "not ok"
- echo " $output"
- fi
+ if [ "$w_status" -ne 0 ] ; then
+ output=`echo "$output" | sed 's/:.*//'`
fi
-}
-
-# Run a program expected to fail and make sure it fails with an exit status
-# of 2 and the right failure message. Strip the colon and everything after
-# it off the error message since it's system-specific.
-runfailure () {
- output=`$xmalloc "$1" "$2" "$3" 2>&1 >/dev/null`
- status=$?
- output=`echo "$output" | sed 's/:.*//'`
- if test $status = 1 && test x"$output" = x"$4" ; then
- printcount "ok"
+ if [ $status = $w_status ] && [ x"$output" = x"$w_output" ] ; then
+ ok "$desc" true
+ elif [ $status = 2 ] ; then
+ skip "no data limit support"
else
- if test $status = 2 ; then
- printcount "ok" "# skip - no data limit support"
- else
- printcount "not ok"
- echo " saw: $output"
- echo " not: $4"
- fi
+ echo "# saw: ($status) $output"
+ echo "# not: ($w_status) $w_output"
+ ok "$desc" false
fi
}
-# Find where the helper program is.
-xmalloc=xmalloc
-for file in ./xmalloc lib/xmalloc ../xmalloc ; do
- [ -x $file ] && xmalloc=$file
-done
+# Skip this test suite unless maintainer-mode tests are enabled. All of the
+# failures in automated testing have been problems with the assumptions around
+# memory allocation or problems with the test suite, not problems with the
+# underlying xmalloc code.
+#if [ -z "$RRA_MAINTAINER_TESTS" ] ; then
+# skip_all 'xmalloc tests only run for maintainer'
+#fi
# Total tests.
-echo 36
+plan 36
# First run the tests expected to succeed.
-runsuccess "m" "21" "0"
-runsuccess "m" "128000" "0"
-runsuccess "m" "0" "0"
-runsuccess "r" "21" "0"
-runsuccess "r" "128000" "0"
-runsuccess "s" "21" "0"
-runsuccess "s" "128000" "0"
-runsuccess "n" "21" "0"
-runsuccess "n" "128000" "0"
-runsuccess "c" "24" "0"
-runsuccess "c" "128000" "0"
-runsuccess "a" "24" "0"
-runsuccess "a" "128000" "0"
-runsuccess "v" "24" "0"
-runsuccess "v" "128000" "0"
+ok_xmalloc "malloc small" 0 "" "m" "21" "0"
+ok_xmalloc "malloc large" 0 "" "m" "3500000" "0"
+ok_xmalloc "malloc zero" 0 "" "m" "0" "0"
+ok_xmalloc "realloc small" 0 "" "r" "21" "0"
+ok_xmalloc "realloc large" 0 "" "r" "3500000" "0"
+ok_xmalloc "strdup small" 0 "" "s" "21" "0"
+ok_xmalloc "strdup large" 0 "" "s" "3500000" "0"
+ok_xmalloc "strndup small" 0 "" "n" "21" "0"
+ok_xmalloc "strndup large" 0 "" "n" "3500000" "0"
+ok_xmalloc "calloc small" 0 "" "c" "24" "0"
+ok_xmalloc "calloc large" 0 "" "c" "3500000" "0"
+ok_xmalloc "asprintf small" 0 "" "a" "24" "0"
+ok_xmalloc "asprintf large" 0 "" "a" "3500000" "0"
+ok_xmalloc "vasprintf small" 0 "" "v" "24" "0"
+ok_xmalloc "vasprintf large" 0 "" "v" "3500000" "0"
-# Now limit our memory to 96KB and then try the large ones again, all of
+# Now limit our memory to 3.5MB and then try the large ones again, all of
# which should fail.
-runfailure "m" "128000" "96000" \
- "failed to malloc 128000 bytes at lib/xmalloc.c line 36"
-runfailure "r" "128000" "96000" \
- "failed to realloc 128000 bytes at lib/xmalloc.c line 62"
-runfailure "s" "64000" "96000" \
- "failed to strdup 64000 bytes at lib/xmalloc.c line 91"
-runfailure "n" "64000" "96000" \
- "failed to strndup 64000 bytes at lib/xmalloc.c line 115"
-runfailure "c" "128000" "96000" \
- "failed to calloc 128000 bytes at lib/xmalloc.c line 137"
-runfailure "a" "64000" "96000" \
- "failed to asprintf 64000 bytes at lib/xmalloc.c line 159"
-runfailure "v" "64000" "96000" \
- "failed to vasprintf 64000 bytes at lib/xmalloc.c line 178"
+#
+# The exact memory limits used here are essentially black magic. They need to
+# be large enough to allow the program to be loaded and do small allocations,
+# but not so large that we can't reasonably expect to allocate that much
+# memory normally. 3.5MB seems to work reasonably well on both Solaris and
+# Linux.
+#
+# We assume that there are enough miscellaneous allocations that an allocation
+# exactly as large as the limit will always fail.
+ok_xmalloc "malloc fail" 1 \
+ "failed to malloc 3500000 bytes at xmalloc.c line 38" \
+ "m" "3500000" "3500000"
+ok_xmalloc "realloc fail" 1 \
+ "failed to realloc 3500000 bytes at xmalloc.c line 66" \
+ "r" "3500000" "3500000"
+ok_xmalloc "strdup fail" 1 \
+ "failed to strdup 3500000 bytes at xmalloc.c line 97" \
+ "s" "3500000" "3500000"
+ok_xmalloc "strndup fail" 1 \
+ "failed to strndup 3500000 bytes at xmalloc.c line 124" \
+ "n" "3500000" "3500000"
+ok_xmalloc "calloc fail" 1 \
+ "failed to calloc 3500000 bytes at xmalloc.c line 148" \
+ "c" "3500000" "3500000"
+ok_xmalloc "asprintf fail" 1 \
+ "failed to asprintf 3500000 bytes at xmalloc.c line 173" \
+ "a" "3500000" "3500000"
+ok_xmalloc "vasprintf fail" 1 \
+ "failed to vasprintf 3500000 bytes at xmalloc.c line 195" \
+ "v" "3500000" "3500000"
# Check our custom error handler.
-runfailure "M" "128000" "96000" "malloc 128000 lib/xmalloc.c 36"
-runfailure "R" "128000" "96000" "realloc 128000 lib/xmalloc.c 62"
-runfailure "S" "64000" "96000" "strdup 64000 lib/xmalloc.c 91"
-runfailure "N" "64000" "96000" "strndup 64000 lib/xmalloc.c 115"
-runfailure "C" "128000" "96000" "calloc 128000 lib/xmalloc.c 137"
-runfailure "A" "64000" "96000" "asprintf 64000 lib/xmalloc.c 159"
-runfailure "V" "64000" "96000" "vasprintf 64000 lib/xmalloc.c 178"
+ok_xmalloc "malloc custom" 1 "malloc 3500000 xmalloc.c 38" \
+ "M" "3500000" "3500000"
+ok_xmalloc "realloc custom" 1 "realloc 3500000 xmalloc.c 66" \
+ "R" "3500000" "3500000"
+ok_xmalloc "strdup custom" 1 "strdup 3500000 xmalloc.c 97" \
+ "S" "3500000" "3500000"
+ok_xmalloc "strndup custom" 1 "strndup 3500000 xmalloc.c 124" \
+ "N" "3500000" "3500000"
+ok_xmalloc "calloc custom" 1 "calloc 3500000 xmalloc.c 148" \
+ "C" "3500000" "3500000"
+ok_xmalloc "asprintf custom" 1 "asprintf 3500000 xmalloc.c 173" \
+ "A" "3500000" "3500000"
+ok_xmalloc "vasprintf custom" 1 "vasprintf 3500000 xmalloc.c 195" \
+ "V" "3500000" "3500000"
# Check the smaller ones again just for grins.
-runsuccess "m" "21" "96000"
-runsuccess "r" "32" "96000"
-runsuccess "s" "64" "96000"
-runsuccess "n" "20" "96000"
-runsuccess "c" "24" "96000"
-runsuccess "a" "30" "96000"
-runsuccess "v" "35" "96000"
+ok_xmalloc "malloc retry" 0 "" "m" "21" "3500000"
+ok_xmalloc "realloc retry" 0 "" "r" "32" "3500000"
+ok_xmalloc "strdup retry" 0 "" "s" "64" "3500000"
+ok_xmalloc "strndup retry" 0 "" "n" "20" "3500000"
+ok_xmalloc "calloc retry" 0 "" "c" "24" "3500000"
+ok_xmalloc "asprintf retry" 0 "" "a" "30" "3500000"
+ok_xmalloc "vasprintf retry" 0 "" "v" "35" "3500000"
More information about the inn-committers
mailing list