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