Fri Nov 15 09:44:44 2019 UTC ()
Make kMSan compatible with KCOV. With kMSan we are forced to stay with the
fsanitize flag on subr_kcov.c, which means that kMSan will instrument KCOV.
We add a bunch of __nomsan attributes to reduce this instrumentation, but
it does not remove it completely. That's fine.


(maxv)
diff -r1.295 -r1.296 src/share/mk/bsd.sys.mk
diff -r1.8 -r1.9 src/sys/kern/subr_kcov.c

cvs diff -r1.295 -r1.296 src/share/mk/bsd.sys.mk (expand / switch to unified diff)

--- src/share/mk/bsd.sys.mk 2019/11/05 20:19:17 1.295
+++ src/share/mk/bsd.sys.mk 2019/11/15 09:44:44 1.296
@@ -1,14 +1,14 @@ @@ -1,14 +1,14 @@
1# $NetBSD: bsd.sys.mk,v 1.295 2019/11/05 20:19:17 maxv Exp $ 1# $NetBSD: bsd.sys.mk,v 1.296 2019/11/15 09:44:44 maxv Exp $
2# 2#
3# Build definitions used for NetBSD source tree builds. 3# Build definitions used for NetBSD source tree builds.
4 4
5.if !defined(_BSD_SYS_MK_) 5.if !defined(_BSD_SYS_MK_)
6_BSD_SYS_MK_=1 6_BSD_SYS_MK_=1
7 7
8.if !empty(.INCLUDEDFROMFILE:MMakefile*) 8.if !empty(.INCLUDEDFROMFILE:MMakefile*)
9error1: 9error1:
10 @(echo "bsd.sys.mk should not be included from Makefiles" >& 2; exit 1) 10 @(echo "bsd.sys.mk should not be included from Makefiles" >& 2; exit 1)
11.endif 11.endif
12.if !defined(_BSD_OWN_MK_) 12.if !defined(_BSD_OWN_MK_)
13error2: 13error2:
14 @(echo "bsd.own.mk must be included before bsd.sys.mk" >& 2; exit 1) 14 @(echo "bsd.own.mk must be included before bsd.sys.mk" >& 2; exit 1)
@@ -237,27 +237,27 @@ CFLAGS+= ${CPUFLAGS} @@ -237,27 +237,27 @@ CFLAGS+= ${CPUFLAGS}
237AFLAGS+= ${CPUFLAGS} 237AFLAGS+= ${CPUFLAGS}
238 238
239.if ${KLEAK:U0} > 0 239.if ${KLEAK:U0} > 0
240KLEAKFLAGS= -fsanitize-coverage=trace-pc 240KLEAKFLAGS= -fsanitize-coverage=trace-pc
241.for f in subr_kleak.c 241.for f in subr_kleak.c
242KLEAKFLAGS.${f}= # empty 242KLEAKFLAGS.${f}= # empty
243.endfor 243.endfor
244CFLAGS+= ${KLEAKFLAGS.${.IMPSRC:T}:U${KLEAKFLAGS}} 244CFLAGS+= ${KLEAKFLAGS.${.IMPSRC:T}:U${KLEAKFLAGS}}
245.endif 245.endif
246 246
247.if ${KCOV:U0} > 0 247.if ${KCOV:U0} > 0
248KCOVFLAGS= -fsanitize-coverage=trace-pc 248KCOVFLAGS= -fsanitize-coverage=trace-pc
249.for f in subr_kcov.c subr_lwp_specificdata.c subr_specificdata.c subr_asan.c \ 249.for f in subr_kcov.c subr_lwp_specificdata.c subr_specificdata.c subr_asan.c \
250 subr_csan.c 250 subr_csan.c subr_msan.c
251KCOVFLAGS.${f}= # empty 251KCOVFLAGS.${f}= # empty
252.endfor 252.endfor
253CFLAGS+= ${KCOVFLAGS.${.IMPSRC:T}:U${KCOVFLAGS}} 253CFLAGS+= ${KCOVFLAGS.${.IMPSRC:T}:U${KCOVFLAGS}}
254.endif 254.endif
255 255
256.if !defined(NOPIE) && (!defined(LDSTATIC) || ${LDSTATIC} != "-static") 256.if !defined(NOPIE) && (!defined(LDSTATIC) || ${LDSTATIC} != "-static")
257# Position Independent Executable flags 257# Position Independent Executable flags
258PIE_CFLAGS?= -fPIE 258PIE_CFLAGS?= -fPIE
259PIE_LDFLAGS?= -pie ${${ACTIVE_CC} == "gcc":? -shared-libgcc :} 259PIE_LDFLAGS?= -pie ${${ACTIVE_CC} == "gcc":? -shared-libgcc :}
260PIE_AFLAGS?= -fPIE 260PIE_AFLAGS?= -fPIE
261.endif 261.endif
262 262
263ARM_ELF2AOUT?= elf2aout 263ARM_ELF2AOUT?= elf2aout

