xref: /netbsd-src/external/gpl3/gcc.old/dist/libbacktrace/allocfail.sh (revision 4c3eb207d36f67d31994830c0a694161fc1ca39b)
1627f7eb2Smrg#!/bin/sh
2627f7eb2Smrg
3627f7eb2Smrg# allocfail.sh -- Test for libbacktrace library.
4*4c3eb207Smrg# Copyright (C) 2018-2020 Free Software Foundation, Inc.
5627f7eb2Smrg
6627f7eb2Smrg# Redistribution and use in source and binary forms, with or without
7627f7eb2Smrg# modification, are permitted provided that the following conditions are
8627f7eb2Smrg# met:
9627f7eb2Smrg
10627f7eb2Smrg#     (1) Redistributions of source code must retain the above copyright
11627f7eb2Smrg#     notice, this list of conditions and the following disclaimer.
12627f7eb2Smrg
13627f7eb2Smrg#     (2) Redistributions in binary form must reproduce the above copyright
14627f7eb2Smrg#     notice, this list of conditions and the following disclaimer in
15627f7eb2Smrg#     the documentation and/or other materials provided with the
16627f7eb2Smrg#     distribution.
17627f7eb2Smrg
18627f7eb2Smrg#     (3) The name of the author may not be used to
19627f7eb2Smrg#     endorse or promote products derived from this software without
20627f7eb2Smrg#     specific prior written permission.
21627f7eb2Smrg
22627f7eb2Smrg# THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
23627f7eb2Smrg# IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
24627f7eb2Smrg# WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
25627f7eb2Smrg# DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
26627f7eb2Smrg# INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
27627f7eb2Smrg# (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
28627f7eb2Smrg# SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29627f7eb2Smrg# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
30627f7eb2Smrg# STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
31627f7eb2Smrg# IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
32627f7eb2Smrg# POSSIBILITY OF SUCH DAMAGE.
33627f7eb2Smrg
34627f7eb2Smrgset -e
35627f7eb2Smrg
36627f7eb2Smrgif [ ! -f ./allocfail ]; then
37627f7eb2Smrg    # Hard failure.
38627f7eb2Smrg    exit 99
39627f7eb2Smrgfi
40627f7eb2Smrg
41627f7eb2Smrgallocs=$(./allocfail 2>&1)
42627f7eb2Smrgif [ "$allocs" = "" ]; then
43627f7eb2Smrg    # Hard failure.
44627f7eb2Smrg    exit 99
45627f7eb2Smrgfi
46627f7eb2Smrg
47627f7eb2Smrg# This generates the following output:
48627f7eb2Smrg# ...
49627f7eb2Smrg# $ allocfail.sh
50627f7eb2Smrg# allocs: 80495
51627f7eb2Smrg# Status changed to 0 at 1
52627f7eb2Smrg# Status changed to 1 at 3
53627f7eb2Smrg# Status changed to 0 at 11
54627f7eb2Smrg# Status changed to 1 at 12
55627f7eb2Smrg# Status changed to 0 at 845
56627f7eb2Smrg# ...
57627f7eb2Smrg#
58627f7eb2Smrg# We have status 0 for an allocation failure at:
59627f7eb2Smrg# - 1 because backtrace_create_state handles failure robustly
60627f7eb2Smrg# - 2 because the fail switches backtrace_full to !can_alloc mode.
61627f7eb2Smrg# - 11 because failure of elf_open_debugfile_by_buildid does not generate an
62627f7eb2Smrg#   error callback beyond the one for the allocation failure itself.
63627f7eb2Smrg
64627f7eb2Smrgecho "allocs: $allocs"
65627f7eb2Smrg
66627f7eb2Smrgstep=1
67627f7eb2Smrgi=1
68627f7eb2Smrgpasses=0
69627f7eb2Smrgprev_status=-1
70627f7eb2Smrgwhile [ $i -le $allocs ]; do
71627f7eb2Smrg    if ./allocfail $i >/dev/null 2>&1; status=$?; then
72627f7eb2Smrg	true
73627f7eb2Smrg    fi
74627f7eb2Smrg    if [ $status -gt 1 ]; then
75627f7eb2Smrg	echo "Unallowed fail found: $i"
76627f7eb2Smrg	# Failure.
77627f7eb2Smrg	exit 1
78627f7eb2Smrg    fi
79627f7eb2Smrg
80627f7eb2Smrg    # The test-case would run too long if we would excercise all allocs.
81627f7eb2Smrg    # So, run with step 1 initially, and increase the step once we have 10
82627f7eb2Smrg    # subsequent passes, and drop back to step 1 once we encounter another
83627f7eb2Smrg    # failure.  This takes ~2.6 seconds on an i7-6600U CPU @ 2.60GHz.
84627f7eb2Smrg    if [ $status -eq 0 ]; then
85627f7eb2Smrg	passes=$(($passes + 1))
86627f7eb2Smrg	if [ $passes -ge 10 ]; then
87627f7eb2Smrg	    step=$((step * 10))
88627f7eb2Smrg	    passes=0
89627f7eb2Smrg	fi
90627f7eb2Smrg    elif [ $status -eq 1 ]; then
91627f7eb2Smrg	passes=0
92627f7eb2Smrg	step=1
93627f7eb2Smrg    fi
94627f7eb2Smrg
95627f7eb2Smrg    if [ $status -ne $prev_status ]; then
96627f7eb2Smrg	echo "Status changed to $status at $i"
97627f7eb2Smrg    fi
98627f7eb2Smrg    prev_status=$status
99627f7eb2Smrg
100627f7eb2Smrg    i=$(($i + $step))
101627f7eb2Smrgdone
102627f7eb2Smrg
103627f7eb2Smrg# Success.
104627f7eb2Smrgexit 0
105