// https://syzkaller.appspot.com/bug?id=be49c2247d07ad4a4faa018ebea0432f6f0c1a82
// 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_bpf
#define __NR_bpf 386
#endif
#ifndef __NR_mmap
#define __NR_mmap 192
#endif
#undef __NR_mmap
#define __NR_mmap __NR_mmap2

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 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 (;;) {
      if (waitpid(-1, &status, WNOHANG | WAIT_FLAGS) == pid)
        break;
      sleep_ms(1);
      if (current_time_ms() - start < 15000)
        continue;
      kill_and_wait(pid, &status);
      break;
    }
  }
}

void execute_one(void)
{
  *(uint32_t*)0x2000e000 = 0xe;
  *(uint32_t*)0x2000e004 = 4;
  *(uint64_t*)0x2000e008 = 0x20000940;
  memcpy(
      (void*)0x20000940,
      "\xb4\x05\x00\x00\x20\x00\x80\x00\x61\x10\x00\x00\x00\x00\x00\x00\xc6\x00"
      "\x00\x00\x00\x00\x00\x00\x95\x00\xd8\x00\x00\x00\x00\x00\x9f\x33\xef\x60"
      "\x91\x6e\x55\x89\x3f\x1e\xeb\x0b\xe2\x56\x6c\x43\xd7\x29\x18\xa8\xa9\x32"
      "\x3f\xd0\x72\x30\x43\xc4\x7c\x89\x6c\xe0\xbc\xe6\x6a\x24\x5a\xd9\x9b\x81"
      "\x7f\xd9\x8c\xd8\x24\x49\x89\x49\x71\x4f\xfa\xac\x8a\x6f\x77\xef\x26\xdc"
      "\xca\x5d\x82\x05\x4d\x54\xd5\x3c\xd2\xb6\xdb\x71\x4e\x4b\x94\xbd\xae\x21"
      "\x4f\xa6\x8a\x05\x57\xeb\x2c\x5c\xa6\x83\xa4\xb6\xfc\xfc\xff\xff\xff\xff"
      "\xff\xff\xff\xd4\x42\x01\x7c\xfa\x6f\xa2\x6f\xa7\xa3\x47\xc7\xfa\xa8\xe7"
      "\x00\x45\x8c\x60\x89\x7d\x4a\x61\x48\xa1\xc1\x14\x28\x42\x7c\x40\xde\x60"
      "\xbe\xac\x67\x1e\x8e\x8f\xde\xcb\x03\x58\x68\xa6\x23\xfa\x71\xf8\x71\xab"
      "\x5c\x2f\xf8\x8a\x02\x08\x4e\x5b\x52\x71\xe4\x5f\x00\x00\x38\x26\xfb\x85"
      "\x79\xc1\xfb\x01\xd2\xc5\x55\x3d\x2c\xcb\x5f\xc5\xb5\x1f\xe6\xb1\x74\xeb"
      "\xd9\x90\x7d\xcf\xf4\x14\xed\x55\xb0\xc2\x0c\xdb\xe7\x00\x9a\x6f\xe7\xcc"
      "\x78\x76\x2f\x1d\x4d\xcd\xbc\xa6\x49\x20\xdb\x9a\x50\xf8\x6c\x21\x63\x2f"
      "\x7a\x00\x04\x00\x00\xbd\x74\xff\x05\xd3\x7e\xf6\x8e\x3b\x9d\xb8\x63\xc7"
      "\x58\xff\xff\xff\xff\xab\xe9\x0a\xc5\xd0\x8d\xd9\xd4\xe0\x35\x9c\x41\xcf"
      "\x36\x26\xe1\x23\x0b\xc1\xcd\x4c\x02\xc4\x60\xce\xb4\x42\x76\xe9\xbd\x94"
      "\xd1\xc2\xe6\xd1\x7d\xc5\xc2\xed\xf3\x32\xa6\x2f\x5f\xe6\x8f\xbb\xbb\xfc"
      "\xfd\x00\x00\x00\x00\x00\x0f\xbf\x94\x0e\x66\x52\xd3\x57\x47\x4e\xd5\xf8"
      "\x16\xf6\x6a\xc3\x02\x74\x60\xae\x99\x1e\x7f\x83\xcd\xd7\xa7\xeb\x2a\x70"
      "\x03\xd1\xa6\xcf\x54\x78\x53\x35\x84\x96\x1c\x32\x9f\xcf\x5a\x43\xe0\x5c"
      "\x92\xbf\xef\x8a\x4f\xed\x5c\x94\x55\x64\x0d\xcd\x28\x00\x00\x00\x00\x3f"
      "\x29\x15\xa3\x03\x9c\x9a\x78\xf6\x3b\x8e\xc7\xe6\x0a\x00\x00\xfe\xd7\xd6"
      "\x7c\x44\x0e\x23\xd1\x30\xe5\x1e\xea\x1e\x08\x5b\xeb\xab\xe7\x05\x9d\xe9"
      "\xcb\xfc\x51\x17\xc0\x24\x18\x5a\x06\x2a\xcb\x6b\x8e\xec\x31\xc2\x1b\x3a"
      "\xf8\xb9\xee\xdb\x46\x60\xed\x2d\xeb\x7a\xcf\x2a\x33\xa3\x76\xa5\xcb\x7d"
      "\x42\x66\xd5\xb0\xbe\x14\x48\x8d\x14\xb4\x73\x50\x24\x86\xad\x8d\xd6\x00"
      "\x00\x00\x00\x00\x00\x00\x00\x00\x00\xc7\x76\x6e\xa7\xc5\x81\x78\x2c\x0d"
      "\x90\xf4\x2a\x85\x30\x38\x35\xfc\x29\x1c\x25\xd2\x9e\x6b\xea\xd5\xd7\x36"
      "\x0f\x2e\x19\x29\xd7\x6e\xbc\x85\x58\xc4\x50\x64\x07\xd3\x04\x60\x22\xbd"
      "\xf2\x54\x85\xbd\x54\x42\x16\x9e\x9b\x4c\x12\x78\x34\x35\x81\xb7\xa0\x6f"
      "\x65\xe8\xea\x6b\x04\x2c\x4f\xd0\x83\x81\xe5\x00\x00\x00\x00\x00\x00\x00"
      "\x63\x98\xd6\xe2\x57\x34\x3c\x1a\x72\x3b\x91\x03\x00\x00\x00\x64\x80\x30"
      "\x4c\x66\xb2\x17\xae\xa0\x15\x6c\xe9\xee\xf9\x11\xfe\x5b\x73\x70\xf7\x99"
      "\x87\x30\x3e\xcb\x3a\xab\xba\x3c\x60\x01\x4a\x01\x01\xab\x76\x67\x54\xf5"
      "\x96\xb4\x1d\xa9\x53\x4d\x12\xb8\x30\x6a\x1b\x36\xcf\x3b\x03\xf0\xd7\x90"
      "\x87\x9f\x52\x3e\xab\xfb\xee\x83\xd8\xbd\x47\x2e\xf6\x96\x60\xcf\x6e\xc8"
      "\x97\x10\x6c\x51\xe5\x4a\x17\x49\x7f\x38\x78\xb1\xe1\x13\x16\xd8\xdd\xae"
      "\x1c\x6c\x3b\x8f\xaa\xf7\xa9\xfc\xaf\x8f\x5d\x61\x86\xc4\x25\x42\xd6\x8b"
      "\xa7\x26\x82\xc9\x38\xd3\xc0\xa2\xe6\xe1\x0e\xed\x71\xb1\xd3\x1c\x9f\x30"
      "\x0b\x41\x74\x53\x29\xbf\x34\x49\x5c\x63\xe4\x3f\xb8\x96\xe4\x90\x3f\xb0"
      "\xfa\xe5\x4a\x8f\x0f\xe3\xb4\x8a\x5b\x29\xd2\x79\x07\x06\x47\xe6\x50\x97"
      "\xc8\xec\xf3\x2a\x15\x08\x00\x00\x00\x00\x00\x00\x00\x90\xee\x7b\xa4\xa7"
      "\x0a\x08\x4b\xd9\x94\xac\x5e\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"
      "\x00\x00\x35\x1a\x30\xcd\xf5\x7a\x3d\x72\x63\x1d\x0f\xe9\x2e\xfa\x97\x4a"
      "\x53\xf4\xdc\x1e\xb9\xa8\x6d\xf6\x32\xa6\xd4\x63\x68\x81\x23\xf6\x4d\x42"
      "\xa9\x19\xbc\xfc\x44\xa9\x0f\xfd\x68\xa3\x4e\x28\x69\x91\xf8\x42\xa9\x1c"
      "\x97\x7f\x60\x75\xd0\x7e\x39\xe6\x69\xb0\x71\x3a\xf0\x49\x8a\x99\xbf\x52"
      "\x61\xcb\x32\x69\xd4\x99\xa5\x20\x2d\x7a\x08\xb3\x3a\xde\x7b\x38\x82\x9b"
      "\x9b\xd3\x96\x19\x68\x8d\x5e\x9a\xf2\x21\x70\xef\x83\xe5\xb9\x2c\xbb\x32"
      "\xb6\x55\xc4\x5d\xe1\xc1\x54\xaa\xd8\x1b\xf6\x43\x3f\x76\xd5\xaf\x45\x15"
      "\x55\x36\xa1\xa4\x4b\xfc\xbf\xbf\xd2\x32\xaf\x00\x00\x52\xf9\x9b\xe1\x31"
      "\x56\xf7\xd6\x04\xd9\x33\xe5\x93\x98\x5a\x6f\x9a\x00",
      949);
  *(uint64_t*)0x2000e010 = 0x20003ff6;
  memcpy((void*)0x20003ff6, "GPL\000", 4);
  *(uint32_t*)0x2000e018 = 4;
  *(uint32_t*)0x2000e01c = 0xfd90;
  *(uint64_t*)0x2000e020 = 0x2000cf3d;
  *(uint32_t*)0x2000e028 = 0;
  *(uint32_t*)0x2000e02c = 0;
  memset((void*)0x2000e030, 0, 16);
  *(uint32_t*)0x2000e040 = 0;
  *(uint32_t*)0x2000e044 = 0;
  *(uint32_t*)0x2000e048 = -1;
  *(uint32_t*)0x2000e04c = 8;
  *(uint64_t*)0x2000e050 = 0x20000000;
  *(uint32_t*)0x20000000 = 0;
  *(uint32_t*)0x20000004 = 0;
  *(uint32_t*)0x2000e058 = 0x366;
  *(uint32_t*)0x2000e05c = 0x10;
  *(uint64_t*)0x2000e060 = 0x20000000;
  *(uint32_t*)0x20000000 = 0;
  *(uint32_t*)0x20000004 = 0;
  *(uint32_t*)0x20000008 = 0;
  *(uint32_t*)0x2000000c = 0;
  *(uint32_t*)0x2000e068 = 0x1dd;
  *(uint32_t*)0x2000e06c = 0;
  *(uint32_t*)0x2000e070 = -1;
  *(uint32_t*)0x2000e074 = 0;
  *(uint64_t*)0x2000e078 = 0;
  *(uint64_t*)0x2000e080 = 0;
  *(uint32_t*)0x2000e088 = 0x10;
  *(uint32_t*)0x2000e08c = 0;
  syscall(__NR_bpf, /*cmd=*/5, /*arg=*/0x2000e000, /*size=*/0x48);
}
int main(void)
{
  syscall(__NR_mmap, /*addr=*/0x1ffff000, /*len=*/0x1000, /*prot=*/0,
          /*flags=MAP_FIXED|MAP_ANONYMOUS|MAP_PRIVATE*/ 0x32, /*fd=*/-1,
          /*offset=*/0);
  syscall(__NR_mmap, /*addr=*/0x20000000, /*len=*/0x1000000,
          /*prot=PROT_WRITE|PROT_READ|PROT_EXEC*/ 7,
          /*flags=MAP_FIXED|MAP_ANONYMOUS|MAP_PRIVATE*/ 0x32, /*fd=*/-1,
          /*offset=*/0);
  syscall(__NR_mmap, /*addr=*/0x21000000, /*len=*/0x1000, /*prot=*/0,
          /*flags=MAP_FIXED|MAP_ANONYMOUS|MAP_PRIVATE*/ 0x32, /*fd=*/-1,
          /*offset=*/0);
  loop();
  return 0;
}