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

#define _GNU_SOURCE

#include <endian.h>
#include <errno.h>
#include <fcntl.h>
#include <pthread.h>
#include <setjmp.h>
#include <signal.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/stat.h>
#include <sys/syscall.h>
#include <sys/types.h>
#include <time.h>
#include <unistd.h>

#include <linux/futex.h>

static __thread int skip_segv;
static __thread jmp_buf segv_env;

static void segv_handler(int sig, siginfo_t* info, void* ctx)
{
  uintptr_t addr = (uintptr_t)info->si_addr;
  const uintptr_t prog_start = 1 << 20;
  const uintptr_t prog_end = 100 << 20;
  if (__atomic_load_n(&skip_segv, __ATOMIC_RELAXED) &&
      (addr < prog_start || addr > prog_end)) {
    _longjmp(segv_env, 1);
  }
  exit(sig);
}

static void install_segv_handler(void)
{
  struct sigaction sa;
  memset(&sa, 0, sizeof(sa));
  sa.sa_handler = SIG_IGN;
  syscall(SYS_rt_sigaction, 0x20, &sa, NULL, 8);
  syscall(SYS_rt_sigaction, 0x21, &sa, NULL, 8);
  memset(&sa, 0, sizeof(sa));
  sa.sa_sigaction = segv_handler;
  sa.sa_flags = SA_NODEFER | SA_SIGINFO;
  sigaction(SIGSEGV, &sa, NULL);
  sigaction(SIGBUS, &sa, NULL);
}

#define NONFAILING(...)                                                        \
  {                                                                            \
    __atomic_fetch_add(&skip_segv, 1, __ATOMIC_SEQ_CST);                       \
    if (_setjmp(segv_env) == 0) {                                              \
      __VA_ARGS__;                                                             \
    }                                                                          \
    __atomic_fetch_sub(&skip_segv, 1, __ATOMIC_SEQ_CST);                       \
  }

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 long syz_open_dev(volatile long a0, volatile long a1, volatile long a2)
{
  if (a0 == 0xc || a0 == 0xb) {
    char buf[128];
    sprintf(buf, "/dev/%s/%d:%d", a0 == 0xc ? "char" : "block", (uint8_t)a1,
            (uint8_t)a2);
    return open(buf, O_RDWR, 0);
  } else {
    char buf[1024];
    char* hash;
    NONFAILING(strncpy(buf, (char*)a0, sizeof(buf) - 1));
    buf[sizeof(buf) - 1] = 0;
    while ((hash = strchr(buf, '#'))) {
      *hash = '0' + (char)(a1 % 10);
      a1 /= 10;
    }
    return open(buf, a2, 0);
  }
}

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 loop(void)
{
  int i, call, thread;
  int collide = 0;
again:
  for (call = 0; call < 6; 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 (collide && (call % 2) == 0)
        break;
      event_timedwait(&th->done, 45);
      break;
    }
  }
  for (i = 0; i < 100 && __atomic_load_n(&running, __ATOMIC_RELAXED); i++)
    sleep_ms(1);
  if (!collide) {
    collide = 1;
    goto again;
  }
}

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

