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

#define _GNU_SOURCE

#include <dirent.h>
#include <endian.h>
#include <errno.h>
#include <fcntl.h>
#include <signal.h>
#include <stdarg.h>
#include <stdbool.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.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>

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

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 setup_test()
{
  prctl(PR_SET_PDEATHSIG, SIGKILL, 0, 0, 0);
  setpgrp();
  write_file("/proc/self/oom_score_adj", "1000");
}

static void execute_one(void);

#define WAIT_FLAGS __WALL

static void loop(void)
{
  int iter = 0;
  for (;; iter++) {
    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[3] = {0xffffffffffffffff, 0xffffffffffffffff, 0xffffffffffffffff};

void execute_one(void)
{
  intptr_t res = 0;
  if (write(1, "executing program\n", sizeof("executing program\n") - 1)) {
  }
  memcpy((void*)0x200000000140, "/dev/loop#\000", 11);
  res = -1;
  res = syz_open_dev(
      /*dev=*/0x200000000140, /*id=*/0x75f,
      /*flags=O_TRUNC|O_NOFOLLOW|O_NOCTTY|O_LARGEFILE|O_EXCL|FASYNC|0x2*/
      0x2a382);
  if (res != -1)
    r[0] = res;
  memcpy(
      (void*)0x200000000880,
      "C\023\3742\225WD\252\272^\220\375\215\302\261["
      "\201\332\332\326\214\3119\354\016*||\344\263\304\266\v\252\025\206,"
      "\254\215\211cu\020\334\223\233\264\223\257E*:\344\335\245\2475\270\036;"
      "7\267.V\334rw[\r\230\223j\234\366\370\231\357F_\315\337!"
      "b\305\354\ntb\377\b\252F?!"
      "\237\a\032\003\f\3514\035eU\006zS\3110\271voI\245/"
      "\264\247@\241\\B\302@\r_b\232\353\b\201\000V\326/"
      "N\305\306f\261\225Z\345w^"
      "\330\347J\200\367\256\257uv\204\236G\321\347\233\360_"
      "9\302\233\375\303\363\344\225P\361m\317\302\341\346\246\214\021\373\270S"
      "\213\222\\\asW-Ee\002\000\000\000\320;Q\301~"
      "\211\354\310\233\210\a\362\223\202("
      "\213\000\330\264T\200\225\223\2345\317\t\004\000\000\000\000\000\000v"
      "\357\356+\253\234\000^R\262n?i="
      "\276\026\212\277\343\315B\355\3414\350\320\267\377\376Q\034\205n8\033"
      "\301\b\000\000\000\000\000\000\000\027\224\337W<"
      "GE\361\351\361q\214\360\256\230\214\340\301g}\256W\252\241\220\214\n$"
      "\246\273\020\257\307~\021\003<v\351\307K\366]\021)u\323\025\001}\3425$"
      "\260\206v\200\r\234\270\346\323(\240G2s\251&\263QU~u\023\005kKp\246&"
      "\216u\035\262\251!\311\372\320dG5\313f<}r\253\234\331f6iN\252>"
      "\222z\276\262R)\361K\327\257\231\366d\350\354\267\275+T3\246\251\372Y-"
      "1qs\202\357n*\226\311\036\364\321\002Dt\300\031\367\211\226.D "
      "[F\356YW\225\023\307;\224\023^"
      "\023\257\360C\234\253f\035aCS2\002\260\357\307\214\236\355\a\n<"
      "V\252\277Z1\2502\205\231\016$"
      "U\264X\307\372\f\b\217\304\276It\344\3051\272\271H\350\226\224\327\334"
      "\201\0211\t\257l\227\330T\3240\220ON\252FY\264\263\364\370JT\305:"
      "\305\aGc\265\022\220\177\000\221\316@\345\323A\314\325|"
      "\237\2165\0042\232\301\241\a\267\365\274,\321\323k8\305",
      557);
  res = syscall(__NR_memfd_create, /*name=*/0x200000000880ul,
                /*flags=MFD_CLOEXEC*/ 1ul);
  if (res != -1)
    r[1] = res;
  syscall(__NR_ioctl, /*fd=*/r[0], /*cmd=*/0x4c00, /*arg=*/r[1]);
  memcpy((void*)0x200000000140, "/dev/loop#\000", 11);
  res = -1;
  res = syz_open_dev(
      /*dev=*/0x200000000140, /*id=*/0x75f,
      /*flags=O_TRUNC|O_NOFOLLOW|O_NOCTTY|O_LARGEFILE|O_EXCL|FASYNC|0x2*/
      0x2a382);
  if (res != -1)
    r[2] = res;
  *(uint32_t*)0x200000000200 = 0;
  *(uint16_t*)0x200000000208 = 0;
  *(uint64_t*)0x200000000210 = 0;
  *(uint16_t*)0x200000000218 = 0;
  *(uint32_t*)0x200000000220 = 1;
  *(uint32_t*)0x200000000224 = 5;
  *(uint32_t*)0x200000000228 = 0x17;
  *(uint32_t*)0x20000000022c = 0x11;
  memcpy((void*)0x200000000230,
         "\xee\xdd\xb7\x21\x48\x6a\x3a\x32\xfe\xca\x34\x4d\x3e\x01\x57\xce\xd0"
         "\x61\x61\x24\x6c\xd5\xc6\xa3\xe9\x97\x79\x56\xbe\x87\x17\x6a\x30\xa9"
         "\x00\x00\x00\xb7\x00\x00\xab\x80\x00\x00\x09\xe8\x00\x00\x00\x00\x00"
         "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00",
         64);
  memcpy((void*)0x200000000270,
         "\xa8\x63\xa5\x17\x0a\x11\xd2\x6a\x92\x0c\xb3\xd1\xe9\xfb\x18\xcb\x0e"
         "\x58\x04\x00\x58\xe8\x91\xbb\xe5\xe4\x23\x0d\xe8\x4e\xea\x7a",
         32);
  *(uint64_t*)0x200000000290 = 1;
  *(uint64_t*)0x200000000298 = 0x400;
  *(uint32_t*)0x2000000002a0 = 0;
  syscall(__NR_ioctl, /*fd=*/r[2], /*cmd=*/0x4c02, /*arg=*/0x200000000200ul);
}
int main(void)
{
  syscall(__NR_mmap, /*addr=*/0x1ffffffff000ul, /*len=*/0x1000ul, /*prot=*/0ul,
          /*flags=MAP_FIXED|MAP_ANONYMOUS|MAP_PRIVATE*/ 0x32ul, /*fd=*/-1,
          /*offset=*/0ul);
  syscall(__NR_mmap, /*addr=*/0x200000000000ul, /*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=*/0x200001000000ul, /*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) {
      loop();
    }
  }
  sleep(1000000);
  return 0;
}