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

#define _GNU_SOURCE

#include <dirent.h>
#include <endian.h>
#include <errno.h>
#include <fcntl.h>
#include <pthread.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>

#include <linux/futex.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;
}

static void thread_start(void* (*fn)(void*), void* arg)
{
  pthread_t th;
  pthread_attr_t attr;
  pthread_attr_init(&attr);
  pthread_attr_setstacksize(&attr, 128 << 10);
  int i = 0;
  for (; i < 100; i++) {
    if (pthread_create(&th, &attr, fn, arg) == 0) {
      pthread_attr_destroy(&attr);
      return;
    }
    if (errno == EAGAIN) {
      usleep(50);
      continue;
    }
    break;
  }
  exit(1);
}

typedef struct {
  int state;
} event_t;

static void event_init(event_t* ev)
{
  ev->state = 0;
}

static void event_reset(event_t* ev)
{
  ev->state = 0;
}

static void event_set(event_t* ev)
{
  if (ev->state)
    exit(1);
  __atomic_store_n(&ev->state, 1, __ATOMIC_RELEASE);
  syscall(SYS_futex, &ev->state, FUTEX_WAKE | FUTEX_PRIVATE_FLAG, 1000000);
}

static void event_wait(event_t* ev)
{
  while (!__atomic_load_n(&ev->state, __ATOMIC_ACQUIRE))
    syscall(SYS_futex, &ev->state, FUTEX_WAIT | FUTEX_PRIVATE_FLAG, 0, 0);
}

static int event_isset(event_t* ev)
{
  return __atomic_load_n(&ev->state, __ATOMIC_ACQUIRE);
}

static int event_timedwait(event_t* ev, uint64_t timeout)
{
  uint64_t start = current_time_ms();
  uint64_t now = start;
  for (;;) {
    uint64_t remain = timeout - (now - start);
    struct timespec ts;
    ts.tv_sec = remain / 1000;
    ts.tv_nsec = (remain % 1000) * 1000 * 1000;
    syscall(SYS_futex, &ev->state, FUTEX_WAIT | FUTEX_PRIVATE_FLAG, 0, &ts);
    if (__atomic_load_n(&ev->state, __ATOMIC_ACQUIRE))
      return 1;
    now = current_time_ms();
    if (now - start > timeout)
      return 0;
  }
}

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

struct thread_t {
  int created, call;
  event_t ready, done;
};

static struct thread_t threads[16];
static void execute_call(int call);
static int running;

static void* thr(void* arg)
{
  struct thread_t* th = (struct thread_t*)arg;
  for (;;) {
    event_wait(&th->ready);
    event_reset(&th->ready);
    execute_call(th->call);
    __atomic_fetch_sub(&running, 1, __ATOMIC_RELAXED);
    event_set(&th->done);
  }
  return 0;
}

static void execute_one(void)
{
  int i, call, thread;
  for (call = 0; call < 5; call++) {
    for (thread = 0; thread < (int)(sizeof(threads) / sizeof(threads[0]));
         thread++) {
      struct thread_t* th = &threads[thread];
      if (!th->created) {
        th->created = 1;
        event_init(&th->ready);
        event_init(&th->done);
        event_set(&th->done);
        thread_start(thr, th);
      }
      if (!event_isset(&th->done))
        continue;
      event_reset(&th->done);
      th->call = call;
      __atomic_fetch_add(&running, 1, __ATOMIC_RELAXED);
      event_set(&th->ready);
      if (call == 3)
        break;
      event_timedwait(&th->done, 50);
      break;
    }
  }
  for (i = 0; i < 100 && __atomic_load_n(&running, __ATOMIC_RELAXED); i++)
    sleep_ms(1);
}

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[2] = {0xffffffffffffffff, 0xffffffffffffffff};

