Extract the coverage sanitizer KPI to a new file.
This will allow multiple consumers of the coverage data to be compiled into the kernel together. The only requirement is only one can be registered at a given point in time, however it is expected they will only register when the coverage data is needed. A new kernel conflig option COVERAGE is added. This will allow kcov to become a module that can be loaded as needed, or compiled into the kernel. While here clean up the #include style a little. Reviewed by: kib Sponsored by: DARPA, AFRL Differential Revision: https://reviews.freebsd.org/D18955
This commit is contained in:
parent
a56136a1ba
commit
524553f56d
Notes:
svn2git
2020-12-20 02:59:44 +00:00
svn path=/head/; revision=343550
@ -100,9 +100,12 @@ options WITNESS # Enable checks to detect deadlocks and cycles
|
|||||||
options WITNESS_SKIPSPIN # Don't run witness on spinlocks for speed
|
options WITNESS_SKIPSPIN # Don't run witness on spinlocks for speed
|
||||||
options MALLOC_DEBUG_MAXZONES=8 # Separate malloc(9) zones
|
options MALLOC_DEBUG_MAXZONES=8 # Separate malloc(9) zones
|
||||||
options VERBOSE_SYSINIT=0 # Support debug.verbose_sysinit, off by default
|
options VERBOSE_SYSINIT=0 # Support debug.verbose_sysinit, off by default
|
||||||
|
|
||||||
|
# Kernel Sanitizers
|
||||||
|
#options COVERAGE # Generic kernel coverage. Used by KCOV
|
||||||
|
#options KCOV # Kernel Coverage Sanitizer
|
||||||
# Warning: KUBSAN can result in a kernel too large for loader to load
|
# Warning: KUBSAN can result in a kernel too large for loader to load
|
||||||
#options KUBSAN # Kernel Undefined Behavior Sanitizer
|
#options KUBSAN # Kernel Undefined Behavior Sanitizer
|
||||||
#options KCOV # Kernel Coverage Sanitizer
|
|
||||||
|
|
||||||
# Kernel dump features.
|
# Kernel dump features.
|
||||||
options EKCD # Support for encrypted kernel dumps
|
options EKCD # Support for encrypted kernel dumps
|
||||||
|
@ -37,4 +37,5 @@ nooptions WITNESS_SKIPSPIN
|
|||||||
nooptions BUF_TRACKING
|
nooptions BUF_TRACKING
|
||||||
nooptions DEADLKRES
|
nooptions DEADLKRES
|
||||||
nooptions FULL_BUF_TRACKING
|
nooptions FULL_BUF_TRACKING
|
||||||
|
nooptions COVERAGE
|
||||||
|
nooptions KCOV
|
||||||
|
@ -92,9 +92,12 @@ options MALLOC_DEBUG_MAXZONES=8 # Separate malloc(9) zones
|
|||||||
options ALT_BREAK_TO_DEBUGGER # Enter debugger on keyboard escape sequence
|
options ALT_BREAK_TO_DEBUGGER # Enter debugger on keyboard escape sequence
|
||||||
options USB_DEBUG # enable debug msgs
|
options USB_DEBUG # enable debug msgs
|
||||||
options VERBOSE_SYSINIT=0 # Support debug.verbose_sysinit, off by default
|
options VERBOSE_SYSINIT=0 # Support debug.verbose_sysinit, off by default
|
||||||
|
|
||||||
|
# Kernel Sanitizers
|
||||||
|
#options COVERAGE # Generic kernel coverage. Used by KCOV
|
||||||
|
#options KCOV # Kernel Coverage Sanitizer
|
||||||
# Warning: KUBSAN can result in a kernel too large for loader to load
|
# Warning: KUBSAN can result in a kernel too large for loader to load
|
||||||
#options KUBSAN # Kernel Undefined Behavior Sanitizer
|
#options KUBSAN # Kernel Undefined Behavior Sanitizer
|
||||||
#options KCOV # Kernel Coverage Sanitizer
|
|
||||||
|
|
||||||
# Kernel dump features.
|
# Kernel dump features.
|
||||||
options EKCD # Support for encrypted kernel dumps
|
options EKCD # Support for encrypted kernel dumps
|
||||||
|
@ -36,3 +36,5 @@ nooptions WITNESS
|
|||||||
nooptions WITNESS_SKIPSPIN
|
nooptions WITNESS_SKIPSPIN
|
||||||
nooptions DEADLKRES
|
nooptions DEADLKRES
|
||||||
nooptions USB_DEBUG
|
nooptions USB_DEBUG
|
||||||
|
nooptions COVERAGE
|
||||||
|
nooptions KCOV
|
||||||
|
@ -3883,6 +3883,8 @@ kern/subr_capability.c standard
|
|||||||
kern/subr_clock.c standard
|
kern/subr_clock.c standard
|
||||||
kern/subr_compressor.c standard \
|
kern/subr_compressor.c standard \
|
||||||
compile-with "${NORMAL_C} -I$S/contrib/zstd/lib/freebsd"
|
compile-with "${NORMAL_C} -I$S/contrib/zstd/lib/freebsd"
|
||||||
|
kern/subr_coverage.c optional coverage \
|
||||||
|
compile-with "${NORMAL_C} -fno-sanitize=all"
|
||||||
kern/subr_counter.c standard
|
kern/subr_counter.c standard
|
||||||
kern/subr_devstat.c standard
|
kern/subr_devstat.c standard
|
||||||
kern/subr_disk.c standard
|
kern/subr_disk.c standard
|
||||||
|
@ -118,8 +118,8 @@ KUBSAN_ENABLED!= grep KUBSAN opt_global.h || true ; echo
|
|||||||
SAN_CFLAGS+= -fsanitize=undefined
|
SAN_CFLAGS+= -fsanitize=undefined
|
||||||
.endif
|
.endif
|
||||||
|
|
||||||
KCOV_ENABLED!= grep KCOV opt_kcov.h || true ; echo
|
COVERAGE_ENABLED!= grep COVERAGE opt_global.h || true ; echo
|
||||||
.if !empty(KCOV_ENABLED)
|
.if !empty(COVERAGE_ENABLED)
|
||||||
SAN_CFLAGS+= -fsanitize-coverage=trace-pc,trace-cmp
|
SAN_CFLAGS+= -fsanitize-coverage=trace-pc,trace-cmp
|
||||||
.endif
|
.endif
|
||||||
|
|
||||||
|
@ -57,7 +57,6 @@ DDB_CTF opt_ddb.h
|
|||||||
DDB_NUMSYM opt_ddb.h
|
DDB_NUMSYM opt_ddb.h
|
||||||
FULL_BUF_TRACKING opt_global.h
|
FULL_BUF_TRACKING opt_global.h
|
||||||
GDB
|
GDB
|
||||||
KCOV opt_kcov.h
|
|
||||||
KDB opt_global.h
|
KDB opt_global.h
|
||||||
KDB_TRACE opt_kdb.h
|
KDB_TRACE opt_kdb.h
|
||||||
KDB_UNATTENDED opt_kdb.h
|
KDB_UNATTENDED opt_kdb.h
|
||||||
@ -234,6 +233,8 @@ VERBOSE_SYSINIT
|
|||||||
ZSTDIO opt_zstdio.h
|
ZSTDIO opt_zstdio.h
|
||||||
|
|
||||||
# Sanitizers
|
# Sanitizers
|
||||||
|
COVERAGE opt_global.h
|
||||||
|
KCOV
|
||||||
KUBSAN opt_global.h
|
KUBSAN opt_global.h
|
||||||
|
|
||||||
# POSIX kernel options
|
# POSIX kernel options
|
||||||
|
@ -39,29 +39,26 @@
|
|||||||
__FBSDID("$FreeBSD$");
|
__FBSDID("$FreeBSD$");
|
||||||
|
|
||||||
#include <sys/param.h>
|
#include <sys/param.h>
|
||||||
|
#include <sys/systm.h>
|
||||||
#include <sys/conf.h>
|
#include <sys/conf.h>
|
||||||
#include <sys/file.h>
|
|
||||||
#include <sys/kcov.h>
|
#include <sys/kcov.h>
|
||||||
#include <sys/kernel.h>
|
#include <sys/kernel.h>
|
||||||
|
#include <sys/limits.h>
|
||||||
#include <sys/lock.h>
|
#include <sys/lock.h>
|
||||||
#include <sys/malloc.h>
|
#include <sys/malloc.h>
|
||||||
#include <sys/mman.h>
|
#include <sys/mman.h>
|
||||||
#include <sys/mutex.h>
|
#include <sys/mutex.h>
|
||||||
#include <sys/proc.h>
|
#include <sys/proc.h>
|
||||||
#include <sys/rwlock.h>
|
#include <sys/rwlock.h>
|
||||||
#include <sys/stat.h>
|
|
||||||
#include <sys/sysctl.h>
|
#include <sys/sysctl.h>
|
||||||
#include <sys/systm.h>
|
|
||||||
#include <sys/types.h>
|
|
||||||
|
|
||||||
#include <vm/vm.h>
|
#include <vm/vm.h>
|
||||||
|
#include <vm/pmap.h>
|
||||||
#include <vm/vm_extern.h>
|
#include <vm/vm_extern.h>
|
||||||
#include <vm/vm_object.h>
|
#include <vm/vm_object.h>
|
||||||
#include <vm/vm_page.h>
|
#include <vm/vm_page.h>
|
||||||
#include <vm/vm_pager.h>
|
#include <vm/vm_pager.h>
|
||||||
|
|
||||||
#include <vm/pmap.h>
|
|
||||||
|
|
||||||
MALLOC_DEFINE(M_KCOV_INFO, "kcovinfo", "KCOV info type");
|
MALLOC_DEFINE(M_KCOV_INFO, "kcovinfo", "KCOV info type");
|
||||||
|
|
||||||
#define KCOV_ELEMENT_SIZE sizeof(uint64_t)
|
#define KCOV_ELEMENT_SIZE sizeof(uint64_t)
|
||||||
@ -138,17 +135,6 @@ static d_close_t kcov_close;
|
|||||||
static d_mmap_single_t kcov_mmap_single;
|
static d_mmap_single_t kcov_mmap_single;
|
||||||
static d_ioctl_t kcov_ioctl;
|
static d_ioctl_t kcov_ioctl;
|
||||||
|
|
||||||
void __sanitizer_cov_trace_pc(void);
|
|
||||||
void __sanitizer_cov_trace_cmp1(uint8_t, uint8_t);
|
|
||||||
void __sanitizer_cov_trace_cmp2(uint16_t, uint16_t);
|
|
||||||
void __sanitizer_cov_trace_cmp4(uint32_t, uint32_t);
|
|
||||||
void __sanitizer_cov_trace_cmp8(uint64_t, uint64_t);
|
|
||||||
void __sanitizer_cov_trace_const_cmp1(uint8_t, uint8_t);
|
|
||||||
void __sanitizer_cov_trace_const_cmp2(uint16_t, uint16_t);
|
|
||||||
void __sanitizer_cov_trace_const_cmp4(uint32_t, uint32_t);
|
|
||||||
void __sanitizer_cov_trace_const_cmp8(uint64_t, uint64_t);
|
|
||||||
void __sanitizer_cov_trace_switch(uint64_t, uint64_t *);
|
|
||||||
|
|
||||||
static int kcov_alloc(struct kcov_info *info, size_t entries);
|
static int kcov_alloc(struct kcov_info *info, size_t entries);
|
||||||
static void kcov_init(const void *unused);
|
static void kcov_init(const void *unused);
|
||||||
|
|
||||||
@ -169,6 +155,7 @@ SYSCTL_UINT(_kern_kcov, OID_AUTO, max_entries, CTLFLAG_RW,
|
|||||||
"Maximum number of entries in the kcov buffer");
|
"Maximum number of entries in the kcov buffer");
|
||||||
|
|
||||||
static struct mtx kcov_lock;
|
static struct mtx kcov_lock;
|
||||||
|
static int active_count;
|
||||||
|
|
||||||
static struct kcov_info *
|
static struct kcov_info *
|
||||||
get_kinfo(struct thread *td)
|
get_kinfo(struct thread *td)
|
||||||
@ -197,25 +184,13 @@ get_kinfo(struct thread *td)
|
|||||||
return (info);
|
return (info);
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
static void
|
||||||
* Main entry point. A call to this function will be inserted
|
trace_pc(uintptr_t ret)
|
||||||
* at every edge, and if coverage is enabled for the thread
|
|
||||||
* this function will add the PC to the buffer.
|
|
||||||
*/
|
|
||||||
void
|
|
||||||
__sanitizer_cov_trace_pc(void)
|
|
||||||
{
|
{
|
||||||
struct thread *td;
|
struct thread *td;
|
||||||
struct kcov_info *info;
|
struct kcov_info *info;
|
||||||
uint64_t *buf, index;
|
uint64_t *buf, index;
|
||||||
|
|
||||||
/*
|
|
||||||
* To guarantee curthread is properly set, we exit early
|
|
||||||
* until the driver has been initialized
|
|
||||||
*/
|
|
||||||
if (cold)
|
|
||||||
return;
|
|
||||||
|
|
||||||
td = curthread;
|
td = curthread;
|
||||||
info = get_kinfo(td);
|
info = get_kinfo(td);
|
||||||
if (info == NULL)
|
if (info == NULL)
|
||||||
@ -237,7 +212,7 @@ __sanitizer_cov_trace_pc(void)
|
|||||||
if (index + 2 > info->entries)
|
if (index + 2 > info->entries)
|
||||||
return;
|
return;
|
||||||
|
|
||||||
buf[index + 1] = (uint64_t)__builtin_return_address(0);
|
buf[index + 1] = ret;
|
||||||
buf[0] = index + 1;
|
buf[0] = index + 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -248,13 +223,6 @@ trace_cmp(uint64_t type, uint64_t arg1, uint64_t arg2, uint64_t ret)
|
|||||||
struct kcov_info *info;
|
struct kcov_info *info;
|
||||||
uint64_t *buf, index;
|
uint64_t *buf, index;
|
||||||
|
|
||||||
/*
|
|
||||||
* To guarantee curthread is properly set, we exit early
|
|
||||||
* until the driver has been initialized
|
|
||||||
*/
|
|
||||||
if (cold)
|
|
||||||
return (false);
|
|
||||||
|
|
||||||
td = curthread;
|
td = curthread;
|
||||||
info = get_kinfo(td);
|
info = get_kinfo(td);
|
||||||
if (info == NULL)
|
if (info == NULL)
|
||||||
@ -287,108 +255,6 @@ trace_cmp(uint64_t type, uint64_t arg1, uint64_t arg2, uint64_t ret)
|
|||||||
return (true);
|
return (true);
|
||||||
}
|
}
|
||||||
|
|
||||||
void
|
|
||||||
__sanitizer_cov_trace_cmp1(uint8_t arg1, uint8_t arg2)
|
|
||||||
{
|
|
||||||
|
|
||||||
trace_cmp(KCOV_CMP_SIZE(0), arg1, arg2,
|
|
||||||
(uint64_t)__builtin_return_address(0));
|
|
||||||
}
|
|
||||||
|
|
||||||
void
|
|
||||||
__sanitizer_cov_trace_cmp2(uint16_t arg1, uint16_t arg2)
|
|
||||||
{
|
|
||||||
|
|
||||||
trace_cmp(KCOV_CMP_SIZE(1), arg1, arg2,
|
|
||||||
(uint64_t)__builtin_return_address(0));
|
|
||||||
}
|
|
||||||
|
|
||||||
void
|
|
||||||
__sanitizer_cov_trace_cmp4(uint32_t arg1, uint32_t arg2)
|
|
||||||
{
|
|
||||||
|
|
||||||
trace_cmp(KCOV_CMP_SIZE(2), arg1, arg2,
|
|
||||||
(uint64_t)__builtin_return_address(0));
|
|
||||||
}
|
|
||||||
|
|
||||||
void
|
|
||||||
__sanitizer_cov_trace_cmp8(uint64_t arg1, uint64_t arg2)
|
|
||||||
{
|
|
||||||
|
|
||||||
trace_cmp(KCOV_CMP_SIZE(3), arg1, arg2,
|
|
||||||
(uint64_t)__builtin_return_address(0));
|
|
||||||
}
|
|
||||||
|
|
||||||
void
|
|
||||||
__sanitizer_cov_trace_const_cmp1(uint8_t arg1, uint8_t arg2)
|
|
||||||
{
|
|
||||||
|
|
||||||
trace_cmp(KCOV_CMP_SIZE(0) | KCOV_CMP_CONST, arg1, arg2,
|
|
||||||
(uint64_t)__builtin_return_address(0));
|
|
||||||
}
|
|
||||||
|
|
||||||
void
|
|
||||||
__sanitizer_cov_trace_const_cmp2(uint16_t arg1, uint16_t arg2)
|
|
||||||
{
|
|
||||||
|
|
||||||
trace_cmp(KCOV_CMP_SIZE(1) | KCOV_CMP_CONST, arg1, arg2,
|
|
||||||
(uint64_t)__builtin_return_address(0));
|
|
||||||
}
|
|
||||||
|
|
||||||
void
|
|
||||||
__sanitizer_cov_trace_const_cmp4(uint32_t arg1, uint32_t arg2)
|
|
||||||
{
|
|
||||||
|
|
||||||
trace_cmp(KCOV_CMP_SIZE(2) | KCOV_CMP_CONST, arg1, arg2,
|
|
||||||
(uint64_t)__builtin_return_address(0));
|
|
||||||
}
|
|
||||||
|
|
||||||
void
|
|
||||||
__sanitizer_cov_trace_const_cmp8(uint64_t arg1, uint64_t arg2)
|
|
||||||
{
|
|
||||||
|
|
||||||
trace_cmp(KCOV_CMP_SIZE(3) | KCOV_CMP_CONST, arg1, arg2,
|
|
||||||
(uint64_t)__builtin_return_address(0));
|
|
||||||
}
|
|
||||||
|
|
||||||
/*
|
|
||||||
* val is the switch operand
|
|
||||||
* cases[0] is the number of case constants
|
|
||||||
* cases[1] is the size of val in bits
|
|
||||||
* cases[2..n] are the case constants
|
|
||||||
*/
|
|
||||||
void
|
|
||||||
__sanitizer_cov_trace_switch(uint64_t val, uint64_t *cases)
|
|
||||||
{
|
|
||||||
uint64_t i, count, ret, type;
|
|
||||||
|
|
||||||
count = cases[0];
|
|
||||||
ret = (uint64_t)__builtin_return_address(0);
|
|
||||||
|
|
||||||
switch (cases[1]) {
|
|
||||||
case 8:
|
|
||||||
type = KCOV_CMP_SIZE(0);
|
|
||||||
break;
|
|
||||||
case 16:
|
|
||||||
type = KCOV_CMP_SIZE(1);
|
|
||||||
break;
|
|
||||||
case 32:
|
|
||||||
type = KCOV_CMP_SIZE(2);
|
|
||||||
break;
|
|
||||||
case 64:
|
|
||||||
type = KCOV_CMP_SIZE(3);
|
|
||||||
break;
|
|
||||||
default:
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
val |= KCOV_CMP_CONST;
|
|
||||||
|
|
||||||
for (i = 0; i < count; i++)
|
|
||||||
if (!trace_cmp(type, val, cases[i + 2], ret))
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* The fd is being closed, cleanup everything we can.
|
* The fd is being closed, cleanup everything we can.
|
||||||
*/
|
*/
|
||||||
@ -456,6 +322,7 @@ kcov_close(struct cdev *dev, int fflag, int devtype, struct thread *td)
|
|||||||
struct kcov_info *info;
|
struct kcov_info *info;
|
||||||
int error;
|
int error;
|
||||||
|
|
||||||
|
|
||||||
if ((error = devfs_get_cdevpriv((void **)&info)) != 0)
|
if ((error = devfs_get_cdevpriv((void **)&info)) != 0)
|
||||||
return (error);
|
return (error);
|
||||||
|
|
||||||
@ -571,6 +438,16 @@ kcov_ioctl(struct cdev *dev, u_long cmd, caddr_t data, int fflag __unused,
|
|||||||
error = EINVAL;
|
error = EINVAL;
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/* Lets hope nobody opens this 2 billion times */
|
||||||
|
KASSERT(active_count < INT_MAX,
|
||||||
|
("%s: Open too many times", __func__));
|
||||||
|
active_count++;
|
||||||
|
if (active_count == 1) {
|
||||||
|
cov_register_pc(&trace_pc);
|
||||||
|
cov_register_cmp(&trace_cmp);
|
||||||
|
}
|
||||||
|
|
||||||
KASSERT(info->thread == NULL,
|
KASSERT(info->thread == NULL,
|
||||||
("Enabling kcov when already enabled"));
|
("Enabling kcov when already enabled"));
|
||||||
info->thread = td;
|
info->thread = td;
|
||||||
@ -589,6 +466,13 @@ kcov_ioctl(struct cdev *dev, u_long cmd, caddr_t data, int fflag __unused,
|
|||||||
error = EINVAL;
|
error = EINVAL;
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
KASSERT(active_count > 0, ("%s: Open count is zero", __func__));
|
||||||
|
active_count--;
|
||||||
|
if (active_count == 0) {
|
||||||
|
cov_register_pc(&trace_pc);
|
||||||
|
cov_register_cmp(&trace_cmp);
|
||||||
|
}
|
||||||
|
|
||||||
td->td_kcov_info = NULL;
|
td->td_kcov_info = NULL;
|
||||||
atomic_store_int(&info->state, KCOV_STATE_READY);
|
atomic_store_int(&info->state, KCOV_STATE_READY);
|
||||||
/*
|
/*
|
||||||
@ -618,6 +502,12 @@ kcov_thread_dtor(void *arg __unused, struct thread *td)
|
|||||||
return;
|
return;
|
||||||
|
|
||||||
mtx_lock_spin(&kcov_lock);
|
mtx_lock_spin(&kcov_lock);
|
||||||
|
KASSERT(active_count > 0, ("%s: Open count is zero", __func__));
|
||||||
|
active_count--;
|
||||||
|
if (active_count == 0) {
|
||||||
|
cov_register_pc(&trace_pc);
|
||||||
|
cov_register_cmp(&trace_cmp);
|
||||||
|
}
|
||||||
td->td_kcov_info = NULL;
|
td->td_kcov_info = NULL;
|
||||||
if (info->state != KCOV_STATE_DYING) {
|
if (info->state != KCOV_STATE_DYING) {
|
||||||
/*
|
/*
|
||||||
@ -673,4 +563,4 @@ kcov_init(const void *unused)
|
|||||||
EVENTHANDLER_PRI_ANY);
|
EVENTHANDLER_PRI_ANY);
|
||||||
}
|
}
|
||||||
|
|
||||||
SYSINIT(kcovdev, SI_SUB_DEVFS, SI_ORDER_ANY, kcov_init, NULL);
|
SYSINIT(kcovdev, SI_SUB_LAST, SI_ORDER_ANY, kcov_init, NULL);
|
||||||
|
237
sys/kern/subr_coverage.c
Normal file
237
sys/kern/subr_coverage.c
Normal file
@ -0,0 +1,237 @@
|
|||||||
|
/*-
|
||||||
|
* SPDX-License-Identifier: BSD-2-Clause-FreeBSD
|
||||||
|
*
|
||||||
|
* Copyright (C) 2018 The FreeBSD Foundation. All rights reserved.
|
||||||
|
* Copyright (C) 2018, 2019 Andrew Turner
|
||||||
|
*
|
||||||
|
* This software was developed by Mitchell Horne under sponsorship of
|
||||||
|
* the FreeBSD Foundation.
|
||||||
|
*
|
||||||
|
* 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.
|
||||||
|
*
|
||||||
|
* $FreeBSD$
|
||||||
|
*/
|
||||||
|
|
||||||
|
#include <sys/cdefs.h>
|
||||||
|
__FBSDID("$FreeBSD$");
|
||||||
|
|
||||||
|
#include <sys/param.h>
|
||||||
|
#include <sys/coverage.h>
|
||||||
|
|
||||||
|
#include <machine/atomic.h>
|
||||||
|
|
||||||
|
void __sanitizer_cov_trace_pc(void);
|
||||||
|
void __sanitizer_cov_trace_cmp1(uint8_t, uint8_t);
|
||||||
|
void __sanitizer_cov_trace_cmp2(uint16_t, uint16_t);
|
||||||
|
void __sanitizer_cov_trace_cmp4(uint32_t, uint32_t);
|
||||||
|
void __sanitizer_cov_trace_cmp8(uint64_t, uint64_t);
|
||||||
|
void __sanitizer_cov_trace_const_cmp1(uint8_t, uint8_t);
|
||||||
|
void __sanitizer_cov_trace_const_cmp2(uint16_t, uint16_t);
|
||||||
|
void __sanitizer_cov_trace_const_cmp4(uint32_t, uint32_t);
|
||||||
|
void __sanitizer_cov_trace_const_cmp8(uint64_t, uint64_t);
|
||||||
|
void __sanitizer_cov_trace_switch(uint64_t, uint64_t *);
|
||||||
|
|
||||||
|
static cov_trace_pc_t cov_trace_pc;
|
||||||
|
static cov_trace_cmp_t cov_trace_cmp;
|
||||||
|
|
||||||
|
void
|
||||||
|
cov_register_pc(cov_trace_pc_t trace_pc)
|
||||||
|
{
|
||||||
|
|
||||||
|
atomic_store_ptr(&cov_trace_pc, trace_pc);
|
||||||
|
}
|
||||||
|
|
||||||
|
void
|
||||||
|
cov_unregister_pc(void)
|
||||||
|
{
|
||||||
|
|
||||||
|
atomic_store_ptr(&cov_trace_pc, NULL);
|
||||||
|
}
|
||||||
|
|
||||||
|
void
|
||||||
|
cov_register_cmp(cov_trace_cmp_t trace_cmp)
|
||||||
|
{
|
||||||
|
|
||||||
|
atomic_store_ptr(&cov_trace_cmp, trace_cmp);
|
||||||
|
}
|
||||||
|
|
||||||
|
void
|
||||||
|
cov_unregister_cmp(void)
|
||||||
|
{
|
||||||
|
|
||||||
|
atomic_store_ptr(&cov_trace_cmp, NULL);
|
||||||
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
|
* Main entry point. A call to this function will be inserted
|
||||||
|
* at every edge, and if coverage is enabled for the thread
|
||||||
|
* this function will add the PC to the buffer.
|
||||||
|
*/
|
||||||
|
void
|
||||||
|
__sanitizer_cov_trace_pc(void)
|
||||||
|
{
|
||||||
|
cov_trace_pc_t trace_pc;
|
||||||
|
|
||||||
|
trace_pc = (cov_trace_pc_t)atomic_load_ptr(&cov_trace_pc);
|
||||||
|
if (trace_pc != NULL)
|
||||||
|
trace_pc((uint64_t)__builtin_return_address(0));
|
||||||
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
|
* Comparison entry points. When the kernel performs a comparison
|
||||||
|
* operation the compiler inserts a call to one of the following
|
||||||
|
* functions to record the operation.
|
||||||
|
*/
|
||||||
|
void
|
||||||
|
__sanitizer_cov_trace_cmp1(uint8_t arg1, uint8_t arg2)
|
||||||
|
{
|
||||||
|
cov_trace_cmp_t trace_cmp;
|
||||||
|
|
||||||
|
trace_cmp = (cov_trace_cmp_t)atomic_load_ptr(&cov_trace_cmp);
|
||||||
|
if (trace_cmp != NULL)
|
||||||
|
trace_cmp(COV_CMP_SIZE(0), arg1, arg2,
|
||||||
|
(uint64_t)__builtin_return_address(0));
|
||||||
|
}
|
||||||
|
|
||||||
|
void
|
||||||
|
__sanitizer_cov_trace_cmp2(uint16_t arg1, uint16_t arg2)
|
||||||
|
{
|
||||||
|
cov_trace_cmp_t trace_cmp;
|
||||||
|
|
||||||
|
trace_cmp = (cov_trace_cmp_t)atomic_load_ptr(&cov_trace_cmp);
|
||||||
|
if (trace_cmp != NULL)
|
||||||
|
trace_cmp(COV_CMP_SIZE(1), arg1, arg2,
|
||||||
|
(uint64_t)__builtin_return_address(0));
|
||||||
|
}
|
||||||
|
|
||||||
|
void
|
||||||
|
__sanitizer_cov_trace_cmp4(uint32_t arg1, uint32_t arg2)
|
||||||
|
{
|
||||||
|
cov_trace_cmp_t trace_cmp;
|
||||||
|
|
||||||
|
trace_cmp = (cov_trace_cmp_t)atomic_load_ptr(&cov_trace_cmp);
|
||||||
|
if (trace_cmp != NULL)
|
||||||
|
trace_cmp(COV_CMP_SIZE(2), arg1, arg2,
|
||||||
|
(uint64_t)__builtin_return_address(0));
|
||||||
|
}
|
||||||
|
|
||||||
|
void
|
||||||
|
__sanitizer_cov_trace_cmp8(uint64_t arg1, uint64_t arg2)
|
||||||
|
{
|
||||||
|
cov_trace_cmp_t trace_cmp;
|
||||||
|
|
||||||
|
trace_cmp = (cov_trace_cmp_t)atomic_load_ptr(&cov_trace_cmp);
|
||||||
|
if (trace_cmp != NULL)
|
||||||
|
trace_cmp(COV_CMP_SIZE(3), arg1, arg2,
|
||||||
|
(uint64_t)__builtin_return_address(0));
|
||||||
|
}
|
||||||
|
|
||||||
|
void
|
||||||
|
__sanitizer_cov_trace_const_cmp1(uint8_t arg1, uint8_t arg2)
|
||||||
|
{
|
||||||
|
cov_trace_cmp_t trace_cmp;
|
||||||
|
|
||||||
|
trace_cmp = (cov_trace_cmp_t)atomic_load_ptr(&cov_trace_cmp);
|
||||||
|
if (trace_cmp != NULL)
|
||||||
|
trace_cmp(COV_CMP_SIZE(0) | COV_CMP_CONST, arg1, arg2,
|
||||||
|
(uint64_t)__builtin_return_address(0));
|
||||||
|
}
|
||||||
|
|
||||||
|
void
|
||||||
|
__sanitizer_cov_trace_const_cmp2(uint16_t arg1, uint16_t arg2)
|
||||||
|
{
|
||||||
|
cov_trace_cmp_t trace_cmp;
|
||||||
|
|
||||||
|
trace_cmp = (cov_trace_cmp_t)atomic_load_ptr(&cov_trace_cmp);
|
||||||
|
if (trace_cmp != NULL)
|
||||||
|
trace_cmp(COV_CMP_SIZE(1) | COV_CMP_CONST, arg1, arg2,
|
||||||
|
(uint64_t)__builtin_return_address(0));
|
||||||
|
}
|
||||||
|
|
||||||
|
void
|
||||||
|
__sanitizer_cov_trace_const_cmp4(uint32_t arg1, uint32_t arg2)
|
||||||
|
{
|
||||||
|
cov_trace_cmp_t trace_cmp;
|
||||||
|
|
||||||
|
trace_cmp = (cov_trace_cmp_t)atomic_load_ptr(&cov_trace_cmp);
|
||||||
|
if (trace_cmp != NULL)
|
||||||
|
trace_cmp(COV_CMP_SIZE(2) | COV_CMP_CONST, arg1, arg2,
|
||||||
|
(uint64_t)__builtin_return_address(0));
|
||||||
|
}
|
||||||
|
|
||||||
|
void
|
||||||
|
__sanitizer_cov_trace_const_cmp8(uint64_t arg1, uint64_t arg2)
|
||||||
|
{
|
||||||
|
cov_trace_cmp_t trace_cmp;
|
||||||
|
|
||||||
|
trace_cmp = (cov_trace_cmp_t)atomic_load_ptr(&cov_trace_cmp);
|
||||||
|
if (trace_cmp != NULL)
|
||||||
|
trace_cmp(COV_CMP_SIZE(3) | COV_CMP_CONST, arg1, arg2,
|
||||||
|
(uint64_t)__builtin_return_address(0));
|
||||||
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
|
* val is the switch operand
|
||||||
|
* cases[0] is the number of case constants
|
||||||
|
* cases[1] is the size of val in bits
|
||||||
|
* cases[2..n] are the case constants
|
||||||
|
*/
|
||||||
|
void
|
||||||
|
__sanitizer_cov_trace_switch(uint64_t val, uint64_t *cases)
|
||||||
|
{
|
||||||
|
uint64_t i, count, ret, type;
|
||||||
|
cov_trace_cmp_t trace_cmp;
|
||||||
|
|
||||||
|
trace_cmp = (cov_trace_cmp_t)atomic_load_ptr(&cov_trace_cmp);
|
||||||
|
if (trace_cmp == NULL)
|
||||||
|
return;
|
||||||
|
|
||||||
|
count = cases[0];
|
||||||
|
ret = (uint64_t)__builtin_return_address(0);
|
||||||
|
|
||||||
|
switch (cases[1]) {
|
||||||
|
case 8:
|
||||||
|
type = COV_CMP_SIZE(0);
|
||||||
|
break;
|
||||||
|
case 16:
|
||||||
|
type = COV_CMP_SIZE(1);
|
||||||
|
break;
|
||||||
|
case 32:
|
||||||
|
type = COV_CMP_SIZE(2);
|
||||||
|
break;
|
||||||
|
case 64:
|
||||||
|
type = COV_CMP_SIZE(3);
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
val |= COV_CMP_CONST;
|
||||||
|
|
||||||
|
for (i = 0; i < count; i++)
|
||||||
|
if (!trace_cmp(type, val, cases[i + 2], ret))
|
||||||
|
return;
|
||||||
|
}
|
60
sys/sys/coverage.h
Normal file
60
sys/sys/coverage.h
Normal file
@ -0,0 +1,60 @@
|
|||||||
|
/*-
|
||||||
|
* SPDX-License-Identifier: BSD-2-Clause-FreeBSD
|
||||||
|
*
|
||||||
|
* Copyright (C) 2018 The FreeBSD Foundation. All rights reserved.
|
||||||
|
* Copyright (C) 2018, 2019 Andrew Turner.
|
||||||
|
*
|
||||||
|
* This software was developed by Mitchell Horne under sponsorship of
|
||||||
|
* the FreeBSD Foundation.
|
||||||
|
*
|
||||||
|
* 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.
|
||||||
|
*
|
||||||
|
* $FreeBSD$
|
||||||
|
*/
|
||||||
|
|
||||||
|
#ifndef _SYS_COVERAGE_H_
|
||||||
|
#define _SYS_COVERAGE_H_
|
||||||
|
|
||||||
|
#if !defined(_KERNEL) && !defined(_SYS_KCOV_H_)
|
||||||
|
#error Do not include this file directly in userspace, use sys/kcov.h
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#define COV_CMP_CONST (1 << 0)
|
||||||
|
#define COV_CMP_SIZE(x) ((x) << 1)
|
||||||
|
#define COV_CMP_MASK (3 << 1)
|
||||||
|
#define COV_CMP_GET_SIZE(x) (((x) >> 1) & 3)
|
||||||
|
|
||||||
|
#ifdef _KERNEL
|
||||||
|
typedef void (*cov_trace_pc_t)(uintptr_t);
|
||||||
|
typedef bool (*cov_trace_cmp_t)(uint64_t, uint64_t, uint64_t, uint64_t);
|
||||||
|
|
||||||
|
void cov_register_cmp(cov_trace_cmp_t);
|
||||||
|
void cov_unregister_cmp(void);
|
||||||
|
void cov_register_pc(cov_trace_pc_t);
|
||||||
|
void cov_unregister_pc(void);
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#endif /* _SYS_COVERAGE_H_ */
|
@ -38,6 +38,7 @@
|
|||||||
#ifndef _SYS_KCOV_H_
|
#ifndef _SYS_KCOV_H_
|
||||||
#define _SYS_KCOV_H_
|
#define _SYS_KCOV_H_
|
||||||
|
|
||||||
|
#include <sys/coverage.h>
|
||||||
#include <sys/ioccom.h>
|
#include <sys/ioccom.h>
|
||||||
|
|
||||||
#define KCOV_MAXENTRIES (1 << 24) /* 16M */
|
#define KCOV_MAXENTRIES (1 << 24) /* 16M */
|
||||||
@ -51,9 +52,9 @@
|
|||||||
#define KIODISABLE _IO('c', 3) /* Disable coverage recording */
|
#define KIODISABLE _IO('c', 3) /* Disable coverage recording */
|
||||||
#define KIOSETBUFSIZE _IOWINT('c', 4) /* Set the buffer size */
|
#define KIOSETBUFSIZE _IOWINT('c', 4) /* Set the buffer size */
|
||||||
|
|
||||||
#define KCOV_CMP_CONST (1 << 0)
|
#define KCOV_CMP_CONST COV_CMP_CONST
|
||||||
#define KCOV_CMP_SIZE(x) ((x) << 1)
|
#define KCOV_CMP_SIZE(x) COV_CMP_SIZE(x)
|
||||||
#define KCOV_CMP_MASK (3 << 1)
|
#define KCOV_CMP_MASK COV_CMP_MASK
|
||||||
#define KCOV_CMP_GET_SIZE(x) (((x) >> 1) & 3)
|
#define KCOV_CMP_GET_SIZE(x) COV_CMP_GET_SIZE(x)
|
||||||
|
|
||||||
#endif /* _SYS_KCOV_H_ */
|
#endif /* _SYS_KCOV_H_ */
|
||||||
|
Loading…
Reference in New Issue
Block a user