// https://syzkaller.appspot.com/bug?id=2d36e7f205f5655cb59ce49754c466cd6c0a2b22
// 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 unsigned long long procid;

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;
  for (i = 0; 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);
  int i;
  for (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 < 11; 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);
      event_timedwait(&th->done, 45);
      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;
  for (iter = 0;; 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 < 5 * 1000)
        continue;
      kill_and_wait(pid, &status);
      break;
    }
  }
}

#ifndef __NR_bpf
#define __NR_bpf 321
#endif

uint64_t r[4] = {0xffffffffffffffff, 0xffffffffffffffff, 0xffffffffffffffff,
                 0xffffffffffffffff};

void execute_call(int call)
{
  intptr_t res;
  switch (call) {
  case 0:
    res = syscall(__NR_socket, 2ul, 1ul, 0);
    if (res != -1)
      r[0] = res;
    break;
  case 1:
    *(uint16_t*)0x20e5b000 = 2;
    *(uint16_t*)0x20e5b002 = htobe16(0x4e20);
    *(uint32_t*)0x20e5b004 = htobe32(0xe0000001);
    syscall(__NR_bind, r[0], 0x20e5b000ul, 0x10ul);
    break;
  case 2:
    *(uint16_t*)0x20ccb000 = 2;
    *(uint16_t*)0x20ccb002 = htobe16(0x4e20);
    *(uint32_t*)0x20ccb004 = htobe32(0);
    syscall(__NR_connect, r[0], 0x20ccb000ul, 0x10ul);
    break;
  case 3:
    *(uint32_t*)0x20000100 = 1;
    *(uint32_t*)0x20000104 = 3;
    *(uint64_t*)0x20000108 = 0x200003c0;
    memcpy((void*)0x200003c0,
           "\x18\x00\x00\x00\x03\x00\x00\x00\x00\x00\x00\x00\x26\xd0\x00\x00"
           "\x95\x00\x2b\x00\x00\x00\x00\x00\x93\xad\xff\xa8\x22\x55\xf6\x74"
           "\x41\x2d\x02\x00\x00\x00\x00\x00\x00\x5a\xb5\x27\xee\x36\x97\xf5"
           "\x7f\xe1\x2e\xa7\x50\x9e\x1f\xcf\xb0\xb3\xf4\x27\x9e\x7b\x34\x60"
           "\xdd\x37\x53\x6b\xed\xf6\xba\x6b\xda\x6d\x2a\xfe\xe3\x30\x25\xa3"
           "\x0b\x45\xbd\xcf\x1d\x27\x36\x83\x62\x6e\x00\xdc\x25\x4d\x57\x0d"
           "\xca\x6b\x78\xad\x83\x34\x88\xcf\xe4\x10\x9e\xd2\x04\x9e\xdd\x0d"
           "\x69\x61\x3d\x3c\xd6\x1f\x00\x15\x8e\x6e\x0e\x86\x32\x15\x1d\x72"
           "\xab\x8a\xba\xa9\x65\x23\x00\x00\x00\x00\x00\x00\x00\x00\xe2\x58"
           "\xf8\x2b\x6c\xe7\x23\x13\xa0\x75\xbb\x49\xc5\x2f\xa5\x53\x42\xa6"
           "\x20\xc3\x02\x09\x37\x7f\xb2\x27\x46\xec\xf5\x9f\x45\xa2\x44\x36"
           "\x30\xfc\xb5\xb4\xf9\xeb\x5e\x84\xb1\xaf\x1a\x8a\x2b\x36\xf8\x79"
           "\x96\x32\xb6\xca\x6f\x69\x77\xe3\x34\xa4\xee\xfc\xd5\x6e\xb6\xee"
           "\x1e\x3f\x9b\x89\x16\x44\x6f\x0a\x8c\x2a\x8a\x50\x7c\xab\x0b\x04"
           "\x00\x00\x00\xd1\x41\x7b\xde\x5c\x82\x9a\x76\x5a\x78\xcc\xdc\x89"
           "\x09\x43\xec\xc2\x91\x6f\x14\x07\x08\x8b\x81\x19\x5b\xb1\x12\xa3"
           "\x47\x1c\xbf\xe8\x2e\xb5\xca\x3f\x9e\x42\x04\x90\x28\x90\x0f\xb3"
           "\x8f\xfa\x35\xe4\x71\x2e\xef\xd6\x44\x94\x21\x0e\xbd\x07\x24\xbb"
           "\xe3\x3c\xa3\xaf\xf5\xac\x81\x4e\x7e\xa0\xfa\x4a\xd9\x37\xd9\x1f"
           "\x14\x84\xf0\xd6\x4e\xed\x8f\x4d\x66\x1a\xa8\xea\xa7\xa3\x84\x73"
           "\x6c\x8f\xc4\xc3\x7b\x93\x77\x55\x7b\xfa\x4d\xd1\xf7\xea\x38\xb8"
           "\x86\x86\xa4\x6f\x6b\x93\xc9\xcb\xad\xb9\x5c\x91\x61\x82\x69\x42"
           "\xfc\x0a\xe0\xb1\x1a\xe0\x0a\x15\xae\xd2\xdb\x19\xb5\x9f\x94\x47"
           "\x7d\x15\xd4\xa9\xc2\x7c\x6b\x6d\x9d\x60\x53\xef\x16\xf9\xf7\xd3"
           "\xe9\x72\xdd\x85\x65\xc7\x35\xe1\xfe\xe5\x80\xc9\x30\x1f\xef\x96"
           "\xbe\x02\x95\x8a\x62\x80\x8b\x84\xb7\x9b\x17\x76\x27\xc9\xd1\x0c"
           "\x5a\xc3\x91\x8f\x25\xba\xa0\xe9\x4a\xf1\xe1\x67\x58\x42\x98\xf1"
           "\x24\x2e\x75\x76\x0e\x85\x80\x18\x19\x4f\xb2\x68\xbc\xee\xfe\x4e"
           "\x91\x69\x17\x40\x16\x3a\x85\x99\xf1\xbb\x23\x81\x65\x39\x8e\x3f"
           "\x67\x0e\xc7\x11\x5d\xd4\xd7\x83\x1b\x82\xd0\x1d\x47\x42\xdd\x8d"
           "\xc7\xf1\x44\x9c\x83\xbc\x89\xe3\xba\xdb\xae\x7b\xf5\xd4",
           494);
    *(uint64_t*)0x20000110 = 0x202bf000;
    memcpy((void*)0x202bf000, "syzkaller\000", 10);
    *(uint32_t*)0x20000118 = 4;
    *(uint32_t*)0x2000011c = 0x175;
    *(uint64_t*)0x20000120 = 0x20000040;
    *(uint32_t*)0x20000128 = 0;
    *(uint32_t*)0x2000012c = 0;
    *(uint8_t*)0x20000130 = 0;
    *(uint8_t*)0x20000131 = 0;
    *(uint8_t*)0x20000132 = 0;
    *(uint8_t*)0x20000133 = 0;
    *(uint8_t*)0x20000134 = 0;
    *(uint8_t*)0x20000135 = 0;
    *(uint8_t*)0x20000136 = 0;
    *(uint8_t*)0x20000137 = 0;
    *(uint8_t*)0x20000138 = 0;
    *(uint8_t*)0x20000139 = 0;
    *(uint8_t*)0x2000013a = 0;
    *(uint8_t*)0x2000013b = 0;
    *(uint8_t*)0x2000013c = 0;
    *(uint8_t*)0x2000013d = 0;
    *(uint8_t*)0x2000013e = 0;
    *(uint8_t*)0x2000013f = 0;
    *(uint32_t*)0x20000140 = 0;
    *(uint32_t*)0x20000144 = 0;
    *(uint32_t*)0x20000148 = -1;
    *(uint32_t*)0x2000014c = 8;
    *(uint64_t*)0x20000150 = 0x20000000;
    *(uint32_t*)0x20000000 = 0;
    *(uint32_t*)0x20000004 = 0;
    *(uint32_t*)0x20000158 = 0;
    *(uint32_t*)0x2000015c = 0x10;
    *(uint64_t*)0x20000160 = 0x20000000;
    *(uint32_t*)0x20000000 = 0;
    *(uint32_t*)0x20000004 = 0;
    *(uint32_t*)0x20000008 = 0;
    *(uint32_t*)0x2000000c = 0;
    *(uint32_t*)0x20000168 = 0;
    *(uint32_t*)0x2000016c = 0;
    *(uint32_t*)0x20000170 = -1;
    res = syscall(__NR_bpf, 5ul, 0x20000100ul, 0x48ul);
    if (res != -1)
      r[1] = res;
    break;
  case 4:
    res = syscall(__NR_socket, 0x29ul, 5ul, 0);
    if (res != -1)
      r[2] = res;
    break;
  case 5:
    memcpy((void*)0x200001c0, "cgroup.controllers\000", 19);
    res = syscall(__NR_openat, 0xffffff9c, 0x200001c0ul, 0x275aul, 0ul);
    if (res != -1)
      r[3] = res;
    break;
  case 6:
    syscall(__NR_write, r[3], 0x20000040ul, 0x208e24bul);
    break;
  case 7:
    syscall(__NR_mmap, 0x20000000ul, 0xb36000ul, 2ul, 0x28011ul, r[3], 0ul);
    break;
  case 8:
    *(uint32_t*)0x20000380 = r[0];
    *(uint32_t*)0x20000384 = r[1];
    syscall(__NR_ioctl, r[2], 0x89e0, 0x20000380ul);
    break;
  case 9:
    *(uint64_t*)0x20002940 = 0;
    *(uint32_t*)0x20002948 = 0;
    *(uint64_t*)0x20002950 = 0x20002800;
    *(uint64_t*)0x20002800 = 0x20002980;
    *(uint64_t*)0x20002808 = 0xfffffde7;
    *(uint64_t*)0x20002958 = 1;
    *(uint64_t*)0x20002960 = 0;
    *(uint64_t*)0x20002968 = 0;
    *(uint32_t*)0x20002970 = 0;
    syscall(__NR_sendmsg, r[2], 0x20002940ul, 0ul);
    break;
  case 10:
    syscall(__NR_write, r[2], 0ul, 0ul);
    break;
  }
}
int main(void)
{
  syscall(__NR_mmap, 0x20000000ul, 0x1000000ul, 3ul, 0x32ul, -1, 0ul);
  for (procid = 0; procid < 6; procid++) {
    if (fork() == 0) {
      loop();
    }
  }
  sleep(1000000);
  return 0;
}