17330f729Sjoerg //=== StdLibraryFunctionsChecker.cpp - Model standard functions -*- C++ -*-===//
27330f729Sjoerg //
37330f729Sjoerg // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
47330f729Sjoerg // See https://llvm.org/LICENSE.txt for license information.
57330f729Sjoerg // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
67330f729Sjoerg //
77330f729Sjoerg //===----------------------------------------------------------------------===//
87330f729Sjoerg //
97330f729Sjoerg // This checker improves modeling of a few simple library functions.
107330f729Sjoerg //
11*e038c9c4Sjoerg // This checker provides a specification format - `Summary' - and
127330f729Sjoerg // contains descriptions of some library functions in this format. Each
137330f729Sjoerg // specification contains a list of branches for splitting the program state
147330f729Sjoerg // upon call, and range constraints on argument and return-value symbols that
157330f729Sjoerg // are satisfied on each branch. This spec can be expanded to include more
167330f729Sjoerg // items, like external effects of the function.
177330f729Sjoerg //
187330f729Sjoerg // The main difference between this approach and the body farms technique is
197330f729Sjoerg // in more explicit control over how many branches are produced. For example,
207330f729Sjoerg // consider standard C function `ispunct(int x)', which returns a non-zero value
217330f729Sjoerg // iff `x' is a punctuation character, that is, when `x' is in range
227330f729Sjoerg // ['!', '/'] [':', '@'] U ['[', '\`'] U ['{', '~'].
23*e038c9c4Sjoerg // `Summary' provides only two branches for this function. However,
247330f729Sjoerg // any attempt to describe this range with if-statements in the body farm
257330f729Sjoerg // would result in many more branches. Because each branch needs to be analyzed
267330f729Sjoerg // independently, this significantly reduces performance. Additionally,
277330f729Sjoerg // once we consider a branch on which `x' is in range, say, ['!', '/'],
287330f729Sjoerg // we assume that such branch is an important separate path through the program,
297330f729Sjoerg // which may lead to false positives because considering this particular path
307330f729Sjoerg // was not consciously intended, and therefore it might have been unreachable.
317330f729Sjoerg //
32*e038c9c4Sjoerg // This checker uses eval::Call for modeling pure functions (functions without
33*e038c9c4Sjoerg // side effets), for which their `Summary' is a precise model. This avoids
34*e038c9c4Sjoerg // unnecessary invalidation passes. Conflicts with other checkers are unlikely
35*e038c9c4Sjoerg // because if the function has no other effects, other checkers would probably
36*e038c9c4Sjoerg // never want to improve upon the modeling done by this checker.
377330f729Sjoerg //
38*e038c9c4Sjoerg // Non-pure functions, for which only partial improvement over the default
397330f729Sjoerg // behavior is expected, are modeled via check::PostCall, non-intrusively.
407330f729Sjoerg //
417330f729Sjoerg // The following standard C functions are currently supported:
427330f729Sjoerg //
43*e038c9c4Sjoerg // fgetc getline isdigit isupper toascii
447330f729Sjoerg // fread isalnum isgraph isxdigit
457330f729Sjoerg // fwrite isalpha islower read
467330f729Sjoerg // getc isascii isprint write
47*e038c9c4Sjoerg // getchar isblank ispunct toupper
48*e038c9c4Sjoerg // getdelim iscntrl isspace tolower
497330f729Sjoerg //
507330f729Sjoerg //===----------------------------------------------------------------------===//
517330f729Sjoerg
527330f729Sjoerg #include "clang/StaticAnalyzer/Checkers/BuiltinCheckerRegistration.h"
53*e038c9c4Sjoerg #include "clang/StaticAnalyzer/Core/BugReporter/BugType.h"
547330f729Sjoerg #include "clang/StaticAnalyzer/Core/Checker.h"
557330f729Sjoerg #include "clang/StaticAnalyzer/Core/CheckerManager.h"
567330f729Sjoerg #include "clang/StaticAnalyzer/Core/PathSensitive/CallEvent.h"
577330f729Sjoerg #include "clang/StaticAnalyzer/Core/PathSensitive/CheckerContext.h"
58*e038c9c4Sjoerg #include "clang/StaticAnalyzer/Core/PathSensitive/CheckerHelpers.h"
59*e038c9c4Sjoerg #include "clang/StaticAnalyzer/Core/PathSensitive/DynamicExtent.h"
60*e038c9c4Sjoerg #include "llvm/ADT/SmallString.h"
61*e038c9c4Sjoerg #include "llvm/ADT/StringExtras.h"
62*e038c9c4Sjoerg
63*e038c9c4Sjoerg #include <string>
647330f729Sjoerg
657330f729Sjoerg using namespace clang;
667330f729Sjoerg using namespace clang::ento;
677330f729Sjoerg
687330f729Sjoerg namespace {
69*e038c9c4Sjoerg class StdLibraryFunctionsChecker
70*e038c9c4Sjoerg : public Checker<check::PreCall, check::PostCall, eval::Call> {
71*e038c9c4Sjoerg
72*e038c9c4Sjoerg class Summary;
737330f729Sjoerg
747330f729Sjoerg /// Specify how much the analyzer engine should entrust modeling this function
757330f729Sjoerg /// to us. If he doesn't, he performs additional invalidations.
76*e038c9c4Sjoerg enum InvalidationKind { NoEvalCall, EvalCallAsPure };
777330f729Sjoerg
787330f729Sjoerg // The universal integral type to use in value range descriptions.
797330f729Sjoerg // Unsigned to make sure overflows are well-defined.
80*e038c9c4Sjoerg typedef uint64_t RangeInt;
817330f729Sjoerg
827330f729Sjoerg /// Normally, describes a single range constraint, eg. {{0, 1}, {3, 4}} is
837330f729Sjoerg /// a non-negative integer, which less than 5 and not equal to 2. For
847330f729Sjoerg /// `ComparesToArgument', holds information about how exactly to compare to
857330f729Sjoerg /// the argument.
86*e038c9c4Sjoerg typedef std::vector<std::pair<RangeInt, RangeInt>> IntRangeVector;
877330f729Sjoerg
887330f729Sjoerg /// A reference to an argument or return value by its number.
897330f729Sjoerg /// ArgNo in CallExpr and CallEvent is defined as Unsigned, but
907330f729Sjoerg /// obviously uint32_t should be enough for all practical purposes.
91*e038c9c4Sjoerg typedef uint32_t ArgNo;
92*e038c9c4Sjoerg static const ArgNo Ret;
937330f729Sjoerg
94*e038c9c4Sjoerg /// Returns the string representation of an argument index.
95*e038c9c4Sjoerg /// E.g.: (1) -> '1st arg', (2) - > '2nd arg'
96*e038c9c4Sjoerg static SmallString<8> getArgDesc(ArgNo);
97*e038c9c4Sjoerg
98*e038c9c4Sjoerg class ValueConstraint;
99*e038c9c4Sjoerg
100*e038c9c4Sjoerg // Pointer to the ValueConstraint. We need a copyable, polymorphic and
101*e038c9c4Sjoerg // default initialize able type (vector needs that). A raw pointer was good,
102*e038c9c4Sjoerg // however, we cannot default initialize that. unique_ptr makes the Summary
103*e038c9c4Sjoerg // class non-copyable, therefore not an option. Releasing the copyability
104*e038c9c4Sjoerg // requirement would render the initialization of the Summary map infeasible.
105*e038c9c4Sjoerg using ValueConstraintPtr = std::shared_ptr<ValueConstraint>;
106*e038c9c4Sjoerg
107*e038c9c4Sjoerg /// Polymorphic base class that represents a constraint on a given argument
108*e038c9c4Sjoerg /// (or return value) of a function. Derived classes implement different kind
109*e038c9c4Sjoerg /// of constraints, e.g range constraints or correlation between two
110*e038c9c4Sjoerg /// arguments.
111*e038c9c4Sjoerg class ValueConstraint {
112*e038c9c4Sjoerg public:
ValueConstraint(ArgNo ArgN)113*e038c9c4Sjoerg ValueConstraint(ArgNo ArgN) : ArgN(ArgN) {}
~ValueConstraint()114*e038c9c4Sjoerg virtual ~ValueConstraint() {}
115*e038c9c4Sjoerg /// Apply the effects of the constraint on the given program state. If null
116*e038c9c4Sjoerg /// is returned then the constraint is not feasible.
117*e038c9c4Sjoerg virtual ProgramStateRef apply(ProgramStateRef State, const CallEvent &Call,
118*e038c9c4Sjoerg const Summary &Summary,
119*e038c9c4Sjoerg CheckerContext &C) const = 0;
negate() const120*e038c9c4Sjoerg virtual ValueConstraintPtr negate() const {
121*e038c9c4Sjoerg llvm_unreachable("Not implemented");
122*e038c9c4Sjoerg };
123*e038c9c4Sjoerg
124*e038c9c4Sjoerg // Check whether the constraint is malformed or not. It is malformed if the
125*e038c9c4Sjoerg // specified argument has a mismatch with the given FunctionDecl (e.g. the
126*e038c9c4Sjoerg // arg number is out-of-range of the function's argument list).
checkValidity(const FunctionDecl * FD) const127*e038c9c4Sjoerg bool checkValidity(const FunctionDecl *FD) const {
128*e038c9c4Sjoerg const bool ValidArg = ArgN == Ret || ArgN < FD->getNumParams();
129*e038c9c4Sjoerg assert(ValidArg && "Arg out of range!");
130*e038c9c4Sjoerg if (!ValidArg)
131*e038c9c4Sjoerg return false;
132*e038c9c4Sjoerg // Subclasses may further refine the validation.
133*e038c9c4Sjoerg return checkSpecificValidity(FD);
134*e038c9c4Sjoerg }
getArgNo() const135*e038c9c4Sjoerg ArgNo getArgNo() const { return ArgN; }
136*e038c9c4Sjoerg
137*e038c9c4Sjoerg // Return those arguments that should be tracked when we report a bug. By
138*e038c9c4Sjoerg // default it is the argument that is constrained, however, in some special
139*e038c9c4Sjoerg // cases we need to track other arguments as well. E.g. a buffer size might
140*e038c9c4Sjoerg // be encoded in another argument.
getArgsToTrack() const141*e038c9c4Sjoerg virtual std::vector<ArgNo> getArgsToTrack() const { return {ArgN}; }
142*e038c9c4Sjoerg
143*e038c9c4Sjoerg virtual StringRef getName() const = 0;
144*e038c9c4Sjoerg
145*e038c9c4Sjoerg // Give a description that explains the constraint to the user. Used when
146*e038c9c4Sjoerg // the bug is reported.
describe(ProgramStateRef State,const Summary & Summary) const147*e038c9c4Sjoerg virtual std::string describe(ProgramStateRef State,
148*e038c9c4Sjoerg const Summary &Summary) const {
149*e038c9c4Sjoerg // There are some descendant classes that are not used as argument
150*e038c9c4Sjoerg // constraints, e.g. ComparisonConstraint. In that case we can safely
151*e038c9c4Sjoerg // ignore the implementation of this function.
152*e038c9c4Sjoerg llvm_unreachable("Not implemented");
153*e038c9c4Sjoerg }
154*e038c9c4Sjoerg
155*e038c9c4Sjoerg protected:
156*e038c9c4Sjoerg ArgNo ArgN; // Argument to which we apply the constraint.
157*e038c9c4Sjoerg
158*e038c9c4Sjoerg /// Do polymorphic sanity check on the constraint.
checkSpecificValidity(const FunctionDecl * FD) const159*e038c9c4Sjoerg virtual bool checkSpecificValidity(const FunctionDecl *FD) const {
160*e038c9c4Sjoerg return true;
161*e038c9c4Sjoerg }
162*e038c9c4Sjoerg };
163*e038c9c4Sjoerg
164*e038c9c4Sjoerg /// Given a range, should the argument stay inside or outside this range?
165*e038c9c4Sjoerg enum RangeKind { OutOfRange, WithinRange };
166*e038c9c4Sjoerg
167*e038c9c4Sjoerg /// Encapsulates a range on a single symbol.
168*e038c9c4Sjoerg class RangeConstraint : public ValueConstraint {
169*e038c9c4Sjoerg RangeKind Kind;
170*e038c9c4Sjoerg // A range is formed as a set of intervals (sub-ranges).
171*e038c9c4Sjoerg // E.g. {['A', 'Z'], ['a', 'z']}
172*e038c9c4Sjoerg //
173*e038c9c4Sjoerg // The default constructed RangeConstraint has an empty range set, applying
174*e038c9c4Sjoerg // such constraint does not involve any assumptions, thus the State remains
175*e038c9c4Sjoerg // unchanged. This is meaningful, if the range is dependent on a looked up
176*e038c9c4Sjoerg // type (e.g. [0, Socklen_tMax]). If the type is not found, then the range
177*e038c9c4Sjoerg // is default initialized to be empty.
178*e038c9c4Sjoerg IntRangeVector Ranges;
1797330f729Sjoerg
1807330f729Sjoerg public:
getName() const181*e038c9c4Sjoerg StringRef getName() const override { return "Range"; }
RangeConstraint(ArgNo ArgN,RangeKind Kind,const IntRangeVector & Ranges)182*e038c9c4Sjoerg RangeConstraint(ArgNo ArgN, RangeKind Kind, const IntRangeVector &Ranges)
183*e038c9c4Sjoerg : ValueConstraint(ArgN), Kind(Kind), Ranges(Ranges) {}
1847330f729Sjoerg
185*e038c9c4Sjoerg std::string describe(ProgramStateRef State,
186*e038c9c4Sjoerg const Summary &Summary) const override;
1877330f729Sjoerg
getRanges() const188*e038c9c4Sjoerg const IntRangeVector &getRanges() const { return Ranges; }
1897330f729Sjoerg
1907330f729Sjoerg private:
191*e038c9c4Sjoerg ProgramStateRef applyAsOutOfRange(ProgramStateRef State,
192*e038c9c4Sjoerg const CallEvent &Call,
193*e038c9c4Sjoerg const Summary &Summary) const;
194*e038c9c4Sjoerg ProgramStateRef applyAsWithinRange(ProgramStateRef State,
195*e038c9c4Sjoerg const CallEvent &Call,
196*e038c9c4Sjoerg const Summary &Summary) const;
1977330f729Sjoerg
1987330f729Sjoerg public:
apply(ProgramStateRef State,const CallEvent & Call,const Summary & Summary,CheckerContext & C) const1997330f729Sjoerg ProgramStateRef apply(ProgramStateRef State, const CallEvent &Call,
200*e038c9c4Sjoerg const Summary &Summary,
201*e038c9c4Sjoerg CheckerContext &C) const override {
2027330f729Sjoerg switch (Kind) {
2037330f729Sjoerg case OutOfRange:
2047330f729Sjoerg return applyAsOutOfRange(State, Call, Summary);
2057330f729Sjoerg case WithinRange:
2067330f729Sjoerg return applyAsWithinRange(State, Call, Summary);
2077330f729Sjoerg }
208*e038c9c4Sjoerg llvm_unreachable("Unknown range kind!");
209*e038c9c4Sjoerg }
210*e038c9c4Sjoerg
negate() const211*e038c9c4Sjoerg ValueConstraintPtr negate() const override {
212*e038c9c4Sjoerg RangeConstraint Tmp(*this);
213*e038c9c4Sjoerg switch (Kind) {
214*e038c9c4Sjoerg case OutOfRange:
215*e038c9c4Sjoerg Tmp.Kind = WithinRange;
216*e038c9c4Sjoerg break;
217*e038c9c4Sjoerg case WithinRange:
218*e038c9c4Sjoerg Tmp.Kind = OutOfRange;
219*e038c9c4Sjoerg break;
220*e038c9c4Sjoerg }
221*e038c9c4Sjoerg return std::make_shared<RangeConstraint>(Tmp);
222*e038c9c4Sjoerg }
223*e038c9c4Sjoerg
checkSpecificValidity(const FunctionDecl * FD) const224*e038c9c4Sjoerg bool checkSpecificValidity(const FunctionDecl *FD) const override {
225*e038c9c4Sjoerg const bool ValidArg =
226*e038c9c4Sjoerg getArgType(FD, ArgN)->isIntegralType(FD->getASTContext());
227*e038c9c4Sjoerg assert(ValidArg &&
228*e038c9c4Sjoerg "This constraint should be applied on an integral type");
229*e038c9c4Sjoerg return ValidArg;
2307330f729Sjoerg }
2317330f729Sjoerg };
2327330f729Sjoerg
233*e038c9c4Sjoerg class ComparisonConstraint : public ValueConstraint {
234*e038c9c4Sjoerg BinaryOperator::Opcode Opcode;
235*e038c9c4Sjoerg ArgNo OtherArgN;
2367330f729Sjoerg
2377330f729Sjoerg public:
getName() const238*e038c9c4Sjoerg virtual StringRef getName() const override { return "Comparison"; };
ComparisonConstraint(ArgNo ArgN,BinaryOperator::Opcode Opcode,ArgNo OtherArgN)239*e038c9c4Sjoerg ComparisonConstraint(ArgNo ArgN, BinaryOperator::Opcode Opcode,
240*e038c9c4Sjoerg ArgNo OtherArgN)
241*e038c9c4Sjoerg : ValueConstraint(ArgN), Opcode(Opcode), OtherArgN(OtherArgN) {}
getOtherArgNo() const242*e038c9c4Sjoerg ArgNo getOtherArgNo() const { return OtherArgN; }
getOpcode() const243*e038c9c4Sjoerg BinaryOperator::Opcode getOpcode() const { return Opcode; }
244*e038c9c4Sjoerg ProgramStateRef apply(ProgramStateRef State, const CallEvent &Call,
245*e038c9c4Sjoerg const Summary &Summary,
246*e038c9c4Sjoerg CheckerContext &C) const override;
247*e038c9c4Sjoerg };
248*e038c9c4Sjoerg
249*e038c9c4Sjoerg class NotNullConstraint : public ValueConstraint {
250*e038c9c4Sjoerg using ValueConstraint::ValueConstraint;
251*e038c9c4Sjoerg // This variable has a role when we negate the constraint.
252*e038c9c4Sjoerg bool CannotBeNull = true;
253*e038c9c4Sjoerg
254*e038c9c4Sjoerg public:
255*e038c9c4Sjoerg std::string describe(ProgramStateRef State,
256*e038c9c4Sjoerg const Summary &Summary) const override;
getName() const257*e038c9c4Sjoerg StringRef getName() const override { return "NonNull"; }
apply(ProgramStateRef State,const CallEvent & Call,const Summary & Summary,CheckerContext & C) const258*e038c9c4Sjoerg ProgramStateRef apply(ProgramStateRef State, const CallEvent &Call,
259*e038c9c4Sjoerg const Summary &Summary,
260*e038c9c4Sjoerg CheckerContext &C) const override {
261*e038c9c4Sjoerg SVal V = getArgSVal(Call, getArgNo());
262*e038c9c4Sjoerg if (V.isUndef())
263*e038c9c4Sjoerg return State;
264*e038c9c4Sjoerg
265*e038c9c4Sjoerg DefinedOrUnknownSVal L = V.castAs<DefinedOrUnknownSVal>();
266*e038c9c4Sjoerg if (!L.getAs<Loc>())
267*e038c9c4Sjoerg return State;
268*e038c9c4Sjoerg
269*e038c9c4Sjoerg return State->assume(L, CannotBeNull);
270*e038c9c4Sjoerg }
271*e038c9c4Sjoerg
negate() const272*e038c9c4Sjoerg ValueConstraintPtr negate() const override {
273*e038c9c4Sjoerg NotNullConstraint Tmp(*this);
274*e038c9c4Sjoerg Tmp.CannotBeNull = !this->CannotBeNull;
275*e038c9c4Sjoerg return std::make_shared<NotNullConstraint>(Tmp);
276*e038c9c4Sjoerg }
277*e038c9c4Sjoerg
checkSpecificValidity(const FunctionDecl * FD) const278*e038c9c4Sjoerg bool checkSpecificValidity(const FunctionDecl *FD) const override {
279*e038c9c4Sjoerg const bool ValidArg = getArgType(FD, ArgN)->isPointerType();
280*e038c9c4Sjoerg assert(ValidArg &&
281*e038c9c4Sjoerg "This constraint should be applied only on a pointer type");
282*e038c9c4Sjoerg return ValidArg;
283*e038c9c4Sjoerg }
284*e038c9c4Sjoerg };
285*e038c9c4Sjoerg
286*e038c9c4Sjoerg // Represents a buffer argument with an additional size constraint. The
287*e038c9c4Sjoerg // constraint may be a concrete value, or a symbolic value in an argument.
288*e038c9c4Sjoerg // Example 1. Concrete value as the minimum buffer size.
289*e038c9c4Sjoerg // char *asctime_r(const struct tm *restrict tm, char *restrict buf);
290*e038c9c4Sjoerg // // `buf` size must be at least 26 bytes according the POSIX standard.
291*e038c9c4Sjoerg // Example 2. Argument as a buffer size.
292*e038c9c4Sjoerg // ctime_s(char *buffer, rsize_t bufsz, const time_t *time);
293*e038c9c4Sjoerg // Example 3. The size is computed as a multiplication of other args.
294*e038c9c4Sjoerg // size_t fread(void *ptr, size_t size, size_t nmemb, FILE *stream);
295*e038c9c4Sjoerg // // Here, ptr is the buffer, and its minimum size is `size * nmemb`.
296*e038c9c4Sjoerg class BufferSizeConstraint : public ValueConstraint {
297*e038c9c4Sjoerg // The concrete value which is the minimum size for the buffer.
298*e038c9c4Sjoerg llvm::Optional<llvm::APSInt> ConcreteSize;
299*e038c9c4Sjoerg // The argument which holds the size of the buffer.
300*e038c9c4Sjoerg llvm::Optional<ArgNo> SizeArgN;
301*e038c9c4Sjoerg // The argument which is a multiplier to size. This is set in case of
302*e038c9c4Sjoerg // `fread` like functions where the size is computed as a multiplication of
303*e038c9c4Sjoerg // two arguments.
304*e038c9c4Sjoerg llvm::Optional<ArgNo> SizeMultiplierArgN;
305*e038c9c4Sjoerg // The operator we use in apply. This is negated in negate().
306*e038c9c4Sjoerg BinaryOperator::Opcode Op = BO_LE;
307*e038c9c4Sjoerg
308*e038c9c4Sjoerg public:
getName() const309*e038c9c4Sjoerg StringRef getName() const override { return "BufferSize"; }
BufferSizeConstraint(ArgNo Buffer,llvm::APSInt BufMinSize)310*e038c9c4Sjoerg BufferSizeConstraint(ArgNo Buffer, llvm::APSInt BufMinSize)
311*e038c9c4Sjoerg : ValueConstraint(Buffer), ConcreteSize(BufMinSize) {}
BufferSizeConstraint(ArgNo Buffer,ArgNo BufSize)312*e038c9c4Sjoerg BufferSizeConstraint(ArgNo Buffer, ArgNo BufSize)
313*e038c9c4Sjoerg : ValueConstraint(Buffer), SizeArgN(BufSize) {}
BufferSizeConstraint(ArgNo Buffer,ArgNo BufSize,ArgNo BufSizeMultiplier)314*e038c9c4Sjoerg BufferSizeConstraint(ArgNo Buffer, ArgNo BufSize, ArgNo BufSizeMultiplier)
315*e038c9c4Sjoerg : ValueConstraint(Buffer), SizeArgN(BufSize),
316*e038c9c4Sjoerg SizeMultiplierArgN(BufSizeMultiplier) {}
317*e038c9c4Sjoerg
getArgsToTrack() const318*e038c9c4Sjoerg std::vector<ArgNo> getArgsToTrack() const override {
319*e038c9c4Sjoerg std::vector<ArgNo> Result{ArgN};
320*e038c9c4Sjoerg if (SizeArgN)
321*e038c9c4Sjoerg Result.push_back(*SizeArgN);
322*e038c9c4Sjoerg if (SizeMultiplierArgN)
323*e038c9c4Sjoerg Result.push_back(*SizeMultiplierArgN);
324*e038c9c4Sjoerg return Result;
325*e038c9c4Sjoerg }
326*e038c9c4Sjoerg
327*e038c9c4Sjoerg std::string describe(ProgramStateRef State,
328*e038c9c4Sjoerg const Summary &Summary) const override;
329*e038c9c4Sjoerg
apply(ProgramStateRef State,const CallEvent & Call,const Summary & Summary,CheckerContext & C) const330*e038c9c4Sjoerg ProgramStateRef apply(ProgramStateRef State, const CallEvent &Call,
331*e038c9c4Sjoerg const Summary &Summary,
332*e038c9c4Sjoerg CheckerContext &C) const override {
333*e038c9c4Sjoerg SValBuilder &SvalBuilder = C.getSValBuilder();
334*e038c9c4Sjoerg // The buffer argument.
335*e038c9c4Sjoerg SVal BufV = getArgSVal(Call, getArgNo());
336*e038c9c4Sjoerg
337*e038c9c4Sjoerg // Get the size constraint.
338*e038c9c4Sjoerg const SVal SizeV = [this, &State, &Call, &Summary, &SvalBuilder]() {
339*e038c9c4Sjoerg if (ConcreteSize) {
340*e038c9c4Sjoerg return SVal(SvalBuilder.makeIntVal(*ConcreteSize));
341*e038c9c4Sjoerg }
342*e038c9c4Sjoerg assert(SizeArgN && "The constraint must be either a concrete value or "
343*e038c9c4Sjoerg "encoded in an argument.");
344*e038c9c4Sjoerg // The size argument.
345*e038c9c4Sjoerg SVal SizeV = getArgSVal(Call, *SizeArgN);
346*e038c9c4Sjoerg // Multiply with another argument if given.
347*e038c9c4Sjoerg if (SizeMultiplierArgN) {
348*e038c9c4Sjoerg SVal SizeMulV = getArgSVal(Call, *SizeMultiplierArgN);
349*e038c9c4Sjoerg SizeV = SvalBuilder.evalBinOp(State, BO_Mul, SizeV, SizeMulV,
350*e038c9c4Sjoerg Summary.getArgType(*SizeArgN));
351*e038c9c4Sjoerg }
352*e038c9c4Sjoerg return SizeV;
353*e038c9c4Sjoerg }();
354*e038c9c4Sjoerg
355*e038c9c4Sjoerg // The dynamic size of the buffer argument, got from the analyzer engine.
356*e038c9c4Sjoerg SVal BufDynSize = getDynamicExtentWithOffset(State, BufV);
357*e038c9c4Sjoerg
358*e038c9c4Sjoerg SVal Feasible = SvalBuilder.evalBinOp(State, Op, SizeV, BufDynSize,
359*e038c9c4Sjoerg SvalBuilder.getContext().BoolTy);
360*e038c9c4Sjoerg if (auto F = Feasible.getAs<DefinedOrUnknownSVal>())
361*e038c9c4Sjoerg return State->assume(*F, true);
362*e038c9c4Sjoerg
363*e038c9c4Sjoerg // We can get here only if the size argument or the dynamic size is
364*e038c9c4Sjoerg // undefined. But the dynamic size should never be undefined, only
365*e038c9c4Sjoerg // unknown. So, here, the size of the argument is undefined, i.e. we
366*e038c9c4Sjoerg // cannot apply the constraint. Actually, other checkers like
367*e038c9c4Sjoerg // CallAndMessage should catch this situation earlier, because we call a
368*e038c9c4Sjoerg // function with an uninitialized argument.
369*e038c9c4Sjoerg llvm_unreachable("Size argument or the dynamic size is Undefined");
370*e038c9c4Sjoerg }
371*e038c9c4Sjoerg
negate() const372*e038c9c4Sjoerg ValueConstraintPtr negate() const override {
373*e038c9c4Sjoerg BufferSizeConstraint Tmp(*this);
374*e038c9c4Sjoerg Tmp.Op = BinaryOperator::negateComparisonOp(Op);
375*e038c9c4Sjoerg return std::make_shared<BufferSizeConstraint>(Tmp);
376*e038c9c4Sjoerg }
377*e038c9c4Sjoerg
checkSpecificValidity(const FunctionDecl * FD) const378*e038c9c4Sjoerg bool checkSpecificValidity(const FunctionDecl *FD) const override {
379*e038c9c4Sjoerg const bool ValidArg = getArgType(FD, ArgN)->isPointerType();
380*e038c9c4Sjoerg assert(ValidArg &&
381*e038c9c4Sjoerg "This constraint should be applied only on a pointer type");
382*e038c9c4Sjoerg return ValidArg;
383*e038c9c4Sjoerg }
384*e038c9c4Sjoerg };
385*e038c9c4Sjoerg
386*e038c9c4Sjoerg /// The complete list of constraints that defines a single branch.
387*e038c9c4Sjoerg typedef std::vector<ValueConstraintPtr> ConstraintSet;
388*e038c9c4Sjoerg
389*e038c9c4Sjoerg using ArgTypes = std::vector<Optional<QualType>>;
390*e038c9c4Sjoerg using RetType = Optional<QualType>;
391*e038c9c4Sjoerg
392*e038c9c4Sjoerg // A placeholder type, we use it whenever we do not care about the concrete
393*e038c9c4Sjoerg // type in a Signature.
394*e038c9c4Sjoerg const QualType Irrelevant{};
isIrrelevant(QualType T)395*e038c9c4Sjoerg bool static isIrrelevant(QualType T) { return T.isNull(); }
396*e038c9c4Sjoerg
397*e038c9c4Sjoerg // The signature of a function we want to describe with a summary. This is a
398*e038c9c4Sjoerg // concessive signature, meaning there may be irrelevant types in the
399*e038c9c4Sjoerg // signature which we do not check against a function with concrete types.
400*e038c9c4Sjoerg // All types in the spec need to be canonical.
401*e038c9c4Sjoerg class Signature {
402*e038c9c4Sjoerg using ArgQualTypes = std::vector<QualType>;
403*e038c9c4Sjoerg ArgQualTypes ArgTys;
404*e038c9c4Sjoerg QualType RetTy;
405*e038c9c4Sjoerg // True if any component type is not found by lookup.
406*e038c9c4Sjoerg bool Invalid = false;
407*e038c9c4Sjoerg
408*e038c9c4Sjoerg public:
409*e038c9c4Sjoerg // Construct a signature from optional types. If any of the optional types
410*e038c9c4Sjoerg // are not set then the signature will be invalid.
Signature(ArgTypes ArgTys,RetType RetTy)411*e038c9c4Sjoerg Signature(ArgTypes ArgTys, RetType RetTy) {
412*e038c9c4Sjoerg for (Optional<QualType> Arg : ArgTys) {
413*e038c9c4Sjoerg if (!Arg) {
414*e038c9c4Sjoerg Invalid = true;
415*e038c9c4Sjoerg return;
416*e038c9c4Sjoerg } else {
417*e038c9c4Sjoerg assertArgTypeSuitableForSignature(*Arg);
418*e038c9c4Sjoerg this->ArgTys.push_back(*Arg);
419*e038c9c4Sjoerg }
420*e038c9c4Sjoerg }
421*e038c9c4Sjoerg if (!RetTy) {
422*e038c9c4Sjoerg Invalid = true;
423*e038c9c4Sjoerg return;
424*e038c9c4Sjoerg } else {
425*e038c9c4Sjoerg assertRetTypeSuitableForSignature(*RetTy);
426*e038c9c4Sjoerg this->RetTy = *RetTy;
427*e038c9c4Sjoerg }
428*e038c9c4Sjoerg }
429*e038c9c4Sjoerg
isInvalid() const430*e038c9c4Sjoerg bool isInvalid() const { return Invalid; }
431*e038c9c4Sjoerg bool matches(const FunctionDecl *FD) const;
432*e038c9c4Sjoerg
433*e038c9c4Sjoerg private:
assertArgTypeSuitableForSignature(QualType T)434*e038c9c4Sjoerg static void assertArgTypeSuitableForSignature(QualType T) {
435*e038c9c4Sjoerg assert((T.isNull() || !T->isVoidType()) &&
436*e038c9c4Sjoerg "We should have no void types in the spec");
437*e038c9c4Sjoerg assert((T.isNull() || T.isCanonical()) &&
438*e038c9c4Sjoerg "We should only have canonical types in the spec");
439*e038c9c4Sjoerg }
assertRetTypeSuitableForSignature(QualType T)440*e038c9c4Sjoerg static void assertRetTypeSuitableForSignature(QualType T) {
441*e038c9c4Sjoerg assert((T.isNull() || T.isCanonical()) &&
442*e038c9c4Sjoerg "We should only have canonical types in the spec");
443*e038c9c4Sjoerg }
444*e038c9c4Sjoerg };
445*e038c9c4Sjoerg
getArgType(const FunctionDecl * FD,ArgNo ArgN)446*e038c9c4Sjoerg static QualType getArgType(const FunctionDecl *FD, ArgNo ArgN) {
447*e038c9c4Sjoerg assert(FD && "Function must be set");
448*e038c9c4Sjoerg QualType T = (ArgN == Ret)
449*e038c9c4Sjoerg ? FD->getReturnType().getCanonicalType()
450*e038c9c4Sjoerg : FD->getParamDecl(ArgN)->getType().getCanonicalType();
4517330f729Sjoerg return T;
4527330f729Sjoerg }
4537330f729Sjoerg
454*e038c9c4Sjoerg using Cases = std::vector<ConstraintSet>;
4557330f729Sjoerg
456*e038c9c4Sjoerg /// A summary includes information about
457*e038c9c4Sjoerg /// * function prototype (signature)
458*e038c9c4Sjoerg /// * approach to invalidation,
459*e038c9c4Sjoerg /// * a list of branches - a list of list of ranges -
460*e038c9c4Sjoerg /// A branch represents a path in the exploded graph of a function (which
461*e038c9c4Sjoerg /// is a tree). So, a branch is a series of assumptions. In other words,
462*e038c9c4Sjoerg /// branches represent split states and additional assumptions on top of
463*e038c9c4Sjoerg /// the splitting assumption.
464*e038c9c4Sjoerg /// For example, consider the branches in `isalpha(x)`
465*e038c9c4Sjoerg /// Branch 1)
466*e038c9c4Sjoerg /// x is in range ['A', 'Z'] or in ['a', 'z']
467*e038c9c4Sjoerg /// then the return value is not 0. (I.e. out-of-range [0, 0])
468*e038c9c4Sjoerg /// Branch 2)
469*e038c9c4Sjoerg /// x is out-of-range ['A', 'Z'] and out-of-range ['a', 'z']
470*e038c9c4Sjoerg /// then the return value is 0.
471*e038c9c4Sjoerg /// * a list of argument constraints, that must be true on every branch.
472*e038c9c4Sjoerg /// If these constraints are not satisfied that means a fatal error
473*e038c9c4Sjoerg /// usually resulting in undefined behaviour.
474*e038c9c4Sjoerg ///
475*e038c9c4Sjoerg /// Application of a summary:
476*e038c9c4Sjoerg /// The signature and argument constraints together contain information
477*e038c9c4Sjoerg /// about which functions are handled by the summary. The signature can use
478*e038c9c4Sjoerg /// "wildcards", i.e. Irrelevant types. Irrelevant type of a parameter in
479*e038c9c4Sjoerg /// a signature means that type is not compared to the type of the parameter
480*e038c9c4Sjoerg /// in the found FunctionDecl. Argument constraints may specify additional
481*e038c9c4Sjoerg /// rules for the given parameter's type, those rules are checked once the
482*e038c9c4Sjoerg /// signature is matched.
483*e038c9c4Sjoerg class Summary {
484*e038c9c4Sjoerg const InvalidationKind InvalidationKd;
485*e038c9c4Sjoerg Cases CaseConstraints;
486*e038c9c4Sjoerg ConstraintSet ArgConstraints;
487*e038c9c4Sjoerg
488*e038c9c4Sjoerg // The function to which the summary applies. This is set after lookup and
489*e038c9c4Sjoerg // match to the signature.
490*e038c9c4Sjoerg const FunctionDecl *FD = nullptr;
491*e038c9c4Sjoerg
492*e038c9c4Sjoerg public:
Summary(InvalidationKind InvalidationKd)493*e038c9c4Sjoerg Summary(InvalidationKind InvalidationKd) : InvalidationKd(InvalidationKd) {}
494*e038c9c4Sjoerg
Case(ConstraintSet && CS)495*e038c9c4Sjoerg Summary &Case(ConstraintSet &&CS) {
496*e038c9c4Sjoerg CaseConstraints.push_back(std::move(CS));
497*e038c9c4Sjoerg return *this;
498*e038c9c4Sjoerg }
Case(const ConstraintSet & CS)499*e038c9c4Sjoerg Summary &Case(const ConstraintSet &CS) {
500*e038c9c4Sjoerg CaseConstraints.push_back(CS);
501*e038c9c4Sjoerg return *this;
502*e038c9c4Sjoerg }
ArgConstraint(ValueConstraintPtr VC)503*e038c9c4Sjoerg Summary &ArgConstraint(ValueConstraintPtr VC) {
504*e038c9c4Sjoerg assert(VC->getArgNo() != Ret &&
505*e038c9c4Sjoerg "Arg constraint should not refer to the return value");
506*e038c9c4Sjoerg ArgConstraints.push_back(VC);
507*e038c9c4Sjoerg return *this;
508*e038c9c4Sjoerg }
509*e038c9c4Sjoerg
getInvalidationKd() const510*e038c9c4Sjoerg InvalidationKind getInvalidationKd() const { return InvalidationKd; }
getCaseConstraints() const511*e038c9c4Sjoerg const Cases &getCaseConstraints() const { return CaseConstraints; }
getArgConstraints() const512*e038c9c4Sjoerg const ConstraintSet &getArgConstraints() const { return ArgConstraints; }
513*e038c9c4Sjoerg
getArgType(ArgNo ArgN) const514*e038c9c4Sjoerg QualType getArgType(ArgNo ArgN) const {
515*e038c9c4Sjoerg return StdLibraryFunctionsChecker::getArgType(FD, ArgN);
516*e038c9c4Sjoerg }
517*e038c9c4Sjoerg
518*e038c9c4Sjoerg // Returns true if the summary should be applied to the given function.
519*e038c9c4Sjoerg // And if yes then store the function declaration.
matchesAndSet(const Signature & Sign,const FunctionDecl * FD)520*e038c9c4Sjoerg bool matchesAndSet(const Signature &Sign, const FunctionDecl *FD) {
521*e038c9c4Sjoerg bool Result = Sign.matches(FD) && validateByConstraints(FD);
522*e038c9c4Sjoerg if (Result) {
523*e038c9c4Sjoerg assert(!this->FD && "FD must not be set more than once");
524*e038c9c4Sjoerg this->FD = FD;
525*e038c9c4Sjoerg }
526*e038c9c4Sjoerg return Result;
527*e038c9c4Sjoerg }
528*e038c9c4Sjoerg
529*e038c9c4Sjoerg private:
530*e038c9c4Sjoerg // Once we know the exact type of the function then do sanity check on all
531*e038c9c4Sjoerg // the given constraints.
validateByConstraints(const FunctionDecl * FD) const532*e038c9c4Sjoerg bool validateByConstraints(const FunctionDecl *FD) const {
533*e038c9c4Sjoerg for (const ConstraintSet &Case : CaseConstraints)
534*e038c9c4Sjoerg for (const ValueConstraintPtr &Constraint : Case)
535*e038c9c4Sjoerg if (!Constraint->checkValidity(FD))
536*e038c9c4Sjoerg return false;
537*e038c9c4Sjoerg for (const ValueConstraintPtr &Constraint : ArgConstraints)
538*e038c9c4Sjoerg if (!Constraint->checkValidity(FD))
539*e038c9c4Sjoerg return false;
540*e038c9c4Sjoerg return true;
541*e038c9c4Sjoerg }
542*e038c9c4Sjoerg };
5437330f729Sjoerg
5447330f729Sjoerg // The map of all functions supported by the checker. It is initialized
5457330f729Sjoerg // lazily, and it doesn't change after initialization.
546*e038c9c4Sjoerg using FunctionSummaryMapType = llvm::DenseMap<const FunctionDecl *, Summary>;
547*e038c9c4Sjoerg mutable FunctionSummaryMapType FunctionSummaryMap;
5487330f729Sjoerg
549*e038c9c4Sjoerg mutable std::unique_ptr<BugType> BT_InvalidArg;
550*e038c9c4Sjoerg mutable bool SummariesInitialized = false;
551*e038c9c4Sjoerg
getArgSVal(const CallEvent & Call,ArgNo ArgN)552*e038c9c4Sjoerg static SVal getArgSVal(const CallEvent &Call, ArgNo ArgN) {
553*e038c9c4Sjoerg return ArgN == Ret ? Call.getReturnValue() : Call.getArgSVal(ArgN);
5547330f729Sjoerg }
5557330f729Sjoerg
5567330f729Sjoerg public:
557*e038c9c4Sjoerg void checkPreCall(const CallEvent &Call, CheckerContext &C) const;
5587330f729Sjoerg void checkPostCall(const CallEvent &Call, CheckerContext &C) const;
5597330f729Sjoerg bool evalCall(const CallEvent &Call, CheckerContext &C) const;
5607330f729Sjoerg
561*e038c9c4Sjoerg enum CheckKind {
562*e038c9c4Sjoerg CK_StdCLibraryFunctionArgsChecker,
563*e038c9c4Sjoerg CK_StdCLibraryFunctionsTesterChecker,
564*e038c9c4Sjoerg CK_NumCheckKinds
565*e038c9c4Sjoerg };
566*e038c9c4Sjoerg DefaultBool ChecksEnabled[CK_NumCheckKinds];
567*e038c9c4Sjoerg CheckerNameRef CheckNames[CK_NumCheckKinds];
568*e038c9c4Sjoerg
569*e038c9c4Sjoerg bool DisplayLoadedSummaries = false;
570*e038c9c4Sjoerg bool ModelPOSIX = false;
571*e038c9c4Sjoerg
5727330f729Sjoerg private:
573*e038c9c4Sjoerg Optional<Summary> findFunctionSummary(const FunctionDecl *FD,
574*e038c9c4Sjoerg CheckerContext &C) const;
575*e038c9c4Sjoerg Optional<Summary> findFunctionSummary(const CallEvent &Call,
5767330f729Sjoerg CheckerContext &C) const;
5777330f729Sjoerg
578*e038c9c4Sjoerg void initFunctionSummaries(CheckerContext &C) const;
579*e038c9c4Sjoerg
reportBug(const CallEvent & Call,ExplodedNode * N,const ValueConstraint * VC,const Summary & Summary,CheckerContext & C) const580*e038c9c4Sjoerg void reportBug(const CallEvent &Call, ExplodedNode *N,
581*e038c9c4Sjoerg const ValueConstraint *VC, const Summary &Summary,
582*e038c9c4Sjoerg CheckerContext &C) const {
583*e038c9c4Sjoerg if (!ChecksEnabled[CK_StdCLibraryFunctionArgsChecker])
584*e038c9c4Sjoerg return;
585*e038c9c4Sjoerg std::string Msg =
586*e038c9c4Sjoerg (Twine("Function argument constraint is not satisfied, constraint: ") +
587*e038c9c4Sjoerg VC->getName().data())
588*e038c9c4Sjoerg .str();
589*e038c9c4Sjoerg if (!BT_InvalidArg)
590*e038c9c4Sjoerg BT_InvalidArg = std::make_unique<BugType>(
591*e038c9c4Sjoerg CheckNames[CK_StdCLibraryFunctionArgsChecker],
592*e038c9c4Sjoerg "Unsatisfied argument constraints", categories::LogicError);
593*e038c9c4Sjoerg auto R = std::make_unique<PathSensitiveBugReport>(*BT_InvalidArg, Msg, N);
594*e038c9c4Sjoerg
595*e038c9c4Sjoerg for (ArgNo ArgN : VC->getArgsToTrack())
596*e038c9c4Sjoerg bugreporter::trackExpressionValue(N, Call.getArgExpr(ArgN), *R);
597*e038c9c4Sjoerg
598*e038c9c4Sjoerg // Highlight the range of the argument that was violated.
599*e038c9c4Sjoerg R->addRange(Call.getArgSourceRange(VC->getArgNo()));
600*e038c9c4Sjoerg
601*e038c9c4Sjoerg // Describe the argument constraint in a note.
602*e038c9c4Sjoerg R->addNote(VC->describe(C.getState(), Summary), R->getLocation(),
603*e038c9c4Sjoerg Call.getArgSourceRange(VC->getArgNo()));
604*e038c9c4Sjoerg
605*e038c9c4Sjoerg C.emitReport(std::move(R));
606*e038c9c4Sjoerg }
6077330f729Sjoerg };
608*e038c9c4Sjoerg
609*e038c9c4Sjoerg const StdLibraryFunctionsChecker::ArgNo StdLibraryFunctionsChecker::Ret =
610*e038c9c4Sjoerg std::numeric_limits<ArgNo>::max();
611*e038c9c4Sjoerg
6127330f729Sjoerg } // end of anonymous namespace
6137330f729Sjoerg
getBVF(ProgramStateRef State)614*e038c9c4Sjoerg static BasicValueFactory &getBVF(ProgramStateRef State) {
615*e038c9c4Sjoerg ProgramStateManager &Mgr = State->getStateManager();
616*e038c9c4Sjoerg SValBuilder &SVB = Mgr.getSValBuilder();
617*e038c9c4Sjoerg return SVB.getBasicValueFactory();
618*e038c9c4Sjoerg }
619*e038c9c4Sjoerg
describe(ProgramStateRef State,const Summary & Summary) const620*e038c9c4Sjoerg std::string StdLibraryFunctionsChecker::NotNullConstraint::describe(
621*e038c9c4Sjoerg ProgramStateRef State, const Summary &Summary) const {
622*e038c9c4Sjoerg SmallString<48> Result;
623*e038c9c4Sjoerg Result += "The ";
624*e038c9c4Sjoerg Result += getArgDesc(ArgN);
625*e038c9c4Sjoerg Result += " should not be NULL";
626*e038c9c4Sjoerg return Result.c_str();
627*e038c9c4Sjoerg }
628*e038c9c4Sjoerg
describe(ProgramStateRef State,const Summary & Summary) const629*e038c9c4Sjoerg std::string StdLibraryFunctionsChecker::RangeConstraint::describe(
630*e038c9c4Sjoerg ProgramStateRef State, const Summary &Summary) const {
631*e038c9c4Sjoerg
632*e038c9c4Sjoerg BasicValueFactory &BVF = getBVF(State);
633*e038c9c4Sjoerg
634*e038c9c4Sjoerg QualType T = Summary.getArgType(getArgNo());
635*e038c9c4Sjoerg SmallString<48> Result;
636*e038c9c4Sjoerg Result += "The ";
637*e038c9c4Sjoerg Result += getArgDesc(ArgN);
638*e038c9c4Sjoerg Result += " should be ";
639*e038c9c4Sjoerg
640*e038c9c4Sjoerg // Range kind as a string.
641*e038c9c4Sjoerg Kind == OutOfRange ? Result += "out of" : Result += "within";
642*e038c9c4Sjoerg
643*e038c9c4Sjoerg // Get the range values as a string.
644*e038c9c4Sjoerg Result += " the range ";
645*e038c9c4Sjoerg if (Ranges.size() > 1)
646*e038c9c4Sjoerg Result += "[";
647*e038c9c4Sjoerg unsigned I = Ranges.size();
648*e038c9c4Sjoerg for (const std::pair<RangeInt, RangeInt> &R : Ranges) {
649*e038c9c4Sjoerg Result += "[";
650*e038c9c4Sjoerg const llvm::APSInt &Min = BVF.getValue(R.first, T);
651*e038c9c4Sjoerg const llvm::APSInt &Max = BVF.getValue(R.second, T);
652*e038c9c4Sjoerg Min.toString(Result);
653*e038c9c4Sjoerg Result += ", ";
654*e038c9c4Sjoerg Max.toString(Result);
655*e038c9c4Sjoerg Result += "]";
656*e038c9c4Sjoerg if (--I > 0)
657*e038c9c4Sjoerg Result += ", ";
658*e038c9c4Sjoerg }
659*e038c9c4Sjoerg if (Ranges.size() > 1)
660*e038c9c4Sjoerg Result += "]";
661*e038c9c4Sjoerg
662*e038c9c4Sjoerg return Result.c_str();
663*e038c9c4Sjoerg }
664*e038c9c4Sjoerg
665*e038c9c4Sjoerg SmallString<8>
getArgDesc(StdLibraryFunctionsChecker::ArgNo ArgN)666*e038c9c4Sjoerg StdLibraryFunctionsChecker::getArgDesc(StdLibraryFunctionsChecker::ArgNo ArgN) {
667*e038c9c4Sjoerg SmallString<8> Result;
668*e038c9c4Sjoerg Result += std::to_string(ArgN + 1);
669*e038c9c4Sjoerg Result += llvm::getOrdinalSuffix(ArgN + 1);
670*e038c9c4Sjoerg Result += " arg";
671*e038c9c4Sjoerg return Result;
672*e038c9c4Sjoerg }
673*e038c9c4Sjoerg
describe(ProgramStateRef State,const Summary & Summary) const674*e038c9c4Sjoerg std::string StdLibraryFunctionsChecker::BufferSizeConstraint::describe(
675*e038c9c4Sjoerg ProgramStateRef State, const Summary &Summary) const {
676*e038c9c4Sjoerg SmallString<96> Result;
677*e038c9c4Sjoerg Result += "The size of the ";
678*e038c9c4Sjoerg Result += getArgDesc(ArgN);
679*e038c9c4Sjoerg Result += " should be equal to or less than the value of ";
680*e038c9c4Sjoerg if (ConcreteSize) {
681*e038c9c4Sjoerg ConcreteSize->toString(Result);
682*e038c9c4Sjoerg } else if (SizeArgN) {
683*e038c9c4Sjoerg Result += "the ";
684*e038c9c4Sjoerg Result += getArgDesc(*SizeArgN);
685*e038c9c4Sjoerg if (SizeMultiplierArgN) {
686*e038c9c4Sjoerg Result += " times the ";
687*e038c9c4Sjoerg Result += getArgDesc(*SizeMultiplierArgN);
688*e038c9c4Sjoerg }
689*e038c9c4Sjoerg }
690*e038c9c4Sjoerg return Result.c_str();
691*e038c9c4Sjoerg }
692*e038c9c4Sjoerg
applyAsOutOfRange(ProgramStateRef State,const CallEvent & Call,const Summary & Summary) const693*e038c9c4Sjoerg ProgramStateRef StdLibraryFunctionsChecker::RangeConstraint::applyAsOutOfRange(
6947330f729Sjoerg ProgramStateRef State, const CallEvent &Call,
695*e038c9c4Sjoerg const Summary &Summary) const {
696*e038c9c4Sjoerg if (Ranges.empty())
697*e038c9c4Sjoerg return State;
6987330f729Sjoerg
6997330f729Sjoerg ProgramStateManager &Mgr = State->getStateManager();
7007330f729Sjoerg SValBuilder &SVB = Mgr.getSValBuilder();
7017330f729Sjoerg BasicValueFactory &BVF = SVB.getBasicValueFactory();
7027330f729Sjoerg ConstraintManager &CM = Mgr.getConstraintManager();
703*e038c9c4Sjoerg QualType T = Summary.getArgType(getArgNo());
7047330f729Sjoerg SVal V = getArgSVal(Call, getArgNo());
7057330f729Sjoerg
7067330f729Sjoerg if (auto N = V.getAs<NonLoc>()) {
707*e038c9c4Sjoerg const IntRangeVector &R = getRanges();
7087330f729Sjoerg size_t E = R.size();
7097330f729Sjoerg for (size_t I = 0; I != E; ++I) {
7107330f729Sjoerg const llvm::APSInt &Min = BVF.getValue(R[I].first, T);
7117330f729Sjoerg const llvm::APSInt &Max = BVF.getValue(R[I].second, T);
7127330f729Sjoerg assert(Min <= Max);
7137330f729Sjoerg State = CM.assumeInclusiveRange(State, *N, Min, Max, false);
7147330f729Sjoerg if (!State)
7157330f729Sjoerg break;
7167330f729Sjoerg }
7177330f729Sjoerg }
7187330f729Sjoerg
7197330f729Sjoerg return State;
7207330f729Sjoerg }
7217330f729Sjoerg
applyAsWithinRange(ProgramStateRef State,const CallEvent & Call,const Summary & Summary) const722*e038c9c4Sjoerg ProgramStateRef StdLibraryFunctionsChecker::RangeConstraint::applyAsWithinRange(
7237330f729Sjoerg ProgramStateRef State, const CallEvent &Call,
724*e038c9c4Sjoerg const Summary &Summary) const {
725*e038c9c4Sjoerg if (Ranges.empty())
726*e038c9c4Sjoerg return State;
7277330f729Sjoerg
7287330f729Sjoerg ProgramStateManager &Mgr = State->getStateManager();
7297330f729Sjoerg SValBuilder &SVB = Mgr.getSValBuilder();
7307330f729Sjoerg BasicValueFactory &BVF = SVB.getBasicValueFactory();
7317330f729Sjoerg ConstraintManager &CM = Mgr.getConstraintManager();
732*e038c9c4Sjoerg QualType T = Summary.getArgType(getArgNo());
7337330f729Sjoerg SVal V = getArgSVal(Call, getArgNo());
7347330f729Sjoerg
7357330f729Sjoerg // "WithinRange R" is treated as "outside [T_MIN, T_MAX] \ R".
7367330f729Sjoerg // We cut off [T_MIN, min(R) - 1] and [max(R) + 1, T_MAX] if necessary,
7377330f729Sjoerg // and then cut away all holes in R one by one.
738*e038c9c4Sjoerg //
739*e038c9c4Sjoerg // E.g. consider a range list R as [A, B] and [C, D]
740*e038c9c4Sjoerg // -------+--------+------------------+------------+----------->
741*e038c9c4Sjoerg // A B C D
742*e038c9c4Sjoerg // Then we assume that the value is not in [-inf, A - 1],
743*e038c9c4Sjoerg // then not in [D + 1, +inf], then not in [B + 1, C - 1]
7447330f729Sjoerg if (auto N = V.getAs<NonLoc>()) {
745*e038c9c4Sjoerg const IntRangeVector &R = getRanges();
7467330f729Sjoerg size_t E = R.size();
7477330f729Sjoerg
7487330f729Sjoerg const llvm::APSInt &MinusInf = BVF.getMinValue(T);
7497330f729Sjoerg const llvm::APSInt &PlusInf = BVF.getMaxValue(T);
7507330f729Sjoerg
7517330f729Sjoerg const llvm::APSInt &Left = BVF.getValue(R[0].first - 1ULL, T);
7527330f729Sjoerg if (Left != PlusInf) {
7537330f729Sjoerg assert(MinusInf <= Left);
7547330f729Sjoerg State = CM.assumeInclusiveRange(State, *N, MinusInf, Left, false);
7557330f729Sjoerg if (!State)
7567330f729Sjoerg return nullptr;
7577330f729Sjoerg }
7587330f729Sjoerg
7597330f729Sjoerg const llvm::APSInt &Right = BVF.getValue(R[E - 1].second + 1ULL, T);
7607330f729Sjoerg if (Right != MinusInf) {
7617330f729Sjoerg assert(Right <= PlusInf);
7627330f729Sjoerg State = CM.assumeInclusiveRange(State, *N, Right, PlusInf, false);
7637330f729Sjoerg if (!State)
7647330f729Sjoerg return nullptr;
7657330f729Sjoerg }
7667330f729Sjoerg
7677330f729Sjoerg for (size_t I = 1; I != E; ++I) {
7687330f729Sjoerg const llvm::APSInt &Min = BVF.getValue(R[I - 1].second + 1ULL, T);
7697330f729Sjoerg const llvm::APSInt &Max = BVF.getValue(R[I].first - 1ULL, T);
770*e038c9c4Sjoerg if (Min <= Max) {
7717330f729Sjoerg State = CM.assumeInclusiveRange(State, *N, Min, Max, false);
7727330f729Sjoerg if (!State)
7737330f729Sjoerg return nullptr;
7747330f729Sjoerg }
7757330f729Sjoerg }
776*e038c9c4Sjoerg }
7777330f729Sjoerg
7787330f729Sjoerg return State;
7797330f729Sjoerg }
7807330f729Sjoerg
apply(ProgramStateRef State,const CallEvent & Call,const Summary & Summary,CheckerContext & C) const781*e038c9c4Sjoerg ProgramStateRef StdLibraryFunctionsChecker::ComparisonConstraint::apply(
782*e038c9c4Sjoerg ProgramStateRef State, const CallEvent &Call, const Summary &Summary,
783*e038c9c4Sjoerg CheckerContext &C) const {
7847330f729Sjoerg
7857330f729Sjoerg ProgramStateManager &Mgr = State->getStateManager();
7867330f729Sjoerg SValBuilder &SVB = Mgr.getSValBuilder();
7877330f729Sjoerg QualType CondT = SVB.getConditionType();
788*e038c9c4Sjoerg QualType T = Summary.getArgType(getArgNo());
7897330f729Sjoerg SVal V = getArgSVal(Call, getArgNo());
7907330f729Sjoerg
7917330f729Sjoerg BinaryOperator::Opcode Op = getOpcode();
792*e038c9c4Sjoerg ArgNo OtherArg = getOtherArgNo();
7937330f729Sjoerg SVal OtherV = getArgSVal(Call, OtherArg);
794*e038c9c4Sjoerg QualType OtherT = Summary.getArgType(OtherArg);
7957330f729Sjoerg // Note: we avoid integral promotion for comparison.
7967330f729Sjoerg OtherV = SVB.evalCast(OtherV, T, OtherT);
7977330f729Sjoerg if (auto CompV = SVB.evalBinOp(State, Op, V, OtherV, CondT)
7987330f729Sjoerg .getAs<DefinedOrUnknownSVal>())
7997330f729Sjoerg State = State->assume(*CompV, true);
8007330f729Sjoerg return State;
8017330f729Sjoerg }
8027330f729Sjoerg
checkPreCall(const CallEvent & Call,CheckerContext & C) const803*e038c9c4Sjoerg void StdLibraryFunctionsChecker::checkPreCall(const CallEvent &Call,
8047330f729Sjoerg CheckerContext &C) const {
805*e038c9c4Sjoerg Optional<Summary> FoundSummary = findFunctionSummary(Call, C);
8067330f729Sjoerg if (!FoundSummary)
8077330f729Sjoerg return;
8087330f729Sjoerg
809*e038c9c4Sjoerg const Summary &Summary = *FoundSummary;
8107330f729Sjoerg ProgramStateRef State = C.getState();
8117330f729Sjoerg
8127330f729Sjoerg ProgramStateRef NewState = State;
813*e038c9c4Sjoerg for (const ValueConstraintPtr &Constraint : Summary.getArgConstraints()) {
814*e038c9c4Sjoerg ProgramStateRef SuccessSt = Constraint->apply(NewState, Call, Summary, C);
815*e038c9c4Sjoerg ProgramStateRef FailureSt =
816*e038c9c4Sjoerg Constraint->negate()->apply(NewState, Call, Summary, C);
817*e038c9c4Sjoerg // The argument constraint is not satisfied.
818*e038c9c4Sjoerg if (FailureSt && !SuccessSt) {
819*e038c9c4Sjoerg if (ExplodedNode *N = C.generateErrorNode(NewState))
820*e038c9c4Sjoerg reportBug(Call, N, Constraint.get(), Summary, C);
821*e038c9c4Sjoerg break;
822*e038c9c4Sjoerg } else {
823*e038c9c4Sjoerg // We will apply the constraint even if we cannot reason about the
824*e038c9c4Sjoerg // argument. This means both SuccessSt and FailureSt can be true. If we
825*e038c9c4Sjoerg // weren't applying the constraint that would mean that symbolic
826*e038c9c4Sjoerg // execution continues on a code whose behaviour is undefined.
827*e038c9c4Sjoerg assert(SuccessSt);
828*e038c9c4Sjoerg NewState = SuccessSt;
829*e038c9c4Sjoerg }
830*e038c9c4Sjoerg }
831*e038c9c4Sjoerg if (NewState && NewState != State)
832*e038c9c4Sjoerg C.addTransition(NewState);
833*e038c9c4Sjoerg }
834*e038c9c4Sjoerg
checkPostCall(const CallEvent & Call,CheckerContext & C) const835*e038c9c4Sjoerg void StdLibraryFunctionsChecker::checkPostCall(const CallEvent &Call,
836*e038c9c4Sjoerg CheckerContext &C) const {
837*e038c9c4Sjoerg Optional<Summary> FoundSummary = findFunctionSummary(Call, C);
838*e038c9c4Sjoerg if (!FoundSummary)
839*e038c9c4Sjoerg return;
840*e038c9c4Sjoerg
841*e038c9c4Sjoerg // Now apply the constraints.
842*e038c9c4Sjoerg const Summary &Summary = *FoundSummary;
843*e038c9c4Sjoerg ProgramStateRef State = C.getState();
844*e038c9c4Sjoerg
845*e038c9c4Sjoerg // Apply case/branch specifications.
846*e038c9c4Sjoerg for (const ConstraintSet &Case : Summary.getCaseConstraints()) {
847*e038c9c4Sjoerg ProgramStateRef NewState = State;
848*e038c9c4Sjoerg for (const ValueConstraintPtr &Constraint : Case) {
849*e038c9c4Sjoerg NewState = Constraint->apply(NewState, Call, Summary, C);
8507330f729Sjoerg if (!NewState)
8517330f729Sjoerg break;
8527330f729Sjoerg }
8537330f729Sjoerg
8547330f729Sjoerg if (NewState && NewState != State)
8557330f729Sjoerg C.addTransition(NewState);
8567330f729Sjoerg }
8577330f729Sjoerg }
8587330f729Sjoerg
evalCall(const CallEvent & Call,CheckerContext & C) const8597330f729Sjoerg bool StdLibraryFunctionsChecker::evalCall(const CallEvent &Call,
8607330f729Sjoerg CheckerContext &C) const {
861*e038c9c4Sjoerg Optional<Summary> FoundSummary = findFunctionSummary(Call, C);
8627330f729Sjoerg if (!FoundSummary)
8637330f729Sjoerg return false;
8647330f729Sjoerg
865*e038c9c4Sjoerg const Summary &Summary = *FoundSummary;
866*e038c9c4Sjoerg switch (Summary.getInvalidationKd()) {
8677330f729Sjoerg case EvalCallAsPure: {
8687330f729Sjoerg ProgramStateRef State = C.getState();
8697330f729Sjoerg const LocationContext *LC = C.getLocationContext();
870*e038c9c4Sjoerg const auto *CE = cast<CallExpr>(Call.getOriginExpr());
8717330f729Sjoerg SVal V = C.getSValBuilder().conjureSymbolVal(
8727330f729Sjoerg CE, LC, CE->getType().getCanonicalType(), C.blockCount());
8737330f729Sjoerg State = State->BindExpr(CE, LC, V);
8747330f729Sjoerg C.addTransition(State);
8757330f729Sjoerg return true;
8767330f729Sjoerg }
8777330f729Sjoerg case NoEvalCall:
8787330f729Sjoerg // Summary tells us to avoid performing eval::Call. The function is possibly
8797330f729Sjoerg // evaluated by another checker, or evaluated conservatively.
8807330f729Sjoerg return false;
8817330f729Sjoerg }
8827330f729Sjoerg llvm_unreachable("Unknown invalidation kind!");
8837330f729Sjoerg }
8847330f729Sjoerg
matches(const FunctionDecl * FD) const885*e038c9c4Sjoerg bool StdLibraryFunctionsChecker::Signature::matches(
886*e038c9c4Sjoerg const FunctionDecl *FD) const {
887*e038c9c4Sjoerg assert(!isInvalid());
888*e038c9c4Sjoerg // Check the number of arguments.
889*e038c9c4Sjoerg if (FD->param_size() != ArgTys.size())
8907330f729Sjoerg return false;
8917330f729Sjoerg
892*e038c9c4Sjoerg // The "restrict" keyword is illegal in C++, however, many libc
893*e038c9c4Sjoerg // implementations use the "__restrict" compiler intrinsic in functions
894*e038c9c4Sjoerg // prototypes. The "__restrict" keyword qualifies a type as a restricted type
895*e038c9c4Sjoerg // even in C++.
896*e038c9c4Sjoerg // In case of any non-C99 languages, we don't want to match based on the
897*e038c9c4Sjoerg // restrict qualifier because we cannot know if the given libc implementation
898*e038c9c4Sjoerg // qualifies the paramter type or not.
899*e038c9c4Sjoerg auto RemoveRestrict = [&FD](QualType T) {
900*e038c9c4Sjoerg if (!FD->getASTContext().getLangOpts().C99)
901*e038c9c4Sjoerg T.removeLocalRestrict();
902*e038c9c4Sjoerg return T;
903*e038c9c4Sjoerg };
9047330f729Sjoerg
905*e038c9c4Sjoerg // Check the return type.
906*e038c9c4Sjoerg if (!isIrrelevant(RetTy)) {
907*e038c9c4Sjoerg QualType FDRetTy = RemoveRestrict(FD->getReturnType().getCanonicalType());
908*e038c9c4Sjoerg if (RetTy != FDRetTy)
909*e038c9c4Sjoerg return false;
910*e038c9c4Sjoerg }
911*e038c9c4Sjoerg
912*e038c9c4Sjoerg // Check the argument types.
913*e038c9c4Sjoerg for (size_t I = 0, E = ArgTys.size(); I != E; ++I) {
914*e038c9c4Sjoerg QualType ArgTy = ArgTys[I];
915*e038c9c4Sjoerg if (isIrrelevant(ArgTy))
9167330f729Sjoerg continue;
917*e038c9c4Sjoerg QualType FDArgTy =
918*e038c9c4Sjoerg RemoveRestrict(FD->getParamDecl(I)->getType().getCanonicalType());
919*e038c9c4Sjoerg if (ArgTy != FDArgTy)
9207330f729Sjoerg return false;
9217330f729Sjoerg }
9227330f729Sjoerg
9237330f729Sjoerg return true;
9247330f729Sjoerg }
9257330f729Sjoerg
926*e038c9c4Sjoerg Optional<StdLibraryFunctionsChecker::Summary>
findFunctionSummary(const FunctionDecl * FD,CheckerContext & C) const9277330f729Sjoerg StdLibraryFunctionsChecker::findFunctionSummary(const FunctionDecl *FD,
9287330f729Sjoerg CheckerContext &C) const {
9297330f729Sjoerg if (!FD)
9307330f729Sjoerg return None;
9317330f729Sjoerg
932*e038c9c4Sjoerg initFunctionSummaries(C);
9337330f729Sjoerg
934*e038c9c4Sjoerg auto FSMI = FunctionSummaryMap.find(FD->getCanonicalDecl());
9357330f729Sjoerg if (FSMI == FunctionSummaryMap.end())
9367330f729Sjoerg return None;
937*e038c9c4Sjoerg return FSMI->second;
938*e038c9c4Sjoerg }
9397330f729Sjoerg
940*e038c9c4Sjoerg Optional<StdLibraryFunctionsChecker::Summary>
findFunctionSummary(const CallEvent & Call,CheckerContext & C) const941*e038c9c4Sjoerg StdLibraryFunctionsChecker::findFunctionSummary(const CallEvent &Call,
942*e038c9c4Sjoerg CheckerContext &C) const {
943*e038c9c4Sjoerg const FunctionDecl *FD = dyn_cast_or_null<FunctionDecl>(Call.getDecl());
944*e038c9c4Sjoerg if (!FD)
9457330f729Sjoerg return None;
946*e038c9c4Sjoerg return findFunctionSummary(FD, C);
9477330f729Sjoerg }
9487330f729Sjoerg
initFunctionSummaries(CheckerContext & C) const9497330f729Sjoerg void StdLibraryFunctionsChecker::initFunctionSummaries(
950*e038c9c4Sjoerg CheckerContext &C) const {
951*e038c9c4Sjoerg if (SummariesInitialized)
9527330f729Sjoerg return;
9537330f729Sjoerg
954*e038c9c4Sjoerg SValBuilder &SVB = C.getSValBuilder();
955*e038c9c4Sjoerg BasicValueFactory &BVF = SVB.getBasicValueFactory();
956*e038c9c4Sjoerg const ASTContext &ACtx = BVF.getContext();
957*e038c9c4Sjoerg
958*e038c9c4Sjoerg // Helper class to lookup a type by its name.
959*e038c9c4Sjoerg class LookupType {
960*e038c9c4Sjoerg const ASTContext &ACtx;
961*e038c9c4Sjoerg
962*e038c9c4Sjoerg public:
963*e038c9c4Sjoerg LookupType(const ASTContext &ACtx) : ACtx(ACtx) {}
964*e038c9c4Sjoerg
965*e038c9c4Sjoerg // Find the type. If not found then the optional is not set.
966*e038c9c4Sjoerg llvm::Optional<QualType> operator()(StringRef Name) {
967*e038c9c4Sjoerg IdentifierInfo &II = ACtx.Idents.get(Name);
968*e038c9c4Sjoerg auto LookupRes = ACtx.getTranslationUnitDecl()->lookup(&II);
969*e038c9c4Sjoerg if (LookupRes.empty())
970*e038c9c4Sjoerg return None;
971*e038c9c4Sjoerg
972*e038c9c4Sjoerg // Prioritze typedef declarations.
973*e038c9c4Sjoerg // This is needed in case of C struct typedefs. E.g.:
974*e038c9c4Sjoerg // typedef struct FILE FILE;
975*e038c9c4Sjoerg // In this case, we have a RecordDecl 'struct FILE' with the name 'FILE'
976*e038c9c4Sjoerg // and we have a TypedefDecl with the name 'FILE'.
977*e038c9c4Sjoerg for (Decl *D : LookupRes)
978*e038c9c4Sjoerg if (auto *TD = dyn_cast<TypedefNameDecl>(D))
979*e038c9c4Sjoerg return ACtx.getTypeDeclType(TD).getCanonicalType();
980*e038c9c4Sjoerg
981*e038c9c4Sjoerg // Find the first TypeDecl.
982*e038c9c4Sjoerg // There maybe cases when a function has the same name as a struct.
983*e038c9c4Sjoerg // E.g. in POSIX: `struct stat` and the function `stat()`:
984*e038c9c4Sjoerg // int stat(const char *restrict path, struct stat *restrict buf);
985*e038c9c4Sjoerg for (Decl *D : LookupRes)
986*e038c9c4Sjoerg if (auto *TD = dyn_cast<TypeDecl>(D))
987*e038c9c4Sjoerg return ACtx.getTypeDeclType(TD).getCanonicalType();
988*e038c9c4Sjoerg return None;
989*e038c9c4Sjoerg }
990*e038c9c4Sjoerg } lookupTy(ACtx);
991*e038c9c4Sjoerg
992*e038c9c4Sjoerg // Below are auxiliary classes to handle optional types that we get as a
993*e038c9c4Sjoerg // result of the lookup.
994*e038c9c4Sjoerg class GetRestrictTy {
995*e038c9c4Sjoerg const ASTContext &ACtx;
996*e038c9c4Sjoerg
997*e038c9c4Sjoerg public:
998*e038c9c4Sjoerg GetRestrictTy(const ASTContext &ACtx) : ACtx(ACtx) {}
999*e038c9c4Sjoerg QualType operator()(QualType Ty) {
1000*e038c9c4Sjoerg return ACtx.getLangOpts().C99 ? ACtx.getRestrictType(Ty) : Ty;
1001*e038c9c4Sjoerg }
1002*e038c9c4Sjoerg Optional<QualType> operator()(Optional<QualType> Ty) {
1003*e038c9c4Sjoerg if (Ty)
1004*e038c9c4Sjoerg return operator()(*Ty);
1005*e038c9c4Sjoerg return None;
1006*e038c9c4Sjoerg }
1007*e038c9c4Sjoerg } getRestrictTy(ACtx);
1008*e038c9c4Sjoerg class GetPointerTy {
1009*e038c9c4Sjoerg const ASTContext &ACtx;
1010*e038c9c4Sjoerg
1011*e038c9c4Sjoerg public:
1012*e038c9c4Sjoerg GetPointerTy(const ASTContext &ACtx) : ACtx(ACtx) {}
1013*e038c9c4Sjoerg QualType operator()(QualType Ty) { return ACtx.getPointerType(Ty); }
1014*e038c9c4Sjoerg Optional<QualType> operator()(Optional<QualType> Ty) {
1015*e038c9c4Sjoerg if (Ty)
1016*e038c9c4Sjoerg return operator()(*Ty);
1017*e038c9c4Sjoerg return None;
1018*e038c9c4Sjoerg }
1019*e038c9c4Sjoerg } getPointerTy(ACtx);
1020*e038c9c4Sjoerg class {
1021*e038c9c4Sjoerg public:
1022*e038c9c4Sjoerg Optional<QualType> operator()(Optional<QualType> Ty) {
1023*e038c9c4Sjoerg return Ty ? Optional<QualType>(Ty->withConst()) : None;
1024*e038c9c4Sjoerg }
1025*e038c9c4Sjoerg QualType operator()(QualType Ty) { return Ty.withConst(); }
1026*e038c9c4Sjoerg } getConstTy;
1027*e038c9c4Sjoerg class GetMaxValue {
1028*e038c9c4Sjoerg BasicValueFactory &BVF;
1029*e038c9c4Sjoerg
1030*e038c9c4Sjoerg public:
1031*e038c9c4Sjoerg GetMaxValue(BasicValueFactory &BVF) : BVF(BVF) {}
1032*e038c9c4Sjoerg Optional<RangeInt> operator()(QualType Ty) {
1033*e038c9c4Sjoerg return BVF.getMaxValue(Ty).getLimitedValue();
1034*e038c9c4Sjoerg }
1035*e038c9c4Sjoerg Optional<RangeInt> operator()(Optional<QualType> Ty) {
1036*e038c9c4Sjoerg if (Ty) {
1037*e038c9c4Sjoerg return operator()(*Ty);
1038*e038c9c4Sjoerg }
1039*e038c9c4Sjoerg return None;
1040*e038c9c4Sjoerg }
1041*e038c9c4Sjoerg } getMaxValue(BVF);
10427330f729Sjoerg
10437330f729Sjoerg // These types are useful for writing specifications quickly,
10447330f729Sjoerg // New specifications should probably introduce more types.
10457330f729Sjoerg // Some types are hard to obtain from the AST, eg. "ssize_t".
10467330f729Sjoerg // In such cases it should be possible to provide multiple variants
10477330f729Sjoerg // of function summary for common cases (eg. ssize_t could be int or long
10487330f729Sjoerg // or long long, so three summary variants would be enough).
10497330f729Sjoerg // Of course, function variants are also useful for C++ overloads.
1050*e038c9c4Sjoerg const QualType VoidTy = ACtx.VoidTy;
1051*e038c9c4Sjoerg const QualType CharTy = ACtx.CharTy;
1052*e038c9c4Sjoerg const QualType WCharTy = ACtx.WCharTy;
1053*e038c9c4Sjoerg const QualType IntTy = ACtx.IntTy;
1054*e038c9c4Sjoerg const QualType UnsignedIntTy = ACtx.UnsignedIntTy;
1055*e038c9c4Sjoerg const QualType LongTy = ACtx.LongTy;
1056*e038c9c4Sjoerg const QualType SizeTy = ACtx.getSizeType();
10577330f729Sjoerg
1058*e038c9c4Sjoerg const QualType VoidPtrTy = getPointerTy(VoidTy); // void *
1059*e038c9c4Sjoerg const QualType IntPtrTy = getPointerTy(IntTy); // int *
1060*e038c9c4Sjoerg const QualType UnsignedIntPtrTy =
1061*e038c9c4Sjoerg getPointerTy(UnsignedIntTy); // unsigned int *
1062*e038c9c4Sjoerg const QualType VoidPtrRestrictTy = getRestrictTy(VoidPtrTy);
1063*e038c9c4Sjoerg const QualType ConstVoidPtrTy =
1064*e038c9c4Sjoerg getPointerTy(getConstTy(VoidTy)); // const void *
1065*e038c9c4Sjoerg const QualType CharPtrTy = getPointerTy(CharTy); // char *
1066*e038c9c4Sjoerg const QualType CharPtrRestrictTy = getRestrictTy(CharPtrTy);
1067*e038c9c4Sjoerg const QualType ConstCharPtrTy =
1068*e038c9c4Sjoerg getPointerTy(getConstTy(CharTy)); // const char *
1069*e038c9c4Sjoerg const QualType ConstCharPtrRestrictTy = getRestrictTy(ConstCharPtrTy);
1070*e038c9c4Sjoerg const QualType Wchar_tPtrTy = getPointerTy(WCharTy); // wchar_t *
1071*e038c9c4Sjoerg const QualType ConstWchar_tPtrTy =
1072*e038c9c4Sjoerg getPointerTy(getConstTy(WCharTy)); // const wchar_t *
1073*e038c9c4Sjoerg const QualType ConstVoidPtrRestrictTy = getRestrictTy(ConstVoidPtrTy);
1074*e038c9c4Sjoerg const QualType SizePtrTy = getPointerTy(SizeTy);
1075*e038c9c4Sjoerg const QualType SizePtrRestrictTy = getRestrictTy(SizePtrTy);
1076*e038c9c4Sjoerg
1077*e038c9c4Sjoerg const RangeInt IntMax = BVF.getMaxValue(IntTy).getLimitedValue();
1078*e038c9c4Sjoerg const RangeInt UnsignedIntMax =
1079*e038c9c4Sjoerg BVF.getMaxValue(UnsignedIntTy).getLimitedValue();
1080*e038c9c4Sjoerg const RangeInt LongMax = BVF.getMaxValue(LongTy).getLimitedValue();
1081*e038c9c4Sjoerg const RangeInt SizeMax = BVF.getMaxValue(SizeTy).getLimitedValue();
1082*e038c9c4Sjoerg
1083*e038c9c4Sjoerg // Set UCharRangeMax to min of int or uchar maximum value.
1084*e038c9c4Sjoerg // The C standard states that the arguments of functions like isalpha must
1085*e038c9c4Sjoerg // be representable as an unsigned char. Their type is 'int', so the max
1086*e038c9c4Sjoerg // value of the argument should be min(UCharMax, IntMax). This just happen
1087*e038c9c4Sjoerg // to be true for commonly used and well tested instruction set
1088*e038c9c4Sjoerg // architectures, but not for others.
1089*e038c9c4Sjoerg const RangeInt UCharRangeMax =
1090*e038c9c4Sjoerg std::min(BVF.getMaxValue(ACtx.UnsignedCharTy).getLimitedValue(), IntMax);
1091*e038c9c4Sjoerg
1092*e038c9c4Sjoerg // The platform dependent value of EOF.
1093*e038c9c4Sjoerg // Try our best to parse this from the Preprocessor, otherwise fallback to -1.
1094*e038c9c4Sjoerg const auto EOFv = [&C]() -> RangeInt {
1095*e038c9c4Sjoerg if (const llvm::Optional<int> OptInt =
1096*e038c9c4Sjoerg tryExpandAsInteger("EOF", C.getPreprocessor()))
1097*e038c9c4Sjoerg return *OptInt;
1098*e038c9c4Sjoerg return -1;
1099*e038c9c4Sjoerg }();
1100*e038c9c4Sjoerg
1101*e038c9c4Sjoerg // Auxiliary class to aid adding summaries to the summary map.
1102*e038c9c4Sjoerg struct AddToFunctionSummaryMap {
1103*e038c9c4Sjoerg const ASTContext &ACtx;
1104*e038c9c4Sjoerg FunctionSummaryMapType ⤅
1105*e038c9c4Sjoerg bool DisplayLoadedSummaries;
1106*e038c9c4Sjoerg AddToFunctionSummaryMap(const ASTContext &ACtx, FunctionSummaryMapType &FSM,
1107*e038c9c4Sjoerg bool DisplayLoadedSummaries)
1108*e038c9c4Sjoerg : ACtx(ACtx), Map(FSM), DisplayLoadedSummaries(DisplayLoadedSummaries) {
1109*e038c9c4Sjoerg }
1110*e038c9c4Sjoerg
1111*e038c9c4Sjoerg // Add a summary to a FunctionDecl found by lookup. The lookup is performed
1112*e038c9c4Sjoerg // by the given Name, and in the global scope. The summary will be attached
1113*e038c9c4Sjoerg // to the found FunctionDecl only if the signatures match.
1114*e038c9c4Sjoerg //
1115*e038c9c4Sjoerg // Returns true if the summary has been added, false otherwise.
1116*e038c9c4Sjoerg bool operator()(StringRef Name, Signature Sign, Summary Sum) {
1117*e038c9c4Sjoerg if (Sign.isInvalid())
1118*e038c9c4Sjoerg return false;
1119*e038c9c4Sjoerg IdentifierInfo &II = ACtx.Idents.get(Name);
1120*e038c9c4Sjoerg auto LookupRes = ACtx.getTranslationUnitDecl()->lookup(&II);
1121*e038c9c4Sjoerg if (LookupRes.empty())
1122*e038c9c4Sjoerg return false;
1123*e038c9c4Sjoerg for (Decl *D : LookupRes) {
1124*e038c9c4Sjoerg if (auto *FD = dyn_cast<FunctionDecl>(D)) {
1125*e038c9c4Sjoerg if (Sum.matchesAndSet(Sign, FD)) {
1126*e038c9c4Sjoerg auto Res = Map.insert({FD->getCanonicalDecl(), Sum});
1127*e038c9c4Sjoerg assert(Res.second && "Function already has a summary set!");
1128*e038c9c4Sjoerg (void)Res;
1129*e038c9c4Sjoerg if (DisplayLoadedSummaries) {
1130*e038c9c4Sjoerg llvm::errs() << "Loaded summary for: ";
1131*e038c9c4Sjoerg FD->print(llvm::errs());
1132*e038c9c4Sjoerg llvm::errs() << "\n";
1133*e038c9c4Sjoerg }
1134*e038c9c4Sjoerg return true;
1135*e038c9c4Sjoerg }
1136*e038c9c4Sjoerg }
1137*e038c9c4Sjoerg }
1138*e038c9c4Sjoerg return false;
1139*e038c9c4Sjoerg }
1140*e038c9c4Sjoerg // Add the same summary for different names with the Signature explicitly
1141*e038c9c4Sjoerg // given.
1142*e038c9c4Sjoerg void operator()(std::vector<StringRef> Names, Signature Sign, Summary Sum) {
1143*e038c9c4Sjoerg for (StringRef Name : Names)
1144*e038c9c4Sjoerg operator()(Name, Sign, Sum);
1145*e038c9c4Sjoerg }
1146*e038c9c4Sjoerg } addToFunctionSummaryMap(ACtx, FunctionSummaryMap, DisplayLoadedSummaries);
1147*e038c9c4Sjoerg
1148*e038c9c4Sjoerg // Below are helpers functions to create the summaries.
1149*e038c9c4Sjoerg auto ArgumentCondition = [](ArgNo ArgN, RangeKind Kind,
1150*e038c9c4Sjoerg IntRangeVector Ranges) {
1151*e038c9c4Sjoerg return std::make_shared<RangeConstraint>(ArgN, Kind, Ranges);
1152*e038c9c4Sjoerg };
1153*e038c9c4Sjoerg auto BufferSize = [](auto... Args) {
1154*e038c9c4Sjoerg return std::make_shared<BufferSizeConstraint>(Args...);
1155*e038c9c4Sjoerg };
1156*e038c9c4Sjoerg struct {
1157*e038c9c4Sjoerg auto operator()(RangeKind Kind, IntRangeVector Ranges) {
1158*e038c9c4Sjoerg return std::make_shared<RangeConstraint>(Ret, Kind, Ranges);
1159*e038c9c4Sjoerg }
1160*e038c9c4Sjoerg auto operator()(BinaryOperator::Opcode Op, ArgNo OtherArgN) {
1161*e038c9c4Sjoerg return std::make_shared<ComparisonConstraint>(Ret, Op, OtherArgN);
1162*e038c9c4Sjoerg }
1163*e038c9c4Sjoerg } ReturnValueCondition;
1164*e038c9c4Sjoerg struct {
1165*e038c9c4Sjoerg auto operator()(RangeInt b, RangeInt e) {
1166*e038c9c4Sjoerg return IntRangeVector{std::pair<RangeInt, RangeInt>{b, e}};
1167*e038c9c4Sjoerg }
1168*e038c9c4Sjoerg auto operator()(RangeInt b, Optional<RangeInt> e) {
1169*e038c9c4Sjoerg if (e)
1170*e038c9c4Sjoerg return IntRangeVector{std::pair<RangeInt, RangeInt>{b, *e}};
1171*e038c9c4Sjoerg return IntRangeVector{};
1172*e038c9c4Sjoerg }
1173*e038c9c4Sjoerg auto operator()(std::pair<RangeInt, RangeInt> i0,
1174*e038c9c4Sjoerg std::pair<RangeInt, Optional<RangeInt>> i1) {
1175*e038c9c4Sjoerg if (i1.second)
1176*e038c9c4Sjoerg return IntRangeVector{i0, {i1.first, *(i1.second)}};
1177*e038c9c4Sjoerg return IntRangeVector{i0};
1178*e038c9c4Sjoerg }
1179*e038c9c4Sjoerg } Range;
1180*e038c9c4Sjoerg auto SingleValue = [](RangeInt v) {
1181*e038c9c4Sjoerg return IntRangeVector{std::pair<RangeInt, RangeInt>{v, v}};
1182*e038c9c4Sjoerg };
1183*e038c9c4Sjoerg auto LessThanOrEq = BO_LE;
1184*e038c9c4Sjoerg auto NotNull = [&](ArgNo ArgN) {
1185*e038c9c4Sjoerg return std::make_shared<NotNullConstraint>(ArgN);
1186*e038c9c4Sjoerg };
1187*e038c9c4Sjoerg
1188*e038c9c4Sjoerg Optional<QualType> FileTy = lookupTy("FILE");
1189*e038c9c4Sjoerg Optional<QualType> FilePtrTy = getPointerTy(FileTy);
1190*e038c9c4Sjoerg Optional<QualType> FilePtrRestrictTy = getRestrictTy(FilePtrTy);
11917330f729Sjoerg
11927330f729Sjoerg // We are finally ready to define specifications for all supported functions.
11937330f729Sjoerg //
11947330f729Sjoerg // Argument ranges should always cover all variants. If return value
11957330f729Sjoerg // is completely unknown, omit it from the respective range set.
11967330f729Sjoerg //
11977330f729Sjoerg // Every item in the list of range sets represents a particular
11987330f729Sjoerg // execution path the analyzer would need to explore once
11997330f729Sjoerg // the call is modeled - a new program state is constructed
12007330f729Sjoerg // for every range set, and each range line in the range set
12017330f729Sjoerg // corresponds to a specific constraint within this state.
12027330f729Sjoerg
12037330f729Sjoerg // The isascii() family of functions.
1204*e038c9c4Sjoerg // The behavior is undefined if the value of the argument is not
1205*e038c9c4Sjoerg // representable as unsigned char or is not equal to EOF. See e.g. C99
1206*e038c9c4Sjoerg // 7.4.1.2 The isalpha function (p: 181-182).
1207*e038c9c4Sjoerg addToFunctionSummaryMap(
1208*e038c9c4Sjoerg "isalnum", Signature(ArgTypes{IntTy}, RetType{IntTy}),
1209*e038c9c4Sjoerg Summary(EvalCallAsPure)
1210*e038c9c4Sjoerg // Boils down to isupper() or islower() or isdigit().
1211*e038c9c4Sjoerg .Case({ArgumentCondition(0U, WithinRange,
1212*e038c9c4Sjoerg {{'0', '9'}, {'A', 'Z'}, {'a', 'z'}}),
1213*e038c9c4Sjoerg ReturnValueCondition(OutOfRange, SingleValue(0))})
1214*e038c9c4Sjoerg // The locale-specific range.
12157330f729Sjoerg // No post-condition. We are completely unaware of
12167330f729Sjoerg // locale-specific return values.
1217*e038c9c4Sjoerg .Case({ArgumentCondition(0U, WithinRange, {{128, UCharRangeMax}})})
1218*e038c9c4Sjoerg .Case(
1219*e038c9c4Sjoerg {ArgumentCondition(
1220*e038c9c4Sjoerg 0U, OutOfRange,
1221*e038c9c4Sjoerg {{'0', '9'}, {'A', 'Z'}, {'a', 'z'}, {128, UCharRangeMax}}),
1222*e038c9c4Sjoerg ReturnValueCondition(WithinRange, SingleValue(0))})
1223*e038c9c4Sjoerg .ArgConstraint(ArgumentCondition(
1224*e038c9c4Sjoerg 0U, WithinRange, {{EOFv, EOFv}, {0, UCharRangeMax}})));
1225*e038c9c4Sjoerg addToFunctionSummaryMap(
1226*e038c9c4Sjoerg "isalpha", Signature(ArgTypes{IntTy}, RetType{IntTy}),
1227*e038c9c4Sjoerg Summary(EvalCallAsPure)
1228*e038c9c4Sjoerg .Case({ArgumentCondition(0U, WithinRange, {{'A', 'Z'}, {'a', 'z'}}),
1229*e038c9c4Sjoerg ReturnValueCondition(OutOfRange, SingleValue(0))})
1230*e038c9c4Sjoerg // The locale-specific range.
1231*e038c9c4Sjoerg .Case({ArgumentCondition(0U, WithinRange, {{128, UCharRangeMax}})})
1232*e038c9c4Sjoerg .Case({ArgumentCondition(
1233*e038c9c4Sjoerg 0U, OutOfRange,
1234*e038c9c4Sjoerg {{'A', 'Z'}, {'a', 'z'}, {128, UCharRangeMax}}),
1235*e038c9c4Sjoerg ReturnValueCondition(WithinRange, SingleValue(0))}));
1236*e038c9c4Sjoerg addToFunctionSummaryMap(
1237*e038c9c4Sjoerg "isascii", Signature(ArgTypes{IntTy}, RetType{IntTy}),
1238*e038c9c4Sjoerg Summary(EvalCallAsPure)
1239*e038c9c4Sjoerg .Case({ArgumentCondition(0U, WithinRange, Range(0, 127)),
1240*e038c9c4Sjoerg ReturnValueCondition(OutOfRange, SingleValue(0))})
1241*e038c9c4Sjoerg .Case({ArgumentCondition(0U, OutOfRange, Range(0, 127)),
1242*e038c9c4Sjoerg ReturnValueCondition(WithinRange, SingleValue(0))}));
1243*e038c9c4Sjoerg addToFunctionSummaryMap(
1244*e038c9c4Sjoerg "isblank", Signature(ArgTypes{IntTy}, RetType{IntTy}),
1245*e038c9c4Sjoerg Summary(EvalCallAsPure)
1246*e038c9c4Sjoerg .Case({ArgumentCondition(0U, WithinRange, {{'\t', '\t'}, {' ', ' '}}),
1247*e038c9c4Sjoerg ReturnValueCondition(OutOfRange, SingleValue(0))})
1248*e038c9c4Sjoerg .Case({ArgumentCondition(0U, OutOfRange, {{'\t', '\t'}, {' ', ' '}}),
1249*e038c9c4Sjoerg ReturnValueCondition(WithinRange, SingleValue(0))}));
1250*e038c9c4Sjoerg addToFunctionSummaryMap(
1251*e038c9c4Sjoerg "iscntrl", Signature(ArgTypes{IntTy}, RetType{IntTy}),
1252*e038c9c4Sjoerg Summary(EvalCallAsPure)
1253*e038c9c4Sjoerg .Case({ArgumentCondition(0U, WithinRange, {{0, 32}, {127, 127}}),
1254*e038c9c4Sjoerg ReturnValueCondition(OutOfRange, SingleValue(0))})
1255*e038c9c4Sjoerg .Case({ArgumentCondition(0U, OutOfRange, {{0, 32}, {127, 127}}),
1256*e038c9c4Sjoerg ReturnValueCondition(WithinRange, SingleValue(0))}));
1257*e038c9c4Sjoerg addToFunctionSummaryMap(
1258*e038c9c4Sjoerg "isdigit", Signature(ArgTypes{IntTy}, RetType{IntTy}),
1259*e038c9c4Sjoerg Summary(EvalCallAsPure)
1260*e038c9c4Sjoerg .Case({ArgumentCondition(0U, WithinRange, Range('0', '9')),
1261*e038c9c4Sjoerg ReturnValueCondition(OutOfRange, SingleValue(0))})
1262*e038c9c4Sjoerg .Case({ArgumentCondition(0U, OutOfRange, Range('0', '9')),
1263*e038c9c4Sjoerg ReturnValueCondition(WithinRange, SingleValue(0))}));
1264*e038c9c4Sjoerg addToFunctionSummaryMap(
1265*e038c9c4Sjoerg "isgraph", Signature(ArgTypes{IntTy}, RetType{IntTy}),
1266*e038c9c4Sjoerg Summary(EvalCallAsPure)
1267*e038c9c4Sjoerg .Case({ArgumentCondition(0U, WithinRange, Range(33, 126)),
1268*e038c9c4Sjoerg ReturnValueCondition(OutOfRange, SingleValue(0))})
1269*e038c9c4Sjoerg .Case({ArgumentCondition(0U, OutOfRange, Range(33, 126)),
1270*e038c9c4Sjoerg ReturnValueCondition(WithinRange, SingleValue(0))}));
1271*e038c9c4Sjoerg addToFunctionSummaryMap(
1272*e038c9c4Sjoerg "islower", Signature(ArgTypes{IntTy}, RetType{IntTy}),
1273*e038c9c4Sjoerg Summary(EvalCallAsPure)
1274*e038c9c4Sjoerg // Is certainly lowercase.
1275*e038c9c4Sjoerg .Case({ArgumentCondition(0U, WithinRange, Range('a', 'z')),
1276*e038c9c4Sjoerg ReturnValueCondition(OutOfRange, SingleValue(0))})
1277*e038c9c4Sjoerg // Is ascii but not lowercase.
1278*e038c9c4Sjoerg .Case({ArgumentCondition(0U, WithinRange, Range(0, 127)),
1279*e038c9c4Sjoerg ArgumentCondition(0U, OutOfRange, Range('a', 'z')),
1280*e038c9c4Sjoerg ReturnValueCondition(WithinRange, SingleValue(0))})
1281*e038c9c4Sjoerg // The locale-specific range.
1282*e038c9c4Sjoerg .Case({ArgumentCondition(0U, WithinRange, {{128, UCharRangeMax}})})
1283*e038c9c4Sjoerg // Is not an unsigned char.
1284*e038c9c4Sjoerg .Case({ArgumentCondition(0U, OutOfRange, Range(0, UCharRangeMax)),
1285*e038c9c4Sjoerg ReturnValueCondition(WithinRange, SingleValue(0))}));
1286*e038c9c4Sjoerg addToFunctionSummaryMap(
1287*e038c9c4Sjoerg "isprint", Signature(ArgTypes{IntTy}, RetType{IntTy}),
1288*e038c9c4Sjoerg Summary(EvalCallAsPure)
1289*e038c9c4Sjoerg .Case({ArgumentCondition(0U, WithinRange, Range(32, 126)),
1290*e038c9c4Sjoerg ReturnValueCondition(OutOfRange, SingleValue(0))})
1291*e038c9c4Sjoerg .Case({ArgumentCondition(0U, OutOfRange, Range(32, 126)),
1292*e038c9c4Sjoerg ReturnValueCondition(WithinRange, SingleValue(0))}));
1293*e038c9c4Sjoerg addToFunctionSummaryMap(
1294*e038c9c4Sjoerg "ispunct", Signature(ArgTypes{IntTy}, RetType{IntTy}),
1295*e038c9c4Sjoerg Summary(EvalCallAsPure)
1296*e038c9c4Sjoerg .Case({ArgumentCondition(
1297*e038c9c4Sjoerg 0U, WithinRange,
1298*e038c9c4Sjoerg {{'!', '/'}, {':', '@'}, {'[', '`'}, {'{', '~'}}),
1299*e038c9c4Sjoerg ReturnValueCondition(OutOfRange, SingleValue(0))})
1300*e038c9c4Sjoerg .Case({ArgumentCondition(
1301*e038c9c4Sjoerg 0U, OutOfRange,
1302*e038c9c4Sjoerg {{'!', '/'}, {':', '@'}, {'[', '`'}, {'{', '~'}}),
1303*e038c9c4Sjoerg ReturnValueCondition(WithinRange, SingleValue(0))}));
1304*e038c9c4Sjoerg addToFunctionSummaryMap(
1305*e038c9c4Sjoerg "isspace", Signature(ArgTypes{IntTy}, RetType{IntTy}),
1306*e038c9c4Sjoerg Summary(EvalCallAsPure)
1307*e038c9c4Sjoerg // Space, '\f', '\n', '\r', '\t', '\v'.
1308*e038c9c4Sjoerg .Case({ArgumentCondition(0U, WithinRange, {{9, 13}, {' ', ' '}}),
1309*e038c9c4Sjoerg ReturnValueCondition(OutOfRange, SingleValue(0))})
1310*e038c9c4Sjoerg // The locale-specific range.
1311*e038c9c4Sjoerg .Case({ArgumentCondition(0U, WithinRange, {{128, UCharRangeMax}})})
1312*e038c9c4Sjoerg .Case({ArgumentCondition(0U, OutOfRange,
1313*e038c9c4Sjoerg {{9, 13}, {' ', ' '}, {128, UCharRangeMax}}),
1314*e038c9c4Sjoerg ReturnValueCondition(WithinRange, SingleValue(0))}));
1315*e038c9c4Sjoerg addToFunctionSummaryMap(
1316*e038c9c4Sjoerg "isupper", Signature(ArgTypes{IntTy}, RetType{IntTy}),
1317*e038c9c4Sjoerg Summary(EvalCallAsPure)
1318*e038c9c4Sjoerg // Is certainly uppercase.
1319*e038c9c4Sjoerg .Case({ArgumentCondition(0U, WithinRange, Range('A', 'Z')),
1320*e038c9c4Sjoerg ReturnValueCondition(OutOfRange, SingleValue(0))})
1321*e038c9c4Sjoerg // The locale-specific range.
1322*e038c9c4Sjoerg .Case({ArgumentCondition(0U, WithinRange, {{128, UCharRangeMax}})})
1323*e038c9c4Sjoerg // Other.
1324*e038c9c4Sjoerg .Case({ArgumentCondition(0U, OutOfRange,
1325*e038c9c4Sjoerg {{'A', 'Z'}, {128, UCharRangeMax}}),
1326*e038c9c4Sjoerg ReturnValueCondition(WithinRange, SingleValue(0))}));
1327*e038c9c4Sjoerg addToFunctionSummaryMap(
1328*e038c9c4Sjoerg "isxdigit", Signature(ArgTypes{IntTy}, RetType{IntTy}),
1329*e038c9c4Sjoerg Summary(EvalCallAsPure)
1330*e038c9c4Sjoerg .Case({ArgumentCondition(0U, WithinRange,
1331*e038c9c4Sjoerg {{'0', '9'}, {'A', 'F'}, {'a', 'f'}}),
1332*e038c9c4Sjoerg ReturnValueCondition(OutOfRange, SingleValue(0))})
1333*e038c9c4Sjoerg .Case({ArgumentCondition(0U, OutOfRange,
1334*e038c9c4Sjoerg {{'0', '9'}, {'A', 'F'}, {'a', 'f'}}),
1335*e038c9c4Sjoerg ReturnValueCondition(WithinRange, SingleValue(0))}));
1336*e038c9c4Sjoerg addToFunctionSummaryMap(
1337*e038c9c4Sjoerg "toupper", Signature(ArgTypes{IntTy}, RetType{IntTy}),
1338*e038c9c4Sjoerg Summary(EvalCallAsPure)
1339*e038c9c4Sjoerg .ArgConstraint(ArgumentCondition(
1340*e038c9c4Sjoerg 0U, WithinRange, {{EOFv, EOFv}, {0, UCharRangeMax}})));
1341*e038c9c4Sjoerg addToFunctionSummaryMap(
1342*e038c9c4Sjoerg "tolower", Signature(ArgTypes{IntTy}, RetType{IntTy}),
1343*e038c9c4Sjoerg Summary(EvalCallAsPure)
1344*e038c9c4Sjoerg .ArgConstraint(ArgumentCondition(
1345*e038c9c4Sjoerg 0U, WithinRange, {{EOFv, EOFv}, {0, UCharRangeMax}})));
1346*e038c9c4Sjoerg addToFunctionSummaryMap(
1347*e038c9c4Sjoerg "toascii", Signature(ArgTypes{IntTy}, RetType{IntTy}),
1348*e038c9c4Sjoerg Summary(EvalCallAsPure)
1349*e038c9c4Sjoerg .ArgConstraint(ArgumentCondition(
1350*e038c9c4Sjoerg 0U, WithinRange, {{EOFv, EOFv}, {0, UCharRangeMax}})));
13517330f729Sjoerg
13527330f729Sjoerg // The getc() family of functions that returns either a char or an EOF.
1353*e038c9c4Sjoerg addToFunctionSummaryMap(
1354*e038c9c4Sjoerg {"getc", "fgetc"}, Signature(ArgTypes{FilePtrTy}, RetType{IntTy}),
1355*e038c9c4Sjoerg Summary(NoEvalCall)
1356*e038c9c4Sjoerg .Case({ReturnValueCondition(WithinRange,
1357*e038c9c4Sjoerg {{EOFv, EOFv}, {0, UCharRangeMax}})}));
1358*e038c9c4Sjoerg addToFunctionSummaryMap(
1359*e038c9c4Sjoerg "getchar", Signature(ArgTypes{}, RetType{IntTy}),
1360*e038c9c4Sjoerg Summary(NoEvalCall)
1361*e038c9c4Sjoerg .Case({ReturnValueCondition(WithinRange,
1362*e038c9c4Sjoerg {{EOFv, EOFv}, {0, UCharRangeMax}})}));
13637330f729Sjoerg
13647330f729Sjoerg // read()-like functions that never return more than buffer size.
1365*e038c9c4Sjoerg auto FreadSummary =
1366*e038c9c4Sjoerg Summary(NoEvalCall)
1367*e038c9c4Sjoerg .Case({ReturnValueCondition(LessThanOrEq, ArgNo(2)),
1368*e038c9c4Sjoerg ReturnValueCondition(WithinRange, Range(0, SizeMax))})
1369*e038c9c4Sjoerg .ArgConstraint(NotNull(ArgNo(0)))
1370*e038c9c4Sjoerg .ArgConstraint(NotNull(ArgNo(3)))
1371*e038c9c4Sjoerg .ArgConstraint(BufferSize(/*Buffer=*/ArgNo(0), /*BufSize=*/ArgNo(1),
1372*e038c9c4Sjoerg /*BufSizeMultiplier=*/ArgNo(2)));
1373*e038c9c4Sjoerg
1374*e038c9c4Sjoerg // size_t fread(void *restrict ptr, size_t size, size_t nitems,
1375*e038c9c4Sjoerg // FILE *restrict stream);
1376*e038c9c4Sjoerg addToFunctionSummaryMap(
1377*e038c9c4Sjoerg "fread",
1378*e038c9c4Sjoerg Signature(ArgTypes{VoidPtrRestrictTy, SizeTy, SizeTy, FilePtrRestrictTy},
1379*e038c9c4Sjoerg RetType{SizeTy}),
1380*e038c9c4Sjoerg FreadSummary);
1381*e038c9c4Sjoerg // size_t fwrite(const void *restrict ptr, size_t size, size_t nitems,
1382*e038c9c4Sjoerg // FILE *restrict stream);
1383*e038c9c4Sjoerg addToFunctionSummaryMap("fwrite",
1384*e038c9c4Sjoerg Signature(ArgTypes{ConstVoidPtrRestrictTy, SizeTy,
1385*e038c9c4Sjoerg SizeTy, FilePtrRestrictTy},
1386*e038c9c4Sjoerg RetType{SizeTy}),
1387*e038c9c4Sjoerg FreadSummary);
1388*e038c9c4Sjoerg
1389*e038c9c4Sjoerg Optional<QualType> Ssize_tTy = lookupTy("ssize_t");
1390*e038c9c4Sjoerg Optional<RangeInt> Ssize_tMax = getMaxValue(Ssize_tTy);
1391*e038c9c4Sjoerg
1392*e038c9c4Sjoerg auto ReadSummary =
1393*e038c9c4Sjoerg Summary(NoEvalCall)
1394*e038c9c4Sjoerg .Case({ReturnValueCondition(LessThanOrEq, ArgNo(2)),
1395*e038c9c4Sjoerg ReturnValueCondition(WithinRange, Range(-1, Ssize_tMax))});
1396*e038c9c4Sjoerg
1397*e038c9c4Sjoerg // FIXME these are actually defined by POSIX and not by the C standard, we
1398*e038c9c4Sjoerg // should handle them together with the rest of the POSIX functions.
1399*e038c9c4Sjoerg // ssize_t read(int fildes, void *buf, size_t nbyte);
1400*e038c9c4Sjoerg addToFunctionSummaryMap(
1401*e038c9c4Sjoerg "read", Signature(ArgTypes{IntTy, VoidPtrTy, SizeTy}, RetType{Ssize_tTy}),
1402*e038c9c4Sjoerg ReadSummary);
1403*e038c9c4Sjoerg // ssize_t write(int fildes, const void *buf, size_t nbyte);
1404*e038c9c4Sjoerg addToFunctionSummaryMap(
1405*e038c9c4Sjoerg "write",
1406*e038c9c4Sjoerg Signature(ArgTypes{IntTy, ConstVoidPtrTy, SizeTy}, RetType{Ssize_tTy}),
1407*e038c9c4Sjoerg ReadSummary);
1408*e038c9c4Sjoerg
1409*e038c9c4Sjoerg auto GetLineSummary =
1410*e038c9c4Sjoerg Summary(NoEvalCall)
1411*e038c9c4Sjoerg .Case({ReturnValueCondition(WithinRange,
1412*e038c9c4Sjoerg Range({-1, -1}, {1, Ssize_tMax}))});
1413*e038c9c4Sjoerg
1414*e038c9c4Sjoerg QualType CharPtrPtrRestrictTy = getRestrictTy(getPointerTy(CharPtrTy));
14157330f729Sjoerg
14167330f729Sjoerg // getline()-like functions either fail or read at least the delimiter.
1417*e038c9c4Sjoerg // FIXME these are actually defined by POSIX and not by the C standard, we
1418*e038c9c4Sjoerg // should handle them together with the rest of the POSIX functions.
1419*e038c9c4Sjoerg // ssize_t getline(char **restrict lineptr, size_t *restrict n,
1420*e038c9c4Sjoerg // FILE *restrict stream);
1421*e038c9c4Sjoerg addToFunctionSummaryMap(
1422*e038c9c4Sjoerg "getline",
1423*e038c9c4Sjoerg Signature(
1424*e038c9c4Sjoerg ArgTypes{CharPtrPtrRestrictTy, SizePtrRestrictTy, FilePtrRestrictTy},
1425*e038c9c4Sjoerg RetType{Ssize_tTy}),
1426*e038c9c4Sjoerg GetLineSummary);
1427*e038c9c4Sjoerg // ssize_t getdelim(char **restrict lineptr, size_t *restrict n,
1428*e038c9c4Sjoerg // int delimiter, FILE *restrict stream);
1429*e038c9c4Sjoerg addToFunctionSummaryMap(
1430*e038c9c4Sjoerg "getdelim",
1431*e038c9c4Sjoerg Signature(ArgTypes{CharPtrPtrRestrictTy, SizePtrRestrictTy, IntTy,
1432*e038c9c4Sjoerg FilePtrRestrictTy},
1433*e038c9c4Sjoerg RetType{Ssize_tTy}),
1434*e038c9c4Sjoerg GetLineSummary);
1435*e038c9c4Sjoerg
1436*e038c9c4Sjoerg if (ModelPOSIX) {
1437*e038c9c4Sjoerg
1438*e038c9c4Sjoerg // long a64l(const char *str64);
1439*e038c9c4Sjoerg addToFunctionSummaryMap(
1440*e038c9c4Sjoerg "a64l", Signature(ArgTypes{ConstCharPtrTy}, RetType{LongTy}),
1441*e038c9c4Sjoerg Summary(NoEvalCall).ArgConstraint(NotNull(ArgNo(0))));
1442*e038c9c4Sjoerg
1443*e038c9c4Sjoerg // char *l64a(long value);
1444*e038c9c4Sjoerg addToFunctionSummaryMap("l64a",
1445*e038c9c4Sjoerg Signature(ArgTypes{LongTy}, RetType{CharPtrTy}),
1446*e038c9c4Sjoerg Summary(NoEvalCall)
1447*e038c9c4Sjoerg .ArgConstraint(ArgumentCondition(
1448*e038c9c4Sjoerg 0, WithinRange, Range(0, LongMax))));
1449*e038c9c4Sjoerg
1450*e038c9c4Sjoerg const auto ReturnsZeroOrMinusOne =
1451*e038c9c4Sjoerg ConstraintSet{ReturnValueCondition(WithinRange, Range(-1, 0))};
1452*e038c9c4Sjoerg const auto ReturnsFileDescriptor =
1453*e038c9c4Sjoerg ConstraintSet{ReturnValueCondition(WithinRange, Range(-1, IntMax))};
1454*e038c9c4Sjoerg
1455*e038c9c4Sjoerg // int access(const char *pathname, int amode);
1456*e038c9c4Sjoerg addToFunctionSummaryMap(
1457*e038c9c4Sjoerg "access", Signature(ArgTypes{ConstCharPtrTy, IntTy}, RetType{IntTy}),
1458*e038c9c4Sjoerg Summary(NoEvalCall)
1459*e038c9c4Sjoerg .Case(ReturnsZeroOrMinusOne)
1460*e038c9c4Sjoerg .ArgConstraint(NotNull(ArgNo(0))));
1461*e038c9c4Sjoerg
1462*e038c9c4Sjoerg // int faccessat(int dirfd, const char *pathname, int mode, int flags);
1463*e038c9c4Sjoerg addToFunctionSummaryMap(
1464*e038c9c4Sjoerg "faccessat",
1465*e038c9c4Sjoerg Signature(ArgTypes{IntTy, ConstCharPtrTy, IntTy, IntTy},
1466*e038c9c4Sjoerg RetType{IntTy}),
1467*e038c9c4Sjoerg Summary(NoEvalCall)
1468*e038c9c4Sjoerg .Case(ReturnsZeroOrMinusOne)
1469*e038c9c4Sjoerg .ArgConstraint(NotNull(ArgNo(1))));
1470*e038c9c4Sjoerg
1471*e038c9c4Sjoerg // int dup(int fildes);
1472*e038c9c4Sjoerg addToFunctionSummaryMap("dup", Signature(ArgTypes{IntTy}, RetType{IntTy}),
1473*e038c9c4Sjoerg Summary(NoEvalCall)
1474*e038c9c4Sjoerg .Case(ReturnsFileDescriptor)
1475*e038c9c4Sjoerg .ArgConstraint(ArgumentCondition(
1476*e038c9c4Sjoerg 0, WithinRange, Range(0, IntMax))));
1477*e038c9c4Sjoerg
1478*e038c9c4Sjoerg // int dup2(int fildes1, int filedes2);
1479*e038c9c4Sjoerg addToFunctionSummaryMap(
1480*e038c9c4Sjoerg "dup2", Signature(ArgTypes{IntTy, IntTy}, RetType{IntTy}),
1481*e038c9c4Sjoerg Summary(NoEvalCall)
1482*e038c9c4Sjoerg .Case(ReturnsFileDescriptor)
1483*e038c9c4Sjoerg .ArgConstraint(ArgumentCondition(0, WithinRange, Range(0, IntMax)))
1484*e038c9c4Sjoerg .ArgConstraint(
1485*e038c9c4Sjoerg ArgumentCondition(1, WithinRange, Range(0, IntMax))));
1486*e038c9c4Sjoerg
1487*e038c9c4Sjoerg // int fdatasync(int fildes);
1488*e038c9c4Sjoerg addToFunctionSummaryMap("fdatasync",
1489*e038c9c4Sjoerg Signature(ArgTypes{IntTy}, RetType{IntTy}),
1490*e038c9c4Sjoerg Summary(NoEvalCall)
1491*e038c9c4Sjoerg .Case(ReturnsZeroOrMinusOne)
1492*e038c9c4Sjoerg .ArgConstraint(ArgumentCondition(
1493*e038c9c4Sjoerg 0, WithinRange, Range(0, IntMax))));
1494*e038c9c4Sjoerg
1495*e038c9c4Sjoerg // int fnmatch(const char *pattern, const char *string, int flags);
1496*e038c9c4Sjoerg addToFunctionSummaryMap(
1497*e038c9c4Sjoerg "fnmatch",
1498*e038c9c4Sjoerg Signature(ArgTypes{ConstCharPtrTy, ConstCharPtrTy, IntTy},
1499*e038c9c4Sjoerg RetType{IntTy}),
1500*e038c9c4Sjoerg Summary(EvalCallAsPure)
1501*e038c9c4Sjoerg .ArgConstraint(NotNull(ArgNo(0)))
1502*e038c9c4Sjoerg .ArgConstraint(NotNull(ArgNo(1))));
1503*e038c9c4Sjoerg
1504*e038c9c4Sjoerg // int fsync(int fildes);
1505*e038c9c4Sjoerg addToFunctionSummaryMap("fsync", Signature(ArgTypes{IntTy}, RetType{IntTy}),
1506*e038c9c4Sjoerg Summary(NoEvalCall)
1507*e038c9c4Sjoerg .Case(ReturnsZeroOrMinusOne)
1508*e038c9c4Sjoerg .ArgConstraint(ArgumentCondition(
1509*e038c9c4Sjoerg 0, WithinRange, Range(0, IntMax))));
1510*e038c9c4Sjoerg
1511*e038c9c4Sjoerg Optional<QualType> Off_tTy = lookupTy("off_t");
1512*e038c9c4Sjoerg
1513*e038c9c4Sjoerg // int truncate(const char *path, off_t length);
1514*e038c9c4Sjoerg addToFunctionSummaryMap(
1515*e038c9c4Sjoerg "truncate",
1516*e038c9c4Sjoerg Signature(ArgTypes{ConstCharPtrTy, Off_tTy}, RetType{IntTy}),
1517*e038c9c4Sjoerg Summary(NoEvalCall)
1518*e038c9c4Sjoerg .Case(ReturnsZeroOrMinusOne)
1519*e038c9c4Sjoerg .ArgConstraint(NotNull(ArgNo(0))));
1520*e038c9c4Sjoerg
1521*e038c9c4Sjoerg // int symlink(const char *oldpath, const char *newpath);
1522*e038c9c4Sjoerg addToFunctionSummaryMap(
1523*e038c9c4Sjoerg "symlink",
1524*e038c9c4Sjoerg Signature(ArgTypes{ConstCharPtrTy, ConstCharPtrTy}, RetType{IntTy}),
1525*e038c9c4Sjoerg Summary(NoEvalCall)
1526*e038c9c4Sjoerg .Case(ReturnsZeroOrMinusOne)
1527*e038c9c4Sjoerg .ArgConstraint(NotNull(ArgNo(0)))
1528*e038c9c4Sjoerg .ArgConstraint(NotNull(ArgNo(1))));
1529*e038c9c4Sjoerg
1530*e038c9c4Sjoerg // int symlinkat(const char *oldpath, int newdirfd, const char *newpath);
1531*e038c9c4Sjoerg addToFunctionSummaryMap(
1532*e038c9c4Sjoerg "symlinkat",
1533*e038c9c4Sjoerg Signature(ArgTypes{ConstCharPtrTy, IntTy, ConstCharPtrTy},
1534*e038c9c4Sjoerg RetType{IntTy}),
1535*e038c9c4Sjoerg Summary(NoEvalCall)
1536*e038c9c4Sjoerg .Case(ReturnsZeroOrMinusOne)
1537*e038c9c4Sjoerg .ArgConstraint(NotNull(ArgNo(0)))
1538*e038c9c4Sjoerg .ArgConstraint(ArgumentCondition(1, WithinRange, Range(0, IntMax)))
1539*e038c9c4Sjoerg .ArgConstraint(NotNull(ArgNo(2))));
1540*e038c9c4Sjoerg
1541*e038c9c4Sjoerg // int lockf(int fd, int cmd, off_t len);
1542*e038c9c4Sjoerg addToFunctionSummaryMap(
1543*e038c9c4Sjoerg "lockf", Signature(ArgTypes{IntTy, IntTy, Off_tTy}, RetType{IntTy}),
1544*e038c9c4Sjoerg Summary(NoEvalCall)
1545*e038c9c4Sjoerg .Case(ReturnsZeroOrMinusOne)
1546*e038c9c4Sjoerg .ArgConstraint(
1547*e038c9c4Sjoerg ArgumentCondition(0, WithinRange, Range(0, IntMax))));
1548*e038c9c4Sjoerg
1549*e038c9c4Sjoerg Optional<QualType> Mode_tTy = lookupTy("mode_t");
1550*e038c9c4Sjoerg
1551*e038c9c4Sjoerg // int creat(const char *pathname, mode_t mode);
1552*e038c9c4Sjoerg addToFunctionSummaryMap(
1553*e038c9c4Sjoerg "creat", Signature(ArgTypes{ConstCharPtrTy, Mode_tTy}, RetType{IntTy}),
1554*e038c9c4Sjoerg Summary(NoEvalCall)
1555*e038c9c4Sjoerg .Case(ReturnsFileDescriptor)
1556*e038c9c4Sjoerg .ArgConstraint(NotNull(ArgNo(0))));
1557*e038c9c4Sjoerg
1558*e038c9c4Sjoerg // unsigned int sleep(unsigned int seconds);
1559*e038c9c4Sjoerg addToFunctionSummaryMap(
1560*e038c9c4Sjoerg "sleep", Signature(ArgTypes{UnsignedIntTy}, RetType{UnsignedIntTy}),
1561*e038c9c4Sjoerg Summary(NoEvalCall)
1562*e038c9c4Sjoerg .ArgConstraint(
1563*e038c9c4Sjoerg ArgumentCondition(0, WithinRange, Range(0, UnsignedIntMax))));
1564*e038c9c4Sjoerg
1565*e038c9c4Sjoerg Optional<QualType> DirTy = lookupTy("DIR");
1566*e038c9c4Sjoerg Optional<QualType> DirPtrTy = getPointerTy(DirTy);
1567*e038c9c4Sjoerg
1568*e038c9c4Sjoerg // int dirfd(DIR *dirp);
1569*e038c9c4Sjoerg addToFunctionSummaryMap("dirfd",
1570*e038c9c4Sjoerg Signature(ArgTypes{DirPtrTy}, RetType{IntTy}),
1571*e038c9c4Sjoerg Summary(NoEvalCall)
1572*e038c9c4Sjoerg .Case(ReturnsFileDescriptor)
1573*e038c9c4Sjoerg .ArgConstraint(NotNull(ArgNo(0))));
1574*e038c9c4Sjoerg
1575*e038c9c4Sjoerg // unsigned int alarm(unsigned int seconds);
1576*e038c9c4Sjoerg addToFunctionSummaryMap(
1577*e038c9c4Sjoerg "alarm", Signature(ArgTypes{UnsignedIntTy}, RetType{UnsignedIntTy}),
1578*e038c9c4Sjoerg Summary(NoEvalCall)
1579*e038c9c4Sjoerg .ArgConstraint(
1580*e038c9c4Sjoerg ArgumentCondition(0, WithinRange, Range(0, UnsignedIntMax))));
1581*e038c9c4Sjoerg
1582*e038c9c4Sjoerg // int closedir(DIR *dir);
1583*e038c9c4Sjoerg addToFunctionSummaryMap("closedir",
1584*e038c9c4Sjoerg Signature(ArgTypes{DirPtrTy}, RetType{IntTy}),
1585*e038c9c4Sjoerg Summary(NoEvalCall)
1586*e038c9c4Sjoerg .Case(ReturnsZeroOrMinusOne)
1587*e038c9c4Sjoerg .ArgConstraint(NotNull(ArgNo(0))));
1588*e038c9c4Sjoerg
1589*e038c9c4Sjoerg // char *strdup(const char *s);
1590*e038c9c4Sjoerg addToFunctionSummaryMap(
1591*e038c9c4Sjoerg "strdup", Signature(ArgTypes{ConstCharPtrTy}, RetType{CharPtrTy}),
1592*e038c9c4Sjoerg Summary(NoEvalCall).ArgConstraint(NotNull(ArgNo(0))));
1593*e038c9c4Sjoerg
1594*e038c9c4Sjoerg // char *strndup(const char *s, size_t n);
1595*e038c9c4Sjoerg addToFunctionSummaryMap(
1596*e038c9c4Sjoerg "strndup",
1597*e038c9c4Sjoerg Signature(ArgTypes{ConstCharPtrTy, SizeTy}, RetType{CharPtrTy}),
1598*e038c9c4Sjoerg Summary(NoEvalCall)
1599*e038c9c4Sjoerg .ArgConstraint(NotNull(ArgNo(0)))
1600*e038c9c4Sjoerg .ArgConstraint(
1601*e038c9c4Sjoerg ArgumentCondition(1, WithinRange, Range(0, SizeMax))));
1602*e038c9c4Sjoerg
1603*e038c9c4Sjoerg // wchar_t *wcsdup(const wchar_t *s);
1604*e038c9c4Sjoerg addToFunctionSummaryMap(
1605*e038c9c4Sjoerg "wcsdup", Signature(ArgTypes{ConstWchar_tPtrTy}, RetType{Wchar_tPtrTy}),
1606*e038c9c4Sjoerg Summary(NoEvalCall).ArgConstraint(NotNull(ArgNo(0))));
1607*e038c9c4Sjoerg
1608*e038c9c4Sjoerg // int mkstemp(char *template);
1609*e038c9c4Sjoerg addToFunctionSummaryMap("mkstemp",
1610*e038c9c4Sjoerg Signature(ArgTypes{CharPtrTy}, RetType{IntTy}),
1611*e038c9c4Sjoerg Summary(NoEvalCall)
1612*e038c9c4Sjoerg .Case(ReturnsFileDescriptor)
1613*e038c9c4Sjoerg .ArgConstraint(NotNull(ArgNo(0))));
1614*e038c9c4Sjoerg
1615*e038c9c4Sjoerg // char *mkdtemp(char *template);
1616*e038c9c4Sjoerg addToFunctionSummaryMap(
1617*e038c9c4Sjoerg "mkdtemp", Signature(ArgTypes{CharPtrTy}, RetType{CharPtrTy}),
1618*e038c9c4Sjoerg Summary(NoEvalCall).ArgConstraint(NotNull(ArgNo(0))));
1619*e038c9c4Sjoerg
1620*e038c9c4Sjoerg // char *getcwd(char *buf, size_t size);
1621*e038c9c4Sjoerg addToFunctionSummaryMap(
1622*e038c9c4Sjoerg "getcwd", Signature(ArgTypes{CharPtrTy, SizeTy}, RetType{CharPtrTy}),
1623*e038c9c4Sjoerg Summary(NoEvalCall)
1624*e038c9c4Sjoerg .ArgConstraint(
1625*e038c9c4Sjoerg ArgumentCondition(1, WithinRange, Range(0, SizeMax))));
1626*e038c9c4Sjoerg
1627*e038c9c4Sjoerg // int mkdir(const char *pathname, mode_t mode);
1628*e038c9c4Sjoerg addToFunctionSummaryMap(
1629*e038c9c4Sjoerg "mkdir", Signature(ArgTypes{ConstCharPtrTy, Mode_tTy}, RetType{IntTy}),
1630*e038c9c4Sjoerg Summary(NoEvalCall)
1631*e038c9c4Sjoerg .Case(ReturnsZeroOrMinusOne)
1632*e038c9c4Sjoerg .ArgConstraint(NotNull(ArgNo(0))));
1633*e038c9c4Sjoerg
1634*e038c9c4Sjoerg // int mkdirat(int dirfd, const char *pathname, mode_t mode);
1635*e038c9c4Sjoerg addToFunctionSummaryMap(
1636*e038c9c4Sjoerg "mkdirat",
1637*e038c9c4Sjoerg Signature(ArgTypes{IntTy, ConstCharPtrTy, Mode_tTy}, RetType{IntTy}),
1638*e038c9c4Sjoerg Summary(NoEvalCall)
1639*e038c9c4Sjoerg .Case(ReturnsZeroOrMinusOne)
1640*e038c9c4Sjoerg .ArgConstraint(NotNull(ArgNo(1))));
1641*e038c9c4Sjoerg
1642*e038c9c4Sjoerg Optional<QualType> Dev_tTy = lookupTy("dev_t");
1643*e038c9c4Sjoerg
1644*e038c9c4Sjoerg // int mknod(const char *pathname, mode_t mode, dev_t dev);
1645*e038c9c4Sjoerg addToFunctionSummaryMap(
1646*e038c9c4Sjoerg "mknod",
1647*e038c9c4Sjoerg Signature(ArgTypes{ConstCharPtrTy, Mode_tTy, Dev_tTy}, RetType{IntTy}),
1648*e038c9c4Sjoerg Summary(NoEvalCall)
1649*e038c9c4Sjoerg .Case(ReturnsZeroOrMinusOne)
1650*e038c9c4Sjoerg .ArgConstraint(NotNull(ArgNo(0))));
1651*e038c9c4Sjoerg
1652*e038c9c4Sjoerg // int mknodat(int dirfd, const char *pathname, mode_t mode, dev_t dev);
1653*e038c9c4Sjoerg addToFunctionSummaryMap(
1654*e038c9c4Sjoerg "mknodat",
1655*e038c9c4Sjoerg Signature(ArgTypes{IntTy, ConstCharPtrTy, Mode_tTy, Dev_tTy},
1656*e038c9c4Sjoerg RetType{IntTy}),
1657*e038c9c4Sjoerg Summary(NoEvalCall)
1658*e038c9c4Sjoerg .Case(ReturnsZeroOrMinusOne)
1659*e038c9c4Sjoerg .ArgConstraint(NotNull(ArgNo(1))));
1660*e038c9c4Sjoerg
1661*e038c9c4Sjoerg // int chmod(const char *path, mode_t mode);
1662*e038c9c4Sjoerg addToFunctionSummaryMap(
1663*e038c9c4Sjoerg "chmod", Signature(ArgTypes{ConstCharPtrTy, Mode_tTy}, RetType{IntTy}),
1664*e038c9c4Sjoerg Summary(NoEvalCall)
1665*e038c9c4Sjoerg .Case(ReturnsZeroOrMinusOne)
1666*e038c9c4Sjoerg .ArgConstraint(NotNull(ArgNo(0))));
1667*e038c9c4Sjoerg
1668*e038c9c4Sjoerg // int fchmodat(int dirfd, const char *pathname, mode_t mode, int flags);
1669*e038c9c4Sjoerg addToFunctionSummaryMap(
1670*e038c9c4Sjoerg "fchmodat",
1671*e038c9c4Sjoerg Signature(ArgTypes{IntTy, ConstCharPtrTy, Mode_tTy, IntTy},
1672*e038c9c4Sjoerg RetType{IntTy}),
1673*e038c9c4Sjoerg Summary(NoEvalCall)
1674*e038c9c4Sjoerg .Case(ReturnsZeroOrMinusOne)
1675*e038c9c4Sjoerg .ArgConstraint(ArgumentCondition(0, WithinRange, Range(0, IntMax)))
1676*e038c9c4Sjoerg .ArgConstraint(NotNull(ArgNo(1))));
1677*e038c9c4Sjoerg
1678*e038c9c4Sjoerg // int fchmod(int fildes, mode_t mode);
1679*e038c9c4Sjoerg addToFunctionSummaryMap(
1680*e038c9c4Sjoerg "fchmod", Signature(ArgTypes{IntTy, Mode_tTy}, RetType{IntTy}),
1681*e038c9c4Sjoerg Summary(NoEvalCall)
1682*e038c9c4Sjoerg .Case(ReturnsZeroOrMinusOne)
1683*e038c9c4Sjoerg .ArgConstraint(
1684*e038c9c4Sjoerg ArgumentCondition(0, WithinRange, Range(0, IntMax))));
1685*e038c9c4Sjoerg
1686*e038c9c4Sjoerg Optional<QualType> Uid_tTy = lookupTy("uid_t");
1687*e038c9c4Sjoerg Optional<QualType> Gid_tTy = lookupTy("gid_t");
1688*e038c9c4Sjoerg
1689*e038c9c4Sjoerg // int fchownat(int dirfd, const char *pathname, uid_t owner, gid_t group,
1690*e038c9c4Sjoerg // int flags);
1691*e038c9c4Sjoerg addToFunctionSummaryMap(
1692*e038c9c4Sjoerg "fchownat",
1693*e038c9c4Sjoerg Signature(ArgTypes{IntTy, ConstCharPtrTy, Uid_tTy, Gid_tTy, IntTy},
1694*e038c9c4Sjoerg RetType{IntTy}),
1695*e038c9c4Sjoerg Summary(NoEvalCall)
1696*e038c9c4Sjoerg .Case(ReturnsZeroOrMinusOne)
1697*e038c9c4Sjoerg .ArgConstraint(ArgumentCondition(0, WithinRange, Range(0, IntMax)))
1698*e038c9c4Sjoerg .ArgConstraint(NotNull(ArgNo(1))));
1699*e038c9c4Sjoerg
1700*e038c9c4Sjoerg // int chown(const char *path, uid_t owner, gid_t group);
1701*e038c9c4Sjoerg addToFunctionSummaryMap(
1702*e038c9c4Sjoerg "chown",
1703*e038c9c4Sjoerg Signature(ArgTypes{ConstCharPtrTy, Uid_tTy, Gid_tTy}, RetType{IntTy}),
1704*e038c9c4Sjoerg Summary(NoEvalCall)
1705*e038c9c4Sjoerg .Case(ReturnsZeroOrMinusOne)
1706*e038c9c4Sjoerg .ArgConstraint(NotNull(ArgNo(0))));
1707*e038c9c4Sjoerg
1708*e038c9c4Sjoerg // int lchown(const char *path, uid_t owner, gid_t group);
1709*e038c9c4Sjoerg addToFunctionSummaryMap(
1710*e038c9c4Sjoerg "lchown",
1711*e038c9c4Sjoerg Signature(ArgTypes{ConstCharPtrTy, Uid_tTy, Gid_tTy}, RetType{IntTy}),
1712*e038c9c4Sjoerg Summary(NoEvalCall)
1713*e038c9c4Sjoerg .Case(ReturnsZeroOrMinusOne)
1714*e038c9c4Sjoerg .ArgConstraint(NotNull(ArgNo(0))));
1715*e038c9c4Sjoerg
1716*e038c9c4Sjoerg // int fchown(int fildes, uid_t owner, gid_t group);
1717*e038c9c4Sjoerg addToFunctionSummaryMap(
1718*e038c9c4Sjoerg "fchown", Signature(ArgTypes{IntTy, Uid_tTy, Gid_tTy}, RetType{IntTy}),
1719*e038c9c4Sjoerg Summary(NoEvalCall)
1720*e038c9c4Sjoerg .Case(ReturnsZeroOrMinusOne)
1721*e038c9c4Sjoerg .ArgConstraint(
1722*e038c9c4Sjoerg ArgumentCondition(0, WithinRange, Range(0, IntMax))));
1723*e038c9c4Sjoerg
1724*e038c9c4Sjoerg // int rmdir(const char *pathname);
1725*e038c9c4Sjoerg addToFunctionSummaryMap("rmdir",
1726*e038c9c4Sjoerg Signature(ArgTypes{ConstCharPtrTy}, RetType{IntTy}),
1727*e038c9c4Sjoerg Summary(NoEvalCall)
1728*e038c9c4Sjoerg .Case(ReturnsZeroOrMinusOne)
1729*e038c9c4Sjoerg .ArgConstraint(NotNull(ArgNo(0))));
1730*e038c9c4Sjoerg
1731*e038c9c4Sjoerg // int chdir(const char *path);
1732*e038c9c4Sjoerg addToFunctionSummaryMap("chdir",
1733*e038c9c4Sjoerg Signature(ArgTypes{ConstCharPtrTy}, RetType{IntTy}),
1734*e038c9c4Sjoerg Summary(NoEvalCall)
1735*e038c9c4Sjoerg .Case(ReturnsZeroOrMinusOne)
1736*e038c9c4Sjoerg .ArgConstraint(NotNull(ArgNo(0))));
1737*e038c9c4Sjoerg
1738*e038c9c4Sjoerg // int link(const char *oldpath, const char *newpath);
1739*e038c9c4Sjoerg addToFunctionSummaryMap(
1740*e038c9c4Sjoerg "link",
1741*e038c9c4Sjoerg Signature(ArgTypes{ConstCharPtrTy, ConstCharPtrTy}, RetType{IntTy}),
1742*e038c9c4Sjoerg Summary(NoEvalCall)
1743*e038c9c4Sjoerg .Case(ReturnsZeroOrMinusOne)
1744*e038c9c4Sjoerg .ArgConstraint(NotNull(ArgNo(0)))
1745*e038c9c4Sjoerg .ArgConstraint(NotNull(ArgNo(1))));
1746*e038c9c4Sjoerg
1747*e038c9c4Sjoerg // int linkat(int fd1, const char *path1, int fd2, const char *path2,
1748*e038c9c4Sjoerg // int flag);
1749*e038c9c4Sjoerg addToFunctionSummaryMap(
1750*e038c9c4Sjoerg "linkat",
1751*e038c9c4Sjoerg Signature(ArgTypes{IntTy, ConstCharPtrTy, IntTy, ConstCharPtrTy, IntTy},
1752*e038c9c4Sjoerg RetType{IntTy}),
1753*e038c9c4Sjoerg Summary(NoEvalCall)
1754*e038c9c4Sjoerg .Case(ReturnsZeroOrMinusOne)
1755*e038c9c4Sjoerg .ArgConstraint(ArgumentCondition(0, WithinRange, Range(0, IntMax)))
1756*e038c9c4Sjoerg .ArgConstraint(NotNull(ArgNo(1)))
1757*e038c9c4Sjoerg .ArgConstraint(ArgumentCondition(2, WithinRange, Range(0, IntMax)))
1758*e038c9c4Sjoerg .ArgConstraint(NotNull(ArgNo(3))));
1759*e038c9c4Sjoerg
1760*e038c9c4Sjoerg // int unlink(const char *pathname);
1761*e038c9c4Sjoerg addToFunctionSummaryMap("unlink",
1762*e038c9c4Sjoerg Signature(ArgTypes{ConstCharPtrTy}, RetType{IntTy}),
1763*e038c9c4Sjoerg Summary(NoEvalCall)
1764*e038c9c4Sjoerg .Case(ReturnsZeroOrMinusOne)
1765*e038c9c4Sjoerg .ArgConstraint(NotNull(ArgNo(0))));
1766*e038c9c4Sjoerg
1767*e038c9c4Sjoerg // int unlinkat(int fd, const char *path, int flag);
1768*e038c9c4Sjoerg addToFunctionSummaryMap(
1769*e038c9c4Sjoerg "unlinkat",
1770*e038c9c4Sjoerg Signature(ArgTypes{IntTy, ConstCharPtrTy, IntTy}, RetType{IntTy}),
1771*e038c9c4Sjoerg Summary(NoEvalCall)
1772*e038c9c4Sjoerg .Case(ReturnsZeroOrMinusOne)
1773*e038c9c4Sjoerg .ArgConstraint(ArgumentCondition(0, WithinRange, Range(0, IntMax)))
1774*e038c9c4Sjoerg .ArgConstraint(NotNull(ArgNo(1))));
1775*e038c9c4Sjoerg
1776*e038c9c4Sjoerg Optional<QualType> StructStatTy = lookupTy("stat");
1777*e038c9c4Sjoerg Optional<QualType> StructStatPtrTy = getPointerTy(StructStatTy);
1778*e038c9c4Sjoerg Optional<QualType> StructStatPtrRestrictTy = getRestrictTy(StructStatPtrTy);
1779*e038c9c4Sjoerg
1780*e038c9c4Sjoerg // int fstat(int fd, struct stat *statbuf);
1781*e038c9c4Sjoerg addToFunctionSummaryMap(
1782*e038c9c4Sjoerg "fstat", Signature(ArgTypes{IntTy, StructStatPtrTy}, RetType{IntTy}),
1783*e038c9c4Sjoerg Summary(NoEvalCall)
1784*e038c9c4Sjoerg .Case(ReturnsZeroOrMinusOne)
1785*e038c9c4Sjoerg .ArgConstraint(ArgumentCondition(0, WithinRange, Range(0, IntMax)))
1786*e038c9c4Sjoerg .ArgConstraint(NotNull(ArgNo(1))));
1787*e038c9c4Sjoerg
1788*e038c9c4Sjoerg // int stat(const char *restrict path, struct stat *restrict buf);
1789*e038c9c4Sjoerg addToFunctionSummaryMap(
1790*e038c9c4Sjoerg "stat",
1791*e038c9c4Sjoerg Signature(ArgTypes{ConstCharPtrRestrictTy, StructStatPtrRestrictTy},
1792*e038c9c4Sjoerg RetType{IntTy}),
1793*e038c9c4Sjoerg Summary(NoEvalCall)
1794*e038c9c4Sjoerg .Case(ReturnsZeroOrMinusOne)
1795*e038c9c4Sjoerg .ArgConstraint(NotNull(ArgNo(0)))
1796*e038c9c4Sjoerg .ArgConstraint(NotNull(ArgNo(1))));
1797*e038c9c4Sjoerg
1798*e038c9c4Sjoerg // int lstat(const char *restrict path, struct stat *restrict buf);
1799*e038c9c4Sjoerg addToFunctionSummaryMap(
1800*e038c9c4Sjoerg "lstat",
1801*e038c9c4Sjoerg Signature(ArgTypes{ConstCharPtrRestrictTy, StructStatPtrRestrictTy},
1802*e038c9c4Sjoerg RetType{IntTy}),
1803*e038c9c4Sjoerg Summary(NoEvalCall)
1804*e038c9c4Sjoerg .Case(ReturnsZeroOrMinusOne)
1805*e038c9c4Sjoerg .ArgConstraint(NotNull(ArgNo(0)))
1806*e038c9c4Sjoerg .ArgConstraint(NotNull(ArgNo(1))));
1807*e038c9c4Sjoerg
1808*e038c9c4Sjoerg // int fstatat(int fd, const char *restrict path,
1809*e038c9c4Sjoerg // struct stat *restrict buf, int flag);
1810*e038c9c4Sjoerg addToFunctionSummaryMap(
1811*e038c9c4Sjoerg "fstatat",
1812*e038c9c4Sjoerg Signature(ArgTypes{IntTy, ConstCharPtrRestrictTy,
1813*e038c9c4Sjoerg StructStatPtrRestrictTy, IntTy},
1814*e038c9c4Sjoerg RetType{IntTy}),
1815*e038c9c4Sjoerg Summary(NoEvalCall)
1816*e038c9c4Sjoerg .Case(ReturnsZeroOrMinusOne)
1817*e038c9c4Sjoerg .ArgConstraint(ArgumentCondition(0, WithinRange, Range(0, IntMax)))
1818*e038c9c4Sjoerg .ArgConstraint(NotNull(ArgNo(1)))
1819*e038c9c4Sjoerg .ArgConstraint(NotNull(ArgNo(2))));
1820*e038c9c4Sjoerg
1821*e038c9c4Sjoerg // DIR *opendir(const char *name);
1822*e038c9c4Sjoerg addToFunctionSummaryMap(
1823*e038c9c4Sjoerg "opendir", Signature(ArgTypes{ConstCharPtrTy}, RetType{DirPtrTy}),
1824*e038c9c4Sjoerg Summary(NoEvalCall).ArgConstraint(NotNull(ArgNo(0))));
1825*e038c9c4Sjoerg
1826*e038c9c4Sjoerg // DIR *fdopendir(int fd);
1827*e038c9c4Sjoerg addToFunctionSummaryMap("fdopendir",
1828*e038c9c4Sjoerg Signature(ArgTypes{IntTy}, RetType{DirPtrTy}),
1829*e038c9c4Sjoerg Summary(NoEvalCall)
1830*e038c9c4Sjoerg .ArgConstraint(ArgumentCondition(
1831*e038c9c4Sjoerg 0, WithinRange, Range(0, IntMax))));
1832*e038c9c4Sjoerg
1833*e038c9c4Sjoerg // int isatty(int fildes);
1834*e038c9c4Sjoerg addToFunctionSummaryMap(
1835*e038c9c4Sjoerg "isatty", Signature(ArgTypes{IntTy}, RetType{IntTy}),
1836*e038c9c4Sjoerg Summary(NoEvalCall)
1837*e038c9c4Sjoerg .Case({ReturnValueCondition(WithinRange, Range(0, 1))})
1838*e038c9c4Sjoerg .ArgConstraint(
1839*e038c9c4Sjoerg ArgumentCondition(0, WithinRange, Range(0, IntMax))));
1840*e038c9c4Sjoerg
1841*e038c9c4Sjoerg // FILE *popen(const char *command, const char *type);
1842*e038c9c4Sjoerg addToFunctionSummaryMap(
1843*e038c9c4Sjoerg "popen",
1844*e038c9c4Sjoerg Signature(ArgTypes{ConstCharPtrTy, ConstCharPtrTy}, RetType{FilePtrTy}),
1845*e038c9c4Sjoerg Summary(NoEvalCall)
1846*e038c9c4Sjoerg .ArgConstraint(NotNull(ArgNo(0)))
1847*e038c9c4Sjoerg .ArgConstraint(NotNull(ArgNo(1))));
1848*e038c9c4Sjoerg
1849*e038c9c4Sjoerg // int pclose(FILE *stream);
1850*e038c9c4Sjoerg addToFunctionSummaryMap(
1851*e038c9c4Sjoerg "pclose", Signature(ArgTypes{FilePtrTy}, RetType{IntTy}),
1852*e038c9c4Sjoerg Summary(NoEvalCall).ArgConstraint(NotNull(ArgNo(0))));
1853*e038c9c4Sjoerg
1854*e038c9c4Sjoerg // int close(int fildes);
1855*e038c9c4Sjoerg addToFunctionSummaryMap("close", Signature(ArgTypes{IntTy}, RetType{IntTy}),
1856*e038c9c4Sjoerg Summary(NoEvalCall)
1857*e038c9c4Sjoerg .Case(ReturnsZeroOrMinusOne)
1858*e038c9c4Sjoerg .ArgConstraint(ArgumentCondition(
1859*e038c9c4Sjoerg 0, WithinRange, Range(-1, IntMax))));
1860*e038c9c4Sjoerg
1861*e038c9c4Sjoerg // long fpathconf(int fildes, int name);
1862*e038c9c4Sjoerg addToFunctionSummaryMap("fpathconf",
1863*e038c9c4Sjoerg Signature(ArgTypes{IntTy, IntTy}, RetType{LongTy}),
1864*e038c9c4Sjoerg Summary(NoEvalCall)
1865*e038c9c4Sjoerg .ArgConstraint(ArgumentCondition(
1866*e038c9c4Sjoerg 0, WithinRange, Range(0, IntMax))));
1867*e038c9c4Sjoerg
1868*e038c9c4Sjoerg // long pathconf(const char *path, int name);
1869*e038c9c4Sjoerg addToFunctionSummaryMap(
1870*e038c9c4Sjoerg "pathconf", Signature(ArgTypes{ConstCharPtrTy, IntTy}, RetType{LongTy}),
1871*e038c9c4Sjoerg Summary(NoEvalCall).ArgConstraint(NotNull(ArgNo(0))));
1872*e038c9c4Sjoerg
1873*e038c9c4Sjoerg // FILE *fdopen(int fd, const char *mode);
1874*e038c9c4Sjoerg addToFunctionSummaryMap(
1875*e038c9c4Sjoerg "fdopen",
1876*e038c9c4Sjoerg Signature(ArgTypes{IntTy, ConstCharPtrTy}, RetType{FilePtrTy}),
1877*e038c9c4Sjoerg Summary(NoEvalCall)
1878*e038c9c4Sjoerg .ArgConstraint(ArgumentCondition(0, WithinRange, Range(0, IntMax)))
1879*e038c9c4Sjoerg .ArgConstraint(NotNull(ArgNo(1))));
1880*e038c9c4Sjoerg
1881*e038c9c4Sjoerg // void rewinddir(DIR *dir);
1882*e038c9c4Sjoerg addToFunctionSummaryMap(
1883*e038c9c4Sjoerg "rewinddir", Signature(ArgTypes{DirPtrTy}, RetType{VoidTy}),
1884*e038c9c4Sjoerg Summary(NoEvalCall).ArgConstraint(NotNull(ArgNo(0))));
1885*e038c9c4Sjoerg
1886*e038c9c4Sjoerg // void seekdir(DIR *dirp, long loc);
1887*e038c9c4Sjoerg addToFunctionSummaryMap(
1888*e038c9c4Sjoerg "seekdir", Signature(ArgTypes{DirPtrTy, LongTy}, RetType{VoidTy}),
1889*e038c9c4Sjoerg Summary(NoEvalCall).ArgConstraint(NotNull(ArgNo(0))));
1890*e038c9c4Sjoerg
1891*e038c9c4Sjoerg // int rand_r(unsigned int *seedp);
1892*e038c9c4Sjoerg addToFunctionSummaryMap(
1893*e038c9c4Sjoerg "rand_r", Signature(ArgTypes{UnsignedIntPtrTy}, RetType{IntTy}),
1894*e038c9c4Sjoerg Summary(NoEvalCall).ArgConstraint(NotNull(ArgNo(0))));
1895*e038c9c4Sjoerg
1896*e038c9c4Sjoerg // int fileno(FILE *stream);
1897*e038c9c4Sjoerg addToFunctionSummaryMap("fileno",
1898*e038c9c4Sjoerg Signature(ArgTypes{FilePtrTy}, RetType{IntTy}),
1899*e038c9c4Sjoerg Summary(NoEvalCall)
1900*e038c9c4Sjoerg .Case(ReturnsFileDescriptor)
1901*e038c9c4Sjoerg .ArgConstraint(NotNull(ArgNo(0))));
1902*e038c9c4Sjoerg
1903*e038c9c4Sjoerg // int fseeko(FILE *stream, off_t offset, int whence);
1904*e038c9c4Sjoerg addToFunctionSummaryMap(
1905*e038c9c4Sjoerg "fseeko",
1906*e038c9c4Sjoerg Signature(ArgTypes{FilePtrTy, Off_tTy, IntTy}, RetType{IntTy}),
1907*e038c9c4Sjoerg Summary(NoEvalCall)
1908*e038c9c4Sjoerg .Case(ReturnsZeroOrMinusOne)
1909*e038c9c4Sjoerg .ArgConstraint(NotNull(ArgNo(0))));
1910*e038c9c4Sjoerg
1911*e038c9c4Sjoerg // off_t ftello(FILE *stream);
1912*e038c9c4Sjoerg addToFunctionSummaryMap(
1913*e038c9c4Sjoerg "ftello", Signature(ArgTypes{FilePtrTy}, RetType{Off_tTy}),
1914*e038c9c4Sjoerg Summary(NoEvalCall).ArgConstraint(NotNull(ArgNo(0))));
1915*e038c9c4Sjoerg
1916*e038c9c4Sjoerg // void *mmap(void *addr, size_t length, int prot, int flags, int fd,
1917*e038c9c4Sjoerg // off_t offset);
1918*e038c9c4Sjoerg addToFunctionSummaryMap(
1919*e038c9c4Sjoerg "mmap",
1920*e038c9c4Sjoerg Signature(ArgTypes{VoidPtrTy, SizeTy, IntTy, IntTy, IntTy, Off_tTy},
1921*e038c9c4Sjoerg RetType{VoidPtrTy}),
1922*e038c9c4Sjoerg Summary(NoEvalCall)
1923*e038c9c4Sjoerg .ArgConstraint(ArgumentCondition(1, WithinRange, Range(1, SizeMax)))
1924*e038c9c4Sjoerg .ArgConstraint(
1925*e038c9c4Sjoerg ArgumentCondition(4, WithinRange, Range(-1, IntMax))));
1926*e038c9c4Sjoerg
1927*e038c9c4Sjoerg Optional<QualType> Off64_tTy = lookupTy("off64_t");
1928*e038c9c4Sjoerg // void *mmap64(void *addr, size_t length, int prot, int flags, int fd,
1929*e038c9c4Sjoerg // off64_t offset);
1930*e038c9c4Sjoerg addToFunctionSummaryMap(
1931*e038c9c4Sjoerg "mmap64",
1932*e038c9c4Sjoerg Signature(ArgTypes{VoidPtrTy, SizeTy, IntTy, IntTy, IntTy, Off64_tTy},
1933*e038c9c4Sjoerg RetType{VoidPtrTy}),
1934*e038c9c4Sjoerg Summary(NoEvalCall)
1935*e038c9c4Sjoerg .ArgConstraint(ArgumentCondition(1, WithinRange, Range(1, SizeMax)))
1936*e038c9c4Sjoerg .ArgConstraint(
1937*e038c9c4Sjoerg ArgumentCondition(4, WithinRange, Range(-1, IntMax))));
1938*e038c9c4Sjoerg
1939*e038c9c4Sjoerg // int pipe(int fildes[2]);
1940*e038c9c4Sjoerg addToFunctionSummaryMap("pipe",
1941*e038c9c4Sjoerg Signature(ArgTypes{IntPtrTy}, RetType{IntTy}),
1942*e038c9c4Sjoerg Summary(NoEvalCall)
1943*e038c9c4Sjoerg .Case(ReturnsZeroOrMinusOne)
1944*e038c9c4Sjoerg .ArgConstraint(NotNull(ArgNo(0))));
1945*e038c9c4Sjoerg
1946*e038c9c4Sjoerg // off_t lseek(int fildes, off_t offset, int whence);
1947*e038c9c4Sjoerg addToFunctionSummaryMap(
1948*e038c9c4Sjoerg "lseek", Signature(ArgTypes{IntTy, Off_tTy, IntTy}, RetType{Off_tTy}),
1949*e038c9c4Sjoerg Summary(NoEvalCall)
1950*e038c9c4Sjoerg .ArgConstraint(
1951*e038c9c4Sjoerg ArgumentCondition(0, WithinRange, Range(0, IntMax))));
1952*e038c9c4Sjoerg
1953*e038c9c4Sjoerg // ssize_t readlink(const char *restrict path, char *restrict buf,
1954*e038c9c4Sjoerg // size_t bufsize);
1955*e038c9c4Sjoerg addToFunctionSummaryMap(
1956*e038c9c4Sjoerg "readlink",
1957*e038c9c4Sjoerg Signature(ArgTypes{ConstCharPtrRestrictTy, CharPtrRestrictTy, SizeTy},
1958*e038c9c4Sjoerg RetType{Ssize_tTy}),
1959*e038c9c4Sjoerg Summary(NoEvalCall)
1960*e038c9c4Sjoerg .Case({ReturnValueCondition(LessThanOrEq, ArgNo(2)),
1961*e038c9c4Sjoerg ReturnValueCondition(WithinRange, Range(-1, Ssize_tMax))})
1962*e038c9c4Sjoerg .ArgConstraint(NotNull(ArgNo(0)))
1963*e038c9c4Sjoerg .ArgConstraint(NotNull(ArgNo(1)))
1964*e038c9c4Sjoerg .ArgConstraint(BufferSize(/*Buffer=*/ArgNo(1),
1965*e038c9c4Sjoerg /*BufSize=*/ArgNo(2)))
1966*e038c9c4Sjoerg .ArgConstraint(
1967*e038c9c4Sjoerg ArgumentCondition(2, WithinRange, Range(0, SizeMax))));
1968*e038c9c4Sjoerg
1969*e038c9c4Sjoerg // ssize_t readlinkat(int fd, const char *restrict path,
1970*e038c9c4Sjoerg // char *restrict buf, size_t bufsize);
1971*e038c9c4Sjoerg addToFunctionSummaryMap(
1972*e038c9c4Sjoerg "readlinkat",
1973*e038c9c4Sjoerg Signature(
1974*e038c9c4Sjoerg ArgTypes{IntTy, ConstCharPtrRestrictTy, CharPtrRestrictTy, SizeTy},
1975*e038c9c4Sjoerg RetType{Ssize_tTy}),
1976*e038c9c4Sjoerg Summary(NoEvalCall)
1977*e038c9c4Sjoerg .Case({ReturnValueCondition(LessThanOrEq, ArgNo(3)),
1978*e038c9c4Sjoerg ReturnValueCondition(WithinRange, Range(-1, Ssize_tMax))})
1979*e038c9c4Sjoerg .ArgConstraint(ArgumentCondition(0, WithinRange, Range(0, IntMax)))
1980*e038c9c4Sjoerg .ArgConstraint(NotNull(ArgNo(1)))
1981*e038c9c4Sjoerg .ArgConstraint(NotNull(ArgNo(2)))
1982*e038c9c4Sjoerg .ArgConstraint(BufferSize(/*Buffer=*/ArgNo(2),
1983*e038c9c4Sjoerg /*BufSize=*/ArgNo(3)))
1984*e038c9c4Sjoerg .ArgConstraint(
1985*e038c9c4Sjoerg ArgumentCondition(3, WithinRange, Range(0, SizeMax))));
1986*e038c9c4Sjoerg
1987*e038c9c4Sjoerg // int renameat(int olddirfd, const char *oldpath, int newdirfd, const char
1988*e038c9c4Sjoerg // *newpath);
1989*e038c9c4Sjoerg addToFunctionSummaryMap(
1990*e038c9c4Sjoerg "renameat",
1991*e038c9c4Sjoerg Signature(ArgTypes{IntTy, ConstCharPtrTy, IntTy, ConstCharPtrTy},
1992*e038c9c4Sjoerg RetType{IntTy}),
1993*e038c9c4Sjoerg Summary(NoEvalCall)
1994*e038c9c4Sjoerg .Case(ReturnsZeroOrMinusOne)
1995*e038c9c4Sjoerg .ArgConstraint(NotNull(ArgNo(1)))
1996*e038c9c4Sjoerg .ArgConstraint(NotNull(ArgNo(3))));
1997*e038c9c4Sjoerg
1998*e038c9c4Sjoerg // char *realpath(const char *restrict file_name,
1999*e038c9c4Sjoerg // char *restrict resolved_name);
2000*e038c9c4Sjoerg addToFunctionSummaryMap(
2001*e038c9c4Sjoerg "realpath",
2002*e038c9c4Sjoerg Signature(ArgTypes{ConstCharPtrRestrictTy, CharPtrRestrictTy},
2003*e038c9c4Sjoerg RetType{CharPtrTy}),
2004*e038c9c4Sjoerg Summary(NoEvalCall).ArgConstraint(NotNull(ArgNo(0))));
2005*e038c9c4Sjoerg
2006*e038c9c4Sjoerg QualType CharPtrConstPtr = getPointerTy(getConstTy(CharPtrTy));
2007*e038c9c4Sjoerg
2008*e038c9c4Sjoerg // int execv(const char *path, char *const argv[]);
2009*e038c9c4Sjoerg addToFunctionSummaryMap(
2010*e038c9c4Sjoerg "execv",
2011*e038c9c4Sjoerg Signature(ArgTypes{ConstCharPtrTy, CharPtrConstPtr}, RetType{IntTy}),
2012*e038c9c4Sjoerg Summary(NoEvalCall)
2013*e038c9c4Sjoerg .Case({ReturnValueCondition(WithinRange, SingleValue(-1))})
2014*e038c9c4Sjoerg .ArgConstraint(NotNull(ArgNo(0))));
2015*e038c9c4Sjoerg
2016*e038c9c4Sjoerg // int execvp(const char *file, char *const argv[]);
2017*e038c9c4Sjoerg addToFunctionSummaryMap(
2018*e038c9c4Sjoerg "execvp",
2019*e038c9c4Sjoerg Signature(ArgTypes{ConstCharPtrTy, CharPtrConstPtr}, RetType{IntTy}),
2020*e038c9c4Sjoerg Summary(NoEvalCall)
2021*e038c9c4Sjoerg .Case({ReturnValueCondition(WithinRange, SingleValue(-1))})
2022*e038c9c4Sjoerg .ArgConstraint(NotNull(ArgNo(0))));
2023*e038c9c4Sjoerg
2024*e038c9c4Sjoerg // int getopt(int argc, char * const argv[], const char *optstring);
2025*e038c9c4Sjoerg addToFunctionSummaryMap(
2026*e038c9c4Sjoerg "getopt",
2027*e038c9c4Sjoerg Signature(ArgTypes{IntTy, CharPtrConstPtr, ConstCharPtrTy},
2028*e038c9c4Sjoerg RetType{IntTy}),
2029*e038c9c4Sjoerg Summary(NoEvalCall)
2030*e038c9c4Sjoerg .Case({ReturnValueCondition(WithinRange, Range(-1, UCharRangeMax))})
2031*e038c9c4Sjoerg .ArgConstraint(ArgumentCondition(0, WithinRange, Range(0, IntMax)))
2032*e038c9c4Sjoerg .ArgConstraint(NotNull(ArgNo(1)))
2033*e038c9c4Sjoerg .ArgConstraint(NotNull(ArgNo(2))));
2034*e038c9c4Sjoerg
2035*e038c9c4Sjoerg Optional<QualType> StructSockaddrTy = lookupTy("sockaddr");
2036*e038c9c4Sjoerg Optional<QualType> StructSockaddrPtrTy = getPointerTy(StructSockaddrTy);
2037*e038c9c4Sjoerg Optional<QualType> ConstStructSockaddrPtrTy =
2038*e038c9c4Sjoerg getPointerTy(getConstTy(StructSockaddrTy));
2039*e038c9c4Sjoerg Optional<QualType> StructSockaddrPtrRestrictTy =
2040*e038c9c4Sjoerg getRestrictTy(StructSockaddrPtrTy);
2041*e038c9c4Sjoerg Optional<QualType> ConstStructSockaddrPtrRestrictTy =
2042*e038c9c4Sjoerg getRestrictTy(ConstStructSockaddrPtrTy);
2043*e038c9c4Sjoerg Optional<QualType> Socklen_tTy = lookupTy("socklen_t");
2044*e038c9c4Sjoerg Optional<QualType> Socklen_tPtrTy = getPointerTy(Socklen_tTy);
2045*e038c9c4Sjoerg Optional<QualType> Socklen_tPtrRestrictTy = getRestrictTy(Socklen_tPtrTy);
2046*e038c9c4Sjoerg Optional<RangeInt> Socklen_tMax = getMaxValue(Socklen_tTy);
2047*e038c9c4Sjoerg
2048*e038c9c4Sjoerg // In 'socket.h' of some libc implementations with C99, sockaddr parameter
2049*e038c9c4Sjoerg // is a transparent union of the underlying sockaddr_ family of pointers
2050*e038c9c4Sjoerg // instead of being a pointer to struct sockaddr. In these cases, the
2051*e038c9c4Sjoerg // standardized signature will not match, thus we try to match with another
2052*e038c9c4Sjoerg // signature that has the joker Irrelevant type. We also remove those
2053*e038c9c4Sjoerg // constraints which require pointer types for the sockaddr param.
2054*e038c9c4Sjoerg auto Accept =
2055*e038c9c4Sjoerg Summary(NoEvalCall)
2056*e038c9c4Sjoerg .Case(ReturnsFileDescriptor)
2057*e038c9c4Sjoerg .ArgConstraint(ArgumentCondition(0, WithinRange, Range(0, IntMax)));
2058*e038c9c4Sjoerg if (!addToFunctionSummaryMap(
2059*e038c9c4Sjoerg "accept",
2060*e038c9c4Sjoerg // int accept(int socket, struct sockaddr *restrict address,
2061*e038c9c4Sjoerg // socklen_t *restrict address_len);
2062*e038c9c4Sjoerg Signature(ArgTypes{IntTy, StructSockaddrPtrRestrictTy,
2063*e038c9c4Sjoerg Socklen_tPtrRestrictTy},
2064*e038c9c4Sjoerg RetType{IntTy}),
2065*e038c9c4Sjoerg Accept))
2066*e038c9c4Sjoerg addToFunctionSummaryMap(
2067*e038c9c4Sjoerg "accept",
2068*e038c9c4Sjoerg Signature(ArgTypes{IntTy, Irrelevant, Socklen_tPtrRestrictTy},
2069*e038c9c4Sjoerg RetType{IntTy}),
2070*e038c9c4Sjoerg Accept);
2071*e038c9c4Sjoerg
2072*e038c9c4Sjoerg // int bind(int socket, const struct sockaddr *address, socklen_t
2073*e038c9c4Sjoerg // address_len);
2074*e038c9c4Sjoerg if (!addToFunctionSummaryMap(
2075*e038c9c4Sjoerg "bind",
2076*e038c9c4Sjoerg Signature(ArgTypes{IntTy, ConstStructSockaddrPtrTy, Socklen_tTy},
2077*e038c9c4Sjoerg RetType{IntTy}),
2078*e038c9c4Sjoerg Summary(NoEvalCall)
2079*e038c9c4Sjoerg .Case(ReturnsZeroOrMinusOne)
2080*e038c9c4Sjoerg .ArgConstraint(
2081*e038c9c4Sjoerg ArgumentCondition(0, WithinRange, Range(0, IntMax)))
2082*e038c9c4Sjoerg .ArgConstraint(NotNull(ArgNo(1)))
2083*e038c9c4Sjoerg .ArgConstraint(
2084*e038c9c4Sjoerg BufferSize(/*Buffer=*/ArgNo(1), /*BufSize=*/ArgNo(2)))
2085*e038c9c4Sjoerg .ArgConstraint(
2086*e038c9c4Sjoerg ArgumentCondition(2, WithinRange, Range(0, Socklen_tMax)))))
2087*e038c9c4Sjoerg // Do not add constraints on sockaddr.
2088*e038c9c4Sjoerg addToFunctionSummaryMap(
2089*e038c9c4Sjoerg "bind",
2090*e038c9c4Sjoerg Signature(ArgTypes{IntTy, Irrelevant, Socklen_tTy}, RetType{IntTy}),
2091*e038c9c4Sjoerg Summary(NoEvalCall)
2092*e038c9c4Sjoerg .Case(ReturnsZeroOrMinusOne)
2093*e038c9c4Sjoerg .ArgConstraint(
2094*e038c9c4Sjoerg ArgumentCondition(0, WithinRange, Range(0, IntMax)))
2095*e038c9c4Sjoerg .ArgConstraint(
2096*e038c9c4Sjoerg ArgumentCondition(2, WithinRange, Range(0, Socklen_tMax))));
2097*e038c9c4Sjoerg
2098*e038c9c4Sjoerg // int getpeername(int socket, struct sockaddr *restrict address,
2099*e038c9c4Sjoerg // socklen_t *restrict address_len);
2100*e038c9c4Sjoerg if (!addToFunctionSummaryMap(
2101*e038c9c4Sjoerg "getpeername",
2102*e038c9c4Sjoerg Signature(ArgTypes{IntTy, StructSockaddrPtrRestrictTy,
2103*e038c9c4Sjoerg Socklen_tPtrRestrictTy},
2104*e038c9c4Sjoerg RetType{IntTy}),
2105*e038c9c4Sjoerg Summary(NoEvalCall)
2106*e038c9c4Sjoerg .Case(ReturnsZeroOrMinusOne)
2107*e038c9c4Sjoerg .ArgConstraint(
2108*e038c9c4Sjoerg ArgumentCondition(0, WithinRange, Range(0, IntMax)))
2109*e038c9c4Sjoerg .ArgConstraint(NotNull(ArgNo(1)))
2110*e038c9c4Sjoerg .ArgConstraint(NotNull(ArgNo(2)))))
2111*e038c9c4Sjoerg addToFunctionSummaryMap(
2112*e038c9c4Sjoerg "getpeername",
2113*e038c9c4Sjoerg Signature(ArgTypes{IntTy, Irrelevant, Socklen_tPtrRestrictTy},
2114*e038c9c4Sjoerg RetType{IntTy}),
2115*e038c9c4Sjoerg Summary(NoEvalCall)
2116*e038c9c4Sjoerg .Case(ReturnsZeroOrMinusOne)
2117*e038c9c4Sjoerg .ArgConstraint(
2118*e038c9c4Sjoerg ArgumentCondition(0, WithinRange, Range(0, IntMax))));
2119*e038c9c4Sjoerg
2120*e038c9c4Sjoerg // int getsockname(int socket, struct sockaddr *restrict address,
2121*e038c9c4Sjoerg // socklen_t *restrict address_len);
2122*e038c9c4Sjoerg if (!addToFunctionSummaryMap(
2123*e038c9c4Sjoerg "getsockname",
2124*e038c9c4Sjoerg Signature(ArgTypes{IntTy, StructSockaddrPtrRestrictTy,
2125*e038c9c4Sjoerg Socklen_tPtrRestrictTy},
2126*e038c9c4Sjoerg RetType{IntTy}),
2127*e038c9c4Sjoerg Summary(NoEvalCall)
2128*e038c9c4Sjoerg .Case(ReturnsZeroOrMinusOne)
2129*e038c9c4Sjoerg .ArgConstraint(
2130*e038c9c4Sjoerg ArgumentCondition(0, WithinRange, Range(0, IntMax)))
2131*e038c9c4Sjoerg .ArgConstraint(NotNull(ArgNo(1)))
2132*e038c9c4Sjoerg .ArgConstraint(NotNull(ArgNo(2)))))
2133*e038c9c4Sjoerg addToFunctionSummaryMap(
2134*e038c9c4Sjoerg "getsockname",
2135*e038c9c4Sjoerg Signature(ArgTypes{IntTy, Irrelevant, Socklen_tPtrRestrictTy},
2136*e038c9c4Sjoerg RetType{IntTy}),
2137*e038c9c4Sjoerg Summary(NoEvalCall)
2138*e038c9c4Sjoerg .Case(ReturnsZeroOrMinusOne)
2139*e038c9c4Sjoerg .ArgConstraint(
2140*e038c9c4Sjoerg ArgumentCondition(0, WithinRange, Range(0, IntMax))));
2141*e038c9c4Sjoerg
2142*e038c9c4Sjoerg // int connect(int socket, const struct sockaddr *address, socklen_t
2143*e038c9c4Sjoerg // address_len);
2144*e038c9c4Sjoerg if (!addToFunctionSummaryMap(
2145*e038c9c4Sjoerg "connect",
2146*e038c9c4Sjoerg Signature(ArgTypes{IntTy, ConstStructSockaddrPtrTy, Socklen_tTy},
2147*e038c9c4Sjoerg RetType{IntTy}),
2148*e038c9c4Sjoerg Summary(NoEvalCall)
2149*e038c9c4Sjoerg .Case(ReturnsZeroOrMinusOne)
2150*e038c9c4Sjoerg .ArgConstraint(
2151*e038c9c4Sjoerg ArgumentCondition(0, WithinRange, Range(0, IntMax)))
2152*e038c9c4Sjoerg .ArgConstraint(NotNull(ArgNo(1)))))
2153*e038c9c4Sjoerg addToFunctionSummaryMap(
2154*e038c9c4Sjoerg "connect",
2155*e038c9c4Sjoerg Signature(ArgTypes{IntTy, Irrelevant, Socklen_tTy}, RetType{IntTy}),
2156*e038c9c4Sjoerg Summary(NoEvalCall)
2157*e038c9c4Sjoerg .Case(ReturnsZeroOrMinusOne)
2158*e038c9c4Sjoerg .ArgConstraint(
2159*e038c9c4Sjoerg ArgumentCondition(0, WithinRange, Range(0, IntMax))));
2160*e038c9c4Sjoerg
2161*e038c9c4Sjoerg auto Recvfrom =
2162*e038c9c4Sjoerg Summary(NoEvalCall)
2163*e038c9c4Sjoerg .Case({ReturnValueCondition(LessThanOrEq, ArgNo(2)),
2164*e038c9c4Sjoerg ReturnValueCondition(WithinRange, Range(-1, Ssize_tMax))})
2165*e038c9c4Sjoerg .ArgConstraint(ArgumentCondition(0, WithinRange, Range(0, IntMax)))
2166*e038c9c4Sjoerg .ArgConstraint(BufferSize(/*Buffer=*/ArgNo(1),
2167*e038c9c4Sjoerg /*BufSize=*/ArgNo(2)));
2168*e038c9c4Sjoerg if (!addToFunctionSummaryMap(
2169*e038c9c4Sjoerg "recvfrom",
2170*e038c9c4Sjoerg // ssize_t recvfrom(int socket, void *restrict buffer,
2171*e038c9c4Sjoerg // size_t length,
2172*e038c9c4Sjoerg // int flags, struct sockaddr *restrict address,
2173*e038c9c4Sjoerg // socklen_t *restrict address_len);
2174*e038c9c4Sjoerg Signature(ArgTypes{IntTy, VoidPtrRestrictTy, SizeTy, IntTy,
2175*e038c9c4Sjoerg StructSockaddrPtrRestrictTy,
2176*e038c9c4Sjoerg Socklen_tPtrRestrictTy},
2177*e038c9c4Sjoerg RetType{Ssize_tTy}),
2178*e038c9c4Sjoerg Recvfrom))
2179*e038c9c4Sjoerg addToFunctionSummaryMap(
2180*e038c9c4Sjoerg "recvfrom",
2181*e038c9c4Sjoerg Signature(ArgTypes{IntTy, VoidPtrRestrictTy, SizeTy, IntTy,
2182*e038c9c4Sjoerg Irrelevant, Socklen_tPtrRestrictTy},
2183*e038c9c4Sjoerg RetType{Ssize_tTy}),
2184*e038c9c4Sjoerg Recvfrom);
2185*e038c9c4Sjoerg
2186*e038c9c4Sjoerg auto Sendto =
2187*e038c9c4Sjoerg Summary(NoEvalCall)
2188*e038c9c4Sjoerg .Case({ReturnValueCondition(LessThanOrEq, ArgNo(2)),
2189*e038c9c4Sjoerg ReturnValueCondition(WithinRange, Range(-1, Ssize_tMax))})
2190*e038c9c4Sjoerg .ArgConstraint(ArgumentCondition(0, WithinRange, Range(0, IntMax)))
2191*e038c9c4Sjoerg .ArgConstraint(BufferSize(/*Buffer=*/ArgNo(1),
2192*e038c9c4Sjoerg /*BufSize=*/ArgNo(2)));
2193*e038c9c4Sjoerg if (!addToFunctionSummaryMap(
2194*e038c9c4Sjoerg "sendto",
2195*e038c9c4Sjoerg // ssize_t sendto(int socket, const void *message, size_t length,
2196*e038c9c4Sjoerg // int flags, const struct sockaddr *dest_addr,
2197*e038c9c4Sjoerg // socklen_t dest_len);
2198*e038c9c4Sjoerg Signature(ArgTypes{IntTy, ConstVoidPtrTy, SizeTy, IntTy,
2199*e038c9c4Sjoerg ConstStructSockaddrPtrTy, Socklen_tTy},
2200*e038c9c4Sjoerg RetType{Ssize_tTy}),
2201*e038c9c4Sjoerg Sendto))
2202*e038c9c4Sjoerg addToFunctionSummaryMap(
2203*e038c9c4Sjoerg "sendto",
2204*e038c9c4Sjoerg Signature(ArgTypes{IntTy, ConstVoidPtrTy, SizeTy, IntTy, Irrelevant,
2205*e038c9c4Sjoerg Socklen_tTy},
2206*e038c9c4Sjoerg RetType{Ssize_tTy}),
2207*e038c9c4Sjoerg Sendto);
2208*e038c9c4Sjoerg
2209*e038c9c4Sjoerg // int listen(int sockfd, int backlog);
2210*e038c9c4Sjoerg addToFunctionSummaryMap("listen",
2211*e038c9c4Sjoerg Signature(ArgTypes{IntTy, IntTy}, RetType{IntTy}),
2212*e038c9c4Sjoerg Summary(NoEvalCall)
2213*e038c9c4Sjoerg .Case(ReturnsZeroOrMinusOne)
2214*e038c9c4Sjoerg .ArgConstraint(ArgumentCondition(
2215*e038c9c4Sjoerg 0, WithinRange, Range(0, IntMax))));
2216*e038c9c4Sjoerg
2217*e038c9c4Sjoerg // ssize_t recv(int sockfd, void *buf, size_t len, int flags);
2218*e038c9c4Sjoerg addToFunctionSummaryMap(
2219*e038c9c4Sjoerg "recv",
2220*e038c9c4Sjoerg Signature(ArgTypes{IntTy, VoidPtrTy, SizeTy, IntTy},
2221*e038c9c4Sjoerg RetType{Ssize_tTy}),
2222*e038c9c4Sjoerg Summary(NoEvalCall)
2223*e038c9c4Sjoerg .Case({ReturnValueCondition(LessThanOrEq, ArgNo(2)),
2224*e038c9c4Sjoerg ReturnValueCondition(WithinRange, Range(-1, Ssize_tMax))})
2225*e038c9c4Sjoerg .ArgConstraint(ArgumentCondition(0, WithinRange, Range(0, IntMax)))
2226*e038c9c4Sjoerg .ArgConstraint(BufferSize(/*Buffer=*/ArgNo(1),
2227*e038c9c4Sjoerg /*BufSize=*/ArgNo(2))));
2228*e038c9c4Sjoerg
2229*e038c9c4Sjoerg Optional<QualType> StructMsghdrTy = lookupTy("msghdr");
2230*e038c9c4Sjoerg Optional<QualType> StructMsghdrPtrTy = getPointerTy(StructMsghdrTy);
2231*e038c9c4Sjoerg Optional<QualType> ConstStructMsghdrPtrTy =
2232*e038c9c4Sjoerg getPointerTy(getConstTy(StructMsghdrTy));
2233*e038c9c4Sjoerg
2234*e038c9c4Sjoerg // ssize_t recvmsg(int sockfd, struct msghdr *msg, int flags);
2235*e038c9c4Sjoerg addToFunctionSummaryMap(
2236*e038c9c4Sjoerg "recvmsg",
2237*e038c9c4Sjoerg Signature(ArgTypes{IntTy, StructMsghdrPtrTy, IntTy},
2238*e038c9c4Sjoerg RetType{Ssize_tTy}),
2239*e038c9c4Sjoerg Summary(NoEvalCall)
2240*e038c9c4Sjoerg .Case({ReturnValueCondition(WithinRange, Range(-1, Ssize_tMax))})
2241*e038c9c4Sjoerg .ArgConstraint(
2242*e038c9c4Sjoerg ArgumentCondition(0, WithinRange, Range(0, IntMax))));
2243*e038c9c4Sjoerg
2244*e038c9c4Sjoerg // ssize_t sendmsg(int sockfd, const struct msghdr *msg, int flags);
2245*e038c9c4Sjoerg addToFunctionSummaryMap(
2246*e038c9c4Sjoerg "sendmsg",
2247*e038c9c4Sjoerg Signature(ArgTypes{IntTy, ConstStructMsghdrPtrTy, IntTy},
2248*e038c9c4Sjoerg RetType{Ssize_tTy}),
2249*e038c9c4Sjoerg Summary(NoEvalCall)
2250*e038c9c4Sjoerg .Case({ReturnValueCondition(WithinRange, Range(-1, Ssize_tMax))})
2251*e038c9c4Sjoerg .ArgConstraint(
2252*e038c9c4Sjoerg ArgumentCondition(0, WithinRange, Range(0, IntMax))));
2253*e038c9c4Sjoerg
2254*e038c9c4Sjoerg // int setsockopt(int socket, int level, int option_name,
2255*e038c9c4Sjoerg // const void *option_value, socklen_t option_len);
2256*e038c9c4Sjoerg addToFunctionSummaryMap(
2257*e038c9c4Sjoerg "setsockopt",
2258*e038c9c4Sjoerg Signature(ArgTypes{IntTy, IntTy, IntTy, ConstVoidPtrTy, Socklen_tTy},
2259*e038c9c4Sjoerg RetType{IntTy}),
2260*e038c9c4Sjoerg Summary(NoEvalCall)
2261*e038c9c4Sjoerg .Case(ReturnsZeroOrMinusOne)
2262*e038c9c4Sjoerg .ArgConstraint(NotNull(ArgNo(3)))
2263*e038c9c4Sjoerg .ArgConstraint(
2264*e038c9c4Sjoerg BufferSize(/*Buffer=*/ArgNo(3), /*BufSize=*/ArgNo(4)))
2265*e038c9c4Sjoerg .ArgConstraint(
2266*e038c9c4Sjoerg ArgumentCondition(4, WithinRange, Range(0, Socklen_tMax))));
2267*e038c9c4Sjoerg
2268*e038c9c4Sjoerg // int getsockopt(int socket, int level, int option_name,
2269*e038c9c4Sjoerg // void *restrict option_value,
2270*e038c9c4Sjoerg // socklen_t *restrict option_len);
2271*e038c9c4Sjoerg addToFunctionSummaryMap(
2272*e038c9c4Sjoerg "getsockopt",
2273*e038c9c4Sjoerg Signature(ArgTypes{IntTy, IntTy, IntTy, VoidPtrRestrictTy,
2274*e038c9c4Sjoerg Socklen_tPtrRestrictTy},
2275*e038c9c4Sjoerg RetType{IntTy}),
2276*e038c9c4Sjoerg Summary(NoEvalCall)
2277*e038c9c4Sjoerg .Case(ReturnsZeroOrMinusOne)
2278*e038c9c4Sjoerg .ArgConstraint(NotNull(ArgNo(3)))
2279*e038c9c4Sjoerg .ArgConstraint(NotNull(ArgNo(4))));
2280*e038c9c4Sjoerg
2281*e038c9c4Sjoerg // ssize_t send(int sockfd, const void *buf, size_t len, int flags);
2282*e038c9c4Sjoerg addToFunctionSummaryMap(
2283*e038c9c4Sjoerg "send",
2284*e038c9c4Sjoerg Signature(ArgTypes{IntTy, ConstVoidPtrTy, SizeTy, IntTy},
2285*e038c9c4Sjoerg RetType{Ssize_tTy}),
2286*e038c9c4Sjoerg Summary(NoEvalCall)
2287*e038c9c4Sjoerg .Case({ReturnValueCondition(LessThanOrEq, ArgNo(2)),
2288*e038c9c4Sjoerg ReturnValueCondition(WithinRange, Range(-1, Ssize_tMax))})
2289*e038c9c4Sjoerg .ArgConstraint(ArgumentCondition(0, WithinRange, Range(0, IntMax)))
2290*e038c9c4Sjoerg .ArgConstraint(BufferSize(/*Buffer=*/ArgNo(1),
2291*e038c9c4Sjoerg /*BufSize=*/ArgNo(2))));
2292*e038c9c4Sjoerg
2293*e038c9c4Sjoerg // int socketpair(int domain, int type, int protocol, int sv[2]);
2294*e038c9c4Sjoerg addToFunctionSummaryMap(
2295*e038c9c4Sjoerg "socketpair",
2296*e038c9c4Sjoerg Signature(ArgTypes{IntTy, IntTy, IntTy, IntPtrTy}, RetType{IntTy}),
2297*e038c9c4Sjoerg Summary(NoEvalCall)
2298*e038c9c4Sjoerg .Case(ReturnsZeroOrMinusOne)
2299*e038c9c4Sjoerg .ArgConstraint(NotNull(ArgNo(3))));
2300*e038c9c4Sjoerg
2301*e038c9c4Sjoerg // int getnameinfo(const struct sockaddr *restrict sa, socklen_t salen,
2302*e038c9c4Sjoerg // char *restrict node, socklen_t nodelen,
2303*e038c9c4Sjoerg // char *restrict service,
2304*e038c9c4Sjoerg // socklen_t servicelen, int flags);
2305*e038c9c4Sjoerg //
2306*e038c9c4Sjoerg // This is defined in netdb.h. And contrary to 'socket.h', the sockaddr
2307*e038c9c4Sjoerg // parameter is never handled as a transparent union in netdb.h
2308*e038c9c4Sjoerg addToFunctionSummaryMap(
2309*e038c9c4Sjoerg "getnameinfo",
2310*e038c9c4Sjoerg Signature(ArgTypes{ConstStructSockaddrPtrRestrictTy, Socklen_tTy,
2311*e038c9c4Sjoerg CharPtrRestrictTy, Socklen_tTy, CharPtrRestrictTy,
2312*e038c9c4Sjoerg Socklen_tTy, IntTy},
2313*e038c9c4Sjoerg RetType{IntTy}),
2314*e038c9c4Sjoerg Summary(NoEvalCall)
2315*e038c9c4Sjoerg .ArgConstraint(
2316*e038c9c4Sjoerg BufferSize(/*Buffer=*/ArgNo(0), /*BufSize=*/ArgNo(1)))
2317*e038c9c4Sjoerg .ArgConstraint(
2318*e038c9c4Sjoerg ArgumentCondition(1, WithinRange, Range(0, Socklen_tMax)))
2319*e038c9c4Sjoerg .ArgConstraint(
2320*e038c9c4Sjoerg BufferSize(/*Buffer=*/ArgNo(2), /*BufSize=*/ArgNo(3)))
2321*e038c9c4Sjoerg .ArgConstraint(
2322*e038c9c4Sjoerg ArgumentCondition(3, WithinRange, Range(0, Socklen_tMax)))
2323*e038c9c4Sjoerg .ArgConstraint(
2324*e038c9c4Sjoerg BufferSize(/*Buffer=*/ArgNo(4), /*BufSize=*/ArgNo(5)))
2325*e038c9c4Sjoerg .ArgConstraint(
2326*e038c9c4Sjoerg ArgumentCondition(5, WithinRange, Range(0, Socklen_tMax))));
2327*e038c9c4Sjoerg
2328*e038c9c4Sjoerg Optional<QualType> StructUtimbufTy = lookupTy("utimbuf");
2329*e038c9c4Sjoerg Optional<QualType> StructUtimbufPtrTy = getPointerTy(StructUtimbufTy);
2330*e038c9c4Sjoerg
2331*e038c9c4Sjoerg // int utime(const char *filename, struct utimbuf *buf);
2332*e038c9c4Sjoerg addToFunctionSummaryMap(
2333*e038c9c4Sjoerg "utime",
2334*e038c9c4Sjoerg Signature(ArgTypes{ConstCharPtrTy, StructUtimbufPtrTy}, RetType{IntTy}),
2335*e038c9c4Sjoerg Summary(NoEvalCall)
2336*e038c9c4Sjoerg .Case(ReturnsZeroOrMinusOne)
2337*e038c9c4Sjoerg .ArgConstraint(NotNull(ArgNo(0))));
2338*e038c9c4Sjoerg
2339*e038c9c4Sjoerg Optional<QualType> StructTimespecTy = lookupTy("timespec");
2340*e038c9c4Sjoerg Optional<QualType> StructTimespecPtrTy = getPointerTy(StructTimespecTy);
2341*e038c9c4Sjoerg Optional<QualType> ConstStructTimespecPtrTy =
2342*e038c9c4Sjoerg getPointerTy(getConstTy(StructTimespecTy));
2343*e038c9c4Sjoerg
2344*e038c9c4Sjoerg // int futimens(int fd, const struct timespec times[2]);
2345*e038c9c4Sjoerg addToFunctionSummaryMap(
2346*e038c9c4Sjoerg "futimens",
2347*e038c9c4Sjoerg Signature(ArgTypes{IntTy, ConstStructTimespecPtrTy}, RetType{IntTy}),
2348*e038c9c4Sjoerg Summary(NoEvalCall)
2349*e038c9c4Sjoerg .Case(ReturnsZeroOrMinusOne)
2350*e038c9c4Sjoerg .ArgConstraint(
2351*e038c9c4Sjoerg ArgumentCondition(0, WithinRange, Range(0, IntMax))));
2352*e038c9c4Sjoerg
2353*e038c9c4Sjoerg // int utimensat(int dirfd, const char *pathname,
2354*e038c9c4Sjoerg // const struct timespec times[2], int flags);
2355*e038c9c4Sjoerg addToFunctionSummaryMap("utimensat",
2356*e038c9c4Sjoerg Signature(ArgTypes{IntTy, ConstCharPtrTy,
2357*e038c9c4Sjoerg ConstStructTimespecPtrTy, IntTy},
2358*e038c9c4Sjoerg RetType{IntTy}),
2359*e038c9c4Sjoerg Summary(NoEvalCall)
2360*e038c9c4Sjoerg .Case(ReturnsZeroOrMinusOne)
2361*e038c9c4Sjoerg .ArgConstraint(NotNull(ArgNo(1))));
2362*e038c9c4Sjoerg
2363*e038c9c4Sjoerg Optional<QualType> StructTimevalTy = lookupTy("timeval");
2364*e038c9c4Sjoerg Optional<QualType> ConstStructTimevalPtrTy =
2365*e038c9c4Sjoerg getPointerTy(getConstTy(StructTimevalTy));
2366*e038c9c4Sjoerg
2367*e038c9c4Sjoerg // int utimes(const char *filename, const struct timeval times[2]);
2368*e038c9c4Sjoerg addToFunctionSummaryMap(
2369*e038c9c4Sjoerg "utimes",
2370*e038c9c4Sjoerg Signature(ArgTypes{ConstCharPtrTy, ConstStructTimevalPtrTy},
2371*e038c9c4Sjoerg RetType{IntTy}),
2372*e038c9c4Sjoerg Summary(NoEvalCall)
2373*e038c9c4Sjoerg .Case(ReturnsZeroOrMinusOne)
2374*e038c9c4Sjoerg .ArgConstraint(NotNull(ArgNo(0))));
2375*e038c9c4Sjoerg
2376*e038c9c4Sjoerg // int nanosleep(const struct timespec *rqtp, struct timespec *rmtp);
2377*e038c9c4Sjoerg addToFunctionSummaryMap(
2378*e038c9c4Sjoerg "nanosleep",
2379*e038c9c4Sjoerg Signature(ArgTypes{ConstStructTimespecPtrTy, StructTimespecPtrTy},
2380*e038c9c4Sjoerg RetType{IntTy}),
2381*e038c9c4Sjoerg Summary(NoEvalCall)
2382*e038c9c4Sjoerg .Case(ReturnsZeroOrMinusOne)
2383*e038c9c4Sjoerg .ArgConstraint(NotNull(ArgNo(0))));
2384*e038c9c4Sjoerg
2385*e038c9c4Sjoerg Optional<QualType> Time_tTy = lookupTy("time_t");
2386*e038c9c4Sjoerg Optional<QualType> ConstTime_tPtrTy = getPointerTy(getConstTy(Time_tTy));
2387*e038c9c4Sjoerg Optional<QualType> ConstTime_tPtrRestrictTy =
2388*e038c9c4Sjoerg getRestrictTy(ConstTime_tPtrTy);
2389*e038c9c4Sjoerg
2390*e038c9c4Sjoerg Optional<QualType> StructTmTy = lookupTy("tm");
2391*e038c9c4Sjoerg Optional<QualType> StructTmPtrTy = getPointerTy(StructTmTy);
2392*e038c9c4Sjoerg Optional<QualType> StructTmPtrRestrictTy = getRestrictTy(StructTmPtrTy);
2393*e038c9c4Sjoerg Optional<QualType> ConstStructTmPtrTy =
2394*e038c9c4Sjoerg getPointerTy(getConstTy(StructTmTy));
2395*e038c9c4Sjoerg Optional<QualType> ConstStructTmPtrRestrictTy =
2396*e038c9c4Sjoerg getRestrictTy(ConstStructTmPtrTy);
2397*e038c9c4Sjoerg
2398*e038c9c4Sjoerg // struct tm * localtime(const time_t *tp);
2399*e038c9c4Sjoerg addToFunctionSummaryMap(
2400*e038c9c4Sjoerg "localtime",
2401*e038c9c4Sjoerg Signature(ArgTypes{ConstTime_tPtrTy}, RetType{StructTmPtrTy}),
2402*e038c9c4Sjoerg Summary(NoEvalCall).ArgConstraint(NotNull(ArgNo(0))));
2403*e038c9c4Sjoerg
2404*e038c9c4Sjoerg // struct tm *localtime_r(const time_t *restrict timer,
2405*e038c9c4Sjoerg // struct tm *restrict result);
2406*e038c9c4Sjoerg addToFunctionSummaryMap(
2407*e038c9c4Sjoerg "localtime_r",
2408*e038c9c4Sjoerg Signature(ArgTypes{ConstTime_tPtrRestrictTy, StructTmPtrRestrictTy},
2409*e038c9c4Sjoerg RetType{StructTmPtrTy}),
2410*e038c9c4Sjoerg Summary(NoEvalCall)
2411*e038c9c4Sjoerg .ArgConstraint(NotNull(ArgNo(0)))
2412*e038c9c4Sjoerg .ArgConstraint(NotNull(ArgNo(1))));
2413*e038c9c4Sjoerg
2414*e038c9c4Sjoerg // char *asctime_r(const struct tm *restrict tm, char *restrict buf);
2415*e038c9c4Sjoerg addToFunctionSummaryMap(
2416*e038c9c4Sjoerg "asctime_r",
2417*e038c9c4Sjoerg Signature(ArgTypes{ConstStructTmPtrRestrictTy, CharPtrRestrictTy},
2418*e038c9c4Sjoerg RetType{CharPtrTy}),
2419*e038c9c4Sjoerg Summary(NoEvalCall)
2420*e038c9c4Sjoerg .ArgConstraint(NotNull(ArgNo(0)))
2421*e038c9c4Sjoerg .ArgConstraint(NotNull(ArgNo(1)))
2422*e038c9c4Sjoerg .ArgConstraint(BufferSize(/*Buffer=*/ArgNo(1),
2423*e038c9c4Sjoerg /*MinBufSize=*/BVF.getValue(26, IntTy))));
2424*e038c9c4Sjoerg
2425*e038c9c4Sjoerg // char *ctime_r(const time_t *timep, char *buf);
2426*e038c9c4Sjoerg addToFunctionSummaryMap(
2427*e038c9c4Sjoerg "ctime_r",
2428*e038c9c4Sjoerg Signature(ArgTypes{ConstTime_tPtrTy, CharPtrTy}, RetType{CharPtrTy}),
2429*e038c9c4Sjoerg Summary(NoEvalCall)
2430*e038c9c4Sjoerg .ArgConstraint(NotNull(ArgNo(0)))
2431*e038c9c4Sjoerg .ArgConstraint(NotNull(ArgNo(1)))
2432*e038c9c4Sjoerg .ArgConstraint(BufferSize(
2433*e038c9c4Sjoerg /*Buffer=*/ArgNo(1),
2434*e038c9c4Sjoerg /*MinBufSize=*/BVF.getValue(26, IntTy))));
2435*e038c9c4Sjoerg
2436*e038c9c4Sjoerg // struct tm *gmtime_r(const time_t *restrict timer,
2437*e038c9c4Sjoerg // struct tm *restrict result);
2438*e038c9c4Sjoerg addToFunctionSummaryMap(
2439*e038c9c4Sjoerg "gmtime_r",
2440*e038c9c4Sjoerg Signature(ArgTypes{ConstTime_tPtrRestrictTy, StructTmPtrRestrictTy},
2441*e038c9c4Sjoerg RetType{StructTmPtrTy}),
2442*e038c9c4Sjoerg Summary(NoEvalCall)
2443*e038c9c4Sjoerg .ArgConstraint(NotNull(ArgNo(0)))
2444*e038c9c4Sjoerg .ArgConstraint(NotNull(ArgNo(1))));
2445*e038c9c4Sjoerg
2446*e038c9c4Sjoerg // struct tm * gmtime(const time_t *tp);
2447*e038c9c4Sjoerg addToFunctionSummaryMap(
2448*e038c9c4Sjoerg "gmtime", Signature(ArgTypes{ConstTime_tPtrTy}, RetType{StructTmPtrTy}),
2449*e038c9c4Sjoerg Summary(NoEvalCall).ArgConstraint(NotNull(ArgNo(0))));
2450*e038c9c4Sjoerg
2451*e038c9c4Sjoerg Optional<QualType> Clockid_tTy = lookupTy("clockid_t");
2452*e038c9c4Sjoerg
2453*e038c9c4Sjoerg // int clock_gettime(clockid_t clock_id, struct timespec *tp);
2454*e038c9c4Sjoerg addToFunctionSummaryMap(
2455*e038c9c4Sjoerg "clock_gettime",
2456*e038c9c4Sjoerg Signature(ArgTypes{Clockid_tTy, StructTimespecPtrTy}, RetType{IntTy}),
2457*e038c9c4Sjoerg Summary(NoEvalCall)
2458*e038c9c4Sjoerg .Case(ReturnsZeroOrMinusOne)
2459*e038c9c4Sjoerg .ArgConstraint(NotNull(ArgNo(1))));
2460*e038c9c4Sjoerg
2461*e038c9c4Sjoerg Optional<QualType> StructItimervalTy = lookupTy("itimerval");
2462*e038c9c4Sjoerg Optional<QualType> StructItimervalPtrTy = getPointerTy(StructItimervalTy);
2463*e038c9c4Sjoerg
2464*e038c9c4Sjoerg // int getitimer(int which, struct itimerval *curr_value);
2465*e038c9c4Sjoerg addToFunctionSummaryMap(
2466*e038c9c4Sjoerg "getitimer",
2467*e038c9c4Sjoerg Signature(ArgTypes{IntTy, StructItimervalPtrTy}, RetType{IntTy}),
2468*e038c9c4Sjoerg Summary(NoEvalCall)
2469*e038c9c4Sjoerg .Case(ReturnsZeroOrMinusOne)
2470*e038c9c4Sjoerg .ArgConstraint(NotNull(ArgNo(1))));
2471*e038c9c4Sjoerg
2472*e038c9c4Sjoerg Optional<QualType> Pthread_cond_tTy = lookupTy("pthread_cond_t");
2473*e038c9c4Sjoerg Optional<QualType> Pthread_cond_tPtrTy = getPointerTy(Pthread_cond_tTy);
2474*e038c9c4Sjoerg Optional<QualType> Pthread_tTy = lookupTy("pthread_t");
2475*e038c9c4Sjoerg Optional<QualType> Pthread_tPtrTy = getPointerTy(Pthread_tTy);
2476*e038c9c4Sjoerg Optional<QualType> Pthread_tPtrRestrictTy = getRestrictTy(Pthread_tPtrTy);
2477*e038c9c4Sjoerg Optional<QualType> Pthread_mutex_tTy = lookupTy("pthread_mutex_t");
2478*e038c9c4Sjoerg Optional<QualType> Pthread_mutex_tPtrTy = getPointerTy(Pthread_mutex_tTy);
2479*e038c9c4Sjoerg Optional<QualType> Pthread_mutex_tPtrRestrictTy =
2480*e038c9c4Sjoerg getRestrictTy(Pthread_mutex_tPtrTy);
2481*e038c9c4Sjoerg Optional<QualType> Pthread_attr_tTy = lookupTy("pthread_attr_t");
2482*e038c9c4Sjoerg Optional<QualType> Pthread_attr_tPtrTy = getPointerTy(Pthread_attr_tTy);
2483*e038c9c4Sjoerg Optional<QualType> ConstPthread_attr_tPtrTy =
2484*e038c9c4Sjoerg getPointerTy(getConstTy(Pthread_attr_tTy));
2485*e038c9c4Sjoerg Optional<QualType> ConstPthread_attr_tPtrRestrictTy =
2486*e038c9c4Sjoerg getRestrictTy(ConstPthread_attr_tPtrTy);
2487*e038c9c4Sjoerg Optional<QualType> Pthread_mutexattr_tTy = lookupTy("pthread_mutexattr_t");
2488*e038c9c4Sjoerg Optional<QualType> ConstPthread_mutexattr_tPtrTy =
2489*e038c9c4Sjoerg getPointerTy(getConstTy(Pthread_mutexattr_tTy));
2490*e038c9c4Sjoerg Optional<QualType> ConstPthread_mutexattr_tPtrRestrictTy =
2491*e038c9c4Sjoerg getRestrictTy(ConstPthread_mutexattr_tPtrTy);
2492*e038c9c4Sjoerg
2493*e038c9c4Sjoerg QualType PthreadStartRoutineTy = getPointerTy(
2494*e038c9c4Sjoerg ACtx.getFunctionType(/*ResultTy=*/VoidPtrTy, /*Args=*/VoidPtrTy,
2495*e038c9c4Sjoerg FunctionProtoType::ExtProtoInfo()));
2496*e038c9c4Sjoerg
2497*e038c9c4Sjoerg // int pthread_cond_signal(pthread_cond_t *cond);
2498*e038c9c4Sjoerg // int pthread_cond_broadcast(pthread_cond_t *cond);
2499*e038c9c4Sjoerg addToFunctionSummaryMap(
2500*e038c9c4Sjoerg {"pthread_cond_signal", "pthread_cond_broadcast"},
2501*e038c9c4Sjoerg Signature(ArgTypes{Pthread_cond_tPtrTy}, RetType{IntTy}),
2502*e038c9c4Sjoerg Summary(NoEvalCall).ArgConstraint(NotNull(ArgNo(0))));
2503*e038c9c4Sjoerg
2504*e038c9c4Sjoerg // int pthread_create(pthread_t *restrict thread,
2505*e038c9c4Sjoerg // const pthread_attr_t *restrict attr,
2506*e038c9c4Sjoerg // void *(*start_routine)(void*), void *restrict arg);
2507*e038c9c4Sjoerg addToFunctionSummaryMap(
2508*e038c9c4Sjoerg "pthread_create",
2509*e038c9c4Sjoerg Signature(ArgTypes{Pthread_tPtrRestrictTy,
2510*e038c9c4Sjoerg ConstPthread_attr_tPtrRestrictTy,
2511*e038c9c4Sjoerg PthreadStartRoutineTy, VoidPtrRestrictTy},
2512*e038c9c4Sjoerg RetType{IntTy}),
2513*e038c9c4Sjoerg Summary(NoEvalCall)
2514*e038c9c4Sjoerg .ArgConstraint(NotNull(ArgNo(0)))
2515*e038c9c4Sjoerg .ArgConstraint(NotNull(ArgNo(2))));
2516*e038c9c4Sjoerg
2517*e038c9c4Sjoerg // int pthread_attr_destroy(pthread_attr_t *attr);
2518*e038c9c4Sjoerg // int pthread_attr_init(pthread_attr_t *attr);
2519*e038c9c4Sjoerg addToFunctionSummaryMap(
2520*e038c9c4Sjoerg {"pthread_attr_destroy", "pthread_attr_init"},
2521*e038c9c4Sjoerg Signature(ArgTypes{Pthread_attr_tPtrTy}, RetType{IntTy}),
2522*e038c9c4Sjoerg Summary(NoEvalCall).ArgConstraint(NotNull(ArgNo(0))));
2523*e038c9c4Sjoerg
2524*e038c9c4Sjoerg // int pthread_attr_getstacksize(const pthread_attr_t *restrict attr,
2525*e038c9c4Sjoerg // size_t *restrict stacksize);
2526*e038c9c4Sjoerg // int pthread_attr_getguardsize(const pthread_attr_t *restrict attr,
2527*e038c9c4Sjoerg // size_t *restrict guardsize);
2528*e038c9c4Sjoerg addToFunctionSummaryMap(
2529*e038c9c4Sjoerg {"pthread_attr_getstacksize", "pthread_attr_getguardsize"},
2530*e038c9c4Sjoerg Signature(ArgTypes{ConstPthread_attr_tPtrRestrictTy, SizePtrRestrictTy},
2531*e038c9c4Sjoerg RetType{IntTy}),
2532*e038c9c4Sjoerg Summary(NoEvalCall)
2533*e038c9c4Sjoerg .ArgConstraint(NotNull(ArgNo(0)))
2534*e038c9c4Sjoerg .ArgConstraint(NotNull(ArgNo(1))));
2535*e038c9c4Sjoerg
2536*e038c9c4Sjoerg // int pthread_attr_setstacksize(pthread_attr_t *attr, size_t stacksize);
2537*e038c9c4Sjoerg // int pthread_attr_setguardsize(pthread_attr_t *attr, size_t guardsize);
2538*e038c9c4Sjoerg addToFunctionSummaryMap(
2539*e038c9c4Sjoerg {"pthread_attr_setstacksize", "pthread_attr_setguardsize"},
2540*e038c9c4Sjoerg Signature(ArgTypes{Pthread_attr_tPtrTy, SizeTy}, RetType{IntTy}),
2541*e038c9c4Sjoerg Summary(NoEvalCall)
2542*e038c9c4Sjoerg .ArgConstraint(NotNull(ArgNo(0)))
2543*e038c9c4Sjoerg .ArgConstraint(
2544*e038c9c4Sjoerg ArgumentCondition(1, WithinRange, Range(0, SizeMax))));
2545*e038c9c4Sjoerg
2546*e038c9c4Sjoerg // int pthread_mutex_init(pthread_mutex_t *restrict mutex, const
2547*e038c9c4Sjoerg // pthread_mutexattr_t *restrict attr);
2548*e038c9c4Sjoerg addToFunctionSummaryMap(
2549*e038c9c4Sjoerg "pthread_mutex_init",
2550*e038c9c4Sjoerg Signature(ArgTypes{Pthread_mutex_tPtrRestrictTy,
2551*e038c9c4Sjoerg ConstPthread_mutexattr_tPtrRestrictTy},
2552*e038c9c4Sjoerg RetType{IntTy}),
2553*e038c9c4Sjoerg Summary(NoEvalCall).ArgConstraint(NotNull(ArgNo(0))));
2554*e038c9c4Sjoerg
2555*e038c9c4Sjoerg // int pthread_mutex_destroy(pthread_mutex_t *mutex);
2556*e038c9c4Sjoerg // int pthread_mutex_lock(pthread_mutex_t *mutex);
2557*e038c9c4Sjoerg // int pthread_mutex_trylock(pthread_mutex_t *mutex);
2558*e038c9c4Sjoerg // int pthread_mutex_unlock(pthread_mutex_t *mutex);
2559*e038c9c4Sjoerg addToFunctionSummaryMap(
2560*e038c9c4Sjoerg {"pthread_mutex_destroy", "pthread_mutex_lock", "pthread_mutex_trylock",
2561*e038c9c4Sjoerg "pthread_mutex_unlock"},
2562*e038c9c4Sjoerg Signature(ArgTypes{Pthread_mutex_tPtrTy}, RetType{IntTy}),
2563*e038c9c4Sjoerg Summary(NoEvalCall).ArgConstraint(NotNull(ArgNo(0))));
2564*e038c9c4Sjoerg }
2565*e038c9c4Sjoerg
2566*e038c9c4Sjoerg // Functions for testing.
2567*e038c9c4Sjoerg if (ChecksEnabled[CK_StdCLibraryFunctionsTesterChecker]) {
2568*e038c9c4Sjoerg addToFunctionSummaryMap(
2569*e038c9c4Sjoerg "__not_null", Signature(ArgTypes{IntPtrTy}, RetType{IntTy}),
2570*e038c9c4Sjoerg Summary(EvalCallAsPure).ArgConstraint(NotNull(ArgNo(0))));
2571*e038c9c4Sjoerg
2572*e038c9c4Sjoerg // Test range values.
2573*e038c9c4Sjoerg addToFunctionSummaryMap(
2574*e038c9c4Sjoerg "__single_val_1", Signature(ArgTypes{IntTy}, RetType{IntTy}),
2575*e038c9c4Sjoerg Summary(EvalCallAsPure)
2576*e038c9c4Sjoerg .ArgConstraint(ArgumentCondition(0U, WithinRange, SingleValue(1))));
2577*e038c9c4Sjoerg addToFunctionSummaryMap(
2578*e038c9c4Sjoerg "__range_1_2", Signature(ArgTypes{IntTy}, RetType{IntTy}),
2579*e038c9c4Sjoerg Summary(EvalCallAsPure)
2580*e038c9c4Sjoerg .ArgConstraint(ArgumentCondition(0U, WithinRange, Range(1, 2))));
2581*e038c9c4Sjoerg addToFunctionSummaryMap("__range_1_2__4_5",
2582*e038c9c4Sjoerg Signature(ArgTypes{IntTy}, RetType{IntTy}),
2583*e038c9c4Sjoerg Summary(EvalCallAsPure)
2584*e038c9c4Sjoerg .ArgConstraint(ArgumentCondition(
2585*e038c9c4Sjoerg 0U, WithinRange, Range({1, 2}, {4, 5}))));
2586*e038c9c4Sjoerg
2587*e038c9c4Sjoerg // Test range kind.
2588*e038c9c4Sjoerg addToFunctionSummaryMap(
2589*e038c9c4Sjoerg "__within", Signature(ArgTypes{IntTy}, RetType{IntTy}),
2590*e038c9c4Sjoerg Summary(EvalCallAsPure)
2591*e038c9c4Sjoerg .ArgConstraint(ArgumentCondition(0U, WithinRange, SingleValue(1))));
2592*e038c9c4Sjoerg addToFunctionSummaryMap(
2593*e038c9c4Sjoerg "__out_of", Signature(ArgTypes{IntTy}, RetType{IntTy}),
2594*e038c9c4Sjoerg Summary(EvalCallAsPure)
2595*e038c9c4Sjoerg .ArgConstraint(ArgumentCondition(0U, OutOfRange, SingleValue(1))));
2596*e038c9c4Sjoerg
2597*e038c9c4Sjoerg addToFunctionSummaryMap(
2598*e038c9c4Sjoerg "__two_constrained_args",
2599*e038c9c4Sjoerg Signature(ArgTypes{IntTy, IntTy}, RetType{IntTy}),
2600*e038c9c4Sjoerg Summary(EvalCallAsPure)
2601*e038c9c4Sjoerg .ArgConstraint(ArgumentCondition(0U, WithinRange, SingleValue(1)))
2602*e038c9c4Sjoerg .ArgConstraint(ArgumentCondition(1U, WithinRange, SingleValue(1))));
2603*e038c9c4Sjoerg addToFunctionSummaryMap(
2604*e038c9c4Sjoerg "__arg_constrained_twice", Signature(ArgTypes{IntTy}, RetType{IntTy}),
2605*e038c9c4Sjoerg Summary(EvalCallAsPure)
2606*e038c9c4Sjoerg .ArgConstraint(ArgumentCondition(0U, OutOfRange, SingleValue(1)))
2607*e038c9c4Sjoerg .ArgConstraint(ArgumentCondition(0U, OutOfRange, SingleValue(2))));
2608*e038c9c4Sjoerg addToFunctionSummaryMap(
2609*e038c9c4Sjoerg "__defaultparam",
2610*e038c9c4Sjoerg Signature(ArgTypes{Irrelevant, IntTy}, RetType{IntTy}),
2611*e038c9c4Sjoerg Summary(EvalCallAsPure).ArgConstraint(NotNull(ArgNo(0))));
2612*e038c9c4Sjoerg addToFunctionSummaryMap(
2613*e038c9c4Sjoerg "__variadic",
2614*e038c9c4Sjoerg Signature(ArgTypes{VoidPtrTy, ConstCharPtrTy}, RetType{IntTy}),
2615*e038c9c4Sjoerg Summary(EvalCallAsPure)
2616*e038c9c4Sjoerg .ArgConstraint(NotNull(ArgNo(0)))
2617*e038c9c4Sjoerg .ArgConstraint(NotNull(ArgNo(1))));
2618*e038c9c4Sjoerg addToFunctionSummaryMap(
2619*e038c9c4Sjoerg "__buf_size_arg_constraint",
2620*e038c9c4Sjoerg Signature(ArgTypes{ConstVoidPtrTy, SizeTy}, RetType{IntTy}),
2621*e038c9c4Sjoerg Summary(EvalCallAsPure)
2622*e038c9c4Sjoerg .ArgConstraint(
2623*e038c9c4Sjoerg BufferSize(/*Buffer=*/ArgNo(0), /*BufSize=*/ArgNo(1))));
2624*e038c9c4Sjoerg addToFunctionSummaryMap(
2625*e038c9c4Sjoerg "__buf_size_arg_constraint_mul",
2626*e038c9c4Sjoerg Signature(ArgTypes{ConstVoidPtrTy, SizeTy, SizeTy}, RetType{IntTy}),
2627*e038c9c4Sjoerg Summary(EvalCallAsPure)
2628*e038c9c4Sjoerg .ArgConstraint(BufferSize(/*Buffer=*/ArgNo(0), /*BufSize=*/ArgNo(1),
2629*e038c9c4Sjoerg /*BufSizeMultiplier=*/ArgNo(2))));
2630*e038c9c4Sjoerg addToFunctionSummaryMap(
2631*e038c9c4Sjoerg "__buf_size_arg_constraint_concrete",
2632*e038c9c4Sjoerg Signature(ArgTypes{ConstVoidPtrTy}, RetType{IntTy}),
2633*e038c9c4Sjoerg Summary(EvalCallAsPure)
2634*e038c9c4Sjoerg .ArgConstraint(BufferSize(/*Buffer=*/ArgNo(0),
2635*e038c9c4Sjoerg /*BufSize=*/BVF.getValue(10, IntTy))));
2636*e038c9c4Sjoerg addToFunctionSummaryMap(
2637*e038c9c4Sjoerg {"__test_restrict_param_0", "__test_restrict_param_1",
2638*e038c9c4Sjoerg "__test_restrict_param_2"},
2639*e038c9c4Sjoerg Signature(ArgTypes{VoidPtrRestrictTy}, RetType{VoidTy}),
2640*e038c9c4Sjoerg Summary(EvalCallAsPure));
2641*e038c9c4Sjoerg }
2642*e038c9c4Sjoerg
2643*e038c9c4Sjoerg SummariesInitialized = true;
26447330f729Sjoerg }
26457330f729Sjoerg
registerStdCLibraryFunctionsChecker(CheckerManager & mgr)26467330f729Sjoerg void ento::registerStdCLibraryFunctionsChecker(CheckerManager &mgr) {
2647*e038c9c4Sjoerg auto *Checker = mgr.registerChecker<StdLibraryFunctionsChecker>();
2648*e038c9c4Sjoerg Checker->DisplayLoadedSummaries =
2649*e038c9c4Sjoerg mgr.getAnalyzerOptions().getCheckerBooleanOption(
2650*e038c9c4Sjoerg Checker, "DisplayLoadedSummaries");
2651*e038c9c4Sjoerg Checker->ModelPOSIX =
2652*e038c9c4Sjoerg mgr.getAnalyzerOptions().getCheckerBooleanOption(Checker, "ModelPOSIX");
26537330f729Sjoerg }
26547330f729Sjoerg
shouldRegisterStdCLibraryFunctionsChecker(const CheckerManager & mgr)2655*e038c9c4Sjoerg bool ento::shouldRegisterStdCLibraryFunctionsChecker(
2656*e038c9c4Sjoerg const CheckerManager &mgr) {
26577330f729Sjoerg return true;
26587330f729Sjoerg }
2659*e038c9c4Sjoerg
2660*e038c9c4Sjoerg #define REGISTER_CHECKER(name) \
2661*e038c9c4Sjoerg void ento::register##name(CheckerManager &mgr) { \
2662*e038c9c4Sjoerg StdLibraryFunctionsChecker *checker = \
2663*e038c9c4Sjoerg mgr.getChecker<StdLibraryFunctionsChecker>(); \
2664*e038c9c4Sjoerg checker->ChecksEnabled[StdLibraryFunctionsChecker::CK_##name] = true; \
2665*e038c9c4Sjoerg checker->CheckNames[StdLibraryFunctionsChecker::CK_##name] = \
2666*e038c9c4Sjoerg mgr.getCurrentCheckerName(); \
2667*e038c9c4Sjoerg } \
2668*e038c9c4Sjoerg \
2669*e038c9c4Sjoerg bool ento::shouldRegister##name(const CheckerManager &mgr) { return true; }
2670*e038c9c4Sjoerg
2671*e038c9c4Sjoerg REGISTER_CHECKER(StdCLibraryFunctionArgsChecker)
2672*e038c9c4Sjoerg REGISTER_CHECKER(StdCLibraryFunctionsTesterChecker)
2673