| 1 | //===----------------------------------------------------------------------===// |
| 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 | /// \file |
| 10 | /// This file declares `DWARFCFIAnalysis` class. |
| 11 | /// `DWARFCFIAnalysis` is a minimal implementation of a DWARF CFI checker |
| 12 | /// described in this link: |
| 13 | /// https://discourse.llvm.org/t/rfc-dwarf-cfi-validation/86936 |
| 14 | /// |
| 15 | /// The goal of the checker is to validate DWARF CFI directives using the |
| 16 | /// prologue directives and the machine instructions. The main proposed |
| 17 | /// algorithm validates the directives by comparing the CFI state in each |
| 18 | /// instruction with the state achieved by abstract execution of the instruction |
| 19 | /// on the CFI state. However, the current version implemented here is a simple |
| 20 | /// conditional check based on the registers modified by each instruction. |
| 21 | /// |
| 22 | //===----------------------------------------------------------------------===// |
| 23 | |
| 24 | #ifndef LLVM_DWARFCFICHECKER_DWARFCFIANALYSIS_H |
| 25 | #define LLVM_DWARFCFICHECKER_DWARFCFIANALYSIS_H |
| 26 | |
| 27 | #include "DWARFCFIState.h" |
| 28 | #include "llvm/ADT/ArrayRef.h" |
| 29 | #include "llvm/ADT/SmallSet.h" |
| 30 | #include "llvm/ADT/SmallVector.h" |
| 31 | #include "llvm/DebugInfo/DWARF/LowLevel/DWARFUnwindTable.h" |
| 32 | #include "llvm/MC/MCContext.h" |
| 33 | #include "llvm/MC/MCDwarf.h" |
| 34 | #include "llvm/MC/MCExpr.h" |
| 35 | #include "llvm/MC/MCInst.h" |
| 36 | #include "llvm/MC/MCInstrInfo.h" |
| 37 | #include "llvm/MC/MCRegisterInfo.h" |
| 38 | #include "llvm/MC/MCStreamer.h" |
| 39 | #include "llvm/MC/MCSubtargetInfo.h" |
| 40 | #include "llvm/MC/TargetRegistry.h" |
| 41 | #include "llvm/Support/Compiler.h" |
| 42 | |
| 43 | namespace llvm { |
| 44 | |
| 45 | /// `DWARFCFIAnalysis` validates the DWARF Call Frame Information one machine |
| 46 | /// instruction at a time. This class maintains an internal CFI state |
| 47 | /// initialized with the prologue directives and updated with each instruction's |
| 48 | /// associated directives. In each update, it checks if the machine |
| 49 | /// instruction changes the CFI state in a way that matches the changes |
| 50 | /// from the CFI directives. This checking may results in errors and warnings. |
| 51 | /// |
| 52 | /// In current stage, the analysis is only aware of what registers the |
| 53 | /// instruction modifies. If the modification is happening to a sub-register, |
| 54 | /// the analysis considers the super-register is modified. |
| 55 | /// |
| 56 | /// In each update, for each register (or CFA), the following cases can happen: |
| 57 | /// 1. The unwinding rule is not changed: |
| 58 | /// a. The registers involved in this rule are not modified: the analysis |
| 59 | /// proceeds without emitting error or warning. |
| 60 | /// b. The registers involved in this rule are modified: it emits an error. |
| 61 | /// 2. The unwinding rule is changed: |
| 62 | /// a. The rule is structurally modified (i.e., the location is changed): It |
| 63 | /// emits a warning. |
| 64 | /// b. The rule is structurally the same, but the register set is changed: it |
| 65 | /// emits a warning. |
| 66 | /// c. The rule is structurally the same, using the same set of registers, but |
| 67 | /// the offset is changed: |
| 68 | /// i. If the registers included in the rule are modified as well: It |
| 69 | /// emits a warning. |
| 70 | /// ii. If the registers included in the rule are not modified: It emits an |
| 71 | /// error. |
| 72 | /// |
| 73 | /// The analysis only checks the CFA unwinding rule when the rule is a register |
| 74 | /// plus some offset. Therefore, for CFA, only cases 1, 2.b, and 2.c are |
| 75 | /// checked, and in all other case(s), a warning is emitted. |
| 76 | class DWARFCFIAnalysis { |
| 77 | public: |
| 78 | LLVM_ABI DWARFCFIAnalysis(MCContext *Context, MCInstrInfo const &MCII, |
| 79 | bool IsEH, ArrayRef<MCCFIInstruction> Prologue); |
| 80 | |
| 81 | LLVM_ABI void update(const MCInst &Inst, |
| 82 | ArrayRef<MCCFIInstruction> Directives); |
| 83 | |
| 84 | private: |
| 85 | void checkRegDiff(const MCInst &Inst, DWARFRegNum Reg, |
| 86 | const dwarf::UnwindRow &PrevRow, |
| 87 | const dwarf::UnwindRow &NextRow, |
| 88 | const SmallSet<DWARFRegNum, 4> &Writes); |
| 89 | |
| 90 | void checkCFADiff(const MCInst &Inst, const dwarf::UnwindRow &PrevRow, |
| 91 | const dwarf::UnwindRow &NextRow, |
| 92 | const SmallSet<DWARFRegNum, 4> &Writes); |
| 93 | |
| 94 | private: |
| 95 | DWARFCFIState State; |
| 96 | MCContext *Context; |
| 97 | MCInstrInfo const &MCII; |
| 98 | MCRegisterInfo const *MCRI; |
| 99 | bool IsEH; |
| 100 | }; |
| 101 | |
| 102 | } // namespace llvm |
| 103 | |
| 104 | #endif |
| 105 | |