// autogenerated by syzkaller (http://github.com/google/syzkaller)

#define _GNU_SOURCE

#include <errno.h>
#include <fcntl.h>
#include <fcntl.h>
#include <stdarg.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdio.h>
#include <string.h>
#include <sys/stat.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 <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);
}

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 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 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;
    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 bool write_file(const char* file, const char* what, ...)
{
  char buf[1024];
  va_list args;
  va_start(args, what);
  vsnprintf(buf, sizeof(buf), what, args);
  va_end(args);
  buf[sizeof(buf) - 1] = 0;
  int len = strlen(buf);

  int fd = open(file, O_WRONLY | O_CLOEXEC);
  if (fd == -1)
    return false;
  if (write(fd, buf, len) != len) {
    close(fd);
    return false;
  }
  close(fd);
  return true;
}

static int inject_fault(int nth)
{
  int fd;
  char buf[16];

  fd = open("/proc/thread-self/fail-nth", O_RDWR);
  if (fd == -1)
    exitf("failed to open /proc/thread-self/fail-nth");
  sprintf(buf, "%d", nth + 1);
  if (write(fd, buf, strlen(buf)) != (ssize_t)strlen(buf))
    exitf("failed to write /proc/thread-self/fail-nth");
  return fd;
}

#ifndef __NR_bpf
#define __NR_bpf 321
#endif

long r[25];
void loop()
{
  memset(r, -1, sizeof(r));
  r[0] = syscall(__NR_mmap, 0x20000000ul, 0xfff000ul, 0x3ul, 0x32ul,
                 0xfffffffffffffffful, 0x0ul);
  r[1] = syscall(__NR_socket, 0xaul, 0x2ul, 0x0ul);
  memcpy((void*)0x2081fff3,
         "\x2f\x64\x65\x76\x2f\x6e\x65\x74\x2f\x74\x75\x6e\x00", 13);
  r[3] = syz_open_dev(0x2081fff3ul, 0x0ul, 0x0ul);
  r[4] = syscall(__NR_fcntl, r[3], 0x0ul, r[3]);
  memcpy((void*)0x20db3fd8, "\x67\x72\x65\x30\x00\x00\x00\x00\x00\x00"
                            "\x00\x00\x00\x00\x00\x00",
         16);
  memcpy((void*)0x20db3fe8, "\x16\xf1\xa0\x33\x40\x48\x7c\x87\x35\xdb"
                            "\x24\xa2\x66\x32\x5d\x59",
         16);
  r[7] = syscall(__NR_ioctl, r[4], 0x400454caul, 0x20db3fd8ul);
  *(uint32_t*)0x20236ff0 = (uint32_t)0xffffffffffffffff;
  *(uint32_t*)0x20236ff4 = (uint32_t)0x18;
  *(uint64_t*)0x20236ff8 = (uint64_t)0x20d02000;
  r[11] = syscall(__NR_bpf, 0xful, 0x20236ff0ul, 0x10ul);
  memcpy((void*)0x20481000,
         "\x2f\x64\x65\x76\x2f\x6e\x65\x74\x2f\x74\x75\x6e\x00", 13);
  r[13] = syz_open_dev(0x20481000ul, 0x0ul, 0x0ul);
  r[14] = syscall(__NR_fcntl, r[13], 0x0ul, r[13]);
  memcpy((void*)0x20927fd8, "\x67\x72\x65\x30\x00\x00\x00\x00\x00\x00"
                            "\x00\x00\x00\x00\x00\x00",
         16);
  *(uint32_t*)0x20927fe8 = (uint32_t)0x7de;
  *(uint32_t*)0x20927fec = (uint32_t)0x0;
  *(uint64_t*)0x20927ff0 = (uint64_t)0x2044fff8;
  *(uint32_t*)0x2044fff8 = (uint32_t)0x0;
  *(uint32_t*)0x2044fffc = (uint32_t)0x0;
  r[21] = syscall(__NR_ioctl, r[14], 0x400454caul, 0x20927fd8ul);
  memcpy((void*)0x2062ffe0, "\x67\x72\x65\x30\x00\x00\xe4\x00\x00\x00"
                            "\x00\x00\x00\x00\x00\x00",
         16);
  *(uint16_t*)0x2062fff0 = (uint16_t)0x2fd;
  write_file("/sys/kernel/debug/failslab/ignore-gfp-wait", "N");
  write_file("/sys/kernel/debug/fail_futex/ignore-private", "N");
  inject_fault(3);
  r[24] = syscall(__NR_ioctl, r[1], 0x8914ul, 0x2062ffe0ul);
}

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