Verifier.h 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. //===- Verifier.h - LLVM IR Verifier ----------------------------*- C++ -*-===//
  2. //
  3. // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
  4. // See https://llvm.org/LICENSE.txt for license information.
  5. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  6. //
  7. //===----------------------------------------------------------------------===//
  8. //
  9. // This file defines the function verifier interface, that can be used for some
  10. // sanity checking of input to the system, and for checking that transformations
  11. // haven't done something bad.
  12. //
  13. // Note that this does not provide full 'java style' security and verifications,
  14. // instead it just tries to ensure that code is well formed.
  15. //
  16. // To see what specifically is checked, look at the top of Verifier.cpp
  17. //
  18. //===----------------------------------------------------------------------===//
  19. #ifndef LLVM_IR_VERIFIER_H
  20. #define LLVM_IR_VERIFIER_H
  21. #include "llvm/ADT/DenseMap.h"
  22. #include "llvm/IR/PassManager.h"
  23. #include <utility>
  24. namespace llvm {
  25. class APInt;
  26. class Function;
  27. class FunctionPass;
  28. class Instruction;
  29. class MDNode;
  30. class Module;
  31. class raw_ostream;
  32. struct VerifierSupport;
  33. /// Verify that the TBAA Metadatas are valid.
  34. class TBAAVerifier {
  35. VerifierSupport *Diagnostic = nullptr;
  36. /// Helper to diagnose a failure
  37. template <typename... Tys> void CheckFailed(Tys &&... Args);
  38. /// Cache of TBAA base nodes that have already been visited. This cachce maps
  39. /// a node that has been visited to a pair (IsInvalid, BitWidth) where
  40. ///
  41. /// \c IsInvalid is true iff the node is invalid.
  42. /// \c BitWidth, if non-zero, is the bitwidth of the integer used to denoting
  43. /// the offset of the access. If zero, only a zero offset is allowed.
  44. ///
  45. /// \c BitWidth has no meaning if \c IsInvalid is true.
  46. using TBAABaseNodeSummary = std::pair<bool, unsigned>;
  47. DenseMap<const MDNode *, TBAABaseNodeSummary> TBAABaseNodes;
  48. /// Maps an alleged scalar TBAA node to a boolean that is true if the said
  49. /// TBAA node is a valid scalar TBAA node or false otherwise.
  50. DenseMap<const MDNode *, bool> TBAAScalarNodes;
  51. /// \name Helper functions used by \c visitTBAAMetadata.
  52. /// @{
  53. MDNode *getFieldNodeFromTBAABaseNode(Instruction &I, const MDNode *BaseNode,
  54. APInt &Offset, bool IsNewFormat);
  55. TBAAVerifier::TBAABaseNodeSummary verifyTBAABaseNode(Instruction &I,
  56. const MDNode *BaseNode,
  57. bool IsNewFormat);
  58. TBAABaseNodeSummary verifyTBAABaseNodeImpl(Instruction &I,
  59. const MDNode *BaseNode,
  60. bool IsNewFormat);
  61. bool isValidScalarTBAANode(const MDNode *MD);
  62. /// @}
  63. public:
  64. TBAAVerifier(VerifierSupport *Diagnostic = nullptr)
  65. : Diagnostic(Diagnostic) {}
  66. /// Visit an instruction and return true if it is valid, return false if an
  67. /// invalid TBAA is attached.
  68. bool visitTBAAMetadata(Instruction &I, const MDNode *MD);
  69. };
  70. /// Check a function for errors, useful for use when debugging a
  71. /// pass.
  72. ///
  73. /// If there are no errors, the function returns false. If an error is found,
  74. /// a message describing the error is written to OS (if non-null) and true is
  75. /// returned.
  76. bool verifyFunction(const Function &F, raw_ostream *OS = nullptr);
  77. /// Check a module for errors.
  78. ///
  79. /// If there are no errors, the function returns false. If an error is
  80. /// found, a message describing the error is written to OS (if
  81. /// non-null) and true is returned.
  82. ///
  83. /// \return true if the module is broken. If BrokenDebugInfo is
  84. /// supplied, DebugInfo verification failures won't be considered as
  85. /// error and instead *BrokenDebugInfo will be set to true. Debug
  86. /// info errors can be "recovered" from by stripping the debug info.
  87. bool verifyModule(const Module &M, raw_ostream *OS = nullptr,
  88. bool *BrokenDebugInfo = nullptr);
  89. FunctionPass *createVerifierPass(bool FatalErrors = true);
  90. /// Check a module for errors, and report separate error states for IR
  91. /// and debug info errors.
  92. class VerifierAnalysis : public AnalysisInfoMixin<VerifierAnalysis> {
  93. friend AnalysisInfoMixin<VerifierAnalysis>;
  94. static AnalysisKey Key;
  95. public:
  96. struct Result {
  97. bool IRBroken, DebugInfoBroken;
  98. };
  99. Result run(Module &M, ModuleAnalysisManager &);
  100. Result run(Function &F, FunctionAnalysisManager &);
  101. static bool isRequired() { return true; }
  102. };
  103. /// Check a module for errors, but report debug info errors separately.
  104. /// Otherwise behaves as the normal verifyModule. Debug info errors can be
  105. /// "recovered" from by stripping the debug info.
  106. bool verifyModule(bool &BrokenDebugInfo, const Module &M, raw_ostream *OS);
  107. /// Create a verifier pass.
  108. ///
  109. /// Check a module or function for validity. This is essentially a pass wrapped
  110. /// around the above verifyFunction and verifyModule routines and
  111. /// functionality. When the pass detects a verification error it is always
  112. /// printed to stderr, and by default they are fatal. You can override that by
  113. /// passing \c false to \p FatalErrors.
  114. ///
  115. /// Note that this creates a pass suitable for the legacy pass manager. It has
  116. /// nothing to do with \c VerifierPass.
  117. class VerifierPass : public PassInfoMixin<VerifierPass> {
  118. bool FatalErrors;
  119. public:
  120. explicit VerifierPass(bool FatalErrors = true) : FatalErrors(FatalErrors) {}
  121. PreservedAnalyses run(Module &M, ModuleAnalysisManager &AM);
  122. PreservedAnalyses run(Function &F, FunctionAnalysisManager &AM);
  123. static bool isRequired() { return true; }
  124. };
  125. } // end namespace llvm
  126. #endif // LLVM_IR_VERIFIER_H