[XDK] Introduce ExAllocate*Zero and ExAllocate*Uninitialized functions
authorVictor Perevertkin <victor.perevertkin@reactos.org>
Sun, 26 Jul 2020 11:10:52 +0000 (14:10 +0300)
committerVictor Perevertkin <victor.perevertkin@reactos.org>
Sun, 26 Jul 2020 11:10:52 +0000 (14:10 +0300)
These has been first added to DDK 10.0.19041.0 and made backwards-compatible
with all Windows versions since Windows 2000.
Our implementation does not use POOL_ZERO_ALLOCATION flag because it's not
supported by ReactOS yet, and thus erases the memory unconditionally.

sdk/include/xdk/exfuncs.h
sdk/include/xdk/iotypes.h

index 5dc7ba9..1f9a1ad 100644 (file)
@@ -574,6 +574,164 @@ ExAllocatePoolWithTagPriority(
   _In_ ULONG Tag,
   _In_ __drv_strictTypeMatch(__drv_typeExpr) EX_POOL_PRIORITY Priority);
 
+FORCEINLINE
+__drv_allocatesMem(Mem)
+_When_((PoolType & PagedPool) != 0, _IRQL_requires_max_(APC_LEVEL))
+_When_((PoolType & PagedPool) == 0, _IRQL_requires_max_(DISPATCH_LEVEL))
+_When_((PoolType & NonPagedPoolMustSucceed) != 0,
+  __drv_reportError("Must succeed pool allocations are forbidden. "
+                    "Allocation failures cause a system crash"))
+_When_((PoolType & (NonPagedPoolMustSucceed | POOL_RAISE_IF_ALLOCATION_FAILURE)) == 0,
+  _Post_maybenull_ _Must_inspect_result_)
+_When_((PoolType & (NonPagedPoolMustSucceed | POOL_RAISE_IF_ALLOCATION_FAILURE)) != 0,
+  _Post_notnull_)
+_Post_writable_byte_size_(NumberOfBytes)
+PVOID
+NTAPI
+ExAllocatePoolZero(
+  _In_ __drv_strictTypeMatch(__drv_typeExpr) POOL_TYPE PoolType,
+  _In_ SIZE_T NumberOfBytes,
+  _In_ ULONG Tag)
+{
+  PVOID Allocation;
+
+  Allocation = ExAllocatePoolWithTag(PoolType, NumberOfBytes, Tag);
+
+  if (Allocation != NULL) {
+    RtlZeroMemory(Allocation, NumberOfBytes);
+  }
+
+  return Allocation;
+}
+
+FORCEINLINE
+__drv_allocatesMem(Mem)
+_When_((PoolType & PagedPool) != 0, _IRQL_requires_max_(APC_LEVEL))
+_When_((PoolType & PagedPool) == 0, _IRQL_requires_max_(DISPATCH_LEVEL))
+_When_((PoolType & NonPagedPoolMustSucceed) != 0,
+  __drv_reportError("Must succeed pool allocations are forbidden. "
+                    "Allocation failures cause a system crash"))
+_When_((PoolType & (NonPagedPoolMustSucceed | POOL_RAISE_IF_ALLOCATION_FAILURE)) == 0,
+  _Post_maybenull_ _Must_inspect_result_)
+_When_((PoolType & (NonPagedPoolMustSucceed | POOL_RAISE_IF_ALLOCATION_FAILURE)) != 0,
+  _Post_notnull_)
+_Post_writable_byte_size_(NumberOfBytes)
+PVOID
+NTAPI
+ExAllocatePoolUninitialized(
+  _In_ __drv_strictTypeMatch(__drv_typeExpr) POOL_TYPE PoolType,
+  _In_ SIZE_T NumberOfBytes,
+  _In_ ULONG Tag)
+{
+  return ExAllocatePoolWithTag(PoolType, NumberOfBytes, Tag);
+}
+
+FORCEINLINE
+__drv_allocatesMem(Mem)
+_When_((PoolType & PagedPool) != 0, _IRQL_requires_max_(APC_LEVEL))
+_When_((PoolType & PagedPool) == 0, _IRQL_requires_max_(DISPATCH_LEVEL))
+_When_((PoolType & NonPagedPoolMustSucceed) != 0,
+  __drv_reportError("Must succeed pool allocations are forbidden. "
+                         "Allocation failures cause a system crash"))
+_When_((PoolType & (NonPagedPoolMustSucceed | POOL_RAISE_IF_ALLOCATION_FAILURE)) == 0,
+  _Post_maybenull_ _Must_inspect_result_)
+_When_((PoolType & (NonPagedPoolMustSucceed | POOL_RAISE_IF_ALLOCATION_FAILURE)) != 0,
+  _Post_notnull_)
+_Post_writable_byte_size_(NumberOfBytes)
+PVOID
+NTAPI
+ExAllocatePoolQuotaZero (
+  _In_ __drv_strictTypeMatch(__drv_typeExpr) POOL_TYPE PoolType,
+  _In_ SIZE_T NumberOfBytes,
+  _In_ ULONG Tag)
+{
+  PVOID Allocation;
+
+  Allocation = ExAllocatePoolWithQuotaTag(PoolType, NumberOfBytes, Tag);
+
+  if (Allocation != NULL) {
+    RtlZeroMemory(Allocation, NumberOfBytes);
+  }
+
+  return Allocation;
+}
+
+FORCEINLINE
+__drv_allocatesMem(Mem)
+_When_((PoolType & PagedPool) != 0, _IRQL_requires_max_(APC_LEVEL))
+_When_((PoolType & PagedPool) == 0, _IRQL_requires_max_(DISPATCH_LEVEL))
+_When_((PoolType & NonPagedPoolMustSucceed) != 0,
+  __drv_reportError("Must succeed pool allocations are forbidden. "
+                    "Allocation failures cause a system crash"))
+_When_((PoolType & (NonPagedPoolMustSucceed | POOL_RAISE_IF_ALLOCATION_FAILURE)) == 0,
+  _Post_maybenull_ _Must_inspect_result_)
+_When_((PoolType & (NonPagedPoolMustSucceed | POOL_RAISE_IF_ALLOCATION_FAILURE)) != 0,
+  _Post_notnull_)
+_Post_writable_byte_size_(NumberOfBytes)
+PVOID
+NTAPI
+ExAllocatePoolQuotaUninitialized(
+    _In_ __drv_strictTypeMatch(__drv_typeExpr) POOL_TYPE PoolType,
+    _In_ SIZE_T NumberOfBytes,
+    _In_ ULONG Tag)
+{
+  return ExAllocatePoolWithQuotaTag(PoolType, NumberOfBytes, Tag);
+}
+
+FORCEINLINE
+__drv_allocatesMem(Mem)
+_When_((PoolType & PagedPool) != 0, _IRQL_requires_max_(APC_LEVEL))
+_When_((PoolType & PagedPool) == 0, _IRQL_requires_max_(DISPATCH_LEVEL))
+_When_((PoolType & NonPagedPoolMustSucceed) != 0,
+  __drv_reportError("Must succeed pool allocations are forbidden. "
+                    "Allocation failures cause a system crash"))
+_When_((PoolType & (NonPagedPoolMustSucceed | POOL_RAISE_IF_ALLOCATION_FAILURE)) == 0,
+  _Post_maybenull_ _Must_inspect_result_)
+_When_((PoolType & (NonPagedPoolMustSucceed | POOL_RAISE_IF_ALLOCATION_FAILURE)) != 0,
+  _Post_notnull_)
+_Post_writable_byte_size_(NumberOfBytes)
+PVOID
+NTAPI
+ExAllocatePoolPriorityZero(
+  _In_ __drv_strictTypeMatch(__drv_typeExpr) POOL_TYPE PoolType,
+  _In_ SIZE_T NumberOfBytes,
+  _In_ ULONG Tag,
+  _In_ EX_POOL_PRIORITY Priority)
+{
+  PVOID Allocation;
+
+  Allocation = ExAllocatePoolWithTagPriority(PoolType, NumberOfBytes, Tag, Priority);
+
+  if (Allocation != NULL) {
+    RtlZeroMemory(Allocation, NumberOfBytes);
+  }
+
+  return Allocation;
+}
+
+FORCEINLINE
+__drv_allocatesMem(Mem)
+_When_((PoolType & PagedPool) != 0, _IRQL_requires_max_(APC_LEVEL))
+_When_((PoolType & PagedPool) == 0, _IRQL_requires_max_(DISPATCH_LEVEL))
+_When_((PoolType & NonPagedPoolMustSucceed) != 0,
+  __drv_reportError("Must succeed pool allocations are forbidden. "
+                    "Allocation failures cause a system crash"))
+_When_((PoolType & (NonPagedPoolMustSucceed | POOL_RAISE_IF_ALLOCATION_FAILURE)) == 0,
+  _Post_maybenull_ _Must_inspect_result_)
+_When_((PoolType & (NonPagedPoolMustSucceed | POOL_RAISE_IF_ALLOCATION_FAILURE)) != 0,
+  _Post_notnull_)
+_Post_writable_byte_size_(NumberOfBytes)
+PVOID
+NTAPI
+ExAllocatePoolPriorityUninitialized(
+  _In_ __drv_strictTypeMatch(__drv_typeExpr) POOL_TYPE PoolType,
+  _In_ SIZE_T NumberOfBytes,
+  _In_ ULONG Tag,
+  _In_ EX_POOL_PRIORITY Priority)
+{
+  return ExAllocatePoolWithTagPriority(PoolType, NumberOfBytes, Tag, Priority);
+}
+
 _IRQL_requires_max_(DISPATCH_LEVEL)
 NTKERNELAPI
 VOID
index 2ae56a6..5a40a71 100644 (file)
@@ -14,9 +14,11 @@ $if (_WDMDDK_)
 #define CONNECT_FULLY_SPECIFIED_GROUP   0x4
 #define CONNECT_CURRENT_VERSION         0x4
 
-#define POOL_COLD_ALLOCATION                256
-#define POOL_QUOTA_FAIL_INSTEAD_OF_RAISE    8
-#define POOL_RAISE_IF_ALLOCATION_FAILURE    16
+#define POOL_QUOTA_FAIL_INSTEAD_OF_RAISE    0x8
+#define POOL_RAISE_IF_ALLOCATION_FAILURE    0x10
+#define POOL_COLD_ALLOCATION                0x100
+#define POOL_NX_ALLOCATION                  0x200
+#define POOL_ZERO_ALLOCATION                0x400
 
 #define IO_TYPE_ADAPTER                 1
 #define IO_TYPE_CONTROLLER              2