allocfail.sh 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. #!/bin/sh
  2. # allocfail.sh -- Test for libbacktrace library.
  3. # Copyright (C) 2018-2022 Free Software Foundation, Inc.
  4. # Redistribution and use in source and binary forms, with or without
  5. # modification, are permitted provided that the following conditions are
  6. # met:
  7. # (1) Redistributions of source code must retain the above copyright
  8. # notice, this list of conditions and the following disclaimer.
  9. # (2) Redistributions in binary form must reproduce the above copyright
  10. # notice, this list of conditions and the following disclaimer in
  11. # the documentation and/or other materials provided with the
  12. # distribution.
  13. # (3) The name of the author may not be used to
  14. # endorse or promote products derived from this software without
  15. # specific prior written permission.
  16. # THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
  17. # IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  18. # WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  19. # DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
  20. # INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  21. # (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  22. # SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  23. # HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
  24. # STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
  25. # IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  26. # POSSIBILITY OF SUCH DAMAGE.
  27. set -e
  28. if [ ! -f ./allocfail ]; then
  29. # Hard failure.
  30. exit 99
  31. fi
  32. allocs=$(./allocfail 2>&1)
  33. if [ "$allocs" = "" ]; then
  34. # Hard failure.
  35. exit 99
  36. fi
  37. # This generates the following output:
  38. # ...
  39. # $ allocfail.sh
  40. # allocs: 80495
  41. # Status changed to 0 at 1
  42. # Status changed to 1 at 3
  43. # Status changed to 0 at 11
  44. # Status changed to 1 at 12
  45. # Status changed to 0 at 845
  46. # ...
  47. #
  48. # We have status 0 for an allocation failure at:
  49. # - 1 because backtrace_create_state handles failure robustly
  50. # - 2 because the fail switches backtrace_full to !can_alloc mode.
  51. # - 11 because failure of elf_open_debugfile_by_buildid does not generate an
  52. # error callback beyond the one for the allocation failure itself.
  53. echo "allocs: $allocs"
  54. step=1
  55. i=1
  56. passes=0
  57. prev_status=-1
  58. while [ $i -le $allocs ]; do
  59. if ./allocfail $i >/dev/null 2>&1; status=$?; then
  60. true
  61. fi
  62. if [ $status -gt 1 ]; then
  63. echo "Unallowed fail found: $i"
  64. # Failure.
  65. exit 1
  66. fi
  67. # The test-case would run too long if we would excercise all allocs.
  68. # So, run with step 1 initially, and increase the step once we have 10
  69. # subsequent passes, and drop back to step 1 once we encounter another
  70. # failure. This takes ~2.6 seconds on an i7-6600U CPU @ 2.60GHz.
  71. if [ $status -eq 0 ]; then
  72. passes=$(($passes + 1))
  73. if [ $passes -ge 10 ]; then
  74. step=$((step * 10))
  75. passes=0
  76. fi
  77. elif [ $status -eq 1 ]; then
  78. passes=0
  79. step=1
  80. fi
  81. if [ $status -ne $prev_status ]; then
  82. echo "Status changed to $status at $i"
  83. fi
  84. prev_status=$status
  85. i=$(($i + $step))
  86. done
  87. # Success.
  88. exit 0