// general protection fault in hash_ip4_uadt
// https://syzkaller.appspot.com/bug?id=79c7cd17a7d2b58c5973606a4fa7e63d56974443
// status:dup
// autogenerated by syzkaller (https://github.com/google/syzkaller)

#define _GNU_SOURCE

#include <endian.h>
#include <errno.h>
#include <pthread.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/syscall.h>
#include <sys/types.h>
#include <time.h>
#include <unistd.h>

#include <linux/futex.h>

static void sleep_ms(uint64_t ms)
{
  usleep(ms * 1000);
}

static uint64_t current_time_ms(void)
{
  struct timespec ts;
  if (clock_gettime(CLOCK_MONOTONIC, &ts))
    exit(1);
  return (uint64_t)ts.tv_sec * 1000 + (uint64_t)ts.tv_nsec / 1000000;
}

static void thread_start(void* (*fn)(void*), void* arg)
{
  pthread_t th;
  pthread_attr_t attr;
  pthread_attr_init(&attr);
  pthread_attr_setstacksize(&attr, 128 << 10);
  int i;
  for (i = 0; i < 100; i++) {
    if (pthread_create(&th, &attr, fn, arg) == 0) {
      pthread_attr_destroy(&attr);
      return;
    }
    if (errno == EAGAIN) {
      usleep(50);
      continue;
    }
    break;
  }
  exit(1);
}

#define BITMASK(bf_off, bf_len) (((1ull << (bf_len)) - 1) << (bf_off))
#define STORE_BY_BITMASK(type, htobe, addr, val, bf_off, bf_len)               \
  *(type*)(addr) =                                                             \
      htobe((htobe(*(type*)(addr)) & ~BITMASK((bf_off), (bf_len))) |           \
            (((type)(val) << (bf_off)) & BITMASK((bf_off), (bf_len))))

typedef struct {
  int state;
} event_t;

static void event_init(event_t* ev)
{
  ev->state = 0;
}

static void event_reset(event_t* ev)
{
  ev->state = 0;
}

static void event_set(event_t* ev)
{
  if (ev->state)
    exit(1);
  __atomic_store_n(&ev->state, 1, __ATOMIC_RELEASE);
  syscall(SYS_futex, &ev->state, FUTEX_WAKE | FUTEX_PRIVATE_FLAG, 1000000);
}

static void event_wait(event_t* ev)
{
  while (!__atomic_load_n(&ev->state, __ATOMIC_ACQUIRE))
    syscall(SYS_futex, &ev->state, FUTEX_WAIT | FUTEX_PRIVATE_FLAG, 0, 0);
}

static int event_isset(event_t* ev)
{
  return __atomic_load_n(&ev->state, __ATOMIC_ACQUIRE);
}

static int event_timedwait(event_t* ev, uint64_t timeout)
{
  uint64_t start = current_time_ms();
  uint64_t now = start;
  for (;;) {
    uint64_t remain = timeout - (now - start);
    struct timespec ts;
    ts.tv_sec = remain / 1000;
    ts.tv_nsec = (remain % 1000) * 1000 * 1000;
    syscall(SYS_futex, &ev->state, FUTEX_WAIT | FUTEX_PRIVATE_FLAG, 0, &ts);
    if (__atomic_load_n(&ev->state, __ATOMIC_RELAXED))
      return 1;
    now = current_time_ms();
    if (now - start > timeout)
      return 0;
  }
}

struct thread_t {
  int created, call;
  event_t ready, done;
};

static struct thread_t threads[16];
static void execute_call(int call);
static int running;

static void* thr(void* arg)
{
  struct thread_t* th = (struct thread_t*)arg;
  for (;;) {
    event_wait(&th->ready);
    event_reset(&th->ready);
    execute_call(th->call);
    __atomic_fetch_sub(&running, 1, __ATOMIC_RELAXED);
    event_set(&th->done);
  }
  return 0;
}

