1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
|
/* $OpenBSD: kcov.c,v 1.6 2019/01/03 08:51:31 anton Exp $ */
/*
* Copyright (c) 2018 Anton Lindqvist <anton@openbsd.org>
*
* Permission to use, copy, modify, and distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
#include <sys/ioctl.h>
#include <sys/kcov.h>
#include <sys/mman.h>
#include <sys/wait.h>
#include <err.h>
#include <errno.h>
#include <fcntl.h>
#include <pthread.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
static int test_close(int, int);
static int test_coverage(int, int);
static int test_dying(int, int);
static int test_exec(int, int);
static int test_fork(int, int);
static int test_open(int, int);
static int test_state(int, int);
static int check_coverage(const unsigned long *, int, unsigned long, int);
static void do_syscall(void);
static void dump(const unsigned long *, int mode);
static void kcov_disable(int);
static void kcov_enable(int, int);
static int kcov_open(void);
static __dead void usage(void);
static const char *self;
static unsigned long bufsize = 256 << 10;
int
main(int argc, char *argv[])
{
struct {
const char *name;
int (*fn)(int, int);
int coverage; /* test must produce coverage */
} tests[] = {
{ "close", test_close, 0 },
{ "coverage", test_coverage, 1 },
{ "dying", test_dying, 1 },
{ "exec", test_exec, 1 },
{ "fork", test_fork, 1 },
{ "open", test_open, 0 },
{ "state", test_state, 1 },
{ NULL, NULL, 0 },
};
unsigned long *cover;
int c, fd, i;
int error = 0;
int mode = 0;
int prereq = 0;
int reexec = 0;
int verbose = 0;
self = argv[0];
while ((c = getopt(argc, argv, "Em:pv")) != -1)
switch (c) {
case 'E':
reexec = 1;
break;
case 'm':
if (strcmp(optarg, "pc") == 0)
mode = KCOV_MODE_TRACE_PC;
else
errx(1, "unknown mode %s", optarg);
break;
case 'p':
prereq = 1;
break;
case 'v':
verbose = 1;
break;
default:
usage();
}
argc -= optind;
argv += optind;
if (prereq) {
fd = kcov_open();
close(fd);
return 0;
}
if (reexec) {
do_syscall();
return 0;
}
if (mode == 0 || argc != 1)
usage();
for (i = 0; tests[i].name != NULL; i++)
if (strcmp(argv[0], tests[i].name) == 0)
break;
if (tests[i].name == NULL)
errx(1, "%s: no such test", argv[0]);
fd = kcov_open();
if (ioctl(fd, KIOSETBUFSIZE, &bufsize) == -1)
err(1, "ioctl: KIOSETBUFSIZE");
cover = mmap(NULL, bufsize * sizeof(unsigned long),
PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
if (cover == MAP_FAILED)
err(1, "mmap");
*cover = 0;
error = tests[i].fn(fd, mode);
if (verbose)
dump(cover, mode);
if (check_coverage(cover, mode, bufsize, tests[i].coverage))
error = 1;
if (munmap(cover, bufsize * sizeof(unsigned long)) == -1)
err(1, "munmap");
close(fd);
return error;
}
static __dead void
usage(void)
{
fprintf(stderr, "usage: kcov [-Epv] -t mode test\n");
exit(1);
}
static void
do_syscall(void)
{
getpid();
}
static int
check_coverage(const unsigned long *cover, int mode, unsigned long maxsize,
int nonzero)
{
int error = 0;
if (nonzero && cover[0] == 0) {
warnx("coverage empty (count=0)\n");
return 1;
} else if (!nonzero && cover[0] != 0) {
warnx("coverage not empty (count=%lu)\n", *cover);
return 1;
} else if (cover[0] >= maxsize) {
warnx("coverage overflow (count=%lu, max=%lu)\n", *cover, maxsize);
return 1;
}
return error;
}
static void
dump(const unsigned long *cover, int mode)
{
unsigned long i;
int stride = 1;
for (i = 0; i < cover[0]; i++)
printf("%p\n", (void *)cover[i * stride + 1]);
}
static int
kcov_open(void)
{
int fd;
fd = open("/dev/kcov", O_RDWR);
if (fd == -1)
err(1, "open: /dev/kcov");
return fd;
}
static void
kcov_enable(int fd, int mode)
{
if (ioctl(fd, KIOENABLE, &mode) == -1)
err(1, "ioctl: KIOENABLE");
}
static void
kcov_disable(int fd)
{
if (ioctl(fd, KIODISABLE) == -1)
err(1, "ioctl: KIODISABLE");
}
/*
* Close before mmap.
*/
static int
test_close(int oldfd, int mode)
{
int fd;
fd = kcov_open();
close(fd);
return 0;
}
/*
* Coverage of current thread.
*/
static int
test_coverage(int fd, int mode)
{
kcov_enable(fd, mode);
do_syscall();
kcov_disable(fd);
return 0;
}
static void *
closer(void *arg)
{
int fd = *((int *)arg);
close(fd);
return NULL;
}
/*
* Close kcov descriptor in another thread during tracing.
*/
static int
test_dying(int fd, int mode)
{
pthread_t th;
int error;
kcov_enable(fd, mode);
if ((error = pthread_create(&th, NULL, closer, &fd)))
errc(1, error, "pthread_create");
if ((error = pthread_join(th, NULL)))
errc(1, error, "pthread_join");
if (close(fd) == -1) {
if (errno != EBADF)
err(1, "close");
} else {
warnx("expected kcov descriptor to be closed");
return 1;
}
return 0;
}
/*
* Coverage of thread after exec.
*/
static int
test_exec(int fd, int mode)
{
pid_t pid;
int status;
pid = fork();
if (pid == -1)
err(1, "fork");
if (pid == 0) {
kcov_enable(fd, mode);
execlp(self, self, "-E", NULL);
_exit(1);
}
if (waitpid(pid, &status, 0) == -1)
err(1, "waitpid");
if (WIFSIGNALED(status)) {
warnx("terminated by signal (%d)", WTERMSIG(status));
return 1;
} else if (WEXITSTATUS(status) != 0) {
warnx("non-zero exit (%d)", WEXITSTATUS(status));
return 1;
}
/* Upon exit, the kcov descriptor must be reusable again. */
kcov_enable(fd, mode);
kcov_disable(fd);
return 0;
}
/*
* Coverage of thread after fork.
*/
static int
test_fork(int fd, int mode)
{
pid_t pid;
int status;
pid = fork();
if (pid == -1)
err(1, "fork");
if (pid == 0) {
kcov_enable(fd, mode);
do_syscall();
_exit(0);
}
if (waitpid(pid, &status, 0) == -1)
err(1, "waitpid");
if (WIFSIGNALED(status)) {
warnx("terminated by signal (%d)", WTERMSIG(status));
return 1;
} else if (WEXITSTATUS(status) != 0) {
warnx("non-zero exit (%d)", WEXITSTATUS(status));
return 1;
}
/* Upon exit, the kcov descriptor must be reusable again. */
kcov_enable(fd, mode);
kcov_disable(fd);
return 0;
}
/*
* Open /dev/kcov more than once.
*/
static int
test_open(int oldfd, int mode)
{
unsigned long *cover;
int fd;
int error = 0;
fd = kcov_open();
if (ioctl(fd, KIOSETBUFSIZE, &bufsize) == -1)
err(1, "ioctl: KIOSETBUFSIZE");
cover = mmap(NULL, bufsize * sizeof(unsigned long),
PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
if (cover == MAP_FAILED)
err(1, "mmap");
kcov_enable(fd, mode);
do_syscall();
kcov_disable(fd);
error = check_coverage(cover, mode, bufsize, 1);
if (munmap(cover, bufsize * sizeof(unsigned long)))
err(1, "munmap");
close(fd);
return error;
}
/*
* State transitions.
*/
static int
test_state(int fd, int mode)
{
if (ioctl(fd, KIOENABLE, &mode) == -1) {
warn("KIOSETBUFSIZE -> KIOENABLE");
return 1;
}
if (ioctl(fd, KIODISABLE) == -1) {
warn("KIOENABLE -> KIODISABLE");
return 1;
}
if (ioctl(fd, KIOSETBUFSIZE, 0) != -1) {
warnx("KIOSETBUFSIZE -> KIOSETBUFSIZE");
return 1;
}
if (ioctl(fd, KIODISABLE) != -1) {
warnx("KIOSETBUFSIZE -> KIODISABLE");
return 1;
}
kcov_enable(fd, mode);
if (ioctl(fd, KIOENABLE, &mode) != -1) {
warnx("KIOENABLE -> KIOENABLE");
return 1;
}
if (ioctl(fd, KIOSETBUFSIZE, 0) != -1) {
warnx("KIOENABLE -> KIOSETBUFSIZE");
return 1;
}
kcov_disable(fd);
return 0;
}
|