aboutsummaryrefslogtreecommitdiff
path: root/clang/lib/StaticAnalyzer/Checkers/StdLibraryFunctionsChecker.cpp
AgeCommit message (Collapse)AuthorFilesLines
9 daysReland [Clang] Make the SizeType, SignedSizeType and PtrdiffType be named ↵YexuanXiao1-36/+44
sugar types (#149613) The checks for the 'z' and 't' format specifiers added in the original PR #143653 had some issues and were overly strict, causing some build failures and were consequently reverted at https://github.com/llvm/llvm-project/commit/4c85bf2fe8042c855c9dd5be4b02191e9d071ffd. In the latest commit https://github.com/llvm/llvm-project/pull/149613/commits/27c58629ec76a703fde9c0b99b170573170b4a7a, I relaxed the checks for the 'z' and 't' format specifiers, so warnings are now only issued when they are used with mismatched types. The original intent of these checks was to diagnose code that assumes the underlying type of `size_t` is `unsigned` or `unsigned long`, for example: ```c printf("%zu", 1ul); // Not portable, but not an error when size_t is unsigned long ``` However, it produced a significant number of false positives. This was partly because Clang does not treat the `typedef` `size_t` and `__size_t` as having a common "sugar" type, and partly because a large amount of existing code either assumes `unsigned` (or `unsigned long`) is `size_t`, or they define the equivalent of size_t in their own way (such as sanitizer_internal_defs.h).https://github.com/llvm/llvm-project/blob/2e67dcfdcd023df2f06e0823eeea23990ce41534/compiler-rt/lib/sanitizer_common/sanitizer_internal_defs.h#L203
10 daysRevert "[Clang] Make the SizeType, SignedSizeType and PtrdiffType be named ↵Kazu Hirata1-44/+36
sugar types instead of built-in types (#143653)" This reverts commit c27e283cfbca2bd22f34592430e98ee76ed60ad8. A builbot failure has been reported: https://lab.llvm.org/buildbot/#/builders/186/builds/10819/steps/10/logs/stdio I'm also getting a large number of warnings related to %zu and %zx.
10 days[Clang] Make the SizeType, SignedSizeType and PtrdiffType be named sugar ↵YexuanXiao1-36/+44
types instead of built-in types (#143653) Including the results of `sizeof`, `sizeof...`, `__datasizeof`, `__alignof`, `_Alignof`, `alignof`, `_Countof`, `size_t` literals, and signed `size_t` literals, the results of pointer-pointer subtraction and checks for standard library functions (and their calls). The goal is to enable clang and downstream tools such as clangd and clang-tidy to provide more portable hints and diagnostics. The previous discussion can be found at #136542. This PR implements this feature by introducing a new subtype of `Type` called `PredefinedSugarType`, which was considered appropriate in discussions. I tried to keep `PredefinedSugarType` simple enough yet not limited to `size_t` and `ptrdiff_t` so that it can be used for other purposes. `PredefinedSugarType` wraps a canonical `Type` and provides a name, conceptually similar to a compiler internal `TypedefType` but without depending on a `TypedefDecl` or a source file. Additionally, checks for the `z` and `t` format specifiers in format strings for `scanf` and `printf` were added. It will precisely match expressions using `typedef`s or built-in expressions. The affected tests indicates that it works very well. Several code require that `SizeType` is canonical, so I kept `SizeType` to its canonical form. The failed tests in CI are allowed to fail. See the [comment](https://github.com/llvm/llvm-project/pull/135386#issuecomment-3049426611) in another PR #135386.
2025-05-25[StaticAnalyzer] Drop const from a return type (NFC) (#141414)Kazu Hirata1-3/+3
2025-05-23[clang][analyzer] Refine modeling of 'getcwd' in StdCLibraryFunctions ↵Balázs Kéri1-4/+10
checker (#141076) Add extra branches for the case when the buffer argument is NULL. Fixes #135720
2025-05-12Reland [Clang][analyzer] replace Stmt* with ConstCFGElement in ↵Fangyi Zhou1-1/+1
SymbolConjured (#137355) Closes #57270. This PR changes the `Stmt *` field in `SymbolConjured` with `CFGBlock::ConstCFGElementRef`. The motivation is that, when conjuring a symbol, there might not always be a statement available, causing information to be lost for conjured symbols, whereas the CFGElementRef can always be provided at the callsite. Following the idea, this PR changes callsites of functions to create conjured symbols, and replaces them with appropriate `CFGElementRef`s. There is a caveat at loop widening, where the correct location is the CFG terminator (which is not an element and does not have a ref). In this case, the first element in the block is passed as a location. Previous PR #128251, Reverted at #137304.
2025-04-25[clang][analyzer][NFC] Add a helper for conjuring symbols at call events ↵Fangyi Zhou1-7/+4
(#137182) Per suggestion in https://github.com/llvm/llvm-project/pull/128251#discussion_r2055916229, adding a new helper function in `SValBuilder` to conjure a symbol when given a `CallEvent`. Tested manually (with assertions) that the `LocationContext *` obtained from the `CallEvent` are identical to those passed in the original argument.
2025-04-25Revert "[Clang][analyzer] replace Stmt* with ConstCFGElementRef in ↵Balazs Benics1-4/+3
SymbolConjured" (#137304) Reverts llvm/llvm-project#128251 ASAN bots reported some errors: https://lab.llvm.org/buildbot/#/builders/55/builds/10398 Reverting for investigation. ``` Failed Tests (6): Clang :: Analysis/loop-widening-ignore-static-methods.cpp Clang :: Analysis/loop-widening-notes.cpp Clang :: Analysis/loop-widening-preserve-reference-type.cpp Clang :: Analysis/loop-widening.c Clang :: Analysis/loop-widening.cpp Clang :: Analysis/this-pointer.cpp Testing Time: 411.55s Total Discovered Tests: 118563 Skipped : 33 (0.03%) Unsupported : 2015 (1.70%) Passed : 116291 (98.08%) Expectedly Failed: 218 (0.18%) Failed : 6 (0.01%) FAILED: CMakeFiles/check-all /home/b/sanitizer-aarch64-linux-bootstrap-hwasan/build/llvm_build_hwasan/CMakeFiles/check-all cd /home/b/sanitizer-aarch64-linux-bootstrap-hwasan/build/llvm_build_hwasan && /usr/bin/python3 /home/b/sanitizer-aarch64-linux-bootstrap-hwasan/build/llvm_build_hwasan/./bin/llvm-lit -sv --param USE_Z3_SOLVER=0 /home/b/sanitizer-aarch64-linux-bootstrap-hwasan/build/llvm_build_hwasan/utils/mlgo-utils /home/b/sanitizer-aarch64-linux-bootstrap-hwasan/build/llvm_build_hwasan/tools/lld/test /home/b/sanitizer-aarch64-linux-bootstrap-hwasan/build/llvm_build_hwasan/tools/mlir/test /home/b/sanitizer-aarch64-linux-bootstrap-hwasan/build/llvm_build_hwasan/tools/clang/test /home/b/sanitizer-aarch64-linux-bootstrap-hwasan/build/llvm_build_hwasan/utils/lit /home/b/sanitizer-aarch64-linux-bootstrap-hwasan/build/llvm_build_hwasan/test ninja: build stopped: subcommand failed. ``` ``` /home/b/sanitizer-aarch64-linux-bootstrap-hwasan/build/llvm_build_hwasan/bin/clang -cc1 -internal-isystem /home/b/sanitizer-aarch64-linux-bootstrap-hwasan/build/llvm_build_hwasan/lib/clang/21/include -nostdsysteminc -analyze -analyzer-constraints=range -setup-static-analyzer -analyzer-checker=core,unix.Malloc,debug.ExprInspection -analyzer-max-loop 4 -analyzer-config widen-loops=true -verify -analyzer-config eagerly-assume=false /home/b/sanitizer-aarch64-linux-bootstrap-hwasan/build/llvm-project/clang/test/Analysis/loop-widening.c # RUN: at line 1 + /home/b/sanitizer-aarch64-linux-bootstrap-hwasan/build/llvm_build_hwasan/bin/clang -cc1 -internal-isystem /home/b/sanitizer-aarch64-linux-bootstrap-hwasan/build/llvm_build_hwasan/lib/clang/21/include -nostdsysteminc -analyze -analyzer-constraints=range -setup-static-analyzer -analyzer-checker=core,unix.Malloc,debug.ExprInspection -analyzer-max-loop 4 -analyzer-config widen-loops=true -verify -analyzer-config eagerly-assume=false /home/b/sanitizer-aarch64-linux-bootstrap-hwasan/build/llvm-project/clang/test/Analysis/loop-widening.c PLEASE submit a bug report to https://github.com/llvm/llvm-project/issues/ and include the crash backtrace, preprocessed source, and associated run script. Stack dump: 0. Program arguments: /home/b/sanitizer-aarch64-linux-bootstrap-hwasan/build/llvm_build_hwasan/bin/clang -cc1 -internal-isystem /home/b/sanitizer-aarch64-linux-bootstrap-hwasan/build/llvm_build_hwasan/lib/clang/21/include -nostdsysteminc -analyze -analyzer-constraints=range -setup-static-analyzer -analyzer-checker=core,unix.Malloc,debug.ExprInspection -analyzer-max-loop 4 -analyzer-config widen-loops=true -verify -analyzer-config eagerly-assume=false /home/b/sanitizer-aarch64-linux-bootstrap-hwasan/build/llvm-project/clang/test/Analysis/loop-widening.c 1. <eof> parser at end of file 2. While analyzing stack: #0 Calling nested_loop_inner_widen #0 0x0000c894cca289cc llvm::sys::PrintStackTrace(llvm::raw_ostream&, int) /home/b/sanitizer-aarch64-linux-bootstrap-hwasan/build/llvm-project/llvm/lib/Support/Unix/Signals.inc:804:13 #1 0x0000c894cca23324 llvm::sys::RunSignalHandlers() /home/b/sanitizer-aarch64-linux-bootstrap-hwasan/build/llvm-project/llvm/lib/Support/Signals.cpp:106:18 #2 0x0000c894cca29bbc SignalHandler(int, siginfo_t*, void*) /home/b/sanitizer-aarch64-linux-bootstrap-hwasan/build/llvm-project/llvm/lib/Support/Unix/Signals.inc:0:3 #3 0x0000f6898da4a8f8 (linux-vdso.so.1+0x8f8) #4 0x0000f6898d377608 (/lib/aarch64-linux-gnu/libc.so.6+0x87608) #5 0x0000f6898d32cb3c raise (/lib/aarch64-linux-gnu/libc.so.6+0x3cb3c) #6 0x0000f6898d317e00 abort (/lib/aarch64-linux-gnu/libc.so.6+0x27e00) #7 0x0000c894c5e77fec __sanitizer::Atexit(void (*)()) /home/b/sanitizer-aarch64-linux-bootstrap-hwasan/build/llvm-project/compiler-rt/lib/sanitizer_common/sanitizer_posix_libcdep.cpp:168:10 #8 0x0000c894c5e76680 __sanitizer::Die() /home/b/sanitizer-aarch64-linux-bootstrap-hwasan/build/llvm-project/compiler-rt/lib/sanitizer_common/sanitizer_termination.cpp:52:5 #9 0x0000c894c5e69650 Unlock /home/b/sanitizer-aarch64-linux-bootstrap-hwasan/build/llvm-project/compiler-rt/lib/hwasan/../sanitizer_common/sanitizer_mutex.h:250:16 #10 0x0000c894c5e69650 ~GenericScopedLock /home/b/sanitizer-aarch64-linux-bootstrap-hwasan/build/llvm-project/compiler-rt/lib/hwasan/../sanitizer_common/sanitizer_mutex.h:386:51 #11 0x0000c894c5e69650 __hwasan::ScopedReport::~ScopedReport() /home/b/sanitizer-aarch64-linux-bootstrap-hwasan/build/llvm-project/compiler-rt/lib/hwasan/hwasan_report.cpp:54:5 #12 0x0000c894c5e68de0 __hwasan::(anonymous namespace)::BaseReport::~BaseReport() /home/b/sanitizer-aarch64-linux-bootstrap-hwasan/build/llvm-project/compiler-rt/lib/hwasan/hwasan_report.cpp:476:7 #13 0x0000c894c5e66b74 __hwasan::ReportTagMismatch(__sanitizer::StackTrace*, unsigned long, unsigned long, bool, bool, unsigned long*) /home/b/sanitizer-aarch64-linux-bootstrap-hwasan/build/llvm-project/compiler-rt/lib/hwasan/hwasan_report.cpp:1091:1 #14 0x0000c894c5e52cf8 Destroy /home/b/sanitizer-aarch64-linux-bootstrap-hwasan/build/llvm-project/compiler-rt/lib/hwasan/../sanitizer_common/sanitizer_common.h:532:31 #15 0x0000c894c5e52cf8 ~InternalMmapVector /home/b/sanitizer-aarch64-linux-bootstrap-hwasan/build/llvm-project/compiler-rt/lib/hwasan/../sanitizer_common/sanitizer_common.h:642:56 #16 0x0000c894c5e52cf8 __hwasan::HandleTagMismatch(__hwasan::AccessInfo, unsigned long, unsigned long, void*, unsigned long*) /home/b/sanitizer-aarch64-linux-bootstrap-hwasan/build/llvm-project/compiler-rt/lib/hwasan/hwasan.cpp:245:1 #17 0x0000c894c5e551c8 __hwasan_tag_mismatch4 /home/b/sanitizer-aarch64-linux-bootstrap-hwasan/build/llvm-project/compiler-rt/lib/hwasan/hwasan.cpp:764:1 #18 0x0000c894c5e6a2f8 __interception::InterceptFunction(char const*, unsigned long*, unsigned long, unsigned long) /home/b/sanitizer-aarch64-linux-bootstrap-hwasan/build/llvm-project/compiler-rt/lib/interception/interception_linux.cpp:60:0 #19 0x0000c894d166f664 getBlock /home/b/sanitizer-aarch64-linux-bootstrap-hwasan/build/llvm-project/clang/include/clang/StaticAnalyzer/Core/PathSensitive/CoreEngine.h:217:45 #20 0x0000c894d166f664 getCFGElementRef /home/b/sanitizer-aarch64-linux-bootstrap-hwasan/build/llvm-project/clang/include/clang/StaticAnalyzer/Core/PathSensitive/ExprEngine.h:230:59 #21 0x0000c894d166f664 clang::ento::ExprEngine::processCFGBlockEntrance(clang::BlockEdge const&, clang::ento::NodeBuilderWithSinks&, clang::ento::ExplodedNode*) /home/b/sanitizer-aarch64-linux-bootstrap-hwasan/build/llvm-project/clang/lib/StaticAnalyzer/Core/ExprEngine.cpp:2570:45 #22 0x0000c894d15f3a1c hasGeneratedNodes /home/b/sanitizer-aarch64-linux-bootstrap-hwasan/build/llvm-project/clang/include/clang/StaticAnalyzer/Core/PathSensitive/CoreEngine.h:333:37 #23 0x0000c894d15f3a1c clang::ento::CoreEngine::HandleBlockEdge(clang::BlockEdge const&, clang::ento::ExplodedNode*) /home/b/sanitizer-aarch64-linux-bootstrap-hwasan/build/llvm-project/clang/lib/StaticAnalyzer/Core/CoreEngine.cpp:319:20 #24 0x0000c894d15f2c34 clang::ento::CoreEngine::dispatchWorkItem(clang::ento::ExplodedNode*, clang::ProgramPoint, clang::ento::WorkListUnit const&) /home/b/sanitizer-aarch64-linux-bootstrap-hwasan/build/llvm-project/clang/lib/StaticAnalyzer/Core/CoreEngine.cpp:220:7 #25 0x0000c894d15f2398 operator-> /home/b/sanitizer-aarch64-linux-bootstrap-hwasan/build/libcxx_install_hwasan/include/c++/v1/__memory/unique_ptr.h:267:101 #26 0x0000c894d15f2398 clang::ento::CoreEngine::ExecuteWorkList(clang::LocationContext const*, unsigned int, llvm::IntrusiveRefCntPtr<clang::ento::ProgramState const>)::$_0::operator()(unsigned int) const /home/b/sanitizer-aarch64-linux-bootstrap-hwasan/build/llvm-project/clang/lib/StaticAnalyzer/Core/CoreEngine.cpp:140:12 #27 0x0000c894d15f14b4 clang::ento::CoreEngine::ExecuteWorkList(clang::LocationContext const*, unsigned int, llvm::IntrusiveRefCntPtr<clang::ento::ProgramState const>) /home/b/sanitizer-aarch64-linux-bootstrap-hwasan/build/llvm-project/clang/lib/StaticAnalyzer/Core/CoreEngine.cpp:165:7 #28 0x0000c894d0ebb9dc release /home/b/sanitizer-aarch64-linux-bootstrap-hwasan/build/llvm-project/llvm/include/llvm/ADT/IntrusiveRefCntPtr.h:232:9 #29 0x0000c894d0ebb9dc ~IntrusiveRefCntPtr /home/b/sanitizer-aarch64-linux-bootstrap-hwasan/build/llvm-project/llvm/include/llvm/ADT/IntrusiveRefCntPtr.h:196:27 #30 0x0000c894d0ebb9dc ExecuteWorkList /home/b/sanitizer-aarch64-linux-bootstrap-hwasan/build/llvm-project/clang/include/clang/StaticAnalyzer/Core/PathSensitive/ExprEngine.h:192:5 #31 0x0000c894d0ebb9dc RunPathSensitiveChecks /home/b/sanitizer-aarch64-linux-bootstrap-hwasan/build/llvm-project/clang/lib/StaticAnalyzer/Frontend/AnalysisConsumer.cpp:772:7 #32 0x0000c894d0ebb9dc (anonymous namespace)::AnalysisConsumer::HandleCode(clang::Decl*, unsigned int, clang::ento::ExprEngine::InliningModes, llvm::DenseSet<clang::Decl const*, llvm::DenseMapInfo<clang::Decl const*, void>>*) /home/b/sanitizer-aarch64-linux-bootstrap-hwasan/build/llvm-project/clang/lib/StaticAnalyzer/Frontend/AnalysisConsumer.cpp:741:5 #33 0x0000c894d0eb6ee4 begin /home/b/sanitizer-aarch64-linux-bootstrap-hwasan/build/llvm-project/llvm/include/llvm/ADT/DenseMap.h:0:0 #34 0x0000c894d0eb6ee4 begin /home/b/sanitizer-aarch64-linux-bootstrap-hwasan/build/llvm-project/llvm/include/llvm/ADT/DenseSet.h:187:45 #35 0x0000c894d0eb6ee4 HandleDeclsCallGraph /home/b/sanitizer-aarch64-linux-bootstrap-hwasan/build/llvm-project/clang/lib/StaticAnalyzer/Frontend/AnalysisConsumer.cpp:516:29 #36 0x0000c894d0eb6ee4 runAnalysisOnTranslationUnit /home/b/sanitizer-aarch64-linux-bootstrap-hwasan/build/llvm-project/clang/lib/StaticAnalyzer/Frontend/AnalysisConsumer.cpp:584:5 #37 0x0000c894d0eb6ee4 (anonymous namespace)::AnalysisConsumer::HandleTranslationUnit(clang::ASTContext&) /home/b/sanitizer-aarch64-linux-bootstrap-hwasan/build/llvm-project/clang/lib/StaticAnalyzer/Frontend/AnalysisConsumer.cpp:647:3 #38 0x0000c894d18a7a38 clang::ParseAST(clang::Sema&, bool, bool) /home/b/sanitizer-aarch64-linux-bootstrap-hwasan/build/llvm-project/clang/lib/Parse/ParseAST.cpp:0:13 #39 0x0000c894ce81ed70 clang::FrontendAction::Execute() /home/b/sanitizer-aarch64-linux-bootstrap-hwasan/build/llvm-project/clang/lib/Frontend/FrontendAction.cpp:1231:10 #40 0x0000c894ce6f2144 getPtr /home/b/sanitizer-aarch64-linux-bootstrap-hwasan/build/llvm-project/llvm/include/llvm/Support/Error.h:278:42 #41 0x0000c894ce6f2144 operator bool /home/b/sanitizer-aarch64-linux-bootstrap-hwasan/build/llvm-project/llvm/include/llvm/Support/Error.h:241:16 #42 0x0000c894ce6f2144 clang::CompilerInstance::ExecuteAction(clang::FrontendAction&) /home/b/sanitizer-aarch64-linux-bootstrap-hwasan/build/llvm-project/clang/lib/Frontend/CompilerInstance.cpp:1058:23 #43 0x0000c894cea718cc operator-> /home/b/sanitizer-aarch64-linux-bootstrap-hwasan/build/libcxx_install_hwasan/include/c++/v1/__memory/shared_ptr.h:635:12 #44 0x0000c894cea718cc getFrontendOpts /home/b/sanitizer-aarch64-linux-bootstrap-hwasan/build/llvm-project/clang/include/clang/Frontend/CompilerInstance.h:307:12 #45 0x0000c894cea718cc clang::ExecuteCompilerInvocation(clang::CompilerInstance*) /home/b/sanitizer-aarch64-linux-bootstrap-hwasan/build/llvm-project/clang/lib/FrontendTool/ExecuteCompilerInvocation.cpp:301:14 #46 0x0000c894c5e9cf28 cc1_main(llvm::ArrayRef<char const*>, char const*, void*) /home/b/sanitizer-aarch64-linux-bootstrap-hwasan/build/llvm-project/clang/tools/driver/cc1_main.cpp:294:15 #47 0x0000c894c5e92a9c ExecuteCC1Tool(llvm::SmallVectorImpl<char const*>&, llvm::ToolContext const&) /home/b/sanitizer-aarch64-linux-bootstrap-hwasan/build/llvm-project/clang/tools/driver/driver.cpp:223:12 #48 0x0000c894c5e902ac clang_main(int, char**, llvm::ToolContext const&) /home/b/sanitizer-aarch64-linux-bootstrap-hwasan/build/llvm-project/clang/tools/driver/driver.cpp:0:12 #49 0x0000c894c5eb2e34 main /home/b/sanitizer-aarch64-linux-bootstrap-hwasan/build/llvm_build_hwasan/tools/clang/tools/driver/clang-driver.cpp:17:3 #50 0x0000f6898d3184c4 (/lib/aarch64-linux-gnu/libc.so.6+0x284c4) #51 0x0000f6898d318598 __libc_start_main (/lib/aarch64-linux-gnu/libc.so.6+0x28598) #52 0x0000c894c5e52a30 _start (/home/b/sanitizer-aarch64-linux-bootstrap-hwasan/build/llvm_build_hwasan/bin/clang+0x6512a30) /home/b/sanitizer-aarch64-linux-bootstrap-hwasan/build/llvm_build_hwasan/tools/clang/test/Analysis/Output/loop-widening.c.script: line 2: 2870204 Aborted /home/b/sanitizer-aarch64-linux-bootstrap-hwasan/build/llvm_build_hwasan/bin/clang -cc1 -internal-isystem /home/b/sanitizer-aarch64-linux-bootstrap-hwasan/build/llvm_build_hwasan/lib/clang/21/include -nostdsysteminc -analyze -analyzer-constraints=range -setup-static-analyzer -analyzer-checker=core,unix.Malloc,debug.ExprInspection -analyzer-max-loop 4 -analyzer-config widen-loops=true -verify -analyzer-config eagerly-assume=false /home/b/sanitizer-aarch64-linux-bootstrap-hwasan/build/llvm-project/clang/test/Analysis/loop-widening.c ```
2025-04-25[Clang][analyzer] Replace Stmt* with ConstCFGElementRef in SymbolConjured ↵Fangyi Zhou1-3/+4
(#128251) This PR changes the `Stmt *` field in `SymbolConjured` with `CFGBlock::ConstCFGElementRef`. The motivation is that, when conjuring a symbol, there might not always be a statement available, causing information to be lost for conjured symbols, whereas the CFGElementRef can always be provided at the callsite. Following the idea, this PR changes callsites of functions to create conjured symbols, and replaces them with appropriate `CFGElementRef`s. Closes #57270
2025-03-28[NFC] Fix a typo in StdLibraryFunctionsChecker.cpp comments (#133375)Qinkun Bao1-1/+1
2025-03-25[NFC][StaticAnalyzer] Rename `NotNullConstraint` & `NotNullBufferConstraint` ↵Ziqing Luo1-28/+31
(#131374) `NotNullConstraint` is used to check both null and non-null of a pointer. So the name, which was created originally for just checking non-nullness, becomes less suitable. The same reason applies to `NotNullBufferConstraint`. This commit renames them. In addition, messages of the assertions in `describe` and ` describeArgumentValue` are updated to indicate that these two functions can be called on any constraint though they were partially implemented for `NotNullConstraint` & `NotNullBufferConstraint`.
2025-03-11[StaticAnalyzer] Relax the pre-condition of 'setsockopt' (#130683)Ziqing Luo1-2/+3
For the unix function `int setsockopt(int, int, int, const void *, socklen_t);`, the last two parameters represent a buffer and a size. In case the size is zero, buffer can be null. Previously, the hard-coded pre-condition requires the buffer to never be null, which can cause false positives. (rdar://146678142)
2024-12-19[analyzer][NFC] Introduce APSIntPtr, a safe wrapper of APSInt (1/4) (#120435)Balazs Benics1-7/+7
One could create dangling APSInt references in various ways in the past, that were sometimes assumed to be persisted in the BasicValueFactor. One should always use BasicValueFactory to create persistent APSInts, that could be used by ConcreteInts or SymIntExprs and similar long-living objects. If one used a temporary or local variables for this, these would dangle. To enforce the contract of the analyzer BasicValueFactory and the uses of APSInts, let's have a dedicated strong-type for this. The idea is that APSIntPtr is always owned by the BasicValueFactory, and that is the only component that can construct it. These PRs are all NFC - besides fixing dangling APSInt references.
2024-11-15Revert "[analyzer][Solver] Early return if sym is concrete on assuming" ↵Balazs Benics1-2/+0
(#116362) Reverts llvm/llvm-project#115579 This introduced a breakage: https://lab.llvm.org/buildbot/#/builders/46/builds/7928
2024-11-15[analyzer][Solver] Early return if sym is concrete on assuming (#115579)Ding Fei1-0/+2
This could deduce some complex syms derived from simple ones whose values could be constrainted to be concrete during execution, thus reducing some overconstrainted states. This commit also fix `unix.StdCLibraryFunctions` crash due to these overconstrainted states being added to the graph, which is marked as sink node (PosteriorlyOverconstrained). The 'assume' API is used in non-dual style so the checker should protectively test whether these newly added nodes are actually impossible. 1. The crash: https://godbolt.org/z/8KKWeKb86 2. The solver needs to solve equivalent: https://godbolt.org/z/ed8WqsbTh
2024-08-29Revert "Revert "[Support] Validate number of arguments passed to formatv()"" ↵Rahul Joshi1-1/+4
(#106592) Reverts llvm/llvm-project#106589 The fix for bot failures caused by the reverted commit was committed already, so this revert is not needed.
2024-08-29Revert "[Support] Validate number of arguments passed to formatv()" (#106589)Mehdi Amini1-4/+1
Reverts llvm/llvm-project#105745 Some bots are broken apparently.
2024-08-29[Support] Validate number of arguments passed to formatv() (#105745)Rahul Joshi1-1/+4
Change formatv() to validate that the number of arguments passed matches number of replacement fields in the format string, and that the replacement indices do not contain holes. To support cases where this cannot be guaranteed, introduce a formatv() overload that allows disabling validation with a bool flag as its first argument.
2024-06-28[analyzer][NFC] Use ArrayRef for input parameters (#93203)Balazs Benics1-2/+2
Fixes #79684
2024-04-08[NFC] Fix misspellings of effects (#87795)Zentrik1-1/+1
2024-02-21[clang][analyzer] Change modeling of 'fileno' in checkers. (#81842)Balázs Kéri1-3/+6
Function 'fileno' fails only if invalid pointer is passed, this is a case that is often ignored in source code. The failure case leads to many "false positive" reports when `fileno` returns -1 and this is not checked in the program. Because this, the function is now assumed to not fail (this is assumption that the passed file pointer is correct). The change affects `StdCLibraryFunctionsChecker` and `StreamChecker`.
2024-02-08[clang][analyzer] Add missing stream related functions to ↵Balázs Kéri1-11/+68
StdLibraryFunctionsChecker. (#76979) Some stream functions were recently added to `StreamChecker` that were not modeled by `StdCLibraryFunctionsChecker`. To ensure consistency these functions are added to the other checker too. Some of the related tests are re-organized.
2024-01-31[clang][analyzer] Improve modeling of 'realpath' in ↵Ben Shi1-2/+6
StdLibraryFunctionsChecker (#79939)
2024-01-27[clang][analyzer] Improve modeling of 'popen' and 'pclose' in ↵Ben Shi1-15/+19
StdLibraryFunctionsChecker (#78895)
2024-01-25[clang][analyzer] Improve modeling of 'execv' and 'execvp' in ↵Ben Shi1-2/+2
StdLibraryFunctionsChecker (#78930) These functions always return -1 and set 'errno'.
2024-01-22[clang][analyzer][NFC] Simplify ranges in StdLibraryFunctionsChecker (#78886)Ben Shi1-9/+7
2024-01-20[clang][analyzer] Improve modeling of 'fdopen' in StdLibraryFunctionsChecker ↵Ben Shi1-9/+10
(#78680)
2024-01-16Improve modeling of two functions in StdLibraryFunctionsChecker (#78079)Ben Shi1-8/+11
Improve 'errno' modeling of 'opendir' and 'fdopendir'.
2024-01-16[clang][analyzer] Improve modeling of 'fseeko' and 'ftello' in ↵Ben Shi1-15/+22
StdLibraryFunctionsChecker (#77902)
2024-01-11[clang][analyzer] Fix incorrect range of 'ftell' in the ↵Ben Shi1-1/+1
StdLibraryFunctionsChecker (#77576) According to https://pubs.opengroup.org/onlinepubs/9699919799/, the return value of `ftell` is not restricted to `> 0`, and may return `0` in real world.
2024-01-10[clang][analyzer] Add function 'ungetc' to StreamChecker. (#77331)Balázs Kéri1-0/+19
`StdLibraryFunctionsChecker` is updated too with `ungetc`.
2024-01-09Improve modeling of 'getcwd' in the StdLibraryFunctionsChecker (#77040)Ben Shi1-1/+12
1. Improve the 'errno' modeling. 2. Improve constraints of the arguments.
2024-01-03[clang][analyzer] Improve 'errno' modeling of 'mkdtemp' (#76671)Ben Shi1-2/+5
2024-01-01[analyzer][NFC] Cleanup BugType lazy-init patterns (#76655)Balazs Benics1-6/+2
Cleanup most of the lazy-init `BugType` legacy. Some will be preserved, as those are slightly more complicated to refactor. Notice, that the default category for `BugType` is `LogicError`. I omitted setting this explicitly where I could. Please, actually have a look at the diff. I did this manually, and we rarely check the bug type descriptions and stuff in tests, so the testing might be shallow on this one.
2023-12-30[clang][analyzer] Support 'fflush' in the StdLibraryFunctionsChecker (#76557)Ben Shi1-0/+8
Co-authored-by: Balazs Benics <benicsbalazs@gmail.com>
2023-11-16[StaticAnalyzer] Fix warnings about missing overrideKazu Hirata1-2/+2
This patch fixes: clang/lib/StaticAnalyzer/Checkers/StdLibraryFunctionsChecker.cpp:609:23: error: 'describe' overrides a member function but is not marked 'override' [-Werror,-Winconsistent-missing-override] clang/lib/StaticAnalyzer/Checkers/StdLibraryFunctionsChecker.cpp:627:23: error: 'describe' overrides a member function but is not marked 'override' [-Werror,-Winconsistent-missing-override]
2023-11-16[clang][analyzer] Improve 'errno' handling in StdLibraryFunctionsChecker. ↵Balázs Kéri1-45/+63
(#71392) The checker now displays one combined note tag for errno-related and "case"-related notes. Previous functions in the errno-modeling part that were used for construction of note tags are removed. The note tag added by StdLibraryFunctionsChecker contains the code to display the note tag for 'errno' (this was done previously by these removed functions).
2023-11-14[clang][analyzer] Improve StdLibraryFunctionsChecker 'readlink' modeling. ↵Balázs Kéri1-4/+14
(#71373) The functions 'readlink' and 'readlinkat' do return 0 only if the 'bufsize' argument is 0.
2023-09-14[analyzer] Fix StdLibraryFunctionsChecker crash on surprising sink node (#66109)DonatNagyE1-3/+8
Recent changes in StdLibraryFunctionsChecker introduced a situation where the checker sequentially performed two state transitions to add two separate note tags. In the unlikely case when the updated state (the variable `NewState`) was posteriorly overconstrained, the engine marked the node after the first state transition as a sink to stop the "natural" graph exploration after that point. However, in this particular case the checker tried to directly add a second node, and this triggered an assertion in the `addPredecessor()` method of `ExplodedNode`. This commit introduces an explicit `isSink()` check to avoid this crash. To avoid similar bugs in the future, perhaps it would be possible to tweak `addTransition()` and ensure that it returns `nullptr` when it would return a sink node (to unify the two possible error conditions). This crash was observed in an analysis of the curl project (in a very long and complex function), and there I validated that this is the root cause, but I don't have a self-contained testcase that can trigger the creation of a PosteriorlyOverconstrained node in this situation.
2023-08-07[clang][analyzer] Improve StdCLibraryFunctions socket send/recv functions.Balázs Kéri1-6/+18
The modeling of send, recv, sendmsg, recvmsg, sendto, recvfrom is changed: These functions do not return 0, except if the message length is 0. (In sendmsg, recvmsg the length is not checkable but it is more likely that a message with 0 length is invalid for these functions.) Reviewed By: donat.nagy Differential Revision: https://reviews.llvm.org/D155715
2023-07-19[clang][analyzer] StdLibraryFunctionsChecker: Allow NULL buffer in `fread` ↵Balázs Kéri1-4/+106
and `fwrite` if size is zero. Reviewed By: donat.nagy Differential Revision: https://reviews.llvm.org/D154509
2023-07-18[clang][analyzer] Add all success/failure messages to ↵Balázs Kéri1-175/+222
StdLibraryFunctionsChecker. Success or failure messages are now shown at all checked functions, if the call (return value) is interesting. Additionally new functions are added: open, openat, socket, shutdown Reviewed By: donat.nagy Differential Revision: https://reviews.llvm.org/D154423
2023-07-18[clang][analyzer] Display notes in StdLibraryFunctionsChecker only if ↵Balázs Kéri1-6/+12
interesting The note tag that was previously added in all cases when a standard function call is found is displayed now only if the function call (return value) is "interesting". This results in less unneeded notes but some of the previously good notes disappear too. This is because interestingness is not always set as it should be. Reviewed By: donat.nagy Differential Revision: https://reviews.llvm.org/D153776
2023-07-18[clang][analyzer] Add and change NoteTags in StdLibraryFunctionsChecker.Balázs Kéri1-51/+81
Change 1: ErrnoChecker notes show only messages related to errno, not to assumption of success or failure of functions. Change 2: StdLibraryFunctionsChecker adds its own note about success or failure of functions, and the errno related note, independently. Change 3: Every modeled function in StdLibraryFunctionsChecker should have a note tag message in all "cases". This is not implemented yet, only for file (stream) related functions. Reviewed By: donat.nagy Differential Revision: https://reviews.llvm.org/D153612
2023-07-05[analyzer][NFC] Move away from using raw-for loops inside StaticAnalyzerBalazs Benics1-8/+6
I'm involved with the Static Analyzer for the most part. I think we should embrace newer language standard features and gradually move forward. Differential Revision: https://reviews.llvm.org/D154325
2023-06-01[clang][analyzer] Merge apiModeling.StdCLibraryFunctions and ↵Balázs Kéri1-27/+16
StdCLibraryFunctionArgs checkers into one. Main reason for this change is that these checkers were implemented in the same class but had different dependency ordering. (NonNullParamChecker should run before StdCLibraryFunctionArgs to get more special warning about null arguments, but the apiModeling.StdCLibraryFunctions was a modeling checker that should run before other non-modeling checkers. The modeling checker changes state in a way that makes it impossible to detect a null argument by NonNullParamChecker.) To make it more simple, the modeling part is removed as separate checker and can be only used if checker StdCLibraryFunctions is turned on, that produces the warnings too. Modeling the functions without bug detection (for invalid argument) is not possible. The modeling of standard functions does not happen by default from this change on. Reviewed By: Szelethus Differential Revision: https://reviews.llvm.org/D151225
2023-05-17[clang][analyzer] Display buffer sizes in StdCLibraryFunctionArgs checkerBalázs Kéri1-0/+35
If a wrong (too small) buffer argument is found, the dynamic buffer size and values of connected arguments are displayed in the warning message, if these are simple known integer values. Reviewed By: Szelethus Differential Revision: https://reviews.llvm.org/D149321
2023-05-16[clang][analyzer] Handle special value AT_FDCWD in affected standard functionsBalázs Kéri1-16/+25
Some file and directory related functions have an integer file descriptor argument that can be a valid file descriptor or a special value AT_FDCWD. This value is relatively often used in open source projects and is usually defined as a negative number, and the checker reports false warnings (a valid file descriptor is not negative) if this fix is not included. Reviewed By: steakhal Differential Revision: https://reviews.llvm.org/D149160
2023-04-12[clang][analyzer] Improve bug reports of StdLibraryFunctionsChecker.Balázs Kéri1-129/+207
Add an additional explanation of what is wrong if a constraint is not satisfied, in some cases. Additionally the bug report generation is changed to use raw_ostream. Reviewed By: Szelethus, NoQ Differential Revision: https://reviews.llvm.org/D144003
2023-03-09[clang][analyzer][NFC] Refactor code of StdLibraryFunctionsChecker.Balázs Kéri1-276/+330
The code was difficult to maintain (big internal class definitions with long inline functions, other functions of the same class at different location far away, irregular ordering of classes and function definitions). It is now improved to some extent. New functions are added to RangeConstraint to remove code repetition, these are useful for planned new features too. Comments are improved. Reviewed By: Szelethus Differential Revision: https://reviews.llvm.org/D143751