From 757d26edba7d7de8c564a87a262a0b1321ddf804 Mon Sep 17 00:00:00 2001 From: Aleksandr Nogikh Date: Wed, 14 Jun 2023 14:58:25 +0200 Subject: all: support swap feature on Linux If the feature is supported on the device, allocate a 128MB swap file after VM boot and activate it. --- pkg/csource/generated.go | 60 ++++++++++++++++++++++++++++++++++++++++-------- 1 file changed, 51 insertions(+), 9 deletions(-) (limited to 'pkg/csource/generated.go') diff --git a/pkg/csource/generated.go b/pkg/csource/generated.go index 8c80cc363..bceb2dba4 100644 --- a/pkg/csource/generated.go +++ b/pkg/csource/generated.go @@ -3767,6 +3767,18 @@ static void initialize_wifi_devices(void) } #endif +#if SYZ_EXECUTOR || (SYZ_NET_DEVICES && SYZ_NIC_VF) || SYZ_SWAP +static int runcmdline(char* cmdline) +{ + debug("%s\n", cmdline); + int ret = system(cmdline); + if (ret) { + debug("FAIL: %s\n", cmdline); + } + return ret; +} +#endif + #if SYZ_EXECUTOR || SYZ_NET_DEVICES #include #include @@ -4028,15 +4040,6 @@ error: } #if SYZ_EXECUTOR || SYZ_NIC_VF -static int runcmdline(char* cmdline) -{ - debug("%s\n", cmdline); - int ret = system(cmdline); - if (ret) { - debug("FAIL: %s\n", cmdline); - } - return ret; -} static void netlink_nicvf_setup(void) { @@ -11762,6 +11765,42 @@ static long syz_pkey_set(volatile long pkey, volatile long val) } #endif +#if SYZ_EXECUTOR || SYZ_SWAP +#include +#include +#include +#include +#include +#include + +#define SWAP_FILE "./swap-file" +#define SWAP_FILE_SIZE (128 * 1000 * 1000) + +static void setup_swap() +{ + swapoff(SWAP_FILE); + unlink(SWAP_FILE); + int fd = open(SWAP_FILE, O_CREAT | O_WRONLY | O_CLOEXEC, 0600); + if (fd == -1) { + failmsg("swap file open failed", "file: %s", SWAP_FILE); + return; + } + fallocate(fd, FALLOC_FL_ZERO_RANGE, 0, SWAP_FILE_SIZE); + close(fd); + char cmdline[64]; + sprintf(cmdline, "mkswap %s", SWAP_FILE); + if (runcmdline(cmdline)) { + fail("mkswap failed"); + return; + } + if (swapon(SWAP_FILE, SWAP_FLAG_PREFER) == 1) { + failmsg("swapon failed", "file: %s", SWAP_FILE); + return; + } +} + +#endif + #elif GOOS_test #include @@ -12645,6 +12684,9 @@ int main(void) #if SYZ_802154 setup_802154(); #endif +#if SYZ_SWAP + setup_swap(); +#endif #if SYZ_HANDLE_SEGV install_segv_handler(); #endif -- cgit mrf-deployment