OLD | NEW |
(Empty) | |
| 1 /* |
| 2 * Copyright (c) 2012 The Native Client Authors. All rights reserved. |
| 3 * Use of this source code is governed by a BSD-style license that can be |
| 4 * found in the LICENSE file. |
| 5 */ |
| 6 |
| 7 #include "native_client/src/untrusted/crash_dump/untrusted_crash_dump.h" |
| 8 |
| 9 #include <assert.h> |
| 10 #include <inttypes.h> |
| 11 #include <pthread.h> |
| 12 #include <stdio.h> |
| 13 #include <stdlib.h> |
| 14 #include <string.h> |
| 15 #include <sys/mman.h> |
| 16 #include <sys/nacl_syscalls.h> |
| 17 |
| 18 #ifdef __GLIBC__ |
| 19 #include <elf.h> |
| 20 #include <link.h> |
| 21 #endif /* __GLIBC__ */ |
| 22 |
| 23 #include "native_client/src/untrusted/nacl/syscall_bindings_trampoline.h" |
| 24 |
| 25 |
| 26 #define CRASH_PAGE_CHUNK (64 * 1024) |
| 27 #define CRASH_STACK_SIZE (CRASH_PAGE_CHUNK * 4) |
| 28 #define CRASH_STACK_GUARD_SIZE CRASH_PAGE_CHUNK |
| 29 #define CRASH_STACK_COMPLETE_SIZE (CRASH_STACK_GUARD_SIZE + CRASH_STACK_SIZE) |
| 30 |
| 31 |
| 32 static pthread_key_t g_CrashStackKey; |
| 33 |
| 34 |
| 35 #ifdef __GLIBC__ |
| 36 |
| 37 struct ProgramTableData { |
| 38 FILE *core; |
| 39 int first; |
| 40 }; |
| 41 |
| 42 |
| 43 static int PrintSegmentsOne( |
| 44 struct dl_phdr_info *info, size_t size, void *data) { |
| 45 int i; |
| 46 struct ProgramTableData *ptd = (struct ProgramTableData*) data; |
| 47 |
| 48 if (ptd->first) { |
| 49 ptd->first = 0; |
| 50 } else { |
| 51 fprintf(ptd->core, ",\n"); |
| 52 } |
| 53 fprintf(ptd->core, "{\n"); |
| 54 /* TODO(bradnelson): json escape filenames properly. */ |
| 55 fprintf(ptd->core, "\"dlpi_name\": \"%s\",\n", info->dlpi_name); |
| 56 fprintf(ptd->core, "\"dlpi_addr\": %"PRIuPTR",\n", info->dlpi_addr); |
| 57 fprintf(ptd->core, "\"dlpi_phdr\": [\n"); |
| 58 for (i = 0; i < info->dlpi_phnum; i++) { |
| 59 /* Skip non-LOAD type segments. */ |
| 60 if (info->dlpi_phdr[i].p_type != PT_LOAD) { |
| 61 continue; |
| 62 } |
| 63 if (i != 0) { |
| 64 fprintf(ptd->core, ",\n"); |
| 65 } |
| 66 fprintf(ptd->core, "{\n"); |
| 67 fprintf(ptd->core, "\"p_vaddr\": %"PRIuPTR",\n", |
| 68 info->dlpi_phdr[i].p_vaddr); |
| 69 fprintf(ptd->core, "\"p_memsz\": %"PRIuPTR"\n", |
| 70 info->dlpi_phdr[i].p_memsz); |
| 71 fprintf(ptd->core, "}\n"); |
| 72 } |
| 73 fprintf(ptd->core, "]\n"); |
| 74 fprintf(ptd->core, "}\n"); |
| 75 return 0; |
| 76 } |
| 77 |
| 78 static void PrintSegments(FILE *core) { |
| 79 struct ProgramTableData data; |
| 80 data.core = core; |
| 81 data.first = 1; |
| 82 dl_iterate_phdr(PrintSegmentsOne, &data); |
| 83 } |
| 84 |
| 85 #else /* __GLIBC__ */ |
| 86 |
| 87 static void PrintSegments(FILE *core) { |
| 88 } |
| 89 |
| 90 #endif /* __GLIBC__ */ |
| 91 |
| 92 uintptr_t SafeRead(uintptr_t a) { |
| 93 /* TODO(bradnelson): use exception handling to recover from reads. */ |
| 94 return *(uintptr_t*)a; |
| 95 } |
| 96 |
| 97 static void StackWalk(FILE *core, uintptr_t prog_ctr, uintptr_t frame_ptr) { |
| 98 uintptr_t next; |
| 99 uintptr_t i; |
| 100 int first = 1; |
| 101 |
| 102 fprintf(core, "\"frames\": [\n"); |
| 103 for (;;) { |
| 104 next = SafeRead(frame_ptr); |
| 105 if (next <= frame_ptr || next == 0) { |
| 106 break; |
| 107 } |
| 108 if (first) { |
| 109 first = 0; |
| 110 } else { |
| 111 fprintf(core, ","); |
| 112 } |
| 113 fprintf(core, "{\n"); |
| 114 fprintf(core, "\"frame_ptr\": %"PRIuPTR",\n", frame_ptr); |
| 115 fprintf(core, "\"prog_ctr\": %"PRIuPTR",\n", prog_ctr); |
| 116 fprintf(core, "\"data\": [\n"); |
| 117 for (i = frame_ptr + 8; i < next; i += 4) { |
| 118 if (i != frame_ptr + 8) { |
| 119 fprintf(core, ","); |
| 120 } |
| 121 fprintf(core, "%"PRIuPTR"\n", SafeRead(i)); |
| 122 } |
| 123 fprintf(core, "]\n"); |
| 124 fprintf(core, "}\n"); |
| 125 |
| 126 prog_ctr = SafeRead(frame_ptr + 4); |
| 127 frame_ptr = next; |
| 128 } |
| 129 |
| 130 fprintf(core, "]\n"); |
| 131 } |
| 132 |
| 133 void CrashHandlerWrapper(int prog_ctr, int stack_ptr); |
| 134 asm(".pushsection .text, \"ax\", @progbits\n" |
| 135 ".p2align NACLENTRYALIGN\n" |
| 136 "CrashHandlerWrapper:\n" |
| 137 "popl %eax\n" |
| 138 "pushl %ebp\n" |
| 139 "call CrashHandler\n" |
| 140 ".popsection\n"); |
| 141 |
| 142 void CrashHandler(int frame_ptr, int prog_ctr, int stack_ptr) { |
| 143 FILE *core; |
| 144 const char *core_filename; |
| 145 |
| 146 /* Pick core file name. */ |
| 147 core_filename = getenv("NACLCOREFILE"); |
| 148 if (core_filename == NULL) { |
| 149 core_filename = "naclcore.json"; |
| 150 } |
| 151 |
| 152 /* Attempt to open core file, otherwise use stdout. */ |
| 153 core = fopen(core_filename, "w"); |
| 154 if (core == NULL) { |
| 155 core = stdout; |
| 156 } |
| 157 |
| 158 fprintf(core, "{\n"); |
| 159 |
| 160 fprintf(core, "\"segments\": ["); |
| 161 PrintSegments(core); |
| 162 fprintf(core, "],\n"); |
| 163 |
| 164 fprintf(core, "\"handler\": {\n"); |
| 165 fprintf(core, "\"prog_ctr\": %"PRIuPTR",\n", prog_ctr); |
| 166 fprintf(core, "\"stack_ptr\": %"PRIuPTR",\n", stack_ptr); |
| 167 fprintf(core, "\"frame_ptr\": %"PRIuPTR"\n", frame_ptr); |
| 168 fprintf(core, "},\n"); |
| 169 |
| 170 StackWalk(core, (uintptr_t) prog_ctr, (uintptr_t) frame_ptr); |
| 171 |
| 172 fprintf(core, "}\n"); |
| 173 |
| 174 if (core != stdout) { |
| 175 fclose(core); |
| 176 } |
| 177 |
| 178 exit(166); |
| 179 } |
| 180 |
| 181 void NaClCrashDumpThreadDestructor(void *arg) { |
| 182 munmap(arg, CRASH_STACK_COMPLETE_SIZE); |
| 183 } |
| 184 |
| 185 void NaClCrashDumpInit(void) { |
| 186 int result; |
| 187 result = pthread_key_create(&g_CrashStackKey, NaClCrashDumpThreadDestructor); |
| 188 assert(result == 0); |
| 189 result = NACL_SYSCALL(exception_handler)(CrashHandlerWrapper, NULL); |
| 190 assert(result == 0); |
| 191 NaClCrashDumpInitThread(); |
| 192 } |
| 193 |
| 194 void NaClCrashDumpInitThread(void) { |
| 195 void *stack; |
| 196 void *guard; |
| 197 int result; |
| 198 /* |
| 199 * NOTE: Setting up a per thread stack is only particularly interesting |
| 200 * for stack overflow. |
| 201 */ |
| 202 stack = mmap(NULL, CRASH_STACK_COMPLETE_SIZE, |
| 203 PROT_READ | PROT_WRITE, |
| 204 MAP_PRIVATE | MAP_ANONYMOUS, -1, 0); |
| 205 assert(stack != MAP_FAILED); |
| 206 guard = mmap(stack, CRASH_STACK_GUARD_SIZE, |
| 207 PROT_NONE, MAP_FIXED | MAP_PRIVATE | MAP_ANONYMOUS, -1, 0); |
| 208 assert(guard == stack); |
| 209 pthread_setspecific(g_CrashStackKey, stack); |
| 210 result = NACL_SYSCALL(exception_stack)(stack, CRASH_STACK_COMPLETE_SIZE); |
| 211 assert(result == 0); |
| 212 } |
OLD | NEW |