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

#define _GNU_SOURCE

#include <endian.h>
#include <pthread.h>
#include <signal.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/prctl.h>
#include <sys/syscall.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <time.h>
#include <unistd.h>

#include <linux/futex.h>

unsigned long long procid;

static void sleep_ms(uint64_t ms)
{
  usleep(ms * 1000);
}

static uint64_t current_time_ms()
{
  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);
  if (pthread_create(&th, &attr, fn, arg))
    exit(1);
  pthread_attr_destroy(&attr);
}

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

static void event_wait(event_t* ev)
{
  while (!__atomic_load_n(&ev->state, __ATOMIC_ACQUIRE))
    syscall(SYS_futex, &ev->state, FUTEX_WAIT, 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, 0, &ts);
    if (__atomic_load_n(&ev->state, __ATOMIC_RELAXED))
      return 1;
    now = current_time_ms();
    if (now - start > timeout)
      return 0;
  }
}

#define SYZ_HAVE_SETUP_TEST 1
static void setup_test()
{
  prctl(PR_SET_PDEATHSIG, SIGKILL, 0, 0, 0);
  setpgrp();
}

#define SYZ_HAVE_RESET_TEST 1
static void reset_test()
{
  int fd;
  for (fd = 3; fd < 30; fd++)
    close(fd);
}

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()
{
  int call, thread;
  for (call = 0; call < 2; call++) {
    for (thread = 0; thread < 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, 25);
      if (__atomic_load_n(&running, __ATOMIC_RELAXED))
        sleep_ms((call == 2 - 1) ? 10 : 2);
      break;
    }
  }
}

static void execute_one();

#define WAIT_FLAGS __WALL

static void loop()
{
  int iter;
  for (iter = 0;; iter++) {
    int pid = fork();
    if (pid < 0)
      exit(1);
    if (pid == 0) {
      setup_test();
      execute_one();
      reset_test();
      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(-pid, SIGKILL);
      kill(pid, SIGKILL);
      while (waitpid(-1, &status, WAIT_FLAGS) != pid) {
      }
      break;
    }
  }
}

uint64_t r[1] = {0xffffffffffffffff};

void execute_call(int call)
{
  long res;
  switch (call) {
  case 0:
    res = syscall(__NR_socket, 2, 1, 0);
    if (res != -1)
      r[0] = res;
    break;
  case 1:
    memcpy((void*)0x20000700,
           "\x66\x69\x6c\x74\x65\x72\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"
           "\x00\x00\x5c\x1e\x2e\x79\x02\xe6\x6f\x43\xb7\xa9\xff\xea\x87\xdc"
           "\x84\x8e\x00\x00\x00\x00\x00\x0d\xe4\x59\x6e\xdd\x00\x00\x00\x00"
           "\x00\x00\x22\x08\x00\x07\x00\x00\x00\x04\x00\x00\x00\x58\x04\x00"
           "\x00\x58\x02\x00\x00\x58\x03\x00\x00\x00\x00\x00\x00\x13\x9e\xc7"
           "\x39\xa6\x0c\x1b\xb4\x0a\x96\xd9\x14\x90\xa0\x40\x92\x3f\x3c\x59"
           "\x5a\x76\xfe\xdb\x81\xcc\x24\x5b\xe8\xdd\xdb\x18\xbb\x3f\xbe\xc8"
           "\x3a\xbe\x68\x45\x20\x1c\x33\x00\xd8\x61\xe5\x80\x2b\xc4\x35\x33"
           "\x53\x7b\xd6\xed\xa7\xb2\x81\xa7\x82\xa2\x7b\x3d\xf4\x33\xe1\xb3"
           "\x26\xb1\x16\x82\xf1\x4d\x33\xf2\xfe\x6d\x01\x2f\x09\x76\x87\x52"
           "\x38\x29\x2c\x1a\xa3\x1e\x15\x1c\xc7\xae\xa5\xa0\x8a\x78\xaa\x20"
           "\xb6\x30\x3c\x7c\x77\x83\x63\xa0\x1c\x9e\xf4\xe6\x63\xa0\x5b\x23"
           "\xdf\x75\x28\xa9\x52\xc8\x83\xc2\x36\x39\x90\x82\x8e\x78\x11\xf1"
           "\x33\x52\x77\x0a\x3e\xba\xf6\x64\x10\xa6\x87\x5b\x0c\xe5\xea\xf5"
           "\xd6\x05\x85\xde\xc7\x55\x40\x4e\xae\xa6\x6b\x24\xee\x5b\xf7\xb2"
           "\x9e\x16\xa9\x25\xea\x07\x1e\x9f\xb9\xcf\x61\x88\x4e\xdf\xe6\x87"
           "\x33\xd4\x73\xa0\x9f\x1d\x71\x52\x5d\xb6\xa1\xb2\x43\x86\xa7\x06"
           "\x9b\x2e\xe8\xe7\x5f\xad\x28\xf1\x6b\x8b\x59\x80\x88\xc2\x2c\x2d"
           "\x63\xb4\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00",
           301);
    *(uint64_t*)0x2000082d = 0x20000000;
    syscall(__NR_setsockopt, r[0], 0, 0x60, 0x20000700, 3);
    break;
  }
}
int main()
{
  syscall(__NR_mmap, 0x20000000, 0x1000000, 3, 0x32, -1, 0);
  for (procid = 0; procid < 8; procid++) {
    if (fork() == 0) {
      loop();
    }
  }
  sleep(1000000);
  return 0;
}