summaryrefslogtreecommitdiff
path: root/Kernel/SanCov.cpp
blob: d8ddf2487a4497a82d57187b5f281e8730ed3c53 (plain)
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
/*
 * Copyright (c) 2021, Patrick Meyer <git@the-space.agency>
 *
 * SPDX-License-Identifier: BSD-2-Clause
 */

#include <Kernel/Devices/KCOVDevice.h>
#include <Kernel/Tasks/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 const* 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));
}
}