xref: /spdk/test/fuzz/llvm/common.sh (revision 956f5e5be9b5933c3fb22832bae5a91d48f59d9c)
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 get_testn() {
17	local fuzz_num=$1
18	local mem_size=$2
19	local nproc
20	nproc=$(nproc)
21	# Choose lower value, best case scenario is one test per core
22	local testn=$((fuzz_num < nproc ? fuzz_num : nproc))
23
24	export HUGEMEM=$((mem_size * testn))
25	setup
26	TESTN=$testn
27}
28
29function start_llvm_fuzz_all() {
30	local testn=$1    # Number of test to run in parallel
31	local fuzz_num=$2 # Number of fuzzer tests
32	local time=$3     # Time available for all fuzzers
33	local core
34
35	# Calculate time for a single test and multiply it by number
36	# of test execute in parallel and round it up to 1 sek per test
37	timen=$(printf %d $(((time / fuzz_num) * testn)))
38	timen=$((timen == 0 ? 1 : timen))
39
40	for ((i = 0; i < fuzz_num; i++)); do
41		core=$(printf "0x%x" $((0x1 << (i % testn))))
42		start_llvm_fuzz $i $timen $core &> $output_dir/llvm/llvm_"$FUZZER"_$i.txt &
43		pids+=($!)
44
45		# Wait for processes to finish
46		if (((i + 1) % testn == 0 || fuzz_num - i - 1 == 0)); then
47			(
48				sleep $((timen * 10 + 100))
49				echo "Timeout $time"
50				exit 1
51			) &
52			timeout_pid=$!
53			for pid in "${pids[@]}"; do
54				wait $pid
55			done
56			kill $timeout_pid || :
57			pids=()
58		fi
59	done
60}
61
62function start_llvm_fuzz_short() {
63	local fuzz_num=$1
64	local time=$2
65
66	export HUGEMEM=5120
67	setup
68
69	for ((i = 0; i < fuzz_num; i++)); do
70		start_llvm_fuzz $i $time 0x1
71	done
72}
73