|
| 1 | +/// @file safetyhook/allocator.hpp |
| 2 | +/// @brief Allocator for allocating memory near target addresses. |
| 3 | + |
| 4 | +#pragma once |
| 5 | + |
| 6 | +#ifndef SAFETYHOOK_USE_CXXMODULES |
| 7 | +#include <cstdint> |
| 8 | +#include <expected> |
| 9 | +#include <memory> |
| 10 | +#include <mutex> |
| 11 | +#include <vector> |
| 12 | +#else |
| 13 | +import std.compat; |
| 14 | +#endif |
| 15 | + |
| 16 | +#include "safetyhook/common.hpp" |
| 17 | + |
| 18 | +namespace safetyhook { |
| 19 | +class Allocator; |
| 20 | + |
| 21 | +/// @brief A memory allocation. |
| 22 | +class SAFETYHOOK_API Allocation final { |
| 23 | +public: |
| 24 | + Allocation() = default; |
| 25 | + Allocation(const Allocation&) = delete; |
| 26 | + Allocation(Allocation&& other) noexcept; |
| 27 | + Allocation& operator=(const Allocation&) = delete; |
| 28 | + Allocation& operator=(Allocation&& other) noexcept; |
| 29 | + ~Allocation(); |
| 30 | + |
| 31 | + /// @brief Frees the allocation. |
| 32 | + /// @note This is called automatically when the Allocation object is destroyed. |
| 33 | + void free(); |
| 34 | + |
| 35 | + /// @brief Returns a pointer to the data of the allocation. |
| 36 | + /// @return Pointer to the data of the allocation. |
| 37 | + [[nodiscard]] uint8_t* data() const noexcept { return m_address; } |
| 38 | + |
| 39 | + /// @brief Returns the address of the allocation. |
| 40 | + /// @return The address of the allocation. |
| 41 | + [[nodiscard]] uintptr_t address() const noexcept { return reinterpret_cast<uintptr_t>(m_address); } |
| 42 | + |
| 43 | + /// @brief Returns the size of the allocation. |
| 44 | + /// @return The size of the allocation. |
| 45 | + [[nodiscard]] size_t size() const noexcept { return m_size; } |
| 46 | + |
| 47 | + /// @brief Tests if the allocation is valid. |
| 48 | + /// @return True if the allocation is valid, false otherwise. |
| 49 | + explicit operator bool() const noexcept { return m_address != nullptr && m_size != 0; } |
| 50 | + |
| 51 | +protected: |
| 52 | + friend Allocator; |
| 53 | + |
| 54 | + Allocation(std::shared_ptr<Allocator> allocator, uint8_t* address, size_t size) noexcept; |
| 55 | + |
| 56 | +private: |
| 57 | + std::shared_ptr<Allocator> m_allocator{}; |
| 58 | + uint8_t* m_address{}; |
| 59 | + size_t m_size{}; |
| 60 | +}; |
| 61 | + |
| 62 | +/// @brief Allocates memory near target addresses. |
| 63 | +class SAFETYHOOK_API Allocator final : public std::enable_shared_from_this<Allocator> { |
| 64 | +public: |
| 65 | + /// @brief Returns the global Allocator. |
| 66 | + /// @return The global Allocator. |
| 67 | + [[nodiscard]] static std::shared_ptr<Allocator> global(); |
| 68 | + |
| 69 | + /// @brief Creates a new Allocator. |
| 70 | + /// @return The new Allocator. |
| 71 | + [[nodiscard]] static std::shared_ptr<Allocator> create(); |
| 72 | + |
| 73 | + Allocator(const Allocator&) = delete; |
| 74 | + Allocator(Allocator&&) noexcept = delete; |
| 75 | + Allocator& operator=(const Allocator&) = delete; |
| 76 | + Allocator& operator=(Allocator&&) noexcept = delete; |
| 77 | + ~Allocator() = default; |
| 78 | + |
| 79 | + /// @brief The error type returned by the allocate functions. |
| 80 | + enum class Error : uint8_t { |
| 81 | + BAD_VIRTUAL_ALLOC, ///< VirtualAlloc failed. |
| 82 | + NO_MEMORY_IN_RANGE, ///< No memory in range. |
| 83 | + }; |
| 84 | + |
| 85 | + /// @brief Allocates memory. |
| 86 | + /// @param size The size of the allocation. |
| 87 | + /// @return The Allocation or an Allocator::Error if the allocation failed. |
| 88 | + [[nodiscard]] std::expected<Allocation, Error> allocate(size_t size); |
| 89 | + |
| 90 | + /// @brief Allocates memory near a target address. |
| 91 | + /// @param desired_addresses The target address. |
| 92 | + /// @param size The size of the allocation. |
| 93 | + /// @param max_distance The maximum distance from the target address. |
| 94 | + /// @return The Allocation or an Allocator::Error if the allocation failed. |
| 95 | + [[nodiscard]] std::expected<Allocation, Error> allocate_near( |
| 96 | + const std::vector<uint8_t*>& desired_addresses, size_t size, size_t max_distance = 0x7FFF'FFFF); |
| 97 | + |
| 98 | +protected: |
| 99 | + friend Allocation; |
| 100 | + |
| 101 | + void free(uint8_t* address, size_t size); |
| 102 | + |
| 103 | +private: |
| 104 | + struct FreeNode { |
| 105 | + std::unique_ptr<FreeNode> next{}; |
| 106 | + uint8_t* start{}; |
| 107 | + uint8_t* end{}; |
| 108 | + }; |
| 109 | + |
| 110 | + struct Memory { |
| 111 | + uint8_t* address{}; |
| 112 | + size_t size{}; |
| 113 | + std::unique_ptr<FreeNode> freelist{}; |
| 114 | + |
| 115 | + ~Memory(); |
| 116 | + }; |
| 117 | + |
| 118 | + std::vector<std::unique_ptr<Memory>> m_memory{}; |
| 119 | + std::mutex m_mutex{}; |
| 120 | + |
| 121 | + Allocator() = default; |
| 122 | + |
| 123 | + [[nodiscard]] std::expected<Allocation, Error> internal_allocate_near( |
| 124 | + const std::vector<uint8_t*>& desired_addresses, size_t size, size_t max_distance = 0x7FFF'FFFF); |
| 125 | + void internal_free(uint8_t* address, size_t size); |
| 126 | + |
| 127 | + static void combine_adjacent_freenodes(Memory& memory); |
| 128 | + [[nodiscard]] static std::expected<uint8_t*, Error> allocate_nearby_memory( |
| 129 | + const std::vector<uint8_t*>& desired_addresses, size_t size, size_t max_distance); |
| 130 | + [[nodiscard]] static bool in_range( |
| 131 | + uint8_t* address, const std::vector<uint8_t*>& desired_addresses, size_t max_distance); |
| 132 | +}; |
| 133 | +} // namespace safetyhook |
0 commit comments