// https://syzkaller.appspot.com/bug?id=f86606dfb403cc9435c0ed5f17e1d80c110cddef
// 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 __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 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;
}

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

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 < 10; 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++) {
    reset_loop();
    int pid = fork();
    if (pid < 0)
      exit(1);
    if (pid == 0) {
      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;
    }
  }
}

uint64_t r[1] = {0xffffffffffffffff};

void execute_call(int call)
{
  intptr_t res = 0;
  switch (call) {
  case 0:
    NONFAILING(memcpy((void*)0x20000e00, "hfsplus\000", 8));
    NONFAILING(memcpy((void*)0x20000240, "./file1\000", 8));
    NONFAILING(memcpy(
        (void*)0x20000340,
        "\x6e\x6c\x73\x3d\x63\x70\x39\x35\x30\x2c\x6e\x6f\x62\x61\x72\x72\x69"
        "\x65\x72\x2c\x66\x6f\x72\x63\x65\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"
        "\x31\x30\x30\x30\x2c\x00\xd0\x12\x80\xf5\x32\xb0\xf4\xcf\x3b\x36\xfd"
        "\x5c\x6e\xf6\x42\x69\xa5\x33\xfc\x6b\x05\x2f\x92\xff\xca\xc0\x8c\xa1"
        "\x85\x19\xd5\xb3\x71\x1b\x97\xec\x29\x1e\x41\x35\x5f\xa6\x5d\x9c\x0d"
        "\x15\xe7\xc1\x0c\x63\xab\x0b\xba\x73\x6f\x92\xab\x9d\x30\x47\x8a\xe1"
        "\x45\x2a\x01\x7b\xe9\x8c\x2a\x05\x07\xfe\xbf\x29\x5d\xb1\x7e\x98\xd0"
        "\xff\xfc\x0d\x34\x92\x64\xce\x2a\xcd\xed\xc0\xa6\x1a\xe1\x32\xd4\x02"
        "\x4d\x39\xe1\x1d\xe2\xdc\x86\x79\xfa\xc7\xef\xcb\x61\x37\xaa\x2e\x36"
        "\x1c\x5f\x6c\xb8\x51\xe3\xa6\x0b\xa0\x5f\x83\x46\x66\x69\x46\x67\xf2"
        "\x7f\x0d\x63\x2c\xf9\xa8\x5f\xe2\x47\xe2\x72\x51\xac\xbd\x73\x9f\xf8"
        "\x50\x88\xcb\x20\x10\x10\x90\x01\x32\x00\x00\x00\x00\x00\x00\x00\x00"
        "\x00\x00\x25\x93\xd6\x00\x00\x00\x00",
        230));
    NONFAILING(*(uint16_t*)0x20000426 = -1);
    NONFAILING(sprintf((char*)0x20000428, "0x%016llx", (long long)-1));
    NONFAILING(memcpy(
        (void*)0x20002200,
        "\x78\x9c\xec\xdd\x4d\x6c\x1c\x77\xdd\x07\xf0\xef\xac\x37\x6b\x3b\xcf"
        "\xa3\xd4\x6d\x93\xb6\x20\xa4\x46\x8d\x88\xa0\x11\x89\x9d\xa5\x24\x08"
        "\xaa\x06\x84\x50\x0e\x15\x8a\xc4\xa5\x17\x0e\x56\xe2\x34\x56\x36\x69"
        "\xe5\xb8\xc8\xad\x10\xdd\xf2\x7a\xe5\xd8\x43\x0f\x45\x28\x1c\x7a\x42"
        "\x3d\x20\x15\x71\xa8\x28\x67\x24\x24\x4e\x5c\x72\x45\x91\xb8\xfb\xc4"
        "\xa2\x99\x9d\xb5\xd7\x59\xbf\x26\x71\x36\x2e\x9f\x8f\x34\x33\xff\x99"
        "\xff\xcb\xfc\xe6\xb7\x33\xb3\xde\x1d\x59\x1b\xe0\x7f\xd6\xc5\xd7\x72"
        "\xa8\x9b\x22\x17\x4f\xbd\xba\x52\xae\xdf\xb9\xdd\xee\xdc\xb9\xdd\xbe"
        "\x31\x28\x27\x99\x4c\xd2\x48\x9a\xfd\x45\x8a\x9b\x49\xf1\x59\x72\x21"
        "\xfd\x29\x5f\x28\x37\xd6\xc3\x15\x5b\xed\xe7\xa5\xbb\x9f\x7c\x70\xf2"
        "\xfd\x8f\xda\x49\x6f\xa2\x3f\x56\x73\xd0\xbe\x31\xdc\xaf\xd8\x72\x84"
        "\xed\xf6\xd0\xad\xa7\x1c\x4f\x32\x51\x2f\x1f\xc0\x86\xf1\x2e\x3f\xf0"
        "\x78\xc5\x5a\xdc\x65\xc2\x4e\x0c\x12\x07\xe3\xd6\x1b\xd1\xdd\x4b\xf7"
        "\x9d\xae\x56\xe0\x00\x28\xfa\xef\x9b\x23\x66\x92\xc3\x49\xa6\xea\xbf"
        "\x03\x52\xdf\x1d\x1a\x8f\x36\xba\x87\x6f\x4f\x77\x39\x00\x00\x00\x38"
        "\xa0\x9e\x58\xcd\x6a\x56\x72\x64\xdc\x71\x00\x00\x00\x00\x00\x00\x00"
        "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"
        "\x00\x00\x00\xc0\x41\x52\xff\xfe\x7f\x51\x4f\x8d\x41\xf9\x78\x8a\xc1"
        "\xef\xff\xb7\xaa\x6d\xad\xaa\x79\x6b\xdc\xf1\x0e\x29\xee\xa7\xd3\xa7"
        "\x0f\x3f\x0e\x00\x00\x00\x00\x00\x00\x00\x78\xe4\x9e\x5f\xcd\x6a\x56"
        "\x72\x64\xb0\xde\x2b\xaa\x67\xfe\x2f\x54\x2b\x47\xab\xf9\xff\xe5\xad"
        "\xdc\xca\x42\x96\x72\x3a\x2b\x99\xcf\x72\x96\xb3\x94\xb9\x24\x33\x55"
        "\xfd\x7b\xd5\xbc\xb5\x32\xbf\xbc\xbc\x34\xb7\x55\xcf\x5e\xaf\xf7\x6e"
        "\x6f\xa2\xea\x79\x76\xad\x67\x86\x7a\x9e\x1d\x09\x6d\x87\xe7\xf9\xbd"
        "\xc9\x07\x3e\x78\x00\x00\x00\x00\x00\x00\x00\x38\x60\xb6\x79\x58\xfe"
        "\xb3\x5c\x5c\x7f\xfe\x0f\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"
        "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"
        "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"
        "\x00\x00\x8f\x83\x22\x99\xe8\x2f\xaa\xe9\xe8\xa0\x3c\x93\x46\x33\xc9"
        "\x54\x92\x56\xd9\xae\x9b\xfc\x79\x50\x3e\xc8\x3e\x1d\x77\x00\x00\x00"
        "\x00\xf0\x08\x3c\xb1\x9a\xd5\xac\xe4\x48\xf1\x9f\xfe\x7a\xaf\xa8\x3e"
        "\xf3\x3f\x53\x7d\xee\x9f\xca\x5b\xb9\x99\xe5\x2c\x66\x39\x9d\x2c\xe4"
        "\x4a\xf5\x5d\x40\xff\x53\x7f\xe3\xef\xdd\x76\xe7\xce\xed\xf6\x8d\x72"
        "\x1a\x1d\xf7\x3b\xff\xde\x53\x18\xd5\x88\xe9\x7f\xf7\xb0\xf9\x9e\x67"
        "\xab\x16\xc7\xea\xf6\xdd\x24\xdf\xcf\x0f\x73\x2a\xc7\x73\x29\x4b\x59"
        "\xcc\x8f\x33\x9f\xe5\x2c\xe4\x78\xbe\x57\x95\xe6\x53\x64\xa6\x1a\xeb"
        "\x9f\x2f\x27\x75\x9c\x23\xf1\x4e\x96\xb3\x0b\x1b\x42\xb9\xb4\x53\xac"
        "\xcf\x55\x91\x4c\xe7\x6a\x16\xab\xd8\x4e\xe7\xf2\xda\xd7\x20\x8d\x41"
        "\x9b\xa1\xbd\xfd\xb1\x95\x6c\xc8\x50\x23\xef\x95\xd9\x29\x5e\xa9\xed"
        "\x32\x47\x57\xea\x65\xf9\x1a\xfc\xa6\x5e\xee\xaf\xee\x2e\xdb\xcd\x54"
        "\x07\x75\x68\x2d\x23\xb3\x75\xee\xcb\x6c\x3c\x79\x67\xab\xdc\x57\xfa"
        "\xe7\xc9\xe4\xb6\xc3\x37\x36\xdb\x53\x73\xad\x72\x90\xfc\xa3\xdb\xed"
        "\x69\x28\xe7\x45\x37\x79\xe5\xde\x57\x7d\x0b\x87\xeb\x65\x99\xeb\x5f"
        "\x0d\xe7\xfc\xf9\xdd\xf4\xde\x4f\xc3\x39\x2f\xa3\x3a\x9b\xc6\x5a\xa2"
        "\x9e\xd9\x3e\xe7\xc9\x57\xff\xf1\x97\x4b\xd7\x3a\x37\xaf\x5f\xbb\x7a"
        "\xeb\xd4\xfe\x9f\x46\x0f\xac\xd7\xeb\xbd\xbb\x75\xed\xbd\x67\x5f\x7b"
        "\x2d\x13\x53\x79\x76\xf7\x99\xe8\x6e\x91\x89\x8f\x37\xae\x4e\x6c\xd6"
        "\x66\x6a\x8f\x07\xb4\x4f\x5a\x75\x36\xfa\x31\xae\xdf\x2d\x93\x8b\xc3"
        "\x77\xcb\x5e\x2f\x19\xb9\x5b\xbe\x50\xf5\x3d\x92\xc5\xfc\x20\x6f\xe4"
        "\x4a\x16\x72\x2e\xb3\x99\xcb\xf9\xcc\xe6\x9b\x39\x9b\xf6\x86\x33\xec"
        "\xd8\xf6\x79\xad\xae\xb5\xc6\xde\xee\x6f\x27\xbe\x52\x17\xa6\x93\xfc"
        "\xba\x5e\x3e\x1e\xca\xbc\x3e\x39\x94\xd7\xe1\x3b\xdd\x4c\x55\x37\xbc"
        "\x65\x3d\x4b\x4f\xed\x22\x4b\x7b\x7c\x17\x68\x7e\xb1\x2e\x94\xfb\xf8"
        "\xf9\xc6\x9b\xe3\x98\xdd\x9b\x89\xb9\xa1\x4c\x3c\xbd\x7d\x26\x7e\x5b"
        "\x9e\x91\xb9\xd5\xb9\x79\x7d\xe9\xda\xfc\x9b\xbb\xdc\xdf\xc9\x7a\x59"
        "\x5e\xb6\xbf\xdc\xf8\x7e\xf8\xbb\x87\x71\x3c\xf7\xaf\x3c\x5f\x9e\x2a"
        "\x5f\xac\x6a\x6d\xe3\xd9\x51\xd6\x3d\xbd\x69\xdd\x5c\x55\x77\x74\xad"
        "\xae\x31\x52\x77\x6c\xad\x6e\xa7\x2b\xb5\x55\xff\x0d\x37\x3a\x52\xbf"
        "\xee\xd9\x4d\xeb\xda\x55\xdd\x73\x43\x75\x1b\xfe\xca\xc9\x1b\xe9\xe4"
        "\xca\x81\x7f\xe4\x03\xf0\x79\xd3\x1b\xdd\x74\xf8\xc5\xc3\xad\xe9\xbb"
        "\xd3\x7f\x9b\xfe\x70\xfa\x17\xd3\xd7\xa6\x5f\x9d\xfa\xee\xe4\xf9\xc9"
        "\x2f\xb5\x72\xe8\xaf\xcd\x3f\x4d\xfc\xa1\xf1\xfb\xc6\xb7\x8b\x17\xf3"
        "\x61\x7e\x9a\x23\xe3\x08\x18\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"
        "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x3e\x6f\x6e\xbd"
        "\xfd\xce\xf5\xf9\x4e\xe7\x5f\x2f\xd7\x85\x85\xa5\xcd\x0b\xad\x4e\x91"
        "\x2d\xaa\xf6\xad\x90\xc6\xde\x7a\x95\xc7\xb3\xab\xc6\xbd\x3c\xc2\xa3"
        "\x18\x43\x61\x22\xa3\x55\x93\x49\xf6\x3a\x4e\xd1\xbc\xff\x30\x7a\x9b"
        "\x54\x7d\x9c\xdc\x7f\xe6\x07\xbf\x15\x38\xd8\xf2\xe5\xce\x42\xfe\x7f"
        "\x9b\x5e\xcd\xa1\x33\x7c\xbd\xd7\x63\xf0\xea\x3c\x8c\xc2\xb7\x7e\x54"
        "\x27\x63\x87\xc6\xcd\x8d\x97\xf9\x4e\x23\x4f\xd7\x03\x3e\x3e\x3f\x2e"
        "\x06\xec\x9f\x33\xcb\x37\xde\x3c\x73\xeb\xed\x77\xbe\xb6\x78\x63\xfe"
        "\xf5\x85\xd7\x17\x6e\x9e\x3d\x7f\xee\xfc\xb9\xf6\x37\xe6\xbe\x7e\xe6"
        "\xea\x62\x67\x61\xb6\x3f\x1f\x77\x94\xc0\x7e\x58\x7f\xf7\x1f\x77\x24"
        "\x00\x00\x00\x00\x00\x00\x00\x00\x00\xc0\x6e\x3d\x8a\xff\x58\x18\xf7"
        "\x31\x02\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"
        "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"
        "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"
        "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"
        "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"
        "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"
        "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"
        "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"
        "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"
        "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"
        "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"
        "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"
        "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"
        "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"
        "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"
        "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"
        "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"
        "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"
        "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"
        "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"
        "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"
        "\x00\x00\x00\x00\x00\x00\x00\x00\x07\xdb\xc5\xd7\x72\xa8\x9b\x22\x73"
        "\xb3\xa7\x67\xcb\xf5\x3b\xb7\xdb\x9d\x72\x1a\x94\xd7\x5b\x36\x93\x34"
        "\x92\x14\x3f\x49\x8a\xcf\x92\x0b\xe9\x4f\x99\xa9\xaa\x8b\xa1\xf9\x26"
        "\x5e\xba\xfb\xc9\x07\x27\xdf\xff\xa8\xbd\x3e\x56\x73\xd0\xbe\xb1\x5d"
        "\xbf\xdd\xe9\xd6\x53\x8e\x27\x99\xa8\x97\xf7\x36\x18\x35\xb9\xab\xf1"
        "\x2e\x0f\x8f\xd7\xb8\x9f\xf0\x8a\xb5\x23\x2c\x13\x76\x62\x90\x38\x18"
        "\xb7\xff\x06\x00\x00\xff\xff\xa6\x50\x00\x4c",
        1728));
    NONFAILING(syz_mount_image(/*fs=*/0x20000e00, /*dir=*/0x20000240,
                               /*flags=MS_SILENT|MS_RELATIME*/ 0x208000,
                               /*opts=*/0x20000340, /*chdir=*/0xfe,
                               /*size=*/0x6c0, /*img=*/0x20002200));
    break;
  case 1:
    NONFAILING(memcpy((void*)0x20000040, "memory.events\000", 14));
    syscall(__NR_openat, /*fd=*/0xffffff9c, /*file=*/0x20000040ul,
            /*flags=*/0x275a, /*mode=*/0);
    break;
  case 2:
    NONFAILING(memcpy((void*)0x20000140, "./file4\000", 8));
    syscall(__NR_mknodat, /*dirfd=*/0xffffff9c, /*file=*/0x20000140ul,
            /*mode=*/0ul, /*dev=*/0);
    break;
  case 3:
    NONFAILING(memcpy((void*)0x20000000, "./file4\000", 8));
    NONFAILING(memcpy((void*)0x200006c0, "./file5\000", 8));
    syscall(__NR_linkat, /*oldfd=*/0xffffff9c, /*old=*/0x20000000ul,
            /*newfd=*/0xffffff9c, /*new=*/0x200006c0ul, /*flags=*/0ul);
    break;
  case 4:
    NONFAILING(memcpy((void*)0x20000040, "cpuacct.usage_percpu_user\000", 26));
    syscall(__NR_openat, /*fd=*/0xffffff9c, /*file=*/0x20000040ul,
            /*flags=*/0x275a, /*mode=*/0);
    break;
  case 5:
    NONFAILING(memcpy((void*)0x20000100, "cpuset.effective_cpus\000", 22));
    syscall(__NR_openat, /*fd=*/0xffffff9c, /*file=*/0x20000100ul,
            /*flags=*/0x275a, /*mode=*/0);
    break;
  case 6:
    NONFAILING(memcpy((void*)0x20000100,
                      "blkio.bfq.io_service_time_recursive\000", 36));
    syscall(__NR_openat, /*fd=*/0xffffff9c, /*file=*/0x20000100ul,
            /*flags=*/0x275a, /*mode=*/0);
    break;
  case 7:
    NONFAILING(memcpy((void*)0x20000040, ".\000", 2));
    res = syscall(__NR_openat, /*fd=*/0xffffff9c, /*file=*/0x20000040ul,
                  /*flags=*/0, /*mode=*/0);
    if (res != -1)
      r[0] = res;
    break;
  case 8:
    NONFAILING(memcpy((void*)0x20000000, "cpu.stat\000", 9));
    syscall(__NR_openat, /*fd=*/0xffffff9c, /*file=*/0x20000000ul,
            /*flags=*/0x275a, /*mode=*/0);
    break;
  case 9:
    NONFAILING(memcpy((void*)0x20000080, "cgroup.controllers\000", 19));
    syscall(__NR_openat, /*fd=*/r[0], /*file=*/0x20000080ul, /*flags=*/0x275a,
            /*mode=*/0);
    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;
  install_segv_handler();
  loop();
  return 0;
}