|
3 | 3 | #include <stdlib.h>
|
4 | 4 | #include <stddef.h>
|
5 | 5 | #include <stdio.h>
|
| 6 | +#include <inttypes.h> |
6 | 7 | #include "julia.h"
|
7 | 8 | #include "julia_internal.h"
|
| 9 | +#ifndef _OS_WINDOWS_ |
| 10 | +#include <unistd.h> |
| 11 | +#include <sys/mman.h> |
| 12 | +#endif |
8 | 13 |
|
9 | 14 | #ifdef __cplusplus
|
10 | 15 | extern "C" {
|
@@ -88,12 +93,133 @@ JL_DLLEXPORT void jl_exit_on_sigint(int on)
|
88 | 93 | exit_on_sigint = on;
|
89 | 94 | }
|
90 | 95 |
|
| 96 | +static uintptr_t jl_get_pc_from_ctx(const void *_ctx); |
| 97 | +void jl_show_sigill(void *_ctx); |
| 98 | +static size_t jl_safe_read_mem(const volatile char *ptr, char *out, size_t len) |
| 99 | +{ |
| 100 | + jl_ptls_t ptls = jl_get_ptls_states(); |
| 101 | + jl_jmp_buf *old_buf = ptls->safe_restore; |
| 102 | + jl_jmp_buf buf; |
| 103 | + ptls->safe_restore = &buf; |
| 104 | + volatile size_t i = 0; |
| 105 | + if (!jl_setjmp(buf, 0)) { |
| 106 | + for (;i < len;i++) { |
| 107 | + out[i] = ptr[i]; |
| 108 | + } |
| 109 | + } |
| 110 | + ptls->safe_restore = old_buf; |
| 111 | + return i; |
| 112 | +} |
| 113 | + |
91 | 114 | #if defined(_WIN32)
|
92 | 115 | #include "signals-win.c"
|
93 | 116 | #else
|
94 | 117 | #include "signals-unix.c"
|
95 | 118 | #endif
|
96 | 119 |
|
| 120 | +static uintptr_t jl_get_pc_from_ctx(const void *_ctx) |
| 121 | +{ |
| 122 | +#if defined(_OS_LINUX_) && defined(_CPU_X86_64_) |
| 123 | + return ((ucontext_t*)_ctx)->uc_mcontext.gregs[REG_RIP]; |
| 124 | +#elif defined(_OS_FREEBSD_) && defined(_CPU_X86_64_) |
| 125 | + return ((ucontext_t*)_ctx)->uc_mcontext.mc_rip; |
| 126 | +#elif defined(_OS_LINUX_) && defined(_CPU_X86_) |
| 127 | + return ((ucontext_t*)_ctx)->uc_mcontext.gregs[REG_EIP]; |
| 128 | +#elif defined(_OS_FREEBSD_) && defined(_CPU_X86_) |
| 129 | + return ((ucontext_t*)_ctx)->uc_mcontext.mc_eip; |
| 130 | +#elif defined(_OS_DARWIN_) |
| 131 | + return ((ucontext64_t*)_ctx)->uc_mcontext64->__ss.__rip; |
| 132 | +#elif defined(_OS_WINDOWS_) && defined(_CPU_X86_) |
| 133 | + return ((CONTEXT*)_ctx)->Eip; |
| 134 | +#elif defined(_OS_WINDOWS_) && defined(_CPU_X86_64_) |
| 135 | + return ((CONTEXT*)_ctx)->Rip; |
| 136 | +#elif defined(_OS_LINUX_) && defined(_CPU_AARCH64_) |
| 137 | + return ((ucontext_t*)_ctx)->uc_mcontext.pc; |
| 138 | +#elif defined(_OS_LINUX_) && defined(_CPU_ARM_) |
| 139 | + return ((ucontext_t*)_ctx)->uc_mcontext.arm_pc; |
| 140 | +#else |
| 141 | + // TODO for PPC |
| 142 | + return 0; |
| 143 | +#endif |
| 144 | +} |
| 145 | + |
| 146 | +void jl_show_sigill(void *_ctx) |
| 147 | +{ |
| 148 | + char *pc = (char*)jl_get_pc_from_ctx(_ctx); |
| 149 | + // unsupported platform |
| 150 | + if (!pc) |
| 151 | + return; |
| 152 | +#if defined(_CPU_X86_64_) || defined(_CPU_X86_) |
| 153 | + uint8_t inst[15]; // max length of x86 instruction |
| 154 | + size_t len = jl_safe_read_mem(pc, (char*)inst, sizeof(inst)); |
| 155 | + // ud2 |
| 156 | + if (len >= 2 && inst[0] == 0x0f && inst[1] == 0x0b) { |
| 157 | + jl_safe_printf("Unreachable reached at %p\n", (void*)pc); |
| 158 | + } |
| 159 | + else { |
| 160 | + jl_safe_printf("Invalid instruction at %p: ", (void*)pc); |
| 161 | + for (int i = 0;i < len;i++) { |
| 162 | + if (i == 0) { |
| 163 | + jl_safe_printf("0x%02" PRIx8, inst[i]); |
| 164 | + } |
| 165 | + else { |
| 166 | + jl_safe_printf(", 0x%02" PRIx8, inst[i]); |
| 167 | + } |
| 168 | + } |
| 169 | + jl_safe_printf("\n"); |
| 170 | + } |
| 171 | +#elif defined(_OS_LINUX_) && defined(_CPU_AARCH64_) |
| 172 | + uint32_t inst = 0; |
| 173 | + size_t len = jl_safe_read_mem(pc, (char*)&inst, 4); |
| 174 | + if (len < 4) |
| 175 | + jl_safe_printf("Fault when reading instruction: %d bytes read\n", (int)len); |
| 176 | + if (inst == 0xd4200020) { // brk #0x1 |
| 177 | + // The signal might actually be SIGTRAP instead, doesn't hurt to handle it here though. |
| 178 | + jl_safe_printf("Unreachable reached at %p\n", pc); |
| 179 | + } |
| 180 | + else { |
| 181 | + jl_safe_printf("Invalid instruction at %p: 0x%08" PRIx32 "\n", pc, inst); |
| 182 | + } |
| 183 | +#elif defined(_OS_LINUX_) && defined(_CPU_ARM_) |
| 184 | + ucontext_t *ctx = (ucontext_t*)_ctx; |
| 185 | + if (ctx->uc_mcontext.arm_cpsr & (1 << 5)) { |
| 186 | + // Thumb |
| 187 | + uint16_t inst[2] = {0, 0}; |
| 188 | + size_t len = jl_safe_read_mem(pc, (char*)&inst, 4); |
| 189 | + if (len < 2) |
| 190 | + jl_safe_printf("Fault when reading Thumb instruction: %d bytes read\n", (int)len); |
| 191 | + // LLVM and GCC uses different code for the trap... |
| 192 | + if (inst[0] == 0xdefe || inst[0] == 0xdeff) { |
| 193 | + // The signal might actually be SIGTRAP instead, doesn't hurt to handle it here though. |
| 194 | + jl_safe_printf("Unreachable reached in Thumb mode at %p: 0x%04" PRIx16 "\n", |
| 195 | + (void*)pc, inst[0]); |
| 196 | + } |
| 197 | + else { |
| 198 | + jl_safe_printf("Invalid Thumb instruction at %p: 0x%04" PRIx16 ", 0x%04" PRIx16 "\n", |
| 199 | + (void*)pc, inst[0], inst[1]); |
| 200 | + } |
| 201 | + } |
| 202 | + else { |
| 203 | + uint32_t inst = 0; |
| 204 | + size_t len = jl_safe_read_mem(pc, (char*)&inst, 4); |
| 205 | + if (len < 4) |
| 206 | + jl_safe_printf("Fault when reading instruction: %d bytes read\n", (int)len); |
| 207 | + // LLVM and GCC uses different code for the trap... |
| 208 | + if (inst == 0xe7ffdefe || inst == 0xe7f000f0) { |
| 209 | + // The signal might actually be SIGTRAP instead, doesn't hurt to handle it here though. |
| 210 | + jl_safe_printf("Unreachable reached in ARM mode at %p: 0x%08" PRIx32 "\n", |
| 211 | + (void*)pc, inst); |
| 212 | + } |
| 213 | + else { |
| 214 | + jl_safe_printf("Invalid ARM instruction at %p: 0x%08" PRIx32 "\n", (void*)pc, inst); |
| 215 | + } |
| 216 | + } |
| 217 | +#else |
| 218 | + // TODO for PPC |
| 219 | + (void)_ctx; |
| 220 | +#endif |
| 221 | +} |
| 222 | + |
97 | 223 | // what to do on a critical error
|
98 | 224 | void jl_critical_error(int sig, bt_context_t *context, uintptr_t *bt_data, size_t *bt_size)
|
99 | 225 | {
|
|
0 commit comments