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

#define _GNU_SOURCE

#include <endian.h>
#include <fcntl.h>
#include <signal.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/prctl.h>
#include <sys/stat.h>
#include <sys/syscall.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <time.h>
#include <unistd.h>

static void sleep_ms(uint64_t ms)
{
  usleep(ms * 1000);
}

static uint64_t current_time_ms()
{
  struct timespec ts;
  if (clock_gettime(CLOCK_MONOTONIC, &ts))
    exit(1);
  return (uint64_t)ts.tv_sec * 1000 + (uint64_t)ts.tv_nsec / 1000000;
}

static long syz_open_dev(long a0, long a1, long 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) - 1);
    buf[sizeof(buf) - 1] = 0;
    while ((hash = strchr(buf, '#'))) {
      *hash = '0' + (char)(a1 % 10);
      a1 /= 10;
    }
    return open(buf, a2, 0);
  }
}

static long syz_open_procfs(long a0, long a1)
{
  char buf[128];
  memset(buf, 0, sizeof(buf));
  if (a0 == 0) {
    snprintf(buf, sizeof(buf), "/proc/self/%s", (char*)a1);
  } else if (a0 == -1) {
    snprintf(buf, sizeof(buf), "/proc/thread-self/%s", (char*)a1);
  } else {
    snprintf(buf, sizeof(buf), "/proc/self/task/%d/%s", (int)a0, (char*)a1);
  }
  int fd = open(buf, O_RDWR);
  if (fd == -1)
    fd = open(buf, O_RDONLY);
  return fd;
}

#define SYZ_HAVE_SETUP_TEST 1
static void setup_test()
{
  prctl(PR_SET_PDEATHSIG, SIGKILL, 0, 0, 0);
  setpgrp();
}

#define SYZ_HAVE_RESET_TEST 1
static void reset_test()
{
  int fd;
  for (fd = 3; fd < 30; fd++)
    close(fd);
}

static void execute_one();

#define WAIT_FLAGS __WALL

static void loop()
{
  int iter;
  for (iter = 0;; iter++) {
    int pid = fork();
    if (pid < 0)
      exit(1);
    if (pid == 0) {
      setup_test();
      execute_one();
      reset_test();
      exit(0);
    }
    int status = 0;
    uint64_t start = current_time_ms();
    for (;;) {
      if (waitpid(-1, &status, WNOHANG | WAIT_FLAGS) == pid)
        break;
      sleep_ms(1);
      if (current_time_ms() - start < 5 * 1000)
        continue;
      kill(-pid, SIGKILL);
      kill(pid, SIGKILL);
      while (waitpid(-1, &status, WAIT_FLAGS) != pid) {
      }
      break;
    }
  }
}

uint64_t r[8] = {0xffffffffffffffff, 0x0,
                 0xffffffffffffffff, 0xffffffffffffffff,
                 0xffffffffffffffff, 0xffffffffffffffff,
                 0xffffffffffffffff, 0xffffffffffffffff};

