OLD | NEW |
---|---|
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include <errno.h> | 5 #include <errno.h> |
6 #include <pthread.h> | 6 #include <pthread.h> |
7 #include <stdlib.h> | 7 #include <stdlib.h> |
8 | 8 |
9 #include "base/logging.h" | |
9 #include "base/macros.h" | 10 #include "base/macros.h" |
10 #include "base/memory/scoped_ptr.h" | 11 #include "base/memory/scoped_ptr.h" |
11 #include "components/nacl/loader/nonsfi/irt_interfaces.h" | 12 #include "components/nacl/loader/nonsfi/irt_interfaces.h" |
13 #include "native_client/src/trusted/service_runtime/nacl_signal.h" | |
12 | 14 |
13 namespace nacl { | 15 namespace nacl { |
14 namespace nonsfi { | 16 namespace nonsfi { |
15 namespace { | 17 namespace { |
16 | 18 |
17 // We heuristically chose 1M for the stack size per thread. | 19 // We heuristically chose 1M for the stack size per thread. |
18 const int kStackSize = 1024 * 1024; | 20 const int kStackSize = 1024 * 1024; |
19 | 21 |
20 // For RAII of pthread_attr_t. | 22 // For RAII of pthread_attr_t. |
21 class ScopedPthreadAttrPtr { | 23 class ScopedPthreadAttrPtr { |
22 public: | 24 public: |
23 ScopedPthreadAttrPtr(pthread_attr_t* attr) : attr_(attr) { | 25 ScopedPthreadAttrPtr(pthread_attr_t* attr) : attr_(attr) { |
24 } | 26 } |
25 ~ScopedPthreadAttrPtr() { | 27 ~ScopedPthreadAttrPtr() { |
26 pthread_attr_destroy(attr_); | 28 pthread_attr_destroy(attr_); |
27 } | 29 } |
28 | 30 |
29 private: | 31 private: |
30 pthread_attr_t* attr_; | 32 pthread_attr_t* attr_; |
31 }; | 33 }; |
32 | 34 |
33 struct ThreadContext { | 35 struct ThreadContext { |
34 void (*start_func)(); | 36 void (*start_func)(); |
35 void* thread_ptr; | 37 void* thread_ptr; |
38 void* signal_stack; | |
36 }; | 39 }; |
37 | 40 |
38 // A thread local pointer to support nacl_irt_tls. | 41 // A thread local pointer to support nacl_irt_tls. |
39 // This should be initialized at the beginning of ThreadMain, which is a thin | 42 // This should be initialized at the beginning of ThreadMain, which is a thin |
40 // wrapper of a user function called on a newly created thread, and may be | 43 // wrapper of a user function called on a newly created thread, and may be |
41 // reset via IrtTlsInit(). The pointer can be obtained via IrtTlsGet(). | 44 // reset via IrtTlsInit(). The pointer can be obtained via IrtTlsGet(). |
42 __thread void* g_thread_ptr; | 45 __thread void* g_thread_ptr; |
43 | 46 |
44 void* ThreadMain(void *arg) { | 47 void* ThreadMain(void *arg) { |
45 ::scoped_ptr<ThreadContext> context(static_cast<ThreadContext*>(arg)); | 48 ::scoped_ptr<ThreadContext> context(static_cast<ThreadContext*>(arg)); |
46 g_thread_ptr = context->thread_ptr; | 49 g_thread_ptr = context->thread_ptr; |
47 | 50 |
51 DVLOG(4) << "Registering alternate signal stack: " << context->signal_stack; | |
52 NaClSignalStackRegister(context->signal_stack); | |
Mark Seaborn
2014/04/23 03:49:50
This probably doesn't work: the seccomp sandbox wi
Junichi Uekawa
2014/04/23 07:56:15
I was wondering why my tests are succeeding and I
| |
53 | |
48 // Release the memory of context before running start_func. | 54 // Release the memory of context before running start_func. |
49 void (*start_func)() = context->start_func; | 55 void (*start_func)() = context->start_func; |
50 context.reset(); | 56 context.reset(); |
51 | 57 |
52 start_func(); | 58 start_func(); |
53 abort(); | 59 abort(); |
54 } | 60 } |
55 | 61 |
56 int IrtThreadCreate(void (*start_func)(), void* stack, void* thread_ptr) { | 62 int IrtThreadCreate(void (*start_func)(), void* stack, void* thread_ptr) { |
57 pthread_attr_t attr; | 63 pthread_attr_t attr; |
58 int error = pthread_attr_init(&attr); | 64 int error = pthread_attr_init(&attr); |
59 if (error != 0) | 65 if (error != 0) |
60 return error; | 66 return error; |
61 ScopedPthreadAttrPtr scoped_attr_ptr(&attr); | 67 ScopedPthreadAttrPtr scoped_attr_ptr(&attr); |
62 | 68 |
63 // Note: Currently we ignore the argument stack. | 69 // Note: Currently we ignore the argument stack. |
64 error = pthread_attr_setstacksize(&attr, kStackSize); | 70 error = pthread_attr_setstacksize(&attr, kStackSize); |
65 if (error != 0) | 71 if (error != 0) |
66 return error; | 72 return error; |
67 | 73 |
68 error = pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED); | 74 error = pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED); |
69 if (error != 0) | 75 if (error != 0) |
70 return error; | 76 return error; |
71 | 77 |
72 ::scoped_ptr<ThreadContext> context(new ThreadContext); | 78 ::scoped_ptr<ThreadContext> context(new ThreadContext); |
73 context->start_func = start_func; | 79 context->start_func = start_func; |
74 context->thread_ptr = thread_ptr; | 80 context->thread_ptr = thread_ptr; |
81 if (!NaClSignalStackAllocate(&context->signal_stack)) { | |
82 DVLOG(1) << "Failed to allocate alternate stack\n"; | |
83 return error; | |
84 } | |
75 | 85 |
76 pthread_t tid; | 86 pthread_t tid; |
77 error = pthread_create(&tid, &attr, ThreadMain, context.get()); | 87 error = pthread_create(&tid, &attr, ThreadMain, context.get()); |
78 if (error != 0) | 88 if (error != 0) |
79 return error; | 89 return error; |
80 | 90 |
81 // The ownership of the context is taken by the created thread. So, here we | 91 // The ownership of the context is taken by the created thread. So, here we |
82 // just manually release it. | 92 // just manually release it. |
83 ignore_result(context.release()); | 93 ignore_result(context.release()); |
84 return 0; | 94 return 0; |
(...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
117 IrtThreadNice, | 127 IrtThreadNice, |
118 }; | 128 }; |
119 | 129 |
120 const nacl_irt_tls kIrtTls = { | 130 const nacl_irt_tls kIrtTls = { |
121 IrtTlsInit, | 131 IrtTlsInit, |
122 IrtTlsGet, | 132 IrtTlsGet, |
123 }; | 133 }; |
124 | 134 |
125 } // namespace nonsfi | 135 } // namespace nonsfi |
126 } // namespace nacl | 136 } // namespace nacl |
OLD | NEW |