Ted Kremenek | 381d1bf | 2010-02-25 00:20:35 +0000 | [diff] [blame] | 1 | //= UnixAPIChecker.h - Checks preconditions for various Unix APIs --*- C++ -*-// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
| 10 | // This defines UnixAPIChecker, which is an assortment of checks on calls |
| 11 | // to various, widely used UNIX/Posix functions. |
| 12 | // |
| 13 | //===----------------------------------------------------------------------===// |
| 14 | |
| 15 | #include "clang/Checker/PathSensitive/CheckerVisitor.h" |
| 16 | #include "clang/Checker/BugReporter/BugReporter.h" |
| 17 | #include "clang/Checker/PathSensitive/GRStateTrait.h" |
| 18 | #include "llvm/ADT/StringSwitch.h" |
| 19 | #include "GRExprEngineInternalChecks.h" |
| 20 | #include <fcntl.h> |
| 21 | |
| 22 | using namespace clang; |
| 23 | |
| 24 | namespace { |
| 25 | class UnixAPIChecker : public CheckerVisitor<UnixAPIChecker> { |
| 26 | enum SubChecks { |
| 27 | OpenFn = 0, |
| 28 | NumChecks |
| 29 | }; |
| 30 | |
| 31 | BugType *BTypes[NumChecks]; |
| 32 | |
| 33 | public: |
| 34 | UnixAPIChecker() { memset(BTypes, 0, sizeof(*BTypes) * NumChecks); } |
| 35 | static void *getTag() { static unsigned tag = 0; return &tag; } |
| 36 | |
| 37 | void PreVisitCallExpr(CheckerContext &C, const CallExpr *CE); |
| 38 | }; |
| 39 | } //end anonymous namespace |
| 40 | |
| 41 | void clang::RegisterUnixAPIChecker(GRExprEngine &Eng) { |
| 42 | Eng.registerCheck(new UnixAPIChecker()); |
| 43 | } |
| 44 | |
| 45 | //===----------------------------------------------------------------------===// |
| 46 | // Utility functions. |
| 47 | //===----------------------------------------------------------------------===// |
| 48 | |
| 49 | static inline void LazyInitialize(BugType *&BT, const char *name) { |
| 50 | if (BT) |
| 51 | return; |
| 52 | BT = new BugType(name, "Unix API"); |
| 53 | } |
| 54 | |
| 55 | //===----------------------------------------------------------------------===// |
| 56 | // "open" (man 2 open) |
| 57 | //===----------------------------------------------------------------------===// |
| 58 | |
| 59 | static void CheckOpen(CheckerContext &C, const CallExpr *CE, BugType *&BT) { |
| 60 | LazyInitialize(BT, "Improper use of 'open'"); |
| 61 | |
| 62 | // Look at the 'oflags' argument for the O_CREAT flag. |
| 63 | const GRState *state = C.getState(); |
| 64 | |
| 65 | if (CE->getNumArgs() < 2) { |
| 66 | // The frontend should issue a warning for this case, so this is a sanity |
| 67 | // check. |
| 68 | return; |
| 69 | } |
| 70 | |
| 71 | // Now check if oflags has O_CREAT set. |
| 72 | const Expr *oflagsEx = CE->getArg(1); |
| 73 | const SVal V = state->getSVal(oflagsEx); |
| 74 | if (!isa<NonLoc>(V)) { |
| 75 | // The case where 'V' can be a location can only be due to a bad header, |
| 76 | // so in this case bail out. |
| 77 | return; |
| 78 | } |
| 79 | NonLoc oflags = cast<NonLoc>(V); |
| 80 | NonLoc ocreateFlag = |
| 81 | cast<NonLoc>(C.getValueManager().makeIntVal((uint64_t) O_CREAT, |
| 82 | oflagsEx->getType())); |
| 83 | SVal maskedFlagsUC = C.getSValuator().EvalBinOpNN(state, BinaryOperator::And, |
| 84 | oflags, ocreateFlag, |
| 85 | oflagsEx->getType()); |
| 86 | if (maskedFlagsUC.isUnknownOrUndef()) |
| 87 | return; |
| 88 | DefinedSVal maskedFlags = cast<DefinedSVal>(maskedFlagsUC); |
| 89 | |
| 90 | // Check if maskedFlags is non-zero. |
| 91 | const GRState *trueState, *falseState; |
| 92 | llvm::tie(trueState, falseState) = state->Assume(maskedFlags); |
| 93 | |
| 94 | // Only emit an error if the value of 'maskedFlags' is properly |
| 95 | // constrained; |
| 96 | if (!(trueState && !falseState)) |
| 97 | return; |
| 98 | |
| 99 | if (CE->getNumArgs() < 3) { |
| 100 | ExplodedNode *N = C.GenerateSink(trueState); |
Ted Kremenek | c757d79 | 2010-02-25 05:44:05 +0000 | [diff] [blame] | 101 | if (!N) |
| 102 | return; |
| 103 | |
Ted Kremenek | 381d1bf | 2010-02-25 00:20:35 +0000 | [diff] [blame] | 104 | EnhancedBugReport *report = |
| 105 | new EnhancedBugReport(*BT, |
| 106 | "Call to 'open' requires a third argument when " |
| 107 | "the 'O_CREAT' flag is set", N); |
| 108 | report->addRange(oflagsEx->getSourceRange()); |
| 109 | C.EmitReport(report); |
| 110 | } |
| 111 | } |
| 112 | |
| 113 | //===----------------------------------------------------------------------===// |
| 114 | // Central dispatch function. |
| 115 | //===----------------------------------------------------------------------===// |
| 116 | |
| 117 | typedef void (*SubChecker)(CheckerContext &C, const CallExpr *CE, BugType *&BT); |
| 118 | namespace { |
| 119 | class SubCheck { |
| 120 | SubChecker SC; |
| 121 | BugType **BT; |
| 122 | public: |
| 123 | SubCheck(SubChecker sc, BugType *& bt) : SC(sc), BT(&bt) {} |
| 124 | SubCheck() : SC(NULL), BT(NULL) {} |
| 125 | |
| 126 | void run(CheckerContext &C, const CallExpr *CE) const { |
| 127 | if (SC) |
| 128 | SC(C, CE, *BT); |
| 129 | } |
| 130 | }; |
| 131 | } // end anonymous namespace |
| 132 | |
| 133 | void UnixAPIChecker::PreVisitCallExpr(CheckerContext &C, const CallExpr *CE) { |
| 134 | // Get the callee. All the functions we care about are C functions |
| 135 | // with simple identifiers. |
| 136 | const GRState *state = C.getState(); |
| 137 | const Expr *Callee = CE->getCallee(); |
| 138 | const FunctionTextRegion *Fn = |
| 139 | dyn_cast_or_null<FunctionTextRegion>(state->getSVal(Callee).getAsRegion()); |
| 140 | |
| 141 | if (!Fn) |
| 142 | return; |
| 143 | |
| 144 | const IdentifierInfo *FI = Fn->getDecl()->getIdentifier(); |
| 145 | if (!FI) |
| 146 | return; |
| 147 | |
| 148 | const SubCheck &SC = |
| 149 | llvm::StringSwitch<SubCheck>(FI->getName()) |
| 150 | .Case("open", SubCheck(CheckOpen, BTypes[OpenFn])) |
| 151 | .Default(SubCheck()); |
| 152 | |
| 153 | SC.run(C, CE); |
| 154 | } |