// INFO: task hung in ucma_destroy_id
// https://syzkaller.appspot.com/bug?id=c1db72f9e6c0685cbbae718b3d1aeff45ffb6c71
// status:fixed
// autogenerated by syzkaller (http://github.com/google/syzkaller)

#define _GNU_SOURCE
#include <endian.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);                       \
  }

uint64_t r[2] = {0xffffffffffffffff, 0xffffffff};
void loop()
{
  long res;
  NONFAILING(memcpy((void*)0x20000040, "/dev/infiniband/rdma_cm", 24));
  res = syscall(__NR_openat, 0xffffffffffffff9c, 0x20000040, 2, 0);
  if (res != -1)
    r[0] = res;
  NONFAILING(*(uint32_t*)0x20000880 = 0);
  NONFAILING(*(uint16_t*)0x20000884 = 0x18);
  NONFAILING(*(uint16_t*)0x20000886 = 0xfa00);
  NONFAILING(*(uint64_t*)0x20000888 = 2);
  NONFAILING(*(uint64_t*)0x20000890 = 0x20000840);
  NONFAILING(*(uint16_t*)0x20000898 = 0x106);
  NONFAILING(*(uint8_t*)0x2000089a = 0);
  NONFAILING(*(uint8_t*)0x2000089b = 0);
  NONFAILING(*(uint8_t*)0x2000089c = 0);
  NONFAILING(*(uint8_t*)0x2000089d = 0);
  NONFAILING(*(uint8_t*)0x2000089e = 0);
  NONFAILING(*(uint8_t*)0x2000089f = 0);
  res = syscall(__NR_write, r[0], 0x20000880, 0x20);
  if (res != -1)
    NONFAILING(r[1] = *(uint32_t*)0x20000840);
  NONFAILING(*(uint32_t*)0x200002c0 = 0xe);
  NONFAILING(*(uint16_t*)0x200002c4 = 0x18);
  NONFAILING(*(uint16_t*)0x200002c6 = 0xfa00);
  NONFAILING(*(uint64_t*)0x200002c8 = 0x20000280);
  NONFAILING(*(uint32_t*)0x20000280 = 1);
  NONFAILING(*(uint32_t*)0x200002d0 = r[1]);
  NONFAILING(*(uint32_t*)0x200002d4 = 0);
  NONFAILING(*(uint32_t*)0x200002d8 = 1);
  NONFAILING(*(uint32_t*)0x200002dc = 4);
  syscall(__NR_write, r[0], 0x200002c0, 0x20);
  NONFAILING(*(uint32_t*)0x20000080 = 1);
  NONFAILING(*(uint16_t*)0x20000084 = 0x10);
  NONFAILING(*(uint16_t*)0x20000086 = 0xfa00);
  NONFAILING(*(uint64_t*)0x20000088 = 0x200000c0);
  NONFAILING(*(uint32_t*)0x20000090 = r[1]);
  NONFAILING(*(uint32_t*)0x20000094 = 0);
  syscall(__NR_write, r[0], 0x20000080, 0x18);
}

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