1181254a7Smrg#!/bin/sh 2181254a7Smrg 3181254a7Smrg# allocfail.sh -- Test for libbacktrace library. 4*b1e83836Smrg# Copyright (C) 2018-2022 Free Software Foundation, Inc. 5181254a7Smrg 6181254a7Smrg# Redistribution and use in source and binary forms, with or without 7181254a7Smrg# modification, are permitted provided that the following conditions are 8181254a7Smrg# met: 9181254a7Smrg 10181254a7Smrg# (1) Redistributions of source code must retain the above copyright 11181254a7Smrg# notice, this list of conditions and the following disclaimer. 12181254a7Smrg 13181254a7Smrg# (2) Redistributions in binary form must reproduce the above copyright 14181254a7Smrg# notice, this list of conditions and the following disclaimer in 15181254a7Smrg# the documentation and/or other materials provided with the 16181254a7Smrg# distribution. 17181254a7Smrg 18181254a7Smrg# (3) The name of the author may not be used to 19181254a7Smrg# endorse or promote products derived from this software without 20181254a7Smrg# specific prior written permission. 21181254a7Smrg 22181254a7Smrg# THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 23181254a7Smrg# IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 24181254a7Smrg# WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 25181254a7Smrg# DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, 26181254a7Smrg# INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 27181254a7Smrg# (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR 28181254a7Smrg# SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 29181254a7Smrg# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, 30181254a7Smrg# STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING 31181254a7Smrg# IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 32181254a7Smrg# POSSIBILITY OF SUCH DAMAGE. 33181254a7Smrg 34181254a7Smrgset -e 35181254a7Smrg 36181254a7Smrgif [ ! -f ./allocfail ]; then 37181254a7Smrg # Hard failure. 38181254a7Smrg exit 99 39181254a7Smrgfi 40181254a7Smrg 41181254a7Smrgallocs=$(./allocfail 2>&1) 42181254a7Smrgif [ "$allocs" = "" ]; then 43181254a7Smrg # Hard failure. 44181254a7Smrg exit 99 45181254a7Smrgfi 46181254a7Smrg 47181254a7Smrg# This generates the following output: 48181254a7Smrg# ... 49181254a7Smrg# $ allocfail.sh 50181254a7Smrg# allocs: 80495 51181254a7Smrg# Status changed to 0 at 1 52181254a7Smrg# Status changed to 1 at 3 53181254a7Smrg# Status changed to 0 at 11 54181254a7Smrg# Status changed to 1 at 12 55181254a7Smrg# Status changed to 0 at 845 56181254a7Smrg# ... 57181254a7Smrg# 58181254a7Smrg# We have status 0 for an allocation failure at: 59181254a7Smrg# - 1 because backtrace_create_state handles failure robustly 60181254a7Smrg# - 2 because the fail switches backtrace_full to !can_alloc mode. 61181254a7Smrg# - 11 because failure of elf_open_debugfile_by_buildid does not generate an 62181254a7Smrg# error callback beyond the one for the allocation failure itself. 63181254a7Smrg 64181254a7Smrgecho "allocs: $allocs" 65181254a7Smrg 66181254a7Smrgstep=1 67181254a7Smrgi=1 68181254a7Smrgpasses=0 69181254a7Smrgprev_status=-1 70181254a7Smrgwhile [ $i -le $allocs ]; do 71181254a7Smrg if ./allocfail $i >/dev/null 2>&1; status=$?; then 72181254a7Smrg true 73181254a7Smrg fi 74181254a7Smrg if [ $status -gt 1 ]; then 75181254a7Smrg echo "Unallowed fail found: $i" 76181254a7Smrg # Failure. 77181254a7Smrg exit 1 78181254a7Smrg fi 79181254a7Smrg 80181254a7Smrg # The test-case would run too long if we would excercise all allocs. 81181254a7Smrg # So, run with step 1 initially, and increase the step once we have 10 82181254a7Smrg # subsequent passes, and drop back to step 1 once we encounter another 83181254a7Smrg # failure. This takes ~2.6 seconds on an i7-6600U CPU @ 2.60GHz. 84181254a7Smrg if [ $status -eq 0 ]; then 85181254a7Smrg passes=$(($passes + 1)) 86181254a7Smrg if [ $passes -ge 10 ]; then 87181254a7Smrg step=$((step * 10)) 88181254a7Smrg passes=0 89181254a7Smrg fi 90181254a7Smrg elif [ $status -eq 1 ]; then 91181254a7Smrg passes=0 92181254a7Smrg step=1 93181254a7Smrg fi 94181254a7Smrg 95181254a7Smrg if [ $status -ne $prev_status ]; then 96181254a7Smrg echo "Status changed to $status at $i" 97181254a7Smrg fi 98181254a7Smrg prev_status=$status 99181254a7Smrg 100181254a7Smrg i=$(($i + $step)) 101181254a7Smrgdone 102181254a7Smrg 103181254a7Smrg# Success. 104181254a7Smrgexit 0 105