Skip to content

Commit 22b615a

Browse files
[libunwind] Support for leaf function unwinding.
Unwinding leaf function is useful in cases when the backtrace finds a leaf function for example when it caused a signal. This patch also add the support for the DW_CFA_undefined because it marks the end of the frames. Ryan Prichard provided code for the tests. Reviewed By: #libunwind, mstorsjo Differential Revision: https://reviews.llvm.org/D83573 Reland with limit the test to the x86_64-linux target.
1 parent 2d35092 commit 22b615a

File tree

5 files changed

+110
-2
lines changed

5 files changed

+110
-2
lines changed

libunwind/src/DwarfInstructions.hpp

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,8 @@ typename A::pint_t DwarfInstructions<A, R>::getSavedRegister(
9393

9494
case CFI_Parser<A>::kRegisterInRegister:
9595
return registers.getRegister((int)savedReg.value);
96-
96+
case CFI_Parser<A>::kRegisterUndefined:
97+
return 0;
9798
case CFI_Parser<A>::kRegisterUnused:
9899
case CFI_Parser<A>::kRegisterOffsetFromCFA:
99100
// FIX ME
@@ -117,6 +118,7 @@ double DwarfInstructions<A, R>::getSavedFloatRegister(
117118

118119
case CFI_Parser<A>::kRegisterIsExpression:
119120
case CFI_Parser<A>::kRegisterUnused:
121+
case CFI_Parser<A>::kRegisterUndefined:
120122
case CFI_Parser<A>::kRegisterOffsetFromCFA:
121123
case CFI_Parser<A>::kRegisterInRegister:
122124
// FIX ME
@@ -140,6 +142,7 @@ v128 DwarfInstructions<A, R>::getSavedVectorRegister(
140142

141143
case CFI_Parser<A>::kRegisterIsExpression:
142144
case CFI_Parser<A>::kRegisterUnused:
145+
case CFI_Parser<A>::kRegisterUndefined:
143146
case CFI_Parser<A>::kRegisterOffsetFromCFA:
144147
case CFI_Parser<A>::kRegisterInRegister:
145148
// FIX ME
@@ -190,6 +193,10 @@ int DwarfInstructions<A, R>::stepWithDwarf(A &addressSpace, pint_t pc,
190193
prolog.savedRegisters[i]));
191194
else
192195
return UNW_EBADREG;
196+
} else if (i == (int)cieInfo.returnAddressRegister) {
197+
// Leaf function keeps the return address in register and there is no
198+
// explicit intructions how to restore it.
199+
returnAddress = registers.getRegister(cieInfo.returnAddressRegister);
193200
}
194201
}
195202

libunwind/src/DwarfParser.hpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,7 @@ class CFI_Parser {
6969
};
7070
enum RegisterSavedWhere {
7171
kRegisterUnused,
72+
kRegisterUndefined,
7273
kRegisterInCFA,
7374
kRegisterOffsetFromCFA,
7475
kRegisterInRegister,
@@ -505,7 +506,7 @@ bool CFI_Parser<A>::parseInstructions(A &addressSpace, pint_t instructions,
505506
"malformed DW_CFA_undefined DWARF unwind, reg too big");
506507
return false;
507508
}
508-
results->setRegisterLocation(reg, kRegisterUnused, initialState);
509+
results->setRegisterLocation(reg, kRegisterUndefined, initialState);
509510
_LIBUNWIND_TRACE_DWARF("DW_CFA_undefined(reg=%" PRIu64 ")\n", reg);
510511
break;
511512
case DW_CFA_same_value:

libunwind/test/lit.site.cfg.in

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,10 @@ config.test_source_root = os.path.join(config.libunwind_src_root, 'test')
4444
# Allow expanding substitutions that are based on other substitutions
4545
config.recursiveExpansionLimit = 10
4646

47+
# Make symbols available in the tests.
48+
config.test_compiler_flags += " -funwind-tables "
49+
config.test_linker_flags += " -Wl,--export-dynamic "
50+
4751
# Infer the test_exec_root from the build directory.
4852
config.test_exec_root = os.path.join(config.libunwind_obj_root, 'test')
4953

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
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+
// Ensure that the unwinder can cope with the signal handler.
11+
// REQUIRES: x86_64-linux
12+
13+
#include <assert.h>
14+
#include <dlfcn.h>
15+
#include <signal.h>
16+
#include <stdio.h>
17+
#include <stdlib.h>
18+
#include <string.h>
19+
#include <sys/types.h>
20+
#include <unistd.h>
21+
#include <unwind.h>
22+
23+
_Unwind_Reason_Code frame_handler(struct _Unwind_Context* ctx, void* arg) {
24+
(void)arg;
25+
Dl_info info = { 0, 0, 0, 0 };
26+
assert(dladdr((void*)_Unwind_GetIP(ctx), &info));
27+
28+
// Unwind util the main is reached, above frames deeped on the platfrom and architecture.
29+
if(info.dli_sname && !strcmp("main", info.dli_sname)) {
30+
_Exit(0);
31+
}
32+
return _URC_NO_REASON;
33+
}
34+
35+
void signal_handler(int signum) {
36+
(void)signum;
37+
_Unwind_Backtrace(frame_handler, NULL);
38+
_Exit(-1);
39+
}
40+
41+
int main() {
42+
signal(SIGUSR1, signal_handler);
43+
kill(getpid(), SIGUSR1);
44+
return -2;
45+
}
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
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+
// Ensure that leaf function can be unwund.
11+
// REQUIRES: x86_64-linux
12+
13+
#include <assert.h>
14+
#include <dlfcn.h>
15+
#include <signal.h>
16+
#include <stdio.h>
17+
#include <stdlib.h>
18+
#include <string.h>
19+
#include <sys/types.h>
20+
#include <unistd.h>
21+
#include <unwind.h>
22+
23+
_Unwind_Reason_Code frame_handler(struct _Unwind_Context* ctx, void* arg) {
24+
(void)arg;
25+
Dl_info info = { 0, 0, 0, 0 };
26+
assert(dladdr((void*)_Unwind_GetIP(ctx), &info));
27+
28+
// Unwind util the main is reached, above frames deeped on the platfrom and architecture.
29+
if(info.dli_sname && !strcmp("main", info.dli_sname)) {
30+
_Exit(0);
31+
}
32+
return _URC_NO_REASON;
33+
}
34+
35+
void signal_handler(int signum) {
36+
(void)signum;
37+
_Unwind_Backtrace(frame_handler, NULL);
38+
_Exit(-1);
39+
}
40+
41+
int* faultyPointer = NULL;
42+
43+
__attribute__((noinline)) void crashing_leaf_func(void) {
44+
*faultyPointer = 0;
45+
}
46+
47+
int main() {
48+
signal(SIGSEGV, signal_handler);
49+
crashing_leaf_func();
50+
return -2;
51+
}

0 commit comments

Comments
 (0)