Changeset View
Standalone View
sys/kern/kern_kcov.c
- This file was added.
/*- | |||||
* SPDX-License-Identifier: BSD-2-Clause-FreeBSD | |||||
* | |||||
* Copyright (C) 2018 The FreeBSD Foundation. All rights reserved. | |||||
* | |||||
* This software was developed by Mitchell Horne under sponsorship of | |||||
* the FreeBSD Foundation. | |||||
* | |||||
* 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/conf.h> | |||||
#include <sys/file.h> | |||||
#include <sys/kcov.h> | |||||
#include <sys/kernel.h> | |||||
#include <sys/malloc.h> | |||||
#include <sys/proc.h> | |||||
#include <sys/stat.h> | |||||
#include <sys/sx.h> | |||||
#include <sys/sysctl.h> | |||||
#include <sys/systm.h> | |||||
#include <sys/types.h> | |||||
#include <sys/uio.h> | |||||
#include <machine/atomic.h> | |||||
#include <machine/cpufunc.h> | |||||
#define BUF_SIZE_BYTES(info) \ | |||||
(info != NULL ? (size_t)info->size * sizeof(uint32_t) : 0) | |||||
#define BUF_AVAIL_BYTES(info) \ | |||||
(info != NULL ? (size_t)info->index * sizeof(uint32_t) : 0) | |||||
MALLOC_DEFINE(M_KCOV_INFO, "kcovinfo", "KCOV info type"); | |||||
MALLOC_DEFINE(M_KCOV_BUF, "kcovbuffer", "KCOV buffer type"); | |||||
mhorne063_gmail.com: This include is (probably) no longer needed. | |||||
struct kcov_info { | |||||
struct sx lock; | |||||
struct thread *td; | |||||
uint32_t *buf; | |||||
u_int size; | |||||
u_int index; | |||||
int mode; | |||||
}; | |||||
/* Prototypes */ | |||||
static d_open_t kcov_open; | |||||
static d_open_t kcov_close; | |||||
andrewAuthorUnsubmitted Done Inline ActionsShouldn't this be d_close_t? andrew: Shouldn't this be `d_close_t`? | |||||
static d_read_t kcov_read; | |||||
static d_ioctl_t kcov_ioctl; | |||||
static void kcov_info_reset(struct kcov_info *info); | |||||
static int kcov_alloc(struct kcov_info *info, u_int entries); | |||||
static void kcov_init(const void *unused); | |||||
static bool kcov_initialized = false; | |||||
static struct cdevsw kcov_cdevsw = { | |||||
.d_version = D_VERSION, | |||||
.d_open = kcov_open, | |||||
.d_close = kcov_close, | |||||
.d_read = kcov_read, | |||||
.d_ioctl = kcov_ioctl, | |||||
.d_name = "kcov", | |||||
}; | |||||
static u_int kcov_max_entries = KCOV_MAXENTRIES; | |||||
SYSCTL_UINT(_kern, OID_AUTO, kcov_max_entries, CTLFLAG_RW, | |||||
&kcov_max_entries, 0, | |||||
"Maximum number of entries that can be stored in a kcov buffer"); | |||||
/* | |||||
* 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) | |||||
{ | |||||
struct thread *td; | |||||
struct kcov_info *info; | |||||
/* | |||||
* To guarantee curthread is properly set, we exit early | |||||
* until the driver has been initialized | |||||
*/ | |||||
if (!kcov_initialized) | |||||
return; | |||||
td = curthread; | |||||
info = td->td_kcov_info; | |||||
/* | |||||
* Check first that KCOV is enabled for the current thread. | |||||
* Additionally, we want to exclude (for now) all code that | |||||
* is not explicitly part of syscall call chain, such as | |||||
* interrupt handlers, since we are mainly interested in | |||||
* finding non-trivial paths through the syscall. | |||||
*/ | |||||
if (info == NULL || info->buf == NULL || | |||||
info->mode != KCOV_MODE_TRACE_PC || | |||||
Not Done Inline ActionsUse KCOV_MAXSIZE instead of KCOV_MAXENTRIES here, too. tuexen: Use `KCOV_MAXSIZE` instead of `KCOV_MAXENTRIES` here, too. | |||||
Done Inline ActionsThe size is now in terms of entries, where an entry is sizeof(uint64_t) sized. andrew: The size is now in terms of entries, where an entry is `sizeof(uint64_t)` sized. | |||||
td->td_intr_nesting_level > 0 || !interrupts_enabled()) | |||||
return; | |||||
if (info->index < info->size) { | |||||
/* | |||||
* XXX Address may be trucated since we are only | |||||
* interested in the lower 32 bits | |||||
*/ | |||||
info->buf[info->index] = | |||||
(uint32_t)__builtin_return_address(0); | |||||
atomic_fetchadd_int(&info->index, 1); | |||||
} | |||||
} | |||||
static int | |||||
kcov_open(struct cdev *dev, int oflags, int devtype, struct thread *td) | |||||
{ | |||||
struct kcov_info *info; | |||||
info = malloc(sizeof(struct kcov_info), M_KCOV_INFO, | |||||
M_ZERO | M_WAITOK); | |||||
kcov_info_reset(info); | |||||
sx_init(&info->lock, "kcov_lock"); | |||||
dev->si_drv1 = info; | |||||
return (0); | |||||
} | |||||
static int | |||||
Not Done Inline ActionsWhy are the results of this check valid after the check ? kib: Why are the results of this check valid after the check ? | |||||
Done Inline ActionsHow do you mean? The code checks if kcov is running on a given thread. This is only used in the functions that handle tracing so td == curthread. There is nothing stopping another thread to disable tracing just after this check, however I've tried to be careful to allow for this in the code by only freeing the buffer in specific safe places. andrew: How do you mean?
The code checks if kcov is running on a given thread. This is only used in… | |||||
Not Done Inline ActionsWhat prevents other thread from unmapping the buffer after we checked that the state is RUNNING ? kib: What prevents other thread from unmapping the buffer after we checked that the state is RUNNING… | |||||
Done Inline ActionsWe need to both unmap the buffer and exit the thread before freeing the buffer. The kernel memory is not allowed to be freed by another thread unless the thread has exited as it doesn't know if the buffer is still in use. The only places we can free the kernel buffer are in kcov_thread_dtor and kcov_mmap_cleanup. Both of these check if the state is DYING before freeing the buffer. There is a bug here where we may not free the buffer if the thread exits before munmap is called that should be fixed. andrew: We need to both unmap the buffer and exit the thread before freeing the buffer. The kernel… | |||||
kcov_close(struct cdev *dev, int fflag, int devtype, struct thread *td) | |||||
Not Done Inline ActionsI do not understand what seq_cst() does there at all. If you see NULL info you cannot dereference it at all. But if you see it non-NULL, there is no guarantee that a parallel thread would not change state after we checked it. Or I missed a mechanism which would prevent this. Why cannot destructor proceed while we are executing trace_cmp() ? kib: I do not understand what seq_cst() does there at all. If you see NULL info you cannot… | |||||
Done Inline ActionsThere is nothing stopping another thread from changing it after this check. The rest of the code is written such that if this does happen trace_cmp and __sanitizer_cov_trace_pc are safe. We may get extra entries, but these are for userspace to deal with by trying to trace a non-local thread. andrew: There is nothing stopping another thread from changing it after this check. The rest of the… | |||||
Not Done Inline ActionsWhat guarantees the safety ? Why the buffer cannot be unmapped from the KVA ? kib: What guarantees the safety ? Why the buffer cannot be unmapped from the KVA ? | |||||
Done Inline ActionsI've updated to try be more explicit. It requires a thread to stop using the info struct, either by thread exit or, from the thread running being traced, by calling KIODISABLE. It also requires userspace to stop using the buffer and close the fd. When both of these are the case we can free the buffer. As these can happen in either order the code to handle this is in the devfs cdevpriv dtor and the thread dtor. andrew: I've updated to try be more explicit.
It requires a thread to stop using the info struct… | |||||
{ | |||||
struct kcov_info *info; | |||||
info = dev->si_drv1; | |||||
if (info == NULL) | |||||
return (EINVAL); | |||||
td->td_kcov_info = NULL; | |||||
dev->si_drv1 = NULL; | |||||
Not Done Inline ActionsIs this correct? What would happen if we open on one thread, but close on another? andrew: Is this correct? What would happen if we open on one thread, but close on another? | |||||
sx_destroy(&info->lock); | |||||
free(info->buf, M_KCOV_BUF); | |||||
free(info, M_KCOV_INFO); | |||||
return (0); | |||||
} | |||||
static int | |||||
kcov_read(struct cdev *dev, struct uio *uio, int ioflag) | |||||
{ | |||||
Done Inline ActionsDo we still need the read interface or should we just use mmap? andrew: Do we still need the read interface or should we just use mmap? | |||||
Not Done Inline ActionsI'm somewhat unsure on this, but I don't think we will need it anymore. In general is there ever a reason to provide one if you have the other? mhorne063_gmail.com: I'm somewhat unsure on this, but I don't think we will need it anymore. In general is there… | |||||
struct kcov_info *info; | |||||
size_t len; | |||||
int error; | |||||
info = dev->si_drv1; | |||||
if (info == NULL || info->buf == NULL) { | |||||
return (EINVAL); | |||||
} | |||||
sx_slock(&info->lock); | |||||
len = uio->uio_resid <= BUF_AVAIL_BYTES(info) - uio->uio_offset ? | |||||
uio->uio_resid : BUF_AVAIL_BYTES(info) - uio->uio_offset; | |||||
error = uiomove(info->buf, len, uio); | |||||
sx_sunlock(&info->lock); | |||||
return (error); | |||||
} | |||||
static void | |||||
kcov_info_reset(struct kcov_info *info) | |||||
{ | |||||
Not Done Inline ActionsI'm assuming this is the incrementing of the count for this location? bdrewery: I'm assuming this is the incrementing of the count for this location?
If not this storage… | |||||
Done Inline ActionsIt's a per-thread buffer. The first item indicates the number of records. It will be used on a single CPU up until the scheduler moves it. It is expected userspace will normally read it from the same CPU as this will normally happen within the same thread, however this isn't a hard requirement. andrew: It's a per-thread buffer. The first item indicates the number of records. It will be used on a… | |||||
if (info == NULL) | |||||
return; | |||||
free(info->buf, M_KCOV_BUF); | |||||
info->buf = NULL; | |||||
info->mode = KCOV_MODE_NONE; | |||||
info->size = 0; | |||||
info->index = 0; | |||||
} | |||||
static int | |||||
kcov_alloc(struct kcov_info *info, u_int entries) | |||||
{ | |||||
if (entries > kcov_max_entries) | |||||
return (EINVAL); | |||||
kcov_info_reset(info); | |||||
info->buf = malloc(entries * sizeof(uint32_t), M_KCOV_BUF, M_WAITOK); | |||||
info->size = entries; | |||||
return (0); | |||||
} | |||||
static int | |||||
kcov_ioctl(struct cdev *dev, u_long cmd, caddr_t data, int fflag __unused, | |||||
struct thread *td) | |||||
{ | |||||
struct kcov_info *info; | |||||
int error; | |||||
error = 0; | |||||
info = dev->si_drv1; | |||||
sx_xlock(&info->lock); | |||||
switch (cmd) { | |||||
case KIOSETBUFSIZE: | |||||
/* | |||||
* Set the size of the coverage buffer. Should be called | |||||
* before enabling coverage collection for that thread. | |||||
Done Inline ActionsWouldn't it be better to calculate the size then round up to PAGE_SIZE? This will allocate 4 or 8 times too many pages on 32 or 64 bit architectures respectively. andrew: Wouldn't it be better to calculate the size then round up to `PAGE_SIZE`? This will allocate 4… | |||||
*/ | |||||
error = kcov_alloc(info, *(u_int *)data); | |||||
break; | |||||
Done Inline ActionsYou should zero the buffer with the M_ZERO flag. Userpace can mmap it & read what was previously in the page. andrew: You should zero the buffer with the `M_ZERO` flag. Userpace can mmap it & read what was… | |||||
case KIOENABLE: | |||||
/* Only enable if not currently owned */ | |||||
if (info->td != NULL) { | |||||
error = EBUSY; | |||||
break; | |||||
Not Done Inline ActionsThis section would be a little clearer if we named the constant. Something like #define KCOV_CMP_ITEM_SIZE 4? It could also be used in user code for iterating through the buffer. mhorne063_gmail.com: This section would be a little clearer if we named the constant. Something like `#define… | |||||
} | |||||
info->mode = *(int *)data; | |||||
td->td_kcov_info = info; | |||||
info->td = td; | |||||
break; | |||||
case KIODISABLE: | |||||
/* Only the currently enabled thread may disable itself */ | |||||
if (info->td != td) { | |||||
error = EINVAL; | |||||
} | |||||
info->mode = KCOV_MODE_NONE; | |||||
td->td_kcov_info = NULL; | |||||
info->td = NULL; | |||||
break; | |||||
case KIORESET: | |||||
info->index = 0; | |||||
break; | |||||
case KIONREAD: | |||||
/* Return the number of entries available to be read */ | |||||
*(u_int *)data = info->index; | |||||
break; | |||||
default: | |||||
error = EINVAL; | |||||
break; | |||||
} | |||||
sx_xunlock(&info->lock); | |||||
return (error); | |||||
} | |||||
static void | |||||
kcov_init(const void *unused) | |||||
{ | |||||
struct make_dev_args args; | |||||
struct cdev *dev; | |||||
make_dev_args_init(&args); | |||||
args.mda_devsw = &kcov_cdevsw; | |||||
args.mda_uid = UID_ROOT; | |||||
args.mda_gid = GID_WHEEL; | |||||
args.mda_mode = 0660; | |||||
if (make_dev_s(&args, &dev, "kcov") != 0) { | |||||
printf("%s", "Failed to create kcov device"); | |||||
return; | |||||
} | |||||
Not Done Inline ActionsWhat is the overhead of this ioctl? i.e. if you query it in a loop on the current thread how quickly does it change? andrew: What is the overhead of this ioctl? i.e. if you query it in a loop on the current thread how… | |||||
Not Done Inline ActionsNot sure exactly what you mean by "how quickly does it change". When using syzkaller, it appears that it gets called once after each execution -- essentially making it 1 in every 2 system calls. This is probably enough to justify removing the ioctl and storing it in the buffer like in the Linux kcov. mhorne063_gmail.com: Not sure exactly what you mean by "how quickly does it change". When using syzkaller, it… | |||||
kcov_initialized = true; | |||||
} | |||||
/* | |||||
* thread_exit() hook | |||||
*/ | |||||
void | |||||
kcov_thread_exit(struct thread *td) | |||||
{ | |||||
if (td->td_kcov_info != NULL) { | |||||
td->td_kcov_info->td = NULL; | |||||
td->td_kcov_info = NULL; | |||||
} | |||||
} | |||||
SYSINIT(kcovdev, SI_SUB_DEVFS, SI_ORDER_ANY, kcov_init, NULL); | |||||
Not Done Inline ActionsChanging if (info->state != KCOV_STATE_READY) { to if ((info->state != KCOV_STATE_READY) && (info->state != KCOV_STATE_DYING)) { allows syzkaller to work. Basically, allowing iotcl(..., KIOENABLE, ...) to succeed after the thread exited. tuexen: Changing
```
if (info->state != KCOV_STATE_READY) {
```
to
```
if ((info->state !=… | |||||
Done Inline ActionsI've changed the logic to return to KCOV_STATE_READY on thread exit. andrew: I've changed the logic to return to `KCOV_STATE_READY` on thread exit. | |||||
Done Inline ActionsRemove/update this comment since you removed the function. mhorne063_gmail.com: Remove/update this comment since you removed the function. | |||||
Done Inline ActionsI've updated it as it still holds, just not with the given function name. andrew: I've updated it as it still holds, just not with the given function name. | |||||
Not Done Inline ActionsWhat is the purpose of this (and all other) seq_cst() fences in the patch ? kib: What is the purpose of this (and all other) seq_cst() fences in the patch ? | |||||
Done Inline ActionsIt's to ensure the state has been updated, even in the presence of interrupts. It may not be needed in all cases, however I've decided to be safe for now. andrew: It's to ensure the state has been updated, even in the presence of interrupts. It may not be… | |||||
Not Done Inline ActionsThis is not how fences work. They only order actions and do not guarantee completion. Also, you need the residual fence on the reader side, between accesses to td_kcov_info and state (note the residual order as well), for this fence to have any effect. kib: This is not how fences work. They only order actions and do not guarantee completion. Also… | |||||
Not Done Inline ActionsWhy this cannot be store_rel ? kib: Why this cannot be store_rel ? | |||||
Done Inline ActionsThe fence is to ensure the ordering when moving from RUNNING to another state. It's there so, if an interrupt happens, the store to leave the RUNNING state is ordered and observed on the same CPU before any further changes to the info struct. A store_rel would only ensure prior loads and stores are observed before the store, not later stores. andrew: The fence is to ensure the ordering when moving from RUNNING to another state. It's there so… | |||||
Not Done Inline ActionsThis is very weird requirement, for later stores to not be observed before specified one. I am aware of only one situation where such arrangement is needed, and I really do not see how it happens that you need it for indicating consistent state of the structure. That said, I mean atomic_thread_fence_rel(), and it guarantees (r,w|w) barrier. Can you add a comment to the code, explaining the kcov_info lifecycle. Esp. please put details about which context is allowed to destroy kcov_info, and how it is ensured that the coverage code in the context of the measured thread never access dangled td_kcov_info or unmapped buffer pointed to by kinfo. kib: This is very weird requirement, for later stores to not be observed before specified one. I am… | |||||
Not Done Inline ActionsYou can move unlock before if() ? Why the spinlock is needed ? kib: You can move unlock before if() ? Why the spinlock is needed ? | |||||
Done Inline ActionsThe lock id protecting reading the thread pointer, e.g. one thread is closing the fd while another is exiting. I could change it to struct thread *thread; ... thread = info->thread; mtx_unlock_spin(&kcov_lock); if (thread != NULL) return; andrew: The lock id protecting reading the thread pointer, e.g. one thread is closing the fd while… | |||||
Not Done Inline ActionsThis probably could be fence_rel() kib: This probably could be fence_rel() | |||||
Done Inline ActionsDoes this stop later loads/stores to be observed before the atomic store? andrew: Does this stop later loads/stores to be observed before the atomic store? |
This include is (probably) no longer needed.