Memperbarui panggilan ExAllocatePool yang tidak digunakan lagi ke ExAllocatePool2 dan ExAllocatePool3
DDI berikut tidak digunakan lagi dimulai dengan Windows 10, versi 2004 dan harus diganti seperti yang dijelaskan dalam topik ini.
Pembaruan driver untuk versi Windows 10, versi 2004 dan yang lebih baru
Jika Anda membangun driver yang menargetkan Windows 10, versi 2004 dan yang lebih baru, gunakan API pengganti ExAllocatePool2 dan ExAllocatePool3 sebagai gantinya.
API Lama | API Baru |
---|---|
ExAllocatePool | ExAllocatePool2 |
ExAllocatePoolWithTag | ExAllocatePool2 |
ExAllocatePoolWithQuota | ExAllocatePool2 |
ExAllocatePoolWithQuotaTag | ExAllocatePool2 |
ExAllocatePoolWithTagPriority | ExAllocatePool3 |
API baru akan nol alokasi kumpulan secara default, untuk membantu menghindari kemungkinan bug pengungkapan memori.
ExAllocatePool/ExAllocatePoolWithTag
// Old code
PVOID Allocation = ExAllocatePoolWithTag(PagedPool, 100, 'abcd');
RtlZeroMemory(Allocation, 100);
// New code
PVOID Allocation = ExAllocatePool2(POOL_FLAG_PAGED, 100, 'abcd');
API alokasi kumpulan lama menerima argumen POOL_TYPE , tetapi API alokasi baru menerima argumen POOL_FLAGS . Perbarui kode terkait untuk menggunakan argumen POOL_FLAGS baru.
ExAllocatePoolWithQuota/ExAllocatePoolWithQuotaTag
Fungsi baru sekarang akan mengembalikan NULL pada kegagalan alokasi secara default. Agar alokator menaikkan pengecualian pada kegagalan sebagai gantinya, bendera POOL_FLAG_RAISE_ON_FAILURE harus diteruskan seperti yang dibahas di ExAllocatePool2.
// Old code
PVOID Allocation = ExAllocatePoolWithQuotaTag(PagedPool | POOL_QUOTA_FAIL_INSTEAD_OF_RAISE, 100, 'abcd');
RtlZeroMemory(Allocation, 100);
// New code
PVOID Allocation = ExAllocatePool2(POOL_FLAG_PAGED | POOL_FLAG_USE_QUOTA, 100, 'abcd');
ExAllocatePoolWithTagPriority
// Old code
PVOID Allocation = ExAllocatePoolWithTagPriority(PagedPool, 100, 'abcd', HighPoolPriority);
RtlZeroMemory(Allocation, 100);
// New code
POOL_EXTENDED_PARAMETER params = {0};
params.Type = PoolExtendedParameterPriority;
params.Priority = HighPoolPriority;
PVOID Allocation = ExAllocatePool3(POOL_FLAG_PAGED, 100, 'abcd', ¶ms, 1);
Pembaruan driver untuk versi Windows yang lebih lama dari Windows 10, versi 2004
Jika Anda membangun driver yang menargetkan versi Windows sebelum Windows 10, versi 2004, Anda harus menggunakan fungsi pembungkus sebaris paksa berikut.
Anda juga harus #define POOL_ZERO_DOWN_LEVEL_SUPPORT dan memanggil ExInitializeDriverRuntime selama inisialisasi driver, sebelum memanggil fungsi alokasi kumpulan.
Fungsi sebaris yang ditentukan secara lokal
PVOID
NTAPI
ExAllocatePoolZero (
_In_ __drv_strictTypeMatch(__drv_typeExpr) POOL_TYPE PoolType,
_In_ SIZE_T NumberOfBytes,
_In_ ULONG Tag
)
PVOID
NTAPI
ExAllocatePoolQuotaZero (
_In_ __drv_strictTypeMatch(__drv_typeExpr) POOL_TYPE PoolType,
_In_ SIZE_T NumberOfBytes,
_In_ ULONG Tag
)
PVOID
NTAPI
ExAllocatePoolPriorityZero (
_In_ __drv_strictTypeMatch(__drv_typeExpr) POOL_TYPE PoolType,
_In_ SIZE_T NumberOfBytes,
_In_ ULONG Tag,
_In_ EX_POOL_PRIORITY Priority
)
Lihat header wdm.h terbaru untuk kode implementasi untuk pembungkus kode ini. Misalnya ini adalah implementasi untuk ExAllocatePoolPriorityZero, yang menunjukkan penggunaan RtlZeroMemory.
{
PVOID Allocation;
Allocation = ExAllocatePoolWithTagPriority((POOL_TYPE) (PoolType | POOL_ZERO_ALLOCATION),
NumberOfBytes,
Tag,
Priority);
#if defined(POOL_ZERO_DOWN_LEVEL_SUPPORT)
if ((!ExPoolZeroingNativelySupported) && (Allocation != NULL)) {
RtlZeroMemory(Allocation, NumberOfBytes);
}
#endif
return Allocation;
}
Pemetaan API lama ke API baru
API Lama | API Baru |
---|---|
ExAllocatePool | ExAllocatePoolZero |
ExAllocatePoolWithTag | ExAllocatePoolZero |
ExAllocatePoolWithQuota | ExAllocatePoolQuotaZero |
ExAllocatePoolWithQuotaTag | ExAllocatePoolQuotaZero |
ExAllocatePoolWithTagPriority | ExAllocatePoolPriorityZero |
Contoh
// Old code
PVOID Allocation = ExAllocatePoolWithTag(PagedPool, 100, 'abcd');
RtlZeroMemory(Allocation, 100);
// New code
// Before headers are pulled in (or compiler defined)
#define POOL_ZERO_DOWN_LEVEL_SUPPORT
// Once during driver initialization
// Argument can be any value
ExInitializeDriverRuntime(0);
// Replacement for each pool allocation
PVOID Allocation = ExAllocatePoolZero(PagedPool, 100, 'abcd');
Aturan UnSafeAllocatePool pemverifikasi driver
Aturan UnSafeAllocatePool pemverifikasi driver adalah aturan keamanan penting yang memeriksa bahwa driver tidak menggunakan DDI yang tidak digunakan lagi untuk mengalokasikan memori. Aturan ini tersedia dalam pratinjau build WDK 20236 ke atas.