ErrnoModeling.h 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  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. #ifndef LLVM_CLANG_LIB_STATICANALYZER_CHECKERS_ERRNOMODELING_H
  13. #define LLVM_CLANG_LIB_STATICANALYZER_CHECKERS_ERRNOMODELING_H
  14. #include "clang/StaticAnalyzer/Core/PathSensitive/CheckerContext.h"
  15. #include "clang/StaticAnalyzer/Core/PathSensitive/ProgramState.h"
  16. #include "clang/StaticAnalyzer/Core/PathSensitive/SVals.h"
  17. #include <optional>
  18. namespace clang {
  19. namespace ento {
  20. namespace errno_modeling {
  21. /// Describe how reads and writes of \c errno are handled by the checker.
  22. enum ErrnoCheckState : unsigned {
  23. /// We do not know anything about 'errno'.
  24. /// Read and write is always allowed.
  25. Irrelevant = 0,
  26. /// Value of 'errno' should be checked to find out if a previous function call
  27. /// has failed.
  28. /// When this state is set \c errno must be read by the program before a next
  29. /// standard function call or other overwrite of \c errno follows, otherwise
  30. /// a bug report is emitted.
  31. MustBeChecked = 1,
  32. /// Value of 'errno' is not allowed to be read, it can contain an unspecified
  33. /// value.
  34. /// When this state is set \c errno is not allowed to be read by the program
  35. /// until it is overwritten or invalidated.
  36. MustNotBeChecked = 2
  37. };
  38. /// Returns the value of 'errno', if 'errno' was found in the AST.
  39. std::optional<SVal> getErrnoValue(ProgramStateRef State);
  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. /// Returns the location that points to the \c MemoryRegion where the 'errno'
  44. /// value is stored. Returns \c std::nullopt if 'errno' was not found. Otherwise
  45. /// it always returns a valid memory region in the system global memory space.
  46. std::optional<Loc> getErrnoLoc(ProgramStateRef State);
  47. /// Set value of 'errno' to any SVal, if possible.
  48. /// The errno check state is set always when the 'errno' value is set.
  49. ProgramStateRef setErrnoValue(ProgramStateRef State,
  50. const LocationContext *LCtx, SVal Value,
  51. ErrnoCheckState EState);
  52. /// Set value of 'errno' to a concrete (signed) integer, if possible.
  53. /// The errno check state is set always when the 'errno' value is set.
  54. ProgramStateRef setErrnoValue(ProgramStateRef State, CheckerContext &C,
  55. uint64_t Value, ErrnoCheckState EState);
  56. /// Set the errno check state, do not modify the errno value.
  57. ProgramStateRef setErrnoState(ProgramStateRef State, ErrnoCheckState EState);
  58. /// Clear state of errno (make it irrelevant).
  59. ProgramStateRef clearErrnoState(ProgramStateRef State);
  60. /// Determine if a `Decl` node related to 'errno'.
  61. /// This is true if the declaration is the errno variable or a function
  62. /// that returns a pointer to the 'errno' value (usually the 'errno' macro is
  63. /// defined with this function). \p D is not required to be a canonical
  64. /// declaration.
  65. bool isErrno(const Decl *D);
  66. /// Produce a textual description about how \c errno is allowed to be used
  67. /// (in a \c ErrnoCheckState).
  68. /// The returned string is insertable into a longer warning message in the form
  69. /// "the value 'errno' <...>".
  70. /// Currently only the \c errno_modeling::MustNotBeChecked state is supported,
  71. /// others are not used by the clients.
  72. const char *describeErrnoCheckState(ErrnoCheckState CS);
  73. /// Create a NoteTag that displays the message if the 'errno' memory region is
  74. /// marked as interesting, and resets the interestingness.
  75. const NoteTag *getErrnoNoteTag(CheckerContext &C, const std::string &Message);
  76. /// Set errno state for the common case when a standard function is successful.
  77. /// Set \c ErrnoCheckState to \c MustNotBeChecked (the \c errno value is not
  78. /// affected). At the state transition a note tag created by
  79. /// \c getNoteTagForStdSuccess can be used.
  80. ProgramStateRef setErrnoForStdSuccess(ProgramStateRef State, CheckerContext &C);
  81. /// Set errno state for the common case when a standard function fails.
  82. /// Set \c errno value to be not equal to zero and \c ErrnoCheckState to
  83. /// \c Irrelevant . The irrelevant errno state ensures that no related bug
  84. /// report is emitted later and no note tag is needed.
  85. /// \arg \c ErrnoSym Value to be used for \c errno and constrained to be
  86. /// non-zero.
  87. ProgramStateRef setErrnoForStdFailure(ProgramStateRef State, CheckerContext &C,
  88. NonLoc ErrnoSym);
  89. /// Set errno state for the common case when a standard function indicates
  90. /// failure only by \c errno. Sets \c ErrnoCheckState to \c MustBeChecked, and
  91. /// invalidates the errno region (clear of previous value).
  92. /// At the state transition a note tag created by
  93. /// \c getNoteTagForStdMustBeChecked can be used.
  94. /// \arg \c InvalE Expression that causes invalidation of \c errno.
  95. ProgramStateRef setErrnoStdMustBeChecked(ProgramStateRef State,
  96. CheckerContext &C, const Expr *InvalE);
  97. /// Generate the note tag that can be applied at the state generated by
  98. /// \c setErrnoForStdSuccess .
  99. /// \arg \c Fn Name of the (standard) function that is modeled.
  100. const NoteTag *getNoteTagForStdSuccess(CheckerContext &C, llvm::StringRef Fn);
  101. /// Generate the note tag that can be applied at the state generated by
  102. /// \c setErrnoStdMustBeChecked .
  103. /// \arg \c Fn Name of the (standard) function that is modeled.
  104. const NoteTag *getNoteTagForStdMustBeChecked(CheckerContext &C,
  105. llvm::StringRef Fn);
  106. } // namespace errno_modeling
  107. } // namespace ento
  108. } // namespace clang
  109. #endif // LLVM_CLANG_LIB_STATICANALYZER_CHECKERS_ERRNOMODELING_H