53#include "llvm/ADT/STLExtras.h"
54#include "llvm/ADT/SmallString.h"
55#include "llvm/ADT/StringExtras.h"
56#include "llvm/Support/FormatVariadic.h"
65class StdLibraryFunctionsChecker
66 :
public Checker<check::PreCall, check::PostCall, eval::Call> {
72 enum InvalidationKind {
81 enum RangeKind { OutOfRange, WithinRange };
83 static RangeKind negateKind(RangeKind K) {
90 llvm_unreachable(
"Unknown range kind");
99 typedef std::vector<std::pair<RangeInt, RangeInt>> IntRangeVector;
106 static const ArgNo Ret;
110 static void printArgDesc(ArgNo, llvm::raw_ostream &Out);
115 const CallEvent &
Call, llvm::raw_ostream &Out);
118 static void appendInsideRangeDesc(llvm::APSInt RMin, llvm::APSInt RMax,
119 QualType ArgT, BasicValueFactory &BVF,
120 llvm::raw_ostream &Out);
123 static void appendOutOfRangeDesc(llvm::APSInt RMin, llvm::APSInt RMax,
124 QualType ArgT, BasicValueFactory &BVF,
125 llvm::raw_ostream &Out);
127 class ValueConstraint;
136 using ValueConstraintPtr = std::shared_ptr<ValueConstraint>;
148 class ValueConstraint {
150 ValueConstraint(ArgNo ArgN) : ArgN(ArgN) {}
151 virtual ~ValueConstraint() {}
156 const Summary &Summary,
157 CheckerContext &
C)
const = 0;
161 enum DescriptionKind {
177 virtual void describe(DescriptionKind DK,
const CallEvent &
Call,
179 llvm::raw_ostream &Out)
const {
184 "Description not implemented for summary case constraints");
199 virtual bool describeArgumentValue(
const CallEvent &
Call,
201 const Summary &Summary,
202 llvm::raw_ostream &Out)
const {
203 if (
auto N = getArgSVal(
Call, getArgNo()).getAs<NonLoc>()) {
204 if (
const llvm::APSInt *
Int = N->getAsInteger()) {
218 virtual std::vector<ArgNo> getArgsToTrack()
const {
return {ArgN}; }
221 virtual ValueConstraintPtr negate()
const {
222 llvm_unreachable(
"Not implemented");
230 bool checkValidity(
const FunctionDecl *FD)
const {
232 assert(ValidArg &&
"Arg out of range!");
236 return checkSpecificValidity(FD);
240 ArgNo getArgNo()
const {
return ArgN; }
251 virtual bool checkSpecificValidity(
const FunctionDecl *FD)
const {
266 class RangeConstraint :
public ValueConstraint {
273 IntRangeVector Ranges;
278 StringRef Description;
281 RangeConstraint(ArgNo ArgN, RangeKind Kind,
const IntRangeVector &Ranges,
283 : ValueConstraint(ArgN), Kind(Kind), Ranges(Ranges), Description(Desc) {
286 const IntRangeVector &getRanges()
const {
return Ranges; }
289 const Summary &Summary,
290 CheckerContext &
C)
const override;
292 void describe(DescriptionKind DK,
const CallEvent &
Call,
294 llvm::raw_ostream &Out)
const override;
297 const Summary &Summary,
298 llvm::raw_ostream &Out)
const override;
300 ValueConstraintPtr negate()
const override {
301 RangeConstraint Tmp(*
this);
302 Tmp.Kind = negateKind(Kind);
303 return std::make_shared<RangeConstraint>(Tmp);
307 bool checkSpecificValidity(
const FunctionDecl *FD)
const override {
308 return getArgType(FD, ArgN)->isIntegralType(FD->
getASTContext());
316 using RangeApplyFunction =
321 void applyOnWithinRange(BasicValueFactory &BVF, QualType ArgT,
322 const RangeApplyFunction &F)
const;
333 void applyOnOutOfRange(BasicValueFactory &BVF, QualType ArgT,
334 const RangeApplyFunction &F)
const;
337 void applyOnRange(RangeKind Kind, BasicValueFactory &BVF, QualType ArgT,
338 const RangeApplyFunction &F)
const {
341 applyOnOutOfRange(BVF, ArgT, F);
344 applyOnWithinRange(BVF, ArgT, F);
351 class ComparisonConstraint :
public ValueConstraint {
358 : ValueConstraint(ArgN), Opcode(Opcode), OtherArgN(OtherArgN) {}
359 ArgNo getOtherArgNo()
const {
return OtherArgN; }
362 const Summary &Summary,
363 CheckerContext &
C)
const override;
367 class NullnessConstraint :
public ValueConstraint {
368 using ValueConstraint::ValueConstraint;
370 bool CannotBeNull =
true;
373 NullnessConstraint(ArgNo ArgN,
bool CannotBeNull =
true)
374 : ValueConstraint(ArgN), CannotBeNull(CannotBeNull) {}
377 const Summary &Summary,
378 CheckerContext &
C)
const override;
380 void describe(DescriptionKind DK,
const CallEvent &
Call,
382 llvm::raw_ostream &Out)
const override;
385 const Summary &Summary,
386 llvm::raw_ostream &Out)
const override;
388 ValueConstraintPtr negate()
const override {
389 NullnessConstraint Tmp(*
this);
390 Tmp.CannotBeNull = !this->CannotBeNull;
391 return std::make_shared<NullnessConstraint>(Tmp);
395 bool checkSpecificValidity(
const FunctionDecl *FD)
const override {
396 const bool ValidArg = getArgType(FD, ArgN)->isPointerType();
398 "This constraint should be applied only on a pointer type");
409 class BufferNullnessConstraint :
public ValueConstraint {
410 using ValueConstraint::ValueConstraint;
412 std::optional<ArgNo> SizeArg2N;
414 bool CannotBeNull =
true;
417 BufferNullnessConstraint(ArgNo ArgN, ArgNo SizeArg1N,
418 std::optional<ArgNo> SizeArg2N,
419 bool CannotBeNull =
true)
420 : ValueConstraint(ArgN), SizeArg1N(SizeArg1N), SizeArg2N(SizeArg2N),
421 CannotBeNull(CannotBeNull) {}
424 const Summary &Summary,
425 CheckerContext &
C)
const override;
427 void describe(DescriptionKind DK,
const CallEvent &
Call,
429 llvm::raw_ostream &Out)
const override;
432 const Summary &Summary,
433 llvm::raw_ostream &Out)
const override;
435 ValueConstraintPtr negate()
const override {
436 BufferNullnessConstraint Tmp(*
this);
437 Tmp.CannotBeNull = !this->CannotBeNull;
438 return std::make_shared<BufferNullnessConstraint>(Tmp);
442 bool checkSpecificValidity(
const FunctionDecl *FD)
const override {
443 const bool ValidArg = getArgType(FD, ArgN)->isPointerType();
445 "This constraint should be applied only on a pointer type");
460 class BufferSizeConstraint :
public ValueConstraint {
462 std::optional<llvm::APSInt> ConcreteSize;
464 std::optional<ArgNo> SizeArgN;
468 std::optional<ArgNo> SizeMultiplierArgN;
473 BufferSizeConstraint(ArgNo Buffer, llvm::APSInt BufMinSize)
474 : ValueConstraint(Buffer), ConcreteSize(BufMinSize) {}
475 BufferSizeConstraint(ArgNo Buffer, ArgNo BufSize)
476 : ValueConstraint(Buffer), SizeArgN(BufSize) {}
477 BufferSizeConstraint(ArgNo Buffer, ArgNo BufSize, ArgNo BufSizeMultiplier)
478 : ValueConstraint(Buffer), SizeArgN(BufSize),
479 SizeMultiplierArgN(BufSizeMultiplier) {}
482 const Summary &Summary,
483 CheckerContext &
C)
const override;
485 void describe(DescriptionKind DK,
const CallEvent &
Call,
487 llvm::raw_ostream &Out)
const override;
490 const Summary &Summary,
491 llvm::raw_ostream &Out)
const override;
493 std::vector<ArgNo> getArgsToTrack()
const override {
494 std::vector<ArgNo>
Result{ArgN};
496 Result.push_back(*SizeArgN);
497 if (SizeMultiplierArgN)
498 Result.push_back(*SizeMultiplierArgN);
502 ValueConstraintPtr negate()
const override {
503 BufferSizeConstraint Tmp(*
this);
505 return std::make_shared<BufferSizeConstraint>(Tmp);
509 bool checkSpecificValidity(
const FunctionDecl *FD)
const override {
510 const bool ValidArg = getArgType(FD, ArgN)->isPointerType();
512 "This constraint should be applied only on a pointer type");
518 using ConstraintSet = std::vector<ValueConstraintPtr>;
527 class ErrnoConstraintBase {
531 const Summary &Summary,
532 CheckerContext &
C)
const = 0;
537 virtual std::string
describe(CheckerContext &
C)
const {
return ""; }
539 virtual ~ErrnoConstraintBase() {}
542 ErrnoConstraintBase() =
default;
552 class ResetErrnoConstraint :
public ErrnoConstraintBase {
555 const Summary &Summary,
556 CheckerContext &
C)
const override {
565 class NoErrnoConstraint :
public ErrnoConstraintBase {
568 const Summary &Summary,
569 CheckerContext &
C)
const override {
578 class FailureErrnoConstraint :
public ErrnoConstraintBase {
581 const Summary &Summary,
582 CheckerContext &
C)
const override {
583 SValBuilder &SVB =
C.getSValBuilder();
585 C.blockCount(), &Tag)
596 class SuccessErrnoConstraint :
public ErrnoConstraintBase {
599 const Summary &Summary,
600 CheckerContext &
C)
const override {
604 std::string
describe(CheckerContext &
C)
const override {
605 return "'errno' becomes undefined after the call";
613 class ErrnoMustBeCheckedConstraint :
public ErrnoConstraintBase {
616 const Summary &Summary,
617 CheckerContext &
C)
const override {
619 Call.getCFGElementRef());
622 std::string
describe(CheckerContext &
C)
const override {
623 return "reading 'errno' is required to find out if the call has failed";
649 ConstraintSet Constraints;
650 const ErrnoConstraintBase &ErrnoConstraint;
654 SummaryCase(ConstraintSet &&Constraints,
const ErrnoConstraintBase &ErrnoC,
656 : Constraints(std::move(Constraints)), ErrnoConstraint(ErrnoC),
659 SummaryCase(
const ConstraintSet &Constraints,
660 const ErrnoConstraintBase &ErrnoC, StringRef Note)
661 : Constraints(Constraints), ErrnoConstraint(ErrnoC), Note(Note) {}
663 const ConstraintSet &getConstraints()
const {
return Constraints; }
664 const ErrnoConstraintBase &getErrnoConstraint()
const {
665 return ErrnoConstraint;
667 StringRef getNote()
const {
return Note; }
670 using ArgTypes = ArrayRef<std::optional<QualType>>;
671 using RetType = std::optional<QualType>;
675 const QualType Irrelevant{};
676 bool static isIrrelevant(QualType
T) {
return T.isNull(); }
683 using ArgQualTypes = std::vector<QualType>;
687 bool Invalid =
false;
692 Signature(ArgTypes ArgTys, RetType RetTy) {
693 for (std::optional<QualType> Arg : ArgTys) {
698 assertArgTypeSuitableForSignature(*Arg);
699 this->ArgTys.push_back(*Arg);
706 assertRetTypeSuitableForSignature(*RetTy);
707 this->RetTy = *RetTy;
711 bool isInvalid()
const {
return Invalid; }
712 bool matches(
const FunctionDecl *FD)
const;
715 static void assertArgTypeSuitableForSignature(QualType
T) {
717 "We should have no void types in the spec");
718 assert((
T.isNull() ||
T.isCanonical()) &&
719 "We should only have canonical types in the spec");
721 static void assertRetTypeSuitableForSignature(QualType
T) {
722 assert((
T.isNull() ||
T.isCanonical()) &&
723 "We should only have canonical types in the spec");
727 static QualType getArgType(
const FunctionDecl *FD, ArgNo ArgN) {
728 assert(FD &&
"Function must be set");
729 QualType
T = (ArgN == Ret)
735 using SummaryCases = std::vector<SummaryCase>;
754 const InvalidationKind InvalidationKd;
756 ConstraintSet ArgConstraints;
760 const FunctionDecl *FD =
nullptr;
763 Summary(InvalidationKind InvalidationKd) : InvalidationKd(InvalidationKd) {}
765 Summary &Case(ConstraintSet &&CS,
const ErrnoConstraintBase &ErrnoC,
766 StringRef
Note =
"") {
767 Cases.push_back(SummaryCase(std::move(CS), ErrnoC,
Note));
770 Summary &Case(
const ConstraintSet &CS,
const ErrnoConstraintBase &ErrnoC,
771 StringRef
Note =
"") {
772 Cases.push_back(SummaryCase(CS, ErrnoC,
Note));
775 Summary &ArgConstraint(ValueConstraintPtr VC) {
776 assert(VC->getArgNo() != Ret &&
777 "Arg constraint should not refer to the return value");
778 ArgConstraints.push_back(VC);
782 InvalidationKind getInvalidationKd()
const {
return InvalidationKd; }
783 const SummaryCases &getCases()
const {
return Cases; }
784 const ConstraintSet &getArgConstraints()
const {
return ArgConstraints; }
786 QualType getArgType(ArgNo ArgN)
const {
787 return StdLibraryFunctionsChecker::getArgType(FD, ArgN);
792 bool matchesAndSet(
const Signature &Sign,
const FunctionDecl *FD) {
793 bool Result = Sign.matches(FD) && validateByConstraints(FD);
795 assert(!this->FD &&
"FD must not be set more than once");
804 bool validateByConstraints(
const FunctionDecl *FD)
const {
805 for (
const SummaryCase &Case : Cases)
806 for (
const ValueConstraintPtr &Constraint : Case.getConstraints())
807 if (!Constraint->checkValidity(FD))
809 for (
const ValueConstraintPtr &Constraint : ArgConstraints)
810 if (!Constraint->checkValidity(FD))
818 using FunctionSummaryMapType = llvm::DenseMap<const FunctionDecl *, Summary>;
819 mutable FunctionSummaryMapType FunctionSummaryMap;
821 const BugType BT_InvalidArg{
this,
"Function call with invalid argument"};
822 mutable bool SummariesInitialized =
false;
824 static SVal getArgSVal(
const CallEvent &
Call, ArgNo ArgN) {
825 return ArgN == Ret ?
Call.getReturnValue() :
Call.getArgSVal(ArgN);
828 assert(
Call.getDecl() &&
829 "Call was found by a summary, should have declaration");
834 void checkPreCall(
const CallEvent &
Call, CheckerContext &
C)
const;
835 void checkPostCall(
const CallEvent &
Call, CheckerContext &
C)
const;
836 bool evalCall(
const CallEvent &
Call, CheckerContext &
C)
const;
838 CheckerNameRef CheckName;
839 bool AddTestFunctions =
false;
841 bool DisplayLoadedSummaries =
false;
842 bool ModelPOSIX =
false;
843 bool ShouldAssumeControlledEnvironment =
false;
846 std::optional<Summary> findFunctionSummary(
const FunctionDecl *FD,
847 CheckerContext &
C)
const;
848 std::optional<Summary> findFunctionSummary(
const CallEvent &
Call,
849 CheckerContext &
C)
const;
851 LLVM_ATTRIBUTE_MINSIZE
void initFunctionSummaries(CheckerContext &
C)
const;
853 void reportBug(
const CallEvent &
Call, ExplodedNode *N,
854 const ValueConstraint *VC,
const ValueConstraint *NegatedVC,
855 const Summary &Summary, CheckerContext &
C)
const {
856 assert(
Call.getDecl() &&
857 "Function found in summary must have a declaration available");
858 SmallString<256> Msg;
859 llvm::raw_svector_ostream MsgOs(Msg);
862 printArgDesc(VC->getArgNo(), MsgOs);
865 NegatedVC->describeArgumentValue(
Call, N->
getState(), Summary, MsgOs);
869 MsgOs <<
"is out of the accepted range; it ";
870 VC->describe(ValueConstraint::Violation,
Call,
C.getState(), Summary,
872 Msg[0] = toupper(Msg[0]);
873 auto R = std::make_unique<PathSensitiveBugReport>(BT_InvalidArg, Msg, N);
875 for (ArgNo ArgN : VC->getArgsToTrack()) {
877 R->markInteresting(
Call.getArgSVal(ArgN));
879 R->addRange(
Call.getArgSourceRange(ArgN));
882 C.emitReport(std::move(R));
890 const NoErrnoConstraint ErrnoUnchanged{};
891 const ResetErrnoConstraint ErrnoIrrelevant{};
892 const ErrnoMustBeCheckedConstraint ErrnoMustBeChecked{};
893 const SuccessErrnoConstraint ErrnoMustNotBeChecked{};
894 const FailureErrnoConstraint ErrnoNEZeroIrrelevant{};
897int StdLibraryFunctionsChecker::ErrnoConstraintBase::Tag = 0;
899const StdLibraryFunctionsChecker::ArgNo StdLibraryFunctionsChecker::Ret =
900 std::numeric_limits<ArgNo>::max();
910void StdLibraryFunctionsChecker::printArgDesc(
911 StdLibraryFunctionsChecker::ArgNo ArgN, llvm::raw_ostream &Out) {
912 Out << std::to_string(ArgN + 1);
913 Out << llvm::getOrdinalSuffix(ArgN + 1);
917void StdLibraryFunctionsChecker::printArgValueInfo(ArgNo ArgN,
919 const CallEvent &
Call,
920 llvm::raw_ostream &Out) {
921 if (
const llvm::APSInt *Val =
922 State->getStateManager().getSValBuilder().getKnownValue(
923 State, getArgSVal(
Call, ArgN)))
924 Out <<
" (which is " << *Val <<
")";
927void StdLibraryFunctionsChecker::appendInsideRangeDesc(llvm::APSInt RMin,
930 BasicValueFactory &BVF,
931 llvm::raw_ostream &Out) {
932 if (RMin.isZero() && RMax.isZero())
934 else if (RMin == RMax)
940 Out <<
"<= " << RMax;
945 Out <<
">= " << RMin;
946 }
else if (RMin.isNegative() == RMax.isNegative() &&
947 RMin.getLimitedValue() == RMax.getLimitedValue() - 1) {
948 Out << RMin <<
" or " << RMax;
950 Out <<
"between " << RMin <<
" and " << RMax;
954void StdLibraryFunctionsChecker::appendOutOfRangeDesc(llvm::APSInt RMin,
957 BasicValueFactory &BVF,
958 llvm::raw_ostream &Out) {
959 if (RMin.isZero() && RMax.isZero())
961 else if (RMin == RMax) {
962 Out <<
"not equal to " << RMin;
973 }
else if (RMin.isNegative() == RMax.isNegative() &&
974 RMin.getLimitedValue() == RMax.getLimitedValue() - 1) {
975 Out <<
"not " << RMin <<
" and not " << RMax;
977 Out <<
"not between " << RMin <<
" and " << RMax;
981void StdLibraryFunctionsChecker::RangeConstraint::applyOnWithinRange(
982 BasicValueFactory &BVF, QualType ArgT,
const RangeApplyFunction &F)
const {
986 for (
auto [Start, End] : getRanges()) {
987 const llvm::APSInt &
Min = BVF.getValue(Start, ArgT);
988 const llvm::APSInt &
Max = BVF.getValue(End, ArgT);
995void StdLibraryFunctionsChecker::RangeConstraint::applyOnOutOfRange(
996 BasicValueFactory &BVF, QualType ArgT,
const RangeApplyFunction &F)
const {
1000 const IntRangeVector &
R = getRanges();
1001 size_t E =
R.size();
1003 const llvm::APSInt &MinusInf = BVF.
getMinValue(ArgT);
1004 const llvm::APSInt &PlusInf = BVF.
getMaxValue(ArgT);
1006 const llvm::APSInt &RangeLeft = BVF.getValue(R[0].first - 1ULL, ArgT);
1007 const llvm::APSInt &RangeRight = BVF.getValue(R[E - 1].second + 1ULL, ArgT);
1010 for (
size_t I = 1; I != E; ++I) {
1011 const llvm::APSInt &
Min = BVF.getValue(R[I - 1].second + 1ULL, ArgT);
1012 const llvm::APSInt &
Max = BVF.getValue(R[I].first - 1ULL, ArgT);
1019 if (RangeLeft != PlusInf) {
1020 assert(MinusInf <= RangeLeft);
1021 if (!F(MinusInf, RangeLeft))
1025 if (RangeRight != MinusInf) {
1026 assert(RangeRight <= PlusInf);
1027 if (!F(RangeRight, PlusInf))
1034 CheckerContext &
C)
const {
1035 ConstraintManager &CM =
C.getConstraintManager();
1036 SVal
V = getArgSVal(
Call, getArgNo());
1037 QualType
T = Summary.getArgType(getArgNo());
1039 if (
auto N =
V.getAs<NonLoc>()) {
1040 auto ExcludeRangeFromArg = [&](
const llvm::APSInt &
Min,
1041 const llvm::APSInt &
Max) {
1043 return static_cast<bool>(State);
1047 applyOnRange(negateKind(Kind),
C.getSValBuilder().getBasicValueFactory(),
T,
1048 ExcludeRangeFromArg);
1054void StdLibraryFunctionsChecker::RangeConstraint::describe(
1056 const Summary &Summary, llvm::raw_ostream &Out)
const {
1058 BasicValueFactory &BVF = getBVF(State);
1059 QualType
T = Summary.getArgType(getArgNo());
1061 Out << ((DK == Violation) ?
"should be " :
"is ");
1062 if (!Description.empty()) {
1065 unsigned I = Ranges.size();
1066 if (Kind == WithinRange) {
1067 for (
const std::pair<RangeInt, RangeInt> &R : Ranges) {
1068 appendInsideRangeDesc(BVF.getValue(
R.first,
T),
1069 BVF.getValue(
R.second,
T),
T, BVF, Out);
1074 for (
const std::pair<RangeInt, RangeInt> &R : Ranges) {
1075 appendOutOfRangeDesc(BVF.getValue(
R.first,
T),
1076 BVF.getValue(
R.second,
T),
T, BVF, Out);
1084bool StdLibraryFunctionsChecker::RangeConstraint::describeArgumentValue(
1086 llvm::raw_ostream &Out)
const {
1087 unsigned int NRanges = 0;
1088 bool HaveAllRanges =
true;
1090 ProgramStateManager &Mgr = State->getStateManager();
1093 SVal
V = getArgSVal(
Call, getArgNo());
1095 if (
auto N =
V.getAs<NonLoc>()) {
1096 if (
const llvm::APSInt *
Int = N->getAsInteger()) {
1101 QualType
T = Summary.getArgType(getArgNo());
1102 SmallString<128> MoreInfo;
1103 llvm::raw_svector_ostream MoreInfoOs(MoreInfo);
1104 auto ApplyF = [&](
const llvm::APSInt &
Min,
const llvm::APSInt &
Max) {
1107 MoreInfoOs <<
" or ";
1108 appendInsideRangeDesc(
Min,
Max,
T, BVF, MoreInfoOs);
1111 HaveAllRanges =
false;
1116 applyOnRange(Kind, BVF,
T, ApplyF);
1117 assert(NRanges > 0);
1118 if (!HaveAllRanges || NRanges == 1) {
1127ProgramStateRef StdLibraryFunctionsChecker::ComparisonConstraint::apply(
1129 CheckerContext &
C)
const {
1131 ProgramStateManager &Mgr = State->getStateManager();
1134 QualType
T = Summary.getArgType(getArgNo());
1135 SVal
V = getArgSVal(
Call, getArgNo());
1138 ArgNo OtherArg = getOtherArgNo();
1139 SVal OtherV = getArgSVal(
Call, OtherArg);
1140 QualType OtherT = Summary.getArgType(OtherArg);
1142 OtherV = SVB.
evalCast(OtherV,
T, OtherT);
1143 if (
auto CompV = SVB.
evalBinOp(State, Op,
V, OtherV, CondT)
1144 .
getAs<DefinedOrUnknownSVal>())
1145 State = State->assume(*CompV,
true);
1151 CheckerContext &
C)
const {
1152 SVal
V = getArgSVal(
Call, getArgNo());
1156 DefinedOrUnknownSVal L =
V.castAs<DefinedOrUnknownSVal>();
1160 return State->assume(L, CannotBeNull);
1163void StdLibraryFunctionsChecker::NullnessConstraint::describe(
1165 const Summary &Summary, llvm::raw_ostream &Out)
const {
1166 assert(CannotBeNull &&
1167 "'describe' is not implemented when the value must be NULL");
1168 if (DK == Violation)
1169 Out <<
"should not be NULL";
1171 Out <<
"is not NULL";
1174bool StdLibraryFunctionsChecker::NullnessConstraint::describeArgumentValue(
1176 llvm::raw_ostream &Out)
const {
1177 assert(!CannotBeNull &&
"'describeArgumentValue' is not implemented when the "
1178 "value must be non-NULL");
1183ProgramStateRef StdLibraryFunctionsChecker::BufferNullnessConstraint::apply(
1185 CheckerContext &
C)
const {
1186 SVal
V = getArgSVal(
Call, getArgNo());
1189 DefinedOrUnknownSVal L =
V.
castAs<DefinedOrUnknownSVal>();
1193 std::optional<DefinedOrUnknownSVal> SizeArg1 =
1194 getArgSVal(
Call, SizeArg1N).getAs<DefinedOrUnknownSVal>();
1195 std::optional<DefinedOrUnknownSVal> SizeArg2;
1197 SizeArg2 = getArgSVal(
Call, *SizeArg2N).getAs<DefinedOrUnknownSVal>();
1199 auto IsArgZero = [State](std::optional<DefinedOrUnknownSVal> Val) {
1206 if (IsArgZero(SizeArg1) || IsArgZero(SizeArg2))
1209 return State->assume(L, CannotBeNull);
1212void StdLibraryFunctionsChecker::BufferNullnessConstraint::describe(
1214 const Summary &Summary, llvm::raw_ostream &Out)
const {
1215 assert(CannotBeNull &&
1216 "'describe' is not implemented when the buffer must be NULL");
1217 if (DK == Violation)
1218 Out <<
"should not be NULL";
1220 Out <<
"is not NULL";
1223bool StdLibraryFunctionsChecker::BufferNullnessConstraint::
1225 const Summary &Summary,
1226 llvm::raw_ostream &Out)
const {
1227 assert(!CannotBeNull &&
"'describeArgumentValue' is not implemented when the "
1228 "buffer must be non-NULL");
1233ProgramStateRef StdLibraryFunctionsChecker::BufferSizeConstraint::apply(
1235 CheckerContext &
C)
const {
1236 SValBuilder &SvalBuilder =
C.getSValBuilder();
1238 SVal BufV = getArgSVal(
Call, getArgNo());
1241 const SVal SizeV = [
this, &State, &
Call, &Summary, &SvalBuilder]() {
1243 return SVal(SvalBuilder.
makeIntVal(*ConcreteSize));
1245 assert(SizeArgN &&
"The constraint must be either a concrete value or "
1246 "encoded in an argument.");
1248 SVal SizeV = getArgSVal(
Call, *SizeArgN);
1250 if (SizeMultiplierArgN) {
1251 SVal SizeMulV = getArgSVal(
Call, *SizeMultiplierArgN);
1252 SizeV = SvalBuilder.
evalBinOp(State, BO_Mul, SizeV, SizeMulV,
1253 Summary.getArgType(*SizeArgN));
1261 SVal Feasible = SvalBuilder.
evalBinOp(State, Op, SizeV, BufDynSize,
1263 if (
auto F = Feasible.
getAs<DefinedOrUnknownSVal>())
1264 return State->assume(*F,
true);
1272 llvm_unreachable(
"Size argument or the dynamic size is Undefined");
1275void StdLibraryFunctionsChecker::BufferSizeConstraint::describe(
1277 const Summary &Summary, llvm::raw_ostream &Out)
const {
1278 Out << ((DK == Violation) ?
"should be " :
"is ");
1279 Out <<
"a buffer with size equal to or greater than ";
1281 Out << *ConcreteSize;
1282 }
else if (SizeArgN) {
1283 Out <<
"the value of the ";
1284 printArgDesc(*SizeArgN, Out);
1285 printArgValueInfo(*SizeArgN, State,
Call, Out);
1286 if (SizeMultiplierArgN) {
1287 Out <<
" times the ";
1288 printArgDesc(*SizeMultiplierArgN, Out);
1289 printArgValueInfo(*SizeMultiplierArgN, State,
Call, Out);
1294bool StdLibraryFunctionsChecker::BufferSizeConstraint::describeArgumentValue(
1296 llvm::raw_ostream &Out)
const {
1297 SVal BufV = getArgSVal(
Call, getArgNo());
1299 if (
const llvm::APSInt *Val =
1300 State->getStateManager().getSValBuilder().getKnownValue(State,
1302 Out <<
"is a buffer with size " << *Val;
1308void StdLibraryFunctionsChecker::checkPreCall(
const CallEvent &
Call,
1309 CheckerContext &
C)
const {
1310 std::optional<Summary> FoundSummary = findFunctionSummary(
Call,
C);
1314 const Summary &Summary = *FoundSummary;
1318 ExplodedNode *NewNode =
C.getPredecessor();
1319 for (
const ValueConstraintPtr &Constraint : Summary.getArgConstraints()) {
1320 ValueConstraintPtr NegatedConstraint = Constraint->negate();
1323 NegatedConstraint->apply(NewState,
Call, Summary,
C);
1325 if (FailureSt && !SuccessSt) {
1326 if (ExplodedNode *N =
C.addSink(State, NewNode))
1327 reportBug(
Call, N, Constraint.get(), NegatedConstraint.get(), Summary,
1336 NewState = SuccessSt;
1337 if (NewState != State) {
1338 SmallString<128> Msg;
1339 llvm::raw_svector_ostream Os(Msg);
1340 Os <<
"Assuming that the ";
1341 printArgDesc(Constraint->getArgNo(), Os);
1345 Constraint->describe(ValueConstraint::Assumption,
Call, NewState, Summary,
1347 const auto ArgSVal =
Call.getArgSVal(Constraint->getArgNo());
1348 NewNode =
C.addTransition(
1350 C.getNoteTag([Msg = std::move(Msg), ArgSVal](
1351 PathSensitiveBugReport &BR, llvm::raw_ostream &
OS) {
1352 if (BR.isInteresting(ArgSVal))
1359void StdLibraryFunctionsChecker::checkPostCall(
const CallEvent &
Call,
1360 CheckerContext &
C)
const {
1361 std::optional<Summary> FoundSummary = findFunctionSummary(
Call,
C);
1366 const Summary &Summary = *FoundSummary;
1368 ExplodedNode *Node =
C.getPredecessor();
1371 for (
const SummaryCase &Case : Summary.getCases()) {
1373 for (
const ValueConstraintPtr &Constraint : Case.getConstraints()) {
1374 NewState = Constraint->apply(NewState,
Call, Summary,
C);
1380 NewState = Case.getErrnoConstraint().apply(NewState,
Call, Summary,
C);
1391 ExplodedNode *Pred = Node;
1392 DeclarationName FunctionName =
1395 std::string ErrnoNote = Case.getErrnoConstraint().describe(
C);
1396 std::string CaseNote;
1397 if (Case.getNote().empty()) {
1398 if (!ErrnoNote.empty())
1400 llvm::formatv(
"After calling '{0}' {1}", FunctionName, ErrnoNote);
1405 llvm::formatv(
false, Case.getNote().str().c_str(), FunctionName);
1407 const SVal RV =
Call.getReturnValue();
1409 if (Summary.getInvalidationKd() == EvalCallAsPure) {
1412 if (!CaseNote.empty()) {
1413 const NoteTag *
Tag =
C.getNoteTag(
1414 [Node, CaseNote, RV](PathSensitiveBugReport &BR) -> std::string {
1429 Pred =
C.addTransition(NewState, Pred, Tag);
1432 if (!CaseNote.empty() || !ErrnoNote.empty()) {
1433 const NoteTag *
Tag =
1434 C.getNoteTag([CaseNote, ErrnoNote,
1435 RV](PathSensitiveBugReport &BR) -> std::string {
1442 std::optional<Loc> ErrnoLoc =
1444 bool ErrnoImportant = !ErrnoNote.empty() && ErrnoLoc &&
1446 if (ErrnoImportant) {
1448 if (CaseNote.empty())
1450 return llvm::formatv(
"{0}; {1}", CaseNote, ErrnoNote);
1457 Pred =
C.addTransition(NewState, Pred, Tag);
1462 if (Pred == Node && NewState != State)
1463 C.addTransition(NewState);
1467bool StdLibraryFunctionsChecker::evalCall(
const CallEvent &
Call,
1468 CheckerContext &
C)
const {
1469 std::optional<Summary> FoundSummary = findFunctionSummary(
Call,
C);
1473 const Summary &Summary = *FoundSummary;
1474 switch (Summary.getInvalidationKd()) {
1475 case EvalCallAsPure: {
1478 SVal
V =
C.getSValBuilder().conjureSymbolVal(
Call,
C.blockCount());
1479 State = State->BindExpr(CE,
C.getStackFrame(),
V);
1481 C.addTransition(State);
1490 llvm_unreachable(
"Unknown invalidation kind!");
1493bool StdLibraryFunctionsChecker::Signature::matches(
1494 const FunctionDecl *FD)
const {
1500 auto RemoveRestrict = [](QualType
T) {
1501 T =
T.getCanonicalType();
1502 T.removeLocalRestrict();
1507 if (!isIrrelevant(RetTy)) {
1509 if (RetTy != FDRetTy)
1514 for (
auto [Idx, ArgTy] : llvm::enumerate(ArgTys)) {
1515 if (isIrrelevant(ArgTy))
1518 if (ArgTy != FDArgTy)
1525std::optional<StdLibraryFunctionsChecker::Summary>
1526StdLibraryFunctionsChecker::findFunctionSummary(
const FunctionDecl *FD,
1527 CheckerContext &
C)
const {
1529 return std::nullopt;
1531 initFunctionSummaries(
C);
1534 if (FSMI == FunctionSummaryMap.end())
1535 return std::nullopt;
1536 return FSMI->second;
1539std::optional<StdLibraryFunctionsChecker::Summary>
1540StdLibraryFunctionsChecker::findFunctionSummary(
const CallEvent &
Call,
1541 CheckerContext &
C)
const {
1542 const FunctionDecl *FD = dyn_cast_or_null<FunctionDecl>(
Call.getDecl());
1544 return std::nullopt;
1545 return findFunctionSummary(FD,
C);
1548void StdLibraryFunctionsChecker::initFunctionSummaries(
1549 CheckerContext &
C)
const {
1550 if (SummariesInitialized)
1552 SummariesInitialized =
true;
1554 SValBuilder &SVB =
C.getSValBuilder();
1557 Preprocessor &PP =
C.getPreprocessor();
1561 const ASTContext &ACtx;
1564 LookupType(
const ASTContext &ACtx) : ACtx(ACtx) {}
1567 std::optional<QualType> operator()(StringRef Name) {
1568 IdentifierInfo &II = ACtx.
Idents.
get(Name);
1570 if (LookupRes.empty())
1571 return std::nullopt;
1578 for (Decl *D : LookupRes)
1579 if (
auto *TD = dyn_cast<TypedefNameDecl>(D))
1586 for (Decl *D : LookupRes)
1587 if (
auto *TD = dyn_cast<TypeDecl>(D))
1589 return std::nullopt;
1595 class GetPointerTy {
1596 const ASTContext &ACtx;
1599 GetPointerTy(
const ASTContext &ACtx) : ACtx(ACtx) {}
1600 QualType operator()(QualType Ty) {
return ACtx.
getPointerType(Ty); }
1601 std::optional<QualType> operator()(std::optional<QualType> Ty) {
1603 return operator()(*Ty);
1604 return std::nullopt;
1606 } getPointerTy(ACtx);
1609 std::optional<QualType> operator()(std::optional<QualType> Ty) {
1610 return Ty ? std::optional<QualType>(Ty->withConst()) : std::nullopt;
1612 QualType operator()(QualType Ty) {
return Ty.
withConst(); }
1615 BasicValueFactory &BVF;
1618 GetMaxValue(BasicValueFactory &BVF) : BVF(BVF) {}
1619 std::optional<RangeInt> operator()(QualType Ty) {
1622 std::optional<RangeInt> operator()(std::optional<QualType> Ty) {
1624 return operator()(*Ty);
1626 return std::nullopt;
1637 const QualType VoidTy = ACtx.
VoidTy;
1638 const QualType CharTy = ACtx.
CharTy;
1639 const QualType WCharTy = ACtx.
WCharTy;
1640 const QualType IntTy = ACtx.
IntTy;
1642 const QualType LongTy = ACtx.
LongTy;
1645 const QualType VoidPtrTy = getPointerTy(VoidTy);
1646 const QualType IntPtrTy = getPointerTy(IntTy);
1647 const QualType UnsignedIntPtrTy =
1648 getPointerTy(UnsignedIntTy);
1649 const QualType ConstVoidPtrTy =
1650 getPointerTy(getConstTy(VoidTy));
1651 const QualType CharPtrTy = getPointerTy(CharTy);
1652 const QualType ConstCharPtrTy =
1653 getPointerTy(getConstTy(CharTy));
1654 const QualType Wchar_tPtrTy = getPointerTy(WCharTy);
1655 const QualType ConstWchar_tPtrTy =
1656 getPointerTy(getConstTy(WCharTy));
1657 const QualType SizePtrTy = getPointerTy(SizeTyCanonTy);
1659 const RangeInt IntMax = BVF.
getMaxValue(IntTy)->getLimitedValue();
1660 const RangeInt UnsignedIntMax =
1661 BVF.
getMaxValue(UnsignedIntTy)->getLimitedValue();
1662 const RangeInt LongMax = BVF.
getMaxValue(LongTy)->getLimitedValue();
1663 const RangeInt SizeMax = BVF.
getMaxValue(SizeTyCanonTy)->getLimitedValue();
1671 const RangeInt UCharRangeMax =
1681 struct AddToFunctionSummaryMap {
1682 const ASTContext &ACtx;
1683 FunctionSummaryMapType ⤅
1684 bool DisplayLoadedSummaries;
1685 AddToFunctionSummaryMap(
const ASTContext &ACtx, FunctionSummaryMapType &FSM,
1686 bool DisplayLoadedSummaries)
1687 : ACtx(ACtx), Map(FSM), DisplayLoadedSummaries(DisplayLoadedSummaries) {
1695 bool operator()(StringRef Name, Signature Sign, Summary Sum) {
1696 if (Sign.isInvalid())
1698 IdentifierInfo &II = ACtx.
Idents.
get(Name);
1700 if (LookupRes.empty())
1702 for (Decl *D : LookupRes) {
1703 if (
auto *FD = dyn_cast<FunctionDecl>(D)) {
1704 if (Sum.matchesAndSet(Sign, FD)) {
1706 assert(Res.second &&
"Function already has a summary set!");
1708 if (DisplayLoadedSummaries) {
1709 llvm::errs() <<
"Loaded summary for: ";
1710 FD->
print(llvm::errs());
1711 llvm::errs() <<
"\n";
1721 void operator()(ArrayRef<StringRef> Names, Signature Sign, Summary Sum) {
1722 for (StringRef Name : Names)
1723 operator()(Name, Sign, Sum);
1725 } addToFunctionSummaryMap(ACtx, FunctionSummaryMap, DisplayLoadedSummaries);
1728 auto ArgumentCondition = [](ArgNo ArgN, RangeKind
Kind, IntRangeVector Ranges,
1729 StringRef Desc =
"") {
1730 return std::make_shared<RangeConstraint>(ArgN, Kind, Ranges, Desc);
1732 auto BufferSize = [](
auto... Args) {
1733 return std::make_shared<BufferSizeConstraint>(Args...);
1736 auto operator()(RangeKind Kind, IntRangeVector Ranges) {
1737 return std::make_shared<RangeConstraint>(Ret, Kind, Ranges);
1740 return std::make_shared<ComparisonConstraint>(Ret, Op, OtherArgN);
1742 } ReturnValueCondition;
1744 auto operator()(RangeInt b, RangeInt e) {
1745 return IntRangeVector{std::pair<RangeInt, RangeInt>{b, e}};
1747 auto operator()(RangeInt b, std::optional<RangeInt> e) {
1749 return IntRangeVector{std::pair<RangeInt, RangeInt>{b, *e}};
1750 return IntRangeVector{};
1752 auto operator()(std::pair<RangeInt, RangeInt> i0,
1753 std::pair<RangeInt, std::optional<RangeInt>> i1) {
1755 return IntRangeVector{i0, {i1.first, *(i1.second)}};
1756 return IntRangeVector{i0};
1759 auto SingleValue = [](RangeInt v) {
1760 return IntRangeVector{std::pair<RangeInt, RangeInt>{v, v}};
1762 auto LessThanOrEq = BO_LE;
1763 auto NotNull = [&](ArgNo ArgN) {
1764 return std::make_shared<NullnessConstraint>(ArgN);
1766 auto IsNull = [&](ArgNo ArgN) {
1767 return std::make_shared<NullnessConstraint>(ArgN,
false);
1769 auto NotNullBuffer = [&](ArgNo ArgN, ArgNo SizeArg1N,
1770 std::optional<ArgNo> SizeArg2N = std::nullopt) {
1771 return std::make_shared<BufferNullnessConstraint>(ArgN, SizeArg1N,
1775 std::optional<QualType> FileTy = lookupTy(
"FILE");
1776 std::optional<QualType> FilePtrTy = getPointerTy(FileTy);
1778 std::optional<QualType> FPosTTy = lookupTy(
"fpos_t");
1779 std::optional<QualType> FPosTPtrTy = getPointerTy(FPosTTy);
1780 std::optional<QualType> ConstFPosTPtrTy = getPointerTy(getConstTy(FPosTTy));
1782 constexpr llvm::StringLiteral GenericSuccessMsg(
1783 "Assuming that '{0}' is successful");
1784 constexpr llvm::StringLiteral GenericFailureMsg(
"Assuming that '{0}' fails");
1801 addToFunctionSummaryMap(
1802 "isalnum", Signature(ArgTypes{IntTy}, RetType{IntTy}),
1803 Summary(EvalCallAsPure)
1805 .Case({ArgumentCondition(0U, WithinRange,
1806 {{
'0',
'9'}, {
'A',
'Z'}, {
'a',
'z'}}),
1807 ReturnValueCondition(OutOfRange, SingleValue(0))},
1808 ErrnoIrrelevant,
"Assuming the character is alphanumeric")
1812 .Case({ArgumentCondition(0U, WithinRange, {{128, UCharRangeMax}})},
1817 {{
'0',
'9'}, {
'A',
'Z'}, {
'a',
'z'}, {128, UCharRangeMax}}),
1818 ReturnValueCondition(WithinRange, SingleValue(0))},
1819 ErrnoIrrelevant,
"Assuming the character is non-alphanumeric")
1820 .ArgConstraint(ArgumentCondition(0U, WithinRange,
1821 {{EOFv, EOFv}, {0, UCharRangeMax}},
1822 "an unsigned char value or EOF")));
1823 addToFunctionSummaryMap(
1824 "isalpha", Signature(ArgTypes{IntTy}, RetType{IntTy}),
1825 Summary(EvalCallAsPure)
1826 .Case({ArgumentCondition(0U, WithinRange, {{
'A',
'Z'}, {
'a',
'z'}}),
1827 ReturnValueCondition(OutOfRange, SingleValue(0))},
1828 ErrnoIrrelevant,
"Assuming the character is alphabetical")
1830 .Case({ArgumentCondition(0U, WithinRange, {{128, UCharRangeMax}})},
1832 .Case({ArgumentCondition(
1834 {{
'A',
'Z'}, {
'a',
'z'}, {128, UCharRangeMax}}),
1835 ReturnValueCondition(WithinRange, SingleValue(0))},
1836 ErrnoIrrelevant,
"Assuming the character is non-alphabetical"));
1837 addToFunctionSummaryMap(
1838 "isascii", Signature(ArgTypes{IntTy}, RetType{IntTy}),
1839 Summary(EvalCallAsPure)
1840 .Case({ArgumentCondition(0U, WithinRange,
Range(0, 127)),
1841 ReturnValueCondition(OutOfRange, SingleValue(0))},
1842 ErrnoIrrelevant,
"Assuming the character is an ASCII character")
1843 .Case({ArgumentCondition(0U, OutOfRange,
Range(0, 127)),
1844 ReturnValueCondition(WithinRange, SingleValue(0))},
1846 "Assuming the character is not an ASCII character"));
1847 addToFunctionSummaryMap(
1848 "isblank", Signature(ArgTypes{IntTy}, RetType{IntTy}),
1849 Summary(EvalCallAsPure)
1850 .Case({ArgumentCondition(0U, WithinRange, {{
'\t',
'\t'}, {
' ',
' '}}),
1851 ReturnValueCondition(OutOfRange, SingleValue(0))},
1852 ErrnoIrrelevant,
"Assuming the character is a blank character")
1853 .Case({ArgumentCondition(0U, OutOfRange, {{
'\t',
'\t'}, {
' ',
' '}}),
1854 ReturnValueCondition(WithinRange, SingleValue(0))},
1856 "Assuming the character is not a blank character"));
1857 addToFunctionSummaryMap(
1858 "iscntrl", Signature(ArgTypes{IntTy}, RetType{IntTy}),
1859 Summary(EvalCallAsPure)
1860 .Case({ArgumentCondition(0U, WithinRange, {{0, 32}, {127, 127}}),
1861 ReturnValueCondition(OutOfRange, SingleValue(0))},
1863 "Assuming the character is a control character")
1864 .Case({ArgumentCondition(0U, OutOfRange, {{0, 32}, {127, 127}}),
1865 ReturnValueCondition(WithinRange, SingleValue(0))},
1867 "Assuming the character is not a control character"));
1868 addToFunctionSummaryMap(
1869 "isdigit", Signature(ArgTypes{IntTy}, RetType{IntTy}),
1870 Summary(EvalCallAsPure)
1871 .Case({ArgumentCondition(0U, WithinRange,
Range(
'0',
'9')),
1872 ReturnValueCondition(OutOfRange, SingleValue(0))},
1873 ErrnoIrrelevant,
"Assuming the character is a digit")
1874 .Case({ArgumentCondition(0U, OutOfRange,
Range(
'0',
'9')),
1875 ReturnValueCondition(WithinRange, SingleValue(0))},
1876 ErrnoIrrelevant,
"Assuming the character is not a digit"));
1877 addToFunctionSummaryMap(
1878 "isgraph", Signature(ArgTypes{IntTy}, RetType{IntTy}),
1879 Summary(EvalCallAsPure)
1880 .Case({ArgumentCondition(0U, WithinRange,
Range(33, 126)),
1881 ReturnValueCondition(OutOfRange, SingleValue(0))},
1883 "Assuming the character has graphical representation")
1885 {ArgumentCondition(0U, OutOfRange,
Range(33, 126)),
1886 ReturnValueCondition(WithinRange, SingleValue(0))},
1888 "Assuming the character does not have graphical representation"));
1889 addToFunctionSummaryMap(
1890 "islower", Signature(ArgTypes{IntTy}, RetType{IntTy}),
1891 Summary(EvalCallAsPure)
1893 .Case({ArgumentCondition(0U, WithinRange,
Range(
'a',
'z')),
1894 ReturnValueCondition(OutOfRange, SingleValue(0))},
1895 ErrnoIrrelevant,
"Assuming the character is a lowercase letter")
1897 .Case({ArgumentCondition(0U, WithinRange,
Range(0, 127)),
1898 ArgumentCondition(0U, OutOfRange,
Range(
'a',
'z')),
1899 ReturnValueCondition(WithinRange, SingleValue(0))},
1901 "Assuming the character is not a lowercase letter")
1903 .Case({ArgumentCondition(0U, WithinRange, {{128, UCharRangeMax}})},
1906 .Case({ArgumentCondition(0U, OutOfRange,
Range(0, UCharRangeMax)),
1907 ReturnValueCondition(WithinRange, SingleValue(0))},
1909 addToFunctionSummaryMap(
1910 "isprint", Signature(ArgTypes{IntTy}, RetType{IntTy}),
1911 Summary(EvalCallAsPure)
1912 .Case({ArgumentCondition(0U, WithinRange,
Range(32, 126)),
1913 ReturnValueCondition(OutOfRange, SingleValue(0))},
1914 ErrnoIrrelevant,
"Assuming the character is printable")
1915 .Case({ArgumentCondition(0U, OutOfRange,
Range(32, 126)),
1916 ReturnValueCondition(WithinRange, SingleValue(0))},
1917 ErrnoIrrelevant,
"Assuming the character is non-printable"));
1918 addToFunctionSummaryMap(
1919 "ispunct", Signature(ArgTypes{IntTy}, RetType{IntTy}),
1920 Summary(EvalCallAsPure)
1921 .Case({ArgumentCondition(
1923 {{
'!',
'/'}, {
':',
'@'}, {
'[',
'`'}, {
'{',
'~'}}),
1924 ReturnValueCondition(OutOfRange, SingleValue(0))},
1925 ErrnoIrrelevant,
"Assuming the character is a punctuation mark")
1926 .Case({ArgumentCondition(
1928 {{
'!',
'/'}, {
':',
'@'}, {
'[',
'`'}, {
'{',
'~'}}),
1929 ReturnValueCondition(WithinRange, SingleValue(0))},
1931 "Assuming the character is not a punctuation mark"));
1932 addToFunctionSummaryMap(
1933 "isspace", Signature(ArgTypes{IntTy}, RetType{IntTy}),
1934 Summary(EvalCallAsPure)
1936 .Case({ArgumentCondition(0U, WithinRange, {{9, 13}, {
' ',
' '}}),
1937 ReturnValueCondition(OutOfRange, SingleValue(0))},
1939 "Assuming the character is a whitespace character")
1941 .Case({ArgumentCondition(0U, WithinRange, {{128, UCharRangeMax}})},
1943 .Case({ArgumentCondition(0U, OutOfRange,
1944 {{9, 13}, {
' ',
' '}, {128, UCharRangeMax}}),
1945 ReturnValueCondition(WithinRange, SingleValue(0))},
1947 "Assuming the character is not a whitespace character"));
1948 addToFunctionSummaryMap(
1949 "isupper", Signature(ArgTypes{IntTy}, RetType{IntTy}),
1950 Summary(EvalCallAsPure)
1952 .Case({ArgumentCondition(0U, WithinRange,
Range(
'A',
'Z')),
1953 ReturnValueCondition(OutOfRange, SingleValue(0))},
1955 "Assuming the character is an uppercase letter")
1957 .Case({ArgumentCondition(0U, WithinRange, {{128, UCharRangeMax}})},
1960 .Case({ArgumentCondition(0U, OutOfRange,
1961 {{
'A',
'Z'}, {128, UCharRangeMax}}),
1962 ReturnValueCondition(WithinRange, SingleValue(0))},
1964 "Assuming the character is not an uppercase letter"));
1965 addToFunctionSummaryMap(
1966 "isxdigit", Signature(ArgTypes{IntTy}, RetType{IntTy}),
1967 Summary(EvalCallAsPure)
1968 .Case({ArgumentCondition(0U, WithinRange,
1969 {{
'0',
'9'}, {
'A',
'F'}, {
'a',
'f'}}),
1970 ReturnValueCondition(OutOfRange, SingleValue(0))},
1972 "Assuming the character is a hexadecimal digit")
1973 .Case({ArgumentCondition(0U, OutOfRange,
1974 {{
'0',
'9'}, {
'A',
'F'}, {
'a',
'f'}}),
1975 ReturnValueCondition(WithinRange, SingleValue(0))},
1977 "Assuming the character is not a hexadecimal digit"));
1978 addToFunctionSummaryMap(
1979 "toupper", Signature(ArgTypes{IntTy}, RetType{IntTy}),
1980 Summary(EvalCallAsPure)
1981 .ArgConstraint(ArgumentCondition(0U, WithinRange,
1982 {{EOFv, EOFv}, {0, UCharRangeMax}},
1983 "an unsigned char value or EOF")));
1984 addToFunctionSummaryMap(
1985 "tolower", Signature(ArgTypes{IntTy}, RetType{IntTy}),
1986 Summary(EvalCallAsPure)
1987 .ArgConstraint(ArgumentCondition(0U, WithinRange,
1988 {{EOFv, EOFv}, {0, UCharRangeMax}},
1989 "an unsigned char value or EOF")));
1990 addToFunctionSummaryMap(
1991 "toascii", Signature(ArgTypes{IntTy}, RetType{IntTy}),
1992 Summary(EvalCallAsPure)
1993 .ArgConstraint(ArgumentCondition(0U, WithinRange,
1994 {{EOFv, EOFv}, {0, UCharRangeMax}},
1995 "an unsigned char value or EOF")));
1997 addToFunctionSummaryMap(
1998 "getchar", Signature(ArgTypes{}, RetType{IntTy}),
2000 .Case({ReturnValueCondition(WithinRange,
2001 {{EOFv, EOFv}, {0, UCharRangeMax}})},
2007 .Case({ArgumentCondition(1U, WithinRange,
Range(1, SizeMax)),
2008 ArgumentCondition(2U, WithinRange,
Range(1, SizeMax)),
2009 ReturnValueCondition(BO_LT, ArgNo(2)),
2010 ReturnValueCondition(WithinRange,
Range(0, SizeMax))},
2011 ErrnoNEZeroIrrelevant, GenericFailureMsg)
2012 .Case({ArgumentCondition(1U, WithinRange,
Range(1, SizeMax)),
2013 ReturnValueCondition(BO_EQ, ArgNo(2)),
2014 ReturnValueCondition(WithinRange,
Range(0, SizeMax))},
2015 ErrnoMustNotBeChecked, GenericSuccessMsg)
2016 .Case({ArgumentCondition(1U, WithinRange, SingleValue(0)),
2017 ReturnValueCondition(WithinRange, SingleValue(0))},
2018 ErrnoMustNotBeChecked,
2019 "Assuming that argument 'size' to '{0}' is 0")
2020 .ArgConstraint(NotNullBuffer(ArgNo(0), ArgNo(1), ArgNo(2)))
2021 .ArgConstraint(NotNull(ArgNo(3)))
2022 .ArgConstraint(BufferSize(ArgNo(0), ArgNo(1),
2027 addToFunctionSummaryMap(
2029 Signature(ArgTypes{VoidPtrTy, SizeTyCanonTy, SizeTyCanonTy, FilePtrTy},
2030 RetType{SizeTyCanonTy}),
2034 addToFunctionSummaryMap(
"fwrite",
2035 Signature(ArgTypes{ConstVoidPtrTy, SizeTyCanonTy,
2036 SizeTyCanonTy, FilePtrTy},
2037 RetType{SizeTyCanonTy}),
2040 std::optional<QualType> Ssize_tTy = lookupTy(
"ssize_t");
2041 std::optional<RangeInt> Ssize_tMax = getMaxValue(Ssize_tTy);
2045 .Case({ReturnValueCondition(LessThanOrEq, ArgNo(2)),
2046 ReturnValueCondition(WithinRange,
Range(-1, Ssize_tMax))},
2052 addToFunctionSummaryMap(
2054 Signature(ArgTypes{IntTy, VoidPtrTy, SizeTyCanonTy}, RetType{Ssize_tTy}),
2057 addToFunctionSummaryMap(
2059 Signature(ArgTypes{IntTy, ConstVoidPtrTy, SizeTyCanonTy},
2060 RetType{Ssize_tTy}),
2063 auto GetLineSummary =
2065 .Case({ReturnValueCondition(WithinRange,
2066 Range({-1, -1}, {1, Ssize_tMax}))},
2069 QualType CharPtrPtrTy = getPointerTy(CharPtrTy);
2076 addToFunctionSummaryMap(
2078 Signature(ArgTypes{CharPtrPtrTy, SizePtrTy, FilePtrTy},
2079 RetType{Ssize_tTy}),
2083 addToFunctionSummaryMap(
2085 Signature(ArgTypes{CharPtrPtrTy, SizePtrTy, IntTy, FilePtrTy},
2086 RetType{Ssize_tTy}),
2090 Summary GetenvSummary =
2092 .ArgConstraint(NotNull(ArgNo(0)))
2093 .Case({NotNull(Ret)}, ErrnoIrrelevant,
2094 "Assuming the environment variable exists");
2096 if (!ShouldAssumeControlledEnvironment)
2097 GetenvSummary.Case({NotNull(Ret)->negate()}, ErrnoIrrelevant,
2098 "Assuming the environment variable does not exist");
2101 addToFunctionSummaryMap(
2102 "getenv", Signature(ArgTypes{ConstCharPtrTy}, RetType{CharPtrTy}),
2103 std::move(GetenvSummary));
2109 addToFunctionSummaryMap(
2110 {
"getc",
"fgetc"}, Signature(ArgTypes{FilePtrTy}, RetType{IntTy}),
2112 .Case({ReturnValueCondition(WithinRange,
2113 {{EOFv, EOFv}, {0, UCharRangeMax}})},
2115 .ArgConstraint(NotNull(ArgNo(0))));
2117 const auto ReturnsZero =
2118 ConstraintSet{ReturnValueCondition(WithinRange, SingleValue(0))};
2119 const auto ReturnsMinusOne =
2120 ConstraintSet{ReturnValueCondition(WithinRange, SingleValue(-1))};
2121 const auto ReturnsEOF =
2122 ConstraintSet{ReturnValueCondition(WithinRange, SingleValue(EOFv))};
2123 const auto ReturnsNonnegative =
2124 ConstraintSet{ReturnValueCondition(WithinRange,
Range(0, IntMax))};
2125 const auto ReturnsNonZero =
2126 ConstraintSet{ReturnValueCondition(OutOfRange, SingleValue(0))};
2127 const auto &ReturnsValidFileDescriptor = ReturnsNonnegative;
2129 auto ValidFileDescriptorOrAtFdcwd = [&](ArgNo ArgN) {
2130 return std::make_shared<RangeConstraint>(
2131 ArgN, WithinRange,
Range({AT_FDCWDv, AT_FDCWDv}, {0, IntMax}),
2132 "a valid file descriptor or AT_FDCWD");
2136 addToFunctionSummaryMap(
2138 Signature(ArgTypes{ConstCharPtrTy, ConstCharPtrTy}, RetType{FilePtrTy}),
2140 .Case({NotNull(Ret)}, ErrnoMustNotBeChecked, GenericSuccessMsg)
2141 .Case({
IsNull(Ret)}, ErrnoNEZeroIrrelevant, GenericFailureMsg)
2142 .ArgConstraint(NotNull(ArgNo(0)))
2143 .ArgConstraint(NotNull(ArgNo(1))));
2146 addToFunctionSummaryMap(
2148 Signature(ArgTypes{IntTy, ConstCharPtrTy}, RetType{FilePtrTy}),
2150 .Case({NotNull(Ret)}, ErrnoMustNotBeChecked, GenericSuccessMsg)
2151 .Case({
IsNull(Ret)}, ErrnoNEZeroIrrelevant, GenericFailureMsg)
2152 .ArgConstraint(ArgumentCondition(0, WithinRange,
Range(0, IntMax)))
2153 .ArgConstraint(NotNull(ArgNo(1))));
2156 addToFunctionSummaryMap(
2157 "tmpfile", Signature(ArgTypes{}, RetType{FilePtrTy}),
2159 .Case({NotNull(Ret)}, ErrnoMustNotBeChecked, GenericSuccessMsg)
2160 .Case({
IsNull(Ret)}, ErrnoNEZeroIrrelevant, GenericFailureMsg));
2164 addToFunctionSummaryMap(
2166 Signature(ArgTypes{ConstCharPtrTy, ConstCharPtrTy, FilePtrTy},
2167 RetType{FilePtrTy}),
2169 .Case({ReturnValueCondition(BO_EQ, ArgNo(2))},
2170 ErrnoMustNotBeChecked, GenericSuccessMsg)
2171 .Case({
IsNull(Ret)}, ErrnoNEZeroIrrelevant, GenericFailureMsg)
2172 .ArgConstraint(NotNull(ArgNo(1)))
2173 .ArgConstraint(NotNull(ArgNo(2))));
2176 addToFunctionSummaryMap(
2178 Signature(ArgTypes{ConstCharPtrTy, ConstCharPtrTy}, RetType{FilePtrTy}),
2180 .Case({NotNull(Ret)}, ErrnoMustNotBeChecked, GenericSuccessMsg)
2181 .Case({
IsNull(Ret)}, ErrnoNEZeroIrrelevant, GenericFailureMsg)
2182 .ArgConstraint(NotNull(ArgNo(0)))
2183 .ArgConstraint(NotNull(ArgNo(1))));
2186 addToFunctionSummaryMap(
2187 "fclose", Signature(ArgTypes{FilePtrTy}, RetType{IntTy}),
2189 .Case(ReturnsZero, ErrnoMustNotBeChecked, GenericSuccessMsg)
2190 .Case(ReturnsEOF, ErrnoNEZeroIrrelevant, GenericFailureMsg)
2191 .ArgConstraint(NotNull(ArgNo(0))));
2194 addToFunctionSummaryMap(
2195 "pclose", Signature(ArgTypes{FilePtrTy}, RetType{IntTy}),
2197 .Case({ReturnValueCondition(WithinRange, {{0, IntMax}})},
2198 ErrnoMustNotBeChecked, GenericSuccessMsg)
2199 .Case(ReturnsMinusOne, ErrnoNEZeroIrrelevant, GenericFailureMsg)
2200 .ArgConstraint(NotNull(ArgNo(0))));
2202 std::optional<QualType> Off_tTy = lookupTy(
"off_t");
2203 std::optional<RangeInt> Off_tMax = getMaxValue(Off_tTy);
2207 addToFunctionSummaryMap(
2208 {
"getc",
"fgetc"}, Signature(ArgTypes{FilePtrTy}, RetType{IntTy}),
2210 .Case({ReturnValueCondition(WithinRange, {{0, UCharRangeMax}})},
2211 ErrnoMustNotBeChecked, GenericSuccessMsg)
2212 .Case({ReturnValueCondition(WithinRange, SingleValue(EOFv))},
2213 ErrnoIrrelevant, GenericFailureMsg)
2214 .ArgConstraint(NotNull(ArgNo(0))));
2218 addToFunctionSummaryMap(
2220 Signature(ArgTypes{IntTy, FilePtrTy}, RetType{IntTy}),
2222 .Case({ArgumentCondition(0, WithinRange,
Range(0, UCharRangeMax)),
2223 ReturnValueCondition(BO_EQ, ArgNo(0))},
2224 ErrnoMustNotBeChecked, GenericSuccessMsg)
2225 .Case({ArgumentCondition(0, OutOfRange,
Range(0, UCharRangeMax)),
2226 ReturnValueCondition(WithinRange,
Range(0, UCharRangeMax))},
2227 ErrnoMustNotBeChecked, GenericSuccessMsg)
2228 .Case({ReturnValueCondition(WithinRange, SingleValue(EOFv))},
2229 ErrnoNEZeroIrrelevant, GenericFailureMsg)
2230 .ArgConstraint(NotNull(ArgNo(1))));
2233 addToFunctionSummaryMap(
2235 Signature(ArgTypes{CharPtrTy, IntTy, FilePtrTy}, RetType{CharPtrTy}),
2237 .Case({NotNull(Ret), ReturnValueCondition(BO_EQ, ArgNo(0))},
2238 ErrnoMustNotBeChecked, GenericSuccessMsg)
2239 .Case({
IsNull(Ret)}, ErrnoIrrelevant, GenericFailureMsg)
2240 .ArgConstraint(NotNull(ArgNo(0)))
2241 .ArgConstraint(ArgumentCondition(1, WithinRange,
Range(0, IntMax)))
2243 BufferSize(ArgNo(0), ArgNo(1)))
2244 .ArgConstraint(NotNull(ArgNo(2))));
2247 addToFunctionSummaryMap(
2248 "fputs", Signature(ArgTypes{ConstCharPtrTy, FilePtrTy}, RetType{IntTy}),
2250 .Case(ReturnsNonnegative, ErrnoMustNotBeChecked, GenericSuccessMsg)
2251 .Case({ReturnValueCondition(WithinRange, SingleValue(EOFv))},
2252 ErrnoNEZeroIrrelevant, GenericFailureMsg)
2253 .ArgConstraint(NotNull(ArgNo(0)))
2254 .ArgConstraint(NotNull(ArgNo(1))));
2257 addToFunctionSummaryMap(
2258 "ungetc", Signature(ArgTypes{IntTy, FilePtrTy}, RetType{IntTy}),
2260 .Case({ReturnValueCondition(BO_EQ, ArgNo(0)),
2261 ArgumentCondition(0, WithinRange, {{0, UCharRangeMax}})},
2262 ErrnoMustNotBeChecked, GenericSuccessMsg)
2263 .Case({ReturnValueCondition(WithinRange, SingleValue(EOFv)),
2264 ArgumentCondition(0, WithinRange, SingleValue(EOFv))},
2265 ErrnoNEZeroIrrelevant,
2266 "Assuming that 'ungetc' fails because EOF was passed as "
2268 .Case({ReturnValueCondition(WithinRange, SingleValue(EOFv)),
2269 ArgumentCondition(0, WithinRange, {{0, UCharRangeMax}})},
2270 ErrnoNEZeroIrrelevant, GenericFailureMsg)
2271 .ArgConstraint(ArgumentCondition(
2272 0, WithinRange, {{EOFv, EOFv}, {0, UCharRangeMax}}))
2273 .ArgConstraint(NotNull(ArgNo(1))));
2279 addToFunctionSummaryMap(
2280 "fseek", Signature(ArgTypes{FilePtrTy, LongTy, IntTy}, RetType{IntTy}),
2282 .Case(ReturnsZero, ErrnoMustNotBeChecked, GenericSuccessMsg)
2283 .Case(ReturnsMinusOne, ErrnoNEZeroIrrelevant, GenericFailureMsg)
2284 .ArgConstraint(NotNull(ArgNo(0)))
2285 .ArgConstraint(ArgumentCondition(2, WithinRange, {{0, 2}})));
2288 addToFunctionSummaryMap(
2290 Signature(ArgTypes{FilePtrTy, Off_tTy, IntTy}, RetType{IntTy}),
2292 .Case(ReturnsZero, ErrnoMustNotBeChecked, GenericSuccessMsg)
2293 .Case(ReturnsMinusOne, ErrnoNEZeroIrrelevant, GenericFailureMsg)
2294 .ArgConstraint(NotNull(ArgNo(0)))
2295 .ArgConstraint(ArgumentCondition(2, WithinRange, {{0, 2}})));
2301 addToFunctionSummaryMap(
2302 "fgetpos", Signature(ArgTypes{FilePtrTy, FPosTPtrTy}, RetType{IntTy}),
2304 .Case(ReturnsZero, ErrnoUnchanged, GenericSuccessMsg)
2305 .Case(ReturnsNonZero, ErrnoNEZeroIrrelevant, GenericFailureMsg)
2306 .ArgConstraint(NotNull(ArgNo(0)))
2307 .ArgConstraint(NotNull(ArgNo(1))));
2313 addToFunctionSummaryMap(
2315 Signature(ArgTypes{FilePtrTy, ConstFPosTPtrTy}, RetType{IntTy}),
2317 .Case(ReturnsZero, ErrnoUnchanged, GenericSuccessMsg)
2318 .Case(ReturnsNonZero, ErrnoNEZeroIrrelevant, GenericFailureMsg)
2319 .ArgConstraint(NotNull(ArgNo(0)))
2320 .ArgConstraint(NotNull(ArgNo(1))));
2323 addToFunctionSummaryMap(
2324 "fflush", Signature(ArgTypes{FilePtrTy}, RetType{IntTy}),
2326 .Case(ReturnsZero, ErrnoMustNotBeChecked, GenericSuccessMsg)
2327 .Case(ReturnsEOF, ErrnoNEZeroIrrelevant, GenericFailureMsg));
2333 addToFunctionSummaryMap(
2334 "ftell", Signature(ArgTypes{FilePtrTy}, RetType{LongTy}),
2336 .Case({ReturnValueCondition(WithinRange,
Range(0, LongMax))},
2337 ErrnoUnchanged, GenericSuccessMsg)
2338 .Case(ReturnsMinusOne, ErrnoNEZeroIrrelevant, GenericFailureMsg)
2339 .ArgConstraint(NotNull(ArgNo(0))));
2342 addToFunctionSummaryMap(
2343 "ftello", Signature(ArgTypes{FilePtrTy}, RetType{Off_tTy}),
2345 .Case({ReturnValueCondition(WithinRange,
Range(0, Off_tMax))},
2346 ErrnoMustNotBeChecked, GenericSuccessMsg)
2347 .Case(ReturnsMinusOne, ErrnoNEZeroIrrelevant, GenericFailureMsg)
2348 .ArgConstraint(NotNull(ArgNo(0))));
2356 addToFunctionSummaryMap(
2357 "fileno", Signature(ArgTypes{FilePtrTy}, RetType{IntTy}),
2359 .Case(ReturnsValidFileDescriptor, ErrnoUnchanged, GenericSuccessMsg)
2360 .ArgConstraint(NotNull(ArgNo(0))));
2364 addToFunctionSummaryMap(
"rewind",
2365 Signature(ArgTypes{FilePtrTy}, RetType{VoidTy}),
2367 .Case({}, ErrnoMustBeChecked)
2368 .ArgConstraint(NotNull(ArgNo(0))));
2371 addToFunctionSummaryMap(
2372 "clearerr", Signature(ArgTypes{FilePtrTy}, RetType{VoidTy}),
2373 Summary(NoEvalCall).ArgConstraint(NotNull(ArgNo(0))));
2376 addToFunctionSummaryMap(
2377 "feof", Signature(ArgTypes{FilePtrTy}, RetType{IntTy}),
2378 Summary(NoEvalCall).ArgConstraint(NotNull(ArgNo(0))));
2381 addToFunctionSummaryMap(
2382 "ferror", Signature(ArgTypes{FilePtrTy}, RetType{IntTy}),
2383 Summary(NoEvalCall).ArgConstraint(NotNull(ArgNo(0))));
2386 addToFunctionSummaryMap(
2387 "a64l", Signature(ArgTypes{ConstCharPtrTy}, RetType{LongTy}),
2388 Summary(NoEvalCall).ArgConstraint(NotNull(ArgNo(0))));
2391 addToFunctionSummaryMap(
"l64a",
2392 Signature(ArgTypes{LongTy}, RetType{CharPtrTy}),
2394 .ArgConstraint(ArgumentCondition(
2395 0, WithinRange,
Range(0, LongMax))));
2398 addToFunctionSummaryMap(
2399 "open", Signature(ArgTypes{ConstCharPtrTy, IntTy}, RetType{IntTy}),
2401 .Case(ReturnsValidFileDescriptor, ErrnoMustNotBeChecked,
2403 .Case(ReturnsMinusOne, ErrnoNEZeroIrrelevant, GenericFailureMsg)
2404 .ArgConstraint(NotNull(ArgNo(0))));
2407 addToFunctionSummaryMap(
2409 Signature(ArgTypes{IntTy, ConstCharPtrTy, IntTy}, RetType{IntTy}),
2411 .Case(ReturnsValidFileDescriptor, ErrnoMustNotBeChecked,
2413 .Case(ReturnsMinusOne, ErrnoNEZeroIrrelevant, GenericFailureMsg)
2414 .ArgConstraint(ValidFileDescriptorOrAtFdcwd(ArgNo(0)))
2415 .ArgConstraint(NotNull(ArgNo(1))));
2418 addToFunctionSummaryMap(
2419 "access", Signature(ArgTypes{ConstCharPtrTy, IntTy}, RetType{IntTy}),
2421 .Case(ReturnsZero, ErrnoMustNotBeChecked, GenericSuccessMsg)
2422 .Case(ReturnsMinusOne, ErrnoNEZeroIrrelevant, GenericFailureMsg)
2423 .ArgConstraint(NotNull(ArgNo(0))));
2426 addToFunctionSummaryMap(
2428 Signature(ArgTypes{IntTy, ConstCharPtrTy, IntTy, IntTy},
2431 .Case(ReturnsZero, ErrnoMustNotBeChecked, GenericSuccessMsg)
2432 .Case(ReturnsMinusOne, ErrnoNEZeroIrrelevant, GenericFailureMsg)
2433 .ArgConstraint(ValidFileDescriptorOrAtFdcwd(ArgNo(0)))
2434 .ArgConstraint(NotNull(ArgNo(1))));
2437 addToFunctionSummaryMap(
2438 "dup", Signature(ArgTypes{IntTy}, RetType{IntTy}),
2440 .Case(ReturnsValidFileDescriptor, ErrnoMustNotBeChecked,
2442 .Case(ReturnsMinusOne, ErrnoNEZeroIrrelevant, GenericFailureMsg)
2444 ArgumentCondition(0, WithinRange,
Range(0, IntMax))));
2447 addToFunctionSummaryMap(
2448 "dup2", Signature(ArgTypes{IntTy, IntTy}, RetType{IntTy}),
2450 .Case(ReturnsValidFileDescriptor, ErrnoMustNotBeChecked,
2452 .Case(ReturnsMinusOne, ErrnoNEZeroIrrelevant, GenericFailureMsg)
2453 .ArgConstraint(ArgumentCondition(0, WithinRange,
Range(0, IntMax)))
2455 ArgumentCondition(1, WithinRange,
Range(0, IntMax))));
2458 addToFunctionSummaryMap(
2459 "fdatasync", Signature(ArgTypes{IntTy}, RetType{IntTy}),
2461 .Case(ReturnsZero, ErrnoMustNotBeChecked, GenericSuccessMsg)
2462 .Case(ReturnsMinusOne, ErrnoNEZeroIrrelevant, GenericFailureMsg)
2464 ArgumentCondition(0, WithinRange,
Range(0, IntMax))));
2467 addToFunctionSummaryMap(
2469 Signature(ArgTypes{ConstCharPtrTy, ConstCharPtrTy, IntTy},
2472 .ArgConstraint(NotNull(ArgNo(0)))
2473 .ArgConstraint(NotNull(ArgNo(1))));
2476 addToFunctionSummaryMap(
2477 "fsync", Signature(ArgTypes{IntTy}, RetType{IntTy}),
2479 .Case(ReturnsZero, ErrnoMustNotBeChecked, GenericSuccessMsg)
2480 .Case(ReturnsMinusOne, ErrnoNEZeroIrrelevant, GenericFailureMsg)
2482 ArgumentCondition(0, WithinRange,
Range(0, IntMax))));
2485 addToFunctionSummaryMap(
2487 Signature(ArgTypes{ConstCharPtrTy, Off_tTy}, RetType{IntTy}),
2489 .Case(ReturnsZero, ErrnoMustNotBeChecked, GenericSuccessMsg)
2490 .Case(ReturnsMinusOne, ErrnoNEZeroIrrelevant, GenericFailureMsg)
2491 .ArgConstraint(NotNull(ArgNo(0))));
2494 addToFunctionSummaryMap(
2496 Signature(ArgTypes{ConstCharPtrTy, ConstCharPtrTy}, RetType{IntTy}),
2498 .Case(ReturnsZero, ErrnoMustNotBeChecked, GenericSuccessMsg)
2499 .Case(ReturnsMinusOne, ErrnoNEZeroIrrelevant, GenericFailureMsg)
2500 .ArgConstraint(NotNull(ArgNo(0)))
2501 .ArgConstraint(NotNull(ArgNo(1))));
2504 addToFunctionSummaryMap(
2506 Signature(ArgTypes{ConstCharPtrTy, IntTy, ConstCharPtrTy},
2509 .Case(ReturnsZero, ErrnoMustNotBeChecked, GenericSuccessMsg)
2510 .Case(ReturnsMinusOne, ErrnoNEZeroIrrelevant, GenericFailureMsg)
2511 .ArgConstraint(NotNull(ArgNo(0)))
2512 .ArgConstraint(ValidFileDescriptorOrAtFdcwd(ArgNo(1)))
2513 .ArgConstraint(NotNull(ArgNo(2))));
2516 addToFunctionSummaryMap(
2517 "lockf", Signature(ArgTypes{IntTy, IntTy, Off_tTy}, RetType{IntTy}),
2519 .Case(ReturnsZero, ErrnoMustNotBeChecked, GenericSuccessMsg)
2520 .Case(ReturnsMinusOne, ErrnoNEZeroIrrelevant, GenericFailureMsg)
2522 ArgumentCondition(0, WithinRange,
Range(0, IntMax))));
2524 std::optional<QualType> Mode_tTy = lookupTy(
"mode_t");
2527 addToFunctionSummaryMap(
2528 "creat", Signature(ArgTypes{ConstCharPtrTy, Mode_tTy}, RetType{IntTy}),
2530 .Case(ReturnsValidFileDescriptor, ErrnoMustNotBeChecked,
2532 .Case(ReturnsMinusOne, ErrnoNEZeroIrrelevant, GenericFailureMsg)
2533 .ArgConstraint(NotNull(ArgNo(0))));
2536 addToFunctionSummaryMap(
2537 "sleep", Signature(ArgTypes{UnsignedIntTy}, RetType{UnsignedIntTy}),
2540 ArgumentCondition(0, WithinRange,
Range(0, UnsignedIntMax))));
2542 std::optional<QualType> DirTy = lookupTy(
"DIR");
2543 std::optional<QualType> DirPtrTy = getPointerTy(DirTy);
2546 addToFunctionSummaryMap(
2547 "dirfd", Signature(ArgTypes{DirPtrTy}, RetType{IntTy}),
2549 .Case(ReturnsValidFileDescriptor, ErrnoMustNotBeChecked,
2551 .Case(ReturnsMinusOne, ErrnoNEZeroIrrelevant, GenericFailureMsg)
2552 .ArgConstraint(NotNull(ArgNo(0))));
2555 addToFunctionSummaryMap(
2556 "alarm", Signature(ArgTypes{UnsignedIntTy}, RetType{UnsignedIntTy}),
2559 ArgumentCondition(0, WithinRange,
Range(0, UnsignedIntMax))));
2562 addToFunctionSummaryMap(
2563 "closedir", Signature(ArgTypes{DirPtrTy}, RetType{IntTy}),
2565 .Case(ReturnsZero, ErrnoMustNotBeChecked, GenericSuccessMsg)
2566 .Case(ReturnsMinusOne, ErrnoNEZeroIrrelevant, GenericFailureMsg)
2567 .ArgConstraint(NotNull(ArgNo(0))));
2570 addToFunctionSummaryMap(
2571 "strdup", Signature(ArgTypes{ConstCharPtrTy}, RetType{CharPtrTy}),
2572 Summary(NoEvalCall).ArgConstraint(NotNull(ArgNo(0))));
2575 addToFunctionSummaryMap(
2577 Signature(ArgTypes{ConstCharPtrTy, SizeTyCanonTy}, RetType{CharPtrTy}),
2579 .ArgConstraint(NotNull(ArgNo(0)))
2581 ArgumentCondition(1, WithinRange,
Range(0, SizeMax))));
2584 addToFunctionSummaryMap(
2585 "wcsdup", Signature(ArgTypes{ConstWchar_tPtrTy}, RetType{Wchar_tPtrTy}),
2586 Summary(NoEvalCall).ArgConstraint(NotNull(ArgNo(0))));
2589 addToFunctionSummaryMap(
2590 "mkstemp", Signature(ArgTypes{CharPtrTy}, RetType{IntTy}),
2592 .Case(ReturnsValidFileDescriptor, ErrnoMustNotBeChecked,
2594 .Case(ReturnsMinusOne, ErrnoNEZeroIrrelevant, GenericFailureMsg)
2595 .ArgConstraint(NotNull(ArgNo(0))));
2598 addToFunctionSummaryMap(
2599 "mkdtemp", Signature(ArgTypes{CharPtrTy}, RetType{CharPtrTy}),
2601 .Case({NotNull(Ret), ReturnValueCondition(BO_EQ, ArgNo(0))},
2602 ErrnoMustNotBeChecked, GenericSuccessMsg)
2603 .Case({
IsNull(Ret)}, ErrnoNEZeroIrrelevant, GenericFailureMsg)
2604 .ArgConstraint(NotNull(ArgNo(0))));
2607 addToFunctionSummaryMap(
2609 Signature(ArgTypes{CharPtrTy, SizeTyCanonTy}, RetType{CharPtrTy}),
2612 ArgumentCondition(1, WithinRange,
Range(1, SizeMax)),
2613 ReturnValueCondition(BO_EQ, ArgNo(0)), NotNull(Ret)},
2614 ErrnoMustNotBeChecked, GenericSuccessMsg)
2616 ArgumentCondition(1, WithinRange, SingleValue(0)),
2618 ErrnoNEZeroIrrelevant,
"Assuming that argument 'size' is 0")
2620 ArgumentCondition(1, WithinRange,
Range(1, SizeMax)),
2622 ErrnoNEZeroIrrelevant, GenericFailureMsg)
2623 .Case({
IsNull(0), NotNull(Ret)}, ErrnoMustNotBeChecked,
2628 BufferSize( ArgNo(0), ArgNo(1)))
2630 ArgumentCondition(1, WithinRange,
Range(0, SizeMax))));
2633 addToFunctionSummaryMap(
2634 "mkdir", Signature(ArgTypes{ConstCharPtrTy, Mode_tTy}, RetType{IntTy}),
2636 .Case(ReturnsZero, ErrnoMustNotBeChecked, GenericSuccessMsg)
2637 .Case(ReturnsMinusOne, ErrnoNEZeroIrrelevant, GenericFailureMsg)
2638 .ArgConstraint(NotNull(ArgNo(0))));
2641 addToFunctionSummaryMap(
2643 Signature(ArgTypes{IntTy, ConstCharPtrTy, Mode_tTy}, RetType{IntTy}),
2645 .Case(ReturnsZero, ErrnoMustNotBeChecked, GenericSuccessMsg)
2646 .Case(ReturnsMinusOne, ErrnoNEZeroIrrelevant, GenericFailureMsg)
2647 .ArgConstraint(ValidFileDescriptorOrAtFdcwd(ArgNo(0)))
2648 .ArgConstraint(NotNull(ArgNo(1))));
2650 std::optional<QualType> Dev_tTy = lookupTy(
"dev_t");
2653 addToFunctionSummaryMap(
2655 Signature(ArgTypes{ConstCharPtrTy, Mode_tTy, Dev_tTy}, RetType{IntTy}),
2657 .Case(ReturnsZero, ErrnoMustNotBeChecked, GenericSuccessMsg)
2658 .Case(ReturnsMinusOne, ErrnoNEZeroIrrelevant, GenericFailureMsg)
2659 .ArgConstraint(NotNull(ArgNo(0))));
2662 addToFunctionSummaryMap(
2664 Signature(ArgTypes{IntTy, ConstCharPtrTy, Mode_tTy, Dev_tTy},
2667 .Case(ReturnsZero, ErrnoMustNotBeChecked, GenericSuccessMsg)
2668 .Case(ReturnsMinusOne, ErrnoNEZeroIrrelevant, GenericFailureMsg)
2669 .ArgConstraint(ValidFileDescriptorOrAtFdcwd(ArgNo(0)))
2670 .ArgConstraint(NotNull(ArgNo(1))));
2673 addToFunctionSummaryMap(
2674 "chmod", Signature(ArgTypes{ConstCharPtrTy, Mode_tTy}, RetType{IntTy}),
2676 .Case(ReturnsZero, ErrnoMustNotBeChecked, GenericSuccessMsg)
2677 .Case(ReturnsMinusOne, ErrnoNEZeroIrrelevant, GenericFailureMsg)
2678 .ArgConstraint(NotNull(ArgNo(0))));
2681 addToFunctionSummaryMap(
2683 Signature(ArgTypes{IntTy, ConstCharPtrTy, Mode_tTy, IntTy},
2686 .Case(ReturnsZero, ErrnoMustNotBeChecked, GenericSuccessMsg)
2687 .Case(ReturnsMinusOne, ErrnoNEZeroIrrelevant, GenericFailureMsg)
2688 .ArgConstraint(ValidFileDescriptorOrAtFdcwd(ArgNo(0)))
2689 .ArgConstraint(NotNull(ArgNo(1))));
2692 addToFunctionSummaryMap(
2693 "fchmod", Signature(ArgTypes{IntTy, Mode_tTy}, RetType{IntTy}),
2695 .Case(ReturnsZero, ErrnoMustNotBeChecked, GenericSuccessMsg)
2696 .Case(ReturnsMinusOne, ErrnoNEZeroIrrelevant, GenericFailureMsg)
2698 ArgumentCondition(0, WithinRange,
Range(0, IntMax))));
2700 std::optional<QualType> Uid_tTy = lookupTy(
"uid_t");
2701 std::optional<QualType> Gid_tTy = lookupTy(
"gid_t");
2705 addToFunctionSummaryMap(
2707 Signature(ArgTypes{IntTy, ConstCharPtrTy, Uid_tTy, Gid_tTy, IntTy},
2710 .Case(ReturnsZero, ErrnoMustNotBeChecked, GenericSuccessMsg)
2711 .Case(ReturnsMinusOne, ErrnoNEZeroIrrelevant, GenericFailureMsg)
2712 .ArgConstraint(ValidFileDescriptorOrAtFdcwd(ArgNo(0)))
2713 .ArgConstraint(NotNull(ArgNo(1))));
2716 addToFunctionSummaryMap(
2718 Signature(ArgTypes{ConstCharPtrTy, Uid_tTy, Gid_tTy}, RetType{IntTy}),
2720 .Case(ReturnsZero, ErrnoMustNotBeChecked, GenericSuccessMsg)
2721 .Case(ReturnsMinusOne, ErrnoNEZeroIrrelevant, GenericFailureMsg)
2722 .ArgConstraint(NotNull(ArgNo(0))));
2725 addToFunctionSummaryMap(
2727 Signature(ArgTypes{ConstCharPtrTy, Uid_tTy, Gid_tTy}, RetType{IntTy}),
2729 .Case(ReturnsZero, ErrnoMustNotBeChecked, GenericSuccessMsg)
2730 .Case(ReturnsMinusOne, ErrnoNEZeroIrrelevant, GenericFailureMsg)
2731 .ArgConstraint(NotNull(ArgNo(0))));
2734 addToFunctionSummaryMap(
2735 "fchown", Signature(ArgTypes{IntTy, Uid_tTy, Gid_tTy}, RetType{IntTy}),
2737 .Case(ReturnsZero, ErrnoMustNotBeChecked, GenericSuccessMsg)
2738 .Case(ReturnsMinusOne, ErrnoNEZeroIrrelevant, GenericFailureMsg)
2740 ArgumentCondition(0, WithinRange,
Range(0, IntMax))));
2743 addToFunctionSummaryMap(
2744 "rmdir", Signature(ArgTypes{ConstCharPtrTy}, RetType{IntTy}),
2746 .Case(ReturnsZero, ErrnoMustNotBeChecked, GenericSuccessMsg)
2747 .Case(ReturnsMinusOne, ErrnoNEZeroIrrelevant, GenericFailureMsg)
2748 .ArgConstraint(NotNull(ArgNo(0))));
2751 addToFunctionSummaryMap(
2752 "chdir", Signature(ArgTypes{ConstCharPtrTy}, RetType{IntTy}),
2754 .Case(ReturnsZero, ErrnoMustNotBeChecked, GenericSuccessMsg)
2755 .Case(ReturnsMinusOne, ErrnoNEZeroIrrelevant, GenericFailureMsg)
2756 .ArgConstraint(NotNull(ArgNo(0))));
2759 addToFunctionSummaryMap(
2761 Signature(ArgTypes{ConstCharPtrTy, ConstCharPtrTy}, RetType{IntTy}),
2763 .Case(ReturnsZero, ErrnoMustNotBeChecked, GenericSuccessMsg)
2764 .Case(ReturnsMinusOne, ErrnoNEZeroIrrelevant, GenericFailureMsg)
2765 .ArgConstraint(NotNull(ArgNo(0)))
2766 .ArgConstraint(NotNull(ArgNo(1))));
2770 addToFunctionSummaryMap(
2772 Signature(ArgTypes{IntTy, ConstCharPtrTy, IntTy, ConstCharPtrTy, IntTy},
2775 .Case(ReturnsZero, ErrnoMustNotBeChecked, GenericSuccessMsg)
2776 .Case(ReturnsMinusOne, ErrnoNEZeroIrrelevant, GenericFailureMsg)
2777 .ArgConstraint(ValidFileDescriptorOrAtFdcwd(ArgNo(0)))
2778 .ArgConstraint(NotNull(ArgNo(1)))
2779 .ArgConstraint(ValidFileDescriptorOrAtFdcwd(ArgNo(2)))
2780 .ArgConstraint(NotNull(ArgNo(3))));
2783 addToFunctionSummaryMap(
2784 "unlink", Signature(ArgTypes{ConstCharPtrTy}, RetType{IntTy}),
2786 .Case(ReturnsZero, ErrnoMustNotBeChecked, GenericSuccessMsg)
2787 .Case(ReturnsMinusOne, ErrnoNEZeroIrrelevant, GenericFailureMsg)
2788 .ArgConstraint(NotNull(ArgNo(0))));
2791 addToFunctionSummaryMap(
2793 Signature(ArgTypes{IntTy, ConstCharPtrTy, IntTy}, RetType{IntTy}),
2795 .Case(ReturnsZero, ErrnoMustNotBeChecked, GenericSuccessMsg)
2796 .Case(ReturnsMinusOne, ErrnoNEZeroIrrelevant, GenericFailureMsg)
2797 .ArgConstraint(ValidFileDescriptorOrAtFdcwd(ArgNo(0)))
2798 .ArgConstraint(NotNull(ArgNo(1))));
2800 std::optional<QualType> StructStatTy = lookupTy(
"stat");
2801 std::optional<QualType> StructStatPtrTy = getPointerTy(StructStatTy);
2804 addToFunctionSummaryMap(
2805 "fstat", Signature(ArgTypes{IntTy, StructStatPtrTy}, RetType{IntTy}),
2807 .Case(ReturnsZero, ErrnoMustNotBeChecked, GenericSuccessMsg)
2808 .Case(ReturnsMinusOne, ErrnoNEZeroIrrelevant, GenericFailureMsg)
2809 .ArgConstraint(ArgumentCondition(0, WithinRange,
Range(0, IntMax)))
2810 .ArgConstraint(NotNull(ArgNo(1))));
2813 addToFunctionSummaryMap(
2815 Signature(ArgTypes{ConstCharPtrTy, StructStatPtrTy}, RetType{IntTy}),
2817 .Case(ReturnsZero, ErrnoMustNotBeChecked, GenericSuccessMsg)
2818 .Case(ReturnsMinusOne, ErrnoNEZeroIrrelevant, GenericFailureMsg)
2819 .ArgConstraint(NotNull(ArgNo(0)))
2820 .ArgConstraint(NotNull(ArgNo(1))));
2823 addToFunctionSummaryMap(
2825 Signature(ArgTypes{ConstCharPtrTy, StructStatPtrTy}, RetType{IntTy}),
2827 .Case(ReturnsZero, ErrnoMustNotBeChecked, GenericSuccessMsg)
2828 .Case(ReturnsMinusOne, ErrnoNEZeroIrrelevant, GenericFailureMsg)
2829 .ArgConstraint(NotNull(ArgNo(0)))
2830 .ArgConstraint(NotNull(ArgNo(1))));
2834 addToFunctionSummaryMap(
2836 Signature(ArgTypes{IntTy, ConstCharPtrTy, StructStatPtrTy, IntTy},
2839 .Case(ReturnsZero, ErrnoMustNotBeChecked, GenericSuccessMsg)
2840 .Case(ReturnsMinusOne, ErrnoNEZeroIrrelevant, GenericFailureMsg)
2841 .ArgConstraint(ValidFileDescriptorOrAtFdcwd(ArgNo(0)))
2842 .ArgConstraint(NotNull(ArgNo(1)))
2843 .ArgConstraint(NotNull(ArgNo(2))));
2846 addToFunctionSummaryMap(
2847 "opendir", Signature(ArgTypes{ConstCharPtrTy}, RetType{DirPtrTy}),
2849 .Case({NotNull(Ret)}, ErrnoMustNotBeChecked, GenericSuccessMsg)
2850 .Case({
IsNull(Ret)}, ErrnoNEZeroIrrelevant, GenericFailureMsg)
2851 .ArgConstraint(NotNull(ArgNo(0))));
2854 addToFunctionSummaryMap(
2855 "fdopendir", Signature(ArgTypes{IntTy}, RetType{DirPtrTy}),
2857 .Case({NotNull(Ret)}, ErrnoMustNotBeChecked, GenericSuccessMsg)
2858 .Case({
IsNull(Ret)}, ErrnoNEZeroIrrelevant, GenericFailureMsg)
2860 ArgumentCondition(0, WithinRange,
Range(0, IntMax))));
2863 addToFunctionSummaryMap(
2864 "isatty", Signature(ArgTypes{IntTy}, RetType{IntTy}),
2866 .Case({ReturnValueCondition(WithinRange,
Range(0, 1))},
2869 ArgumentCondition(0, WithinRange,
Range(0, IntMax))));
2872 addToFunctionSummaryMap(
2873 "close", Signature(ArgTypes{IntTy}, RetType{IntTy}),
2875 .Case(ReturnsZero, ErrnoMustNotBeChecked, GenericSuccessMsg)
2876 .Case(ReturnsMinusOne, ErrnoNEZeroIrrelevant, GenericFailureMsg)
2878 ArgumentCondition(0, WithinRange,
Range(-1, IntMax))));
2881 addToFunctionSummaryMap(
"fpathconf",
2882 Signature(ArgTypes{IntTy, IntTy}, RetType{LongTy}),
2884 .ArgConstraint(ArgumentCondition(
2885 0, WithinRange,
Range(0, IntMax))));
2888 addToFunctionSummaryMap(
2889 "pathconf", Signature(ArgTypes{ConstCharPtrTy, IntTy}, RetType{LongTy}),
2890 Summary(NoEvalCall).ArgConstraint(NotNull(ArgNo(0))));
2893 addToFunctionSummaryMap(
2894 "rewinddir", Signature(ArgTypes{DirPtrTy}, RetType{VoidTy}),
2895 Summary(NoEvalCall).ArgConstraint(NotNull(ArgNo(0))));
2898 addToFunctionSummaryMap(
2899 "seekdir", Signature(ArgTypes{DirPtrTy, LongTy}, RetType{VoidTy}),
2900 Summary(NoEvalCall).ArgConstraint(NotNull(ArgNo(0))));
2903 addToFunctionSummaryMap(
2904 "rand_r", Signature(ArgTypes{UnsignedIntPtrTy}, RetType{IntTy}),
2905 Summary(NoEvalCall).ArgConstraint(NotNull(ArgNo(0))));
2910 auto MmapSignature = Signature(
2911 ArgTypes{VoidPtrTy, SizeTyCanonTy, IntTy, IntTy, IntTy, Off_tTy},
2912 RetType{VoidPtrTy});
2913 auto MmapSummaryWithLengthConstraint =
2916 ArgumentCondition(1, WithinRange,
Range(1, SizeMax)));
2923 addToFunctionSummaryMap(
"mmap", MmapSignature,
2924 MmapSummaryWithLengthConstraint);
2927 addToFunctionSummaryMap(
2928 "mmap", MmapSignature,
2929 MmapSummaryWithLengthConstraint.ArgConstraint(
2930 ArgumentCondition(4, WithinRange,
Range(-1, IntMax))));
2933 std::optional<QualType> Off64_tTy = lookupTy(
"off64_t");
2937 addToFunctionSummaryMap(
2940 ArgTypes{VoidPtrTy, SizeTyCanonTy, IntTy, IntTy, IntTy, Off64_tTy},
2941 RetType{VoidPtrTy}),
2943 .ArgConstraint(ArgumentCondition(1, WithinRange,
Range(1, SizeMax)))
2945 ArgumentCondition(4, WithinRange,
Range(-1, IntMax))));
2948 addToFunctionSummaryMap(
2949 "pipe", Signature(ArgTypes{IntPtrTy}, RetType{IntTy}),
2951 .Case(ReturnsZero, ErrnoMustNotBeChecked, GenericSuccessMsg)
2952 .Case(ReturnsMinusOne, ErrnoNEZeroIrrelevant, GenericFailureMsg)
2953 .ArgConstraint(NotNull(ArgNo(0))));
2960 addToFunctionSummaryMap(
2961 "lseek", Signature(ArgTypes{IntTy, Off_tTy, IntTy}, RetType{Off_tTy}),
2963 .Case(ReturnsNonnegative, ErrnoIrrelevant)
2964 .Case(ReturnsMinusOne, ErrnoNEZeroIrrelevant, GenericFailureMsg)
2966 ArgumentCondition(0, WithinRange,
Range(0, IntMax))));
2970 addToFunctionSummaryMap(
2972 Signature(ArgTypes{ConstCharPtrTy, CharPtrTy, SizeTyCanonTy},
2973 RetType{Ssize_tTy}),
2975 .Case({ArgumentCondition(2, WithinRange,
Range(1, IntMax)),
2976 ReturnValueCondition(LessThanOrEq, ArgNo(2)),
2977 ReturnValueCondition(WithinRange,
Range(1, Ssize_tMax))},
2978 ErrnoMustNotBeChecked, GenericSuccessMsg)
2979 .Case({ArgumentCondition(2, WithinRange, SingleValue(0)),
2980 ReturnValueCondition(WithinRange, SingleValue(0))},
2981 ErrnoMustNotBeChecked,
2982 "Assuming that argument 'bufsize' is 0")
2983 .Case(ReturnsMinusOne, ErrnoNEZeroIrrelevant, GenericFailureMsg)
2984 .ArgConstraint(NotNull(ArgNo(0)))
2985 .ArgConstraint(NotNull(ArgNo(1)))
2986 .ArgConstraint(BufferSize(ArgNo(1),
2989 ArgumentCondition(2, WithinRange,
Range(0, SizeMax))));
2993 addToFunctionSummaryMap(
2995 Signature(ArgTypes{IntTy, ConstCharPtrTy, CharPtrTy, SizeTyCanonTy},
2996 RetType{Ssize_tTy}),
2998 .Case({ArgumentCondition(3, WithinRange,
Range(1, IntMax)),
2999 ReturnValueCondition(LessThanOrEq, ArgNo(3)),
3000 ReturnValueCondition(WithinRange,
Range(1, Ssize_tMax))},
3001 ErrnoMustNotBeChecked, GenericSuccessMsg)
3002 .Case({ArgumentCondition(3, WithinRange, SingleValue(0)),
3003 ReturnValueCondition(WithinRange, SingleValue(0))},
3004 ErrnoMustNotBeChecked,
3005 "Assuming that argument 'bufsize' is 0")
3006 .Case(ReturnsMinusOne, ErrnoNEZeroIrrelevant, GenericFailureMsg)
3007 .ArgConstraint(ValidFileDescriptorOrAtFdcwd(ArgNo(0)))
3008 .ArgConstraint(NotNull(ArgNo(1)))
3009 .ArgConstraint(NotNull(ArgNo(2)))
3010 .ArgConstraint(BufferSize(ArgNo(2),
3013 ArgumentCondition(3, WithinRange,
Range(0, SizeMax))));
3017 addToFunctionSummaryMap(
3019 Signature(ArgTypes{IntTy, ConstCharPtrTy, IntTy, ConstCharPtrTy},
3022 .Case(ReturnsZero, ErrnoMustNotBeChecked, GenericSuccessMsg)
3023 .Case(ReturnsMinusOne, ErrnoNEZeroIrrelevant, GenericFailureMsg)
3024 .ArgConstraint(ValidFileDescriptorOrAtFdcwd(ArgNo(0)))
3025 .ArgConstraint(NotNull(ArgNo(1)))
3026 .ArgConstraint(ValidFileDescriptorOrAtFdcwd(ArgNo(2)))
3027 .ArgConstraint(NotNull(ArgNo(3))));
3033 addToFunctionSummaryMap(
3035 Signature(ArgTypes{ConstCharPtrTy, CharPtrTy}, RetType{CharPtrTy}),
3037 .Case({NotNull(Ret)}, ErrnoMustNotBeChecked, GenericSuccessMsg)
3038 .Case({
IsNull(Ret)}, ErrnoNEZeroIrrelevant, GenericFailureMsg)
3039 .ArgConstraint(NotNull(ArgNo(0))));
3041 QualType CharPtrConstPtr = getPointerTy(getConstTy(CharPtrTy));
3044 addToFunctionSummaryMap(
3046 Signature(ArgTypes{ConstCharPtrTy, CharPtrConstPtr}, RetType{IntTy}),
3048 .Case(ReturnsMinusOne, ErrnoNEZeroIrrelevant)
3049 .ArgConstraint(NotNull(ArgNo(0))));
3052 addToFunctionSummaryMap(
3054 Signature(ArgTypes{ConstCharPtrTy, CharPtrConstPtr}, RetType{IntTy}),
3056 .Case(ReturnsMinusOne, ErrnoNEZeroIrrelevant)
3057 .ArgConstraint(NotNull(ArgNo(0))));
3060 addToFunctionSummaryMap(
3062 Signature(ArgTypes{IntTy, CharPtrConstPtr, ConstCharPtrTy},
3065 .Case({ReturnValueCondition(WithinRange,
Range(-1, UCharRangeMax))},
3067 .ArgConstraint(ArgumentCondition(0, WithinRange,
Range(0, IntMax)))
3068 .ArgConstraint(NotNull(ArgNo(1)))
3069 .ArgConstraint(NotNull(ArgNo(2))));
3071 std::optional<QualType> StructSockaddrTy = lookupTy(
"sockaddr");
3072 std::optional<QualType> StructSockaddrPtrTy =
3073 getPointerTy(StructSockaddrTy);
3074 std::optional<QualType> ConstStructSockaddrPtrTy =
3075 getPointerTy(getConstTy(StructSockaddrTy));
3076 std::optional<QualType> Socklen_tTy = lookupTy(
"socklen_t");
3077 std::optional<QualType> Socklen_tPtrTy = getPointerTy(Socklen_tTy);
3078 std::optional<RangeInt> Socklen_tMax = getMaxValue(Socklen_tTy);
3088 addToFunctionSummaryMap(
3089 "socket", Signature(ArgTypes{IntTy, IntTy, IntTy}, RetType{IntTy}),
3091 .Case(ReturnsValidFileDescriptor, ErrnoMustNotBeChecked,
3093 .Case(ReturnsMinusOne, ErrnoNEZeroIrrelevant, GenericFailureMsg));
3097 .Case(ReturnsValidFileDescriptor, ErrnoMustNotBeChecked,
3099 .Case(ReturnsMinusOne, ErrnoNEZeroIrrelevant, GenericFailureMsg)
3100 .ArgConstraint(ArgumentCondition(0, WithinRange,
Range(0, IntMax)));
3101 if (!addToFunctionSummaryMap(
3105 Signature(ArgTypes{IntTy, StructSockaddrPtrTy, Socklen_tPtrTy},
3108 addToFunctionSummaryMap(
3110 Signature(ArgTypes{IntTy,
Irrelevant, Socklen_tPtrTy},
3116 if (!addToFunctionSummaryMap(
3118 Signature(ArgTypes{IntTy, ConstStructSockaddrPtrTy, Socklen_tTy},
3121 .Case(ReturnsZero, ErrnoMustNotBeChecked, GenericSuccessMsg)
3122 .Case(ReturnsMinusOne, ErrnoNEZeroIrrelevant, GenericFailureMsg)
3124 ArgumentCondition(0, WithinRange,
Range(0, IntMax)))
3125 .ArgConstraint(NotNull(ArgNo(1)))
3127 BufferSize(ArgNo(1), ArgNo(2)))
3129 ArgumentCondition(2, WithinRange,
Range(0, Socklen_tMax)))))
3131 addToFunctionSummaryMap(
3133 Signature(ArgTypes{IntTy,
Irrelevant, Socklen_tTy}, RetType{IntTy}),
3135 .Case(ReturnsZero, ErrnoMustNotBeChecked, GenericSuccessMsg)
3136 .Case(ReturnsMinusOne, ErrnoNEZeroIrrelevant, GenericFailureMsg)
3138 ArgumentCondition(0, WithinRange,
Range(0, IntMax)))
3140 ArgumentCondition(2, WithinRange,
Range(0, Socklen_tMax))));
3144 if (!addToFunctionSummaryMap(
3146 Signature(ArgTypes{IntTy, StructSockaddrPtrTy, Socklen_tPtrTy},
3149 .Case(ReturnsZero, ErrnoMustNotBeChecked, GenericSuccessMsg)
3150 .Case(ReturnsMinusOne, ErrnoNEZeroIrrelevant, GenericFailureMsg)
3152 ArgumentCondition(0, WithinRange,
Range(0, IntMax)))
3153 .ArgConstraint(NotNull(ArgNo(1)))
3154 .ArgConstraint(NotNull(ArgNo(2)))))
3155 addToFunctionSummaryMap(
3157 Signature(ArgTypes{IntTy,
Irrelevant, Socklen_tPtrTy},
3160 .Case(ReturnsZero, ErrnoMustNotBeChecked, GenericSuccessMsg)
3161 .Case(ReturnsMinusOne, ErrnoNEZeroIrrelevant, GenericFailureMsg)
3163 ArgumentCondition(0, WithinRange,
Range(0, IntMax))));
3167 if (!addToFunctionSummaryMap(
3169 Signature(ArgTypes{IntTy, StructSockaddrPtrTy, Socklen_tPtrTy},
3172 .Case(ReturnsZero, ErrnoMustNotBeChecked, GenericSuccessMsg)
3173 .Case(ReturnsMinusOne, ErrnoNEZeroIrrelevant, GenericFailureMsg)
3175 ArgumentCondition(0, WithinRange,
Range(0, IntMax)))
3176 .ArgConstraint(NotNull(ArgNo(1)))
3177 .ArgConstraint(NotNull(ArgNo(2)))))
3178 addToFunctionSummaryMap(
3180 Signature(ArgTypes{IntTy,
Irrelevant, Socklen_tPtrTy},
3183 .Case(ReturnsZero, ErrnoMustNotBeChecked, GenericSuccessMsg)
3184 .Case(ReturnsMinusOne, ErrnoNEZeroIrrelevant, GenericFailureMsg)
3186 ArgumentCondition(0, WithinRange,
Range(0, IntMax))));
3190 if (!addToFunctionSummaryMap(
3192 Signature(ArgTypes{IntTy, ConstStructSockaddrPtrTy, Socklen_tTy},
3195 .Case(ReturnsZero, ErrnoMustNotBeChecked, GenericSuccessMsg)
3196 .Case(ReturnsMinusOne, ErrnoNEZeroIrrelevant, GenericFailureMsg)
3198 ArgumentCondition(0, WithinRange,
Range(0, IntMax)))
3199 .ArgConstraint(NotNull(ArgNo(1)))))
3200 addToFunctionSummaryMap(
3202 Signature(ArgTypes{IntTy,
Irrelevant, Socklen_tTy}, RetType{IntTy}),
3204 .Case(ReturnsZero, ErrnoMustNotBeChecked, GenericSuccessMsg)
3205 .Case(ReturnsMinusOne, ErrnoNEZeroIrrelevant, GenericFailureMsg)
3207 ArgumentCondition(0, WithinRange,
Range(0, IntMax))));
3211 .Case({ReturnValueCondition(LessThanOrEq, ArgNo(2)),
3212 ReturnValueCondition(WithinRange,
Range(1, Ssize_tMax))},
3213 ErrnoMustNotBeChecked, GenericSuccessMsg)
3214 .Case({ReturnValueCondition(WithinRange, SingleValue(0)),
3215 ArgumentCondition(2, WithinRange, SingleValue(0))},
3216 ErrnoMustNotBeChecked, GenericSuccessMsg)
3217 .Case(ReturnsMinusOne, ErrnoNEZeroIrrelevant, GenericFailureMsg)
3218 .ArgConstraint(ArgumentCondition(0, WithinRange,
Range(0, IntMax)))
3219 .ArgConstraint(BufferSize(ArgNo(1),
3221 if (!addToFunctionSummaryMap(
3227 Signature(ArgTypes{IntTy, VoidPtrTy, SizeTyCanonTy, IntTy,
3228 StructSockaddrPtrTy, Socklen_tPtrTy},
3229 RetType{Ssize_tTy}),
3231 addToFunctionSummaryMap(
3233 Signature(ArgTypes{IntTy, VoidPtrTy, SizeTyCanonTy, IntTy,
Irrelevant,
3235 RetType{Ssize_tTy}),
3240 .Case({ReturnValueCondition(LessThanOrEq, ArgNo(2)),
3241 ReturnValueCondition(WithinRange,
Range(1, Ssize_tMax))},
3242 ErrnoMustNotBeChecked, GenericSuccessMsg)
3243 .Case({ReturnValueCondition(WithinRange, SingleValue(0)),
3244 ArgumentCondition(2, WithinRange, SingleValue(0))},
3245 ErrnoMustNotBeChecked, GenericSuccessMsg)
3246 .Case(ReturnsMinusOne, ErrnoNEZeroIrrelevant, GenericFailureMsg)
3247 .ArgConstraint(ArgumentCondition(0, WithinRange,
Range(0, IntMax)))
3248 .ArgConstraint(BufferSize(ArgNo(1),
3250 if (!addToFunctionSummaryMap(
3255 Signature(ArgTypes{IntTy, ConstVoidPtrTy, SizeTyCanonTy, IntTy,
3256 ConstStructSockaddrPtrTy, Socklen_tTy},
3257 RetType{Ssize_tTy}),
3259 addToFunctionSummaryMap(
3261 Signature(ArgTypes{IntTy, ConstVoidPtrTy, SizeTyCanonTy, IntTy,
3263 RetType{Ssize_tTy}),
3267 addToFunctionSummaryMap(
3268 "listen", Signature(ArgTypes{IntTy, IntTy}, RetType{IntTy}),
3270 .Case(ReturnsZero, ErrnoMustNotBeChecked, GenericSuccessMsg)
3271 .Case(ReturnsMinusOne, ErrnoNEZeroIrrelevant, GenericFailureMsg)
3273 ArgumentCondition(0, WithinRange,
Range(0, IntMax))));
3276 addToFunctionSummaryMap(
3278 Signature(ArgTypes{IntTy, VoidPtrTy, SizeTyCanonTy, IntTy},
3279 RetType{Ssize_tTy}),
3281 .Case({ReturnValueCondition(LessThanOrEq, ArgNo(2)),
3282 ReturnValueCondition(WithinRange,
Range(1, Ssize_tMax))},
3283 ErrnoMustNotBeChecked, GenericSuccessMsg)
3284 .Case({ReturnValueCondition(WithinRange, SingleValue(0)),
3285 ArgumentCondition(2, WithinRange, SingleValue(0))},
3286 ErrnoMustNotBeChecked, GenericSuccessMsg)
3287 .Case(ReturnsMinusOne, ErrnoNEZeroIrrelevant, GenericFailureMsg)
3288 .ArgConstraint(ArgumentCondition(0, WithinRange,
Range(0, IntMax)))
3289 .ArgConstraint(BufferSize(ArgNo(1),
3292 std::optional<QualType> StructMsghdrTy = lookupTy(
"msghdr");
3293 std::optional<QualType> StructMsghdrPtrTy = getPointerTy(StructMsghdrTy);
3294 std::optional<QualType> ConstStructMsghdrPtrTy =
3295 getPointerTy(getConstTy(StructMsghdrTy));
3298 addToFunctionSummaryMap(
3300 Signature(ArgTypes{IntTy, StructMsghdrPtrTy, IntTy},
3301 RetType{Ssize_tTy}),
3303 .Case({ReturnValueCondition(WithinRange,
Range(1, Ssize_tMax))},
3304 ErrnoMustNotBeChecked, GenericSuccessMsg)
3305 .Case(ReturnsMinusOne, ErrnoNEZeroIrrelevant, GenericFailureMsg)
3307 ArgumentCondition(0, WithinRange,
Range(0, IntMax))));
3310 addToFunctionSummaryMap(
3312 Signature(ArgTypes{IntTy, ConstStructMsghdrPtrTy, IntTy},
3313 RetType{Ssize_tTy}),
3315 .Case({ReturnValueCondition(WithinRange,
Range(1, Ssize_tMax))},
3316 ErrnoMustNotBeChecked, GenericSuccessMsg)
3317 .Case(ReturnsMinusOne, ErrnoNEZeroIrrelevant, GenericFailureMsg)
3319 ArgumentCondition(0, WithinRange,
Range(0, IntMax))));
3323 addToFunctionSummaryMap(
3325 Signature(ArgTypes{IntTy, IntTy, IntTy, ConstVoidPtrTy, Socklen_tTy},
3328 .Case(ReturnsZero, ErrnoMustNotBeChecked, GenericSuccessMsg)
3329 .Case(ReturnsMinusOne, ErrnoNEZeroIrrelevant, GenericFailureMsg)
3330 .ArgConstraint(NotNullBuffer(ArgNo(3), ArgNo(4)))
3332 BufferSize(ArgNo(3), ArgNo(4)))
3334 ArgumentCondition(4, WithinRange,
Range(0, Socklen_tMax))));
3339 addToFunctionSummaryMap(
3341 Signature(ArgTypes{IntTy, IntTy, IntTy, VoidPtrTy, Socklen_tPtrTy},
3344 .Case(ReturnsZero, ErrnoMustNotBeChecked, GenericSuccessMsg)
3345 .Case(ReturnsMinusOne, ErrnoNEZeroIrrelevant, GenericFailureMsg)
3346 .ArgConstraint(NotNull(ArgNo(3)))
3347 .ArgConstraint(NotNull(ArgNo(4))));
3350 addToFunctionSummaryMap(
3352 Signature(ArgTypes{IntTy, ConstVoidPtrTy, SizeTyCanonTy, IntTy},
3353 RetType{Ssize_tTy}),
3355 .Case({ReturnValueCondition(LessThanOrEq, ArgNo(2)),
3356 ReturnValueCondition(WithinRange,
Range(1, Ssize_tMax))},
3357 ErrnoMustNotBeChecked, GenericSuccessMsg)
3358 .Case({ReturnValueCondition(WithinRange, SingleValue(0)),
3359 ArgumentCondition(2, WithinRange, SingleValue(0))},
3360 ErrnoMustNotBeChecked, GenericSuccessMsg)
3361 .Case(ReturnsMinusOne, ErrnoNEZeroIrrelevant, GenericFailureMsg)
3362 .ArgConstraint(ArgumentCondition(0, WithinRange,
Range(0, IntMax)))
3363 .ArgConstraint(BufferSize(ArgNo(1),
3367 addToFunctionSummaryMap(
3369 Signature(ArgTypes{IntTy, IntTy, IntTy, IntPtrTy}, RetType{IntTy}),
3371 .Case(ReturnsZero, ErrnoMustNotBeChecked, GenericSuccessMsg)
3372 .Case(ReturnsMinusOne, ErrnoNEZeroIrrelevant, GenericFailureMsg)
3373 .ArgConstraint(NotNull(ArgNo(3))));
3376 addToFunctionSummaryMap(
3377 "shutdown", Signature(ArgTypes{IntTy, IntTy}, RetType{IntTy}),
3379 .Case(ReturnsZero, ErrnoMustNotBeChecked, GenericSuccessMsg)
3380 .Case(ReturnsMinusOne, ErrnoNEZeroIrrelevant, GenericFailureMsg)
3382 ArgumentCondition(0, WithinRange,
Range(0, IntMax))));
3391 addToFunctionSummaryMap(
3393 Signature(ArgTypes{ConstStructSockaddrPtrTy, Socklen_tTy, CharPtrTy,
3394 Socklen_tTy, CharPtrTy, Socklen_tTy, IntTy},
3398 BufferSize(ArgNo(0), ArgNo(1)))
3400 ArgumentCondition(1, WithinRange,
Range(0, Socklen_tMax)))
3402 BufferSize(ArgNo(2), ArgNo(3)))
3404 ArgumentCondition(3, WithinRange,
Range(0, Socklen_tMax)))
3406 BufferSize(ArgNo(4), ArgNo(5)))
3408 ArgumentCondition(5, WithinRange,
Range(0, Socklen_tMax))));
3410 std::optional<QualType> StructUtimbufTy = lookupTy(
"utimbuf");
3411 std::optional<QualType> StructUtimbufPtrTy = getPointerTy(StructUtimbufTy);
3414 addToFunctionSummaryMap(
3416 Signature(ArgTypes{ConstCharPtrTy, StructUtimbufPtrTy}, RetType{IntTy}),
3418 .Case(ReturnsZero, ErrnoMustNotBeChecked, GenericSuccessMsg)
3419 .Case(ReturnsMinusOne, ErrnoNEZeroIrrelevant, GenericFailureMsg)
3420 .ArgConstraint(NotNull(ArgNo(0))));
3422 std::optional<QualType> StructTimespecTy = lookupTy(
"timespec");
3423 std::optional<QualType> StructTimespecPtrTy =
3424 getPointerTy(StructTimespecTy);
3425 std::optional<QualType> ConstStructTimespecPtrTy =
3426 getPointerTy(getConstTy(StructTimespecTy));
3429 addToFunctionSummaryMap(
3431 Signature(ArgTypes{IntTy, ConstStructTimespecPtrTy}, RetType{IntTy}),
3433 .Case(ReturnsZero, ErrnoMustNotBeChecked, GenericSuccessMsg)
3434 .Case(ReturnsMinusOne, ErrnoNEZeroIrrelevant, GenericFailureMsg)
3436 ArgumentCondition(0, WithinRange,
Range(0, IntMax))));
3440 addToFunctionSummaryMap(
3443 ArgTypes{IntTy, ConstCharPtrTy, ConstStructTimespecPtrTy, IntTy},
3446 .Case(ReturnsZero, ErrnoMustNotBeChecked, GenericSuccessMsg)
3447 .Case(ReturnsMinusOne, ErrnoNEZeroIrrelevant, GenericFailureMsg)
3448 .ArgConstraint(NotNull(ArgNo(1))));
3450 std::optional<QualType> StructTimevalTy = lookupTy(
"timeval");
3451 std::optional<QualType> ConstStructTimevalPtrTy =
3452 getPointerTy(getConstTy(StructTimevalTy));
3455 addToFunctionSummaryMap(
3457 Signature(ArgTypes{ConstCharPtrTy, ConstStructTimevalPtrTy},
3460 .Case(ReturnsZero, ErrnoMustNotBeChecked, GenericSuccessMsg)
3461 .Case(ReturnsMinusOne, ErrnoNEZeroIrrelevant, GenericFailureMsg)
3462 .ArgConstraint(NotNull(ArgNo(0))));
3465 addToFunctionSummaryMap(
3467 Signature(ArgTypes{ConstStructTimespecPtrTy, StructTimespecPtrTy},
3470 .Case(ReturnsZero, ErrnoMustNotBeChecked, GenericSuccessMsg)
3471 .Case(ReturnsMinusOne, ErrnoNEZeroIrrelevant, GenericFailureMsg)
3472 .ArgConstraint(NotNull(ArgNo(0))));
3474 std::optional<QualType> Time_tTy = lookupTy(
"time_t");
3475 std::optional<QualType> ConstTime_tPtrTy =
3476 getPointerTy(getConstTy(Time_tTy));
3478 std::optional<QualType> StructTmTy = lookupTy(
"tm");
3479 std::optional<QualType> StructTmPtrTy = getPointerTy(StructTmTy);
3480 std::optional<QualType> ConstStructTmPtrTy =
3481 getPointerTy(getConstTy(StructTmTy));
3484 addToFunctionSummaryMap(
3486 Signature(ArgTypes{ConstTime_tPtrTy}, RetType{StructTmPtrTy}),
3487 Summary(NoEvalCall).ArgConstraint(NotNull(ArgNo(0))));
3491 addToFunctionSummaryMap(
"localtime_r",
3492 Signature(ArgTypes{ConstTime_tPtrTy, StructTmPtrTy},
3493 RetType{StructTmPtrTy}),
3495 .ArgConstraint(NotNull(ArgNo(0)))
3496 .ArgConstraint(NotNull(ArgNo(1))));
3499 addToFunctionSummaryMap(
3501 Signature(ArgTypes{ConstStructTmPtrTy, CharPtrTy}, RetType{CharPtrTy}),
3503 .ArgConstraint(NotNull(ArgNo(0)))
3504 .ArgConstraint(NotNull(ArgNo(1)))
3505 .ArgConstraint(BufferSize(ArgNo(1),
3506 BVF.getValue(26, IntTy))));
3509 addToFunctionSummaryMap(
3511 Signature(ArgTypes{ConstTime_tPtrTy, CharPtrTy}, RetType{CharPtrTy}),
3513 .ArgConstraint(NotNull(ArgNo(0)))
3514 .ArgConstraint(NotNull(ArgNo(1)))
3515 .ArgConstraint(BufferSize(
3517 BVF.getValue(26, IntTy))));
3521 addToFunctionSummaryMap(
"gmtime_r",
3522 Signature(ArgTypes{ConstTime_tPtrTy, StructTmPtrTy},
3523 RetType{StructTmPtrTy}),
3525 .ArgConstraint(NotNull(ArgNo(0)))
3526 .ArgConstraint(NotNull(ArgNo(1))));
3529 addToFunctionSummaryMap(
3530 "gmtime", Signature(ArgTypes{ConstTime_tPtrTy}, RetType{StructTmPtrTy}),
3531 Summary(NoEvalCall).ArgConstraint(NotNull(ArgNo(0))));
3533 std::optional<QualType> Clockid_tTy = lookupTy(
"clockid_t");
3536 addToFunctionSummaryMap(
3538 Signature(ArgTypes{Clockid_tTy, StructTimespecPtrTy}, RetType{IntTy}),
3540 .Case(ReturnsZero, ErrnoMustNotBeChecked, GenericSuccessMsg)
3541 .Case(ReturnsMinusOne, ErrnoNEZeroIrrelevant, GenericFailureMsg)
3542 .ArgConstraint(NotNull(ArgNo(1))));
3544 std::optional<QualType> StructItimervalTy = lookupTy(
"itimerval");
3545 std::optional<QualType> StructItimervalPtrTy =
3546 getPointerTy(StructItimervalTy);
3549 addToFunctionSummaryMap(
3551 Signature(ArgTypes{IntTy, StructItimervalPtrTy}, RetType{IntTy}),
3553 .Case(ReturnsZero, ErrnoMustNotBeChecked, GenericSuccessMsg)
3554 .Case(ReturnsMinusOne, ErrnoNEZeroIrrelevant, GenericFailureMsg)
3555 .ArgConstraint(NotNull(ArgNo(1))));
3557 std::optional<QualType> Pthread_cond_tTy = lookupTy(
"pthread_cond_t");
3558 std::optional<QualType> Pthread_cond_tPtrTy =
3559 getPointerTy(Pthread_cond_tTy);
3560 std::optional<QualType> Pthread_tTy = lookupTy(
"pthread_t");
3561 std::optional<QualType> Pthread_tPtrTy = getPointerTy(Pthread_tTy);
3562 std::optional<QualType> Pthread_mutex_tTy = lookupTy(
"pthread_mutex_t");
3563 std::optional<QualType> Pthread_mutex_tPtrTy =
3564 getPointerTy(Pthread_mutex_tTy);
3565 std::optional<QualType> Pthread_attr_tTy = lookupTy(
"pthread_attr_t");
3566 std::optional<QualType> Pthread_attr_tPtrTy =
3567 getPointerTy(Pthread_attr_tTy);
3568 std::optional<QualType> ConstPthread_attr_tPtrTy =
3569 getPointerTy(getConstTy(Pthread_attr_tTy));
3570 std::optional<QualType> Pthread_mutexattr_tTy =
3571 lookupTy(
"pthread_mutexattr_t");
3572 std::optional<QualType> ConstPthread_mutexattr_tPtrTy =
3573 getPointerTy(getConstTy(Pthread_mutexattr_tTy));
3575 QualType PthreadStartRoutineTy = getPointerTy(
3577 FunctionProtoType::ExtProtoInfo()));
3581 addToFunctionSummaryMap(
3582 {
"pthread_cond_signal",
"pthread_cond_broadcast"},
3583 Signature(ArgTypes{Pthread_cond_tPtrTy}, RetType{IntTy}),
3584 Summary(NoEvalCall).ArgConstraint(NotNull(ArgNo(0))));
3589 addToFunctionSummaryMap(
3591 Signature(ArgTypes{Pthread_tPtrTy, ConstPthread_attr_tPtrTy,
3592 PthreadStartRoutineTy, VoidPtrTy},
3595 .ArgConstraint(NotNull(ArgNo(0)))
3596 .ArgConstraint(NotNull(ArgNo(2))));
3600 addToFunctionSummaryMap(
3601 {
"pthread_attr_destroy",
"pthread_attr_init"},
3602 Signature(ArgTypes{Pthread_attr_tPtrTy}, RetType{IntTy}),
3603 Summary(NoEvalCall).ArgConstraint(NotNull(ArgNo(0))));
3609 addToFunctionSummaryMap(
3610 {
"pthread_attr_getstacksize",
"pthread_attr_getguardsize"},
3611 Signature(ArgTypes{ConstPthread_attr_tPtrTy, SizePtrTy},
3614 .ArgConstraint(NotNull(ArgNo(0)))
3615 .ArgConstraint(NotNull(ArgNo(1))));
3619 addToFunctionSummaryMap(
3620 {
"pthread_attr_setstacksize",
"pthread_attr_setguardsize"},
3621 Signature(ArgTypes{Pthread_attr_tPtrTy, SizeTyCanonTy}, RetType{IntTy}),
3623 .ArgConstraint(NotNull(ArgNo(0)))
3625 ArgumentCondition(1, WithinRange,
Range(0, SizeMax))));
3629 addToFunctionSummaryMap(
3630 "pthread_mutex_init",
3631 Signature(ArgTypes{Pthread_mutex_tPtrTy, ConstPthread_mutexattr_tPtrTy},
3633 Summary(NoEvalCall).ArgConstraint(NotNull(ArgNo(0))));
3639 addToFunctionSummaryMap(
3640 {
"pthread_mutex_destroy",
"pthread_mutex_lock",
"pthread_mutex_trylock",
3641 "pthread_mutex_unlock"},
3642 Signature(ArgTypes{Pthread_mutex_tPtrTy}, RetType{IntTy}),
3643 Summary(NoEvalCall).ArgConstraint(NotNull(ArgNo(0))));
3647 if (AddTestFunctions) {
3648 const RangeInt IntMin = BVF.
getMinValue(IntTy)->getLimitedValue();
3650 addToFunctionSummaryMap(
3651 "__not_null", Signature(ArgTypes{IntPtrTy}, RetType{IntTy}),
3652 Summary(EvalCallAsPure).ArgConstraint(NotNull(ArgNo(0))));
3654 addToFunctionSummaryMap(
3655 "__not_null_buffer",
3656 Signature(ArgTypes{VoidPtrTy, IntTy, IntTy}, RetType{IntTy}),
3657 Summary(EvalCallAsPure)
3658 .ArgConstraint(NotNullBuffer(ArgNo(0), ArgNo(1), ArgNo(2))));
3661 addToFunctionSummaryMap(
3662 "__single_val_0", Signature(ArgTypes{IntTy}, RetType{IntTy}),
3663 Summary(EvalCallAsPure)
3664 .ArgConstraint(ArgumentCondition(0U, WithinRange, SingleValue(0))));
3665 addToFunctionSummaryMap(
3666 "__single_val_1", Signature(ArgTypes{IntTy}, RetType{IntTy}),
3667 Summary(EvalCallAsPure)
3668 .ArgConstraint(ArgumentCondition(0U, WithinRange, SingleValue(1))));
3669 addToFunctionSummaryMap(
3670 "__range_1_2", Signature(ArgTypes{IntTy}, RetType{IntTy}),
3671 Summary(EvalCallAsPure)
3672 .ArgConstraint(ArgumentCondition(0U, WithinRange,
Range(1, 2))));
3673 addToFunctionSummaryMap(
3674 "__range_m1_1", Signature(ArgTypes{IntTy}, RetType{IntTy}),
3675 Summary(EvalCallAsPure)
3676 .ArgConstraint(ArgumentCondition(0U, WithinRange,
Range(-1, 1))));
3677 addToFunctionSummaryMap(
3678 "__range_m2_m1", Signature(ArgTypes{IntTy}, RetType{IntTy}),
3679 Summary(EvalCallAsPure)
3680 .ArgConstraint(ArgumentCondition(0U, WithinRange,
Range(-2, -1))));
3681 addToFunctionSummaryMap(
3682 "__range_m10_10", Signature(ArgTypes{IntTy}, RetType{IntTy}),
3683 Summary(EvalCallAsPure)
3684 .ArgConstraint(ArgumentCondition(0U, WithinRange,
Range(-10, 10))));
3685 addToFunctionSummaryMap(
"__range_m1_inf",
3686 Signature(ArgTypes{IntTy}, RetType{IntTy}),
3687 Summary(EvalCallAsPure)
3688 .ArgConstraint(ArgumentCondition(
3689 0U, WithinRange,
Range(-1, IntMax))));
3690 addToFunctionSummaryMap(
"__range_0_inf",
3691 Signature(ArgTypes{IntTy}, RetType{IntTy}),
3692 Summary(EvalCallAsPure)
3693 .ArgConstraint(ArgumentCondition(
3694 0U, WithinRange,
Range(0, IntMax))));
3695 addToFunctionSummaryMap(
"__range_1_inf",
3696 Signature(ArgTypes{IntTy}, RetType{IntTy}),
3697 Summary(EvalCallAsPure)
3698 .ArgConstraint(ArgumentCondition(
3699 0U, WithinRange,
Range(1, IntMax))));
3700 addToFunctionSummaryMap(
"__range_minf_m1",
3701 Signature(ArgTypes{IntTy}, RetType{IntTy}),
3702 Summary(EvalCallAsPure)
3703 .ArgConstraint(ArgumentCondition(
3704 0U, WithinRange,
Range(IntMin, -1))));
3705 addToFunctionSummaryMap(
"__range_minf_0",
3706 Signature(ArgTypes{IntTy}, RetType{IntTy}),
3707 Summary(EvalCallAsPure)
3708 .ArgConstraint(ArgumentCondition(
3709 0U, WithinRange,
Range(IntMin, 0))));
3710 addToFunctionSummaryMap(
"__range_minf_1",
3711 Signature(ArgTypes{IntTy}, RetType{IntTy}),
3712 Summary(EvalCallAsPure)
3713 .ArgConstraint(ArgumentCondition(
3714 0U, WithinRange,
Range(IntMin, 1))));
3715 addToFunctionSummaryMap(
"__range_1_2__4_6",
3716 Signature(ArgTypes{IntTy}, RetType{IntTy}),
3717 Summary(EvalCallAsPure)
3718 .ArgConstraint(ArgumentCondition(
3719 0U, WithinRange,
Range({1, 2}, {4, 6}))));
3720 addToFunctionSummaryMap(
3721 "__range_1_2__4_inf", Signature(ArgTypes{IntTy}, RetType{IntTy}),
3722 Summary(EvalCallAsPure)
3723 .ArgConstraint(ArgumentCondition(0U, WithinRange,
3724 Range({1, 2}, {4, IntMax}))));
3727 addToFunctionSummaryMap(
3728 "__single_val_out_0", Signature(ArgTypes{IntTy}, RetType{IntTy}),
3729 Summary(EvalCallAsPure)
3730 .ArgConstraint(ArgumentCondition(0U, OutOfRange, SingleValue(0))));
3731 addToFunctionSummaryMap(
3732 "__single_val_out_1", Signature(ArgTypes{IntTy}, RetType{IntTy}),
3733 Summary(EvalCallAsPure)
3734 .ArgConstraint(ArgumentCondition(0U, OutOfRange, SingleValue(1))));
3735 addToFunctionSummaryMap(
3736 "__range_out_1_2", Signature(ArgTypes{IntTy}, RetType{IntTy}),
3737 Summary(EvalCallAsPure)
3738 .ArgConstraint(ArgumentCondition(0U, OutOfRange,
Range(1, 2))));
3739 addToFunctionSummaryMap(
3740 "__range_out_m1_1", Signature(ArgTypes{IntTy}, RetType{IntTy}),
3741 Summary(EvalCallAsPure)
3742 .ArgConstraint(ArgumentCondition(0U, OutOfRange,
Range(-1, 1))));
3743 addToFunctionSummaryMap(
3744 "__range_out_m2_m1", Signature(ArgTypes{IntTy}, RetType{IntTy}),
3745 Summary(EvalCallAsPure)
3746 .ArgConstraint(ArgumentCondition(0U, OutOfRange,
Range(-2, -1))));
3747 addToFunctionSummaryMap(
3748 "__range_out_m10_10", Signature(ArgTypes{IntTy}, RetType{IntTy}),
3749 Summary(EvalCallAsPure)
3750 .ArgConstraint(ArgumentCondition(0U, OutOfRange,
Range(-10, 10))));
3751 addToFunctionSummaryMap(
"__range_out_m1_inf",
3752 Signature(ArgTypes{IntTy}, RetType{IntTy}),
3753 Summary(EvalCallAsPure)
3754 .ArgConstraint(ArgumentCondition(
3755 0U, OutOfRange,
Range(-1, IntMax))));
3756 addToFunctionSummaryMap(
"__range_out_0_inf",
3757 Signature(ArgTypes{IntTy}, RetType{IntTy}),
3758 Summary(EvalCallAsPure)
3759 .ArgConstraint(ArgumentCondition(
3760 0U, OutOfRange,
Range(0, IntMax))));
3761 addToFunctionSummaryMap(
"__range_out_1_inf",
3762 Signature(ArgTypes{IntTy}, RetType{IntTy}),
3763 Summary(EvalCallAsPure)
3764 .ArgConstraint(ArgumentCondition(
3765 0U, OutOfRange,
Range(1, IntMax))));
3766 addToFunctionSummaryMap(
"__range_out_minf_m1",
3767 Signature(ArgTypes{IntTy}, RetType{IntTy}),
3768 Summary(EvalCallAsPure)
3769 .ArgConstraint(ArgumentCondition(
3770 0U, OutOfRange,
Range(IntMin, -1))));
3771 addToFunctionSummaryMap(
"__range_out_minf_0",
3772 Signature(ArgTypes{IntTy}, RetType{IntTy}),
3773 Summary(EvalCallAsPure)
3774 .ArgConstraint(ArgumentCondition(
3775 0U, OutOfRange,
Range(IntMin, 0))));
3776 addToFunctionSummaryMap(
"__range_out_minf_1",
3777 Signature(ArgTypes{IntTy}, RetType{IntTy}),
3778 Summary(EvalCallAsPure)
3779 .ArgConstraint(ArgumentCondition(
3780 0U, OutOfRange,
Range(IntMin, 1))));
3781 addToFunctionSummaryMap(
"__range_out_1_2__4_6",
3782 Signature(ArgTypes{IntTy}, RetType{IntTy}),
3783 Summary(EvalCallAsPure)
3784 .ArgConstraint(ArgumentCondition(
3785 0U, OutOfRange,
Range({1, 2}, {4, 6}))));
3786 addToFunctionSummaryMap(
3787 "__range_out_1_2__4_inf", Signature(ArgTypes{IntTy}, RetType{IntTy}),
3788 Summary(EvalCallAsPure)
3790 ArgumentCondition(0U, OutOfRange,
Range({1, 2}, {4, IntMax}))));
3793 addToFunctionSummaryMap(
3794 "__within", Signature(ArgTypes{IntTy}, RetType{IntTy}),
3795 Summary(EvalCallAsPure)
3796 .ArgConstraint(ArgumentCondition(0U, WithinRange, SingleValue(1))));
3797 addToFunctionSummaryMap(
3798 "__out_of", Signature(ArgTypes{IntTy}, RetType{IntTy}),
3799 Summary(EvalCallAsPure)
3800 .ArgConstraint(ArgumentCondition(0U, OutOfRange, SingleValue(1))));
3802 addToFunctionSummaryMap(
3803 "__two_constrained_args",
3804 Signature(ArgTypes{IntTy, IntTy}, RetType{IntTy}),
3805 Summary(EvalCallAsPure)
3806 .ArgConstraint(ArgumentCondition(0U, WithinRange, SingleValue(1)))
3807 .ArgConstraint(ArgumentCondition(1U, WithinRange, SingleValue(1))));
3808 addToFunctionSummaryMap(
3809 "__arg_constrained_twice", Signature(ArgTypes{IntTy}, RetType{IntTy}),
3810 Summary(EvalCallAsPure)
3811 .ArgConstraint(ArgumentCondition(0U, OutOfRange, SingleValue(1)))
3812 .ArgConstraint(ArgumentCondition(0U, OutOfRange, SingleValue(2))));
3813 addToFunctionSummaryMap(
3815 Signature(ArgTypes{
Irrelevant, IntTy}, RetType{IntTy}),
3816 Summary(EvalCallAsPure).ArgConstraint(NotNull(ArgNo(0))));
3817 addToFunctionSummaryMap(
3819 Signature(ArgTypes{VoidPtrTy, ConstCharPtrTy}, RetType{IntTy}),
3820 Summary(EvalCallAsPure)
3821 .ArgConstraint(NotNull(ArgNo(0)))
3822 .ArgConstraint(NotNull(ArgNo(1))));
3823 addToFunctionSummaryMap(
3824 "__buf_size_arg_constraint",
3825 Signature(ArgTypes{ConstVoidPtrTy, SizeTyCanonTy}, RetType{IntTy}),
3826 Summary(EvalCallAsPure)
3828 BufferSize(ArgNo(0), ArgNo(1))));
3829 addToFunctionSummaryMap(
3830 "__buf_size_arg_constraint_mul",
3831 Signature(ArgTypes{ConstVoidPtrTy, SizeTyCanonTy, SizeTyCanonTy},
3833 Summary(EvalCallAsPure)
3834 .ArgConstraint(BufferSize(ArgNo(0), ArgNo(1),
3836 addToFunctionSummaryMap(
3837 "__buf_size_arg_constraint_concrete",
3838 Signature(ArgTypes{ConstVoidPtrTy}, RetType{IntTy}),
3839 Summary(EvalCallAsPure)
3840 .ArgConstraint(BufferSize(ArgNo(0),
3841 BVF.getValue(10, IntTy))));
3842 addToFunctionSummaryMap({
"__test_restrict_param_0",
3843 "__test_restrict_param_1",
3844 "__test_restrict_param_2"},
3845 Signature(ArgTypes{VoidPtrTy}, RetType{VoidTy}),
3846 Summary(EvalCallAsPure));
3849 addToFunctionSummaryMap(
3850 "__test_case_note", Signature(ArgTypes{}, RetType{IntTy}),
3851 Summary(EvalCallAsPure)
3852 .Case({ReturnValueCondition(WithinRange, SingleValue(0))},
3853 ErrnoIrrelevant,
"Function returns 0")
3854 .Case({ReturnValueCondition(WithinRange, SingleValue(1))},
3855 ErrnoIrrelevant,
"Function returns 1"));
3856 addToFunctionSummaryMap(
3857 "__test_case_range_1_2__4_6",
3858 Signature(ArgTypes{IntTy}, RetType{IntTy}),
3859 Summary(EvalCallAsPure)
3860 .Case({ArgumentCondition(0U, WithinRange,
3861 IntRangeVector{{IntMin, 0}, {3, 3}}),
3862 ReturnValueCondition(WithinRange, SingleValue(1))},
3864 .Case({ArgumentCondition(0U, WithinRange,
3865 IntRangeVector{{3, 3}, {7, IntMax}}),
3866 ReturnValueCondition(WithinRange, SingleValue(2))},
3868 .Case({ArgumentCondition(0U, WithinRange,
3869 IntRangeVector{{IntMin, 0}, {7, IntMax}}),
3870 ReturnValueCondition(WithinRange, SingleValue(3))},
3872 .Case({ArgumentCondition(
3874 IntRangeVector{{IntMin, 0}, {3, 3}, {7, IntMax}}),
3875 ReturnValueCondition(WithinRange, SingleValue(4))},
3880void ento::registerStdCLibraryFunctionsChecker(CheckerManager &mgr) {
3884 Checker->DisplayLoadedSummaries =
3885 Opts.getCheckerBooleanOption(Checker,
"DisplayLoadedSummaries");
3886 Checker->ModelPOSIX = Opts.getCheckerBooleanOption(Checker,
"ModelPOSIX");
3887 Checker->ShouldAssumeControlledEnvironment =
3888 Opts.ShouldAssumeControlledEnvironment;
3891bool ento::shouldRegisterStdCLibraryFunctionsChecker(
3892 const CheckerManager &mgr) {
3896void ento::registerStdCLibraryFunctionsTesterChecker(CheckerManager &mgr) {
3897 auto *Checker = mgr.
getChecker<StdLibraryFunctionsChecker>();
3898 Checker->AddTestFunctions =
true;
3901bool ento::shouldRegisterStdCLibraryFunctionsTesterChecker(
3902 const CheckerManager &mgr) {
static std::string getFunctionName(const CallEvent &Call)
static bool isInvalid(LocType Loc, bool *Invalid)
TranslationUnitDecl * getTranslationUnitDecl() const
QualType getPointerType(QualType T) const
Return the uniqued reference to the type for a pointer to the specified type.
CanQualType getCanonicalSizeType() const
CanQualType getCanonicalTypeDeclType(const TypeDecl *TD) const
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.
const TargetInfo & getTargetInfo() const
static Opcode negateComparisonOp(Opcode Opc)
BinaryOperatorKind Opcode
lookup_result lookup(DeclarationName Name) const
lookup - Find the declarations (if any) with the given Name in this context.
ASTContext & getASTContext() const LLVM_READONLY
void print(raw_ostream &Out, unsigned Indentation=0, bool PrintInstantiation=false) const
const ParmVarDecl * getParamDecl(unsigned i) const
QualType getReturnType() const
FunctionDecl * getCanonicalDecl() override
Retrieves the "canonical" declaration of the given declaration.
unsigned getNumParams() const
Return the number of parameters this function must have based on its FunctionType.
size_t param_size() const
IdentifierInfo & get(StringRef Name)
Return the identifier token info for the specified named identifier.
QualType withConst() const
QualType getCanonicalType() const
const llvm::Triple & getTriple() const
Returns the target triple of the primary target.
APSIntPtr getMaxValue(const llvm::APSInt &v)
APSIntPtr getMinValue(const llvm::APSInt &v)
ASTContext & getContext() const
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.
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
void markNotInteresting(SymbolRef sym)
bool isInteresting(SymbolRef sym) const
SValBuilder & getSValBuilder()
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.
T castAs() const
Convert to the specified SVal type, asserting that this SVal is of the desired type.
Defines the clang::TargetInfo interface.
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.
PRESERVE_NONE bool Ret(InterpState &S)
bool IsNonNull(InterpState &S)
bool matches(const til::SExpr *E1, const til::SExpr *E2)
Top level wrappers for InstallAPI frontend operations.
bool isa(CodeGen::Address addr)
@ Result
The result type of a method or function.
const FunctionProtoType * T
U cast(CodeGen::Address addr)
int const char * function