|
| 1 | + |
| 2 | +#pragma once |
| 3 | + |
| 4 | +#include "binaryninjaapi.h" |
| 5 | +#include "lowlevelilinstruction.h" |
| 6 | +#include "mediumlevelilinstruction.h" |
| 7 | +#include "highlevelilinstruction.h" |
| 8 | + |
| 9 | +class ILVerifier |
| 10 | +{ |
| 11 | +public: |
| 12 | + BNFunctionGraphType m_ilType; |
| 13 | + |
| 14 | + struct Diagnostic |
| 15 | + { |
| 16 | + BNTypeParserErrorSeverity severity; |
| 17 | + BNFunctionGraphType ilType; |
| 18 | + size_t exprIndex; |
| 19 | + size_t instrIndex; |
| 20 | + std::string message; |
| 21 | + |
| 22 | + static Diagnostic Diag( |
| 23 | + BNTypeParserErrorSeverity severity, |
| 24 | + const ILVerifier* verifier, |
| 25 | + const std::string& message |
| 26 | + ) |
| 27 | + { |
| 28 | + Diagnostic d; |
| 29 | + d.severity = severity; |
| 30 | + d.ilType = verifier->m_ilType; |
| 31 | + d.exprIndex = BN_INVALID_EXPR; |
| 32 | + d.instrIndex = BN_INVALID_EXPR; |
| 33 | + d.message = message; |
| 34 | + return d; |
| 35 | + } |
| 36 | + static Diagnostic Error( |
| 37 | + const ILVerifier* verifier, |
| 38 | + const std::string& message |
| 39 | + ) |
| 40 | + { |
| 41 | + Diagnostic d; |
| 42 | + d.severity = ErrorSeverity; |
| 43 | + d.ilType = verifier->m_ilType; |
| 44 | + d.exprIndex = BN_INVALID_EXPR; |
| 45 | + d.instrIndex = BN_INVALID_EXPR; |
| 46 | + d.message = message; |
| 47 | + return d; |
| 48 | + } |
| 49 | + static Diagnostic Diag( |
| 50 | + BNTypeParserErrorSeverity severity, |
| 51 | + const ILVerifier* verifier, |
| 52 | + const BinaryNinja::LowLevelILInstruction& instr, |
| 53 | + const std::string& message |
| 54 | + ) |
| 55 | + { |
| 56 | + Diagnostic d; |
| 57 | + d.severity = severity; |
| 58 | + d.ilType = verifier->m_ilType; |
| 59 | + d.exprIndex = instr.exprIndex; |
| 60 | + d.instrIndex = instr.instructionIndex; |
| 61 | + d.message = fmt::format("{:?} {}", instr, message); |
| 62 | + return d; |
| 63 | + } |
| 64 | + static Diagnostic Error( |
| 65 | + const ILVerifier* verifier, |
| 66 | + const BinaryNinja::LowLevelILInstruction& instr, |
| 67 | + const std::string& message |
| 68 | + ) |
| 69 | + { |
| 70 | + Diagnostic d; |
| 71 | + d.severity = ErrorSeverity; |
| 72 | + d.ilType = verifier->m_ilType; |
| 73 | + d.exprIndex = instr.exprIndex; |
| 74 | + d.instrIndex = instr.instructionIndex; |
| 75 | + d.message = fmt::format("{:?} {}", instr, message); |
| 76 | + return d; |
| 77 | + } |
| 78 | + static Diagnostic Diag( |
| 79 | + BNTypeParserErrorSeverity severity, |
| 80 | + const ILVerifier* verifier, |
| 81 | + const BinaryNinja::MediumLevelILInstruction& instr, |
| 82 | + const std::string& message |
| 83 | + ) |
| 84 | + { |
| 85 | + Diagnostic d; |
| 86 | + d.severity = severity; |
| 87 | + d.ilType = verifier->m_ilType; |
| 88 | + d.exprIndex = instr.exprIndex; |
| 89 | + d.instrIndex = instr.instructionIndex; |
| 90 | + d.message = fmt::format("{:?} {}", instr, message); |
| 91 | + return d; |
| 92 | + } |
| 93 | + static Diagnostic Error( |
| 94 | + const ILVerifier* verifier, |
| 95 | + const BinaryNinja::MediumLevelILInstruction& instr, |
| 96 | + const std::string& message |
| 97 | + ) |
| 98 | + { |
| 99 | + Diagnostic d; |
| 100 | + d.severity = ErrorSeverity; |
| 101 | + d.ilType = verifier->m_ilType; |
| 102 | + d.exprIndex = instr.exprIndex; |
| 103 | + d.instrIndex = instr.instructionIndex; |
| 104 | + d.message = fmt::format("{:?} {}", instr, message); |
| 105 | + return d; |
| 106 | + } |
| 107 | + static Diagnostic Diag( |
| 108 | + BNTypeParserErrorSeverity severity, |
| 109 | + const ILVerifier* verifier, |
| 110 | + const BinaryNinja::HighLevelILInstruction& instr, |
| 111 | + const std::string& message |
| 112 | + ) |
| 113 | + { |
| 114 | + Diagnostic d; |
| 115 | + d.severity = severity; |
| 116 | + d.ilType = verifier->m_ilType; |
| 117 | + d.exprIndex = instr.exprIndex; |
| 118 | + d.instrIndex = instr.instructionIndex; |
| 119 | + d.message = fmt::format("{:?} {}", instr, message); |
| 120 | + return d; |
| 121 | + } |
| 122 | + static Diagnostic Error( |
| 123 | + const ILVerifier* verifier, |
| 124 | + const BinaryNinja::HighLevelILInstruction& instr, |
| 125 | + const std::string& message |
| 126 | + ) |
| 127 | + { |
| 128 | + Diagnostic d; |
| 129 | + d.severity = ErrorSeverity; |
| 130 | + d.ilType = verifier->m_ilType; |
| 131 | + d.exprIndex = instr.exprIndex; |
| 132 | + d.instrIndex = instr.instructionIndex; |
| 133 | + d.message = fmt::format("{:?} {}", instr, message); |
| 134 | + return d; |
| 135 | + } |
| 136 | + }; |
| 137 | + |
| 138 | +protected: |
| 139 | + std::vector<Diagnostic> m_diagnostics; |
| 140 | + |
| 141 | +public: |
| 142 | + explicit ILVerifier(BNFunctionGraphType graphType); |
| 143 | + virtual ~ILVerifier() = default; |
| 144 | + virtual void Verify() = 0; |
| 145 | + |
| 146 | + std::vector<Diagnostic>&& GetDiagnostics() { return std::move(m_diagnostics); } |
| 147 | +}; |
0 commit comments