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

#define _GNU_SOURCE
#include <endian.h>
#include <fcntl.h>
#include <linux/futex.h>
#include <pthread.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/stat.h>
#include <sys/syscall.h>
#include <unistd.h>

__attribute__((noreturn)) static void doexit(int status)
{
  volatile unsigned i;
  syscall(__NR_exit_group, status);
  for (i = 0;; i++) {
  }
}
#include <setjmp.h>
#include <signal.h>
#include <stdint.h>
#include <string.h>
#include <string.h>

static __thread int skip_segv;
static __thread jmp_buf segv_env;

static void segv_handler(int sig, siginfo_t* info, void* uctx)
{
  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);
  }
  doexit(sig);
}

static void install_segv_handler()
{
  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 uintptr_t syz_open_dev(uintptr_t a0, uintptr_t a1, uintptr_t 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)));
    buf[sizeof(buf) - 1] = 0;
    while ((hash = strchr(buf, '#'))) {
      *hash = '0' + (char)(a1 % 10);
      a1 /= 10;
    }
    return open(buf, a2, 0);
  }
}

static void test();

void loop()
{
  while (1) {
    test();
  }
}

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_getuid
#define __NR_getuid 24
#endif
#ifndef __NR_fgetxattr
#define __NR_fgetxattr 231
#endif
#ifndef __NR_setsockopt
#define __NR_setsockopt 366
#endif
#ifndef __NR_getsockopt
#define __NR_getsockopt 365
#endif
#ifndef __NR_fcntl
#define __NR_fcntl 55
#endif
#ifndef __NR_mmap
#define __NR_mmap 192
#endif
#ifndef __NR_sendmmsg
#define __NR_sendmmsg 345
#endif
#ifndef __NR_ioctl
#define __NR_ioctl 54
#endif
#ifndef __NR_getgid
#define __NR_getgid 47
#endif
#ifndef __NR_read
#define __NR_read 3
#endif
#undef __NR_mmap
#define __NR_mmap __NR_mmap2

