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