LLVM 22.0.0git
DWARFCFIAnalysis.h
Go to the documentation of this file.
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"
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"
38#include "llvm/MC/MCStreamer.h"
42
43namespace 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.
77public:
78 LLVM_ABI DWARFCFIAnalysis(MCContext *Context, MCInstrInfo const &MCII,
79 bool IsEH, ArrayRef<MCCFIInstruction> Prologue);
80
81 LLVM_ABI void update(const MCInst &Inst,
83
84private:
85 void checkRegDiff(const MCInst &Inst, DWARFRegNum Reg,
86 const dwarf::UnwindRow &PrevRow,
87 const dwarf::UnwindRow &NextRow,
88 const SmallSet<DWARFRegNum, 4> &Reads,
89 const SmallSet<DWARFRegNum, 4> &Writes);
90
91 void checkCFADiff(const MCInst &Inst, const dwarf::UnwindRow &PrevRow,
92 const dwarf::UnwindRow &NextRow,
93 const SmallSet<DWARFRegNum, 4> &Reads,
94 const SmallSet<DWARFRegNum, 4> &Writes);
95
96private:
97 DWARFCFIState State;
98 MCContext *Context;
99 MCInstrInfo const &MCII;
100 MCRegisterInfo const *MCRI;
101 bool IsEH;
102};
103
104} // namespace llvm
105
106#endif
#define LLVM_ABI
Definition Compiler.h:213
This file declares DWARFCFIState class.
Register Reg
This file defines the SmallSet class.
This file defines the SmallVector class.
ArrayRef - Represent a constant reference to an array (0 or more elements consecutively in memory),...
Definition ArrayRef.h:41
LLVM_ABI void update(const MCInst &Inst, ArrayRef< MCCFIInstruction > Directives)
LLVM_ABI DWARFCFIAnalysis(MCContext *Context, MCInstrInfo const &MCII, bool IsEH, ArrayRef< MCCFIInstruction > Prologue)
This class is used to maintain a CFI state, referred to as an unwinding row, during CFI analysis.
Context object for machine code objects.
Definition MCContext.h:83
Instances of this class represent a single low-level machine instruction.
Definition MCInst.h:188
Interface to description of machine instruction set.
Definition MCInstrInfo.h:27
MCRegisterInfo base class - We assume that the target defines a static array of MCRegisterDesc object...
SmallSet - This maintains a set of unique values, optimizing for the case when the set is small (less...
Definition SmallSet.h:133
A class that represents a single row in the unwind table that is decoded by parsing the DWARF Call Fr...
This is an optimization pass for GlobalISel generic memory operations.
uint32_t DWARFRegNum