Zhongxing Xu | 7c9624b | 2009-12-08 09:07:59 +0000 | [diff] [blame] | 1 | //=== BuiltinFunctionChecker.cpp --------------------------------*- 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 checker evaluates clang builtin functions. |
| 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | |
Argyrios Kyrtzidis | 1034870 | 2011-02-28 01:27:07 +0000 | [diff] [blame] | 14 | #include "ClangSACheckers.h" |
Argyrios Kyrtzidis | ec8605f | 2011-03-01 01:16:21 +0000 | [diff] [blame] | 15 | #include "clang/StaticAnalyzer/Core/Checker.h" |
Argyrios Kyrtzidis | 1034870 | 2011-02-28 01:27:07 +0000 | [diff] [blame] | 16 | #include "clang/StaticAnalyzer/Core/CheckerManager.h" |
| 17 | #include "clang/StaticAnalyzer/Core/PathSensitive/CheckerContext.h" |
Zhongxing Xu | 7c9624b | 2009-12-08 09:07:59 +0000 | [diff] [blame] | 18 | #include "clang/Basic/Builtins.h" |
Zhongxing Xu | 7c9624b | 2009-12-08 09:07:59 +0000 | [diff] [blame] | 19 | |
| 20 | using namespace clang; |
Ted Kremenek | 9ef6537 | 2010-12-23 07:20:52 +0000 | [diff] [blame] | 21 | using namespace ento; |
Zhongxing Xu | 7c9624b | 2009-12-08 09:07:59 +0000 | [diff] [blame] | 22 | |
| 23 | namespace { |
| 24 | |
Argyrios Kyrtzidis | ec8605f | 2011-03-01 01:16:21 +0000 | [diff] [blame] | 25 | class BuiltinFunctionChecker : public Checker<eval::Call> { |
Zhongxing Xu | 7c9624b | 2009-12-08 09:07:59 +0000 | [diff] [blame] | 26 | public: |
Argyrios Kyrtzidis | 1034870 | 2011-02-28 01:27:07 +0000 | [diff] [blame] | 27 | bool evalCall(const CallExpr *CE, CheckerContext &C) const; |
Zhongxing Xu | 7c9624b | 2009-12-08 09:07:59 +0000 | [diff] [blame] | 28 | }; |
| 29 | |
| 30 | } |
| 31 | |
Argyrios Kyrtzidis | 1034870 | 2011-02-28 01:27:07 +0000 | [diff] [blame] | 32 | bool BuiltinFunctionChecker::evalCall(const CallExpr *CE, |
| 33 | CheckerContext &C) const{ |
Ted Kremenek | 18c66fd | 2011-08-15 22:09:50 +0000 | [diff] [blame] | 34 | const ProgramState *state = C.getState(); |
Zhongxing Xu | 7c9624b | 2009-12-08 09:07:59 +0000 | [diff] [blame] | 35 | const Expr *Callee = CE->getCallee(); |
Ted Kremenek | 1397663 | 2010-02-08 16:18:51 +0000 | [diff] [blame] | 36 | SVal L = state->getSVal(Callee); |
Zhongxing Xu | 7c9624b | 2009-12-08 09:07:59 +0000 | [diff] [blame] | 37 | const FunctionDecl *FD = L.getAsFunctionDecl(); |
| 38 | |
| 39 | if (!FD) |
| 40 | return false; |
| 41 | |
| 42 | unsigned id = FD->getBuiltinID(); |
| 43 | |
| 44 | if (!id) |
| 45 | return false; |
| 46 | |
| 47 | switch (id) { |
| 48 | case Builtin::BI__builtin_expect: { |
| 49 | // For __builtin_expect, just return the value of the subexpression. |
| 50 | assert (CE->arg_begin() != CE->arg_end()); |
Ted Kremenek | 1397663 | 2010-02-08 16:18:51 +0000 | [diff] [blame] | 51 | SVal X = state->getSVal(*(CE->arg_begin())); |
Ted Kremenek | d048c6e | 2010-12-20 21:19:09 +0000 | [diff] [blame] | 52 | C.generateNode(state->BindExpr(CE, X)); |
Zhongxing Xu | 7c9624b | 2009-12-08 09:07:59 +0000 | [diff] [blame] | 53 | return true; |
| 54 | } |
| 55 | |
| 56 | case Builtin::BI__builtin_alloca: { |
| 57 | // FIXME: Refactor into StoreManager itself? |
| 58 | MemRegionManager& RM = C.getStoreManager().getRegionManager(); |
Jordy Rose | 32f2656 | 2010-07-04 00:00:41 +0000 | [diff] [blame] | 59 | const AllocaRegion* R = |
Anna Zaks | 5d0ea6d | 2011-10-04 20:43:05 +0000 | [diff] [blame^] | 60 | RM.getAllocaRegion(CE, C.getCurrentBlockCount(), |
Zhongxing Xu | 7c9624b | 2009-12-08 09:07:59 +0000 | [diff] [blame] | 61 | C.getPredecessor()->getLocationContext()); |
| 62 | |
| 63 | // Set the extent of the region in bytes. This enables us to use the |
| 64 | // SVal of the argument directly. If we save the extent in bits, we |
| 65 | // cannot represent values like symbol*8. |
Jordy Rose | 32f2656 | 2010-07-04 00:00:41 +0000 | [diff] [blame] | 66 | DefinedOrUnknownSVal Size = |
| 67 | cast<DefinedOrUnknownSVal>(state->getSVal(*(CE->arg_begin()))); |
| 68 | |
Ted Kremenek | c8413fd | 2010-12-02 07:49:45 +0000 | [diff] [blame] | 69 | SValBuilder& svalBuilder = C.getSValBuilder(); |
| 70 | DefinedOrUnknownSVal Extent = R->getExtent(svalBuilder); |
| 71 | DefinedOrUnknownSVal extentMatchesSizeArg = |
Ted Kremenek | 9c14953 | 2010-12-01 21:57:22 +0000 | [diff] [blame] | 72 | svalBuilder.evalEQ(state, Extent, Size); |
Ted Kremenek | c8413fd | 2010-12-02 07:49:45 +0000 | [diff] [blame] | 73 | state = state->assume(extentMatchesSizeArg, true); |
Jordy Rose | 32f2656 | 2010-07-04 00:00:41 +0000 | [diff] [blame] | 74 | |
Ted Kremenek | d048c6e | 2010-12-20 21:19:09 +0000 | [diff] [blame] | 75 | C.generateNode(state->BindExpr(CE, loc::MemRegionVal(R))); |
Zhongxing Xu | 7c9624b | 2009-12-08 09:07:59 +0000 | [diff] [blame] | 76 | return true; |
| 77 | } |
| 78 | } |
| 79 | |
| 80 | return false; |
| 81 | } |
Argyrios Kyrtzidis | 1034870 | 2011-02-28 01:27:07 +0000 | [diff] [blame] | 82 | |
| 83 | void ento::registerBuiltinFunctionChecker(CheckerManager &mgr) { |
| 84 | mgr.registerChecker<BuiltinFunctionChecker>(); |
| 85 | } |