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

#define _GNU_SOURCE

#include <errno.h>
#include <fcntl.h>
#include <stdarg.h>
#include <stdbool.h>
#include <stdio.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 <stdint.h>
#include <string.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 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)
    fail("failed to open /proc/thread-self/fail-nth");
  sprintf(buf, "%d", nth + 1);
  if (write(fd, buf, strlen(buf)) != (ssize_t)strlen(buf))
    fail("failed to write /proc/thread-self/fail-nth");
  return fd;
}

long r[21];
void loop()
{
  memset(r, -1, sizeof(r));
  r[0] = syscall(__NR_mmap, 0x20000000ul, 0xa000ul, 0x3ul, 0x32ul,
                 0xfffffffffffffffful, 0x0ul);
  r[1] = syscall(__NR_socket, 0x26ul, 0x5ul, 0x0ul);
  *(uint16_t*)0x20008af7 = (uint16_t)0x26;
  memcpy((void*)0x20008af9,
         "\x73\x6b\x63\x69\x70\x68\x65\x72\x00\x00\x00\x00\x00\x00",
         14);
  *(uint32_t*)0x20008b07 = (uint32_t)0x0;
  *(uint32_t*)0x20008b0b = (uint32_t)0x0;
  memcpy((void*)0x20008b0f,
         "\x65\x63\x62\x28\x61\x65\x73\x29\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\x00\x00"
         "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"
         "\x00\x00\x00\x00",
         64);
  r[7] = syscall(__NR_bind, r[1], 0x20008af7ul, 0x58ul);
  memcpy((void*)0x20007000, "\x86\xab\x27\x39\x1a\x1e\x5d\x23\x4c\xba"
                            "\x60\xdd\x55\x2d\x5c\x42",
         16);
  r[9] = syscall(__NR_setsockopt, r[1], 0x117ul, 0x1ul, 0x20007000ul,
                 0x10ul);
  r[10] = syscall(__NR_accept, r[1], 0x0ul, 0x0ul);
  *(uint64_t*)0x20008000 = (uint64_t)0x20000000;
  *(uint32_t*)0x20008008 = (uint32_t)0x0;
  *(uint64_t*)0x20008010 = (uint64_t)0x20001000;
  *(uint64_t*)0x20008018 = (uint64_t)0x1;
  *(uint64_t*)0x20008020 = (uint64_t)0x20000000;
  *(uint64_t*)0x20008028 = (uint64_t)0x0;
  *(uint32_t*)0x20008030 = (uint32_t)0x0;
  *(uint64_t*)0x20001000 = (uint64_t)0x20008f59;
  *(uint64_t*)0x20001008 = (uint64_t)0x1;
  write_file("/sys/kernel/debug/failslab/ignore-gfp-wait", "N");
  write_file("/sys/kernel/debug/fail_futex/ignore-private", "N");
  inject_fault(1);
  r[20] = syscall(__NR_recvmsg, r[10], 0x20008000ul, 0x0ul);
}

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