1#
2#  Copyright (c) 2016 Dell EMC Isilon
3#  All rights reserved.
4#
5#  Redistribution and use in source and binary forms, with or without
6#  modification, are permitted provided that the following conditions
7#  are met:
8#  1. Redistributions of source code must retain the above copyright
9#     notice, this list of conditions, and the following disclaimer,
10#     without modification.
11#  2. Redistributions in binary form must reproduce at minimum a disclaimer
12#     substantially similar to the "NO WARRANTY" disclaimer below
13#     ("Disclaimer") and any redistribution must be conditioned upon
14#     including a substantially similar Disclaimer requirement for further
15#     binary redistribution.
16#
17#  NO WARRANTY
18#  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19#  "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20#  LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR
21#  A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
22#  HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23#  DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24#  OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25#  HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
26#  STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
27#  IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
28#  POSSIBILITY OF SUCH DAMAGES.
29#
30# $FreeBSD$
31
32atf_test_case coredump_phnum cleanup
33coredump_phnum_head()
34{
35	atf_set "descr" "More than 65534 segments"
36	atf_set "require.config" "allow_sysctl_side_effects"
37	atf_set "require.progs" "readelf procstat"
38	atf_set "require.user" "root"
39}
40coredump_phnum_body()
41{
42	atf_expect_fail "elftoolchain (base) readelf doesn't handle extended program header numbers; bug # 215019"
43
44	# Set up core dumping
45	cat > coredump_phnum_restore_state.sh <<-EOF
46	#!/bin/sh
47	ulimit -c '$(ulimit -c)'
48	sysctl kern.coredump=$(sysctl -n kern.coredump)
49	sysctl kern.corefile='$(sysctl -n kern.corefile)'
50EOF
51	chmod +x coredump_phnum_restore_state.sh
52
53	ulimit -c unlimited
54	sysctl kern.coredump=1
55	sysctl kern.corefile="$(pwd)/coredump_phnum_helper.core"
56
57	atf_check -s signal:sigabrt "$(atf_get_srcdir)/coredump_phnum_helper"
58
59	# Check that core looks good
60	if [ ! -f coredump_phnum_helper.core ]; then
61	    atf_fail "Helper program did not dump core"
62	fi
63
64	# These magic numbers don't have any real significance.  They are just
65	# the result of running the helper program and dumping core.  The only
66	# important bit is that they're larger than 65535 (UINT16_MAX).
67	readelf -h coredump_phnum_helper.core | \
68	    atf_check -o "match:65535 \(66[0-9]{3}\)" \
69	    grep "Number of program headers:"
70	readelf -l coredump_phnum_helper.core | \
71	    atf_check -o "match:There are 66[0-9]{3} program headers" \
72	    grep -1 "program headers"
73	readelf -S coredump_phnum_helper.core | \
74	    atf_check -o "match: 0000000000000001 .* 66[0-9]{3} " \
75	    grep -A1 "^  \[ 0\] "
76
77	procstat -v coredump_phnum_helper.core | \
78	    atf_check -o "match:66[0-9]{3}" wc -l
79}
80coredump_phnum_cleanup()
81{
82	rm -f coredump_phnum_helper.core
83	if [ -x coredump_phnum_restore_state.sh ]; then
84		./coredump_phnum_restore_state.sh
85	fi
86	rm -f coredump_phnum_restore_state.sh
87}
88
89atf_init_test_cases()
90{
91	atf_add_test_case coredump_phnum
92}
93