blob: 3a37255a4eed71938b3ccdc98dae978a89c04fce [file] [log] [blame]
Lorenzo Colitti08c05d32014-03-27 20:10:14 +09001#!/bin/bash
2
Maciej Żenczykowski99cd00d2018-10-14 20:03:23 -07003# Builds mysteriously fail if stdout is non-blocking.
4fixup_ptys() {
Maciej Żenczykowskidc93e522022-04-28 01:56:40 -07005 python3 << 'EOF'
Maciej Żenczykowski99cd00d2018-10-14 20:03:23 -07006import fcntl, os, sys
7fd = sys.stdout.fileno()
8flags = fcntl.fcntl(fd, fcntl.F_GETFL)
9flags &= ~(fcntl.FASYNC | os.O_NONBLOCK | os.O_APPEND)
10fcntl.fcntl(fd, fcntl.F_SETFL, flags)
11EOF
12}
13
Alistair Strachanab987102018-05-10 16:44:52 -070014# Common kernel options
Maciej Żenczykowski38ea59b2022-10-10 15:51:20 -070015OPTIONS=" ANDROID GKI_NET_XFRM_HACKS"
16OPTIONS="$OPTIONS DEBUG_SPINLOCK DEBUG_ATOMIC_SLEEP DEBUG_MUTEXES DEBUG_RT_MUTEXES"
Alistair Delvaafbd6322019-07-10 17:18:39 -070017OPTIONS="$OPTIONS WARN_ALL_UNSEEDED_RANDOM IKCONFIG IKCONFIG_PROC"
Alistair Strachan9493cb12018-06-17 14:10:52 -070018OPTIONS="$OPTIONS DEVTMPFS DEVTMPFS_MOUNT FHANDLE"
Lorenzo Colitti4620bd82015-12-21 11:39:16 +090019OPTIONS="$OPTIONS IPV6 IPV6_ROUTER_PREF IPV6_MULTIPLE_TABLES IPV6_ROUTE_INFO"
Lorenzo Colitti611c9082014-03-31 13:25:48 +090020OPTIONS="$OPTIONS TUN SYN_COOKIES IP_ADVANCED_ROUTER IP_MULTIPLE_TABLES"
Lorenzo Colitti08c05d32014-03-27 20:10:14 +090021OPTIONS="$OPTIONS NETFILTER NETFILTER_ADVANCED NETFILTER_XTABLES"
22OPTIONS="$OPTIONS NETFILTER_XT_MARK NETFILTER_XT_TARGET_MARK"
Erik Kline7cfe31e2016-05-31 16:18:01 +090023OPTIONS="$OPTIONS IP_NF_IPTABLES IP_NF_MANGLE IP_NF_FILTER"
24OPTIONS="$OPTIONS IP6_NF_IPTABLES IP6_NF_MANGLE IP6_NF_FILTER INET6_IPCOMP"
Alistair Strachanab987102018-05-10 16:44:52 -070025OPTIONS="$OPTIONS IPV6_OPTIMISTIC_DAD"
Alistair Strachan5055c072018-05-10 15:36:55 -070026OPTIONS="$OPTIONS IPV6_ROUTE_INFO IPV6_ROUTER_PREF"
Maciej Żenczykowskica849772020-08-19 17:50:50 -070027OPTIONS="$OPTIONS NETFILTER_XT_TARGET_IDLETIMER"
Alistair Strachan5055c072018-05-10 15:36:55 -070028OPTIONS="$OPTIONS NETFILTER_XT_TARGET_NFLOG"
Maciej Żenczykowskiccad07c2019-03-21 13:33:37 -070029OPTIONS="$OPTIONS NETFILTER_XT_MATCH_POLICY"
Alistair Strachan5055c072018-05-10 15:36:55 -070030OPTIONS="$OPTIONS NETFILTER_XT_MATCH_QUOTA"
31OPTIONS="$OPTIONS NETFILTER_XT_MATCH_QUOTA2"
32OPTIONS="$OPTIONS NETFILTER_XT_MATCH_QUOTA2_LOG"
Alistair Strachan5055c072018-05-10 15:36:55 -070033OPTIONS="$OPTIONS NETFILTER_XT_MATCH_SOCKET"
34OPTIONS="$OPTIONS NETFILTER_XT_MATCH_QTAGUID"
Alistair Delvaafbd6322019-07-10 17:18:39 -070035OPTIONS="$OPTIONS INET_DIAG INET_UDP_DIAG INET_DIAG_DESTROY"
Alistair Strachanab987102018-05-10 16:44:52 -070036OPTIONS="$OPTIONS IP_SCTP"
Alistair Strachan5055c072018-05-10 15:36:55 -070037OPTIONS="$OPTIONS IP_NF_TARGET_REJECT IP_NF_TARGET_REJECT_SKERR"
38OPTIONS="$OPTIONS IP6_NF_TARGET_REJECT IP6_NF_TARGET_REJECT_SKERR"
Alistair Strachanab987102018-05-10 16:44:52 -070039OPTIONS="$OPTIONS NET_KEY XFRM_USER XFRM_STATISTICS CRYPTO_CBC"
Alistair Strachan8f52a622018-05-10 15:48:44 -070040OPTIONS="$OPTIONS CRYPTO_CTR CRYPTO_HMAC CRYPTO_AES CRYPTO_SHA1"
Maciej Żenczykowski87eedb02020-09-25 16:45:04 -070041OPTIONS="$OPTIONS CRYPTO_XCBC CRYPTO_CHACHA20POLY1305"
Alistair Strachan8f52a622018-05-10 15:48:44 -070042OPTIONS="$OPTIONS CRYPTO_USER INET_ESP INET_XFRM_MODE_TRANSPORT"
43OPTIONS="$OPTIONS INET_XFRM_MODE_TUNNEL INET6_ESP"
Lorenzo Colitticbcfcf02016-12-22 14:42:58 +090044OPTIONS="$OPTIONS INET6_XFRM_MODE_TRANSPORT INET6_XFRM_MODE_TUNNEL"
Jonathan Basseri3187de02017-11-22 10:09:23 -080045OPTIONS="$OPTIONS CRYPTO_SHA256 CRYPTO_SHA512 CRYPTO_AES_X86_64 CRYPTO_NULL"
Alistair Strachanab987102018-05-10 16:44:52 -070046OPTIONS="$OPTIONS CRYPTO_GCM CRYPTO_ECHAINIV NET_IPVTI"
Maciej Żenczykowski0be11c12019-08-15 17:00:22 -070047OPTIONS="$OPTIONS DUMMY"
Lorenzo Colittia76a4502015-09-18 15:12:39 +090048
Alistair Strachanab987102018-05-10 16:44:52 -070049# Kernel version specific options
50OPTIONS="$OPTIONS XFRM_INTERFACE" # Various device kernels
Yan Yanac77a012020-12-22 17:23:50 -080051OPTIONS="$OPTIONS XFRM_MIGRATE" # Added in 5.10
Alistair Strachanab987102018-05-10 16:44:52 -070052OPTIONS="$OPTIONS CGROUP_BPF" # Added in android-4.9
53OPTIONS="$OPTIONS NF_SOCKET_IPV4 NF_SOCKET_IPV6" # Added in 4.9
54OPTIONS="$OPTIONS INET_SCTP_DIAG" # Added in 4.7
55OPTIONS="$OPTIONS SOCK_CGROUP_DATA" # Added in 4.5
56OPTIONS="$OPTIONS CRYPTO_ECHAINIV" # Added in 4.1
57OPTIONS="$OPTIONS BPF_SYSCALL" # Added in 3.18
58OPTIONS="$OPTIONS IPV6_VTI" # Added in 3.13
59OPTIONS="$OPTIONS IPV6_PRIVACY" # Removed in 3.12
60OPTIONS="$OPTIONS NETFILTER_TPROXY" # Removed in 3.11
Dmitry Shmidt77a507a2018-01-08 13:26:19 -080061
Alistair Strachan473536b2018-05-14 12:58:46 -070062# UML specific options
63OPTIONS="$OPTIONS BLK_DEV_UBD HOSTFS"
64
65# QEMU specific options
Maciej Żenczykowski2b0d0522019-04-16 17:25:52 -070066OPTIONS="$OPTIONS PCI VIRTIO VIRTIO_PCI VIRTIO_BLK NET_9P NET_9P_VIRTIO 9P_FS"
Alistair Delvaafbd6322019-07-10 17:18:39 -070067OPTIONS="$OPTIONS CRYPTO_DEV_VIRTIO SERIAL_8250 SERIAL_8250_PCI"
Alistair Delva2d10e032022-12-05 16:06:40 -080068OPTIONS="$OPTIONS SERIAL_8250_CONSOLE PCI_HOST_GENERIC SERIAL_AMBA_PL011"
69OPTIONS="$OPTIONS SERIAL_AMBA_PL011_CONSOLE"
Alistair Strachan473536b2018-05-14 12:58:46 -070070
Alistair Strachanab987102018-05-10 16:44:52 -070071# Obsolete options present at some time in Android kernels
72OPTIONS="$OPTIONS IP_NF_TARGET_REJECT_SKERR IP6_NF_TARGET_REJECT_SKERR"
Lorenzo Colitti08c05d32014-03-27 20:10:14 +090073
Maciej Żenczykowskie2e6f032022-12-19 09:42:38 +000074# b/262323440 - UML *sometimes* seems to have issues with:
75# UPSTREAM: hardening: Clarify Kconfig text for auto-var-init
76# which is in 4.14.~299/4.19.~266 LTS and which does:
77# prompt "Initialize kernel stack variables at function entry"
78# default GCC_PLUGIN_STRUCTLEAK_BYREF_ALL if COMPILE_TEST && GCC_PLUGINS
79# default INIT_STACK_ALL_PATTERN if COMPILE_TEST && CC_HAS_AUTO_VAR_INIT_PATTERN
80# + default INIT_STACK_ALL_ZERO if CC_HAS_AUTO_VAR_INIT_PATTERN
81# default INIT_STACK_NONE
82# and thus presumably switches from INIT_STACK_NONE to INIT_STACK_ALL_ZERO
83#
84# My guess it that this is triggering some sort of UML and/or compiler bug...
85# Let's just turn it off... we don't care that much.
86OPTIONS="$OPTIONS INIT_STACK_NONE"
87
Lorenzo Colitti5812a192015-05-20 15:53:30 +090088# These two break the flo kernel due to differences in -Werror on recent GCC.
Alistair Strachan5055c072018-05-10 15:36:55 -070089DISABLE_OPTIONS=" REISERFS_FS ANDROID_PMEM"
90
Lorenzo Colitti93ea2182015-10-16 12:57:39 +090091# How many TAP interfaces to create to provide the VM with real network access
92# via the host. This requires privileges (e.g., root access) on the host.
93#
94# This is not needed to run the tests, but can be used, for example, to allow
95# the VM to update system packages, or to write tests that need access to a
96# real network. The VM does not set up networking by default, but it contains a
97# DHCP client and has the ability to use IPv6 autoconfiguration. This script
98# does not perform any host-level setup beyond configuring tap interfaces;
99# configuring IPv4 NAT and/or IPv6 router advertisements or ND proxying must
100# be done separately.
101NUMTAPINTERFACES=0
Lorenzo Colitti08c05d32014-03-27 20:10:14 +0900102
103# The root filesystem disk image we'll use.
Krzysztof Kosiński3dee82d2022-11-30 09:34:05 +0000104ROOTFS=${ROOTFS:-net_test.rootfs.20221014}
Lorenzo Colitticd7717e2015-02-03 16:20:57 +0900105COMPRESSED_ROOTFS=$ROOTFS.xz
106URL=https://dl.google.com/dl/android/$COMPRESSED_ROOTFS
Lorenzo Colitti08c05d32014-03-27 20:10:14 +0900107
Lorenzo Colittib41d4972016-08-29 13:49:02 +0900108# Parse arguments and figure out which test to run.
Alistair Strachan473536b2018-05-14 12:58:46 -0700109ARCH=${ARCH:-um}
Alessio Balsini0f368562019-07-23 09:42:39 +0100110J=${J:-$(nproc)}
Patrick Tjin848fe272016-09-07 15:27:19 -0700111MAKE="make"
112OUT_DIR=$(readlink -f ${OUT_DIR:-.})
113KERNEL_DIR=$(readlink -f ${KERNEL_DIR:-.})
114if [ "$OUT_DIR" != "$KERNEL_DIR" ]; then
115 MAKE="$MAKE O=$OUT_DIR"
116fi
117SCRIPT_DIR=$(dirname $(readlink -f $0))
118CONFIG_SCRIPT=${KERNEL_DIR}/scripts/config
119CONFIG_FILE=${OUT_DIR}/.config
Bindu Mahadev2fac1092016-03-03 16:45:10 -0800120consolemode=
Alistair Strachan473536b2018-05-14 12:58:46 -0700121netconfig=
Bindu Mahadev2fac1092016-03-03 16:45:10 -0800122testmode=
Alistair Strachan473536b2018-05-14 12:58:46 -0700123cmdline=
Maciej Żenczykowski43115d52018-10-12 15:02:07 -0700124nowrite=1
Lorenzo Colitti9e4fc1b2016-12-06 13:30:46 +0900125nobuild=0
Lorenzo Colitti695d4152016-12-06 17:21:15 +0900126norun=0
127
Patrick Rohre5d5a0e2024-04-15 14:32:26 -0700128if [[ ! -f "${KERNEL_DIR}/Makefile" ]]; then
129 echo "No kernel Makefile found. Are you running this from a kernel directory?"
130 exit 1
131fi
132
Maciej Żenczykowski04a54a92022-05-25 04:31:30 -0700133KVER_MAJOR="$(sed -rn 's@^ *VERSION *= *([0-9]+)$@\1@p' < "${KERNEL_DIR}/Makefile")"
134KVER_MINOR="$(sed -rn 's@^ *PATCHLEVEL *= *([0-9]+)$@\1@p' < "${KERNEL_DIR}/Makefile")"
135KVER_LEVEL="$(sed -rn 's@^ *SUBLEVEL *= *([0-9]+)$@\1@p' < "${KERNEL_DIR}/Makefile")"
136KVER="${KVER_MAJOR}.${KVER_MINOR}.${KVER_LEVEL}"
137echo "Detected kernel version ${KVER}"
138
Maciej Żenczykowski5e0d8882021-05-21 06:25:58 -0700139if [[ -z "${DEFCONFIG:-}" ]]; then
Maciej Żenczykowskid07fcb02020-01-30 02:22:42 -0800140 case "${ARCH}" in
141 um)
142 export DEFCONFIG=defconfig
143 ;;
144 arm64)
145 if [[ -e arch/arm64/configs/gki_defconfig ]]; then
146 export DEFCONFIG=gki_defconfig
147 elif [[ -e arch/arm64/configs/cuttlefish_defconfig ]]; then
148 export DEFCONFIG=cuttlefish_defconfig
149 fi
150 ;;
151 x86_64)
152 if [[ -e arch/x86/configs/gki_defconfig ]]; then
153 export DEFCONFIG=gki_defconfig
154 elif [[ -e arch/x86/configs/x86_64_cuttlefish_defconfig ]]; then
155 export DEFCONFIG=x86_64_cuttlefish_defconfig
156 fi
157 esac
158fi
159
Maciej Żenczykowski42f96342018-11-07 02:16:37 -0800160if tty >/dev/null; then
161 verbose=
162else
163 verbose=1
164fi
165
Maciej Żenczykowski1dbe5052020-01-30 02:27:39 -0800166test=all_tests.sh
Maciej Żenczykowski43115d52018-10-12 15:02:07 -0700167while [[ -n "$1" ]]; do
Maciej Żenczykowskid2d9f4a2020-01-30 02:26:45 -0800168 if [[ "$1" == "--builder" || "$1" == "-b" ]]; then
Lorenzo Colitti9e4fc1b2016-12-06 13:30:46 +0900169 consolemode="con=null,fd:1"
170 testmode=builder
171 shift
Maciej Żenczykowski43115d52018-10-12 15:02:07 -0700172 elif [[ "$1" == "--readwrite" || "$1" == "--rw" ]]; then
173 nowrite=0
174 shift
175 elif [[ "$1" == "--readonly" || "$1" == "--ro" ]]; then
Alistair Strachan473536b2018-05-14 12:58:46 -0700176 nowrite=1
Lorenzo Colitti9e4fc1b2016-12-06 13:30:46 +0900177 shift
Maciej Żenczykowski43115d52018-10-12 15:02:07 -0700178 elif [[ "$1" == "--nobuild" ]]; then
Lorenzo Colitti9e4fc1b2016-12-06 13:30:46 +0900179 nobuild=1
180 shift
Maciej Żenczykowski19a58222025-02-11 18:01:42 -0800181 elif [[ "$1" == "--build" ]]; then
182 nobuild=0
183 shift
Maciej Żenczykowski43115d52018-10-12 15:02:07 -0700184 elif [[ "$1" == "--norun" ]]; then
Lorenzo Colitti695d4152016-12-06 17:21:15 +0900185 norun=1
186 shift
Maciej Żenczykowski19a58222025-02-11 18:01:42 -0800187 elif [[ "$1" == "--run" ]]; then
188 norun=0
189 shift
Maciej Żenczykowski42f96342018-11-07 02:16:37 -0800190 elif [[ "$1" == "--verbose" ]]; then
191 verbose=1
192 shift
193 elif [[ "$1" == "--noverbose" ]]; then
194 verbose=
195 shift
Lorenzo Colitti9e4fc1b2016-12-06 13:30:46 +0900196 else
197 test=$1
Joel Scherpelzde316e82017-02-23 13:51:31 +0900198 break # Arguments after the test file are passed to the test itself.
Lorenzo Colitti9e4fc1b2016-12-06 13:30:46 +0900199 fi
200done
Lorenzo Colittib41d4972016-08-29 13:49:02 +0900201
Joel Scherpelzde316e82017-02-23 13:51:31 +0900202# Check that test file exists and is readable
203test_file=$SCRIPT_DIR/$test
204if [[ ! -e $test_file ]]; then
205 echo "test file '${test_file}' does not exist"
206 exit 1
207fi
208
209if [[ ! -x $test_file ]]; then
210 echo "test file '${test_file}' is not executable"
211 exit 1
212fi
213
214# Collect trailing arguments to pass to $test
215test_args=${@:2}
216
Lorenzo Colitti695d4152016-12-06 17:21:15 +0900217function isRunningTest() {
Lorenzo Colitti8506ff92020-05-09 16:17:46 +0900218 ! (( norun ))
Lorenzo Colitti695d4152016-12-06 17:21:15 +0900219}
220
221function isBuildOnly() {
Lorenzo Colitti8506ff92020-05-09 16:17:46 +0900222 (( norun )) && ! (( nobuild ))
Lorenzo Colitti695d4152016-12-06 17:21:15 +0900223}
224
225if ! isRunningTest && ! isBuildOnly; then
226 echo "Usage:" >&2
Lorenzo Colitti8506ff92020-05-09 16:17:46 +0900227 echo " $0 [--builder] [--readonly|--ro|--readwrite|--rw] [--nobuild] [--verbose] [<test>]" >&2
228 echo " - if [<test>] is not specified, run all_tests.sh" >&2
Lorenzo Colitti695d4152016-12-06 17:21:15 +0900229 echo " $0 --norun" >&2
Lorenzo Colittib41d4972016-08-29 13:49:02 +0900230 exit 1
Bindu Mahadev2fac1092016-03-03 16:45:10 -0800231fi
Lorenzo Colitti08c05d32014-03-27 20:10:14 +0900232
Patrick Tjin848fe272016-09-07 15:27:19 -0700233cd $OUT_DIR
234echo Running tests from: `pwd`
235
Lorenzo Colitti08c05d32014-03-27 20:10:14 +0900236set -e
237
238# Check if we need to uncompress the disk image.
239# We use xz because it compresses better: to 42M vs 72M (gzip) / 62M (bzip2).
Patrick Tjin848fe272016-09-07 15:27:19 -0700240cd $SCRIPT_DIR
Lorenzo Colitticd7717e2015-02-03 16:20:57 +0900241if [ ! -f $ROOTFS ]; then
242 echo "Deleting $COMPRESSED_ROOTFS" >&2
243 rm -f $COMPRESSED_ROOTFS
244 echo "Downloading $URL" >&2
Bindu Mahadev3f8e37d2016-07-13 17:00:02 -0700245 wget -nv $URL
Lorenzo Colitticd7717e2015-02-03 16:20:57 +0900246 echo "Uncompressing $COMPRESSED_ROOTFS" >&2
247 unxz $COMPRESSED_ROOTFS
Lorenzo Colitti08c05d32014-03-27 20:10:14 +0900248fi
Maciej Żenczykowski952a9fa2020-09-02 15:32:24 -0700249if ! [[ "${ROOTFS}" =~ ^/ ]]; then
250 ROOTFS="${SCRIPT_DIR}/${ROOTFS}"
251fi
Lorenzo Colitticd7717e2015-02-03 16:20:57 +0900252echo "Using $ROOTFS"
253cd -
Lorenzo Colitti08c05d32014-03-27 20:10:14 +0900254
Lorenzo Colitti93ea2182015-10-16 12:57:39 +0900255# If network access was requested, create NUMTAPINTERFACES tap interfaces on
256# the host, and prepare UML command line params to use them. The interfaces are
257# called <user>TAP0, <user>TAP1, on the host, and eth0, eth1, ..., in the VM.
258if (( $NUMTAPINTERFACES > 0 )); then
259 user=${USER:0:10}
260 tapinterfaces=
Lorenzo Colitti93ea2182015-10-16 12:57:39 +0900261 for id in $(seq 0 $(( NUMTAPINTERFACES - 1 )) ); do
262 tap=${user}TAP$id
263 tapinterfaces="$tapinterfaces $tap"
264 mac=$(printf fe:fd:00:00:00:%02x $id)
Alistair Strachan473536b2018-05-14 12:58:46 -0700265 if [ "$ARCH" == "um" ]; then
266 netconfig="$netconfig eth$id=tuntap,$tap,$mac"
267 else
268 netconfig="$netconfig -netdev tap,id=hostnet$id,ifname=$tap,script=no,downscript=no"
269 netconfig="$netconfig -device virtio-net-pci,netdev=hostnet$id,id=net$id,mac=$mac"
270 fi
Lorenzo Colitti93ea2182015-10-16 12:57:39 +0900271 done
Lorenzo Colitti08c05d32014-03-27 20:10:14 +0900272
Lorenzo Colitti93ea2182015-10-16 12:57:39 +0900273 for tap in $tapinterfaces; do
274 if ! ip link list $tap > /dev/null; then
275 echo "Creating tap interface $tap" >&2
276 sudo tunctl -u $USER -t $tap
277 sudo ip link set $tap up
278 fi
279 done
280fi
Lorenzo Colitti08c05d32014-03-27 20:10:14 +0900281
Maciej Żenczykowski5e0d8882021-05-21 06:25:58 -0700282if [[ -n "${KERNEL_BINARY:-}" ]]; then
Lorenzo Colitti9e4fc1b2016-12-06 13:30:46 +0900283 nobuild=1
Benedict Wong7878ca72018-07-13 15:56:36 -0700284else
285 # Set default KERNEL_BINARY location if it was not provided.
286 if [ "$ARCH" == "um" ]; then
287 KERNEL_BINARY=./linux
Alistair Strachan9ea60922018-10-25 13:36:34 -0700288 elif [ "$ARCH" == "i386" -o "$ARCH" == "x86_64" -o "$ARCH" == "x86" ]; then
Benedict Wong7878ca72018-07-13 15:56:36 -0700289 KERNEL_BINARY=./arch/x86/boot/bzImage
Alistair Strachan9ea60922018-10-25 13:36:34 -0700290 elif [ "$ARCH" == "arm64" ]; then
291 KERNEL_BINARY=./arch/arm64/boot/Image.gz
Benedict Wong7878ca72018-07-13 15:56:36 -0700292 fi
Lorenzo Colitti9e4fc1b2016-12-06 13:30:46 +0900293fi
294
295if ((nobuild == 0)); then
Alistair Strachan473536b2018-05-14 12:58:46 -0700296 make_flags=
297 if [ "$ARCH" == "um" ]; then
298 # Exporting ARCH=um SUBARCH=x86_64 doesn't seem to work, as it
299 # "sometimes" (?) results in a 32-bit kernel.
Maciej Żenczykowski9520f1f2020-09-02 13:31:56 -0700300 make_flags="$make_flags ARCH=$ARCH SUBARCH=${SUBARCH:-x86_64} CROSS_COMPILE= "
Alistair Strachan473536b2018-05-14 12:58:46 -0700301 fi
Maciej Żenczykowski5e0d8882021-05-21 06:25:58 -0700302 if [[ -n "${CC:-}" ]]; then
Alistair Strachan473536b2018-05-14 12:58:46 -0700303 # The CC flag is *not* inherited from the environment, so it must be
304 # passed in on the command line.
305 make_flags="$make_flags CC=$CC"
Lorenzo Colitti3544a6f2015-09-18 11:48:49 +0900306 fi
307
Alistair Strachan473536b2018-05-14 12:58:46 -0700308 # If there's no kernel config at all, create one or UML won't work.
Alistair Strachan473536b2018-05-14 12:58:46 -0700309 [ -f $CONFIG_FILE ] || (cd $KERNEL_DIR && $MAKE $make_flags $DEFCONFIG)
310
311 # Enable the kernel config options listed in $OPTIONS.
312 $CONFIG_SCRIPT --file $CONFIG_FILE ${OPTIONS// / -e }
313
Maciej Żenczykowskief5ee4d2024-04-04 20:06:58 -0700314 # Increase acceptable frame size.
315 $CONFIG_SCRIPT --file $CONFIG_FILE --set-val FRAME_WARN 3172
316
Alistair Strachan473536b2018-05-14 12:58:46 -0700317 # Disable the kernel config options listed in $DISABLE_OPTIONS.
318 $CONFIG_SCRIPT --file $CONFIG_FILE ${DISABLE_OPTIONS// / -d }
319
Maciej Żenczykowski5e0d8882021-05-21 06:25:58 -0700320 echo "Running: $MAKE $make_flags olddefconfig"
Alistair Strachan473536b2018-05-14 12:58:46 -0700321 $MAKE $make_flags olddefconfig
322
Lorenzo Colitti3544a6f2015-09-18 11:48:49 +0900323 # Compile the kernel.
Alistair Strachan473536b2018-05-14 12:58:46 -0700324 if [ "$ARCH" == "um" ]; then
Maciej Żenczykowski5e0d8882021-05-21 06:25:58 -0700325 echo "Running: $MAKE -j$J $make_flags linux"
Alistair Strachan473536b2018-05-14 12:58:46 -0700326 $MAKE -j$J $make_flags linux
Alistair Strachan473536b2018-05-14 12:58:46 -0700327 else
Maciej Żenczykowski5e0d8882021-05-21 06:25:58 -0700328 echo "Running: $MAKE -j$J $make_flags"
Alistair Strachan473536b2018-05-14 12:58:46 -0700329 $MAKE -j$J $make_flags
Alistair Strachan473536b2018-05-14 12:58:46 -0700330 fi
Lorenzo Colitti62ec9042014-05-11 20:29:42 +0900331fi
Lorenzo Colitti08c05d32014-03-27 20:10:14 +0900332
Lorenzo Colitti695d4152016-12-06 17:21:15 +0900333if (( norun == 1 )); then
334 exit 0
335fi
336
Alistair Strachan473536b2018-05-14 12:58:46 -0700337if (( nowrite == 1 )); then
338 cmdline="ro"
339fi
Maciej Żenczykowski42f96342018-11-07 02:16:37 -0800340
341if (( verbose == 1 )); then
342 cmdline="$cmdline verbose=1"
343fi
344
Maciej Żenczykowskicfa88f72019-09-02 09:33:34 -0700345cmdline="$cmdline panic=1 init=/sbin/net_test.sh"
Alistair Strachan473536b2018-05-14 12:58:46 -0700346cmdline="$cmdline net_test_args=\"$test_args\" net_test_mode=$testmode"
Lorenzo Colitti08c05d32014-03-27 20:10:14 +0900347
Maciej Żenczykowski40fe1562020-01-28 19:18:07 -0800348# Experience shows that we need at least 128 bits of entropy for the
349# kernel's crng init to complete (before it fully initializes stuff behaves
350# *weirdly* and there's plenty of kernel warnings and some tests even fail),
351# hence net_test.sh needs at least 32 hex chars (which is the amount of hex
352# in a single random UUID) provided to it on the kernel cmdline.
353#
354# Just to be safe, we'll pass in 384 bits, and we'll do this as a random
355# 64 character base64 seed (because this is shorter than base16).
356# We do this by getting *three* random UUIDs and concatenating their hex
357# digits into an *even* length hex encoded string, which we then convert
358# into base64.
359entropy="$(cat /proc/sys/kernel/random{/,/,/}uuid | tr -d '\n-')"
360entropy="$(xxd -r -p <<< "${entropy}" | base64 -w 0)"
Maciej Żenczykowski8bc63c32020-02-05 07:42:59 -0800361cmdline="${cmdline} random.trust_cpu=on entropy=${entropy}"
Maciej Żenczykowski40fe1562020-01-28 19:18:07 -0800362
Alistair Strachan473536b2018-05-14 12:58:46 -0700363if [ "$ARCH" == "um" ]; then
364 # Get the absolute path to the test file that's being run.
365 cmdline="$cmdline net_test=/host$SCRIPT_DIR/$test"
366
Maciej Żenczykowski0c89e402021-02-02 21:25:59 -0800367 # We'd use UML's /proc/exitcode feature to communicate errors on test failure,
368 # if not for UML having a tendency to crash during shutdown,
369 # so instead use an extra serial line we'll redirect to an open fd...
Alistair Delva668071d2021-04-09 11:54:59 -0700370 cmdline="$cmdline exitcode=/dev/ttyS3"
Alistair Strachan473536b2018-05-14 12:58:46 -0700371
372 # Map the --readonly flag to UML block device names
373 if ((nowrite == 0)); then
374 blockdevice=ubda
375 else
Maciej Żenczykowski75b9e5e2018-10-12 14:57:55 -0700376 blockdevice=ubdar
Alistair Strachan473536b2018-05-14 12:58:46 -0700377 fi
378
Maciej Żenczykowski0c89e402021-02-02 21:25:59 -0800379 # Create a temp file for 'serial line 3' for return code.
380 SSL3="$(mktemp)"
381
Maciej Żenczykowskid8cd1252019-02-05 15:43:34 -0800382 exitcode=0
Maciej Żenczykowski0c89e402021-02-02 21:25:59 -0800383 $KERNEL_BINARY >&2 3>"${SSL3}" umid=net_test mem=512M \
384 $blockdevice=$ROOTFS $netconfig $consolemode ssl3=null,fd:3 $cmdline \
Maciej Żenczykowskid8cd1252019-02-05 15:43:34 -0800385 || exitcode=$?
386
Maciej Żenczykowski7c95cc32021-10-11 11:16:34 -0700387 # Return to beginning of line (via carriage return) after the above newline moved us down.
388 echo -en '\r'
389 # re-enable: 'postprocess output' and 'translate newline to carriage return-newline'
390 stty opost onlcr || :
391
Maciej Żenczykowski0c89e402021-02-02 21:25:59 -0800392 if [[ "${exitcode}" == 134 && -s "${SSL3}" && "$(tr -d '\r' < "${SSL3}")" == 0 ]]; then
393 # Sometimes the tests all pass, but UML crashes during the shutdown process itself.
394 # As such we can't actually rely on the /proc/exitcode returned value.
395 echo "Warning: UML appears to have crashed after successfully executing the tests." 1>&2
396 elif [[ "${exitcode}" != 0 ]]; then
397 echo "Warning: UML exited with ${exitcode} instead of zero." 1>&2
398 fi
399
400 if [[ -s "${SSL3}" ]]; then
401 exitcode="$(tr -d '\r' < "${SSL3}")"
402 echo "Info: retrieved exit code ${exitcode}." 1>&2
403 fi
404
405 rm -f "${SSL3}"
406 unset SSL3
407
Maciej Żenczykowskid8cd1252019-02-05 15:43:34 -0800408 # UML is kind of crazy in how guest syscalls work. It requires host kernel
409 # to not be in vsyscall=none mode.
410 if [[ "${exitcode}" != '0' ]]; then
411 {
412 # Hopefully one of these exists
413 cat /proc/config || :
414 zcat /proc/config.gz || :
415 cat "/boot/config-$(uname -r)" || :
416 zcat "/boot/config-$(uname -r).gz" || :
417 } 2>/dev/null \
418 | egrep -q '^CONFIG_LEGACY_VSYSCALL_NONE=y' \
Maciej Żenczykowski58a81412019-10-23 13:52:51 -0700419 && ! egrep -q '(^| )vsyscall=(native|emulate|xonly)( |$)' /proc/cmdline \
Maciej Żenczykowskid8cd1252019-02-05 15:43:34 -0800420 && {
Maciej Żenczykowskifd59e752019-03-05 17:51:32 -0800421 echo -e "\r"
422 echo -e "-----=====-----\r"
423 echo -e "If above you saw a 'net_test.sh[1]: segfault at ...' followed by\r"
424 echo -e "'Kernel panic - not syncing: Attempted to kill init!' then please\r"
425 echo -e "set 'vsyscall=emulate' on *host* kernel command line.\r"
Maciej Żenczykowski58a81412019-10-23 13:52:51 -0700426 echo -e "On Linux 5.2+ you can instead use the slightly safer 'vsyscall=xonly'.\r"
Maciej Żenczykowskifd59e752019-03-05 17:51:32 -0800427 echo -e "(for example via GRUB_CMDLINE_LINUX in /etc/default/grub)\r"
428 echo -e "-----=====-----\r"
Maciej Żenczykowskid8cd1252019-02-05 15:43:34 -0800429 }
430 fi
Alistair Strachan473536b2018-05-14 12:58:46 -0700431else
432 # We boot into the filesystem image directly in all cases
433 cmdline="$cmdline root=/dev/vda"
434
435 # The path is stripped by the 9p export; we don't need SCRIPT_DIR
436 cmdline="$cmdline net_test=/host/$test"
437
Alistair Strachan473536b2018-05-14 12:58:46 -0700438 # Map the --readonly flag to a QEMU block device flag
Alistair Strachan473536b2018-05-14 12:58:46 -0700439 if ((nowrite > 0)); then
Alistair Delva0797d522022-12-05 16:08:46 -0800440 blockdevice=",readonly=on"
Maciej Żenczykowski75b9e5e2018-10-12 14:57:55 -0700441 else
442 blockdevice=
Alistair Strachan473536b2018-05-14 12:58:46 -0700443 fi
Maciej Żenczykowski952a9fa2020-09-02 15:32:24 -0700444 blockdevice="-drive file=$ROOTFS,format=raw,if=none,id=drive-virtio-disk0$blockdevice"
Alistair Strachan473536b2018-05-14 12:58:46 -0700445 blockdevice="$blockdevice -device virtio-blk-pci,drive=drive-virtio-disk0"
446
Maciej Żenczykowski42f96342018-11-07 02:16:37 -0800447 # Pass through our current console/screen size to inner shell session
448 read rows cols < <(stty size 2>/dev/null)
449 [[ -z "${rows}" ]] || cmdline="${cmdline} console_rows=${rows}"
450 [[ -z "${cols}" ]] || cmdline="${cmdline} console_cols=${cols}"
451 unset rows cols
452
Alistair Strachan9ea60922018-10-25 13:36:34 -0700453 # QEMU has no way to modify its exitcode; simulate it with a serial port.
454 #
455 # Choose to do it this way over writing a file to /host, because QEMU will
456 # initialize the 'exitcode' file for us, it avoids unnecessary writes to the
457 # host filesystem (which is normally not written to) and it allows us to
458 # communicate an exit code back in cases we do not have /host mounted.
459 #
460 if [ "$ARCH" == "i386" -o "$ARCH" == "x86_64" -o "$ARCH" == "x86" ]; then
461 # Assume we have hardware-accelerated virtualization support for amd64
462 qemu="qemu-system-x86_64 -machine pc,accel=kvm -cpu host"
463
Alistair Delva297632a2019-07-10 17:22:15 -0700464 # We know 'ttyS0' will be our serial port on x86 from the hard-coded
465 # '-serial mon:stdio' flag below
466 cmdline="$cmdline console=ttyS0"
467
468 # The assignment of 'ttyS1' here is magical; we know ttyS0 was used up
469 # by '-serial mon:stdio', and so this second serial port will be 'ttyS1'
Alistair Delva668071d2021-04-09 11:54:59 -0700470 cmdline="$cmdline exitcode=/dev/ttyS1"
Alistair Strachan9ea60922018-10-25 13:36:34 -0700471 elif [ "$ARCH" == "arm64" ]; then
472 # This uses a software model CPU, based on cortex-a57
473 qemu="qemu-system-aarch64 -machine virt -cpu cortex-a57"
474
Alistair Delva297632a2019-07-10 17:22:15 -0700475 # We know 'ttyAMA0' will be our serial port on arm64 from the hard-coded
476 # '-serial mon:stdio' flag below
477 cmdline="$cmdline console=ttyAMA0"
478
Alistair Strachan9ea60922018-10-25 13:36:34 -0700479 # The kernel will print messages via a virtual ARM serial port (ttyAMA0),
480 # but for command line consistency with x86, we put the exitcode serial
481 # port on the PCI bus, and it will be the only one.
Alistair Delva668071d2021-04-09 11:54:59 -0700482 cmdline="$cmdline exitcode=/dev/ttyS0"
Alistair Strachan9ea60922018-10-25 13:36:34 -0700483 fi
484
485 $qemu >&2 -name net_test -m 512 \
Alistair Strachan473536b2018-05-14 12:58:46 -0700486 -kernel $KERNEL_BINARY \
Maciej Żenczykowski42f96342018-11-07 02:16:37 -0800487 -no-user-config -nodefaults -no-reboot \
488 -display none -nographic -serial mon:stdio -parallel none \
Alistair Strachan9ea60922018-10-25 13:36:34 -0700489 -smp 4,sockets=4,cores=1,threads=1 \
Maciej Żenczykowski2cc987d2018-10-09 15:54:58 -0700490 -device virtio-rng-pci \
Alistair Strachan473536b2018-05-14 12:58:46 -0700491 -chardev file,id=exitcode,path=exitcode \
Alistair Strachan9ea60922018-10-25 13:36:34 -0700492 -device pci-serial,chardev=exitcode \
Alistair Strachan473536b2018-05-14 12:58:46 -0700493 -fsdev local,security_model=mapped-xattr,id=fsdev0,fmode=0644,dmode=0755,path=$SCRIPT_DIR \
494 -device virtio-9p-pci,id=fs0,fsdev=fsdev0,mount_tag=host \
Maciej Żenczykowski42f96342018-11-07 02:16:37 -0800495 $blockdevice $netconfig -append "$cmdline"
496 [[ -s exitcode ]] && exitcode=`cat exitcode | tr -d '\r'` || exitcode=1
Alistair Strachan473536b2018-05-14 12:58:46 -0700497 rm -f exitcode
Alistair Strachan473536b2018-05-14 12:58:46 -0700498fi
Maciej Żenczykowski99cd00d2018-10-14 20:03:23 -0700499
500# UML reliably screws up the ptys, QEMU probably can as well...
501fixup_ptys
Maciej Żenczykowski42f96342018-11-07 02:16:37 -0800502stty sane || :
Maciej Żenczykowskieedec7f2019-08-28 22:51:12 -0700503tput smam || :
Maciej Żenczykowski42f96342018-11-07 02:16:37 -0800504
505echo "Returning exit code ${exitcode}." 1>&2
Maciej Żenczykowski99cd00d2018-10-14 20:03:23 -0700506exit "${exitcode}"