23 #include "llvm/ADT/STLExtras.h" 24 #include "llvm/ADT/SmallString.h" 25 #include "llvm/Support/raw_ostream.h" 27 using namespace clang;
31 class VLASizeChecker :
public Checker< check::PreStmt<DeclStmt> > {
32 mutable std::unique_ptr<BugType> BT;
33 enum VLASize_Kind { VLA_Garbage, VLA_Zero, VLA_Tainted, VLA_Negative };
37 std::unique_ptr<BugReporterVisitor> Visitor =
nullptr)
const;
40 void checkPreStmt(
const DeclStmt *DS, CheckerContext &C)
const;
44 void VLASizeChecker::reportBug(
46 CheckerContext &C, std::unique_ptr<BugReporterVisitor> Visitor)
const {
48 ExplodedNode *N = C.generateErrorNode(State);
53 BT.reset(
new BuiltinBug(
54 this,
"Dangerous variable-length array (VLA) declaration"));
57 llvm::raw_svector_ostream os(buf);
58 os <<
"Declared variable-length array (VLA) ";
61 os <<
"uses a garbage value as its size";
64 os <<
"has zero size";
67 os <<
"has tainted size";
70 os <<
"has negative size";
74 auto report = llvm::make_unique<BugReport>(*BT, os.str(), N);
75 report->addVisitor(std::move(Visitor));
77 bugreporter::trackExpressionValue(N, SizeE, *report);
78 C.emitReport(std::move(report));
81 void VLASizeChecker::checkPreStmt(
const DeclStmt *DS, CheckerContext &C)
const {
97 SVal sizeV = C.getSVal(SE);
99 if (sizeV.isUndef()) {
100 reportBug(VLA_Garbage, SE, state, C);
106 if (sizeV.isUnknown())
110 if (state->isTainted(sizeV)) {
111 reportBug(VLA_Tainted, SE,
nullptr, C,
112 llvm::make_unique<TaintBugVisitor>(sizeV));
117 DefinedSVal sizeD = sizeV.castAs<DefinedSVal>();
120 std::tie(stateNotZero, stateZero) = state->assume(sizeD);
122 if (stateZero && !stateNotZero) {
123 reportBug(VLA_Zero, SE, stateZero, C);
128 state = stateNotZero;
135 SValBuilder &svalBuilder = C.getSValBuilder();
138 DefinedOrUnknownSVal Zero = svalBuilder.makeZeroVal(Ty);
140 SVal LessThanZeroVal = svalBuilder.evalBinOp(state, BO_LT, sizeD, Zero, Ty);
142 LessThanZeroVal.getAs<DefinedSVal>()) {
143 ConstraintManager &CM = C.getConstraintManager();
146 std::tie(StateNeg, StatePos) = CM.assumeDual(state, *LessThanZeroDVal);
147 if (StateNeg && !StatePos) {
148 reportBug(VLA_Negative, SE, state, C);
157 svalBuilder.evalCast(sizeD, SizeTy, SE->
getType()).castAs<NonLoc>();
161 SVal EleSizeVal = svalBuilder.makeIntVal(EleSize.
getQuantity(), SizeTy);
164 SVal ArraySizeVal = svalBuilder.evalBinOpNN(
165 state, BO_Mul, ArrayLength, EleSizeVal.castAs<NonLoc>(), SizeTy);
169 DefinedOrUnknownSVal Extent =
170 state->getRegion(VD, LC)->getExtent(svalBuilder);
171 DefinedOrUnknownSVal ArraySize = ArraySizeVal.castAs<DefinedOrUnknownSVal>();
172 DefinedOrUnknownSVal sizeIsKnown =
173 svalBuilder.evalEQ(state, Extent, ArraySize);
174 state = state->assume(sizeIsKnown,
true);
180 C.addTransition(state);
183 void ento::registerVLASizeChecker(CheckerManager &mgr) {
184 mgr.registerChecker<VLASizeChecker>();
A (possibly-)qualified type.
IntrusiveRefCntPtr< const ProgramState > ProgramStateRef
QualType getElementType() const
Represents a variable declaration or definition.
Holds long-lived AST nodes (such as types and decls) that can be referred to throughout the semantic ...
i32 captured_struct **param SharedsTy A type which contains references the shared variables *param Shareds Context with the list of shared variables from the p *TaskFunction *param Data Additional data for task generation like final * state
CharUnits - This is an opaque type for sizes expressed in character units.
Expr * getSizeExpr() const
QuantityType getQuantity() const
getQuantity - Get the raw integer representation of this quantity.
This represents one expression.
DeclStmt - Adaptor class for mixing declarations with statements and expressions. ...
Dataflow Directional Tag Classes.
const Decl * getSingleDecl() const
bool isSingleDecl() const
isSingleDecl - This method returns true if this DeclStmt refers to a single Decl. ...
SourceRange getSourceRange() const LLVM_READONLY
SourceLocation tokens are not useful in isolation - they are low level value objects created/interpre...
const VariableArrayType * getAsVariableArrayType(QualType T) const
CharUnits getTypeSizeInChars(QualType T) const
Return the size of the specified (complete) type T, in characters.
Represents a C array with a specified size that is not an integer-constant-expression.
CanQualType getSizeType() const
Return the unique type for "size_t" (C99 7.17), defined in <stddef.h>.