CommonLib
Loading...
Searching...
No Matches
Memory.h
Go to the documentation of this file.
1#pragma once
2
3#include <array>
4#include <vector>
5
6#define ASLR(ADDRESS) (hedgedev::csl::mem::ToASLR((void*)(ADDRESS)))
7
8#define IS_NOP(ADDRESS) (hedgedev::csl::mem::IsNop((void*)(ADDRESS)))
9
10#define READ(ADDRESS, TYPE) (hedgedev::csl::mem::Read<TYPE>((void*)(ADDRESS)))
11#define READ_ARRAY(ADDRESS, TYPE, COUNT) (hedgedev::csl::mem::Read<TYPE, COUNT>((void*)(ADDRESS)))
12#define READ_INSTR_ADDRESS(ADDRESS, TYPE, OFFSET, STRIDE) (hedgedev::csl::mem::ReadInstructionAddress<TYPE>((void*)(ADDRESS), OFFSET, STRIDE))
13#define READ_CALL(ADDRESS) (hedgedev::csl::mem::ReadCall((void*)(ADDRESS)))
14#define READ_JUMP(ADDRESS) (hedgedev::csl::mem::ReadJump((void*)(ADDRESS)))
15
16#define WRITE(ADDRESS, TYPE, ...) (hedgedev::csl::mem::Write<TYPE>((void*)(ADDRESS), { __VA_ARGS__ }))
17#define WRITE_ARRAY(ADDRESS, TYPE, DATA) (hedgedev::csl::mem::Write<TYPE>((void*)(ADDRESS), DATA))
18#define WRITE_CALL(ADDRESS, DESTINATION) (hedgedev::csl::mem::WriteCall((void*)(ADDRESS), (void*)(DESTINATION)))
19#define WRITE_JUMP(ADDRESS, DESTINATION) (hedgedev::csl::mem::WriteJump((void*)(ADDRESS), (void*)(DESTINATION)))
20#define WRITE_NOP(ADDRESS, LENGTH) (hedgedev::csl::mem::WriteNop((void*)(ADDRESS), LENGTH))
21#define WRITE_STRING(ADDRESS, STR) (hedgedev::csl::mem::WriteString((void*)(ADDRESS), STR))
22#define WRITE_STRING_FIXED(ADDRESS, STR, ...) (hedgedev::csl::mem::WriteStringFixedLength((void*)(ADDRESS), STR, __VA_ARGS__))
23
24namespace hedgedev::csl::mem
25{
44
45 enum class BranchType;
46 enum class BranchDistance;
47 enum class BranchCondition;
48
58
59 ///
60 /// Gets the original base address of the current module.
61 ///
62 inline void* GetOriginalModuleBase();
63
64 ///
65 /// Gets the system flags for the specified page protection.
66 ///
67 inline uint32_t GetProtectionFlags(PageProtection in_protection);
68
69 ///
70 /// Sets the protection of a page of memory.
71 ///
72 inline bool Protect(void* in_pAddress, size_t in_length, uint32_t in_newProtectionFlags, uint32_t* out_pOldProtectionFlags = nullptr);
73
74 ///
75 /// Transforms a virtual address to the current module's ASLR base.
76 ///
77 /// \param in_pAddress The address to transform.
78 /// \param in_pBaseAddress The original base address of the module containing the address (optional).
79 ///
80 /// \returns The input address transformed into the ASLR base.
81 ///
82 inline void* ToASLR(void* in_pAddress, void* in_pBaseAddress = nullptr);
83
84 ///
85 /// Transforms a virtual address from the current module's ASLR base.
86 ///
87 /// \param in_pAddress The address to transform.
88 /// \param in_pBaseAddress The original base address of the module containing the address (optional).
89 ///
90 /// \returns The input address transformed from the ASLR base.
91 ///
92 inline void* FromASLR(void* in_pAddress, void* in_pBaseAddress = nullptr);
93
94 ///
95 /// Checks if the instruction at the given memory address is a no-operation (NOP) instruction.
96 ///
97 /// \param in_pAddress The address to check.
98 ///
99 /// \returns `true` if the instruction is no-operation (NOP). Otherwise, `false`.
100 ///
101 inline bool IsNop(void* in_pAddress);
102
103 ///
104 /// Reads a value in memory.
105 ///
106 /// \tparam T The type to read.
107 ///
108 /// \param in_pAddress The address to read from.
109 ///
110 /// \returns An instance of the type read from memory.
111 ///
112 template <typename T>
113 inline T Read(void* in_pAddress);
114
115 ///
116 /// Reads an array of values in memory.
117 ///
118 /// \tparam T The type to read.
119 /// \tparam Count The total number of values to read.
120 ///
121 /// \param in_pAddress The address to read from.
122 ///
123 /// \returns An array of instances of the type read from memory.
124 ///
125 template <typename T, size_t Count>
126 inline std::array<T, Count> Read(void* in_pAddress);
127
128 ///
129 /// Gets information about a branch instruction in memory.
130 ///
131 /// \param in_pAddress The address of the branch instruction.
132 ///
133 /// \returns A structure containing information about the branch.
134 ///
135 inline BranchInfo GetBranchInfo(void* in_pAddress);
136
137 ///
138 /// Reads the address of an instruction in memory.
139 ///
140 /// \tparam T The type to read.
141 ///
142 /// \param in_pAddress The address of the instruction to read.
143 /// \param in_offset The offset of the address after \ref in_pAddress.
144 /// \param in_stride The length of the instruction that was read.
145 ///
146 /// \returns The address referenced by the instruction.
147 ///
148 template <typename T>
149 inline void* ReadInstructionAddress(void* in_pAddress, size_t in_offset, size_t in_stride);
150
151 ///
152 /// Reads the address of a call instruction in memory.
153 ///
154 /// \param in_pAddress The address of the call instruction to read.
155 ///
156 /// \returns The address referenced by the call instruction.
157 ///
158 inline void* ReadCall(void* in_pAddress);
159
160 ///
161 /// Reads the address of a jump instruction in memory.
162 ///
163 /// \param in_pAddress The address of the jump instruction to read.
164 ///
165 /// \returns The address referenced by the jump instruction.
166 ///
167 inline void* ReadJump(void* in_pAddress);
168
169 ///
170 /// Writes a value in memory.
171 ///
172 /// \tparam T The type to write.
173 ///
174 /// \param in_pAddress The address to write to.
175 /// \param in_rData The data to write.
176 /// \param in_count The total number of values to write.
177 ///
178 template <typename T>
179 inline bool Write(void* in_pAddress, const T& in_rData, size_t in_count = 1);
180
181 ///
182 /// Writes an array of values in memory.
183 ///
184 /// \tparam T The type to write.
185 ///
186 /// \param in_pAddress The address to write to.
187 /// \param in_rData The collection of values to write.
188 ///
189 template <typename T>
190 inline bool Write(void* in_pAddress, const std::vector<T>& in_rData);
191
192 ///
193 /// Writes a call instruction in memory.
194 ///
195 /// \param in_pAddress The address to write to.
196 /// \param in_pDestination The address of the function to call.
197 ///
198 inline bool WriteCall(void* in_pAddress, void* in_pDestination);
199
200 ///
201 /// Writes a jump instruction in memory.
202 ///
203 /// \param in_pAddress The address to write to.
204 /// \param in_pDestination The address of the address to jump to.
205 /// \param in_isCall Determines whether the jump should be encoded as a call instruction.
206 ///
207 inline bool WriteJump(void* in_pAddress, void* in_pDestination, bool in_isCall = false);
208
209 ///
210 /// Writes a no-operation (NOP) instruction in memory.
211 ///
212 /// \param in_pAddress The address to write to.
213 /// \param in_count The total number of no-operation (NOP) instructions to write.
214 ///
215 inline bool WriteNop(void* in_pAddress, size_t in_count = 1);
216
217 ///
218 /// Writes a string in memory.
219 ///
220 /// \param in_pAddress The address to write to.
221 /// \param in_rStr The string to write.
222 ///
223 template <hedgedev::csl::ut::expr::AnyString T>
224 inline bool WriteString(void* in_pAddress, const T& in_rStr);
225
226 ///
227 /// Writes a string of fixed length in memory.
228 ///
229 /// \param in_pAddress The address to write to.
230 /// \param in_rStr The string to write.
231 /// \param in_length The length of the string to write.
232 /// If zero, the length will be determined by the length of an existing
233 /// string located at \ref in_pAddress.
234 ///
235 template <hedgedev::csl::ut::expr::AnyString T>
236 inline bool WriteStringFixedLength(void* in_pAddress, const T& in_rStr, size_t in_length = 0);
237}
238
239#include "Memory.inl"
240
241#if defined(CMNLIB_X64) || defined(CMNLIB_X86)
242#include "x86/Memory.inl"
243#endif
244
245#ifdef WIN32
246#include "Win32/Memory.inl"
247#endif
#define __CMNLIB_INTERNAL_MAKE_NAMESPACE_ALIAS(PARENT, NAME, ALIAS)
Definition CommonLib.h:6
#define ASSERT_RETURN(CONDITION, RETURN_VALUE)
Definition Preprocessor.h:17
Definition StringTypes.h:89
Definition StringTypes.h:83
Definition StringTypes.h:77
Definition StringTypes.h:59
Definition StringTypes.h:65
Definition StringTypes.h:71
bool IsNeighbour(const std::filesystem::path &in_rPath)
Definition ThisProcess.inl:10
std::filesystem::path GetExecutableRoot()
Definition ThisProcess.inl:5
Definition ThisProcess.inl:4
Definition Memory.h:25
void * ToASLR(void *in_pAddress, void *in_pBaseAddress=nullptr)
Definition Memory.inl:52
bool Write(void *in_pAddress, const std::vector< T > &in_rData)
Definition Memory.inl:41
void * FromASLR(void *in_pAddress, void *in_pBaseAddress=nullptr)
Definition Memory.inl:60
bool Write(void *in_pAddress, const T &in_rData, size_t in_count=1)
Definition Memory.inl:23
std::array< T, Count > Read(void *in_pAddress)
Definition Memory.inl:12
PageProtection
Definition Memory.h:27
@ X
Definition Memory.h:39
@ Write
Definition Memory.h:31
@ Execute
Definition Memory.h:32
@ RWX
Definition Memory.h:42
@ RW
Definition Memory.h:40
@ W
Definition Memory.h:38
@ ReadWriteExecute
Definition Memory.h:35
@ ReadWrite
Definition Memory.h:33
@ Read
Definition Memory.h:30
@ ReadExecute
Definition Memory.h:34
@ NoAccess
Definition Memory.h:28
@ R
Definition Memory.h:37
@ RX
Definition Memory.h:41
BranchType
Definition Memory.inl:4
bool IsNop(void *in_pAddress)
Definition Memory.inl:37
void * ReadInstructionAddress(void *in_pAddress, size_t in_offset, size_t in_stride)
Definition Memory.inl:124
bool WriteNop(void *in_pAddress, size_t in_count=1)
Definition Memory.inl:182
bool WriteCall(void *in_pAddress, void *in_pDestination)
Definition Memory.inl:142
bool WriteString(void *in_pAddress, const T &in_rStr)
Definition Memory.inl:58
BranchCondition
Definition Memory.inl:18
bool WriteJump(void *in_pAddress, void *in_pDestination, bool in_isCall=false)
Definition Memory.inl:147
bool WriteStringFixedLength(void *in_pAddress, const T &in_rStr, size_t in_length=0)
Definition Memory.inl:69
BranchDistance
Definition Memory.inl:11
BranchInfo GetBranchInfo(void *in_pAddress)
Definition Memory.inl:42
bool Protect(void *in_pAddress, size_t in_length, uint32_t in_newProtectionFlags, uint32_t *out_pOldProtectionFlags=nullptr)
Definition Memory.inl:47
void * GetOriginalModuleBase()
Definition Memory.inl:6
void * ReadCall(void *in_pAddress)
Definition Memory.inl:132
void * ReadJump(void *in_pAddress)
Definition Memory.inl:137
uint32_t GetProtectionFlags(PageProtection in_protection)
Definition Memory.inl:18
T Read(void *in_pAddress)
Definition Memory.inl:6
constexpr bool HasFlag(T in_mask, T in_flag)
Definition Expressions.inl:4
constexpr bool IsSameUnderlyingCharType
Definition StringTypes.h:95
std::tuple_element_t< Index, std::tuple< TArgs... > > GetPackType
Definition Expressions.h:9
constexpr bool IsBasicString_v
Definition StringTypes.h:47
constexpr TDst CreateInferredString(const TSrc &in_rStr)
Definition StringTypes.inl:4
constexpr size_t GetStringTypePrecedence()
Definition StringTypes.inl:12
constexpr bool IsBasicStringView_v
Definition StringTypes.h:53
constexpr bool IsAllSame
Definition Expressions.h:15
Definition FileSystem.inl:4
std::error_code TruncateFilesByAge(const std::filesystem::path &in_rPath, std::string_view in_extension, size_t in_max)
Definition FileSystem.inl:44
std::error_code TruncateFiles(const std::filesystem::path &in_rPath, std::string_view in_extension, size_t in_max, T &&in_rrCompare)
Definition FileSystem.inl:11
bool IsNeighbour(const std::filesystem::path &in_rPathA, const std::filesystem::path &in_rPathB)
Definition FileSystem.inl:5
std::filesystem::path FindInEnvironment(const std::filesystem::path &in_rFileName)
Definition FileSystem.inl:3
Definition String.h:8
size_t GetWidth(const TString &in_rStr)
Definition String.inl:133
bool Contains(const TString &in_rStr, const TSubString &in_rSubStr, bool in_isCaseSensitive=true)
Definition String.inl:26
TString ToHex(TValue in_value, size_t in_maxLength=sizeof(size_t) *2, bool in_prefix=true)
Definition String.inl:427
std::vector< TOut > Split(const TString &in_rStr, const TDelimiter &in_rDelimiter)
Definition String.inl:283
std::array< TOut, sizeof...(TArgs)> PrecedentConvert(const TArgs &... in_rArgs)
Definition String.inl:228
expr::InferredString< TString > Escape(const TString &in_rStr, TChar in_searchChar, TChar in_escapeChar=TChar('\\'))
Definition String.inl:74
expr::InferredString< TString > TrimEnd(const TString &in_rStr, std::optional< std::vector< TChar > > in_trimChars={})
Definition String.inl:361
TOut Pad(const TString &in_rStr, const TPadString &in_rPadStr)
Definition String.inl:203
TOut Parse(const TString &in_rStr)
Definition String.inl:218
TOut Hyperlink(const TString &in_rStr, const TUrl &in_rUrl)
Definition String.inl:147
expr::InferredString< TCollection > Join(const TDelimiter &in_rDelimiter, const std::vector< TCollection > &in_rStrings)
Definition String.inl:167
bool TryParse(const TString &in_rStr, TOut &out_rValue)
Definition String.inl:516
TOut Join(const TDelimiter &in_rDelimiter, const TArgs &... in_rArgs)
Definition String.inl:195
bool Compare(const TLeft &in_rLeft, const TRight &in_rRight, bool in_isCaseSensitive=true)
Definition String.inl:9
expr::InferredString< TString > Truncate(const TString &in_rStr, size_t in_maxLength, bool in_truncateEnd=true, bool in_ellipsis=true)
Definition String.inl:387
expr::InferredString< TString > RemoveXmlTags(const TString &in_rStr)
Definition String.inl:275
expr::InferredString< TString > Wrap(const TString &in_rStr, size_t in_maxWidth)
Definition String.inl:546
expr::InferredString< TString > ToLower(const TString &in_rStr)
Definition String.inl:314
expr::InferredString< TString > Format(const TString in_str,...)
Definition String.inl:90
expr::InferredString< TString > TrimStart(const TString &in_rStr, std::optional< std::vector< TChar > > in_trimChars={})
Definition String.inl:342
bool TryConvert(const TSrc &in_rSrc, TDst &out_rDst)
Definition String.inl:447
expr::InferredString< TString > ToUpper(const TString &in_rStr)
Definition String.inl:328
expr::InferredString< TString > Trim(const TString &in_rStr, std::optional< std::vector< TChar > > in_trimChars={})
Definition String.inl:381
Definition Expressions.inl:2
Definition Memory.h:50
uint8_t InstrLength
Definition Memory.h:55
BranchDistance Distance
Definition Memory.h:52
uint8_t OpcodeLength
Definition Memory.h:54
BranchType Type
Definition Memory.h:51
BranchCondition Condition
Definition Memory.h:53
void * pDestination
Definition Memory.h:56
Definition StringTypes.h:11
Definition StringTypes.h:26