diff options
| author | Aleksandr Nogikh <nogikh@google.com> | 2023-06-14 14:58:25 +0200 |
|---|---|---|
| committer | Aleksandr Nogikh <wp32pw@gmail.com> | 2023-06-15 11:49:04 +0200 |
| commit | 757d26edba7d7de8c564a87a262a0b1321ddf804 (patch) | |
| tree | f78b5e053c0a61ef2771e130af410ee7ee1d40da /executor/common_linux.h | |
| parent | 90d4044eae0123561d6cf2f667a4bc3357d93e7a (diff) | |
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.
Diffstat (limited to 'executor/common_linux.h')
| -rw-r--r-- | executor/common_linux.h | 61 |
1 files changed, 52 insertions, 9 deletions
diff --git a/executor/common_linux.h b/executor/common_linux.h index 621f0b2d8..d5152e058 100644 --- a/executor/common_linux.h +++ b/executor/common_linux.h @@ -1153,6 +1153,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 <arpa/inet.h> #include <errno.h> @@ -1428,15 +1440,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) { @@ -5574,3 +5577,43 @@ static long syz_pkey_set(volatile long pkey, volatile long val) return 0; } #endif + +#if SYZ_EXECUTOR || SYZ_SWAP +#include <fcntl.h> +#include <stdio.h> +#include <string.h> +#include <sys/stat.h> +#include <sys/swap.h> +#include <sys/types.h> + +#define SWAP_FILE "./swap-file" +#define SWAP_FILE_SIZE (128 * 1000 * 1000) // 128 MB. + +static void setup_swap() +{ + // The call must be idempotent, so first disable swap and remove the swap file. + swapoff(SWAP_FILE); + unlink(SWAP_FILE); + // Zero-fill the 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; + } + // We cannot do ftruncate -- swapon complains about this. Do fallocate instead. + fallocate(fd, FALLOC_FL_ZERO_RANGE, 0, SWAP_FILE_SIZE); + close(fd); + // Set up the swap file. + 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 |
