// 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*)0x202a0fb8 = 3;
  *(uint32_t*)0x202a0fbc = 4;
  *(uint64_t*)0x202a0fc0 = 0x200004c0;
  memcpy(
      (void*)0x200004c0,
      "\x85\x00\x00\x00\x4f\x00\x00\x00\x35\x00\x00\x00\x00\x00\x00\x00\x85\x00"
      "\x00\x00\x05\x00\x00\x00\x95\x00\x00\x00\x00\x00\x00\x00\xf4\x67\x08\x80"
      "\x27\x1e\x35\x5a\x93\x26\x82\x03\x20\x0f\xfa\x95\xa2\xc8\xc0\x37\xc5\xa1"
      "\x42\xdf\xa8\xba\x62\x87\x06\x6c\x51\x97\xfa\xbd\x5f\x70\x10\xe8\x1a\xe0"
      "\xb7\x37\x12\x6e\xa6\xf7\xdc\x39\xcd\x34\x01\x01\x00\x00\x00\x00\x00\x00"
      "\xe2\x2f\xf5\xdd\xe5\x47\x04\xd2\x5c\x79\x94\x9c\x23\xe2\xeb\x15\x55\xa2"
      "\x35\x0e\xa7\xc0\x9c\xc2\x8d\xe1\x94\xf4\x48\x00\x00\x00\x00\xb0\xd3\x71"
      "\x2c\x7e\x93\x36\x3a\xf3\xc0\x75\xff\x1e\x23\x16\x01\x04\xd9\x54\x33\xb3"
      "\xd6\x34\x06\x5b\xcc\xba\xa2\xbb\x75\x5a\xf3\xd5\x76\x09\x0c\x48\x67\xa7"
      "\xb6\x39\x3e\x36\x6c\x63\x9e\xd5\xec\x72\x09\xd0\x31\xf4\x0f\x30\x03\x00"
      "\x6b\x6e\x51\xa7\xf5\x50\xaf\xc8\x52\x00\x3b\xad\x07\x42\xc6\x2f\x78\x46"
      "\xc7\x44\xae\x6a\xf3\xc0\x41\x43\xcc\xa8\xd9\x5c\x2c\x50\x5d\x5e\x37\x10"
      "\x21\x24\xd8\x5c\xec\x07\xad\x3f\x9a\x0f\x25\x8b\x09\x76\x64\xed\x72\x18"
      "\x8e\x09\x6d\x4c\x69\x49\xe1\xd7\x6d\x06\x7a\x99\x2d\x09\x6a\xce\x8f\x3b"
      "\x68\x2c\x97\x00\x02\x47\xfe\x5f\x17\xfd\x08\x00\x00\x00\x00\x00\x00\x00"
      "\x7f\x47\xef\x6d\x02\xba\x53\x6c\xda\xce\xcf\x7e\xb6\xba\xaa\x4a\x97\x79"
      "\xf8\x55\x5e\xae\xa7\x68\xc1\xf2\xc2\x21\xc1\x10\xef\x05\x00\x00\x00\x0e"
      "\xe2\x82\xab\x76\xf5\x93\xd9\x28\xcf\x95\x84\x01\x00\x27\x7c\x04\xb8\xc5"
      "\x32\x48\x12\x69\x6a\x62\xd9\x92\xa4\xf8\xdc\x8d\xcb\xa0\x0b\x1b\x2d\x25"
      "\x47\xc4\x09\x00\x52\x08\x7b\x5e\xfa\xbf\x84\x96\xb9\xa9\x51\x66\xbd\x00"
      "\x8e\xa0\x32\x7b\x56\xc0\xeb\xfb\x19\xa3\x42\x68\x33\x56\x48\xe1\xf8\x44"
      "\xce\x32\x8c\xdc\xa5\x2f\xb9\x8c\x14\x52\xb6\x51\xeb\xf9\x42\xf7\x29\x7f"
      "\x7b\x27\x44\x41\x9a\x2f\x23\x8f\x17\x3d\x0c\xd4\x6d\xaf\xc6\xe9\x55\x00"
      "\xf5\x3e\x53\x5d\xcd\x91\xd8\x3c\xf4\xfb\xd7\x75\xd9\xc0\x7d\x59\x10\x19"
      "\x49\xf8\x98\x2b\x6c\xe6\x29\xc0\xb2\x00\x00\x00\x00\x00\x00\x00\x00\xcc"
      "\xb6\x04\x87\x76\x25\x40\x67\x90\x74\x9e\x1a\xec\x3b\xf9\xf1\x04\xa0\x6c"
      "\x1e\x9c\x70\xc3\xcb\x5a\xd2\xe3\xc4\xaa\x11\xce\x98\x6e\x52\x16\x64\x68"
      "\x0b\x25\x7b\xb1\x38\xae\xf7\xc0\x8d\x3b\xed\x6a\xc5\xb0\xb2\x09\xa5\xfb"
      "\x71\xfd\x84\xb8\x06\xed\xf5\x96\xf3\x13\x11\x24\xe7\x24\x0a\x5b\xba\x1c"
      "\x07\x83\x7c\x4b\x87\x18\x59\x66\x5f\x2f\x1d\xa9\xa3\x5f\xed\xd7\xf7\x42"
      "\xce\x81\x89\xfc\xb7\xfc\xef\x31\x5c\xa9\xc7\x5e\xa2\x7e\x8b\xe8\xd1\xc0"
      "\x34\x95\x9d\x4c\x1e\xe6\x61\xe2\x14\xfd\xff\x62\x5f\xee\x83\x70\x94\x5f"
      "\x1a\x19\x56\xf2\x7f\x5d\xef\xe8\x32\xf8\x4d\xa8\x8f\xb2\xd7\x9f\xbc\xb7"
      "\xc4\x22\xba\x41\xae\x32\xab\x91\xec\x87\xde\xa5\x7f\x52\x2f\xdb\x4d\xfa"
      "\x72\x59\xfc\x60\xaa\xef\x47\x5d\xcc\x50\x27\xfb\xdb\xff\x20\x6c\xde\x62"
      "\xec\x2b\x72\x2d\xed\x82\xc6\x74\x19\x2d\xf1\xd2\xd0\x58\x2d\xde\x5b\x05"
      "\xdc\xd4\xb2\xb1\x96\x07\x99\x56\x3e\x2b\x63\x0e\x06\x2c\x0e\xbc\xcb\xa2"
      "\xc2\x0f\xa1\xb6\xd9\xf8\x2c\x27\x78\x29\xdd\x86\xe5\x60\xdf\xd3\x98\x8c"
      "\x11\x68\xd8\xd5\xfe\x51\xbd\xe1\x73\x57\xd1\x7e\x4f\x4c\x00\x00\x00\x00"
      "\x00\x00",
      704);
  *(uint64_t*)0x202a0fc8 = 0x20000140;
  memcpy((void*)0x20000140, "GPL\000", 4);
  *(uint32_t*)0x202a0fd0 = 0;
  *(uint32_t*)0x202a0fd4 = 0xe0;
  *(uint64_t*)0x202a0fd8 = 0x20000180;
  *(uint32_t*)0x202a0fe0 = 0;
  *(uint32_t*)0x202a0fe4 = 0;
  memset((void*)0x202a0fe8, 0, 16);
  *(uint32_t*)0x202a0ff8 = 0;
  *(uint32_t*)0x202a0ffc = 0;
  *(uint32_t*)0x202a1000 = -1;
  *(uint32_t*)0x202a1004 = 8;
  *(uint64_t*)0x202a1008 = 0;
  *(uint32_t*)0x202a1010 = 0;
  *(uint32_t*)0x202a1014 = 0x10;
  *(uint64_t*)0x202a1018 = 0;
  *(uint32_t*)0x202a1020 = 0xfffffc1a;
  *(uint32_t*)0x202a1024 = 0;
  *(uint32_t*)0x202a1028 = -1;
  *(uint32_t*)0x202a102c = 0;
  *(uint64_t*)0x202a1030 = 0;
  *(uint64_t*)0x202a1038 = 0;
  *(uint32_t*)0x202a1040 = 0x10;
  *(uint32_t*)0x202a1044 = 0;
  syscall(__NR_bpf, /*cmd=*/5, /*arg=*/0x202a0fb8, /*size=*/0x15);
}
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;
}