-
Notifications
You must be signed in to change notification settings - Fork 0
/
intercept_memory.cpp
176 lines (153 loc) · 6.76 KB
/
intercept_memory.cpp
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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
#include <atomic>
#include <cerrno>
#include <csignal>
#include <cstdlib>
#include <cstring>
#include <thread>
#include <optional>
#include <poll.h>
#include <semaphore.h>
#include <sys/mman.h>
#include <unistd.h>
#include <unordered_map>
#include "intercept_memory.h"
#include "utpx.h"
namespace utpx::fault {
namespace detail {
} // namespace detail
static long pageSize{};
static long GUARD_THREAD_TIMEOUT_SECONDS = 10;
static std::atomic_uintptr_t sigFaultAddress = 0;
static sem_t sigHandlerPendingEvent{}, sigHandlerPendingResume{};
static std::shared_mutex allocationLock{};
static std::unordered_map<void *, size_t> allocations{};
// static std::atomic_flag sigFaultLatch = ATOMIC_FLAG_INIT;
// FIXME we really need to adhere to signal-safety(7) in this whole block
// XXX POSIX calls only in the handler!
static void handler(int signal, siginfo_t *siginfo, void *context) {
// XXX only handle SIGSEGV with ACCERR which is caused by r/w protected pages by mprotect
if (signal != SIGSEGV || siginfo->si_code != SEGV_ACCERR) return;
auto x86PC = static_cast<ucontext_t *>(context)->uc_mcontext.gregs[REG_RIP];
log("[MEM] SIGSEGV: Accessing memory at address %p, code=%d, pc=0x%llx", siginfo->si_addr, siginfo->si_code, x86PC); // FIXME AS unsafe
sigFaultAddress = reinterpret_cast<uintptr_t>(siginfo->si_addr); // AS safe
::sem_post(&sigHandlerPendingEvent); // AS safe
timespec ts{};
if (clock_gettime(CLOCK_REALTIME, &ts) == -1) { // AS safe
log("[MEM] SIGSEGV: clock_gettime failed, terminating..."); // FIXME AS unsafe
::abort(); // AS safe
}
ts.tv_sec += GUARD_THREAD_TIMEOUT_SECONDS;
int res = sem_timedwait(&sigHandlerPendingResume, &ts); // FIXME AS unsafe
if (res == -1) {
log("[MEM] SIGSEGV: resume timeout: guard thread did not respond within %lds, terminating...",
GUARD_THREAD_TIMEOUT_SECONDS); // FIXME AS unsafe
::abort();
}
// while (sigFaultLatch.test_and_set(std::memory_order_acquire)) // AS safe
// {
// }
log("[MEM] SIGSEGV: resume %p", siginfo->si_addr); // FIXME AS unsafe
}
std::unique_ptr<std::thread> sigHandlerGuardThread{};
std::atomic_bool sigHandlerTerminate;
static void handleFault(void *faultAddr) {
log("[MEM]\tUPH guard thread handling fault at address %p", faultAddr);
if (const auto page = lookupRegisteredPage(faultAddr); page) {
const auto &[allocAddr, allocLength] = *page;
log("[MEM]\tSIGSEGV: resuming access to %p with mprotect(%p, %ld, PROT_READ | PROT_WRITE)", faultAddr, allocAddr, allocLength);
if (mprotect(allocAddr, allocLength, PROT_READ | PROT_WRITE) != 0) {
fatal("[MEM]\tFATAL: mprotect(%p, %ld, PROT_READ | PROT_WRITE) failed: %s", allocAddr, allocLength, strerror(errno));
}
handleUserspaceFault(faultAddr, allocAddr, allocLength);
sigFaultAddress = 0;
sem_post(&sigHandlerPendingResume);
// sigFaultLatch.clear(std::memory_order_release);
} else {
fatal("[MEM]\tFATAL: address %p is not a registered page", faultAddr);
}
}
void initialiseUserspacePagefaultHandling() {
static_assert(std::atomic<bool>::is_always_lock_free);
pageSize = sysconf(_SC_PAGE_SIZE);
if (pageSize != -1) log("[MEM] page size = %ld", pageSize);
else
fatal("[MEM] Cannot resolve page size with sysconf, reason=%s, terminating...", strerror(errno));
if (sem_init(&sigHandlerPendingEvent, 0, 0) == -1)
fatal("[MEM] FATAL: Cannot create semaphore for sigHandlerPendingEvent, reason=%s, terminating...", strerror(errno));
if (sem_init(&sigHandlerPendingResume, 0, 0) == -1)
fatal("[MEM] FATAL: Cannot create semaphore for sigHandlerPendingResume, reason=%s, terminating...", strerror(errno));
log("[MEM] UPH initialised");
struct sigaction act {};
sigemptyset(&act.sa_mask);
act.sa_flags = SA_SIGINFO | SA_ONSTACK;
act.sa_sigaction = handler;
sigaction(SIGSEGV, &act, nullptr);
log("[MEM] UPH signal handler installed");
sem_post(&sigHandlerPendingEvent);
sigHandlerGuardThread = std::make_unique<std::thread>([]() {
log("[MEM]\tUPH guard thread started");
while (true) {
if (sem_wait(&sigHandlerPendingEvent) == -1) {
std::abort();
}
if (sigHandlerTerminate) break;
auto address = sigFaultAddress.load();
if (address) handleFault(reinterpret_cast<void *>(address));
}
log("[MEM]\tUPH guard thread terminated");
});
log("[MEM] UPH initialised");
}
void terminateUserspacePagefaultHandling() {
log("[MEM] UPH termination requested");
std::unique_lock<std::shared_mutex> write(allocationLock);
for (auto &[ptr, size] : allocations) {
log("[MEM]\trelease: %p, %ld", ptr, size);
if (mprotect(ptr, size, PROT_READ | PROT_WRITE) != 0) {
log("[MEM]\tWARN: mprotect(%p, %ld, PROT_READ | PROT_WRITE) failed: %s", ptr, size, strerror(errno));
}
}
sigHandlerTerminate = true;
sem_post(&sigHandlerPendingEvent);
if (sigHandlerGuardThread) sigHandlerGuardThread->join();
log("[MEM] UPH terminated");
}
void registerPage(void *ptr, size_t size) {
std::unique_lock<std::shared_mutex> write(allocationLock);
log("[MEM] UPH register page (%p, %ld) total=%zu", ptr, size, allocations.size());
auto [_, inserted] = allocations.emplace(ptr, size);
if (inserted) {
if (mprotect(ptr, size, PROT_NONE) != 0) {
fatal("[MEM] mprotect failed, reason=%s, terminating...", strerror(errno));
}
} else {
log("[MEM] UPH page already registered");
}
}
void unregisterPage(void *ptr) {
std::unique_lock<std::shared_mutex> write(allocationLock);
log("[MEM] UPH unregister page (%p)", ptr);
if (auto it = allocations.find(ptr); it != allocations.end()) {
if (mprotect(it->first, it->second, PROT_READ | PROT_WRITE) != 0) {
fatal("[MEM]\tmprotect(%p, %ld, PROT_READ | PROT_WRITE) failed: %s", it->first, it->second, strerror(errno));
}
allocations.erase(it);
} else
fatal("[MEM] UPH unregister nonexistent page (%p)", ptr);
}
std::optional<std::pair<void *, size_t>> lookupRegisteredPage(const void *ptr) {
std::shared_lock<std::shared_mutex> read(allocationLock);
for (auto &[addr, length] : allocations) {
// log("[MEM] \tUPH page (%p, %ld)", addr, length);
auto signalAddrI = reinterpret_cast<uintptr_t>(ptr);
auto allocAddrI = reinterpret_cast<uintptr_t>(addr);
if (signalAddrI >= allocAddrI && signalAddrI < (allocAddrI + length)) {
// std::fprintf(stderr, "offset from base=%d", (( ((uintptr_t) faultAddr)) - ( (uintptr_t) addr)));
// size = length - (reinterpret_cast<uintptr_t>(faultAddr) - reinterpret_cast<uintptr_t>(addr));
return std::pair{addr, length};
}
}
return {};
}
size_t hostPageSize() { return pageSize; }
} // namespace utpx::fault