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

#define _GNU_SOURCE
#include <endian.h>
#include <linux/futex.h>
#include <pthread.h>
#include <stdlib.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 <errno.h>
#include <stdarg.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/stat.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);
}

#define BITMASK_LEN(type, bf_len) (type)((1ull << (bf_len)) - 1)

#define BITMASK_LEN_OFF(type, bf_off, bf_len)                                  \
  (type)(BITMASK_LEN(type, (bf_len)) << (bf_off))

#define STORE_BY_BITMASK(type, addr, val, bf_off, bf_len)                      \
  if ((bf_off) == 0 && (bf_len) == 0) {                                        \
    *(type*)(addr) = (type)(val);                                              \
  } else {                                                                     \
    type new_val = *(type*)(addr);                                             \
    new_val &= ~BITMASK_LEN_OFF(type, (bf_off), (bf_len));                     \
    new_val |= ((type)(val)&BITMASK_LEN(type, (bf_len))) << (bf_off);          \
    *(type*)(addr) = new_val;                                                  \
  }

static void use_temporary_dir()
{
  char tmpdir_template[] = "./syzkaller.XXXXXX";
  char* tmpdir = mkdtemp(tmpdir_template);
  if (!tmpdir)
    fail("failed to mkdtemp");
  if (chmod(tmpdir, 0777))
    fail("failed to chmod");
  if (chdir(tmpdir))
    fail("failed to chdir");
}

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 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);
        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_userfaultfd
#define __NR_userfaultfd 323
#endif

uint64_t r[8] = {0xffffffffffffffff, 0xffffffffffffffff,
                 0xffffffffffffffff, 0x0,
                 0xffffffffffffffff, 0xffffffffffffffff,
                 0xffffffffffffffff, 0xffffffffffffffff};
