// https://syzkaller.appspot.com/bug?id=779a64ccaefd969d5d9bc72a7c4fe39598d53884
// 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 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, 0x0,
                 0xffffffffffffffff, 0xffffffffffffffff};

void execute_one(void)
{
  intptr_t res = 0;
  res = syscall(__NR_socket, 0xaul, 1ul, 0);
  if (res != -1)
    r[0] = res;
  memcpy((void*)0x20000a80,
         "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*)0x20000aa0 = 0xe;
  *(uint32_t*)0x20000aa4 = 4;
  *(uint32_t*)0x20000aa8 = 0x400;
  *(uint32_t*)0x20000aac = 0xd0;
  *(uint32_t*)0x20000ab0 = 0x108;
  *(uint32_t*)0x20000ab4 = 0x1f8;
  *(uint32_t*)0x20000ab8 = 0x108;
  *(uint32_t*)0x20000abc = 0xd0;
  *(uint32_t*)0x20000ac0 = 0x2c0;
  *(uint32_t*)0x20000ac4 = 0x330;
  *(uint32_t*)0x20000ac8 = 0x330;
  *(uint32_t*)0x20000acc = 0x330;
  *(uint32_t*)0x20000ad0 = 0x2c0;
  *(uint32_t*)0x20000ad4 = 4;
  *(uint64_t*)0x20000ad8 = 0;
  *(uint8_t*)0x20000ae0 = 0xfe;
  *(uint8_t*)0x20000ae1 = 0x80;
  memset((void*)0x20000ae2, 0, 13);
  *(uint8_t*)0x20000aef = 0;
  *(uint8_t*)0x20000af0 = -1;
  *(uint8_t*)0x20000af1 = 1;
  memset((void*)0x20000af2, 0, 13);
  *(uint8_t*)0x20000aff = 1;
  *(uint32_t*)0x20000b00 = htobe32(0);
  *(uint32_t*)0x20000b04 = htobe32(0);
  *(uint32_t*)0x20000b08 = htobe32(0);
  *(uint32_t*)0x20000b0c = htobe32(0);
  *(uint32_t*)0x20000b10 = htobe32(0);
  *(uint32_t*)0x20000b14 = htobe32(0);
  *(uint32_t*)0x20000b18 = htobe32(0);
  *(uint32_t*)0x20000b1c = htobe32(0);
  memcpy((void*)0x20000b20, "rose0\000\000\000\000\000\000\000\000\000\000\000",
         16);
  memcpy((void*)0x20000b30, "netdevsim0\000\000\000\000\000\000", 16);
  *(uint8_t*)0x20000b40 = 0;
  *(uint8_t*)0x20000b50 = 0;
  *(uint16_t*)0x20000b60 = 0;
  *(uint8_t*)0x20000b62 = 0;
  *(uint8_t*)0x20000b63 = 0;
  *(uint8_t*)0x20000b64 = 0;
  *(uint32_t*)0x20000b68 = 0;
  *(uint16_t*)0x20000b6c = 0xa8;
  *(uint16_t*)0x20000b6e = 0x108;
  *(uint32_t*)0x20000b70 = 0;
  *(uint64_t*)0x20000b78 = 0;
  *(uint64_t*)0x20000b80 = 0;
  *(uint16_t*)0x20000b88 = 0x60;
  memcpy((void*)0x20000b8a,
         "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*)0x20000ba7 = 0;
  *(uint32_t*)0x20000ba8 = htobe32(0x7f000001);
  *(uint32_t*)0x20000bb8 = htobe32(0);
  *(uint32_t*)0x20000bbc = htobe32(0);
  *(uint32_t*)0x20000bc0 = htobe32(0);
  *(uint32_t*)0x20000bc4 = htobe32(0);
  *(uint16_t*)0x20000bc8 = htobe16(0);
  *(uint16_t*)0x20000bca = htobe16(0);
  *(uint16_t*)0x20000bcc = htobe16(0);
  *(uint16_t*)0x20000bce = htobe16(0);
  *(uint32_t*)0x20000bd0 = 0;
  *(uint16_t*)0x20000bd4 = 0;
  *(uint32_t*)0x20000bd8 = 0;
  *(uint32_t*)0x20000bdc = 4;
  *(uint32_t*)0x20000be0 = 0;
  memset((void*)0x20000be8, 0, 136);
  *(uint32_t*)0x20000c70 = 0;
  *(uint16_t*)0x20000c74 = 0xa8;
  *(uint16_t*)0x20000c76 = 0xf0;
  *(uint32_t*)0x20000c78 = 0;
  *(uint64_t*)0x20000c80 = 0;
  *(uint64_t*)0x20000c88 = 0;
  *(uint16_t*)0x20000c90 = 0x48;
  memcpy((void*)0x20000c92,
         "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*)0x20000caf = 1;
  *(uint8_t*)0x20000cb0 = -1;
  *(uint8_t*)0x20000cb1 = 2;
  memset((void*)0x20000cb2, 0, 13);
  *(uint8_t*)0x20000cbf = 1;
  memcpy((void*)0x20000cc0, "geneve0\000\000\000\000\000\000\000\000\000", 16);
  *(uint64_t*)0x20000cd0 = 0;
  memset((void*)0x20000cd8, 0, 136);
  *(uint32_t*)0x20000d60 = 0;
  *(uint16_t*)0x20000d64 = 0x110;
  *(uint16_t*)0x20000d66 = 0x138;
  *(uint32_t*)0x20000d68 = 0;
  *(uint64_t*)0x20000d70 = 0;
  *(uint64_t*)0x20000d78 = 0;
  *(uint16_t*)0x20000d80 = 0x68;
  memcpy((void*)0x20000d82,
         "iprange\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*)0x20000d9f = 1;
  *(uint8_t*)0x20000da0 = 0xfe;
  *(uint8_t*)0x20000da1 = 0x80;
  memset((void*)0x20000da2, 0, 13);
  *(uint8_t*)0x20000daf = 0xaa;
  *(uint8_t*)0x20000db0 = 0xfe;
  *(uint8_t*)0x20000db1 = 0x88;
  memset((void*)0x20000db2, 0, 12);
  *(uint8_t*)0x20000dbe = 0;
  *(uint8_t*)0x20000dbf = 1;
  *(uint8_t*)0x20000dc0 = 0xfc;
  *(uint8_t*)0x20000dc1 = 1;
  memset((void*)0x20000dc2, 0, 13);
  *(uint8_t*)0x20000dcf = 0;
  *(uint8_t*)0x20000dd0 = 0xac;
  *(uint8_t*)0x20000dd1 = 0x14;
  *(uint8_t*)0x20000dd2 = 0x14;
  *(uint8_t*)0x20000dd3 = 0;
  *(uint8_t*)0x20000de0 = 3;
  *(uint16_t*)0x20000de8 = 0x28;
  memcpy((void*)0x20000dea,
         "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*)0x20000e07 = 0;
  *(uint32_t*)0x20000e08 = 0;
  memset((void*)0x20000e10, 0, 136);
  *(uint32_t*)0x20000e98 = 0;
  *(uint16_t*)0x20000e9c = 0xa8;
  *(uint16_t*)0x20000e9e = 0xd0;
  *(uint32_t*)0x20000ea0 = 0;
  *(uint64_t*)0x20000ea8 = 0;
  *(uint64_t*)0x20000eb0 = 0;
  *(uint16_t*)0x20000eb8 = 0x28;
  memset((void*)0x20000eba, 0, 29);
  *(uint8_t*)0x20000ed7 = 0;
  *(uint32_t*)0x20000ed8 = 0xfffffffe;
  syscall(__NR_setsockopt, r[0], 0x29, 0x40, 0x20000a80ul, 0x1a9ul);
  res = syscall(__NR_socket, 0x11ul, 2ul, 0x300);
  if (res != -1)
    r[1] = res;
  memcpy((void*)0x20000180, "bond0\000\000\000\000\000\000\000\000\000\000\000",
         16);
  res = syscall(__NR_ioctl, r[1], 0x8933, 0x20000180ul);
  if (res != -1)
    r[2] = *(uint32_t*)0x20000190;
  res = syscall(__NR_socket, 0x10ul, 3ul, 0);
  if (res != -1)
    r[3] = res;
  *(uint64_t*)0x20000080 = 0;
  *(uint32_t*)0x20000088 = 0;
  *(uint64_t*)0x20000090 = 0x20000140;
  *(uint64_t*)0x20000140 = 0x200001c0;
  *(uint32_t*)0x200001c0 = 0x5c;
  *(uint16_t*)0x200001c4 = 0x10;
  *(uint16_t*)0x200001c6 = 0x401;
  *(uint32_t*)0x200001c8 = 0x70bd2b;
  *(uint32_t*)0x200001cc = 0;
  *(uint8_t*)0x200001d0 = 0;
  *(uint8_t*)0x200001d1 = 0;
  *(uint16_t*)0x200001d2 = 0;
  *(uint32_t*)0x200001d4 = 0;
  *(uint32_t*)0x200001d8 = 0;
  *(uint32_t*)0x200001dc = 0x6100;
  *(uint16_t*)0x200001e0 = 0x24;
  STORE_BY_BITMASK(uint16_t, , 0x200001e2, 0x12, 0, 14);
  STORE_BY_BITMASK(uint16_t, , 0x200001e3, 0, 6, 1);
  STORE_BY_BITMASK(uint16_t, , 0x200001e3, 1, 7, 1);
  *(uint16_t*)0x200001e4 = 0xb;
  *(uint16_t*)0x200001e6 = 1;
  memcpy((void*)0x200001e8, "macsec\000", 7);
  *(uint16_t*)0x200001f0 = 0x14;
  STORE_BY_BITMASK(uint16_t, , 0x200001f2, 2, 0, 14);
  STORE_BY_BITMASK(uint16_t, , 0x200001f3, 0, 6, 1);
  STORE_BY_BITMASK(uint16_t, , 0x200001f3, 1, 7, 1);
  *(uint16_t*)0x200001f4 = 5;
  *(uint16_t*)0x200001f6 = 6;
  *(uint8_t*)0x200001f8 = 3;
  *(uint16_t*)0x200001fc = 5;
  *(uint16_t*)0x200001fe = 9;
  *(uint8_t*)0x20000200 = 1;
  *(uint16_t*)0x20000204 = 8;
  *(uint16_t*)0x20000206 = 5;
  *(uint32_t*)0x20000208 = r[2];
  *(uint16_t*)0x2000020c = 8;
  *(uint16_t*)0x2000020e = 0xa;
  *(uint32_t*)0x20000210 = 0;
  *(uint16_t*)0x20000214 = 8;
  *(uint16_t*)0x20000216 = 4;
  *(uint32_t*)0x20000218 = 4;
  *(uint64_t*)0x20000148 = 0x5c;
  *(uint64_t*)0x20000098 = 1;
  *(uint64_t*)0x200000a0 = 0;
  *(uint64_t*)0x200000a8 = 0;
  *(uint32_t*)0x200000b0 = 0;
  syscall(__NR_sendmsg, r[3], 0x20000080ul, 0x40010ul);
  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;
}