// https://syzkaller.appspot.com/bug?id=546ac3e48fbcc053a2875763e188a29f8f3966ea
// autogenerated by syzkaller (https://github.com/google/syzkaller)

#define _GNU_SOURCE

#include <dirent.h>
#include <endian.h>
#include <errno.h>
#include <fcntl.h>
#include <pthread.h>
#include <setjmp.h>
#include <signal.h>
#include <stdarg.h>
#include <stdbool.h>
#include <stddef.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/ioctl.h>
#include <sys/mman.h>
#include <sys/mount.h>
#include <sys/prctl.h>
#include <sys/stat.h>
#include <sys/syscall.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <time.h>
#include <unistd.h>

#include <linux/futex.h>
#include <linux/loop.h>

#ifndef __NR_add_key
#define __NR_add_key 217
#endif
#ifndef __NR_getpid
#define __NR_getpid 172
#endif
#ifndef __NR_keyctl
#define __NR_keyctl 219
#endif
#ifndef __NR_mbind
#define __NR_mbind 235
#endif
#ifndef __NR_memfd_create
#define __NR_memfd_create 279
#endif
#ifndef __NR_mmap
#define __NR_mmap 222
#endif
#ifndef __NR_openat
#define __NR_openat 56
#endif
#ifndef __NR_sendmmsg
#define __NR_sendmmsg 269
#endif
#ifndef __NR_socket
#define __NR_socket 198
#endif
#ifndef __NR_sync
#define __NR_sync 81
#endif
#ifndef __NR_write
#define __NR_write 64
#endif

static unsigned long long procid;

static __thread int clone_ongoing;
static __thread int skip_segv;
static __thread jmp_buf segv_env;

static void segv_handler(int sig, siginfo_t* info, void* ctx)
{
  if (__atomic_load_n(&clone_ongoing, __ATOMIC_RELAXED) != 0) {
    exit(sig);
  }
  uintptr_t addr = (uintptr_t)info->si_addr;
  const uintptr_t prog_start = 1 << 20;
  const uintptr_t prog_end = 100 << 20;
  int skip = __atomic_load_n(&skip_segv, __ATOMIC_RELAXED) != 0;
  int valid = addr < prog_start || addr > prog_end;
  if (skip && valid) {
    _longjmp(segv_env, 1);
  }
  exit(sig);
}

static void install_segv_handler(void)
{
  struct sigaction sa;
  memset(&sa, 0, sizeof(sa));
  sa.sa_handler = SIG_IGN;
  syscall(SYS_rt_sigaction, 0x20, &sa, NULL, 8);
  syscall(SYS_rt_sigaction, 0x21, &sa, NULL, 8);
  memset(&sa, 0, sizeof(sa));
  sa.sa_sigaction = segv_handler;
  sa.sa_flags = SA_NODEFER | SA_SIGINFO;
  sigaction(SIGSEGV, &sa, NULL);
  sigaction(SIGBUS, &sa, NULL);
}

#define NONFAILING(...)                                                        \
  ({                                                                           \
    int ok = 1;                                                                \
    __atomic_fetch_add(&skip_segv, 1, __ATOMIC_SEQ_CST);                       \
    if (_setjmp(segv_env) == 0) {                                              \
      __VA_ARGS__;                                                             \
    } else                                                                     \
      ok = 0;                                                                  \
    __atomic_fetch_sub(&skip_segv, 1, __ATOMIC_SEQ_CST);                       \
    ok;                                                                        \
  })

static void sleep_ms(uint64_t ms)
{
  usleep(ms * 1000);
}

static uint64_t current_time_ms(void)
{
  struct timespec ts;
  if (clock_gettime(CLOCK_MONOTONIC, &ts))
    exit(1);
  return (uint64_t)ts.tv_sec * 1000 + (uint64_t)ts.tv_nsec / 1000000;
}

static void use_temporary_dir(void)
{
  char tmpdir_template[] = "./syzkaller.XXXXXX";
  char* tmpdir = mkdtemp(tmpdir_template);
  if (!tmpdir)
    exit(1);
  if (chmod(tmpdir, 0777))
    exit(1);
  if (chdir(tmpdir))
    exit(1);
}

static void thread_start(void* (*fn)(void*), void* arg)
{
  pthread_t th;
  pthread_attr_t attr;
  pthread_attr_init(&attr);
  pthread_attr_setstacksize(&attr, 128 << 10);
  int i = 0;
  for (; i < 100; i++) {
    if (pthread_create(&th, &attr, fn, arg) == 0) {
      pthread_attr_destroy(&attr);
      return;
    }
    if (errno == EAGAIN) {
      usleep(50);
      continue;
    }
    break;
  }
  exit(1);
}

typedef struct {
  int state;
} event_t;

static void event_init(event_t* ev)
{
  ev->state = 0;
}

static void event_reset(event_t* ev)
{
  ev->state = 0;
}

static void event_set(event_t* ev)
{
  if (ev->state)
    exit(1);
  __atomic_store_n(&ev->state, 1, __ATOMIC_RELEASE);
  syscall(SYS_futex, &ev->state, FUTEX_WAKE | FUTEX_PRIVATE_FLAG, 1000000);
}

static void event_wait(event_t* ev)
{
  while (!__atomic_load_n(&ev->state, __ATOMIC_ACQUIRE))
    syscall(SYS_futex, &ev->state, FUTEX_WAIT | FUTEX_PRIVATE_FLAG, 0, 0);
}

static int event_isset(event_t* ev)
{
  return __atomic_load_n(&ev->state, __ATOMIC_ACQUIRE);
}

static int event_timedwait(event_t* ev, uint64_t timeout)
{
  uint64_t start = current_time_ms();
  uint64_t now = start;
  for (;;) {
    uint64_t remain = timeout - (now - start);
    struct timespec ts;
    ts.tv_sec = remain / 1000;
    ts.tv_nsec = (remain % 1000) * 1000 * 1000;
    syscall(SYS_futex, &ev->state, FUTEX_WAIT | FUTEX_PRIVATE_FLAG, 0, &ts);
    if (__atomic_load_n(&ev->state, __ATOMIC_ACQUIRE))
      return 1;
    now = current_time_ms();
    if (now - start > timeout)
      return 0;
  }
}

static bool write_file(const char* file, const char* what, ...)
{
  char buf[1024];
  va_list args;
  va_start(args, what);
  vsnprintf(buf, sizeof(buf), what, args);
  va_end(args);
  buf[sizeof(buf) - 1] = 0;
  int len = strlen(buf);
  int fd = open(file, O_WRONLY | O_CLOEXEC);
  if (fd == -1)
    return false;
  if (write(fd, buf, len) != len) {
    int err = errno;
    close(fd);
    errno = err;
    return false;
  }
  close(fd);
  return true;
}