void execute_call(int call)
{
  intptr_t res = 0;
  switch (call) {
  case 0:
    res = syscall(__NR_socketpair, /*domain=*/0x1eul, /*type=*/1ul, /*proto=*/0,
                  /*fds=*/0x20000000ul);
    if (res != -1) {
      r[0] = *(uint32_t*)0x20000000;
      r[1] = *(uint32_t*)0x20000004;
    }
    break;
  case 1:
    *(uint32_t*)0x20000540 = -1;
    syscall(__NR_setsockopt, /*fd=*/r[0], /*level=*/1, /*optname=*/0x21,
            /*optval=*/0x20000540ul, /*optlen=*/4ul);
    break;
  case 2:
    *(uint64_t*)0x20001c40 = 0;
    *(uint32_t*)0x20001c48 = 0;
    *(uint64_t*)0x20001c50 = 0x20001a00;
    *(uint64_t*)0x20001a00 = 0x20001380;
    memcpy(
        (void*)0x20001380,
        "\xd3\x8d\xc2\x7b\x40\xc0\xf2\xaa\x5f\xaf\xc5\x3f\x9c\xee\x29\x48\xbf"
        "\xe9\x0b\xf0\x8e\x70\x58\xcc\xe8\x35\x93\xb7\x1d\x2d\x39\x9d\x19\x2f"
        "\xf2\xcd\x60\x04\xb1\xc2\x47\xde\xad\x13\x50\xf7\x4e\x73\x51\xf2\x20"
        "\x17\xc5\xfa\x08\xdf\xcf\x9c\x9d\xd7\x8c\x53\x6f\x63\xef\x51\xc4\x94"
        "\x17\x77\xcc\xdf\xa4\xb9\x64\x67\xa9\xe8\x9c\x59\xe7\xcf\x43\x02\x1f"
        "\xa7\xf1\xa0\x67\xfc\x61\xcf\xf0\xe7\x6a\x5d\xd8\x12\x45\x67\x37\x12"
        "\x83\x26\xfc\x6b\x9a\xe0\x25\x68\xc0\x82\x46\xb9\xf9\x9d\x75\xd5\xde"
        "\x5b\xa3\xbc\x7e\xc0\x1f\x07\x00\x67\xc3\xee\xdd\x7b\xc3\x6f",
        134);
    *(uint64_t*)0x20001a08 = 0x86;
    *(uint64_t*)0x20001a10 = 0x20001440;
    memcpy(
        (void*)0x20001440,
        "\x66\x60\xc6\xfc\x36\x69\x71\xd5\xa5\x52\xde\xd1\x90\xfa\x56\x8b\x1b"
        "\xca\x5e\xab\x35\x42\x4d\x7f\xc0\xa8\xe5\x5c\xf4\x24\x43\x98\x83\x20"
        "\x0d\x7c\x2e\xbf\x47\x61\x3e\x48\xee\xd9\xd3\x2b\x06\x02\xde\x9d\x63"
        "\x48\x14\x50\x37\xc2\x05\xb4\xde\x23\x7a\x9f\x84\x8d\xba\x40\xc3\xa1"
        "\x62\xf9\x07\x85\xd7\xef\xb3\x06\xd6\xb7\xea\x73\xcd\xa8\xac\xf5\x35"
        "\xa1\x37\xef\x6c\x12\x38\x13\xaa\x5d\x19\x2a\xe6\xfc\x0b\x58\xf1\x9b"
        "\xbe\xfc\xb6\xd8\x64\x61\x37\x24\x01\x1a\xcb\xe4\x15\x2c\xa0\x4f\x21"
        "\xd7\xba\xdf\xb8\xb1\x16\x62\x7f\xd8\xb8\xed\xb5\x55\x47\xfb\x13\xac"
        "\x7f\xab\xe8\xe0\x9f\x0f\x4a\x9f\x35\x6d\x92\x2f\x88\xad\x5f\x8a\x6f"
        "\x86\x74\x5f\xf3\xbc\xf6\x15\x38\x1a\x08\xdd\x7a\x06\xe2\x86\xc5\xd3"
        "\xc7\x74\x81\x5e\xb0\x5f\xf0\x9c\x61\x84\x4c\x92\x1d\x48\xc2\x04\xa7"
        "\x8d\x3f\xc8\xdb\x23\x78\x33\x15\xa0\xe9\xa0\x18\xfa\xca\x73\x81\x1e"
        "\x26\x3e\x04\x50\xc1\xd5\x81\x65\xd2\x57\xcd\xad\xb9\xd8\xbd\x8a\x13"
        "\xe7\x28\x58\xd1\xce\xae\x92\xb9\x70\x81\x55\xf7\x1c\x9e\x5c\x9d\x4b"
        "\x01\x74\x6c\x1e\x41\x5a\xd8\x6d",
        246);
    *(uint64_t*)0x20001a18 = 0xf6;
    *(uint64_t*)0x20001a20 = 0x20001540;
    memcpy((void*)0x20001540,
           "\xd7\xe0\x6f\xfa\x93\x9e\xa9\x48\xc5\xcd\xd9\x02\x67\x0a\x56\x85"
           "\x58\xf5\x98\xc9\xd5\x6f\xf8\xb0\xe8\xae\x08\xf3\x64\x8b\x01\x57"
           "\x67\xb5\x7d\x5f\x55\x8f\x93\xb0\x98\x7a\x89\x2d\x5c",
           45);
    *(uint64_t*)0x20001a28 = 0x2d;
    *(uint64_t*)0x20001c58 = 3;
    *(uint64_t*)0x20001c60 = 0;
    *(uint64_t*)0x20001c68 = 0;
    *(uint32_t*)0x20001c70 = 0;
    syscall(__NR_sendmsg, /*fd=*/r[1], /*msg=*/0x20001c40ul, /*f=*/0ul);
    break;
  case 3:
    syscall(__NR_close, /*fd=*/r[1]);
    break;
  case 4:
    *(uint64_t*)0x20000200 = 0;
    *(uint32_t*)0x20000208 = 0;
    *(uint64_t*)0x20000210 = 0;
    *(uint64_t*)0x20000218 = 0;
    *(uint64_t*)0x20000220 = 0;
    *(uint64_t*)0x20000228 = 0;
    *(uint32_t*)0x20000230 = 0;
    syscall(__NR_sendmsg, /*fd=*/r[0], /*msg=*/0x20000200ul, /*f=*/0ul);
    {
      int i;
      for (i = 0; i < 32; i++) {
        syscall(__NR_sendmsg, /*fd=*/r[0], /*msg=*/0x20000200ul, /*f=*/0ul);
      }
    }
    break;
  }
}
int main(void)
{
  syscall(__NR_mmap, /*addr=*/0x1ffff000ul, /*len=*/0x1000ul, /*prot=*/0ul,
          /*flags=MAP_FIXED|MAP_ANONYMOUS|MAP_PRIVATE*/ 0x32ul, /*fd=*/-1,
          /*offset=*/0ul);
  syscall(__NR_mmap, /*addr=*/0x20000000ul, /*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=*/0x21000000ul, /*len=*/0x1000ul, /*prot=*/0ul,
          /*flags=MAP_FIXED|MAP_ANONYMOUS|MAP_PRIVATE*/ 0x32ul, /*fd=*/-1,
          /*offset=*/0ul);
  loop();
  return 0;
}