diff --git a/runtime/libia2/ia2.c b/runtime/libia2/ia2.c index 3a1551161..ede4b7088 100644 --- a/runtime/libia2/ia2.c +++ b/runtime/libia2/ia2.c @@ -1,6 +1,7 @@ #ifndef _GNU_SOURCE #define _GNU_SOURCE #endif +#include #include #include #include @@ -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 diff --git a/runtime/libia2/include/ia2.h b/runtime/libia2/include/ia2.h index 53e9b7888..bcf883e37 100644 --- a/runtime/libia2/include/ia2.h +++ b/runtime/libia2/include/ia2.h @@ -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" @@ -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