Verifier.h 5.5 KB

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