Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Wdf* APIs required by ebpf-for-windows #76

Merged
merged 4 commits into from
Aug 7, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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(
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: what is the difference between usersim_allocate_with_tag() with PoolType NonPagedPoolNxCacheAligned and usersim_allocate_cache_aligned()?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Haven't done cleanup, but usersim_allocate_cache_aligned doesn't do anything with the tag, so I think it should be removed. usersim_allocate_cache_aligned_with_tag and usersim_allocate_with_tag() with PoolType NonPagedPoolNxCacheAligned are similar, but usersim_allocate_with_tag should really detect any cache-aligned pool type (it doesn't yet). I suspect in the DLL form, nothing calls usersim_allocate_cache_aligned or usersim_allocate_cache_aligned_with_tag, but will check.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I will clean this up in a separate PR as part of the work for microsoft/ebpf-for-windows#2677. In the meantime, I have added some TODO comments in the code.

_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
Loading