| 1 | #! /bin/sh |
| 2 | # |
| 3 | # Test suite for xmalloc and friends. |
| 4 | # |
| 5 | # The canonical version of this file is maintained in the rra-c-util package, |
| 6 | # which can be found at <http://www.eyrie.org/~eagle/software/rra-c-util/>. |
| 7 | # |
| 8 | # Written by Russ Allbery <[email protected]> |
| 9 | # Copyright 2000, 2001, 2006, 2014 Russ Allbery <[email protected]> |
| 10 | # Copyright 2008, 2009, 2010, 2012 |
| 11 | # The Board of Trustees of the Leland Stanford Junior University |
| 12 | # |
| 13 | # Permission is hereby granted, free of charge, to any person obtaining a |
| 14 | # copy of this software and associated documentation files (the "Software"), |
| 15 | # to deal in the Software without restriction, including without limitation |
| 16 | # the rights to use, copy, modify, merge, publish, distribute, sublicense, |
| 17 | # and/or sell copies of the Software, and to permit persons to whom the |
| 18 | # Software is furnished to do so, subject to the following conditions: |
| 19 | # |
| 20 | # The above copyright notice and this permission notice shall be included in |
| 21 | # all copies or substantial portions of the Software. |
| 22 | # |
| 23 | # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 24 | # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 25 | # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL |
| 26 | # THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 27 | # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING |
| 28 | # FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER |
| 29 | # DEALINGS IN THE SOFTWARE. |
| 30 | |
| 31 | . "$SOURCE/tap/libtap.sh" |
| 32 | cd "$BUILD/util" |
| 33 | |
| 34 | # Run an xmalloc test. Takes the description, the expectd exit status, the |
| 35 | # output, and the arguments. |
| 36 | ok_xmalloc () { |
| 37 | local desc w_status w_output output status |
| 38 | desc="$1" |
| 39 | shift |
| 40 | w_status="$1" |
| 41 | shift |
| 42 | w_output="$1" |
| 43 | shift |
| 44 | output=`strip_colon_error ./xmalloc "$@" 2>&1` |
| 45 | status=$? |
| 46 | if [ $status = $w_status ] && [ x"$output" = x"$w_output" ] ; then |
| 47 | ok "$desc" true |
| 48 | elif [ $status = 2 ] ; then |
| 49 | diag "$output" |
| 50 | skip "no data limit support" |
| 51 | else |
| 52 | diag "saw: ($status) $output" |
| 53 | diag "not: ($w_status) $w_output" |
| 54 | ok "$desc" false |
| 55 | fi |
| 56 | } |
| 57 | |
| 58 | # Skip this test suite unless maintainer-mode tests are enabled. All of the |
| 59 | # failures in automated testing have been problems with the assumptions around |
| 60 | # memory allocation or problems with the test suite, not problems with the |
| 61 | # underlying xmalloc code. |
| 62 | if [ -z "$AUTHOR_TESTING" ] ; then |
| 63 | skip_all 'xmalloc tests only run for author' |
| 64 | fi |
| 65 | |
| 66 | # Total tests. |
| 67 | plan 41 |
| 68 | |
| 69 | # First run the tests expected to succeed. |
| 70 | ok_xmalloc "malloc small" 0 "" "m" "21" "0" |
| 71 | ok_xmalloc "malloc large" 0 "" "m" "30000000" "0" |
| 72 | ok_xmalloc "malloc zero" 0 "" "m" "0" "0" |
| 73 | ok_xmalloc "realloc small" 0 "" "r" "21" "0" |
| 74 | ok_xmalloc "realloc large" 0 "" "r" "30000000" "0" |
| 75 | ok_xmalloc "reallocarray small" 0 "" "y" "20" "0" |
| 76 | ok_xmalloc "reallocarray large" 0 "" "y" "30000000" "0" |
| 77 | ok_xmalloc "strdup small" 0 "" "s" "21" "0" |
| 78 | ok_xmalloc "strdup large" 0 "" "s" "30000000" "0" |
| 79 | ok_xmalloc "strndup small" 0 "" "n" "21" "0" |
| 80 | ok_xmalloc "strndup large" 0 "" "n" "30000000" "0" |
| 81 | ok_xmalloc "calloc small" 0 "" "c" "24" "0" |
| 82 | ok_xmalloc "calloc large" 0 "" "c" "30000000" "0" |
| 83 | ok_xmalloc "asprintf small" 0 "" "a" "24" "0" |
| 84 | ok_xmalloc "asprintf large" 0 "" "a" "30000000" "0" |
| 85 | ok_xmalloc "vasprintf small" 0 "" "v" "24" "0" |
| 86 | ok_xmalloc "vasprintf large" 0 "" "v" "30000000" "0" |
| 87 | |
| 88 | # Now limit our memory to 30MB and then try the large ones again, all of |
| 89 | # which should fail. |
| 90 | # |
| 91 | # The exact memory limits used here are essentially black magic. They need to |
| 92 | # be large enough to allow the program to be loaded and do small allocations, |
| 93 | # but not so large that we can't reasonably expect to allocate that much |
| 94 | # memory normally. The amount of memory required varies a lot based on what |
| 95 | # shared libraries are loaded, and if it's too small, all memory allocations |
| 96 | # fail. 30MB seems to work reasonably well on both Solaris and Linux, even |
| 97 | # when the program is linked with additional libraries. |
| 98 | # |
| 99 | # We assume that there are enough miscellaneous allocations that an allocation |
| 100 | # exactly as large as the limit will always fail. |
| 101 | ok_xmalloc "malloc fail" 1 \ |
| 102 | "failed to malloc 30000000 bytes at xmalloc.c line 41" \ |
| 103 | "m" "30000000" "30000000" |
| 104 | ok_xmalloc "realloc fail" 1 \ |
| 105 | "failed to realloc 30000000 bytes at xmalloc.c line 69" \ |
| 106 | "r" "30000000" "30000000" |
| 107 | ok_xmalloc "reallocarray fail" 1 \ |
| 108 | "failed to reallocarray 30000000 bytes at xmalloc.c line 99" \ |
| 109 | "y" "30000000" "30000000" |
| 110 | ok_xmalloc "strdup fail" 1 \ |
| 111 | "failed to strdup 30000000 bytes at xmalloc.c line 130" \ |
| 112 | "s" "30000000" "30000000" |
| 113 | ok_xmalloc "strndup fail" 1 \ |
| 114 | "failed to strndup 30000000 bytes at xmalloc.c line 176" \ |
| 115 | "n" "30000000" "30000000" |
| 116 | ok_xmalloc "calloc fail" 1 \ |
| 117 | "failed to calloc 30000000 bytes at xmalloc.c line 200" \ |
| 118 | "c" "30000000" "30000000" |
| 119 | ok_xmalloc "asprintf fail" 1 \ |
| 120 | "failed to asprintf 30000000 bytes at xmalloc.c line 224" \ |
| 121 | "a" "30000000" "30000000" |
| 122 | ok_xmalloc "vasprintf fail" 1 \ |
| 123 | "failed to vasprintf 30000000 bytes at xmalloc.c line 243" \ |
| 124 | "v" "30000000" "30000000" |
| 125 | |
| 126 | # Check our custom error handler. |
| 127 | ok_xmalloc "malloc custom" 1 "malloc 30000000 xmalloc.c 41" \ |
| 128 | "M" "30000000" "30000000" |
| 129 | ok_xmalloc "realloc custom" 1 "realloc 30000000 xmalloc.c 69" \ |
| 130 | "R" "30000000" "30000000" |
| 131 | ok_xmalloc "reallocarray custom" 1 "reallocarray 30000000 xmalloc.c 99" \ |
| 132 | "Y" "30000000" "30000000" |
| 133 | ok_xmalloc "strdup custom" 1 "strdup 30000000 xmalloc.c 130" \ |
| 134 | "S" "30000000" "30000000" |
| 135 | ok_xmalloc "strndup custom" 1 "strndup 30000000 xmalloc.c 176" \ |
| 136 | "N" "30000000" "30000000" |
| 137 | ok_xmalloc "calloc custom" 1 "calloc 30000000 xmalloc.c 200" \ |
| 138 | "C" "30000000" "30000000" |
| 139 | ok_xmalloc "asprintf custom" 1 "asprintf 30000000 xmalloc.c 224" \ |
| 140 | "A" "30000000" "30000000" |
| 141 | ok_xmalloc "vasprintf custom" 1 "vasprintf 30000000 xmalloc.c 243" \ |
| 142 | "V" "30000000" "30000000" |
| 143 | |
| 144 | # Check the smaller ones again just for grins. |
| 145 | ok_xmalloc "malloc retry" 0 "" "m" "21" "30000000" |
| 146 | ok_xmalloc "realloc retry" 0 "" "r" "32" "30000000" |
| 147 | ok_xmalloc "reallocarray retry" 0 "" "y" "32" "30000000" |
| 148 | ok_xmalloc "strdup retry" 0 "" "s" "64" "30000000" |
| 149 | ok_xmalloc "strndup retry" 0 "" "n" "20" "30000000" |
| 150 | ok_xmalloc "calloc retry" 0 "" "c" "24" "30000000" |
| 151 | ok_xmalloc "asprintf retry" 0 "" "a" "30" "30000000" |
| 152 | ok_xmalloc "vasprintf retry" 0 "" "v" "35" "30000000" |