Skip to content

Commit

Permalink
Add Wdf* APIs required by ebpf-for-windows
Browse files Browse the repository at this point in the history
Add cache-aligned allocations (fixes #31)

Signed-off-by: Dave Thaler <[email protected]>
  • Loading branch information
dthaler committed Aug 4, 2023
1 parent 7ce8f1a commit 96f6d63
Show file tree
Hide file tree
Showing 16 changed files with 586 additions and 243 deletions.
96 changes: 96 additions & 0 deletions inc/usersim/ex.h
Original file line number Diff line number Diff line change
Expand Up @@ -198,6 +198,80 @@ USERSIM_API _Ret_maybenull_ void*
ExAllocatePoolWithTagCPP(
_In_ __drv_strictTypeMatch(__drv_typeExpr) POOL_TYPE pool_type, SIZE_T number_of_bytes, ULONG tag);

/**
* @brief Allocate memory.
* @param[in] pool_type Pool type to use.
* @param[in] size Size of memory to allocate.
* @param[in] tag Pool tag to use.
* @param[in] initialize False to return "uninitialized" memory.
* @returns Pointer to memory block allocated, or null on failure.
*/
__drv_allocatesMem(Mem) _Must_inspect_result_ _Ret_writes_maybenull_(size) void* usersim_allocate_with_tag(
_In_ __drv_strictTypeMatch(__drv_typeExpr) POOL_TYPE pool_type,
size_t size,
uint32_t tag,
bool initialize);

/**
* @brief Allocate memory.
* @param[in] size Size of memory to allocate.
* @returns Pointer to memory block allocated, or null on failure.
*/
__drv_allocatesMem(Mem) _Must_inspect_result_ _Ret_writes_maybenull_(size) void* usersim_allocate(size_t size);

/**
* @brief Reallocate memory.
* @param[in] memory Allocation to be reallocated.
* @param[in] old_size Old size of memory to reallocate.
* @param[in] new_size New size of memory to reallocate.
* @returns Pointer to memory block allocated, or null on failure.
*/
__drv_allocatesMem(Mem) _Must_inspect_result_ _Ret_writes_maybenull_(new_size) void* usersim_reallocate(
_In_ _Post_invalid_ void* memory, size_t old_size, size_t new_size);

/**
* @brief Reallocate memory with tag.
* @param[in] memory Allocation to be reallocated.
* @param[in] old_size Old size of memory to reallocate.
* @param[in] new_size New size of memory to reallocate.
* @param[in] tag Pool tag to use.
* @returns Pointer to memory block allocated, or null on failure.
*/
__drv_allocatesMem(Mem) _Must_inspect_result_ _Ret_writes_maybenull_(new_size) void* usersim_reallocate_with_tag(
_In_ _Post_invalid_ void* memory, size_t old_size, size_t new_size, uint32_t tag);

/**
* @brief Free memory.
* @param[in] memory Allocation to be freed.
*/
void
usersim_free(_Frees_ptr_opt_ void* memory);

/**
* @brief Allocate memory that has a starting address that is cache aligned.
* @param[in] size Size of memory to allocate
* @returns Pointer to memory block allocated, or null on failure.
*/
USERSIM_API
__drv_allocatesMem(Mem) _Must_inspect_result_
_Ret_writes_maybenull_(size) void* usersim_allocate_cache_aligned(size_t size);

/**
* @brief Allocate memory that has a starting address that is cache aligned with tag.
* @param[in] size Size of memory to allocate
* @param[in] tag Pool tag to use.
* @returns Pointer to memory block allocated, or null on failure.
*/
__drv_allocatesMem(Mem) _Must_inspect_result_
_Ret_writes_maybenull_(size) void* usersim_allocate_cache_aligned_with_tag(size_t size, uint32_t tag);

/**
* @brief Free memory that has a starting address that is cache aligned.
* @param[in] memory Allocation to be freed.
*/
void
usersim_free_cache_aligned(_Frees_ptr_opt_ void* memory);

USERSIM_API _Ret_maybenull_ void*
ExAllocatePoolUninitializedCPP(_In_ POOL_TYPE pool_type, _In_ size_t number_of_bytes, _In_ unsigned long tag);

Expand All @@ -207,4 +281,26 @@ ExRaiseAccessViolationCPP();
USERSIM_API void
ExRaiseDatatypeMisalignmentCPP();

void usersim_initialize_ex(bool leak_detector);
void usersim_clean_up_ex();

#ifdef __cplusplus
#include <memory>
namespace usersim_helper {

struct _usersim_free_functor
{
void
operator()(void* memory)
{
usersim_free(memory);
}
};

typedef std::unique_ptr<void, _usersim_free_functor> usersim_memory_ptr;

} // namespace usersim_helper

#endif

#endif
8 changes: 7 additions & 1 deletion inc/usersim/io.h
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,13 @@ extern "C"
IoGetFileObjectGenericMapping();

USERSIM_API
_IRQL_requires_max_(DISPATCH_LEVEL) NTKERNELAPI PEPROCESS IoGetCurrentProcess(VOID);
_IRQL_requires_max_(DISPATCH_LEVEL) PEPROCESS IoGetCurrentProcess(VOID);

typedef struct _IRP* PIRP;

USERSIM_API
_IRQL_requires_max_(DISPATCH_LEVEL) VOID
IofCompleteRequest(_In_ PIRP irp, _In_ CCHAR priority_boost);

#if defined(__cplusplus)
}
Expand Down
24 changes: 19 additions & 5 deletions inc/usersim/wdf.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,16 +13,14 @@ extern "C"
#endif

