Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/Cleanup preprocessor usage #61

Merged
merged 2 commits into from
Feb 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 18 additions & 19 deletions amalgamate.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,16 +14,16 @@
SAFETYHOOK_ROOT / 'include' / 'safetyhook',
]
INTERNAL_INCLUDE_PATHS = [SAFETYHOOK_ROOT / 'src']
INCLUDE_REGEXP = re.compile(r'^#\s*include\s*<((?:safety).*)>\s*$')
INCLUDE_REGEXP = re.compile(r'^#\s*include\s*"((?:safety).*)"\s*$')
OUTPUT_DIR = SAFETYHOOK_ROOT / 'amalgamated-dist'
FILE_HEADER = ['// DO NOT EDIT. This file is auto-generated by `amalgamate.py`.', '']


# Python versions before 3.10 don't have the root_dir argument for glob, so we
# crudely emulate it here.
def glob_in_dir(
pattern: str,
root_dir: Path,
pattern: str,
root_dir: Path,
):
cwd = os.getcwd()
root_dir = root_dir.resolve()
Expand All @@ -36,8 +36,8 @@ def glob_in_dir(


def find_include_path(
include: str,
search_paths: List[Path],
include: str,
search_paths: List[Path],
) -> Path:
for search_path in search_paths:
path = search_path / include
Expand All @@ -48,11 +48,11 @@ def find_include_path(


def merge_headers(
*,
header: str,
search_paths: List[Path],
covered_headers: Set[Path],
stack: List[str],
*,
header: str,
search_paths: List[Path],
covered_headers: Set[Path],
stack: List[str],
) -> List[str]:
# Locate and load header contents.
path = find_include_path(header, search_paths)
Expand All @@ -69,8 +69,8 @@ def merge_headers(
include_stack = []
if stack:
include_stack = [
'//',
'// Include stack:',
'//',
'// Include stack:',
*(f'// - {x}' for x in stack)
]

Expand All @@ -89,12 +89,12 @@ def merge_headers(
if not match:
filtered.append(line)
continue

# Recurse into includes.
filtered += merge_headers(
header=match.group(1),
header=match.group(1),
search_paths=search_paths,
covered_headers=covered_headers,
covered_headers=covered_headers,
stack=stack + [header],
)

Expand All @@ -105,7 +105,7 @@ def merge_sources(*, source_dir: Path, covered_headers: Set[Path]):
output = [
'#define NOMINMAX',
'',
'#include <safetyhook.hpp>',
'#include "safetyhook.hpp"',
'',
]

Expand Down Expand Up @@ -165,9 +165,9 @@ def main():
covered_headers = set()
with open(OUTPUT_DIR / 'safetyhook.hpp', 'w') as f:
f.write('\n'.join(FILE_HEADER + merge_headers(
header='safetyhook.hpp',
header='safetyhook.hpp',
search_paths=PUBLIC_INCLUDE_PATHS,
covered_headers=covered_headers,
covered_headers=covered_headers,
stack=[],
)))

Expand All @@ -182,4 +182,3 @@ def main():

if __name__ == '__main__':
main()

18 changes: 8 additions & 10 deletions example/midhook.cpp
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
#include <print>

#if __has_include(<Zydis/Zydis.h>)
#include <Zydis/Zydis.h>
#elif __has_include(<Zydis.h>)
#include <Zydis.h>
#if __has_include("Zydis/Zydis.h")
#include "Zydis/Zydis.h"
#elif __has_include("Zydis.h")
#include "Zydis.h"
#else
#error "Zydis not found"
#endif
Expand All @@ -15,9 +15,9 @@ __declspec(noinline) int add_42(int a) {
}

void hooked_add_42(SafetyHookContext& ctx) {
#ifdef _M_X64
#if SAFETYHOOK_ARCH_X86_64
ctx.rax = 1337;
#else
#elif SAFETYHOOK_ARCH_X86_32
ctx.eax = 1337;
#endif
}
Expand All @@ -30,12 +30,10 @@ int main() {
// Let's disassemble add_42 and hook its RET.
ZydisDecoder decoder{};

#if defined(_M_X64)
#if SAFETYHOOK_ARCH_X86_64
ZydisDecoderInit(&decoder, ZYDIS_MACHINE_MODE_LONG_64, ZYDIS_STACK_WIDTH_64);
#elif defined(_M_IX86)
#elif SAFETYHOOK_ARCH_X86_32
ZydisDecoderInit(&decoder, ZYDIS_MACHINE_MODE_LEGACY_32, ZYDIS_STACK_WIDTH_32);
#else
#error "Unsupported architecture"
#endif

auto ip = reinterpret_cast<uint8_t*>(add_42);
Expand Down
10 changes: 5 additions & 5 deletions include/safetyhook.hpp
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
#pragma once

#include <safetyhook/easy.hpp>
#include <safetyhook/inline_hook.hpp>
#include <safetyhook/mid_hook.hpp>
#include <safetyhook/thread_freezer.hpp>
#include <safetyhook/vmt_hook.hpp>
#include "safetyhook/easy.hpp"
#include "safetyhook/inline_hook.hpp"
#include "safetyhook/mid_hook.hpp"
#include "safetyhook/thread_freezer.hpp"
#include "safetyhook/vmt_hook.hpp"

using SafetyHookContext = safetyhook::Context;
using SafetyHookInline = safetyhook::InlineHook;
Expand Down
39 changes: 39 additions & 0 deletions include/safetyhook/common.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
#pragma once

#if defined(_MSC_VER)
#define SAFETYHOOK_COMPILER_MSVC 1
#define SAFETYHOOK_COMPILER_GCC 0
#define SAFETYHOOK_COMPILER_CLANG 0
#elif defined(__GNUC__)
#define SAFETYHOOK_COMPILER_MSVC 0
#define SAFETYHOOK_COMPILER_GCC 1
#define SAFETYHOOK_COMPILER_CLANG 0
#elif defined(__clang__)
#define SAFETYHOOK_COMPILER_MSVC 0
#define SAFETYHOOK_COMPILER_GCC 0
#define SAFETYHOOK_COMPILER_CLANG 1
#else
#error "Unsupported compiler"
#endif

#if SAFETYHOOK_COMPILER_MSVC
#if defined(_M_IX86)
#define SAFETYHOOK_ARCH_X86_32 1
#define SAFETYHOOK_ARCH_X86_64 0
#elif defined(_M_X64)
#define SAFETYHOOK_ARCH_X86_32 0
#define SAFETYHOOK_ARCH_X86_64 1
#else
#error "Unsupported architecture"
#endif
#elif SAFETYHOOK_COMPILER_GCC || SAFETYHOOK_COMPILER_CLANG
#if defined(__i386__)
#define SAFETYHOOK_ARCH_X86_32 1
#define SAFETYHOOK_ARCH_X86_64 0
#elif defined(__x86_64__)
#define SAFETYHOOK_ARCH_X86_32 0
#define SAFETYHOOK_ARCH_X86_64 1
#else
#error "Unsupported architecture"
#endif
#endif
6 changes: 4 additions & 2 deletions include/safetyhook/context.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@

#include <cstdint>

#include "safetyhook/common.hpp"

namespace safetyhook {
union Xmm {
uint8_t u8[16];
Expand Down Expand Up @@ -42,9 +44,9 @@ struct Context32 {
/// to the registers at the moment the hook is called.
/// @note The structure is different depending on architecture.
/// @note The structure only provides access to integer registers.
#ifdef _M_X64
#if SAFETYHOOK_ARCH_X86_64
using Context = Context64;
#else
#elif SAFETYHOOK_ARCH_X86_32
using Context = Context32;
#endif

Expand Down
8 changes: 4 additions & 4 deletions include/safetyhook/easy.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@

#pragma once

#include <safetyhook/inline_hook.hpp>
#include <safetyhook/mid_hook.hpp>
#include <safetyhook/utility.hpp>
#include <safetyhook/vmt_hook.hpp>
#include "safetyhook/inline_hook.hpp"
#include "safetyhook/mid_hook.hpp"
#include "safetyhook/utility.hpp"
#include "safetyhook/vmt_hook.hpp"

namespace safetyhook {
/// @brief Easy to use API for creating an InlineHook.
Expand Down
7 changes: 4 additions & 3 deletions include/safetyhook/inline_hook.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,9 @@
#include <utility>
#include <vector>

#include <safetyhook/allocator.hpp>
#include <safetyhook/utility.hpp>
#include "safetyhook/allocator.hpp"
#include "safetyhook/common.hpp"
#include "safetyhook/utility.hpp"

namespace safetyhook {
/// @brief An inline hook.
Expand Down Expand Up @@ -294,7 +295,7 @@ class InlineHook final {
const std::shared_ptr<Allocator>& allocator, uint8_t* target, uint8_t* destination);
std::expected<void, Error> e9_hook(const std::shared_ptr<Allocator>& allocator);

#ifdef _M_X64
#if SAFETYHOOK_ARCH_X86_64
std::expected<void, Error> ff_hook(const std::shared_ptr<Allocator>& allocator);
#endif

Expand Down
8 changes: 4 additions & 4 deletions include/safetyhook/mid_hook.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@
#include <cstdint>
#include <memory>

#include <safetyhook/allocator.hpp>
#include <safetyhook/context.hpp>
#include <safetyhook/inline_hook.hpp>
#include <safetyhook/utility.hpp>
#include "safetyhook/allocator.hpp"
#include "safetyhook/context.hpp"
#include "safetyhook/inline_hook.hpp"
#include "safetyhook/utility.hpp"

namespace safetyhook {

Expand Down
4 changes: 2 additions & 2 deletions include/safetyhook/vmt_hook.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@
#include <expected>
#include <unordered_map>

#include <safetyhook/allocator.hpp>
#include <safetyhook/utility.hpp>
#include "safetyhook/allocator.hpp"
#include "safetyhook/utility.hpp"

namespace safetyhook {
/// @brief A hook class that allows for hooking a single method in a VMT.
Expand Down
2 changes: 1 addition & 1 deletion src/allocator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
#error "Windows.h not found"
#endif

#include <safetyhook/allocator.hpp>
#include "safetyhook/allocator.hpp"

namespace safetyhook {
template <typename T> constexpr T align_up(T address, size_t align) {
Expand Down
2 changes: 1 addition & 1 deletion src/easy.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#include <safetyhook/easy.hpp>
#include "safetyhook/easy.hpp"

namespace safetyhook {
InlineHook create_inline(void* target, void* destination) {
Expand Down
Loading
Loading