1 / 34

Using Static Analysis Tools When Developing Drivers

Using Static Analysis Tools When Developing Drivers. Adam Shapiro Senior Program Manager Devices and Storage Technologies sdvpfdex@microsoft.com. Session Outline. Static analysis tools What they are Benefits What’s new for the next version of Windows PRE f ast for Drivers (PFD )

gita
Download Presentation

Using Static Analysis Tools When Developing Drivers

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Using Static Analysis Tools When Developing Drivers Adam Shapiro Senior Program Manager Devices and Storage Technologies sdvpfdex@microsoft.com

  2. Session Outline Static analysis tools • What they are • Benefits • What’s new for the next version of Windows PREfast for Drivers (PFD) • Core PREFast annotations Static Driver Verifier (SDV) • RoleTypes Summary

  3. What Is Static Analysis? • Compile-time analysis of the source program • Like code inspection, but performed by a tool • Compile-time type-checking a simple example • Looks for violations of well-defined constraints • Procedure contracts or API contracts

  4. Examples of Bugs Found by Static Analysis • f()requires p to be non-NULL p = NULL;…f(p); • Completing the same IRP twice: IoCompleteRequest (Irp); ... IoCompleteRequest (Irp);

  5. Why Use Static Analysis? Cheap Bugs! Rule of thumb • “A defect that costs $1 to fix on the programmer’s desktop costs $100 to fix once it is incorporated into a complete program and many thousands of dollars if it is identified only after the softwarehas been deployed in the field.” “Building a Better Bug Trap” – The Economist June 2003

  6. Tools Make You More Effective • Push-button technology • 100% path coverage • At little cost (let a computer do it) • Quickly (minutes or hours versus weeks) • Defects are discovered early • Even before device hardware is available • Before designing test cases • Often while still coding • Defect reports are easy to use • A direct reference to the defective path (or point)in the source code reduces cost of debugging

  7. Static Analysis – How Does It Work? • Builds an abstract model of a driver and exhaustively inspects execution along all paths • The abstract model is simpler: it’s reduced... • It’s so much simpler that it’s possible to have it inspected (“simulated”) exhaustively • Over-approximation of the driver • The control part remains the same • All paths are preserved and treated equally • The data state is over-approximated • If argument x is not constrained, assume any value • If (x>0) guards the point of interest, keep track of Boolean (x>0),but not integer value of x: Boolean is simpler than integer • If (x > 0) { IoCompleteRequest (Irp); }

  8. Static Analysis - Not a silver bullet • Does not replace functional testing • Targets violations of a given set of well-defined constraints • Principal limitation • It doesn’t know about every possible error • Algorithms are based on source code abstraction and heuristics • Results in both false positives and false negatives • It is a useful tool

  9. Comparing Static Tools

  10. X X Static Tools Work Together • Two complementary technologies provided in the WDK • PREfast for Drivers: Look inside every procedure for possible violations • Static Driver Verifier: Look along paths, cross inter-procedural boundaries ReadFoo ( PIRP Irp ) { ... status = Bar (Irp); if (status) { IoCompleteRequest(Irp); } } ReadFoo ( PIRP Irp ) { PIRP p = NULL; ... if (status) { IoCompleteRequest(p); } } Bar ( PIRP Irp ) { ... IoCompleteRequest(Irp); return STATUS_SUCCESS; }

  11. What’s New for PFD • “PFD Clean” initiative inside Microsoft • For Windows 7 – Microsoft owned inbox drivers and WDK samples are PFD Clean • Microsoft drivers and WDK public headers are annotated • Internal engine improvements • Broader range of expressions • Supports Const, Member names, Side-effect-free C expressions • Better annotation error checking • Part of Microsoft Automated Code Review (OACR) in the WDK

  12. What’s New for SDV • Inbox drivers and WDK KMDF, WDM samples are SDV Clean • SDV for NDIS – Great collaboration with NDIS team • 39 new rules for NDIS Drivers • Expanded rule set for WDM and KMDF • Reliability, security, IRQL levels, synchronization, correct object creation rules • Role types for WDM, KMDF, and NDIS drivers • Pre-condition rules • Per-entry verification • Engine improvements • Improved performance, scalability, and accuracy • Reduced non-useful result (NUR) especially for all rules

  13. PREfast for Drivers • Fast (2-5x compile time, usually) • Finds a lot of “inadvertent” errors and some “hard” ones • Works on code that compiles; doesn’t need to run

  14. PREfast for Drivers • Some errors PREfast can find • Null pointer, uninitialized variable (along an unusual path) • Local leaks (memory, resource) • Mismatched parameters • Failure to check result • Format/list mismatch • Misuse of some IRQLs • Various special cases that are easily missed • For example, Cancel IRQL • Proper use of callback/function pointers

  15. Problem: Driver-Specific Resource Leak #include <wdm.h> PKSPIN_LOCK MyLock; typedef struct { char buff[100]; } BufInfo; void LeakSample(BOOLEAN Option1) { NTSTATUS Status; KIRQL OldIrql; BufInfo *pBufInfo; KeAcquireSpinLock(MyLock,&OldIrql); //... if (Option1) { pBufInfo = ExAllocatePoolWithTag(NonPagedPool, sizeof(BufInfo), 'fuB_'); if (NULL==pBufInfo) { return STATUS_NO_MEMORY; } //... KeReleaseSpinLock(MyLock, OldIrql); return STATUS_SUCCESS; } //...

  16. Applying PFD: Driver Specific Resource Leak #include <wdm.h> PKSPIN_LOCK MyLock; typedef struct { char buff[100]; } BufInfo; void LeakSample(BOOLEAN Option1) { NTSTATUS Status; KIRQL OldIrql; BufInfo *pBufInfo; KeAcquireSpinLock(MyLock,&OldIrql); //... if (Option1) { pBufInfo = ExAllocatePoolWithTag(NonPagedPool, sizeof(BufInfo), 'fuB_'); if (NULL==pBufInfo) { return STATUS_NO_MEMORY; } //... KeReleaseSpinLock(MyLock, OldIrql); return STATUS_SUCCESS; } //... warning 8103: Leaking the resource stored in 'SpinLock:MyLock'.

  17. Solution: Driver-Specific Resource Leak #include <wdm.h> PKSPIN_LOCK MyLock; typedef struct { char buff[100]; } BufInfo; void LeakSample(BOOLEAN Option1) { NTSTATUS Status; KIRQL OldIrql; BufInfo *pBufInfo; KeAcquireSpinLock(MyLock,&OldIrql); //... if (Option1) { pBufInfo = ExAllocatePoolWithTag(NonPagedPool, sizeof(BufInfo), 'fuB_'); if (NULL==pBufInfo) { KeReleaseSpinLock(MyLock, OldIrql); return STATUS_NO_MEMORY; } //... KeReleaseSpinLock(MyLock, OldIrql); return STATUS_SUCCESS; } //...

  18. Why annotate?Good engineering practice • Precisely describe the “part” you’re building and the contract that represents • Enable automatic checking. Tells tools things they can’t infer. • Effective (and checked) documentation. • Programmers don’t have to guess/experiment. • Code and documentation don’t drift apart. • Comments are nice, but…

  19. Annotation Benefits • Record and express the contract • Developers know what the contract is • Is the contract a good one? • Automatic checking: That is, “cheap” bugs • The sooner a bug is found, the less expensive it is • Annotated code finds many more bugs (with less noise)than un-annotated code • Code that enters test with fewer “easy” bugs makes testing far more efficient—less wasted time for find/fix on easy bugs

  20. Example: Annotations wchar_t * wmemset( __out_ecount(s) wchar_t *p, __in wchar_t v, __in size_ts); • __in: the parameter is input to the function • __out: the parameter is output from the function • __out_ecount(s): the parameter is a buffer with s elements • If the parameter p doesn’t contain at least s elements, PFD will yield a warning

  21. Annotations: __in_opt, __out_opt… NTKERNELAPI BOOLEAN IoIs32bitProcess(     __in_opt PIRP Irp     );

  22. SDV Rules • SDV comes with: • 86 WDM rules • 75 KMDF rules • 39 NDIS rules • Each rule captures an aspect of an interface requirement • Rules are written in a C-like language and define: • State declarations in form of C-style variables • Events that are associated with DDI functions

  23. Example I/O System Irp Driver Callback Irp Irp IoCompleteRequest Device Driver Interface

  24. X Example I/O System Irp Driver Callback Irp Irp IoCompleteRequest DeviceDriver Interface

  25. Verification Sandwich Harnesses Callback-driven c1 c2 c3 c4 c5 c6 c7 c8 c9 Without Role types c1 c2 c3 c4 c5 c6 With Roletypes. KMDF Driver. KMDF & WDM stubs

  26. Role Type Declarations • Type def on a function • Pure C declaration, not a source annotation language (SAL) annotation • Can be used by client drivers to help static analysis tools identify the specific role of the function in relation to the driver model • Enables identification of specific callbacks in your driver • SDV is a callback driver verification tool • See WDK documentation in MSDN for a list of role types • 160 for KMDF • 60 for WDM • 40 for NDIS

  27. Role Types in the KMDF UsbSamp driver. DRIVER_INITIALIZE DriverEntry; EVT_WDF_DRIVER_DEVICE_ADD UsbSamp_EvtDeviceAdd; EVT_WDF_DEVICE_PREPARE_HARDWARE UsbSamp_EvtDevicePrepareHardware; EVT_WDF_DEVICE_RELEASE_HARDWARE UsbSamp_EvtDeviceReleaseHardware; EVT_WDF_DEVICE_FILE_CREATE UsbSamp_EvtDeviceFileCreate; EVT_WDF_IO_QUEUE_IO_READ UsbSamp_EvtIoRead; EVT_WDF_IO_QUEUE_IO_WRITE UsbSamp_EvtIoWrite; EVT_WDF_IO_QUEUE_IO_DEVICE_CONTROL UsbSamp_EvtIoDeviceControl; EVT_WDF_REQUEST_CANCEL UsbSamp_EvtRequestCancel; EVT_WDF_REQUEST_COMPLETION_ROUTINE SubRequestCompletionRoutine; EVT_WDF_REQUEST_COMPLETION_ROUTINE ReadWriteCompletion;

  28. Static Analysis Can… The business case: • Reduce risk of expensive after-deployment bugs • Reduce time to market • Reduce cost of code review and testing • Improve code quality • Achieve higher test coverage The development case: • Find and prevent bugs earlier • More directly and obviously • Find and prevent “hard to test” bugs • Make you more efficient

  29. Call To Action • Run static analysis tools early and often • Especially after modifying a driver • Use static analysis tools in addition to the debugger to identify runtime issues • Use static analysis tools wisely! • It doesn’t know about all possible errors • It doesn’t make testing unnecessary • Both false positives and false negatives can be misleading • Static Analysis Tools complement testing

  30. Additional Resources • White papers on the WHDC Web site • PREfast step-by-stephttp://www.microsoft.com/whdc/DevTools/tools/PREfast_steps.mspx • PREfast annotations http://www.microsoft.com/whdc/DevTools/tools/annotations.mspx • How to Use Function typedefs in C++ Driver Code to Improve PREƒast Resultshttp://go.microsoft.com/fwlink/?LinkId=87238 • Blog: http://blogs.msdn.com/staticdrivertools/default.aspx • PREfast for Drivers in WDK documentation on MSDNhttp://msdn.microsoft.com/en-us/library/aa468782.aspx • Chapter 23, Developing Drivers with the Windows Driver Foundationhttp://www.microsoft.com/MSPress/books/10512.aspx • E-mail sdvpfdex @ microsoft.com

  31. Related Sessions at DDC

More Related