HyperDbg Debugger
Loading...
Searching...
No Matches
PlatformMem.h File Reference

Cross platform APIs for memory allocation. More...

Go to the source code of this file.

Functions

INT PlatformSprintf (char *Buffer, SIZE_T BufferSize, const char *Format,...)
 Platform independent wrapper for sprintf_s / snprintf.
VOID PlatformFreeMemory (PVOID Memory)
 Frees a previously allocated memory block.
VOID PlatformWriteMemory (PVOID Address, PVOID Buffer, SIZE_T Size)
 Writes data from a buffer to a memory address.
VOID PlatformSetMemory (PVOID Destination, int Value, SIZE_T Size)
 Sets a memory block to a specific value.
VOID PlatformZeroMemory (PVOID Destination, SIZE_T Size)
 Zeros a memory block.
PVOID PlatformAllocateMemory (SIZE_T Size)
 ... New Unified API ...
PVOID PlatformMemAllocateContiguousZeroedMemory (SIZE_T NumberOfBytes)
 ... Backward Compatibility / Specific APIs ...
PVOID PlatformMemAllocateNonPagedPool (SIZE_T NumberOfBytes)
 Allocates non-paged pool memory.
PVOID PlatformMemAllocateNonPagedPoolWithQuota (SIZE_T NumberOfBytes)
 Allocates non-paged pool memory with quota charging.
PVOID PlatformMemAllocateZeroedNonPagedPool (SIZE_T NumberOfBytes)
 Allocates zeroed non-paged pool memory.
PVOID PlatformMemFreePool (PVOID BufferAddress)
 Frees a memory pool.

Detailed Description

Cross platform APIs for memory allocation.

Author
Behrooz Abbassi (Behro.nosp@m.ozAb.nosp@m.bassi.nosp@m.@hyp.nosp@m.erdbg.nosp@m..org)
Sina Karvandi (sina@.nosp@m.hype.nosp@m.rdbg..nosp@m.org)
Alirez Moradi (alish014)
Version
0.1
Date
2022-01-17

Function Documentation

◆ PlatformAllocateMemory()

PVOID PlatformAllocateMemory ( SIZE_T Size)

... New Unified API ...

Allocates a block of memory in the kernel pool.

On Windows: Allocates from NonPagedPool and zeroes it. On Linux: Uses kzalloc (GFP_KERNEL) which zeroes memory.

Parameters
SizeThe number of bytes to allocate.
Returns
PVOID Pointer to the allocated memory, or NULL on failure.
60{
61#ifdef _WIN32
62 PVOID Result = ExAllocatePool2(
63 POOL_FLAG_NON_PAGED, // non-paged pool
64 Size,
65 POOLTAG);
66
67 if (Result != NULL)
68 RtlSecureZeroMemory(Result, Size);
69
70 return Result;
71#else
72 // Linux Kernel: kzalloc allocates zeroed memory
73 PVOID ptr = kzalloc(Size, GFP_KERNEL);
74
75 if (ptr)
76 {
77 printk(KERN_INFO "MemAllocKernel: Allocated %zu bytes at %px\n", Size, ptr);
78 }
79 else
80 {
81 printk(KERN_ERR "MemAllocKernel: failed to allocate %zu bytes\n", Size);
82 }
83
84 return ptr;
85#endif
86}
void * PVOID
Definition BasicTypes.h:56
#define POOLTAG
Pool tag.
Definition Constants.h:419

◆ PlatformFreeMemory()

VOID PlatformFreeMemory ( PVOID Memory)

Frees a previously allocated memory block.

Parameters
MemoryPointer to the memory block. Handles NULL safely.
95{
96 if (!Memory)
97 return;
98
99#ifdef _WIN32
100 ExFreePoolWithTag(Memory, POOLTAG);
101#else
102 kfree(Memory);
103#endif
104}

◆ PlatformMemAllocateContiguousZeroedMemory()

PVOID PlatformMemAllocateContiguousZeroedMemory ( SIZE_T NumberOfBytes)

... Backward Compatibility / Specific APIs ...

Allocates contiguous zeroed physical memory.

On Windows: Uses MmAllocateContiguousMemory. On Linux: Uses kmalloc (which is usually physically contiguous) or dma_alloc_coherent. For simplicity in this driver, we map to kzalloc.

Parameters
NumberOfBytesSize in bytes.
Returns
PVOID Pointer to memory or NULL.
185{
186#ifdef _WIN32
187 PVOID Result = NULL;
188 PHYSICAL_ADDRESS MaxPhysicalAddr = {0};
189 MaxPhysicalAddr.QuadPart = MAXULONG64;
190
191 Result = MmAllocateContiguousMemory(NumberOfBytes, MaxPhysicalAddr);
192 if (Result != NULL)
193 RtlSecureZeroMemory(Result, NumberOfBytes);
194 return Result;
195#else
196 // In Linux, kmalloc/kzalloc returns physically contiguous memory
197 // (unless vmalloc is used, which we aren't using here).
198 return kzalloc(NumberOfBytes, GFP_KERNEL);
199#endif
200}
POOL_TYPE SIZE_T NumberOfBytes
Definition Hooks.h:88
NULL()
Definition test-case-generator.py:530

