blob: dfb606b8b88d88e29f54f3a065e1a1d591c9086e [file] [log] [blame]
Zhongxing Xu05a23382009-11-04 01:43:07 +00001//=== VLASizeChecker.cpp - Undefined dereference checker --------*- 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//
Ted Kremenekae162332009-11-07 03:56:57 +000010// This defines VLASizeChecker, a builtin check in GRExprEngine that
Zhongxing Xu05a23382009-11-04 01:43:07 +000011// performs checks for declaration of VLA of undefined or zero size.
12//
13//===----------------------------------------------------------------------===//
14
Ted Kremenek84b35952009-11-06 21:51:50 +000015#include "GRExprEngineInternalChecks.h"
Jordy Rose52e04c52010-07-05 00:50:15 +000016#include "clang/AST/CharUnits.h"
Benjamin Kramer5e2d2c22010-03-27 21:19:47 +000017#include "clang/Checker/BugReporter/BugType.h"
Ted Kremenek1309f9a2010-01-25 04:41:41 +000018#include "clang/Checker/PathSensitive/CheckerVisitor.h"
19#include "clang/Checker/PathSensitive/GRExprEngine.h"
Zhongxing Xu05a23382009-11-04 01:43:07 +000020
21using namespace clang;
22
Ted Kremenek84b35952009-11-06 21:51:50 +000023namespace {
Kovarththanan Rajaratnamba5fb5a2009-11-28 06:07:30 +000024class VLASizeChecker : public CheckerVisitor<VLASizeChecker> {
Ted Kremenekae162332009-11-07 03:56:57 +000025 BugType *BT_zero;
26 BugType *BT_undef;
Ted Kremenek84b35952009-11-06 21:51:50 +000027
28public:
Ted Kremenekae162332009-11-07 03:56:57 +000029 VLASizeChecker() : BT_zero(0), BT_undef(0) {}
30 static void *getTag() { static int tag = 0; return &tag; }
31 void PreVisitDeclStmt(CheckerContext &C, const DeclStmt *DS);
Ted Kremenek84b35952009-11-06 21:51:50 +000032};
33} // end anonymous namespace
34
35void clang::RegisterVLASizeChecker(GRExprEngine &Eng) {
Ted Kremenekae162332009-11-07 03:56:57 +000036 Eng.registerCheck(new VLASizeChecker());
Ted Kremenek84b35952009-11-06 21:51:50 +000037}
38
Ted Kremenekae162332009-11-07 03:56:57 +000039void VLASizeChecker::PreVisitDeclStmt(CheckerContext &C, const DeclStmt *DS) {
40 if (!DS->isSingleDecl())
41 return;
42
43 const VarDecl *VD = dyn_cast<VarDecl>(DS->getSingleDecl());
44 if (!VD)
45 return;
Jordy Rose52e04c52010-07-05 00:50:15 +000046
47 ASTContext &Ctx = C.getASTContext();
48 const VariableArrayType *VLA = Ctx.getAsVariableArrayType(VD->getType());
Ted Kremenekae162332009-11-07 03:56:57 +000049 if (!VLA)
50 return;
Zhongxing Xu05a23382009-11-04 01:43:07 +000051
Ted Kremenekae162332009-11-07 03:56:57 +000052 // FIXME: Handle multi-dimensional VLAs.
53 const Expr* SE = VLA->getSizeExpr();
54 const GRState *state = C.getState();
Ted Kremenek13976632010-02-08 16:18:51 +000055 SVal sizeV = state->getSVal(SE);
Zhongxing Xu05a23382009-11-04 01:43:07 +000056
Ted Kremenekae162332009-11-07 03:56:57 +000057 if (sizeV.isUndef()) {
58 // Generate an error node.
Ted Kremenek19d67b52009-11-23 22:22:01 +000059 ExplodedNode *N = C.GenerateSink();
Ted Kremenekae162332009-11-07 03:56:57 +000060 if (!N)
61 return;
62
63 if (!BT_undef)
64 BT_undef = new BuiltinBug("Declared variable-length array (VLA) uses a "
65 "garbage value as its size");
Zhongxing Xu05a23382009-11-04 01:43:07 +000066
Ted Kremenekae162332009-11-07 03:56:57 +000067 EnhancedBugReport *report =
Benjamin Kramerd02e2322009-11-14 12:08:24 +000068 new EnhancedBugReport(*BT_undef, BT_undef->getName(), N);
Ted Kremenekae162332009-11-07 03:56:57 +000069 report->addRange(SE->getSourceRange());
70 report->addVisitorCreator(bugreporter::registerTrackNullOrUndefValue, SE);
71 C.EmitReport(report);
72 return;
Zhongxing Xu05a23382009-11-04 01:43:07 +000073 }
Jordy Rose52e04c52010-07-05 00:50:15 +000074
75 // See if the size value is known. It can't be undefined because we would have
76 // warned about that already.
77 if (sizeV.isUnknown())
78 return;
Ted Kremenekae162332009-11-07 03:56:57 +000079
80 // Check if the size is zero.
Jordy Rose52e04c52010-07-05 00:50:15 +000081 DefinedSVal sizeD = cast<DefinedSVal>(sizeV);
Zhongxing Xu05a23382009-11-04 01:43:07 +000082
Ted Kremenekae162332009-11-07 03:56:57 +000083 const GRState *stateNotZero, *stateZero;
84 llvm::tie(stateNotZero, stateZero) = state->Assume(sizeD);
Zhongxing Xu05a23382009-11-04 01:43:07 +000085
Ted Kremenekae162332009-11-07 03:56:57 +000086 if (stateZero && !stateNotZero) {
Ted Kremenek19d67b52009-11-23 22:22:01 +000087 ExplodedNode* N = C.GenerateSink(stateZero);
Ted Kremenekae162332009-11-07 03:56:57 +000088 if (!BT_zero)
89 BT_zero = new BuiltinBug("Declared variable-length array (VLA) has zero "
90 "size");
Zhongxing Xu05a23382009-11-04 01:43:07 +000091
Ted Kremenekae162332009-11-07 03:56:57 +000092 EnhancedBugReport *report =
Benjamin Kramerd02e2322009-11-14 12:08:24 +000093 new EnhancedBugReport(*BT_zero, BT_zero->getName(), N);
Ted Kremenekae162332009-11-07 03:56:57 +000094 report->addRange(SE->getSourceRange());
95 report->addVisitorCreator(bugreporter::registerTrackNullOrUndefValue, SE);
96 C.EmitReport(report);
97 return;
Zhongxing Xu05a23382009-11-04 01:43:07 +000098 }
Ted Kremenekae162332009-11-07 03:56:57 +000099
100 // From this point on, assume that the size is not zero.
Jordy Rose52e04c52010-07-05 00:50:15 +0000101 state = stateNotZero;
102
103 // Convert the array length to size_t.
104 ValueManager &ValMgr = C.getValueManager();
105 SValuator &SV = ValMgr.getSValuator();
106 QualType SizeTy = Ctx.getSizeType();
107 NonLoc ArrayLength = cast<NonLoc>(SV.EvalCast(sizeD, SizeTy, SE->getType()));
108
109 // Get the element size.
110 CharUnits EleSize = Ctx.getTypeSizeInChars(VLA->getElementType());
111 SVal EleSizeVal = ValMgr.makeIntVal(EleSize.getQuantity(), SizeTy);
112
113 // Multiply the array length by the element size.
114 SVal ArraySizeVal = SV.EvalBinOpNN(state, BinaryOperator::Mul, ArrayLength,
115 cast<NonLoc>(EleSizeVal), SizeTy);
116
117 // Finally, Assume that the array's extent matches the given size.
118 const LocationContext *LC = C.getPredecessor()->getLocationContext();
119 DefinedOrUnknownSVal Extent = state->getRegion(VD, LC)->getExtent(ValMgr);
120 DefinedOrUnknownSVal ArraySize = cast<DefinedOrUnknownSVal>(ArraySizeVal);
121 DefinedOrUnknownSVal SizeIsKnown = SV.EvalEQ(state, Extent, ArraySize);
122 state = state->Assume(SizeIsKnown, true);
123
124 // Remember our assumptions!
125 C.addTransition(state);
Zhongxing Xu05a23382009-11-04 01:43:07 +0000126}