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

#define _GNU_SOURCE

#include <fcntl.h>
#include <stdio.h>
#include <string.h>
#include <sys/stat.h>
#include <sys/syscall.h>
#include <unistd.h>

#include <stdint.h>
#include <string.h>

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

static void test();

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

#ifndef __NR_memfd_create
#define __NR_memfd_create 356
#endif
#ifndef __NR_mmap
#define __NR_mmap 192
#endif
#ifndef __NR_ioctl
#define __NR_ioctl 54
#endif
#undef __NR_mmap
#define __NR_mmap __NR_mmap2

long r[3];
void test()
{
  memset(r, -1, sizeof(r));
  syscall(__NR_mmap, 0x20000000ul, 0xfff000ul, 0x3ul, 0x32ul,
          0xfffffffffffffffful, 0x0ul);
  memcpy((void*)0x202a0000, "/dev/loop#", 11);
  r[0] = syz_open_dev(0x202a0000ul, 0x0ul, 0x0ul);
  *(uint32_t*)0x20063f68 = (uint32_t)0x0;
  *(uint32_t*)0x20063f6c = (uint32_t)0x0;
  *(uint32_t*)0x20063f70 = (uint32_t)0x0;
  *(uint32_t*)0x20063f74 = (uint32_t)0x0;
  *(uint32_t*)0x20063f78 = (uint32_t)0x0;
  *(uint32_t*)0x20063f7c = (uint32_t)0x0;
  *(uint32_t*)0x20063f80 = (uint32_t)0xc;
  *(uint32_t*)0x20063f84 = (uint32_t)0x8;
  memcpy((void*)0x20063f88,
         "\xbd\x5d\x9b\x38\x0e\x76\xac\x0a\x0d\x19\xf8\x2a\x79\xf8\xbb"
         "\xff\xd4\xae\x6f\x02\xc3\xea\xaa\xa8\x9a\x9b\x7b\xee\x02\x29"
         "\xa4\xe5\xe4\x6b\x5e\x02\x9c\x11\x3a\x0d\x81\xc3\xc6\xfb\xb6"
         "\xb1\x68\x89\xc2\x84\xdf\x72\x8c\x51\x4a\x9b\xcc\x8a\x5b\x3a"
         "\x2d\xe5\xfc\x14",
         64);
  memcpy((void*)0x20063fc8, "\x7b\x0a\xca\x45\x20\x53\x3b\x44\x37\xbc"
                            "\x26\x18\x44\xd4\x38\x86\x0e\xa9\xb5\xbb"
                            "\xe8\x70\xb2\xf7\xfc\xb9\xf5\x7b\x72\x9d"
                            "\xfe\x0f",
         32);
  *(uint32_t*)0x20063fe8 = (uint32_t)0x6;
  *(uint32_t*)0x20063fec = (uint32_t)0x100000001;
  *(uint32_t*)0x20063ff0 = (uint32_t)0x0;
  syscall(__NR_ioctl, r[0], 0x4c02ul, 0x20063f68ul);
  memcpy((void*)0x20fb6ff5, "/dev/loop#", 11);
  r[1] = syz_open_dev(0x20fb6ff5ul, 0x0ul, 0x0ul);
  memcpy((void*)0x20000000, "\xff\xf8", 2);
  r[2] = syscall(__NR_memfd_create, 0x20000000ul, 0x0ul);
  syscall(__NR_ioctl, r[1], 0x4c00ul, r[2]);
}

int main()
{
  loop();
  return 0;
}