[libunwind] Read a saved condition register as a 32-bit word (PR #238317)
Piotr Kubaj via cfe-commits
cfe-commits at lists.llvm.org
Tue Nov 3 05:51:29 PST 2026
https://github.com/pkubaj updated https://github.com/llvm/llvm-project/pull/238317
>From feadf3572e36ab26719e6c72276b0f283d7b948f Mon Sep 17 00:00:00 2001
From: Piotr Kubaj <pkubaj at FreeBSD.org>
Date: Tue, 3 Nov 2026 10:21:49 +0100
Subject: [PATCH] Read a saved condition register as a 32-bit word
The ELFv2 ABI saves CR as a 32-bit word at offset 8 of the caller's
frame (stw), and the CFI describes the save as DW_CFA_offset cr2 at
CFA+8 (likewise cr3 and cr4). stepWithDwarf restored it through
getSavedRegister(), i.e. with a 64-bit load, and
Registers_ppc64::setRegister() then masked the field out of the low 32
bits of that value. On big-endian targets the saved word is the upper
half of the load, so cr2-cr4 were restored from the reserved word at
offset 12 instead: stale stack contents.
Every landing pad reached by unwinding through a frame that saved CR
therefore resumed with corrupted cr2-cr4, and a caller that keeps a
comparison result in cr2 across a call which throws and catches
internally takes the wrong branch afterwards. Inkscape 1.4 on FreeBSD
powerpc64 lost the "not cloned" flag in SPObject::invoke_build() when
sp_version_from_string() caught the std::ios_base::failure thrown by
libc++'s istream sentry for an SVG version="1" attribute; the root
object was never registered and the document crashed on close.
Read the slot as a 32-bit word for the CR registers. Little-endian
targets already read the right half and are unaffected.
Assisted-by: Claude Fable 5.1
---
libunwind/src/DwarfInstructions.hpp | 11 ++++
libunwind/test/ppc64_cr_registers.pass.cpp | 70 ++++++++++++++++++++++
2 files changed, 81 insertions(+)
create mode 100644 libunwind/test/ppc64_cr_registers.pass.cpp
diff --git a/libunwind/src/DwarfInstructions.hpp b/libunwind/src/DwarfInstructions.hpp
index 597024950f47b5..38a13c00e6b5a2 100644
--- a/libunwind/src/DwarfInstructions.hpp
+++ b/libunwind/src/DwarfInstructions.hpp
@@ -304,6 +304,17 @@ int DwarfInstructions<A, R>::stepWithDwarf(
else if (i == (int)cieInfo.returnAddressRegister)
returnAddress = getSavedRegister(addressSpace, registers, cfa,
prolog.savedRegisters[i]);
+#if defined(_LIBUNWIND_TARGET_PPC64)
+ else if (i >= UNW_PPC64_CR0 && i <= UNW_PPC64_CR7 &&
+ prolog.savedRegisters[i].location ==
+ CFI_Parser<A>::kRegisterInCFA)
+ // The condition register is saved as a 32-bit word (stw), so it
+ // has to be read back as one: a 64-bit load returns the saved
+ // word in the upper half on big-endian targets.
+ newRegisters.setRegister(
+ i, addressSpace.get32(cfa +
+ (pint_t)prolog.savedRegisters[i].value));
+#endif
else if (registers.validRegister(i))
newRegisters.setRegister(
i, getSavedRegister(addressSpace, registers, cfa,
diff --git a/libunwind/test/ppc64_cr_registers.pass.cpp b/libunwind/test/ppc64_cr_registers.pass.cpp
new file mode 100644
index 00000000000000..2acbddf80bb90b
--- /dev/null
+++ b/libunwind/test/ppc64_cr_registers.pass.cpp
@@ -0,0 +1,70 @@
+//===----------------------------------------------------------------------===//
+//
+// 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
+//
+//===----------------------------------------------------------------------===//
+
+// REQUIRES: target={{powerpc64(le)?-.+}}
+
+// Check that stepping out of a frame that saved the condition register
+// restores the caller's cr2 field. The ELFv2 ABI saves CR as a 32-bit word at
+// offset 8 of the caller's stack frame (DW_CFA_offset cr2 at CFA+8); reading
+// that slot as a 64-bit register returned the saved word in the upper half on
+// big-endian targets, so cr2-cr4 of the caller were restored from stale stack
+// bytes.
+
+#include <libunwind.h>
+#include <stdint.h>
+#include <stdlib.h>
+
+#define CR2_MASK 0x00F00000u
+
+static unw_word_t expected_cr2;
+
+__attribute__((noinline)) static void check_caller_cr2() {
+ unw_context_t context;
+ unw_cursor_t cursor;
+ unw_word_t cr2;
+
+ unw_getcontext(&context);
+ unw_init_local(&cursor, &context);
+
+ // Step out of this frame into middle(), then out of middle() into outer().
+ // The second step restores cr2 from the slot middle() saved it in.
+ if (unw_step(&cursor) <= 0)
+ abort();
+ if (unw_step(&cursor) <= 0)
+ abort();
+ if (unw_get_reg(&cursor, UNW_PPC64_CR2, &cr2) != UNW_ESUCCESS)
+ abort();
+ if ((cr2 & CR2_MASK) != expected_cr2)
+ abort();
+}
+
+__attribute__((noinline)) static void middle(long a, long b) {
+ // Clobbering the callee-saved cr2 forces this frame to save CR in its
+ // prologue and describe the save slot in its CFI.
+ __asm__ volatile("cmpd cr2, %0, %1" : : "r"(a), "r"(b) : "cr2");
+ check_caller_cr2();
+}
+
+__attribute__((noinline)) static void outer(long a, long b) {
+ uint32_t cr;
+ // Put a known value into cr2 right before the call; the ABI requires
+ // middle() to preserve it.
+ __asm__ volatile("cmpd cr2, %1, %2\n\tmfcr %0"
+ : "=r"(cr)
+ : "r"(a), "r"(b)
+ : "cr2");
+ expected_cr2 = cr & CR2_MASK;
+ middle(b, a);
+}
+
+int main(int, char **) {
+ outer(1, 2); // cr2 = LT
+ outer(2, 1); // cr2 = GT
+ outer(3, 3); // cr2 = EQ
+ return 0;
+}
More information about the cfe-commits
mailing list