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 int GetRaceTestIterations() {
31 if (IsRunningOnValgrind()) {
38 class ScopedProcSelfTask
{
40 ScopedProcSelfTask() : fd_(-1) {
41 fd_
= open("/proc/self/task/", O_RDONLY
| O_DIRECTORY
);
45 ~ScopedProcSelfTask() { PCHECK(0 == IGNORE_EINTR(close(fd_
))); }
47 int fd() { return fd_
; }
51 DISALLOW_COPY_AND_ASSIGN(ScopedProcSelfTask
);
54 #if defined(THREAD_SANITIZER)
55 // These tests fail under ThreadSanitizer, see http://crbug.com/342305
56 #define MAYBE_IsSingleThreadedBasic DISABLED_IsSingleThreadedBasic
57 #define MAYBE_IsSingleThreadedIterated DISABLED_IsSingleThreadedIterated
58 #define MAYBE_IsSingleThreadedStartAndStop DISABLED_IsSingleThreadedStartAndStop
60 #define MAYBE_IsSingleThreadedBasic IsSingleThreadedBasic
61 #define MAYBE_IsSingleThreadedIterated IsSingleThreadedIterated
62 #define MAYBE_IsSingleThreadedStartAndStop IsSingleThreadedStartAndStop
65 TEST(ThreadHelpers
, MAYBE_IsSingleThreadedBasic
) {
66 ScopedProcSelfTask task
;
67 ASSERT_TRUE(ThreadHelpers::IsSingleThreaded(task
.fd()));
68 ASSERT_TRUE(ThreadHelpers::IsSingleThreaded(-1));
70 base::Thread
thread("sandbox_tests");
71 ASSERT_TRUE(thread
.Start());
72 ASSERT_FALSE(ThreadHelpers::IsSingleThreaded(task
.fd()));
73 ASSERT_FALSE(ThreadHelpers::IsSingleThreaded(-1));
74 // Explicitly stop the thread here to not pollute the next test.
75 ASSERT_TRUE(ThreadHelpers::StopThreadAndWatchProcFS(task
.fd(), &thread
));
78 TEST(ThreadHelpers
, MAYBE_IsSingleThreadedIterated
) {
79 ScopedProcSelfTask task
;
80 ASSERT_TRUE(ThreadHelpers::IsSingleThreaded(task
.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(task
.fd()));
87 // Explicitly stop the thread here to not pollute the next test.
88 ASSERT_TRUE(ThreadHelpers::StopThreadAndWatchProcFS(task
.fd(), &thread
));
92 TEST(ThreadHelpers
, MAYBE_IsSingleThreadedStartAndStop
) {
93 ScopedProcSelfTask task
;
94 ASSERT_TRUE(ThreadHelpers::IsSingleThreaded(task
.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(task
.fd()));
103 ASSERT_TRUE(ThreadHelpers::StopThreadAndWatchProcFS(task
.fd(), &thread
));
104 ASSERT_TRUE(ThreadHelpers::IsSingleThreaded(task
.fd()));
105 ASSERT_EQ(1, base::GetNumberOfThreads(base::GetCurrentProcessHandle()));
111 } // namespace sandbox