2

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?

1 Answer 1

0

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.)

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks, @pmdj. OSTypeAlloc expects there to be a gMyCustomObjectMetaClass. I see that other Driver/IOKit classes declare that kind of thing in their headers, but it's not completely clear what it should be initialized with. I am already defining the class in an .iig, for whatever that's worth.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.