Skip to content

Commit

Permalink
Replace uint64 by uintptr
Browse files Browse the repository at this point in the history
  • Loading branch information
qubka committed Sep 24, 2024
1 parent 894b20a commit fd23a67
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 35 deletions.
36 changes: 18 additions & 18 deletions src/callback.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ asmjit::TypeId PLH::Callback::getTypeId(DataType type) {
return asmjit::TypeId::kVoid;
}

uint64_t PLH::Callback::getJitFunc(const asmjit::FuncSignature& sig, const asmjit::Arch arch, const CallbackEntry pre, const CallbackEntry post) {
uintptr_t PLH::Callback::getJitFunc(const asmjit::FuncSignature& sig, const asmjit::Arch arch, const CallbackEntry pre, const CallbackEntry post) {
if (m_functionPtr) {
return m_functionPtr;
}
Expand Down Expand Up @@ -110,7 +110,7 @@ uint64_t PLH::Callback::getJitFunc(const asmjit::FuncSignature& sig, const asmji
}

// setup the stack structure to hold arguments for user callback
uint32_t stackSize = (uint32_t)(sizeof(uint64_t) * sig.argCount());
uint32_t stackSize = (uint32_t)(sizeof(uintptr_t) * sig.argCount());
asmjit::x86::Mem argsStack = cc.newStack(stackSize, 16);
asmjit::x86::Mem argsStackIdx(argsStack);

Expand All @@ -120,8 +120,8 @@ uint64_t PLH::Callback::getJitFunc(const asmjit::FuncSignature& sig, const asmji
// stackIdx <- stack[i].
argsStackIdx.setIndex(i);

// r/w are sizeof(uint64_t) width now
argsStackIdx.setSize(sizeof(uint64_t));
// r/w are sizeof(uintptr_t) width now
argsStackIdx.setSize(sizeof(uintptr_t));

// set i = 0
cc.mov(i, 0);
Expand All @@ -139,8 +139,8 @@ uint64_t PLH::Callback::getJitFunc(const asmjit::FuncSignature& sig, const asmji
return 0;
}

// next structure slot (+= sizeof(uint64_t))
cc.add(i, sizeof(uint64_t));
// next structure slot (+= sizeof(uintptr_t))
cc.add(i, sizeof(uintptr_t));
}

auto callbackSig = asmjit::FuncSignature::build<void, Callback*, Parameters*, Property*, Return*>();
Expand All @@ -154,18 +154,18 @@ uint64_t PLH::Callback::getJitFunc(const asmjit::FuncSignature& sig, const asmji
cc.lea(argStruct, argsStack);

// create buffer for property struct
asmjit::x86::Mem propStack = cc.newStack(sizeof(uint64_t), 16);
asmjit::x86::Mem propStack = cc.newStack(sizeof(uintptr_t), 16);
asmjit::x86::Gp propStruct = cc.newUIntPtr("propStruct");
cc.lea(propStruct, propStack);

// create buffer for return struct
asmjit::x86::Mem retStack = cc.newStack(sizeof(uint64_t), 16);
asmjit::x86::Mem retStack = cc.newStack(sizeof(uintptr_t), 16);
asmjit::x86::Gp retStruct = cc.newUIntPtr("retStruct");
cc.lea(retStruct, retStack);

{
asmjit::x86::Mem propStackIdx(propStack);
propStackIdx.setSize(sizeof(uint64_t));
propStackIdx.setSize(sizeof(uintptr_t));
Property property{ (int32_t) sig.argCount(), ReturnFlag::Default };
cc.mov(propStackIdx, *(int64_t*) &property);
}
Expand Down Expand Up @@ -207,8 +207,8 @@ uint64_t PLH::Callback::getJitFunc(const asmjit::FuncSignature& sig, const asmji
return 0;
}

// next structure slot (+= sizeof(uint64_t))
cc.add(i, sizeof(uint64_t));
// next structure slot (+= sizeof(uintptr_t))
cc.add(i, sizeof(uintptr_t));
}

// deref the trampoline ptr (holder must live longer, must be concrete reg since push later)
Expand All @@ -232,7 +232,7 @@ uint64_t PLH::Callback::getJitFunc(const asmjit::FuncSignature& sig, const asmji
origInvokeNode->setRet(0, retRegister);

asmjit::x86::Mem retStackIdx(retStack);
retStackIdx.setSize(sizeof(uint64_t));
retStackIdx.setSize(sizeof(uintptr_t));
if (asmjit::TypeUtils::isInt(sig.ret())) {
cc.mov(retStackIdx, retRegister.as<asmjit::x86::Gp>());
} else {
Expand Down Expand Up @@ -270,15 +270,15 @@ uint64_t PLH::Callback::getJitFunc(const asmjit::FuncSignature& sig, const asmji
return 0;
}

// next structure slot (+= sizeof(uint64_t))
cc.add(i, sizeof(uint64_t));
// next structure slot (+= sizeof(uintptr_t))
cc.add(i, sizeof(uintptr_t));
}

cc.bind(noPost);

if (sig.hasRet()) {
asmjit::x86::Mem retStackIdx(retStack);
retStackIdx.setSize(sizeof(uint64_t));
retStackIdx.setSize(sizeof(uintptr_t));
if (asmjit::TypeUtils::isInt(sig.ret())) {
asmjit::x86::Gp tmp = cc.newUIntPtr();
cc.mov(tmp, retStackIdx);
Expand Down Expand Up @@ -306,7 +306,7 @@ uint64_t PLH::Callback::getJitFunc(const asmjit::FuncSignature& sig, const asmji
return m_functionPtr;
}

uint64_t PLH::Callback::getJitFunc(const DataType retType, const std::vector<DataType>& paramTypes, const CallbackEntry pre, const CallbackEntry post) {
uintptr_t PLH::Callback::getJitFunc(const DataType retType, const std::vector<DataType>& paramTypes, const CallbackEntry pre, const CallbackEntry post) {
asmjit::FuncSignature sig(asmjit::CallConvId::kHost, asmjit::FuncSignature::kNoVarArgs, getTypeId(retType));
for (const DataType& type : paramTypes) {
sig.addArg(getTypeId(type));
Expand Down Expand Up @@ -381,11 +381,11 @@ PLH::Callback::View PLH::Callback::getCallbacks(const CallbackType type) {
return { m_callbacks[to_integral(type)], std::shared_lock(m_mutex) };
}

uint64_t* PLH::Callback::getTrampolineHolder() {
uintptr_t* PLH::Callback::getTrampolineHolder() {
return &m_trampolinePtr;
}

uint64_t* PLH::Callback::getFunctionHolder() {
uintptr_t* PLH::Callback::getFunctionHolder() {
return &m_functionPtr;
}

Expand Down
18 changes: 9 additions & 9 deletions src/callback.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -66,12 +66,12 @@ namespace PLH {

// asm depends on this specific type
// we the ILCallback allocates stack space that is set to point here
volatile uint64_t m_arguments;
volatile uintptr_t m_arguments;

private:
// must be char* for aliasing rules to work when reading back out
char* getArgPtr(const uint8_t idx) const {
return (char*) &m_arguments + sizeof(uint64_t) * idx;
return (char*) &m_arguments + sizeof(uintptr_t) * idx;
}
};

Expand All @@ -88,7 +88,7 @@ namespace PLH {
uint8_t* getRetPtr() const {
return (unsigned char*)&m_retVal;
}
volatile uint64_t m_retVal;
volatile uintptr_t m_retVal;
};

struct Property {
Expand All @@ -104,11 +104,11 @@ namespace PLH {
Callback();
~Callback();

uint64_t getJitFunc(const asmjit::FuncSignature& sig, asmjit::Arch arch, CallbackEntry pre, CallbackEntry post);
uint64_t getJitFunc(DataType retType, const std::vector<DataType>& paramTypes, CallbackEntry pre, CallbackEntry post);
uintptr_t getJitFunc(const asmjit::FuncSignature& sig, asmjit::Arch arch, CallbackEntry pre, CallbackEntry post);
uintptr_t getJitFunc(DataType retType, const std::vector<DataType>& paramTypes, CallbackEntry pre, CallbackEntry post);

uint64_t* getTrampolineHolder();
uint64_t* getFunctionHolder();
uintptr_t* getTrampolineHolder();
uintptr_t* getFunctionHolder();
View getCallbacks(CallbackType type);
std::string_view getError() const;

Expand All @@ -123,9 +123,9 @@ namespace PLH {

std::array<std::vector<CallbackHandler>, 2> m_callbacks;
std::shared_mutex m_mutex;
uint64_t m_functionPtr = 0;
uintptr_t m_functionPtr = 0;
union {
uint64_t m_trampolinePtr = 0;
uintptr_t m_trampolinePtr = 0;
const char* m_errorCode;
};
};
Expand Down
16 changes: 8 additions & 8 deletions src/plugin.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -63,9 +63,9 @@ Callback* PolyHookPlugin::hookDetour(void* pFunc, DataType returnType, const std
return nullptr;
}

uint64_t JIT = callback->getJitFunc(returnType, arguments, &PreCallback, &PostCallback);
uintptr_t JIT = callback->getJitFunc(returnType, arguments, &PreCallback, &PostCallback);

auto detour = std::make_unique<NatDetour>((uint64_t)pFunc, JIT, callback->getTrampolineHolder());
auto detour = std::make_unique<NatDetour>((uintptr_t)pFunc, JIT, callback->getTrampolineHolder());
if (!detour->hook())
return nullptr;

Expand Down Expand Up @@ -96,16 +96,16 @@ Callback* PolyHookPlugin::hookVirtual(void* pClass, int index, DataType returnTy
return nullptr;
}

uint64_t JIT = callback->getJitFunc(returnType, arguments, &PreCallback, &PostCallback);
uintptr_t JIT = callback->getJitFunc(returnType, arguments, &PreCallback, &PostCallback);

auto& [redirectMap, origVFuncs] = m_tables[pClass];
redirectMap[index] = JIT;

auto vtable = std::make_unique<VTableSwapHook>((uint64_t) pClass, redirectMap, &origVFuncs);
auto vtable = std::make_unique<VTableSwapHook>((uintptr_t) pClass, redirectMap, &origVFuncs);
if (!vtable->hook())
return nullptr;

uint64_t origVFunc = origVFuncs[index];
uintptr_t origVFunc = origVFuncs[index];
*callback->getTrampolineHolder() = origVFunc;

void* key = m_vhooks.emplace(pClass, std::move(vtable)).first->second.get();
Expand Down Expand Up @@ -158,7 +158,7 @@ bool PolyHookPlugin::unhookVirtual(void* pClass, int index) {
return true;
}

vtable = std::make_unique<VTableSwapHook>((uint64_t) pClass, redirectMap, &origVFuncs);
vtable = std::make_unique<VTableSwapHook>((uintptr_t) pClass, redirectMap, &origVFuncs);

if (!vtable->hook())
return false;
Expand Down Expand Up @@ -241,7 +241,7 @@ void* PolyHookPlugin::findOriginalAddr(void* pClass, void* pAddr) {
int PolyHookPlugin::getVTableIndex(void* pFunc) const {
constexpr size_t size = 12;

MemoryProtector protector((uint64_t)pFunc, size, R, *(MemAccessor*)this);
MemoryProtector protector((uintptr_t)pFunc, size, R, *(MemAccessor*)this);

#if defined(__GNUC__) || defined(__clang__)
struct GCC_MemFunPtr {
Expand Down Expand Up @@ -299,7 +299,7 @@ int PolyHookPlugin::getVTableIndex(void* pFunc) const {
// Check where it'd jump
addr += 5 /*size of the instruction*/ + *(uint32_t*)(addr + 1);

protector = std::make_unique<MemoryProtector>((uint64_t)addr, size, R, *(MemAccessor*)this);
protector = std::make_unique<MemoryProtector>((uintptr_t)addr, size, R, *(MemAccessor*)this);
}

bool ok = false;
Expand Down

0 comments on commit fd23a67

Please sign in to comment.