Skip to content

Commit e57f0e9

Browse files
authored
[libunwind] fix pc range condition check bug (#154902)
There is an off-by-one error with current condition check for PC fallen into the range or not. There is another check within libunwind that use the correct checks in https://github.com/llvm/llvm-project/blob/5050da7ba18fc876f80fbeaaca3564d3b4483bb8/libunwind/src/UnwindCursor.hpp#L2757 ``` if ((fdeInfo.pcStart <= pc) && (pc < fdeInfo.pcEnd)) ```
1 parent 2e12299 commit e57f0e9

File tree

2 files changed

+59
-1
lines changed

2 files changed

+59
-1
lines changed

libunwind/src/DwarfParser.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -273,7 +273,7 @@ bool CFI_Parser<A>::findFDE(A &addressSpace, pint_t pc, pint_t ehSectionStart,
273273
pint_t pcRange = addressSpace.getEncodedP(
274274
p, nextCFI, cieInfo->pointerEncoding & 0x0F);
275275
// Test if pc is within the function this FDE covers.
276-
if ((pcStart < pc) && (pc <= pcStart + pcRange)) {
276+
if ((pcStart <= pc) && (pc < pcStart + pcRange)) {
277277
// parse rest of info
278278
fdeInfo->lsda = 0;
279279
// check for augmentation length
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
// -*- C++ -*-
2+
//===----------------------------------------------------------------------===//
3+
//
4+
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
5+
// See https://llvm.org/LICENSE.txt for license information.
6+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7+
//
8+
//===----------------------------------------------------------------------===//
9+
10+
// Manually marking the .eh_frame_hdr as DW_EH_PE_omit to make libunwind to do
11+
// the linear search.
12+
// Assuming the begining of the function is at the start of the FDE range.
13+
14+
// clang-format off
15+
16+
// REQUIRES: linux
17+
18+
// TODO: Figure out why this fails with Memory Sanitizer.
19+
// XFAIL: msan
20+
21+
// RUN: %{build}
22+
// RUN: objcopy --dump-section .eh_frame_hdr=%t_ehf_hdr.bin %t.exe
23+
// RUN: echo -ne '\xFF' | dd of=%t_ehf_hdr.bin bs=1 seek=2 count=2 conv=notrunc status=none
24+
// RUN: objcopy --update-section .eh_frame_hdr=%t_ehf_hdr.bin %t.exe
25+
// RUN: %{exec} %t.exe
26+
27+
// clang-format on
28+
29+
#include <assert.h>
30+
#include <libunwind.h>
31+
#include <stdint.h>
32+
#include <stdio.h>
33+
#include <unwind.h>
34+
35+
void f() {
36+
printf("123\n");
37+
void *pc = __builtin_return_address(0);
38+
void *fpc = (void *)&f;
39+
void *fpc1 = (void *)((uintptr_t)fpc + 1);
40+
41+
struct dwarf_eh_bases bases;
42+
const void *fde_pc = _Unwind_Find_FDE(pc, &bases);
43+
const void *fde_fpc = _Unwind_Find_FDE(fpc, &bases);
44+
const void *fde_fpc1 = _Unwind_Find_FDE(fpc1, &bases);
45+
printf("fde_pc = %p\n", fde_pc);
46+
printf("fde_fpc = %p\n", fde_fpc);
47+
printf("fde_fpc1 = %p\n", fde_fpc1);
48+
fflush(stdout);
49+
assert(fde_pc != NULL);
50+
assert(fde_fpc != NULL);
51+
assert(fde_fpc1 != NULL);
52+
assert(fde_fpc == fde_fpc1);
53+
}
54+
55+
int main() {
56+
f();
57+
return 0;
58+
}

0 commit comments

Comments
 (0)