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 <assert.h> | |
8 #include <inttypes.h> | |
9 #include <pthread.h> | |
10 #include <stdio.h> | |
11 #include <stdlib.h> | |
12 #include <string.h> | |
13 #include <sys/mman.h> | |
14 #include <sys/nacl_syscalls.h> | |
15 | |
16 #ifdef __GLIBC__ | |
17 #include <elf.h> | |
18 #include <link.h> | |
19 #endif /* __GLIBC__ */ | |
20 | |
21 #include "native_client/src/untrusted/nacl/syscall_bindings_trampoline.h" | |
22 #include "native_client/tests/untrusted_crash_dump/untrusted_crash_dump.h" | |
23 | |
24 | |
25 #define CRASH_PAGE_CHUNK (64 * 1024) | |
26 #define CRASH_STACK_SIZE (CRASH_PAGE_CHUNK * 4) | |
27 #define CRASH_STACK_GUARD_SIZE CRASH_PAGE_CHUNK | |
28 #define CRASH_STACK_COMPLETE_SIZE (CRASH_STACK_GUARD_SIZE + CRASH_STACK_SIZE) | |
29 | |
30 | |
31 static void (*g_PrevHandler)(int prog_ctr, int stack_ptr) = 0; | |
32 static pthread_key_t g_CrashStackKey; | |
33 static char *g_ProgramName = 0; | |
34 | |
35 | |
36 #ifdef __GLIBC__ | |
37 | |
38 struct ProgramTableData { | |
39 FILE *core; | |
40 uintptr_t addr; | |
41 int first; | |
42 }; | |
43 | |
44 | |
45 static int PrintSectionsOne( | |
46 struct dl_phdr_info *info, size_t size, void *data) { | |
47 int i; | |
48 struct ProgramTableData *ptd = (struct ProgramTableData*)data; | |
Mark Seaborn
2012/02/13 19:04:08
Nit: spacing style
bradn
2012/02/13 23:42:03
Done.
| |
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); | |
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 PrintSections(FILE *core) { | |
Mark Seaborn
2012/02/13 19:04:08
'Sections' -> 'Segments'
bradn
2012/02/13 23:42:03
Done.
| |
80 struct ProgramTableData data; | |
81 data.core = core; | |
82 data.first = 1; | |
83 dl_iterate_phdr(PrintSectionsOne, &data); | |
84 } | |
85 | |
86 static int PrintMappedAddressOne( | |
Mark Seaborn
2012/02/13 19:04:08
So you have two dl_iterate_phdr() loops. One list
bradn
2012/02/13 23:42:03
I had originally though we might have something in
| |
87 struct dl_phdr_info *info, size_t size, void *data) { | |
88 int i; | |
89 struct ProgramTableData *ptd = (struct ProgramTableData *) data; | |
90 uintptr_t addr = ptd->addr; | |
91 uintptr_t start; | |
92 uintptr_t end; | |
93 | |
94 /* Consider relative to phdr base addr. */ | |
95 addr -= info->dlpi_addr; | |
96 | |
97 for (i = 0; i < info->dlpi_phnum; i++) { | |
98 /* Skip non-LOAD type segments. */ | |
99 if (info->dlpi_phdr[i].p_type != PT_LOAD) { | |
100 continue; | |
101 } | |
102 /* See if it's in range for this part. */ | |
103 start = info->dlpi_phdr[i].p_vaddr; | |
104 end = start + info->dlpi_phdr[i].p_memsz; | |
105 if (addr >= start && addr < end) { | |
106 if (info->dlpi_name[0] == '\0') { | |
107 fprintf(ptd->core, "{\"file\": \"%s\", \"addr\": %"PRIuPTR"}", | |
108 g_ProgramName, addr); | |
109 } else { | |
110 fprintf(ptd->core, "{\"file\": \"%s\", \"addr\": %"PRIuPTR"}", | |
111 info->dlpi_name, addr); | |
112 } | |
113 return 1; | |
114 } | |
115 } | |
116 return 0; | |
117 } | |
118 | |
119 static void PrintMappedAddress(FILE *core, uintptr_t addr) { | |
120 struct ProgramTableData ptd; | |
121 int result; | |
122 | |
123 ptd.core = core; | |
124 ptd.addr = addr; | |
125 result = dl_iterate_phdr(PrintMappedAddressOne, &ptd); | |
126 if (result == 0) { | |
127 fprintf(core, "{\"file\": \"%s\", \"addr\": %"PRIuPTR"}", | |
128 g_ProgramName, addr); | |
129 } | |
130 } | |
131 | |
132 #else /* __GLIBC__ */ | |
133 | |
134 static void PrintSections(FILE *core) { | |
135 } | |
136 | |
137 static void PrintMappedAddress(FILE *core, uintptr_t addr) { | |
138 fprintf(core, "{\"file\": \"%s\", \"addr\": %"PRIuPTR"}", | |
139 g_ProgramName, addr); | |
140 } | |
141 | |
142 #endif /* __GLIBC__ */ | |
143 | |
144 uintptr_t SafeRead(uintptr_t a) { | |
145 /* TODO(bradnelson): make this safer + more architecture general. */ | |
146 /* Only read areas plausibly on the stack. */ | |
147 if (a < 0x30000000 || a > 0x3fffffff) { | |
Mark Seaborn
2012/02/13 19:04:08
I don't think this is kicking in during your test,
bradn
2012/02/13 23:42:03
Taken out.
This raises an interesting question.
If
| |
148 return 0; | |
149 } | |
150 return *(uintptr_t*)a; | |
151 } | |
152 | |
153 static void StackWalk(FILE *core, uintptr_t ip, uintptr_t fp) { | |
154 uintptr_t next; | |
155 uintptr_t i; | |
156 int first = 1; | |
157 | |
158 fprintf(core, "\"frames\": [\n"); | |
159 for (;;) { | |
160 next = SafeRead(fp); | |
161 if (next <= fp || next == 0) { | |
162 break; | |
163 } | |
164 if (first) { | |
165 first = 0; | |
166 } else { | |
167 fprintf(core, ","); | |
168 } | |
169 fprintf(core, "{\n"); | |
170 fprintf(core, "\"fp\": %"PRIuPTR",\n", fp); | |
171 fprintf(core, "\"ip\": %"PRIuPTR",\n", ip); | |
172 fprintf(core, "\"ip_mapped\": "); | |
173 PrintMappedAddress(core, ip); | |
174 fprintf(core, ",\n"); | |
175 fprintf(core, "\"data\": [\n"); | |
176 for (i = fp + 8; i < next; i += 4) { | |
177 if (i != fp + 8) { | |
178 fprintf(core, ","); | |
179 } | |
180 fprintf(core, "%"PRIuPTR"\n", SafeRead(i)); | |
181 } | |
182 fprintf(core, "]\n"); | |
183 fprintf(core, "}\n"); | |
184 | |
185 ip = SafeRead(fp + 4); | |
186 fp = next; | |
187 } | |
188 | |
189 fprintf(core, "]\n"); | |
190 } | |
191 | |
192 void CrashHandlerWrapper(int prog_ctr, int stack_ptr); | |
193 asm(".pushsection .text, \"ax\", @progbits\n" | |
194 ".p2align NACLENTRYALIGN\n" | |
195 "CrashHandlerWrapper:\n" | |
196 "popl %eax\n" | |
197 "pushl %ebp\n" | |
198 "call CrashHandler\n" | |
199 ".popsection\n"); | |
200 | |
201 void CrashHandler(int frame_ptr, int prog_ctr, int stack_ptr) { | |
202 FILE *core; | |
203 const char *core_filename; | |
204 | |
205 /* Pick core file name. */ | |
206 core_filename = getenv("NACLCOREFILE"); | |
207 if (core_filename == NULL) { | |
208 core_filename = "naclcore.json"; | |
209 } | |
210 | |
211 /* Attempt to open core file, otherwise use stdout. */ | |
212 core = fopen(core_filename, "w"); | |
213 if (core == NULL) { | |
214 core = stdout; | |
215 } | |
216 | |
217 fprintf(core, "{\n"); | |
218 | |
219 fprintf(core, "\"sections\": ["); | |
220 PrintSections(core); | |
221 fprintf(core, "],\n"); | |
222 | |
223 fprintf(core, "\"handler\": {\n"); | |
224 fprintf(core, "\"prog_ctr\": %"PRIuPTR",\n", prog_ctr); | |
225 fprintf(core, "\"stack_ptr\": %"PRIuPTR",\n", stack_ptr); | |
226 fprintf(core, "\"frame_ptr\": %"PRIuPTR"\n", frame_ptr); | |
227 fprintf(core, "},\n"); | |
228 | |
229 StackWalk(core, (uintptr_t) prog_ctr, (uintptr_t) frame_ptr); | |
230 | |
231 fprintf(core, "}\n"); | |
232 | |
233 if (core != stdout) { | |
234 fclose(core); | |
235 } | |
236 | |
237 exit(166); | |
238 } | |
239 | |
240 void NaClCrashDumpThreadDestructor(void *arg) { | |
241 munmap(arg, CRASH_STACK_COMPLETE_SIZE); | |
242 } | |
243 | |
244 void NaClCrashDumpInit(char *program_name) { | |
245 int result; | |
246 g_ProgramName = program_name; | |
247 result = pthread_key_create(&g_CrashStackKey, NaClCrashDumpThreadDestructor); | |
248 assert(result == 0); | |
249 fprintf(stderr, "ADDR %x\n", (unsigned int)CrashHandlerWrapper); | |
Mark Seaborn
2012/02/13 19:04:08
Remove debugging code
bradn
2012/02/13 23:42:03
Done.
| |
250 fprintf(stderr, "ADDR %x\n", (unsigned int)CrashHandler); | |
251 result = NACL_SYSCALL(exception_handler)(CrashHandlerWrapper, | |
252 &g_PrevHandler); | |
253 assert(result == 0); | |
254 NaClCrashDumpInitThread(); | |
255 } | |
256 | |
257 void NaClCrashDumpDestroy(void) { | |
258 int result; | |
259 result = NACL_SYSCALL(exception_handler)(g_PrevHandler, NULL); | |
Mark Seaborn
2012/02/13 19:04:08
FWIW, I'm not sure that attempting to restore this
bradn
2012/02/13 23:42:03
Done.
| |
260 assert(result == 0); | |
261 result = pthread_key_delete(g_CrashStackKey); | |
262 assert(result == 0); | |
263 g_ProgramName = 0; | |
Mark Seaborn
2012/02/13 19:04:08
0 -> NULL
bradn
2012/02/13 23:42:03
Moot, gone.
| |
264 } | |
265 | |
266 void NaClCrashDumpInitThread(void) { | |
267 void *stack; | |
268 void *guard; | |
269 int result; | |
270 /* | |
271 * NOTE: Setting up a per thread stack is only particularly interesting | |
272 * for stack overflow. | |
273 */ | |
274 stack = mmap(NULL, CRASH_STACK_COMPLETE_SIZE, | |
275 PROT_READ | PROT_WRITE, | |
276 MAP_PRIVATE | MAP_ANONYMOUS, -1, 0); | |
277 assert(stack != MAP_FAILED); | |
278 guard = mmap(stack, CRASH_STACK_GUARD_SIZE, | |
279 PROT_NONE, MAP_FIXED | MAP_PRIVATE | MAP_ANONYMOUS, -1, 0); | |
280 assert(guard != MAP_FAILED); | |
Mark Seaborn
2012/02/13 19:04:08
A stricter assertion would be
assert(guard == st
bradn
2012/02/13 23:42:03
Done.
| |
281 pthread_setspecific(g_CrashStackKey, stack); | |
282 result = NACL_SYSCALL(exception_stack)(stack, CRASH_STACK_COMPLETE_SIZE); | |
283 assert(result == 0); | |
284 } | |
OLD | NEW |