Chromium Code Reviews| 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 "sandbox/linux/services/thread_helpers.h" | 5 #include "sandbox/linux/services/thread_helpers.h" |
| 6 | 6 |
| 7 #include <errno.h> | 7 #include <errno.h> |
| 8 #include <fcntl.h> | 8 #include <fcntl.h> |
| 9 #include <signal.h> | 9 #include <signal.h> |
| 10 #include <sys/types.h> | 10 #include <sys/types.h> |
| 11 #include <sys/stat.h> | 11 #include <sys/stat.h> |
| 12 #include <unistd.h> | 12 #include <unistd.h> |
| 13 | 13 |
| 14 #include <string> | 14 #include <string> |
| 15 | 15 |
| 16 #include "base/basictypes.h" | 16 #include "base/basictypes.h" |
| 17 #include "base/bind.h" | |
| 18 #include "base/callback.h" | |
| 19 #include "base/files/scoped_file.h" | |
| 17 #include "base/logging.h" | 20 #include "base/logging.h" |
| 18 #include "base/posix/eintr_wrapper.h" | 21 #include "base/posix/eintr_wrapper.h" |
| 19 #include "base/strings/string_number_conversions.h" | 22 #include "base/strings/string_number_conversions.h" |
| 20 #include "base/threading/platform_thread.h" | 23 #include "base/threading/platform_thread.h" |
| 21 #include "base/threading/thread.h" | 24 #include "base/threading/thread.h" |
| 22 | 25 |
| 23 namespace sandbox { | 26 namespace sandbox { |
| 24 | 27 |
| 25 namespace { | 28 namespace { |
| 26 | 29 |
| 30 const char kAssertSingleThreadedError[] = | |
| 31 "Current process is not mono-threaded!"; | |
| 32 | |
| 27 bool IsSingleThreadedImpl(int proc_self_task) { | 33 bool IsSingleThreadedImpl(int proc_self_task) { |
| 28 CHECK_LE(0, proc_self_task); | 34 CHECK_LE(0, proc_self_task); |
| 29 struct stat task_stat; | 35 struct stat task_stat; |
| 30 int fstat_ret = fstat(proc_self_task, &task_stat); | 36 int fstat_ret = fstat(proc_self_task, &task_stat); |
| 31 PCHECK(0 == fstat_ret); | 37 PCHECK(0 == fstat_ret); |
| 32 | 38 |
| 33 // At least "..", "." and the current thread should be present. | 39 // At least "..", "." and the current thread should be present. |
| 34 CHECK_LE(3UL, task_stat.st_nlink); | 40 CHECK_LE(3UL, task_stat.st_nlink); |
| 35 // Counting threads via /proc/self/task could be racy. For the purpose of | 41 // Counting threads via /proc/self/task could be racy. For the purpose of |
| 36 // determining if the current proces is monothreaded it works: if at any | 42 // determining if the current proces is monothreaded it works: if at any |
| 37 // time it becomes monothreaded, it'll stay so. | 43 // time it becomes monothreaded, it'll stay so. |
| 38 return task_stat.st_nlink == 3; | 44 return task_stat.st_nlink == 3; |
| 39 } | 45 } |
| 40 | 46 |
| 47 bool IsThreadPresentInProcFS(int proc_self_task, | |
| 48 const std::string& thread_id_dir_str) { | |
| 49 struct stat task_stat; | |
| 50 const int fstat_ret = | |
| 51 fstatat(proc_self_task, thread_id_dir_str.c_str(), &task_stat, 0); | |
| 52 if (fstat_ret < 0) { | |
| 53 PCHECK(ENOENT == errno); | |
| 54 return false; | |
| 55 } | |
| 56 return true; | |
| 57 } | |
| 58 | |
| 59 // Run |cb| in a loop until it returns false. Every time |cb| runs, sleep | |
| 60 // for an exponentially increasing amount of time. |cb| is expected to return | |
| 61 // false very quickly and this will crash if it doesn't happen withing ~64ms on | |
| 62 // Debug builds (2s on Release builds). | |
| 63 // This is guaranteed to not sleep more than twice as much as the bare minimum | |
| 64 // amount of time. | |
| 65 void RunUntilFalse(const base::Callback<bool(void)>& cb) { | |
| 66 unsigned int iterations = 0; | |
| 67 // Run |cb| with an exponential back-off, sleeping 2^iterations nanoseconds | |
| 68 // in nanosleep(2). | |
| 69 // Note: the clock may not allow for nanosecond granularity, in this case the | |
| 70 // first iterations would sleep a tiny bit more instead, which would not | |
| 71 // change the calculations significantly. | |
| 72 while (true) { | |
| 73 if (!cb.Run()) { | |
| 74 return; | |
| 75 } | |
| 76 | |
| 77 // Increase the waiting time exponentially. | |
| 78 struct timespec ts = {0, 1L << iterations /* nanoseconds */}; | |
| 79 PCHECK(0 == HANDLE_EINTR(nanosleep(&ts, &ts))); | |
| 80 ++iterations; | |
| 81 | |
| 82 #if defined(NDEBUG) | |
| 83 // In Release mode, crash after 30 iterations, which means having spent | |
| 84 // roughly 2s in | |
| 85 // nanosleep(2) cumulatively. | |
| 86 const unsigned int kMaxIterations = 30U; | |
| 87 #else | |
| 88 // In practice, this never goes through more than a couple iterations. In | |
| 89 // debug mode, crash after 64ms (+ eventually 25 times the granularity of | |
| 90 // the clock) in nanosleep(2). This ensures that this is not becoming too | |
| 91 // slow. | |
| 92 const unsigned int kMaxIterations = 25U; | |
| 93 #endif | |
| 94 if (iterations >= kMaxIterations) { | |
| 95 LOG(FATAL) << kAssertSingleThreadedError << " (iterations:" << iterations | |
|
rickyz (no longer on Chrome)
2015/02/05 00:11:56
nit: space after the colon.
jln (very slow on Chromium)
2015/02/05 00:36:49
Done.
| |
| 96 << ")"; | |
| 97 } | |
| 98 } | |
| 99 NOTREACHED(); | |
| 100 } | |
| 101 | |
| 102 // Return a ScopedFD to /proc/self/task/. If |proc_self_task| is -1, try to | |
| 103 // open it directly, otherwise duplicate it. | |
| 104 base::ScopedFD OpenProcSelfTask(int proc_self_task) { | |
| 105 DCHECK_LE(-1, proc_self_task); | |
| 106 if (-1 == proc_self_task) { | |
| 107 return base::ScopedFD(HANDLE_EINTR( | |
| 108 open("/proc/self/task/", O_RDONLY | O_DIRECTORY | O_CLOEXEC))); | |
| 109 } | |
| 110 | |
| 111 return base::ScopedFD(HANDLE_EINTR( | |
|
rickyz (no longer on Chrome)
2015/02/05 00:11:56
Can we just use dup instead? Though maybe it's cle
jln (very slow on Chromium)
2015/02/05 00:36:49
It requires dup3, which I think is not easily avai
| |
| 112 openat(proc_self_task, "./", O_RDONLY | O_DIRECTORY | O_CLOEXEC))); | |
| 113 } | |
| 114 | |
| 115 bool IsMultiThreaded(int proc_self_task) { | |
| 116 return !ThreadHelpers::IsSingleThreaded(proc_self_task); | |
| 117 } | |
| 118 | |
| 41 } // namespace | 119 } // namespace |
| 42 | 120 |
| 121 // static | |
| 43 bool ThreadHelpers::IsSingleThreaded(int proc_self_task) { | 122 bool ThreadHelpers::IsSingleThreaded(int proc_self_task) { |
| 44 DCHECK_LE(-1, proc_self_task); | 123 DCHECK_LE(-1, proc_self_task); |
| 45 if (-1 == proc_self_task) { | 124 base::ScopedFD task_fd(OpenProcSelfTask(proc_self_task)); |
| 46 const int task_fd = | 125 CHECK(task_fd.is_valid()); |
| 47 open("/proc/self/task/", O_RDONLY | O_DIRECTORY | O_CLOEXEC); | 126 return IsSingleThreadedImpl(task_fd.get()); |
| 48 PCHECK(0 <= task_fd); | |
| 49 const bool result = IsSingleThreadedImpl(task_fd); | |
| 50 PCHECK(0 == IGNORE_EINTR(close(task_fd))); | |
| 51 return result; | |
| 52 } else { | |
| 53 return IsSingleThreadedImpl(proc_self_task); | |
| 54 } | |
| 55 } | 127 } |
| 56 | 128 |
| 129 // static | |
| 130 void ThreadHelpers::AssertSingleThreaded(int proc_self_task) { | |
| 131 const base::Callback<bool(void)> cb = | |
| 132 base::Bind(&IsMultiThreaded, proc_self_task); | |
| 133 RunUntilFalse(cb); | |
| 134 } | |
| 135 | |
| 136 // static | |
| 57 bool ThreadHelpers::StopThreadAndWatchProcFS(int proc_self_task, | 137 bool ThreadHelpers::StopThreadAndWatchProcFS(int proc_self_task, |
| 58 base::Thread* thread) { | 138 base::Thread* thread) { |
| 59 DCHECK_LE(0, proc_self_task); | 139 DCHECK_LE(0, proc_self_task); |
| 60 DCHECK(thread); | 140 DCHECK(thread); |
| 61 const base::PlatformThreadId thread_id = thread->thread_id(); | 141 const base::PlatformThreadId thread_id = thread->thread_id(); |
| 62 const std::string thread_id_dir_str = base::IntToString(thread_id) + "/"; | 142 const std::string thread_id_dir_str = base::IntToString(thread_id) + "/"; |
| 63 | 143 |
| 64 // The kernel is at liberty to wake the thread id futex before updating | 144 // The kernel is at liberty to wake the thread id futex before updating |
| 65 // /proc. Following Stop(), the thread is joined, but entries in /proc may | 145 // /proc. Following Stop(), the thread is joined, but entries in /proc may |
| 66 // not have been updated. | 146 // not have been updated. |
| 67 thread->Stop(); | 147 thread->Stop(); |
| 68 | 148 |
| 69 unsigned int iterations = 0; | 149 const base::Callback<bool(void)> cb = |
| 70 bool thread_present_in_procfs = true; | 150 base::Bind(&IsThreadPresentInProcFS, proc_self_task, thread_id_dir_str); |
| 71 // Poll /proc with an exponential back-off, sleeping 2^iterations nanoseconds | |
| 72 // in nanosleep(2). | |
| 73 // Note: the clock may not allow for nanosecond granularity, in this case the | |
| 74 // first iterations would sleep a tiny bit more instead, which would not | |
| 75 // change the calculations significantly. | |
| 76 while (thread_present_in_procfs) { | |
| 77 struct stat task_stat; | |
| 78 const int fstat_ret = | |
| 79 fstatat(proc_self_task, thread_id_dir_str.c_str(), &task_stat, 0); | |
| 80 if (fstat_ret < 0) { | |
| 81 PCHECK(ENOENT == errno); | |
| 82 // The thread disappeared from /proc, we're done. | |
| 83 thread_present_in_procfs = false; | |
| 84 break; | |
| 85 } | |
| 86 // Increase the waiting time exponentially. | |
| 87 struct timespec ts = {0, 1L << iterations /* nanoseconds */}; | |
| 88 PCHECK(0 == HANDLE_EINTR(nanosleep(&ts, &ts))); | |
| 89 ++iterations; | |
| 90 | 151 |
| 91 // Crash after 30 iterations, which means having spent roughly 2s in | 152 RunUntilFalse(cb); |
| 92 // nanosleep(2) cumulatively. | |
| 93 CHECK_GT(30U, iterations); | |
| 94 // In practice, this never goes through more than a couple iterations. In | |
| 95 // debug mode, crash after 64ms (+ eventually 25 times the granularity of | |
| 96 // the clock) in nanosleep(2). | |
| 97 DCHECK_GT(25U, iterations); | |
| 98 } | |
| 99 | 153 |
| 100 return true; | 154 return true; |
| 101 } | 155 } |
| 102 | 156 |
| 157 // static | |
| 158 const char* ThreadHelpers::GetAssertSingleThreadedErrorMessageForTests() { | |
| 159 return kAssertSingleThreadedError; | |
| 160 } | |
| 161 | |
| 103 } // namespace sandbox | 162 } // namespace sandbox |
| OLD | NEW |