| 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/macros.h" | 9 #include "base/macros.h" |
| 10 #include "base/memory/scoped_ptr.h" | 10 #include "base/memory/scoped_ptr.h" |
| 11 #include "components/nacl/loader/nonsfi/irt_interfaces.h" | 11 #include "components/nacl/loader/nonsfi/irt_interfaces.h" |
| 12 #include "native_client/src/trusted/service_runtime/nacl_signal.h" |
| 12 | 13 |
| 13 namespace nacl { | 14 namespace nacl { |
| 14 namespace nonsfi { | 15 namespace nonsfi { |
| 15 namespace { | 16 namespace { |
| 16 | 17 |
| 17 // We heuristically chose 1M for the stack size per thread. | 18 // We heuristically chose 1M for the stack size per thread. |
| 18 const int kStackSize = 1024 * 1024; | 19 const int kStackSize = 1024 * 1024; |
| 19 | 20 |
| 20 // For RAII of pthread_attr_t. | 21 // For RAII of pthread_attr_t. |
| 21 class ScopedPthreadAttrPtr { | 22 class ScopedPthreadAttrPtr { |
| 22 public: | 23 public: |
| 23 ScopedPthreadAttrPtr(pthread_attr_t* attr) : attr_(attr) { | 24 ScopedPthreadAttrPtr(pthread_attr_t* attr) : attr_(attr) { |
| 24 } | 25 } |
| 25 ~ScopedPthreadAttrPtr() { | 26 ~ScopedPthreadAttrPtr() { |
| 26 pthread_attr_destroy(attr_); | 27 pthread_attr_destroy(attr_); |
| 27 } | 28 } |
| 28 | 29 |
| 29 private: | 30 private: |
| 30 pthread_attr_t* attr_; | 31 pthread_attr_t* attr_; |
| 31 }; | 32 }; |
| 32 | 33 |
| 33 struct ThreadContext { | 34 struct ThreadContext { |
| 34 void (*start_func)(); | 35 void (*start_func)(); |
| 35 void* thread_ptr; | 36 void* thread_ptr; |
| 37 void* signal_stack; |
| 36 }; | 38 }; |
| 37 | 39 |
| 38 // A thread local pointer to support nacl_irt_tls. | 40 // A thread local pointer to support nacl_irt_tls. |
| 39 // This should be initialized at the beginning of ThreadMain, which is a thin | 41 // 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 | 42 // 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(). | 43 // reset via IrtTlsInit(). The pointer can be obtained via IrtTlsGet(). |
| 42 __thread void* g_thread_ptr; | 44 __thread void* g_thread_ptr; |
| 43 | 45 |
| 44 void* ThreadMain(void *arg) { | 46 void* ThreadMain(void *arg) { |
| 45 ::scoped_ptr<ThreadContext> context(static_cast<ThreadContext*>(arg)); | 47 ::scoped_ptr<ThreadContext> context(static_cast<ThreadContext*>(arg)); |
| 46 g_thread_ptr = context->thread_ptr; | 48 g_thread_ptr = context->thread_ptr; |
| 47 | 49 |
| 50 // NaClLog(4, "Registering alternate signal stack: %p\n", |
| 51 // context->signal_stack); |
| 52 NaClSignalStackRegister(context->signal_stack); |
| 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 // NaClLog(4, "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 |