Training courses

Kernel and Embedded Linux

Bootlin training courses

Embedded Linux, kernel,
Yocto Project, Buildroot, real-time,
graphics, boot time, debugging...

Bootlin logo

Elixir Cross Referencer

  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
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
/*
 * SPDX-License-Identifier: BSD-2-Clause
 *
 * Copyright (c) 2018, 2019 Andrew Turner
 *
 * This software was developed by SRI International and the University of
 * Cambridge Computer Laboratory under DARPA/AFRL contract FA8750-10-C-0237
 * ("CTSRD"), as part of the DARPA CRASH research programme.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 * SUCH DAMAGE.
 */
#include <sys/cdefs.h>

#include <sys/param.h>
#include <sys/ioctl.h>
#include <sys/kcov.h>
#include <sys/mman.h>

#include <errno.h>
#include <fcntl.h>
#include <pthread.h>
#include <semaphore.h>
#include <unistd.h>

#include <atf-c.h>

#define PAGE_SIZE sysconf(_SC_PAGESIZE)

static int
open_kcov(void)
{
	int fd;

	fd = open("/dev/kcov", O_RDWR);
	if (fd == -1)
		atf_tc_skip("Failed to open /dev/kcov");

	return fd;
}

static int
pick_unassigned_fd(int greater_than_fd)
{
	int fd2;

	fd2 = greater_than_fd;
	do {
		++fd2;
	} while (fcntl(fd2, F_GETFL) != -1 || errno != EBADF);

	return fd2;
}

ATF_TC_WITHOUT_HEAD(kcov_dup2);
ATF_TC_BODY(kcov_dup2, tc)
{
	int fd1, fd2;
	fd1 = open_kcov();

	fd2 = pick_unassigned_fd(fd1);

	/* Test the dup2(2) trick used by syzkaller */
	ATF_REQUIRE_EQ(dup2(fd1, fd2), fd2);

	close(fd1);
	close(fd2);
}

ATF_TC_WITHOUT_HEAD(kcov_multiopen);
ATF_TC_BODY(kcov_multiopen, tc)
{
	int fd1, fd2;
	fd1 = open_kcov();

	fd2 = open("/dev/kcov", O_RDWR);
	ATF_REQUIRE(fd2 != -1);

	close(fd1);
	close(fd2);
}

ATF_TC_WITHOUT_HEAD(kcov_open_close_open);
ATF_TC_BODY(kcov_open_close_open, tc)
{
	int fd;

	fd = open_kcov();
	close(fd);
	fd = open("/dev/kcov", O_RDWR);
	ATF_REQUIRE(fd != -1);

	close(fd);
}

ATF_TC_WITHOUT_HEAD(kcov_bufsize);
ATF_TC_BODY(kcov_bufsize, tc)
{
	int fd;
	uint64_t size;
	fd = open_kcov();

	size = 0;
	ATF_CHECK(ioctl(fd, KCOV_IOC_SETBUFSIZE, &size) == -1);
	size = 2;
	ATF_CHECK(ioctl(fd, KCOV_IOC_SETBUFSIZE, &size) == 0);

	close(fd);
}

ATF_TC_WITHOUT_HEAD(kcov_mmap);
ATF_TC_BODY(kcov_mmap, tc)
{
	void *data;
	int fd;
	uint64_t size = 2 * PAGE_SIZE / KCOV_ENTRY_SIZE;

	fd = open_kcov();

	ATF_CHECK(mmap(NULL, PAGE_SIZE, PROT_READ | PROT_WRITE, MAP_SHARED,
	    fd, 0) == MAP_FAILED);

	ATF_REQUIRE(ioctl(fd, KCOV_IOC_SETBUFSIZE, &size) == 0);

	ATF_REQUIRE((data = mmap(NULL, 2 * PAGE_SIZE, PROT_READ | PROT_WRITE,
	    MAP_SHARED, fd, 0)) != MAP_FAILED);

	munmap(data, 2 * PAGE_SIZE);

	close(fd);
}

