#!/bin/bash -eu
set -o nounset
set -o pipefail
set -o errexit
set -x

go clean --modcache
go mod tidy
go mod vendor

# This one has a fuzzer that breaks
#rm /root/go/pkg/mod/github.com/cilium/ebpf@v0.7.0/internal/btf/fuzz.go

rm -r $SRC/runc/vendor
go get github.com/AdaLogics/go-fuzz-headers

export RUNC_PATH=github.com/opencontainers/runc
export RUNC_FUZZERS=/src/cncf-fuzzing/projects/runc


mv $RUNC_FUZZERS/libcontainer_utils_fuzzer.go $SRC/runc/libcontainer/utils/
compile_go_fuzzer $RUNC_PATH/libcontainer/utils FuzzstripRoot fuzz_strip_root

mv $RUNC_FUZZERS/fs2_fuzzer.go $SRC/runc/libcontainer/cgroups/fs2/
compile_go_fuzzer $RUNC_PATH/libcontainer/cgroups/fs2 FuzzGetStats get_stats_fuzzer
compile_go_fuzzer $RUNC_PATH/libcontainer/cgroups/fs2 FuzzCgroupReader cgroup_reader_fuzzer

mv $RUNC_FUZZERS/specconv_fuzzer.go $SRC/runc/libcontainer/specconv/
compile_go_fuzzer $RUNC_PATH/libcontainer/specconv Fuzz specconv_fuzzer

mv $RUNC_FUZZERS/devices_fuzzer.go $SRC/runc/libcontainer/cgroups/devices
compile_go_fuzzer $RUNC_PATH/libcontainer/cgroups/devices Fuzz devices_fuzzer

mv $RUNC_FUZZERS/fscommon_fuzzer.go $SRC/runc/libcontainer/cgroups/fscommon/
compile_go_fuzzer $RUNC_PATH/libcontainer/cgroups/fscommon FuzzSecurejoin securejoin_fuzzer

mv $RUNC_FUZZERS/intelrdt_fuzzer.go $SRC/runc/libcontainer/intelrdt/
mv $SRC/runc/libcontainer/intelrdt/util_test.go $SRC/runc/libcontainer/intelrdt/util_test_fuzz.go
compile_go_fuzzer $RUNC_PATH/libcontainer/intelrdt FuzzSetCacheScema set_cache_schema_fuzzer
compile_go_fuzzer $RUNC_PATH/libcontainer/intelrdt FuzzParseMonFeatures parse_mon_features_fuzzer

mv $RUNC_FUZZERS/libcontainer_fuzzer.go $SRC/runc/libcontainer/
mv $SRC/runc/libcontainer/container_linux_test.go \
   $SRC/runc/libcontainer/container_linux_test_fuzz.go
CFLAGS=-O1 make -B runc-dmz || echo -n >runc-dmz
compile_go_fuzzer $RUNC_PATH/libcontainer FuzzStateApi state_api_fuzzer
