Skip to content

Commit

Permalink
use hooking logic from AngriestSCV's fork
Browse files Browse the repository at this point in the history
  • Loading branch information
galister committed Jan 27, 2023
1 parent 34d7f12 commit 18e0fc7
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 26 deletions.
69 changes: 46 additions & 23 deletions OpenVR-SpaceCalibratorDriver/Hooking.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@
#include <map>
#include <string>
#include <sys/mman.h>
#include <stdexcept>
#include <vector>
#include <unistd.h>

class IHook
{
Expand All @@ -24,46 +27,66 @@ class IHook
static std::map<std::string, IHook *> hooks;
};

template <typename R, typename... Args>
template <typename FuncType>
class Hook : public IHook
{
void * obj = nullptr;
int offset = 0;

public:
FuncType originalFunc = nullptr;
Hook(const std::string &name) : IHook(name) { }

bool CreateHookInObjectVTable(void *object, int vtableOffset, R(*detourFunction)(Args...))

template<typename T>
bool CreateHookInObjectVTable(void *object, int vtableOffset, T* detourFunction)
{
// For virtual objects, VC++ adds a pointer to the vtable as the first member.
long pageSize = sysconf(_SC_PAGESIZE);

obj = object;
offset = vtableOffset;
// For virtual objects, VC++ (and from what I can tell gcc) adds a pointer to the vtable as the first member.
// To access the vtable, we simply dereference the object.
vFunc = *((void ***)object) + vtableOffset;
void **vtable = *((void ***)object);

// The vtable itself is an array of pointers to member functions,
// in the order they were declared in.
originalFunc = reinterpret_cast<R(*)(Args...)>(*vFunc);
uintptr_t startPage = reinterpret_cast<uintptr_t >(vFunc) & ~0xFFF;
uintptr_t endPage = reinterpret_cast<uintptr_t >(vFunc + sizeof(detourFunction)) & ~0xFFF;
mprotect(reinterpret_cast<void*>(reinterpret_cast<uintptr_t>(vFunc) & ~0xFFF), endPage - startPage + 4096, PROT_READ | PROT_WRITE | PROT_EXEC);
*vFunc = reinterpret_cast<void*>(detourFunction);
mprotect(reinterpret_cast<void*>(reinterpret_cast<uintptr_t>(vFunc) & ~0xFFF), endPage - startPage + 4096, PROT_EXEC);
LOG("Enabled hook for %s", name.c_str());
originalFunc = (FuncType) vtable[vtableOffset];
targetFunc = (void*) vtable[vtableOffset];

if((uintptr_t) vtable % 8 != 0 )
{
obj = nullptr;
originalFunc = nullptr;
throw std::runtime_error("vtable entry not aligned to 8 byte pointer");
}

uintptr_t otherPage = (uintptr_t) vtable & ~(uintptr_t) (pageSize - 1);
int err = mprotect((void*) otherPage, pageSize, PROT_READ | PROT_WRITE | PROT_EXEC);
if(err){
LOG("Failed to set memory protection %d", err);
}
else {
//LOG("%s", "Setting vtable value");
vtable[vtableOffset] = (void*) detourFunction;
//LOG("%s", "Resetting permissions vtable value");
mprotect((void*) otherPage, pageSize, PROT_READ | PROT_EXEC);
}

LOG("Enabled Linux hook for %s", name.c_str());
enabled = true;
return true;
}

void Destroy()
{
if (enabled)
{
uintptr_t startPage = reinterpret_cast<uintptr_t >(vFunc) & ~0xFFF;
uintptr_t endPage = reinterpret_cast<uintptr_t >(vFunc + sizeof(originalFunc)) & ~0xFFF;
mprotect(reinterpret_cast<void*>(reinterpret_cast<uintptr_t>(vFunc) & ~0xFFF), endPage - startPage + 4096, PROT_READ | PROT_WRITE | PROT_EXEC);
*vFunc = reinterpret_cast<void*>(originalFunc);
mprotect(reinterpret_cast<void*>(reinterpret_cast<uintptr_t>(vFunc) & ~0xFFF), endPage - startPage + 4096, PROT_EXEC);
enabled = false;
}

//redoing it is enough if it was done the first time.
if(obj && originalFunc) CreateHookInObjectVTable(obj, offset, originalFunc);
obj = nullptr;
originalFunc = nullptr;
}
R(*originalFunc)(Args...) = nullptr;

private:
bool enabled = false;
void **vFunc;
void* targetFunc = nullptr;
};
6 changes: 3 additions & 3 deletions OpenVR-SpaceCalibratorDriver/InterfaceHookInjector.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,13 @@

static ServerTrackedDeviceProvider *Driver = nullptr;

static Hook<void*, vr::IVRDriverContext*, const char*, vr::EVRInitError*>
static Hook<void*(*)(vr::IVRDriverContext *, const char *, vr::EVRInitError *)>
GetGenericInterfaceHook("IVRDriverContext::GetGenericInterface");

static Hook<void, vr::IVRServerDriverHost*, uint32_t, const vr::DriverPose_t&, uint32_t>
static Hook<void(*)(vr::IVRServerDriverHost *, uint32_t, const vr::DriverPose_t &, uint32_t)>
TrackedDevicePoseUpdatedHook005("IVRServerDriverHost005::TrackedDevicePoseUpdated");

static Hook<void, vr::IVRServerDriverHost*, uint32_t, const vr::DriverPose_t&, uint32_t>
static Hook<void(*)(vr::IVRServerDriverHost *, uint32_t, const vr::DriverPose_t &, uint32_t)>
TrackedDevicePoseUpdatedHook006("IVRServerDriverHost006::TrackedDevicePoseUpdated");

static void DetourTrackedDevicePoseUpdated005(vr::IVRServerDriverHost *_this, uint32_t unWhichDevice, const vr::DriverPose_t &newPose, uint32_t unPoseStructSize)
Expand Down

0 comments on commit 18e0fc7

Please sign in to comment.