cvs diff -r1.8 -r1.9 src/sys/kern/subr_kcov.c (expand / switch to unified diff)

--- src/sys/kern/subr_kcov.c 2019/05/26 05:41:45 1.8
+++ src/sys/kern/subr_kcov.c 2019/11/15 09:44:44 1.9
@@ -1,14 +1,14 @@ @@ -1,14 +1,14 @@
1/* $NetBSD: subr_kcov.c,v 1.8 2019/05/26 05:41:45 kamil Exp $ */ 1/* $NetBSD: subr_kcov.c,v 1.9 2019/11/15 09:44:44 maxv Exp $ */
2 2
3/* 3/*
4 * Copyright (c) 2019 The NetBSD Foundation, Inc. 4 * Copyright (c) 2019 The NetBSD Foundation, Inc.
5 * All rights reserved. 5 * All rights reserved.
6 * 6 *
7 * This code is derived from software contributed to The NetBSD Foundation 7 * This code is derived from software contributed to The NetBSD Foundation
8 * by Siddharth Muralee. 8 * by Siddharth Muralee.
9 * 9 *
10 * Redistribution and use in source and binary forms, with or without 10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions 11 * modification, are permitted provided that the following conditions
12 * are met: 12 * are met:
13 * 1. Redistributions of source code must retain the above copyright 13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer. 14 * notice, this list of conditions and the following disclaimer.
@@ -329,35 +329,35 @@ kcov_fops_mmap(file_t *fp, off_t *offp,  @@ -329,35 +329,35 @@ kcov_fops_mmap(file_t *fp, off_t *offp,
329 } 329 }
330 330
331 uao_reference(kd->uobj); 331 uao_reference(kd->uobj);
332 332
333 *uobjp = kd->uobj; 333 *uobjp = kd->uobj;
334 *maxprotp = prot; 334 *maxprotp = prot;
335 *advicep = UVM_ADV_RANDOM; 335 *advicep = UVM_ADV_RANDOM;
336 336
337out: 337out:
338 kcov_unlock(kd); 338 kcov_unlock(kd);
339 return error; 339 return error;
340} 340}
341 341
342static inline bool 342static inline bool __nomsan
343in_interrupt(void) 343in_interrupt(void)
344{ 344{
345 return curcpu()->ci_idepth >= 0; 345 return curcpu()->ci_idepth >= 0;
346} 346}
347 347
348void __sanitizer_cov_trace_pc(void); 348void __sanitizer_cov_trace_pc(void);
349 349
350void 350void __nomsan
351__sanitizer_cov_trace_pc(void) 351__sanitizer_cov_trace_pc(void)
352{ 352{
353 extern int cold; 353 extern int cold;
354 uint64_t idx; 354 uint64_t idx;
355 kcov_t *kd; 355 kcov_t *kd;
356 356
357 if (__predict_false(cold)) { 357 if (__predict_false(cold)) {
358 /* Do not trace during boot. */ 358 /* Do not trace during boot. */
359 return; 359 return;
360 } 360 }
361 361
362 if (in_interrupt()) { 362 if (in_interrupt()) {
363 /* Do not trace in interrupts. */ 363 /* Do not trace in interrupts. */
@@ -378,27 +378,27 @@ __sanitizer_cov_trace_pc(void) @@ -378,27 +378,27 @@ __sanitizer_cov_trace_pc(void)
378 if (kd->mode != KCOV_MODE_TRACE_PC) { 378 if (kd->mode != KCOV_MODE_TRACE_PC) {
379 /* PC tracing mode not enabled */ 379 /* PC tracing mode not enabled */
380 return; 380 return;
381 } 381 }
382 382
383 idx = kd->buf[0]; 383 idx = kd->buf[0];
384 if (idx < kd->bufnent) { 384 if (idx < kd->bufnent) {
385 kd->buf[idx+1] = 385 kd->buf[idx+1] =
386 (intptr_t)__builtin_return_address(0); 386 (intptr_t)__builtin_return_address(0);
387 kd->buf[0] = idx + 1; 387 kd->buf[0] = idx + 1;
388 } 388 }
389} 389}
390 390
391static void 391static void __nomsan
392trace_cmp(uint64_t type, uint64_t arg1, uint64_t arg2, intptr_t pc) 392trace_cmp(uint64_t type, uint64_t arg1, uint64_t arg2, intptr_t pc)
393{ 393{
394 extern int cold; 394 extern int cold;
395 uint64_t idx; 395 uint64_t idx;
396 kcov_t *kd; 396 kcov_t *kd;
397 397
398 if (__predict_false(cold)) { 398 if (__predict_false(cold)) {
399 /* Do not trace during boot. */ 399 /* Do not trace during boot. */
400 return; 400 return;
401 } 401 }
402 402
403 if (in_interrupt()) { 403 if (in_interrupt()) {
404 /* Do not trace in interrupts. */ 404 /* Do not trace in interrupts. */
@@ -423,107 +423,107 @@ trace_cmp(uint64_t type, uint64_t arg1,  @@ -423,107 +423,107 @@ trace_cmp(uint64_t type, uint64_t arg1,
423 423
424 idx = kd->buf[0]; 424 idx = kd->buf[0];
425 if ((idx * 4 + 4) <= kd->bufnent) { 425 if ((idx * 4 + 4) <= kd->bufnent) {
426 kd->buf[idx * 4 + 1] = type; 426 kd->buf[idx * 4 + 1] = type;
427 kd->buf[idx * 4 + 2] = arg1; 427 kd->buf[idx * 4 + 2] = arg1;
428 kd->buf[idx * 4 + 3] = arg2; 428 kd->buf[idx * 4 + 3] = arg2;
429 kd->buf[idx * 4 + 4] = pc; 429 kd->buf[idx * 4 + 4] = pc;
430 kd->buf[0] = idx + 1; 430 kd->buf[0] = idx + 1;
431 } 431 }
432} 432}
433 433
434void __sanitizer_cov_trace_cmp1(uint8_t arg1, uint8_t arg2); 434void __sanitizer_cov_trace_cmp1(uint8_t arg1, uint8_t arg2);
435 435
436void 436void __nomsan
437__sanitizer_cov_trace_cmp1(uint8_t arg1, uint8_t arg2) 437__sanitizer_cov_trace_cmp1(uint8_t arg1, uint8_t arg2)
438{ 438{
439 439
440 trace_cmp(KCOV_CMP_SIZE(0), arg1, arg2, 440 trace_cmp(KCOV_CMP_SIZE(0), arg1, arg2,
441 (intptr_t)__builtin_return_address(0)); 441 (intptr_t)__builtin_return_address(0));
442} 442}
443 443
444void __sanitizer_cov_trace_cmp2(uint16_t arg1, uint16_t arg2); 444void __sanitizer_cov_trace_cmp2(uint16_t arg1, uint16_t arg2);
445 445
446void 446void __nomsan
447__sanitizer_cov_trace_cmp2(uint16_t arg1, uint16_t arg2) 447__sanitizer_cov_trace_cmp2(uint16_t arg1, uint16_t arg2)
448{ 448{
449 449
450 trace_cmp(KCOV_CMP_SIZE(1), arg1, arg2, 450 trace_cmp(KCOV_CMP_SIZE(1), arg1, arg2,
451 (intptr_t)__builtin_return_address(0)); 451 (intptr_t)__builtin_return_address(0));
452} 452}
453 453
454void __sanitizer_cov_trace_cmp4(uint32_t arg1, uint32_t arg2); 454void __sanitizer_cov_trace_cmp4(uint32_t arg1, uint32_t arg2);
455 455
456void 456void __nomsan
457__sanitizer_cov_trace_cmp4(uint32_t arg1, uint32_t arg2) 457__sanitizer_cov_trace_cmp4(uint32_t arg1, uint32_t arg2)
458{ 458{
459 459
460 trace_cmp(KCOV_CMP_SIZE(2), arg1, arg2, 460 trace_cmp(KCOV_CMP_SIZE(2), arg1, arg2,
461 (intptr_t)__builtin_return_address(0)); 461 (intptr_t)__builtin_return_address(0));
462} 462}
463 463
464void __sanitizer_cov_trace_cmp8(uint64_t arg1, uint64_t arg2); 464void __sanitizer_cov_trace_cmp8(uint64_t arg1, uint64_t arg2);
465 465
466void 466void __nomsan
467__sanitizer_cov_trace_cmp8(uint64_t arg1, uint64_t arg2) 467__sanitizer_cov_trace_cmp8(uint64_t arg1, uint64_t arg2)
468{ 468{
469 469
470 trace_cmp(KCOV_CMP_SIZE(3), arg1, arg2, 470 trace_cmp(KCOV_CMP_SIZE(3), arg1, arg2,
471 (intptr_t)__builtin_return_address(0)); 471 (intptr_t)__builtin_return_address(0));
472} 472}
473 473
474void __sanitizer_cov_trace_const_cmp1(uint8_t arg1, uint8_t arg2); 474void __sanitizer_cov_trace_const_cmp1(uint8_t arg1, uint8_t arg2);
475 475
476void 476void __nomsan
477__sanitizer_cov_trace_const_cmp1(uint8_t arg1, uint8_t arg2) 477__sanitizer_cov_trace_const_cmp1(uint8_t arg1, uint8_t arg2)
478{ 478{
479 479
480 trace_cmp(KCOV_CMP_SIZE(0) | KCOV_CMP_CONST, arg1, arg2, 480 trace_cmp(KCOV_CMP_SIZE(0) | KCOV_CMP_CONST, arg1, arg2,
481 (intptr_t)__builtin_return_address(0)); 481 (intptr_t)__builtin_return_address(0));
482} 482}
483 483
484void __sanitizer_cov_trace_const_cmp2(uint16_t arg1, uint16_t arg2); 484void __sanitizer_cov_trace_const_cmp2(uint16_t arg1, uint16_t arg2);
485 485
486void 486void __nomsan
487__sanitizer_cov_trace_const_cmp2(uint16_t arg1, uint16_t arg2) 487__sanitizer_cov_trace_const_cmp2(uint16_t arg1, uint16_t arg2)
488{ 488{
489 489
490 trace_cmp(KCOV_CMP_SIZE(1) | KCOV_CMP_CONST, arg1, arg2, 490 trace_cmp(KCOV_CMP_SIZE(1) | KCOV_CMP_CONST, arg1, arg2,
491 (intptr_t)__builtin_return_address(0)); 491 (intptr_t)__builtin_return_address(0));
492} 492}
493 493
494void __sanitizer_cov_trace_const_cmp4(uint32_t arg1, uint32_t arg2); 494void __sanitizer_cov_trace_const_cmp4(uint32_t arg1, uint32_t arg2);
495 495
496void 496void __nomsan
497__sanitizer_cov_trace_const_cmp4(uint32_t arg1, uint32_t arg2) 497__sanitizer_cov_trace_const_cmp4(uint32_t arg1, uint32_t arg2)
498{ 498{
499 499
500 trace_cmp(KCOV_CMP_SIZE(2) | KCOV_CMP_CONST, arg1, arg2, 500 trace_cmp(KCOV_CMP_SIZE(2) | KCOV_CMP_CONST, arg1, arg2,
501 (intptr_t)__builtin_return_address(0)); 501 (intptr_t)__builtin_return_address(0));
502} 502}
503 503
504void __sanitizer_cov_trace_const_cmp8(uint64_t arg1, uint64_t arg2); 504void __sanitizer_cov_trace_const_cmp8(uint64_t arg1, uint64_t arg2);
505 505
506void 506void __nomsan
507__sanitizer_cov_trace_const_cmp8(uint64_t arg1, uint64_t arg2) 507__sanitizer_cov_trace_const_cmp8(uint64_t arg1, uint64_t arg2)
508{ 508{
509 509
510 trace_cmp(KCOV_CMP_SIZE(3) | KCOV_CMP_CONST, arg1, arg2, 510 trace_cmp(KCOV_CMP_SIZE(3) | KCOV_CMP_CONST, arg1, arg2,
511 (intptr_t)__builtin_return_address(0)); 511 (intptr_t)__builtin_return_address(0));
512} 512}
513 513
514void __sanitizer_cov_trace_switch(uint64_t val, uint64_t *cases); 514void __sanitizer_cov_trace_switch(uint64_t val, uint64_t *cases);
515 515
516void 516void __nomsan
517__sanitizer_cov_trace_switch(uint64_t val, uint64_t *cases) 517__sanitizer_cov_trace_switch(uint64_t val, uint64_t *cases)
518{ 518{
519 uint64_t i, nbits, ncases, type; 519 uint64_t i, nbits, ncases, type;
520 intptr_t pc; 520 intptr_t pc;
521 521
522 pc = (intptr_t)__builtin_return_address(0); 522 pc = (intptr_t)__builtin_return_address(0);
523 ncases = cases[0]; 523 ncases = cases[0];
524 nbits = cases[1]; 524 nbits = cases[1];
525 525
526 switch (nbits) { 526 switch (nbits) {
527 case 8: 527 case 8:
528 type = KCOV_CMP_SIZE(0); 528 type = KCOV_CMP_SIZE(0);
529 break; 529 break;