Skip to content

Commit 96f6d63

Browse files
committed
Add Wdf* APIs required by ebpf-for-windows
Add cache-aligned allocations (fixes #31) Signed-off-by: Dave Thaler <[email protected]>
1 parent 7ce8f1a commit 96f6d63

File tree

16 files changed

+586
-243
lines changed

16 files changed

+586
-243
lines changed

inc/usersim/ex.h

Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -198,6 +198,80 @@ USERSIM_API _Ret_maybenull_ void*
198198
ExAllocatePoolWithTagCPP(
199199
_In_ __drv_strictTypeMatch(__drv_typeExpr) POOL_TYPE pool_type, SIZE_T number_of_bytes, ULONG tag);
200200

201+
/**
202+
* @brief Allocate memory.
203+
* @param[in] pool_type Pool type to use.
204+
* @param[in] size Size of memory to allocate.
205+
* @param[in] tag Pool tag to use.
206+
* @param[in] initialize False to return "uninitialized" memory.
207+
* @returns Pointer to memory block allocated, or null on failure.
208+
*/
209+
__drv_allocatesMem(Mem) _Must_inspect_result_ _Ret_writes_maybenull_(size) void* usersim_allocate_with_tag(
210+
_In_ __drv_strictTypeMatch(__drv_typeExpr) POOL_TYPE pool_type,
211+
size_t size,
212+
uint32_t tag,
213+
bool initialize);
214+
215+
/**
216+
* @brief Allocate memory.
217+
* @param[in] size Size of memory to allocate.
218+
* @returns Pointer to memory block allocated, or null on failure.
219+
*/
220+
__drv_allocatesMem(Mem) _Must_inspect_result_ _Ret_writes_maybenull_(size) void* usersim_allocate(size_t size);
221+
222+
/**
223+
* @brief Reallocate memory.
224+
* @param[in] memory Allocation to be reallocated.
225+
* @param[in] old_size Old size of memory to reallocate.
226+
* @param[in] new_size New size of memory to reallocate.
227+
* @returns Pointer to memory block allocated, or null on failure.
228+
*/
229+
__drv_allocatesMem(Mem) _Must_inspect_result_ _Ret_writes_maybenull_(new_size) void* usersim_reallocate(
230+
_In_ _Post_invalid_ void* memory, size_t old_size, size_t new_size);
231+
232+
/**
233+
* @brief Reallocate memory with tag.
234+
* @param[in] memory Allocation to be reallocated.
235+
* @param[in] old_size Old size of memory to reallocate.
236+
* @param[in] new_size New size of memory to reallocate.
237+
* @param[in] tag Pool tag to use.
238+
* @returns Pointer to memory block allocated, or null on failure.
239+
*/
240+
__drv_allocatesMem(Mem) _Must_inspect_result_ _Ret_writes_maybenull_(new_size) void* usersim_reallocate_with_tag(
241+
_In_ _Post_invalid_ void* memory, size_t old_size, size_t new_size, uint32_t tag);
242+
243+
/**
244+
* @brief Free memory.
245+
* @param[in] memory Allocation to be freed.
246+
*/
247+
void
248+
usersim_free(_Frees_ptr_opt_ void* memory);
249+
250+
/**
251+
* @brief Allocate memory that has a starting address that is cache aligned.
252+
* @param[in] size Size of memory to allocate
253+
* @returns Pointer to memory block allocated, or null on failure.
254+
*/
255+
USERSIM_API
256+
__drv_allocatesMem(Mem) _Must_inspect_result_
257+
_Ret_writes_maybenull_(size) void* usersim_allocate_cache_aligned(size_t size);
258+
259+
/**
260+
* @brief Allocate memory that has a starting address that is cache aligned with tag.
261+
* @param[in] size Size of memory to allocate
262+
* @param[in] tag Pool tag to use.
263+
* @returns Pointer to memory block allocated, or null on failure.
264+
*/
265+
__drv_allocatesMem(Mem) _Must_inspect_result_
266+
_Ret_writes_maybenull_(size) void* usersim_allocate_cache_aligned_with_tag(size_t size, uint32_t tag);
267+
268+
/**
269+
* @brief Free memory that has a starting address that is cache aligned.
270+
* @param[in] memory Allocation to be freed.
271+
*/
272+
void
273+
usersim_free_cache_aligned(_Frees_ptr_opt_ void* memory);
274+
201275
USERSIM_API _Ret_maybenull_ void*
202276
ExAllocatePoolUninitializedCPP(_In_ POOL_TYPE pool_type, _In_ size_t number_of_bytes, _In_ unsigned long tag);
203277

@@ -207,4 +281,26 @@ ExRaiseAccessViolationCPP();
207281
USERSIM_API void
208282
ExRaiseDatatypeMisalignmentCPP();
209283

284+
void usersim_initialize_ex(bool leak_detector);
285+
void usersim_clean_up_ex();
286+
287+
#ifdef __cplusplus
288+
#include <memory>
289+
namespace usersim_helper {
290+
291+
struct _usersim_free_functor
292+
{
293+
void
294+
operator()(void* memory)
295+
{
296+
usersim_free(memory);
297+
}
298+
};
299+
300+
typedef std::unique_ptr<void, _usersim_free_functor> usersim_memory_ptr;
301+
302+
} // namespace usersim_helper
303+
304+
#endif
305+
210306
#endif

inc/usersim/io.h

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,13 @@ extern "C"
6868
IoGetFileObjectGenericMapping();
6969

7070
USERSIM_API
71-
_IRQL_requires_max_(DISPATCH_LEVEL) NTKERNELAPI PEPROCESS IoGetCurrentProcess(VOID);
71+
_IRQL_requires_max_(DISPATCH_LEVEL) PEPROCESS IoGetCurrentProcess(VOID);
72+
73+
typedef struct _IRP* PIRP;
74+
75+
USERSIM_API
76+
_IRQL_requires_max_(DISPATCH_LEVEL) VOID
77+
IofCompleteRequest(_In_ PIRP irp, _In_ CCHAR priority_boost);
7278

7379
#if defined(__cplusplus)
7480
}

