clang API Documentation

AttrNonNullChecker.cpp
Go to the documentation of this file.
00001 //===--- AttrNonNullChecker.h - Undefined arguments checker ----*- C++ -*--===//
00002 //
00003 //                     The LLVM Compiler Infrastructure
00004 //
00005 // This file is distributed under the University of Illinois Open Source
00006 // License. See LICENSE.TXT for details.
00007 //
00008 //===----------------------------------------------------------------------===//
00009 //
00010 // This defines AttrNonNullChecker, a builtin check in ExprEngine that 
00011 // performs checks for arguments declared to have nonnull attribute.
00012 //
00013 //===----------------------------------------------------------------------===//
00014 
00015 #include "ClangSACheckers.h"
00016 #include "clang/StaticAnalyzer/Core/Checker.h"
00017 #include "clang/StaticAnalyzer/Core/CheckerManager.h"
00018 #include "clang/StaticAnalyzer/Core/PathSensitive/CheckerContext.h"
00019 #include "clang/StaticAnalyzer/Core/BugReporter/BugType.h"
00020 
00021 using namespace clang;
00022 using namespace ento;
00023 
00024 namespace {
00025 class AttrNonNullChecker
00026   : public Checker< check::PreStmt<CallExpr> > {
00027   mutable OwningPtr<BugType> BT;
00028 public:
00029 
00030   void checkPreStmt(const CallExpr *CE, CheckerContext &C) const;
00031 };
00032 } // end anonymous namespace
00033 
00034 void AttrNonNullChecker::checkPreStmt(const CallExpr *CE,
00035                                       CheckerContext &C) const {
00036   ProgramStateRef state = C.getState();
00037   const LocationContext *LCtx = C.getLocationContext();
00038 
00039   // Check if the callee has a 'nonnull' attribute.
00040   SVal X = state->getSVal(CE->getCallee(), LCtx);
00041 
00042   const FunctionDecl *FD = X.getAsFunctionDecl();
00043   if (!FD)
00044     return;
00045 
00046   const NonNullAttr* Att = FD->getAttr<NonNullAttr>();
00047   if (!Att)
00048     return;
00049 
00050   // Iterate through the arguments of CE and check them for null.
00051   unsigned idx = 0;
00052 
00053   for (CallExpr::const_arg_iterator I=CE->arg_begin(), E=CE->arg_end(); I!=E;
00054        ++I, ++idx) {
00055 
00056     if (!Att->isNonNull(idx))
00057       continue;
00058 
00059     SVal V = state->getSVal(*I, LCtx);
00060     DefinedSVal *DV = dyn_cast<DefinedSVal>(&V);
00061 
00062     // If the value is unknown or undefined, we can't perform this check.
00063     if (!DV)
00064       continue;
00065 
00066     if (!isa<Loc>(*DV)) {
00067       // If the argument is a union type, we want to handle a potential
00068       // transparent_unoin GCC extension.
00069       QualType T = (*I)->getType();
00070       const RecordType *UT = T->getAsUnionType();
00071       if (!UT || !UT->getDecl()->hasAttr<TransparentUnionAttr>())
00072         continue;
00073       if (nonloc::CompoundVal *CSV = dyn_cast<nonloc::CompoundVal>(DV)) {
00074         nonloc::CompoundVal::iterator CSV_I = CSV->begin();
00075         assert(CSV_I != CSV->end());
00076         V = *CSV_I;
00077         DV = dyn_cast<DefinedSVal>(&V);
00078         assert(++CSV_I == CSV->end());
00079         if (!DV)
00080           continue;        
00081       }
00082       else {
00083         // FIXME: Handle LazyCompoundVals?
00084         continue;
00085       }
00086     }
00087 
00088     ConstraintManager &CM = C.getConstraintManager();
00089     ProgramStateRef stateNotNull, stateNull;
00090     llvm::tie(stateNotNull, stateNull) = CM.assumeDual(state, *DV);
00091 
00092     if (stateNull && !stateNotNull) {
00093       // Generate an error node.  Check for a null node in case
00094       // we cache out.
00095       if (ExplodedNode *errorNode = C.generateSink(stateNull)) {
00096 
00097         // Lazily allocate the BugType object if it hasn't already been
00098         // created. Ownership is transferred to the BugReporter object once
00099         // the BugReport is passed to 'EmitWarning'.
00100         if (!BT)
00101           BT.reset(new BugType("Argument with 'nonnull' attribute passed null",
00102                                "API"));
00103 
00104         BugReport *R =
00105           new BugReport(*BT, "Null pointer passed as an argument to a "
00106                              "'nonnull' parameter", errorNode);
00107 
00108         // Highlight the range of the argument that was null.
00109         const Expr *arg = *I;
00110         R->addRange(arg->getSourceRange());
00111         R->addVisitor(bugreporter::getTrackNullOrUndefValueVisitor(errorNode,
00112                                                                    arg, R));
00113         // Emit the bug report.
00114         C.EmitReport(R);
00115       }
00116 
00117       // Always return.  Either we cached out or we just emitted an error.
00118       return;
00119     }
00120 
00121     // If a pointer value passed the check we should assume that it is
00122     // indeed not null from this point forward.
00123     assert(stateNotNull);
00124     state = stateNotNull;
00125   }
00126 
00127   // If we reach here all of the arguments passed the nonnull check.
00128   // If 'state' has been updated generated a new node.
00129   C.addTransition(state);
00130 }
00131 
00132 void ento::registerAttrNonNullChecker(CheckerManager &mgr) {
00133   mgr.registerChecker<AttrNonNullChecker>();
00134 }