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

#define _GNU_SOURCE
#include <dirent.h>
#include <endian.h>
#include <errno.h>
#include <signal.h>
#include <stdarg.h>
#include <stdio.h>
#include <sys/mount.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 <errno.h>
#include <setjmp.h>
#include <signal.h>
#include <stdarg.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.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);
}

static void exitf(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(kRetryStatus);
}

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

static void remove_dir(const char* dir)
{
  DIR* dp;
  struct dirent* ep;
  int iter = 0;
retry:
  dp = opendir(dir);
  if (dp == NULL) {
    if (errno == EMFILE) {
      exitf("opendir(%s) failed due to NOFILE, exiting", dir);
    }
    exitf("opendir(%s) failed", dir);
  }
  while ((ep = readdir(dp))) {
    if (strcmp(ep->d_name, ".") == 0 || strcmp(ep->d_name, "..") == 0)
      continue;
    char filename[FILENAME_MAX];
    snprintf(filename, sizeof(filename), "%s/%s", dir, ep->d_name);
    struct stat st;
    if (lstat(filename, &st))
      exitf("lstat(%s) failed", filename);
    if (S_ISDIR(st.st_mode)) {
      remove_dir(filename);
      continue;
    }
    int i;
    for (i = 0;; i++) {
      if (unlink(filename) == 0)
        break;
      if (errno == EROFS) {
        break;
      }
      if (errno != EBUSY || i > 100)
        exitf("unlink(%s) failed", filename);
      if (umount2(filename, MNT_DETACH))
        exitf("umount(%s) failed", filename);
    }
  }
  closedir(dp);
  int i;
  for (i = 0;; i++) {
    if (rmdir(dir) == 0)
      break;
    if (i < 100) {
      if (errno == EROFS) {
        break;
      }
      if (errno == EBUSY) {
        if (umount2(dir, MNT_DETACH))
          exitf("umount(%s) failed", dir);
        continue;
      }
      if (errno == ENOTEMPTY) {
        if (iter < 100) {
          iter++;
          goto retry;
        }
      }
    }
    exitf("rmdir(%s) failed", dir);
  }
}

static void test();

void loop()
{
  int iter;
  for (iter = 0;; iter++) {
    char cwdbuf[256];
    sprintf(cwdbuf, "./%d", iter);
    if (mkdir(cwdbuf, 0777))
      fail("failed to mkdir");
    int pid = fork();
    if (pid < 0)
      fail("loop fork failed");
    if (pid == 0) {
      prctl(PR_SET_PDEATHSIG, SIGKILL, 0, 0, 0);
      setpgrp();
      if (chdir(cwdbuf))
        fail("failed to chdir");
      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;
      }
    }
    remove_dir(cwdbuf);
  }
}

