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 <stdio.h>
9 #include <stdlib.h>
10 #include <string.h>
11 #include <unistd.h>
12 
13 #include "native_client/src/include/nacl/nacl_exception.h"
14 #include "native_client/src/trusted/service_runtime/include/sys/nacl_test_crash.h"
15 #include "native_client/src/untrusted/nacl/syscall_bindings_trampoline.h"
16 
17 
18 typedef int (*TYPE_nacl_test_syscall_1)(void);
19 typedef int (*TYPE_nacl_test_syscall_2)(void);
20 
21 
22 char g_stack[0x10000];
23 
look_up_crash_type(const char * name)24 static int look_up_crash_type(const char *name) {
25 #define MAP_NAME(type) if (strcmp(name, #type) == 0) { return type; }
26   MAP_NAME(NACL_TEST_CRASH_MEMORY);
27   MAP_NAME(NACL_TEST_CRASH_LOG_FATAL);
28   MAP_NAME(NACL_TEST_CRASH_CHECK_FAILURE);
29 #undef MAP_NAME
30 
31   fprintf(stderr, "Unknown crash type: \"%s\"\n", name);
32   exit(1);
33 }
34 
exception_handler(struct NaClExceptionContext * context)35 static void exception_handler(struct NaClExceptionContext *context) {
36   fprintf(stderr, "exception_handler() called unexpectedly\n");
37   _exit(1);
38 }
39 
register_exception_handler(void)40 static void register_exception_handler(void) {
41   int rc = nacl_exception_set_handler(exception_handler);
42   assert(rc == 0);
43   rc = nacl_exception_set_stack(g_stack, sizeof(g_stack));
44   assert(rc == 0);
45 }
46 
main(int argc,char ** argv)47 int main(int argc, char **argv) {
48   if (argc != 2) {
49     fprintf(stderr, "Usage: %s <crash-type>\n", argv[0]);
50     return 1;
51   }
52   char *crash_type = argv[1];
53   if (strcmp(crash_type, "NACL_TEST_CRASH_JUMP_TO_ZERO") == 0) {
54     register_exception_handler();
55     NACL_SYSCALL(test_syscall_1)();
56   } else if (strcmp(crash_type, "NACL_TEST_CRASH_JUMP_INTO_SANDBOX") == 0) {
57     register_exception_handler();
58     NACL_SYSCALL(test_syscall_2)();
59   } else {
60     NACL_SYSCALL(test_crash)(look_up_crash_type(crash_type));
61   }
62   return 1;
63 }
64