llvmorg-github-actions[bot] wrote:
<!--LLVM PR SUMMARY COMMENT--> @llvm/pr-subscribers-libunwind Author: Igor Shevlyakov (ishevlyakov) <details> <summary>Changes</summary> `PrologInfo::cfaRegister` used 0 to mean "the CFA is not register-relative" (not yet defined, or defined by `DW_CFA_def_cfa_expression`), and `getCFA()` only used the register rule when `cfaRegister != 0`. But 0 is a valid DWARF register number on every target: `%rax` on x86-64, `x0` on AArch64, `x0` on RISC-V. A frame whose CFA is defined relative to register 0, e.g. ``` .cfi_def_cfa %rax, 0 ``` therefore fell through to the expression path with no expression. That hits the `getCFA(): unknown location` assertion, or is undefined behavior when assertions are disabled. Use a dedicated `kNoRegister` sentinel (`~0U`) instead. It is set when `PrologInfo` is initialized and by `DW_CFA_def_cfa_expression`, and `getCFA()` tests against it. The new test `cfa_register_zero.pass.cpp` (x86-64) defines the CFA via `%rax` in a naked function, captures a context inside that frame, and checks that `unw_step` reaches the caller with the expected SP and IP. Without the fix it aborts with the assertion above. `check-unwind` passes locally on x86_64-linux. 🤖 Generated with [Claude Code](https://claude.com/claude-code) --- Full diff: https://github.com/llvm/llvm-project/pull/244979.diff 3 Files Affected: - (modified) libunwind/src/DwarfInstructions.hpp (+1-1) - (modified) libunwind/src/DwarfParser.hpp (+9-2) - (added) libunwind/test/cfa_register_zero.pass.cpp (+61) ``````````diff diff --git a/libunwind/src/DwarfInstructions.hpp b/libunwind/src/DwarfInstructions.hpp index 597024950f47b5..0454a1c511efee 100644 --- a/libunwind/src/DwarfInstructions.hpp +++ b/libunwind/src/DwarfInstructions.hpp @@ -65,7 +65,7 @@ class DwarfInstructions { static pint_t getCFA(A &addressSpace, const PrologInfo &prolog, const R ®isters) { - if (prolog.cfaRegister != 0) { + if (prolog.cfaRegister != PrologInfo::kNoRegister) { uintptr_t cfaRegister = registers.getRegister((int)prolog.cfaRegister); return (pint_t)(cfaRegister + prolog.cfaRegisterOffset); } diff --git a/libunwind/src/DwarfParser.hpp b/libunwind/src/DwarfParser.hpp index cb276ab3cfdf43..dda6e9623a5bc8 100644 --- a/libunwind/src/DwarfParser.hpp +++ b/libunwind/src/DwarfParser.hpp @@ -93,6 +93,11 @@ class CFI_Parser { /// Information about a frame layout and registers saved determined /// by "running" the DWARF FDE "instructions" struct PrologInfo { + // Value of cfaRegister when the CFA is not register-relative (undefined or + // given by a DW_CFA_def_cfa_expression). It cannot be 0, since 0 is a valid + // DWARF register number on every target (e.g. %rax on x86-64, x0 on + // AArch64). + static const uint32_t kNoRegister = ~0U; uint32_t cfaRegister; int32_t cfaRegisterOffset; // CFA = (cfaRegister)+cfaRegisterOffset int64_t cfaExpression; // CFA = expression @@ -105,8 +110,10 @@ class CFI_Parser { // When saving registers, this data structure is lazily initialized. PrologInfo(InitializeTime IT = InitializeTime::kNormal) { - if (IT == InitializeTime::kNormal) + if (IT == InitializeTime::kNormal) { memset(this, 0, sizeof(*this)); + cfaRegister = kNoRegister; + } } void checkSaveRegister(uint64_t reg, PrologInfo &initialState) { if (!savedRegisters[reg].initialStateSaved) { @@ -660,7 +667,7 @@ bool CFI_Parser<A>::parseFDEInstructions( results->cfaRegisterOffset); break; case DW_CFA_def_cfa_expression: - results->cfaRegister = 0; + results->cfaRegister = PrologInfo::kNoRegister; results->cfaExpression = (int64_t)p; length = addressSpace.getULEB128(p, instructionsEnd); assert(length < static_cast<pint_t>(~0) && "pointer overflow"); diff --git a/libunwind/test/cfa_register_zero.pass.cpp b/libunwind/test/cfa_register_zero.pass.cpp new file mode 100644 index 00000000000000..f6d64e27229469 --- /dev/null +++ b/libunwind/test/cfa_register_zero.pass.cpp @@ -0,0 +1,61 @@ +// -*- C++ -*- +//===----------------------------------------------------------------------===// +// +// 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 +// +//===----------------------------------------------------------------------===// + +// Ensure that a frame whose CFA is defined relative to DWARF register 0 +// (%rax on x86-64) unwinds correctly. Register number 0 must not be mistaken +// for "no CFA register", which would make the unwinder evaluate a null CFA +// expression instead. + +// REQUIRES: target={{x86_64-.+}} +// UNSUPPORTED: target={{.*-windows.*}} +// UNSUPPORTED: target={{.*-apple.*}} + +// Inline assembly is incompatible with MSAN. +// UNSUPPORTED: msan + +#undef NDEBUG +#include <assert.h> +#include <libunwind.h> +#include <stdint.h> + +// Captured inside cfa_in_reg0, at a point where %rax holds the CFA. +extern "C" unw_context_t reg0_context; +unw_context_t reg0_context; + +extern "C" void check_step() { + uint64_t cfa = reinterpret_cast<uint64_t *>(®0_context)[0]; // saved %rax + + unw_cursor_t cursor; + assert(unw_init_local(&cursor, ®0_context) == UNW_ESUCCESS); + assert(unw_step(&cursor) > 0); + + unw_word_t sp, ip; + assert(unw_get_reg(&cursor, UNW_REG_SP, &sp) == UNW_ESUCCESS); + assert(unw_get_reg(&cursor, UNW_REG_IP, &ip) == UNW_ESUCCESS); + assert(sp == cfa); + assert(ip == *reinterpret_cast<uint64_t *>(cfa - 8)); +} + +__attribute__((naked)) void cfa_in_reg0() { + __asm__("subq $8, %rsp\n" + ".cfi_def_cfa_offset 16\n" + "leaq 16(%rsp), %rax\n" + ".cfi_def_cfa %rax, 0\n" + "leaq reg0_context(%rip), %rdi\n" + "callq unw_getcontext@PLT\n" + "callq check_step\n" + "addq $8, %rsp\n" + ".cfi_def_cfa %rsp, 8\n" + "retq\n"); +} + +int main(int, char **) { + cfa_in_reg0(); + return 0; +} `````````` </details> https://github.com/llvm/llvm-project/pull/244979 _______________________________________________ cfe-commits mailing list [email protected] https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