inc/usersim/wdf.h

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,16 +13,14 @@ extern "C"
1313
#endif
1414

1515
typedef HANDLE WDFDEVICE;
16-
typedef struct _wdfdriver WDFDRIVER;
16+
typedef HANDLE WDFDRIVER;
1717

1818
typedef struct _WDF_OBJECT_ATTRIBUTES WDF_OBJECT_ATTRIBUTES, *PWDF_OBJECT_ATTRIBUTES;
1919
typedef struct _WDFDEVICE_INIT WDFDEVICE_INIT, *PWDFDEVICE_INIT;
2020

2121
#define WDF_NO_OBJECT_ATTRIBUTES 0
2222
#define WDF_NO_HANDLE 0
2323

24-
typedef struct _wdfdriver WDFDRIVER;
25-
2624
typedef struct _driver_object DRIVER_OBJECT, *PDRIVER_OBJECT;
2725

2826
typedef NTSTATUS(DRIVER_INITIALIZE)(_In_ PDRIVER_OBJECT driver_object, _In_ PUNICODE_STRING registry_path);
@@ -44,10 +42,10 @@ extern "C"
4442
ULONG DriverPoolTag;
4543
} WDF_DRIVER_CONFIG, *PWDF_DRIVER_CONFIG;
4644

47-
typedef struct _wdfdriver
45+
struct _driver_object
4846
{
4947
WDF_DRIVER_CONFIG config;
50-
} WDFDRIVER;
48+
};
5149

5250
#define WDF_DRIVER_GLOBALS_NAME_LEN (32)
5351

@@ -78,6 +76,22 @@ extern "C"
7876
void
7977
WDF_DRIVER_CONFIG_INIT(_Out_ PWDF_DRIVER_CONFIG config, _In_opt_ PFN_WDF_DRIVER_DEVICE_ADD evt_driver_device_add);
8078

79+
typedef enum _WDFFUNCENUM
80+
{
81+
WdfControlDeviceInitAllocateTableIndex = 25,
82+
WdfDeviceInitSetDeviceTypeTableIndex = 66,
83+
WdfDeviceInitAssignNameTableIndex = 67,
84+
WdfDeviceInitSetCharacteristicsTableIndex = 70,
85+
WdfDeviceInitSetFileObjectConfigTableIndex = 71,
86+
WdfDeviceInitAssignWdmIrpPreprocessCallbackTableIndex = 73,
87+
WdfDeviceCreateTableIndex = 75,
88+
WdfDeviceCreateSymbolicLinkTableIndex = 80,
89+
WdfDriverCreateTableIndex = 116,
90+
WdfIoQueueCreateTableIndex = 152,
91+
WdfObjectDeleteTableIndex = 208,
92+
WdfFunctionTableNumEntries = 444,
93+
} WDFFUNCENUM;
94+
8195
void
8296
usersim_initialize_wdf();
8397

src/etw.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33

44
#include "platform.h"
55
#include "usersim/etw.h"
6+
#include "usersim/ex.h"
67

78
typedef struct
89
{

0 commit comments

Comments
 (0)