[llvm] Minimal unwinding information (DWARF CFI) checker (PR #145633)

AmirHossein PashaeeHir via llvm-commits llvm-commits at lists.llvm.org
Thu Jun 26 11:02:09 PDT 2025


================
@@ -0,0 +1,144 @@
+//===----------------------------------------------------------------------===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+
+#include "llvm/UnwindInfoChecker/UnwindInfoState.h"
+#include "llvm/BinaryFormat/Dwarf.h"
+#include "llvm/DebugInfo/DWARF/DWARFDebugFrame.h"
+#include "llvm/MC/MCDwarf.h"
+#include "llvm/Support/Error.h"
+#include "llvm/Support/ErrorHandling.h"
+#include "llvm/Support/FormatVariadic.h"
+#include <cassert>
+#include <optional>
+
+using namespace llvm;
+
+std::optional<dwarf::UnwindTable::const_iterator>
+DWARFCFIState::getCurrentUnwindRow() const {
+  if (!Table.size())
+    return std::nullopt;
+
+  return --Table.end();
+}
+
+void DWARFCFIState::update(const MCCFIInstruction &Directive) {
+  auto MaybeCFIP = convert(Directive);
+  if (!MaybeCFIP) {
+    Context->reportError(
+        Directive.getLoc(),
+        "couldn't apply this directive to the unwinding information state");
+  }
+  auto CFIP = *MaybeCFIP;
+
+  auto MaybeCurrentRow = getCurrentUnwindRow();
+  dwarf::UnwindRow Row = MaybeCurrentRow.has_value()
+                             ? *(MaybeCurrentRow.value())
+                             : dwarf::UnwindRow();
+  dwarf::UnwindTable::RowContainer NewRows;
+  if (Error Err = parseRows(CFIP, Row, nullptr).moveInto(NewRows)) {
+    Context->reportError(
+        Directive.getLoc(),
+        formatv("could not parse this CFI directive due to: {0}",
+                toString(std::move(Err))));
+
+    // Proceed the analysis by ignoring this CFI directive.
+    return;
+  }
+  Table.insert(Table.end(), NewRows.begin(), NewRows.end());
+  Table.push_back(Row);
+}
+
+std::optional<dwarf::CFIProgram>
+DWARFCFIState::convert(MCCFIInstruction Directive) {
----------------
amsen20 wrote:

Fixed. 

Only two operations don't add an instruction:
- Expressions are ignored for now.
- OpLabel should be implemented, for now, this instruction should not be reached by the tool.

https://github.com/llvm/llvm-project/pull/145633


More information about the llvm-commits mailing list