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 void (*g_PrevHandler)(int prog_ctr, int stack_ptr) = 0; | |
33 static pthread_key_t g_CrashStackKey; | |
34 | |
35 | |
36 #ifdef __GLIBC__ | |
37 | |
38 struct ProgramTableData { | |
39 FILE *core; | |
40 uintptr_t addr; | |
Mark Seaborn
2012/02/16 18:42:50
Not used?
bradn
2012/02/16 20:13:10
Done.
| |
41 int first; | |
42 }; | |
43 | |
44 | |
45 static int PrintSegmentsOne( | |
46 struct dl_phdr_info *info, size_t size, void *data) { | |
47 int i; | |
48 struct ProgramTableData *ptd = (struct ProgramTableData*) data; | |
49 | |
50 if (ptd->first) { | |
51 ptd->first = 0; | |
52 } else { | |
53 fprintf(ptd->core, ",\n"); | |
54 } | |
55 fprintf(ptd->core, "{\n"); | |
56 fprintf(ptd->core, "\"dlpi_name\": \"%s\",\n", info->dlpi_name); | |
Mark Seaborn
2012/02/16 18:42:50
Please put in a TODO for quoting special character
bradn
2012/02/16 20:13:10
Done.
| |
57 fprintf(ptd->core, "\"dlpi_addr\": %"PRIuPTR",\n", info->dlpi_addr); | |
58 fprintf(ptd->core, "\"dlpi_phdr\": [\n"); | |
59 for (i = 0; i < info->dlpi_phnum; i++) { | |
60 /* Skip non-LOAD type segments. */ | |
61 if (info->dlpi_phdr[i].p_type != PT_LOAD) { | |
62 continue; | |
63 } | |
64 if (i != 0) { | |
65 fprintf(ptd->core, ",\n"); | |
66 } | |
67 fprintf(ptd->core, "{\n"); | |
68 fprintf(ptd->core, "\"p_vaddr\": %"PRIuPTR",\n", | |
69 info->dlpi_phdr[i].p_vaddr); | |
70 fprintf(ptd->core, "\"p_memsz\": %"PRIuPTR"\n", | |
71 info->dlpi_phdr[i].p_memsz); | |
72 fprintf(ptd->core, "}\n"); | |
73 } | |
74 fprintf(ptd->core, "]\n"); | |
75 fprintf(ptd->core, "}\n"); | |
76 return 0; | |
77 } | |
78 | |
79 static void PrintSegments(FILE *core) { | |
80 struct ProgramTableData data; | |
81 data.core = core; | |
82 data.first = 1; | |
83 dl_iterate_phdr(PrintSegmentsOne, &data); | |
84 } | |
85 | |
86 #else /* __GLIBC__ */ | |
87 | |
88 static void PrintSegments(FILE *core) { | |
89 } | |
90 | |
91 #endif /* __GLIBC__ */ | |
92 | |
93 uintptr_t SafeRead(uintptr_t a) { | |
94 /* TODO(bradnelson): use exception handling to recover from reads. */ | |
95 return *(uintptr_t*)a; | |
96 } | |
97 | |
98 static void StackWalk(FILE *core, uintptr_t ip, uintptr_t fp) { | |
Mark Seaborn
2012/02/16 18:42:50
'fp' -> 'frame_ptr' or 'frame'?
'ip' is also rath
bradn
2012/02/16 20:13:10
Done.
| |
99 uintptr_t next; | |
100 uintptr_t i; | |
101 int first = 1; | |
102 | |
103 fprintf(core, "\"frames\": [\n"); | |
104 for (;;) { | |
105 next = SafeRead(fp); | |
106 if (next <= fp || next == 0) { | |
107 break; | |
108 } | |
109 if (first) { | |
110 first = 0; | |
111 } else { | |
112 fprintf(core, ","); | |
113 } | |
114 fprintf(core, "{\n"); | |
115 fprintf(core, "\"fp\": %"PRIuPTR",\n", fp); | |
116 fprintf(core, "\"ip\": %"PRIuPTR",\n", ip); | |
117 fprintf(core, "\"data\": [\n"); | |
118 for (i = fp + 8; i < next; i += 4) { | |
119 if (i != fp + 8) { | |
120 fprintf(core, ","); | |
121 } | |
122 fprintf(core, "%"PRIuPTR"\n", SafeRead(i)); | |
123 } | |
124 fprintf(core, "]\n"); | |
125 fprintf(core, "}\n"); | |
126 | |
127 ip = SafeRead(fp + 4); | |
128 fp = next; | |
129 } | |
130 | |
131 fprintf(core, "]\n"); | |
132 } | |
133 | |
134 void CrashHandlerWrapper(int prog_ctr, int stack_ptr); | |
135 asm(".pushsection .text, \"ax\", @progbits\n" | |
136 ".p2align NACLENTRYALIGN\n" | |
137 "CrashHandlerWrapper:\n" | |
138 "popl %eax\n" | |
139 "pushl %ebp\n" | |
140 "call CrashHandler\n" | |
141 ".popsection\n"); | |
142 | |
143 void CrashHandler(int frame_ptr, int prog_ctr, int stack_ptr) { | |
144 FILE *core; | |
145 const char *core_filename; | |
146 | |
147 /* Pick core file name. */ | |
148 core_filename = getenv("NACLCOREFILE"); | |
149 if (core_filename == NULL) { | |
150 core_filename = "naclcore.json"; | |
151 } | |
152 | |
153 /* Attempt to open core file, otherwise use stdout. */ | |
154 core = fopen(core_filename, "w"); | |
155 if (core == NULL) { | |
156 core = stdout; | |
157 } | |
158 | |
159 fprintf(core, "{\n"); | |
160 | |
161 fprintf(core, "\"segments\": ["); | |
162 PrintSegments(core); | |
163 fprintf(core, "],\n"); | |
164 | |
165 fprintf(core, "\"handler\": {\n"); | |
166 fprintf(core, "\"prog_ctr\": %"PRIuPTR",\n", prog_ctr); | |
167 fprintf(core, "\"stack_ptr\": %"PRIuPTR",\n", stack_ptr); | |
168 fprintf(core, "\"frame_ptr\": %"PRIuPTR"\n", frame_ptr); | |
169 fprintf(core, "},\n"); | |
170 | |
171 StackWalk(core, (uintptr_t) prog_ctr, (uintptr_t) frame_ptr); | |
172 | |
173 fprintf(core, "}\n"); | |
174 | |
175 if (core != stdout) { | |
176 fclose(core); | |
177 } | |
178 | |
179 exit(166); | |
180 } | |
181 | |
182 void NaClCrashDumpThreadDestructor(void *arg) { | |
183 munmap(arg, CRASH_STACK_COMPLETE_SIZE); | |
184 } | |
185 | |
186 void NaClCrashDumpInit(void) { | |
187 int result; | |
188 result = pthread_key_create(&g_CrashStackKey, NaClCrashDumpThreadDestructor); | |
189 assert(result == 0); | |
190 result = NACL_SYSCALL(exception_handler)(CrashHandlerWrapper, | |
191 &g_PrevHandler); | |
Mark Seaborn
2012/02/16 18:42:50
Could use NULL instead of &g_PrevHandler now
bradn
2012/02/16 20:13:10
Done.
| |
192 assert(result == 0); | |
193 NaClCrashDumpInitThread(); | |
194 } | |
195 | |
196 void NaClCrashDumpInitThread(void) { | |
197 void *stack; | |
198 void *guard; | |
199 int result; | |
200 /* | |
201 * NOTE: Setting up a per thread stack is only particularly interesting | |
202 * for stack overflow. | |
203 */ | |
204 stack = mmap(NULL, CRASH_STACK_COMPLETE_SIZE, | |
205 PROT_READ | PROT_WRITE, | |
206 MAP_PRIVATE | MAP_ANONYMOUS, -1, 0); | |
207 assert(stack != MAP_FAILED); | |
208 guard = mmap(stack, CRASH_STACK_GUARD_SIZE, | |
209 PROT_NONE, MAP_FIXED | MAP_PRIVATE | MAP_ANONYMOUS, -1, 0); | |
210 assert(guard == stack); | |
211 pthread_setspecific(g_CrashStackKey, stack); | |
212 result = NACL_SYSCALL(exception_stack)(stack, CRASH_STACK_COMPLETE_SIZE); | |
213 assert(result == 0); | |
214 } | |
OLD | NEW |