Declaring Functions by Using Function Role Types for KMDF Drivers

To enable SDV to analyze a KMDF driver, you must declare your functions using the function role type declarations for KMDF. The function role types are defined in Wdf.h and in other KMDF header files that are included in Wdf.h. For the list of function role types and their corresponding event callback functions, see Static Driver Verifier KMDF Function Declarations.

Each event callback function in a KMDF driver must be declared by specifying the corresponding role type.

For example, the following code example shows the function role type declaration for the EvtDriverDeviceAdd callback function. In this example, the callback function is called myDriver_EvtDriverDeviceAdd. The function role type is EVT_WDF_DRIVER_DEVICE_ADD.

EVT_WDF_DRIVER_DEVICE_ADD myDriver_EvtDriverDeviceAdd;

If a callback function has a function prototype declaration, you must replace the function prototype with the function role type declaration.

The following listing is from the header file Fail_Driver6.h. The related functions are declared in FailDriver6.c.

/*++

Copyright (C) Microsoft.  All rights reserved.
Module Name:
    fail_driver6.h
Environment:
    Kernel mode
--*/

#include <NTDDK.h>  
#include <wdf.h>

#include "fail_library6.h"

DRIVER_INITIALIZE DriverEntry;
EVT_WDF_DRIVER_DEVICE_ADD EvtDriverDeviceAdd;
EVT_WDF_IO_QUEUE_IO_READ EvtIoRead;
EVT_WDF_IO_QUEUE_IO_WRITE EvtIoWrite;
EVT_WDF_IO_QUEUE_IO_DEVICE_CONTROL EvtIoDeviceControl;
EVT_WDF_DEVICE_CONTEXT_CLEANUP DeviceContextCleanUp;
EVT_WDF_DEVICE_CONTEXT_DESTROY DeviceContextDestroy;
EVT_WDF_IO_QUEUE_CONTEXT_CLEANUP_CALLBACK QueueCleanup;
EVT_WDF_IO_QUEUE_CONTEXT_DESTROY_CALLBACK QueueDestroy;
EVT_WDF_FILE_CONTEXT_CLEANUP_CALLBACK FileContextCleanup;
EVT_WDF_FILE_CONTEXT_DESTROY_CALLBACK FileContextDestroy;

After you have declared your driver callback functions using role type declarations, you can scan the driver. Scanning the driver produces the Sdv-map.h file, which you can examine to determine if the entry points were correctly identified.

Running Code Analysis for Drivers to verify the function declarations

To help you determine whether the source code is prepared, run Code Analysis for Drivers. Code Analysis for Drivers checks for function role type declarations and can help identify function declarations that might have been missed or warn you when the parameters of the function definition do not match those in the function role type.

Function Parameters and Function Role Types

As required in the C programming language, the parameter types that you use in the function definition must match the parameter types of the function prototype, or in this case, the function role type. SDV depends upon the function signatures for analysis and ignores functions whose signatures do not match.

For example, you should declare an EvtDriverDeviceAdd routine using the EVT_WDF_DRIVER_DEVICE_ADD function role type.

EVT_WDF_DRIVER_DEVICE_ADD myEvtDriverDeviceAdd;

When you implement the function myEvtDriverDeviceAdd, the parameter types must match those used by EVT_WDF_DRIVER_DEVICE_ADD, namely, WDFDRIVER and PWDFDEVICE_INIT (see EvtDriverDeviceAdd routine for syntax).

NTSTATUS
 myEvtDriverDeviceAdd (
  WDFDRIVER Driver,
 PWDFDEVICE_INIT DeviceInit
 )
{
}