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
3 // found in the LICENSE file.
5 #include "sandbox/linux/services/thread_helpers.h"
10 #include <sys/types.h>
13 #include "base/basictypes.h"
14 #include "base/logging.h"
15 #include "base/memory/scoped_ptr.h"
16 #include "base/posix/eintr_wrapper.h"
17 #include "base/process/process_metrics.h"
18 #include "base/threading/platform_thread.h"
19 #include "base/threading/thread.h"
20 #include "build/build_config.h"
21 #include "sandbox/linux/tests/unit_tests.h"
22 #include "testing/gtest/include/gtest/gtest.h"
24 using base::PlatformThread
;
30 // These tests fail under ThreadSanitizer, see http://crbug.com/342305
31 #if !defined(THREAD_SANITIZER)
33 int GetRaceTestIterations() {
34 if (IsRunningOnValgrind()) {
43 ScopedProc() : fd_(-1) {
44 fd_
= open("/proc/", O_RDONLY
| O_DIRECTORY
);
48 ~ScopedProc() { PCHECK(0 == IGNORE_EINTR(close(fd_
))); }
50 int fd() { return fd_
; }
54 DISALLOW_COPY_AND_ASSIGN(ScopedProc
);
57 TEST(ThreadHelpers
, IsSingleThreadedBasic
) {
59 ASSERT_TRUE(ThreadHelpers::IsSingleThreaded(proc_fd
.fd()));
60 ASSERT_TRUE(ThreadHelpers::IsSingleThreaded());
62 base::Thread
thread("sandbox_tests");
63 ASSERT_TRUE(thread
.Start());
64 ASSERT_FALSE(ThreadHelpers::IsSingleThreaded(proc_fd
.fd()));
65 ASSERT_FALSE(ThreadHelpers::IsSingleThreaded());
66 // Explicitly stop the thread here to not pollute the next test.
67 ASSERT_TRUE(ThreadHelpers::StopThreadAndWatchProcFS(proc_fd
.fd(), &thread
));
70 SANDBOX_TEST(ThreadHelpers
, AssertSingleThreaded
) {
72 SANDBOX_ASSERT(ThreadHelpers::IsSingleThreaded(proc_fd
.fd()));
73 SANDBOX_ASSERT(ThreadHelpers::IsSingleThreaded());
75 ThreadHelpers::AssertSingleThreaded(proc_fd
.fd());
76 ThreadHelpers::AssertSingleThreaded();
79 TEST(ThreadHelpers
, IsSingleThreadedIterated
) {
81 ASSERT_TRUE(ThreadHelpers::IsSingleThreaded(proc_fd
.fd()));
83 // Iterate to check for race conditions.
84 for (int i
= 0; i
< GetRaceTestIterations(); ++i
) {
85 base::Thread
thread("sandbox_tests");
86 ASSERT_TRUE(thread
.Start());
87 ASSERT_FALSE(ThreadHelpers::IsSingleThreaded(proc_fd
.fd()));
88 // Explicitly stop the thread here to not pollute the next test.
89 ASSERT_TRUE(ThreadHelpers::StopThreadAndWatchProcFS(proc_fd
.fd(), &thread
));
93 TEST(ThreadHelpers
, IsSingleThreadedStartAndStop
) {
95 ASSERT_TRUE(ThreadHelpers::IsSingleThreaded(proc_fd
.fd()));
97 base::Thread
thread("sandbox_tests");
98 // This is testing for a race condition, so iterate.
99 // Manually, this has been tested with more that 1M iterations.
100 for (int i
= 0; i
< GetRaceTestIterations(); ++i
) {
101 ASSERT_TRUE(thread
.Start());
102 ASSERT_FALSE(ThreadHelpers::IsSingleThreaded(proc_fd
.fd()));
104 ASSERT_TRUE(ThreadHelpers::StopThreadAndWatchProcFS(proc_fd
.fd(), &thread
));
105 ASSERT_TRUE(ThreadHelpers::IsSingleThreaded(proc_fd
.fd()));
106 ASSERT_EQ(1, base::GetNumberOfThreads(base::GetCurrentProcessHandle()));
110 SANDBOX_TEST(ThreadHelpers
, AssertSingleThreadedAfterThreadStopped
) {
111 SANDBOX_ASSERT(ThreadHelpers::IsSingleThreaded());
113 base::Thread
thread1("sandbox_tests");
114 base::Thread
thread2("sandbox_tests");
116 for (int i
= 0; i
< GetRaceTestIterations(); ++i
) {
117 SANDBOX_ASSERT(thread1
.Start());
118 SANDBOX_ASSERT(thread2
.Start());
119 SANDBOX_ASSERT(!ThreadHelpers::IsSingleThreaded());
123 // This will wait on /proc/ to reflect the state of threads in the
125 ThreadHelpers::AssertSingleThreaded();
126 SANDBOX_ASSERT(ThreadHelpers::IsSingleThreaded());
130 // Only run this test in Debug mode, where AssertSingleThreaded() will return
131 // in less than 64ms.
135 AssertSingleThreadedDies
,
137 ThreadHelpers::GetAssertSingleThreadedErrorMessageForTests())) {
138 base::Thread
thread1("sandbox_tests");
139 SANDBOX_ASSERT(thread1
.Start());
140 ThreadHelpers::AssertSingleThreaded();
142 #endif // !defined(NDEBUG)
144 #endif // !defined(THREAD_SANITIZER)
148 } // namespace sandbox