Skip to content

Commit

Permalink
implement windows 64 bit
Browse files Browse the repository at this point in the history
  • Loading branch information
altalk23 committed May 26, 2024
1 parent 58dc814 commit 78a4aff
Show file tree
Hide file tree
Showing 12 changed files with 101 additions and 26 deletions.
19 changes: 14 additions & 5 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,17 @@ jobs:
fail-fast: false
matrix:
config:
- name: 'Windows'
id: win
- name: 'Windows (32-bit)'
id: win-32
arch: x86
os: windows-latest
build_tests: true
extra_flags: '-DCMAKE_BUILD_TYPE=Debug'
out_paths: './build/src/TulipHook.lib'

- name: 'Windows (64-bit)'
id: win-64
arch: x64
os: windows-latest
build_tests: true
extra_flags: '-DCMAKE_BUILD_TYPE=Debug'
Expand Down Expand Up @@ -65,8 +74,8 @@ jobs:
- name: Setup MSVC
uses: ilammy/[email protected]
with:
arch: x86
if: matrix.config.id == 'win'
arch: ${{ matrix.config.arch }}
if: matrix.config.id == 'win-32' || matrix.config.id == 'win-64'

# https://github.com/hendrikmuhs/ccache-action/pull/182
- name: Setup sccache
Expand All @@ -77,7 +86,7 @@ jobs:

- name: Install ninja-build tool
uses: seanmiddleditch/gha-setup-ninja@v3
if: matrix.config.id != 'win'
if: matrix.config.id != 'win-32' && matrix.config.id != 'win-64'

- name: Configure
shell: bash
Expand Down
10 changes: 8 additions & 2 deletions include/tulip/Platform.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,20 @@

// clang-format off

#if defined(WIN32) || defined(_WIN32) || defined(__WIN32) && !defined(__CYGWIN__)
#if defined(WIN32) || defined(_WIN32) || defined(__WIN32) && !defined(__CYGWIN__) || defined(WIN64) || defined(_WIN64) || defined(__WIN64) && !defined(__CYGWIN__)

#define TULIP_HOOK_WINDOWS 1
#define TULIP_HOOK_SUPPORTED_PLATFORM 1

#define TULIP_HOOK_DEFAULT_CONV __cdecl

#define TULIP_HOOK_X86 1
#if defined(WIN32) || defined(_WIN32) || defined(__WIN32) && !defined(__CYGWIN__)
#if defined(WIN64) || defined(_WIN64) || defined(__WIN64) && !defined(__CYGWIN__)
#define TULIP_HOOK_X64 1
#else
#define TULIP_HOOK_X86 1
#endif
#endif

#ifdef TULIP_HOOK_DYNAMIC
#ifdef TULIP_HOOK_EXPORTING
Expand Down
4 changes: 2 additions & 2 deletions include/tulip/platform/PlatformConvention.hpp
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
#include "../Platform.hpp"
#include "DefaultConvention.hpp"
#include "WindowsConvention.hpp"
#include "Windows32Convention.hpp"

