HI,
Could you please answer on following questions.
Environment Win7 x86
WDK version: 7600.16385.1
I write driver on kernel mode.User-mode application uses ioctl call to request data from driver.
I called ioctl periodically with 3 second interval from user application.
Here is my ioctl driver implementation:
NTSTATUS
IoctlDeviceControl(
PDEVICE_OBJECT DeviceObject,
PIRP Irp
)
{
PIO_STACK_LOCATION irpSp;
NTSTATUS ntStatus = STATUS_SUCCESS;
ULONG inBufLength;
ULONG outBufLength;
PCHAR inBuf;
PCHAR outBuf;
PACKET_INFO* packet;
LIST_ENTRY* listEntry;
KLOCK_QUEUE_HANDLE packetInfoListLockHandle;
int maxSize;
int currentSize;
int packetSize;
int count;
char data[1028];
int counter;
memset(data, 0, sizeof(data));
UNREFERENCED_PARAMETER(DeviceObject);
PAGED_CODE();
irpSp = IoGetCurrentIrpStackLocation( Irp );
inBufLength = irpSp->Parameters.DeviceIoControl.InputBufferLength;
outBufLength = irpSp->Parameters.DeviceIoControl.OutputBufferLength;
DoTraceMessage(FLAG_ONE, “Enter IoctlDeviceControl \n”);
KdPrint((“Enter IoctlDeviceControl \n”));
if (!inBufLength || !outBufLength)
{
ntStatus = STATUS_INVALID_PARAMETER;
goto End;
}
if( irpSp->Parameters.DeviceIoControl.IoControlCode==IOCTL_METHOD_BUFFERED )
{
maxSize=1028;
currentSize=4;
packetSize=sizeof(PACKET_INFO);
counter=0;
while (!IsListEmpty(&gPacketInfoList))
{
if (currentSize+packetSize>maxSize)
break;
packet = NULL;
KeAcquireInStackQueuedSpinLock(
&gPacketInfoListLock,
&packetInfoListLockHandle);
if (!IsListEmpty(&gPacketInfoList))
{
listEntry = RemoveHeadList(&gPacketInfoList);
packet = CONTAINING_RECORD(
listEntry,
PACKET_INFO,
listEntry);
}
KeReleaseInStackQueuedSpinLock(&packetInfoListLockHandle);
if (packet!=NULL)
{
counter++;
KdPrint((“Packet Info %d proccessed \n”,counter));
memcpy(&data[currentSize],(char*)packet,packetSize);
currentSize+=packetSize;
ExFreePoolWithTag(packet, PACKET_INFO_POOL_TAG);
}
}
count=(int)((currentSize-4)/packetSize);
DoTraceMessage(FLAG_ONE, “Count packets %d \n”,count );
KdPrint((“Count packets %d \n”,count ));
inBuf = Irp->AssociatedIrp.SystemBuffer;
outBuf = Irp->AssociatedIrp.SystemBuffer;
memcpy(&data[0],&count,4);
RtlCopyBytes(outBuf, data, currentSize);
Irp->IoStatus.Information = (outBufLength }
End:
Irp->IoStatus.Status = ntStatus;
IoCompleteRequest( Irp, IO_NO_INCREMENT );
DoTraceMessage(FLAG_ONE, “Exit IoctlDeviceControl \n”);
KdPrint((“Exit IoctlDeviceControl \n” ));
return ntStatus;
}
Here is my ioctl user application implementation:
char* FilterManager::GetAvailableDriverData()
{
char OutputBuffer[1028];
char data[1028];
int countPackets=0;
ULONG bytesReturned;
memset(OutputBuffer, 0, sizeof(OutputBuffer));
BOOL resultDriver = DeviceIoControl (
driverManager.driver,
IOCTL_METHOD_BUFFERED ,
&OutputBuffer,
sizeof( OutputBuffer),
&OutputBuffer,
sizeof( OutputBuffer),
&bytesReturned,
NULL
);
if ( !resultDriver )
{
int count=0;
printf ( “Error in DeviceIoControl : %d”, GetLastError());
return data;
}
memcpy(data,OutputBuffer,bytesReturned);
memcpy(&countPackets,&data[0],4);
printf(“Count Recieved Bytes = %d \n”, bytesReturned);
printf(“Count Recieved Packets = %d \n”,countPackets);
return data;
}
After i increased ioctl buffer from 1028 to 64516 byteas i get PAGE_FAULT_IN_NONPAGED_AREA (50) error.
Here is windbg output:
PAGE_FAULT_IN_NONPAGED_AREA (50)
Invalid system memory was referenced. This cannot be protected by try-except,
it must be protected by a Probe. Typically the address is just plain bad or it
is pointing at freed memory.
Arguments:
Arg1: 944f0000, memory referenced.
Arg2: 00000000, value 0 = read operation, 1 = write operation.
Arg3: 828b636b, If non-zero, the instruction address which referenced the bad memory
- Show quoted text -
address.
Arg4: 00000000, (reserved)
READ_ADDRESS: 944f0000
FAULTING_IP:
nt!_alloca_probe+27
828b636b 8500 test dword ptr [eax],eax
MM_INTERNAL_CODE: 0
DEFAULT_BUCKET_ID: VISTA_DRIVER_FAULT
BUGCHECK_STR: 0x50
PROCESS_NAME: TestEngine.exe
CURRENT_IRQL: 2
MANAGED_STACK: !dumpstack -EE
TRAP_FRAME: 944f3b80 – (.trap 0xffffffff944f3b80)
ErrCode = 00000000
eax=944f0000 ebx=00000000 ecx=944e3b9c edx=85e25790 esi=8434ff08 edi=842e8e58
eip=828b636b esp=944f3bf4 ebp=944f3bfc iopl=0 nv up ei ng nz na pe nc
cs=0008 ss=0010 ds=0023 es=0023 fs=0030 gs=0000 efl=00010286
nt!_alloca_probe+0x27:
828b636b 8500 test dword ptr [eax],eax ds:0023:944f0000=???
Resetting default scope
LAST_CONTROL_TRANSFER: from 82928e71 to 828b7394
STACK_TEXT:
944f36cc 82928e71 00000003 be8c7861 00000065 nt!RtlpBreakWithStatusInstruction
944f371c 8292996d 00000003 84265a58 00000000 nt!KiBugCheckDebugBreak+0x1c
944f3ae0 828d18e3 00000050 944f0000 00000000 nt!KeBugCheck2+0x68b
944f3b68 828925f8 00000000 944f0000 00000000 nt!MmAccessFault+0x106
944f3b68 828b636b 00000000 944f0000 00000000 nt!KiTrap0E+0xdc
944f3bfc 828884bc 8434ff08 85e25790 85e25790 nt!_alloca_probe+0x27
944f3c14 82a89eee 842e8e58 85e25790 85e25800 nt!IofCallDriver+0x63
944f3c34 82aa6cd1 8434ff08 842e8e58 00000000 nt!IopSynchronousServiceTail+0x1f8
944f3cd0 82aa94ac 8434ff08 85e25790 00000000 nt!IopXxxControlFile+0x6aa
944f3d04 8288f42a 0000025c 00000000 00000000 nt!NtDeviceIoControlFile+0x2a
944f3d04 77aa64f4 0000025c 00000000 00000000 nt!KiFastCallEntry+0x12a
0032f044 77aa4cac 75dda08f 0000025c 00000000 ntdll!KiFastSystemCallRet
0032f048 75dda08f 0000025c 00000000 00000000 ntdll!NtDeviceIoControlFile+0xc
0032f0a8 778cec25 0000025c 9c402408 0032f178 KERNELBASE!DeviceIoControl+0xf6
0032f0d4 0035bc8b 0000025c 9c402408 0032f178 KERNEL32!DeviceIoControlImplementation+0x80
WARNING: Frame IP not in any known module. Following frames may be wrong.
0032f10c 03e85e60 00000000 000ff9e0 0034f24c 0x35bc8b
0034f1c8 6c8b13fc 01a43aac 01a43a8c 01a43a7c 0x3e85e60
0034f220 6c8a1b6c 00000003 0034f234 0034f2b0 mscorwks!PreStubWorker+0x141
0034f230 6c8b2209 0034f300 00000000 0034f2d0 mscorwks!CallDescrWorker+0x33
0034f2b0 6c8c6511 0034f300 00000000 0034f2d0 mscorwks!CallDescrWorkerWithHandler+0xa3
0034f3f0 6c8c6544 0036c030 0034f4bc 0034f488 mscorwks!MethodDesc::CallDescr+0x19c
0034f40c 6c8c6562 0036c030 0034f4bc 0034f488 mscorwks!MethodDesc::CallTargetWorker+0x1f
0034f424 6c930c45 0034f488 0020ba3b 00000000 mscorwks!MethodDescCallSite::CallWithValueTypes_RetArgSlot+0x1a
0034f588 6c930b65 00362ffc 00000001 0034f5c4 mscorwks!ClassLoader::RunMain+0x223
0034f7f0 6c9310b5 00000000 0020b373 00000001 mscorwks!Assembly::ExecuteMainMethod+0xa6
0034fcc0 6c93129f 00070000 00000000 0020b2a3 mscorwks!SystemDomain::ExecuteMainMethod+0x456
0034fd10 6c9311cf 00070000 0020b2eb 00000000 mscorwks!ExecuteEXE+0x59
0034fd58 6dfc7c24 00000000 6c8a0000 0034fd74 mscorwks!_CorExeMain+0x15c
0034fd68 778d1174 7ffd9000 0034fdb4 77abb3f5 MSCOREE!_CorExeMain+0x2c
0034fd74 77abb3f5 7ffd9000 7782713e 00000000 KERNEL32!BaseThreadInitThunk+0xe
0034fdb4 77abb3c8 6dfc7bf0 7ffd9000 00000000 ntdll!__RtlUserThreadStart+0x70
0034fdcc 00000000 6dfc7bf0 7ffd9000 00000000 ntdll!_RtlUserThreadStart+0x1b
STACK_COMMAND: kb
FOLLOWUP_IP:
nt!_alloca_probe+27
828b636b 8500 test dword ptr [eax],eax
SYMBOL_STACK_INDEX: 5
SYMBOL_NAME: nt!_alloca_probe+27
FOLLOWUP_NAME: MachineOwner
MODULE_NAME: nt
IMAGE_NAME: ntkrpamp.exe
DEBUG_FLR_IMAGE_TIMESTAMP: 4a5bc007
FAILURE_BUCKET_ID: 0x50_nt!_alloca_probe+27
BUCKET_ID: 0x50_nt!_alloca_probe+27
Followup: MachineOwner
1) How to resolve this issue?
2) How often can i call data from the driver?
3) What is optimal buffer size for ioctl?
Thanks for any help.