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

Side by Side Diff: tests/minnacl/minimal_test_host.c

Issue 12226119: Factor out duplicated GioMemoryFileSnapshotCtor() + NaClAppLoadFile() calls (Closed) Base URL: svn://svn.chromium.org/native_client/trunk/src/native_client
Patch Set: Created 7 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
1 /* 1 /*
2 * Copyright (c) 2012 The Native Client Authors. All rights reserved. 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 3 * Use of this source code is governed by a BSD-style license that can be
4 * found in the LICENSE file. 4 * found in the LICENSE file.
5 */ 5 */
6 6
7 #include <string.h> 7 #include <string.h>
8 8
9 #include "native_client/src/shared/gio/gio.h"
10 #include "native_client/src/shared/platform/nacl_check.h" 9 #include "native_client/src/shared/platform/nacl_check.h"
11 #include "native_client/src/shared/platform/nacl_log.h" 10 #include "native_client/src/shared/platform/nacl_log.h"
12 #include "native_client/src/trusted/service_runtime/include/bits/nacl_syscalls.h " 11 #include "native_client/src/trusted/service_runtime/include/bits/nacl_syscalls.h "
12 #include "native_client/src/trusted/service_runtime/load_file.h"
13 #include "native_client/src/trusted/service_runtime/nacl_all_modules.h" 13 #include "native_client/src/trusted/service_runtime/nacl_all_modules.h"
14 #include "native_client/src/trusted/service_runtime/nacl_app.h" 14 #include "native_client/src/trusted/service_runtime/nacl_app.h"
15 #include "native_client/src/trusted/service_runtime/nacl_app_thread.h" 15 #include "native_client/src/trusted/service_runtime/nacl_app_thread.h"
16 #include "native_client/src/trusted/service_runtime/nacl_copy.h" 16 #include "native_client/src/trusted/service_runtime/nacl_copy.h"
17 #include "native_client/src/trusted/service_runtime/nacl_syscall_handlers.h" 17 #include "native_client/src/trusted/service_runtime/nacl_syscall_handlers.h"
18 #include "native_client/src/trusted/service_runtime/nacl_valgrind_hooks.h"
19 #include "native_client/src/trusted/service_runtime/sel_ldr.h" 18 #include "native_client/src/trusted/service_runtime/sel_ldr.h"
20 #include "native_client/tests/minnacl/minimal_test_syscalls.h" 19 #include "native_client/tests/minnacl/minimal_test_syscalls.h"
21 20
22 static int32_t NotImplementedDecoder(struct NaClAppThread *natp) { 21 static int32_t NotImplementedDecoder(struct NaClAppThread *natp) {
23 NaClCopyDropLock(natp->nap); 22 NaClCopyDropLock(natp->nap);
24 printf("Error: entered an unexpected syscall!\n"); 23 printf("Error: entered an unexpected syscall!\n");
25 fflush(stdout); 24 fflush(stdout);
26 _exit(1); 25 _exit(1);
27 } 26 }
28 27
29 static int32_t MySyscallInvoke(struct NaClAppThread *natp) { 28 static int32_t MySyscallInvoke(struct NaClAppThread *natp) {
30 NaClCopyDropLock(natp->nap); 29 NaClCopyDropLock(natp->nap);
31 printf("Inside custom test 'invoke' syscall\n"); 30 printf("Inside custom test 'invoke' syscall\n");
32 fflush(stdout); 31 fflush(stdout);
33 /* Return a value that the test guest program checks for. */ 32 /* Return a value that the test guest program checks for. */
34 return 123; 33 return 123;
35 } 34 }
36 35
37 static int32_t MySyscallExit(struct NaClAppThread *natp) { 36 static int32_t MySyscallExit(struct NaClAppThread *natp) {
38 NaClCopyDropLock(natp->nap); 37 NaClCopyDropLock(natp->nap);
39 printf("Inside custom test 'exit' syscall\n"); 38 printf("Inside custom test 'exit' syscall\n");
40 fflush(stdout); 39 fflush(stdout);
41 _exit(0); 40 _exit(0);
42 } 41 }
43 42
44 int main(int argc, char **argv) { 43 int main(int argc, char **argv) {
45 struct NaClApp app; 44 struct NaClApp app;
46 struct NaClApp *nap = &app; 45 struct NaClApp *nap = &app;
47 struct GioMemoryFileSnapshot gio_file;
48 struct NaClSyscallTableEntry syscall_table[NACL_MAX_SYSCALLS] = {{0}}; 46 struct NaClSyscallTableEntry syscall_table[NACL_MAX_SYSCALLS] = {{0}};
49 int index; 47 int index;
50 int use_separate_thread = 0; 48 int use_separate_thread = 0;
51 49
52 NaClHandleBootstrapArgs(&argc, &argv); 50 NaClHandleBootstrapArgs(&argc, &argv);
53 51
54 for (index = 0; index < NACL_MAX_SYSCALLS; index++) { 52 for (index = 0; index < NACL_MAX_SYSCALLS; index++) {
55 syscall_table[index].handler = NotImplementedDecoder; 53 syscall_table[index].handler = NotImplementedDecoder;
56 } 54 }
57 syscall_table[TEST_SYSCALL_INVOKE].handler = MySyscallInvoke; 55 syscall_table[TEST_SYSCALL_INVOKE].handler = MySyscallInvoke;
58 syscall_table[TEST_SYSCALL_EXIT].handler = MySyscallExit; 56 syscall_table[TEST_SYSCALL_EXIT].handler = MySyscallExit;
59 57
60 if (argc >= 2 && strcmp(argv[1], "--use_separate_thread") == 0) { 58 if (argc >= 2 && strcmp(argv[1], "--use_separate_thread") == 0) {
61 use_separate_thread = 1; 59 use_separate_thread = 1;
62 argc--; 60 argc--;
63 argv++; 61 argv++;
64 } 62 }
65 if (argc != 2) { 63 if (argc != 2) {
66 NaClLog(LOG_FATAL, "Expected 1 argument: executable filename\n"); 64 NaClLog(LOG_FATAL, "Expected 1 argument: executable filename\n");
67 } 65 }
68 66
69 NaClAllModulesInit(); 67 NaClAllModulesInit();
70 68
71 NaClFileNameForValgrind(argv[1]);
72 CHECK(GioMemoryFileSnapshotCtor(&gio_file, argv[1]));
73 CHECK(NaClAppWithSyscallTableCtor(&app, syscall_table)); 69 CHECK(NaClAppWithSyscallTableCtor(&app, syscall_table));
74 CHECK(NaClAppLoadFile((struct Gio *) &gio_file, &app) == LOAD_OK); 70 CHECK(NaClAppLoadFileFromFilename(&app, argv[1]) == LOAD_OK);
75 CHECK(NaClAppPrepareToLaunch(&app) == LOAD_OK); 71 CHECK(NaClAppPrepareToLaunch(&app) == LOAD_OK);
76 72
77 /* These are examples of two different ways to run untrusted code. */ 73 /* These are examples of two different ways to run untrusted code. */
78 if (use_separate_thread) { 74 if (use_separate_thread) {
79 /* Create a new host thread that is managed by NaCl. */ 75 /* Create a new host thread that is managed by NaCl. */
80 CHECK(NaClCreateMainThread(&app, 0, NULL, NULL)); 76 CHECK(NaClCreateMainThread(&app, 0, NULL, NULL));
81 NaClWaitForMainThreadToExit(&app); 77 NaClWaitForMainThreadToExit(&app);
82 78
83 NaClLog(LOG_FATAL, "The exit syscall is not supposed to be callable\n"); 79 NaClLog(LOG_FATAL, "The exit syscall is not supposed to be callable\n");
84 } else { 80 } else {
85 /* Reuse the existing host thread for running untrusted code. */ 81 /* Reuse the existing host thread for running untrusted code. */
86 struct NaClAppThread *natp; 82 struct NaClAppThread *natp;
87 uintptr_t stack_ptr = NaClGetInitialStackTop(nap); 83 uintptr_t stack_ptr = NaClGetInitialStackTop(nap);
88 /* Ensure the stack pointer is suitably aligned. */ 84 /* Ensure the stack pointer is suitably aligned. */
89 stack_ptr &= ~NACL_STACK_ALIGN_MASK; 85 stack_ptr &= ~NACL_STACK_ALIGN_MASK;
90 stack_ptr -= NACL_STACK_PAD_BELOW_ALIGN; 86 stack_ptr -= NACL_STACK_PAD_BELOW_ALIGN;
91 /* TODO(mseaborn): Make this interface more straightforward to use. */ 87 /* TODO(mseaborn): Make this interface more straightforward to use. */
92 stack_ptr = NaClSysToUserStackAddr(nap, NaClUserToSys(nap, stack_ptr)); 88 stack_ptr = NaClSysToUserStackAddr(nap, NaClUserToSys(nap, stack_ptr));
93 89
94 natp = NaClAppThreadMake(nap, nap->initial_entry_pt, stack_ptr, 0, 0); 90 natp = NaClAppThreadMake(nap, nap->initial_entry_pt, stack_ptr, 0, 0);
95 CHECK(natp != NULL); 91 CHECK(natp != NULL);
96 NaClAppThreadLauncher(natp); 92 NaClAppThreadLauncher(natp);
97 93
98 NaClLog(LOG_FATAL, "NaClThreadLauncher() should not return\n"); 94 NaClLog(LOG_FATAL, "NaClThreadLauncher() should not return\n");
99 } 95 }
100 96
101 return 1; 97 return 1;
102 } 98 }
OLDNEW
« no previous file with comments | « tests/faulted_thread_queue/faultqueue_test_host.c ('k') | tests/signal_handler_single_step/regs_step_test_host.c » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698