static long syz_open_procfs(volatile long a0, volatile long a1)
{
  char buf[128];
  memset(buf, 0, sizeof(buf));
  if (a0 == 0) {
    snprintf(buf, sizeof(buf), "/proc/self/%s", (char*)a1);
  } else if (a0 == -1) {
    snprintf(buf, sizeof(buf), "/proc/thread-self/%s", (char*)a1);
  } else {
    snprintf(buf, sizeof(buf), "/proc/self/task/%d/%s", (int)a0, (char*)a1);
  }
  int fd = open(buf, O_RDWR);
  if (fd == -1)
    fd = open(buf, O_RDONLY);
  return fd;
}

//% This code is derived from puff.{c,h}, found in the zlib development. The
//% original files come with the following copyright notice:

//% Copyright (C) 2002-2013 Mark Adler, all rights reserved
//% version 2.3, 21 Jan 2013
//% This software is provided 'as-is', without any express or implied
//% warranty.  In no event will the author be held liable for any damages
//% arising from the use of this software.
//% Permission is granted to anyone to use this software for any purpose,
//% including commercial applications, and to alter it and redistribute it
//% freely, subject to the following restrictions:
//% 1. The origin of this software must not be misrepresented; you must not
//%    claim that you wrote the original software. If you use this software
//%    in a product, an acknowledgment in the product documentation would be
//%    appreciated but is not required.
//% 2. Altered source versions must be plainly marked as such, and must not be
//%    misrepresented as being the original software.
//% 3. This notice may not be removed or altered from any source distribution.
//% Mark Adler    madler@alumni.caltech.edu

//% BEGIN CODE DERIVED FROM puff.{c,h}

#define MAXBITS 15
#define MAXLCODES 286
#define MAXDCODES 30
#define MAXCODES (MAXLCODES + MAXDCODES)
#define FIXLCODES 288

struct puff_state {
  unsigned char* out;
  unsigned long outlen;
  unsigned long outcnt;
  const unsigned char* in;
  unsigned long inlen;
  unsigned long incnt;
  int bitbuf;
  int bitcnt;
  jmp_buf env;
};
static int puff_bits(struct puff_state* s, int need)
{
  long val = s->bitbuf;
  while (s->bitcnt < need) {
    if (s->incnt == s->inlen)
      longjmp(s->env, 1);
    val |= (long)(s->in[s->incnt++]) << s->bitcnt;
    s->bitcnt += 8;
  }
  s->bitbuf = (int)(val >> need);
  s->bitcnt -= need;
  return (int)(val & ((1L << need) - 1));
}
static int puff_stored(struct puff_state* s)
{
  s->bitbuf = 0;
  s->bitcnt = 0;
  if (s->incnt + 4 > s->inlen)
    return 2;
  unsigned len = s->in[s->incnt++];
  len |= s->in[s->incnt++] << 8;
  if (s->in[s->incnt++] != (~len & 0xff) ||
      s->in[s->incnt++] != ((~len >> 8) & 0xff))
    return -2;
  if (s->incnt + len > s->inlen)
    return 2;
  if (s->outcnt + len > s->outlen)
    return 1;
  for (; len--; s->outcnt++, s->incnt++) {
    if (s->in[s->incnt])
      s->out[s->outcnt] = s->in[s->incnt];
  }
  return 0;
}
struct puff_huffman {
  short* count;
  short* symbol;
};
static int puff_decode(struct puff_state* s, const struct puff_huffman* h)
{
  int first = 0;
  int index = 0;
  int bitbuf = s->bitbuf;
  int left = s->bitcnt;
  int code = first = index = 0;
  int len = 1;
  short* next = h->count + 1;
  while (1) {
    while (left--) {
      code |= bitbuf & 1;
      bitbuf >>= 1;
      int count = *next++;
      if (code - count < first) {
        s->bitbuf = bitbuf;
        s->bitcnt = (s->bitcnt - len) & 7;
        return h->symbol[index + (code - first)];
      }
      index += count;
      first += count;
      first <<= 1;
      code <<= 1;
      len++;
    }
    left = (MAXBITS + 1) - len;
    if (left == 0)
      break;
    if (s->incnt == s->inlen)
      longjmp(s->env, 1);
    bitbuf = s->in[s->incnt++];
    if (left > 8)
      left = 8;
  }
  return -10;
}
static int puff_construct(struct puff_huffman* h, const short* length, int n)
{
  int len;
  for (len = 0; len <= MAXBITS; len++)
    h->count[len] = 0;
  int symbol;
  for (symbol = 0; symbol < n; symbol++)
    (h->count[length[symbol]])++;
  if (h->count[0] == n)
    return 0;
  int left = 1;
  for (len = 1; len <= MAXBITS; len++) {
    left <<= 1;
    left -= h->count[len];
    if (left < 0)
      return left;
  }
  short offs[MAXBITS + 1];
  offs[1] = 0;
  for (len = 1; len < MAXBITS; len++)
    offs[len + 1] = offs[len] + h->count[len];
  for (symbol = 0; symbol < n; symbol++)
    if (length[symbol] != 0)
      h->symbol[offs[length[symbol]]++] = symbol;
  return left;
}
static int puff_codes(struct puff_state* s, const struct puff_huffman* lencode,
                      const struct puff_huffman* distcode)
{
  static const short lens[29] = {3,  4,  5,  6,   7,   8,   9,   10,  11, 13,
                                 15, 17, 19, 23,  27,  31,  35,  43,  51, 59,
                                 67, 83, 99, 115, 131, 163, 195, 227, 258};
  static const short lext[29] = {0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 2, 2, 2,
                                 2, 3, 3, 3, 3, 4, 4, 4, 4, 5, 5, 5, 5, 0};
  static const short dists[30] = {
      1,    2,    3,    4,    5,    7,    9,    13,    17,    25,
      33,   49,   65,   97,   129,  193,  257,  385,   513,   769,
      1025, 1537, 2049, 3073, 4097, 6145, 8193, 12289, 16385, 24577};
  static const short dext[30] = {0, 0, 0,  0,  1,  1,  2,  2,  3,  3,
                                 4, 4, 5,  5,  6,  6,  7,  7,  8,  8,
                                 9, 9, 10, 10, 11, 11, 12, 12, 13, 13};
  int symbol;
  do {
    symbol = puff_decode(s, lencode);
    if (symbol < 0)
      return symbol;
    if (symbol < 256) {
      if (s->outcnt == s->outlen)
        return 1;
      if (symbol)
        s->out[s->outcnt] = symbol;
      s->outcnt++;
    } else if (symbol > 256) {
      symbol -= 257;
      if (symbol >= 29)
        return -10;
      int len = lens[symbol] + puff_bits(s, lext[symbol]);
      symbol = puff_decode(s, distcode);
      if (symbol < 0)
        return symbol;
      unsigned dist = dists[symbol] + puff_bits(s, dext[symbol]);
      if (dist > s->outcnt)
        return -11;
      if (s->outcnt + len > s->outlen)
        return 1;
      while (len--) {
        if (dist <= s->outcnt && s->out[s->outcnt - dist])
          s->out[s->outcnt] = s->out[s->outcnt - dist];
        s->outcnt++;
      }
    }
  } while (symbol != 256);
  return 0;
}
static int puff_fixed(struct puff_state* s)
{
  static int virgin = 1;
  static short lencnt[MAXBITS + 1], lensym[FIXLCODES];
  static short distcnt[MAXBITS + 1], distsym[MAXDCODES];
  static struct puff_huffman lencode, distcode;
  if (virgin) {
    lencode.count = lencnt;
    lencode.symbol = lensym;
    distcode.count = distcnt;
    distcode.symbol = distsym;
    short lengths[FIXLCODES];
    int symbol;
    for (symbol = 0; symbol < 144; symbol++)
      lengths[symbol] = 8;
    for (; symbol < 256; symbol++)
      lengths[symbol] = 9;
    for (; symbol < 280; symbol++)
      lengths[symbol] = 7;
    for (; symbol < FIXLCODES; symbol++)
      lengths[symbol] = 8;
    puff_construct(&lencode, lengths, FIXLCODES);
    for (symbol = 0; symbol < MAXDCODES; symbol++)
      lengths[symbol] = 5;
    puff_construct(&distcode, lengths, MAXDCODES);
    virgin = 0;
  }
  return puff_codes(s, &lencode, &distcode);
}
static int puff_dynamic(struct puff_state* s)
{
  static const short order[19] = {16, 17, 18, 0, 8,  7, 9,  6, 10, 5,
                                  11, 4,  12, 3, 13, 2, 14, 1, 15};
  int nlen = puff_bits(s, 5) + 257;
  int ndist = puff_bits(s, 5) + 1;
  int ncode = puff_bits(s, 4) + 4;
  if (nlen > MAXLCODES || ndist > MAXDCODES)
    return -3;
  short lengths[MAXCODES];
  int index;
  for (index = 0; index < ncode; index++)
    lengths[order[index]] = puff_bits(s, 3);
  for (; index < 19; index++)
    lengths[order[index]] = 0;
  short lencnt[MAXBITS + 1], lensym[MAXLCODES];
  struct puff_huffman lencode = {lencnt, lensym};
  int err = puff_construct(&lencode, lengths, 19);
  if (err != 0)
    return -4;
  index = 0;
  while (index < nlen + ndist) {
    int symbol;
    int len;
    symbol = puff_decode(s, &lencode);
    if (symbol < 0)
      return symbol;
    if (symbol < 16)
      lengths[index++] = symbol;
    else {
      len = 0;
      if (symbol == 16) {
        if (index == 0)
          return -5;
        len = lengths[index - 1];
        symbol = 3 + puff_bits(s, 2);
      } else if (symbol == 17)
        symbol = 3 + puff_bits(s, 3);
      else
        symbol = 11 + puff_bits(s, 7);
      if (index + symbol > nlen + ndist)
        return -6;
      while (symbol--)
        lengths[index++] = len;
    }
  }
  if (lengths[256] == 0)
    return -9;
  err = puff_construct(&lencode, lengths, nlen);
  if (err && (err < 0 || nlen != lencode.count[0] + lencode.count[1]))
    return -7;
  short distcnt[MAXBITS + 1], distsym[MAXDCODES];
  struct puff_huffman distcode = {distcnt, distsym};
  err = puff_construct(&distcode, lengths + nlen, ndist);
  if (err && (err < 0 || ndist != distcode.count[0] + distcode.count[1]))
    return -8;
  return puff_codes(s, &lencode, &distcode);
}
static int puff(unsigned char* dest, unsigned long* destlen,
                const unsigned char* source, unsigned long sourcelen)
{
  struct puff_state s = {
      .out = dest,
      .outlen = *destlen,
      .outcnt = 0,
      .in = source,
      .inlen = sourcelen,
      .incnt = 0,
      .bitbuf = 0,
      .bitcnt = 0,
  };
  int err;
  if (setjmp(s.env) != 0)
    err = 2;
  else {
    int last;
    do {
      last = puff_bits(&s, 1);
      int type = puff_bits(&s, 2);
      err = type == 0 ? puff_stored(&s)
                      : (type == 1 ? puff_fixed(&s)
                                   : (type == 2 ? puff_dynamic(&s) : -1));
      if (err != 0)
        break;
    } while (!last);
  }
  *destlen = s.outcnt;
  return err;
}

