ca3853aa1999bd57e25d5505ea769ffdf14f56a7
[openwrt/staging/ynezz.git] /
1 From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
2 From: "Jason A. Donenfeld" <Jason@zx2c4.com>
3 Date: Sun, 15 Dec 2019 22:08:00 +0100
4 Subject: [PATCH] wireguard: selftests: import harness makefile for test suite
5
6 commit 65d88d04114bca7d85faebd5fed61069cb2b632c upstream.
7
8 WireGuard has been using this on build.wireguard.com for the last
9 several years with considerable success. It allows for very quick and
10 iterative development cycles, and supports several platforms.
11
12 To run the test suite on your current platform in QEMU:
13
14 $ make -C tools/testing/selftests/wireguard/qemu -j$(nproc)
15
16 To run it with KASAN and such turned on:
17
18 $ DEBUG_KERNEL=yes make -C tools/testing/selftests/wireguard/qemu -j$(nproc)
19
20 To run it emulated for another platform in QEMU:
21
22 $ ARCH=arm make -C tools/testing/selftests/wireguard/qemu -j$(nproc)
23
24 At the moment, we support aarch64_be, aarch64, arm, armeb, i686, m68k,
25 mips64, mips64el, mips, mipsel, powerpc64le, powerpc, and x86_64.
26
27 The system supports incremental rebuilding, so it should be very fast to
28 change a single file and then test it out and have immediate feedback.
29
30 This requires for the right toolchain and qemu to be installed prior.
31 I've had success with those from musl.cc.
32
33 This is tailored for WireGuard at the moment, though later projects
34 might generalize it for other network testing.
35
36 Signed-off-by: Jason A. Donenfeld <Jason@zx2c4.com>
37 Signed-off-by: David S. Miller <davem@davemloft.net>
38 Signed-off-by: Jason A. Donenfeld <Jason@zx2c4.com>
39 ---
40 .../selftests/wireguard/qemu/.gitignore | 2 +
41 .../testing/selftests/wireguard/qemu/Makefile | 385 ++++++++++++++++++
42 .../wireguard/qemu/arch/aarch64.config | 5 +
43 .../wireguard/qemu/arch/aarch64_be.config | 6 +
44 .../selftests/wireguard/qemu/arch/arm.config | 9 +
45 .../wireguard/qemu/arch/armeb.config | 10 +
46 .../selftests/wireguard/qemu/arch/i686.config | 5 +
47 .../selftests/wireguard/qemu/arch/m68k.config | 9 +
48 .../selftests/wireguard/qemu/arch/mips.config | 11 +
49 .../wireguard/qemu/arch/mips64.config | 14 +
50 .../wireguard/qemu/arch/mips64el.config | 15 +
51 .../wireguard/qemu/arch/mipsel.config | 12 +
52 .../wireguard/qemu/arch/powerpc.config | 10 +
53 .../wireguard/qemu/arch/powerpc64le.config | 12 +
54 .../wireguard/qemu/arch/x86_64.config | 5 +
55 .../selftests/wireguard/qemu/debug.config | 67 +++
56 tools/testing/selftests/wireguard/qemu/init.c | 284 +++++++++++++
57 .../selftests/wireguard/qemu/kernel.config | 86 ++++
58 18 files changed, 947 insertions(+)
59 create mode 100644 tools/testing/selftests/wireguard/qemu/.gitignore
60 create mode 100644 tools/testing/selftests/wireguard/qemu/Makefile
61 create mode 100644 tools/testing/selftests/wireguard/qemu/arch/aarch64.config
62 create mode 100644 tools/testing/selftests/wireguard/qemu/arch/aarch64_be.config
63 create mode 100644 tools/testing/selftests/wireguard/qemu/arch/arm.config
64 create mode 100644 tools/testing/selftests/wireguard/qemu/arch/armeb.config
65 create mode 100644 tools/testing/selftests/wireguard/qemu/arch/i686.config
66 create mode 100644 tools/testing/selftests/wireguard/qemu/arch/m68k.config
67 create mode 100644 tools/testing/selftests/wireguard/qemu/arch/mips.config
68 create mode 100644 tools/testing/selftests/wireguard/qemu/arch/mips64.config
69 create mode 100644 tools/testing/selftests/wireguard/qemu/arch/mips64el.config
70 create mode 100644 tools/testing/selftests/wireguard/qemu/arch/mipsel.config
71 create mode 100644 tools/testing/selftests/wireguard/qemu/arch/powerpc.config
72 create mode 100644 tools/testing/selftests/wireguard/qemu/arch/powerpc64le.config
73 create mode 100644 tools/testing/selftests/wireguard/qemu/arch/x86_64.config
74 create mode 100644 tools/testing/selftests/wireguard/qemu/debug.config
75 create mode 100644 tools/testing/selftests/wireguard/qemu/init.c
76 create mode 100644 tools/testing/selftests/wireguard/qemu/kernel.config
77
78 --- /dev/null
79 +++ b/tools/testing/selftests/wireguard/qemu/.gitignore
80 @@ -0,0 +1,2 @@
81 +build/
82 +distfiles/
83 --- /dev/null
84 +++ b/tools/testing/selftests/wireguard/qemu/Makefile
85 @@ -0,0 +1,385 @@
86 +# SPDX-License-Identifier: GPL-2.0
87 +#
88 +# Copyright (C) 2015-2019 Jason A. Donenfeld <Jason@zx2c4.com>. All Rights Reserved.
89 +
90 +PWD := $(shell pwd)
91 +
92 +CHOST := $(shell gcc -dumpmachine)
93 +ifneq (,$(ARCH))
94 +CBUILD := $(subst -gcc,,$(lastword $(subst /, ,$(firstword $(wildcard $(foreach bindir,$(subst :, ,$(PATH)),$(bindir)/$(ARCH)-*-gcc))))))
95 +ifeq (,$(CBUILD))
96 +$(error The toolchain for $(ARCH) is not installed)
97 +endif
98 +else
99 +CBUILD := $(CHOST)
100 +ARCH := $(firstword $(subst -, ,$(CBUILD)))
101 +endif
102 +
103 +# Set these from the environment to override
104 +KERNEL_PATH ?= $(PWD)/../../../../..
105 +BUILD_PATH ?= $(PWD)/build/$(ARCH)
106 +DISTFILES_PATH ?= $(PWD)/distfiles
107 +NR_CPUS ?= 4
108 +
109 +MIRROR := https://download.wireguard.com/qemu-test/distfiles/
110 +
111 +default: qemu
112 +
113 +# variable name, tarball project name, version, tarball extension, default URI base
114 +define tar_download =
115 +$(1)_VERSION := $(3)
116 +$(1)_NAME := $(2)-$$($(1)_VERSION)
117 +$(1)_TAR := $(DISTFILES_PATH)/$$($(1)_NAME)$(4)
118 +$(1)_PATH := $(BUILD_PATH)/$$($(1)_NAME)
119 +$(call file_download,$$($(1)_NAME)$(4),$(5),$(6))
120 +endef
121 +
122 +define file_download =
123 +$(DISTFILES_PATH)/$(1):
124 + mkdir -p $(DISTFILES_PATH)
125 + flock -x $$@.lock -c '[ -f $$@ ] && exit 0; wget -O $$@.tmp $(MIRROR)$(1) || wget -t inf --retry-on-http-error=404 -O $$@.tmp $(2)$(1) || rm -f $$@.tmp'
126 + if echo "$(3) $$@.tmp" | sha256sum -c -; then mv $$@.tmp $$@; else rm -f $$@.tmp; exit 71; fi
127 +endef
128 +
129 +$(eval $(call tar_download,MUSL,musl,1.1.20,.tar.gz,https://www.musl-libc.org/releases/,44be8771d0e6c6b5f82dd15662eb2957c9a3173a19a8b49966ac0542bbd40d61))
130 +$(eval $(call tar_download,LIBMNL,libmnl,1.0.4,.tar.bz2,https://www.netfilter.org/projects/libmnl/files/,171f89699f286a5854b72b91d06e8f8e3683064c5901fb09d954a9ab6f551f81))
131 +$(eval $(call tar_download,IPERF,iperf,3.1.7,.tar.gz,http://downloads.es.net/pub/iperf/,a4ef73406fe92250602b8da2ae89ec53211f805df97a1d1d629db5a14043734f))
132 +$(eval $(call tar_download,BASH,bash,5.0,.tar.gz,https://ftp.gnu.org/gnu/bash/,b4a80f2ac66170b2913efbfb9f2594f1f76c7b1afd11f799e22035d63077fb4d))
133 +$(eval $(call tar_download,IPROUTE2,iproute2,5.1.0,.tar.gz,https://www.kernel.org/pub/linux/utils/net/iproute2/,9b43707d6075ecdca14803ca8ce0c8553848c49fa1586d12fd508d66577243f2))
134 +$(eval $(call tar_download,IPTABLES,iptables,1.6.1,.tar.bz2,https://www.netfilter.org/projects/iptables/files/,0fc2d7bd5d7be11311726466789d4c65fb4c8e096c9182b56ce97440864f0cf5))
135 +$(eval $(call tar_download,NMAP,nmap,7.60,.tar.bz2,https://nmap.org/dist/,a8796ecc4fa6c38aad6139d9515dc8113023a82e9d787e5a5fb5fa1b05516f21))
136 +$(eval $(call tar_download,IPUTILS,iputils,s20161105,.tar.gz,https://github.com/iputils/iputils/archive/s20161105.tar.gz/#,f813092f03d17294fd23544b129b95cdb87fe19f7970a51908a6b88509acad8a))
137 +$(eval $(call tar_download,WIREGUARD_TOOLS,WireGuard,0.0.20191212,.tar.xz,https://git.zx2c4.com/WireGuard/snapshot/,b0d718380f7a8822b2f12d75e462fa4eafa3a77871002981f367cd4fe2a1b071))
138 +
139 +KERNEL_BUILD_PATH := $(BUILD_PATH)/kernel$(if $(findstring yes,$(DEBUG_KERNEL)),-debug)
140 +rwildcard=$(foreach d,$(wildcard $1*),$(call rwildcard,$d/,$2) $(filter $(subst *,%,$2),$d))
141 +WIREGUARD_SOURCES := $(call rwildcard,$(KERNEL_PATH)/drivers/net/wireguard/,*)
142 +
143 +export CFLAGS ?= -O3 -pipe
144 +export LDFLAGS ?=
145 +export CPPFLAGS := -I$(BUILD_PATH)/include
146 +
147 +ifeq ($(CHOST),$(CBUILD))
148 +CROSS_COMPILE_FLAG := --host=$(CHOST)
149 +NOPIE_GCC := gcc -fno-PIE
150 +CFLAGS += -march=native
151 +STRIP := strip
152 +else
153 +$(info Cross compilation: building for $(CBUILD) using $(CHOST))
154 +CROSS_COMPILE_FLAG := --build=$(CBUILD) --host=$(CHOST)
155 +export CROSS_COMPILE=$(CBUILD)-
156 +NOPIE_GCC := $(CBUILD)-gcc -fno-PIE
157 +STRIP := $(CBUILD)-strip
158 +endif
159 +ifeq ($(ARCH),aarch64)
160 +QEMU_ARCH := aarch64
161 +KERNEL_ARCH := arm64
162 +KERNEL_BZIMAGE := $(KERNEL_BUILD_PATH)/arch/arm64/boot/Image
163 +ifeq ($(CHOST),$(CBUILD))
164 +QEMU_MACHINE := -cpu host -machine virt,gic_version=host,accel=kvm
165 +else
166 +QEMU_MACHINE := -cpu cortex-a53 -machine virt
167 +CFLAGS += -march=armv8-a -mtune=cortex-a53
168 +endif
169 +else ifeq ($(ARCH),aarch64_be)
170 +QEMU_ARCH := aarch64
171 +KERNEL_ARCH := arm64
172 +KERNEL_BZIMAGE := $(KERNEL_BUILD_PATH)/arch/arm64/boot/Image
173 +ifeq ($(CHOST),$(CBUILD))
174 +QEMU_MACHINE := -cpu host -machine virt,gic_version=host,accel=kvm
175 +else
176 +QEMU_MACHINE := -cpu cortex-a53 -machine virt
177 +CFLAGS += -march=armv8-a -mtune=cortex-a53
178 +endif
179 +else ifeq ($(ARCH),arm)
180 +QEMU_ARCH := arm
181 +KERNEL_ARCH := arm
182 +KERNEL_BZIMAGE := $(KERNEL_BUILD_PATH)/arch/arm/boot/zImage
183 +ifeq ($(CHOST),$(CBUILD))
184 +QEMU_MACHINE := -cpu host -machine virt,gic_version=host,accel=kvm
185 +else
186 +QEMU_MACHINE := -cpu cortex-a15 -machine virt
187 +CFLAGS += -march=armv7-a -mtune=cortex-a15 -mabi=aapcs-linux
188 +endif
189 +else ifeq ($(ARCH),armeb)
190 +QEMU_ARCH := arm
191 +KERNEL_ARCH := arm
192 +KERNEL_BZIMAGE := $(KERNEL_BUILD_PATH)/arch/arm/boot/zImage
193 +ifeq ($(CHOST),$(CBUILD))
194 +QEMU_MACHINE := -cpu host -machine virt,gic_version=host,accel=kvm
195 +else
196 +QEMU_MACHINE := -cpu cortex-a15 -machine virt
197 +CFLAGS += -march=armv7-a -mabi=aapcs-linux # We don't pass -mtune=cortex-a15 due to a compiler bug on big endian.
198 +LDFLAGS += -Wl,--be8
199 +endif
200 +else ifeq ($(ARCH),x86_64)
201 +QEMU_ARCH := x86_64
202 +KERNEL_ARCH := x86_64
203 +KERNEL_BZIMAGE := $(KERNEL_BUILD_PATH)/arch/x86/boot/bzImage
204 +ifeq ($(CHOST),$(CBUILD))
205 +QEMU_MACHINE := -cpu host -machine q35,accel=kvm
206 +else
207 +QEMU_MACHINE := -cpu Skylake-Server -machine q35
208 +CFLAGS += -march=skylake-avx512
209 +endif
210 +else ifeq ($(ARCH),i686)
211 +QEMU_ARCH := i386
212 +KERNEL_ARCH := x86
213 +KERNEL_BZIMAGE := $(KERNEL_BUILD_PATH)/arch/x86/boot/bzImage
214 +ifeq ($(subst i686,x86_64,$(CBUILD)),$(CHOST))
215 +QEMU_MACHINE := -cpu host -machine q35,accel=kvm
216 +else
217 +QEMU_MACHINE := -cpu coreduo -machine q35
218 +CFLAGS += -march=prescott
219 +endif
220 +else ifeq ($(ARCH),mips64)
221 +QEMU_ARCH := mips64
222 +KERNEL_ARCH := mips
223 +KERNEL_BZIMAGE := $(KERNEL_BUILD_PATH)/vmlinux
224 +ifeq ($(CHOST),$(CBUILD))
225 +QEMU_MACHINE := -cpu host -machine malta,accel=kvm
226 +CFLAGS += -EB
227 +else
228 +QEMU_MACHINE := -cpu MIPS64R2-generic -machine malta -smp 1
229 +CFLAGS += -march=mips64r2 -EB
230 +endif
231 +else ifeq ($(ARCH),mips64el)
232 +QEMU_ARCH := mips64el
233 +KERNEL_ARCH := mips
234 +KERNEL_BZIMAGE := $(KERNEL_BUILD_PATH)/vmlinux
235 +ifeq ($(CHOST),$(CBUILD))
236 +QEMU_MACHINE := -cpu host -machine malta,accel=kvm
237 +CFLAGS += -EL
238 +else
239 +QEMU_MACHINE := -cpu MIPS64R2-generic -machine malta -smp 1
240 +CFLAGS += -march=mips64r2 -EL
241 +endif
242 +else ifeq ($(ARCH),mips)
243 +QEMU_ARCH := mips
244 +KERNEL_ARCH := mips
245 +KERNEL_BZIMAGE := $(KERNEL_BUILD_PATH)/vmlinux
246 +ifeq ($(CHOST),$(CBUILD))
247 +QEMU_MACHINE := -cpu host -machine malta,accel=kvm
248 +CFLAGS += -EB
249 +else
250 +QEMU_MACHINE := -cpu 24Kf -machine malta -smp 1
251 +CFLAGS += -march=mips32r2 -EB
252 +endif
253 +else ifeq ($(ARCH),mipsel)
254 +QEMU_ARCH := mipsel
255 +KERNEL_ARCH := mips
256 +KERNEL_BZIMAGE := $(KERNEL_BUILD_PATH)/vmlinux
257 +ifeq ($(CHOST),$(CBUILD))
258 +QEMU_MACHINE := -cpu host -machine malta,accel=kvm
259 +CFLAGS += -EL
260 +else
261 +QEMU_MACHINE := -cpu 24Kf -machine malta -smp 1
262 +CFLAGS += -march=mips32r2 -EL
263 +endif
264 +else ifeq ($(ARCH),powerpc64le)
265 +QEMU_ARCH := ppc64
266 +KERNEL_ARCH := powerpc
267 +KERNEL_BZIMAGE := $(KERNEL_BUILD_PATH)/vmlinux
268 +ifeq ($(CHOST),$(CBUILD))
269 +QEMU_MACHINE := -cpu host,accel=kvm -machine pseries
270 +else
271 +QEMU_MACHINE := -machine pseries
272 +endif
273 +CFLAGS += -mcpu=powerpc64le -mlong-double-64
274 +else ifeq ($(ARCH),powerpc)
275 +QEMU_ARCH := ppc
276 +KERNEL_ARCH := powerpc
277 +KERNEL_BZIMAGE := $(KERNEL_BUILD_PATH)/arch/powerpc/boot/uImage
278 +ifeq ($(CHOST),$(CBUILD))
279 +QEMU_MACHINE := -cpu host,accel=kvm -machine ppce500
280 +else
281 +QEMU_MACHINE := -machine ppce500
282 +endif
283 +CFLAGS += -mcpu=powerpc -mlong-double-64 -msecure-plt
284 +else ifeq ($(ARCH),m68k)
285 +QEMU_ARCH := m68k
286 +KERNEL_ARCH := m68k
287 +KERNEL_BZIMAGE := $(KERNEL_BUILD_PATH)/vmlinux
288 +ifeq ($(CHOST),$(CBUILD))
289 +QEMU_MACHINE := -cpu host,accel=kvm -machine q800
290 +else
291 +QEMU_MACHINE := -machine q800
292 +endif
293 +else
294 +$(error I only build: x86_64, i686, arm, armeb, aarch64, aarch64_be, mips, mipsel, mips64, mips64el, powerpc64le, powerpc, m68k)
295 +endif
296 +
297 +REAL_CC := $(CBUILD)-gcc
298 +MUSL_CC := $(BUILD_PATH)/musl-gcc
299 +export CC := $(MUSL_CC)
300 +USERSPACE_DEPS := $(MUSL_CC) $(BUILD_PATH)/include/.installed $(BUILD_PATH)/include/linux/.installed
301 +
302 +build: $(KERNEL_BZIMAGE)
303 +qemu: $(KERNEL_BZIMAGE)
304 + rm -f $(BUILD_PATH)/result
305 + timeout --foreground 20m qemu-system-$(QEMU_ARCH) \
306 + -nodefaults \
307 + -nographic \
308 + -smp $(NR_CPUS) \
309 + $(QEMU_MACHINE) \
310 + -m $$(grep -q CONFIG_DEBUG_KMEMLEAK=y $(KERNEL_BUILD_PATH)/.config && echo 1G || echo 256M) \
311 + -serial stdio \
312 + -serial file:$(BUILD_PATH)/result \
313 + -no-reboot \
314 + -monitor none \
315 + -kernel $<
316 + grep -Fq success $(BUILD_PATH)/result
317 +
318 +$(BUILD_PATH)/init-cpio-spec.txt:
319 + mkdir -p $(BUILD_PATH)
320 + echo "file /init $(BUILD_PATH)/init 755 0 0" > $@
321 + echo "file /init.sh $(PWD)/../netns.sh 755 0 0" >> $@
322 + echo "dir /dev 755 0 0" >> $@
323 + echo "nod /dev/console 644 0 0 c 5 1" >> $@
324 + echo "dir /bin 755 0 0" >> $@
325 + echo "file /bin/iperf3 $(IPERF_PATH)/src/iperf3 755 0 0" >> $@
326 + echo "file /bin/wg $(WIREGUARD_TOOLS_PATH)/src/tools/wg 755 0 0" >> $@
327 + echo "file /bin/bash $(BASH_PATH)/bash 755 0 0" >> $@
328 + echo "file /bin/ip $(IPROUTE2_PATH)/ip/ip 755 0 0" >> $@
329 + echo "file /bin/ss $(IPROUTE2_PATH)/misc/ss 755 0 0" >> $@
330 + echo "file /bin/ping $(IPUTILS_PATH)/ping 755 0 0" >> $@
331 + echo "file /bin/ncat $(NMAP_PATH)/ncat/ncat 755 0 0" >> $@
332 + echo "file /bin/xtables-multi $(IPTABLES_PATH)/iptables/xtables-multi 755 0 0" >> $@
333 + echo "slink /bin/iptables xtables-multi 777 0 0" >> $@
334 + echo "slink /bin/ping6 ping 777 0 0" >> $@
335 + echo "dir /lib 755 0 0" >> $@
336 + echo "file /lib/libc.so $(MUSL_PATH)/lib/libc.so 755 0 0" >> $@
337 + echo "slink /lib/ld-linux.so.1 libc.so 777 0 0" >> $@
338 +
339 +$(KERNEL_BUILD_PATH)/.config: kernel.config arch/$(ARCH).config
340 + mkdir -p $(KERNEL_BUILD_PATH)
341 + cp kernel.config $(KERNEL_BUILD_PATH)/minimal.config
342 + printf 'CONFIG_NR_CPUS=$(NR_CPUS)\nCONFIG_INITRAMFS_SOURCE="$(BUILD_PATH)/init-cpio-spec.txt"\n' >> $(KERNEL_BUILD_PATH)/minimal.config
343 + cat arch/$(ARCH).config >> $(KERNEL_BUILD_PATH)/minimal.config
344 + $(MAKE) -C $(KERNEL_PATH) O=$(KERNEL_BUILD_PATH) ARCH=$(KERNEL_ARCH) allnoconfig
345 + cd $(KERNEL_BUILD_PATH) && ARCH=$(KERNEL_ARCH) $(KERNEL_PATH)/scripts/kconfig/merge_config.sh -n $(KERNEL_BUILD_PATH)/.config $(KERNEL_BUILD_PATH)/minimal.config
346 + $(if $(findstring yes,$(DEBUG_KERNEL)),cp debug.config $(KERNEL_BUILD_PATH) && cd $(KERNEL_BUILD_PATH) && ARCH=$(KERNEL_ARCH) $(KERNEL_PATH)/scripts/kconfig/merge_config.sh -n $(KERNEL_BUILD_PATH)/.config debug.config,)
347 +
348 +$(KERNEL_BZIMAGE): $(KERNEL_BUILD_PATH)/.config $(BUILD_PATH)/init-cpio-spec.txt $(MUSL_PATH)/lib/libc.so $(IPERF_PATH)/src/iperf3 $(IPUTILS_PATH)/ping $(BASH_PATH)/bash $(IPROUTE2_PATH)/misc/ss $(IPROUTE2_PATH)/ip/ip $(IPTABLES_PATH)/iptables/xtables-multi $(NMAP_PATH)/ncat/ncat $(WIREGUARD_TOOLS_PATH)/src/tools/wg $(BUILD_PATH)/init ../netns.sh $(WIREGUARD_SOURCES)
349 + $(MAKE) -C $(KERNEL_PATH) O=$(KERNEL_BUILD_PATH) ARCH=$(KERNEL_ARCH) CROSS_COMPILE=$(CROSS_COMPILE) CC="$(NOPIE_GCC)"
350 +
351 +$(BUILD_PATH)/include/linux/.installed: | $(KERNEL_BUILD_PATH)/.config
352 + $(MAKE) -C $(KERNEL_PATH) O=$(KERNEL_BUILD_PATH) INSTALL_HDR_PATH=$(BUILD_PATH) ARCH=$(KERNEL_ARCH) CROSS_COMPILE=$(CROSS_COMPILE) headers_install
353 + touch $@
354 +
355 +$(MUSL_PATH)/lib/libc.so: $(MUSL_TAR)
356 + mkdir -p $(BUILD_PATH)
357 + flock -s $<.lock tar -C $(BUILD_PATH) -xf $<
358 + cd $(MUSL_PATH) && CC=$(REAL_CC) ./configure --prefix=/ --disable-static --build=$(CBUILD)
359 + $(MAKE) -C $(MUSL_PATH)
360 + $(STRIP) -s $@
361 +
362 +$(BUILD_PATH)/include/.installed: $(MUSL_PATH)/lib/libc.so
363 + $(MAKE) -C $(MUSL_PATH) DESTDIR=$(BUILD_PATH) install-headers
364 + touch $@
365 +
366 +$(MUSL_CC): $(MUSL_PATH)/lib/libc.so
367 + sh $(MUSL_PATH)/tools/musl-gcc.specs.sh $(BUILD_PATH)/include $(MUSL_PATH)/lib /lib/ld-linux.so.1 > $(BUILD_PATH)/musl-gcc.specs
368 + printf '#!/bin/sh\nexec "$(REAL_CC)" --specs="$(BUILD_PATH)/musl-gcc.specs" -fno-stack-protector -no-pie "$$@"\n' > $(BUILD_PATH)/musl-gcc
369 + chmod +x $(BUILD_PATH)/musl-gcc
370 +
371 +$(IPERF_PATH)/.installed: $(IPERF_TAR)
372 + mkdir -p $(BUILD_PATH)
373 + flock -s $<.lock tar -C $(BUILD_PATH) -xf $<
374 + sed -i '1s/^/#include <stdint.h>/' $(IPERF_PATH)/src/cjson.h $(IPERF_PATH)/src/timer.h
375 + sed -i -r 's/-p?g//g' $(IPERF_PATH)/src/Makefile*
376 + touch $@
377 +
378 +$(IPERF_PATH)/src/iperf3: | $(IPERF_PATH)/.installed $(USERSPACE_DEPS)
379 + cd $(IPERF_PATH) && CFLAGS="$(CFLAGS) -D_GNU_SOURCE" ./configure --prefix=/ $(CROSS_COMPILE_FLAG) --enable-static --disable-shared
380 + $(MAKE) -C $(IPERF_PATH)
381 + $(STRIP) -s $@
382 +
383 +$(LIBMNL_PATH)/.installed: $(LIBMNL_TAR)
384 + flock -s $<.lock tar -C $(BUILD_PATH) -xf $<
385 + touch $@
386 +
387 +$(LIBMNL_PATH)/src/.libs/libmnl.a: | $(LIBMNL_PATH)/.installed $(USERSPACE_DEPS)
388 + cd $(LIBMNL_PATH) && ./configure --prefix=/ $(CROSS_COMPILE_FLAG) --enable-static --disable-shared
389 + $(MAKE) -C $(LIBMNL_PATH)
390 + sed -i 's:prefix=.*:prefix=$(LIBMNL_PATH):' $(LIBMNL_PATH)/libmnl.pc
391 +
392 +$(WIREGUARD_TOOLS_PATH)/.installed: $(WIREGUARD_TOOLS_TAR)
393 + flock -s $<.lock tar -C $(BUILD_PATH) -xf $<
394 + touch $@
395 +
396 +$(WIREGUARD_TOOLS_PATH)/src/tools/wg: | $(WIREGUARD_TOOLS_PATH)/.installed $(LIBMNL_PATH)/src/.libs/libmnl.a $(USERSPACE_DEPS)
397 + LDFLAGS="$(LDFLAGS) -L$(LIBMNL_PATH)/src/.libs" $(MAKE) -C $(WIREGUARD_TOOLS_PATH)/src/tools LIBMNL_CFLAGS="-I$(LIBMNL_PATH)/include" LIBMNL_LDLIBS="-lmnl" wg
398 + $(STRIP) -s $@
399 +
400 +$(BUILD_PATH)/init: init.c | $(USERSPACE_DEPS)
401 + mkdir -p $(BUILD_PATH)
402 + $(MUSL_CC) -o $@ $(CFLAGS) $(LDFLAGS) -std=gnu11 $<
403 + $(STRIP) -s $@
404 +
405 +$(IPUTILS_PATH)/.installed: $(IPUTILS_TAR)
406 + mkdir -p $(BUILD_PATH)
407 + flock -s $<.lock tar -C $(BUILD_PATH) -xf $<
408 + touch $@
409 +
410 +$(IPUTILS_PATH)/ping: | $(IPUTILS_PATH)/.installed $(USERSPACE_DEPS)
411 + $(MAKE) -C $(IPUTILS_PATH) USE_CAP=no USE_IDN=no USE_NETTLE=no USE_CRYPTO=no ping
412 + $(STRIP) -s $@
413 +
414 +$(BASH_PATH)/.installed: $(BASH_TAR)
415 + mkdir -p $(BUILD_PATH)
416 + flock -s $<.lock tar -C $(BUILD_PATH) -xf $<
417 + touch $@
418 +
419 +$(BASH_PATH)/bash: | $(BASH_PATH)/.installed $(USERSPACE_DEPS)
420 + cd $(BASH_PATH) && ./configure --prefix=/ $(CROSS_COMPILE_FLAG) --without-bash-malloc --disable-debugger --disable-help-builtin --disable-history --disable-multibyte --disable-progcomp --disable-readline --disable-mem-scramble
421 + $(MAKE) -C $(BASH_PATH)
422 + $(STRIP) -s $@
423 +
424 +$(IPROUTE2_PATH)/.installed: $(IPROUTE2_TAR)
425 + mkdir -p $(BUILD_PATH)
426 + flock -s $<.lock tar -C $(BUILD_PATH) -xf $<
427 + printf 'CC:=$(CC)\nPKG_CONFIG:=pkg-config\nTC_CONFIG_XT:=n\nTC_CONFIG_ATM:=n\nTC_CONFIG_IPSET:=n\nIP_CONFIG_SETNS:=y\nHAVE_ELF:=n\nHAVE_MNL:=y\nHAVE_BERKELEY_DB:=n\nHAVE_LATEX:=n\nHAVE_PDFLATEX:=n\nCFLAGS+=-DHAVE_SETNS -DHAVE_LIBMNL -I$(LIBMNL_PATH)/include\nLDLIBS+=-lmnl' > $(IPROUTE2_PATH)/config.mk
428 + printf 'lib: snapshot\n\t$$(MAKE) -C lib\nip/ip: lib\n\t$$(MAKE) -C ip ip\nmisc/ss: lib\n\t$$(MAKE) -C misc ss\n' >> $(IPROUTE2_PATH)/Makefile
429 + touch $@
430 +
431 +$(IPROUTE2_PATH)/ip/ip: | $(IPROUTE2_PATH)/.installed $(LIBMNL_PATH)/src/.libs/libmnl.a $(USERSPACE_DEPS)
432 + LDFLAGS="$(LDFLAGS) -L$(LIBMNL_PATH)/src/.libs" PKG_CONFIG_LIBDIR="$(LIBMNL_PATH)" $(MAKE) -C $(IPROUTE2_PATH) PREFIX=/ ip/ip
433 + $(STRIP) -s $(IPROUTE2_PATH)/ip/ip
434 +
435 +$(IPROUTE2_PATH)/misc/ss: | $(IPROUTE2_PATH)/.installed $(LIBMNL_PATH)/src/.libs/libmnl.a $(USERSPACE_DEPS)
436 + LDFLAGS="$(LDFLAGS) -L$(LIBMNL_PATH)/src/.libs" PKG_CONFIG_LIBDIR="$(LIBMNL_PATH)" $(MAKE) -C $(IPROUTE2_PATH) PREFIX=/ misc/ss
437 + $(STRIP) -s $(IPROUTE2_PATH)/misc/ss
438 +
439 +$(IPTABLES_PATH)/.installed: $(IPTABLES_TAR)
440 + mkdir -p $(BUILD_PATH)
441 + flock -s $<.lock tar -C $(BUILD_PATH) -xf $<
442 + sed -i -e "/nfnetlink=[01]/s:=[01]:=0:" -e "/nfconntrack=[01]/s:=[01]:=0:" $(IPTABLES_PATH)/configure
443 + touch $@
444 +
445 +$(IPTABLES_PATH)/iptables/xtables-multi: | $(IPTABLES_PATH)/.installed $(LIBMNL_PATH)/src/.libs/libmnl.a $(USERSPACE_DEPS)
446 + cd $(IPTABLES_PATH) && PKG_CONFIG_LIBDIR="$(LIBMNL_PATH)" ./configure --prefix=/ $(CROSS_COMPILE_FLAG) --enable-static --disable-shared --disable-nftables --disable-bpf-compiler --disable-nfsynproxy --disable-libipq --with-kernel=$(BUILD_PATH)/include
447 + $(MAKE) -C $(IPTABLES_PATH)
448 + $(STRIP) -s $@
449 +
450 +$(NMAP_PATH)/.installed: $(NMAP_TAR)
451 + mkdir -p $(BUILD_PATH)
452 + flock -s $<.lock tar -C $(BUILD_PATH) -xf $<
453 + touch $@
454 +
455 +$(NMAP_PATH)/ncat/ncat: | $(NMAP_PATH)/.installed $(USERSPACE_DEPS)
456 + cd $(NMAP_PATH) && ./configure --prefix=/ $(CROSS_COMPILE_FLAG) --enable-static --disable-shared --without-ndiff --without-zenmap --without-nping --with-libpcap=included --with-libpcre=included --with-libdnet=included --without-liblua --with-liblinear=included --without-nmap-update --without-openssl --with-pcap=linux
457 + $(MAKE) -C $(NMAP_PATH) build-ncat
458 + $(STRIP) -s $@
459 +
460 +clean:
461 + rm -rf $(BUILD_PATH)
462 +
463 +distclean: clean
464 + rm -rf $(DISTFILES_PATH)
465 +
466 +menuconfig: $(KERNEL_BUILD_PATH)/.config
467 + $(MAKE) -C $(KERNEL_PATH) O=$(KERNEL_BUILD_PATH) ARCH=$(KERNEL_ARCH) CROSS_COMPILE=$(CROSS_COMPILE) CC="$(NOPIE_GCC)" menuconfig
468 +
469 +.PHONY: qemu build clean distclean menuconfig
470 +.DELETE_ON_ERROR:
471 --- /dev/null
472 +++ b/tools/testing/selftests/wireguard/qemu/arch/aarch64.config
473 @@ -0,0 +1,5 @@
474 +CONFIG_SERIAL_AMBA_PL011=y
475 +CONFIG_SERIAL_AMBA_PL011_CONSOLE=y
476 +CONFIG_CMDLINE_BOOL=y
477 +CONFIG_CMDLINE="console=ttyAMA0 wg.success=ttyAMA1"
478 +CONFIG_FRAME_WARN=1280
479 --- /dev/null
480 +++ b/tools/testing/selftests/wireguard/qemu/arch/aarch64_be.config
481 @@ -0,0 +1,6 @@
482 +CONFIG_CPU_BIG_ENDIAN=y
483 +CONFIG_SERIAL_AMBA_PL011=y
484 +CONFIG_SERIAL_AMBA_PL011_CONSOLE=y
485 +CONFIG_CMDLINE_BOOL=y
486 +CONFIG_CMDLINE="console=ttyAMA0 wg.success=ttyAMA1"
487 +CONFIG_FRAME_WARN=1280
488 --- /dev/null
489 +++ b/tools/testing/selftests/wireguard/qemu/arch/arm.config
490 @@ -0,0 +1,9 @@
491 +CONFIG_MMU=y
492 +CONFIG_ARCH_MULTI_V7=y
493 +CONFIG_ARCH_VIRT=y
494 +CONFIG_THUMB2_KERNEL=n
495 +CONFIG_SERIAL_AMBA_PL011=y
496 +CONFIG_SERIAL_AMBA_PL011_CONSOLE=y
497 +CONFIG_CMDLINE_BOOL=y
498 +CONFIG_CMDLINE="console=ttyAMA0 wg.success=ttyAMA1"
499 +CONFIG_FRAME_WARN=1024
500 --- /dev/null
501 +++ b/tools/testing/selftests/wireguard/qemu/arch/armeb.config
502 @@ -0,0 +1,10 @@
503 +CONFIG_MMU=y
504 +CONFIG_ARCH_MULTI_V7=y
505 +CONFIG_ARCH_VIRT=y
506 +CONFIG_THUMB2_KERNEL=n
507 +CONFIG_SERIAL_AMBA_PL011=y
508 +CONFIG_SERIAL_AMBA_PL011_CONSOLE=y
509 +CONFIG_CMDLINE_BOOL=y
510 +CONFIG_CMDLINE="console=ttyAMA0 wg.success=ttyAMA1"
511 +CONFIG_CPU_BIG_ENDIAN=y
512 +CONFIG_FRAME_WARN=1024
513 --- /dev/null
514 +++ b/tools/testing/selftests/wireguard/qemu/arch/i686.config
515 @@ -0,0 +1,5 @@
516 +CONFIG_SERIAL_8250=y
517 +CONFIG_SERIAL_8250_CONSOLE=y
518 +CONFIG_CMDLINE_BOOL=y
519 +CONFIG_CMDLINE="console=ttyS0 wg.success=ttyS1"
520 +CONFIG_FRAME_WARN=1024
521 --- /dev/null
522 +++ b/tools/testing/selftests/wireguard/qemu/arch/m68k.config
523 @@ -0,0 +1,9 @@
524 +CONFIG_MMU=y
525 +CONFIG_M68040=y
526 +CONFIG_MAC=y
527 +CONFIG_SERIAL_PMACZILOG=y
528 +CONFIG_SERIAL_PMACZILOG_TTYS=y
529 +CONFIG_SERIAL_PMACZILOG_CONSOLE=y
530 +CONFIG_CMDLINE_BOOL=y
531 +CONFIG_CMDLINE="console=ttyS0 wg.success=ttyS1"
532 +CONFIG_FRAME_WARN=1024
533 --- /dev/null
534 +++ b/tools/testing/selftests/wireguard/qemu/arch/mips.config
535 @@ -0,0 +1,11 @@
536 +CONFIG_CPU_MIPS32_R2=y
537 +CONFIG_MIPS_MALTA=y
538 +CONFIG_MIPS_CPS=y
539 +CONFIG_MIPS_FP_SUPPORT=y
540 +CONFIG_POWER_RESET=y
541 +CONFIG_POWER_RESET_SYSCON=y
542 +CONFIG_SERIAL_8250=y
543 +CONFIG_SERIAL_8250_CONSOLE=y
544 +CONFIG_CMDLINE_BOOL=y
545 +CONFIG_CMDLINE="console=ttyS0 wg.success=ttyS1"
546 +CONFIG_FRAME_WARN=1024
547 --- /dev/null
548 +++ b/tools/testing/selftests/wireguard/qemu/arch/mips64.config
549 @@ -0,0 +1,14 @@
550 +CONFIG_64BIT=y
551 +CONFIG_CPU_MIPS64_R2=y
552 +CONFIG_MIPS32_N32=y
553 +CONFIG_CPU_HAS_MSA=y
554 +CONFIG_MIPS_MALTA=y
555 +CONFIG_MIPS_CPS=y
556 +CONFIG_MIPS_FP_SUPPORT=y
557 +CONFIG_POWER_RESET=y
558 +CONFIG_POWER_RESET_SYSCON=y
559 +CONFIG_SERIAL_8250=y
560 +CONFIG_SERIAL_8250_CONSOLE=y
561 +CONFIG_CMDLINE_BOOL=y
562 +CONFIG_CMDLINE="console=ttyS0 wg.success=ttyS1"
563 +CONFIG_FRAME_WARN=1280
564 --- /dev/null
565 +++ b/tools/testing/selftests/wireguard/qemu/arch/mips64el.config
566 @@ -0,0 +1,15 @@
567 +CONFIG_64BIT=y
568 +CONFIG_CPU_MIPS64_R2=y
569 +CONFIG_MIPS32_N32=y
570 +CONFIG_CPU_HAS_MSA=y
571 +CONFIG_MIPS_MALTA=y
572 +CONFIG_CPU_LITTLE_ENDIAN=y
573 +CONFIG_MIPS_CPS=y
574 +CONFIG_MIPS_FP_SUPPORT=y
575 +CONFIG_POWER_RESET=y
576 +CONFIG_POWER_RESET_SYSCON=y
577 +CONFIG_SERIAL_8250=y
578 +CONFIG_SERIAL_8250_CONSOLE=y
579 +CONFIG_CMDLINE_BOOL=y
580 +CONFIG_CMDLINE="console=ttyS0 wg.success=ttyS1"
581 +CONFIG_FRAME_WARN=1280
582 --- /dev/null
583 +++ b/tools/testing/selftests/wireguard/qemu/arch/mipsel.config
584 @@ -0,0 +1,12 @@
585 +CONFIG_CPU_MIPS32_R2=y
586 +CONFIG_MIPS_MALTA=y
587 +CONFIG_CPU_LITTLE_ENDIAN=y
588 +CONFIG_MIPS_CPS=y
589 +CONFIG_MIPS_FP_SUPPORT=y
590 +CONFIG_POWER_RESET=y
591 +CONFIG_POWER_RESET_SYSCON=y
592 +CONFIG_SERIAL_8250=y
593 +CONFIG_SERIAL_8250_CONSOLE=y
594 +CONFIG_CMDLINE_BOOL=y
595 +CONFIG_CMDLINE="console=ttyS0 wg.success=ttyS1"
596 +CONFIG_FRAME_WARN=1024
597 --- /dev/null
598 +++ b/tools/testing/selftests/wireguard/qemu/arch/powerpc.config
599 @@ -0,0 +1,10 @@
600 +CONFIG_PPC_QEMU_E500=y
601 +CONFIG_FSL_SOC_BOOKE=y
602 +CONFIG_PPC_85xx=y
603 +CONFIG_PHYS_64BIT=y
604 +CONFIG_SERIAL_8250=y
605 +CONFIG_SERIAL_8250_CONSOLE=y
606 +CONFIG_MATH_EMULATION=y
607 +CONFIG_CMDLINE_BOOL=y
608 +CONFIG_CMDLINE="console=ttyS0 wg.success=ttyS1"
609 +CONFIG_FRAME_WARN=1024
610 --- /dev/null
611 +++ b/tools/testing/selftests/wireguard/qemu/arch/powerpc64le.config
612 @@ -0,0 +1,12 @@
613 +CONFIG_PPC64=y
614 +CONFIG_PPC_PSERIES=y
615 +CONFIG_ALTIVEC=y
616 +CONFIG_VSX=y
617 +CONFIG_PPC_OF_BOOT_TRAMPOLINE=y
618 +CONFIG_PPC_RADIX_MMU=y
619 +CONFIG_HVC_CONSOLE=y
620 +CONFIG_CPU_LITTLE_ENDIAN=y
621 +CONFIG_CMDLINE_BOOL=y
622 +CONFIG_CMDLINE="console=hvc0 wg.success=hvc1"
623 +CONFIG_SECTION_MISMATCH_WARN_ONLY=y
624 +CONFIG_FRAME_WARN=1280
625 --- /dev/null
626 +++ b/tools/testing/selftests/wireguard/qemu/arch/x86_64.config
627 @@ -0,0 +1,5 @@
628 +CONFIG_SERIAL_8250=y
629 +CONFIG_SERIAL_8250_CONSOLE=y
630 +CONFIG_CMDLINE_BOOL=y
631 +CONFIG_CMDLINE="console=ttyS0 wg.success=ttyS1"
632 +CONFIG_FRAME_WARN=1280
633 --- /dev/null
634 +++ b/tools/testing/selftests/wireguard/qemu/debug.config
635 @@ -0,0 +1,67 @@
636 +CONFIG_LOCALVERSION="-debug"
637 +CONFIG_ENABLE_WARN_DEPRECATED=y
638 +CONFIG_ENABLE_MUST_CHECK=y
639 +CONFIG_FRAME_POINTER=y
640 +CONFIG_STACK_VALIDATION=y
641 +CONFIG_DEBUG_KERNEL=y
642 +CONFIG_DEBUG_INFO=y
643 +CONFIG_DEBUG_INFO_DWARF4=y
644 +CONFIG_PAGE_EXTENSION=y
645 +CONFIG_PAGE_POISONING=y
646 +CONFIG_DEBUG_OBJECTS=y
647 +CONFIG_DEBUG_OBJECTS_FREE=y
648 +CONFIG_DEBUG_OBJECTS_TIMERS=y
649 +CONFIG_DEBUG_OBJECTS_WORK=y
650 +CONFIG_DEBUG_OBJECTS_RCU_HEAD=y
651 +CONFIG_DEBUG_OBJECTS_PERCPU_COUNTER=y
652 +CONFIG_DEBUG_OBJECTS_ENABLE_DEFAULT=1
653 +CONFIG_SLUB_DEBUG_ON=y
654 +CONFIG_DEBUG_VM=y
655 +CONFIG_DEBUG_MEMORY_INIT=y
656 +CONFIG_HAVE_DEBUG_STACKOVERFLOW=y
657 +CONFIG_DEBUG_STACKOVERFLOW=y
658 +CONFIG_HAVE_ARCH_KMEMCHECK=y
659 +CONFIG_HAVE_ARCH_KASAN=y
660 +CONFIG_KASAN=y
661 +CONFIG_KASAN_INLINE=y
662 +CONFIG_UBSAN=y
663 +CONFIG_UBSAN_SANITIZE_ALL=y
664 +CONFIG_UBSAN_NO_ALIGNMENT=y
665 +CONFIG_UBSAN_NULL=y
666 +CONFIG_DEBUG_KMEMLEAK=y
667 +CONFIG_DEBUG_KMEMLEAK_EARLY_LOG_SIZE=8192
668 +CONFIG_DEBUG_STACK_USAGE=y
669 +CONFIG_DEBUG_SHIRQ=y
670 +CONFIG_WQ_WATCHDOG=y
671 +CONFIG_SCHED_DEBUG=y
672 +CONFIG_SCHED_INFO=y
673 +CONFIG_SCHEDSTATS=y
674 +CONFIG_SCHED_STACK_END_CHECK=y
675 +CONFIG_DEBUG_TIMEKEEPING=y
676 +CONFIG_TIMER_STATS=y
677 +CONFIG_DEBUG_PREEMPT=y
678 +CONFIG_DEBUG_RT_MUTEXES=y
679 +CONFIG_DEBUG_SPINLOCK=y
680 +CONFIG_DEBUG_MUTEXES=y
681 +CONFIG_DEBUG_LOCK_ALLOC=y
682 +CONFIG_PROVE_LOCKING=y
683 +CONFIG_LOCKDEP=y
684 +CONFIG_DEBUG_ATOMIC_SLEEP=y
685 +CONFIG_TRACE_IRQFLAGS=y
686 +CONFIG_DEBUG_BUGVERBOSE=y
687 +CONFIG_DEBUG_LIST=y
688 +CONFIG_DEBUG_PI_LIST=y
689 +CONFIG_PROVE_RCU=y
690 +CONFIG_SPARSE_RCU_POINTER=y
691 +CONFIG_RCU_CPU_STALL_TIMEOUT=21
692 +CONFIG_RCU_TRACE=y
693 +CONFIG_RCU_EQS_DEBUG=y
694 +CONFIG_USER_STACKTRACE_SUPPORT=y
695 +CONFIG_DEBUG_SG=y
696 +CONFIG_DEBUG_NOTIFIERS=y
697 +CONFIG_DOUBLEFAULT=y
698 +CONFIG_X86_DEBUG_FPU=y
699 +CONFIG_DEBUG_SECTION_MISMATCH=y
700 +CONFIG_DEBUG_PAGEALLOC=y
701 +CONFIG_DEBUG_PAGEALLOC_ENABLE_DEFAULT=y
702 +CONFIG_DEBUG_WW_MUTEX_SLOWPATH=y
703 --- /dev/null
704 +++ b/tools/testing/selftests/wireguard/qemu/init.c
705 @@ -0,0 +1,284 @@
706 +// SPDX-License-Identifier: GPL-2.0
707 +/*
708 + * Copyright (C) 2015-2019 Jason A. Donenfeld <Jason@zx2c4.com>. All Rights Reserved.
709 + */
710 +
711 +#define _GNU_SOURCE
712 +#include <unistd.h>
713 +#include <errno.h>
714 +#include <string.h>
715 +#include <stdio.h>
716 +#include <stdlib.h>
717 +#include <stdbool.h>
718 +#include <fcntl.h>
719 +#include <sys/wait.h>
720 +#include <sys/mount.h>
721 +#include <sys/types.h>
722 +#include <sys/stat.h>
723 +#include <sys/types.h>
724 +#include <sys/io.h>
725 +#include <sys/ioctl.h>
726 +#include <sys/reboot.h>
727 +#include <sys/utsname.h>
728 +#include <sys/sendfile.h>
729 +#include <linux/random.h>
730 +#include <linux/version.h>
731 +
732 +__attribute__((noreturn)) static void poweroff(void)
733 +{
734 + fflush(stdout);
735 + fflush(stderr);
736 + reboot(RB_AUTOBOOT);
737 + sleep(30);
738 + fprintf(stderr, "\x1b[37m\x1b[41m\x1b[1mFailed to power off!!!\x1b[0m\n");
739 + exit(1);
740 +}
741 +
742 +static void panic(const char *what)
743 +{
744 + fprintf(stderr, "\n\n\x1b[37m\x1b[41m\x1b[1mSOMETHING WENT HORRIBLY WRONG\x1b[0m\n\n \x1b[31m\x1b[1m%s: %s\x1b[0m\n\n\x1b[37m\x1b[44m\x1b[1mPower off...\x1b[0m\n\n", what, strerror(errno));
745 + poweroff();
746 +}
747 +
748 +#define pretty_message(msg) puts("\x1b[32m\x1b[1m" msg "\x1b[0m")
749 +
750 +static void print_banner(void)
751 +{
752 + struct utsname utsname;
753 + int len;
754 +
755 + if (uname(&utsname) < 0)
756 + panic("uname");
757 +
758 + len = strlen(" WireGuard Test Suite on ") + strlen(utsname.sysname) + strlen(utsname.release) + strlen(utsname.machine);
759 + printf("\x1b[45m\x1b[33m\x1b[1m%*.s\x1b[0m\n\x1b[45m\x1b[33m\x1b[1m WireGuard Test Suite on %s %s %s \x1b[0m\n\x1b[45m\x1b[33m\x1b[1m%*.s\x1b[0m\n\n", len, "", utsname.sysname, utsname.release, utsname.machine, len, "");
760 +}
761 +
762 +static void seed_rng(void)
763 +{
764 + int fd;
765 + struct {
766 + int entropy_count;
767 + int buffer_size;
768 + unsigned char buffer[256];
769 + } entropy = {
770 + .entropy_count = sizeof(entropy.buffer) * 8,
771 + .buffer_size = sizeof(entropy.buffer),
772 + .buffer = "Adding real entropy is not actually important for these tests. Don't try this at home, kids!"
773 + };
774 +
775 + if (mknod("/dev/urandom", S_IFCHR | 0644, makedev(1, 9)))
776 + panic("mknod(/dev/urandom)");
777 + fd = open("/dev/urandom", O_WRONLY);
778 + if (fd < 0)
779 + panic("open(urandom)");
780 + for (int i = 0; i < 256; ++i) {
781 + if (ioctl(fd, RNDADDENTROPY, &entropy) < 0)
782 + panic("ioctl(urandom)");
783 + }
784 + close(fd);
785 +}
786 +
787 +static void mount_filesystems(void)
788 +{
789 + pretty_message("[+] Mounting filesystems...");
790 + mkdir("/dev", 0755);
791 + mkdir("/proc", 0755);
792 + mkdir("/sys", 0755);
793 + mkdir("/tmp", 0755);
794 + mkdir("/run", 0755);
795 + mkdir("/var", 0755);
796 + if (mount("none", "/dev", "devtmpfs", 0, NULL))
797 + panic("devtmpfs mount");
798 + if (mount("none", "/proc", "proc", 0, NULL))
799 + panic("procfs mount");
800 + if (mount("none", "/sys", "sysfs", 0, NULL))
801 + panic("sysfs mount");
802 + if (mount("none", "/tmp", "tmpfs", 0, NULL))
803 + panic("tmpfs mount");
804 + if (mount("none", "/run", "tmpfs", 0, NULL))
805 + panic("tmpfs mount");
806 + if (mount("none", "/sys/kernel/debug", "debugfs", 0, NULL))
807 + ; /* Not a problem if it fails.*/
808 + if (symlink("/run", "/var/run"))
809 + panic("run symlink");
810 + if (symlink("/proc/self/fd", "/dev/fd"))
811 + panic("fd symlink");
812 +}
813 +
814 +static void enable_logging(void)
815 +{
816 + int fd;
817 + pretty_message("[+] Enabling logging...");
818 + fd = open("/proc/sys/kernel/printk", O_WRONLY);
819 + if (fd >= 0) {
820 + if (write(fd, "9\n", 2) != 2)
821 + panic("write(printk)");
822 + close(fd);
823 + }
824 + fd = open("/proc/sys/debug/exception-trace", O_WRONLY);
825 + if (fd >= 0) {
826 + if (write(fd, "1\n", 2) != 2)
827 + panic("write(exception-trace)");
828 + close(fd);
829 + }
830 + fd = open("/proc/sys/kernel/panic_on_warn", O_WRONLY);
831 + if (fd >= 0) {
832 + if (write(fd, "1\n", 2) != 2)
833 + panic("write(panic_on_warn)");
834 + close(fd);
835 + }
836 +}
837 +
838 +static void kmod_selftests(void)
839 +{
840 + FILE *file;
841 + char line[2048], *start, *pass;
842 + bool success = true;
843 + pretty_message("[+] Module self-tests:");
844 + file = fopen("/proc/kmsg", "r");
845 + if (!file)
846 + panic("fopen(kmsg)");
847 + if (fcntl(fileno(file), F_SETFL, O_NONBLOCK) < 0)
848 + panic("fcntl(kmsg, nonblock)");
849 + while (fgets(line, sizeof(line), file)) {
850 + start = strstr(line, "wireguard: ");
851 + if (!start)
852 + continue;
853 + start += 11;
854 + *strchrnul(start, '\n') = '\0';
855 + if (strstr(start, "www.wireguard.com"))
856 + break;
857 + pass = strstr(start, ": pass");
858 + if (!pass || pass[6] != '\0') {
859 + success = false;
860 + printf(" \x1b[31m* %s\x1b[0m\n", start);
861 + } else
862 + printf(" \x1b[32m* %s\x1b[0m\n", start);
863 + }
864 + fclose(file);
865 + if (!success) {
866 + puts("\x1b[31m\x1b[1m[-] Tests failed! \u2639\x1b[0m");
867 + poweroff();
868 + }
869 +}
870 +
871 +static void launch_tests(void)
872 +{
873 + char cmdline[4096], *success_dev;
874 + int status, fd;
875 + pid_t pid;
876 +
877 + pretty_message("[+] Launching tests...");
878 + pid = fork();
879 + if (pid == -1)
880 + panic("fork");
881 + else if (pid == 0) {
882 + execl("/init.sh", "init", NULL);
883 + panic("exec");
884 + }
885 + if (waitpid(pid, &status, 0) < 0)
886 + panic("waitpid");
887 + if (WIFEXITED(status) && WEXITSTATUS(status) == 0) {
888 + pretty_message("[+] Tests successful! :-)");
889 + fd = open("/proc/cmdline", O_RDONLY);
890 + if (fd < 0)
891 + panic("open(/proc/cmdline)");
892 + if (read(fd, cmdline, sizeof(cmdline) - 1) <= 0)
893 + panic("read(/proc/cmdline)");
894 + cmdline[sizeof(cmdline) - 1] = '\0';
895 + for (success_dev = strtok(cmdline, " \n"); success_dev; success_dev = strtok(NULL, " \n")) {
896 + if (strncmp(success_dev, "wg.success=", 11))
897 + continue;
898 + memcpy(success_dev + 11 - 5, "/dev/", 5);
899 + success_dev += 11 - 5;
900 + break;
901 + }
902 + if (!success_dev || !strlen(success_dev))
903 + panic("Unable to find success device");
904 +
905 + fd = open(success_dev, O_WRONLY);
906 + if (fd < 0)
907 + panic("open(success_dev)");
908 + if (write(fd, "success\n", 8) != 8)
909 + panic("write(success_dev)");
910 + close(fd);
911 + } else {
912 + const char *why = "unknown cause";
913 + int what = -1;
914 +
915 + if (WIFEXITED(status)) {
916 + why = "exit code";
917 + what = WEXITSTATUS(status);
918 + } else if (WIFSIGNALED(status)) {
919 + why = "signal";
920 + what = WTERMSIG(status);
921 + }
922 + printf("\x1b[31m\x1b[1m[-] Tests failed with %s %d! \u2639\x1b[0m\n", why, what);
923 + }
924 +}
925 +
926 +static void ensure_console(void)
927 +{
928 + for (unsigned int i = 0; i < 1000; ++i) {
929 + int fd = open("/dev/console", O_RDWR);
930 + if (fd < 0) {
931 + usleep(50000);
932 + continue;
933 + }
934 + dup2(fd, 0);
935 + dup2(fd, 1);
936 + dup2(fd, 2);
937 + close(fd);
938 + if (write(1, "\0\0\0\0\n", 5) == 5)
939 + return;
940 + }
941 + panic("Unable to open console device");
942 +}
943 +
944 +static void clear_leaks(void)
945 +{
946 + int fd;
947 +
948 + fd = open("/sys/kernel/debug/kmemleak", O_WRONLY);
949 + if (fd < 0)
950 + return;
951 + pretty_message("[+] Starting memory leak detection...");
952 + write(fd, "clear\n", 5);
953 + close(fd);
954 +}
955 +
956 +static void check_leaks(void)
957 +{
958 + int fd;
959 +
960 + fd = open("/sys/kernel/debug/kmemleak", O_WRONLY);
961 + if (fd < 0)
962 + return;
963 + pretty_message("[+] Scanning for memory leaks...");
964 + sleep(2); /* Wait for any grace periods. */
965 + write(fd, "scan\n", 5);
966 + close(fd);
967 +
968 + fd = open("/sys/kernel/debug/kmemleak", O_RDONLY);
969 + if (fd < 0)
970 + return;
971 + if (sendfile(1, fd, NULL, 0x7ffff000) > 0)
972 + panic("Memory leaks encountered");
973 + close(fd);
974 +}
975 +
976 +int main(int argc, char *argv[])
977 +{
978 + seed_rng();
979 + ensure_console();
980 + print_banner();
981 + mount_filesystems();
982 + kmod_selftests();
983 + enable_logging();
984 + clear_leaks();
985 + launch_tests();
986 + check_leaks();
987 + poweroff();
988 + return 1;
989 +}
990 --- /dev/null
991 +++ b/tools/testing/selftests/wireguard/qemu/kernel.config
992 @@ -0,0 +1,86 @@
993 +CONFIG_LOCALVERSION=""
994 +CONFIG_NET=y
995 +CONFIG_NETDEVICES=y
996 +CONFIG_NET_CORE=y
997 +CONFIG_NET_IPIP=y
998 +CONFIG_DUMMY=y
999 +CONFIG_VETH=y
1000 +CONFIG_MULTIUSER=y
1001 +CONFIG_NAMESPACES=y
1002 +CONFIG_NET_NS=y
1003 +CONFIG_UNIX=y
1004 +CONFIG_INET=y
1005 +CONFIG_IPV6=y
1006 +CONFIG_NETFILTER=y
1007 +CONFIG_NETFILTER_ADVANCED=y
1008 +CONFIG_NF_CONNTRACK=y
1009 +CONFIG_NF_NAT=y
1010 +CONFIG_NETFILTER_XTABLES=y
1011 +CONFIG_NETFILTER_XT_NAT=y
1012 +CONFIG_NETFILTER_XT_MATCH_LENGTH=y
1013 +CONFIG_NF_CONNTRACK_IPV4=y
1014 +CONFIG_NF_NAT_IPV4=y
1015 +CONFIG_IP_NF_IPTABLES=y
1016 +CONFIG_IP_NF_FILTER=y
1017 +CONFIG_IP_NF_NAT=y
1018 +CONFIG_IP_ADVANCED_ROUTER=y
1019 +CONFIG_IP_MULTIPLE_TABLES=y
1020 +CONFIG_IPV6_MULTIPLE_TABLES=y
1021 +CONFIG_TTY=y
1022 +CONFIG_BINFMT_ELF=y
1023 +CONFIG_BINFMT_SCRIPT=y
1024 +CONFIG_VDSO=y
1025 +CONFIG_VIRTUALIZATION=y
1026 +CONFIG_HYPERVISOR_GUEST=y
1027 +CONFIG_PARAVIRT=y
1028 +CONFIG_KVM_GUEST=y
1029 +CONFIG_PARAVIRT_SPINLOCKS=y
1030 +CONFIG_PRINTK=y
1031 +CONFIG_KALLSYMS=y
1032 +CONFIG_BUG=y
1033 +CONFIG_CC_OPTIMIZE_FOR_PERFORMANCE=y
1034 +CONFIG_EMBEDDED=n
1035 +CONFIG_BASE_FULL=y
1036 +CONFIG_FUTEX=y
1037 +CONFIG_SHMEM=y
1038 +CONFIG_SLUB=y
1039 +CONFIG_SPARSEMEM_VMEMMAP=y
1040 +CONFIG_SMP=y
1041 +CONFIG_SCHED_SMT=y
1042 +CONFIG_SCHED_MC=y
1043 +CONFIG_NUMA=y
1044 +CONFIG_PREEMPT=y
1045 +CONFIG_NO_HZ=y
1046 +CONFIG_NO_HZ_IDLE=y
1047 +CONFIG_NO_HZ_FULL=n
1048 +CONFIG_HZ_PERIODIC=n
1049 +CONFIG_HIGH_RES_TIMERS=y
1050 +CONFIG_ARCH_RANDOM=y
1051 +CONFIG_FILE_LOCKING=y
1052 +CONFIG_POSIX_TIMERS=y
1053 +CONFIG_DEVTMPFS=y
1054 +CONFIG_PROC_FS=y
1055 +CONFIG_PROC_SYSCTL=y
1056 +CONFIG_SYSFS=y
1057 +CONFIG_TMPFS=y
1058 +CONFIG_CONSOLE_LOGLEVEL_DEFAULT=15
1059 +CONFIG_PRINTK_TIME=y
1060 +CONFIG_BLK_DEV_INITRD=y
1061 +CONFIG_LEGACY_VSYSCALL_NONE=y
1062 +CONFIG_KERNEL_GZIP=y
1063 +CONFIG_PANIC_ON_OOPS=y
1064 +CONFIG_BUG_ON_DATA_CORRUPTION=y
1065 +CONFIG_LOCKUP_DETECTOR=y
1066 +CONFIG_SOFTLOCKUP_DETECTOR=y
1067 +CONFIG_HARDLOCKUP_DETECTOR=y
1068 +CONFIG_WQ_WATCHDOG=y
1069 +CONFIG_DETECT_HUNG_TASK=y
1070 +CONFIG_BOOTPARAM_HARDLOCKUP_PANIC=y
1071 +CONFIG_BOOTPARAM_SOFTLOCKUP_PANIC=y
1072 +CONFIG_BOOTPARAM_HUNG_TASK_PANIC=y
1073 +CONFIG_PANIC_TIMEOUT=-1
1074 +CONFIG_STACKTRACE=y
1075 +CONFIG_EARLY_PRINTK=y
1076 +CONFIG_GDB_SCRIPTS=y
1077 +CONFIG_WIREGUARD=y
1078 +CONFIG_WIREGUARD_DEBUG=y