xref: /spdk/test/fuzz/llvm/common.sh (revision 501cec428e672413920e588baef8f13e3e54da18)
1#!/usr/bin/env bash
2#  SPDX-License-Identifier: BSD-3-Clause
3#  Copyright (C) 2022 Intel Corporation
4#  All rights reserved.
5#
6
7# Store pids of parallel fuzzer tests
8pids=()
9
10function cleanup() {
11	rm -rf "$@"
12	# Make sure that there is no process left hanging
13	kill -9 "${pids[@]}" || :
14}
15
16function fuzzer_out_handler() {
17	if [[ -n $SEND_LLVM_FUZZER_TO_SYSLOG ]]; then
18		logger -p user.debug -t LLVM
19	elif [[ -n $COMPRESS_LLVM_FUZZER ]]; then
20		gzip -c > "$1.gz"
21	else
22		cat > "$1"
23	fi
24}
25
26function get_testn() {
27	local fuzz_num=$1
28	local mem_size=$2
29	local nproc
30	nproc=$(nproc)
31	# Choose lower value, best case scenario is one test per core
32	local testn=$((fuzz_num < nproc ? fuzz_num : nproc))
33
34	export HUGEMEM=$((mem_size * testn))
35	setup
36	TESTN=$testn
37}
38
39function start_llvm_fuzz_all() {
40	local testn=$1    # Number of test to run in parallel
41	local fuzz_num=$2 # Number of fuzzer tests
42	local time=$3     # Time available for all fuzzers
43	local testn_idx idx
44	local core
45	local pid
46
47	# Calculate time for a single test and multiply it by number
48	# of test execute in parallel and round it up to 1 sek per test
49	timen=$(printf %d $(((time / fuzz_num) * testn)))
50	timen=$((timen == 0 ? 1 : timen))
51
52	for ((i = 0; i < fuzz_num; i += testn)); do
53		idx=-1 pids=()
54		# Run max up to $testn tests in parallel ...
55		while ((testn_idx = i + ++idx, testn_idx < fuzz_num && idx < testn)); do
56			core=$(printf "0x%x" $((0x1 << idx)))
57			start_llvm_fuzz $testn_idx $timen $core &> >(fuzzer_out_handler "$output_dir/llvm/llvm_${FUZZER}_${testn_idx}.txt") &
58			pids+=($!)
59		done
60		# ... and now wait for them
61		for pid in "${pids[@]}"; do
62			wait "$pid"
63		done
64		# queue up another $testn bundle at next iteration
65	done
66}
67
68function start_llvm_fuzz_short() {
69	local fuzz_num=$1
70	local time=$2
71
72	for ((i = 0; i < fuzz_num; i++)); do
73		start_llvm_fuzz $i $time 0x1
74	done
75}
76