Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(99)

Side by Side Diff: tests/untrusted_crash_dump/untrusted_crash_dump.c

Issue 9316125: Adding untrusted crash dump / stack trace tests. (Closed) Base URL: svn://svn.chromium.org/native_client/trunk/src/native_client
Patch Set: Turning on for linux32 Created 8 years, 10 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
(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;
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) {
80 struct ProgramTableData data;
81 data.core = core;
82 data.first = 1;
83 dl_iterate_phdr(PrintSectionsOne, &data);
84 }
85
86 static int PrintMappedAddressOne(
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) {
148 return 0;
149 }
150 return *(uintptr_t*)a;
151 }
152
153 static void StackWalk(FILE *core, uintptr_t ip, uintptr_t sp) {
154 uintptr_t next;
155 uintptr_t i;
156 int first = 1;
157
158 fprintf(core, "\"frames\": [\n");
159 for (;;) {
160 next = SafeRead(sp);
Mark Seaborn 2012/02/09 22:24:55 This doesn't work for crashes in non-trivial funct
bradn 2012/02/11 01:04:14 Now using frame_ptr you propagated thru in other C
161 if (next <= sp || next == 0) {
162 break;
163 }
164 if (first) {
165 first = 0;
166 } else {
167 fprintf(core, ",");
168 }
169 fprintf(core, "{\n");
170 fprintf(core, "\"sp\": %"PRIuPTR",\n", sp);
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 = sp + 8; i < next; i += 4) {
177 if (i != sp + 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(sp + 4);
186 sp = next;
187 }
188
189 fprintf(core, "]\n");
190 }
191
192 static void CrashHandler(int prog_ctr, int stack_ptr) {
193 FILE *core;
194 const char *core_filename;
195
196 /* Pick core file name. */
197 core_filename = getenv("NACLCOREFILE");
198 if (core_filename == NULL) {
199 core_filename = "naclcore.json";
200 }
201
202 /* Attempt to open core file, otherwise use stdout. */
203 core = fopen(core_filename, "w");
204 if (core == NULL) {
205 core = stdout;
206 }
207
208 fprintf(core, "{\n");
209
210 fprintf(core, "\"sections\": [");
211 PrintSections(core);
212 fprintf(core, "],\n");
213
214 fprintf(core, "\"handler\": {\n");
215 fprintf(core, "\"prog_ctr\": %"PRIuPTR",\n", prog_ctr);
216 fprintf(core, "\"stack_ptr\": %"PRIuPTR"\n", stack_ptr);
217 fprintf(core, "},\n");
218 StackWalk(core, (uintptr_t) prog_ctr, (uintptr_t) stack_ptr);
219
220 fprintf(core, "}\n");
221
222 if (core != stdout) {
223 fclose(core);
224 }
225
226 exit(166);
227 }
228
229 void NaClCrashDumpThreadDestructor(void *arg) {
230 munmap(arg, CRASH_STACK_COMPLETE_SIZE);
231 }
232
233 void NaClCrashDumpInit(char *program_name) {
234 int result;
235 g_ProgramName = program_name;
236 result = pthread_key_create(&g_CrashStackKey, NaClCrashDumpThreadDestructor);
237 assert(result == 0);
238 result = NACL_SYSCALL(exception_handler)(CrashHandler, &g_PrevHandler);
239 assert(result == 0);
240 NaClCrashDumpInitThread();
241 }
242
243 void NaClCrashDumpDestroy(void) {
244 int result;
245 result = NACL_SYSCALL(exception_handler)(g_PrevHandler, NULL);
246 assert(result == 0);
247 result = pthread_key_delete(g_CrashStackKey);
248 assert(result == 0);
249 g_ProgramName = 0;
250 }
251
252 void NaClCrashDumpInitThread(void) {
253 void *stack;
254 void *guard;
255 int result;
256 /*
257 * NOTE: Setting up a per thread stack is only particularly interesting
258 * for stack overflow.
259 */
260 stack = mmap(NULL, CRASH_STACK_COMPLETE_SIZE,
261 PROT_READ | PROT_WRITE,
262 MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
263 assert(stack != MAP_FAILED);
264 guard = mmap(stack, CRASH_STACK_GUARD_SIZE,
265 PROT_NONE, MAP_FIXED | MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
266 assert(guard != MAP_FAILED);
267 pthread_setspecific(g_CrashStackKey, stack);
268 result = NACL_SYSCALL(exception_stack)(stack, CRASH_STACK_COMPLETE_SIZE);
269 assert(result == 0);
270 }
OLDNEW
« no previous file with comments | « tests/untrusted_crash_dump/untrusted_crash_dump.h ('k') | tests/untrusted_crash_dump/untrusted_crash_dump_lib.c » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698