// https://syzkaller.appspot.com/bug?id=2bf7b7983c2398ec6f0c4c6c87cb50223e8873f8
// autogenerated by syzkaller (http://github.com/google/syzkaller)

#define _GNU_SOURCE

#include <endian.h>
#include <errno.h>
#include <linux/futex.h>
#include <pthread.h>
#include <signal.h>
#include <stdarg.h>
#include <stdio.h>
#include <stdlib.h>
#include <sys/prctl.h>
#include <sys/syscall.h>
#include <sys/time.h>
#include <sys/wait.h>
#include <time.h>
#include <unistd.h>

__attribute__((noreturn)) static void doexit(int status)
{
  volatile unsigned i;
  syscall(__NR_exit_group, status);
  for (i = 0;; i++) {
  }
}

#include <stdint.h>
#include <string.h>

const int kFailStatus = 67;
const int kRetryStatus = 69;

static void fail(const char* msg, ...)
{
  int e = errno;
  va_list args;
  va_start(args, msg);
  vfprintf(stderr, msg, args);
  va_end(args);
  fprintf(stderr, " (errno %d)\n", e);
  doexit((e == ENOMEM || e == EAGAIN) ? kRetryStatus : kFailStatus);
}

static uint64_t current_time_ms()
{
  struct timespec ts;

  if (clock_gettime(CLOCK_MONOTONIC, &ts))
    fail("clock_gettime failed");
  return (uint64_t)ts.tv_sec * 1000 + (uint64_t)ts.tv_nsec / 1000000;
}

static void test();

void loop()
{
  int iter;
  for (iter = 0;; iter++) {
    int pid = fork();
    if (pid < 0)
      fail("loop fork failed");
    if (pid == 0) {
      prctl(PR_SET_PDEATHSIG, SIGKILL, 0, 0, 0);
      setpgrp();
      test();
      doexit(0);
    }
    int status = 0;
    uint64_t start = current_time_ms();
    for (;;) {
      int res = waitpid(-1, &status, __WALL | WNOHANG);
      if (res == pid)
        break;
      usleep(1000);
      if (current_time_ms() - start > 5 * 1000) {
        kill(-pid, SIGKILL);
        kill(pid, SIGKILL);
        while (waitpid(-1, &status, __WALL) != pid) {
        }
        break;
      }
    }
  }
}

struct thread_t {
  int created, running, call;
  pthread_t th;
};

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

static void* thr(void* arg)
{
  struct thread_t* th = (struct thread_t*)arg;
  for (;;) {
    while (!__atomic_load_n(&th->running, __ATOMIC_ACQUIRE))
      syscall(SYS_futex, &th->running, FUTEX_WAIT, 0, 0);
    execute_call(th->call);
    __atomic_fetch_sub(&running, 1, __ATOMIC_RELAXED);
    __atomic_store_n(&th->running, 0, __ATOMIC_RELEASE);
    syscall(SYS_futex, &th->running, FUTEX_WAKE);
  }
  return 0;
}

static void execute(int num_calls)
{
  int call, thread;
  running = 0;
  for (call = 0; call < num_calls; call++) {
    for (thread = 0; thread < sizeof(threads) / sizeof(threads[0]);
         thread++) {
      struct thread_t* th = &threads[thread];
      if (!th->created) {
        th->created = 1;
        pthread_attr_t attr;
        pthread_attr_init(&attr);
        pthread_attr_setstacksize(&attr, 128 << 10);
        pthread_create(&th->th, &attr, thr, th);
      }
      if (!__atomic_load_n(&th->running, __ATOMIC_ACQUIRE)) {
        th->call = call;
        __atomic_fetch_add(&running, 1, __ATOMIC_RELAXED);
        __atomic_store_n(&th->running, 1, __ATOMIC_RELEASE);
        syscall(SYS_futex, &th->running, FUTEX_WAKE);
        if (collide && call % 2)
          break;
        struct timespec ts;
        ts.tv_sec = 0;
        ts.tv_nsec = 20 * 1000 * 1000;
        syscall(SYS_futex, &th->running, FUTEX_WAIT, 1, &ts);
        if (running)
          usleep((call == num_calls - 1) ? 10000 : 1000);
        break;
      }
    }
  }
}