static void loop(void)
{
  int i, call, thread;
  int collide = 0;
again:
  for (call = 0; call < 4; call++) {
    for (thread = 0; thread < (int)(sizeof(threads) / sizeof(threads[0]));
         thread++) {
      struct thread_t* th = &threads[thread];
      if (!th->created) {
        th->created = 1;
        event_init(&th->ready);
        event_init(&th->done);
        event_set(&th->done);
        thread_start(thr, th);
      }
      if (!event_isset(&th->done))
        continue;
      event_reset(&th->done);
      th->call = call;
      __atomic_fetch_add(&running, 1, __ATOMIC_RELAXED);
      event_set(&th->ready);
      if (collide && (call % 2) == 0)
        break;
      event_timedwait(&th->done, 45);
      break;
    }
  }
  for (i = 0; i < 100 && __atomic_load_n(&running, __ATOMIC_RELAXED); i++)
    sleep_ms(1);
  if (!collide) {
    collide = 1;
    goto again;
  }
}

uint64_t r[2] = {0xffffffffffffffff, 0xffffffffffffffff};

void execute_call(int call)
{
  intptr_t res;
  switch (call) {
  case 0:
    res = syscall(__NR_socket, 0x10ul, 3ul, 0xcul);
    if (res != -1)
      r[0] = res;
    break;
  case 1:
    *(uint64_t*)0x20000300 = 0;
    *(uint32_t*)0x20000308 = 0;
    *(uint64_t*)0x20000310 = 0x200002c0;
    *(uint64_t*)0x200002c0 = 0x20000440;
    *(uint32_t*)0x20000440 = 0x50;
    *(uint8_t*)0x20000444 = 2;
    *(uint8_t*)0x20000445 = 6;
    *(uint16_t*)0x20000446 = 1;
    *(uint32_t*)0x20000448 = 0;
    *(uint32_t*)0x2000044c = 0;
    *(uint8_t*)0x20000450 = 0;
    *(uint8_t*)0x20000451 = 0;
    *(uint16_t*)0x20000452 = htobe16(0);
    *(uint16_t*)0x20000454 = 9;
    *(uint16_t*)0x20000456 = 2;
    memcpy((void*)0x20000458, "syz2\000", 5);
    *(uint16_t*)0x20000460 = 0xc;
    *(uint16_t*)0x20000462 = 3;
    memcpy((void*)0x20000464, "hash:ip\000", 8);
    *(uint16_t*)0x2000046c = 0xc;
    STORE_BY_BITMASK(uint16_t, , 0x2000046e, 7, 0, 14);
    STORE_BY_BITMASK(uint16_t, , 0x2000046f, 0, 6, 1);
    STORE_BY_BITMASK(uint16_t, , 0x2000046f, 1, 7, 1);
    *(uint16_t*)0x20000470 = 8;
    STORE_BY_BITMASK(uint16_t, , 0x20000472, 6, 0, 14);
    STORE_BY_BITMASK(uint16_t, , 0x20000473, 1, 6, 1);
    STORE_BY_BITMASK(uint16_t, , 0x20000473, 0, 7, 1);
    *(uint32_t*)0x20000474 = htobe32(3);
    *(uint16_t*)0x20000478 = 5;
    *(uint16_t*)0x2000047a = 1;
    *(uint8_t*)0x2000047c = 7;
    *(uint16_t*)0x20000480 = 5;
    *(uint16_t*)0x20000482 = 4;
    *(uint8_t*)0x20000484 = 0;
    *(uint16_t*)0x20000488 = 5;
    *(uint16_t*)0x2000048a = 5;
    *(uint8_t*)0x2000048c = 2;
    *(uint64_t*)0x200002c8 = 0x50;
    *(uint64_t*)0x20000318 = 1;
    *(uint64_t*)0x20000320 = 0x200000000000000;
    *(uint64_t*)0x20000328 = 0;
    *(uint32_t*)0x20000330 = 0;
    syscall(__NR_sendmsg, r[0], 0x20000300ul, 0ul);
    break;
  case 2:
    res = syscall(__NR_socket, 0x10ul, 3ul, 0xcul);
    if (res != -1)
      r[1] = res;
    break;
  case 3:
    *(uint64_t*)0x20000500 = 0;
    *(uint32_t*)0x20000508 = 0;
    *(uint64_t*)0x20000510 = 0x200004c0;
    *(uint64_t*)0x200004c0 = 0x200003c0;
    *(uint32_t*)0x200003c0 = 0x100;
    *(uint8_t*)0x200003c4 = 0xb;
    *(uint8_t*)0x200003c5 = 6;
    *(uint16_t*)0x200003c6 = 1;
    *(uint32_t*)0x200003c8 = 0x70bd2d;
    *(uint32_t*)0x200003cc = 0x25dfdbfb;
    *(uint8_t*)0x200003d0 = 0x9a;
    *(uint8_t*)0x200003d1 = 0;
    *(uint16_t*)0x200003d2 = htobe16(5);
    *(uint16_t*)0x200003d4 = 8;
    STORE_BY_BITMASK(uint16_t, , 0x200003d6, 9, 0, 14);
    STORE_BY_BITMASK(uint16_t, , 0x200003d7, 1, 6, 1);
    STORE_BY_BITMASK(uint16_t, , 0x200003d7, 0, 7, 1);
    *(uint32_t*)0x200003d8 = htobe32(0x40);
    *(uint16_t*)0x200003dc = 0x44;
    STORE_BY_BITMASK(uint16_t, , 0x200003de, 8, 0, 14);
    STORE_BY_BITMASK(uint16_t, , 0x200003df, 0, 6, 1);
    STORE_BY_BITMASK(uint16_t, , 0x200003df, 1, 7, 1);
    *(uint16_t*)0x200003e0 = 0x10;
    STORE_BY_BITMASK(uint16_t, , 0x200003e2, 7, 0, 14);
    STORE_BY_BITMASK(uint16_t, , 0x200003e3, 0, 6, 1);
    STORE_BY_BITMASK(uint16_t, , 0x200003e3, 1, 7, 1);
    *(uint16_t*)0x200003e4 = 0xc;
    STORE_BY_BITMASK(uint16_t, , 0x200003e6, 0x18, 0, 14);
    STORE_BY_BITMASK(uint16_t, , 0x200003e7, 1, 6, 1);
    STORE_BY_BITMASK(uint16_t, , 0x200003e7, 0, 7, 1);
    *(uint64_t*)0x200003e8 = htobe64(3);
    *(uint16_t*)0x200003f0 = 0xc;
    STORE_BY_BITMASK(uint16_t, , 0x200003f2, 7, 0, 14);
    STORE_BY_BITMASK(uint16_t, , 0x200003f3, 0, 6, 1);
    STORE_BY_BITMASK(uint16_t, , 0x200003f3, 1, 7, 1);
    *(uint16_t*)0x200003f4 = 8;
    STORE_BY_BITMASK(uint16_t, , 0x200003f6, 0xb, 0, 14);
    STORE_BY_BITMASK(uint16_t, , 0x200003f7, 1, 6, 1);
    STORE_BY_BITMASK(uint16_t, , 0x200003f7, 0, 7, 1);
    *(uint32_t*)0x200003f8 = htobe32(0x81);
    *(uint16_t*)0x200003fc = 0xc;
    STORE_BY_BITMASK(uint16_t, , 0x200003fe, 7, 0, 14);
    STORE_BY_BITMASK(uint16_t, , 0x200003ff, 0, 6, 1);
    STORE_BY_BITMASK(uint16_t, , 0x200003ff, 1, 7, 1);
    *(uint16_t*)0x20000400 = 5;
    *(uint16_t*)0x20000402 = 0x1a;
    memcpy((void*)0x20000404, "\000", 1);
    *(uint16_t*)0x20000408 = 0xc;
    STORE_BY_BITMASK(uint16_t, , 0x2000040a, 7, 0, 14);
    STORE_BY_BITMASK(uint16_t, , 0x2000040b, 0, 6, 1);
    STORE_BY_BITMASK(uint16_t, , 0x2000040b, 1, 7, 1);
    *(uint16_t*)0x2000040c = 8;
    STORE_BY_BITMASK(uint16_t, , 0x2000040e, 0xa, 0, 14);
    STORE_BY_BITMASK(uint16_t, , 0x2000040f, 1, 6, 1);
    STORE_BY_BITMASK(uint16_t, , 0x2000040f, 0, 7, 1);
    *(uint32_t*)0x20000410 = htobe32(8);
    *(uint16_t*)0x20000414 = 0xc;
    STORE_BY_BITMASK(uint16_t, , 0x20000416, 7, 0, 14);
    STORE_BY_BITMASK(uint16_t, , 0x20000417, 0, 6, 1);
    STORE_BY_BITMASK(uint16_t, , 0x20000417, 1, 7, 1);
    *(uint16_t*)0x20000418 = 6;
    STORE_BY_BITMASK(uint16_t, , 0x2000041a, 0x1d, 0, 14);
    STORE_BY_BITMASK(uint16_t, , 0x2000041b, 1, 6, 1);
    STORE_BY_BITMASK(uint16_t, , 0x2000041b, 0, 7, 1);
    *(uint16_t*)0x2000041c = htobe16(0x3f);
    *(uint16_t*)0x20000420 = 8;
    STORE_BY_BITMASK(uint16_t, , 0x20000422, 9, 0, 14);
    STORE_BY_BITMASK(uint16_t, , 0x20000423, 1, 6, 1);
    STORE_BY_BITMASK(uint16_t, , 0x20000423, 0, 7, 1);
    *(uint32_t*)0x20000424 = htobe32(9);
    *(uint16_t*)0x20000428 = 5;
    *(uint16_t*)0x2000042a = 1;
    *(uint8_t*)0x2000042c = 7;
    *(uint16_t*)0x20000430 = 5;
    *(uint16_t*)0x20000432 = 1;
    *(uint8_t*)0x20000434 = 7;
    *(uint16_t*)0x20000438 = 5;
    *(uint16_t*)0x2000043a = 1;
    *(uint8_t*)0x2000043c = 7;
    *(uint16_t*)0x20000440 = 9;
    *(uint16_t*)0x20000442 = 2;
    memcpy((void*)0x20000444, "syz2\000", 5);
    *(uint16_t*)0x2000044c = 0x30;
    STORE_BY_BITMASK(uint16_t, , 0x2000044e, 7, 0, 14);
    STORE_BY_BITMASK(uint16_t, , 0x2000044f, 0, 6, 1);
    STORE_BY_BITMASK(uint16_t, , 0x2000044f, 1, 7, 1);
    *(uint16_t*)0x20000450 = 0x14;
    *(uint16_t*)0x20000452 = 0x17;
    memcpy((void*)0x20000454, "veth0_to_bridge\000", 16);
    *(uint16_t*)0x20000464 = 8;
    STORE_BY_BITMASK(uint16_t, , 0x20000466, 0xa, 0, 14);
    STORE_BY_BITMASK(uint16_t, , 0x20000467, 1, 6, 1);
    STORE_BY_BITMASK(uint16_t, , 0x20000467, 0, 7, 1);
    *(uint32_t*)0x20000468 = htobe32(0x2b);
    *(uint16_t*)0x2000046c = 6;
    STORE_BY_BITMASK(uint16_t, , 0x2000046e, 5, 0, 14);
    STORE_BY_BITMASK(uint16_t, , 0x2000046f, 1, 6, 1);
    STORE_BY_BITMASK(uint16_t, , 0x2000046f, 0, 7, 1);
    *(uint16_t*)0x20000470 = htobe16(0x4e24);
    *(uint16_t*)0x20000474 = 8;
    STORE_BY_BITMASK(uint16_t, , 0x20000476, 0xb, 0, 14);
    STORE_BY_BITMASK(uint16_t, , 0x20000477, 1, 6, 1);
    STORE_BY_BITMASK(uint16_t, , 0x20000477, 0, 7, 1);
    *(uint32_t*)0x20000478 = htobe32(0x1f);
    *(uint16_t*)0x2000047c = 0x44;
    STORE_BY_BITMASK(uint16_t, , 0x2000047e, 7, 0, 14);
    STORE_BY_BITMASK(uint16_t, , 0x2000047f, 0, 6, 1);
    STORE_BY_BITMASK(uint16_t, , 0x2000047f, 1, 7, 1);
    *(uint16_t*)0x20000480 = 0x18;
    STORE_BY_BITMASK(uint16_t, , 0x20000482, 1, 0, 14);
    STORE_BY_BITMASK(uint16_t, , 0x20000483, 0, 6, 1);
    STORE_BY_BITMASK(uint16_t, , 0x20000483, 1, 7, 1);
    *(uint16_t*)0x20000484 = 0x14;
    STORE_BY_BITMASK(uint16_t, , 0x20000486, 2, 0, 14);
    STORE_BY_BITMASK(uint16_t, , 0x20000487, 1, 6, 1);
    STORE_BY_BITMASK(uint16_t, , 0x20000487, 0, 7, 1);
    *(uint8_t*)0x20000488 = -1;
    *(uint8_t*)0x20000489 = 2;
    *(uint8_t*)0x2000048a = 0;
    *(uint8_t*)0x2000048b = 0;
    *(uint8_t*)0x2000048c = 0;
    *(uint8_t*)0x2000048d = 0;
    *(uint8_t*)0x2000048e = 0;
    *(uint8_t*)0x2000048f = 0;
    *(uint8_t*)0x20000490 = 0;
    *(uint8_t*)0x20000491 = 0;
    *(uint8_t*)0x20000492 = 0;
    *(uint8_t*)0x20000493 = 0;
    *(uint8_t*)0x20000494 = 0;
    *(uint8_t*)0x20000495 = 0;
    *(uint8_t*)0x20000496 = 0;
    *(uint8_t*)0x20000497 = 1;
    *(uint16_t*)0x20000498 = 0x18;
    STORE_BY_BITMASK(uint16_t, , 0x2000049a, 2, 0, 14);
    STORE_BY_BITMASK(uint16_t, , 0x2000049b, 0, 6, 1);
    STORE_BY_BITMASK(uint16_t, , 0x2000049b, 1, 7, 1);
    *(uint16_t*)0x2000049c = 0x14;
    STORE_BY_BITMASK(uint16_t, , 0x2000049e, 2, 0, 14);
    STORE_BY_BITMASK(uint16_t, , 0x2000049f, 1, 6, 1);
    STORE_BY_BITMASK(uint16_t, , 0x2000049f, 0, 7, 1);
    *(uint8_t*)0x200004a0 = 0xfe;
    *(uint8_t*)0x200004a1 = 0x80;
    *(uint8_t*)0x200004a2 = 0;
    *(uint8_t*)0x200004a3 = 0;
    *(uint8_t*)0x200004a4 = 0;
    *(uint8_t*)0x200004a5 = 0;
    *(uint8_t*)0x200004a6 = 0;
    *(uint8_t*)0x200004a7 = 0;
    *(uint8_t*)0x200004a8 = 0;
    *(uint8_t*)0x200004a9 = 0;
    *(uint8_t*)0x200004aa = 0;
    *(uint8_t*)0x200004ab = 0;
    *(uint8_t*)0x200004ac = 0;
    *(uint8_t*)0x200004ad = 0;
    *(uint8_t*)0x200004ae = 0;
    *(uint8_t*)0x200004af = 0xa;
    *(uint16_t*)0x200004b0 = 8;
    STORE_BY_BITMASK(uint16_t, , 0x200004b2, 9, 0, 14);
    STORE_BY_BITMASK(uint16_t, , 0x200004b3, 1, 6, 1);
    STORE_BY_BITMASK(uint16_t, , 0x200004b3, 0, 7, 1);
    *(uint32_t*)0x200004b4 = htobe32(0x9b);
    *(uint16_t*)0x200004b8 = 8;
    STORE_BY_BITMASK(uint16_t, , 0x200004ba, 6, 0, 14);
    STORE_BY_BITMASK(uint16_t, , 0x200004bb, 1, 6, 1);
    STORE_BY_BITMASK(uint16_t, , 0x200004bb, 0, 7, 1);
    *(uint32_t*)0x200004bc = htobe32(0x80);
    *(uint64_t*)0x200004c8 = 0x100;
    *(uint64_t*)0x20000518 = 1;
    *(uint64_t*)0x20000520 = 0;
    *(uint64_t*)0x20000528 = 0;
    *(uint32_t*)0x20000530 = 0x40000;
    syscall(__NR_sendmsg, r[1], 0x20000500ul, 0ul);
    break;
  }
}
int main(void)
{
  syscall(__NR_mmap, 0x20000000ul, 0x1000000ul, 3ul, 0x32ul, -1, 0);
  loop();
  return 0;
}
