1 //=== ErrnoModeling.h - Tracking value of 'errno'. -----------------*- C++ -*-//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 // Defines inter-checker API for using the system value 'errno'.
10 //
11 //===----------------------------------------------------------------------===//
12 
13 #ifndef LLVM_CLANG_LIB_STATICANALYZER_CHECKERS_ERRNOMODELING_H
14 #define LLVM_CLANG_LIB_STATICANALYZER_CHECKERS_ERRNOMODELING_H
15 
16 #include "clang/StaticAnalyzer/Core/PathSensitive/CheckerContext.h"
17 #include "clang/StaticAnalyzer/Core/PathSensitive/ProgramState.h"
18 #include "clang/StaticAnalyzer/Core/PathSensitive/SVals.h"
19 
20 namespace clang {
21 namespace ento {
22 namespace errno_modeling {
23 
24 enum ErrnoCheckState : unsigned {
25   /// We do not know anything about 'errno'.
26   Irrelevant = 0,
27 
28   /// Value of 'errno' should be checked to find out if a previous function call
29   /// has failed.
30   MustBeChecked = 1,
31 
32   /// Value of 'errno' is not allowed to be read, it can contain an unspecified
33   /// value.
34   MustNotBeChecked = 2
35 };
36 
37 /// Returns the value of 'errno', if 'errno' was found in the AST.
38 llvm::Optional<SVal> getErrnoValue(ProgramStateRef State);
39 
40 /// Returns the errno check state, \c Errno_Irrelevant if 'errno' was not found
41 /// (this is not the only case for that value).
42 ErrnoCheckState getErrnoState(ProgramStateRef State);
43 
44 /// Returns the location that points to the \c MemoryRegion where the 'errno'
45 /// value is stored. Returns \c None if 'errno' was not found. Otherwise it
46 /// always returns a valid memory region in the system global memory space.
47 llvm::Optional<Loc> getErrnoLoc(ProgramStateRef State);
48 
49 /// Set value of 'errno' to any SVal, if possible.
50 /// The errno check state is set always when the 'errno' value is set.
51 ProgramStateRef setErrnoValue(ProgramStateRef State,
52                               const LocationContext *LCtx, SVal Value,
53                               ErrnoCheckState EState);
54 
55 /// Set value of 'errno' to a concrete (signed) integer, if possible.
56 /// The errno check state is set always when the 'errno' value is set.
57 ProgramStateRef setErrnoValue(ProgramStateRef State, CheckerContext &C,
58                               uint64_t Value, ErrnoCheckState EState);
59 
60 /// Set the errno check state, do not modify the errno value.
61 ProgramStateRef setErrnoState(ProgramStateRef State, ErrnoCheckState EState);
62 
63 /// Determine if a `Decl` node related to 'errno'.
64 /// This is true if the declaration is the errno variable or a function
65 /// that returns a pointer to the 'errno' value (usually the 'errno' macro is
66 /// defined with this function). \p D is not required to be a canonical
67 /// declaration.
68 bool isErrno(const Decl *D);
69 
70 /// Create a NoteTag that displays the message if the 'errno' memory region is
71 /// marked as interesting, and resets the interestingness.
72 const NoteTag *getErrnoNoteTag(CheckerContext &C, const std::string &Message);
73 
74 } // namespace errno_modeling
75 } // namespace ento
76 } // namespace clang
77 
78 #endif // LLVM_CLANG_LIB_STATICANALYZER_CHECKERS_ERRNOMODELING_H
79