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/logging.h"
14 #include "base/memory/scoped_ptr.h"
15 #include "base/posix/eintr_wrapper.h"
16 #include "base/process/process_metrics.h"
17 #include "base/threading/platform_thread.h"
18 #include "base/threading/thread.h"
19 #include "build/build_config.h"
20 #include "sandbox/linux/tests/unit_tests.h"
21 #include "testing/gtest/include/gtest/gtest.h"
23 using base::PlatformThread
;
29 // These tests fail under ThreadSanitizer, see http://crbug.com/342305
30 #if !defined(THREAD_SANITIZER)
32 int GetRaceTestIterations() {
33 if (IsRunningOnValgrind()) {
42 ScopedProc() : fd_(-1) {
43 fd_
= open("/proc/", O_RDONLY
| O_DIRECTORY
);
47 ~ScopedProc() { PCHECK(0 == IGNORE_EINTR(close(fd_
))); }
49 int fd() { return fd_
; }
53 DISALLOW_COPY_AND_ASSIGN(ScopedProc
);
56 TEST(ThreadHelpers
, IsSingleThreadedBasic
) {
58 ASSERT_TRUE(ThreadHelpers::IsSingleThreaded(proc_fd
.fd()));
59 ASSERT_TRUE(ThreadHelpers::IsSingleThreaded());
61 base::Thread
thread("sandbox_tests");
62 ASSERT_TRUE(thread
.Start());
63 ASSERT_FALSE(ThreadHelpers::IsSingleThreaded(proc_fd
.fd()));
64 ASSERT_FALSE(ThreadHelpers::IsSingleThreaded());
65 // Explicitly stop the thread here to not pollute the next test.
66 ASSERT_TRUE(ThreadHelpers::StopThreadAndWatchProcFS(proc_fd
.fd(), &thread
));
69 SANDBOX_TEST(ThreadHelpers
, AssertSingleThreaded
) {
71 SANDBOX_ASSERT(ThreadHelpers::IsSingleThreaded(proc_fd
.fd()));
72 SANDBOX_ASSERT(ThreadHelpers::IsSingleThreaded());
74 ThreadHelpers::AssertSingleThreaded(proc_fd
.fd());
75 ThreadHelpers::AssertSingleThreaded();
78 TEST(ThreadHelpers
, IsSingleThreadedIterated
) {
80 ASSERT_TRUE(ThreadHelpers::IsSingleThreaded(proc_fd
.fd()));
82 // Iterate to check for race conditions.
83 for (int i
= 0; i
< GetRaceTestIterations(); ++i
) {
84 base::Thread
thread("sandbox_tests");
85 ASSERT_TRUE(thread
.Start());
86 ASSERT_FALSE(ThreadHelpers::IsSingleThreaded(proc_fd
.fd()));
87 // Explicitly stop the thread here to not pollute the next test.
88 ASSERT_TRUE(ThreadHelpers::StopThreadAndWatchProcFS(proc_fd
.fd(), &thread
));
92 TEST(ThreadHelpers
, IsSingleThreadedStartAndStop
) {
94 ASSERT_TRUE(ThreadHelpers::IsSingleThreaded(proc_fd
.fd()));
96 base::Thread
thread("sandbox_tests");
97 // This is testing for a race condition, so iterate.
98 // Manually, this has been tested with more that 1M iterations.
99 for (int i
= 0; i
< GetRaceTestIterations(); ++i
) {
100 ASSERT_TRUE(thread
.Start());
101 ASSERT_FALSE(ThreadHelpers::IsSingleThreaded(proc_fd
.fd()));
103 ASSERT_TRUE(ThreadHelpers::StopThreadAndWatchProcFS(proc_fd
.fd(), &thread
));
104 ASSERT_TRUE(ThreadHelpers::IsSingleThreaded(proc_fd
.fd()));
105 ASSERT_EQ(1, base::GetNumberOfThreads(base::GetCurrentProcessHandle()));
109 SANDBOX_TEST(ThreadHelpers
, AssertSingleThreadedAfterThreadStopped
) {
110 SANDBOX_ASSERT(ThreadHelpers::IsSingleThreaded());
112 base::Thread
thread1("sandbox_tests");
113 base::Thread
thread2("sandbox_tests");
115 for (int i
= 0; i
< GetRaceTestIterations(); ++i
) {
116 SANDBOX_ASSERT(thread1
.Start());
117 SANDBOX_ASSERT(thread2
.Start());
118 SANDBOX_ASSERT(!ThreadHelpers::IsSingleThreaded());
122 // This will wait on /proc/ to reflect the state of threads in the
124 ThreadHelpers::AssertSingleThreaded();
125 SANDBOX_ASSERT(ThreadHelpers::IsSingleThreaded());
129 // Only run this test in Debug mode, where AssertSingleThreaded() will return
130 // in less than 64ms.
134 AssertSingleThreadedDies
,
136 ThreadHelpers::GetAssertSingleThreadedErrorMessageForTests())) {
137 base::Thread
thread1("sandbox_tests");
138 SANDBOX_ASSERT(thread1
.Start());
139 ThreadHelpers::AssertSingleThreaded();
141 #endif // !defined(NDEBUG)
143 #endif // !defined(THREAD_SANITIZER)
147 } // namespace sandbox