xref: /freebsd/tools/test/stress2/misc/ptrace8.sh (revision e32fecd0c2c3ee37c47ee100f169e7eb0282a873)
1#!/bin/sh
2
3#
4# Copyright (c) 2016 EMC Corp.
5# All rights reserved.
6#
7# Redistribution and use in source and binary forms, with or without
8# modification, are permitted provided that the following conditions
9# are met:
10# 1. Redistributions of source code must retain the above copyright
11#    notice, this list of conditions and the following disclaimer.
12# 2. Redistributions in binary form must reproduce the above copyright
13#    notice, this list of conditions and the following disclaimer in the
14#    documentation and/or other materials provided with the distribution.
15#
16# THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19# ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22# OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25# OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26# SUCH DAMAGE.
27#
28
29# A regression test for r302919.
30# Triggered a witness message:
31#
32# vmspace_free() called with the following non-sleepable locks held:
33# shared rw vm object (vm object) r = 0 locked @ kern/sys_process.c:432
34
35. ../default.cfg
36
37dir=/tmp
38odir=`pwd`
39cd $dir
40sed '1,/^EOF/d' < $odir/$0 > $dir/ptrace8.c
41mycc -o ptrace8 -Wall -Wextra -O0 -g ptrace8.c || exit 1
42rm -f ptrace8.c
43cd $odir
44
45/tmp/ptrace8
46s=$?
47
48rm -rf /tmp/ptrace8
49exit $s
50
51EOF
52#include <sys/param.h>
53#include <sys/mman.h>
54#include <sys/ptrace.h>
55#include <sys/stat.h>
56#include <sys/wait.h>
57
58#include <machine/atomic.h>
59
60#include <err.h>
61#include <errno.h>
62#include <fcntl.h>
63#include <stdio.h>
64#include <stdlib.h>
65#include <time.h>
66#include <unistd.h>
67
68volatile u_int *share;
69
70#define SYNC 0
71
72int
73main(void)
74{
75	struct ptrace_vm_entry ent;
76	size_t len;
77	int pid, r, status;
78	char path[MAXPATHLEN + 1];
79
80	len = PAGE_SIZE;
81	if ((share = mmap(NULL, len, PROT_READ | PROT_WRITE,
82	    MAP_ANON | MAP_SHARED, -1, 0)) == MAP_FAILED)
83		err(1, "mmap");
84
85	if ((pid = fork()) == 0) {
86		while (share[SYNC] == 0)
87			sleep(1);
88
89		_exit(0);
90	}
91
92	if (ptrace(PT_ATTACH, pid, 0, 0) == -1)
93		err(1, "ptrace");
94
95	if (waitpid(pid, &status, 0) == -1)
96		err(1, "waitpid");
97	else if (!WIFSTOPPED(status))
98		errx(1, "failed to stop child");
99
100	ent.pve_entry = 0;
101	ent.pve_path = path;
102	ent.pve_pathlen = sizeof(path);
103	do {
104		r = ptrace(PT_VM_ENTRY, pid, (caddr_t)&ent, 0);
105#if defined(DEBUG)
106		if (r == 0)
107			fprintf(stderr, "path = %s 0x%lx - 0x%lx\n",
108			    ent.pve_path, ent.pve_start, ent.pve_end);
109#endif
110	} while (r == 0);
111	if (r == -1 && errno != ENOENT)
112		err(1, "ptrace(PT_VM_ENTRY)");
113
114	share[SYNC] = 1;
115	if (ptrace(PT_DETACH, pid, 0, 0) == -1)
116		err(1, "ptrace");
117
118	if (waitpid(pid, &status, 0) == -1)
119		err(1, "waitpid(%d)", pid);
120
121	return (status != 0);
122}
123