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 <sys/stat.h> | 9 #include <sys/stat.h> |
10 #include <sys/types.h> | 10 #include <sys/types.h> |
(...skipping 20 matching lines...) Expand all Loading... |
31 #if !defined(THREAD_SANITIZER) | 31 #if !defined(THREAD_SANITIZER) |
32 | 32 |
33 int GetRaceTestIterations() { | 33 int GetRaceTestIterations() { |
34 if (IsRunningOnValgrind()) { | 34 if (IsRunningOnValgrind()) { |
35 return 2; | 35 return 2; |
36 } else { | 36 } else { |
37 return 1000; | 37 return 1000; |
38 } | 38 } |
39 } | 39 } |
40 | 40 |
41 class ScopedProcSelfTask { | 41 class ScopedProc { |
42 public: | 42 public: |
43 ScopedProcSelfTask() : fd_(-1) { | 43 ScopedProc() : fd_(-1) { |
44 fd_ = open("/proc/self/task/", O_RDONLY | O_DIRECTORY); | 44 fd_ = open("/proc/", O_RDONLY | O_DIRECTORY); |
45 CHECK_LE(0, fd_); | 45 CHECK_LE(0, fd_); |
46 } | 46 } |
47 | 47 |
48 ~ScopedProcSelfTask() { PCHECK(0 == IGNORE_EINTR(close(fd_))); } | 48 ~ScopedProc() { PCHECK(0 == IGNORE_EINTR(close(fd_))); } |
49 | 49 |
50 int fd() { return fd_; } | 50 int fd() { return fd_; } |
51 | 51 |
52 private: | 52 private: |
53 int fd_; | 53 int fd_; |
54 DISALLOW_COPY_AND_ASSIGN(ScopedProcSelfTask); | 54 DISALLOW_COPY_AND_ASSIGN(ScopedProc); |
55 }; | 55 }; |
56 | 56 |
57 TEST(ThreadHelpers, IsSingleThreadedBasic) { | 57 TEST(ThreadHelpers, IsSingleThreadedBasic) { |
58 ScopedProcSelfTask task; | 58 ScopedProc proc_fd; |
59 ASSERT_TRUE(ThreadHelpers::IsSingleThreaded(task.fd())); | 59 ASSERT_TRUE(ThreadHelpers::IsSingleThreaded(proc_fd.fd())); |
60 ASSERT_TRUE(ThreadHelpers::IsSingleThreaded()); | 60 ASSERT_TRUE(ThreadHelpers::IsSingleThreaded()); |
61 | 61 |
62 base::Thread thread("sandbox_tests"); | 62 base::Thread thread("sandbox_tests"); |
63 ASSERT_TRUE(thread.Start()); | 63 ASSERT_TRUE(thread.Start()); |
64 ASSERT_FALSE(ThreadHelpers::IsSingleThreaded(task.fd())); | 64 ASSERT_FALSE(ThreadHelpers::IsSingleThreaded(proc_fd.fd())); |
65 ASSERT_FALSE(ThreadHelpers::IsSingleThreaded()); | 65 ASSERT_FALSE(ThreadHelpers::IsSingleThreaded()); |
66 // Explicitly stop the thread here to not pollute the next test. | 66 // Explicitly stop the thread here to not pollute the next test. |
67 ASSERT_TRUE(ThreadHelpers::StopThreadAndWatchProcFS(task.fd(), &thread)); | 67 ASSERT_TRUE(ThreadHelpers::StopThreadAndWatchProcFS(proc_fd.fd(), &thread)); |
68 } | 68 } |
69 | 69 |
70 SANDBOX_TEST(ThreadHelpers, AssertSingleThreaded) { | 70 SANDBOX_TEST(ThreadHelpers, AssertSingleThreaded) { |
71 ScopedProcSelfTask task; | 71 ScopedProc proc_fd; |
72 SANDBOX_ASSERT(ThreadHelpers::IsSingleThreaded(task.fd())); | 72 SANDBOX_ASSERT(ThreadHelpers::IsSingleThreaded(proc_fd.fd())); |
73 SANDBOX_ASSERT(ThreadHelpers::IsSingleThreaded()); | 73 SANDBOX_ASSERT(ThreadHelpers::IsSingleThreaded()); |
74 | 74 |
75 ThreadHelpers::AssertSingleThreaded(task.fd()); | 75 ThreadHelpers::AssertSingleThreaded(proc_fd.fd()); |
76 ThreadHelpers::AssertSingleThreaded(); | 76 ThreadHelpers::AssertSingleThreaded(); |
77 } | 77 } |
78 | 78 |
79 TEST(ThreadHelpers, IsSingleThreadedIterated) { | 79 TEST(ThreadHelpers, IsSingleThreadedIterated) { |
80 ScopedProcSelfTask task; | 80 ScopedProc proc_fd; |
81 ASSERT_TRUE(ThreadHelpers::IsSingleThreaded(task.fd())); | 81 ASSERT_TRUE(ThreadHelpers::IsSingleThreaded(proc_fd.fd())); |
82 | 82 |
83 // Iterate to check for race conditions. | 83 // Iterate to check for race conditions. |
84 for (int i = 0; i < GetRaceTestIterations(); ++i) { | 84 for (int i = 0; i < GetRaceTestIterations(); ++i) { |
85 base::Thread thread("sandbox_tests"); | 85 base::Thread thread("sandbox_tests"); |
86 ASSERT_TRUE(thread.Start()); | 86 ASSERT_TRUE(thread.Start()); |
87 ASSERT_FALSE(ThreadHelpers::IsSingleThreaded(task.fd())); | 87 ASSERT_FALSE(ThreadHelpers::IsSingleThreaded(proc_fd.fd())); |
88 // Explicitly stop the thread here to not pollute the next test. | 88 // Explicitly stop the thread here to not pollute the next test. |
89 ASSERT_TRUE(ThreadHelpers::StopThreadAndWatchProcFS(task.fd(), &thread)); | 89 ASSERT_TRUE(ThreadHelpers::StopThreadAndWatchProcFS(proc_fd.fd(), &thread)); |
90 } | 90 } |
91 } | 91 } |
92 | 92 |
93 TEST(ThreadHelpers, IsSingleThreadedStartAndStop) { | 93 TEST(ThreadHelpers, IsSingleThreadedStartAndStop) { |
94 ScopedProcSelfTask task; | 94 ScopedProc proc_fd; |
95 ASSERT_TRUE(ThreadHelpers::IsSingleThreaded(task.fd())); | 95 ASSERT_TRUE(ThreadHelpers::IsSingleThreaded(proc_fd.fd())); |
96 | 96 |
97 base::Thread thread("sandbox_tests"); | 97 base::Thread thread("sandbox_tests"); |
98 // This is testing for a race condition, so iterate. | 98 // This is testing for a race condition, so iterate. |
99 // Manually, this has been tested with more that 1M iterations. | 99 // Manually, this has been tested with more that 1M iterations. |
100 for (int i = 0; i < GetRaceTestIterations(); ++i) { | 100 for (int i = 0; i < GetRaceTestIterations(); ++i) { |
101 ASSERT_TRUE(thread.Start()); | 101 ASSERT_TRUE(thread.Start()); |
102 ASSERT_FALSE(ThreadHelpers::IsSingleThreaded(task.fd())); | 102 ASSERT_FALSE(ThreadHelpers::IsSingleThreaded(proc_fd.fd())); |
103 | 103 |
104 ASSERT_TRUE(ThreadHelpers::StopThreadAndWatchProcFS(task.fd(), &thread)); | 104 ASSERT_TRUE(ThreadHelpers::StopThreadAndWatchProcFS(proc_fd.fd(), &thread)); |
105 ASSERT_TRUE(ThreadHelpers::IsSingleThreaded(task.fd())); | 105 ASSERT_TRUE(ThreadHelpers::IsSingleThreaded(proc_fd.fd())); |
106 ASSERT_EQ(1, base::GetNumberOfThreads(base::GetCurrentProcessHandle())); | 106 ASSERT_EQ(1, base::GetNumberOfThreads(base::GetCurrentProcessHandle())); |
107 } | 107 } |
108 } | 108 } |
109 | 109 |
110 SANDBOX_TEST(ThreadHelpers, AssertSingleThreadedAfterThreadStopped) { | 110 SANDBOX_TEST(ThreadHelpers, AssertSingleThreadedAfterThreadStopped) { |
111 SANDBOX_ASSERT(ThreadHelpers::IsSingleThreaded()); | 111 SANDBOX_ASSERT(ThreadHelpers::IsSingleThreaded()); |
112 | 112 |
113 base::Thread thread1("sandbox_tests"); | 113 base::Thread thread1("sandbox_tests"); |
114 base::Thread thread2("sandbox_tests"); | 114 base::Thread thread2("sandbox_tests"); |
115 | 115 |
(...skipping 23 matching lines...) Expand all Loading... |
139 SANDBOX_ASSERT(thread1.Start()); | 139 SANDBOX_ASSERT(thread1.Start()); |
140 ThreadHelpers::AssertSingleThreaded(); | 140 ThreadHelpers::AssertSingleThreaded(); |
141 } | 141 } |
142 #endif // !defined(NDEBUG) | 142 #endif // !defined(NDEBUG) |
143 | 143 |
144 #endif // !defined(THREAD_SANITIZER) | 144 #endif // !defined(THREAD_SANITIZER) |
145 | 145 |
146 } // namespace | 146 } // namespace |
147 | 147 |
148 } // namespace sandbox | 148 } // namespace sandbox |
OLD | NEW |