//% END CODE DERIVED FROM puff.{c,h}

#define ZLIB_HEADER_WIDTH 2

static int puff_zlib_to_file(const unsigned char* source,
                             unsigned long sourcelen, int dest_fd)
{
  if (sourcelen < ZLIB_HEADER_WIDTH)
    return 0;
  source += ZLIB_HEADER_WIDTH;
  sourcelen -= ZLIB_HEADER_WIDTH;
  const unsigned long max_destlen = 132 << 20;
  void* ret = mmap(0, max_destlen, PROT_WRITE | PROT_READ,
                   MAP_PRIVATE | MAP_ANON, -1, 0);
  if (ret == MAP_FAILED)
    return -1;
  unsigned char* dest = (unsigned char*)ret;
  unsigned long destlen = max_destlen;
  int err = puff(dest, &destlen, source, sourcelen);
  if (err) {
    munmap(dest, max_destlen);
    errno = -err;
    return -1;
  }
  if (write(dest_fd, dest, destlen) != (ssize_t)destlen) {
    munmap(dest, max_destlen);
    return -1;
  }
  return munmap(dest, max_destlen);
}

static int setup_loop_device(unsigned char* data, unsigned long size,
                             const char* loopname, int* loopfd_p)
{
  int err = 0, loopfd = -1;
  int memfd = syscall(__NR_memfd_create, "syzkaller", 0);
  if (memfd == -1) {
    err = errno;
    goto error;
  }
  if (puff_zlib_to_file(data, size, memfd)) {
    err = errno;
    goto error_close_memfd;
  }
  loopfd = open(loopname, O_RDWR);
  if (loopfd == -1) {
    err = errno;
    goto error_close_memfd;
  }
  if (ioctl(loopfd, LOOP_SET_FD, memfd)) {
    if (errno != EBUSY) {
      err = errno;
      goto error_close_loop;
    }
    ioctl(loopfd, LOOP_CLR_FD, 0);
    usleep(1000);
    if (ioctl(loopfd, LOOP_SET_FD, memfd)) {
      err = errno;
      goto error_close_loop;
    }
  }
  close(memfd);
  *loopfd_p = loopfd;
  return 0;

error_close_loop:
  close(loopfd);
error_close_memfd:
  close(memfd);
error:
  errno = err;
  return -1;
}

static void reset_loop_device(const char* loopname)
{
  int loopfd = open(loopname, O_RDWR);
  if (loopfd == -1) {
    return;
  }
  if (ioctl(loopfd, LOOP_CLR_FD, 0)) {
  }
  close(loopfd);
}

