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

WIP Print backtrace on failed ASSERT_PKRUs #453

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions runtime/libia2/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ endif()

target_link_options(libia2
INTERFACE
"-lunwind"
"-pthread"
"-Wl,--wrap=pthread_create"
"-Wl,--wrap=main"
Expand All @@ -29,6 +30,10 @@ target_link_options(libia2
"-Wl,-z,relro"
)

if (LIBIA2_X86_64)
target_link_options(libia2 INTERFACE "-lunwind-x86_64")
endif()

target_link_libraries(libia2 PRIVATE dl)

target_include_directories(libia2 PUBLIC include)
Expand Down
25 changes: 25 additions & 0 deletions runtime/libia2/ia2.c
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#ifndef _GNU_SOURCE
#define _GNU_SOURCE
#endif
#include <libunwind.h>
#include <inttypes.h>
#include <stdio.h>
#include <string.h>
Expand Down Expand Up @@ -507,3 +508,27 @@ int protect_pages(struct dl_phdr_info *info, size_t size, void *data) {
// library
return 0;
}

#if IA2_DEBUG
void ia2_print_backtrace(void) {
unw_cursor_t cursor;
unw_context_t uc;
unw_word_t pc;

unw_getcontext(&uc);
unw_init_local(&cursor, &uc);
int i = 0;
while (unw_step(&cursor) > 0) {
unw_get_reg(&cursor, UNW_REG_IP, &pc);
Dl_info dlinf = {0};
bool found = dladdr((void *)pc, &dlinf) != 0;
if (found) {
fprintf(stderr, "#%d %p in %s ()\n", i++, (void *)pc, dlinf.dli_sname);
} else {
fprintf(stderr, "#%d %p ()\n", i++, (void *)pc);
}
}
fflush(stderr);
_exit(-1);
}
#endif
11 changes: 10 additions & 1 deletion runtime/libia2/include/ia2.h
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,12 @@
"rdpkru\n" \
"cmpl $" #pkru ", %eax\n" \
"je 1f\n" \
"ud2\n" \
"movq %rax, %rax\n" \
"movq %rcx, %rcx\n" \
"movq %rdx, %rdx\n" \
"wrpkru\n" \
"subq $8, %rsp\n" \
"call ia2_print_backtrace\n" \
"1:\n" \
"movq %r11, %rdx\n" \
"movq %r10, %rcx\n"
Expand Down Expand Up @@ -157,6 +162,10 @@ uint32_t ia2_get_pkru();
/// Returns the current compartment pkey
size_t ia2_get_pkey();

#if IA2_DEBUG
void ia2_print_backtrace(void);
#endif

#ifdef __cplusplus
}
#endif
Loading