xref: /freebsd/tools/test/stress2/misc/syzkaller14.sh (revision 7f658f99f7ed5d1d0e0802073bb22eb8a0a784fb)
18a272653SPeter Holm#!/bin/sh
28a272653SPeter Holm
38a272653SPeter Holm# panic: mtx_lock() of spin mutex (null) @ ../../../kern/uipc_ktls.c:1034
48a272653SPeter Holm# cpuid = 5
58a272653SPeter Holm# time = 1591684934
68a272653SPeter Holm# KDB: stack backtrace:
78a272653SPeter Holm# db_trace_self_wrapper() at db_trace_self_wrapper+0x2b/frame 0xfffffe013a91b6c0
88a272653SPeter Holm# vpanic() at vpanic+0x182/frame 0xfffffe013a91b710
98a272653SPeter Holm# panic() at panic+0x43/frame 0xfffffe013a91b770
108a272653SPeter Holm# __mtx_lock_flags() at __mtx_lock_flags+0x13c/frame 0xfffffe013a91b7c0
118a272653SPeter Holm# ktls_get_rx_mode() at ktls_get_rx_mode+0x4e/frame 0xfffffe013a91b7f0
128a272653SPeter Holm# tcp_default_ctloutput() at tcp_default_ctloutput+0x800/frame 0xfffffe013a91b960
138a272653SPeter Holm# tcp_ctloutput() at tcp_ctloutput+0x1f5/frame 0xfffffe013a91b9d0
148a272653SPeter Holm# sogetopt() at sogetopt+0x101/frame 0xfffffe013a91ba40
158a272653SPeter Holm# kern_getsockopt() at kern_getsockopt+0xb4/frame 0xfffffe013a91baa0
168a272653SPeter Holm# sys_getsockopt() at sys_getsockopt+0x52/frame 0xfffffe013a91bad0
178a272653SPeter Holm# amd64_syscall() at amd64_syscall+0x159/frame 0xfffffe013a91bbf0
188a272653SPeter Holm# fast_syscall_common() at fast_syscall_common+0x101/frame 0xfffffe013a91bbf0
198a272653SPeter Holm# --- syscall (0, FreeBSD ELF64, nosys), rip = 0x80045446a, rsp = 0x7fffdffdcf88, rbp = 0x7fffdffdcfc0 ---
208a272653SPeter Holm# KDB: enter: panic
218a272653SPeter Holm# [ thread pid 5313 tid 101679 ]
228a272653SPeter Holm# Stopped at      kdb_enter+0x37: movq    $0,0x10c7fa6(%rip)
238a272653SPeter Holm# db> x/s version
248a272653SPeter Holm# version:        FreeBSD 13.0-CURRENT #0 r361886: Tue Jun  9 08:32:24 CEST 2020\012    pho@t2.osted.lan:/usr/src/sys/amd64/compile/KTLS\012
258a272653SPeter Holm# db>
268a272653SPeter Holm
278a272653SPeter Holm# Reproduced on r361886 with "options KERN_TLS"
288a272653SPeter Holm
298a272653SPeter Holm[ `uname -p` = "i386" ] && exit 0
308a272653SPeter Holm
318a272653SPeter Holm. ../default.cfg
328a272653SPeter Holmcat > /tmp/syzkaller14.c <<EOF
338a272653SPeter Holm// https://syzkaller.appspot.com/bug?id=d50164ce557a20a785510aac5d6f104d8a62e779
348a272653SPeter Holm// autogenerated by syzkaller (https://github.com/google/syzkaller)
358a272653SPeter Holm// Reported-by: syzbot+a8829e888a93a4a04619@syzkaller.appspotmail.com
368a272653SPeter Holm
378a272653SPeter Holm#define _GNU_SOURCE
388a272653SPeter Holm
398a272653SPeter Holm#include <sys/types.h>
408a272653SPeter Holm
418a272653SPeter Holm#include <errno.h>
428a272653SPeter Holm#include <pthread.h>
438a272653SPeter Holm#include <pwd.h>
448a272653SPeter Holm#include <signal.h>
458a272653SPeter Holm#include <stdarg.h>
468a272653SPeter Holm#include <stdbool.h>
478a272653SPeter Holm#include <stdint.h>
488a272653SPeter Holm#include <stdio.h>
498a272653SPeter Holm#include <stdlib.h>
508a272653SPeter Holm#include <string.h>
518a272653SPeter Holm#include <sys/endian.h>
528a272653SPeter Holm#include <sys/syscall.h>
538a272653SPeter Holm#include <sys/wait.h>
548a272653SPeter Holm#include <time.h>
558a272653SPeter Holm#include <unistd.h>
568a272653SPeter Holm
578a272653SPeter Holmstatic unsigned long long procid;
588a272653SPeter Holm
598a272653SPeter Holmstatic void kill_and_wait(int pid, int* status)
608a272653SPeter Holm{
618a272653SPeter Holm  kill(pid, SIGKILL);
628a272653SPeter Holm  while (waitpid(-1, status, 0) != pid) {
638a272653SPeter Holm  }
648a272653SPeter Holm}
658a272653SPeter Holm
668a272653SPeter Holmstatic void sleep_ms(uint64_t ms)
678a272653SPeter Holm{
688a272653SPeter Holm  usleep(ms * 1000);
698a272653SPeter Holm}
708a272653SPeter Holm
718a272653SPeter Holmstatic uint64_t current_time_ms(void)
728a272653SPeter Holm{
738a272653SPeter Holm  struct timespec ts;
748a272653SPeter Holm  if (clock_gettime(CLOCK_MONOTONIC, &ts))
758a272653SPeter Holm    exit(1);
768a272653SPeter Holm  return (uint64_t)ts.tv_sec * 1000 + (uint64_t)ts.tv_nsec / 1000000;
778a272653SPeter Holm}
788a272653SPeter Holm
798a272653SPeter Holmstatic void thread_start(void* (*fn)(void*), void* arg)
808a272653SPeter Holm{
818a272653SPeter Holm  pthread_t th;
828a272653SPeter Holm  pthread_attr_t attr;
838a272653SPeter Holm  pthread_attr_init(&attr);
848a272653SPeter Holm  pthread_attr_setstacksize(&attr, 128 << 10);
858a272653SPeter Holm  int i;
868a272653SPeter Holm  for (i = 0; i < 100; i++) {
878a272653SPeter Holm    if (pthread_create(&th, &attr, fn, arg) == 0) {
888a272653SPeter Holm      pthread_attr_destroy(&attr);
898a272653SPeter Holm      return;
908a272653SPeter Holm    }
918a272653SPeter Holm    if (errno == EAGAIN) {
928a272653SPeter Holm      usleep(50);
938a272653SPeter Holm      continue;
948a272653SPeter Holm    }
958a272653SPeter Holm    break;
968a272653SPeter Holm  }
978a272653SPeter Holm  exit(1);
988a272653SPeter Holm}
998a272653SPeter Holm
1008a272653SPeter Holmtypedef struct {
1018a272653SPeter Holm  pthread_mutex_t mu;
1028a272653SPeter Holm  pthread_cond_t cv;
1038a272653SPeter Holm  int state;
1048a272653SPeter Holm} event_t;
1058a272653SPeter Holm
1068a272653SPeter Holmstatic void event_init(event_t* ev)
1078a272653SPeter Holm{
1088a272653SPeter Holm  if (pthread_mutex_init(&ev->mu, 0))
1098a272653SPeter Holm    exit(1);
1108a272653SPeter Holm  if (pthread_cond_init(&ev->cv, 0))
1118a272653SPeter Holm    exit(1);
1128a272653SPeter Holm  ev->state = 0;
1138a272653SPeter Holm}
1148a272653SPeter Holm
1158a272653SPeter Holmstatic void event_reset(event_t* ev)
1168a272653SPeter Holm{
1178a272653SPeter Holm  ev->state = 0;
1188a272653SPeter Holm}
1198a272653SPeter Holm
1208a272653SPeter Holmstatic void event_set(event_t* ev)
1218a272653SPeter Holm{
1228a272653SPeter Holm  pthread_mutex_lock(&ev->mu);
1238a272653SPeter Holm  if (ev->state)
1248a272653SPeter Holm    exit(1);
1258a272653SPeter Holm  ev->state = 1;
1268a272653SPeter Holm  pthread_mutex_unlock(&ev->mu);
1278a272653SPeter Holm  pthread_cond_broadcast(&ev->cv);
1288a272653SPeter Holm}
1298a272653SPeter Holm
1308a272653SPeter Holmstatic void event_wait(event_t* ev)
1318a272653SPeter Holm{
1328a272653SPeter Holm  pthread_mutex_lock(&ev->mu);
1338a272653SPeter Holm  while (!ev->state)
1348a272653SPeter Holm    pthread_cond_wait(&ev->cv, &ev->mu);
1358a272653SPeter Holm  pthread_mutex_unlock(&ev->mu);
1368a272653SPeter Holm}
1378a272653SPeter Holm
1388a272653SPeter Holmstatic int event_isset(event_t* ev)
1398a272653SPeter Holm{
1408a272653SPeter Holm  pthread_mutex_lock(&ev->mu);
1418a272653SPeter Holm  int res = ev->state;
1428a272653SPeter Holm  pthread_mutex_unlock(&ev->mu);
1438a272653SPeter Holm  return res;
1448a272653SPeter Holm}
1458a272653SPeter Holm
1468a272653SPeter Holmstatic int event_timedwait(event_t* ev, uint64_t timeout)
1478a272653SPeter Holm{
1488a272653SPeter Holm  uint64_t start = current_time_ms();
1498a272653SPeter Holm  uint64_t now = start;
1508a272653SPeter Holm  pthread_mutex_lock(&ev->mu);
1518a272653SPeter Holm  for (;;) {
1528a272653SPeter Holm    if (ev->state)
1538a272653SPeter Holm      break;
1548a272653SPeter Holm    uint64_t remain = timeout - (now - start);
1558a272653SPeter Holm    struct timespec ts;
1568a272653SPeter Holm    ts.tv_sec = remain / 1000;
1578a272653SPeter Holm    ts.tv_nsec = (remain % 1000) * 1000 * 1000;
1588a272653SPeter Holm    pthread_cond_timedwait(&ev->cv, &ev->mu, &ts);
1598a272653SPeter Holm    now = current_time_ms();
1608a272653SPeter Holm    if (now - start > timeout)
1618a272653SPeter Holm      break;
1628a272653SPeter Holm  }
1638a272653SPeter Holm  int res = ev->state;
1648a272653SPeter Holm  pthread_mutex_unlock(&ev->mu);
1658a272653SPeter Holm  return res;
1668a272653SPeter Holm}
1678a272653SPeter Holm
1688a272653SPeter Holmstruct thread_t {
1698a272653SPeter Holm  int created, call;
1708a272653SPeter Holm  event_t ready, done;
1718a272653SPeter Holm};
1728a272653SPeter Holm
1738a272653SPeter Holmstatic struct thread_t threads[16];
1748a272653SPeter Holmstatic void execute_call(int call);
1758a272653SPeter Holmstatic int running;
1768a272653SPeter Holm
1778a272653SPeter Holmstatic void* thr(void* arg)
1788a272653SPeter Holm{
1798a272653SPeter Holm  struct thread_t* th = (struct thread_t*)arg;
1808a272653SPeter Holm  for (;;) {
1818a272653SPeter Holm    event_wait(&th->ready);
1828a272653SPeter Holm    event_reset(&th->ready);
1838a272653SPeter Holm    execute_call(th->call);
1848a272653SPeter Holm    __atomic_fetch_sub(&running, 1, __ATOMIC_RELAXED);
1858a272653SPeter Holm    event_set(&th->done);
1868a272653SPeter Holm  }
1878a272653SPeter Holm  return 0;
1888a272653SPeter Holm}
1898a272653SPeter Holm
1908a272653SPeter Holmstatic void execute_one(void)
1918a272653SPeter Holm{
1928a272653SPeter Holm  int i, call, thread;
1938a272653SPeter Holm  int collide = 0;
1948a272653SPeter Holmagain:
1958a272653SPeter Holm  for (call = 0; call < 3; call++) {
1968a272653SPeter Holm    for (thread = 0; thread < (int)(sizeof(threads) / sizeof(threads[0]));
1978a272653SPeter Holm         thread++) {
1988a272653SPeter Holm      struct thread_t* th = &threads[thread];
1998a272653SPeter Holm      if (!th->created) {
2008a272653SPeter Holm        th->created = 1;
2018a272653SPeter Holm        event_init(&th->ready);
2028a272653SPeter Holm        event_init(&th->done);
2038a272653SPeter Holm        event_set(&th->done);
2048a272653SPeter Holm        thread_start(thr, th);
2058a272653SPeter Holm      }
2068a272653SPeter Holm      if (!event_isset(&th->done))
2078a272653SPeter Holm        continue;
2088a272653SPeter Holm      event_reset(&th->done);
2098a272653SPeter Holm      th->call = call;
2108a272653SPeter Holm      __atomic_fetch_add(&running, 1, __ATOMIC_RELAXED);
2118a272653SPeter Holm      event_set(&th->ready);
2128a272653SPeter Holm      if (collide && (call % 2) == 0)
2138a272653SPeter Holm        break;
2148a272653SPeter Holm      event_timedwait(&th->done, 45);
2158a272653SPeter Holm      break;
2168a272653SPeter Holm    }
2178a272653SPeter Holm  }
2188a272653SPeter Holm  for (i = 0; i < 100 && __atomic_load_n(&running, __ATOMIC_RELAXED); i++)
2198a272653SPeter Holm    sleep_ms(1);
2208a272653SPeter Holm  if (!collide) {
2218a272653SPeter Holm    collide = 1;
2228a272653SPeter Holm    goto again;
2238a272653SPeter Holm  }
2248a272653SPeter Holm}
2258a272653SPeter Holm
2268a272653SPeter Holmstatic void execute_one(void);
2278a272653SPeter Holm
2288a272653SPeter Holm#define WAIT_FLAGS 0
2298a272653SPeter Holm
2308a272653SPeter Holmstatic void loop(void)
2318a272653SPeter Holm{
232*7f658f99SPeter Holm  int iter __unused;
2338a272653SPeter Holm  for (iter = 0;; iter++) {
2348a272653SPeter Holm    int pid = fork();
2358a272653SPeter Holm    if (pid < 0)
2368a272653SPeter Holm      exit(1);
2378a272653SPeter Holm    if (pid == 0) {
2388a272653SPeter Holm      execute_one();
2398a272653SPeter Holm      exit(0);
2408a272653SPeter Holm    }
2418a272653SPeter Holm    int status = 0;
2428a272653SPeter Holm    uint64_t start = current_time_ms();
2438a272653SPeter Holm    for (;;) {
2448a272653SPeter Holm      if (waitpid(-1, &status, WNOHANG | WAIT_FLAGS) == pid)
2458a272653SPeter Holm        break;
2468a272653SPeter Holm      sleep_ms(1);
2478a272653SPeter Holm      if (current_time_ms() - start < 5 * 1000)
2488a272653SPeter Holm        continue;
2498a272653SPeter Holm      kill_and_wait(pid, &status);
2508a272653SPeter Holm      break;
2518a272653SPeter Holm    }
2528a272653SPeter Holm  }
2538a272653SPeter Holm}
2548a272653SPeter Holm
2558a272653SPeter Holmuint64_t r[1] = {0xffffffffffffffff};
2568a272653SPeter Holm
2578a272653SPeter Holmvoid execute_call(int call)
2588a272653SPeter Holm{
2598a272653SPeter Holm  intptr_t res = 0;
2608a272653SPeter Holm  switch (call) {
2618a272653SPeter Holm  case 0:
2628a272653SPeter Holm    res = syscall(SYS_socket, 0x1cul, 1ul, 0);
2638a272653SPeter Holm    if (res != -1)
2648a272653SPeter Holm      r[0] = res;
2658a272653SPeter Holm    break;
2668a272653SPeter Holm  case 1:
2678a272653SPeter Holm    syscall(SYS_getsockopt, r[0], 6, 0x2a, 0ul, 0ul);
2688a272653SPeter Holm    break;
2698a272653SPeter Holm  case 2:
2708a272653SPeter Holm    syscall(SYS_listen, r[0], 1);
2718a272653SPeter Holm    break;
2728a272653SPeter Holm  }
2738a272653SPeter Holm}
2748a272653SPeter Holmint main(void)
2758a272653SPeter Holm{
2768a272653SPeter Holm  syscall(SYS_mmap, 0x20000000ul, 0x1000000ul, 7ul, 0x1012ul, -1, 0ul);
2778a272653SPeter Holm  for (procid = 0; procid < 4; procid++) {
2788a272653SPeter Holm    if (fork() == 0) {
2798a272653SPeter Holm      loop();
2808a272653SPeter Holm    }
2818a272653SPeter Holm  }
2828a272653SPeter Holm  sleep(1000000);
2838a272653SPeter Holm  return 0;
2848a272653SPeter Holm}
2858a272653SPeter HolmEOF
2868a272653SPeter Holmmycc -o /tmp/syzkaller14 -Wall -Wextra -O2 /tmp/syzkaller14.c -lpthread ||
2878a272653SPeter Holm    exit 1
2888a272653SPeter Holm
2898a272653SPeter Holm(cd /tmp; ./syzkaller14) &
2908a272653SPeter Holmsleep 60
2918a272653SPeter Holmpkill -9 syzkaller14
2928a272653SPeter Holmwait
2938a272653SPeter Holm
2948a272653SPeter Holmrm -f /tmp/syzkaller14 /tmp/syzkaller14.c /tmp/syzkaller14.core
2958a272653SPeter Holmexit 0
296