long r[7];
void execute_call(int call)
{
  switch (call) {
  case 0:
    syscall(__NR_mmap, 0x20000000, 0xfff000, 3, 0x32, -1, 0);
    break;
  case 1:
    NONFAILING(memcpy((void*)0x20005000, "/dev/sg#", 9));
    r[0] = syz_open_dev(0x20005000, 0, 0);
    break;
  case 2:
    NONFAILING(memcpy((void*)0x20d89fe7, "system.posix_acl_default", 25));
    syscall(__NR_fgetxattr, r[0], 0x20d89fe7, 0x20a62000, 0);
    break;
  case 3:
    NONFAILING(*(uint32_t*)0x200be000 = 0x203eeff6);
    NONFAILING(*(uint32_t*)0x200be004 = 0xa);
    NONFAILING(*(uint32_t*)0x200be008 = 0x20603000);
    NONFAILING(*(uint32_t*)0x200be00c = 1);
    NONFAILING(*(uint32_t*)0x200be010 = 0x20b88000);
    NONFAILING(*(uint32_t*)0x200be014 = 0);
    NONFAILING(*(uint32_t*)0x200be018 = 0);
    NONFAILING(*(uint16_t*)0x203eeff6 = 0);
    NONFAILING(memcpy((void*)0x203eeff8, "./file0", 8));
    NONFAILING(*(uint32_t*)0x20603000 = 0x20007000);
    NONFAILING(*(uint32_t*)0x20603004 = 0);
    syscall(__NR_sendmmsg, -1, 0x200be000, 1, 0);
    break;
  case 4:
    if (syscall(__NR_ioctl, r[0], 0x8903, 0x201d0000) != -1)
      NONFAILING(r[1] = *(uint32_t*)0x201d0000);
    break;
  case 5:
    NONFAILING(*(uint32_t*)0x20704000 = 0x1000);
    syscall(__NR_setsockopt, r[0], 1, 5, 0x20704000, 4);
    break;
  case 6:
    NONFAILING(*(uint32_t*)0x2009a000 = 0xe8);
    if (syscall(__NR_getsockopt, r[0], 0x29, 0x23, 0x20dff000, 0x2009a000) !=
        -1)
      NONFAILING(r[2] = *(uint32_t*)0x20dff034);
    break;
  case 7:
    r[3] = syscall(__NR_getgid);
    break;
  case 8:
    r[4] = syscall(__NR_fcntl, r[0], 9);
    break;
  case 9:
    r[5] = syscall(__NR_getuid);
    break;
  case 10:
    if (syscall(__NR_getsockopt, r[0], 1, 0x11, 0x20d36ff4, 0xc) != -1)
      NONFAILING(r[6] = *(uint32_t*)0x20d36ffc);
    break;
  case 11:
    NONFAILING(*(uint32_t*)0x20a42fc8 = 0x20a10e41);
    NONFAILING(*(uint32_t*)0x20a42fcc = 8);
    NONFAILING(*(uint32_t*)0x20a42fd0 = 0x200b3ff0);
    NONFAILING(*(uint32_t*)0x20a42fd4 = 1);
    NONFAILING(*(uint32_t*)0x20a42fd8 = 0x2087e000);
    NONFAILING(*(uint32_t*)0x20a42fdc = 0x40);
    NONFAILING(*(uint32_t*)0x20a42fe0 = 0x4000);
    NONFAILING(*(uint16_t*)0x20a10e41 = 1);
    NONFAILING(*(uint8_t*)0x20a10e43 = 0);
    NONFAILING(*(uint32_t*)0x20a10e45 = 0x4e23);
    NONFAILING(*(uint32_t*)0x200b3ff0 = 0x20962f5c);
    NONFAILING(*(uint32_t*)0x200b3ff4 = 0xa4);
    NONFAILING(memcpy(
        (void*)0x20962f5c,
        "\x16\x81\xc9\xb9\xfe\x3f\xf5\xa2\x86\xb7\x81\x48\x23\xdf\x4c\xf7\x42"
        "\x57\x86\x3c\xb6\xe0\x4e\x37\x81\xd9\x6e\xcd\xff\x3e\xdc\xc4\x1d\xb1"
        "\xc0\x91\xb8\x7c\x8c\x4e\xa0\xc0\xf1\x2e\xfc\xa0\x4b\xc8\xe7\x51\x32"
        "\xc1\xf4\xc0\xba\xaf\xda\x6a\x06\x6a\x28\xf9\xd5\x4f\x5e\x76\xc8\x37"
        "\x84\x1f\xd4\x47\xe0\x90\x4b\x92\x42\x23\x03\xdc\xba\xdb\xb3\xcd\x59"
        "\x05\x14\x7c\x49\x03\x30\x3a\x92\x55\x52\x1a\x63\x40\x9c\xe0\x3f\xe3"
        "\x3a\xd0\x45\xe0\xe7\x0f\x02\x29\x2b\x5a\x86\x9c\x6c\xf9\x9e\x62\xc3"
        "\xf9\xcc\xe7\xca\xc3\xc4\x2a\x5c\x0a\x1f\x85\x1b\x35\x47\xc8\x41\xdd"
        "\xf5\xbc\x91\xf1\x9f\x7c\x38\xf2\x09\x76\xe9\x66\x49\x6a\x4b\xea\xfb"
        "\x53\xe1\x5b\xa9\x19\xdf\xcf\xe6\x4b\x63\x87",
        164));
    NONFAILING(*(uint32_t*)0x2087e000 = 0x18);
    NONFAILING(*(uint32_t*)0x2087e004 = 1);
    NONFAILING(*(uint32_t*)0x2087e008 = 2);
    NONFAILING(*(uint32_t*)0x2087e00c = r[1]);
    NONFAILING(*(uint32_t*)0x2087e010 = r[2]);
    NONFAILING(*(uint32_t*)0x2087e014 = r[3]);
    NONFAILING(*(uint32_t*)0x2087e018 = 0x10);
    NONFAILING(*(uint32_t*)0x2087e01c = 1);
    NONFAILING(*(uint32_t*)0x2087e020 = 1);
    NONFAILING(*(uint32_t*)0x2087e024 = r[0]);
    NONFAILING(*(uint32_t*)0x2087e028 = 0x18);
    NONFAILING(*(uint32_t*)0x2087e02c = 1);
    NONFAILING(*(uint32_t*)0x2087e030 = 2);
    NONFAILING(*(uint32_t*)0x2087e034 = r[4]);
    NONFAILING(*(uint32_t*)0x2087e038 = r[5]);
    NONFAILING(*(uint32_t*)0x2087e03c = r[6]);
    syscall(__NR_sendmmsg, r[0], 0x20a42fc8, 1, 0x4000040);
    break;
  case 12:
    NONFAILING(*(uint8_t*)0x20583ff4 = 2);
    NONFAILING(*(uint16_t*)0x20583ff6 = 0x100);
    NONFAILING(*(uint16_t*)0x20583ff8 = 0xa23);
    NONFAILING(*(uint16_t*)0x20583ffa = 0x1f);
    NONFAILING(*(uint16_t*)0x20583ffc = 0xba);
    NONFAILING(*(uint16_t*)0x20583ffe = 0);
    syscall(__NR_ioctl, r[0], 0x541c, 0x20583ff4);
    break;
  case 13:
    syscall(__NR_ioctl, r[0], 0x2285, 0x20007000);
    break;
  case 14:
    syscall(__NR_read, r[0], 0x20b07000, 0);
    break;
  }
}

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

int main()
{
  install_segv_handler();
  for (;;) {
    loop();
  }
}