diff options
author | Patrick Meyer <git@the-space.agency> | 2021-06-06 16:15:07 -0700 |
---|---|---|
committer | Gunnar Beutner <gunnar@beutner.name> | 2021-07-26 17:40:28 +0200 |
commit | 83f88df7574c41bd3c7255d7f46930eef6744b6b (patch) | |
tree | 4ec44f915b8c0c38f07493d344b39d8e71eac948 /Kernel/SanCov.cpp | |
parent | 67b3255fe84324e384a52e19fb5233ccb3665c1d (diff) | |
download | serenity-83f88df7574c41bd3c7255d7f46930eef6744b6b.zip |
Kernel: Add option to build with coverage instrumentation and KCOV
GCC and Clang allow us to inject a call to a function named
__sanitizer_cov_trace_pc on every edge. This function has to be defined
by us. By noting down the caller in that function we can trace the code
we have encountered during execution. Such information is used by
coverage guided fuzzers like AFL and LibFuzzer to determine if a new
input resulted in a new code path. This makes fuzzing much more
effective.
Additionally this adds a basic KCOV implementation. KCOV is an API that
allows user space to request the kernel to start collecting coverage
information for a given user space thread. Furthermore KCOV then exposes
the collected program counters to user space via a BlockDevice which can
be mmaped from user space.
This work is required to add effective support for fuzzing SerenityOS to
the Syzkaller syscall fuzzer. :^) :^)
Diffstat (limited to 'Kernel/SanCov.cpp')
-rw-r--r-- | Kernel/SanCov.cpp | 38 |
1 files changed, 38 insertions, 0 deletions
diff --git a/Kernel/SanCov.cpp b/Kernel/SanCov.cpp new file mode 100644 index 0000000000..dfb81d2c45 --- /dev/null +++ b/Kernel/SanCov.cpp @@ -0,0 +1,38 @@ +/* + * Copyright (c) 2021, Patrick Meyer <git@the-space.agency> + * + * SPDX-License-Identifier: BSD-2-Clause + */ + +#include <Kernel/Devices/KCOVDevice.h> +#include <Kernel/Process.h> +#include <Kernel/Thread.h> + +extern bool g_in_early_boot; + +extern "C" { + +void __sanitizer_cov_trace_pc(void); +void __sanitizer_cov_trace_pc(void) +{ + if (g_in_early_boot) [[unlikely]] + return; + + if (Processor::current().in_irq()) [[unlikely]] { + // Do not trace in interrupts. + return; + } + + auto thread = Thread::current(); + auto tid = thread->tid(); + auto maybe_kcov_instance = KCOVDevice::thread_instance->get(tid); + if (!maybe_kcov_instance.has_value()) [[likely]] { + // not traced + return; + } + auto kcov_instance = maybe_kcov_instance.value(); + if (kcov_instance->state < KCOVInstance::TRACING) [[likely]] + return; + kcov_instance->buffer_add_pc((u64)__builtin_return_address(0)); +} +} |