void execute_call(int call)
{
  intptr_t res = 0;
  switch (call) {
  case 0:
    NONFAILING(memcpy((void*)0x200002c0, "/dev/kvm\000", 9));
    syscall(__NR_openat, 0xffffffffffffff9cul, 0x200002c0ul, 0ul, 0ul);
    break;
  case 1:
    NONFAILING(memcpy((void*)0x20000180, "/dev/fb0\000", 9));
    res = syscall(__NR_openat, 0xffffffffffffff9cul, 0x20000180ul, 0ul, 0ul);
    if (res != -1)
      r[0] = res;
    break;
  case 2:
    syz_open_dev(0, 5, 0x100);
    break;
  case 3:
    NONFAILING(*(uint32_t*)0x20000000 = 0x504);
    NONFAILING(*(uint32_t*)0x20000004 = 0);
    NONFAILING(*(uint32_t*)0x20000008 = 0);
    NONFAILING(*(uint32_t*)0x2000000c = 0xf0);
    NONFAILING(*(uint32_t*)0x20000010 = 0);
    NONFAILING(*(uint32_t*)0x20000014 = 0);
    NONFAILING(*(uint32_t*)0x20000018 = 4);
    NONFAILING(*(uint32_t*)0x2000001c = 0);
    NONFAILING(*(uint32_t*)0x20000020 = 0);
    NONFAILING(*(uint32_t*)0x20000024 = 0);
    NONFAILING(*(uint32_t*)0x20000028 = 0);
    NONFAILING(*(uint32_t*)0x2000002c = 0);
    NONFAILING(*(uint32_t*)0x20000030 = 0);
    NONFAILING(*(uint32_t*)0x20000034 = 0);
    NONFAILING(*(uint32_t*)0x20000038 = 0);
    NONFAILING(*(uint32_t*)0x2000003c = 0);
    NONFAILING(*(uint32_t*)0x20000040 = 0);
    NONFAILING(*(uint32_t*)0x20000044 = 0);
    NONFAILING(*(uint32_t*)0x20000048 = 0);
    NONFAILING(*(uint32_t*)0x2000004c = 0);
    NONFAILING(*(uint32_t*)0x20000050 = 3);
    NONFAILING(*(uint32_t*)0x20000054 = 0);
    NONFAILING(*(uint32_t*)0x20000058 = 0);
    NONFAILING(*(uint32_t*)0x2000005c = 0);
    NONFAILING(*(uint32_t*)0x20000060 = 0);
    NONFAILING(*(uint32_t*)0x20000064 = 0);
    NONFAILING(*(uint32_t*)0x20000068 = 0);
    NONFAILING(*(uint32_t*)0x2000006c = 0);
    NONFAILING(*(uint32_t*)0x20000070 = 0);
    NONFAILING(*(uint32_t*)0x20000074 = 0);
    NONFAILING(*(uint32_t*)0x20000078 = 0);
    NONFAILING(*(uint32_t*)0x2000007c = 0);
    NONFAILING(*(uint32_t*)0x20000080 = 0);
    NONFAILING(*(uint32_t*)0x20000084 = 0);
    NONFAILING(*(uint32_t*)0x20000088 = 0);
    NONFAILING(*(uint32_t*)0x2000008c = 0);
    NONFAILING(*(uint32_t*)0x20000090 = 0);
    NONFAILING(*(uint32_t*)0x20000094 = 0);
    NONFAILING(*(uint32_t*)0x20000098 = 0);
    NONFAILING(*(uint32_t*)0x2000009c = 0);
    syscall(__NR_ioctl, r[0], 0x4601, 0x20000000ul);
    break;
  case 4:
    res = syz_open_dev(0xc, 4, 1);
    if (res != -1)
      r[1] = res;
    break;
  case 5:
    NONFAILING(*(uint16_t*)0x20000000 = 0x200);
    NONFAILING(*(uint16_t*)0x20000002 = 2);
    NONFAILING(*(uint64_t*)0x20000008 = 0x200000c0);
    NONFAILING(memcpy(
        (void*)0x200000c0,
        "\x00\x00\x40\x5a\xf6\xf3\x09\x14\x7a\x2d\xda\xe3\x85\x53\x0f\x98\x1c"
        "\xc2\x7a\x7a\x3e\x9f\x9d\x4a\x0c\x12\x38\x62\x6f\xbb\xf3\xec\xf5\x0f"
        "\xee\xcb\x38\x3e\x98\xe0\xb2\xfb\x66\x9d\x7a\xcb\x26\x20\x94\x6a\x5c"
        "\x80\xcb\x07\x7c\x8a\xbb\xce\x62\xac\xa2\xb4\x6d\x2e\x9c\x53\x41\x72"
        "\xdb\xa1\x88\x8a\x09\xa0\x66\xe7\xa5\xe6\x18\xf0\x18\x48\x59\x00\x20"
        "\x0e\x4c\x54\x40\x45\xce\xa0\xe3\xea\x53\x31\x63\x28\xa6\x0a\xba\xc5"
        "\x00\x9d\x42\x50\x16\xff\xe4\xc3\xba\x81\x18\x79\x90\x87\xb7\x94\xd2"
        "\x3d\x37\x40\x42\xb4\xd6\x83\x77\x88\xbc\xf7\x35\x94\xf3\xe7\x6c\x3f"
        "\x49\x48\x78\xa8\xc2\x70\xe1\xa6\x46\x8c\x33\xed\x06\xae\x5e\xf2\x01"
        "\x90\x97\x50\x54\xa9\xdb\x93\x97\xd1\xf0\x0f\x02\x77\xc3\xa0\x25\x19"
        "\xf7\x68\x96\xc4\x84\xe8\x80\x6b\xfa\x7d\x5c\x67\xb2\xf2\x18\xb2\xd4"
        "\x50\xd1\x69\x36\x32\xa3\xc8\x83\x04\xcf\x92\xd2\xff\x7f\x00\x00\x00"
        "\x00\x00\x00\x96\xce\x1a\xde\xac\x36\x73\xf7\x5e\xc6\x56\xf2\x03\xcd"
        "\x83\x2f\xbb\xdb\x23\xaa\x83\x69\x98\xa8\x4a\x86\xd1\x74\x7e\xde\xfe"
        "\x0c\x6b\x80\x89\x92\xee\x18\x46\x9a\x5b\x9e\xee\xfe\xb3\x32\x40\x58"
        "\x67\x37\x30\xe0\x9f\x4b\x5c\x3c\x23\x5e\xc3\x9e\xb6\x50\x04\xdf\xd8"
        "\x1d\xaa\x40\x96\x45\x99\xd5\x29\x7b\x6f\xdd\x76\x26\xb9\x6f\x38\x29"
        "\x06\x00\x00\x00\x00\x00\x00\x00\xaf\x89\xd5\x57\x4e\xdb\x15\x58\xd2"
        "\x67\x5a\xf9\xae\x7c\xb0\xfe\x16\x50\x1b\xef\x10\xfa\x06\x77\xcf\xb6"
        "\xce\x0c\x2e\x0b\xa5\xf6\x72\x90\xb8\xbe\xb9\x4f\x0f\xac\x98\x23\xe4"
        "\x24\x43\x62\x0d\x53\x89\x5c\xf6\x69\x50\x96\xc0\x08\xda\xdd\x5f\x3d"
        "\x14\xbb\xaa\xd0\x47\x4b\xfb\x01\x49\x95\xdf\x0a\x97\x0d\x65\x59\x7f"
        "\xf1\x03\x81\xd8\xbb\x3b\xe3\x13\x9a\x4d\x33\xc6\xc2\x1c\xe1\x42\xcd"
        "\x54\xa1\x7b\x7b\x1b\x9b\x89\xaf\x2b\x36\x32\xb0\xd2\x46\x02\x98\x9c"
        "\x74\x74\xf5\x70\x81\x61\x12\xe3\x56\x00\x00\x00\x00\x00\x00\x00\x06"
        "\xcf\xb6\x71\x5e\xef\x4e\x2a\x9b\x37\x21\x1c\x10\x8e\x38\x71\xb9\x2e"
        "\xc1\x1f\x89\xfb\xcc\x7a\x1f\x47\x20\x6d\x5f\xa4\x1f\x46\xcf\x84\x51"
        "\x4c\xdf\x2f\xea\x7e\x99\xc6\x82\xec\xe8\x34\x6e\x5b\x9b\x80\x35\xcb"
        "\x28\x4b\x26\x6a\x42\x81\x8a\x9e\x2d\x5d\x77\x6b\xd1\x0f\x43\x82\x9f"
        "\x59\x13\xe1\x48\x1c\x1f\x93\x99\xe6\xfd\xcc\x88\xc3\x47\x00\x31\x4e"
        "\x5b\x53\x94\x1b\x2a\xe5\x40\xa1\xd2\x60\x0c\x91\xdb\xfe\x88\xd7\x90"
        "\x18\x11\xa2\x88\xa2\x4f\x76\xb1\x7b\x00\x0a\x7f\x6c\xfe\xa9\xb5\xe6"
        "\xbd\x8c\x0a\xfe\x2e\x7f\x1e\x9c\xb9\x3a\xcf\x31\x8f\xee\x28\x9d\xbf"
        "\xb6\xe3\x01\xf8\xaf\x99\x26\x2c\x7c\x66\x0a\x5d\x78\x62\x22\xb6\x6c"
        "\x79\xe8\xac\x39\xc6\x9e\x66\xe5\xd9\xaa\x11\xe3\x4c\x0e\x80\xa4\xa9"
        "\xd7\x71\x60\x3d\xa3\xc1\xb9\xec\x0b\xf7\x5e\x57\x99\x1b\xbe\x04\x41"
        "\x7b\xd9\x09\x3b\xdb\x3e\x3b\xa3\xcc\xc7\x09\xc5\x1f\x9a\xd9\xfa\xb9"
        "\x08\x11\xec\x1f\x9f\xd5\x19\x2d\x16\x85\x9b\x24\x57\xd4\x2c\xaa\xc2"
        "\xc8\x5a\xbd\x6c\xea\x00\x70\x21\xaa\xd5\x03\x6c\x7b\x00\x50\xb6\x31"
        "\x56\xca\x00\x9d\x1b\xff\x2b\x46\x1a\xb0\x25\xa7\x47\x23\x0c\xe5\x51"
        "\x58\x83\x97\xb3\x7a\xda\xd5\x08\xc7\xbb\x23\xef\xb5\xee\x4d\xc5\x78"
        "\xb3\x6d\xda\x45\x96\x78\x00\xdd\xfc\xb3\x3a\x08\xd9\x97\xed\x68\xbf"
        "\x77\x46\xc2\x2e\x20\x00\x94\x11\x49\x1a\xc5\x8c\xb9\x60\x51\x01\x41"
        "\xc7\xc2\xff\x16\x51\xd4\xc9\x35\x82\x1a\x36\x5e\xb2\x21\x47\x65\xa7"
        "\x14\x00\x04\x00\x00\xa4\xe9\x41\x38\x8b\x02\x7c\xc2\x50\x71\x43\xb3"
        "\xfb\xae\xaa\xc2\x06\x87\x34\xdd\xb9\x90\x72\x33\x97\x3f\x36\x1a\x2e"
        "\xea\x42\x33\xe0\x95\xa7\x57\x35\x21\x30\x15\xae\x9f\xe8\x73\xde\x64"
        "\x40\x83\x01\x9b\x39\x40\x72\x25\xde\xe4\xf2\xac\x3d\x9d\xb5\xbf\xaa"
        "\x03\xc1\x6a\x39\x4d\x99\xa0\xfa\x1f\xf8\xa0\xfb\x45\x09\x6a\xe3\xe3"
        "\xac\x48\x5a\x91\x2f\x47\x95\x64\x8d\x64\x98\x0b\xeb\x6a\x4a\x84\x3f"
        "\x61\x43\xf5\xf6\x91\x83\x9b\x67\x60\x6f\x3d\x10\xd7\x37\x75\x44\x5b"
        "\x54\x16\x20\x29\x09\xb3\xbe\x00\x00\x9e\xe1\x9e\xe0\x12\xff\x20\xa7"
        "\xb3\x66\xec\x4a\x9c\x8b\xc8\x10\xfa\x1a\xa3\x1f\x1d\xf2\x19\x33\xf4"
        "\x68\x84\xc2\xeb\x36\xf0\x45\xd2\x8a\xae\x8c\x13\xf3\x0e\xa2\x39\xd8"
        "\xcd\x6d\x51\x2f\xfc\xa3\x93\x2a\x70\xb9\x81\x9c\x8f\x60\x25\x94\x18"
        "\x3f\xa9\x54\x5c\x94\x85\x50\xc1\x06\x22\x8a\x03\x06\x85\x38\xff\x03"
        "\x00\x00\xce\x92\x16\xc3\x6f\xb4\x13\x5f\xc8\x88\x4c\x34\xe0\x51\x47"
        "\x50\x6e\xa8\xbd\x59\x31\xf8\x80\x00\x00\x00\x00\x00\x00\x00\x00\x00"
        "\x00\x00\xc0\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"
        "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"
        "\x00\x00\x00\x00",
        1024));
    syscall(__NR_ioctl, r[1], 0x4b6c, 0x20000000ul);
    break;
  }
}
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);
  install_segv_handler();
  loop();
  return 0;
}