#ifndef __NR_openat
#define __NR_openat 295
#endif
#ifndef __NR_ioctl
#define __NR_ioctl 54
#endif
#ifndef __NR_lstat
#define __NR_lstat 107
#endif
#ifndef __NR_mmap
#define __NR_mmap 192
#endif
#ifndef __NR_getsockopt
#define __NR_getsockopt 365
#endif
#ifndef __NR_ftruncate
#define __NR_ftruncate 93
#endif
#ifndef __NR_socket
#define __NR_socket 359
#endif
#ifndef __NR_writev
#define __NR_writev 146
#endif
#undef __NR_mmap
#define __NR_mmap __NR_mmap2

long r[4];
void execute_call(int call)
{
  switch (call) {
  case 0:
    syscall(__NR_mmap, 0x20000000, 0xfff000, 0x3, 0x32, 0xffffffff,
            0x0);
    break;
  case 1:
    *(uint32_t*)0x204afef8 = 0x0;
    *(uint16_t*)0x204aff00 = 0xa;
    *(uint16_t*)0x204aff02 = htobe16(0x4e21);
    *(uint32_t*)0x204aff04 = 0xffff;
    *(uint8_t*)0x204aff08 = 0xfe;
    *(uint8_t*)0x204aff09 = 0x80;
    *(uint8_t*)0x204aff0a = 0x0;
    *(uint8_t*)0x204aff0b = 0x0;
    *(uint8_t*)0x204aff0c = 0x0;
    *(uint8_t*)0x204aff0d = 0x0;
    *(uint8_t*)0x204aff0e = 0x0;
    *(uint8_t*)0x204aff0f = 0x0;
    *(uint8_t*)0x204aff10 = 0x0;
    *(uint8_t*)0x204aff11 = 0x0;
    *(uint8_t*)0x204aff12 = 0x0;
    *(uint8_t*)0x204aff13 = 0x0;
    *(uint8_t*)0x204aff14 = 0x0;
    *(uint8_t*)0x204aff15 = 0x0;
    *(uint8_t*)0x204aff16 = 0x0;
    *(uint8_t*)0x204aff17 = 0xbb;
    *(uint32_t*)0x204aff18 = 0x9;
    *(uint64_t*)0x204aff20 = 0x0;
    *(uint64_t*)0x204aff28 = 0x0;
    *(uint64_t*)0x204aff30 = 0x0;
    *(uint64_t*)0x204aff38 = 0x0;
    *(uint64_t*)0x204aff40 = 0x0;
    *(uint64_t*)0x204aff48 = 0x0;
    *(uint64_t*)0x204aff50 = 0x0;
    *(uint64_t*)0x204aff58 = 0x0;
    *(uint64_t*)0x204aff60 = 0x0;
    *(uint64_t*)0x204aff68 = 0x0;
    *(uint64_t*)0x204aff70 = 0x0;
    *(uint64_t*)0x204aff78 = 0x0;
    *(uint64_t*)0x204aff88 = 0x3;
    *(uint64_t*)0x204aff90 = 0x7fff;
    *(uint64_t*)0x204aff98 = 0x400;
    *(uint64_t*)0x204affa0 = 0xaca;
    *(uint64_t*)0x204affa8 = 0x0;
    *(uint64_t*)0x204affb0 = 0xee38;
    *(uint64_t*)0x204affb8 = 0x1000;
    *(uint64_t*)0x204affc0 = 0x5;
    *(uint64_t*)0x204affc8 = 0x10000;
    *(uint64_t*)0x204affd0 = 0x2;
    *(uint64_t*)0x204affd8 = 0x8824;
    *(uint64_t*)0x204affe0 = 0x2;
    *(uint64_t*)0x204affe8 = 0xfffffffffffffff7;
    *(uint64_t*)0x204afff0 = 0x1ff;
    *(uint64_t*)0x204afff8 = 0x39a;
    *(uint32_t*)0x20b25ffc = 0x108;
    syscall(__NR_getsockopt, 0xffffffff, 0x84, 0x70, 0x204afef8,
            0x20b25ffc);
    break;
  case 2:
    syscall(__NR_ftruncate, 0xffffffff, 0x0);
    break;
  case 3:
    r[0] = syscall(__NR_socket, 0x10, 0x3, 0x0);
    break;
  case 4:
    *(uint32_t*)0x203bc000 = 0x20628fd7;
    *(uint32_t*)0x203bc004 = 0x29;
    memcpy((void*)0x20628fd7,
           "\x29\x00\x00\x00\x20\x00\x19\xff\xe6\x1d\x00\x14\x04\x00"
           "\x00\xe5\x02\x00\x00\x16\x15\xde\x1f\x05\x00\x00\x00\x04"
           "\x0d\x00\x0a\x00\x00\x00\x15\x00\x00\x00\x01\xff\x08",
           41);
    syscall(__NR_writev, r[0], 0x203bc000, 0x1);
    break;
  case 5:
    memcpy((void*)0x2052dff7, "/dev/kvm", 9);
    r[1] = syscall(__NR_openat, 0xffffff9c, 0x2052dff7, 0x0, 0x0);
    break;
  case 6:
    r[2] = syscall(__NR_ioctl, r[1], 0xae01, 0x0);
    break;
  case 7:
    syscall(__NR_ioctl, r[2], 0xae47, 0x0);
    break;
  case 8:
    *(uint32_t*)0x20473000 = 0x79;
    *(uint32_t*)0x20473004 = 0x0;
    *(uint64_t*)0x20473008 = 0x0;
    *(uint64_t*)0x20473010 = 0x0;
    *(uint64_t*)0x20473018 = 0x0;
    *(uint64_t*)0x20473020 = 0x0;
    *(uint8_t*)0x20473028 = 0x0;
    *(uint8_t*)0x20473029 = 0x0;
    *(uint8_t*)0x2047302a = 0x0;
    *(uint8_t*)0x2047302b = 0x0;
    *(uint8_t*)0x2047302c = 0x0;
    *(uint8_t*)0x2047302d = 0x0;
    *(uint8_t*)0x2047302e = 0x0;
    *(uint8_t*)0x2047302f = 0x0;
    *(uint8_t*)0x20473030 = 0x0;
    *(uint8_t*)0x20473031 = 0x0;
    *(uint8_t*)0x20473032 = 0x0;
    *(uint8_t*)0x20473033 = 0x0;
    *(uint8_t*)0x20473034 = 0x0;
    *(uint8_t*)0x20473035 = 0x0;
    *(uint8_t*)0x20473036 = 0x0;
    *(uint8_t*)0x20473037 = 0x0;
    *(uint8_t*)0x20473038 = 0x0;
    *(uint8_t*)0x20473039 = 0x0;
    *(uint8_t*)0x2047303a = 0x0;
    *(uint8_t*)0x2047303b = 0x0;
    *(uint8_t*)0x2047303c = 0x0;
    *(uint8_t*)0x2047303d = 0x0;
    *(uint8_t*)0x2047303e = 0x0;
    *(uint8_t*)0x2047303f = 0x0;
    *(uint8_t*)0x20473040 = 0x0;
    *(uint8_t*)0x20473041 = 0x0;
    *(uint8_t*)0x20473042 = 0x0;
    *(uint8_t*)0x20473043 = 0x0;
    *(uint8_t*)0x20473044 = 0x0;
    *(uint8_t*)0x20473045 = 0x0;
    *(uint8_t*)0x20473046 = 0x0;
    *(uint8_t*)0x20473047 = 0x0;
    *(uint8_t*)0x20473048 = 0x0;
    *(uint8_t*)0x20473049 = 0x0;
    *(uint8_t*)0x2047304a = 0x0;
    *(uint8_t*)0x2047304b = 0x0;
    *(uint8_t*)0x2047304c = 0x0;
    *(uint8_t*)0x2047304d = 0x0;
    *(uint8_t*)0x2047304e = 0x0;
    *(uint8_t*)0x2047304f = 0x0;
    *(uint8_t*)0x20473050 = 0x0;
    *(uint8_t*)0x20473051 = 0x0;
    *(uint8_t*)0x20473052 = 0x0;
    *(uint8_t*)0x20473053 = 0x0;
    *(uint8_t*)0x20473054 = 0x0;
    *(uint8_t*)0x20473055 = 0x0;
    *(uint8_t*)0x20473056 = 0x0;
    *(uint8_t*)0x20473057 = 0x0;
    *(uint8_t*)0x20473058 = 0x0;
    *(uint8_t*)0x20473059 = 0x0;
    *(uint8_t*)0x2047305a = 0x0;
    *(uint8_t*)0x2047305b = 0x0;
    *(uint8_t*)0x2047305c = 0x0;
    *(uint8_t*)0x2047305d = 0x0;
    *(uint8_t*)0x2047305e = 0x0;
    *(uint8_t*)0x2047305f = 0x0;
    *(uint8_t*)0x20473060 = 0x0;
    *(uint8_t*)0x20473061 = 0x0;
    *(uint8_t*)0x20473062 = 0x0;
    *(uint8_t*)0x20473063 = 0x0;
    *(uint8_t*)0x20473064 = 0x0;
    *(uint8_t*)0x20473065 = 0x0;
    *(uint8_t*)0x20473066 = 0x0;
    *(uint8_t*)0x20473067 = 0x0;
    syscall(__NR_ioctl, r[2], 0x4068aea3, 0x20473000);
    break;
  case 9:
    r[3] = syscall(__NR_ioctl, r[2], 0xae41, 0x0);
    break;
  case 10:
    memcpy(
        (void*)0x2057b000,
        "\xb3\xa6\x40\x56\x4b\x6b\x3c\x4a\x55\x2c\x1b\xf7\xa0\x97\xe2"
        "\x59\x9a\x7d\xce\xd9\x2e\xdc\x20\xee\xf8\x03\xca\x9a\xb9\xaa"
        "\x95\xad\xa7\x6e\xa3\xbe\x32\xc7\x3b\x52\xbc\x39\x10\xe9\x0c"
        "\x3c\xd9\xb0\x8f\x9f\x0c\x66\xc5\x97\x08\xca\x65\x7f\x5b\x2c"
        "\xd8\x5a\x00\x9f\x83\x4c\xa0\x27\x73\x19\xbe\xcd\xe7\xfa\x12"
        "\x82\x4b\x8c\x67\xf2\x3e\x2e\x4b\x5c\x9a\x5d\x70\xa0\x89\xb1"
        "\x9a\xdb\x31\x9c\x21\x3e\xca\x82\xfd\x2a\x3f\xba\x9e\xcd\x74"
        "\x0e\x45\x04\x8c\x23\xef\xba\xee\x09\x2f\x30\x20\x5a\x1c\x24"
        "\xc0\xf4\x04\x8b\xf5\xa4\xb0\xc4\xf3\xf1\x27\x87\xf1\x4e\x6a"
        "\xc5\x56\xaa\x27\x20\x2e\xb1\xfc\x1b\x53\x62\x90\x37\xa6\x89"
        "\x1e\x0c\x4a\xdb\xc0\x15\x7a\x66\xb4\xee\x4b\x05\x07\x87\x0f"
        "\x2d\x43\xb2\x2a\x43\x37\x11\xd0\x09\x8e\x74\x19\x6c\x34\x10"
        "\x41\x73\x32\xed\x4b\x74\x25\x2b\x0b\x35\xe0\xf9\x93\xc5\x0d"
        "\x1a\xc5\x61\xc8\x86\x2c\xa2\xff\x90\x21\xf0\x14\xd5\xa1\x7a"
        "\x6d\xa1\xa8\x5f\x43\xb1\x3b\x54\xd2\x09\x68\xd3\x58\x9c\x70"
        "\x23\xc5\xe5\x2e\xfc\xe2\x67\xbb\x40\x35\xcc\xfc\xaf\x06\xd8"
        "\xe1\x23\x8a\x43\x21\x4b\x2c\x01\x00\x01\x00\x8e\xf5\xd9\xad"
        "\xeb\xe5\x9d\x34\x4b\x7d\xdb\xc7\x5a\x52\x33\xf7\xe3\xbf\x29"
        "\x43\x8b\x9d\xc6\xb4\x29\x71\xb0\x8f\x7c\x31\x0c\xb7\x89\x30"
        "\x25\xc3\x77\xcd\x55\x2e\x24\x6a\xe7\xca\x3e\x3c\x3e\x2c\x57"
        "\x96\x15\x00\x01\x0d\xca\x5c\x05\x0f\x70\xff\xa0\xcd\x81\x6a"
        "\xaa\xc8\xff\xe4\x3a\xde\x51\xf8\xb2\x28\xf1\x10\x40\xbb\x05"
        "\x6a\x4d\xc0\x9e\x63\x6e\xa6\x3f\x6e\x46\xb9\x60\x35\x55\x53"
        "\x96\x9e\xe6\x22\x99\xd3\x91\xea\x90\x48\x4d\x7f\xc9\x01\x65"
        "\x9a\xb8\xca\x00\xe1\xff\xff\xff\xff\xff\xff\xff\x00\x00\x00"
        "\xe8\x9f\xb7\xba\xd8\x67\x91\x89\x1f\xc5\xeb\x42\x6f\xeb\x1d"
        "\x03\xd0\x65\x59\x8e\x98\x43\xf6\xa6\x41\xe5\x00\x06\xeb\xae"
        "\xa5\xe0\x0d\x55\x4e\xa1\x1b\xd1\x7a\x55\x90\x3b\x55\xa5\xbd"
        "\xdc\x1c\x41\x0d\x8f\xce\x7f\xbc\xf9\x31\xaf\x94\xb0\x7d\x10"
        "\x51\xc0\x7b\x63\xfb\x26\x30\x8a\x45\x13\x8f\x8f\x61\x71\xea"
        "\x68\xba\x25\x03\xa4\x6e\xb6\x81\x07\x8c\xa5\xbc\x21\x2a\xc0"
        "\x3d\x2a\x19\xae\xc1\x37\x04\x8a\xc9\xfd\xb5\x9b\xec\xc8\x6a"
        "\x4d\x32\x7a\x65\x8d\x6d\xb1\x95\x41\x5b\x98\xbc\x9e\x40\xf3"
        "\x1f\x4a\x29\x98\xf7\x0e\xfb\xf5\xb9\x36\x23\xbf\xb6\x8c\x88"
        "\x0f\x29\x5d\x60\xc7\x60\xf0\x14\xbd\x00\x00\x00\xff\x48\x82"
        "\x0d\x62\x44\x0a\x63\x72\x0f\x84\x39\x8b\x0e\xcd\x36\x6e\x33"
        "\x78\x6c\xb7\x03\x45\x99\x9f\x59\x2e\x25\xf9\xa6\xdd\xd1\xf3"
        "\x58\x82\xa9\x6c\x51\x17\x9a\x30\xa7\x98\x5a\xc3\xef\x08\x00"
        "\xb1\x9a\xbc\x81\x2d\x75\xfe\x2d\x1b\x72\xd4\xc3\x7b\x0b\x73"
        "\x11\x01\xb0\xe3\x6c\x55\x7b\x50\x36\xcb\x0f\x55\xf6\xcb\x8b"
        "\xde\x2d\xbd\x5e\xdd\xbb\x1a\x16\x15\xe7\x96\xba\xb2\xf5\xab"
        "\x8a\x81\x6f\x8e\x3f\x7d\x05\xf3\x6e\x0e\xb8\x6a\x76\x2a\x9f"
        "\xb8\xf9\x68\x64\x52\x83\x45\x79\x21\x60\x48\x2c\x4c\x3d\x63"
        "\xb5\x35\xc4\x9c\xb0\xa1\xc7\x15\x41\x42\x19\xbf\xd1\x52\x18"
        "\x06\xf4\xe8\x69\x4f\xd3\x86\x67\xf3\x43\x1c\x29\x8d\x85\x59"
        "\xbc\x04\x6d\x73\xc7\x07\x67\xd0\xed\xa8\x39\xfd\xff\xff\xff"
        "\xff\xff\xff\xfe\xff\xcb\xbe\x29\x00\x00\x00\x08\x70\x79\x16"
        "\xae\xa7\x06\x5c\x6c\x48\xd8\x0a\x6b\x34\x17\x69\x0f\x4a\x7d"
        "\x6b\xa1\x1a\x88\x15\x42\x27\x64\x62\xbb\x0d\xfd\x30\x56\x81"
        "\x15\xcd\x0e\xc4\x1c\x41\x18\x87\x8d\xae\xb1\xea\x2c\x67\xf2"
        "\x07\x1b\xcc\xde\x6a\x20\xef\x04\x7a\xfb\x70\x02\x71\x13\x23"
        "\x52\x2e\x3e\x24\x51\x94\x87\x60\x51\xae\x20\xf6\x6d\x35\x90"
        "\xbf\x1b\xfc\x28\x96\x91\x71\x08\xd4\x22\x02\x09\xfd\x33\xf3"
        "\xda\x7e\x62\x07\xf6\x0a\x18\x7e\x38\x70\x4c\xa0\xb8\xb0\xcc"
        "\xc6\x90\x23\x8d\xfb\x59\x7e\xb3\x3a\x48\xb9\x72\xd6\x35\xfa"
        "\xb0\xdb\x5b\x2c\xda\x45\xdd\x74\x12\x7f\x5f\xa3\x7a\xc2\x7e"
        "\x30\xa6\x71\xae\x4a\xc3\x59\x0e\x99\x88\x71\x3d\xbd\x00\x13"
        "\x75\xdc\x0b\x9f\xf6\x89\x90\x57\x3b\x19\x85\x82\x48\xea\x1d"
        "\xf6\x1d\xbf\xd5\x10\x7c\x87\x21\x23\xb7\xdd\xc9\x11\x07\x6a"
        "\x41\xec\xdc\x97\x39\x08\xbf\x65\x41\x7d\xc1\xc3\x3c\xdc\xd4"
        "\x5d\xf4\xdd\xf4\x38\x90\xae\xe9\x99\xf8\xbb\x2f\x2a\x25\x31"
        "\x5c\xad\x61\x55\x02\x46\x06\x5a\x69\xc6\xf9\x7b\x21\x6a\xb7"
        "\x84\x7b\x0a\x4c\x34\xa3\x6e\xba\x20\x46\x06\x57\xb1\xc3\x69"
        "\xfc\xcc\x12\x1d\x3a\xc2\xc7\x5e\xa6\x4d\xf3\x87\x7b\xca\x56"
        "\x24\x08\xd4\x31\x09\x1c\xb1\x57\x83\x89\xc4\x8d\x25\x12\xf5"
        "\xf8\x08\x20\x4a\x21\x32\x64\xcf\xe8\x90\xc4\x43\x9c\xa9\xf3"
        "\x2e\x0f\x69\xad\x10\x06\xa6\x18\x12\x8d\xbd\xf1\x54\x50\x79"
        "\xd7\x97\x66\x47\x65\xf9\x72\xc2\x21\x26\xdc\xdc\x50\x8d\xbb"
        "\xa6\x01\x00\x43",
        1024);
    syscall(__NR_ioctl, r[3], 0x4400ae8f, 0x2057b000);
    break;
  case 11:
    syscall(__NR_ioctl, r[3], 0xae80, 0x0);
    break;
  case 12:
    *(uint32_t*)0x207e8ffc = 0xe8;
    syscall(__NR_getsockopt, r[1], 0x29, 0x23, 0x2022b000, 0x207e8ffc);
    break;
  case 13:
    memcpy((void*)0x20100ff3, "/selinux/mls", 13);
    syscall(__NR_openat, 0xffffff9c, 0x20100ff3, 0x0, 0x0);
    break;
  case 14:
    memcpy((void*)0x208dfff8, "./file0", 8);
    syscall(__NR_lstat, 0x208dfff8, 0x20e19000);
    break;
  case 15:
    *(uint32_t*)0x20544000 = 0xe8;
    syscall(__NR_getsockopt, 0xffffffff, 0x29, 0x22, 0x20b32000,
            0x20544000);
    break;
  }
}

void test()
{
  memset(r, -1, sizeof(r));
  execute(16);
  collide = 1;
  execute(16);
}

int main()
{
  loop();
  return 0;
}