Skip to content

Commit 6763a59

Browse files
committed
Print backtrace on failed ASSERT_PKRU
1 parent 94f890b commit 6763a59

File tree

2 files changed

+29
-1
lines changed

2 files changed

+29
-1
lines changed

runtime/libia2/ia2.c

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
#ifndef _GNU_SOURCE
22
#define _GNU_SOURCE
33
#endif
4+
#include <execinfo.h>
45
#include <inttypes.h>
56
#include <stdio.h>
67
#include <string.h>
@@ -497,3 +498,21 @@ int protect_pages(struct dl_phdr_info *info, size_t size, void *data) {
497498
// library
498499
return 0;
499500
}
501+
502+
#if IA2_DEBUG
503+
void ia2_print_backtrace(void) {
504+
#define IA2_BT_SZ 10
505+
void *ra_buf[IA2_BT_SZ];
506+
int num_ra = backtrace(ra_buf, IA2_BT_SZ);
507+
char **fn_names = backtrace_symbols(ra_buf, num_ra);
508+
for (size_t i = 0; i < num_ra; i++) {
509+
if (fn_names) {
510+
fprintf(stderr, "#%d %p in %s ()\n", i, ra_buf[i], fn_names[i]);
511+
} else {
512+
fprintf(stderr, "#%d %p ()\n", i, ra_buf[i]);
513+
}
514+
}
515+
fflush(stderr);
516+
_exit(-1);
517+
}
518+
#endif

runtime/libia2/include/ia2.h

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,12 @@
102102
"rdpkru\n" \
103103
"cmpl $" #pkru ", %eax\n" \
104104
"je 1f\n" \
105-
"ud2\n" \
105+
"movq %rax, %rax\n" \
106+
"movq %rcx, %rcx\n" \
107+
"movq %rdx, %rdx\n" \
108+
"wrpkru\n" \
109+
"subq $8, %rsp\n" \
110+
"call ia2_print_backtrace\n" \
106111
"1:\n" \
107112
"movq %r11, %rdx\n" \
108113
"movq %r10, %rcx\n"
@@ -157,6 +162,10 @@ uint32_t ia2_get_pkru();
157162
/// Returns the current compartment pkey
158163
size_t ia2_get_pkey();
159164

165+
#if IA2_DEBUG
166+
void ia2_print_backtrace(void);
167+
#endif
168+
160169
#ifdef __cplusplus
161170
}
162171
#endif

0 commit comments

Comments
 (0)