static long syz_mount_image(volatile long fsarg, volatile long dir,
                            volatile long flags, volatile long optsarg,
                            volatile long change_dir,
                            volatile unsigned long size, volatile long image)
{
  unsigned char* data = (unsigned char*)image;
  int res = -1, err = 0, need_loop_device = !!size;
  char* mount_opts = (char*)optsarg;
  char* target = (char*)dir;
  char* fs = (char*)fsarg;
  char* source = NULL;
  char loopname[64];
  if (need_loop_device) {
    int loopfd;
    memset(loopname, 0, sizeof(loopname));
    snprintf(loopname, sizeof(loopname), "/dev/loop%llu", procid);
    if (setup_loop_device(data, size, loopname, &loopfd) == -1)
      return -1;
    close(loopfd);
    source = loopname;
  }
  mkdir(target, 0777);
  char opts[256];
  memset(opts, 0, sizeof(opts));
  if (strlen(mount_opts) > (sizeof(opts) - 32)) {
  }
  strncpy(opts, mount_opts, sizeof(opts) - 32);
  if (strcmp(fs, "iso9660") == 0) {
    flags |= MS_RDONLY;
  } else if (strncmp(fs, "ext", 3) == 0) {
    bool has_remount_ro = false;
    char* remount_ro_start = strstr(opts, "errors=remount-ro");
    if (remount_ro_start != NULL) {
      char after = *(remount_ro_start + strlen("errors=remount-ro"));
      char before = remount_ro_start == opts ? '\0' : *(remount_ro_start - 1);
      has_remount_ro = ((before == '\0' || before == ',') &&
                        (after == '\0' || after == ','));
    }
    if (strstr(opts, "errors=panic") || !has_remount_ro)
      strcat(opts, ",errors=continue");
  } else if (strcmp(fs, "xfs") == 0) {
    strcat(opts, ",nouuid");
  }
  res = mount(source, target, fs, flags, opts);
  if (res == -1) {
    err = errno;
    goto error_clear_loop;
  }
  res = open(target, O_RDONLY | O_DIRECTORY);
  if (res == -1) {
    err = errno;
    goto error_clear_loop;
  }
  if (change_dir) {
    res = chdir(target);
    if (res == -1) {
      err = errno;
    }
  }

error_clear_loop:
  if (need_loop_device)
    reset_loop_device(loopname);
  errno = err;
  return res;
}

#define FS_IOC_SETFLAGS _IOW('f', 2, long)
static void remove_dir(const char* dir)
{
  int iter = 0;
  DIR* dp = 0;
retry:
  while (umount2(dir, MNT_DETACH | UMOUNT_NOFOLLOW) == 0) {
  }
  dp = opendir(dir);
  if (dp == NULL) {
    if (errno == EMFILE) {
      exit(1);
    }
    exit(1);
  }
  struct dirent* ep = 0;
  while ((ep = readdir(dp))) {
    if (strcmp(ep->d_name, ".") == 0 || strcmp(ep->d_name, "..") == 0)
      continue;
    char filename[FILENAME_MAX];
    snprintf(filename, sizeof(filename), "%s/%s", dir, ep->d_name);
    while (umount2(filename, MNT_DETACH | UMOUNT_NOFOLLOW) == 0) {
    }
    struct stat st;
    if (lstat(filename, &st))
      exit(1);
    if (S_ISDIR(st.st_mode)) {
      remove_dir(filename);
      continue;
    }
    int i;
    for (i = 0;; i++) {
      if (unlink(filename) == 0)
        break;
      if (errno == EPERM) {
        int fd = open(filename, O_RDONLY);
        if (fd != -1) {
          long flags = 0;
          if (ioctl(fd, FS_IOC_SETFLAGS, &flags) == 0) {
          }
          close(fd);
          continue;
        }
      }
      if (errno == EROFS) {
        break;
      }
      if (errno != EBUSY || i > 100)
        exit(1);
      if (umount2(filename, MNT_DETACH | UMOUNT_NOFOLLOW))
        exit(1);
    }
  }
  closedir(dp);
  for (int i = 0;; i++) {
    if (rmdir(dir) == 0)
      break;
    if (i < 100) {
      if (errno == EPERM) {
        int fd = open(dir, O_RDONLY);
        if (fd != -1) {
          long flags = 0;
          if (ioctl(fd, FS_IOC_SETFLAGS, &flags) == 0) {
          }
          close(fd);
          continue;
        }
      }
      if (errno == EROFS) {
        break;
      }
      if (errno == EBUSY) {
        if (umount2(dir, MNT_DETACH | UMOUNT_NOFOLLOW))
          exit(1);
        continue;
      }
      if (errno == ENOTEMPTY) {
        if (iter < 100) {
          iter++;
          goto retry;
        }
      }
    }
    exit(1);
  }
}

static void kill_and_wait(int pid, int* status)
{
  kill(-pid, SIGKILL);
  kill(pid, SIGKILL);
  for (int i = 0; i < 100; i++) {
    if (waitpid(-1, status, WNOHANG | __WALL) == pid)
      return;
    usleep(1000);
  }
  DIR* dir = opendir("/sys/fs/fuse/connections");
  if (dir) {
    for (;;) {
      struct dirent* ent = readdir(dir);
      if (!ent)
        break;
      if (strcmp(ent->d_name, ".") == 0 || strcmp(ent->d_name, "..") == 0)
        continue;
      char abort[300];
      snprintf(abort, sizeof(abort), "/sys/fs/fuse/connections/%s/abort",
               ent->d_name);
      int fd = open(abort, O_WRONLY);
      if (fd == -1) {
        continue;
      }
      if (write(fd, abort, 1) < 0) {
      }
      close(fd);
    }
    closedir(dir);
  } else {
  }
  while (waitpid(-1, status, __WALL) != pid) {
  }
}

static void reset_loop()
{
  char buf[64];
  snprintf(buf, sizeof(buf), "/dev/loop%llu", procid);
  int loopfd = open(buf, O_RDWR);
  if (loopfd != -1) {
    ioctl(loopfd, LOOP_CLR_FD, 0);
    close(loopfd);
  }
}

static void setup_test()
{
  prctl(PR_SET_PDEATHSIG, SIGKILL, 0, 0, 0);
  setpgrp();
  write_file("/proc/self/oom_score_adj", "1000");
  if (symlink("/dev/binderfs", "./binderfs")) {
  }
}

struct thread_t {
  int created, call;
  event_t ready, done;
};

static struct thread_t threads[16];
static void execute_call(int call);
static int running;

static void* thr(void* arg)
{
  struct thread_t* th = (struct thread_t*)arg;
  for (;;) {
    event_wait(&th->ready);
    event_reset(&th->ready);
    execute_call(th->call);
    __atomic_fetch_sub(&running, 1, __ATOMIC_RELAXED);
    event_set(&th->done);
  }
  return 0;
}

static void execute_one(void)
{
  int i, call, thread;
  for (call = 0; call < 16; call++) {
    for (thread = 0; thread < (int)(sizeof(threads) / sizeof(threads[0]));
         thread++) {
      struct thread_t* th = &threads[thread];
      if (!th->created) {
        th->created = 1;
        event_init(&th->ready);
        event_init(&th->done);
        event_set(&th->done);
        thread_start(thr, th);
      }
      if (!event_isset(&th->done))
        continue;
      event_reset(&th->done);
      th->call = call;
      __atomic_fetch_add(&running, 1, __ATOMIC_RELAXED);
      event_set(&th->ready);
      event_timedwait(&th->done,
                      50 + (call == 0 ? 4000 : 0) + (call == 10 ? 4000 : 0));
      break;
    }
  }
  for (i = 0; i < 100 && __atomic_load_n(&running, __ATOMIC_RELAXED); i++)
    sleep_ms(1);
}

