reactos/rostests/kmtests/ntos_ex/ExPools.c
Thomas Faber 454f222ade [ROSTESTS]
- Merge the Kernel Mode Test Suite Google Summer of Code project
- Happy testing, everyone ;)

svn path=/trunk/; revision=53671
2011-09-10 11:41:33 +00:00

151 lines
3.8 KiB
C

/*
* PROJECT: ReactOS kernel-mode tests
* LICENSE: LGPLv2+ - See COPYING.LIB in the top level directory
* PURPOSE: Kernel-Mode Test Suite Pools test routines KM-Test
* PROGRAMMER: Aleksey Bragin <aleksey@reactos.org>
*/
/* TODO: PoolsCorruption tests fail because accessing invalid memory doesn't necessarily cause an access violation */
#include <kmt_test.h>
#define NDEBUG
#include <debug.h>
#define TAG_POOLTEST 'tstP'
static VOID PoolsTest(VOID)
{
PVOID Ptr;
ULONG AllocSize, i, AllocNumber;
PVOID *Allocs;
// Stress-test nonpaged pool
for (i=1; i<10000; i++)
{
// make up some increasing, a bit irregular size
AllocSize = i*10;
if (i % 10)
AllocSize++;
if (i % 25)
AllocSize += 13;
// start with non-paged pool
Ptr = ExAllocatePoolWithTag(NonPagedPool, AllocSize, TAG_POOLTEST);
// it may fail due to no-memory condition
if (!Ptr) break;
// try to fully fill it
RtlFillMemory(Ptr, AllocSize, 0xAB);
// free it
ExFreePoolWithTag(Ptr, TAG_POOLTEST);
}
// now paged one
for (i=1; i<10000; i++)
{
// make up some increasing, a bit irregular size
AllocSize = i*50;
if (i % 10)
AllocSize++;
if (i % 25)
AllocSize += 13;
// start with non-paged pool
Ptr = ExAllocatePoolWithTag(PagedPool, AllocSize, TAG_POOLTEST);
// it may fail due to no-memory condition
if (!Ptr) break;
// try to fully fill it
RtlFillMemory(Ptr, AllocSize, 0xAB);
// free it
ExFreePoolWithTag(Ptr, TAG_POOLTEST);
}
// test super-big allocations
/*AllocSize = 2UL * 1024 * 1024 * 1024;
Ptr = ExAllocatePoolWithTag(NonPagedPool, AllocSize, TAG_POOLTEST);
ok(Ptr == NULL, "Allocating 2Gb of nonpaged pool should fail\n");
Ptr = ExAllocatePoolWithTag(PagedPool, AllocSize, TAG_POOLTEST);
ok(Ptr == NULL, "Allocating 2Gb of paged pool should fail\n");*/
// now test allocating lots of small/medium blocks
AllocNumber = 100000;
Allocs = ExAllocatePoolWithTag(PagedPool, sizeof(Allocs) * AllocNumber, TAG_POOLTEST);
// alloc blocks
for (i=0; i<AllocNumber; i++)
{
AllocSize = 42;
Allocs[i] = ExAllocatePoolWithTag(NonPagedPool, AllocSize, TAG_POOLTEST);
}
// now free them
for (i=0; i<AllocNumber; i++)
{
ExFreePoolWithTag(Allocs[i], TAG_POOLTEST);
}
ExFreePoolWithTag(Allocs, TAG_POOLTEST);
}
static VOID PoolsCorruption(VOID)
{
PULONG Ptr, TestPtr;
ULONG AllocSize;
NTSTATUS Status = STATUS_SUCCESS;
// start with non-paged pool
AllocSize = 4096 + 0x10;
Ptr = ExAllocatePoolWithTag(NonPagedPool, AllocSize, TAG_POOLTEST);
// touch all bytes, it shouldn't cause an exception
RtlZeroMemory(Ptr, AllocSize);
// test buffer overrun, right after our allocation ends
_SEH2_TRY
{
TestPtr = (PULONG)((PUCHAR)Ptr + AllocSize);
//Ptr[4] = 0xd33dbeef;
*TestPtr = 0xd33dbeef;
}
_SEH2_EXCEPT(EXCEPTION_EXECUTE_HANDLER)
{
/* Get the status */
Status = _SEH2_GetExceptionCode();
} _SEH2_END;
ok(Status == STATUS_ACCESS_VIOLATION, "Exception should occur, but got Status 0x%08lX\n", Status);
// test overrun in a distant byte range, but within 4096KB
_SEH2_TRY
{
Ptr[2020] = 0xdeadb33f;
}
_SEH2_EXCEPT(EXCEPTION_EXECUTE_HANDLER)
{
/* Get the status */
Status = _SEH2_GetExceptionCode();
} _SEH2_END;
ok(Status == STATUS_ACCESS_VIOLATION, "Exception should occur, but got Status 0x%08lX\n", Status);
// free the pool
ExFreePoolWithTag(Ptr, TAG_POOLTEST);
}
START_TEST(ExPools)
{
PoolsTest();
PoolsCorruption();
}