From 3d0f3786a6dd57306dc02b1cfefe72c098880601 Mon Sep 17 00:00:00 2001 From: angelfor3v3r Date: Thu, 19 Dec 2024 15:15:45 -0500 Subject: [PATCH] Remove `FnPtr` and use templates instead (#83) --- format.ps1 | 2 +- include/safetyhook/easy.hpp | 9 +++++---- include/safetyhook/inline_hook.hpp | 7 ++++--- include/safetyhook/mid_hook.hpp | 8 +++++--- include/safetyhook/utility.hpp | 7 ++----- include/safetyhook/vmt_hook.hpp | 2 +- test/main.cpp | 3 ++- 7 files changed, 20 insertions(+), 18 deletions(-) diff --git a/format.ps1 b/format.ps1 index 4df26f9..019b72d 100644 --- a/format.ps1 +++ b/format.ps1 @@ -1,4 +1,4 @@ -Get-ChildItem -Path .\src, .\include, .\tests, .\unittest -Include *.hpp, *.cpp -Recurse | +Get-ChildItem -Path .\src, .\include, .\test, .\example -Include *.hpp, *.cpp -Recurse | ForEach-Object { Write-Output $_.FullName &clang-format -i -style=file $_.FullName diff --git a/include/safetyhook/easy.hpp b/include/safetyhook/easy.hpp index 080a2a8..e28e15a 100644 --- a/include/safetyhook/easy.hpp +++ b/include/safetyhook/easy.hpp @@ -21,8 +21,8 @@ namespace safetyhook { /// @param destination The address of the destination function. /// @param flags The flags to use. /// @return The InlineHook object. -[[nodiscard]] InlineHook create_inline( - FnPtr auto target, FnPtr auto destination, InlineHook::Flags flags = InlineHook::Default) { +template +[[nodiscard]] InlineHook create_inline(T target, T destination, InlineHook::Flags flags = InlineHook::Default) { return create_inline(reinterpret_cast(target), reinterpret_cast(destination), flags); } @@ -38,7 +38,8 @@ namespace safetyhook { /// @param destination The destination function. /// @param flags The flags to use. /// @return The MidHook object. -[[nodiscard]] MidHook create_mid(FnPtr auto target, MidHookFn destination, MidHook::Flags flags = MidHook::Default) { +template +[[nodiscard]] MidHook create_mid(T target, MidHookFn destination, MidHook::Flags flags = MidHook::Default) { return create_mid(reinterpret_cast(target), destination, flags); } @@ -52,7 +53,7 @@ namespace safetyhook { /// @param index The index of the method to hook. /// @param destination The destination function. /// @return The VmHook object. -[[nodiscard]] VmHook create_vm(VmtHook& vmt, size_t index, FnPtr auto destination) { +template [[nodiscard]] VmHook create_vm(VmtHook& vmt, size_t index, T destination) { if (auto hook = vmt.hook_method(index, destination)) { return std::move(*hook); } else { diff --git a/include/safetyhook/inline_hook.hpp b/include/safetyhook/inline_hook.hpp index cb7e2be..becb89d 100644 --- a/include/safetyhook/inline_hook.hpp +++ b/include/safetyhook/inline_hook.hpp @@ -110,8 +110,8 @@ class InlineHook final { /// @return The InlineHook or an InlineHook::Error if an error occurred. /// @note This will use the default global Allocator. /// @note If you don't care about error handling, use the easy API (safetyhook::create_inline). - [[nodiscard]] static std::expected create( - FnPtr auto target, FnPtr auto destination, Flags flags = Default) { + template + [[nodiscard]] static std::expected create(T target, T destination, Flags flags = Default) { return create(reinterpret_cast(target), reinterpret_cast(destination), flags); } @@ -132,8 +132,9 @@ class InlineHook final { /// @param flags The flags to use. /// @return The InlineHook or an InlineHook::Error if an error occurred. /// @note If you don't care about error handling, use the easy API (safetyhook::create_inline). + template [[nodiscard]] static std::expected create( - const std::shared_ptr& allocator, FnPtr auto target, FnPtr auto destination, Flags flags = Default) { + const std::shared_ptr& allocator, T target, T destination, Flags flags = Default) { return create(allocator, reinterpret_cast(target), reinterpret_cast(destination), flags); } diff --git a/include/safetyhook/mid_hook.hpp b/include/safetyhook/mid_hook.hpp index 7b2fb79..9678ffb 100644 --- a/include/safetyhook/mid_hook.hpp +++ b/include/safetyhook/mid_hook.hpp @@ -75,8 +75,9 @@ class MidHook final { /// @return The MidHook object or a MidHook::Error if an error occurred. /// @note This will use the default global Allocator. /// @note If you don't care about error handling, use the easy API (safetyhook::create_mid). + template [[nodiscard]] static std::expected create( - FnPtr auto target, MidHookFn destination_fn, Flags flags = Default) { + T target, MidHookFn destination_fn, Flags flags = Default) { return create(reinterpret_cast(target), destination_fn, flags); } @@ -98,8 +99,9 @@ class MidHook final { /// @param flags The flags to use. /// @return The MidHook object or a MidHook::Error if an error occurred. /// @note If you don't care about error handling, use the easy API (safetyhook::create_mid). - [[nodiscard]] static std::expected create(const std::shared_ptr& allocator, - FnPtr auto target, MidHookFn destination_fn, Flags flags = Default) { + template + [[nodiscard]] static std::expected create( + const std::shared_ptr& allocator, T target, MidHookFn destination_fn, Flags flags = Default) { return create(allocator, reinterpret_cast(target), destination_fn, flags); } diff --git a/include/safetyhook/utility.hpp b/include/safetyhook/utility.hpp index 9b24dcb..fe45580 100644 --- a/include/safetyhook/utility.hpp +++ b/include/safetyhook/utility.hpp @@ -14,17 +14,14 @@ template constexpr void store(uint8_t* address, const T& value) { std::copy_n(reinterpret_cast(&value), sizeof(T), address); } -template constexpr T address_cast(auto address) { - if constexpr (std::is_integral_v && std::is_integral_v) { +template constexpr T address_cast(U address) { + if constexpr (std::is_integral_v && std::is_integral_v) { return static_cast(address); } else { return reinterpret_cast(address); } } -template -concept FnPtr = requires(T f) { std::is_pointer_v&& std::is_function_v>; }; - bool is_executable(uint8_t* address); class UnprotectMemory { diff --git a/include/safetyhook/vmt_hook.hpp b/include/safetyhook/vmt_hook.hpp index 5cf2b11..bc3daf0 100644 --- a/include/safetyhook/vmt_hook.hpp +++ b/include/safetyhook/vmt_hook.hpp @@ -141,7 +141,7 @@ class VmtHook final { /// @brief Hooks a method in the VMT. /// @param index The index of the method to hook. /// @param new_function The new function to use. - [[nodiscard]] std::expected hook_method(size_t index, FnPtr auto new_function) { + template [[nodiscard]] std::expected hook_method(size_t index, T new_function) { VmHook hook{}; ++index; // Skip RTTI pointer. diff --git a/test/main.cpp b/test/main.cpp index a11de10..14d6345 100644 --- a/test/main.cpp +++ b/test/main.cpp @@ -1,3 +1,4 @@ #include -int main() {} \ No newline at end of file +int main() { +} \ No newline at end of file