clang 23.0.0git
StdLibraryFunctionsChecker.cpp
Go to the documentation of this file.
1//=== StdLibraryFunctionsChecker.cpp - Model standard functions -*- C++ -*-===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8//
9// This checker improves modeling of a few simple library functions.
10//
11// This checker provides a specification format - `Summary' - and
12// contains descriptions of some library functions in this format. Each
13// specification contains a list of branches for splitting the program state
14// upon call, and range constraints on argument and return-value symbols that
15// are satisfied on each branch. This spec can be expanded to include more
16// items, like external effects of the function.
17//
18// The main difference between this approach and the body farms technique is
19// in more explicit control over how many branches are produced. For example,
20// consider standard C function `ispunct(int x)', which returns a non-zero value
21// iff `x' is a punctuation character, that is, when `x' is in range
22// ['!', '/'] [':', '@'] U ['[', '\`'] U ['{', '~'].
23// `Summary' provides only two branches for this function. However,
24// any attempt to describe this range with if-statements in the body farm
25// would result in many more branches. Because each branch needs to be analyzed
26// independently, this significantly reduces performance. Additionally,
27// once we consider a branch on which `x' is in range, say, ['!', '/'],
28// we assume that such branch is an important separate path through the program,
29// which may lead to false positives because considering this particular path
30// was not consciously intended, and therefore it might have been unreachable.
31//
32// This checker uses eval::Call for modeling pure functions (functions without
33// side effects), for which their `Summary' is a precise model. This avoids
34// unnecessary invalidation passes. Conflicts with other checkers are unlikely
35// because if the function has no other effects, other checkers would probably
36// never want to improve upon the modeling done by this checker.
37//
38// Non-pure functions, for which only partial improvement over the default
39// behavior is expected, are modeled via check::PostCall, non-intrusively.
40//
41//===----------------------------------------------------------------------===//
42
43#include "ErrnoModeling.h"
52#include "llvm/ADT/STLExtras.h"
53#include "llvm/ADT/SmallString.h"
54#include "llvm/ADT/StringExtras.h"
55#include "llvm/Support/FormatVariadic.h"
56
57#include <optional>
58#include <string>
59
60using namespace clang;
61using namespace clang::ento;
62
63namespace {
64class StdLibraryFunctionsChecker
65 : public Checker<check::PreCall, check::PostCall, eval::Call> {
66
67 class Summary;
68
69 /// Specify how much the analyzer engine should entrust modeling this function
70 /// to us.
71 enum InvalidationKind {
72 /// No \c eval::Call for the function, it can be modeled elsewhere.
73 /// This checker checks only pre and post conditions.
74 NoEvalCall,
75 /// The function is modeled completely in this checker.
76 EvalCallAsPure
77 };
78
79 /// Given a range, should the argument stay inside or outside this range?
80 enum RangeKind { OutOfRange, WithinRange };
81
82 static RangeKind negateKind(RangeKind K) {
83 switch (K) {
84 case OutOfRange:
85 return WithinRange;
86 case WithinRange:
87 return OutOfRange;
88 }
89 llvm_unreachable("Unknown range kind");
90 }
91
92 /// The universal integral type to use in value range descriptions.
93 /// Unsigned to make sure overflows are well-defined.
94 typedef uint64_t RangeInt;
95
96 /// Describes a single range constraint. Eg. {{0, 1}, {3, 4}} is
97 /// a non-negative integer, which less than 5 and not equal to 2.
98 typedef std::vector<std::pair<RangeInt, RangeInt>> IntRangeVector;
99
100 /// A reference to an argument or return value by its number.
101 /// ArgNo in CallExpr and CallEvent is defined as Unsigned, but
102 /// obviously uint32_t should be enough for all practical purposes.
103 typedef uint32_t ArgNo;
104 /// Special argument number for specifying the return value.
105 static const ArgNo Ret;
106
107 /// Get a string representation of an argument index.
108 /// E.g.: (1) -> '1st arg', (2) - > '2nd arg'
109 static void printArgDesc(ArgNo, llvm::raw_ostream &Out);
110 /// Print value X of the argument in form " (which is X)",
111 /// if the value is a fixed known value, otherwise print nothing.
112 /// This is used as simple explanation of values if possible.
113 static void printArgValueInfo(ArgNo ArgN, ProgramStateRef State,
114 const CallEvent &Call, llvm::raw_ostream &Out);
115 /// Append textual description of a numeric range [RMin,RMax] to
116 /// \p Out.
117 static void appendInsideRangeDesc(llvm::APSInt RMin, llvm::APSInt RMax,
118 QualType ArgT, BasicValueFactory &BVF,
119 llvm::raw_ostream &Out);
120 /// Append textual description of a numeric range out of [RMin,RMax] to
121 /// \p Out.
122 static void appendOutOfRangeDesc(llvm::APSInt RMin, llvm::APSInt RMax,
123 QualType ArgT, BasicValueFactory &BVF,
124 llvm::raw_ostream &Out);
125
126 class ValueConstraint;
127
128 /// Pointer to the ValueConstraint. We need a copyable, polymorphic and
129 /// default initializable type (vector needs that). A raw pointer was good,
130 /// however, we cannot default initialize that. unique_ptr makes the Summary
131 /// class non-copyable, therefore not an option. Releasing the copyability
132 /// requirement would render the initialization of the Summary map infeasible.
133 /// Mind that a pointer to a new value constraint is created when the negate
134 /// function is used.
135 using ValueConstraintPtr = std::shared_ptr<ValueConstraint>;
136
137 /// Polymorphic base class that represents a constraint on a given argument
138 /// (or return value) of a function. Derived classes implement different kind
139 /// of constraints, e.g range constraints or correlation between two
140 /// arguments.
141 /// These are used as argument constraints (preconditions) of functions, in
142 /// which case a bug report may be emitted if the constraint is not satisfied.
143 /// Another use is as conditions for summary cases, to create different
144 /// classes of behavior for a function. In this case no description of the
145 /// constraint is needed because the summary cases have an own (not generated)
146 /// description string.
147 class ValueConstraint {
148 public:
149 ValueConstraint(ArgNo ArgN) : ArgN(ArgN) {}
150 virtual ~ValueConstraint() {}
151
152 /// Apply the effects of the constraint on the given program state. If null
153 /// is returned then the constraint is not feasible.
154 virtual ProgramStateRef apply(ProgramStateRef State, const CallEvent &Call,
155 const Summary &Summary,
156 CheckerContext &C) const = 0;
157
158 /// Represents that in which context do we require a description of the
159 /// constraint.
160 enum DescriptionKind {
161 /// Describe a constraint that was violated.
162 /// Description should start with something like "should be".
163 Violation,
164 /// Describe a constraint that was assumed to be true.
165 /// This can be used when a precondition is satisfied, or when a summary
166 /// case is applied.
167 /// Description should start with something like "is".
168 Assumption
169 };
170
171 /// Give a description that explains the constraint to the user. Used when
172 /// a bug is reported or when the constraint is applied and displayed as a
173 /// note. The description should not mention the argument (getArgNo).
174 /// See StdLibraryFunctionsChecker::reportBug about how this function is
175 /// used (this function is used not only there).
176 virtual void describe(DescriptionKind DK, const CallEvent &Call,
177 ProgramStateRef State, const Summary &Summary,
178 llvm::raw_ostream &Out) const {
179 // There are some descendant classes that are not used as argument
180 // constraints, e.g. ComparisonConstraint. In that case we can safely
181 // ignore the implementation of this function.
182 llvm_unreachable(
183 "Description not implemented for summary case constraints");
184 }
185
186 /// Give a description that explains the actual argument value (where the
187 /// current ValueConstraint applies to) to the user. This function should be
188 /// called only when the current constraint is satisfied by the argument.
189 /// It should produce a more precise description than the constraint itself.
190 /// The actual value of the argument and the program state can be used to
191 /// make the description more precise. In the most simple case, if the
192 /// argument has a fixed known value this value can be printed into \p Out,
193 /// this is done by default.
194 /// The function should return true if a description was printed to \p Out,
195 /// otherwise false.
196 /// See StdLibraryFunctionsChecker::reportBug about how this function is
197 /// used.
198 virtual bool describeArgumentValue(const CallEvent &Call,
199 ProgramStateRef State,
200 const Summary &Summary,
201 llvm::raw_ostream &Out) const {
202 if (auto N = getArgSVal(Call, getArgNo()).getAs<NonLoc>()) {
203 if (const llvm::APSInt *Int = N->getAsInteger()) {
204 Out << *Int;
205 return true;
206 }
207 }
208 return false;
209 }
210
211 /// Return those arguments that should be tracked when we report a bug about
212 /// argument constraint violation. By default it is the argument that is
213 /// constrained, however, in some special cases we need to track other
214 /// arguments as well. E.g. a buffer size might be encoded in another
215 /// argument.
216 /// The "return value" argument number can not occur as returned value.
217 virtual std::vector<ArgNo> getArgsToTrack() const { return {ArgN}; }
218
219 /// Get a constraint that represents exactly the opposite of the current.
220 virtual ValueConstraintPtr negate() const {
221 llvm_unreachable("Not implemented");
222 };
223
224 /// Check whether the constraint is malformed or not. It is malformed if the
225 /// specified argument has a mismatch with the given FunctionDecl (e.g. the
226 /// arg number is out-of-range of the function's argument list).
227 /// This condition can indicate if a probably wrong or unexpected function
228 /// was found where the constraint is to be applied.
229 bool checkValidity(const FunctionDecl *FD) const {
230 const bool ValidArg = ArgN == Ret || ArgN < FD->getNumParams();
231 assert(ValidArg && "Arg out of range!");
232 if (!ValidArg)
233 return false;
234 // Subclasses may further refine the validation.
235 return checkSpecificValidity(FD);
236 }
237
238 /// Return the argument number (may be placeholder for "return value").
239 ArgNo getArgNo() const { return ArgN; }
240
241 protected:
242 /// Argument to which to apply the constraint. It can be a real argument of
243 /// the function to check, or a special value to indicate the return value
244 /// of the function.
245 /// Every constraint is assigned to one main argument, even if other
246 /// arguments are involved.
247 ArgNo ArgN;
248
249 /// Do constraint-specific validation check.
250 virtual bool checkSpecificValidity(const FunctionDecl *FD) const {
251 return true;
252 }
253 };
254
255 /// Check if a single argument falls into a specific "range".
256 /// A range is formed as a set of intervals.
257 /// E.g. \code {['A', 'Z'], ['a', 'z'], ['_', '_']} \endcode
258 /// The intervals are closed intervals that contain one or more values.
259 ///
260 /// The default constructed RangeConstraint has an empty range, applying
261 /// such constraint does not involve any assumptions, thus the State remains
262 /// unchanged. This is meaningful, if the range is dependent on a looked up
263 /// type (e.g. [0, Socklen_tMax]). If the type is not found, then the range
264 /// is default initialized to be empty.
265 class RangeConstraint : public ValueConstraint {
266 /// The constraint can be specified by allowing or disallowing the range.
267 /// WithinRange indicates allowing the range, OutOfRange indicates
268 /// disallowing it (allowing the complementary range).
269 RangeKind Kind;
270
271 /// A set of intervals.
272 IntRangeVector Ranges;
273
274 /// A textual description of this constraint for the specific case where the
275 /// constraint is used. If empty a generated description will be used that
276 /// is built from the range of the constraint.
277 StringRef Description;
278
279 public:
280 RangeConstraint(ArgNo ArgN, RangeKind Kind, const IntRangeVector &Ranges,
281 StringRef Desc = "")
282 : ValueConstraint(ArgN), Kind(Kind), Ranges(Ranges), Description(Desc) {
283 }
284
285 const IntRangeVector &getRanges() const { return Ranges; }
286
287 ProgramStateRef apply(ProgramStateRef State, const CallEvent &Call,
288 const Summary &Summary,
289 CheckerContext &C) const override;
290
291 void describe(DescriptionKind DK, const CallEvent &Call,
292 ProgramStateRef State, const Summary &Summary,
293 llvm::raw_ostream &Out) const override;
294
295 bool describeArgumentValue(const CallEvent &Call, ProgramStateRef State,
296 const Summary &Summary,
297 llvm::raw_ostream &Out) const override;
298
299 ValueConstraintPtr negate() const override {
300 RangeConstraint Tmp(*this);
301 Tmp.Kind = negateKind(Kind);
302 return std::make_shared<RangeConstraint>(Tmp);
303 }
304
305 protected:
306 bool checkSpecificValidity(const FunctionDecl *FD) const override {
307 const bool ValidArg =
308 getArgType(FD, ArgN)->isIntegralType(FD->getASTContext());
309 assert(ValidArg &&
310 "This constraint should be applied on an integral type");
311 return ValidArg;
312 }
313
314 private:
315 /// A callback function that is used when iterating over the range
316 /// intervals. It gets the begin and end (inclusive) of one interval.
317 /// This is used to make any kind of task possible that needs an iteration
318 /// over the intervals.
319 using RangeApplyFunction =
320 std::function<bool(const llvm::APSInt &Min, const llvm::APSInt &Max)>;
321
322 /// Call a function on the intervals of the range.
323 /// The function is called with all intervals in the range.
324 void applyOnWithinRange(BasicValueFactory &BVF, QualType ArgT,
325 const RangeApplyFunction &F) const;
326 /// Call a function on all intervals in the complementary range.
327 /// The function is called with all intervals that fall out of the range.
328 /// E.g. consider an interval list [A, B] and [C, D]
329 /// \code
330 /// -------+--------+------------------+------------+----------->
331 /// A B C D
332 /// \endcode
333 /// We get the ranges [-inf, A - 1], [D + 1, +inf], [B + 1, C - 1].
334 /// The \p ArgT is used to determine the min and max of the type that is
335 /// used as "-inf" and "+inf".
336 void applyOnOutOfRange(BasicValueFactory &BVF, QualType ArgT,
337 const RangeApplyFunction &F) const;
338 /// Call a function on the intervals of the range or the complementary
339 /// range.
340 void applyOnRange(RangeKind Kind, BasicValueFactory &BVF, QualType ArgT,
341 const RangeApplyFunction &F) const {
342 switch (Kind) {
343 case OutOfRange:
344 applyOnOutOfRange(BVF, ArgT, F);
345 break;
346 case WithinRange:
347 applyOnWithinRange(BVF, ArgT, F);
348 break;
349 };
350 }
351 };
352
353 /// Check relation of an argument to another.
354 class ComparisonConstraint : public ValueConstraint {
356 ArgNo OtherArgN;
357
358 public:
359 ComparisonConstraint(ArgNo ArgN, BinaryOperator::Opcode Opcode,
360 ArgNo OtherArgN)
361 : ValueConstraint(ArgN), Opcode(Opcode), OtherArgN(OtherArgN) {}
362 ArgNo getOtherArgNo() const { return OtherArgN; }
363 BinaryOperator::Opcode getOpcode() const { return Opcode; }
364 ProgramStateRef apply(ProgramStateRef State, const CallEvent &Call,
365 const Summary &Summary,
366 CheckerContext &C) const override;
367 };
368
369 /// Check null or non-null-ness of an argument that is of pointer type.
370 class NullnessConstraint : public ValueConstraint {
371 using ValueConstraint::ValueConstraint;
372 // This variable has a role when we negate the constraint.
373 bool CannotBeNull = true;
374
375 public:
376 NullnessConstraint(ArgNo ArgN, bool CannotBeNull = true)
377 : ValueConstraint(ArgN), CannotBeNull(CannotBeNull) {}
378
379 ProgramStateRef apply(ProgramStateRef State, const CallEvent &Call,
380 const Summary &Summary,
381 CheckerContext &C) const override;
382
383 void describe(DescriptionKind DK, const CallEvent &Call,
384 ProgramStateRef State, const Summary &Summary,
385 llvm::raw_ostream &Out) const override;
386
387 bool describeArgumentValue(const CallEvent &Call, ProgramStateRef State,
388 const Summary &Summary,
389 llvm::raw_ostream &Out) const override;
390
391 ValueConstraintPtr negate() const override {
392 NullnessConstraint Tmp(*this);
393 Tmp.CannotBeNull = !this->CannotBeNull;
394 return std::make_shared<NullnessConstraint>(Tmp);
395 }
396
397 protected:
398 bool checkSpecificValidity(const FunctionDecl *FD) const override {
399 const bool ValidArg = getArgType(FD, ArgN)->isPointerType();
400 assert(ValidArg &&
401 "This constraint should be applied only on a pointer type");
402 return ValidArg;
403 }
404 };
405
406 /// Check null or non-null-ness of an argument that is of pointer type.
407 /// The argument is meant to be a buffer that has a size constraint, and it
408 /// is allowed to have a NULL value if the size is 0. The size can depend on
409 /// 1 or 2 additional arguments, if one of these is 0 the buffer is allowed to
410 /// be NULL. Otherwise, the buffer pointer must be non-null. This is useful
411 /// for functions like `fread` which have this special property.
412 class BufferNullnessConstraint : public ValueConstraint {
413 using ValueConstraint::ValueConstraint;
414 ArgNo SizeArg1N;
415 std::optional<ArgNo> SizeArg2N;
416 // This variable has a role when we negate the constraint.
417 bool CannotBeNull = true;
418
419 public:
420 BufferNullnessConstraint(ArgNo ArgN, ArgNo SizeArg1N,
421 std::optional<ArgNo> SizeArg2N,
422 bool CannotBeNull = true)
423 : ValueConstraint(ArgN), SizeArg1N(SizeArg1N), SizeArg2N(SizeArg2N),
424 CannotBeNull(CannotBeNull) {}
425
426 ProgramStateRef apply(ProgramStateRef State, const CallEvent &Call,
427 const Summary &Summary,
428 CheckerContext &C) const override;
429
430 void describe(DescriptionKind DK, const CallEvent &Call,
431 ProgramStateRef State, const Summary &Summary,
432 llvm::raw_ostream &Out) const override;
433
434 bool describeArgumentValue(const CallEvent &Call, ProgramStateRef State,
435 const Summary &Summary,
436 llvm::raw_ostream &Out) const override;
437
438 ValueConstraintPtr negate() const override {
439 BufferNullnessConstraint Tmp(*this);
440 Tmp.CannotBeNull = !this->CannotBeNull;
441 return std::make_shared<BufferNullnessConstraint>(Tmp);
442 }
443
444 protected:
445 bool checkSpecificValidity(const FunctionDecl *FD) const override {
446 const bool ValidArg = getArgType(FD, ArgN)->isPointerType();
447 assert(ValidArg &&
448 "This constraint should be applied only on a pointer type");
449 return ValidArg;
450 }
451 };
452
453 // Represents a buffer argument with an additional size constraint. The
454 // constraint may be a concrete value, or a symbolic value in an argument.
455 // Example 1. Concrete value as the minimum buffer size.
456 // char *asctime_r(const struct tm *restrict tm, char *restrict buf);
457 // // `buf` size must be at least 26 bytes according the POSIX standard.
458 // Example 2. Argument as a buffer size.
459 // ctime_s(char *buffer, rsize_t bufsz, const time_t *time);
460 // Example 3. The size is computed as a multiplication of other args.
461 // size_t fread(void *ptr, size_t size, size_t nmemb, FILE *stream);
462 // // Here, ptr is the buffer, and its minimum size is `size * nmemb`.
463 class BufferSizeConstraint : public ValueConstraint {
464 // The concrete value which is the minimum size for the buffer.
465 std::optional<llvm::APSInt> ConcreteSize;
466 // The argument which holds the size of the buffer.
467 std::optional<ArgNo> SizeArgN;
468 // The argument which is a multiplier to size. This is set in case of
469 // `fread` like functions where the size is computed as a multiplication of
470 // two arguments.
471 std::optional<ArgNo> SizeMultiplierArgN;
472 // The operator we use in apply. This is negated in negate().
473 BinaryOperator::Opcode Op = BO_LE;
474
475 public:
476 BufferSizeConstraint(ArgNo Buffer, llvm::APSInt BufMinSize)
477 : ValueConstraint(Buffer), ConcreteSize(BufMinSize) {}
478 BufferSizeConstraint(ArgNo Buffer, ArgNo BufSize)
479 : ValueConstraint(Buffer), SizeArgN(BufSize) {}
480 BufferSizeConstraint(ArgNo Buffer, ArgNo BufSize, ArgNo BufSizeMultiplier)
481 : ValueConstraint(Buffer), SizeArgN(BufSize),
482 SizeMultiplierArgN(BufSizeMultiplier) {}
483
484 ProgramStateRef apply(ProgramStateRef State, const CallEvent &Call,
485 const Summary &Summary,
486 CheckerContext &C) const override;
487
488 void describe(DescriptionKind DK, const CallEvent &Call,
489 ProgramStateRef State, const Summary &Summary,
490 llvm::raw_ostream &Out) const override;
491
492 bool describeArgumentValue(const CallEvent &Call, ProgramStateRef State,
493 const Summary &Summary,
494 llvm::raw_ostream &Out) const override;
495
496 std::vector<ArgNo> getArgsToTrack() const override {
497 std::vector<ArgNo> Result{ArgN};
498 if (SizeArgN)
499 Result.push_back(*SizeArgN);
500 if (SizeMultiplierArgN)
501 Result.push_back(*SizeMultiplierArgN);
502 return Result;
503 }
504
505 ValueConstraintPtr negate() const override {
506 BufferSizeConstraint Tmp(*this);
508 return std::make_shared<BufferSizeConstraint>(Tmp);
509 }
510
511 protected:
512 bool checkSpecificValidity(const FunctionDecl *FD) const override {
513 const bool ValidArg = getArgType(FD, ArgN)->isPointerType();
514 assert(ValidArg &&
515 "This constraint should be applied only on a pointer type");
516 return ValidArg;
517 }
518 };
519
520 /// The complete list of constraints that defines a single branch.
521 using ConstraintSet = std::vector<ValueConstraintPtr>;
522
523 /// Define how a function affects the system variable 'errno'.
524 /// This works together with the \c ErrnoModeling and \c ErrnoChecker classes.
525 /// Currently 3 use cases exist: success, failure, irrelevant.
526 /// In the future the failure case can be customized to set \c errno to a
527 /// more specific constraint (for example > 0), or new case can be added
528 /// for functions which require check of \c errno in both success and failure
529 /// case.
530 class ErrnoConstraintBase {
531 public:
532 /// Apply specific state changes related to the errno variable.
533 virtual ProgramStateRef apply(ProgramStateRef State, const CallEvent &Call,
534 const Summary &Summary,
535 CheckerContext &C) const = 0;
536 /// Get a description about what happens with 'errno' here and how it causes
537 /// a later bug report created by ErrnoChecker.
538 /// Empty return value means that 'errno' related bug may not happen from
539 /// the current analyzed function.
540 virtual std::string describe(CheckerContext &C) const { return ""; }
541
542 virtual ~ErrnoConstraintBase() {}
543
544 protected:
545 ErrnoConstraintBase() = default;
546
547 /// This is used for conjure symbol for errno to differentiate from the
548 /// original call expression (same expression is used for the errno symbol).
549 static int Tag;
550 };
551
552 /// Reset errno constraints to irrelevant.
553 /// This is applicable to functions that may change 'errno' and are not
554 /// modeled elsewhere.
555 class ResetErrnoConstraint : public ErrnoConstraintBase {
556 public:
557 ProgramStateRef apply(ProgramStateRef State, const CallEvent &Call,
558 const Summary &Summary,
559 CheckerContext &C) const override {
561 }
562 };
563
564 /// Do not change errno constraints.
565 /// This is applicable to functions that are modeled in another checker
566 /// and the already set errno constraints should not be changed in the
567 /// post-call event.
568 class NoErrnoConstraint : public ErrnoConstraintBase {
569 public:
570 ProgramStateRef apply(ProgramStateRef State, const CallEvent &Call,
571 const Summary &Summary,
572 CheckerContext &C) const override {
573 return State;
574 }
575 };
576
577 /// Set errno constraint at failure cases of standard functions.
578 /// Failure case: 'errno' becomes not equal to 0 and may or may not be checked
579 /// by the program. \c ErrnoChecker does not emit a bug report after such a
580 /// function call.
581 class FailureErrnoConstraint : public ErrnoConstraintBase {
582 public:
583 ProgramStateRef apply(ProgramStateRef State, const CallEvent &Call,
584 const Summary &Summary,
585 CheckerContext &C) const override {
586 SValBuilder &SVB = C.getSValBuilder();
587 NonLoc ErrnoSVal = SVB.conjureSymbolVal(Call, C.getASTContext().IntTy,
588 C.blockCount(), &Tag)
589 .castAs<NonLoc>();
590 return errno_modeling::setErrnoForStdFailure(State, C, ErrnoSVal);
591 }
592 };
593
594 /// Set errno constraint at success cases of standard functions.
595 /// Success case: 'errno' is not allowed to be used because the value is
596 /// undefined after successful call.
597 /// \c ErrnoChecker can emit bug report after such a function call if errno
598 /// is used.
599 class SuccessErrnoConstraint : public ErrnoConstraintBase {
600 public:
601 ProgramStateRef apply(ProgramStateRef State, const CallEvent &Call,
602 const Summary &Summary,
603 CheckerContext &C) const override {
605 }
606
607 std::string describe(CheckerContext &C) const override {
608 return "'errno' becomes undefined after the call";
609 }
610 };
611
612 /// Set errno constraint at functions that indicate failure only with 'errno'.
613 /// In this case 'errno' is required to be observed.
614 /// \c ErrnoChecker can emit bug report after such a function call if errno
615 /// is overwritten without a read before.
616 class ErrnoMustBeCheckedConstraint : public ErrnoConstraintBase {
617 public:
618 ProgramStateRef apply(ProgramStateRef State, const CallEvent &Call,
619 const Summary &Summary,
620 CheckerContext &C) const override {
622 Call.getCFGElementRef());
623 }
624
625 std::string describe(CheckerContext &C) const override {
626 return "reading 'errno' is required to find out if the call has failed";
627 }
628 };
629
630 /// A single branch of a function summary.
631 ///
632 /// A branch is defined by a series of constraints - "assumptions" -
633 /// that together form a single possible outcome of invoking the function.
634 /// When static analyzer considers a branch, it tries to introduce
635 /// a child node in the Exploded Graph. The child node has to include
636 /// constraints that define the branch. If the constraints contradict
637 /// existing constraints in the state, the node is not created and the branch
638 /// is dropped; otherwise it's queued for future exploration.
639 /// The branch is accompanied by a note text that may be displayed
640 /// to the user when a bug is found on a path that takes this branch.
641 ///
642 /// For example, consider the branches in `isalpha(x)`:
643 /// Branch 1)
644 /// x is in range ['A', 'Z'] or in ['a', 'z']
645 /// then the return value is not 0. (I.e. out-of-range [0, 0])
646 /// and the note may say "Assuming the character is alphabetical"
647 /// Branch 2)
648 /// x is out-of-range ['A', 'Z'] and out-of-range ['a', 'z']
649 /// then the return value is 0
650 /// and the note may say "Assuming the character is non-alphabetical".
651 class SummaryCase {
652 ConstraintSet Constraints;
653 const ErrnoConstraintBase &ErrnoConstraint;
654 StringRef Note;
655
656 public:
657 SummaryCase(ConstraintSet &&Constraints, const ErrnoConstraintBase &ErrnoC,
658 StringRef Note)
659 : Constraints(std::move(Constraints)), ErrnoConstraint(ErrnoC),
660 Note(Note) {}
661
662 SummaryCase(const ConstraintSet &Constraints,
663 const ErrnoConstraintBase &ErrnoC, StringRef Note)
664 : Constraints(Constraints), ErrnoConstraint(ErrnoC), Note(Note) {}
665
666 const ConstraintSet &getConstraints() const { return Constraints; }
667 const ErrnoConstraintBase &getErrnoConstraint() const {
668 return ErrnoConstraint;
669 }
670 StringRef getNote() const { return Note; }
671 };
672
673 using ArgTypes = ArrayRef<std::optional<QualType>>;
674 using RetType = std::optional<QualType>;
675
676 // A placeholder type, we use it whenever we do not care about the concrete
677 // type in a Signature.
678 const QualType Irrelevant{};
679 bool static isIrrelevant(QualType T) { return T.isNull(); }
680
681 // The signature of a function we want to describe with a summary. This is a
682 // concessive signature, meaning there may be irrelevant types in the
683 // signature which we do not check against a function with concrete types.
684 // All types in the spec need to be canonical.
685 class Signature {
686 using ArgQualTypes = std::vector<QualType>;
687 ArgQualTypes ArgTys;
688 QualType RetTy;
689 // True if any component type is not found by lookup.
690 bool Invalid = false;
691
692 public:
693 // Construct a signature from optional types. If any of the optional types
694 // are not set then the signature will be invalid.
695 Signature(ArgTypes ArgTys, RetType RetTy) {
696 for (std::optional<QualType> Arg : ArgTys) {
697 if (!Arg) {
698 Invalid = true;
699 return;
700 } else {
701 assertArgTypeSuitableForSignature(*Arg);
702 this->ArgTys.push_back(*Arg);
703 }
704 }
705 if (!RetTy) {
706 Invalid = true;
707 return;
708 } else {
709 assertRetTypeSuitableForSignature(*RetTy);
710 this->RetTy = *RetTy;
711 }
712 }
713
714 bool isInvalid() const { return Invalid; }
715 bool matches(const FunctionDecl *FD) const;
716
717 private:
718 static void assertArgTypeSuitableForSignature(QualType T) {
719 assert((T.isNull() || !T->isVoidType()) &&
720 "We should have no void types in the spec");
721 assert((T.isNull() || T.isCanonical()) &&
722 "We should only have canonical types in the spec");
723 }
724 static void assertRetTypeSuitableForSignature(QualType T) {
725 assert((T.isNull() || T.isCanonical()) &&
726 "We should only have canonical types in the spec");
727 }
728 };
729
730 static QualType getArgType(const FunctionDecl *FD, ArgNo ArgN) {
731 assert(FD && "Function must be set");
732 QualType T = (ArgN == Ret)
734 : FD->getParamDecl(ArgN)->getType().getCanonicalType();
735 return T;
736 }
737
738 using SummaryCases = std::vector<SummaryCase>;
739
740 /// A summary includes information about
741 /// * function prototype (signature)
742 /// * approach to invalidation,
743 /// * a list of branches - so, a list of list of ranges,
744 /// * a list of argument constraints, that must be true on every branch.
745 /// If these constraints are not satisfied that means a fatal error
746 /// usually resulting in undefined behaviour.
747 ///
748 /// Application of a summary:
749 /// The signature and argument constraints together contain information
750 /// about which functions are handled by the summary. The signature can use
751 /// "wildcards", i.e. Irrelevant types. Irrelevant type of a parameter in
752 /// a signature means that type is not compared to the type of the parameter
753 /// in the found FunctionDecl. Argument constraints may specify additional
754 /// rules for the given parameter's type, those rules are checked once the
755 /// signature is matched.
756 class Summary {
757 const InvalidationKind InvalidationKd;
758 SummaryCases Cases;
759 ConstraintSet ArgConstraints;
760
761 // The function to which the summary applies. This is set after lookup and
762 // match to the signature.
763 const FunctionDecl *FD = nullptr;
764
765 public:
766 Summary(InvalidationKind InvalidationKd) : InvalidationKd(InvalidationKd) {}
767
768 Summary &Case(ConstraintSet &&CS, const ErrnoConstraintBase &ErrnoC,
769 StringRef Note = "") {
770 Cases.push_back(SummaryCase(std::move(CS), ErrnoC, Note));
771 return *this;
772 }
773 Summary &Case(const ConstraintSet &CS, const ErrnoConstraintBase &ErrnoC,
774 StringRef Note = "") {
775 Cases.push_back(SummaryCase(CS, ErrnoC, Note));
776 return *this;
777 }
778 Summary &ArgConstraint(ValueConstraintPtr VC) {
779 assert(VC->getArgNo() != Ret &&
780 "Arg constraint should not refer to the return value");
781 ArgConstraints.push_back(VC);
782 return *this;
783 }
784
785 InvalidationKind getInvalidationKd() const { return InvalidationKd; }
786 const SummaryCases &getCases() const { return Cases; }
787 const ConstraintSet &getArgConstraints() const { return ArgConstraints; }
788
789 QualType getArgType(ArgNo ArgN) const {
790 return StdLibraryFunctionsChecker::getArgType(FD, ArgN);
791 }
792
793 // Returns true if the summary should be applied to the given function.
794 // And if yes then store the function declaration.
795 bool matchesAndSet(const Signature &Sign, const FunctionDecl *FD) {
796 bool Result = Sign.matches(FD) && validateByConstraints(FD);
797 if (Result) {
798 assert(!this->FD && "FD must not be set more than once");
799 this->FD = FD;
800 }
801 return Result;
802 }
803
804 private:
805 // Once we know the exact type of the function then do validation check on
806 // all the given constraints.
807 bool validateByConstraints(const FunctionDecl *FD) const {
808 for (const SummaryCase &Case : Cases)
809 for (const ValueConstraintPtr &Constraint : Case.getConstraints())
810 if (!Constraint->checkValidity(FD))
811 return false;
812 for (const ValueConstraintPtr &Constraint : ArgConstraints)
813 if (!Constraint->checkValidity(FD))
814 return false;
815 return true;
816 }
817 };
818
819 // The map of all functions supported by the checker. It is initialized
820 // lazily, and it doesn't change after initialization.
821 using FunctionSummaryMapType = llvm::DenseMap<const FunctionDecl *, Summary>;
822 mutable FunctionSummaryMapType FunctionSummaryMap;
823
824 const BugType BT_InvalidArg{this, "Function call with invalid argument"};
825 mutable bool SummariesInitialized = false;
826
827 static SVal getArgSVal(const CallEvent &Call, ArgNo ArgN) {
828 return ArgN == Ret ? Call.getReturnValue() : Call.getArgSVal(ArgN);
829 }
830 static std::string getFunctionName(const CallEvent &Call) {
831 assert(Call.getDecl() &&
832 "Call was found by a summary, should have declaration");
833 return cast<NamedDecl>(Call.getDecl())->getNameAsString();
834 }
835
836public:
837 void checkPreCall(const CallEvent &Call, CheckerContext &C) const;
838 void checkPostCall(const CallEvent &Call, CheckerContext &C) const;
839 bool evalCall(const CallEvent &Call, CheckerContext &C) const;
840
841 CheckerNameRef CheckName;
842 bool AddTestFunctions = false;
843
844 bool DisplayLoadedSummaries = false;
845 bool ModelPOSIX = false;
846 bool ShouldAssumeControlledEnvironment = false;
847
848private:
849 std::optional<Summary> findFunctionSummary(const FunctionDecl *FD,
850 CheckerContext &C) const;
851 std::optional<Summary> findFunctionSummary(const CallEvent &Call,
852 CheckerContext &C) const;
853
854 LLVM_ATTRIBUTE_MINSIZE void initFunctionSummaries(CheckerContext &C) const;
855
856 void reportBug(const CallEvent &Call, ExplodedNode *N,
857 const ValueConstraint *VC, const ValueConstraint *NegatedVC,
858 const Summary &Summary, CheckerContext &C) const {
859 assert(Call.getDecl() &&
860 "Function found in summary must have a declaration available");
861 SmallString<256> Msg;
862 llvm::raw_svector_ostream MsgOs(Msg);
863
864 MsgOs << "The ";
865 printArgDesc(VC->getArgNo(), MsgOs);
866 MsgOs << " to '" << getFunctionName(Call) << "' ";
867 bool ValuesPrinted =
868 NegatedVC->describeArgumentValue(Call, N->getState(), Summary, MsgOs);
869 if (ValuesPrinted)
870 MsgOs << " but ";
871 else
872 MsgOs << "is out of the accepted range; It ";
873 VC->describe(ValueConstraint::Violation, Call, C.getState(), Summary,
874 MsgOs);
875 Msg[0] = toupper(Msg[0]);
876 auto R = std::make_unique<PathSensitiveBugReport>(BT_InvalidArg, Msg, N);
877
878 for (ArgNo ArgN : VC->getArgsToTrack()) {
879 bugreporter::trackExpressionValue(N, Call.getArgExpr(ArgN), *R);
880 R->markInteresting(Call.getArgSVal(ArgN));
881 // All tracked arguments are important, highlight them.
882 R->addRange(Call.getArgSourceRange(ArgN));
883 }
884
885 C.emitReport(std::move(R));
886 }
887
888 /// These are the errno constraints that can be passed to summary cases.
889 /// One of these should fit for a single summary case.
890 /// Usually if a failure return value exists for function, that function
891 /// needs different cases for success and failure with different errno
892 /// constraints (and different return value constraints).
893 const NoErrnoConstraint ErrnoUnchanged{};
894 const ResetErrnoConstraint ErrnoIrrelevant{};
895 const ErrnoMustBeCheckedConstraint ErrnoMustBeChecked{};
896 const SuccessErrnoConstraint ErrnoMustNotBeChecked{};
897 const FailureErrnoConstraint ErrnoNEZeroIrrelevant{};
898};
899
900int StdLibraryFunctionsChecker::ErrnoConstraintBase::Tag = 0;
901
902const StdLibraryFunctionsChecker::ArgNo StdLibraryFunctionsChecker::Ret =
903 std::numeric_limits<ArgNo>::max();
904
905static BasicValueFactory &getBVF(ProgramStateRef State) {
906 ProgramStateManager &Mgr = State->getStateManager();
907 SValBuilder &SVB = Mgr.getSValBuilder();
908 return SVB.getBasicValueFactory();
909}
910
911} // end of anonymous namespace
912
913void StdLibraryFunctionsChecker::printArgDesc(
914 StdLibraryFunctionsChecker::ArgNo ArgN, llvm::raw_ostream &Out) {
915 Out << std::to_string(ArgN + 1);
916 Out << llvm::getOrdinalSuffix(ArgN + 1);
917 Out << " argument";
918}
919
920void StdLibraryFunctionsChecker::printArgValueInfo(ArgNo ArgN,
921 ProgramStateRef State,
922 const CallEvent &Call,
923 llvm::raw_ostream &Out) {
924 if (const llvm::APSInt *Val =
925 State->getStateManager().getSValBuilder().getKnownValue(
926 State, getArgSVal(Call, ArgN)))
927 Out << " (which is " << *Val << ")";
928}
929
930void StdLibraryFunctionsChecker::appendInsideRangeDesc(llvm::APSInt RMin,
931 llvm::APSInt RMax,
932 QualType ArgT,
933 BasicValueFactory &BVF,
934 llvm::raw_ostream &Out) {
935 if (RMin.isZero() && RMax.isZero())
936 Out << "zero";
937 else if (RMin == RMax)
938 Out << RMin;
939 else if (RMin == BVF.getMinValue(ArgT)) {
940 if (RMax == -1)
941 Out << "< 0";
942 else
943 Out << "<= " << RMax;
944 } else if (RMax == BVF.getMaxValue(ArgT)) {
945 if (RMin.isOne())
946 Out << "> 0";
947 else
948 Out << ">= " << RMin;
949 } else if (RMin.isNegative() == RMax.isNegative() &&
950 RMin.getLimitedValue() == RMax.getLimitedValue() - 1) {
951 Out << RMin << " or " << RMax;
952 } else {
953 Out << "between " << RMin << " and " << RMax;
954 }
955}
956
957void StdLibraryFunctionsChecker::appendOutOfRangeDesc(llvm::APSInt RMin,
958 llvm::APSInt RMax,
959 QualType ArgT,
960 BasicValueFactory &BVF,
961 llvm::raw_ostream &Out) {
962 if (RMin.isZero() && RMax.isZero())
963 Out << "nonzero";
964 else if (RMin == RMax) {
965 Out << "not equal to " << RMin;
966 } else if (RMin == BVF.getMinValue(ArgT)) {
967 if (RMax == -1)
968 Out << ">= 0";
969 else
970 Out << "> " << RMax;
971 } else if (RMax == BVF.getMaxValue(ArgT)) {
972 if (RMin.isOne())
973 Out << "<= 0";
974 else
975 Out << "< " << RMin;
976 } else if (RMin.isNegative() == RMax.isNegative() &&
977 RMin.getLimitedValue() == RMax.getLimitedValue() - 1) {
978 Out << "not " << RMin << " and not " << RMax;
979 } else {
980 Out << "not between " << RMin << " and " << RMax;
981 }
982}
983
984void StdLibraryFunctionsChecker::RangeConstraint::applyOnWithinRange(
985 BasicValueFactory &BVF, QualType ArgT, const RangeApplyFunction &F) const {
986 if (Ranges.empty())
987 return;
988
989 for (auto [Start, End] : getRanges()) {
990 const llvm::APSInt &Min = BVF.getValue(Start, ArgT);
991 const llvm::APSInt &Max = BVF.getValue(End, ArgT);
992 assert(Min <= Max);
993 if (!F(Min, Max))
994 return;
995 }
996}
997
998void StdLibraryFunctionsChecker::RangeConstraint::applyOnOutOfRange(
999 BasicValueFactory &BVF, QualType ArgT, const RangeApplyFunction &F) const {
1000 if (Ranges.empty())
1001 return;
1002
1003 const IntRangeVector &R = getRanges();
1004 size_t E = R.size();
1005
1006 const llvm::APSInt &MinusInf = BVF.getMinValue(ArgT);
1007 const llvm::APSInt &PlusInf = BVF.getMaxValue(ArgT);
1008
1009 const llvm::APSInt &RangeLeft = BVF.getValue(R[0].first - 1ULL, ArgT);
1010 const llvm::APSInt &RangeRight = BVF.getValue(R[E - 1].second + 1ULL, ArgT);
1011
1012 // Iterate over the "holes" between intervals.
1013 for (size_t I = 1; I != E; ++I) {
1014 const llvm::APSInt &Min = BVF.getValue(R[I - 1].second + 1ULL, ArgT);
1015 const llvm::APSInt &Max = BVF.getValue(R[I].first - 1ULL, ArgT);
1016 if (Min <= Max) {
1017 if (!F(Min, Max))
1018 return;
1019 }
1020 }
1021 // Check the interval [T_MIN, min(R) - 1].
1022 if (RangeLeft != PlusInf) {
1023 assert(MinusInf <= RangeLeft);
1024 if (!F(MinusInf, RangeLeft))
1025 return;
1026 }
1027 // Check the interval [max(R) + 1, T_MAX],
1028 if (RangeRight != MinusInf) {
1029 assert(RangeRight <= PlusInf);
1030 if (!F(RangeRight, PlusInf))
1031 return;
1032 }
1033}
1034
1035ProgramStateRef StdLibraryFunctionsChecker::RangeConstraint::apply(
1036 ProgramStateRef State, const CallEvent &Call, const Summary &Summary,
1037 CheckerContext &C) const {
1038 ConstraintManager &CM = C.getConstraintManager();
1039 SVal V = getArgSVal(Call, getArgNo());
1040 QualType T = Summary.getArgType(getArgNo());
1041
1042 if (auto N = V.getAs<NonLoc>()) {
1043 auto ExcludeRangeFromArg = [&](const llvm::APSInt &Min,
1044 const llvm::APSInt &Max) {
1045 State = CM.assumeInclusiveRange(State, *N, Min, Max, false);
1046 return static_cast<bool>(State);
1047 };
1048 // "OutOfRange R" is handled by excluding all ranges in R.
1049 // "WithinRange R" is treated as "OutOfRange [T_MIN, T_MAX] \ R".
1050 applyOnRange(negateKind(Kind), C.getSValBuilder().getBasicValueFactory(), T,
1051 ExcludeRangeFromArg);
1052 }
1053
1054 return State;
1055}
1056
1057void StdLibraryFunctionsChecker::RangeConstraint::describe(
1058 DescriptionKind DK, const CallEvent &Call, ProgramStateRef State,
1059 const Summary &Summary, llvm::raw_ostream &Out) const {
1060
1061 BasicValueFactory &BVF = getBVF(State);
1062 QualType T = Summary.getArgType(getArgNo());
1063
1064 Out << ((DK == Violation) ? "should be " : "is ");
1065 if (!Description.empty()) {
1066 Out << Description;
1067 } else {
1068 unsigned I = Ranges.size();
1069 if (Kind == WithinRange) {
1070 for (const std::pair<RangeInt, RangeInt> &R : Ranges) {
1071 appendInsideRangeDesc(BVF.getValue(R.first, T),
1072 BVF.getValue(R.second, T), T, BVF, Out);
1073 if (--I > 0)
1074 Out << " or ";
1075 }
1076 } else {
1077 for (const std::pair<RangeInt, RangeInt> &R : Ranges) {
1078 appendOutOfRangeDesc(BVF.getValue(R.first, T),
1079 BVF.getValue(R.second, T), T, BVF, Out);
1080 if (--I > 0)
1081 Out << " and ";
1082 }
1083 }
1084 }
1085}
1086
1087bool StdLibraryFunctionsChecker::RangeConstraint::describeArgumentValue(
1088 const CallEvent &Call, ProgramStateRef State, const Summary &Summary,
1089 llvm::raw_ostream &Out) const {
1090 unsigned int NRanges = 0;
1091 bool HaveAllRanges = true;
1092
1093 ProgramStateManager &Mgr = State->getStateManager();
1094 BasicValueFactory &BVF = Mgr.getSValBuilder().getBasicValueFactory();
1095 ConstraintManager &CM = Mgr.getConstraintManager();
1096 SVal V = getArgSVal(Call, getArgNo());
1097
1098 if (auto N = V.getAs<NonLoc>()) {
1099 if (const llvm::APSInt *Int = N->getAsInteger()) {
1100 Out << "is ";
1101 Out << *Int;
1102 return true;
1103 }
1104 QualType T = Summary.getArgType(getArgNo());
1105 SmallString<128> MoreInfo;
1106 llvm::raw_svector_ostream MoreInfoOs(MoreInfo);
1107 auto ApplyF = [&](const llvm::APSInt &Min, const llvm::APSInt &Max) {
1108 if (CM.assumeInclusiveRange(State, *N, Min, Max, true)) {
1109 if (NRanges > 0)
1110 MoreInfoOs << " or ";
1111 appendInsideRangeDesc(Min, Max, T, BVF, MoreInfoOs);
1112 ++NRanges;
1113 } else {
1114 HaveAllRanges = false;
1115 }
1116 return true;
1117 };
1118
1119 applyOnRange(Kind, BVF, T, ApplyF);
1120 assert(NRanges > 0);
1121 if (!HaveAllRanges || NRanges == 1) {
1122 Out << "is ";
1123 Out << MoreInfo;
1124 return true;
1125 }
1126 }
1127 return false;
1128}
1129
1130ProgramStateRef StdLibraryFunctionsChecker::ComparisonConstraint::apply(
1131 ProgramStateRef State, const CallEvent &Call, const Summary &Summary,
1132 CheckerContext &C) const {
1133
1134 ProgramStateManager &Mgr = State->getStateManager();
1135 SValBuilder &SVB = Mgr.getSValBuilder();
1136 QualType CondT = SVB.getConditionType();
1137 QualType T = Summary.getArgType(getArgNo());
1138 SVal V = getArgSVal(Call, getArgNo());
1139
1140 BinaryOperator::Opcode Op = getOpcode();
1141 ArgNo OtherArg = getOtherArgNo();
1142 SVal OtherV = getArgSVal(Call, OtherArg);
1143 QualType OtherT = Summary.getArgType(OtherArg);
1144 // Note: we avoid integral promotion for comparison.
1145 OtherV = SVB.evalCast(OtherV, T, OtherT);
1146 if (auto CompV = SVB.evalBinOp(State, Op, V, OtherV, CondT)
1147 .getAs<DefinedOrUnknownSVal>())
1148 State = State->assume(*CompV, true);
1149 return State;
1150}
1151
1152ProgramStateRef StdLibraryFunctionsChecker::NullnessConstraint::apply(
1153 ProgramStateRef State, const CallEvent &Call, const Summary &Summary,
1154 CheckerContext &C) const {
1155 SVal V = getArgSVal(Call, getArgNo());
1156 if (V.isUndef())
1157 return State;
1158
1159 DefinedOrUnknownSVal L = V.castAs<DefinedOrUnknownSVal>();
1160 if (!isa<Loc>(L))
1161 return State;
1162
1163 return State->assume(L, CannotBeNull);
1164}
1165
1166void StdLibraryFunctionsChecker::NullnessConstraint::describe(
1167 DescriptionKind DK, const CallEvent &Call, ProgramStateRef State,
1168 const Summary &Summary, llvm::raw_ostream &Out) const {
1169 assert(CannotBeNull &&
1170 "'describe' is not implemented when the value must be NULL");
1171 if (DK == Violation)
1172 Out << "should not be NULL";
1173 else
1174 Out << "is not NULL";
1175}
1176
1177bool StdLibraryFunctionsChecker::NullnessConstraint::describeArgumentValue(
1178 const CallEvent &Call, ProgramStateRef State, const Summary &Summary,
1179 llvm::raw_ostream &Out) const {
1180 assert(!CannotBeNull && "'describeArgumentValue' is not implemented when the "
1181 "value must be non-NULL");
1182 Out << "is NULL";
1183 return true;
1184}
1185
1186ProgramStateRef StdLibraryFunctionsChecker::BufferNullnessConstraint::apply(
1187 ProgramStateRef State, const CallEvent &Call, const Summary &Summary,
1188 CheckerContext &C) const {
1189 SVal V = getArgSVal(Call, getArgNo());
1190 if (V.isUndef())
1191 return State;
1192 DefinedOrUnknownSVal L = V.castAs<DefinedOrUnknownSVal>();
1193 if (!isa<Loc>(L))
1194 return State;
1195
1196 std::optional<DefinedOrUnknownSVal> SizeArg1 =
1197 getArgSVal(Call, SizeArg1N).getAs<DefinedOrUnknownSVal>();
1198 std::optional<DefinedOrUnknownSVal> SizeArg2;
1199 if (SizeArg2N)
1200 SizeArg2 = getArgSVal(Call, *SizeArg2N).getAs<DefinedOrUnknownSVal>();
1201
1202 auto IsArgZero = [State](std::optional<DefinedOrUnknownSVal> Val) {
1203 if (!Val)
1204 return false;
1205 auto [IsNonNull, IsNull] = State->assume(*Val);
1206 return IsNull && !IsNonNull;
1207 };
1208
1209 if (IsArgZero(SizeArg1) || IsArgZero(SizeArg2))
1210 return State;
1211
1212 return State->assume(L, CannotBeNull);
1213}
1214
1215void StdLibraryFunctionsChecker::BufferNullnessConstraint::describe(
1216 DescriptionKind DK, const CallEvent &Call, ProgramStateRef State,
1217 const Summary &Summary, llvm::raw_ostream &Out) const {
1218 assert(CannotBeNull &&
1219 "'describe' is not implemented when the buffer must be NULL");
1220 if (DK == Violation)
1221 Out << "should not be NULL";
1222 else
1223 Out << "is not NULL";
1224}
1225
1226bool StdLibraryFunctionsChecker::BufferNullnessConstraint::
1227 describeArgumentValue(const CallEvent &Call, ProgramStateRef State,
1228 const Summary &Summary,
1229 llvm::raw_ostream &Out) const {
1230 assert(!CannotBeNull && "'describeArgumentValue' is not implemented when the "
1231 "buffer must be non-NULL");
1232 Out << "is NULL";
1233 return true;
1234}
1235
1236ProgramStateRef StdLibraryFunctionsChecker::BufferSizeConstraint::apply(
1237 ProgramStateRef State, const CallEvent &Call, const Summary &Summary,
1238 CheckerContext &C) const {
1239 SValBuilder &SvalBuilder = C.getSValBuilder();
1240 // The buffer argument.
1241 SVal BufV = getArgSVal(Call, getArgNo());
1242
1243 // Get the size constraint.
1244 const SVal SizeV = [this, &State, &Call, &Summary, &SvalBuilder]() {
1245 if (ConcreteSize) {
1246 return SVal(SvalBuilder.makeIntVal(*ConcreteSize));
1247 }
1248 assert(SizeArgN && "The constraint must be either a concrete value or "
1249 "encoded in an argument.");
1250 // The size argument.
1251 SVal SizeV = getArgSVal(Call, *SizeArgN);
1252 // Multiply with another argument if given.
1253 if (SizeMultiplierArgN) {
1254 SVal SizeMulV = getArgSVal(Call, *SizeMultiplierArgN);
1255 SizeV = SvalBuilder.evalBinOp(State, BO_Mul, SizeV, SizeMulV,
1256 Summary.getArgType(*SizeArgN));
1257 }
1258 return SizeV;
1259 }();
1260
1261 // The dynamic size of the buffer argument, got from the analyzer engine.
1262 SVal BufDynSize = getDynamicExtentWithOffset(State, BufV);
1263
1264 SVal Feasible = SvalBuilder.evalBinOp(State, Op, SizeV, BufDynSize,
1265 SvalBuilder.getContext().BoolTy);
1266 if (auto F = Feasible.getAs<DefinedOrUnknownSVal>())
1267 return State->assume(*F, true);
1268
1269 // We can get here only if the size argument or the dynamic size is
1270 // undefined. But the dynamic size should never be undefined, only
1271 // unknown. So, here, the size of the argument is undefined, i.e. we
1272 // cannot apply the constraint. Actually, other checkers like
1273 // CallAndMessage should catch this situation earlier, because we call a
1274 // function with an uninitialized argument.
1275 llvm_unreachable("Size argument or the dynamic size is Undefined");
1276}
1277
1278void StdLibraryFunctionsChecker::BufferSizeConstraint::describe(
1279 DescriptionKind DK, const CallEvent &Call, ProgramStateRef State,
1280 const Summary &Summary, llvm::raw_ostream &Out) const {
1281 Out << ((DK == Violation) ? "should be " : "is ");
1282 Out << "a buffer with size equal to or greater than ";
1283 if (ConcreteSize) {
1284 Out << *ConcreteSize;
1285 } else if (SizeArgN) {
1286 Out << "the value of the ";
1287 printArgDesc(*SizeArgN, Out);
1288 printArgValueInfo(*SizeArgN, State, Call, Out);
1289 if (SizeMultiplierArgN) {
1290 Out << " times the ";
1291 printArgDesc(*SizeMultiplierArgN, Out);
1292 printArgValueInfo(*SizeMultiplierArgN, State, Call, Out);
1293 }
1294 }
1295}
1296
1297bool StdLibraryFunctionsChecker::BufferSizeConstraint::describeArgumentValue(
1298 const CallEvent &Call, ProgramStateRef State, const Summary &Summary,
1299 llvm::raw_ostream &Out) const {
1300 SVal BufV = getArgSVal(Call, getArgNo());
1301 SVal BufDynSize = getDynamicExtentWithOffset(State, BufV);
1302 if (const llvm::APSInt *Val =
1303 State->getStateManager().getSValBuilder().getKnownValue(State,
1304 BufDynSize)) {
1305 Out << "is a buffer with size " << *Val;
1306 return true;
1307 }
1308 return false;
1309}
1310
1311void StdLibraryFunctionsChecker::checkPreCall(const CallEvent &Call,
1312 CheckerContext &C) const {
1313 std::optional<Summary> FoundSummary = findFunctionSummary(Call, C);
1314 if (!FoundSummary)
1315 return;
1316
1317 const Summary &Summary = *FoundSummary;
1318 ProgramStateRef State = C.getState();
1319
1320 ProgramStateRef NewState = State;
1321 ExplodedNode *NewNode = C.getPredecessor();
1322 for (const ValueConstraintPtr &Constraint : Summary.getArgConstraints()) {
1323 ValueConstraintPtr NegatedConstraint = Constraint->negate();
1324 ProgramStateRef SuccessSt = Constraint->apply(NewState, Call, Summary, C);
1325 ProgramStateRef FailureSt =
1326 NegatedConstraint->apply(NewState, Call, Summary, C);
1327 // The argument constraint is not satisfied.
1328 if (FailureSt && !SuccessSt) {
1329 if (ExplodedNode *N = C.generateErrorNode(State, NewNode))
1330 reportBug(Call, N, Constraint.get(), NegatedConstraint.get(), Summary,
1331 C);
1332 break;
1333 }
1334 // We will apply the constraint even if we cannot reason about the
1335 // argument. This means both SuccessSt and FailureSt can be true. If we
1336 // weren't applying the constraint that would mean that symbolic
1337 // execution continues on a code whose behaviour is undefined.
1338 assert(SuccessSt);
1339 NewState = SuccessSt;
1340 if (NewState != State) {
1341 SmallString<128> Msg;
1342 llvm::raw_svector_ostream Os(Msg);
1343 Os << "Assuming that the ";
1344 printArgDesc(Constraint->getArgNo(), Os);
1345 Os << " to '";
1346 Os << getFunctionName(Call);
1347 Os << "' ";
1348 Constraint->describe(ValueConstraint::Assumption, Call, NewState, Summary,
1349 Os);
1350 const auto ArgSVal = Call.getArgSVal(Constraint->getArgNo());
1351 NewNode = C.addTransition(
1352 NewState, NewNode,
1353 C.getNoteTag([Msg = std::move(Msg), ArgSVal](
1354 PathSensitiveBugReport &BR, llvm::raw_ostream &OS) {
1355 if (BR.isInteresting(ArgSVal))
1356 OS << Msg;
1357 }));
1358 }
1359 }
1360}
1361
1362void StdLibraryFunctionsChecker::checkPostCall(const CallEvent &Call,
1363 CheckerContext &C) const {
1364 std::optional<Summary> FoundSummary = findFunctionSummary(Call, C);
1365 if (!FoundSummary)
1366 return;
1367
1368 // Now apply the constraints.
1369 const Summary &Summary = *FoundSummary;
1370 ProgramStateRef State = C.getState();
1371 ExplodedNode *Node = C.getPredecessor();
1372
1373 // Apply case/branch specifications.
1374 for (const SummaryCase &Case : Summary.getCases()) {
1375 ProgramStateRef NewState = State;
1376 for (const ValueConstraintPtr &Constraint : Case.getConstraints()) {
1377 NewState = Constraint->apply(NewState, Call, Summary, C);
1378 if (!NewState)
1379 break;
1380 }
1381
1382 if (NewState)
1383 NewState = Case.getErrnoConstraint().apply(NewState, Call, Summary, C);
1384
1385 if (!NewState)
1386 continue;
1387
1388 // Here it's possible that NewState == State, e.g. when other checkers
1389 // already applied the same constraints (or stricter ones).
1390 // Still add these note tags, the other checker should add only its
1391 // specialized note tags. These general note tags are handled always by
1392 // StdLibraryFunctionsChecker.
1393
1394 ExplodedNode *Pred = Node;
1395 DeclarationName FunctionName =
1396 cast<NamedDecl>(Call.getDecl())->getDeclName();
1397
1398 std::string ErrnoNote = Case.getErrnoConstraint().describe(C);
1399 std::string CaseNote;
1400 if (Case.getNote().empty()) {
1401 if (!ErrnoNote.empty())
1402 ErrnoNote =
1403 llvm::formatv("After calling '{0}' {1}", FunctionName, ErrnoNote);
1404 } else {
1405 // Disable formatv() validation as the case note may not always have the
1406 // {0} placeholder for function name.
1407 CaseNote =
1408 llvm::formatv(false, Case.getNote().str().c_str(), FunctionName);
1409 }
1410 const SVal RV = Call.getReturnValue();
1411
1412 if (Summary.getInvalidationKd() == EvalCallAsPure) {
1413 // Do not expect that errno is interesting (the "pure" functions do not
1414 // affect it).
1415 if (!CaseNote.empty()) {
1416 const NoteTag *Tag = C.getNoteTag(
1417 [Node, CaseNote, RV](PathSensitiveBugReport &BR) -> std::string {
1418 // Try to omit the note if we know in advance which branch is
1419 // taken (this means, only one branch exists).
1420 // This check is performed inside the lambda, after other
1421 // (or this) checkers had a chance to add other successors.
1422 // Dereferencing the saved node object is valid because it's part
1423 // of a bug report call sequence.
1424 // FIXME: This check is not exact. We may be here after a state
1425 // split that was performed by another checker (and can not find
1426 // the successors). This is why this check is only used in the
1427 // EvalCallAsPure case.
1428 if (BR.isInteresting(RV) && Node->succ_size() > 1)
1429 return CaseNote;
1430 return "";
1431 });
1432 Pred = C.addTransition(NewState, Pred, Tag);
1433 }
1434 } else {
1435 if (!CaseNote.empty() || !ErrnoNote.empty()) {
1436 const NoteTag *Tag =
1437 C.getNoteTag([CaseNote, ErrnoNote,
1438 RV](PathSensitiveBugReport &BR) -> std::string {
1439 // If 'errno' is interesting, show the user a note about the case
1440 // (what happened at the function call) and about how 'errno'
1441 // causes the problem. ErrnoChecker sets the errno (but not RV) to
1442 // interesting.
1443 // If only the return value is interesting, show only the case
1444 // note.
1445 std::optional<Loc> ErrnoLoc =
1447 bool ErrnoImportant = !ErrnoNote.empty() && ErrnoLoc &&
1448 BR.isInteresting(ErrnoLoc->getAsRegion());
1449 if (ErrnoImportant) {
1450 BR.markNotInteresting(ErrnoLoc->getAsRegion());
1451 if (CaseNote.empty())
1452 return ErrnoNote;
1453 return llvm::formatv("{0}; {1}", CaseNote, ErrnoNote);
1454 } else {
1455 if (BR.isInteresting(RV))
1456 return CaseNote;
1457 }
1458 return "";
1459 });
1460 Pred = C.addTransition(NewState, Pred, Tag);
1461 }
1462 }
1463
1464 // Add the transition if no note tag was added.
1465 if (Pred == Node && NewState != State)
1466 C.addTransition(NewState);
1467 }
1468}
1469
1470bool StdLibraryFunctionsChecker::evalCall(const CallEvent &Call,
1471 CheckerContext &C) const {
1472 std::optional<Summary> FoundSummary = findFunctionSummary(Call, C);
1473 if (!FoundSummary)
1474 return false;
1475
1476 const Summary &Summary = *FoundSummary;
1477 switch (Summary.getInvalidationKd()) {
1478 case EvalCallAsPure: {
1479 ProgramStateRef State = C.getState();
1480 const auto *CE = cast<CallExpr>(Call.getOriginExpr());
1481 SVal V = C.getSValBuilder().conjureSymbolVal(Call, C.blockCount());
1482 State = State->BindExpr(CE, C.getStackFrame(), V);
1483
1484 C.addTransition(State);
1485
1486 return true;
1487 }
1488 case NoEvalCall:
1489 // Summary tells us to avoid performing eval::Call. The function is possibly
1490 // evaluated by another checker, or evaluated conservatively.
1491 return false;
1492 }
1493 llvm_unreachable("Unknown invalidation kind!");
1494}
1495
1496bool StdLibraryFunctionsChecker::Signature::matches(
1497 const FunctionDecl *FD) const {
1498 assert(!isInvalid());
1499 // Check the number of arguments.
1500 if (FD->param_size() != ArgTys.size())
1501 return false;
1502
1503 // The "restrict" keyword is illegal in C++, however, many libc
1504 // implementations use the "__restrict" compiler intrinsic in functions
1505 // prototypes. The "__restrict" keyword qualifies a type as a restricted type
1506 // even in C++.
1507 // In case of any non-C99 languages, we don't want to match based on the
1508 // restrict qualifier because we cannot know if the given libc implementation
1509 // qualifies the paramter type or not.
1510 auto RemoveRestrict = [&FD](QualType T) {
1511 if (!FD->getASTContext().getLangOpts().C99)
1513 return T;
1514 };
1515
1516 // Check the return type.
1517 if (!isIrrelevant(RetTy)) {
1518 QualType FDRetTy = RemoveRestrict(FD->getReturnType().getCanonicalType());
1519 if (RetTy != FDRetTy)
1520 return false;
1521 }
1522
1523 // Check the argument types.
1524 for (auto [Idx, ArgTy] : llvm::enumerate(ArgTys)) {
1525 if (isIrrelevant(ArgTy))
1526 continue;
1527 QualType FDArgTy =
1528 RemoveRestrict(FD->getParamDecl(Idx)->getType().getCanonicalType());
1529 if (ArgTy != FDArgTy)
1530 return false;
1531 }
1532
1533 return true;
1534}
1535
1536std::optional<StdLibraryFunctionsChecker::Summary>
1537StdLibraryFunctionsChecker::findFunctionSummary(const FunctionDecl *FD,
1538 CheckerContext &C) const {
1539 if (!FD)
1540 return std::nullopt;
1541
1542 initFunctionSummaries(C);
1543
1544 auto FSMI = FunctionSummaryMap.find(FD->getCanonicalDecl());
1545 if (FSMI == FunctionSummaryMap.end())
1546 return std::nullopt;
1547 return FSMI->second;
1548}
1549
1550std::optional<StdLibraryFunctionsChecker::Summary>
1551StdLibraryFunctionsChecker::findFunctionSummary(const CallEvent &Call,
1552 CheckerContext &C) const {
1553 const FunctionDecl *FD = dyn_cast_or_null<FunctionDecl>(Call.getDecl());
1554 if (!FD)
1555 return std::nullopt;
1556 return findFunctionSummary(FD, C);
1557}
1558
1559void StdLibraryFunctionsChecker::initFunctionSummaries(
1560 CheckerContext &C) const {
1561 if (SummariesInitialized)
1562 return;
1563 SummariesInitialized = true;
1564
1565 SValBuilder &SVB = C.getSValBuilder();
1566 BasicValueFactory &BVF = SVB.getBasicValueFactory();
1567 const ASTContext &ACtx = BVF.getContext();
1568 Preprocessor &PP = C.getPreprocessor();
1569
1570 // Helper class to lookup a type by its name.
1571 class LookupType {
1572 const ASTContext &ACtx;
1573
1574 public:
1575 LookupType(const ASTContext &ACtx) : ACtx(ACtx) {}
1576
1577 // Find the type. If not found then the optional is not set.
1578 std::optional<QualType> operator()(StringRef Name) {
1579 IdentifierInfo &II = ACtx.Idents.get(Name);
1580 auto LookupRes = ACtx.getTranslationUnitDecl()->lookup(&II);
1581 if (LookupRes.empty())
1582 return std::nullopt;
1583
1584 // Prioritize typedef declarations.
1585 // This is needed in case of C struct typedefs. E.g.:
1586 // typedef struct FILE FILE;
1587 // In this case, we have a RecordDecl 'struct FILE' with the name 'FILE'
1588 // and we have a TypedefDecl with the name 'FILE'.
1589 for (Decl *D : LookupRes)
1590 if (auto *TD = dyn_cast<TypedefNameDecl>(D))
1591 return ACtx.getCanonicalTypeDeclType(TD);
1592
1593 // Find the first TypeDecl.
1594 // There maybe cases when a function has the same name as a struct.
1595 // E.g. in POSIX: `struct stat` and the function `stat()`:
1596 // int stat(const char *restrict path, struct stat *restrict buf);
1597 for (Decl *D : LookupRes)
1598 if (auto *TD = dyn_cast<TypeDecl>(D))
1599 return ACtx.getCanonicalTypeDeclType(TD);
1600 return std::nullopt;
1601 }
1602 } lookupTy(ACtx);
1603
1604 // Below are auxiliary classes to handle optional types that we get as a
1605 // result of the lookup.
1606 class GetRestrictTy {
1607 const ASTContext &ACtx;
1608
1609 public:
1610 GetRestrictTy(const ASTContext &ACtx) : ACtx(ACtx) {}
1611 QualType operator()(QualType Ty) {
1612 return ACtx.getLangOpts().C99 ? ACtx.getRestrictType(Ty) : Ty;
1613 }
1614 std::optional<QualType> operator()(std::optional<QualType> Ty) {
1615 if (Ty)
1616 return operator()(*Ty);
1617 return std::nullopt;
1618 }
1619 } getRestrictTy(ACtx);
1620 class GetPointerTy {
1621 const ASTContext &ACtx;
1622
1623 public:
1624 GetPointerTy(const ASTContext &ACtx) : ACtx(ACtx) {}
1625 QualType operator()(QualType Ty) { return ACtx.getPointerType(Ty); }
1626 std::optional<QualType> operator()(std::optional<QualType> Ty) {
1627 if (Ty)
1628 return operator()(*Ty);
1629 return std::nullopt;
1630 }
1631 } getPointerTy(ACtx);
1632 class {
1633 public:
1634 std::optional<QualType> operator()(std::optional<QualType> Ty) {
1635 return Ty ? std::optional<QualType>(Ty->withConst()) : std::nullopt;
1636 }
1637 QualType operator()(QualType Ty) { return Ty.withConst(); }
1638 } getConstTy;
1639 class GetMaxValue {
1640 BasicValueFactory &BVF;
1641
1642 public:
1643 GetMaxValue(BasicValueFactory &BVF) : BVF(BVF) {}
1644 std::optional<RangeInt> operator()(QualType Ty) {
1645 return BVF.getMaxValue(Ty)->getLimitedValue();
1646 }
1647 std::optional<RangeInt> operator()(std::optional<QualType> Ty) {
1648 if (Ty) {
1649 return operator()(*Ty);
1650 }
1651 return std::nullopt;
1652 }
1653 } getMaxValue(BVF);
1654
1655 // These types are useful for writing specifications quickly,
1656 // New specifications should probably introduce more types.
1657 // Some types are hard to obtain from the AST, eg. "ssize_t".
1658 // In such cases it should be possible to provide multiple variants
1659 // of function summary for common cases (eg. ssize_t could be int or long
1660 // or long long, so three summary variants would be enough).
1661 // Of course, function variants are also useful for C++ overloads.
1662 const QualType VoidTy = ACtx.VoidTy;
1663 const QualType CharTy = ACtx.CharTy;
1664 const QualType WCharTy = ACtx.WCharTy;
1665 const QualType IntTy = ACtx.IntTy;
1666 const QualType UnsignedIntTy = ACtx.UnsignedIntTy;
1667 const QualType LongTy = ACtx.LongTy;
1668 const QualType SizeTyCanonTy = ACtx.getCanonicalSizeType();
1669
1670 const QualType VoidPtrTy = getPointerTy(VoidTy); // void *
1671 const QualType IntPtrTy = getPointerTy(IntTy); // int *
1672 const QualType UnsignedIntPtrTy =
1673 getPointerTy(UnsignedIntTy); // unsigned int *
1674 const QualType VoidPtrRestrictTy = getRestrictTy(VoidPtrTy);
1675 const QualType ConstVoidPtrTy =
1676 getPointerTy(getConstTy(VoidTy)); // const void *
1677 const QualType CharPtrTy = getPointerTy(CharTy); // char *
1678 const QualType CharPtrRestrictTy = getRestrictTy(CharPtrTy);
1679 const QualType ConstCharPtrTy =
1680 getPointerTy(getConstTy(CharTy)); // const char *
1681 const QualType ConstCharPtrRestrictTy = getRestrictTy(ConstCharPtrTy);
1682 const QualType Wchar_tPtrTy = getPointerTy(WCharTy); // wchar_t *
1683 const QualType ConstWchar_tPtrTy =
1684 getPointerTy(getConstTy(WCharTy)); // const wchar_t *
1685 const QualType ConstVoidPtrRestrictTy = getRestrictTy(ConstVoidPtrTy);
1686 const QualType SizePtrTy = getPointerTy(SizeTyCanonTy);
1687 const QualType SizePtrRestrictTy = getRestrictTy(SizePtrTy);
1688
1689 const RangeInt IntMax = BVF.getMaxValue(IntTy)->getLimitedValue();
1690 const RangeInt UnsignedIntMax =
1691 BVF.getMaxValue(UnsignedIntTy)->getLimitedValue();
1692 const RangeInt LongMax = BVF.getMaxValue(LongTy)->getLimitedValue();
1693 const RangeInt SizeMax = BVF.getMaxValue(SizeTyCanonTy)->getLimitedValue();
1694
1695 // Set UCharRangeMax to min of int or uchar maximum value.
1696 // The C standard states that the arguments of functions like isalpha must
1697 // be representable as an unsigned char. Their type is 'int', so the max
1698 // value of the argument should be min(UCharMax, IntMax). This just happen
1699 // to be true for commonly used and well tested instruction set
1700 // architectures, but not for others.
1701 const RangeInt UCharRangeMax =
1702 std::min(BVF.getMaxValue(ACtx.UnsignedCharTy)->getLimitedValue(), IntMax);
1703
1704 // Get platform dependent values of some macros.
1705 // Try our best to parse this from the Preprocessor, otherwise fallback to a
1706 // default value (what is found in a library header).
1707 const auto EOFv = tryExpandAsInteger("EOF", PP).value_or(-1);
1708 const auto AT_FDCWDv = tryExpandAsInteger("AT_FDCWD", PP).value_or(-100);
1709
1710 // Auxiliary class to aid adding summaries to the summary map.
1711 struct AddToFunctionSummaryMap {
1712 const ASTContext &ACtx;
1713 FunctionSummaryMapType &Map;
1714 bool DisplayLoadedSummaries;
1715 AddToFunctionSummaryMap(const ASTContext &ACtx, FunctionSummaryMapType &FSM,
1716 bool DisplayLoadedSummaries)
1717 : ACtx(ACtx), Map(FSM), DisplayLoadedSummaries(DisplayLoadedSummaries) {
1718 }
1719
1720 // Add a summary to a FunctionDecl found by lookup. The lookup is performed
1721 // by the given Name, and in the global scope. The summary will be attached
1722 // to the found FunctionDecl only if the signatures match.
1723 //
1724 // Returns true if the summary has been added, false otherwise.
1725 bool operator()(StringRef Name, Signature Sign, Summary Sum) {
1726 if (Sign.isInvalid())
1727 return false;
1728 IdentifierInfo &II = ACtx.Idents.get(Name);
1729 auto LookupRes = ACtx.getTranslationUnitDecl()->lookup(&II);
1730 if (LookupRes.empty())
1731 return false;
1732 for (Decl *D : LookupRes) {
1733 if (auto *FD = dyn_cast<FunctionDecl>(D)) {
1734 if (Sum.matchesAndSet(Sign, FD)) {
1735 auto Res = Map.insert({FD->getCanonicalDecl(), Sum});
1736 assert(Res.second && "Function already has a summary set!");
1737 (void)Res;
1738 if (DisplayLoadedSummaries) {
1739 llvm::errs() << "Loaded summary for: ";
1740 FD->print(llvm::errs());
1741 llvm::errs() << "\n";
1742 }
1743 return true;
1744 }
1745 }
1746 }
1747 return false;
1748 }
1749 // Add the same summary for different names with the Signature explicitly
1750 // given.
1751 void operator()(ArrayRef<StringRef> Names, Signature Sign, Summary Sum) {
1752 for (StringRef Name : Names)
1753 operator()(Name, Sign, Sum);
1754 }
1755 } addToFunctionSummaryMap(ACtx, FunctionSummaryMap, DisplayLoadedSummaries);
1756
1757 // Below are helpers functions to create the summaries.
1758 auto ArgumentCondition = [](ArgNo ArgN, RangeKind Kind, IntRangeVector Ranges,
1759 StringRef Desc = "") {
1760 return std::make_shared<RangeConstraint>(ArgN, Kind, Ranges, Desc);
1761 };
1762 auto BufferSize = [](auto... Args) {
1763 return std::make_shared<BufferSizeConstraint>(Args...);
1764 };
1765 struct {
1766 auto operator()(RangeKind Kind, IntRangeVector Ranges) {
1767 return std::make_shared<RangeConstraint>(Ret, Kind, Ranges);
1768 }
1769 auto operator()(BinaryOperator::Opcode Op, ArgNo OtherArgN) {
1770 return std::make_shared<ComparisonConstraint>(Ret, Op, OtherArgN);
1771 }
1772 } ReturnValueCondition;
1773 struct {
1774 auto operator()(RangeInt b, RangeInt e) {
1775 return IntRangeVector{std::pair<RangeInt, RangeInt>{b, e}};
1776 }
1777 auto operator()(RangeInt b, std::optional<RangeInt> e) {
1778 if (e)
1779 return IntRangeVector{std::pair<RangeInt, RangeInt>{b, *e}};
1780 return IntRangeVector{};
1781 }
1782 auto operator()(std::pair<RangeInt, RangeInt> i0,
1783 std::pair<RangeInt, std::optional<RangeInt>> i1) {
1784 if (i1.second)
1785 return IntRangeVector{i0, {i1.first, *(i1.second)}};
1786 return IntRangeVector{i0};
1787 }
1788 } Range;
1789 auto SingleValue = [](RangeInt v) {
1790 return IntRangeVector{std::pair<RangeInt, RangeInt>{v, v}};
1791 };
1792 auto LessThanOrEq = BO_LE;
1793 auto NotNull = [&](ArgNo ArgN) {
1794 return std::make_shared<NullnessConstraint>(ArgN);
1795 };
1796 auto IsNull = [&](ArgNo ArgN) {
1797 return std::make_shared<NullnessConstraint>(ArgN, false);
1798 };
1799 auto NotNullBuffer = [&](ArgNo ArgN, ArgNo SizeArg1N,
1800 std::optional<ArgNo> SizeArg2N = std::nullopt) {
1801 return std::make_shared<BufferNullnessConstraint>(ArgN, SizeArg1N,
1802 SizeArg2N);
1803 };
1804
1805 std::optional<QualType> FileTy = lookupTy("FILE");
1806 std::optional<QualType> FilePtrTy = getPointerTy(FileTy);
1807 std::optional<QualType> FilePtrRestrictTy = getRestrictTy(FilePtrTy);
1808
1809 std::optional<QualType> FPosTTy = lookupTy("fpos_t");
1810 std::optional<QualType> FPosTPtrTy = getPointerTy(FPosTTy);
1811 std::optional<QualType> ConstFPosTPtrTy = getPointerTy(getConstTy(FPosTTy));
1812 std::optional<QualType> FPosTPtrRestrictTy = getRestrictTy(FPosTPtrTy);
1813
1814 constexpr llvm::StringLiteral GenericSuccessMsg(
1815 "Assuming that '{0}' is successful");
1816 constexpr llvm::StringLiteral GenericFailureMsg("Assuming that '{0}' fails");
1817
1818 // We are finally ready to define specifications for all supported functions.
1819 //
1820 // Argument ranges should always cover all variants. If return value
1821 // is completely unknown, omit it from the respective range set.
1822 //
1823 // Every item in the list of range sets represents a particular
1824 // execution path the analyzer would need to explore once
1825 // the call is modeled - a new program state is constructed
1826 // for every range set, and each range line in the range set
1827 // corresponds to a specific constraint within this state.
1828
1829 // The isascii() family of functions.
1830 // The behavior is undefined if the value of the argument is not
1831 // representable as unsigned char or is not equal to EOF. See e.g. C99
1832 // 7.4.1.2 The isalpha function (p: 181-182).
1833 addToFunctionSummaryMap(
1834 "isalnum", Signature(ArgTypes{IntTy}, RetType{IntTy}),
1835 Summary(EvalCallAsPure)
1836 // Boils down to isupper() or islower() or isdigit().
1837 .Case({ArgumentCondition(0U, WithinRange,
1838 {{'0', '9'}, {'A', 'Z'}, {'a', 'z'}}),
1839 ReturnValueCondition(OutOfRange, SingleValue(0))},
1840 ErrnoIrrelevant, "Assuming the character is alphanumeric")
1841 // The locale-specific range.
1842 // No post-condition. We are completely unaware of
1843 // locale-specific return values.
1844 .Case({ArgumentCondition(0U, WithinRange, {{128, UCharRangeMax}})},
1845 ErrnoIrrelevant)
1846 .Case(
1847 {ArgumentCondition(
1848 0U, OutOfRange,
1849 {{'0', '9'}, {'A', 'Z'}, {'a', 'z'}, {128, UCharRangeMax}}),
1850 ReturnValueCondition(WithinRange, SingleValue(0))},
1851 ErrnoIrrelevant, "Assuming the character is non-alphanumeric")
1852 .ArgConstraint(ArgumentCondition(0U, WithinRange,
1853 {{EOFv, EOFv}, {0, UCharRangeMax}},
1854 "an unsigned char value or EOF")));
1855 addToFunctionSummaryMap(
1856 "isalpha", Signature(ArgTypes{IntTy}, RetType{IntTy}),
1857 Summary(EvalCallAsPure)
1858 .Case({ArgumentCondition(0U, WithinRange, {{'A', 'Z'}, {'a', 'z'}}),
1859 ReturnValueCondition(OutOfRange, SingleValue(0))},
1860 ErrnoIrrelevant, "Assuming the character is alphabetical")
1861 // The locale-specific range.
1862 .Case({ArgumentCondition(0U, WithinRange, {{128, UCharRangeMax}})},
1863 ErrnoIrrelevant)
1864 .Case({ArgumentCondition(
1865 0U, OutOfRange,
1866 {{'A', 'Z'}, {'a', 'z'}, {128, UCharRangeMax}}),
1867 ReturnValueCondition(WithinRange, SingleValue(0))},
1868 ErrnoIrrelevant, "Assuming the character is non-alphabetical"));
1869 addToFunctionSummaryMap(
1870 "isascii", Signature(ArgTypes{IntTy}, RetType{IntTy}),
1871 Summary(EvalCallAsPure)
1872 .Case({ArgumentCondition(0U, WithinRange, Range(0, 127)),
1873 ReturnValueCondition(OutOfRange, SingleValue(0))},
1874 ErrnoIrrelevant, "Assuming the character is an ASCII character")
1875 .Case({ArgumentCondition(0U, OutOfRange, Range(0, 127)),
1876 ReturnValueCondition(WithinRange, SingleValue(0))},
1877 ErrnoIrrelevant,
1878 "Assuming the character is not an ASCII character"));
1879 addToFunctionSummaryMap(
1880 "isblank", Signature(ArgTypes{IntTy}, RetType{IntTy}),
1881 Summary(EvalCallAsPure)
1882 .Case({ArgumentCondition(0U, WithinRange, {{'\t', '\t'}, {' ', ' '}}),
1883 ReturnValueCondition(OutOfRange, SingleValue(0))},
1884 ErrnoIrrelevant, "Assuming the character is a blank character")
1885 .Case({ArgumentCondition(0U, OutOfRange, {{'\t', '\t'}, {' ', ' '}}),
1886 ReturnValueCondition(WithinRange, SingleValue(0))},
1887 ErrnoIrrelevant,
1888 "Assuming the character is not a blank character"));
1889 addToFunctionSummaryMap(
1890 "iscntrl", Signature(ArgTypes{IntTy}, RetType{IntTy}),
1891 Summary(EvalCallAsPure)
1892 .Case({ArgumentCondition(0U, WithinRange, {{0, 32}, {127, 127}}),
1893 ReturnValueCondition(OutOfRange, SingleValue(0))},
1894 ErrnoIrrelevant,
1895 "Assuming the character is a control character")
1896 .Case({ArgumentCondition(0U, OutOfRange, {{0, 32}, {127, 127}}),
1897 ReturnValueCondition(WithinRange, SingleValue(0))},
1898 ErrnoIrrelevant,
1899 "Assuming the character is not a control character"));
1900 addToFunctionSummaryMap(
1901 "isdigit", Signature(ArgTypes{IntTy}, RetType{IntTy}),
1902 Summary(EvalCallAsPure)
1903 .Case({ArgumentCondition(0U, WithinRange, Range('0', '9')),
1904 ReturnValueCondition(OutOfRange, SingleValue(0))},
1905 ErrnoIrrelevant, "Assuming the character is a digit")
1906 .Case({ArgumentCondition(0U, OutOfRange, Range('0', '9')),
1907 ReturnValueCondition(WithinRange, SingleValue(0))},
1908 ErrnoIrrelevant, "Assuming the character is not a digit"));
1909 addToFunctionSummaryMap(
1910 "isgraph", Signature(ArgTypes{IntTy}, RetType{IntTy}),
1911 Summary(EvalCallAsPure)
1912 .Case({ArgumentCondition(0U, WithinRange, Range(33, 126)),
1913 ReturnValueCondition(OutOfRange, SingleValue(0))},
1914 ErrnoIrrelevant,
1915 "Assuming the character has graphical representation")
1916 .Case(
1917 {ArgumentCondition(0U, OutOfRange, Range(33, 126)),
1918 ReturnValueCondition(WithinRange, SingleValue(0))},
1919 ErrnoIrrelevant,
1920 "Assuming the character does not have graphical representation"));
1921 addToFunctionSummaryMap(
1922 "islower", Signature(ArgTypes{IntTy}, RetType{IntTy}),
1923 Summary(EvalCallAsPure)
1924 // Is certainly lowercase.
1925 .Case({ArgumentCondition(0U, WithinRange, Range('a', 'z')),
1926 ReturnValueCondition(OutOfRange, SingleValue(0))},
1927 ErrnoIrrelevant, "Assuming the character is a lowercase letter")
1928 // Is ascii but not lowercase.
1929 .Case({ArgumentCondition(0U, WithinRange, Range(0, 127)),
1930 ArgumentCondition(0U, OutOfRange, Range('a', 'z')),
1931 ReturnValueCondition(WithinRange, SingleValue(0))},
1932 ErrnoIrrelevant,
1933 "Assuming the character is not a lowercase letter")
1934 // The locale-specific range.
1935 .Case({ArgumentCondition(0U, WithinRange, {{128, UCharRangeMax}})},
1936 ErrnoIrrelevant)
1937 // Is not an unsigned char.
1938 .Case({ArgumentCondition(0U, OutOfRange, Range(0, UCharRangeMax)),
1939 ReturnValueCondition(WithinRange, SingleValue(0))},
1940 ErrnoIrrelevant));
1941 addToFunctionSummaryMap(
1942 "isprint", Signature(ArgTypes{IntTy}, RetType{IntTy}),
1943 Summary(EvalCallAsPure)
1944 .Case({ArgumentCondition(0U, WithinRange, Range(32, 126)),
1945 ReturnValueCondition(OutOfRange, SingleValue(0))},
1946 ErrnoIrrelevant, "Assuming the character is printable")
1947 .Case({ArgumentCondition(0U, OutOfRange, Range(32, 126)),
1948 ReturnValueCondition(WithinRange, SingleValue(0))},
1949 ErrnoIrrelevant, "Assuming the character is non-printable"));
1950 addToFunctionSummaryMap(
1951 "ispunct", Signature(ArgTypes{IntTy}, RetType{IntTy}),
1952 Summary(EvalCallAsPure)
1953 .Case({ArgumentCondition(
1954 0U, WithinRange,
1955 {{'!', '/'}, {':', '@'}, {'[', '`'}, {'{', '~'}}),
1956 ReturnValueCondition(OutOfRange, SingleValue(0))},
1957 ErrnoIrrelevant, "Assuming the character is a punctuation mark")
1958 .Case({ArgumentCondition(
1959 0U, OutOfRange,
1960 {{'!', '/'}, {':', '@'}, {'[', '`'}, {'{', '~'}}),
1961 ReturnValueCondition(WithinRange, SingleValue(0))},
1962 ErrnoIrrelevant,
1963 "Assuming the character is not a punctuation mark"));
1964 addToFunctionSummaryMap(
1965 "isspace", Signature(ArgTypes{IntTy}, RetType{IntTy}),
1966 Summary(EvalCallAsPure)
1967 // Space, '\f', '\n', '\r', '\t', '\v'.
1968 .Case({ArgumentCondition(0U, WithinRange, {{9, 13}, {' ', ' '}}),
1969 ReturnValueCondition(OutOfRange, SingleValue(0))},
1970 ErrnoIrrelevant,
1971 "Assuming the character is a whitespace character")
1972 // The locale-specific range.
1973 .Case({ArgumentCondition(0U, WithinRange, {{128, UCharRangeMax}})},
1974 ErrnoIrrelevant)
1975 .Case({ArgumentCondition(0U, OutOfRange,
1976 {{9, 13}, {' ', ' '}, {128, UCharRangeMax}}),
1977 ReturnValueCondition(WithinRange, SingleValue(0))},
1978 ErrnoIrrelevant,
1979 "Assuming the character is not a whitespace character"));
1980 addToFunctionSummaryMap(
1981 "isupper", Signature(ArgTypes{IntTy}, RetType{IntTy}),
1982 Summary(EvalCallAsPure)
1983 // Is certainly uppercase.
1984 .Case({ArgumentCondition(0U, WithinRange, Range('A', 'Z')),
1985 ReturnValueCondition(OutOfRange, SingleValue(0))},
1986 ErrnoIrrelevant,
1987 "Assuming the character is an uppercase letter")
1988 // The locale-specific range.
1989 .Case({ArgumentCondition(0U, WithinRange, {{128, UCharRangeMax}})},
1990 ErrnoIrrelevant)
1991 // Other.
1992 .Case({ArgumentCondition(0U, OutOfRange,
1993 {{'A', 'Z'}, {128, UCharRangeMax}}),
1994 ReturnValueCondition(WithinRange, SingleValue(0))},
1995 ErrnoIrrelevant,
1996 "Assuming the character is not an uppercase letter"));
1997 addToFunctionSummaryMap(
1998 "isxdigit", Signature(ArgTypes{IntTy}, RetType{IntTy}),
1999 Summary(EvalCallAsPure)
2000 .Case({ArgumentCondition(0U, WithinRange,
2001 {{'0', '9'}, {'A', 'F'}, {'a', 'f'}}),
2002 ReturnValueCondition(OutOfRange, SingleValue(0))},
2003 ErrnoIrrelevant,
2004 "Assuming the character is a hexadecimal digit")
2005 .Case({ArgumentCondition(0U, OutOfRange,
2006 {{'0', '9'}, {'A', 'F'}, {'a', 'f'}}),
2007 ReturnValueCondition(WithinRange, SingleValue(0))},
2008 ErrnoIrrelevant,
2009 "Assuming the character is not a hexadecimal digit"));
2010 addToFunctionSummaryMap(
2011 "toupper", Signature(ArgTypes{IntTy}, RetType{IntTy}),
2012 Summary(EvalCallAsPure)
2013 .ArgConstraint(ArgumentCondition(0U, WithinRange,
2014 {{EOFv, EOFv}, {0, UCharRangeMax}},
2015 "an unsigned char value or EOF")));
2016 addToFunctionSummaryMap(
2017 "tolower", Signature(ArgTypes{IntTy}, RetType{IntTy}),
2018 Summary(EvalCallAsPure)
2019 .ArgConstraint(ArgumentCondition(0U, WithinRange,
2020 {{EOFv, EOFv}, {0, UCharRangeMax}},
2021 "an unsigned char value or EOF")));
2022 addToFunctionSummaryMap(
2023 "toascii", Signature(ArgTypes{IntTy}, RetType{IntTy}),
2024 Summary(EvalCallAsPure)
2025 .ArgConstraint(ArgumentCondition(0U, WithinRange,
2026 {{EOFv, EOFv}, {0, UCharRangeMax}},
2027 "an unsigned char value or EOF")));
2028
2029 addToFunctionSummaryMap(
2030 "getchar", Signature(ArgTypes{}, RetType{IntTy}),
2031 Summary(NoEvalCall)
2032 .Case({ReturnValueCondition(WithinRange,
2033 {{EOFv, EOFv}, {0, UCharRangeMax}})},
2034 ErrnoIrrelevant));
2035
2036 // read()-like functions that never return more than buffer size.
2037 auto FreadSummary =
2038 Summary(NoEvalCall)
2039 .Case({ArgumentCondition(1U, WithinRange, Range(1, SizeMax)),
2040 ArgumentCondition(2U, WithinRange, Range(1, SizeMax)),
2041 ReturnValueCondition(BO_LT, ArgNo(2)),
2042 ReturnValueCondition(WithinRange, Range(0, SizeMax))},
2043 ErrnoNEZeroIrrelevant, GenericFailureMsg)
2044 .Case({ArgumentCondition(1U, WithinRange, Range(1, SizeMax)),
2045 ReturnValueCondition(BO_EQ, ArgNo(2)),
2046 ReturnValueCondition(WithinRange, Range(0, SizeMax))},
2047 ErrnoMustNotBeChecked, GenericSuccessMsg)
2048 .Case({ArgumentCondition(1U, WithinRange, SingleValue(0)),
2049 ReturnValueCondition(WithinRange, SingleValue(0))},
2050 ErrnoMustNotBeChecked,
2051 "Assuming that argument 'size' to '{0}' is 0")
2052 .ArgConstraint(NotNullBuffer(ArgNo(0), ArgNo(1), ArgNo(2)))
2053 .ArgConstraint(NotNull(ArgNo(3)))
2054 .ArgConstraint(BufferSize(/*Buffer=*/ArgNo(0), /*BufSize=*/ArgNo(1),
2055 /*BufSizeMultiplier=*/ArgNo(2)));
2056
2057 // size_t fread(void *restrict ptr, size_t size, size_t nitems,
2058 // FILE *restrict stream);
2059 addToFunctionSummaryMap("fread",
2060 Signature(ArgTypes{VoidPtrRestrictTy, SizeTyCanonTy,
2061 SizeTyCanonTy, FilePtrRestrictTy},
2062 RetType{SizeTyCanonTy}),
2063 FreadSummary);
2064 // size_t fwrite(const void *restrict ptr, size_t size, size_t nitems,
2065 // FILE *restrict stream);
2066 addToFunctionSummaryMap(
2067 "fwrite",
2068 Signature(ArgTypes{ConstVoidPtrRestrictTy, SizeTyCanonTy, SizeTyCanonTy,
2069 FilePtrRestrictTy},
2070 RetType{SizeTyCanonTy}),
2071 FreadSummary);
2072
2073 std::optional<QualType> Ssize_tTy = lookupTy("ssize_t");
2074 std::optional<RangeInt> Ssize_tMax = getMaxValue(Ssize_tTy);
2075
2076 auto ReadSummary =
2077 Summary(NoEvalCall)
2078 .Case({ReturnValueCondition(LessThanOrEq, ArgNo(2)),
2079 ReturnValueCondition(WithinRange, Range(-1, Ssize_tMax))},
2080 ErrnoIrrelevant);
2081
2082 // FIXME these are actually defined by POSIX and not by the C standard, we
2083 // should handle them together with the rest of the POSIX functions.
2084 // ssize_t read(int fildes, void *buf, size_t nbyte);
2085 addToFunctionSummaryMap(
2086 "read",
2087 Signature(ArgTypes{IntTy, VoidPtrTy, SizeTyCanonTy}, RetType{Ssize_tTy}),
2088 ReadSummary);
2089 // ssize_t write(int fildes, const void *buf, size_t nbyte);
2090 addToFunctionSummaryMap(
2091 "write",
2092 Signature(ArgTypes{IntTy, ConstVoidPtrTy, SizeTyCanonTy},
2093 RetType{Ssize_tTy}),
2094 ReadSummary);
2095
2096 auto GetLineSummary =
2097 Summary(NoEvalCall)
2098 .Case({ReturnValueCondition(WithinRange,
2099 Range({-1, -1}, {1, Ssize_tMax}))},
2100 ErrnoIrrelevant);
2101
2102 QualType CharPtrPtrRestrictTy = getRestrictTy(getPointerTy(CharPtrTy));
2103
2104 // getline()-like functions either fail or read at least the delimiter.
2105 // FIXME these are actually defined by POSIX and not by the C standard, we
2106 // should handle them together with the rest of the POSIX functions.
2107 // ssize_t getline(char **restrict lineptr, size_t *restrict n,
2108 // FILE *restrict stream);
2109 addToFunctionSummaryMap(
2110 "getline",
2111 Signature(
2112 ArgTypes{CharPtrPtrRestrictTy, SizePtrRestrictTy, FilePtrRestrictTy},
2113 RetType{Ssize_tTy}),
2114 GetLineSummary);
2115 // ssize_t getdelim(char **restrict lineptr, size_t *restrict n,
2116 // int delimiter, FILE *restrict stream);
2117 addToFunctionSummaryMap(
2118 "getdelim",
2119 Signature(ArgTypes{CharPtrPtrRestrictTy, SizePtrRestrictTy, IntTy,
2120 FilePtrRestrictTy},
2121 RetType{Ssize_tTy}),
2122 GetLineSummary);
2123
2124 {
2125 Summary GetenvSummary =
2126 Summary(NoEvalCall)
2127 .ArgConstraint(NotNull(ArgNo(0)))
2128 .Case({NotNull(Ret)}, ErrnoIrrelevant,
2129 "Assuming the environment variable exists");
2130 // In untrusted environments the envvar might not exist.
2131 if (!ShouldAssumeControlledEnvironment)
2132 GetenvSummary.Case({NotNull(Ret)->negate()}, ErrnoIrrelevant,
2133 "Assuming the environment variable does not exist");
2134
2135 // char *getenv(const char *name);
2136 addToFunctionSummaryMap(
2137 "getenv", Signature(ArgTypes{ConstCharPtrTy}, RetType{CharPtrTy}),
2138 std::move(GetenvSummary));
2139 }
2140
2141 if (!ModelPOSIX) {
2142 // Without POSIX use of 'errno' is not specified (in these cases).
2143 // Add these functions without 'errno' checks.
2144 addToFunctionSummaryMap(
2145 {"getc", "fgetc"}, Signature(ArgTypes{FilePtrTy}, RetType{IntTy}),
2146 Summary(NoEvalCall)
2147 .Case({ReturnValueCondition(WithinRange,
2148 {{EOFv, EOFv}, {0, UCharRangeMax}})},
2149 ErrnoIrrelevant)
2150 .ArgConstraint(NotNull(ArgNo(0))));
2151 } else {
2152 const auto ReturnsZeroOrMinusOne =
2153 ConstraintSet{ReturnValueCondition(WithinRange, Range(-1, 0))};
2154 const auto ReturnsZero =
2155 ConstraintSet{ReturnValueCondition(WithinRange, SingleValue(0))};
2156 const auto ReturnsMinusOne =
2157 ConstraintSet{ReturnValueCondition(WithinRange, SingleValue(-1))};
2158 const auto ReturnsEOF =
2159 ConstraintSet{ReturnValueCondition(WithinRange, SingleValue(EOFv))};
2160 const auto ReturnsNonnegative =
2161 ConstraintSet{ReturnValueCondition(WithinRange, Range(0, IntMax))};
2162 const auto ReturnsNonZero =
2163 ConstraintSet{ReturnValueCondition(OutOfRange, SingleValue(0))};
2164 const auto ReturnsFileDescriptor =
2165 ConstraintSet{ReturnValueCondition(WithinRange, Range(-1, IntMax))};
2166 const auto &ReturnsValidFileDescriptor = ReturnsNonnegative;
2167
2168 auto ValidFileDescriptorOrAtFdcwd = [&](ArgNo ArgN) {
2169 return std::make_shared<RangeConstraint>(
2170 ArgN, WithinRange, Range({AT_FDCWDv, AT_FDCWDv}, {0, IntMax}),
2171 "a valid file descriptor or AT_FDCWD");
2172 };
2173
2174 // FILE *fopen(const char *restrict pathname, const char *restrict mode);
2175 addToFunctionSummaryMap(
2176 "fopen",
2177 Signature(ArgTypes{ConstCharPtrRestrictTy, ConstCharPtrRestrictTy},
2178 RetType{FilePtrTy}),
2179 Summary(NoEvalCall)
2180 .Case({NotNull(Ret)}, ErrnoMustNotBeChecked, GenericSuccessMsg)
2181 .Case({IsNull(Ret)}, ErrnoNEZeroIrrelevant, GenericFailureMsg)
2182 .ArgConstraint(NotNull(ArgNo(0)))
2183 .ArgConstraint(NotNull(ArgNo(1))));
2184
2185 // FILE *fdopen(int fd, const char *mode);
2186 addToFunctionSummaryMap(
2187 "fdopen",
2188 Signature(ArgTypes{IntTy, ConstCharPtrTy}, RetType{FilePtrTy}),
2189 Summary(NoEvalCall)
2190 .Case({NotNull(Ret)}, ErrnoMustNotBeChecked, GenericSuccessMsg)
2191 .Case({IsNull(Ret)}, ErrnoNEZeroIrrelevant, GenericFailureMsg)
2192 .ArgConstraint(ArgumentCondition(0, WithinRange, Range(0, IntMax)))
2193 .ArgConstraint(NotNull(ArgNo(1))));
2194
2195 // FILE *tmpfile(void);
2196 addToFunctionSummaryMap(
2197 "tmpfile", Signature(ArgTypes{}, RetType{FilePtrTy}),
2198 Summary(NoEvalCall)
2199 .Case({NotNull(Ret)}, ErrnoMustNotBeChecked, GenericSuccessMsg)
2200 .Case({IsNull(Ret)}, ErrnoNEZeroIrrelevant, GenericFailureMsg));
2201
2202 // FILE *freopen(const char *restrict pathname, const char *restrict mode,
2203 // FILE *restrict stream);
2204 addToFunctionSummaryMap(
2205 "freopen",
2206 Signature(ArgTypes{ConstCharPtrRestrictTy, ConstCharPtrRestrictTy,
2207 FilePtrRestrictTy},
2208 RetType{FilePtrTy}),
2209 Summary(NoEvalCall)
2210 .Case({ReturnValueCondition(BO_EQ, ArgNo(2))},
2211 ErrnoMustNotBeChecked, GenericSuccessMsg)
2212 .Case({IsNull(Ret)}, ErrnoNEZeroIrrelevant, GenericFailureMsg)
2213 .ArgConstraint(NotNull(ArgNo(1)))
2214 .ArgConstraint(NotNull(ArgNo(2))));
2215
2216 // FILE *popen(const char *command, const char *type);
2217 addToFunctionSummaryMap(
2218 "popen",
2219 Signature(ArgTypes{ConstCharPtrTy, ConstCharPtrTy}, RetType{FilePtrTy}),
2220 Summary(NoEvalCall)
2221 .Case({NotNull(Ret)}, ErrnoMustNotBeChecked, GenericSuccessMsg)
2222 .Case({IsNull(Ret)}, ErrnoNEZeroIrrelevant, GenericFailureMsg)
2223 .ArgConstraint(NotNull(ArgNo(0)))
2224 .ArgConstraint(NotNull(ArgNo(1))));
2225
2226 // int fclose(FILE *stream);
2227 addToFunctionSummaryMap(
2228 "fclose", Signature(ArgTypes{FilePtrTy}, RetType{IntTy}),
2229 Summary(NoEvalCall)
2230 .Case(ReturnsZero, ErrnoMustNotBeChecked, GenericSuccessMsg)
2231 .Case(ReturnsEOF, ErrnoNEZeroIrrelevant, GenericFailureMsg)
2232 .ArgConstraint(NotNull(ArgNo(0))));
2233
2234 // int pclose(FILE *stream);
2235 addToFunctionSummaryMap(
2236 "pclose", Signature(ArgTypes{FilePtrTy}, RetType{IntTy}),
2237 Summary(NoEvalCall)
2238 .Case({ReturnValueCondition(WithinRange, {{0, IntMax}})},
2239 ErrnoMustNotBeChecked, GenericSuccessMsg)
2240 .Case(ReturnsMinusOne, ErrnoNEZeroIrrelevant, GenericFailureMsg)
2241 .ArgConstraint(NotNull(ArgNo(0))));
2242
2243 std::optional<QualType> Off_tTy = lookupTy("off_t");
2244 std::optional<RangeInt> Off_tMax = getMaxValue(Off_tTy);
2245
2246 // int fgetc(FILE *stream);
2247 // 'getc' is the same as 'fgetc' but may be a macro
2248 addToFunctionSummaryMap(
2249 {"getc", "fgetc"}, Signature(ArgTypes{FilePtrTy}, RetType{IntTy}),
2250 Summary(NoEvalCall)
2251 .Case({ReturnValueCondition(WithinRange, {{0, UCharRangeMax}})},
2252 ErrnoMustNotBeChecked, GenericSuccessMsg)
2253 .Case({ReturnValueCondition(WithinRange, SingleValue(EOFv))},
2254 ErrnoIrrelevant, GenericFailureMsg)
2255 .ArgConstraint(NotNull(ArgNo(0))));
2256
2257 // int fputc(int c, FILE *stream);
2258 // 'putc' is the same as 'fputc' but may be a macro
2259 addToFunctionSummaryMap(
2260 {"putc", "fputc"},
2261 Signature(ArgTypes{IntTy, FilePtrTy}, RetType{IntTy}),
2262 Summary(NoEvalCall)
2263 .Case({ArgumentCondition(0, WithinRange, Range(0, UCharRangeMax)),
2264 ReturnValueCondition(BO_EQ, ArgNo(0))},
2265 ErrnoMustNotBeChecked, GenericSuccessMsg)
2266 .Case({ArgumentCondition(0, OutOfRange, Range(0, UCharRangeMax)),
2267 ReturnValueCondition(WithinRange, Range(0, UCharRangeMax))},
2268 ErrnoMustNotBeChecked, GenericSuccessMsg)
2269 .Case({ReturnValueCondition(WithinRange, SingleValue(EOFv))},
2270 ErrnoNEZeroIrrelevant, GenericFailureMsg)
2271 .ArgConstraint(NotNull(ArgNo(1))));
2272
2273 // char *fgets(char *restrict s, int n, FILE *restrict stream);
2274 addToFunctionSummaryMap(
2275 "fgets",
2276 Signature(ArgTypes{CharPtrRestrictTy, IntTy, FilePtrRestrictTy},
2277 RetType{CharPtrTy}),
2278 Summary(NoEvalCall)
2279 .Case({NotNull(Ret), ReturnValueCondition(BO_EQ, ArgNo(0))},
2280 ErrnoMustNotBeChecked, GenericSuccessMsg)
2281 .Case({IsNull(Ret)}, ErrnoIrrelevant, GenericFailureMsg)
2282 .ArgConstraint(NotNull(ArgNo(0)))
2283 .ArgConstraint(ArgumentCondition(1, WithinRange, Range(0, IntMax)))
2284 .ArgConstraint(
2285 BufferSize(/*Buffer=*/ArgNo(0), /*BufSize=*/ArgNo(1)))
2286 .ArgConstraint(NotNull(ArgNo(2))));
2287
2288 // int fputs(const char *restrict s, FILE *restrict stream);
2289 addToFunctionSummaryMap(
2290 "fputs",
2291 Signature(ArgTypes{ConstCharPtrRestrictTy, FilePtrRestrictTy},
2292 RetType{IntTy}),
2293 Summary(NoEvalCall)
2294 .Case(ReturnsNonnegative, ErrnoMustNotBeChecked, GenericSuccessMsg)
2295 .Case({ReturnValueCondition(WithinRange, SingleValue(EOFv))},
2296 ErrnoNEZeroIrrelevant, GenericFailureMsg)
2297 .ArgConstraint(NotNull(ArgNo(0)))
2298 .ArgConstraint(NotNull(ArgNo(1))));
2299
2300 // int ungetc(int c, FILE *stream);
2301 addToFunctionSummaryMap(
2302 "ungetc", Signature(ArgTypes{IntTy, FilePtrTy}, RetType{IntTy}),
2303 Summary(NoEvalCall)
2304 .Case({ReturnValueCondition(BO_EQ, ArgNo(0)),
2305 ArgumentCondition(0, WithinRange, {{0, UCharRangeMax}})},
2306 ErrnoMustNotBeChecked, GenericSuccessMsg)
2307 .Case({ReturnValueCondition(WithinRange, SingleValue(EOFv)),
2308 ArgumentCondition(0, WithinRange, SingleValue(EOFv))},
2309 ErrnoNEZeroIrrelevant,
2310 "Assuming that 'ungetc' fails because EOF was passed as "
2311 "character")
2312 .Case({ReturnValueCondition(WithinRange, SingleValue(EOFv)),
2313 ArgumentCondition(0, WithinRange, {{0, UCharRangeMax}})},
2314 ErrnoNEZeroIrrelevant, GenericFailureMsg)
2315 .ArgConstraint(ArgumentCondition(
2316 0, WithinRange, {{EOFv, EOFv}, {0, UCharRangeMax}}))
2317 .ArgConstraint(NotNull(ArgNo(1))));
2318
2319 // int fseek(FILE *stream, long offset, int whence);
2320 // FIXME: It can be possible to get the 'SEEK_' values (like EOFv) and use
2321 // these for condition of arg 2.
2322 // Now the range [0,2] is used (the `SEEK_*` constants are usually 0,1,2).
2323 addToFunctionSummaryMap(
2324 "fseek", Signature(ArgTypes{FilePtrTy, LongTy, IntTy}, RetType{IntTy}),
2325 Summary(NoEvalCall)
2326 .Case(ReturnsZero, ErrnoMustNotBeChecked, GenericSuccessMsg)
2327 .Case(ReturnsMinusOne, ErrnoNEZeroIrrelevant, GenericFailureMsg)
2328 .ArgConstraint(NotNull(ArgNo(0)))
2329 .ArgConstraint(ArgumentCondition(2, WithinRange, {{0, 2}})));
2330
2331 // int fseeko(FILE *stream, off_t offset, int whence);
2332 addToFunctionSummaryMap(
2333 "fseeko",
2334 Signature(ArgTypes{FilePtrTy, Off_tTy, IntTy}, RetType{IntTy}),
2335 Summary(NoEvalCall)
2336 .Case(ReturnsZero, ErrnoMustNotBeChecked, GenericSuccessMsg)
2337 .Case(ReturnsMinusOne, ErrnoNEZeroIrrelevant, GenericFailureMsg)
2338 .ArgConstraint(NotNull(ArgNo(0)))
2339 .ArgConstraint(ArgumentCondition(2, WithinRange, {{0, 2}})));
2340
2341 // int fgetpos(FILE *restrict stream, fpos_t *restrict pos);
2342 // From 'The Open Group Base Specifications Issue 7, 2018 edition':
2343 // "The fgetpos() function shall not change the setting of errno if
2344 // successful."
2345 addToFunctionSummaryMap(
2346 "fgetpos",
2347 Signature(ArgTypes{FilePtrRestrictTy, FPosTPtrRestrictTy},
2348 RetType{IntTy}),
2349 Summary(NoEvalCall)
2350 .Case(ReturnsZero, ErrnoUnchanged, GenericSuccessMsg)
2351 .Case(ReturnsNonZero, ErrnoNEZeroIrrelevant, GenericFailureMsg)
2352 .ArgConstraint(NotNull(ArgNo(0)))
2353 .ArgConstraint(NotNull(ArgNo(1))));
2354
2355 // int fsetpos(FILE *stream, const fpos_t *pos);
2356 // From 'The Open Group Base Specifications Issue 7, 2018 edition':
2357 // "The fsetpos() function shall not change the setting of errno if
2358 // successful."
2359 addToFunctionSummaryMap(
2360 "fsetpos",
2361 Signature(ArgTypes{FilePtrTy, ConstFPosTPtrTy}, RetType{IntTy}),
2362 Summary(NoEvalCall)
2363 .Case(ReturnsZero, ErrnoUnchanged, GenericSuccessMsg)
2364 .Case(ReturnsNonZero, ErrnoNEZeroIrrelevant, GenericFailureMsg)
2365 .ArgConstraint(NotNull(ArgNo(0)))
2366 .ArgConstraint(NotNull(ArgNo(1))));
2367
2368 // int fflush(FILE *stream);
2369 addToFunctionSummaryMap(
2370 "fflush", Signature(ArgTypes{FilePtrTy}, RetType{IntTy}),
2371 Summary(NoEvalCall)
2372 .Case(ReturnsZero, ErrnoMustNotBeChecked, GenericSuccessMsg)
2373 .Case(ReturnsEOF, ErrnoNEZeroIrrelevant, GenericFailureMsg));
2374
2375 // long ftell(FILE *stream);
2376 // From 'The Open Group Base Specifications Issue 7, 2018 edition':
2377 // "The ftell() function shall not change the setting of errno if
2378 // successful."
2379 addToFunctionSummaryMap(
2380 "ftell", Signature(ArgTypes{FilePtrTy}, RetType{LongTy}),
2381 Summary(NoEvalCall)
2382 .Case({ReturnValueCondition(WithinRange, Range(0, LongMax))},
2383 ErrnoUnchanged, GenericSuccessMsg)
2384 .Case(ReturnsMinusOne, ErrnoNEZeroIrrelevant, GenericFailureMsg)
2385 .ArgConstraint(NotNull(ArgNo(0))));
2386
2387 // off_t ftello(FILE *stream);
2388 addToFunctionSummaryMap(
2389 "ftello", Signature(ArgTypes{FilePtrTy}, RetType{Off_tTy}),
2390 Summary(NoEvalCall)
2391 .Case({ReturnValueCondition(WithinRange, Range(0, Off_tMax))},
2392 ErrnoMustNotBeChecked, GenericSuccessMsg)
2393 .Case(ReturnsMinusOne, ErrnoNEZeroIrrelevant, GenericFailureMsg)
2394 .ArgConstraint(NotNull(ArgNo(0))));
2395
2396 // int fileno(FILE *stream);
2397 // According to POSIX 'fileno' may fail and set 'errno'.
2398 // But in Linux it may fail only if the specified file pointer is invalid.
2399 // At many places 'fileno' is used without check for failure and a failure
2400 // case here would produce a large amount of likely false positive warnings.
2401 // To avoid this, we assume here that it does not fail.
2402 addToFunctionSummaryMap(
2403 "fileno", Signature(ArgTypes{FilePtrTy}, RetType{IntTy}),
2404 Summary(NoEvalCall)
2405 .Case(ReturnsValidFileDescriptor, ErrnoUnchanged, GenericSuccessMsg)
2406 .ArgConstraint(NotNull(ArgNo(0))));
2407
2408 // void rewind(FILE *stream);
2409 // This function indicates error only by setting of 'errno'.
2410 addToFunctionSummaryMap("rewind",
2411 Signature(ArgTypes{FilePtrTy}, RetType{VoidTy}),
2412 Summary(NoEvalCall)
2413 .Case({}, ErrnoMustBeChecked)
2414 .ArgConstraint(NotNull(ArgNo(0))));
2415
2416 // void clearerr(FILE *stream);
2417 addToFunctionSummaryMap(
2418 "clearerr", Signature(ArgTypes{FilePtrTy}, RetType{VoidTy}),
2419 Summary(NoEvalCall).ArgConstraint(NotNull(ArgNo(0))));
2420
2421 // int feof(FILE *stream);
2422 addToFunctionSummaryMap(
2423 "feof", Signature(ArgTypes{FilePtrTy}, RetType{IntTy}),
2424 Summary(NoEvalCall).ArgConstraint(NotNull(ArgNo(0))));
2425
2426 // int ferror(FILE *stream);
2427 addToFunctionSummaryMap(
2428 "ferror", Signature(ArgTypes{FilePtrTy}, RetType{IntTy}),
2429 Summary(NoEvalCall).ArgConstraint(NotNull(ArgNo(0))));
2430
2431 // long a64l(const char *str64);
2432 addToFunctionSummaryMap(
2433 "a64l", Signature(ArgTypes{ConstCharPtrTy}, RetType{LongTy}),
2434 Summary(NoEvalCall).ArgConstraint(NotNull(ArgNo(0))));
2435
2436 // char *l64a(long value);
2437 addToFunctionSummaryMap("l64a",
2438 Signature(ArgTypes{LongTy}, RetType{CharPtrTy}),
2439 Summary(NoEvalCall)
2440 .ArgConstraint(ArgumentCondition(
2441 0, WithinRange, Range(0, LongMax))));
2442
2443 // int open(const char *path, int oflag, ...);
2444 addToFunctionSummaryMap(
2445 "open", Signature(ArgTypes{ConstCharPtrTy, IntTy}, RetType{IntTy}),
2446 Summary(NoEvalCall)
2447 .Case(ReturnsValidFileDescriptor, ErrnoMustNotBeChecked,
2448 GenericSuccessMsg)
2449 .Case(ReturnsMinusOne, ErrnoNEZeroIrrelevant, GenericFailureMsg)
2450 .ArgConstraint(NotNull(ArgNo(0))));
2451
2452 // int openat(int fd, const char *path, int oflag, ...);
2453 addToFunctionSummaryMap(
2454 "openat",
2455 Signature(ArgTypes{IntTy, ConstCharPtrTy, IntTy}, RetType{IntTy}),
2456 Summary(NoEvalCall)
2457 .Case(ReturnsValidFileDescriptor, ErrnoMustNotBeChecked,
2458 GenericSuccessMsg)
2459 .Case(ReturnsMinusOne, ErrnoNEZeroIrrelevant, GenericFailureMsg)
2460 .ArgConstraint(ValidFileDescriptorOrAtFdcwd(ArgNo(0)))
2461 .ArgConstraint(NotNull(ArgNo(1))));
2462
2463 // int access(const char *pathname, int amode);
2464 addToFunctionSummaryMap(
2465 "access", Signature(ArgTypes{ConstCharPtrTy, IntTy}, RetType{IntTy}),
2466 Summary(NoEvalCall)
2467 .Case(ReturnsZero, ErrnoMustNotBeChecked, GenericSuccessMsg)
2468 .Case(ReturnsMinusOne, ErrnoNEZeroIrrelevant, GenericFailureMsg)
2469 .ArgConstraint(NotNull(ArgNo(0))));
2470
2471 // int faccessat(int dirfd, const char *pathname, int mode, int flags);
2472 addToFunctionSummaryMap(
2473 "faccessat",
2474 Signature(ArgTypes{IntTy, ConstCharPtrTy, IntTy, IntTy},
2475 RetType{IntTy}),
2476 Summary(NoEvalCall)
2477 .Case(ReturnsZero, ErrnoMustNotBeChecked, GenericSuccessMsg)
2478 .Case(ReturnsMinusOne, ErrnoNEZeroIrrelevant, GenericFailureMsg)
2479 .ArgConstraint(ValidFileDescriptorOrAtFdcwd(ArgNo(0)))
2480 .ArgConstraint(NotNull(ArgNo(1))));
2481
2482 // int dup(int fildes);
2483 addToFunctionSummaryMap(
2484 "dup", Signature(ArgTypes{IntTy}, RetType{IntTy}),
2485 Summary(NoEvalCall)
2486 .Case(ReturnsValidFileDescriptor, ErrnoMustNotBeChecked,
2487 GenericSuccessMsg)
2488 .Case(ReturnsMinusOne, ErrnoNEZeroIrrelevant, GenericFailureMsg)
2489 .ArgConstraint(
2490 ArgumentCondition(0, WithinRange, Range(0, IntMax))));
2491
2492 // int dup2(int fildes1, int filedes2);
2493 addToFunctionSummaryMap(
2494 "dup2", Signature(ArgTypes{IntTy, IntTy}, RetType{IntTy}),
2495 Summary(NoEvalCall)
2496 .Case(ReturnsValidFileDescriptor, ErrnoMustNotBeChecked,
2497 GenericSuccessMsg)
2498 .Case(ReturnsMinusOne, ErrnoNEZeroIrrelevant, GenericFailureMsg)
2499 .ArgConstraint(ArgumentCondition(0, WithinRange, Range(0, IntMax)))
2500 .ArgConstraint(
2501 ArgumentCondition(1, WithinRange, Range(0, IntMax))));
2502
2503 // int fdatasync(int fildes);
2504 addToFunctionSummaryMap(
2505 "fdatasync", Signature(ArgTypes{IntTy}, RetType{IntTy}),
2506 Summary(NoEvalCall)
2507 .Case(ReturnsZero, ErrnoMustNotBeChecked, GenericSuccessMsg)
2508 .Case(ReturnsMinusOne, ErrnoNEZeroIrrelevant, GenericFailureMsg)
2509 .ArgConstraint(
2510 ArgumentCondition(0, WithinRange, Range(0, IntMax))));
2511
2512 // int fnmatch(const char *pattern, const char *string, int flags);
2513 addToFunctionSummaryMap(
2514 "fnmatch",
2515 Signature(ArgTypes{ConstCharPtrTy, ConstCharPtrTy, IntTy},
2516 RetType{IntTy}),
2517 Summary(NoEvalCall)
2518 .ArgConstraint(NotNull(ArgNo(0)))
2519 .ArgConstraint(NotNull(ArgNo(1))));
2520
2521 // int fsync(int fildes);
2522 addToFunctionSummaryMap(
2523 "fsync", Signature(ArgTypes{IntTy}, RetType{IntTy}),
2524 Summary(NoEvalCall)
2525 .Case(ReturnsZero, ErrnoMustNotBeChecked, GenericSuccessMsg)
2526 .Case(ReturnsMinusOne, ErrnoNEZeroIrrelevant, GenericFailureMsg)
2527 .ArgConstraint(
2528 ArgumentCondition(0, WithinRange, Range(0, IntMax))));
2529
2530 // int truncate(const char *path, off_t length);
2531 addToFunctionSummaryMap(
2532 "truncate",
2533 Signature(ArgTypes{ConstCharPtrTy, Off_tTy}, RetType{IntTy}),
2534 Summary(NoEvalCall)
2535 .Case(ReturnsZero, ErrnoMustNotBeChecked, GenericSuccessMsg)
2536 .Case(ReturnsMinusOne, ErrnoNEZeroIrrelevant, GenericFailureMsg)
2537 .ArgConstraint(NotNull(ArgNo(0))));
2538
2539 // int symlink(const char *oldpath, const char *newpath);
2540 addToFunctionSummaryMap(
2541 "symlink",
2542 Signature(ArgTypes{ConstCharPtrTy, ConstCharPtrTy}, RetType{IntTy}),
2543 Summary(NoEvalCall)
2544 .Case(ReturnsZero, ErrnoMustNotBeChecked, GenericSuccessMsg)
2545 .Case(ReturnsMinusOne, ErrnoNEZeroIrrelevant, GenericFailureMsg)
2546 .ArgConstraint(NotNull(ArgNo(0)))
2547 .ArgConstraint(NotNull(ArgNo(1))));
2548
2549 // int symlinkat(const char *oldpath, int newdirfd, const char *newpath);
2550 addToFunctionSummaryMap(
2551 "symlinkat",
2552 Signature(ArgTypes{ConstCharPtrTy, IntTy, ConstCharPtrTy},
2553 RetType{IntTy}),
2554 Summary(NoEvalCall)
2555 .Case(ReturnsZero, ErrnoMustNotBeChecked, GenericSuccessMsg)
2556 .Case(ReturnsMinusOne, ErrnoNEZeroIrrelevant, GenericFailureMsg)
2557 .ArgConstraint(NotNull(ArgNo(0)))
2558 .ArgConstraint(ValidFileDescriptorOrAtFdcwd(ArgNo(1)))
2559 .ArgConstraint(NotNull(ArgNo(2))));
2560
2561 // int lockf(int fd, int cmd, off_t len);
2562 addToFunctionSummaryMap(
2563 "lockf", Signature(ArgTypes{IntTy, IntTy, Off_tTy}, RetType{IntTy}),
2564 Summary(NoEvalCall)
2565 .Case(ReturnsZero, ErrnoMustNotBeChecked, GenericSuccessMsg)
2566 .Case(ReturnsMinusOne, ErrnoNEZeroIrrelevant, GenericFailureMsg)
2567 .ArgConstraint(
2568 ArgumentCondition(0, WithinRange, Range(0, IntMax))));
2569
2570 std::optional<QualType> Mode_tTy = lookupTy("mode_t");
2571
2572 // int creat(const char *pathname, mode_t mode);
2573 addToFunctionSummaryMap(
2574 "creat", Signature(ArgTypes{ConstCharPtrTy, Mode_tTy}, RetType{IntTy}),
2575 Summary(NoEvalCall)
2576 .Case(ReturnsValidFileDescriptor, ErrnoMustNotBeChecked,
2577 GenericSuccessMsg)
2578 .Case(ReturnsMinusOne, ErrnoNEZeroIrrelevant, GenericFailureMsg)
2579 .ArgConstraint(NotNull(ArgNo(0))));
2580
2581 // unsigned int sleep(unsigned int seconds);
2582 addToFunctionSummaryMap(
2583 "sleep", Signature(ArgTypes{UnsignedIntTy}, RetType{UnsignedIntTy}),
2584 Summary(NoEvalCall)
2585 .ArgConstraint(
2586 ArgumentCondition(0, WithinRange, Range(0, UnsignedIntMax))));
2587
2588 std::optional<QualType> DirTy = lookupTy("DIR");
2589 std::optional<QualType> DirPtrTy = getPointerTy(DirTy);
2590
2591 // int dirfd(DIR *dirp);
2592 addToFunctionSummaryMap(
2593 "dirfd", Signature(ArgTypes{DirPtrTy}, RetType{IntTy}),
2594 Summary(NoEvalCall)
2595 .Case(ReturnsValidFileDescriptor, ErrnoMustNotBeChecked,
2596 GenericSuccessMsg)
2597 .Case(ReturnsMinusOne, ErrnoNEZeroIrrelevant, GenericFailureMsg)
2598 .ArgConstraint(NotNull(ArgNo(0))));
2599
2600 // unsigned int alarm(unsigned int seconds);
2601 addToFunctionSummaryMap(
2602 "alarm", Signature(ArgTypes{UnsignedIntTy}, RetType{UnsignedIntTy}),
2603 Summary(NoEvalCall)
2604 .ArgConstraint(
2605 ArgumentCondition(0, WithinRange, Range(0, UnsignedIntMax))));
2606
2607 // int closedir(DIR *dir);
2608 addToFunctionSummaryMap(
2609 "closedir", Signature(ArgTypes{DirPtrTy}, RetType{IntTy}),
2610 Summary(NoEvalCall)
2611 .Case(ReturnsZero, ErrnoMustNotBeChecked, GenericSuccessMsg)
2612 .Case(ReturnsMinusOne, ErrnoNEZeroIrrelevant, GenericFailureMsg)
2613 .ArgConstraint(NotNull(ArgNo(0))));
2614
2615 // char *strdup(const char *s);
2616 addToFunctionSummaryMap(
2617 "strdup", Signature(ArgTypes{ConstCharPtrTy}, RetType{CharPtrTy}),
2618 Summary(NoEvalCall).ArgConstraint(NotNull(ArgNo(0))));
2619
2620 // char *strndup(const char *s, size_t n);
2621 addToFunctionSummaryMap(
2622 "strndup",
2623 Signature(ArgTypes{ConstCharPtrTy, SizeTyCanonTy}, RetType{CharPtrTy}),
2624 Summary(NoEvalCall)
2625 .ArgConstraint(NotNull(ArgNo(0)))
2626 .ArgConstraint(
2627 ArgumentCondition(1, WithinRange, Range(0, SizeMax))));
2628
2629 // wchar_t *wcsdup(const wchar_t *s);
2630 addToFunctionSummaryMap(
2631 "wcsdup", Signature(ArgTypes{ConstWchar_tPtrTy}, RetType{Wchar_tPtrTy}),
2632 Summary(NoEvalCall).ArgConstraint(NotNull(ArgNo(0))));
2633
2634 // int mkstemp(char *template);
2635 addToFunctionSummaryMap(
2636 "mkstemp", Signature(ArgTypes{CharPtrTy}, RetType{IntTy}),
2637 Summary(NoEvalCall)
2638 .Case(ReturnsValidFileDescriptor, ErrnoMustNotBeChecked,
2639 GenericSuccessMsg)
2640 .Case(ReturnsMinusOne, ErrnoNEZeroIrrelevant, GenericFailureMsg)
2641 .ArgConstraint(NotNull(ArgNo(0))));
2642
2643 // char *mkdtemp(char *template);
2644 addToFunctionSummaryMap(
2645 "mkdtemp", Signature(ArgTypes{CharPtrTy}, RetType{CharPtrTy}),
2646 Summary(NoEvalCall)
2647 .Case({NotNull(Ret), ReturnValueCondition(BO_EQ, ArgNo(0))},
2648 ErrnoMustNotBeChecked, GenericSuccessMsg)
2649 .Case({IsNull(Ret)}, ErrnoNEZeroIrrelevant, GenericFailureMsg)
2650 .ArgConstraint(NotNull(ArgNo(0))));
2651
2652 // char *getcwd(char *buf, size_t size);
2653 addToFunctionSummaryMap(
2654 "getcwd",
2655 Signature(ArgTypes{CharPtrTy, SizeTyCanonTy}, RetType{CharPtrTy}),
2656 Summary(NoEvalCall)
2657 .Case({NotNull(0),
2658 ArgumentCondition(1, WithinRange, Range(1, SizeMax)),
2659 ReturnValueCondition(BO_EQ, ArgNo(0)), NotNull(Ret)},
2660 ErrnoMustNotBeChecked, GenericSuccessMsg)
2661 .Case({NotNull(0),
2662 ArgumentCondition(1, WithinRange, SingleValue(0)),
2663 IsNull(Ret)},
2664 ErrnoNEZeroIrrelevant, "Assuming that argument 'size' is 0")
2665 .Case({NotNull(0),
2666 ArgumentCondition(1, WithinRange, Range(1, SizeMax)),
2667 IsNull(Ret)},
2668 ErrnoNEZeroIrrelevant, GenericFailureMsg)
2669 .Case({IsNull(0), NotNull(Ret)}, ErrnoMustNotBeChecked,
2670 GenericSuccessMsg)
2671 .Case({IsNull(0), IsNull(Ret)}, ErrnoNEZeroIrrelevant,
2672 GenericFailureMsg)
2673 .ArgConstraint(
2674 BufferSize(/*Buffer*/ ArgNo(0), /*BufSize*/ ArgNo(1)))
2675 .ArgConstraint(
2676 ArgumentCondition(1, WithinRange, Range(0, SizeMax))));
2677
2678 // int mkdir(const char *pathname, mode_t mode);
2679 addToFunctionSummaryMap(
2680 "mkdir", Signature(ArgTypes{ConstCharPtrTy, Mode_tTy}, RetType{IntTy}),
2681 Summary(NoEvalCall)
2682 .Case(ReturnsZero, ErrnoMustNotBeChecked, GenericSuccessMsg)
2683 .Case(ReturnsMinusOne, ErrnoNEZeroIrrelevant, GenericFailureMsg)
2684 .ArgConstraint(NotNull(ArgNo(0))));
2685
2686 // int mkdirat(int dirfd, const char *pathname, mode_t mode);
2687 addToFunctionSummaryMap(
2688 "mkdirat",
2689 Signature(ArgTypes{IntTy, ConstCharPtrTy, Mode_tTy}, RetType{IntTy}),
2690 Summary(NoEvalCall)
2691 .Case(ReturnsZero, ErrnoMustNotBeChecked, GenericSuccessMsg)
2692 .Case(ReturnsMinusOne, ErrnoNEZeroIrrelevant, GenericFailureMsg)
2693 .ArgConstraint(ValidFileDescriptorOrAtFdcwd(ArgNo(0)))
2694 .ArgConstraint(NotNull(ArgNo(1))));
2695
2696 std::optional<QualType> Dev_tTy = lookupTy("dev_t");
2697
2698 // int mknod(const char *pathname, mode_t mode, dev_t dev);
2699 addToFunctionSummaryMap(
2700 "mknod",
2701 Signature(ArgTypes{ConstCharPtrTy, Mode_tTy, Dev_tTy}, RetType{IntTy}),
2702 Summary(NoEvalCall)
2703 .Case(ReturnsZero, ErrnoMustNotBeChecked, GenericSuccessMsg)
2704 .Case(ReturnsMinusOne, ErrnoNEZeroIrrelevant, GenericFailureMsg)
2705 .ArgConstraint(NotNull(ArgNo(0))));
2706
2707 // int mknodat(int dirfd, const char *pathname, mode_t mode, dev_t dev);
2708 addToFunctionSummaryMap(
2709 "mknodat",
2710 Signature(ArgTypes{IntTy, ConstCharPtrTy, Mode_tTy, Dev_tTy},
2711 RetType{IntTy}),
2712 Summary(NoEvalCall)
2713 .Case(ReturnsZero, ErrnoMustNotBeChecked, GenericSuccessMsg)
2714 .Case(ReturnsMinusOne, ErrnoNEZeroIrrelevant, GenericFailureMsg)
2715 .ArgConstraint(ValidFileDescriptorOrAtFdcwd(ArgNo(0)))
2716 .ArgConstraint(NotNull(ArgNo(1))));
2717
2718 // int chmod(const char *path, mode_t mode);
2719 addToFunctionSummaryMap(
2720 "chmod", Signature(ArgTypes{ConstCharPtrTy, Mode_tTy}, RetType{IntTy}),
2721 Summary(NoEvalCall)
2722 .Case(ReturnsZero, ErrnoMustNotBeChecked, GenericSuccessMsg)
2723 .Case(ReturnsMinusOne, ErrnoNEZeroIrrelevant, GenericFailureMsg)
2724 .ArgConstraint(NotNull(ArgNo(0))));
2725
2726 // int fchmodat(int dirfd, const char *pathname, mode_t mode, int flags);
2727 addToFunctionSummaryMap(
2728 "fchmodat",
2729 Signature(ArgTypes{IntTy, ConstCharPtrTy, Mode_tTy, IntTy},
2730 RetType{IntTy}),
2731 Summary(NoEvalCall)
2732 .Case(ReturnsZero, ErrnoMustNotBeChecked, GenericSuccessMsg)
2733 .Case(ReturnsMinusOne, ErrnoNEZeroIrrelevant, GenericFailureMsg)
2734 .ArgConstraint(ValidFileDescriptorOrAtFdcwd(ArgNo(0)))
2735 .ArgConstraint(NotNull(ArgNo(1))));
2736
2737 // int fchmod(int fildes, mode_t mode);
2738 addToFunctionSummaryMap(
2739 "fchmod", Signature(ArgTypes{IntTy, Mode_tTy}, RetType{IntTy}),
2740 Summary(NoEvalCall)
2741 .Case(ReturnsZero, ErrnoMustNotBeChecked, GenericSuccessMsg)
2742 .Case(ReturnsMinusOne, ErrnoNEZeroIrrelevant, GenericFailureMsg)
2743 .ArgConstraint(
2744 ArgumentCondition(0, WithinRange, Range(0, IntMax))));
2745
2746 std::optional<QualType> Uid_tTy = lookupTy("uid_t");
2747 std::optional<QualType> Gid_tTy = lookupTy("gid_t");
2748
2749 // int fchownat(int dirfd, const char *pathname, uid_t owner, gid_t group,
2750 // int flags);
2751 addToFunctionSummaryMap(
2752 "fchownat",
2753 Signature(ArgTypes{IntTy, ConstCharPtrTy, Uid_tTy, Gid_tTy, IntTy},
2754 RetType{IntTy}),
2755 Summary(NoEvalCall)
2756 .Case(ReturnsZero, ErrnoMustNotBeChecked, GenericSuccessMsg)
2757 .Case(ReturnsMinusOne, ErrnoNEZeroIrrelevant, GenericFailureMsg)
2758 .ArgConstraint(ValidFileDescriptorOrAtFdcwd(ArgNo(0)))
2759 .ArgConstraint(NotNull(ArgNo(1))));
2760
2761 // int chown(const char *path, uid_t owner, gid_t group);
2762 addToFunctionSummaryMap(
2763 "chown",
2764 Signature(ArgTypes{ConstCharPtrTy, Uid_tTy, Gid_tTy}, RetType{IntTy}),
2765 Summary(NoEvalCall)
2766 .Case(ReturnsZero, ErrnoMustNotBeChecked, GenericSuccessMsg)
2767 .Case(ReturnsMinusOne, ErrnoNEZeroIrrelevant, GenericFailureMsg)
2768 .ArgConstraint(NotNull(ArgNo(0))));
2769
2770 // int lchown(const char *path, uid_t owner, gid_t group);
2771 addToFunctionSummaryMap(
2772 "lchown",
2773 Signature(ArgTypes{ConstCharPtrTy, Uid_tTy, Gid_tTy}, RetType{IntTy}),
2774 Summary(NoEvalCall)
2775 .Case(ReturnsZero, ErrnoMustNotBeChecked, GenericSuccessMsg)
2776 .Case(ReturnsMinusOne, ErrnoNEZeroIrrelevant, GenericFailureMsg)
2777 .ArgConstraint(NotNull(ArgNo(0))));
2778
2779 // int fchown(int fildes, uid_t owner, gid_t group);
2780 addToFunctionSummaryMap(
2781 "fchown", Signature(ArgTypes{IntTy, Uid_tTy, Gid_tTy}, RetType{IntTy}),
2782 Summary(NoEvalCall)
2783 .Case(ReturnsZero, ErrnoMustNotBeChecked, GenericSuccessMsg)
2784 .Case(ReturnsMinusOne, ErrnoNEZeroIrrelevant, GenericFailureMsg)
2785 .ArgConstraint(
2786 ArgumentCondition(0, WithinRange, Range(0, IntMax))));
2787
2788 // int rmdir(const char *pathname);
2789 addToFunctionSummaryMap(
2790 "rmdir", Signature(ArgTypes{ConstCharPtrTy}, RetType{IntTy}),
2791 Summary(NoEvalCall)
2792 .Case(ReturnsZero, ErrnoMustNotBeChecked, GenericSuccessMsg)
2793 .Case(ReturnsMinusOne, ErrnoNEZeroIrrelevant, GenericFailureMsg)
2794 .ArgConstraint(NotNull(ArgNo(0))));
2795
2796 // int chdir(const char *path);
2797 addToFunctionSummaryMap(
2798 "chdir", Signature(ArgTypes{ConstCharPtrTy}, RetType{IntTy}),
2799 Summary(NoEvalCall)
2800 .Case(ReturnsZero, ErrnoMustNotBeChecked, GenericSuccessMsg)
2801 .Case(ReturnsMinusOne, ErrnoNEZeroIrrelevant, GenericFailureMsg)
2802 .ArgConstraint(NotNull(ArgNo(0))));
2803
2804 // int link(const char *oldpath, const char *newpath);
2805 addToFunctionSummaryMap(
2806 "link",
2807 Signature(ArgTypes{ConstCharPtrTy, ConstCharPtrTy}, RetType{IntTy}),
2808 Summary(NoEvalCall)
2809 .Case(ReturnsZero, ErrnoMustNotBeChecked, GenericSuccessMsg)
2810 .Case(ReturnsMinusOne, ErrnoNEZeroIrrelevant, GenericFailureMsg)
2811 .ArgConstraint(NotNull(ArgNo(0)))
2812 .ArgConstraint(NotNull(ArgNo(1))));
2813
2814 // int linkat(int fd1, const char *path1, int fd2, const char *path2,
2815 // int flag);
2816 addToFunctionSummaryMap(
2817 "linkat",
2818 Signature(ArgTypes{IntTy, ConstCharPtrTy, IntTy, ConstCharPtrTy, IntTy},
2819 RetType{IntTy}),
2820 Summary(NoEvalCall)
2821 .Case(ReturnsZero, ErrnoMustNotBeChecked, GenericSuccessMsg)
2822 .Case(ReturnsMinusOne, ErrnoNEZeroIrrelevant, GenericFailureMsg)
2823 .ArgConstraint(ValidFileDescriptorOrAtFdcwd(ArgNo(0)))
2824 .ArgConstraint(NotNull(ArgNo(1)))
2825 .ArgConstraint(ValidFileDescriptorOrAtFdcwd(ArgNo(2)))
2826 .ArgConstraint(NotNull(ArgNo(3))));
2827
2828 // int unlink(const char *pathname);
2829 addToFunctionSummaryMap(
2830 "unlink", Signature(ArgTypes{ConstCharPtrTy}, RetType{IntTy}),
2831 Summary(NoEvalCall)
2832 .Case(ReturnsZero, ErrnoMustNotBeChecked, GenericSuccessMsg)
2833 .Case(ReturnsMinusOne, ErrnoNEZeroIrrelevant, GenericFailureMsg)
2834 .ArgConstraint(NotNull(ArgNo(0))));
2835
2836 // int unlinkat(int fd, const char *path, int flag);
2837 addToFunctionSummaryMap(
2838 "unlinkat",
2839 Signature(ArgTypes{IntTy, ConstCharPtrTy, IntTy}, RetType{IntTy}),
2840 Summary(NoEvalCall)
2841 .Case(ReturnsZero, ErrnoMustNotBeChecked, GenericSuccessMsg)
2842 .Case(ReturnsMinusOne, ErrnoNEZeroIrrelevant, GenericFailureMsg)
2843 .ArgConstraint(ValidFileDescriptorOrAtFdcwd(ArgNo(0)))
2844 .ArgConstraint(NotNull(ArgNo(1))));
2845
2846 std::optional<QualType> StructStatTy = lookupTy("stat");
2847 std::optional<QualType> StructStatPtrTy = getPointerTy(StructStatTy);
2848 std::optional<QualType> StructStatPtrRestrictTy =
2849 getRestrictTy(StructStatPtrTy);
2850
2851 // int fstat(int fd, struct stat *statbuf);
2852 addToFunctionSummaryMap(
2853 "fstat", Signature(ArgTypes{IntTy, StructStatPtrTy}, RetType{IntTy}),
2854 Summary(NoEvalCall)
2855 .Case(ReturnsZero, ErrnoMustNotBeChecked, GenericSuccessMsg)
2856 .Case(ReturnsMinusOne, ErrnoNEZeroIrrelevant, GenericFailureMsg)
2857 .ArgConstraint(ArgumentCondition(0, WithinRange, Range(0, IntMax)))
2858 .ArgConstraint(NotNull(ArgNo(1))));
2859
2860 // int stat(const char *restrict path, struct stat *restrict buf);
2861 addToFunctionSummaryMap(
2862 "stat",
2863 Signature(ArgTypes{ConstCharPtrRestrictTy, StructStatPtrRestrictTy},
2864 RetType{IntTy}),
2865 Summary(NoEvalCall)
2866 .Case(ReturnsZero, ErrnoMustNotBeChecked, GenericSuccessMsg)
2867 .Case(ReturnsMinusOne, ErrnoNEZeroIrrelevant, GenericFailureMsg)
2868 .ArgConstraint(NotNull(ArgNo(0)))
2869 .ArgConstraint(NotNull(ArgNo(1))));
2870
2871 // int lstat(const char *restrict path, struct stat *restrict buf);
2872 addToFunctionSummaryMap(
2873 "lstat",
2874 Signature(ArgTypes{ConstCharPtrRestrictTy, StructStatPtrRestrictTy},
2875 RetType{IntTy}),
2876 Summary(NoEvalCall)
2877 .Case(ReturnsZero, ErrnoMustNotBeChecked, GenericSuccessMsg)
2878 .Case(ReturnsMinusOne, ErrnoNEZeroIrrelevant, GenericFailureMsg)
2879 .ArgConstraint(NotNull(ArgNo(0)))
2880 .ArgConstraint(NotNull(ArgNo(1))));
2881
2882 // int fstatat(int fd, const char *restrict path,
2883 // struct stat *restrict buf, int flag);
2884 addToFunctionSummaryMap(
2885 "fstatat",
2886 Signature(ArgTypes{IntTy, ConstCharPtrRestrictTy,
2887 StructStatPtrRestrictTy, IntTy},
2888 RetType{IntTy}),
2889 Summary(NoEvalCall)
2890 .Case(ReturnsZero, ErrnoMustNotBeChecked, GenericSuccessMsg)
2891 .Case(ReturnsMinusOne, ErrnoNEZeroIrrelevant, GenericFailureMsg)
2892 .ArgConstraint(ValidFileDescriptorOrAtFdcwd(ArgNo(0)))
2893 .ArgConstraint(NotNull(ArgNo(1)))
2894 .ArgConstraint(NotNull(ArgNo(2))));
2895
2896 // DIR *opendir(const char *name);
2897 addToFunctionSummaryMap(
2898 "opendir", Signature(ArgTypes{ConstCharPtrTy}, RetType{DirPtrTy}),
2899 Summary(NoEvalCall)
2900 .Case({NotNull(Ret)}, ErrnoMustNotBeChecked, GenericSuccessMsg)
2901 .Case({IsNull(Ret)}, ErrnoNEZeroIrrelevant, GenericFailureMsg)
2902 .ArgConstraint(NotNull(ArgNo(0))));
2903
2904 // DIR *fdopendir(int fd);
2905 addToFunctionSummaryMap(
2906 "fdopendir", Signature(ArgTypes{IntTy}, RetType{DirPtrTy}),
2907 Summary(NoEvalCall)
2908 .Case({NotNull(Ret)}, ErrnoMustNotBeChecked, GenericSuccessMsg)
2909 .Case({IsNull(Ret)}, ErrnoNEZeroIrrelevant, GenericFailureMsg)
2910 .ArgConstraint(
2911 ArgumentCondition(0, WithinRange, Range(0, IntMax))));
2912
2913 // int isatty(int fildes);
2914 addToFunctionSummaryMap(
2915 "isatty", Signature(ArgTypes{IntTy}, RetType{IntTy}),
2916 Summary(NoEvalCall)
2917 .Case({ReturnValueCondition(WithinRange, Range(0, 1))},
2918 ErrnoIrrelevant)
2919 .ArgConstraint(
2920 ArgumentCondition(0, WithinRange, Range(0, IntMax))));
2921
2922 // int close(int fildes);
2923 addToFunctionSummaryMap(
2924 "close", Signature(ArgTypes{IntTy}, RetType{IntTy}),
2925 Summary(NoEvalCall)
2926 .Case(ReturnsZero, ErrnoMustNotBeChecked, GenericSuccessMsg)
2927 .Case(ReturnsMinusOne, ErrnoNEZeroIrrelevant, GenericFailureMsg)
2928 .ArgConstraint(
2929 ArgumentCondition(0, WithinRange, Range(-1, IntMax))));
2930
2931 // long fpathconf(int fildes, int name);
2932 addToFunctionSummaryMap("fpathconf",
2933 Signature(ArgTypes{IntTy, IntTy}, RetType{LongTy}),
2934 Summary(NoEvalCall)
2935 .ArgConstraint(ArgumentCondition(
2936 0, WithinRange, Range(0, IntMax))));
2937
2938 // long pathconf(const char *path, int name);
2939 addToFunctionSummaryMap(
2940 "pathconf", Signature(ArgTypes{ConstCharPtrTy, IntTy}, RetType{LongTy}),
2941 Summary(NoEvalCall).ArgConstraint(NotNull(ArgNo(0))));
2942
2943 // void rewinddir(DIR *dir);
2944 addToFunctionSummaryMap(
2945 "rewinddir", Signature(ArgTypes{DirPtrTy}, RetType{VoidTy}),
2946 Summary(NoEvalCall).ArgConstraint(NotNull(ArgNo(0))));
2947
2948 // void seekdir(DIR *dirp, long loc);
2949 addToFunctionSummaryMap(
2950 "seekdir", Signature(ArgTypes{DirPtrTy, LongTy}, RetType{VoidTy}),
2951 Summary(NoEvalCall).ArgConstraint(NotNull(ArgNo(0))));
2952
2953 // int rand_r(unsigned int *seedp);
2954 addToFunctionSummaryMap(
2955 "rand_r", Signature(ArgTypes{UnsignedIntPtrTy}, RetType{IntTy}),
2956 Summary(NoEvalCall).ArgConstraint(NotNull(ArgNo(0))));
2957
2958 // void *mmap(void *addr, size_t length, int prot, int flags, int fd,
2959 // off_t offset);
2960 // FIXME: Improve for errno modeling.
2961 addToFunctionSummaryMap(
2962 "mmap",
2963 Signature(
2964 ArgTypes{VoidPtrTy, SizeTyCanonTy, IntTy, IntTy, IntTy, Off_tTy},
2965 RetType{VoidPtrTy}),
2966 Summary(NoEvalCall)
2967 .ArgConstraint(ArgumentCondition(1, WithinRange, Range(1, SizeMax)))
2968 .ArgConstraint(
2969 ArgumentCondition(4, WithinRange, Range(-1, IntMax))));
2970
2971 std::optional<QualType> Off64_tTy = lookupTy("off64_t");
2972 // void *mmap64(void *addr, size_t length, int prot, int flags, int fd,
2973 // off64_t offset);
2974 // FIXME: Improve for errno modeling.
2975 addToFunctionSummaryMap(
2976 "mmap64",
2977 Signature(
2978 ArgTypes{VoidPtrTy, SizeTyCanonTy, IntTy, IntTy, IntTy, Off64_tTy},
2979 RetType{VoidPtrTy}),
2980 Summary(NoEvalCall)
2981 .ArgConstraint(ArgumentCondition(1, WithinRange, Range(1, SizeMax)))
2982 .ArgConstraint(
2983 ArgumentCondition(4, WithinRange, Range(-1, IntMax))));
2984
2985 // int pipe(int fildes[2]);
2986 addToFunctionSummaryMap(
2987 "pipe", Signature(ArgTypes{IntPtrTy}, RetType{IntTy}),
2988 Summary(NoEvalCall)
2989 .Case(ReturnsZero, ErrnoMustNotBeChecked, GenericSuccessMsg)
2990 .Case(ReturnsMinusOne, ErrnoNEZeroIrrelevant, GenericFailureMsg)
2991 .ArgConstraint(NotNull(ArgNo(0))));
2992
2993 // off_t lseek(int fildes, off_t offset, int whence);
2994 // In the first case we can not tell for sure if it failed or not.
2995 // A return value different from of the expected offset (that is unknown
2996 // here) may indicate failure. For this reason we do not enforce the errno
2997 // check (can cause false positive).
2998 addToFunctionSummaryMap(
2999 "lseek", Signature(ArgTypes{IntTy, Off_tTy, IntTy}, RetType{Off_tTy}),
3000 Summary(NoEvalCall)
3001 .Case(ReturnsNonnegative, ErrnoIrrelevant)
3002 .Case(ReturnsMinusOne, ErrnoNEZeroIrrelevant, GenericFailureMsg)
3003 .ArgConstraint(
3004 ArgumentCondition(0, WithinRange, Range(0, IntMax))));
3005
3006 // ssize_t readlink(const char *restrict path, char *restrict buf,
3007 // size_t bufsize);
3008 addToFunctionSummaryMap(
3009 "readlink",
3010 Signature(
3011 ArgTypes{ConstCharPtrRestrictTy, CharPtrRestrictTy, SizeTyCanonTy},
3012 RetType{Ssize_tTy}),
3013 Summary(NoEvalCall)
3014 .Case({ArgumentCondition(2, WithinRange, Range(1, IntMax)),
3015 ReturnValueCondition(LessThanOrEq, ArgNo(2)),
3016 ReturnValueCondition(WithinRange, Range(1, Ssize_tMax))},
3017 ErrnoMustNotBeChecked, GenericSuccessMsg)
3018 .Case({ArgumentCondition(2, WithinRange, SingleValue(0)),
3019 ReturnValueCondition(WithinRange, SingleValue(0))},
3020 ErrnoMustNotBeChecked,
3021 "Assuming that argument 'bufsize' is 0")
3022 .Case(ReturnsMinusOne, ErrnoNEZeroIrrelevant, GenericFailureMsg)
3023 .ArgConstraint(NotNull(ArgNo(0)))
3024 .ArgConstraint(NotNull(ArgNo(1)))
3025 .ArgConstraint(BufferSize(/*Buffer=*/ArgNo(1),
3026 /*BufSize=*/ArgNo(2)))
3027 .ArgConstraint(
3028 ArgumentCondition(2, WithinRange, Range(0, SizeMax))));
3029
3030 // ssize_t readlinkat(int fd, const char *restrict path,
3031 // char *restrict buf, size_t bufsize);
3032 addToFunctionSummaryMap(
3033 "readlinkat",
3034 Signature(ArgTypes{IntTy, ConstCharPtrRestrictTy, CharPtrRestrictTy,
3035 SizeTyCanonTy},
3036 RetType{Ssize_tTy}),
3037 Summary(NoEvalCall)
3038 .Case({ArgumentCondition(3, WithinRange, Range(1, IntMax)),
3039 ReturnValueCondition(LessThanOrEq, ArgNo(3)),
3040 ReturnValueCondition(WithinRange, Range(1, Ssize_tMax))},
3041 ErrnoMustNotBeChecked, GenericSuccessMsg)
3042 .Case({ArgumentCondition(3, WithinRange, SingleValue(0)),
3043 ReturnValueCondition(WithinRange, SingleValue(0))},
3044 ErrnoMustNotBeChecked,
3045 "Assuming that argument 'bufsize' is 0")
3046 .Case(ReturnsMinusOne, ErrnoNEZeroIrrelevant, GenericFailureMsg)
3047 .ArgConstraint(ValidFileDescriptorOrAtFdcwd(ArgNo(0)))
3048 .ArgConstraint(NotNull(ArgNo(1)))
3049 .ArgConstraint(NotNull(ArgNo(2)))
3050 .ArgConstraint(BufferSize(/*Buffer=*/ArgNo(2),
3051 /*BufSize=*/ArgNo(3)))
3052 .ArgConstraint(
3053 ArgumentCondition(3, WithinRange, Range(0, SizeMax))));
3054
3055 // int renameat(int olddirfd, const char *oldpath, int newdirfd, const char
3056 // *newpath);
3057 addToFunctionSummaryMap(
3058 "renameat",
3059 Signature(ArgTypes{IntTy, ConstCharPtrTy, IntTy, ConstCharPtrTy},
3060 RetType{IntTy}),
3061 Summary(NoEvalCall)
3062 .Case(ReturnsZero, ErrnoMustNotBeChecked, GenericSuccessMsg)
3063 .Case(ReturnsMinusOne, ErrnoNEZeroIrrelevant, GenericFailureMsg)
3064 .ArgConstraint(ValidFileDescriptorOrAtFdcwd(ArgNo(0)))
3065 .ArgConstraint(NotNull(ArgNo(1)))
3066 .ArgConstraint(ValidFileDescriptorOrAtFdcwd(ArgNo(2)))
3067 .ArgConstraint(NotNull(ArgNo(3))));
3068
3069 // char *realpath(const char *restrict file_name,
3070 // char *restrict resolved_name);
3071 // FIXME: If the argument 'resolved_name' is not NULL, macro 'PATH_MAX'
3072 // should be defined in "limits.h" to guarrantee a success.
3073 addToFunctionSummaryMap(
3074 "realpath",
3075 Signature(ArgTypes{ConstCharPtrRestrictTy, CharPtrRestrictTy},
3076 RetType{CharPtrTy}),
3077 Summary(NoEvalCall)
3078 .Case({NotNull(Ret)}, ErrnoMustNotBeChecked, GenericSuccessMsg)
3079 .Case({IsNull(Ret)}, ErrnoNEZeroIrrelevant, GenericFailureMsg)
3080 .ArgConstraint(NotNull(ArgNo(0))));
3081
3082 QualType CharPtrConstPtr = getPointerTy(getConstTy(CharPtrTy));
3083
3084 // int execv(const char *path, char *const argv[]);
3085 addToFunctionSummaryMap(
3086 "execv",
3087 Signature(ArgTypes{ConstCharPtrTy, CharPtrConstPtr}, RetType{IntTy}),
3088 Summary(NoEvalCall)
3089 .Case(ReturnsMinusOne, ErrnoNEZeroIrrelevant)
3090 .ArgConstraint(NotNull(ArgNo(0))));
3091
3092 // int execvp(const char *file, char *const argv[]);
3093 addToFunctionSummaryMap(
3094 "execvp",
3095 Signature(ArgTypes{ConstCharPtrTy, CharPtrConstPtr}, RetType{IntTy}),
3096 Summary(NoEvalCall)
3097 .Case(ReturnsMinusOne, ErrnoNEZeroIrrelevant)
3098 .ArgConstraint(NotNull(ArgNo(0))));
3099
3100 // int getopt(int argc, char * const argv[], const char *optstring);
3101 addToFunctionSummaryMap(
3102 "getopt",
3103 Signature(ArgTypes{IntTy, CharPtrConstPtr, ConstCharPtrTy},
3104 RetType{IntTy}),
3105 Summary(NoEvalCall)
3106 .Case({ReturnValueCondition(WithinRange, Range(-1, UCharRangeMax))},
3107 ErrnoIrrelevant)
3108 .ArgConstraint(ArgumentCondition(0, WithinRange, Range(0, IntMax)))
3109 .ArgConstraint(NotNull(ArgNo(1)))
3110 .ArgConstraint(NotNull(ArgNo(2))));
3111
3112 std::optional<QualType> StructSockaddrTy = lookupTy("sockaddr");
3113 std::optional<QualType> StructSockaddrPtrTy =
3114 getPointerTy(StructSockaddrTy);
3115 std::optional<QualType> ConstStructSockaddrPtrTy =
3116 getPointerTy(getConstTy(StructSockaddrTy));
3117 std::optional<QualType> StructSockaddrPtrRestrictTy =
3118 getRestrictTy(StructSockaddrPtrTy);
3119 std::optional<QualType> ConstStructSockaddrPtrRestrictTy =
3120 getRestrictTy(ConstStructSockaddrPtrTy);
3121 std::optional<QualType> Socklen_tTy = lookupTy("socklen_t");
3122 std::optional<QualType> Socklen_tPtrTy = getPointerTy(Socklen_tTy);
3123 std::optional<QualType> Socklen_tPtrRestrictTy =
3124 getRestrictTy(Socklen_tPtrTy);
3125 std::optional<RangeInt> Socklen_tMax = getMaxValue(Socklen_tTy);
3126
3127 // In 'socket.h' of some libc implementations with C99, sockaddr parameter
3128 // is a transparent union of the underlying sockaddr_ family of pointers
3129 // instead of being a pointer to struct sockaddr. In these cases, the
3130 // standardized signature will not match, thus we try to match with another
3131 // signature that has the joker Irrelevant type. We also remove those
3132 // constraints which require pointer types for the sockaddr param.
3133
3134 // int socket(int domain, int type, int protocol);
3135 addToFunctionSummaryMap(
3136 "socket", Signature(ArgTypes{IntTy, IntTy, IntTy}, RetType{IntTy}),
3137 Summary(NoEvalCall)
3138 .Case(ReturnsValidFileDescriptor, ErrnoMustNotBeChecked,
3139 GenericSuccessMsg)
3140 .Case(ReturnsMinusOne, ErrnoNEZeroIrrelevant, GenericFailureMsg));
3141
3142 auto Accept =
3143 Summary(NoEvalCall)
3144 .Case(ReturnsValidFileDescriptor, ErrnoMustNotBeChecked,
3145 GenericSuccessMsg)
3146 .Case(ReturnsMinusOne, ErrnoNEZeroIrrelevant, GenericFailureMsg)
3147 .ArgConstraint(ArgumentCondition(0, WithinRange, Range(0, IntMax)));
3148 if (!addToFunctionSummaryMap(
3149 "accept",
3150 // int accept(int socket, struct sockaddr *restrict address,
3151 // socklen_t *restrict address_len);
3152 Signature(ArgTypes{IntTy, StructSockaddrPtrRestrictTy,
3153 Socklen_tPtrRestrictTy},
3154 RetType{IntTy}),
3155 Accept))
3156 addToFunctionSummaryMap(
3157 "accept",
3158 Signature(ArgTypes{IntTy, Irrelevant, Socklen_tPtrRestrictTy},
3159 RetType{IntTy}),
3160 Accept);
3161
3162 // int bind(int socket, const struct sockaddr *address, socklen_t
3163 // address_len);
3164 if (!addToFunctionSummaryMap(
3165 "bind",
3166 Signature(ArgTypes{IntTy, ConstStructSockaddrPtrTy, Socklen_tTy},
3167 RetType{IntTy}),
3168 Summary(NoEvalCall)
3169 .Case(ReturnsZero, ErrnoMustNotBeChecked, GenericSuccessMsg)
3170 .Case(ReturnsMinusOne, ErrnoNEZeroIrrelevant, GenericFailureMsg)
3171 .ArgConstraint(
3172 ArgumentCondition(0, WithinRange, Range(0, IntMax)))
3173 .ArgConstraint(NotNull(ArgNo(1)))
3174 .ArgConstraint(
3175 BufferSize(/*Buffer=*/ArgNo(1), /*BufSize=*/ArgNo(2)))
3176 .ArgConstraint(
3177 ArgumentCondition(2, WithinRange, Range(0, Socklen_tMax)))))
3178 // Do not add constraints on sockaddr.
3179 addToFunctionSummaryMap(
3180 "bind",
3181 Signature(ArgTypes{IntTy, Irrelevant, Socklen_tTy}, RetType{IntTy}),
3182 Summary(NoEvalCall)
3183 .Case(ReturnsZero, ErrnoMustNotBeChecked, GenericSuccessMsg)
3184 .Case(ReturnsMinusOne, ErrnoNEZeroIrrelevant, GenericFailureMsg)
3185 .ArgConstraint(
3186 ArgumentCondition(0, WithinRange, Range(0, IntMax)))
3187 .ArgConstraint(
3188 ArgumentCondition(2, WithinRange, Range(0, Socklen_tMax))));
3189
3190 // int getpeername(int socket, struct sockaddr *restrict address,
3191 // socklen_t *restrict address_len);
3192 if (!addToFunctionSummaryMap(
3193 "getpeername",
3194 Signature(ArgTypes{IntTy, StructSockaddrPtrRestrictTy,
3195 Socklen_tPtrRestrictTy},
3196 RetType{IntTy}),
3197 Summary(NoEvalCall)
3198 .Case(ReturnsZero, ErrnoMustNotBeChecked, GenericSuccessMsg)
3199 .Case(ReturnsMinusOne, ErrnoNEZeroIrrelevant, GenericFailureMsg)
3200 .ArgConstraint(
3201 ArgumentCondition(0, WithinRange, Range(0, IntMax)))
3202 .ArgConstraint(NotNull(ArgNo(1)))
3203 .ArgConstraint(NotNull(ArgNo(2)))))
3204 addToFunctionSummaryMap(
3205 "getpeername",
3206 Signature(ArgTypes{IntTy, Irrelevant, Socklen_tPtrRestrictTy},
3207 RetType{IntTy}),
3208 Summary(NoEvalCall)
3209 .Case(ReturnsZero, ErrnoMustNotBeChecked, GenericSuccessMsg)
3210 .Case(ReturnsMinusOne, ErrnoNEZeroIrrelevant, GenericFailureMsg)
3211 .ArgConstraint(
3212 ArgumentCondition(0, WithinRange, Range(0, IntMax))));
3213
3214 // int getsockname(int socket, struct sockaddr *restrict address,
3215 // socklen_t *restrict address_len);
3216 if (!addToFunctionSummaryMap(
3217 "getsockname",
3218 Signature(ArgTypes{IntTy, StructSockaddrPtrRestrictTy,
3219 Socklen_tPtrRestrictTy},
3220 RetType{IntTy}),
3221 Summary(NoEvalCall)
3222 .Case(ReturnsZero, ErrnoMustNotBeChecked, GenericSuccessMsg)
3223 .Case(ReturnsMinusOne, ErrnoNEZeroIrrelevant, GenericFailureMsg)
3224 .ArgConstraint(
3225 ArgumentCondition(0, WithinRange, Range(0, IntMax)))
3226 .ArgConstraint(NotNull(ArgNo(1)))
3227 .ArgConstraint(NotNull(ArgNo(2)))))
3228 addToFunctionSummaryMap(
3229 "getsockname",
3230 Signature(ArgTypes{IntTy, Irrelevant, Socklen_tPtrRestrictTy},
3231 RetType{IntTy}),
3232 Summary(NoEvalCall)
3233 .Case(ReturnsZero, ErrnoMustNotBeChecked, GenericSuccessMsg)
3234 .Case(ReturnsMinusOne, ErrnoNEZeroIrrelevant, GenericFailureMsg)
3235 .ArgConstraint(
3236 ArgumentCondition(0, WithinRange, Range(0, IntMax))));
3237
3238 // int connect(int socket, const struct sockaddr *address, socklen_t
3239 // address_len);
3240 if (!addToFunctionSummaryMap(
3241 "connect",
3242 Signature(ArgTypes{IntTy, ConstStructSockaddrPtrTy, Socklen_tTy},
3243 RetType{IntTy}),
3244 Summary(NoEvalCall)
3245 .Case(ReturnsZero, ErrnoMustNotBeChecked, GenericSuccessMsg)
3246 .Case(ReturnsMinusOne, ErrnoNEZeroIrrelevant, GenericFailureMsg)
3247 .ArgConstraint(
3248 ArgumentCondition(0, WithinRange, Range(0, IntMax)))
3249 .ArgConstraint(NotNull(ArgNo(1)))))
3250 addToFunctionSummaryMap(
3251 "connect",
3252 Signature(ArgTypes{IntTy, Irrelevant, Socklen_tTy}, RetType{IntTy}),
3253 Summary(NoEvalCall)
3254 .Case(ReturnsZero, ErrnoMustNotBeChecked, GenericSuccessMsg)
3255 .Case(ReturnsMinusOne, ErrnoNEZeroIrrelevant, GenericFailureMsg)
3256 .ArgConstraint(
3257 ArgumentCondition(0, WithinRange, Range(0, IntMax))));
3258
3259 auto Recvfrom =
3260 Summary(NoEvalCall)
3261 .Case({ReturnValueCondition(LessThanOrEq, ArgNo(2)),
3262 ReturnValueCondition(WithinRange, Range(1, Ssize_tMax))},
3263 ErrnoMustNotBeChecked, GenericSuccessMsg)
3264 .Case({ReturnValueCondition(WithinRange, SingleValue(0)),
3265 ArgumentCondition(2, WithinRange, SingleValue(0))},
3266 ErrnoMustNotBeChecked, GenericSuccessMsg)
3267 .Case(ReturnsMinusOne, ErrnoNEZeroIrrelevant, GenericFailureMsg)
3268 .ArgConstraint(ArgumentCondition(0, WithinRange, Range(0, IntMax)))
3269 .ArgConstraint(BufferSize(/*Buffer=*/ArgNo(1),
3270 /*BufSize=*/ArgNo(2)));
3271 if (!addToFunctionSummaryMap(
3272 "recvfrom",
3273 // ssize_t recvfrom(int socket, void *restrict buffer,
3274 // size_t length,
3275 // int flags, struct sockaddr *restrict address,
3276 // socklen_t *restrict address_len);
3277 Signature(ArgTypes{IntTy, VoidPtrRestrictTy, SizeTyCanonTy, IntTy,
3278 StructSockaddrPtrRestrictTy,
3279 Socklen_tPtrRestrictTy},
3280 RetType{Ssize_tTy}),
3281 Recvfrom))
3282 addToFunctionSummaryMap(
3283 "recvfrom",
3284 Signature(ArgTypes{IntTy, VoidPtrRestrictTy, SizeTyCanonTy, IntTy,
3285 Irrelevant, Socklen_tPtrRestrictTy},
3286 RetType{Ssize_tTy}),
3287 Recvfrom);
3288
3289 auto Sendto =
3290 Summary(NoEvalCall)
3291 .Case({ReturnValueCondition(LessThanOrEq, ArgNo(2)),
3292 ReturnValueCondition(WithinRange, Range(1, Ssize_tMax))},
3293 ErrnoMustNotBeChecked, GenericSuccessMsg)
3294 .Case({ReturnValueCondition(WithinRange, SingleValue(0)),
3295 ArgumentCondition(2, WithinRange, SingleValue(0))},
3296 ErrnoMustNotBeChecked, GenericSuccessMsg)
3297 .Case(ReturnsMinusOne, ErrnoNEZeroIrrelevant, GenericFailureMsg)
3298 .ArgConstraint(ArgumentCondition(0, WithinRange, Range(0, IntMax)))
3299 .ArgConstraint(BufferSize(/*Buffer=*/ArgNo(1),
3300 /*BufSize=*/ArgNo(2)));
3301 if (!addToFunctionSummaryMap(
3302 "sendto",
3303 // ssize_t sendto(int socket, const void *message, size_t length,
3304 // int flags, const struct sockaddr *dest_addr,
3305 // socklen_t dest_len);
3306 Signature(ArgTypes{IntTy, ConstVoidPtrTy, SizeTyCanonTy, IntTy,
3307 ConstStructSockaddrPtrTy, Socklen_tTy},
3308 RetType{Ssize_tTy}),
3309 Sendto))
3310 addToFunctionSummaryMap(
3311 "sendto",
3312 Signature(ArgTypes{IntTy, ConstVoidPtrTy, SizeTyCanonTy, IntTy,
3313 Irrelevant, Socklen_tTy},
3314 RetType{Ssize_tTy}),
3315 Sendto);
3316
3317 // int listen(int sockfd, int backlog);
3318 addToFunctionSummaryMap(
3319 "listen", Signature(ArgTypes{IntTy, IntTy}, RetType{IntTy}),
3320 Summary(NoEvalCall)
3321 .Case(ReturnsZero, ErrnoMustNotBeChecked, GenericSuccessMsg)
3322 .Case(ReturnsMinusOne, ErrnoNEZeroIrrelevant, GenericFailureMsg)
3323 .ArgConstraint(
3324 ArgumentCondition(0, WithinRange, Range(0, IntMax))));
3325
3326 // ssize_t recv(int sockfd, void *buf, size_t len, int flags);
3327 addToFunctionSummaryMap(
3328 "recv",
3329 Signature(ArgTypes{IntTy, VoidPtrTy, SizeTyCanonTy, IntTy},
3330 RetType{Ssize_tTy}),
3331 Summary(NoEvalCall)
3332 .Case({ReturnValueCondition(LessThanOrEq, ArgNo(2)),
3333 ReturnValueCondition(WithinRange, Range(1, Ssize_tMax))},
3334 ErrnoMustNotBeChecked, GenericSuccessMsg)
3335 .Case({ReturnValueCondition(WithinRange, SingleValue(0)),
3336 ArgumentCondition(2, WithinRange, SingleValue(0))},
3337 ErrnoMustNotBeChecked, GenericSuccessMsg)
3338 .Case(ReturnsMinusOne, ErrnoNEZeroIrrelevant, GenericFailureMsg)
3339 .ArgConstraint(ArgumentCondition(0, WithinRange, Range(0, IntMax)))
3340 .ArgConstraint(BufferSize(/*Buffer=*/ArgNo(1),
3341 /*BufSize=*/ArgNo(2))));
3342
3343 std::optional<QualType> StructMsghdrTy = lookupTy("msghdr");
3344 std::optional<QualType> StructMsghdrPtrTy = getPointerTy(StructMsghdrTy);
3345 std::optional<QualType> ConstStructMsghdrPtrTy =
3346 getPointerTy(getConstTy(StructMsghdrTy));
3347
3348 // ssize_t recvmsg(int sockfd, struct msghdr *msg, int flags);
3349 addToFunctionSummaryMap(
3350 "recvmsg",
3351 Signature(ArgTypes{IntTy, StructMsghdrPtrTy, IntTy},
3352 RetType{Ssize_tTy}),
3353 Summary(NoEvalCall)
3354 .Case({ReturnValueCondition(WithinRange, Range(1, Ssize_tMax))},
3355 ErrnoMustNotBeChecked, GenericSuccessMsg)
3356 .Case(ReturnsMinusOne, ErrnoNEZeroIrrelevant, GenericFailureMsg)
3357 .ArgConstraint(
3358 ArgumentCondition(0, WithinRange, Range(0, IntMax))));
3359
3360 // ssize_t sendmsg(int sockfd, const struct msghdr *msg, int flags);
3361 addToFunctionSummaryMap(
3362 "sendmsg",
3363 Signature(ArgTypes{IntTy, ConstStructMsghdrPtrTy, IntTy},
3364 RetType{Ssize_tTy}),
3365 Summary(NoEvalCall)
3366 .Case({ReturnValueCondition(WithinRange, Range(1, Ssize_tMax))},
3367 ErrnoMustNotBeChecked, GenericSuccessMsg)
3368 .Case(ReturnsMinusOne, ErrnoNEZeroIrrelevant, GenericFailureMsg)
3369 .ArgConstraint(
3370 ArgumentCondition(0, WithinRange, Range(0, IntMax))));
3371
3372 // int setsockopt(int socket, int level, int option_name,
3373 // const void *option_value, socklen_t option_len);
3374 addToFunctionSummaryMap(
3375 "setsockopt",
3376 Signature(ArgTypes{IntTy, IntTy, IntTy, ConstVoidPtrTy, Socklen_tTy},
3377 RetType{IntTy}),
3378 Summary(NoEvalCall)
3379 .Case(ReturnsZero, ErrnoMustNotBeChecked, GenericSuccessMsg)
3380 .Case(ReturnsMinusOne, ErrnoNEZeroIrrelevant, GenericFailureMsg)
3381 .ArgConstraint(NotNullBuffer(ArgNo(3), ArgNo(4)))
3382 .ArgConstraint(
3383 BufferSize(/*Buffer=*/ArgNo(3), /*BufSize=*/ArgNo(4)))
3384 .ArgConstraint(
3385 ArgumentCondition(4, WithinRange, Range(0, Socklen_tMax))));
3386
3387 // int getsockopt(int socket, int level, int option_name,
3388 // void *restrict option_value,
3389 // socklen_t *restrict option_len);
3390 addToFunctionSummaryMap(
3391 "getsockopt",
3392 Signature(ArgTypes{IntTy, IntTy, IntTy, VoidPtrRestrictTy,
3393 Socklen_tPtrRestrictTy},
3394 RetType{IntTy}),
3395 Summary(NoEvalCall)
3396 .Case(ReturnsZero, ErrnoMustNotBeChecked, GenericSuccessMsg)
3397 .Case(ReturnsMinusOne, ErrnoNEZeroIrrelevant, GenericFailureMsg)
3398 .ArgConstraint(NotNull(ArgNo(3)))
3399 .ArgConstraint(NotNull(ArgNo(4))));
3400
3401 // ssize_t send(int sockfd, const void *buf, size_t len, int flags);
3402 addToFunctionSummaryMap(
3403 "send",
3404 Signature(ArgTypes{IntTy, ConstVoidPtrTy, SizeTyCanonTy, IntTy},
3405 RetType{Ssize_tTy}),
3406 Summary(NoEvalCall)
3407 .Case({ReturnValueCondition(LessThanOrEq, ArgNo(2)),
3408 ReturnValueCondition(WithinRange, Range(1, Ssize_tMax))},
3409 ErrnoMustNotBeChecked, GenericSuccessMsg)
3410 .Case({ReturnValueCondition(WithinRange, SingleValue(0)),
3411 ArgumentCondition(2, WithinRange, SingleValue(0))},
3412 ErrnoMustNotBeChecked, GenericSuccessMsg)
3413 .Case(ReturnsMinusOne, ErrnoNEZeroIrrelevant, GenericFailureMsg)
3414 .ArgConstraint(ArgumentCondition(0, WithinRange, Range(0, IntMax)))
3415 .ArgConstraint(BufferSize(/*Buffer=*/ArgNo(1),
3416 /*BufSize=*/ArgNo(2))));
3417
3418 // int socketpair(int domain, int type, int protocol, int sv[2]);
3419 addToFunctionSummaryMap(
3420 "socketpair",
3421 Signature(ArgTypes{IntTy, IntTy, IntTy, IntPtrTy}, RetType{IntTy}),
3422 Summary(NoEvalCall)
3423 .Case(ReturnsZero, ErrnoMustNotBeChecked, GenericSuccessMsg)
3424 .Case(ReturnsMinusOne, ErrnoNEZeroIrrelevant, GenericFailureMsg)
3425 .ArgConstraint(NotNull(ArgNo(3))));
3426
3427 // int shutdown(int socket, int how);
3428 addToFunctionSummaryMap(
3429 "shutdown", Signature(ArgTypes{IntTy, IntTy}, RetType{IntTy}),
3430 Summary(NoEvalCall)
3431 .Case(ReturnsZero, ErrnoMustNotBeChecked, GenericSuccessMsg)
3432 .Case(ReturnsMinusOne, ErrnoNEZeroIrrelevant, GenericFailureMsg)
3433 .ArgConstraint(
3434 ArgumentCondition(0, WithinRange, Range(0, IntMax))));
3435
3436 // int getnameinfo(const struct sockaddr *restrict sa, socklen_t salen,
3437 // char *restrict node, socklen_t nodelen,
3438 // char *restrict service,
3439 // socklen_t servicelen, int flags);
3440 //
3441 // This is defined in netdb.h. And contrary to 'socket.h', the sockaddr
3442 // parameter is never handled as a transparent union in netdb.h
3443 addToFunctionSummaryMap(
3444 "getnameinfo",
3445 Signature(ArgTypes{ConstStructSockaddrPtrRestrictTy, Socklen_tTy,
3446 CharPtrRestrictTy, Socklen_tTy, CharPtrRestrictTy,
3447 Socklen_tTy, IntTy},
3448 RetType{IntTy}),
3449 Summary(NoEvalCall)
3450 .ArgConstraint(
3451 BufferSize(/*Buffer=*/ArgNo(0), /*BufSize=*/ArgNo(1)))
3452 .ArgConstraint(
3453 ArgumentCondition(1, WithinRange, Range(0, Socklen_tMax)))
3454 .ArgConstraint(
3455 BufferSize(/*Buffer=*/ArgNo(2), /*BufSize=*/ArgNo(3)))
3456 .ArgConstraint(
3457 ArgumentCondition(3, WithinRange, Range(0, Socklen_tMax)))
3458 .ArgConstraint(
3459 BufferSize(/*Buffer=*/ArgNo(4), /*BufSize=*/ArgNo(5)))
3460 .ArgConstraint(
3461 ArgumentCondition(5, WithinRange, Range(0, Socklen_tMax))));
3462
3463 std::optional<QualType> StructUtimbufTy = lookupTy("utimbuf");
3464 std::optional<QualType> StructUtimbufPtrTy = getPointerTy(StructUtimbufTy);
3465
3466 // int utime(const char *filename, struct utimbuf *buf);
3467 addToFunctionSummaryMap(
3468 "utime",
3469 Signature(ArgTypes{ConstCharPtrTy, StructUtimbufPtrTy}, RetType{IntTy}),
3470 Summary(NoEvalCall)
3471 .Case(ReturnsZero, ErrnoMustNotBeChecked, GenericSuccessMsg)
3472 .Case(ReturnsMinusOne, ErrnoNEZeroIrrelevant, GenericFailureMsg)
3473 .ArgConstraint(NotNull(ArgNo(0))));
3474
3475 std::optional<QualType> StructTimespecTy = lookupTy("timespec");
3476 std::optional<QualType> StructTimespecPtrTy =
3477 getPointerTy(StructTimespecTy);
3478 std::optional<QualType> ConstStructTimespecPtrTy =
3479 getPointerTy(getConstTy(StructTimespecTy));
3480
3481 // int futimens(int fd, const struct timespec times[2]);
3482 addToFunctionSummaryMap(
3483 "futimens",
3484 Signature(ArgTypes{IntTy, ConstStructTimespecPtrTy}, RetType{IntTy}),
3485 Summary(NoEvalCall)
3486 .Case(ReturnsZero, ErrnoMustNotBeChecked, GenericSuccessMsg)
3487 .Case(ReturnsMinusOne, ErrnoNEZeroIrrelevant, GenericFailureMsg)
3488 .ArgConstraint(
3489 ArgumentCondition(0, WithinRange, Range(0, IntMax))));
3490
3491 // int utimensat(int dirfd, const char *pathname,
3492 // const struct timespec times[2], int flags);
3493 addToFunctionSummaryMap(
3494 "utimensat",
3495 Signature(
3496 ArgTypes{IntTy, ConstCharPtrTy, ConstStructTimespecPtrTy, IntTy},
3497 RetType{IntTy}),
3498 Summary(NoEvalCall)
3499 .Case(ReturnsZero, ErrnoMustNotBeChecked, GenericSuccessMsg)
3500 .Case(ReturnsMinusOne, ErrnoNEZeroIrrelevant, GenericFailureMsg)
3501 .ArgConstraint(NotNull(ArgNo(1))));
3502
3503 std::optional<QualType> StructTimevalTy = lookupTy("timeval");
3504 std::optional<QualType> ConstStructTimevalPtrTy =
3505 getPointerTy(getConstTy(StructTimevalTy));
3506
3507 // int utimes(const char *filename, const struct timeval times[2]);
3508 addToFunctionSummaryMap(
3509 "utimes",
3510 Signature(ArgTypes{ConstCharPtrTy, ConstStructTimevalPtrTy},
3511 RetType{IntTy}),
3512 Summary(NoEvalCall)
3513 .Case(ReturnsZero, ErrnoMustNotBeChecked, GenericSuccessMsg)
3514 .Case(ReturnsMinusOne, ErrnoNEZeroIrrelevant, GenericFailureMsg)
3515 .ArgConstraint(NotNull(ArgNo(0))));
3516
3517 // int nanosleep(const struct timespec *rqtp, struct timespec *rmtp);
3518 addToFunctionSummaryMap(
3519 "nanosleep",
3520 Signature(ArgTypes{ConstStructTimespecPtrTy, StructTimespecPtrTy},
3521 RetType{IntTy}),
3522 Summary(NoEvalCall)
3523 .Case(ReturnsZero, ErrnoMustNotBeChecked, GenericSuccessMsg)
3524 .Case(ReturnsMinusOne, ErrnoNEZeroIrrelevant, GenericFailureMsg)
3525 .ArgConstraint(NotNull(ArgNo(0))));
3526
3527 std::optional<QualType> Time_tTy = lookupTy("time_t");
3528 std::optional<QualType> ConstTime_tPtrTy =
3529 getPointerTy(getConstTy(Time_tTy));
3530 std::optional<QualType> ConstTime_tPtrRestrictTy =
3531 getRestrictTy(ConstTime_tPtrTy);
3532
3533 std::optional<QualType> StructTmTy = lookupTy("tm");
3534 std::optional<QualType> StructTmPtrTy = getPointerTy(StructTmTy);
3535 std::optional<QualType> StructTmPtrRestrictTy =
3536 getRestrictTy(StructTmPtrTy);
3537 std::optional<QualType> ConstStructTmPtrTy =
3538 getPointerTy(getConstTy(StructTmTy));
3539 std::optional<QualType> ConstStructTmPtrRestrictTy =
3540 getRestrictTy(ConstStructTmPtrTy);
3541
3542 // struct tm * localtime(const time_t *tp);
3543 addToFunctionSummaryMap(
3544 "localtime",
3545 Signature(ArgTypes{ConstTime_tPtrTy}, RetType{StructTmPtrTy}),
3546 Summary(NoEvalCall).ArgConstraint(NotNull(ArgNo(0))));
3547
3548 // struct tm *localtime_r(const time_t *restrict timer,
3549 // struct tm *restrict result);
3550 addToFunctionSummaryMap(
3551 "localtime_r",
3552 Signature(ArgTypes{ConstTime_tPtrRestrictTy, StructTmPtrRestrictTy},
3553 RetType{StructTmPtrTy}),
3554 Summary(NoEvalCall)
3555 .ArgConstraint(NotNull(ArgNo(0)))
3556 .ArgConstraint(NotNull(ArgNo(1))));
3557
3558 // char *asctime_r(const struct tm *restrict tm, char *restrict buf);
3559 addToFunctionSummaryMap(
3560 "asctime_r",
3561 Signature(ArgTypes{ConstStructTmPtrRestrictTy, CharPtrRestrictTy},
3562 RetType{CharPtrTy}),
3563 Summary(NoEvalCall)
3564 .ArgConstraint(NotNull(ArgNo(0)))
3565 .ArgConstraint(NotNull(ArgNo(1)))
3566 .ArgConstraint(BufferSize(/*Buffer=*/ArgNo(1),
3567 /*MinBufSize=*/BVF.getValue(26, IntTy))));
3568
3569 // char *ctime_r(const time_t *timep, char *buf);
3570 addToFunctionSummaryMap(
3571 "ctime_r",
3572 Signature(ArgTypes{ConstTime_tPtrTy, CharPtrTy}, RetType{CharPtrTy}),
3573 Summary(NoEvalCall)
3574 .ArgConstraint(NotNull(ArgNo(0)))
3575 .ArgConstraint(NotNull(ArgNo(1)))
3576 .ArgConstraint(BufferSize(
3577 /*Buffer=*/ArgNo(1),
3578 /*MinBufSize=*/BVF.getValue(26, IntTy))));
3579
3580 // struct tm *gmtime_r(const time_t *restrict timer,
3581 // struct tm *restrict result);
3582 addToFunctionSummaryMap(
3583 "gmtime_r",
3584 Signature(ArgTypes{ConstTime_tPtrRestrictTy, StructTmPtrRestrictTy},
3585 RetType{StructTmPtrTy}),
3586 Summary(NoEvalCall)
3587 .ArgConstraint(NotNull(ArgNo(0)))
3588 .ArgConstraint(NotNull(ArgNo(1))));
3589
3590 // struct tm * gmtime(const time_t *tp);
3591 addToFunctionSummaryMap(
3592 "gmtime", Signature(ArgTypes{ConstTime_tPtrTy}, RetType{StructTmPtrTy}),
3593 Summary(NoEvalCall).ArgConstraint(NotNull(ArgNo(0))));
3594
3595 std::optional<QualType> Clockid_tTy = lookupTy("clockid_t");
3596
3597 // int clock_gettime(clockid_t clock_id, struct timespec *tp);
3598 addToFunctionSummaryMap(
3599 "clock_gettime",
3600 Signature(ArgTypes{Clockid_tTy, StructTimespecPtrTy}, RetType{IntTy}),
3601 Summary(NoEvalCall)
3602 .Case(ReturnsZero, ErrnoMustNotBeChecked, GenericSuccessMsg)
3603 .Case(ReturnsMinusOne, ErrnoNEZeroIrrelevant, GenericFailureMsg)
3604 .ArgConstraint(NotNull(ArgNo(1))));
3605
3606 std::optional<QualType> StructItimervalTy = lookupTy("itimerval");
3607 std::optional<QualType> StructItimervalPtrTy =
3608 getPointerTy(StructItimervalTy);
3609
3610 // int getitimer(int which, struct itimerval *curr_value);
3611 addToFunctionSummaryMap(
3612 "getitimer",
3613 Signature(ArgTypes{IntTy, StructItimervalPtrTy}, RetType{IntTy}),
3614 Summary(NoEvalCall)
3615 .Case(ReturnsZero, ErrnoMustNotBeChecked, GenericSuccessMsg)
3616 .Case(ReturnsMinusOne, ErrnoNEZeroIrrelevant, GenericFailureMsg)
3617 .ArgConstraint(NotNull(ArgNo(1))));
3618
3619 std::optional<QualType> Pthread_cond_tTy = lookupTy("pthread_cond_t");
3620 std::optional<QualType> Pthread_cond_tPtrTy =
3621 getPointerTy(Pthread_cond_tTy);
3622 std::optional<QualType> Pthread_tTy = lookupTy("pthread_t");
3623 std::optional<QualType> Pthread_tPtrTy = getPointerTy(Pthread_tTy);
3624 std::optional<QualType> Pthread_tPtrRestrictTy =
3625 getRestrictTy(Pthread_tPtrTy);
3626 std::optional<QualType> Pthread_mutex_tTy = lookupTy("pthread_mutex_t");
3627 std::optional<QualType> Pthread_mutex_tPtrTy =
3628 getPointerTy(Pthread_mutex_tTy);
3629 std::optional<QualType> Pthread_mutex_tPtrRestrictTy =
3630 getRestrictTy(Pthread_mutex_tPtrTy);
3631 std::optional<QualType> Pthread_attr_tTy = lookupTy("pthread_attr_t");
3632 std::optional<QualType> Pthread_attr_tPtrTy =
3633 getPointerTy(Pthread_attr_tTy);
3634 std::optional<QualType> ConstPthread_attr_tPtrTy =
3635 getPointerTy(getConstTy(Pthread_attr_tTy));
3636 std::optional<QualType> ConstPthread_attr_tPtrRestrictTy =
3637 getRestrictTy(ConstPthread_attr_tPtrTy);
3638 std::optional<QualType> Pthread_mutexattr_tTy =
3639 lookupTy("pthread_mutexattr_t");
3640 std::optional<QualType> ConstPthread_mutexattr_tPtrTy =
3641 getPointerTy(getConstTy(Pthread_mutexattr_tTy));
3642 std::optional<QualType> ConstPthread_mutexattr_tPtrRestrictTy =
3643 getRestrictTy(ConstPthread_mutexattr_tPtrTy);
3644
3645 QualType PthreadStartRoutineTy = getPointerTy(
3646 ACtx.getFunctionType(/*ResultTy=*/VoidPtrTy, /*Args=*/VoidPtrTy,
3647 FunctionProtoType::ExtProtoInfo()));
3648
3649 // int pthread_cond_signal(pthread_cond_t *cond);
3650 // int pthread_cond_broadcast(pthread_cond_t *cond);
3651 addToFunctionSummaryMap(
3652 {"pthread_cond_signal", "pthread_cond_broadcast"},
3653 Signature(ArgTypes{Pthread_cond_tPtrTy}, RetType{IntTy}),
3654 Summary(NoEvalCall).ArgConstraint(NotNull(ArgNo(0))));
3655
3656 // int pthread_create(pthread_t *restrict thread,
3657 // const pthread_attr_t *restrict attr,
3658 // void *(*start_routine)(void*), void *restrict arg);
3659 addToFunctionSummaryMap(
3660 "pthread_create",
3661 Signature(ArgTypes{Pthread_tPtrRestrictTy,
3662 ConstPthread_attr_tPtrRestrictTy,
3663 PthreadStartRoutineTy, VoidPtrRestrictTy},
3664 RetType{IntTy}),
3665 Summary(NoEvalCall)
3666 .ArgConstraint(NotNull(ArgNo(0)))
3667 .ArgConstraint(NotNull(ArgNo(2))));
3668
3669 // int pthread_attr_destroy(pthread_attr_t *attr);
3670 // int pthread_attr_init(pthread_attr_t *attr);
3671 addToFunctionSummaryMap(
3672 {"pthread_attr_destroy", "pthread_attr_init"},
3673 Signature(ArgTypes{Pthread_attr_tPtrTy}, RetType{IntTy}),
3674 Summary(NoEvalCall).ArgConstraint(NotNull(ArgNo(0))));
3675
3676 // int pthread_attr_getstacksize(const pthread_attr_t *restrict attr,
3677 // size_t *restrict stacksize);
3678 // int pthread_attr_getguardsize(const pthread_attr_t *restrict attr,
3679 // size_t *restrict guardsize);
3680 addToFunctionSummaryMap(
3681 {"pthread_attr_getstacksize", "pthread_attr_getguardsize"},
3682 Signature(ArgTypes{ConstPthread_attr_tPtrRestrictTy, SizePtrRestrictTy},
3683 RetType{IntTy}),
3684 Summary(NoEvalCall)
3685 .ArgConstraint(NotNull(ArgNo(0)))
3686 .ArgConstraint(NotNull(ArgNo(1))));
3687
3688 // int pthread_attr_setstacksize(pthread_attr_t *attr, size_t stacksize);
3689 // int pthread_attr_setguardsize(pthread_attr_t *attr, size_t guardsize);
3690 addToFunctionSummaryMap(
3691 {"pthread_attr_setstacksize", "pthread_attr_setguardsize"},
3692 Signature(ArgTypes{Pthread_attr_tPtrTy, SizeTyCanonTy}, RetType{IntTy}),
3693 Summary(NoEvalCall)
3694 .ArgConstraint(NotNull(ArgNo(0)))
3695 .ArgConstraint(
3696 ArgumentCondition(1, WithinRange, Range(0, SizeMax))));
3697
3698 // int pthread_mutex_init(pthread_mutex_t *restrict mutex, const
3699 // pthread_mutexattr_t *restrict attr);
3700 addToFunctionSummaryMap(
3701 "pthread_mutex_init",
3702 Signature(ArgTypes{Pthread_mutex_tPtrRestrictTy,
3703 ConstPthread_mutexattr_tPtrRestrictTy},
3704 RetType{IntTy}),
3705 Summary(NoEvalCall).ArgConstraint(NotNull(ArgNo(0))));
3706
3707 // int pthread_mutex_destroy(pthread_mutex_t *mutex);
3708 // int pthread_mutex_lock(pthread_mutex_t *mutex);
3709 // int pthread_mutex_trylock(pthread_mutex_t *mutex);
3710 // int pthread_mutex_unlock(pthread_mutex_t *mutex);
3711 addToFunctionSummaryMap(
3712 {"pthread_mutex_destroy", "pthread_mutex_lock", "pthread_mutex_trylock",
3713 "pthread_mutex_unlock"},
3714 Signature(ArgTypes{Pthread_mutex_tPtrTy}, RetType{IntTy}),
3715 Summary(NoEvalCall).ArgConstraint(NotNull(ArgNo(0))));
3716 }
3717
3718 // Functions for testing.
3719 if (AddTestFunctions) {
3720 const RangeInt IntMin = BVF.getMinValue(IntTy)->getLimitedValue();
3721
3722 addToFunctionSummaryMap(
3723 "__not_null", Signature(ArgTypes{IntPtrTy}, RetType{IntTy}),
3724 Summary(EvalCallAsPure).ArgConstraint(NotNull(ArgNo(0))));
3725
3726 addToFunctionSummaryMap(
3727 "__not_null_buffer",
3728 Signature(ArgTypes{VoidPtrTy, IntTy, IntTy}, RetType{IntTy}),
3729 Summary(EvalCallAsPure)
3730 .ArgConstraint(NotNullBuffer(ArgNo(0), ArgNo(1), ArgNo(2))));
3731
3732 // Test inside range constraints.
3733 addToFunctionSummaryMap(
3734 "__single_val_0", Signature(ArgTypes{IntTy}, RetType{IntTy}),
3735 Summary(EvalCallAsPure)
3736 .ArgConstraint(ArgumentCondition(0U, WithinRange, SingleValue(0))));
3737 addToFunctionSummaryMap(
3738 "__single_val_1", Signature(ArgTypes{IntTy}, RetType{IntTy}),
3739 Summary(EvalCallAsPure)
3740 .ArgConstraint(ArgumentCondition(0U, WithinRange, SingleValue(1))));
3741 addToFunctionSummaryMap(
3742 "__range_1_2", Signature(ArgTypes{IntTy}, RetType{IntTy}),
3743 Summary(EvalCallAsPure)
3744 .ArgConstraint(ArgumentCondition(0U, WithinRange, Range(1, 2))));
3745 addToFunctionSummaryMap(
3746 "__range_m1_1", Signature(ArgTypes{IntTy}, RetType{IntTy}),
3747 Summary(EvalCallAsPure)
3748 .ArgConstraint(ArgumentCondition(0U, WithinRange, Range(-1, 1))));
3749 addToFunctionSummaryMap(
3750 "__range_m2_m1", Signature(ArgTypes{IntTy}, RetType{IntTy}),
3751 Summary(EvalCallAsPure)
3752 .ArgConstraint(ArgumentCondition(0U, WithinRange, Range(-2, -1))));
3753 addToFunctionSummaryMap(
3754 "__range_m10_10", Signature(ArgTypes{IntTy}, RetType{IntTy}),
3755 Summary(EvalCallAsPure)
3756 .ArgConstraint(ArgumentCondition(0U, WithinRange, Range(-10, 10))));
3757 addToFunctionSummaryMap("__range_m1_inf",
3758 Signature(ArgTypes{IntTy}, RetType{IntTy}),
3759 Summary(EvalCallAsPure)
3760 .ArgConstraint(ArgumentCondition(
3761 0U, WithinRange, Range(-1, IntMax))));
3762 addToFunctionSummaryMap("__range_0_inf",
3763 Signature(ArgTypes{IntTy}, RetType{IntTy}),
3764 Summary(EvalCallAsPure)
3765 .ArgConstraint(ArgumentCondition(
3766 0U, WithinRange, Range(0, IntMax))));
3767 addToFunctionSummaryMap("__range_1_inf",
3768 Signature(ArgTypes{IntTy}, RetType{IntTy}),
3769 Summary(EvalCallAsPure)
3770 .ArgConstraint(ArgumentCondition(
3771 0U, WithinRange, Range(1, IntMax))));
3772 addToFunctionSummaryMap("__range_minf_m1",
3773 Signature(ArgTypes{IntTy}, RetType{IntTy}),
3774 Summary(EvalCallAsPure)
3775 .ArgConstraint(ArgumentCondition(
3776 0U, WithinRange, Range(IntMin, -1))));
3777 addToFunctionSummaryMap("__range_minf_0",
3778 Signature(ArgTypes{IntTy}, RetType{IntTy}),
3779 Summary(EvalCallAsPure)
3780 .ArgConstraint(ArgumentCondition(
3781 0U, WithinRange, Range(IntMin, 0))));
3782 addToFunctionSummaryMap("__range_minf_1",
3783 Signature(ArgTypes{IntTy}, RetType{IntTy}),
3784 Summary(EvalCallAsPure)
3785 .ArgConstraint(ArgumentCondition(
3786 0U, WithinRange, Range(IntMin, 1))));
3787 addToFunctionSummaryMap("__range_1_2__4_6",
3788 Signature(ArgTypes{IntTy}, RetType{IntTy}),
3789 Summary(EvalCallAsPure)
3790 .ArgConstraint(ArgumentCondition(
3791 0U, WithinRange, Range({1, 2}, {4, 6}))));
3792 addToFunctionSummaryMap(
3793 "__range_1_2__4_inf", Signature(ArgTypes{IntTy}, RetType{IntTy}),
3794 Summary(EvalCallAsPure)
3795 .ArgConstraint(ArgumentCondition(0U, WithinRange,
3796 Range({1, 2}, {4, IntMax}))));
3797
3798 // Test out of range constraints.
3799 addToFunctionSummaryMap(
3800 "__single_val_out_0", Signature(ArgTypes{IntTy}, RetType{IntTy}),
3801 Summary(EvalCallAsPure)
3802 .ArgConstraint(ArgumentCondition(0U, OutOfRange, SingleValue(0))));
3803 addToFunctionSummaryMap(
3804 "__single_val_out_1", Signature(ArgTypes{IntTy}, RetType{IntTy}),
3805 Summary(EvalCallAsPure)
3806 .ArgConstraint(ArgumentCondition(0U, OutOfRange, SingleValue(1))));
3807 addToFunctionSummaryMap(
3808 "__range_out_1_2", Signature(ArgTypes{IntTy}, RetType{IntTy}),
3809 Summary(EvalCallAsPure)
3810 .ArgConstraint(ArgumentCondition(0U, OutOfRange, Range(1, 2))));
3811 addToFunctionSummaryMap(
3812 "__range_out_m1_1", Signature(ArgTypes{IntTy}, RetType{IntTy}),
3813 Summary(EvalCallAsPure)
3814 .ArgConstraint(ArgumentCondition(0U, OutOfRange, Range(-1, 1))));
3815 addToFunctionSummaryMap(
3816 "__range_out_m2_m1", Signature(ArgTypes{IntTy}, RetType{IntTy}),
3817 Summary(EvalCallAsPure)
3818 .ArgConstraint(ArgumentCondition(0U, OutOfRange, Range(-2, -1))));
3819 addToFunctionSummaryMap(
3820 "__range_out_m10_10", Signature(ArgTypes{IntTy}, RetType{IntTy}),
3821 Summary(EvalCallAsPure)
3822 .ArgConstraint(ArgumentCondition(0U, OutOfRange, Range(-10, 10))));
3823 addToFunctionSummaryMap("__range_out_m1_inf",
3824 Signature(ArgTypes{IntTy}, RetType{IntTy}),
3825 Summary(EvalCallAsPure)
3826 .ArgConstraint(ArgumentCondition(
3827 0U, OutOfRange, Range(-1, IntMax))));
3828 addToFunctionSummaryMap("__range_out_0_inf",
3829 Signature(ArgTypes{IntTy}, RetType{IntTy}),
3830 Summary(EvalCallAsPure)
3831 .ArgConstraint(ArgumentCondition(
3832 0U, OutOfRange, Range(0, IntMax))));
3833 addToFunctionSummaryMap("__range_out_1_inf",
3834 Signature(ArgTypes{IntTy}, RetType{IntTy}),
3835 Summary(EvalCallAsPure)
3836 .ArgConstraint(ArgumentCondition(
3837 0U, OutOfRange, Range(1, IntMax))));
3838 addToFunctionSummaryMap("__range_out_minf_m1",
3839 Signature(ArgTypes{IntTy}, RetType{IntTy}),
3840 Summary(EvalCallAsPure)
3841 .ArgConstraint(ArgumentCondition(
3842 0U, OutOfRange, Range(IntMin, -1))));
3843 addToFunctionSummaryMap("__range_out_minf_0",
3844 Signature(ArgTypes{IntTy}, RetType{IntTy}),
3845 Summary(EvalCallAsPure)
3846 .ArgConstraint(ArgumentCondition(
3847 0U, OutOfRange, Range(IntMin, 0))));
3848 addToFunctionSummaryMap("__range_out_minf_1",
3849 Signature(ArgTypes{IntTy}, RetType{IntTy}),
3850 Summary(EvalCallAsPure)
3851 .ArgConstraint(ArgumentCondition(
3852 0U, OutOfRange, Range(IntMin, 1))));
3853 addToFunctionSummaryMap("__range_out_1_2__4_6",
3854 Signature(ArgTypes{IntTy}, RetType{IntTy}),
3855 Summary(EvalCallAsPure)
3856 .ArgConstraint(ArgumentCondition(
3857 0U, OutOfRange, Range({1, 2}, {4, 6}))));
3858 addToFunctionSummaryMap(
3859 "__range_out_1_2__4_inf", Signature(ArgTypes{IntTy}, RetType{IntTy}),
3860 Summary(EvalCallAsPure)
3861 .ArgConstraint(
3862 ArgumentCondition(0U, OutOfRange, Range({1, 2}, {4, IntMax}))));
3863
3864 // Test range kind.
3865 addToFunctionSummaryMap(
3866 "__within", Signature(ArgTypes{IntTy}, RetType{IntTy}),
3867 Summary(EvalCallAsPure)
3868 .ArgConstraint(ArgumentCondition(0U, WithinRange, SingleValue(1))));
3869 addToFunctionSummaryMap(
3870 "__out_of", Signature(ArgTypes{IntTy}, RetType{IntTy}),
3871 Summary(EvalCallAsPure)
3872 .ArgConstraint(ArgumentCondition(0U, OutOfRange, SingleValue(1))));
3873
3874 addToFunctionSummaryMap(
3875 "__two_constrained_args",
3876 Signature(ArgTypes{IntTy, IntTy}, RetType{IntTy}),
3877 Summary(EvalCallAsPure)
3878 .ArgConstraint(ArgumentCondition(0U, WithinRange, SingleValue(1)))
3879 .ArgConstraint(ArgumentCondition(1U, WithinRange, SingleValue(1))));
3880 addToFunctionSummaryMap(
3881 "__arg_constrained_twice", Signature(ArgTypes{IntTy}, RetType{IntTy}),
3882 Summary(EvalCallAsPure)
3883 .ArgConstraint(ArgumentCondition(0U, OutOfRange, SingleValue(1)))
3884 .ArgConstraint(ArgumentCondition(0U, OutOfRange, SingleValue(2))));
3885 addToFunctionSummaryMap(
3886 "__defaultparam",
3887 Signature(ArgTypes{Irrelevant, IntTy}, RetType{IntTy}),
3888 Summary(EvalCallAsPure).ArgConstraint(NotNull(ArgNo(0))));
3889 addToFunctionSummaryMap(
3890 "__variadic",
3891 Signature(ArgTypes{VoidPtrTy, ConstCharPtrTy}, RetType{IntTy}),
3892 Summary(EvalCallAsPure)
3893 .ArgConstraint(NotNull(ArgNo(0)))
3894 .ArgConstraint(NotNull(ArgNo(1))));
3895 addToFunctionSummaryMap(
3896 "__buf_size_arg_constraint",
3897 Signature(ArgTypes{ConstVoidPtrTy, SizeTyCanonTy}, RetType{IntTy}),
3898 Summary(EvalCallAsPure)
3899 .ArgConstraint(
3900 BufferSize(/*Buffer=*/ArgNo(0), /*BufSize=*/ArgNo(1))));
3901 addToFunctionSummaryMap(
3902 "__buf_size_arg_constraint_mul",
3903 Signature(ArgTypes{ConstVoidPtrTy, SizeTyCanonTy, SizeTyCanonTy},
3904 RetType{IntTy}),
3905 Summary(EvalCallAsPure)
3906 .ArgConstraint(BufferSize(/*Buffer=*/ArgNo(0), /*BufSize=*/ArgNo(1),
3907 /*BufSizeMultiplier=*/ArgNo(2))));
3908 addToFunctionSummaryMap(
3909 "__buf_size_arg_constraint_concrete",
3910 Signature(ArgTypes{ConstVoidPtrTy}, RetType{IntTy}),
3911 Summary(EvalCallAsPure)
3912 .ArgConstraint(BufferSize(/*Buffer=*/ArgNo(0),
3913 /*BufSize=*/BVF.getValue(10, IntTy))));
3914 addToFunctionSummaryMap(
3915 {"__test_restrict_param_0", "__test_restrict_param_1",
3916 "__test_restrict_param_2"},
3917 Signature(ArgTypes{VoidPtrRestrictTy}, RetType{VoidTy}),
3918 Summary(EvalCallAsPure));
3919
3920 // Test the application of cases.
3921 addToFunctionSummaryMap(
3922 "__test_case_note", Signature(ArgTypes{}, RetType{IntTy}),
3923 Summary(EvalCallAsPure)
3924 .Case({ReturnValueCondition(WithinRange, SingleValue(0))},
3925 ErrnoIrrelevant, "Function returns 0")
3926 .Case({ReturnValueCondition(WithinRange, SingleValue(1))},
3927 ErrnoIrrelevant, "Function returns 1"));
3928 addToFunctionSummaryMap(
3929 "__test_case_range_1_2__4_6",
3930 Signature(ArgTypes{IntTy}, RetType{IntTy}),
3931 Summary(EvalCallAsPure)
3932 .Case({ArgumentCondition(0U, WithinRange,
3933 IntRangeVector{{IntMin, 0}, {3, 3}}),
3934 ReturnValueCondition(WithinRange, SingleValue(1))},
3935 ErrnoIrrelevant)
3936 .Case({ArgumentCondition(0U, WithinRange,
3937 IntRangeVector{{3, 3}, {7, IntMax}}),
3938 ReturnValueCondition(WithinRange, SingleValue(2))},
3939 ErrnoIrrelevant)
3940 .Case({ArgumentCondition(0U, WithinRange,
3941 IntRangeVector{{IntMin, 0}, {7, IntMax}}),
3942 ReturnValueCondition(WithinRange, SingleValue(3))},
3943 ErrnoIrrelevant)
3944 .Case({ArgumentCondition(
3945 0U, WithinRange,
3946 IntRangeVector{{IntMin, 0}, {3, 3}, {7, IntMax}}),
3947 ReturnValueCondition(WithinRange, SingleValue(4))},
3948 ErrnoIrrelevant));
3949 }
3950}
3951
3952void ento::registerStdCLibraryFunctionsChecker(CheckerManager &mgr) {
3953 auto *Checker = mgr.registerChecker<StdLibraryFunctionsChecker>();
3954 Checker->CheckName = mgr.getCurrentCheckerName();
3955 const AnalyzerOptions &Opts = mgr.getAnalyzerOptions();
3956 Checker->DisplayLoadedSummaries =
3957 Opts.getCheckerBooleanOption(Checker, "DisplayLoadedSummaries");
3958 Checker->ModelPOSIX = Opts.getCheckerBooleanOption(Checker, "ModelPOSIX");
3959 Checker->ShouldAssumeControlledEnvironment =
3960 Opts.ShouldAssumeControlledEnvironment;
3961}
3962
3963bool ento::shouldRegisterStdCLibraryFunctionsChecker(
3964 const CheckerManager &mgr) {
3965 return true;
3966}
3967
3968void ento::registerStdCLibraryFunctionsTesterChecker(CheckerManager &mgr) {
3969 auto *Checker = mgr.getChecker<StdLibraryFunctionsChecker>();
3970 Checker->AddTestFunctions = true;
3971}
3972
3973bool ento::shouldRegisterStdCLibraryFunctionsTesterChecker(
3974 const CheckerManager &mgr) {
3975 return true;
3976}
#define V(N, I)
static std::string getFunctionName(const CallEvent &Call)
static bool isInvalid(LocType Loc, bool *Invalid)
TranslationUnitDecl * getTranslationUnitDecl() const
CanQualType LongTy
QualType getPointerType(QualType T) const
Return the uniqued reference to the type for a pointer to the specified type.
IdentifierTable & Idents
Definition ASTContext.h:802
const LangOptions & getLangOpts() const
Definition ASTContext.h:959
CanQualType getCanonicalSizeType() const
CanQualType BoolTy
QualType getRestrictType(QualType T) const
Return the uniqued reference to the type for a restrict qualified type.
CanQualType CharTy
CanQualType IntTy
CanQualType getCanonicalTypeDeclType(const TypeDecl *TD) const
CanQualType VoidTy
CanQualType UnsignedCharTy
CanQualType UnsignedIntTy
QualType getFunctionType(QualType ResultTy, ArrayRef< QualType > Args, const FunctionProtoType::ExtProtoInfo &EPI) const
Return a normal function type with a typed argument list.
CanQualType WCharTy
static Opcode negateComparisonOp(Opcode Opc)
Definition Expr.h:4147
BinaryOperatorKind Opcode
Definition Expr.h:4046
lookup_result lookup(DeclarationName Name) const
lookup - Find the declarations (if any) with the given Name in this context.
ASTContext & getASTContext() const LLVM_READONLY
Definition DeclBase.cpp:547
void print(raw_ostream &Out, unsigned Indentation=0, bool PrintInstantiation=false) const
const ParmVarDecl * getParamDecl(unsigned i) const
Definition Decl.h:2815
QualType getReturnType() const
Definition Decl.h:2863
FunctionDecl * getCanonicalDecl() override
Retrieves the "canonical" declaration of the given declaration.
Definition Decl.cpp:3721
unsigned getNumParams() const
Return the number of parameters this function must have based on its FunctionType.
Definition Decl.cpp:3800
size_t param_size() const
Definition Decl.h:2808
IdentifierInfo & get(StringRef Name)
Return the identifier token info for the specified named identifier.
QualType withConst() const
Definition TypeBase.h:1174
bool isNull() const
Return true if this QualType doesn't point to a type yet.
Definition TypeBase.h:1004
QualType getCanonicalType() const
Definition TypeBase.h:8499
void removeLocalRestrict()
Definition TypeBase.h:8559
bool isCanonical() const
Definition TypeBase.h:8504
bool isVoidType() const
Definition TypeBase.h:9050
QualType getType() const
Definition Decl.h:723
APSIntPtr getMaxValue(const llvm::APSInt &v)
APSIntPtr getMinValue(const llvm::APSInt &v)
const AnalyzerOptions & getAnalyzerOptions() const
CHECKER * registerChecker(AT &&...Args)
Register a single-part checker (derived from Checker): construct its singleton instance,...
CheckerNameRef getCurrentCheckerName() const
CHECKER * getChecker(AT &&...Args)
If the the singleton instance of a checker class is not yet constructed, then construct it (with the ...
Simple checker classes that implement one frontend (i.e.
Definition Checker.h:565
ProgramStateRef assumeInclusiveRange(ProgramStateRef State, NonLoc Value, const llvm::APSInt &From, const llvm::APSInt &To, bool InBound)
const ProgramStateRef & getState() const
unsigned succ_size() const
const ExplodedNode * getErrorNode() const
bool isInteresting(SymbolRef sym) const
ConstraintManager & getConstraintManager()
BasicValueFactory & getBasicValueFactory()
ASTContext & getContext()
nonloc::ConcreteInt makeIntVal(const IntegerLiteral *integer)
SVal evalCast(SVal V, QualType CastTy, QualType OriginalTy)
Cast a given SVal to another SVal using given QualType's.
QualType getConditionType() const
SVal evalBinOp(ProgramStateRef state, BinaryOperator::Opcode op, SVal lhs, SVal rhs, QualType type)
DefinedOrUnknownSVal conjureSymbolVal(const void *symbolTag, ConstCFGElementRef elem, const StackFrame *SF, unsigned count)
Create a new symbol with a unique 'name'.
std::optional< T > getAs() const
Convert to the specified SVal type, returning std::nullopt if this SVal is not of the desired type.
Definition SVals.h:87
T castAs() const
Convert to the specified SVal type, asserting that this SVal is of the desired type.
Definition SVals.h:83
bool trackExpressionValue(const ExplodedNode *N, const Expr *E, PathSensitiveBugReport &R, TrackingOptions Opts={})
Attempts to add visitors to track expression value back to its point of origin.
std::optional< Loc > getErrnoLoc(ProgramStateRef State)
Returns the location that points to the MemoryRegion where the 'errno' value is stored.
ProgramStateRef setErrnoForStdSuccess(ProgramStateRef State, CheckerContext &C)
Set errno state for the common case when a standard function is successful.
ProgramStateRef setErrnoStdMustBeChecked(ProgramStateRef State, CheckerContext &C, ConstCFGElementRef Elem)
Set errno state for the common case when a standard function indicates failure only by errno.
ProgramStateRef setErrnoState(ProgramStateRef State, ErrnoCheckState EState)
Set the errno check state, do not modify the errno value.
ProgramStateRef setErrnoForStdFailure(ProgramStateRef State, CheckerContext &C, NonLoc ErrnoSym)
Set errno state for the common case when a standard function fails.
@ Irrelevant
We do not know anything about 'errno'.
IntrusiveRefCntPtr< const ProgramState > ProgramStateRef
SVal getDynamicExtentWithOffset(ProgramStateRef State, SVal BufV)
Get the dynamic extent for a symbolic value that represents a buffer.
@ OS
Indicates that the tracking object is a descendant of a referenced-counted OSObject,...
std::optional< int > tryExpandAsInteger(StringRef Macro, const Preprocessor &PP)
Try to parse the value of a defined preprocessor macro.
bool IsNonNull(InterpState &S, CodePtr OpPC)
Definition Interp.h:3153
PRESERVE_NONE bool Ret(InterpState &S, CodePtr &PC)
Definition Interp.h:260
bool matches(const til::SExpr *E1, const til::SExpr *E2)
Stencil describe(llvm::StringRef Id)
Produces a human-readable rendering of the node bound to Id, suitable for diagnostics and debugging.
The JSON file list parser is used to communicate input to InstallAPI.
bool isa(CodeGen::Address addr)
Definition Address.h:330
@ Result
The result type of a method or function.
Definition TypeBase.h:905
U cast(CodeGen::Address addr)
Definition Address.h:327
unsigned long uint64_t
int const char * function
Definition c++config.h:31
__packed_splat4 __packed_splat2 __packed_splat8 __packed_splat4 __packed_splat2 __packed_splat4 __packed_splat2 __packed_splat8 __packed_splat4 uint32_t