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

Simon Tatham via llvm-commits llvm-commits at lists.llvm.org
Thu Jun 26 05:04:45 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);
----------------
statham-arm wrote:

I don't understand this function at all, I'm afraid. It looks as if you first make `Row` by copying the last entry already in `Table` (if any); then you parse it into some more rows that you append to `Table`; and then you push `Row` on to the end of `Table` _again_. Wasn't it in `Table` already? Isn't that where you got it from in the first place?

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


More information about the llvm-commits mailing list