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

#define _GNU_SOURCE
#include <endian.h>
#include <stdio.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 <setjmp.h>
#include <signal.h>
#include <stdint.h>
#include <string.h>
#include <string.h>

static __thread int skip_segv;
static __thread jmp_buf segv_env;

static void segv_handler(int sig, siginfo_t* info, void* uctx)
{
  uintptr_t addr = (uintptr_t)info->si_addr;
  const uintptr_t prog_start = 1 << 20;
  const uintptr_t prog_end = 100 << 20;
  if (__atomic_load_n(&skip_segv, __ATOMIC_RELAXED) &&
      (addr < prog_start || addr > prog_end)) {
    _longjmp(segv_env, 1);
  }
  doexit(sig);
}

static void install_segv_handler()
{
  struct sigaction sa;

  memset(&sa, 0, sizeof(sa));
  sa.sa_handler = SIG_IGN;
  syscall(SYS_rt_sigaction, 0x20, &sa, NULL, 8);
  syscall(SYS_rt_sigaction, 0x21, &sa, NULL, 8);

  memset(&sa, 0, sizeof(sa));
  sa.sa_sigaction = segv_handler;
  sa.sa_flags = SA_NODEFER | SA_SIGINFO;
  sigaction(SIGSEGV, &sa, NULL);
  sigaction(SIGBUS, &sa, NULL);
}

#define NONFAILING(...)                                                        \
  {                                                                            \
    __atomic_fetch_add(&skip_segv, 1, __ATOMIC_SEQ_CST);                       \
    if (_setjmp(segv_env) == 0) {                                              \
      __VA_ARGS__;                                                             \
    }                                                                          \
    __atomic_fetch_sub(&skip_segv, 1, __ATOMIC_SEQ_CST);                       \
  }

static void execute_one();
extern unsigned long long procid;

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

uint64_t r[2] = {0xffffffffffffffff, 0xffffffffffffffff};
void execute_one()
{
  long res = 0;
  res = syscall(__NR_socket, 2, 1, 0);
  if (res != -1)
    r[0] = res;
  NONFAILING(*(uint16_t*)0x20e5b000 = 2);
  NONFAILING(*(uint16_t*)0x20e5b002 = htobe16(0x4e20));
  NONFAILING(*(uint32_t*)0x20e5b004 = htobe32(0xe0000001));
  NONFAILING(*(uint8_t*)0x20e5b008 = 0);
  NONFAILING(*(uint8_t*)0x20e5b009 = 0);
  NONFAILING(*(uint8_t*)0x20e5b00a = 0);
  NONFAILING(*(uint8_t*)0x20e5b00b = 0);
  NONFAILING(*(uint8_t*)0x20e5b00c = 0);
  NONFAILING(*(uint8_t*)0x20e5b00d = 0);
  NONFAILING(*(uint8_t*)0x20e5b00e = 0);
  NONFAILING(*(uint8_t*)0x20e5b00f = 0);
  syscall(__NR_bind, r[0], 0x20e5b000, 0x10);
  NONFAILING(*(uint16_t*)0x20000000 = 2);
  NONFAILING(*(uint16_t*)0x20000002 = htobe16(0x4e20));
  NONFAILING(*(uint32_t*)0x20000004 = htobe32(0));
  NONFAILING(*(uint8_t*)0x20000008 = 0);
  NONFAILING(*(uint8_t*)0x20000009 = 0);
  NONFAILING(*(uint8_t*)0x2000000a = 0);
  NONFAILING(*(uint8_t*)0x2000000b = 0);
  NONFAILING(*(uint8_t*)0x2000000c = 0);
  NONFAILING(*(uint8_t*)0x2000000d = 0);
  NONFAILING(*(uint8_t*)0x2000000e = 0);
  NONFAILING(*(uint8_t*)0x2000000f = 0);
  syscall(__NR_connect, r[0], 0x20000000, 0x10);
  NONFAILING(memcpy((void*)0x20000140, "./file0", 8));
  res = syscall(__NR_open, 0x20000140, 0x2040, 0);
  if (res != -1)
    r[1] = res;
  NONFAILING(memcpy((void*)0x200000c0, "./file0", 8));
  NONFAILING(memcpy((void*)0x20000100, "9p", 3));
  NONFAILING(memcpy((void*)0x20000180, "trans=fd,", 9));
  NONFAILING(memcpy((void*)0x20000189, "rfdno", 5));
  NONFAILING(*(uint8_t*)0x2000018e = 0x3d);
  NONFAILING(sprintf((char*)0x2000018f, "0x%016llx", (long long)r[1]));
  NONFAILING(*(uint8_t*)0x200001a1 = 0x2c);
  NONFAILING(memcpy((void*)0x200001a2, "wfdno", 5));
  NONFAILING(*(uint8_t*)0x200001a7 = 0x3d);
  NONFAILING(sprintf((char*)0x200001a8, "0x%016llx", (long long)r[0]));
  NONFAILING(*(uint8_t*)0x200001ba = 0x2c);
  NONFAILING(memcpy((void*)0x200001bb, "noextend", 8));
  NONFAILING(*(uint8_t*)0x200001c3 = 0x2c);
  NONFAILING(*(uint8_t*)0x200001c4 = 0);
  syscall(__NR_mount, 0, 0x200000c0, 0x20000100, 0, 0x20000180);
}

int main()
{
  syscall(__NR_mmap, 0x20000000, 0x1000000, 3, 0x32, -1, 0);
  install_segv_handler();
  for (;;) {
    loop();
  }
}