◆ PlatformMemAllocateNonPagedPool()

PVOID PlatformMemAllocateNonPagedPool ( SIZE_T NumberOfBytes)

Allocates non-paged pool memory.

Parameters
NumberOfBytesSize in bytes.
Returns
PVOID Pointer to memory.
209{
210#ifdef _WIN32
211 return ExAllocatePool2(
212 POOL_FLAG_NON_PAGED,
214 POOLTAG);
215#else
216 // Linux kernel memory is non-paged by default (except vmalloc)
217 return kmalloc(NumberOfBytes, GFP_KERNEL);
218#endif
219}

◆ PlatformMemAllocateNonPagedPoolWithQuota()

PVOID PlatformMemAllocateNonPagedPoolWithQuota ( SIZE_T NumberOfBytes)

Allocates non-paged pool memory with quota charging.

Parameters
NumberOfBytesSize in bytes.
Returns
PVOID Pointer to memory.
228{
229#ifdef _WIN32
230 // POOL_FLAG_USE_QUOTA is used with ExAllocatePool2
231 // Note: Ensure your WDK supports ExAllocatePool2, otherwise use ExAllocatePoolWithQuotaTag
232 return ExAllocatePool2(
233 POOL_FLAG_NON_PAGED | POOL_FLAG_USE_QUOTA,
235 POOLTAG);
236#else
237 // Quotas are not explicitly managed in simple Linux kernel allocations like this
238 return kmalloc(NumberOfBytes, GFP_KERNEL);
239#endif
240}

◆ PlatformMemAllocateZeroedNonPagedPool()

PVOID PlatformMemAllocateZeroedNonPagedPool ( SIZE_T NumberOfBytes)

Allocates zeroed non-paged pool memory.

Parameters
NumberOfBytesSize in bytes.
Returns
PVOID Pointer to memory.
249{
250#ifdef _WIN32
251 PVOID Result = ExAllocatePool2(
252 POOL_FLAG_NON_PAGED,
254 POOLTAG);
255 if (Result != NULL)
256 RtlSecureZeroMemory(Result, NumberOfBytes);
257 return Result;
258#else
259 return kzalloc(NumberOfBytes, GFP_KERNEL);
260#endif
261}

◆ PlatformMemFreePool()

PVOID PlatformMemFreePool ( PVOID BufferAddress)

Frees a memory pool.

Parameters
BufferAddressPointer to the memory to free.
Returns
PVOID (Void pointer in original API, usually ignored).
270{
271 if (!BufferAddress)
272 return NULL;
273
274#ifdef _WIN32
275 ExFreePoolWithTag(BufferAddress, POOLTAG);
276#else
277 kfree(BufferAddress);
278#endif
279 return NULL;
280}

◆ PlatformSetMemory()

VOID PlatformSetMemory ( PVOID Destination,
int Value,
SIZE_T Size )

Sets a memory block to a specific value.

Parameters
DestinationMemory address.
ValueValue to set.
SizeNumber of bytes.
138{
139 if (!Destination)
140 return;
141
142#ifdef _WIN32
143 RtlFillMemory(Destination, Size, Value);
144#else
145 memset(Destination, Value, Size);
146#endif
147}
RequestedActionOfThePacket Value(0x1) 00000000

◆ PlatformSprintf()

INT PlatformSprintf ( char * Buffer,
SIZE_T BufferSize,
const char * Format,
... )

Platform independent wrapper for sprintf_s / snprintf.

Parameters
Bufferoutput buffer
BufferSizesize of the output buffer
Formatformat string
Returns
INT number of characters written, or -1 on error
30{
31 va_list Args;
32 va_start(Args, Format);
33 INT Result;
34#if defined(_WIN32) || defined(_WIN64)
35 Result = vsprintf_s(Buffer, BufferSize, Format, Args);
36#elif defined(__linux__)
37 Result = vsnprintf(Buffer, BufferSize, Format, Args);
38#else
39# error "Unsupported platform"
40#endif
41 va_end(Args);
42 return Result;
43}
int INT
Definition BasicTypes.h:43

◆ PlatformWriteMemory()

VOID PlatformWriteMemory ( PVOID Address,
PVOID Buffer,
SIZE_T Size )

Writes data from a buffer to a memory address.

Parameters
ProcessReserved (unused).
AddressDestination address.
BufferSource buffer.
SizeNumber of bytes to copy.
Returns
VOID
119{
120#ifdef _WIN32
121 RtlCopyMemory(Address, Buffer, Size);
122#else
123 memcpy(Address, Buffer, Size);
124#endif
125}

◆ PlatformZeroMemory()

VOID PlatformZeroMemory ( PVOID Destination,
SIZE_T Size )

Zeros a memory block.

Parameters
DestinationMemory address.
SizeNumber of bytes.
158{
159 if (!Destination)
160 return;
161
162#ifdef _WIN32
163 RtlZeroMemory(Destination, Size);
164#elif defined(__linux__)
165 memset(Destination, 0, Size);
166#else
167# error "Unsupported platform"
168#endif
169}