Andrew Madsen
Andrew Madsen

Reputation: 21383

How do you create a custom OSObject subclass in a DriverKit driver?

In my DriverKit driver, I'd like to create a custom class that inherits from OSObject so that I can store it in an OSArray (and other OSObject derived collections).

The documentation for kexts and IOKit indicates that you use the OSDeclareDefaultStructors and OSDefineMetaClassAndStructors macros to define constructors, destructors, and the metaclass info. However, these macros don't seem to exist in DriverKit.

I've done something like this:

class MyCustomObject: public OSObject
{
    using super = OSObject;

public:
    virtual bool init() override;
    virtual void free() override;
}

bool MyCustomClass::init()
{
    if (!super::init()) {
        os_log(OS_LOG_DEFAULT, "[Driver] MyCustomClass super::init() failed");
        return false;
    }
    
    // Initialize stuff

    return true;
}

void MyCustomClass::free(void)
{
    // Release retained members

    super::free();
}

That works as far as it goes, and compiles. But I can't new an instance of it, which also means I don't know how to implement the traditional static withFoo() type method to create one.

Is creating a custom OSObject subclass in a DriverKit dext supported? If so, how should that be set up?

Upvotes: 2

Views: 106

Answers (1)

pmdj
pmdj

Reputation: 23438

It should work via the OSTypeAlloc macro. You'll still need to call init(), and may want to implement a static factory method wrapping both, similar to how other DriverKit classes do it.

If you're still having trouble, try defining the class in an .iig. I forget if that's required. (I think probably not, as your class won't be bridged between DriverKit and the kernel.)

Upvotes: 0

Related Questions