Skip to content

Commit

Permalink
Replace uintptr_t by uint64_t
Browse files Browse the repository at this point in the history
  • Loading branch information
qubka committed Oct 11, 2024
1 parent ed72446 commit 906dd61
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;
}

uintptr_t PLH::Callback::getJitFunc(const asmjit::FuncSignature& sig, const asmjit::Arch arch, const CallbackEntry pre, const CallbackEntry post) {
uint64_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 @@ uintptr_t PLH::Callback::getJitFunc(const asmjit::FuncSignature& sig, const asmj
}

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

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

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

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

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

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

// create buffer for property struct
asmjit::x86::Mem propStack = cc.newStack(sizeof(uintptr_t), 16);
asmjit::x86::Mem propStack = cc.newStack(sizeof(uint64_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(uintptr_t), 16);
asmjit::x86::Mem retStack = cc.newStack(sizeof(uint64_t), 16);
asmjit::x86::Gp retStruct = cc.newUIntPtr("retStruct");
cc.lea(retStruct, retStack);

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

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

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

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

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

cc.bind(noPost);

if (sig.hasRet()) {
asmjit::x86::Mem retStackIdx(retStack);
retStackIdx.setSize(sizeof(uintptr_t));
retStackIdx.setSize(sizeof(uint64_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 @@ uintptr_t PLH::Callback::getJitFunc(const asmjit::FuncSignature& sig, const asmj
return m_functionPtr;
}

uintptr_t PLH::Callback::getJitFunc(const DataType retType, const std::vector<DataType>& paramTypes, const CallbackEntry pre, const CallbackEntry post) {
uint64_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) };
}

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

uintptr_t* PLH::Callback::getFunctionHolder() {
uint64_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 uintptr_t m_arguments;
volatile uint64_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(uintptr_t) * idx;
return (char*) &m_arguments + sizeof(uint64_t) * idx;
}
};

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

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

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 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* getTrampolineHolder();
uintptr_t* getFunctionHolder();
uint64_t* getTrampolineHolder();
uint64_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;
uintptr_t m_functionPtr = 0;
uint64_t m_functionPtr = 0;
union {
uintptr_t m_trampolinePtr = 0;
uint64_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;
}

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

auto detour = std::make_unique<NatDetour>((uintptr_t)pFunc, JIT, callback->getTrampolineHolder());
auto detour = std::make_unique<NatDetour>((uint64_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;
}

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

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

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

uintptr_t origVFunc = origVFuncs[index];
uint64_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>((uintptr_t) pClass, redirectMap, &origVFuncs);
vtable = std::make_unique<VTableSwapHook>((uint64_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((uintptr_t)pFunc, size, R, *(MemAccessor*)this);
MemoryProtector protector((uint64_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>((uintptr_t)addr, size, R, *(MemAccessor*)this);
protector = std::make_unique<MemoryProtector>((uint64_t)addr, size, R, *(MemAccessor*)this);
}

bool ok = false;
Expand Down

0 comments on commit 906dd61

Please sign in to comment.