namespace tulip::hook {
#if defined(TULIP_HOOK_WINDOWS)
#if defined(TULIP_HOOK_WINDOWS) && defined(TULIP_HOOK_X86)
using PlatformConvention = CdeclConvention;
#else
using PlatformConvention = DefaultConvention;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

#include "../Platform.hpp"

#ifdef TULIP_HOOK_WINDOWS
#if defined(TULIP_HOOK_WINDOWS) && defined(TULIP_HOOK_X86)

#include "../CallingConvention.hpp"

Expand Down
5 changes: 3 additions & 2 deletions src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,11 @@ if(WIN32)
file(GLOB TULIP_HOOK_PLATFORM_SOURCES
assembler/X86Assembler.cpp
assembler/X64Assembler.cpp
convention/WindowsConvention.cpp
convention/Windows32Convention.cpp
generator/X86Generator.cpp
generator/X64Generator.cpp
target/WindowsTarget.cpp
target/Windows32Target.cpp
target/Windows64Target.cpp
)
elseif(APPLE)
set(TULIP_HOOK_LINK_DOBBY On)
Expand Down
2 changes: 1 addition & 1 deletion src/Main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ Result<void*> tulip::hook::createReverseWrapper(void* address, WrapperMetadata c
std::shared_ptr<CallingConvention> tulip::hook::createConvention(TulipConvention convention) noexcept {
switch (convention) {
case TulipConvention::Default: return DefaultConvention::create();
#ifdef TULIP_HOOK_WINDOWS
#if defined(TULIP_HOOK_WINDOWS) && defined(TULIP_HOOK_X86)
case TulipConvention::Cdecl: return CdeclConvention::create();
case TulipConvention::Thiscall: return ThiscallConvention::create();
case TulipConvention::Fastcall: return FastcallConvention::create();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
#include <algorithm>
#include <iostream>
#include <optional>
#include <platform/WindowsConvention.hpp>
#include <platform/Windows32Convention.hpp>
#include <variant>

using namespace tulip::hook;
Expand Down
2 changes: 1 addition & 1 deletion src/target/PlatformTarget.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,4 @@
#include "MacosM1Target.hpp"
#include "PosixArmV7Target.hpp"
#include "PosixArmV8Target.hpp"
#include "WindowsTarget.hpp"
#include "Windows32Target.hpp"
20 changes: 10 additions & 10 deletions src/target/WindowsTarget.cpp → src/target/Windows32Target.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#include "WindowsTarget.hpp"
#include "Windows32Target.hpp"

#include <Platform.hpp>
#include <stdexcept>
Expand All @@ -10,7 +10,7 @@ using namespace tulip::hook;
#define WIN32_LEAN_AND_MEAN
#include <Windows.h>

Result<> WindowsTarget::allocatePage() {
Result<> Windows32Target::allocatePage() {
m_allocatedPage = VirtualAlloc(nullptr, 0x4000, MEM_COMMIT | MEM_RESERVE, PAGE_EXECUTE_READ);

if (!m_allocatedPage) {
Expand All @@ -23,7 +23,7 @@ Result<> WindowsTarget::allocatePage() {
return Ok();
}

Result<uint32_t> WindowsTarget::getProtection(void* address) {
Result<uint32_t> Windows32Target::getProtection(void* address) {
MEMORY_BASIC_INFORMATION information;

if (!VirtualQuery(address, &information, sizeof(MEMORY_BASIC_INFORMATION))) {
Expand All @@ -33,7 +33,7 @@ Result<uint32_t> WindowsTarget::getProtection(void* address) {
return Ok(information.Protect);
}

Result<> WindowsTarget::protectMemory(void* address, size_t size, uint32_t protection) {
Result<> Windows32Target::protectMemory(void* address, size_t size, uint32_t protection) {
DWORD oldProtection;

if (!VirtualProtect(address, size, protection, &oldProtection)) {
Expand All @@ -43,23 +43,23 @@ Result<> WindowsTarget::protectMemory(void* address, size_t size, uint32_t prote
return Ok();
}

Result<> WindowsTarget::rawWriteMemory(void* destination, void const* source, size_t size) {
Result<> Windows32Target::rawWriteMemory(void* destination, void const* source, size_t size) {
if (!WriteProcessMemory(GetCurrentProcess(), destination, source, size, nullptr)) {
return Err("Unable to write to memory");
}
return Ok();
}

uint32_t WindowsTarget::getWritableProtection() {
uint32_t Windows32Target::getWritableProtection() {
return PAGE_READWRITE;
}

Target& Target::get() {
static WindowsTarget ret;
static Windows32Target ret;
return ret;
}

Result<csh> WindowsTarget::openCapstone() {
Result<csh> Windows32Target::openCapstone() {
cs_err status;

status = cs_open(CS_ARCH_X86, CS_MODE_32, &m_capstone);
Expand All @@ -70,13 +70,13 @@ Result<csh> WindowsTarget::openCapstone() {
return Ok(m_capstone);
}

std::unique_ptr<HandlerGenerator> WindowsTarget::getHandlerGenerator(
std::unique_ptr<HandlerGenerator> Windows32Target::getHandlerGenerator(
void* address, void* trampoline, void* handler, void* content, void* wrapped, HandlerMetadata const& metadata
) {
return std::make_unique<X86HandlerGenerator>(address, trampoline, handler, content, wrapped, metadata);
}

std::unique_ptr<WrapperGenerator> WindowsTarget::getWrapperGenerator(void* address, WrapperMetadata const& metadata) {
std::unique_ptr<WrapperGenerator> Windows32Target::getWrapperGenerator(void* address, WrapperMetadata const& metadata) {
return std::make_unique<X86WrapperGenerator>(address, metadata);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
#include "Target.hpp"

namespace tulip::hook {
class WindowsTarget : public Target {
class Windows32Target : public Target {
public:
using Target::Target;

Expand Down
35 changes: 35 additions & 0 deletions src/target/Windows64Target.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
#include "Windows64Target.hpp"

#include <Platform.hpp>

using namespace tulip::hook;

#if defined(TULIP_HOOK_WINDOWS) && defined(TULIP_HOOK_X64)

Target& Target::get() {
static Windows64Target ret;
return ret;
}

Result<csh> Windows64Target::openCapstone() {
cs_err status;

status = cs_open(CS_ARCH_X86, CS_MODE_64, &m_capstone);
if (status != CS_ERR_OK) {
return Err("Couldn't open capstone");
}

return Ok(m_capstone);
}

std::unique_ptr<HandlerGenerator> Windows64Target::getHandlerGenerator(
void* address, void* trampoline, void* handler, void* content, void* wrapped, HandlerMetadata const& metadata
) {
return std::make_unique<X64HandlerGenerator>(address, trampoline, handler, content, wrapped, metadata);
}

std::unique_ptr<WrapperGenerator> Windows64Target::getWrapperGenerator(void* address, WrapperMetadata const& metadata) {
return std::make_unique<X64WrapperGenerator>(address, metadata);
}

#endif
24 changes: 24 additions & 0 deletions src/target/Windows64Target.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
#pragma once

#include <Platform.hpp>

#if defined(TULIP_HOOK_WINDOWS) && defined(TULIP_HOOK_X64)

#include "../generator/X86Generator.hpp"
#include "Windows32Target.hpp"

namespace tulip::hook {
class Windows64Target : public Windows32Target {
public:
using Target::Target;

Result<csh> openCapstone() override;

std::unique_ptr<HandlerGenerator> getHandlerGenerator(
void* address, void* trampoline, void* handler, void* content, void* wrapped, HandlerMetadata const& metadata
) override;
std::unique_ptr<WrapperGenerator> getWrapperGenerator(void* address, WrapperMetadata const& metadata) override;
};
}

#endif

0 comments on commit 78a4aff

Please sign in to comment.