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(ThreadHelpers::StartThreadAndWatchProcFS(proc_fd
.fd(), &thread
));
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");
86 ThreadHelpers::StartThreadAndWatchProcFS(proc_fd
.fd(), &thread
));
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
) {
102 ThreadHelpers::StartThreadAndWatchProcFS(proc_fd
.fd(), &thread
));
103 ASSERT_FALSE(ThreadHelpers::IsSingleThreaded(proc_fd
.fd()));
105 ASSERT_TRUE(ThreadHelpers::StopThreadAndWatchProcFS(proc_fd
.fd(), &thread
));
106 ASSERT_TRUE(ThreadHelpers::IsSingleThreaded(proc_fd
.fd()));
107 ASSERT_EQ(1, base::GetNumberOfThreads(base::GetCurrentProcessHandle()));
111 SANDBOX_TEST(ThreadHelpers
, AssertSingleThreadedAfterThreadStopped
) {
113 SANDBOX_ASSERT(ThreadHelpers::IsSingleThreaded());
115 base::Thread
thread1("sandbox_tests");
116 base::Thread
thread2("sandbox_tests");
118 for (int i
= 0; i
< GetRaceTestIterations(); ++i
) {
120 ThreadHelpers::StartThreadAndWatchProcFS(proc_fd
.fd(), &thread1
));
122 ThreadHelpers::StartThreadAndWatchProcFS(proc_fd
.fd(), &thread2
));
123 SANDBOX_ASSERT(!ThreadHelpers::IsSingleThreaded());
127 // This will wait on /proc/ to reflect the state of threads in the
129 ThreadHelpers::AssertSingleThreaded();
130 SANDBOX_ASSERT(ThreadHelpers::IsSingleThreaded());
134 // Only run this test in Debug mode, where AssertSingleThreaded() will return
135 // in less than 64ms.
139 AssertSingleThreadedDies
,
141 ThreadHelpers::GetAssertSingleThreadedErrorMessageForTests())) {
142 base::Thread
thread1("sandbox_tests");
143 SANDBOX_ASSERT(thread1
.Start());
144 ThreadHelpers::AssertSingleThreaded();
146 #endif // !defined(NDEBUG)
148 #endif // !defined(THREAD_SANITIZER)
152 } // namespace sandbox