void execute_one()
{
  long res = 0;
  memcpy((void*)0x200000c0, "/dev/loop#", 11);
  res = syz_open_dev(0x200000c0, 0, 0);
  if (res != -1)
    r[0] = res;
  syscall(__NR_fsync, r[0]);
  *(uint32_t*)0x200003c0 = 0xe8;
  res = syscall(__NR_getsockopt, -1, 0x29, 0x22, 0x200002c0, 0x200003c0);
  if (res != -1)
    r[1] = *(uint32_t*)0x200002f4;
  *(uint64_t*)0x20000580 = 0x20000240;
  *(uint16_t*)0x20000240 = 0x10;
  *(uint16_t*)0x20000242 = 0;
  *(uint32_t*)0x20000244 = 0;
  *(uint32_t*)0x20000248 = 0x800010;
  *(uint32_t*)0x20000588 = 0xc;
  *(uint64_t*)0x20000590 = 0x20000540;
  *(uint64_t*)0x20000540 = 0x20000400;
  *(uint32_t*)0x20000400 = 0x1c;
  *(uint16_t*)0x20000404 = 0x32;
  *(uint16_t*)0x20000406 = 0x710;
  *(uint32_t*)0x20000408 = 0x70bd2c;
  *(uint32_t*)0x2000040c = 0x25dfdbfc;
  *(uint8_t*)0x20000410 = 0x15;
  *(uint8_t*)0x20000411 = 0;
  *(uint16_t*)0x20000412 = 0;
  *(uint16_t*)0x20000414 = 8;
  *(uint16_t*)0x20000416 = 0x38;
  *(uint32_t*)0x20000418 = r[1];
  *(uint64_t*)0x20000548 = 0x1c;
  *(uint64_t*)0x20000598 = 1;
  *(uint64_t*)0x200005a0 = 0;
  *(uint64_t*)0x200005a8 = 0;
  *(uint32_t*)0x200005b0 = 0x20000000;
  syscall(__NR_sendmsg, -1, 0x20000580, 0x8040);
  memcpy((void*)0x200000c0, "/dev/loop#", 11);
  res = syz_open_dev(0x200000c0, 0x4003, 0);
  if (res != -1)
    r[2] = res;
  memcpy((void*)0x20000080, "net/snmp6", 10);
  res = syz_open_procfs(0, 0x20000080);
  if (res != -1)
    r[3] = res;
  syscall(__NR_ioctl, r[2], 0x4c06, r[3]);
  res = syscall(__NR_socket, 0xa, 0x1000000000002, 0);
  if (res != -1)
    r[4] = res;
  syscall(__NR_ioctl, r[4], 0x8912, 0x20000280);
  *(uint32_t*)0x20000000 = 0x78;
  syscall(__NR_setsockopt, -1, 6, 0x80000000000002, 0x20000000, 4);
  *(uint16_t*)0x20deb000 = 2;
  *(uint16_t*)0x20deb002 = htobe16(0x4e23);
  *(uint32_t*)0x20deb004 = htobe32(0xe0000001);
  *(uint8_t*)0x20deb008 = 0;
  *(uint8_t*)0x20deb009 = 0;
  *(uint8_t*)0x20deb00a = 0;
  *(uint8_t*)0x20deb00b = 0;
  *(uint8_t*)0x20deb00c = 0;
  *(uint8_t*)0x20deb00d = 0;
  *(uint8_t*)0x20deb00e = 0;
  *(uint8_t*)0x20deb00f = 0;
  syscall(__NR_bind, -1, 0x20deb000, 0x10);
  memcpy((void*)0x20000100, "dctcp", 6);
  syscall(__NR_setsockopt, -1, 6, 0xd, 0x20000100, 0xff9f);
  *(uint16_t*)0x20e68000 = 2;
  *(uint16_t*)0x20e68002 = htobe16(0x4e23);
  *(uint8_t*)0x20e68004 = 0xac;
  *(uint8_t*)0x20e68005 = 0x14;
  *(uint8_t*)0x20e68006 = 0x14;
  *(uint8_t*)0x20e68007 = 0xaa;
  *(uint8_t*)0x20e68008 = 0;
  *(uint8_t*)0x20e68009 = 0;
  *(uint8_t*)0x20e6800a = 0;
  *(uint8_t*)0x20e6800b = 0;
  *(uint8_t*)0x20e6800c = 0;
  *(uint8_t*)0x20e6800d = 0;
  *(uint8_t*)0x20e6800e = 0;
  *(uint8_t*)0x20e6800f = 0;
  syscall(__NR_sendto, -1, 0x20a88f88, 0xfffffffffffffe6e, 0x20000000,
          0x20e68000, 0x10);
  syscall(__NR_shutdown, -1, 1);
  *(uint32_t*)0x20000140 = 0x100;
  syscall(__NR_setsockopt, -1, 6, 2, 0x20000140, 0xfd50);
  syscall(__NR_socket, 0xa, 3, 0x84);
  memcpy((void*)0x20000140, "/dev/md0", 9);
  res = syscall(__NR_openat, 0xffffffffffffff9c, 0x20000140, 0x24080, 0);
  if (res != -1)
    r[5] = res;
  memcpy((void*)0x200000c0, "/dev/loop#", 11);
  res = syz_open_dev(0x200000c0, 0, 0);
  if (res != -1)
    r[6] = res;
  memcpy((void*)0x20000080, "net/snmp6", 10);
  res = syz_open_procfs(0, 0x20000080);
  if (res != -1)
    r[7] = res;
  *(uint32_t*)0x20000100 = r[6];
  *(uint32_t*)0x20000104 = 0;
  *(uint64_t*)0x20000108 = 0x100000001;
  *(uint64_t*)0x20000110 = 0x66;
  *(uint64_t*)0x20000118 = 0xb4b0;
  syscall(__NR_ioctl, r[7], 0x4020940d, 0x20000100);
  syscall(__NR_read, r[7], 0x20000040, 8);
  syscall(__NR_ioctl, r[6], 0x4c00, r[5]);
}
int main()
{
  syscall(__NR_mmap, 0x20000000, 0x1000000, 3, 0x32, -1, 0);
  loop();
  return 0;
}