/* This shouldn't panic */
ATF_TC_WITHOUT_HEAD(kcov_mmap_no_munmap);
ATF_TC_BODY(kcov_mmap_no_munmap, tc)
{
	int fd;
	uint64_t size = PAGE_SIZE / KCOV_ENTRY_SIZE;

	fd = open_kcov();

	ATF_REQUIRE(ioctl(fd, KCOV_IOC_SETBUFSIZE, &size) ==0);

	ATF_CHECK(mmap(NULL, PAGE_SIZE, PROT_READ | PROT_WRITE, MAP_SHARED,
	    fd, 0) != MAP_FAILED);

	close(fd);
}

ATF_TC_WITHOUT_HEAD(kcov_mmap_no_munmap_no_close);
ATF_TC_BODY(kcov_mmap_no_munmap_no_close, tc)
{
	int fd;
	uint64_t size = PAGE_SIZE / KCOV_ENTRY_SIZE;

	fd = open_kcov();

	ATF_REQUIRE(ioctl(fd, KCOV_IOC_SETBUFSIZE, &size) ==0);

	ATF_CHECK(mmap(NULL, PAGE_SIZE, PROT_READ | PROT_WRITE, MAP_SHARED,
	    fd, 0) != MAP_FAILED);
}

static sem_t sem1, sem2;

static void *
kcov_mmap_enable_thread(void *data)
{
	int fd;
	uint64_t size = PAGE_SIZE / KCOV_ENTRY_SIZE;
	int mode;

	fd = open_kcov();
	*(int *)data = fd;

	ATF_REQUIRE(ioctl(fd, KCOV_IOC_SETBUFSIZE, &size) ==0);
	ATF_CHECK(mmap(NULL, PAGE_SIZE, PROT_READ | PROT_WRITE, MAP_SHARED,
	    fd, 0) != MAP_FAILED);
	mode = KCOV_MODE_NONE;
	ATF_CHECK(ioctl(fd, KCOV_IOC_ENABLE, &mode) == 0);

	sem_post(&sem1);
	sem_wait(&sem2);

	return NULL;
}

ATF_TC_WITHOUT_HEAD(kcov_mmap_enable_thread_close);
ATF_TC_BODY(kcov_mmap_enable_thread_close, tc)
{
	pthread_t thread;
	int fd;

	sem_init(&sem1, 0, 0);
	sem_init(&sem2, 0, 0);
	pthread_create(&thread, NULL,
	    kcov_mmap_enable_thread, &fd);
	sem_wait(&sem1);
	close(fd);
	sem_post(&sem2);
	pthread_join(thread, NULL);
}

ATF_TC_WITHOUT_HEAD(kcov_enable);
ATF_TC_BODY(kcov_enable, tc)
{
	int fd;
	uint64_t size = PAGE_SIZE / KCOV_ENTRY_SIZE;
	int mode;

	fd = open_kcov();

	mode = KCOV_MODE_NONE;
	ATF_CHECK(ioctl(fd, KCOV_IOC_ENABLE, &mode) == -1);

	ATF_REQUIRE(ioctl(fd, KCOV_IOC_SETBUFSIZE, &size) ==0);

	/* We need to enable before disable */
	ATF_CHECK(ioctl(fd, KCOV_IOC_DISABLE) == -1);

	/* Check enabling works only with a valid trace method */
	ATF_CHECK(ioctl(fd, KCOV_IOC_ENABLE, &mode) == 0);
	ATF_CHECK(ioctl(fd, KCOV_IOC_ENABLE, &mode) == -1);

	/* Disable should only be called once */
	ATF_CHECK(ioctl(fd, KCOV_IOC_DISABLE) == 0);
	ATF_CHECK(ioctl(fd, KCOV_IOC_DISABLE) == -1);

	/* Re-enabling and changing mode should also work */
	mode = KCOV_MODE_NONE;
	ATF_CHECK(ioctl(fd, KCOV_IOC_ENABLE, &mode) == 0);
	ATF_CHECK(ioctl(fd, KCOV_IOC_DISABLE) == 0);
	mode = KCOV_MODE_TRACE_PC;
	ATF_CHECK(ioctl(fd, KCOV_IOC_ENABLE, &mode) == 0);
	ATF_CHECK(ioctl(fd, KCOV_IOC_DISABLE) == 0);
	mode = KCOV_MODE_TRACE_CMP;
	ATF_CHECK(ioctl(fd, KCOV_IOC_ENABLE, &mode) == 0);
	ATF_CHECK(ioctl(fd, KCOV_IOC_DISABLE) == 0);

	close(fd);
}

