Skip to content

Commit

Permalink
Print backtrace on failed ASSERT_PKRU
Browse files Browse the repository at this point in the history
  • Loading branch information
ayrtonm committed Oct 22, 2024
1 parent 3dcc745 commit 2789124
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 1 deletion.
19 changes: 19 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 <execinfo.h>
#include <inttypes.h>
#include <stdio.h>
#include <string.h>
Expand Down Expand Up @@ -497,3 +498,21 @@ int protect_pages(struct dl_phdr_info *info, size_t size, void *data) {
// library
return 0;
}

#if IA2_DEBUG
void ia2_print_backtrace(void) {
#define IA2_BT_SZ 10
void *ra_buf[IA2_BT_SZ];
int num_ra = backtrace(ra_buf, IA2_BT_SZ);
char **fn_names = backtrace_symbols(ra_buf, num_ra);
for (size_t i = 0; i < num_ra; i++) {
if (fn_names) {
fprintf(stderr, "#%d %p in %s ()\n", i, ra_buf[i], fn_names[i]);
} else {
fprintf(stderr, "#%d %p ()\n", i, ra_buf[i]);
}
}
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

0 comments on commit 2789124

Please sign in to comment.