xref: /freebsd/tools/test/stress2/misc/syzkaller43.sh (revision c86a43f1c7dbaba4042db017a3c3563f5b826c48)
1*c86a43f1SPeter Holm#!/bin/sh
2*c86a43f1SPeter Holm
3*c86a43f1SPeter Holm# panic: Assertion done != job_total_nbytes failed at ../../../kern/sys_socket.c:672
4*c86a43f1SPeter Holm# cpuid = 6
5*c86a43f1SPeter Holm# time = 1630912129
6*c86a43f1SPeter Holm# KDB: stack backtrace:
7*c86a43f1SPeter Holm# db_trace_self_wrapper() at db_trace_self_wrapper+0x2b/frame 0xfffffe00e4f2fa10
8*c86a43f1SPeter Holm# vpanic() at vpanic+0x187/frame 0xfffffe00e4f2fa70
9*c86a43f1SPeter Holm# panic() at panic+0x43/frame 0xfffffe00e4f2fad0
10*c86a43f1SPeter Holm# soaio_process_sb() at soaio_process_sb+0x79a/frame 0xfffffe00e4f2fb70
11*c86a43f1SPeter Holm# soaio_kproc_loop() at soaio_kproc_loop+0x96/frame 0xfffffe00e4f2fbb0
12*c86a43f1SPeter Holm# fork_exit() at fork_exit+0x80/frame 0xfffffe00e4f2fbf0
13*c86a43f1SPeter Holm# fork_trampoline() at fork_trampoline+0xe/frame 0xfffffe00e4f2fbf0
14*c86a43f1SPeter Holm# --- trap 0, rip = 0, rsp = 0, rbp = 0 ---
15*c86a43f1SPeter Holm# KDB: enter: panic
16*c86a43f1SPeter Holm# [ thread pid 26 tid 100163 ]
17*c86a43f1SPeter Holm# Stopped at      kdb_enter+0x37: movq    $0,0x127297e(%rip)
18*c86a43f1SPeter Holm# db> x/s version
19*c86a43f1SPeter Holm# version: FreeBSD 14.0-CURRENT #0 main-n249158-1f7a6325fe1b: Sun Sep  5 09:12:58 CEST 2021
20*c86a43f1SPeter Holm# pho@mercat1.netperf.freebsd.org:/usr/src/sys/amd64/compile/PHO
21*c86a43f1SPeter Holm# db>
22*c86a43f1SPeter Holm
23*c86a43f1SPeter Holm[ `uname -p` != "amd64" ] && exit 0
24*c86a43f1SPeter Holm[ `id -u ` -ne 0 ] && echo "Must be root!" && exit 1
25*c86a43f1SPeter Holm
26*c86a43f1SPeter Holm. ../default.cfg
27*c86a43f1SPeter Holmcat > /tmp/syzkaller43.c <<EOF
28*c86a43f1SPeter Holm// https://syzkaller.appspot.com/bug?id=0ecbaedb9a54dd1af01c400dabe774e5c6c6fb6c
29*c86a43f1SPeter Holm// autogenerated by syzkaller (https://github.com/google/syzkaller)
30*c86a43f1SPeter Holm// Reported-by: syzbot+c7d22dbbb9aef509421d@syzkaller.appspotmail.com
31*c86a43f1SPeter Holm
32*c86a43f1SPeter Holm#define _GNU_SOURCE
33*c86a43f1SPeter Holm
34*c86a43f1SPeter Holm#include <sys/types.h>
35*c86a43f1SPeter Holm
36*c86a43f1SPeter Holm#include <errno.h>
37*c86a43f1SPeter Holm#include <pthread.h>
38*c86a43f1SPeter Holm#include <pwd.h>
39*c86a43f1SPeter Holm#include <signal.h>
40*c86a43f1SPeter Holm#include <stdarg.h>
41*c86a43f1SPeter Holm#include <stdbool.h>
42*c86a43f1SPeter Holm#include <stdint.h>
43*c86a43f1SPeter Holm#include <stdio.h>
44*c86a43f1SPeter Holm#include <stdlib.h>
45*c86a43f1SPeter Holm#include <string.h>
46*c86a43f1SPeter Holm#include <sys/endian.h>
47*c86a43f1SPeter Holm#include <sys/syscall.h>
48*c86a43f1SPeter Holm#include <sys/wait.h>
49*c86a43f1SPeter Holm#include <time.h>
50*c86a43f1SPeter Holm#include <unistd.h>
51*c86a43f1SPeter Holm
52*c86a43f1SPeter Holmstatic void kill_and_wait(int pid, int* status)
53*c86a43f1SPeter Holm{
54*c86a43f1SPeter Holm  kill(pid, SIGKILL);
55*c86a43f1SPeter Holm  while (waitpid(-1, status, 0) != pid) {
56*c86a43f1SPeter Holm  }
57*c86a43f1SPeter Holm}
58*c86a43f1SPeter Holm
59*c86a43f1SPeter Holmstatic void sleep_ms(uint64_t ms)
60*c86a43f1SPeter Holm{
61*c86a43f1SPeter Holm  usleep(ms * 1000);
62*c86a43f1SPeter Holm}
63*c86a43f1SPeter Holm
64*c86a43f1SPeter Holmstatic uint64_t current_time_ms(void)
65*c86a43f1SPeter Holm{
66*c86a43f1SPeter Holm  struct timespec ts;
67*c86a43f1SPeter Holm  if (clock_gettime(CLOCK_MONOTONIC, &ts))
68*c86a43f1SPeter Holm    exit(1);
69*c86a43f1SPeter Holm  return (uint64_t)ts.tv_sec * 1000 + (uint64_t)ts.tv_nsec / 1000000;
70*c86a43f1SPeter Holm}
71*c86a43f1SPeter Holm
72*c86a43f1SPeter Holmstatic void thread_start(void* (*fn)(void*), void* arg)
73*c86a43f1SPeter Holm{
74*c86a43f1SPeter Holm  pthread_t th;
75*c86a43f1SPeter Holm  pthread_attr_t attr;
76*c86a43f1SPeter Holm  pthread_attr_init(&attr);
77*c86a43f1SPeter Holm  pthread_attr_setstacksize(&attr, 128 << 10);
78*c86a43f1SPeter Holm  int i = 0;
79*c86a43f1SPeter Holm  for (; i < 100; i++) {
80*c86a43f1SPeter Holm    if (pthread_create(&th, &attr, fn, arg) == 0) {
81*c86a43f1SPeter Holm      pthread_attr_destroy(&attr);
82*c86a43f1SPeter Holm      return;
83*c86a43f1SPeter Holm    }
84*c86a43f1SPeter Holm    if (errno == EAGAIN) {
85*c86a43f1SPeter Holm      usleep(50);
86*c86a43f1SPeter Holm      continue;
87*c86a43f1SPeter Holm    }
88*c86a43f1SPeter Holm    break;
89*c86a43f1SPeter Holm  }
90*c86a43f1SPeter Holm  exit(1);
91*c86a43f1SPeter Holm}
92*c86a43f1SPeter Holm
93*c86a43f1SPeter Holmtypedef struct {
94*c86a43f1SPeter Holm  pthread_mutex_t mu;
95*c86a43f1SPeter Holm  pthread_cond_t cv;
96*c86a43f1SPeter Holm  int state;
97*c86a43f1SPeter Holm} event_t;
98*c86a43f1SPeter Holm
99*c86a43f1SPeter Holmstatic void event_init(event_t* ev)
100*c86a43f1SPeter Holm{
101*c86a43f1SPeter Holm  if (pthread_mutex_init(&ev->mu, 0))
102*c86a43f1SPeter Holm    exit(1);
103*c86a43f1SPeter Holm  if (pthread_cond_init(&ev->cv, 0))
104*c86a43f1SPeter Holm    exit(1);
105*c86a43f1SPeter Holm  ev->state = 0;
106*c86a43f1SPeter Holm}
107*c86a43f1SPeter Holm
108*c86a43f1SPeter Holmstatic void event_reset(event_t* ev)
109*c86a43f1SPeter Holm{
110*c86a43f1SPeter Holm  ev->state = 0;
111*c86a43f1SPeter Holm}
112*c86a43f1SPeter Holm
113*c86a43f1SPeter Holmstatic void event_set(event_t* ev)
114*c86a43f1SPeter Holm{
115*c86a43f1SPeter Holm  pthread_mutex_lock(&ev->mu);
116*c86a43f1SPeter Holm  if (ev->state)
117*c86a43f1SPeter Holm    exit(1);
118*c86a43f1SPeter Holm  ev->state = 1;
119*c86a43f1SPeter Holm  pthread_mutex_unlock(&ev->mu);
120*c86a43f1SPeter Holm  pthread_cond_broadcast(&ev->cv);
121*c86a43f1SPeter Holm}
122*c86a43f1SPeter Holm
123*c86a43f1SPeter Holmstatic void event_wait(event_t* ev)
124*c86a43f1SPeter Holm{
125*c86a43f1SPeter Holm  pthread_mutex_lock(&ev->mu);
126*c86a43f1SPeter Holm  while (!ev->state)
127*c86a43f1SPeter Holm    pthread_cond_wait(&ev->cv, &ev->mu);
128*c86a43f1SPeter Holm  pthread_mutex_unlock(&ev->mu);
129*c86a43f1SPeter Holm}
130*c86a43f1SPeter Holm
131*c86a43f1SPeter Holmstatic int event_isset(event_t* ev)
132*c86a43f1SPeter Holm{
133*c86a43f1SPeter Holm  pthread_mutex_lock(&ev->mu);
134*c86a43f1SPeter Holm  int res = ev->state;
135*c86a43f1SPeter Holm  pthread_mutex_unlock(&ev->mu);
136*c86a43f1SPeter Holm  return res;
137*c86a43f1SPeter Holm}
138*c86a43f1SPeter Holm
139*c86a43f1SPeter Holmstatic int event_timedwait(event_t* ev, uint64_t timeout)
140*c86a43f1SPeter Holm{
141*c86a43f1SPeter Holm  uint64_t start = current_time_ms();
142*c86a43f1SPeter Holm  uint64_t now = start;
143*c86a43f1SPeter Holm  pthread_mutex_lock(&ev->mu);
144*c86a43f1SPeter Holm  for (;;) {
145*c86a43f1SPeter Holm    if (ev->state)
146*c86a43f1SPeter Holm      break;
147*c86a43f1SPeter Holm    uint64_t remain = timeout - (now - start);
148*c86a43f1SPeter Holm    struct timespec ts;
149*c86a43f1SPeter Holm    ts.tv_sec = remain / 1000;
150*c86a43f1SPeter Holm    ts.tv_nsec = (remain % 1000) * 1000 * 1000;
151*c86a43f1SPeter Holm    pthread_cond_timedwait(&ev->cv, &ev->mu, &ts);
152*c86a43f1SPeter Holm    now = current_time_ms();
153*c86a43f1SPeter Holm    if (now - start > timeout)
154*c86a43f1SPeter Holm      break;
155*c86a43f1SPeter Holm  }
156*c86a43f1SPeter Holm  int res = ev->state;
157*c86a43f1SPeter Holm  pthread_mutex_unlock(&ev->mu);
158*c86a43f1SPeter Holm  return res;
159*c86a43f1SPeter Holm}
160*c86a43f1SPeter Holm
161*c86a43f1SPeter Holmstruct thread_t {
162*c86a43f1SPeter Holm  int created, call;
163*c86a43f1SPeter Holm  event_t ready, done;
164*c86a43f1SPeter Holm};
165*c86a43f1SPeter Holm
166*c86a43f1SPeter Holmstatic struct thread_t threads[16];
167*c86a43f1SPeter Holmstatic void execute_call(int call);
168*c86a43f1SPeter Holmstatic int running;
169*c86a43f1SPeter Holm
170*c86a43f1SPeter Holmstatic void* thr(void* arg)
171*c86a43f1SPeter Holm{
172*c86a43f1SPeter Holm  struct thread_t* th = (struct thread_t*)arg;
173*c86a43f1SPeter Holm  for (;;) {
174*c86a43f1SPeter Holm    event_wait(&th->ready);
175*c86a43f1SPeter Holm    event_reset(&th->ready);
176*c86a43f1SPeter Holm    execute_call(th->call);
177*c86a43f1SPeter Holm    __atomic_fetch_sub(&running, 1, __ATOMIC_RELAXED);
178*c86a43f1SPeter Holm    event_set(&th->done);
179*c86a43f1SPeter Holm  }
180*c86a43f1SPeter Holm  return 0;
181*c86a43f1SPeter Holm}
182*c86a43f1SPeter Holm
183*c86a43f1SPeter Holmstatic void execute_one(void)
184*c86a43f1SPeter Holm{
185*c86a43f1SPeter Holm  int i, call, thread;
186*c86a43f1SPeter Holm  for (call = 0; call < 5; call++) {
187*c86a43f1SPeter Holm    for (thread = 0; thread < (int)(sizeof(threads) / sizeof(threads[0]));
188*c86a43f1SPeter Holm         thread++) {
189*c86a43f1SPeter Holm      struct thread_t* th = &threads[thread];
190*c86a43f1SPeter Holm      if (!th->created) {
191*c86a43f1SPeter Holm        th->created = 1;
192*c86a43f1SPeter Holm        event_init(&th->ready);
193*c86a43f1SPeter Holm        event_init(&th->done);
194*c86a43f1SPeter Holm        event_set(&th->done);
195*c86a43f1SPeter Holm        thread_start(thr, th);
196*c86a43f1SPeter Holm      }
197*c86a43f1SPeter Holm      if (!event_isset(&th->done))
198*c86a43f1SPeter Holm        continue;
199*c86a43f1SPeter Holm      event_reset(&th->done);
200*c86a43f1SPeter Holm      th->call = call;
201*c86a43f1SPeter Holm      __atomic_fetch_add(&running, 1, __ATOMIC_RELAXED);
202*c86a43f1SPeter Holm      event_set(&th->ready);
203*c86a43f1SPeter Holm      event_timedwait(&th->done, 50);
204*c86a43f1SPeter Holm      break;
205*c86a43f1SPeter Holm    }
206*c86a43f1SPeter Holm  }
207*c86a43f1SPeter Holm  for (i = 0; i < 100 && __atomic_load_n(&running, __ATOMIC_RELAXED); i++)
208*c86a43f1SPeter Holm    sleep_ms(1);
209*c86a43f1SPeter Holm}
210*c86a43f1SPeter Holm
211*c86a43f1SPeter Holmstatic void execute_one(void);
212*c86a43f1SPeter Holm
213*c86a43f1SPeter Holm#define WAIT_FLAGS 0
214*c86a43f1SPeter Holm
215*c86a43f1SPeter Holmstatic void loop(void)
216*c86a43f1SPeter Holm{
217*c86a43f1SPeter Holm  int iter = 0;
218*c86a43f1SPeter Holm  for (;; iter++) {
219*c86a43f1SPeter Holm    int pid = fork();
220*c86a43f1SPeter Holm    if (pid < 0)
221*c86a43f1SPeter Holm      exit(1);
222*c86a43f1SPeter Holm    if (pid == 0) {
223*c86a43f1SPeter Holm      execute_one();
224*c86a43f1SPeter Holm      exit(0);
225*c86a43f1SPeter Holm    }
226*c86a43f1SPeter Holm    int status = 0;
227*c86a43f1SPeter Holm    uint64_t start = current_time_ms();
228*c86a43f1SPeter Holm    for (;;) {
229*c86a43f1SPeter Holm      if (waitpid(-1, &status, WNOHANG | WAIT_FLAGS) == pid)
230*c86a43f1SPeter Holm        break;
231*c86a43f1SPeter Holm      sleep_ms(1);
232*c86a43f1SPeter Holm      if (current_time_ms() - start < 5000) {
233*c86a43f1SPeter Holm        continue;
234*c86a43f1SPeter Holm      }
235*c86a43f1SPeter Holm      kill_and_wait(pid, &status);
236*c86a43f1SPeter Holm      break;
237*c86a43f1SPeter Holm    }
238*c86a43f1SPeter Holm  }
239*c86a43f1SPeter Holm}
240*c86a43f1SPeter Holm
241*c86a43f1SPeter Holm#ifndef SYS_aio_readv
242*c86a43f1SPeter Holm#define SYS_aio_readv 579
243*c86a43f1SPeter Holm#endif
244*c86a43f1SPeter Holm
245*c86a43f1SPeter Holmuint64_t r[1] = {0xffffffffffffffff};
246*c86a43f1SPeter Holm
247*c86a43f1SPeter Holmvoid execute_call(int call)
248*c86a43f1SPeter Holm{
249*c86a43f1SPeter Holm  intptr_t res = 0;
250*c86a43f1SPeter Holm  switch (call) {
251*c86a43f1SPeter Holm  case 0:
252*c86a43f1SPeter Holm    res = syscall(SYS_socket, 0x1cul, 5ul, 0x84);
253*c86a43f1SPeter Holm    if (res != -1)
254*c86a43f1SPeter Holm      r[0] = res;
255*c86a43f1SPeter Holm    break;
256*c86a43f1SPeter Holm  case 1:
257*c86a43f1SPeter Holm    *(uint64_t*)0x20000440 = 0;
258*c86a43f1SPeter Holm    *(uint32_t*)0x20000448 = 0;
259*c86a43f1SPeter Holm    *(uint64_t*)0x20000450 = 0;
260*c86a43f1SPeter Holm    *(uint64_t*)0x20000458 = 0;
261*c86a43f1SPeter Holm    *(uint64_t*)0x20000460 = 0;
262*c86a43f1SPeter Holm    *(uint64_t*)0x20000468 = 0;
263*c86a43f1SPeter Holm    *(uint32_t*)0x20000470 = 0;
264*c86a43f1SPeter Holm    syscall(SYS_recvmsg, r[0], 0x20000440ul, 0x40040ul);
265*c86a43f1SPeter Holm    break;
266*c86a43f1SPeter Holm  case 2:
267*c86a43f1SPeter Holm    *(uint32_t*)0x200006c0 = r[0];
268*c86a43f1SPeter Holm    *(uint64_t*)0x200006c8 = 0x800;
269*c86a43f1SPeter Holm    *(uint64_t*)0x200006d0 = 0;
270*c86a43f1SPeter Holm    *(uint64_t*)0x200006d8 = 0;
271*c86a43f1SPeter Holm    *(uint32_t*)0x200006e0 = -1;
272*c86a43f1SPeter Holm    *(uint32_t*)0x200006e4 = 8;
273*c86a43f1SPeter Holm    *(uint64_t*)0x200006e8 = 0xffffffffffff658c;
274*c86a43f1SPeter Holm    *(uint32_t*)0x200006f0 = 4;
275*c86a43f1SPeter Holm    *(uint32_t*)0x200006f4 = 0x1ff;
276*c86a43f1SPeter Holm    *(uint64_t*)0x200006f8 = 0x40;
277*c86a43f1SPeter Holm    *(uint64_t*)0x20000700 = 0;
278*c86a43f1SPeter Holm    *(uint64_t*)0x20000708 = 0;
279*c86a43f1SPeter Holm    *(uint32_t*)0x20000710 = 4;
280*c86a43f1SPeter Holm    *(uint32_t*)0x20000714 = 8;
281*c86a43f1SPeter Holm    *(uint32_t*)0x20000718 = 6;
282*c86a43f1SPeter Holm    *(uint64_t*)0x20000720 = 0;
283*c86a43f1SPeter Holm    *(uint64_t*)0x20000728 = 0;
284*c86a43f1SPeter Holm    syscall(SYS_aio_readv, 0x200006c0ul);
285*c86a43f1SPeter Holm    break;
286*c86a43f1SPeter Holm  case 3:
287*c86a43f1SPeter Holm    *(uint32_t*)0x20000140 = 0;
288*c86a43f1SPeter Holm    *(uint16_t*)0x20000144 = 4;
289*c86a43f1SPeter Holm    *(uint8_t*)0x20000146 = 1;
290*c86a43f1SPeter Holm    syscall(SYS_setsockopt, r[0], 0x84, 0x1e, 0x20000140ul, 8ul);
291*c86a43f1SPeter Holm    break;
292*c86a43f1SPeter Holm  case 4:
293*c86a43f1SPeter Holm    *(uint64_t*)0x20000580 = 0x20000080;
294*c86a43f1SPeter Holm    *(uint8_t*)0x20000080 = 0x1c;
295*c86a43f1SPeter Holm    *(uint8_t*)0x20000081 = 0x1c;
296*c86a43f1SPeter Holm    *(uint16_t*)0x20000082 = htobe16(0x4e22);
297*c86a43f1SPeter Holm    *(uint32_t*)0x20000084 = 0;
298*c86a43f1SPeter Holm    *(uint64_t*)0x20000088 = htobe64(0);
299*c86a43f1SPeter Holm    *(uint64_t*)0x20000090 = htobe64(1);
300*c86a43f1SPeter Holm    *(uint32_t*)0x20000098 = 0;
301*c86a43f1SPeter Holm    *(uint32_t*)0x20000588 = 0x1c;
302*c86a43f1SPeter Holm    *(uint64_t*)0x20000590 = 0x20000400;
303*c86a43f1SPeter Holm    *(uint64_t*)0x20000400 = 0x200000c0;
304*c86a43f1SPeter Holm    memset((void*)0x200000c0, 239, 1);
305*c86a43f1SPeter Holm    *(uint64_t*)0x20000408 = 1;
306*c86a43f1SPeter Holm    *(uint32_t*)0x20000598 = 1;
307*c86a43f1SPeter Holm    *(uint64_t*)0x200005a0 = 0;
308*c86a43f1SPeter Holm    *(uint32_t*)0x200005a8 = 0xd0;
309*c86a43f1SPeter Holm    *(uint32_t*)0x200005ac = 0;
310*c86a43f1SPeter Holm    syscall(SYS_sendmsg, r[0], 0x20000580ul, 0ul);
311*c86a43f1SPeter Holm    break;
312*c86a43f1SPeter Holm  }
313*c86a43f1SPeter Holm}
314*c86a43f1SPeter Holmint main(void)
315*c86a43f1SPeter Holm{
316*c86a43f1SPeter Holm  syscall(SYS_mmap, 0x20000000ul, 0x1000000ul, 7ul, 0x1012ul, -1, 0ul);
317*c86a43f1SPeter Holm  loop();
318*c86a43f1SPeter Holm  return 0;
319*c86a43f1SPeter Holm}
320*c86a43f1SPeter HolmEOF
321*c86a43f1SPeter Holmmycc -o /tmp/syzkaller43 -Wall -Wextra -O0 /tmp/syzkaller43.c -lpthread ||
322*c86a43f1SPeter Holm    exit 1
323*c86a43f1SPeter Holm
324*c86a43f1SPeter Holm(cd /tmp; timeout 3m ./syzkaller43)
325*c86a43f1SPeter Holm
326*c86a43f1SPeter Holmrm -rf /tmp/syzkaller43 syzkaller43.c /tmp/syzkaller.*
327*c86a43f1SPeter Holmexit 0
328