WDM device object extension pointer

Hello!
IoCreateDevice(…, ULONG DeviceExtensionSize,…) - implicitly allocates memory for DeviceExtensionSize-size extensions in DeviceObject->DeviceExtension .
Can I allocate and delete the memory for this pointer myself? I want to create a class object which represents my device

> I want to create a class object which represents my device…

Use “in-place new” if you are using C++, or cast the returned pointer to the desired type if you are using C.

Phil B
Not speaking for LogRhythm

1 Like

Device Extensions are created using a lookaside list as far as I know.
You cannot delete the DeviceExtension that was implicitly allocated for you. There is no exported functions to delete the DeviceExtension as well.

Either calculate the size of your class object and pass it to IoCreateDevice, or store the original Device->DeviceExtension pointer somewhere and allocate NonPagedPoolNx memory for your class object, setting it to the Device->DeviceExtension. When the driver is unloading, or the device is being deleted, release the memory you’ve allocated for your class object and set the original Device->DeviceExtension pointer back to what it was.

I don’t understand the point of this thought… Oh well, I’ve just seen your post history lol

Phil has the right answer. This is a very common idiom, going clear back to the streaming capture driver samples in 1999. Device Manager allocates the memory for you. You run “placement new” to run your constructor on that memory. When the device unloads, the memory is freed. Your destructor will not run.

     CDeviceExtension * devCtx = new (devobj->DeviceExtension) CDeviceExtension;

Of course, the better answer is to write the driver in KMDF, which handles all of this for you.

1 Like

The device extension is allocated with the device object, it is not allocated from a lookaside list separate from the device object. You can change the DeviceExtension value after the IoCreateDevice, the io manager doesn’t care what the value is.

1 Like

@Doron_Holan said:
The device extension is allocated with the device object, it is not allocated from a lookaside list separate from the device object. You can change the DeviceExtension value after the IoCreateDevice, the io manager doesn’t care what the value is.

Thank you so much Doron !
That removed one confusion I had with fastfat.

This is undocumented implementation detail though, right ?
I haven’t found anything about that detail in msdn for iocreatedevice ?

Technically, yes, how the DeviceExtension is allocated is an internal implementation detail. That is separate from the extension being private to the driver and the io manager ignoring the value of the pointer.

2 Likes

@Doron_Holan said:
Technically, yes, how the DeviceExtension is allocated is an internal implementation detail. That is separate from the extension being private to the driver and the io manager ignoring the value of the pointer.

Thank you !

Before you go further down this path, I want to emphasize a couple of things.

  1. The device extension in the DEVICE_OBJECT is cleaned up automatically when the device is unloaded. If you do your own, you become responsible for freeing that memory.
  2. It takes the same number of lines of code to use placement new to construct your object in the device extension’s memory as it does to construct the object using operator new. Actually, it takes fewer lines, because you have to provide your own “operator new”.
  3. The BEST answer is to write your driver with KMDF. The framework handles all of this for you, along with thousands of other details.
1 Like