2021-06-06 23:15:07 +00:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2021, Patrick Meyer <git@the-space.agency>
|
|
|
|
*
|
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
|
|
|
*/
|
|
|
|
|
|
|
|
#pragma once
|
|
|
|
|
2023-02-24 15:14:24 +00:00
|
|
|
#include <Kernel/API/kcov.h>
|
2021-08-21 23:37:17 +00:00
|
|
|
#include <Kernel/Locking/Spinlock.h>
|
2021-08-06 08:45:34 +00:00
|
|
|
#include <Kernel/Memory/AnonymousVMObject.h>
|
2021-06-06 23:15:07 +00:00
|
|
|
|
|
|
|
namespace Kernel {
|
|
|
|
|
2021-07-26 19:44:49 +00:00
|
|
|
#define KCOV_MAX_ENTRIES (10 * 1024 * 1024)
|
2021-06-06 23:15:07 +00:00
|
|
|
|
|
|
|
/*
|
2024-04-08 00:49:15 +00:00
|
|
|
* 1. When a thread opens /dev/kcov for the first time, a KCOVInstance is
|
|
|
|
* allocated and tracked via an OwnPtr on the Kernel::Process object.
|
|
|
|
* 2. When a thread in the same process then uses the KCOV_SETBUFSIZE ioctl
|
|
|
|
* on the block device, a Memory::Region is allocated and tracked via an
|
|
|
|
* OwnPtr on the KCOVInstance.
|
|
|
|
* 3. When a thread in the same process then uses the KCOV_ENABLE ioctl on
|
|
|
|
* the block device, a flag is set in the Thread object and __sanitizer_cov_trace_pc
|
|
|
|
* will start recording this threads visited code paths .
|
|
|
|
* 3. When the same thread then uses the KCOV_DISABLE ioctl on the block device,
|
|
|
|
* a flag is unset in the Thread object and __sanitizer_cov_trace_pc will
|
|
|
|
* no longer record this threads visited code paths.
|
|
|
|
* 4. When the Process dies, the KCOVInstance and Memory::Region are GCed.
|
2021-06-06 23:15:07 +00:00
|
|
|
*/
|
|
|
|
class KCOVInstance final {
|
|
|
|
public:
|
|
|
|
explicit KCOVInstance(ProcessID pid);
|
|
|
|
|
2021-11-07 23:51:39 +00:00
|
|
|
ErrorOr<void> buffer_allocate(size_t buffer_size_in_entries);
|
2021-06-06 23:15:07 +00:00
|
|
|
bool has_buffer() const { return m_buffer != nullptr; }
|
|
|
|
void buffer_add_pc(u64 pc);
|
|
|
|
|
2021-09-05 22:31:48 +00:00
|
|
|
Memory::VMObject* vmobject() { return m_vmobject; }
|
|
|
|
|
2022-11-09 10:39:58 +00:00
|
|
|
Spinlock<LockRank::None>& spinlock() { return m_lock; }
|
2021-06-06 23:15:07 +00:00
|
|
|
|
|
|
|
private:
|
2021-09-05 22:31:48 +00:00
|
|
|
ProcessID m_pid { 0 };
|
|
|
|
u64 m_buffer_size_in_entries { 0 };
|
|
|
|
size_t m_buffer_size_in_bytes { 0 };
|
|
|
|
kcov_pc_t* m_buffer { nullptr };
|
2022-08-19 18:53:40 +00:00
|
|
|
LockRefPtr<Memory::AnonymousVMObject> m_vmobject;
|
2021-06-06 23:15:07 +00:00
|
|
|
|
|
|
|
// Here to ensure it's not garbage collected at the end of open()
|
2021-08-06 11:49:36 +00:00
|
|
|
OwnPtr<Memory::Region> m_kernel_region;
|
2021-09-05 22:31:48 +00:00
|
|
|
|
2022-11-09 10:39:58 +00:00
|
|
|
Spinlock<LockRank::None> m_lock {};
|
2021-06-06 23:15:07 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
}
|