// https://syzkaller.appspot.com/bug?id=00fadf9a6326e13098672b601d57f644cae380e9
// 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>

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

#define BITMASK(bf_off, bf_len) (((1ull << (bf_len)) - 1) << (bf_off))
#define STORE_BY_BITMASK(type, htobe, addr, val, bf_off, bf_len)               \
  *(type*)(addr) =                                                             \
      htobe((htobe(*(type*)(addr)) & ~BITMASK((bf_off), (bf_len))) |           \
            (((type)(val) << (bf_off)) & BITMASK((bf_off), (bf_len))))

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_procfs(volatile long a0, volatile long a1)
{
  char buf[128];
  memset(buf, 0, sizeof(buf));
  if (a0 == 0) {
    snprintf(buf, sizeof(buf), "/proc/self/%s", (char*)a1);
  } else if (a0 == -1) {
    snprintf(buf, sizeof(buf), "/proc/thread-self/%s", (char*)a1);
  } else {
    snprintf(buf, sizeof(buf), "/proc/self/task/%d/%s", (int)a0, (char*)a1);
  }
  int fd = open(buf, O_RDWR);
  if (fd == -1)
    fd = open(buf, O_RDONLY);
  return fd;
}

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 < 5000)
        continue;
      kill_and_wait(pid, &status);
      break;
    }
  }
}

uint64_t r[5] = {0xffffffffffffffff, 0xffffffffffffffff, 0xffffffffffffffff,
                 0x0, 0xffffffffffffffff};

