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

#define _GNU_SOURCE

#include <endian.h>
#include <errno.h>
#include <fcntl.h>
#include <stdarg.h>
#include <stdbool.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/stat.h>
#include <sys/syscall.h>
#include <sys/types.h>
#include <unistd.h>

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) {
    int err = errno;
    close(fd);
    errno = err;
    return false;
  }
  close(fd);
  return true;
}

static int inject_fault(int nth)
{
  int fd;
  fd = open("/proc/thread-self/fail-nth", O_RDWR);
  if (fd == -1)
    exit(1);
  char buf[16];
  sprintf(buf, "%d", nth);
  if (write(fd, buf, strlen(buf)) != (ssize_t)strlen(buf))
    exit(1);
  return fd;
}

static void setup_fault()
{
  static struct {
    const char* file;
    const char* val;
    bool fatal;
  } files[] = {
      {"/sys/kernel/debug/failslab/ignore-gfp-wait", "N", true},
      {"/sys/kernel/debug/fail_futex/ignore-private", "N", false},
      {"/sys/kernel/debug/fail_page_alloc/ignore-gfp-highmem", "N", false},
      {"/sys/kernel/debug/fail_page_alloc/ignore-gfp-wait", "N", false},
      {"/sys/kernel/debug/fail_page_alloc/min-order", "0", false},
  };
  unsigned i;
  for (i = 0; i < sizeof(files) / sizeof(files[0]); i++) {
    if (!write_file(files[i].file, files[i].val)) {
      if (files[i].fatal)
        exit(1);
    }
  }
}

#ifndef __NR_bpf
#define __NR_bpf 321
#endif

uint64_t r[4] = {0xffffffffffffffff, 0xffffffffffffffff, 0xffffffffffffffff,
                 0xffffffffffffffff};

int main(void)
{
  syscall(__NR_mmap, 0x1ffff000ul, 0x1000ul, 0ul, 0x32ul, -1, 0ul);
  syscall(__NR_mmap, 0x20000000ul, 0x1000000ul, 7ul, 0x32ul, -1, 0ul);
  syscall(__NR_mmap, 0x21000000ul, 0x1000ul, 0ul, 0x32ul, -1, 0ul);
  setup_fault();
  intptr_t res = 0;
  *(uint32_t*)0x2000e000 = 0xe;
  *(uint32_t*)0x2000e004 = 4;
  *(uint64_t*)0x2000e008 = 0x20000040;
  memcpy((void*)0x20000040,
         "\xb4\x05\x00\x00\x00\x00\x00\x00\x61\x10\x58\x00\x00\x00\x00\x00\xc6"
         "\x00\x00\x00\x00\x00\x00\x00\x95\x00\x00\x02\x00\x00\x00\x00",
         32);
  *(uint64_t*)0x2000e010 = 0x20003ff6;
  memcpy((void*)0x20003ff6, "GPL\000", 4);
  *(uint32_t*)0x2000e018 = 4;
  *(uint32_t*)0x2000e01c = 0xfd90;
  *(uint64_t*)0x2000e020 = 0x2000cf3d;
  *(uint32_t*)0x2000e028 = 0;
  *(uint32_t*)0x2000e02c = 0;
  memset((void*)0x2000e030, 0, 16);
  *(uint32_t*)0x2000e040 = 0;
  *(uint32_t*)0x2000e044 = 0;
  *(uint32_t*)0x2000e048 = -1;
  *(uint32_t*)0x2000e04c = 8;
  *(uint64_t*)0x2000e050 = 0x20000000;
  *(uint32_t*)0x20000000 = 0;
  *(uint32_t*)0x20000004 = 0;
  *(uint32_t*)0x2000e058 = 0x366;
  *(uint32_t*)0x2000e05c = 0x10;
  *(uint64_t*)0x2000e060 = 0x20000000;
  *(uint32_t*)0x20000000 = 0;
  *(uint32_t*)0x20000004 = 0;
  *(uint32_t*)0x20000008 = 0;
  *(uint32_t*)0x2000000c = 0;
  *(uint32_t*)0x2000e068 = 0x187;
  *(uint32_t*)0x2000e06c = 0;
  *(uint32_t*)0x2000e070 = -1;
  res = syscall(__NR_bpf, 5ul, 0x2000e000ul, 0x48ul);
  if (res != -1)
    r[0] = res;
  *(uint32_t*)0x20000100 = 0x12;
  *(uint32_t*)0x20000104 = 4;
  *(uint32_t*)0x20000108 = 8;
  *(uint32_t*)0x2000010c = 4;
  *(uint32_t*)0x20000110 = 0;
  *(uint32_t*)0x20000114 = -1;
  *(uint32_t*)0x20000118 = 0;
  memset((void*)0x2000011c, 0, 16);
  *(uint32_t*)0x2000012c = 0;
  *(uint32_t*)0x20000130 = -1;
  *(uint32_t*)0x20000134 = 0;
  *(uint32_t*)0x20000138 = 0;
  *(uint32_t*)0x2000013c = 0;
  res = syscall(__NR_bpf, 0x100000000000000ul, 0x20000100ul, 0x40ul);
  if (res != -1)
    r[1] = res;
  *(uint32_t*)0x20000080 = r[1];
  *(uint32_t*)0x20000084 = r[0];
  *(uint32_t*)0x20000088 = 5;
  *(uint32_t*)0x2000008c = 0;
  syscall(__NR_bpf, 8ul, 0x20000080ul, 0x10ul);
  res = syscall(__NR_socket, 1ul, 0x803ul, 0);
  if (res != -1)
    r[2] = res;
  *(uint32_t*)0x20000200 = r[1];
  *(uint64_t*)0x20000208 = 0x20000140;
  *(uint64_t*)0x20000210 = 0x200000c0;
  *(uint32_t*)0x200000c0 = r[2];
  *(uint64_t*)0x20000218 = 0;
  syscall(__NR_bpf, 2ul, 0x20000200ul, 0x20ul);
  *(uint16_t*)0x20000100 = 1;
  memcpy(
      (void*)0x20000102,
      "\351\037q\211Y\036\2223aK\000\000\000\000\000\000\000\000\000\000\000"
      "\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000"
      "\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000"
      "\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000"
      "\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000"
      "\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000",
      108);
  syscall(__NR_bind, r[2], 0x20000100ul, 0x6eul);
  res = syscall(__NR_socket, 1ul, 2ul, 0);
  if (res != -1)
    r[3] = res;
  *(uint16_t*)0x20000080 = 1;
  memcpy(
      (void*)0x20000082,
      "\351\037q\211Y\036\2223aK\000\000\000\000\000\000\000\000\000\000\000"
      "\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000"
      "\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000"
      "\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000"
      "\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000"
      "\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000",
      108);
  syscall(__NR_connect, r[3], 0x20000080ul, 0x6eul);
  inject_fault(5);
  syscall(__NR_sendmmsg, r[3], 0x20002dc0ul, 0x307017fdb7a66cbul, 0ul);
  return 0;
}