PREfast backgrounder

I’ve been a happy user of PREfast for a couple of years now. It has quickly become as imperative for driver development as Driver Verifier. I’ve written a little about it before, but I thought I’d collect some of my past ramblings into one PREfast background post.

PREfast is a static source code analysis tool that ships with the DDK. If you’ve used lint, you’ll feel fairly at home using PREfast. It can be used with kernel-mode or user-mode code, and it can do simple C-based analysis (e.g. possible NULL pointer deref) as well as more complex OS-specific analysis (e.g. You can’t call KeAcquireSpinLockAtDpcLevel because you’re at PASSIVE_LEVEL).

To use PREfast, you simply open up a build window and feed it your standard build command:

C:\\dev\\sandbox\\scratch\\driver>prefast build -cZ
--------------------------------------------------------
Microsoft (R) PREfast Version 8.0.72190.
Copyright (C) Microsoft Corporation. All rights reserved.
--------------------------------------------------------
BUILD: Compile and Link for x86
BUILD: Start time: Sun Jul 30 22:04:54 2006
BUILD: Examining c:\\dev\\sandbox\\scratch\\driver directory
for files to compile
    c:\\dev\\sandbox\\scratch\\driver
BUILD: Compiling and Linking c:\\dev\\sandbox\\scratch\\driver
directory
Precompiling - pch.h
Compiling - sdriver.c
Compiling - sdriver.c
Linking Executable - objfre_wlh_x86\\i386\\sdriver.sys
BUILD: Finish time: Sun Jul 30 22:04:59 2006
BUILD: Done

    6 files compiled
    1 executable built
----------------------------------------------------------
Removing duplicate defects from the log...
----------------------------------------------------------
PREfast reported 8 defects during execution of the command.
----------------------------------------------------------
Enter PREFAST LIST to list the defect log as text within the
console.
Enter PREFAST VIEW to display the defect log user interface.

PREfast then compiles your code (twice usually; more on that later) and analyzes it in the process. You can then see what PREfast found by typing either of the two commands it so helpfully supplies for you: “prefast list” will get you a text-based list of what defects were found, and “prefast view” will get you a nice GUI with similar information.

A couple of caveats:

  • You can’t use it with 64-bit builds yet. It finds various problems in various places. This should be fixed soon, but meanwhile, run PREfast on 32-bit builds.
  • You have to supply a full build command, not a doskey macro, on the prefast command line. That means prefast build -cZ, NOT prefast bcz.
  • If you set up your own build window with setenv, be sure you pass a fully qualified path – it has to start with a drive letter, not a backslash.

PREfast is one of the best tools in the DDK. It has saved me dozens of bugs in the time I’ve been using it. Give it a try – it’s free and painless, and you don’t even have to spend time setting it up.

Leave a Reply