long r[3];
void test()
{
  memset(r, -1, sizeof(r));
  syscall(__NR_mmap, 0x20000000, 0xe7d000, 3, 0x32, -1, 0);
  if (syscall(__NR_ioctl, 0xffffff9c, 0x8903, 0x2087d000) != -1)
    NONFAILING(r[0] = *(uint32_t*)0x2087d000);
  NONFAILING(*(uint64_t*)0x20e7b000 = 0x20e7bfc9);
  NONFAILING(*(uint64_t*)0x20e7b008 = 0x37);
  NONFAILING(*(uint64_t*)0x20e7b010 = 0x20e7b000);
  NONFAILING(*(uint64_t*)0x20e7b018 = 0x7d);
  syscall(__NR_preadv, -1, 0x20e7b000, 2, 0);
  syscall(__NR_tgkill, r[0], 0, 0);
  r[1] = syscall(__NR_socket, 0xa, 2, 0);
  NONFAILING(*(uint16_t*)0x20e71000 = 0xa);
  NONFAILING(*(uint16_t*)0x20e71002 = htobe16(0x4e20));
  NONFAILING(*(uint32_t*)0x20e71004 = 0x45da8146);
  NONFAILING(*(uint8_t*)0x20e71008 = 0xfe);
  NONFAILING(*(uint8_t*)0x20e71009 = 0x80);
  NONFAILING(*(uint8_t*)0x20e7100a = 0);
  NONFAILING(*(uint8_t*)0x20e7100b = 0);
  NONFAILING(*(uint8_t*)0x20e7100c = 0);
  NONFAILING(*(uint8_t*)0x20e7100d = 0);
  NONFAILING(*(uint8_t*)0x20e7100e = 0);
  NONFAILING(*(uint8_t*)0x20e7100f = 0);
  NONFAILING(*(uint8_t*)0x20e71010 = 0);
  NONFAILING(*(uint8_t*)0x20e71011 = 0);
  NONFAILING(*(uint8_t*)0x20e71012 = 0);
  NONFAILING(*(uint8_t*)0x20e71013 = 0);
  NONFAILING(*(uint8_t*)0x20e71014 = 0);
  NONFAILING(*(uint8_t*)0x20e71015 = 0);
  NONFAILING(*(uint8_t*)0x20e71016 = 0);
  NONFAILING(*(uint8_t*)0x20e71017 = 0x11);
  NONFAILING(*(uint32_t*)0x20e71018 = 2);
  syscall(__NR_bind, r[1], 0x20e71000, 0x1c);
  NONFAILING(*(uint16_t*)0x2035d000 = 0xa);
  NONFAILING(*(uint16_t*)0x2035d002 = htobe16(0x4e23));
  NONFAILING(*(uint32_t*)0x2035d004 = 6);
  NONFAILING(*(uint8_t*)0x2035d008 = 0);
  NONFAILING(*(uint8_t*)0x2035d009 = 0);
  NONFAILING(*(uint8_t*)0x2035d00a = 0);
  NONFAILING(*(uint8_t*)0x2035d00b = 0);
  NONFAILING(*(uint8_t*)0x2035d00c = 0);
  NONFAILING(*(uint8_t*)0x2035d00d = 0);
  NONFAILING(*(uint8_t*)0x2035d00e = 0);
  NONFAILING(*(uint8_t*)0x2035d00f = 0);
  NONFAILING(*(uint8_t*)0x2035d010 = 0);
  NONFAILING(*(uint8_t*)0x2035d011 = 0);
  NONFAILING(*(uint8_t*)0x2035d012 = -1);
  NONFAILING(*(uint8_t*)0x2035d013 = -1);
  NONFAILING(*(uint8_t*)0x2035d014 = 0xac);
  NONFAILING(*(uint8_t*)0x2035d015 = 0x14);
  NONFAILING(*(uint8_t*)0x2035d016 = 0);
  NONFAILING(*(uint8_t*)0x2035d017 = 0x10);
  NONFAILING(*(uint32_t*)0x2035d018 = 1);
  syscall(__NR_connect, r[1], 0x2035d000, 0x1c);
  r[2] = syscall(__NR_socket, 0x18, 1, 1);
  NONFAILING(*(uint16_t*)0x205fafd2 = 0x18);
  NONFAILING(*(uint32_t*)0x205fafd4 = 1);
  NONFAILING(*(uint32_t*)0x205fafd8 = 0);
  NONFAILING(*(uint32_t*)0x205fafdc = r[1]);
  NONFAILING(*(uint16_t*)0x205fafe0 = 2);
  NONFAILING(*(uint16_t*)0x205fafe2 = htobe16(0x4e21));
  NONFAILING(*(uint32_t*)0x205fafe4 = htobe32(0xe0000002));
  NONFAILING(*(uint8_t*)0x205fafe8 = 0);
  NONFAILING(*(uint8_t*)0x205fafe9 = 0);
  NONFAILING(*(uint8_t*)0x205fafea = 0);
  NONFAILING(*(uint8_t*)0x205fafeb = 0);
  NONFAILING(*(uint8_t*)0x205fafec = 0);
  NONFAILING(*(uint8_t*)0x205fafed = 0);
  NONFAILING(*(uint8_t*)0x205fafee = 0);
  NONFAILING(*(uint8_t*)0x205fafef = 0);
  NONFAILING(*(uint32_t*)0x205faff0 = 4);
  NONFAILING(*(uint32_t*)0x205faff4 = 0);
  NONFAILING(*(uint32_t*)0x205faff8 = 2);
  NONFAILING(*(uint32_t*)0x205faffc = 0);
  syscall(__NR_connect, r[2], 0x205fafd2, 0x2e);
  NONFAILING(*(uint64_t*)0x2037ffc8 = 0x209dd000);
  NONFAILING(*(uint32_t*)0x2037ffd0 = 0xc);
  NONFAILING(*(uint64_t*)0x2037ffd8 = 0x202ceff0);
  NONFAILING(*(uint64_t*)0x2037ffe0 = 1);
  NONFAILING(*(uint64_t*)0x2037ffe8 = 0);
  NONFAILING(*(uint64_t*)0x2037fff0 = 0);
  NONFAILING(*(uint32_t*)0x2037fff8 = 0x8820);
  NONFAILING(*(uint16_t*)0x209dd000 = 0x10);
  NONFAILING(*(uint16_t*)0x209dd002 = 0);
  NONFAILING(*(uint32_t*)0x209dd004 = 0);
  NONFAILING(*(uint32_t*)0x209dd008 = 2);
  NONFAILING(*(uint64_t*)0x202ceff0 = 0x2097b000);
  NONFAILING(*(uint64_t*)0x202ceff8 = 0xfff1);
  NONFAILING(*(uint32_t*)0x2097b000 = 0x10);
  NONFAILING(*(uint16_t*)0x2097b004 = 0x14);
  NONFAILING(*(uint16_t*)0x2097b006 = 0x200);
  NONFAILING(*(uint32_t*)0x2097b008 = 0x70bd26);
  NONFAILING(*(uint32_t*)0x2097b00c = 0x25dfdbfe);
  syscall(__NR_sendmsg, r[2], 0x2037ffc8, 0x81);
}

int main()
{
  install_segv_handler();
  char* cwd = get_current_dir_name();
  for (;;) {
    if (chdir(cwd))
      fail("failed to chdir");
    use_temporary_dir();
    loop();
  }
}