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

Remove FnPtr and use templates instead #83

Merged
merged 2 commits into from
Dec 19, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
2 changes: 1 addition & 1 deletion format.ps1
Original file line number Diff line number Diff line change
@@ -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
Expand Down
9 changes: 5 additions & 4 deletions include/safetyhook/easy.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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 <typename T>
[[nodiscard]] InlineHook create_inline(T target, T destination, InlineHook::Flags flags = InlineHook::Default) {
return create_inline(reinterpret_cast<void*>(target), reinterpret_cast<void*>(destination), flags);
}

Expand All @@ -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 <typename T>
[[nodiscard]] MidHook create_mid(T target, MidHookFn destination, MidHook::Flags flags = MidHook::Default) {
return create_mid(reinterpret_cast<void*>(target), destination, flags);
}

Expand All @@ -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 <typename T> [[nodiscard]] VmHook create_vm(VmtHook& vmt, size_t index, T destination) {
if (auto hook = vmt.hook_method(index, destination)) {
return std::move(*hook);
} else {
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 @@ -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<InlineHook, Error> create(
FnPtr auto target, FnPtr auto destination, Flags flags = Default) {
template <typename T>
[[nodiscard]] static std::expected<InlineHook, Error> create(T target, T destination, Flags flags = Default) {
return create(reinterpret_cast<void*>(target), reinterpret_cast<void*>(destination), flags);
}

Expand All @@ -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 <typename T>
[[nodiscard]] static std::expected<InlineHook, Error> create(
const std::shared_ptr<Allocator>& allocator, FnPtr auto target, FnPtr auto destination, Flags flags = Default) {
const std::shared_ptr<Allocator>& allocator, T target, T destination, Flags flags = Default) {
return create(allocator, reinterpret_cast<void*>(target), reinterpret_cast<void*>(destination), flags);
}

Expand Down
8 changes: 5 additions & 3 deletions include/safetyhook/mid_hook.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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 <typename T>
[[nodiscard]] static std::expected<MidHook, Error> create(
FnPtr auto target, MidHookFn destination_fn, Flags flags = Default) {
T target, MidHookFn destination_fn, Flags flags = Default) {
return create(reinterpret_cast<void*>(target), destination_fn, flags);
}

Expand All @@ -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<MidHook, Error> create(const std::shared_ptr<Allocator>& allocator,
FnPtr auto target, MidHookFn destination_fn, Flags flags = Default) {
template <typename T>
[[nodiscard]] static std::expected<MidHook, Error> create(
const std::shared_ptr<Allocator>& allocator, T target, MidHookFn destination_fn, Flags flags = Default) {
return create(allocator, reinterpret_cast<void*>(target), destination_fn, flags);
}

Expand Down
5 changes: 1 addition & 4 deletions include/safetyhook/utility.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,17 +14,14 @@ template <typename T> constexpr void store(uint8_t* address, const T& value) {
std::copy_n(reinterpret_cast<const uint8_t*>(&value), sizeof(T), address);
}

template <typename T> constexpr T address_cast(auto address) {
template <typename T, typename U> constexpr T address_cast(U address) {
if constexpr (std::is_integral_v<T> && std::is_integral_v<decltype(address)>) {
angelfor3v3r marked this conversation as resolved.
Show resolved Hide resolved
return static_cast<T>(address);
} else {
return reinterpret_cast<T>(address);
}
}

template <typename T>
concept FnPtr = requires(T f) { std::is_pointer_v<T>&& std::is_function_v<std::remove_pointer_t<T>>; };

bool is_executable(uint8_t* address);

class UnprotectMemory {
Expand Down
2 changes: 1 addition & 1 deletion include/safetyhook/vmt_hook.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<VmHook, Error> hook_method(size_t index, FnPtr auto new_function) {
template <typename T> [[nodiscard]] std::expected<VmHook, Error> hook_method(size_t index, T new_function) {
VmHook hook{};

++index; // Skip RTTI pointer.
Expand Down
3 changes: 2 additions & 1 deletion test/main.cpp
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
#include <boost/ut.hpp>

int main() {}
int main() {
}