0

I've got kernel mode driver which handles user-mode requests asynchronously. Maximum number of requests in the queue, lats say, 32. All the following requests are completed with STATUS_INSUFFICIENT_RESOURCE status. I need to check in user-mode app if the requests was completed with this status. That's my user-mode app code:

HANDLE hEvents[40] = { 0 };
OVERLAPPED ovls[40] = { 0 };
int index = 0;
while (true)
{
    hEvents[index] = CreateEvent(NULL, FALSE, FALSE, NULL);
    ZeroMemory(&ovls[index], sizeof(OVERLAPPED));
    ovls[index].hEvent = hEvents[index];
    BOOL res = DeviceIoControl(hDevice, SEND_REQUEST_CTL, nullptr, 0,
        nullptr, 0, &dwBytesRet, &ovls[index]);
    ++index;
    if (res == FALSE)
    {
        DWORD err = GetLastError();
        if (err != ERROR_IO_PENDING)
        {
            WaitForMultipleObjects(index, hEvents, TRUE, INFINITE);
            for (int i = 0; i < index; ++i)
                CloseHandle(hEvents[i]);
        }
    }
}

I have array of hEvents and array of OVERLAPPED structures, because I need to wait for requests completion. So I the idea is that when driver returns STATUS_INSUFFICIENT_RESOURCE I just waiting for completion of all the IRPs that were queued to driver. The problem is in that even when driver calls

Irp->IoStatus.Status = STATUS_INSUFFICIENT_RESOURCE;
Irp->IoStatus.Information = 0;
IoCompleteRequest(Irp, 0);

GetLastError() from user-mode app returns ERROR_IO_PENDING so I can't handle STATUS_INSUFFICIENT_BUFFER driver error. So my question is how can I check in user-mode app, that IRP was completed with STATUS_INSUFFICIENT_RESOURCE status?

rudolfninja
  • 467
  • 7
  • 24
  • I can't find a definition for `STATUS_INSUFFICIENT_BUFFER` in the Win-DDK I have; did you mean `ERROR_INSUFFICIENT_BUFFER`? – Adrian Mole Feb 01 '21 at 15:14
  • @AdrianMole faster driver use `STATUS_BUFFER_TOO_SMALL`, `STATUS_INSUFFICIENT_BUFFER` really not exist, if not custom define – RbMm Feb 01 '21 at 15:18
  • @RbMm Aha! So, does the kernel-mode `STATUS_BUFFER_TOO_SMALL` end up giving the user-mode the `ERROR_INSUFFICIENT_BUFFER` error? – Adrian Mole Feb 01 '21 at 15:20
  • @AdrianMole yes. win32 layer translate `STATUS_BUFFER_TOO_SMALL` (c0000023) to `ERROR_INSUFFICIENT_BUFFER` (122) – RbMm Feb 01 '21 at 15:21
  • @RbMm Maybe you can post an answer? Make it a Community-Wiki, if you like. – Adrian Mole Feb 01 '21 at 15:22
  • @AdrianMole - but here problem not in `STATUS_INSUFFICIENT_BUFFER` - i think this or typo or exist `#define STATUS_INSUFFICIENT_BUFFER ?`but problem in that all user mode code design is wrong. loops, etc. need use `BindIoCompletionCallback` or `CreateThreadpoolIo` here. however if `ERROR_IO_PENDING` returned - final operation status will be inside `OVERLAPPED`. and returned by callback, or can check it direct or via `GetOverlappedResult` – RbMm Feb 01 '21 at 15:26
  • Yes, it was typo. I meant ERROR_INSUFFICIENT_BUFFER. I checked `Internal` field of `OVERLAPPED` structure and it the same for both objects: pending and completed by the driver. So the problem is how to handle requests completed with ERROR_INSUFFICIENT_BUFFER status? – rudolfninja Feb 01 '21 at 15:33
  • you check `Internal` at wrong time - this need check only after I/O complete. – RbMm Feb 01 '21 at 16:11
  • @RbMm, could u pls tell how to change user-mode code using `BindIoCompletionCallback` with possibility to handle IRP completion by the driver with `STATUS_BUFFER_TOO_SMALL\STATUS_INSUFFICIENT_RESOURCE ` – rudolfninja Feb 01 '21 at 16:14
  • yes, you need simply call `BindIoCompletionCallback` on file handle. but you need use object, which encapsulate *hDevice*, refcount on this object, objects for `OVERLAPPED` etc. absolute another programming model – RbMm Feb 01 '21 at 16:20
  • @RbMm, so the idea is following: I'm creating event and `OVERLAPPED` structure object on each loop iteration. Then I'm calling `DeviceIoControl` with overlapped object. Before that (outside the loop) I call `BindIoCompletionCallback` and handle all the `OVERLAPPED` objects there by checking `Internal` field? – rudolfninja Feb 01 '21 at 17:16
  • 1
    no, not need any loops. and not need any events. only `BindIoCompletionCallback` on file. and handle asynchronous callbacks – RbMm Feb 01 '21 at 17:28

0 Answers0