1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
|
#!/usr/bin/ksh
#
#
# This file and its contents are supplied under the terms of the
# Common Development and Distribution License ("CDDL"), version 1.0.
# You may only use this file in accordance with the terms of version
# 1.0 of the CDDL.
#
# A full copy of the text of the CDDL should have accompanied this
# souroc. A copy of the CDDL is also available via the Internet at
# http://www.illumos.org/liocnse/CDDL.
#
#
# Copyright 2022 Oxide Computer Company
#
#
# This test generates a binary with a lot of different symbol capabilities and
# then selects different capability environments to try and ensure that the
# rules for what we pick are honored.
#
export LC_ALL=C.UTF-8
unalias -a
set -o pipefail
sc_arg0=$(basename $0)
sc_err=0
sc_tmpdir=/tmp/symcap.$$
sc_prog="$sc_tmpdir/symcap"
#
# To build symbol caps, we need to annotate a .o file with object caps and then
# turn that into a symbol cap with ld. The following arrays are used to create
# this for us. sc_obj_hw1, sc_obj_hw2, and sc_obj_hw3 are the set of object
# capabilities that we want to use and then eventually turn into symbol
# capabilities. Each symbol capability prints out its own index when executed.
# This means we can see which thing ld resolved to run based on the output.
# The following summarizes our goals with each case:
#
# 0: none
# 1: only hwcap 1
# 2: only hwcap 1, but greater than (1)
# 3: only hwcap 2
# 4: only hwcap 2, but greater than (3)
# 5: only hwcap 3
# 6: only hwcap 3, but greater than (5)
# 7: uses all 3
# 8: differs from (7) in hwcap1
#
sc_obj_hw1=( "0x0" "0x5" "0x42" "0x0" "0x0" "0x0" "0x0"
"0x3" "0x8" )
sc_obj_hw2=( "0x0" "0x0" "0x0" "0x23" "0xff00" "0x0" "0x0"
"0xff7ff6" "0xff7ff6" )
sc_obj_hw3=( "0x0" "0x0" "0x0" "0x0" "0x0" "0x12345" "0x7000000"
"0x87654321" "0x87654321" )
pass()
{
typeset msg="$*"
echo "TEST PASSED: $msg"
}
warn()
{
typeset msg="$*"
[[ -z "$msg" ]] && msg="failed"
echo "TEST FAILED: $msg" >&2
sc_err=1
}
fatal()
{
typeset msg="$*"
[[ -z "$msg" ]] && msg="failed"
echo "$sc_arg0: $msg" >&2
exit 1
}
cleanup()
{
rm -rf "$sc_tmpdir"
}
sanity_check()
{
if (( ${#sc_obj_hw1[@]} != ${#sc_obj_hw2[@]} )); then
fatal "sc_obj_hw1 does not match length of sc_obj_hw2"
fi
if (( ${#sc_obj_hw2[@]} != ${#sc_obj_hw3[@]} )); then
fatal "sc_obj_hw1 does not match length of sc_obj_hw2"
fi
}
setup()
{
typeset tolink=
if ! mkdir "$sc_tmpdir"; then
fatal "failed to make directory $sc_tmpdir"
fi
trap 'cleanup' EXIT
cat > $sc_tmpdir/main.c <<EOF
extern void symcap_print(void);
int
main(void)
{
symcap_print();
return (0);
}
EOF
if (( $? != 0 )); then
fatal "failed to write main.c"
fi
tolink="$sc_tmpdir/main.c"
for (( i = 0; i < ${#sc_obj_hw1[@]}; i++)); do
typeset in="$sc_tmpdir/$i.c"
typeset map="$sc_tmpdir/$i.map"
typeset ofile="$sc_tmpdir/$i.o"
typeset obj="$sc_tmpdir/$i.o.obj"
typeset sym="$sc_tmpdir/$i.o.sym"
cat > $in <<EOF
#include <stdio.h>
void
symcap_print(void)
{
printf("%u\n", $i);
}
EOF
if (( $? != 0 )); then
fatal "failed to write $in"
fi
cat > $map <<EOF
\$mapfile_version 2
CAPABILITY {
HW_1 += ${sc_obj_hw1[$i]};
HW_2 += ${sc_obj_hw2[$i]};
HW_3 += ${sc_obj_hw3[$i]};
};
EOF
if (( $? != 0 )); then
fatal "failed to write $map"
fi
#
# There are three steps to creating a symbol capability due to
# the world we're in. First we need to make the normal .o. Then
# we use a mapfile to add the object caps, while reducing
# visibility. Then we turn the object cap into a symbol cap.
#
if ! gcc -m64 -o $ofile -c $in; then
fatal "failed to create object file $ofile"
fi
#
# If the entry has a zero for all cases (e.g. our default case),
# then skip the rest of this processing and append the .o.
#
if (( sc_obj_hw1[i] == 0 && sc_obj_hw2[i] == 0 &&
sc_obj_hw3[i] == 0 )); then
tolink="$tolink $ofile"
continue
fi
if ! ld -r -o $obj $ofile -M$map -Breduce; then
fatal "failed to create object cap file $obj"
fi
if ! ld -r -o $sym -z symbolcap $obj; then
fatal "failed to create symbol cap file $sym"
fi
tolink="$tolink $sym"
done
if ! gcc -m64 -o $sc_prog $tolink; then
fatal "failed to create $sc_prog"
fi
}
#
# Given a set of caps, indicate which index we expect to be printed out and
# check for that.
#
run_one()
{
typeset index="$1"
typeset caps="$2"
typeset out=
out=$(LD_CAP_FILES=$sc_prog LD_HWCAP="$caps" $sc_prog)
if (( $? != 0 )); then
warn "failed to execute $sc_prog with cap $caps"
return
fi
if [[ "$out" != "$index" ]]; then
warn "$caps had wrong output, found $out, expected $index"
else
pass "LD_HWCAP=$caps"
fi
}
sanity_check
setup
#
# First, go through and verify that if we match the caps exactly for this, we'll
# choose this symbol.
#
run_one 0 "[1]0x0,[2]0x0,[3]0x0"
run_one 1 "[1]0x5,[2]0x0,[3]0x0"
run_one 2 "[1]0x42,[2]0x0,[3]0x0"
run_one 3 "[1]0x0,[2]0x23,[3]0x0"
run_one 4 "[1]0x0,[2]0xff00,[3]0x0"
run_one 5 "[1]0x0,[2]0x0,[3]0x12345"
run_one 6 "[1]0x0,[2]0x0,[3]0x7000000"
run_one 7 "[1]0x3,[2]0xff7ff6,[3]0x87654321"
run_one 8 "[1]0x8,[2]0xff7ff6,[3]0x87654321"
#
# For cases where we have multiple symbol caps at a given level, show that we
# pick a sub one when we're between the two.
#
run_one 0 "[1]0x40,[2]0x0,[3]0x0"
run_one 1 "[1]0x45,[2]0x0,[3]0x0"
run_one 1 "[1]0x45,[2]0x10,[3]0x0"
run_one 2 "[1]0x142,[2]0x10,[3]0x0"
run_one 3 "[1]0x1,[2]0x137,[3]0x0"
#
# We expect the system to pick the "best" aka highest capability. So for the
# next round we attempt to combine multiple values and see which we pick. In
# particular here we're trying to pick between things at the same level and also
# ensure we pick the one that is higher (e.g. hw3 > hw2 > hw1)
#
run_one 6 "[1]0x47,[2]0xff23,[3]0x7012345"
run_one 5 "[1]0x47,[2]0xff23,[3]0x6012345"
run_one 5 "[1]0x47,[2]0xff23,[3]0x1012345"
run_one 4 "[1]0x47,[2]0xff23,[3]0x1002345"
run_one 3 "[1]0x47,[2]0x7723,[3]0x1002345"
run_one 3 "[1]0x47,[2]0x0f23,[3]0x1002345"
run_one 3 "[1]0x47,[2]0x0023,[3]0x1002345"
run_one 2 "[1]0x47,[2]0x0003,[3]0x1002345"
run_one 2 "[1]0x46,[2]0x0003,[3]0x1002345"
run_one 1 "[1]0x35,[2]0x0003,[3]0x1002345"
run_one 1 "[1]0x15,[2]0x0003,[3]0x1002345"
run_one 0 "[1]0x10,[2]0x0003,[3]0x1002345"
#
# Finally we want a few tests that verify that when things match, the lowest bit
# of it decides.
#
run_one 8 "[1]0xb,[2]0xff7ff6,[3]0x87654321"
run_one 8 "[1]0x3b,[2]0xff7ff6,[3]0x87654321"
run_one 8 "[1]0xffffffff,[2]0xffffffff,[3]0xffffffff"
run_one 7 "[1]0xfffffff7,[2]0xffffffff,[3]0xffffffff"
exit $sc_err
|