-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathTrampolineHook.cpp
35 lines (27 loc) · 914 Bytes
/
TrampolineHook.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
#include "memory_patch.h"
#include <assert.h>
#include <stdexcept>
#include <string.h>
#include <sys/mman.h>
#include <subhook.h>
namespace memory_patch {
TrampolineHookV::TrampolineHookV(void* addr, const void* func,
size_t e_l, const void* e) :
Base(addr, 18 /* wild guess on trampoline size */, e_l, e),
// NB: Subhook doesn't actually touch the target func so we can discard
// the const.
m_hook(reinterpret_cast<subhook_t>(subhook_new(addr, const_cast<void*>(func), {})))
{
if (subhook_install(reinterpret_cast<subhook_t>(m_hook)) < 0)
throw std::runtime_error("HookV failed to install");
}
void* TrampolineHookV::GetTrampoline() const
{
return subhook_get_trampoline(reinterpret_cast<subhook_t>(m_hook));
}
TrampolineHookV::~TrampolineHookV()
{
subhook_remove(reinterpret_cast<subhook_t>(m_hook));
subhook_free(reinterpret_cast<subhook_t>(m_hook));
}
}