typedef HANDLE WDFDEVICE;
typedef struct _wdfdriver WDFDRIVER;
typedef HANDLE WDFDRIVER;

typedef struct _WDF_OBJECT_ATTRIBUTES WDF_OBJECT_ATTRIBUTES, *PWDF_OBJECT_ATTRIBUTES;
typedef struct _WDFDEVICE_INIT WDFDEVICE_INIT, *PWDFDEVICE_INIT;

#define WDF_NO_OBJECT_ATTRIBUTES 0
#define WDF_NO_HANDLE 0

typedef struct _wdfdriver WDFDRIVER;

typedef struct _driver_object DRIVER_OBJECT, *PDRIVER_OBJECT;

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

typedef struct _wdfdriver
struct _driver_object
{
WDF_DRIVER_CONFIG config;
} WDFDRIVER;
};

#define WDF_DRIVER_GLOBALS_NAME_LEN (32)

Expand Down Expand Up @@ -78,6 +76,22 @@ extern "C"
void
WDF_DRIVER_CONFIG_INIT(_Out_ PWDF_DRIVER_CONFIG config, _In_opt_ PFN_WDF_DRIVER_DEVICE_ADD evt_driver_device_add);

typedef enum _WDFFUNCENUM
{
WdfControlDeviceInitAllocateTableIndex = 25,
WdfDeviceInitSetDeviceTypeTableIndex = 66,
WdfDeviceInitAssignNameTableIndex = 67,
WdfDeviceInitSetCharacteristicsTableIndex = 70,
WdfDeviceInitSetFileObjectConfigTableIndex = 71,
WdfDeviceInitAssignWdmIrpPreprocessCallbackTableIndex = 73,
WdfDeviceCreateTableIndex = 75,
WdfDeviceCreateSymbolicLinkTableIndex = 80,
WdfDriverCreateTableIndex = 116,
WdfIoQueueCreateTableIndex = 152,
WdfObjectDeleteTableIndex = 208,
WdfFunctionTableNumEntries = 444,
} WDFFUNCENUM;

void
usersim_initialize_wdf();

Expand Down
1 change: 1 addition & 0 deletions src/etw.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

#include "platform.h"
#include "usersim/etw.h"
#include "usersim/ex.h"

typedef struct
{
Expand Down
Loading

0 comments on commit 96f6d63

Please sign in to comment.