xref: /freebsd/tools/test/stress2/misc/syzkaller68.sh (revision fd45b686f9d92f583366c75b22c04c7ee49709c0)
1#!/bin/sh
2
3# panic: mutex Giant owned at ../../../kern/kern_thread.c:1409
4# cpuid = 0
5# time = 1688501618
6# KDB: stack backtrace:
7# db_trace_self_wrapper() at db_trace_self_wrapper+0x2b/frame 0xfffffe0171ba2510
8# vpanic() at vpanic+0x150/frame 0xfffffe0171ba2560
9# panic() at panic+0x43/frame 0xfffffe0171ba25c0
10# __mtx_assert() at __mtx_assert+0xc4/frame 0xfffffe0171ba25d0
11# thread_suspend_check() at thread_suspend_check+0x38/frame 0xfffffe0171ba2610
12# sig_intr() at sig_intr+0x78/frame 0xfffffe0171ba2640
13# fork1() at fork1+0x238/frame 0xfffffe0171ba26c0
14# kproc_create() at kproc_create+0x92/frame 0xfffffe0171ba2790
15# kproc_kthread_add() at kproc_kthread_add+0xdd/frame 0xfffffe0171ba28b0
16# zthr_create_timer() at zthr_create_timer+0x109/frame 0xfffffe0171ba2930
17# arc_init() at arc_init+0x1b44/frame 0xfffffe0171ba2970
18# dmu_init() at dmu_init+0x31/frame 0xfffffe0171ba2980
19# spa_init() at spa_init+0xed/frame 0xfffffe0171ba29a0
20# zfs_kmod_init() at zfs_kmod_init+0x1f/frame 0xfffffe0171ba29c0
21# zfs_modevent() at zfs_modevent+0module_register_init() at module_register_init+0xb0/frame 0xfffffe0171ba2a10
22# linker_load_module() at linker_load_module+0xbd2/frame 0xfffffe0171ba2d10
23# kern_kldload() at kern_kldload+0x16f/frame 0xfffffe0171ba2d60
24# vfs_byname_kld() at vfs_byname_kld+0x31/frame 0xfffffe0171ba2da0
25# sys_mount() at sys_mount+0xa9/frame 0xfffffe0171ba2e00
26# amd64_syscall() at amd64_syscall+0x150/frame 0xfffffe0171ba2f30
27# fast_syscall_common() at fast_syscall_common+0xf8/frame 0xfffffe0171ba2f30
28# --- syscall (0, FreeBSD ELF64, syscall), rip = 0x823f70e9a, rsp = 0x82513af78, rbp = 0x82513af90 KDB: enter: panic
29# [ thread pid 43886 tid 178779 ]
30# Stopped at      kdb_enter+0x32: movq    $0,0xde5863(%rip)
31# db> x/s version
32# version:        FreeBSD 14.0-CURRENT #0 main-n263953-d7614c010c762: Tue Jul  4 19:29:44 CEST 2023
33# pho@mercat1.netperf.freebsd.org:/usr/src/sys/amd64/compile/PHO
34# db>
35
36uname -p | grep -Eq "amd64|i386" || exit 0
37[ `id -u ` -ne 0 ] && echo "Must be root!" && exit 1
38
39. ../default.cfg
40prog=$(basename "$0" .sh)
41cat > /tmp/$prog.c <<EOF
42// https://syzkaller.appspot.com/bug?id=85a795d15aa54816d63f71f69bfb3a2c61635472
43// autogenerated by syzkaller (https://github.com/google/syzkaller)
44// Reported-by: syzbot+dce5858451a2329877ff@syzkaller.appspotmail.com
45
46#define _GNU_SOURCE
47
48#include <errno.h>
49#include <pthread.h>
50#include <pwd.h>
51#include <stdarg.h>
52#include <stdbool.h>
53#include <stdint.h>
54#include <stdio.h>
55#include <stdlib.h>
56#include <string.h>
57#include <sys/endian.h>
58#include <sys/syscall.h>
59#include <time.h>
60#include <unistd.h>
61
62static void sleep_ms(uint64_t ms)
63{
64  usleep(ms * 1000);
65}
66
67static uint64_t current_time_ms(void)
68{
69  struct timespec ts;
70  if (clock_gettime(CLOCK_MONOTONIC, &ts))
71    exit(1);
72  return (uint64_t)ts.tv_sec * 1000 + (uint64_t)ts.tv_nsec / 1000000;
73}
74
75static void thread_start(void* (*fn)(void*), void* arg)
76{
77  pthread_t th;
78  pthread_attr_t attr;
79  pthread_attr_init(&attr);
80  pthread_attr_setstacksize(&attr, 128 << 10);
81  int i = 0;
82  for (; i < 100; i++) {
83    if (pthread_create(&th, &attr, fn, arg) == 0) {
84      pthread_attr_destroy(&attr);
85      return;
86    }
87    if (errno == EAGAIN) {
88      usleep(50);
89      continue;
90    }
91    break;
92  }
93  exit(1);
94}
95
96typedef struct {
97  pthread_mutex_t mu;
98  pthread_cond_t cv;
99  int state;
100} event_t;
101
102static void event_init(event_t* ev)
103{
104  if (pthread_mutex_init(&ev->mu, 0))
105    exit(1);
106  if (pthread_cond_init(&ev->cv, 0))
107    exit(1);
108  ev->state = 0;
109}
110
111static void event_reset(event_t* ev)
112{
113  ev->state = 0;
114}
115
116static void event_set(event_t* ev)
117{
118  pthread_mutex_lock(&ev->mu);
119  if (ev->state)
120    exit(1);
121  ev->state = 1;
122  pthread_mutex_unlock(&ev->mu);
123  pthread_cond_broadcast(&ev->cv);
124}
125
126static void event_wait(event_t* ev)
127{
128  pthread_mutex_lock(&ev->mu);
129  while (!ev->state)
130    pthread_cond_wait(&ev->cv, &ev->mu);
131  pthread_mutex_unlock(&ev->mu);
132}
133
134static int event_isset(event_t* ev)
135{
136  pthread_mutex_lock(&ev->mu);
137  int res = ev->state;
138  pthread_mutex_unlock(&ev->mu);
139  return res;
140}
141
142static int event_timedwait(event_t* ev, uint64_t timeout)
143{
144  uint64_t start = current_time_ms();
145  uint64_t now = start;
146  pthread_mutex_lock(&ev->mu);
147  for (;;) {
148    if (ev->state)
149      break;
150    uint64_t remain = timeout - (now - start);
151    struct timespec ts;
152    ts.tv_sec = remain / 1000;
153    ts.tv_nsec = (remain % 1000) * 1000 * 1000;
154    pthread_cond_timedwait(&ev->cv, &ev->mu, &ts);
155    now = current_time_ms();
156    if (now - start > timeout)
157      break;
158  }
159  int res = ev->state;
160  pthread_mutex_unlock(&ev->mu);
161  return res;
162}
163
164struct thread_t {
165  int created, call;
166  event_t ready, done;
167};
168
169static struct thread_t threads[16];
170static void execute_call(int call);
171static int running;
172
173static void* thr(void* arg)
174{
175  struct thread_t* th = (struct thread_t*)arg;
176  for (;;) {
177    event_wait(&th->ready);
178    event_reset(&th->ready);
179    execute_call(th->call);
180    __atomic_fetch_sub(&running, 1, __ATOMIC_RELAXED);
181    event_set(&th->done);
182  }
183  return 0;
184}
185
186static void loop(void)
187{
188  int i, call, thread;
189  for (call = 0; call < 1; call++) {
190    for (thread = 0; thread < (int)(sizeof(threads) / sizeof(threads[0]));
191         thread++) {
192      struct thread_t* th = &threads[thread];
193      if (!th->created) {
194        th->created = 1;
195        event_init(&th->ready);
196        event_init(&th->done);
197        event_set(&th->done);
198        thread_start(thr, th);
199      }
200      if (!event_isset(&th->done))
201        continue;
202      event_reset(&th->done);
203      th->call = call;
204      __atomic_fetch_add(&running, 1, __ATOMIC_RELAXED);
205      event_set(&th->ready);
206      event_timedwait(&th->done, 50);
207      break;
208    }
209  }
210  for (i = 0; i < 100 && __atomic_load_n(&running, __ATOMIC_RELAXED); i++)
211    sleep_ms(1);
212}
213
214void execute_call(int call)
215{
216  switch (call) {
217  case 0:
218    memcpy((void*)0x20000440, "zfs\000", 4);
219    syscall(SYS_mount, 0x20000440ul, 0ul, 0x8300648ul, 0ul);
220    break;
221  }
222}
223int main(void)
224{
225  syscall(SYS_mmap, 0x20000000ul, 0x1000000ul, 7ul, 0x1012ul, -1, 0ul);
226  loop();
227  return 0;
228}
229EOF
230mycc -o /tmp/$prog -Wall -Wextra -O0 /tmp/$prog.c -lpthread || exit 1
231
232(cd /tmp; timeout 2m ./$prog)
233
234rm -rf /tmp/$prog /tmp/$prog.c /tmp/syzkaller.*
235exit 0
236