ATF_TC_WITHOUT_HEAD(kcov_enable_no_disable);
ATF_TC_BODY(kcov_enable_no_disable, tc)
{
	int fd;
	uint64_t size = PAGE_SIZE / KCOV_ENTRY_SIZE;
	int mode;

	fd = open_kcov();
	ATF_REQUIRE(ioctl(fd, KCOV_IOC_SETBUFSIZE, &size) ==0);
	mode = KCOV_MODE_NONE;
	ATF_CHECK(ioctl(fd, KCOV_IOC_ENABLE, &mode) == 0);
	close(fd);
}

ATF_TC_WITHOUT_HEAD(kcov_enable_no_disable_no_close);
ATF_TC_BODY(kcov_enable_no_disable_no_close, tc)
{
	int fd;
	uint64_t size = PAGE_SIZE / KCOV_ENTRY_SIZE;
	int mode;

	fd = open_kcov();
	ATF_REQUIRE(ioctl(fd, KCOV_IOC_SETBUFSIZE, &size) ==0);
	mode = KCOV_MODE_NONE;
	ATF_CHECK(ioctl(fd, KCOV_IOC_ENABLE, &mode) == 0);
}

static void *
common_head_raw(bool fd_dup, int *fdp)
{
	void *data;
	int fd, fd2;
	uint64_t size = PAGE_SIZE / KCOV_ENTRY_SIZE;

	fd = open_kcov();

	/* Test the dup2(2) trick used by syzkaller */
	if (fd_dup) {
		fd2 = pick_unassigned_fd(fd);
		ATF_REQUIRE_EQ(dup2(fd, fd2), fd2);
		close(fd);
		fd = fd2;
	}

	ATF_REQUIRE_MSG(ioctl(fd, KCOV_IOC_SETBUFSIZE, &size) == 0,
	    "Unable to set the kcov buffer size");

	data = mmap(NULL, PAGE_SIZE, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
	ATF_REQUIRE_MSG(data != MAP_FAILED, "Unable to mmap the kcov buffer");

	*fdp = fd;
	return data;
}

static void *
common_head(int *fdp)
{

	return common_head_raw(false, fdp);
}

static void
common_tail(int fd, kcov_int_t *data)
{

	ATF_REQUIRE_MSG(munmap(__UNVOLATILE(data), PAGE_SIZE) == 0,
	    "Unable to unmap the kcov buffer");

	close(fd);
}

static void
kcov_basic(bool fd_dup, int mode)
{
	kcov_int_t *buf;
	int fd;

	buf = common_head_raw(fd_dup, &fd);
	ATF_REQUIRE_MSG(ioctl(fd, KCOV_IOC_ENABLE, &mode) == 0,
	    "Unable to enable kcov ");

	buf[0] = 0;

	sleep(0); /* XXX: Is it enough for all trace types? */
	ATF_REQUIRE_MSG(buf[0] != 0, "No records found");

	ATF_REQUIRE_MSG(ioctl(fd, KCOV_IOC_DISABLE) == 0,
	    "Unable to disable kcov");

	common_tail(fd, buf);
}

ATF_TC_WITHOUT_HEAD(kcov_basic_pc);
ATF_TC_BODY(kcov_basic_pc, tc)
{

	kcov_basic(false, KCOV_MODE_TRACE_PC);
}

ATF_TC_WITHOUT_HEAD(kcov_basic_cmp);
ATF_TC_BODY(kcov_basic_cmp, tc)
{

	atf_tc_skip("XXX: GCC8 needed");

	kcov_basic(false, KCOV_MODE_TRACE_CMP);
}

ATF_TC_WITHOUT_HEAD(kcov_basic_dup2_pc);
ATF_TC_BODY(kcov_basic_dup2_pc, tc)
{

	kcov_basic(true, KCOV_MODE_TRACE_PC);
}

ATF_TC_WITHOUT_HEAD(kcov_basic_dup2_cmp);
ATF_TC_BODY(kcov_basic_dup2_cmp, tc)
{

	atf_tc_skip("XXX: GCC8 needed");

	kcov_basic(true, KCOV_MODE_TRACE_CMP);
}

ATF_TC_WITHOUT_HEAD(kcov_multienable_on_the_same_thread);
ATF_TC_BODY(kcov_multienable_on_the_same_thread, tc)
{
	kcov_int_t *buf1, *buf2;
	int fd1, fd2;
	int mode;

	buf1 = common_head(&fd1);
	buf2 = common_head(&fd2);
	mode = KCOV_MODE_NONE;
	ATF_REQUIRE_MSG(ioctl(fd1, KCOV_IOC_ENABLE, &mode) == 0,
	    "Unable to enable kcov");
	ATF_REQUIRE_ERRNO(EBUSY, ioctl(fd2, KCOV_IOC_ENABLE, &mode) != 0);

	ATF_REQUIRE_MSG(ioctl(fd1, KCOV_IOC_DISABLE) == 0,
	    "Unable to disable kcov");

	common_tail(fd1, buf1);
	common_tail(fd2, buf2);
}

static void *
thread_buffer_access_test_helper(void *ptr)
{
	kcov_int_t *buf = ptr;

	/* Test mapped buffer access from a custom thread */
	buf[0] = buf[0];

	return NULL;
}

ATF_TC_WITHOUT_HEAD(kcov_buffer_access_from_custom_thread);
ATF_TC_BODY(kcov_buffer_access_from_custom_thread, tc)
{
	pthread_t thread;
	kcov_int_t *buf;
	int fd;
	int mode;

	buf = common_head(&fd);

	mode = KCOV_MODE_TRACE_PC;
	ATF_REQUIRE_MSG(ioctl(fd, KCOV_IOC_ENABLE, &mode) == 0,
	    "Unable to enable kcov ");

	pthread_create(&thread, NULL, thread_buffer_access_test_helper,
	    __UNVOLATILE(buf));
	pthread_join(thread, NULL);

	ATF_REQUIRE_MSG(ioctl(fd, KCOV_IOC_DISABLE) == 0,
	    "Unable to disable kcov");

	common_tail(fd, buf);
}

static void *
thread_test_helper(void *ptr)
{
	volatile int i;

	/* It does not matter what operation is in action. */
	for (i = 0; i < 1000; i++) {
		if (getpid() == 0)
			break;
	}

	return NULL;
}

ATF_TC_WITHOUT_HEAD(kcov_thread);
ATF_TC_BODY(kcov_thread, tc)
{
	pthread_t thread;
	kcov_int_t *buf;
	int fd;
	int mode;
	volatile int i;

	buf = common_head(&fd);

	mode = KCOV_MODE_TRACE_PC;
	ATF_REQUIRE_MSG(ioctl(fd, KCOV_IOC_ENABLE, &mode) == 0,
	    "Unable to enable kcov ");

	/* The thread does something, does not matter what exactly. */
	pthread_create(&thread, NULL, thread_test_helper, __UNVOLATILE(buf));

	buf[0] = 0;
	for (i = 0; i < 10000; i++)
		continue;
	ATF_REQUIRE_EQ_MSG(buf[0], 0,
	    "Records changed in blocked thread");

	pthread_join(thread, NULL);

	ATF_REQUIRE_EQ_MSG(ioctl(fd, KCOV_IOC_DISABLE), 0,
	    "Unable to disable kcov");

	common_tail(fd, buf);
}

static void *
multiple_threads_helper(void *ptr __unused)
{
	kcov_int_t *buf;
	int fd;
	int mode;

	buf = common_head(&fd);
	mode = KCOV_MODE_TRACE_PC;
	ATF_REQUIRE_MSG(ioctl(fd, KCOV_IOC_ENABLE, &mode) == 0,
	    "Unable to enable kcov ");

	buf[0] = 0;

	sleep(0);
	ATF_REQUIRE_MSG(buf[0] != 0, "No records found");

	ATF_REQUIRE_MSG(ioctl(fd, KCOV_IOC_DISABLE) == 0,
	    "Unable to disable kcov");

	common_tail(fd, buf);

	return NULL;
}

static void
kcov_multiple_threads(size_t N)
{
	pthread_t thread[32];
	size_t i;
	int fd;

	/*
	 * Check if /dev/kcov is available, if not bail out.
	 * Verifying it on a per-thread basis is flaky.
	 */
	fd = open_kcov();
	ATF_REQUIRE(close(fd) == 0);

	ATF_REQUIRE(__arraycount(thread) >= N);

	for (i = 0; i < N; i++)
		pthread_create(&thread[i], NULL, multiple_threads_helper, NULL);

	for (i = 0; i < N; i++)
		pthread_join(thread[i], NULL);
}

#define KCOV_MULTIPLE_THREADS(n)		\
ATF_TC_WITHOUT_HEAD(kcov_multiple_threads##n);	\
ATF_TC_BODY(kcov_multiple_threads##n, tc)	\
{						\
						\
	kcov_multiple_threads(n);		\
}

KCOV_MULTIPLE_THREADS(2)
KCOV_MULTIPLE_THREADS(4)
KCOV_MULTIPLE_THREADS(8)
KCOV_MULTIPLE_THREADS(16)
KCOV_MULTIPLE_THREADS(32)

ATF_TP_ADD_TCS(tp)
{

	ATF_TP_ADD_TC(tp, kcov_dup2);
	ATF_TP_ADD_TC(tp, kcov_multiopen);
	ATF_TP_ADD_TC(tp, kcov_open_close_open);
	ATF_TP_ADD_TC(tp, kcov_bufsize);
	ATF_TP_ADD_TC(tp, kcov_mmap);
	ATF_TP_ADD_TC(tp, kcov_mmap_no_munmap);
	ATF_TP_ADD_TC(tp, kcov_mmap_no_munmap_no_close);
	ATF_TP_ADD_TC(tp, kcov_enable);
	ATF_TP_ADD_TC(tp, kcov_enable_no_disable);
	ATF_TP_ADD_TC(tp, kcov_enable_no_disable_no_close);
	ATF_TP_ADD_TC(tp, kcov_mmap_enable_thread_close);
	ATF_TP_ADD_TC(tp, kcov_basic_pc);
	ATF_TP_ADD_TC(tp, kcov_basic_cmp);
	ATF_TP_ADD_TC(tp, kcov_basic_dup2_pc);
	ATF_TP_ADD_TC(tp, kcov_basic_dup2_cmp);
	ATF_TP_ADD_TC(tp, kcov_multienable_on_the_same_thread);
	ATF_TP_ADD_TC(tp, kcov_buffer_access_from_custom_thread);
	ATF_TP_ADD_TC(tp, kcov_thread);
	ATF_TP_ADD_TC(tp, kcov_multiple_threads2);
	ATF_TP_ADD_TC(tp, kcov_multiple_threads4);
	ATF_TP_ADD_TC(tp, kcov_multiple_threads8);
	ATF_TP_ADD_TC(tp, kcov_multiple_threads16);
	ATF_TP_ADD_TC(tp, kcov_multiple_threads32);
	return atf_no_error();
}