static void execute_one(void);

#define WAIT_FLAGS __WALL

static void loop(void)
{
  int iter = 0;
  for (;; iter++) {
    char cwdbuf[32];
    sprintf(cwdbuf, "./%d", iter);
    if (mkdir(cwdbuf, 0777))
      exit(1);
    reset_loop();
    int pid = fork();
    if (pid < 0)
      exit(1);
    if (pid == 0) {
      if (chdir(cwdbuf))
        exit(1);
      setup_test();
      execute_one();
      exit(0);
    }
    int status = 0;
    uint64_t start = current_time_ms();
    for (;;) {
      if (waitpid(-1, &status, WNOHANG | WAIT_FLAGS) == pid)
        break;
      sleep_ms(1);
      if (current_time_ms() - start < 5000)
        continue;
      kill_and_wait(pid, &status);
      break;
    }
    remove_dir(cwdbuf);
  }
}

uint64_t r[2] = {0x0, 0xffffffffffffffff};

void execute_call(int call)
{
  intptr_t res = 0;
  switch (call) {
  case 0:
    NONFAILING(memcpy((void*)0x20000000, "ext2\000", 5));
    NONFAILING(memcpy((void*)0x200002c0, "./file0\000", 8));
    NONFAILING(syz_mount_image(/*fs=*/0x20000000, /*dir=*/0x200002c0,
                               /*flags=*/0x3041010, /*opts=*/0, /*chdir=*/0,
                               /*size=*/0, /*img=*/0x20000000));
    break;
  case 1:
    syscall(__NR_add_key, /*type=*/0ul, /*desc=*/0ul, /*payload=*/0ul,
            /*paylen=*/0ul, /*keyring=*/0xfffffffe);
    break;
  case 2:
    res = syscall(__NR_add_key, /*type=*/0ul, /*desc=*/0ul, /*payload=*/0ul,
                  /*paylen=*/0ul, /*keyring=*/0xfffffffc);
    if (res != -1)
      r[0] = res;
    break;
  case 3:
    syscall(__NR_keyctl, /*code=*/4ul, /*key=*/r[0], /*uid=*/0, /*gid=*/0, 0);
    break;
  case 4:
    syscall(__NR_add_key, /*type=*/0ul, /*desc=*/0ul, /*payload=*/0ul,
            /*paylen=*/0ul, /*keyring=*/r[0]);
    break;
  case 5:
    syscall(__NR_sync);
    break;
  case 6:
    syscall(__NR_socket, /*domain=*/0x1eul, /*type=*/3ul, /*proto=*/9);
    break;
  case 7:
    NONFAILING(syz_open_procfs(/*pid=*/0, /*file=*/0));
    break;
  case 8:
    syscall(__NR_getpid);
    break;
  case 9:
    syscall(__NR_add_key, /*type=*/0ul, /*desc=*/0ul, /*payload=*/0ul,
            /*paylen=*/0ul, /*keyring=*/r[0]);
    break;
  case 10:
    NONFAILING(memcpy((void*)0x20000ec0, "nilfs2\000", 7));
    NONFAILING(memcpy((void*)0x20000f00, "./file0\000", 8));
    NONFAILING(memcpy(
        (void*)0x20000040,
        "\x00\xd7\x7f\xbe\x9e\x57\xa6\x34\xf9\x29\xf7\x4b\xee\x0c\x10\xf9\xce"
        "\xcf\xc3\xea\xd3\xb7\x3d\xdb\x73\x12\xd0\xad\x8b\xa2\xe7\x4f\x25\x7f"
        "\x47\xc6\x84\xe0\xae\xc5\xcb\x30\x09\xa5\x02\x8b\xd4\xf7\x0e\xef\x4b"
        "\x27\x4a\x5c\x38\xfe\xc4\x07\x9e\x4d\x43\xb5\x98\xa9\x69\xb3\x32\x6c"
        "\x22\xea\xe3\x05\xe5\x7a\xe7\x86\x34\x73\x45\xf1\xde\x88\x5f\xa6\x95"
        "\x78\x58\xa8\xb0\x37\x78\x54\xb8\x19\x06\x07\x30\x3a\xbd\x6a\xee\x33"
        "\x0a\x8c\xaa\xba\x17\x97\xb7\xa1\x5b\x53\xd6\xd0\xae\x9a\xc3\x3e\x8d"
        "\x02\x6e\xb2\x77\xae\xad\x8d\x55\xf8\x75\x23\x36\x43\x69\x52\x2d\x5f"
        "\x49\xef\xa9\x53\x67\xa9\x9d\x3a\xda\xe7\xb7\xb4\xb6\x7d\x9d\xd0\x57"
        "\xe8\xca\xd2\xc3\x04\x9e\x41\xe7\xd9\xff\xa8\x0d\xd7\xa5\xaf\x9b\xb0"
        "\x21\xff\x24\x74\x35\x6b\xb2\xc9\x75\xe3\xe8\xf8\x7f\x10\x64\xa9\x83"
        "\xb0\x87\xf2\x2b\x7c\x94\x05\x3f\xbb\x53\xa4\x74\x53\x6f\xe5\x9f\x83"
        "\xbf\xc1\x84\x3b\xb4\x44\x91\x60\xb0\xc4\x84\x2d\xac\xee\xf3\x1a\xc9"
        "\xbd\x2f\x5b\xf7\x0f\x69",
        227));
    NONFAILING(memcpy(
        (void*)0x20001e40,
        "\x78\x9c\xec\xdd\x4f\x6c\x1c\x57\x19\x00\xf0\x37\xeb\x7f\x49\xec\xc6"
        "\xeb\xb6\x50\xb7\xa5\x89\x69\x29\x69\x03\x38\xc1\xc9\xa1\xdc\x52\x29"
        "\x02\xa9\xaa\xaa\x5e\xb8\xb7\x4a\xe3\x36\xc2\x2d\x11\x29\x87\x56\x8d"
        "\xe2\x72\x0a\x12\x87\xa2\xaa\x97\x22\x0e\x45\xed\x0d\x29\x1c\x90\x68"
        "\x85\x84\x2a\x24\x24\xfe\xf4\xc0\x99\x53\x05\x17\x10\x0a\x52\xa4\x5c"
        "\x88\x14\x2f\xb2\xf3\xde\x7a\xfd\x9c\x61\xd7\x63\x7b\xd6\xeb\xfd\xfd"
        "\xa4\x6f\xdf\xce\xbc\xd9\xf9\xbe\xf1\x46\xce\xbc\xf1\xec\xdb\x00\x0c"
        "\xad\xc6\xda\xe3\xe9\xd3\xb3\x45\x08\xef\x7d\xf2\xee\xd9\xef\x5c\x59"
        "\xf9\xcd\xea\xba\xa3\xed\x2d\xe6\xd6\x1e\x8b\xb8\xd4\x0c\x21\x8c\x75"
        "\x2c\x17\xd9\xfe\x3e\x8f\x2b\x6e\xdf\x78\xeb\xdc\x6a\xbb\x92\xb5\x45"
        "\x58\x58\x7b\x4c\xfd\xe1\xb9\xeb\xed\xd7\x4e\x86\x10\x96\xc3\x5c\xf8"
        "\x34\x34\xc3\xd1\xe3\xcd\x9b\x57\x47\x9e\x5d\xfc\xe8\xfd\xcf\x8e\x5d"
        "\xbe\xf8\xcc\x2b\xbb\x74\xf8\x00\x00\x30\x54\xae\xfd\x79\xe9\x6f\x4f"
        "\xfe\xf3\x4f\x5f\x9f\xb9\x75\xed\xc8\x99\x30\xd1\x5e\x9f\xce\xcf\x9b"
        "\x71\x79\x32\x9e\xf7\x9f\x8c\xe7\xf7\xe9\xbc\xbf\x11\x36\x2e\x17\x1d"
        "\xd1\x69\x3c\xdb\x6e\x24\x46\x23\xdb\x6e\x24\xdb\x6e\x34\xcb\x33\x5a"
        "\x92\x6f\x2c\xdb\xcf\x58\xc9\x76\xe3\x5d\xf2\x8d\x74\xac\xbb\xdb\x71"
        "\x02\x00\x00\xc0\x20\x6a\xc4\x31\x6e\x33\x14\x8d\xf9\x8e\x71\x6e\x33"
        "\x34\x1a\xf3\xf3\x77\xc6\xfd\xab\x3e\x9f\x1e\x2f\xe6\x5f\xbb\xb0\xb4"
        "\x78\xa9\x8f\xc5\x02\x00\x00\x00\x95\xdc\xbc\xb2\x76\xd3\xad\x10\x42"
        "\x08\x21\xc4\x10\xc7\xad\x56\xab\xd5\xef\x1a\x84\x10\x42\x88\xdd\x8d"
        "\xd6\x74\xbf\xaf\x40\x00\x00\x00\x00\xc3\x26\xcd\x3b\xd0\x9e\x1f\x2c"
        "\xb7\x9c\xcf\x2c\xb0\x3d\xed\xbd\x35\x7b\xcb\x7f\xfd\xe9\xc6\xdd\x5f"
        "\x0f\x3b\xa0\xee\x7f\xff\xf2\x0f\x56\xfe\x0f\xdf\xf6\x1b\x07\x00\x80"
        "\xea\xf6\xeb\xd9\x64\x3a\xae\x74\x1e\x9d\xe6\x31\xc8\xe7\x11\x1c\xc9"
        "\x5e\xb7\xd5\xf3\xff\x46\xb6\x9f\xd1\x2d\xd6\x59\x36\xaf\xe0\xa0\xcc"
        "\x37\x58\x56\x67\xfe\x73\xdd\xab\xca\xea\xdf\xea\xfb\xd8\x2f\x65\xf5"
        "\xe7\xf3\x61\xee\x55\x65\xf5\xe7\xf3\x74\xee\x55\x65\xf5\x4f\xd4\x5c"
        "\x47\x55\x65\xf5\x1f\xa8\xb9\x8e\xaa\xca\xea\x3f\x58\x73\x1d\x55\x95"
        "\xd5\x7f\xa8\xe6\x3a\xaa\x2a\xab\x7f\xb2\xe6\x3a\xaa\x2a\xab\x7f\xaa"
        "\xe6\x3a\xaa\x2a\xab\xff\x9e\x9a\xeb\xa8\xaa\xac\xfe\xc3\x35\xd7\x51"
        "\x55\x59\xfd\x83\x72\x5b\x6d\x59\xfd\xcd\x9a\xeb\xa8\xaa\xac\xfe\x99"
        "\x9a\xeb\xa8\xaa\xac\xfe\x7b\x6b\xae\xa3\xaa\xb2\xfa\xef\xab\xb9\x8e"
        "\xaa\xca\xea\xbf\xbf\xe6\x3a\xfa\xe5\x91\xd8\xa6\x9f\xc3\x91\xac\xbf"
        "\x73\xfc\x9c\x8f\xe9\x06\x65\x8c\x07\x00\x00\x00\xc3\xee\xbf\xe6\xff"
        "\x1b\x9a\x58\xfb\x9b\xf0\x1e\xa8\x43\x08\x21\x76\x3d\x56\xcd\xed\x81"
        "\x3a\x84\x10\x42\x08\x21\xf6\x50\x5c\xe9\xef\xe5\x07\x00\x00\x00\x60"
        "\x0f\x48\x9f\x0b\x48\x9f\x7a\x6f\x45\xa9\x7f\xa4\x4b\xff\x68\x97\xfe"
        "\xb1\x2e\xfd\xe3\x5d\xfa\x27\xba\xf4\x03\x00\x00\x00\x21\xfc\xf6\xea"
        "\xe2\x83\xef\x14\xeb\x9f\xf3\xdf\xee\x7c\x78\x69\xde\xa8\x34\xff\xd2"
        "\x56\xe7\x31\xca\xe7\x23\xdc\x6a\xfe\xed\xce\x7b\xb6\xdd\xfc\x83\x32"
        "\x6f\x19\x00\x00\x00\xc3\xa5\xf8\xf6\xa7\x2b\xc7\xcf\x7e\xf0\xfa\xcc"
        "\xad\x6b\x47\xce\x74\x8c\x7e\x57\xe2\x78\x37\xcd\x03\x3a\x1a\xaf\x0d"
        "\x7c\x1c\x97\xd3\x7d\x01\x53\xd9\x72\x91\xc6\xd0\x67\x36\xe6\x69\x94"
        "\x6c\x97\x5f\x1f\xb8\xa7\x6c\x7f\xcf\x6f\xf3\x40\x01\x00\x00\x60\x88"
        "\xa5\xf1\x7b\x33\x14\x8d\xf9\x8e\x71\x77\x33\x34\x1a\xf3\xf3\xeb\xe3"
        "\xf1\xd9\x30\x56\x2c\x5e\x58\x3a\x7f\x32\x2e\xa7\xef\x67\xf9\xe3\xf4"
        "\xd8\xc4\xea\xfa\x6f\xd6\x5c\x37\x00\x00\x00\xd0\xbb\xf5\xf1\xfe\xdd"
        "\xc7\xff\xe9\x7b\x7c\x67\xc3\x78\x31\xff\xda\x85\xa5\xc5\x4b\x77\x96"
        "\xa7\xda\xeb\xc7\x1a\x9d\xd7\x05\xa6\xd7\xd7\x17\x9d\xd7\x05\x9a\xd9"
        "\xfa\x85\x92\xf5\xa7\xe2\x72\xfa\xfe\xce\x57\xa6\x0f\xae\xad\x9f\x3f"
        "\xf7\xfd\xa5\x97\x76\xfa\xe0\x01\x00\x00\x60\x48\x5c\x7a\xe3\xcd\xef"
        "\xbd\xb8\xb4\x74\xfe\x07\x9e\x78\xe2\x89\x27\xed\x27\xfd\xfe\xcd\x04"
        "\x00\x00\xec\xb4\x8f\xfe\xf1\xee\x5f\x7e\x78\x6a\xea\x77\x77\x3e\xff"
        "\xbf\x3e\xff\x5d\xfa\xfc\xff\x5c\x5c\x6e\xc6\xb9\xfd\xfe\x1a\x37\x48"
        "\xf7\x09\xa4\xcf\x01\x6c\xfa\xbc\xfe\x0b\x1b\xf3\x4c\x97\x6d\x77\x71"
        "\xe3\x76\xcd\x6c\xbb\x91\x18\x13\x59\xdd\x07\x3a\xf6\x13\x3a\xe6\x1b"
        "\x4c\xaf\x9b\x29\xcb\xd7\xdc\xb8\x9f\xf1\x92\x7c\x93\x59\xbe\xa9\x2c"
        "\x5f\x3e\x4f\xc1\x68\xb6\x7d\xca\x77\x38\x5b\x9f\xcf\x4f\x98\xb6\x9b"
        "\xce\xd6\xe7\xf3\x30\x8e\x66\x39\x8a\x2c\xff\xa3\x01\x00\x00\x00\xca"
        "\x9d\x78\xfd\xd5\x8b\x27\x2e\xbd\xf1\xe6\x37\x2e\xbc\xfa\xe2\xcb\xe7"
        "\x5f\x3e\xff\xda\xa9\x93\x0b\xdf\x5a\x78\x6a\x61\xe1\xf4\xc2\x89\xb5"
        "\xfb\xfa\x4f\x74\xde\xdd\x0f\x00\x00\x00\x0c\xa2\xf5\x9b\x7e\xfb\x5d"
        "\x09\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x0c"
        "\xaf\x3a\xbe\x4e\xac\xdf\xc7\x08\x00\x00\x00\xc3\xee\x3f\x57\x42\x08"
        "\xcb\x42\x08\x21\x84\x10\x42\x08\x21\xc4\xe0\xc5\xda\x77\xc5\xef\x81"
        "\x3a\x06\x21\x5a\xad\xfc\x9b\xe6\x01\x00\x00\x00\x76\xd7\xed\x1b\x6f"
        "\x9d\xeb\x6c\x37\x59\x2e\x76\x34\x5f\x7b\x6f\xcd\x3b\xcd\x4a\xcc\x9b"
        "\xda\x3f\x3c\xfe\xf3\xc7\x57\x23\x6d\x76\xfd\xe9\x8d\xd7\x4b\x0e\xed"
        "\x68\x35\x0c\xbb\xba\xff\xfd\xcb\x3f\x58\xf9\x3f\x7c\x7b\x67\xf3\x1f"
        "\x48\x4f\x7a\xfe\xfd\xd7\xd8\xb8\x83\x33\xd5\xf2\x7e\xf5\xa7\xff\x7a"
        "\xa2\x33\xff\x43\xa3\x3d\xe6\xcf\x8f\xff\xf9\x6a\xf9\x8f\x65\xf9\x8f"
        "\x85\xde\xf2\xb7\x3e\xc8\xf2\xbf\x50\x2d\xff\x13\x59\xfe\x43\x3d\xe6"
        "\xdf\x74\xfc\x17\xab\xe5\x7f\x32\xe6\x9f\x4d\xf5\x3c\xd6\x6b\xfe\x8d"
        "\xef\xff\x44\x6c\xd3\x71\x1c\xec\x31\xff\xf1\xec\xf8\x5f\x0a\xbd\xe6"
        "\xcf\x8e\xbf\xd9\x63\xc2\xcc\xd7\x62\x7e\x00\x18\x46\x8d\x7e\x17\xb0"
        "\x4b\xd2\x59\x42\x3a\x8f\x9e\x8c\xcb\xe9\x78\xe3\xe9\x66\xc8\xef\x7e"
        "\xd8\xea\xf9\x7f\x23\xdb\xcf\xe8\xb6\x2b\xdf\xb8\xdf\x74\x1e\xf4\x40"
        "\x5c\x4e\xe7\x4b\x53\x59\xde\x64\xab\xf5\x4f\x66\xfb\xbb\xa7\x62\x9d"
        "\xb9\x41\xb9\xab\xa4\xac\xfe\x9d\x7a\x1f\x77\x5b\x59\xfd\x63\x35\xd7"
        "\x51\x55\x59\xfd\xe3\x35\xd7\x51\x55\x59\xfd\x13\x35\xd7\x51\x55\x59"
        "\xfd\x07\x6a\xae\xa3\xaa\xb2\xfa\x7b\x1d\x87\xf6\x5b\x59\xfd\x83\x72"
        "\x5d\xb9\xac\xfe\xc9\x9a\xeb\xa8\xaa\xac\xfe\xa9\x9a\xeb\xa8\xaa\xac"
        "\xfe\xad\xfe\x3f\xde\x2f\x65\xf5\x1f\xae\xb9\x8e\xaa\xca\xea\x9f\xae"
        "\xb9\x8e\xaa\xca\xea\xaf\x78\x59\xad\x76\x65\xf5\xcf\xd4\x5c\x47\x55"
        "\x65\xf5\xdf\x5b\x73\x1d\x55\x95\xd5\x7f\x5f\xcd\x75\x54\x55\x56\xff"
        "\xfd\x9b\x57\x0d\xca\x29\xd1\x96\x3c\x1c\xdb\xb2\xf1\x70\x1a\x7f\x4e"
        "\xc7\xbe\xb4\xdc\xcc\x96\x27\xee\xf2\xb3\xdc\xaf\xd7\x16\x00\x00\x00"
        "\x60\xd0\xfc\xdb\xfc\x7f\x42\x08\x21\x84\x10\x42\x08\x21\xc4\xbe\x8f"
        "\x56\xab\xdf\x57\x20\xe8\xa7\xdd\xfd\x34\x33\x00\x7b\x95\xdf\xff\xc3"
        "\xcd\xfb\x3f\xdc\xbc\xff\xc3\xcd\xfb\xcf\xff\x93\xee\xe1\x2f\xb2\xe5"
        "\x64\xa4\x4b\xff\x68\x97\xfe\xb1\x2e\xfd\xe3\x59\x7f\xfe\xef\x75\xa2"
        "\x4b\xff\x7d\xd9\x7e\x5b\x51\xea\xbf\xbf\x4b\xff\x17\xba\xf4\x1f\xee"
        "\xd2\xff\x40\x97\xfe\xd9\x2e\xfd\x0f\x76\xe9\x7f\xa8\x4b\xff\xc3\x5d"
        "\xfa\x01\x00\x00\x18\x0e\x5f\x8c\xad\xf1\x21\x00\x00\x00\xec\x5f\x97"
        "\x7f\xf9\xf1\x4f\x7e\x7d\xec\x85\x1b\x33\xb7\xae\x1d\x39\x13\xc6\x37"
        "\xcd\x3b\x7f\x32\x2e\x4f\xc4\xbf\xad\x5f\x8d\xcb\xf9\xbc\xf7\xc9\x58"
        "\xfc\x9b\xff\x8f\xe2\xf2\x2f\x62\xfb\xfb\xd8\xfe\x3d\xdb\xde\xfd\x27"
        "\x00\x00\x00\xb0\xfb\xd2\xf7\xc4\xf8\xfb\x3f\x00\x00\x00\xec\x5f\xe9"
        "\x7b\x4a\x8d\xff\x01\x00\x00\x60\xff\x9a\x89\xad\xf1\x3f\x00\x00\x00"
        "\xec\x5f\xf7\xc6\xd6\xf8\x1f\x00\x00\x00\xf6\xb1\xe2\xc0\xdd\x57\xc7"
        "\x36\x5d\x17\x78\x34\xb6\xbd\xce\xeb\x07\x00\xec\x7d\x5f\x8a\xed\x23"
        "\xb1\x3d\x12\xdb\xa3\xb1\xfd\x72\x6c\xd3\x79\xc0\x63\xb1\xfd\x4a\x4d"
        "\xf5\x01\x00\x3b\xe7\x67\xdf\xfd\xf1\x53\xef\x14\xeb\xf3\xfd\x9f\xca"
        "\xfa\x6f\xc7\xf5\xa9\xdd\x64\xf9\xce\x95\x82\xa2\xb1\x71\x26\xff\x83"
        "\xb1\x3d\x14\xdb\xc7\x7b\xac\x27\xff\x3e\x80\x5e\xf3\x27\x87\x7b\xcc"
        "\xb3\x5b\xf9\xa7\xb7\x99\x1f\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"
        "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"
        "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"
        "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"
        "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"
        "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"
        "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"
        "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"
        "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"
        "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"
        "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"
        "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"
        "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"
        "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"
        "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"
        "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"
        "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"
        "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"
        "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"
        "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"
        "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"
        "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"
        "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"
        "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"
        "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"
        "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"
        "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"
        "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"
        "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"
        "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"
        "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"
        "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"
        "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"
        "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"
        "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"
        "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"
        "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"
        "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"
        "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"
        "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"
        "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"
        "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"
        "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"
        "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"
        "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"
        "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"
        "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"
        "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"
        "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"
        "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"
        "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"
        "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"
        "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"
        "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"
        "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"
        "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"
        "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"
        "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"
        "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"
        "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"
        "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"
        "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"
        "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"
        "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"
        "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"
        "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"
        "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"
        "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"
        "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"
        "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"
        "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"
        "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"
        "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"
        "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"
        "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"
        "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"
        "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"
        "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"
        "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"
        "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"
        "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"
        "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"
        "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"
        "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"
        "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"
        "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"
        "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"
        "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"
        "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"
        "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"
        "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"
        "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"
        "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"
        "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"
        "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"
        "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"
        "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"
        "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"
        "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"
        "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"
        "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"
        "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"
        "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"
        "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"
        "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"
        "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"
        "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"
        "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"
        "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"
        "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"
        "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\xd8\x3f\x1a\x6b"
        "\x8f\xa7\x4f\xcf\x16\x21\xbc\xf7\xc9\xbb\x67\xa7\x2f\x9f\x7d\x79\x75"
        "\xdd\xd1\xf6\x16\x73\x6b\x8f\x45\x5c\x6a\x86\x10\xc6\xda\xaf\x4b\xbd"
        "\xeb\xcb\xbf\x8a\x1b\xde\xbe\xf1\xd6\xb9\xd5\x76\x25\xb6\xad\xd8\x16"
        "\x61\x21\x14\xa1\x68\xf7\x87\xe7\xae\xb7\x33\x4d\x86\x10\x96\xc3\x5c"
        "\xf8\x34\x34\xc3\xd1\xe3\xcd\x9b\x57\x47\x9e\x5d\xfc\xe8\xfd\xcf\x8e"
        "\x5d\xbe\xf8\xcc\x2b\xbb\xf8\x23\x00\x00\x00\x80\x7d\xef\x7f\x01\x00"
        "\x00\xff\xff\xf8\xc4\x2b\xbd",
        3798));
    NONFAILING(syz_mount_image(/*fs=*/0x20000ec0, /*dir=*/0x20000f00,
                               /*flags=*/0, /*opts=*/0x20000040, /*chdir=*/1,
                               /*size=*/0xed6, /*img=*/0x20001e40));
    break;
  case 11:
    NONFAILING(memcpy((void*)0x20000000, "blkio.bfq.time_recursive\000", 25));
    res = syscall(__NR_openat, /*fd=*/0xffffff9c, /*file=*/0x20000000ul,
                  /*flags=*/0x275aul, /*mode=*/0ul);
    if (res != -1)
      r[1] = res;
    break;
  case 12:
    syscall(__NR_write, /*fd=*/r[1], /*data=*/0x20000280ul,
            /*len=*/0x208e24bul);
    break;
  case 13:
    syscall(__NR_mmap, /*addr=*/0x20000000ul, /*len=*/0xb36000ul, /*prot=*/2ul,
            /*flags=*/0x28011ul, /*fd=*/r[1], /*offset=*/0ul);
    break;
  case 14:
    NONFAILING(*(uint64_t*)0x20001880 = 0);
    NONFAILING(*(uint32_t*)0x20001888 = 0);
    NONFAILING(*(uint64_t*)0x20001890 = 0);
    NONFAILING(*(uint64_t*)0x20001898 = 0);
    NONFAILING(*(uint64_t*)0x200018a0 = 0);
    NONFAILING(*(uint64_t*)0x200018a8 = 0);
    NONFAILING(*(uint32_t*)0x200018b0 = 0);
    NONFAILING(*(uint32_t*)0x200018b8 = 0);
    syscall(__NR_sendmmsg, /*fd=*/-1, /*mmsg=*/0x20001880ul, /*vlen=*/1ul,
            /*f=*/0ul);
    break;
  case 15:
    syscall(__NR_mbind, /*addr=*/0x20001000ul, /*len=*/0x800026ul,
            /*mode=*/0x5300000000000000ul, /*nodemask=*/0ul, /*maxnode=*/0ul,
            /*flags=*/2ul);
    break;
  }
}
int main(void)
{
  syscall(__NR_mmap, /*addr=*/0x1ffff000ul, /*len=*/0x1000ul, /*prot=*/0ul,
          /*flags=*/0x32ul, /*fd=*/-1, /*offset=*/0ul);
  syscall(__NR_mmap, /*addr=*/0x20000000ul, /*len=*/0x1000000ul, /*prot=*/7ul,
          /*flags=*/0x32ul, /*fd=*/-1, /*offset=*/0ul);
  syscall(__NR_mmap, /*addr=*/0x21000000ul, /*len=*/0x1000ul, /*prot=*/0ul,
          /*flags=*/0x32ul, /*fd=*/-1, /*offset=*/0ul);
  install_segv_handler();
  for (procid = 0; procid < 6; procid++) {
    if (fork() == 0) {
      use_temporary_dir();
      loop();
    }
  }
  sleep(1000000);
  return 0;
}