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

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

Issue 11543028: Allow creating a NaClAppThread without creating a new host OS thread (Closed) Base URL: svn://svn.chromium.org/native_client/trunk/src/native_client
Patch Set: Review: Add NACL_WUR Created 8 years 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
« no previous file with comments | « src/trusted/service_runtime/win/thread_suspension.c ('k') | tests/minnacl/nacl.scons » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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>
8
7 #include "native_client/src/shared/gio/gio.h" 9 #include "native_client/src/shared/gio/gio.h"
8 #include "native_client/src/shared/platform/nacl_check.h" 10 #include "native_client/src/shared/platform/nacl_check.h"
9 #include "native_client/src/shared/platform/nacl_log.h" 11 #include "native_client/src/shared/platform/nacl_log.h"
10 #include "native_client/src/trusted/service_runtime/include/bits/nacl_syscalls.h " 12 #include "native_client/src/trusted/service_runtime/include/bits/nacl_syscalls.h "
11 #include "native_client/src/trusted/service_runtime/nacl_all_modules.h" 13 #include "native_client/src/trusted/service_runtime/nacl_all_modules.h"
12 #include "native_client/src/trusted/service_runtime/nacl_app.h" 14 #include "native_client/src/trusted/service_runtime/nacl_app.h"
13 #include "native_client/src/trusted/service_runtime/nacl_app_thread.h" 15 #include "native_client/src/trusted/service_runtime/nacl_app_thread.h"
14 #include "native_client/src/trusted/service_runtime/nacl_copy.h" 16 #include "native_client/src/trusted/service_runtime/nacl_copy.h"
15 #include "native_client/src/trusted/service_runtime/nacl_syscall_handlers.h" 17 #include "native_client/src/trusted/service_runtime/nacl_syscall_handlers.h"
16 #include "native_client/src/trusted/service_runtime/nacl_valgrind_hooks.h" 18 #include "native_client/src/trusted/service_runtime/nacl_valgrind_hooks.h"
(...skipping 17 matching lines...) Expand all
34 36
35 static int32_t MySyscallExit(struct NaClAppThread *natp) { 37 static int32_t MySyscallExit(struct NaClAppThread *natp) {
36 NaClCopyInDropLock(natp->nap); 38 NaClCopyInDropLock(natp->nap);
37 printf("Inside custom test 'exit' syscall\n"); 39 printf("Inside custom test 'exit' syscall\n");
38 fflush(stdout); 40 fflush(stdout);
39 _exit(0); 41 _exit(0);
40 } 42 }
41 43
42 int main(int argc, char **argv) { 44 int main(int argc, char **argv) {
43 struct NaClApp app; 45 struct NaClApp app;
46 struct NaClApp *nap = &app;
44 struct GioMemoryFileSnapshot gio_file; 47 struct GioMemoryFileSnapshot gio_file;
45 struct NaClSyscallTableEntry syscall_table[NACL_MAX_SYSCALLS] = {{0}}; 48 struct NaClSyscallTableEntry syscall_table[NACL_MAX_SYSCALLS] = {{0}};
46 int index; 49 int index;
50 int use_separate_thread = 0;
47 51
48 NaClHandleBootstrapArgs(&argc, &argv); 52 NaClHandleBootstrapArgs(&argc, &argv);
49 53
50 for (index = 0; index < NACL_MAX_SYSCALLS; index++) { 54 for (index = 0; index < NACL_MAX_SYSCALLS; index++) {
51 syscall_table[index].handler = NotImplementedDecoder; 55 syscall_table[index].handler = NotImplementedDecoder;
52 } 56 }
53 syscall_table[TEST_SYSCALL_INVOKE].handler = MySyscallInvoke; 57 syscall_table[TEST_SYSCALL_INVOKE].handler = MySyscallInvoke;
54 syscall_table[TEST_SYSCALL_EXIT].handler = MySyscallExit; 58 syscall_table[TEST_SYSCALL_EXIT].handler = MySyscallExit;
55 59
60 if (argc >= 2 && strcmp(argv[1], "--use_separate_thread") == 0) {
61 use_separate_thread = 1;
62 argc--;
63 argv++;
64 }
56 if (argc != 2) { 65 if (argc != 2) {
57 NaClLog(LOG_FATAL, "Expected 1 argument: executable filename\n"); 66 NaClLog(LOG_FATAL, "Expected 1 argument: executable filename\n");
58 } 67 }
59 68
60 NaClAllModulesInit(); 69 NaClAllModulesInit();
61 70
62 NaClFileNameForValgrind(argv[1]); 71 NaClFileNameForValgrind(argv[1]);
63 CHECK(GioMemoryFileSnapshotCtor(&gio_file, argv[1])); 72 CHECK(GioMemoryFileSnapshotCtor(&gio_file, argv[1]));
64 CHECK(NaClAppWithSyscallTableCtor(&app, syscall_table)); 73 CHECK(NaClAppWithSyscallTableCtor(&app, syscall_table));
65 CHECK(NaClAppLoadFile((struct Gio *) &gio_file, &app) == LOAD_OK); 74 CHECK(NaClAppLoadFile((struct Gio *) &gio_file, &app) == LOAD_OK);
66 CHECK(NaClAppPrepareToLaunch(&app) == LOAD_OK); 75 CHECK(NaClAppPrepareToLaunch(&app) == LOAD_OK);
67 /*
68 * TODO(mseaborn): It would be nice if we did not have to create a
69 * separate thread for running the sandboxed code.
70 */
71 CHECK(NaClCreateMainThread(&app, 0, NULL, NULL));
72 NaClWaitForMainThreadToExit(&app);
73 76
74 NaClLog(LOG_FATAL, "The exit syscall is not supposed to be callable\n"); 77 /* These are examples of two different ways to run untrusted code. */
78 if (use_separate_thread) {
79 /* Create a new host thread that is managed by NaCl. */
80 CHECK(NaClCreateMainThread(&app, 0, NULL, NULL));
81 NaClWaitForMainThreadToExit(&app);
82
83 NaClLog(LOG_FATAL, "The exit syscall is not supposed to be callable\n");
84 } else {
85 /* Reuse the existing host thread for running untrusted code. */
86 struct NaClAppThread *natp;
87 uintptr_t stack_ptr = NaClGetInitialStackTop(nap);
88 /* Ensure the stack pointer is suitably aligned. */
89 stack_ptr &= ~NACL_STACK_ALIGN_MASK;
90 stack_ptr -= NACL_STACK_PAD_BELOW_ALIGN;
91 /* TODO(mseaborn): Make this interface more straightforward to use. */
92 stack_ptr = NaClSysToUserStackAddr(nap, NaClUserToSys(nap, stack_ptr));
93
94 natp = NaClAppThreadMake(nap, nap->initial_entry_pt, stack_ptr, 0, 0);
95 CHECK(natp != NULL);
96 NaClAppThreadLauncher(natp);
97
98 NaClLog(LOG_FATAL, "NaClThreadLauncher() should not return\n");
99 }
75 100
76 return 1; 101 return 1;
77 } 102 }
OLDNEW
« no previous file with comments | « src/trusted/service_runtime/win/thread_suspension.c ('k') | tests/minnacl/nacl.scons » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698