Skip to content

Commit

Permalink
Refactor: Add *address() methods that return uintptr_t
Browse files Browse the repository at this point in the history
  • Loading branch information
cursey committed Apr 22, 2023
1 parent caaea53 commit 4e8dace
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 15 deletions.
6 changes: 5 additions & 1 deletion include/safetyhook/allocator.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,13 @@ class Allocation final {
/// @note This is called automatically when the Allocation object is destroyed.
void free();

/// @brief Returns a pointer to the data of the allocation.
/// @return Pointer to the data of the allocation.
[[nodiscard]] uint8_t* data() const noexcept { return m_address; }

/// @brief Returns the address of the allocation.
/// @return The address of the allocation.
[[nodiscard]] uint8_t* address() const noexcept { return m_address; }
[[nodiscard]] uintptr_t address() const noexcept { return (uintptr_t)m_address; }

/// @brief Returns the size of the allocation.
/// @return The size of the allocation.
Expand Down
12 changes: 10 additions & 2 deletions include/safetyhook/inline_hook.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -115,13 +115,21 @@ class InlineHook final {
/// @note This is called automatically in the destructor.
void reset();

/// @brief Get a pointer to the target.
/// @return A pointer to the target.
[[nodiscard]] uint8_t* target() const { return m_target; }

/// @brief Get the target address.
/// @return The target address.
[[nodiscard]] uint8_t* target() const { return m_target; }
[[nodiscard]] uintptr_t target_address() const { return reinterpret_cast<uintptr_t>(m_target); }

/// @brief Get a pointer ot the destination.
/// @return A pointer to the destination.
[[nodiscard]] uint8_t* destination() const { return m_destination; }

/// @brief Get the destination address.
/// @return The destination address.
[[nodiscard]] uint8_t* destination() const { return m_destination; }
[[nodiscard]] uintptr_t destination_address() const { return reinterpret_cast<uintptr_t>(m_destination); }

/// @brief Get the trampoline Allocation.
/// @return The trampoline Allocation.
Expand Down
15 changes: 7 additions & 8 deletions src/inline_hook.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -250,8 +250,7 @@ std::expected<void, InlineHook::Error> InlineHook::e9_hook(const std::shared_ptr

m_trampoline = std::move(*trampoline_allocation);

for (auto ip = m_target, tramp_ip = m_trampoline.address(); ip < m_target + m_original_bytes.size();
ip += ix.length) {
for (auto ip = m_target, tramp_ip = m_trampoline.data(); ip < m_target + m_original_bytes.size(); ip += ix.length) {
if (!decode(&ix, ip)) {
m_trampoline.free();
return std::unexpected{Error::failed_to_decode_instruction(ip)};
Expand Down Expand Up @@ -330,7 +329,7 @@ std::expected<void, InlineHook::Error> InlineHook::e9_hook(const std::shared_ptr
},
[this](uint32_t, HANDLE, CONTEXT& ctx) {
for (size_t i = 0; i < m_original_bytes.size(); ++i) {
fix_ip(ctx, m_target + i, m_trampoline.address() + i);
fix_ip(ctx, m_target + i, m_trampoline.data() + i);
}
});

Expand Down Expand Up @@ -367,10 +366,10 @@ std::expected<void, InlineHook::Error> InlineHook::ff_hook(const std::shared_ptr

m_trampoline = std::move(*trampoline_allocation);

std::copy(m_original_bytes.begin(), m_original_bytes.end(), m_trampoline.address());
std::copy(m_original_bytes.begin(), m_original_bytes.end(), m_trampoline.data());

const auto trampoline_epilogue = reinterpret_cast<TrampolineEpilogueFF*>(
m_trampoline.address() + m_trampoline_size - sizeof(TrampolineEpilogueFF));
const auto trampoline_epilogue =
reinterpret_cast<TrampolineEpilogueFF*>(m_trampoline.data() + m_trampoline_size - sizeof(TrampolineEpilogueFF));

// jmp from trampoline to original.
auto src = reinterpret_cast<uint8_t*>(&trampoline_epilogue->jmp_to_original);
Expand All @@ -388,7 +387,7 @@ std::expected<void, InlineHook::Error> InlineHook::ff_hook(const std::shared_ptr
},
[this](uint32_t, HANDLE, CONTEXT& ctx) {
for (size_t i = 0; i < m_original_bytes.size(); ++i) {
fix_ip(ctx, m_target + i, m_trampoline.address() + i);
fix_ip(ctx, m_target + i, m_trampoline.data() + i);
}
});

Expand All @@ -410,7 +409,7 @@ void InlineHook::destroy() {
},
[this](uint32_t, HANDLE, CONTEXT& ctx) {
for (size_t i = 0; i < m_original_bytes.size(); ++i) {
fix_ip(ctx, m_trampoline.address() + i, m_target + i);
fix_ip(ctx, m_trampoline.data() + i, m_target + i);
}
});

Expand Down
8 changes: 4 additions & 4 deletions src/mid_hook.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -81,10 +81,10 @@ std::expected<void, MidHook::Error> MidHook::setup(

m_stub = std::move(*stub_allocation);

std::copy_n(asm_data, sizeof(asm_data), reinterpret_cast<uint8_t*>(m_stub.address()));
std::copy_n(asm_data, sizeof(asm_data), m_stub.data());

#ifdef _M_X64
store(m_stub.address() + sizeof(asm_data) - 16, m_destination);
store(m_stub.data() + sizeof(asm_data) - 16, m_destination);
#else
store(m_stub.address() + sizeof(asm_data) - 8, m_destination);

Expand All @@ -93,7 +93,7 @@ std::expected<void, MidHook::Error> MidHook::setup(
store(m_stub.address() + 0x1C + 2, m_stub.address() + sizeof(asm_data) - 4);
#endif

auto hook_result = InlineHook::create(allocator, m_target, m_stub.address());
auto hook_result = InlineHook::create(allocator, m_target, m_stub.data());

if (!hook_result) {
m_stub.free();
Expand All @@ -103,7 +103,7 @@ std::expected<void, MidHook::Error> MidHook::setup(
m_hook = std::move(*hook_result);

#ifdef _M_X64
store(m_stub.address() + sizeof(asm_data) - 8, m_hook.trampoline().address());
store(m_stub.data() + sizeof(asm_data) - 8, m_hook.trampoline().data());
#else
store(m_stub.address() + sizeof(asm_data) - 4, m_hook.trampoline().address());
#endif
Expand Down

0 comments on commit 4e8dace

Please sign in to comment.