Skip to content

Commit 4246847

Browse files
authored
Merge pull request #8260 from al45tair/eng/PR-123436891
[libunwind] Tweak tests for musl support.
2 parents 5c7a266 + f95551e commit 4246847

File tree

4 files changed

+62
-27
lines changed

4 files changed

+62
-27
lines changed

libunwind/test/floatregister.pass.cpp

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,20 +11,26 @@
1111

1212
// Basic test for float registers number are accepted.
1313

14-
#include <dlfcn.h>
1514
#include <stdlib.h>
1615
#include <string.h>
1716
#include <unwind.h>
1817

18+
// Using __attribute__((section("main_func"))) is Linux specific, but then
19+
// this entire test is marked as requiring Linux, so we should be good.
20+
//
21+
// We don't use dladdr() because on musl it's a no-op when statically linked.
22+
extern char __start_main_func;
23+
extern char __stop_main_func;
24+
1925
_Unwind_Reason_Code frame_handler(struct _Unwind_Context *ctx, void *arg) {
2026
(void)arg;
21-
Dl_info info = {0, 0, 0, 0};
2227

23-
// Unwind util the main is reached, above frames depend on the platform and
28+
// Unwind until the main is reached, above frames depend on the platform and
2429
// architecture.
25-
if (dladdr(reinterpret_cast<void *>(_Unwind_GetIP(ctx)), &info) &&
26-
info.dli_sname && !strcmp("main", info.dli_sname))
30+
uintptr_t ip = _Unwind_GetIP(ctx);
31+
if (ip >= (uintptr_t)&__start_main_func && ip < (uintptr_t)&__stop_main_func) {
2732
_Exit(0);
33+
}
2834

2935
return _URC_NO_REASON;
3036
}
@@ -45,7 +51,7 @@ __attribute__((noinline)) void foo() {
4551
_Unwind_Backtrace(frame_handler, NULL);
4652
}
4753

