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

#define _GNU_SOURCE

#include <endian.h>
#include <errno.h>
#include <fcntl.h>
#include <setjmp.h>
#include <signal.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/ioctl.h>
#include <sys/mount.h>
#include <sys/stat.h>
#include <sys/syscall.h>
#include <sys/types.h>
#include <unistd.h>

#include <linux/loop.h>

unsigned long long procid;

static __thread int skip_segv;
static __thread jmp_buf segv_env;

static void segv_handler(int sig, siginfo_t* info, void* ctx)
{
  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);
  }
  exit(sig);
}

static void install_segv_handler(void)
{
  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);                       \
  }

struct fs_image_segment {
  void* data;
  uintptr_t size;
  uintptr_t offset;
};

#define IMAGE_MAX_SEGMENTS 4096
#define IMAGE_MAX_SIZE (129 << 20)

#define sys_memfd_create 319

static unsigned long fs_image_segment_check(unsigned long size,
                                            unsigned long nsegs, long segments)
{
  unsigned long i;
  struct fs_image_segment* segs = (struct fs_image_segment*)segments;
  if (nsegs > IMAGE_MAX_SEGMENTS)
    nsegs = IMAGE_MAX_SEGMENTS;
  for (i = 0; i < nsegs; i++) {
    if (segs[i].size > IMAGE_MAX_SIZE)
      segs[i].size = IMAGE_MAX_SIZE;
    segs[i].offset %= IMAGE_MAX_SIZE;
    if (segs[i].offset > IMAGE_MAX_SIZE - segs[i].size)
      segs[i].offset = IMAGE_MAX_SIZE - segs[i].size;
    if (size < segs[i].offset + segs[i].offset)
      size = segs[i].offset + segs[i].offset;
  }
  if (size > IMAGE_MAX_SIZE)
    size = IMAGE_MAX_SIZE;
  return size;
}

static long syz_mount_image(volatile long fsarg, volatile long dir,
                            volatile unsigned long size,
                            volatile unsigned long nsegs,
                            volatile long segments, volatile long flags,
                            volatile long optsarg)
{
  char loopname[64], fs[32], opts[256];
  int loopfd, err = 0, res = -1;
  unsigned long i;
  NONFAILING(size = fs_image_segment_check(size, nsegs, segments));
  int memfd = syscall(sys_memfd_create, "syz_mount_image", 0);
  if (memfd == -1) {
    err = errno;
    goto error;
  }
  if (ftruncate(memfd, size)) {
    err = errno;
    goto error_close_memfd;
  }
  for (i = 0; i < nsegs; i++) {
    struct fs_image_segment* segs = (struct fs_image_segment*)segments;
    int res1 = 0;
    NONFAILING(res1 =
                   pwrite(memfd, segs[i].data, segs[i].size, segs[i].offset));
    if (res1 < 0) {
    }
  }
  snprintf(loopname, sizeof(loopname), "/dev/loop%llu", procid);
  loopfd = open(loopname, O_RDWR);
  if (loopfd == -1) {
    err = errno;
    goto error_close_memfd;
  }
  if (ioctl(loopfd, LOOP_SET_FD, memfd)) {
    if (errno != EBUSY) {
      err = errno;
      goto error_close_loop;
    }
    ioctl(loopfd, LOOP_CLR_FD, 0);
    usleep(1000);
    if (ioctl(loopfd, LOOP_SET_FD, memfd)) {
      err = errno;
      goto error_close_loop;
    }
  }
  mkdir((char*)dir, 0777);
  memset(fs, 0, sizeof(fs));
  NONFAILING(strncpy(fs, (char*)fsarg, sizeof(fs) - 1));
  memset(opts, 0, sizeof(opts));
  NONFAILING(strncpy(opts, (char*)optsarg, sizeof(opts) - 32));
  if (strcmp(fs, "iso9660") == 0) {
    flags |= MS_RDONLY;
  } else if (strncmp(fs, "ext", 3) == 0) {
    if (strstr(opts, "errors=panic") || strstr(opts, "errors=remount-ro") == 0)
      strcat(opts, ",errors=continue");
  } else if (strcmp(fs, "xfs") == 0) {
    strcat(opts, ",nouuid");
  }
  if (mount(loopname, (char*)dir, fs, flags, opts)) {
    err = errno;
    goto error_clear_loop;
  }
  res = 0;
error_clear_loop:
  ioctl(loopfd, LOOP_CLR_FD, 0);
error_close_loop:
  close(loopfd);
error_close_memfd:
  close(memfd);
error:
  errno = err;
  return res;
}

int main(void)
{
  syscall(__NR_mmap, 0x20000000ul, 0x1000000ul, 3ul, 0x32ul, -1, 0);
  install_segv_handler();
  NONFAILING(*(uint8_t*)0x200002c0 = 0);
  NONFAILING(*(uint8_t*)0x200002c1 = 0);
  NONFAILING(*(uint8_t*)0x200002c2 = 0);
  NONFAILING(*(uint8_t*)0x200002c3 = 0);
  NONFAILING(*(uint32_t*)0x200002c4 = 0x77359400);
  NONFAILING(*(uint32_t*)0x200002c8 = 0);
  NONFAILING(*(uint8_t*)0x200002cc = 0);
  NONFAILING(*(uint8_t*)0x200002cd = 0);
  NONFAILING(*(uint8_t*)0x200002ce = 0);
  NONFAILING(*(uint8_t*)0x200002cf = 0);
  NONFAILING(*(uint8_t*)0x200002d0 = 0);
  NONFAILING(*(uint8_t*)0x200002d1 = 0);
  NONFAILING(*(uint8_t*)0x200002d2 = 0);
  NONFAILING(*(uint8_t*)0x200002d3 = 0);
  syscall(__NR_write, -1, 0x200002c0ul, 0x30ul);
  NONFAILING(memcpy((void*)0x20000080, "xfs\000", 4));
  NONFAILING(memcpy((void*)0x20000000, "./file0\000", 8));
  NONFAILING(*(uint64_t*)0x20000040 = 0x20010000);
  NONFAILING(memcpy(
      (void*)0x20010000,
      "\x58\x46\x53\x42\x00\x00\x10\x00\x00\x00\x00\x00\x00\x00\x10\x00\x00\x00"
      "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x34\xfb\x8f\xb9"
      "\xe4\xbf\x48\xb6\xad\x26\xc5\x97\xeb\x4f\x5c\x19\x00\x00\x00\x00\x00\x00"
      "\x00\x04\x00\x00\x00\x00\x00\x00\x0d\x88\x00\x00\x00\x00\x40\x00\x0d\x89"
      "\x00\x00\x00\x00\x00\x00\x0d\x8a\x00\x00\x00\x01\x00\x00\x10\x00\x00\x00"
      "\x00\x01\x00\x00\x00\x00\x00\x08\x03\x5a\xb4\x24\x02\x00\x04\x00\x00\x04"
      "\x00\x00\x00\x00\x00\x00\x00\x00\x00\xec\x00\x00\x0c\x09\x0a\x02\x0c",
      125));
  NONFAILING(*(uint64_t*)0x20000048 = 0x7d);
  NONFAILING(*(uint64_t*)0x20000050 = 0);
  NONFAILING(*(uint64_t*)0x20000058 = 0x20000240);
  NONFAILING(memcpy((void*)0x20000240, "\x2e\x1d\x6e", 3));
  NONFAILING(*(uint64_t*)0x20000060 = 3);
  NONFAILING(*(uint64_t*)0x20000068 = 0xcd);
  syz_mount_image(0x20000080, 0x20000000, 0, 0x103, 0x20000040, 0, 0);
  return 0;
}