void execute_one(void)
{
  intptr_t res = 0;
  res = syscall(__NR_socket, 2ul, 2ul, 0);
  if (res != -1)
    r[0] = res;
  memcpy((void*)0x20000f00,
         "filter\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000"
         "\000\000\000\000\000\000\000\000\000\000\000",
         32);
  *(uint32_t*)0x20000f20 = 0xe;
  *(uint32_t*)0x20000f24 = 4;
  *(uint32_t*)0x20000f28 = 0x4d8;
  *(uint32_t*)0x20000f2c = -1;
  *(uint32_t*)0x20000f30 = 0;
  *(uint32_t*)0x20000f34 = 0x190;
  *(uint32_t*)0x20000f38 = 0;
  *(uint32_t*)0x20000f3c = -1;
  *(uint32_t*)0x20000f40 = -1;
  *(uint32_t*)0x20000f44 = 0x440;
  *(uint32_t*)0x20000f48 = 0x440;
  *(uint32_t*)0x20000f4c = 0x440;
  *(uint32_t*)0x20000f50 = -1;
  *(uint32_t*)0x20000f54 = 4;
  *(uint64_t*)0x20000f58 = 0;
  memset((void*)0x20000f60, 0, 84);
  *(uint32_t*)0x20000fb4 = 0;
  *(uint16_t*)0x20000fb8 = 0x168;
  *(uint16_t*)0x20000fba = 0x190;
  *(uint32_t*)0x20000fbc = 0;
  *(uint64_t*)0x20000fc0 = 0;
  *(uint64_t*)0x20000fc8 = 0;
  *(uint16_t*)0x20000fd0 = 0xf8;
  memcpy((void*)0x20000fd2,
         "recent\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000"
         "\000\000\000\000\000\000\000\000",
         29);
  *(uint8_t*)0x20000fef = 0;
  *(uint32_t*)0x20000ff0 = 0;
  *(uint32_t*)0x20000ff4 = 9;
  *(uint8_t*)0x20000ff8 = 4;
  *(uint8_t*)0x20000ff9 = 0;
  memcpy(
      (void*)0x20000ffa,
      "syz1\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000"
      "\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000"
      "\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000"
      "\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000"
      "\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000"
      "\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000"
      "\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000"
      "\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000"
      "\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000"
      "\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000"
      "\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000",
      200);
  *(uint8_t*)0x200010c2 = 0;
  *(uint16_t*)0x200010c8 = 0x28;
  memcpy((void*)0x200010ca,
         "REJECT\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000"
         "\000\000\000\000\000\000\000\000",
         29);
  *(uint8_t*)0x200010e7 = 0;
  *(uint32_t*)0x200010e8 = 0;
  memset((void*)0x200010f0, 0, 84);
  *(uint32_t*)0x20001144 = 0;
  *(uint16_t*)0x20001148 = 0x198;
  *(uint16_t*)0x2000114a = 0x1f8;
  *(uint32_t*)0x2000114c = 0;
  *(uint64_t*)0x20001150 = 0;
  *(uint64_t*)0x20001158 = 0;
  *(uint16_t*)0x20001160 = 0xf8;
  memcpy((void*)0x20001162,
         "recent\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000"
         "\000\000\000\000\000\000\000\000",
         29);
  *(uint8_t*)0x2000117f = 0;
  *(uint32_t*)0x20001180 = 0;
  *(uint32_t*)0x20001184 = 0;
  *(uint8_t*)0x20001188 = 2;
  *(uint8_t*)0x20001189 = 0;
  memcpy(
      (void*)0x2000118a,
      "syz1\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000"
      "\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000"
      "\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000"
      "\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000"
      "\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000"
      "\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000"
      "\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000"
      "\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000"
      "\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000"
      "\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000"
      "\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000",
      200);
  *(uint8_t*)0x20001252 = 0;
  *(uint16_t*)0x20001258 = 0x30;
  memcpy((void*)0x2000125a,
         "addrtype\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000"
         "\000\000\000\000\000\000",
         29);
  *(uint8_t*)0x20001277 = 0;
  *(uint16_t*)0x20001278 = 0;
  *(uint16_t*)0x2000127a = 0;
  *(uint32_t*)0x2000127c = 0;
  *(uint32_t*)0x20001280 = 0;
  *(uint16_t*)0x20001288 = 0x60;
  memcpy((void*)0x2000128a,
         "HMARK\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000"
         "\000\000\000\000\000\000\000\000",
         29);
  *(uint8_t*)0x200012a7 = 0;
  memset((void*)0x200012a8, 0, 10);
  memset((void*)0x200012b2, 255, 2);
  *(uint8_t*)0x200012b4 = 0xac;
  *(uint8_t*)0x200012b5 = 0x14;
  *(uint8_t*)0x200012b6 = 0x14;
  *(uint8_t*)0x200012b7 = 0;
  *(uint32_t*)0x200012b8 = htobe32(0);
  *(uint32_t*)0x200012bc = htobe32(0);
  *(uint32_t*)0x200012c0 = htobe32(0);
  *(uint32_t*)0x200012c4 = htobe32(0);
  *(uint16_t*)0x200012c8 = htobe16(0);
  *(uint16_t*)0x200012ca = htobe16(0);
  *(uint16_t*)0x200012cc = htobe16(0);
  *(uint16_t*)0x200012ce = htobe16(0);
  *(uint32_t*)0x200012d0 = 0;
  *(uint16_t*)0x200012d4 = 0;
  *(uint32_t*)0x200012d8 = 0;
  *(uint32_t*)0x200012dc = 7;
  *(uint32_t*)0x200012e0 = 0;
  memset((void*)0x200012e8, 0, 84);
  *(uint32_t*)0x2000133c = 0;
  *(uint16_t*)0x20001340 = 0x70;
  *(uint16_t*)0x20001342 = 0xb8;
  *(uint32_t*)0x20001344 = 0;
  *(uint64_t*)0x20001348 = 0;
  *(uint64_t*)0x20001350 = 0;
  *(uint16_t*)0x20001358 = 0x48;
  memcpy((void*)0x2000135a,
         "TEE\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000"
         "\000\000\000\000\000\000\000\000\000\000",
         29);
  *(uint8_t*)0x20001377 = 1;
  *(uint8_t*)0x20001378 = 0xfc;
  *(uint8_t*)0x20001379 = 0;
  memset((void*)0x2000137a, 0, 13);
  *(uint8_t*)0x20001387 = 0;
  memcpy((void*)0x20001388, "macvlan0\000\000\000\000\000\000\000\000", 16);
  *(uint64_t*)0x20001398 = 0;
  memset((void*)0x200013a0, 0, 84);
  *(uint32_t*)0x200013f4 = 0;
  *(uint16_t*)0x200013f8 = 0x70;
  *(uint16_t*)0x200013fa = 0x98;
  *(uint32_t*)0x200013fc = 0;
  *(uint64_t*)0x20001400 = 0;
  *(uint64_t*)0x20001408 = 0;
  *(uint16_t*)0x20001410 = 0x28;
  memset((void*)0x20001412, 0, 29);
  *(uint8_t*)0x2000142f = 0;
  *(uint32_t*)0x20001430 = 0xfffffffe;
  syscall(__NR_setsockopt, r[0], 0, 0x40, 0x20000f00ul, 0x538ul);
  memcpy((void*)0x20000100, "syscall\000", 8);
  syz_open_procfs(0, 0x20000100);
  res = syscall(__NR_socket, 0x10ul, 3ul, 0);
  if (res != -1)
    r[1] = res;
  res = syscall(__NR_socket, 1ul, 0x803ul, 0);
  if (res != -1)
    r[2] = res;
  *(uint32_t*)0x200002c0 = 0x14;
  res = syscall(__NR_getsockname, r[2], 0x20000100ul, 0x200002c0ul);
  if (res != -1)
    r[3] = *(uint32_t*)0x20000104;
  *(uint64_t*)0x20000080 = 0;
  *(uint32_t*)0x20000088 = 0;
  *(uint64_t*)0x20000090 = 0x20000000;
  *(uint64_t*)0x20000000 = 0x20000340;
  *(uint32_t*)0x20000340 = 0x88;
  *(uint16_t*)0x20000344 = 0x10;
  *(uint16_t*)0x20000346 = 0x401;
  *(uint32_t*)0x20000348 = 0x70bd28;
  *(uint32_t*)0x2000034c = 0x25dfdbfd;
  *(uint8_t*)0x20000350 = 0;
  *(uint8_t*)0x20000351 = 0;
  *(uint16_t*)0x20000352 = 0;
  *(uint32_t*)0x20000354 = r[3];
  *(uint32_t*)0x20000358 = 0;
  *(uint32_t*)0x2000035c = 0;
  *(uint16_t*)0x20000360 = 0x60;
  STORE_BY_BITMASK(uint16_t, , 0x20000362, 0x12, 0, 14);
  STORE_BY_BITMASK(uint16_t, , 0x20000363, 0, 6, 1);
  STORE_BY_BITMASK(uint16_t, , 0x20000363, 1, 7, 1);
  *(uint16_t*)0x20000364 = 0xb;
  *(uint16_t*)0x20000366 = 1;
  memcpy((void*)0x20000368, "gretap\000", 7);
  *(uint16_t*)0x20000370 = 0x50;
  STORE_BY_BITMASK(uint16_t, , 0x20000372, 2, 0, 14);
  STORE_BY_BITMASK(uint16_t, , 0x20000373, 0, 6, 1);
  STORE_BY_BITMASK(uint16_t, , 0x20000373, 1, 7, 1);
  *(uint16_t*)0x20000374 = 6;
  *(uint16_t*)0x20000376 = 0xf;
  *(uint16_t*)0x20000378 = 6;
  *(uint16_t*)0x2000037c = 8;
  *(uint16_t*)0x2000037e = 5;
  *(uint32_t*)0x20000380 = 0x80000000;
  *(uint16_t*)0x20000384 = 8;
  *(uint16_t*)0x20000386 = 5;
  *(uint32_t*)0x20000388 = 0xffff8001;
  *(uint16_t*)0x2000038c = 6;
  *(uint16_t*)0x2000038e = 0x11;
  *(uint16_t*)0x20000390 = htobe16(0x4e22);
  *(uint16_t*)0x20000394 = 6;
  *(uint16_t*)0x20000396 = 0x11;
  *(uint16_t*)0x20000398 = htobe16(0x4e24);
  *(uint16_t*)0x2000039c = 6;
  *(uint16_t*)0x2000039e = 0x18;
  *(uint16_t*)0x200003a0 = 0x82ac;
  *(uint16_t*)0x200003a4 = 8;
  *(uint16_t*)0x200003a6 = 0x14;
  *(uint32_t*)0x200003a8 = 0x3f;
  *(uint16_t*)0x200003ac = 4;
  *(uint16_t*)0x200003ae = 0x12;
  *(uint16_t*)0x200003b0 = 5;
  *(uint16_t*)0x200003b2 = 0xa;
  *(uint8_t*)0x200003b4 = 0;
  *(uint16_t*)0x200003b8 = 6;
  *(uint16_t*)0x200003ba = 0x11;
  *(uint16_t*)0x200003bc = htobe16(3);
  *(uint16_t*)0x200003c0 = 8;
  *(uint16_t*)0x200003c2 = 3;
  *(uint32_t*)0x200003c4 = r[3];
  *(uint64_t*)0x20000008 = 0x88;
  *(uint64_t*)0x20000098 = 1;
  *(uint64_t*)0x200000a0 = 0;
  *(uint64_t*)0x200000a8 = 0;
  *(uint32_t*)0x200000b0 = 0;
  syscall(__NR_sendmsg, r[1], 0x20000080ul, 0x4040000ul);
  res = syscall(__NR_socket, 0x10ul, 3ul, 0xc);
  if (res != -1)
    r[4] = res;
  *(uint64_t*)0x20000100 = 0;
  *(uint32_t*)0x20000108 = 0;
  *(uint64_t*)0x20000110 = 0x200000c0;
  *(uint64_t*)0x200000c0 = 0x20000080;
  *(uint32_t*)0x20000080 = 0x30;
  *(uint8_t*)0x20000084 = 0;
  *(uint8_t*)0x20000085 = 0xb;
  *(uint16_t*)0x20000086 = 0x401;
  *(uint32_t*)0x20000088 = 0;
  *(uint32_t*)0x2000008c = 0;
  *(uint8_t*)0x20000090 = 3;
  *(uint8_t*)0x20000091 = 0;
  *(uint16_t*)0x20000092 = htobe16(0);
  *(uint16_t*)0x20000094 = 8;
  STORE_BY_BITMASK(uint16_t, , 0x20000096, 2, 0, 14);
  STORE_BY_BITMASK(uint16_t, , 0x20000097, 1, 6, 1);
  STORE_BY_BITMASK(uint16_t, , 0x20000097, 0, 7, 1);
  *(uint32_t*)0x20000098 = htobe32(0);
  *(uint16_t*)0x2000009c = 9;
  *(uint16_t*)0x2000009e = 1;
  memcpy((void*)0x200000a0, "syz0\000", 5);
  *(uint16_t*)0x200000a8 = 8;
  STORE_BY_BITMASK(uint16_t, , 0x200000aa, 3, 0, 14);
  STORE_BY_BITMASK(uint16_t, , 0x200000ab, 1, 6, 1);
  STORE_BY_BITMASK(uint16_t, , 0x200000ab, 0, 7, 1);
  *(uint32_t*)0x200000ac = htobe32(0);
  *(uint64_t*)0x200000c8 = 0x30;
  *(uint64_t*)0x20000118 = 1;
  *(uint64_t*)0x20000120 = 0;
  *(uint64_t*)0x20000128 = 0;
  *(uint32_t*)0x20000130 = 0;
  syscall(__NR_sendmsg, r[4], 0x20000100ul, 0ul);
}
int main(void)
{
  syscall(__NR_mmap, 0x1ffff000ul, 0x1000ul, 0ul, 0x32ul, -1, 0ul);
  syscall(__NR_mmap, 0x20000000ul, 0x1000000ul, 7ul, 0x32ul, -1, 0ul);
  syscall(__NR_mmap, 0x21000000ul, 0x1000ul, 0ul, 0x32ul, -1, 0ul);
  loop();
  return 0;
}