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

#define _GNU_SOURCE
#include <endian.h>
#include <errno.h>
#include <sched.h>
#include <signal.h>
#include <stdarg.h>
#include <stdbool.h>
#include <stdio.h>
#include <sys/prctl.h>
#include <sys/resource.h>
#include <sys/syscall.h>
#include <sys/time.h>
#include <sys/wait.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 void loop();

static void sandbox_common()
{
  prctl(PR_SET_PDEATHSIG, SIGKILL, 0, 0, 0);
  setpgrp();
  setsid();

  struct rlimit rlim;
  rlim.rlim_cur = rlim.rlim_max = 128 << 20;
  setrlimit(RLIMIT_AS, &rlim);
  rlim.rlim_cur = rlim.rlim_max = 8 << 20;
  setrlimit(RLIMIT_MEMLOCK, &rlim);
  rlim.rlim_cur = rlim.rlim_max = 1 << 20;
  setrlimit(RLIMIT_FSIZE, &rlim);
  rlim.rlim_cur = rlim.rlim_max = 1 << 20;
  setrlimit(RLIMIT_STACK, &rlim);
  rlim.rlim_cur = rlim.rlim_max = 0;
  setrlimit(RLIMIT_CORE, &rlim);

#define CLONE_NEWCGROUP 0x02000000

  if (unshare(CLONE_NEWNS)) {
  }
  if (unshare(CLONE_NEWIPC)) {
  }
  if (unshare(CLONE_NEWCGROUP)) {
  }
  if (unshare(CLONE_NEWUTS)) {
  }
  if (unshare(CLONE_SYSVSEM)) {
  }
}

static int do_sandbox_none(void)
{
  if (unshare(CLONE_NEWPID)) {
  }
  int pid = fork();
  if (pid < 0)
    fail("sandbox fork failed");
  if (pid)
    return pid;

  sandbox_common();
  if (unshare(CLONE_NEWNET)) {
  }

  loop();
  doexit(1);
}

static void test();

void loop()
{
  while (1) {
    test();
  }
}

uint64_t r[1] = {0xffffffffffffffff};
uint64_t procid;
void test()
{
  long res;
  res = syscall(__NR_socket, 0xf, 3, 2);
  if (res != -1)
    r[0] = res;
  *(uint64_t*)0x20f56000 = 0;
  *(uint32_t*)0x20f56008 = 0;
  *(uint64_t*)0x20f56010 = 0x208feff0;
  *(uint64_t*)0x208feff0 = 0x20333f88;
  *(uint8_t*)0x20333f88 = 2;
  *(uint8_t*)0x20333f89 = 2;
  *(uint8_t*)0x20333f8a = 0;
  *(uint8_t*)0x20333f8b = 9;
  *(uint16_t*)0x20333f8c = 0xa;
  *(uint16_t*)0x20333f8e = 0;
  *(uint32_t*)0x20333f90 = -1;
  *(uint32_t*)0x20333f94 = -1;
  *(uint16_t*)0x20333f98 = 3;
  *(uint16_t*)0x20333f9a = 6;
  *(uint8_t*)0x20333f9c = 0;
  *(uint8_t*)0x20333f9d = 0;
  *(uint16_t*)0x20333f9e = 0;
  *(uint16_t*)0x20333fa0 = 2;
  *(uint16_t*)0x20333fa2 = htobe16(0);
  *(uint32_t*)0x20333fa4 = htobe32(0xe0000001);
  *(uint8_t*)0x20333fa8 = 0;
  *(uint8_t*)0x20333fa9 = 0;
  *(uint8_t*)0x20333faa = 0;
  *(uint8_t*)0x20333fab = 0;
  *(uint8_t*)0x20333fac = 0;
  *(uint8_t*)0x20333fad = 0;
  *(uint8_t*)0x20333fae = 0;
  *(uint8_t*)0x20333faf = 0;
  *(uint16_t*)0x20333fb0 = 2;
  *(uint16_t*)0x20333fb2 = 1;
  *(uint32_t*)0x20333fb4 = htobe32(-1);
  *(uint8_t*)0x20333fb8 = 0;
  *(uint8_t*)0x20333fb9 = 0;
  *(uint8_t*)0x20333fba = 0;
  *(uint8_t*)0x20333fbb = 2;
  *(uint32_t*)0x20333fbc = 0;
  *(uint16_t*)0x20333fc0 = 3;
  *(uint16_t*)0x20333fc2 = 5;
  *(uint8_t*)0x20333fc4 = 0;
  *(uint8_t*)0x20333fc5 = 0;
  *(uint16_t*)0x20333fc6 = 0;
  *(uint16_t*)0x20333fc8 = 2;
  *(uint16_t*)0x20333fca = htobe16(0);
  *(uint32_t*)0x20333fcc = htobe32(0xe0000001);
  *(uint8_t*)0x20333fd0 = 0;
  *(uint8_t*)0x20333fd1 = 0;
  *(uint8_t*)0x20333fd2 = 0;
  *(uint8_t*)0x20333fd3 = 0;
  *(uint8_t*)0x20333fd4 = 0;
  *(uint8_t*)0x20333fd5 = 0;
  *(uint8_t*)0x20333fd6 = 0;
  *(uint8_t*)0x20333fd7 = 0;
  *(uint64_t*)0x208feff8 = 0x50;
  *(uint64_t*)0x20f56018 = 1;
  *(uint64_t*)0x20f56020 = 0;
  *(uint64_t*)0x20f56028 = 0;
  *(uint32_t*)0x20f56030 = 0;
  syscall(__NR_sendmsg, r[0], 0x20f56000, 0);
}

int main()
{
  syscall(__NR_mmap, 0x20000000, 0x1000000, 3, 0x32, -1, 0);
  for (procid = 0; procid < 8; procid++) {
    if (fork() == 0) {
      for (;;) {
        int pid = do_sandbox_none();
        int status = 0;
        while (waitpid(pid, &status, __WALL) != pid) {
        }
      }
    }
  }
  sleep(1000000);
  return 0;
}