void execute_call(int call)
{
  long res;
  switch (call) {
  case 0:
    res = syscall(__NR_socket, 0xd, 1, 0);
    if (res != -1)
      r[0] = res;
    break;
  case 1:
    memcpy((void*)0x200000c0, "/dev/rfkill", 12);
    res = syscall(__NR_openat, 0xffffffffffffff9c, 0x200000c0, 0, 0);
    if (res != -1)
      r[1] = res;
    break;
  case 2:
    memcpy((void*)0x20000100, "./file0", 8);
    syscall(__NR_openat, r[1], 0x20000100, 0x800, 1);
    break;
  case 3:
    res = syscall(__NR_socket, 2, 2, 0);
    if (res != -1)
      r[2] = res;
    break;
  case 4:
    *(uint32_t*)0x20000080 = htobe32(0xe0000001);
    *(uint8_t*)0x20000084 = 0xac;
    *(uint8_t*)0x20000085 = 0x14;
    *(uint8_t*)0x20000086 = 0x14;
    *(uint8_t*)0x20000087 = 0xd;
    *(uint32_t*)0x20000088 = htobe32(0);
    syscall(__NR_setsockopt, r[2], 0, 0x27, 0x20000080, 0xc);
    break;
  case 5:
    syscall(__NR_ioctl, r[0], 0x8912, 0x20000240);
    break;
  case 6:
    *(uint32_t*)0x20000200 = 0x80;
    res = syscall(__NR_getsockname, r[1], 0x20000280, 0x20000200);
    if (res != -1)
      r[3] = *(uint32_t*)0x20000284;
    break;
  case 7:
    *(uint8_t*)0x20000300 = 0xfe;
    *(uint8_t*)0x20000301 = 0x80;
    *(uint8_t*)0x20000302 = 0;
    *(uint8_t*)0x20000303 = 0;
    *(uint8_t*)0x20000304 = 0;
    *(uint8_t*)0x20000305 = 0;
    *(uint8_t*)0x20000306 = 0;
    *(uint8_t*)0x20000307 = 0;
    *(uint8_t*)0x20000308 = 0;
    *(uint8_t*)0x20000309 = 0;
    *(uint8_t*)0x2000030a = 0;
    *(uint8_t*)0x2000030b = 0;
    *(uint8_t*)0x2000030c = 0;
    *(uint8_t*)0x2000030d = 0;
    *(uint8_t*)0x2000030e = 0;
    *(uint8_t*)0x2000030f = 0x16;
    *(uint32_t*)0x20000310 = 3;
    *(uint32_t*)0x20000314 = r[3];
    syscall(__NR_ioctl, r[0], 0x8936, 0x20000300);
    break;
  case 8:
    res = syscall(__NR_socket, 2, 2, 0);
    if (res != -1)
      r[4] = res;
    break;
  case 9:
    *(uint32_t*)0x20000040 = htobe32(0xe0000002);
    *(uint8_t*)0x20000044 = 0xac;
    *(uint8_t*)0x20000045 = 0x14;
    *(uint8_t*)0x20000046 = 0x14;
    *(uint8_t*)0x20000047 = 0xd;
    *(uint32_t*)0x20000048 = htobe32(0);
    syscall(__NR_setsockopt, r[4], 0, 0x27, 0x20000040, 0xc);
    break;
  case 10:
    *(uint32_t*)0x20b5a000 = 2;
    *(uint32_t*)0x20b5a004 = 0x70;
    *(uint8_t*)0x20b5a008 = 0xe2;
    *(uint8_t*)0x20b5a009 = 0;
    *(uint8_t*)0x20b5a00a = 0;
    *(uint8_t*)0x20b5a00b = 0;
    *(uint32_t*)0x20b5a00c = 0;
    *(uint64_t*)0x20b5a010 = 0;
    *(uint64_t*)0x20b5a018 = 0;
    *(uint64_t*)0x20b5a020 = 0;
    STORE_BY_BITMASK(uint64_t, 0x20b5a028, 0, 0, 1);
    STORE_BY_BITMASK(uint64_t, 0x20b5a028, 0, 1, 1);
    STORE_BY_BITMASK(uint64_t, 0x20b5a028, 0, 2, 1);
    STORE_BY_BITMASK(uint64_t, 0x20b5a028, 0, 3, 1);
    STORE_BY_BITMASK(uint64_t, 0x20b5a028, 0, 4, 1);
    STORE_BY_BITMASK(uint64_t, 0x20b5a028, 0xffff, 5, 1);
    STORE_BY_BITMASK(uint64_t, 0x20b5a028, 0, 6, 1);
    STORE_BY_BITMASK(uint64_t, 0x20b5a028, 0, 7, 1);
    STORE_BY_BITMASK(uint64_t, 0x20b5a028, 0, 8, 1);
    STORE_BY_BITMASK(uint64_t, 0x20b5a028, 0, 9, 1);
    STORE_BY_BITMASK(uint64_t, 0x20b5a028, 0, 10, 1);
    STORE_BY_BITMASK(uint64_t, 0x20b5a028, 0, 11, 1);
    STORE_BY_BITMASK(uint64_t, 0x20b5a028, 0, 12, 1);
    STORE_BY_BITMASK(uint64_t, 0x20b5a028, 0, 13, 1);
    STORE_BY_BITMASK(uint64_t, 0x20b5a028, 0, 14, 1);
    STORE_BY_BITMASK(uint64_t, 0x20b5a028, 0, 15, 2);
    STORE_BY_BITMASK(uint64_t, 0x20b5a028, 0, 17, 1);
    STORE_BY_BITMASK(uint64_t, 0x20b5a028, 0, 18, 1);
    STORE_BY_BITMASK(uint64_t, 0x20b5a028, 0, 19, 1);
    STORE_BY_BITMASK(uint64_t, 0x20b5a028, 0, 20, 1);
    STORE_BY_BITMASK(uint64_t, 0x20b5a028, 0, 21, 1);
    STORE_BY_BITMASK(uint64_t, 0x20b5a028, 0, 22, 1);
    STORE_BY_BITMASK(uint64_t, 0x20b5a028, 0, 23, 1);
    STORE_BY_BITMASK(uint64_t, 0x20b5a028, 0, 24, 1);
    STORE_BY_BITMASK(uint64_t, 0x20b5a028, 0, 25, 1);
    STORE_BY_BITMASK(uint64_t, 0x20b5a028, 0, 26, 1);
    STORE_BY_BITMASK(uint64_t, 0x20b5a028, 0, 27, 1);
    STORE_BY_BITMASK(uint64_t, 0x20b5a028, 0, 28, 1);
    STORE_BY_BITMASK(uint64_t, 0x20b5a028, 0, 29, 35);
    *(uint32_t*)0x20b5a030 = 0;
    *(uint32_t*)0x20b5a034 = 0;
    *(uint64_t*)0x20b5a038 = 0x20000000;
    *(uint64_t*)0x20b5a040 = 0;
    *(uint64_t*)0x20b5a048 = 0x400;
    *(uint64_t*)0x20b5a050 = 0;
    *(uint32_t*)0x20b5a058 = 0;
    *(uint32_t*)0x20b5a05c = 0;
    *(uint64_t*)0x20b5a060 = 0;
    *(uint32_t*)0x20b5a068 = 0;
    *(uint16_t*)0x20b5a06c = 0;
    *(uint16_t*)0x20b5a06e = 0;
    syscall(__NR_perf_event_open, 0x20b5a000, 0, -1, -1, 0);
    break;
  case 11:
    *(uint32_t*)0x200001c0 = 0x48;
    syscall(__NR_accept4, r[0], 0x20000140, 0x200001c0, 0x80000);
    break;
  case 12:
    res = syscall(__NR_userfaultfd, 0x80000);
    if (res != -1)
      r[5] = res;
    break;
  case 13:
    *(uint64_t*)0x20fcdfe8 = 0xaa;
    *(uint64_t*)0x20fcdff0 = 0;
    *(uint64_t*)0x20fcdff8 = 0;
    syscall(__NR_ioctl, r[5], 0xc018aa3f, 0x20fcdfe8);
    break;
  case 14:
    syscall(__NR_mmap, 0x20000000, 0xfff000, 3, 0x32, -1, 0);
    break;
  case 15:
    syscall(__NR_socket, 0x18, 1, 1);
    break;
  case 16:
    syscall(__NR_socket, 0x10, 3, 0x15);
    break;
  case 17:
    res = syscall(__NR_socket, 2, 2, 0);
    if (res != -1)
      r[6] = res;
    break;
  case 18:
    res = syscall(__NR_socket, 0x18, 1, 1);
    if (res != -1)
      r[7] = res;
    break;
  case 19:
    *(uint16_t*)0x20e92000 = 0x18;
    *(uint32_t*)0x20e92002 = 1;
    *(uint32_t*)0x20e92006 = 0;
    *(uint32_t*)0x20e9200a = r[6];
    *(uint16_t*)0x20e9200e = 2;
    *(uint16_t*)0x20e92010 = htobe16(1);
    *(uint8_t*)0x20e92012 = 0xac;
    *(uint8_t*)0x20e92013 = 0x14;
    *(uint8_t*)0x20e92014 = 0;
    *(uint8_t*)0x20e92015 = 0xbb;
    *(uint8_t*)0x20e92016 = 0;
    *(uint8_t*)0x20e92017 = 0;
    *(uint8_t*)0x20e92018 = 0;
    *(uint8_t*)0x20e92019 = 0;
    *(uint8_t*)0x20e9201a = 0;
    *(uint8_t*)0x20e9201b = 0;
    *(uint8_t*)0x20e9201c = 0;
    *(uint8_t*)0x20e9201d = 0;
    *(uint32_t*)0x20e9201e = 2;
    *(uint32_t*)0x20e92022 = 0;
    *(uint32_t*)0x20e92026 = 4;
    *(uint32_t*)0x20e9202a = 0;
    syscall(__NR_connect, r[7], 0x20e92000, 0x2e);
    break;
  case 20:
    syscall(__NR_close, r[6]);
    break;
  case 21:
    syscall(__NR_close, r[7]);
    break;
  }
}

void loop()
{
  execute(22);
}

int main()
{
  syscall(__NR_mmap, 0x20000000, 0x1000000, 3, 0x32, -1, 0);
  use_temporary_dir();
  loop();
  return 0;
}