48-
int main() {
54+
__attribute__((section("main_func"))) int main() {
4955
foo();
5056
return -2;
5157
}

libunwind/test/forceunwind.pass.cpp

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@
1717

1818
#undef NDEBUG
1919
#include <assert.h>
20-
#include <dlfcn.h>
2120
#include <signal.h>
2221
#include <stdint.h>
2322
#include <stdio.h>
@@ -27,6 +26,13 @@
2726
#include <unistd.h>
2827
#include <unwind.h>
2928

29+
// Using __attribute__((section("main_func"))) is Linux specific, but then
30+
// this entire test is marked as requiring Linux, so we should be good.
31+
//
32+
// We don't use dladdr() because on musl it's a no-op when statically linked.
33+
extern char __start_main_func;
34+
extern char __stop_main_func;
35+
3036
void foo();
3137
_Unwind_Exception ex;
3238

@@ -41,14 +47,13 @@ _Unwind_Reason_Code stop(int version, _Unwind_Action actions,
4147
assert(exceptionObject == &ex);
4248
assert(stop_parameter == &foo);
4349

44-
Dl_info info = {0, 0, 0, 0};
45-
46-
// Unwind util the main is reached, above frames depend on the platform and
50+
// Unwind until the main is reached, above frames depend on the platform and
4751
// architecture.
48-
if (dladdr(reinterpret_cast<void *>(_Unwind_GetIP(context)), &info) &&
49-
info.dli_sname && !strcmp("main", info.dli_sname)) {
52+
uintptr_t ip = _Unwind_GetIP(context);
53+
if (ip >= (uintptr_t)&__start_main_func && ip < (uintptr_t)&__stop_main_func) {
5054
_Exit(0);
5155
}
56+
5257
return _URC_NO_REASON;
5358
}
5459

@@ -66,7 +71,7 @@ __attribute__((noinline)) void foo() {
6671
_Unwind_ForcedUnwind(e, stop, (void *)&foo);
6772
}
6873

69-
int main() {
74+
__attribute__((section("main_func"))) int main() {
7075
foo();
7176
return -2;
7277
}

libunwind/test/signal_unwind.pass.cpp

Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,13 @@
88
//===----------------------------------------------------------------------===//
99

1010
// Ensure that the unwinder can cope with the signal handler.
11-
// REQUIRES: target={{(aarch64|riscv64|s390x|x86_64)-.+linux.*}}
11+
// REQUIRES: target={{(aarch64|riscv64|s390x|x86_64)-.+}}linux-gnu
1212

1313
// TODO: Figure out why this fails with Memory Sanitizer.
1414
// XFAIL: msan
1515

1616
#undef NDEBUG
1717
#include <assert.h>
18-
#include <dlfcn.h>
1918
#include <signal.h>
2019
#include <stdio.h>
2120
#include <stdlib.h>
@@ -24,16 +23,29 @@
2423
#include <unistd.h>
2524
#include <unwind.h>
2625

26+
// Note: this test fails on musl because:
27+
//
28+
// (a) musl disables emission of unwind information for its build, and
29+
// (b) musl's signal trampolines don't include unwind information
30+
//
31+
32+
// Using __attribute__((section("main_func"))) is Linux specific, but then
33+
// this entire test is marked as requiring Linux, so we should be good.
34+
//
35+
// We don't use dladdr() because on musl it's a no-op when statically linked.
36+
extern char __start_main_func;
37+
extern char __stop_main_func;
38+
2739
_Unwind_Reason_Code frame_handler(struct _Unwind_Context* ctx, void* arg) {
2840
(void)arg;
29-
Dl_info info = { 0, 0, 0, 0 };
3041

31-
// Unwind util the main is reached, above frames depend on the platform and
42+
// Unwind until the main is reached, above frames depend on the platform and
3243
// architecture.
33-
if (dladdr(reinterpret_cast<void *>(_Unwind_GetIP(ctx)), &info) &&
34-
info.dli_sname && !strcmp("main", info.dli_sname)) {
44+
uintptr_t ip = _Unwind_GetIP(ctx);
45+
if (ip >= (uintptr_t)&__start_main_func && ip < (uintptr_t)&__stop_main_func) {
3546
_Exit(0);
3647
}
48+
3749
return _URC_NO_REASON;
3850
}
3951

@@ -43,7 +55,7 @@ void signal_handler(int signum) {
4355
_Exit(-1);
4456
}
4557

46-
int main(int, char**) {
58+
__attribute__((section("main_func"))) int main(int, char**) {
4759
signal(SIGUSR1, signal_handler);
4860
kill(getpid(), SIGUSR1);
4961
return -2;

libunwind/test/unwind_leaffunction.pass.cpp

Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,13 @@
88
//===----------------------------------------------------------------------===//
99

1010
// Ensure that leaf function can be unwund.
11-
// REQUIRES: target={{(aarch64|riscv64|s390x|x86_64)-.+linux.*}}
11+
// REQUIRES: target={{(aarch64|riscv64|s390x|x86_64)-.+}}linux-gnu
1212

1313
// TODO: Figure out why this fails with Memory Sanitizer.
1414
// XFAIL: msan
1515

1616
#undef NDEBUG
1717
#include <assert.h>
18-
#include <dlfcn.h>
1918
#include <signal.h>
2019
#include <stdio.h>
2120
#include <stdlib.h>
@@ -24,16 +23,29 @@
2423
#include <unistd.h>
2524
#include <unwind.h>
2625

26+
// Note: this test fails on musl because:
27+
//
28+
// (a) musl disables emission of unwind information for its build, and
29+
// (b) musl's signal trampolines don't include unwind information
30+
//
31+
32+
// Using __attribute__((section("main_func"))) is Linux specific, but then
33+
// this entire test is marked as requiring Linux, so we should be good.
34+
//
35+
// We don't use dladdr() because on musl it's a no-op when statically linked.
36+
extern char __start_main_func;
37+
extern char __stop_main_func;
38+
2739
_Unwind_Reason_Code frame_handler(struct _Unwind_Context* ctx, void* arg) {
2840
(void)arg;
29-
Dl_info info = { 0, 0, 0, 0 };
3041

31-
// Unwind until the main is reached, above frames deeped on the platform and
42+
// Unwind until the main is reached, above frames depend on the platform and
3243
// architecture.
33-
if (dladdr(reinterpret_cast<void *>(_Unwind_GetIP(ctx)), &info) &&
34-
info.dli_sname && !strcmp("main", info.dli_sname)) {
44+
uintptr_t ip = _Unwind_GetIP(ctx);
45+
if (ip >= (uintptr_t)&__start_main_func && ip < (uintptr_t)&__stop_main_func) {
3546
_Exit(0);
3647
}
48+
3749
return _URC_NO_REASON;
3850
}
3951

@@ -52,7 +64,7 @@ __attribute__((noinline)) void crashing_leaf_func(void) {
5264
__builtin_trap();
5365
}
5466

55-
int main(int, char**) {
67+
__attribute__((section("main_func"))) int main(int, char**) {
5668
signal(SIGTRAP, signal_handler);
5769
signal(SIGILL, signal_handler);
5870
crashing_leaf_func();

0 commit comments

Comments
 (0)