// https://syzkaller.appspot.com/bug?id=65f3caae39807b0b41f8e62dec2a517a7e510651
// 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_memfd_create
#define __NR_memfd_create 319
#endif

static unsigned long long procid;

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;
}

//% 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;
  const int umount_flags = MNT_FORCE | UMOUNT_NOFOLLOW;

retry:
  while (umount2(dir, umount_flags) == 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, umount_flags) == 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, umount_flags))
        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, umount_flags))
          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)
{
  if (write(1, "executing program\n", sizeof("executing program\n") - 1)) {
  }
  int i, call, thread;
  for (call = 0; call < 4; 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));
      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 (;;) {
      sleep_ms(10);
      if (waitpid(-1, &status, WNOHANG | WAIT_FLAGS) == pid)
        break;
      if (current_time_ms() - start < 5000)
        continue;
      kill_and_wait(pid, &status);
      break;
    }
    remove_dir(cwdbuf);
  }
}

uint64_t r[1] = {0xffffffffffffffff};

void execute_call(int call)
{
  intptr_t res = 0;
  switch (call) {
  case 0:
    memcpy((void*)0x20000000, "hfsplus\000", 8);
    memcpy((void*)0x20000080, "./file0\000", 8);
    memcpy(
        (void*)0x20000500,
        "\x70\x61\x72\x74\x3d\x30\x78\x30\x30\x30\x30\x30\x30\x30\x30\x30\x30"
        "\x30\x30\x30\x30\x30\x34\x2c\x6e\x6c\x73\x3d\x69\x73\x6f\x38\x38\x35"
        "\x39\x2d\x31\x2c\x73\x65\x73\x73\x69\x6f\x6e\x3d\x30\x78\x66\x66\x66"
        "\x66\x66\x66\x66\x66\x66\x66\x66\x66\x66\x66\x37\x66\x2c\x74\x79\x70"
        "\x65\x3d\x88\x3b\x7f\x38\x2c\x63\x72\x65\x61\x74\x6f\x72\x3d\xdd\xf2"
        "\xbd\x6c\x2c\x64\x65\x63\x6f\x6d\x70\x6f\x73\x65\x2c\x75\x69\x64\x3d",
        102);
    sprintf((char*)0x20000566, "0x%016llx", (long long)0);
    memcpy(
        (void*)0x20000578,
        "\x2c\x75\x6d\x61\x73\x6b\x3d\x30\x30\x30\x30\x30\x30\x30\x30\x30\x30"
        "\x30\x30\x30\x30\x30\x30\x30\x30\x30\x30\x30\x30\x33\x2c\x63\x72\x65"
        "\x61\x74\x6f\x72\x3d\x85\xf1\x94\x71\x2c\x62\x61\x72\x72\x69\x65\x72"
        "\x2c\x63\x72\x65\x61\x74\x6f\x72\x3d\x65\xfe\x04\xc2\x2c\x75\x6d\x61"
        "\x73\x6b\x3d\x30\x30\x30\x30\x30\x30\x30\x30\x30\x30\x30\x30\x30\x30"
        "\x30\x30\x30\x30\x30\x30\x32\x31\x37\x2c\x64\x65\x63\x6f\x6d\x70\x6f"
        "\x73\x65\x2c\x66\x6f\x72\x63\x65\x2c\x75\x69\x64\x3d",
        115);
    *(uint16_t*)0x200005eb = -1;
    memcpy((void*)0x200005ed,
           "\x2c\x75\x6d\x61\x73\x6b\x3d\x30\x30\x2c\x66\x6b\x72\x63\x65\x2c"
           "\x6e\x60\x49\x79\x62\xcb\x13\xd6\xef\x2c\x00\xe4\x1f\x00\x00\x00"
           "\x00\x00\x00\x00\x00\x04\x00\x00\x00\x00",
           42);
    memcpy(
        (void*)0x20001e80,
        "\x78\x9c\xec\xdd\x4b\x6c\x1c\x67\x1d\x00\xf0\xff\xac\xd7\x6b\xaf\x91"
        "\x52\xa7\xcd\xa3\xa0\x4a\x5d\x35\x52\x41\x58\x24\x76\xac\x14\xc2\x85"
        "\x80\x10\xf2\xa1\x42\x15\x1c\x38\x5b\x89\xd3\x58\xd9\x38\x95\xed\x22"
        "\x27\x42\xd4\xe5\x71\xe7\xc0\x89\x53\x39\xf8\x56\x71\x40\xe5\x1e\x09"
        "\xce\x54\x95\x10\x57\x1f\x2b\x21\xf5\xc2\xc9\x37\xa3\x99\x9d\x19\xcf"
        "\xbe\xed\xc4\xaf\x96\xdf\xcf\x9a\xf9\xbe\xf9\x5e\xf3\xcd\x37\x3b\xaf"
        "\x5d\x59\x13\xc0\xff\xad\xa5\xb9\xa8\x3f\x8b\x24\x96\xe6\xde\xde\x4a"
        "\x97\x77\x77\x16\xdb\x13\x3b\x8b\x53\x79\x76\x3b\x22\x1a\x11\x51\x8b"
        "\xa8\x77\x82\x48\xd6\x22\xcd\x9d\x28\x9b\xf8\x7a\x9a\x98\xc7\x93\xc1"
        "\x6b\x29\x0a\xef\x7e\xde\x09\xeb\xf9\x94\x95\xaf\x0d\xaf\x37\x50\xa3"
        "\x3f\x69\x3b\x9f\xa2\x95\xaf\xac\xd5\x9d\x3f\xdd\x09\x26\x87\xb4\xf8"
        "\x49\xef\xea\xbb\xda\xbb\xdb\xdf\xde\x11\x25\xe5\x16\xde\x89\x88\x6b"
        "\x79\x18\xf1\xa7\x17\x6a\x15\x5e\xd8\x7e\x9f\xed\x32\xef\xe3\x7f\x65"
        "\xf3\x51\xd5\x8f\x72\xdc\x02\xe7\x54\x52\xbd\xa2\x57\xcc\x46\xcc\xe4"
        "\x17\xd0\xec\x9e\x20\x3f\x3b\xd4\x4e\xb7\x77\xc7\x6f\xfb\xac\x3b\x00"
        "\x00\x00\x00\x47\xd5\x3c\x7a\x95\x97\xf6\x62\x2f\xb6\xe2\xc2\x49\x74"
        "\x07\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"
        "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\xbe\xaa\xba\xde\xff\xbf"
        "\x9d\xbd\xdf\x3f\x49\xa7\x56\x24\xc5\xfb\xff\x1b\x79\x5a\xe4\xf1\x73"
        "\x68\xfc\x8b\x10\x3f\x9b\xea\x84\xcf\x4e\xbe\x33\x00\x00\x00\x00\x00"
        "\x00\x00\x70\xe2\x5e\xdf\x8b\xbd\xd8\x8a\x0b\xc5\xf2\x7e\x92\xfd\xe6"
        "\xff\x46\xe5\x37\xfe\xaf\xc5\xfb\xb1\x11\x2b\xb1\x1e\xd7\x63\x2b\x96"
        "\x63\x33\x36\x63\x3d\x16\x22\x62\xb6\xd2\x50\x63\x6b\x79\x73\x73\x7d"
        "\x21\xab\x19\x71\x69\x44\xcd\x9b\xf1\xe9\x80\x9a\x37\x87\xf7\xf1\xce"
        "\x31\x6f\x33\x00\x00\x00\x00\x00\x00\x00\x9c\x73\xd3\x63\xf2\x1f\x4e"
        "\xf6\xa7\xfd\x26\x96\x0e\x7e\xff\x07\x00\x00\x00\x00\x00\x00\x00\x00"
        "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"
        "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"
        "\x00\x00\x00\x00\x80\xf3\x20\x89\x98\xe8\x04\xd9\x74\xa9\x88\xcf\x46"
        "\xad\x1e\x11\xd3\x11\xd1\x48\xcb\x6d\x47\x7c\x5a\xc4\xbf\x24\x92\x41"
        "\x89\xcf\x4e\xbf\x1f\x00\x00\x00\xf0\x42\xa6\xbb\x17\x93\xe9\x43\xd4"
        "\x79\xe9\x83\xd8\x8b\xad\xb8\x50\x2c\xef\x27\xd9\x33\xff\x95\xec\x79"
        "\x79\x3a\xde\x8f\xb5\xd8\x8c\xd5\xd8\x8c\x76\xac\xc4\xbd\xfc\x19\x3a"
        "\x7d\xea\xaf\xed\xee\x2c\xb6\x77\x77\x16\x1f\xa5\x53\x7f\xbb\x3f\xfc"
        "\xe2\x48\x5d\xcf\x5a\x8c\xce\x77\x0f\x83\xd7\xfc\x6a\x56\xa2\x19\xf7"
        "\x63\x35\x4b\xb9\x1e\x77\x23\x89\xfd\x4c\x2d\x6f\xe5\xd5\xdd\x9d\xc5"
        "\x34\x7c\x54\xed\xd7\xc1\x63\xff\x87\x69\x9f\x92\x1f\xe4\x46\xf4\x66"
        "\xa2\x12\xbf\x97\xce\xae\x7e\x92\xc5\xff\xd0\xfd\x2d\x42\xfd\x48\x9b"
        "\xf8\x9c\x6a\x43\x73\x66\xb3\xdc\xc9\x72\x44\xe6\xf3\xbe\xa5\x35\x2e"
        "\x16\x23\x30\x78\x0f\x8d\xdd\x3b\xf5\x91\x6b\x5a\x88\x5a\xf9\xcd\xcf"
        "\xa5\xd1\x6b\x1a\x3c\xe6\x1f\x8e\x5e\xfb\x4c\x4f\xa9\x81\xdf\xdc\x9c"
        "\x89\xde\x91\xb8\x19\xb5\x72\x0f\x5d\x19\x3d\x12\x11\xdf\xfc\xdb\xc7"
        "\xbf\x78\xd0\x5e\x7b\xf8\xe0\xfe\xc6\xdc\xf9\xd9\xa4\x81\x3e\x18\x5b"
        "\xa2\x77\x24\x16\x2b\x23\x71\xf5\x2b\x34\x12\xe3\xcd\x67\x23\x71\xb9"
        "\x5c\x5e\x8a\x9f\xc4\xcf\x63\x2e\xbe\x98\x7a\x27\xd6\x63\x35\x7e\x19"
        "\xcb\xb1\x19\x2b\xad\x22\x7f\x39\xff\x3c\xa7\xf3\xd9\xd1\x23\xf5\xd9"
        "\x4c\x75\xe9\x9d\x71\x3d\x49\x8f\xc9\x56\x79\xfe\x1a\xd4\xa7\x56\x74"
        "\xf5\x29\x5a\xf1\xe3\x2c\xb6\x1c\x6f\x64\xfb\xf4\x42\xac\x46\x12\x8f"
        "\x23\x62\x25\xde\xca\xfe\x6e\xc6\x42\x79\x36\x38\xd8\xc3\x97\x07\xf7"
        "\xbb\x38\x29\x64\x47\x7d\xed\x10\x67\xda\x8a\x6b\xdf\xca\x82\x72\x98"
        "\xa2\x39\xbc\xec\x5f\x0e\xd7\xe4\x71\x49\x37\xeb\x62\x65\x5c\xab\xe7"
        "\xdc\xd9\x2c\x2f\x4f\xd9\x4e\x53\x0e\x46\xe9\xe5\x81\xa3\x54\x5c\xeb"
        "\x0e\x7f\x3d\xaa\xa8\x7f\x23\x8f\xa4\x2d\xfc\x76\xe4\xf5\xe1\xb4\x75"
        "\x8d\x44\x7e\x95\x28\x7a\xf7\xca\xb0\xcf\x79\x67\x48\xff\xbc\x9f\xce"
        "\x37\xda\x6b\x0f\xd7\x1f\x2c\xbf\x77\xc8\xf5\xbd\x99\x87\xe9\x71\xf4"
        "\xfb\x73\x75\x95\x48\xf7\xf0\xcb\x31\x9d\x6f\xdc\xc5\x6c\x9e\x64\xc7"
        "\xd4\x7c\x96\xf7\x4a\x79\x85\xed\x1e\xaf\x46\xfe\x8b\x4b\x47\xad\x2f"
        "\xef\x72\x59\xaf\x73\xa4\xfe\x34\x1e\xc7\xbd\xae\x23\xf5\xbb\x71\xab"
        "\xbc\x0d\xbc\x92\x95\x9e\xec\xbb\x62\xa5\x2d\x5d\x2d\x5b\xea\x3e\x87"
        "\xa7\x79\xe9\x9d\x56\xbd\xfc\x61\xa7\x7a\xbf\xf5\x38\xda\x9d\xfb\x21"
        "\x00\xce\xb7\x99\x6f\xcf\x34\x9a\xff\x69\xfe\xb3\xf9\x51\xf3\x77\xcd"
        "\x07\xcd\xb7\xa7\x7f\x34\xf5\xbd\xa9\xd7\x1a\x31\xf9\x8f\xc9\xef\xd7"
        "\xe7\x27\xde\xac\xbd\x96\xfc\x35\x3e\x8a\x5f\x1f\x3c\xff\x03\x00\x00"
        "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"
        "\x00\x00\x00\x00\xcf\x6f\xe3\xc9\xd3\x87\xcb\xed\xf6\xca\xfa\xe0\x48"
        "\x6d\x70\x56\x32\xba\xd6\x72\x7b\xbf\x78\x91\xd8\xe0\x32\xd1\x97\x92"
        "\xe4\xaf\xca\x19\xd3\x9f\x34\x92\x6c\x3c\x79\xba\x3f\xa6\x4c\x6f\x24"
        "\x7a\x52\xa6\xf2\xee\x1d\xb2\xfa\x49\x46\x8a\xd7\xf6\x8c\x2f\xdc\x3a"
        "\xc1\x6e\x24\xdb\xbd\xfb\x6b\x7a\xfc\xbe\x28\xde\xf2\x94\xa7\x8c\xda"
        "\x8a\xa4\x6f\xc0\xd3\xca\xcf\xdd\xe7\x62\xcd\x07\x29\x93\xe7\x60\x57"
        "\xf6\x46\x5a\xc7\xd7\x60\xf1\x81\xad\x1e\x44\x47\xfe\xf4\x36\xbb\xf7"
        "\x57\x27\x6b\x22\x22\x06\x1e\x32\xa3\x4d\x1c\xc3\xc9\x07\x38\x53\x37"
        "\x36\x1f\xbd\x77\x63\xe3\xc9\xd3\xef\xac\x3e\x5a\x7e\x77\xe5\xdd\x95"
        "\xb5\xc9\x5b\xb7\x6e\xcf\xdf\xbe\xf5\xd6\xe2\x8d\xfb\xab\xed\x95\xf9"
        "\xce\xbc\x52\xe1\x54\x5e\x7e\x0b\x9c\x86\xea\xed\x44\xa9\x11\x11\xaf"
        "\x8f\xaf\x3b\xe2\x45\xad\x00\x00\x00\x00\x00\x00\x00\x00\x00\xc0\x09"
        "\x3a\x8d\xff\x85\x38\xeb\x6d\x04\x00\x00\x00\x00\x00\x00\x00\x00\x00"
        "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"
        "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"
        "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"
        "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"
        "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"
        "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"
        "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"
        "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"
        "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"
        "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"
        "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"
        "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"
        "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"
        "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"
        "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"
        "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"
        "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"
        "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"
        "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"
        "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"
        "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\xbe\xdc\x96"
        "\xe6\xa2\xfe\x2c\x92\x58\x98\xbf\x3e\x3f\x95\xa7\xed\xee\x2c\xb6\xab"
        "\x61\x47\x3d\x22\x6a\x11\x91\xfc\x2a\x22\xf9\x7b\xc4\x9d\xe8\x4c\x31"
        "\x5b\x69\x2e\x19\xb6\x9e\x3f\xae\xde\xfe\xd9\xbf\xff\xbb\xfb\xf9\x41"
        "\x5b\xf5\xa2\x7c\x2d\x62\x7b\x68\xbd\xc3\xd9\xce\xa7\x68\x45\xc4\x44"
        "\x1e\x96\x76\x86\x57\x9c\x3d\x44\x7b\x77\x7b\xdb\xeb\xd7\x38\x88\x4e"
        "\x0d\xc8\x4e\xca\x91\x49\x07\xec\x5a\x31\x70\x70\xd6\xfe\x17\x00\x00"
        "\xff\xff\x77\x11\xe5\x7c",
        1774);
    syz_mount_image(
        /*fs=*/0x20000000, /*dir=*/0x20000080,
        /*flags=MS_LAZYTIME|MS_I_VERSION|MS_NOEXEC|MS_NODEV*/ 0x280000c,
        /*opts=*/0x20000500, /*chdir=*/1, /*size=*/0x6ee, /*img=*/0x20001e80);
    break;
  case 1:
    memcpy((void*)0x20000040, "cpuset.effective_cpus\000", 22);
    res = syscall(__NR_openat, /*fd=*/0xffffff9c, /*file=*/0x20000040ul,
                  /*flags=*/0x275a, /*mode=*/0);
    if (res != -1)
      r[0] = res;
    break;
  case 2:
    syscall(__NR_ftruncate, /*fd=*/r[0], /*len=*/0x2000009ul);
    break;
  case 3:
    memcpy((void*)0x200001c0, "./file0/file1\000", 14);
    syscall(__NR_unlinkat, /*fd=*/0xffffff9c, /*path=*/0x200001c0ul,
            /*flags=*/0ul);
    break;
  }
}
int main(void)
{
  syscall(__NR_mmap, /*addr=*/0x1ffff000ul, /*len=*/0x1000ul, /*prot=*/0ul,
          /*flags=MAP_FIXED|MAP_ANONYMOUS|MAP_PRIVATE*/ 0x32ul, /*fd=*/-1,
          /*offset=*/0ul);
  syscall(__NR_mmap, /*addr=*/0x20000000ul, /*len=*/0x1000000ul,
          /*prot=PROT_WRITE|PROT_READ|PROT_EXEC*/ 7ul,
          /*flags=MAP_FIXED|MAP_ANONYMOUS|MAP_PRIVATE*/ 0x32ul, /*fd=*/-1,
          /*offset=*/0ul);
  syscall(__NR_mmap, /*addr=*/0x21000000ul, /*len=*/0x1000ul, /*prot=*/0ul,
          /*flags=MAP_FIXED|MAP_ANONYMOUS|MAP_PRIVATE*/ 0x32ul, /*fd=*/-1,
          /*offset=*/0ul);
  const char* reason;
  (void)reason;
  for (procid = 0; procid < 5; procid++) {
    if (fork() == 0) {
      use_temporary_dir();
      loop();
    }
  }
  sleep(1000000);
  return 0;
}