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

#define _GNU_SOURCE

#include <dirent.h>
#include <endian.h>
#include <errno.h>
#include <fcntl.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/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 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_dev(volatile long a0, volatile long a1, volatile long a2)
{
  if (a0 == 0xc || a0 == 0xb) {
    char buf[128];
    sprintf(buf, "/dev/%s/%d:%d", a0 == 0xc ? "char" : "block", (uint8_t)a1,
            (uint8_t)a2);
    return open(buf, O_RDWR, 0);
  } else {
    char buf[1024];
    char* hash;
    strncpy(buf, (char*)a0, sizeof(buf) - 1);
    buf[sizeof(buf) - 1] = 0;
    while ((hash = strchr(buf, '#'))) {
      *hash = '0' + (char)(a1 % 10);
      a1 /= 10;
    }
    return open(buf, a2, 0);
  }
}

//% 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")) {
  }
}

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[2] = {0xffffffffffffffff, 0xffffffffffffffff};

void execute_one(void)
{
  intptr_t res = 0;
  if (write(1, "executing program\n", sizeof("executing program\n") - 1)) {
  }
  memcpy((void*)0x20000140, "hfsplus\000", 8);
  memcpy((void*)0x20000040, "./file2\000", 8);
  memcpy((void*)0x20000080, "decompose", 9);
  *(uint8_t*)0x20000089 = 0x2c;
  memcpy((void*)0x2000008a, "umask", 5);
  *(uint8_t*)0x2000008f = 0x3d;
  sprintf((char*)0x20000090, "%023llo", (long long)0x20);
  *(uint8_t*)0x200000a7 = 0x2c;
  memcpy((void*)0x200000a8, "type", 4);
  *(uint8_t*)0x200000ac = 0x3d;
  memcpy((void*)0x200000ad, "\xa7\x86\x88\x28", 4);
  *(uint8_t*)0x200000b1 = 0x2c;
  memcpy((void*)0x200000b2, "umask", 5);
  *(uint8_t*)0x200000b7 = 0x3d;
  sprintf((char*)0x200000b8, "%023llo", (long long)0x8001);
  *(uint8_t*)0x200000cf = 0x2c;
  memcpy((void*)0x200000d0, "nls", 3);
  *(uint8_t*)0x200000d3 = 0x3d;
  memcpy((void*)0x200000d4, "cp866", 5);
  *(uint8_t*)0x200000d9 = 0x2c;
  memcpy((void*)0x200000da, "umask", 5);
  *(uint8_t*)0x200000df = 0x3d;
  sprintf((char*)0x200000e0, "%023llo", (long long)9);
  *(uint8_t*)0x200000f7 = 0x2c;
  memcpy((void*)0x200000f8, "barrier", 7);
  *(uint8_t*)0x200000ff = 0x2c;
  memcpy((void*)0x20000100, "creator", 7);
  *(uint8_t*)0x20000107 = 0x3d;
  memcpy((void*)0x20000108, "\x8e\x4d\x91\x37", 4);
  *(uint8_t*)0x2000010c = 0x2c;
  memcpy((void*)0x2000010d, "nobarrier", 9);
  *(uint8_t*)0x20000116 = 0x2c;
  *(uint8_t*)0x20000117 = 0;
  memcpy(
      (void*)0x20000380,
      "\x78\x9c\xec\xdd\xcd\x6f\x1c\x67\x1d\x07\xf0\xef\xac\xd7\x6f\x41\x6a\x9d"
      "\x36\x69\x0b\xaa\x44\x68\xa4\x82\x88\x48\xfc\xa2\x14\xc2\x25\x01\x21\xe4"
      "\x43\x85\x2a\x38\x70\xb6\x12\xa7\xb1\xb2\x49\x2b\xdb\x05\xb7\x42\x34\xbc"
      "\x5f\x7b\xe8\x1f\x50\x0e\xb9\x71\x42\xe2\x1e\x54\xce\x70\x83\xa3\xc5\xa9"
      "\x12\xa2\x17\x4e\xb9\x2d\x9a\x97\xb5\x37\x59\xaf\xb3\x1b\x27\xb1\xad\x7e"
      "\x3e\xd1\xec\xf3\xcc\x3c\xf3\xbc\xfd\x76\x66\x76\x67\xad\x68\x02\x7c\x61"
      "\x2d\x9f\x4b\xfb\x5e\x8a\x2c\x9f\x7b\x73\xab\x5c\xdf\xbe\xbb\xd4\xd9\xbe"
      "\xbb\x34\xdd\x14\x77\x92\x94\xf9\x56\xd2\xae\x93\x14\xb7\x93\xe2\xd3\xe4"
      "\x4a\xea\x25\x5f\x2e\x37\x36\xfb\x17\xc3\xfa\xf9\x78\xed\xd2\x8f\xff\xf9"
      "\xbf\xed\xcf\xea\xb5\x76\xb3\x54\xfb\xb7\x06\xeb\x7d\xd8\x1d\x6f\x16\x77"
      "\x9a\x25\x67\x92\x4c\x34\xe9\xa0\xc9\xc1\x4d\x33\x8f\x6e\xef\xea\xd0\xf6"
      "\x6a\xd3\x7d\xf9\xbd\xe7\x5f\xec\x94\x94\x01\x3b\xdb\x0b\x1c\x1c\xb6\xee"
      "\x80\x3b\xe3\x54\x1f\x7a\xbe\x03\xc7\x47\x51\x7f\x6e\x0e\x98\x4b\x4e\x34"
      "\x1f\x93\xd5\xe7\x5c\x73\x75\x68\x3d\xdb\xd1\x3d\x79\x63\x5d\xe5\x00\x00"
      "\x00\xe0\x90\x2d\xff\xf7\xf4\xe2\x38\xfb\xcf\x36\xe9\xf3\xf7\x73\x3f\x5b"
      "\xc7\xfe\x36\x1e\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"
      "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x9e\xa9\xe6\xf9\xff"
      "\x45\xb3\xb4\x7a\xf9\x33\x29\x7a\xcf\xff\x9f\x6a\xb6\xa5\xc9\x1f\x6b\xf7"
      "\x0e\x7b\x00\x00\x00\x00\x00\x00\x00\x00\x30\x82\xe9\x5e\xe6\xf2\xde\xe5"
      "\x5f\xbd\x9f\xfb\xd9\xca\x73\xbd\xf5\x6e\x51\xfd\xcd\xff\xb5\x6a\xe5\x54"
      "\xf5\xfa\xa5\xbc\x97\x8d\xac\x66\x3d\xe7\xb3\x95\x95\x6c\x66\x33\xeb\x59"
      "\x48\x32\xd7\xd7\xd0\xd4\xd6\xca\xe6\xe6\xfa\xc2\x08\x35\x17\xf7\xac\xb9"
      "\xf8\x64\xe7\x0d\x00\x00\x00\x00\x00\x00\x00\x5f\x30\xbf\xce\xf2\xee\xdf"
      "\xff\x01\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"
      "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"
      "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\xe0\x28\x28\x92\x89\x3a"
      "\xa9\x96\x53\xbd\xfc\x5c\x5a\xed\x24\x33\x49\xa6\xca\xfd\xee\x24\xff\xe8"
      "\xe5\x8f\x89\x62\xaf\x8d\xf7\x9e\xfd\x38\x00\x00\x00\xe0\x40\x66\x1e\xa3"
      "\xce\x54\x72\x3f\x5b\x79\xae\xb7\xde\x2d\xaa\x7b\xfe\x97\xaa\xfb\xe5\x99"
      "\xbc\x97\xdb\xd9\xcc\x5a\x36\xd3\xc9\x6a\xae\x35\xf7\xd0\xe5\x5d\x7f\x6b"
      "\xfb\xee\x52\x67\xfb\xee\xd2\xad\x72\x39\xf0\xd0\xab\x16\x53\xff\xf6\xb0"
      "\x77\xcf\xaf\x54\x7b\xcc\xe6\x7a\xd6\xaa\x2d\xe7\x73\xb5\x1a\xcc\xb5\xb4"
      "\xaa\x9a\xa5\x7f\x27\xb9\xd5\x1b\xd3\xe0\xb8\x7e\xf5\x79\xd9\xf6\xe5\xda"
      "\xcf\x47\x1c\xd9\xb5\x26\x2d\x3b\xfb\xa8\xf7\x2b\xc2\xf4\x81\x27\xfc\x04"
      "\xcc\x25\xed\x56\x26\x77\x22\x32\x5f\x8d\xad\x3e\x08\x4e\xf6\x47\x61\x30"
      "\x12\xdf\xfb\x7c\x58\xa3\x57\xea\xa4\xfd\x70\x4f\xe9\xef\x69\x21\xad\x9d"
      "\x5f\x7e\x4e\xd5\x3d\x94\xd9\x47\xc6\xfc\xf2\x88\x33\x3b\xd1\xa4\xe5\x7c"
      "\x7e\x3f\xec\x97\x9b\x27\x6d\x66\xb7\xd3\x3b\x43\x77\xda\x89\x44\x2b\x55"
      "\x24\x16\xfb\x8e\xbe\x97\xf6\x8f\x79\xf2\xf5\xbf\xfc\xe9\xa3\x1b\x9d\xdb"
      "\x37\x6f\x5c\xdf\x38\xf7\x4c\xa6\xf4\x34\x3d\x7c\x4c\x2c\xf5\x45\xe2\xe5"
      "\x11\x22\xf1\xd3\x23\x1b\x89\xf6\x98\xfb\xcf\x57\x91\x38\xbd\xb3\xbe\x9c"
      "\x1f\xe6\x27\x39\x97\x33\x79\x2b\xeb\x59\xcb\xcf\xb2\x92\xcd\xac\xa6\x68"
      "\x66\xba\xd2\x1c\xcf\xe5\xeb\xdc\xfe\x91\xba\xf2\xc0\xda\x5b\x8f\x1a\xc9"
      "\x54\xf3\xbe\xd4\x57\xd1\x51\xc6\x74\x26\x3f\xa8\x72\x2b\x79\xad\xaa\xfb"
      "\x5c\xd6\x52\xe4\x9d\x5c\xcb\x6a\xde\xa8\xfe\x2d\x66\x21\xdf\xce\xc5\x5c"
      "\xcc\xa5\xbe\x77\xf8\xf4\xd0\x71\x57\x73\xab\xce\xfa\xd6\x78\x67\xfd\xd9"
      "\x6f\x34\x99\xd9\x24\x7f\x68\xd2\xc3\x56\x5f\x14\xca\xb8\x9e\xec\x8b\x6b"
      "\xff\x35\x77\xae\x2a\xeb\xdf\xb2\x1b\xa5\x17\xf6\x7f\x77\x07\xaf\x8d\x8f"
      "\x9e\x72\xfb\x2b\x4d\xa6\xec\xe3\x37\x4d\x7a\x34\x3c\x1c\x89\x85\xbe\x48"
      "\xbc\xb8\x7f\x24\xfe\xd8\x2d\x5f\x37\x3a\xb7\x6f\xae\xdf\x58\x79\x77\xcf"
      "\x4f\xd7\xc9\x26\xed\x76\xbb\xdd\x3a\xf7\x7a\xb3\xa5\x3c\xe2\x7e\xf7\xe8"
      "\xaf\x3f\x7f\x3d\xc8\xdc\x46\xd0\x77\x1d\x2b\x8f\x97\x17\x32\xd3\x5c\x49"
      "\x4e\x66\xb2\x1e\xda\x44\xaf\xec\xc5\x9d\xab\xcc\x83\xf1\x9a\x6a\xfe\xe2"
      "\x52\x97\xb5\x06\xca\x4e\xd7\x6f\x77\xd1\x3b\x53\x7f\x34\xf4\x4c\x9d\x6a"
      "\xbe\xc3\x0d\xb6\xb4\x58\x95\xbd\xbc\x67\xd9\x52\x55\xf6\x4a\x5f\x59\xff"
      "\xf7\xad\xe4\x9d\x74\x76\xbe\x0f\x01\x70\x84\x9d\xf8\xe6\x89\xa9\xd9\xff"
      "\xcc\xfe\x7d\xf6\x93\xd9\xdf\xce\xde\x98\x7d\x73\xe6\xfb\xd3\xdf\x99\x7e"
      "\x75\x2a\x93\x7f\x9b\xfc\x6e\x7b\x7e\xe2\xf5\xd6\xab\xc5\x9f\xf3\x49\x7e"
      "\xb9\x7b\xff\x0f\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"
      "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x3c\xbe\x8d\xf7\x3f\xb8\xb9\xd2\xe9"
      "\xac\xae\xef\x9d\x69\x0d\x2f\xda\x2f\xd3\x7b\x92\xcf\xe8\xb5\x8a\xe6\x49"
      "\x3a\xe3\xf7\x75\x58\x99\xee\x87\xc9\x11\x18\xc6\xfa\xc6\xd7\x92\x3c\x85"
      "\x96\x8b\xa7\x38\xe6\xde\x43\x04\xc7\xab\x35\x3f\x78\x44\x5d\x39\xcc\xc8"
      "\xff\x6b\xdc\x5a\x99\x19\x6b\xca\xdd\xe7\xeb\x28\x8d\xd5\xc5\x5c\x5d\x67"
      "\x84\x9d\xa7\xeb\x60\x4e\x0c\x39\x4f\x9b\xb7\xe8\x71\x1e\x2e\x0a\x1c\x0b"
      "\x17\x36\x6f\xbd\x7b\x61\xe3\xfd\x0f\xbe\xb5\x76\x6b\xe5\xed\xd5\xb7\x57"
      "\x6f\x4f\x5e\xbc\x78\x69\xfe\xd2\xc5\x37\x96\x2e\x5c\x5f\xeb\xac\xce\xd7"
      "\xaf\x87\x3d\x4a\xe0\x69\xd8\xfd\xd0\x3f\xec\x91\x00\x00\x00\x00\x00\x00"
      "\x00\x00\x00\x00\xa3\x7a\x16\xff\x5f\xe2\xb0\xe7\x08\x00\x00\x00\x00\x00"
      "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"
      "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"
      "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"
      "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"
      "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"
      "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"
      "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"
      "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"
      "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"
      "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"
      "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"
      "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"
      "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"
      "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"
      "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"
      "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"
      "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"
      "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"
      "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"
      "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x1c\x6f"
      "\xcb\xe7\xd2\xbe\x97\x22\x0b\xf3\xe7\xe7\xcb\xf5\xed\xbb\x4b\x9d\x72\xe9"
      "\xe5\x77\xf7\x6c\x27\x69\xb5\x92\xe2\x17\x49\xf1\x69\x72\x25\xf5\x92\xb9"
      "\xbe\xe6\x8a\x61\xfd\x7c\xbc\x76\x69\x22\xc9\x67\xbb\x6d\xb5\x7b\xfb\xb7"
      "\xf6\xa9\xd7\x9d\x1e\x69\x16\x77\x9a\x25\x67\x92\x4c\x34\xe9\x01\x3c\xd0"
      "\xde\xd5\x03\xb7\x57\xec\xcc\xb0\x0c\xd8\xd9\x5e\xe0\xe0\xb0\xfd\x3f\x00"
      "\x00\xff\xff\xba\xd2\xf2\x41",
      1699);
  syz_mount_image(/*fs=*/0x20000140, /*dir=*/0x20000040,
                  /*flags=MS_POSIXACL|MS_REC|MS_SYNCHRONOUS|MS_NOEXEC*/ 0x14018,
                  /*opts=*/0x20000080, /*chdir=*/1, /*size=*/0x6a3,
                  /*img=*/0x20000380);
  syscall(__NR_getpid);
  memcpy((void*)0x20000280, "/dev/loop#\000", 11);
  res = -1;
  res = syz_open_dev(/*dev=*/0x20000280, /*id=*/0, /*flags=*/0);
  if (res != -1)
    r[0] = res;
  memcpy((void*)0x20000080, "cgroup.stat\000", 12);
  res = syscall(__NR_openat, /*fd=*/0xffffff9c, /*file=*/0x20000080ul,
                /*flags=*/0x275a, /*mode=*/0);
  if (res != -1)
    r[1] = res;
  *(uint32_t*)0x200002c0 = r[1];
  *(uint32_t*)0x200002c4 = 0;
  *(uint64_t*)0x200002c8 = 0x2a00;
  *(uint64_t*)0x200002d0 = 0x80010000;
  *(uint64_t*)0x200002d8 = 0;
  *(uint64_t*)0x200002e0 = 0;
  *(uint64_t*)0x200002e8 = 0;
  *(uint32_t*)0x200002f0 = 0;
  *(uint32_t*)0x200002f4 = 0;
  *(uint32_t*)0x200002f8 = 8;
  *(uint32_t*)0x200002fc = 0x1c;
  memcpy((void*)0x20000300,
         "\xfe\xe8\xa2\xab\x78\xfc\x17\x9f\xd1\xf8\xa0\xe9\x1d\xda\xac\xa7\xbd"
         "\x64\xc6\xa4\xb4\xe0\x0d\x96\x83\xdd\xa1\xaf\x1e\xa8\x9d\xe2\xb7\xfb"
         "\x0a\x01\x00\x00\x00\x00\x00\x00\x00\x00\x03\x00\x00\x00\x00\x00\x00"
         "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00",
         64);
  memcpy((void*)0x20000340,
         "\x28\x09\xe8\xdb\xe1\x08\x59\x89\x48\x22\x4a\xd5\x4a\xfa\xc1\x1d\x87"
         "\x53\x97\xbd\xb2\x2d\x00\x00\xb4\x20\xa1\xa9\x3c\x52\x40\xf4\x5f\x81"
         "\x9e\x01\x17\x7d\x3d\x45\x8d\xd4\x99\x28\x61\xac\x00\x00\x00\x00\x00"
         "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00",
         64);
  memcpy((void*)0x20000380,
         "\x90\xbe\x8b\x1c\x55\x12\x65\x40\x6c\x7f\x30\x60\x03\xd8\xa0\xf4\xbd"
         "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00",
         32);
  *(uint64_t*)0x200003a0 = 0;
  *(uint64_t*)0x200003a8 = 0;
  memset((void*)0x200003b0, 0, 64);
  syscall(__NR_ioctl, /*fd=*/r[0], /*cmd=*/0x4c0a, /*arg=*/0x200002c0ul);
  *(uint64_t*)0x20001300 = 0;
  *(uint64_t*)0x20001308 = 0;
  *(uint64_t*)0x20001310 = 0;
  *(uint64_t*)0x20001318 = 5;
  *(uint64_t*)0x20001320 = 0x7ff;
  *(uint32_t*)0x20001328 = 0;
  *(uint32_t*)0x2000132c = 0;
  *(uint32_t*)0x20001330 = 0;
  *(uint32_t*)0x20001334 = 0x1a;
  memcpy((void*)0x20001338,
         "\xeb\xbf\x52\x74\x8b\x0b\x2a\xff\x33\x0c\x21\x36\x7b\x06\x62\x4f\xde"
         "\x30\x88\x7f\xda\x17\x2e\xe2\x51\xca\x9d\xea\x03\xca\x09\x48\x80\x22"
         "\x55\x71\x64\x5c\xc0\x27\xcd\x4c\x7a\x81\x38\x8e\x08\xc0\xa4\xa8\x77"
         "\x31\x91\x41\x08\x48\xa8\x94\x10\xf3\x19\xbd\xe2\xed",
         64);
  memcpy((void*)0x20001378,
         "\xc1\x10\x30\xf6\x99\x1c\x26\xeb\x66\x7e\x4d\x7f\xba\xeb\x5b\xe6\xd9"
         "\x58\xdf\x81\x11\x7f\x6c\xfe\x5e\x5b\xf3\xdc\x30\xbb\xfb\x71\x6d\x91"
         "\xad\x12\xa0\xc2\xb9\xcf\x2e\x15\xd1\x61\xe0\x1f\x42\x52\x60\xd3\x1c"
         "\x04\x07\x8e\x90\x7e\x24\x74\xfe\x96\x72\x9f\xd2\xa6",
         64);
  memcpy((void*)0x200013b8,
         "\x0a\x8c\x8c\x37\xab\x0b\xce\xea\xe8\x3d\x73\xe2\x57\x04\x18\x4a\x6e"
         "\x89\x56\x2a\x71\x7a\x4a\xf0\x45\x7d\x7c\x04\x96\x06\x9c\x89",
         32);
  *(uint64_t*)0x200013d8 = 0;
  *(uint64_t*)0x200013e0 = 0;
  syscall(__NR_ioctl, /*fd=*/r[0], /*cmd=*/0x4c04, /*arg=*/0x20001300ul);
}
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;
}