Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[libunwind] Tweak tests for musl support. #8289

Open
wants to merge 1 commit into
base: stable/20240123
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
18 changes: 12 additions & 6 deletions libunwind/test/floatregister.pass.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,20 +11,26 @@

// Basic test for float registers number are accepted.

#include <dlfcn.h>
#include <stdlib.h>
#include <string.h>
#include <unwind.h>

// Using __attribute__((section("main_func"))) is Linux specific, but then
// this entire test is marked as requiring Linux, so we should be good.
//
// We don't use dladdr() because on musl it's a no-op when statically linked.
extern char __start_main_func;
extern char __stop_main_func;

_Unwind_Reason_Code frame_handler(struct _Unwind_Context *ctx, void *arg) {
(void)arg;
Dl_info info = {0, 0, 0, 0};

// Unwind util the main is reached, above frames depend on the platform and
// Unwind until the main is reached, above frames depend on the platform and
// architecture.
if (dladdr(reinterpret_cast<void *>(_Unwind_GetIP(ctx)), &info) &&
info.dli_sname && !strcmp("main", info.dli_sname))
uintptr_t ip = _Unwind_GetIP(ctx);
if (ip >= (uintptr_t)&__start_main_func && ip < (uintptr_t)&__stop_main_func) {
_Exit(0);
}

return _URC_NO_REASON;
}
Expand All @@ -45,7 +51,7 @@ __attribute__((noinline)) void foo() {
_Unwind_Backtrace(frame_handler, NULL);
}

int main() {
__attribute__((section("main_func"))) int main() {
foo();
return -2;
}
19 changes: 12 additions & 7 deletions libunwind/test/forceunwind.pass.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@

#undef NDEBUG
#include <assert.h>
#include <dlfcn.h>
#include <signal.h>
#include <stdint.h>
#include <stdio.h>
Expand All @@ -27,6 +26,13 @@
#include <unistd.h>
#include <unwind.h>

// Using __attribute__((section("main_func"))) is Linux specific, but then
// this entire test is marked as requiring Linux, so we should be good.
//
// We don't use dladdr() because on musl it's a no-op when statically linked.
extern char __start_main_func;
extern char __stop_main_func;

void foo();
_Unwind_Exception ex;

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

Dl_info info = {0, 0, 0, 0};

// Unwind util the main is reached, above frames depend on the platform and
// Unwind until the main is reached, above frames depend on the platform and
// architecture.
if (dladdr(reinterpret_cast<void *>(_Unwind_GetIP(context)), &info) &&
info.dli_sname && !strcmp("main", info.dli_sname)) {
uintptr_t ip = _Unwind_GetIP(context);
if (ip >= (uintptr_t)&__start_main_func && ip < (uintptr_t)&__stop_main_func) {
_Exit(0);
}

return _URC_NO_REASON;
}

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

int main() {
__attribute__((section("main_func"))) int main() {
foo();
return -2;
}
26 changes: 19 additions & 7 deletions libunwind/test/signal_unwind.pass.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,13 @@
//===----------------------------------------------------------------------===//

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

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

#undef NDEBUG
#include <assert.h>
#include <dlfcn.h>
#include <signal.h>
#include <stdio.h>
#include <stdlib.h>
Expand All @@ -24,16 +23,29 @@
#include <unistd.h>
#include <unwind.h>

// Note: this test fails on musl because:
//
// (a) musl disables emission of unwind information for its build, and
// (b) musl's signal trampolines don't include unwind information
//

// Using __attribute__((section("main_func"))) is Linux specific, but then
// this entire test is marked as requiring Linux, so we should be good.
//
// We don't use dladdr() because on musl it's a no-op when statically linked.
extern char __start_main_func;
extern char __stop_main_func;

_Unwind_Reason_Code frame_handler(struct _Unwind_Context* ctx, void* arg) {
(void)arg;
Dl_info info = { 0, 0, 0, 0 };

// Unwind util the main is reached, above frames depend on the platform and
// Unwind until the main is reached, above frames depend on the platform and
// architecture.
if (dladdr(reinterpret_cast<void *>(_Unwind_GetIP(ctx)), &info) &&
info.dli_sname && !strcmp("main", info.dli_sname)) {
uintptr_t ip = _Unwind_GetIP(ctx);
if (ip >= (uintptr_t)&__start_main_func && ip < (uintptr_t)&__stop_main_func) {
_Exit(0);
}

return _URC_NO_REASON;
}

Expand All @@ -43,7 +55,7 @@ void signal_handler(int signum) {
_Exit(-1);
}

int main(int, char**) {
__attribute__((section("main_func"))) int main(int, char**) {
signal(SIGUSR1, signal_handler);
kill(getpid(), SIGUSR1);
return -2;
Expand Down
24 changes: 18 additions & 6 deletions libunwind/test/unwind_leaffunction.pass.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,13 @@
//===----------------------------------------------------------------------===//

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

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

#undef NDEBUG
#include <assert.h>
#include <dlfcn.h>
#include <signal.h>
#include <stdio.h>
#include <stdlib.h>
Expand All @@ -24,16 +23,29 @@
#include <unistd.h>
#include <unwind.h>

// Note: this test fails on musl because:
//
// (a) musl disables emission of unwind information for its build, and
// (b) musl's signal trampolines don't include unwind information
//

// Using __attribute__((section("main_func"))) is Linux specific, but then
// this entire test is marked as requiring Linux, so we should be good.
//
// We don't use dladdr() because on musl it's a no-op when statically linked.
extern char __start_main_func;
extern char __stop_main_func;

_Unwind_Reason_Code frame_handler(struct _Unwind_Context* ctx, void* arg) {
(void)arg;
Dl_info info = { 0, 0, 0, 0 };

// Unwind until the main is reached, above frames depend on the platform and
// architecture.
if (dladdr(reinterpret_cast<void *>(_Unwind_GetIP(ctx)), &info) &&
info.dli_sname && !strcmp("main", info.dli_sname)) {
uintptr_t ip = _Unwind_GetIP(ctx);
if (ip >= (uintptr_t)&__start_main_func && ip < (uintptr_t)&__stop_main_func) {
_Exit(0);
}

return _URC_NO_REASON;
}

Expand All @@ -56,7 +68,7 @@ __attribute__((noinline)) void crashing_leaf_func(int do_trap) {
__builtin_trap();
}

int main(int, char**) {
__attribute__((section("main_func"))) int main(int, char**) {
signal(SIGTRAP, signal_handler);
signal(SIGILL, signal_handler);
crashing_leaf_func(1);
Expand Down