Using Overlapped I / O in DeviceIoControl

Using Overlapped I / O in DeviceIoControl


Various methods can be used, such as synchronizing with events and continuously receiving events through polling, to monitor the frequent events in the kernel driver one by one in user mode .

Scenario in question

Imagine that you continue to receive events with relatively simple polling. The kernel driver queues the event that occurred, and the user mode application issues the IOCTL to retrieve the contents of the queue. In this type of logic, the user-mode application will Whileexecute the same loop as DeviceIoControland will continue to communicate with the driver by calling.
while(true)
{
    BOOL ret =    DeviceIoControl(hDevice, IOCTL_RECV, &data, sizeof(DATA), 0,0,&dwReturn,NULL);
    if(ret)
    {
    // 처리..
    }
}

DeviceIoControl Blocking 발생

In the previously used logic, DeviceIoControlblocking occurs until an event occurs in the kernel driver DeviceIoControlBlocking occurs in the user mode application DeviceIoControlis not returned. A problem occurs here, but the kernel driver is implemented to receive various IOCTLs, but requests for IOCTLs that receive events are not finished, so other DeviceIoControlcalls cannot be processed. The reason is that it can be processed one operation at a time due to the nature of File Object.
(https://community.osr.com/discussion/288276/can-a-deviceiocontrol-block)
Without FILE_FLAG_OVERLAPPED in the CreateFile call, regardless of how the driver is written, the operating system will only allow one outstanding I/O operation at a time on that file handle. It doesn't matter how many threads you have. All other calls will block until the first completes.`

The solution is FILE_FLAG_OVERLAPPEDoverlapped I / O

The prototype of CreateFile is as follows.
HANDLE CreateFileA(
  LPCSTR                lpFileName,
  DWORD                 dwDesiredAccess,
  DWORD                 dwShareMode,
  LPSECURITY_ATTRIBUTES lpSecurityAttributes,
  DWORD                 dwCreationDisposition,
  DWORD                 dwFlagsAndAttributes,
  HANDLE                hTemplateFile
);
https://docs.microsoft.com/en-us/windows/win32/api/fileapi/nf-fileapi-createfilea As
I was learning Windows System programming, I talked about overlapping I / O when I learned about file handling. Overlapped I / O, which may not be used at any time, can be applied here. CreateFileOf dwFlagsAndAttributesthe FILE_FLAG_OVERLAPPEDsetting flags can be processed asynchronously Overlapped I / O when File I / O. Devices correspond to file objects, but device I / O can be treated as overlapped I / O.

Apply overlapped I / O

FILE_FLAG_OVERLAPPEDOpen the device using the flag as shown below .
HANDLE    m_hDevice = CreateFile(_T("\\\\.\\mydev"),
        GENERIC_READ | GENERIC_WRITE, 0, NULL, OPEN_EXISTING,
        FILE_ATTRIBUTE_NORMAL | FILE_FLAG_OVERLAPPED, INVALID_HANDLE_VALUE);
Below is a part of the thread's Loop for receiving events.

//.....
HANDLE event;

OVERLAPPED overlapped;
event = CreateEvent(NULL, FALSE, FALSE, NULL);
memset(&overlapped, 0, sizeof(overlapped));
overlapped.hEvent = event;
while (pDlg->m_hDevice)
{


    DeviceIoControl(pDlg->m_hDevice, IOCTL_RECV, &ioctl, sizeof(MY_IOCTL), 0, 0, &dwReturn, &overlapped);

    DWORD iolen0;

    if (GetLastError() != ERROR_IO_PENDING ||
        !GetOverlappedResult(pDlg->m_hDevice, &overlapped, &iolen0, TRUE))
    {
        OutputDebugString(_T("DeviceIoControl fail."));
        continue;
    }
    dwReturn = iolen0;
        //처리...

    }
If OVERLAPPEDyou set the event in the structure and DeviceIoControlpass it to as above DeviceIoControlis returned immediately upon invocation. It is returned before all requested IOCTLs are processed. Therefore GetOverlappedResult, it waits until the end of I / O by using a function. When the driver processes the event and completes, I / O is completed.
IOCTL can be processed by other threads while processing the event reception.

Comments

Popular posts from this blog

Enabling Com Port on Hyper-V 2nd Generation VM / Windbg

DLL Injection using CreateRemoteThread in Windows 10