OLD | NEW |
---|---|
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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 <fcntl.h> | 5 #include <fcntl.h> |
6 #include <poll.h> | 6 #include <poll.h> |
7 #include <signal.h> | 7 #include <signal.h> |
8 #include <stdio.h> | 8 #include <stdio.h> |
9 #include <sys/resource.h> | 9 #include <sys/resource.h> |
10 #include <sys/types.h> | |
11 #include <sys/wait.h> | |
10 #include <sys/time.h> | 12 #include <sys/time.h> |
11 #include <time.h> | 13 #include <time.h> |
12 #include <unistd.h> | 14 #include <unistd.h> |
13 | 15 |
14 #include "base/debug/leak_annotations.h" | 16 #include "base/debug/leak_annotations.h" |
15 #include "base/files/file_util.h" | 17 #include "base/files/file_util.h" |
16 #include "base/posix/eintr_wrapper.h" | 18 #include "base/posix/eintr_wrapper.h" |
17 #include "base/third_party/valgrind/valgrind.h" | 19 #include "base/third_party/valgrind/valgrind.h" |
18 #include "build/build_config.h" | 20 #include "build/build_config.h" |
19 #include "sandbox/linux/tests/unit_tests.h" | 21 #include "sandbox/linux/tests/unit_tests.h" |
20 | 22 |
23 #if !defined(POLLRDHUP) | |
24 #define POLLRDHUP 0x2000 | |
Mark Seaborn
2015/05/12 18:07:37
See other comment: It would be nice to put this in
hidehiko
2015/05/13 05:43:15
Done.
| |
25 #endif | |
26 | |
21 namespace { | 27 namespace { |
22 std::string TestFailedMessage(const std::string& msg) { | 28 std::string TestFailedMessage(const std::string& msg) { |
23 return msg.empty() ? std::string() : "Actual test failure: " + msg; | 29 return msg.empty() ? std::string() : "Actual test failure: " + msg; |
24 } | 30 } |
25 | 31 |
26 int GetSubProcessTimeoutTimeInSeconds() { | 32 int GetSubProcessTimeoutTimeInSeconds() { |
27 // 10s ought to be enough for anybody. | 33 // 10s ought to be enough for anybody. |
28 return 10; | 34 return 10; |
29 } | 35 } |
30 | 36 |
(...skipping 29 matching lines...) Expand all Loading... | |
60 #endif | 66 #endif |
61 } | 67 } |
62 | 68 |
63 // TODO(jln): figure out why base/.../dynamic_annotations.h's | 69 // TODO(jln): figure out why base/.../dynamic_annotations.h's |
64 // RunningOnValgrind() cannot link. | 70 // RunningOnValgrind() cannot link. |
65 bool IsRunningOnValgrind() { return RUNNING_ON_VALGRIND; } | 71 bool IsRunningOnValgrind() { return RUNNING_ON_VALGRIND; } |
66 | 72 |
67 static const int kExpectedValue = 42; | 73 static const int kExpectedValue = 42; |
68 static const int kIgnoreThisTest = 43; | 74 static const int kIgnoreThisTest = 43; |
69 static const int kExitWithAssertionFailure = 1; | 75 static const int kExitWithAssertionFailure = 1; |
76 #if !defined(OS_NACL_NONSFI) | |
70 static const int kExitForTimeout = 2; | 77 static const int kExitForTimeout = 2; |
78 #endif | |
71 | 79 |
72 #if !defined(OS_ANDROID) | 80 #if !defined(OS_ANDROID) && !defined(OS_NACL_NONSFI) |
73 // This is due to StackDumpSignalHandler() performing _exit(1). | 81 // This is due to StackDumpSignalHandler() performing _exit(1). |
74 // TODO(jln): get rid of the collision with kExitWithAssertionFailure. | 82 // TODO(jln): get rid of the collision with kExitWithAssertionFailure. |
75 const int kExitAfterSIGSEGV = 1; | 83 const int kExitAfterSIGSEGV = 1; |
76 #endif | 84 #endif |
77 | 85 |
86 // PNaCl toolchain's signal ABIs are incompatible with Linux's. | |
87 // So, for simplicity, just drop the "timeout" feature from unittest framework. | |
Mark Seaborn
2015/05/12 18:07:37
You might say that we'll rely on Buildbot's timeou
hidehiko
2015/05/13 05:43:15
Done.
| |
88 #if !defined(OS_NACL_NONSFI) | |
78 static void SigAlrmHandler(int) { | 89 static void SigAlrmHandler(int) { |
79 const char failure_message[] = "Timeout reached!\n"; | 90 const char failure_message[] = "Timeout reached!\n"; |
80 // Make sure that we never block here. | 91 // Make sure that we never block here. |
81 if (!fcntl(2, F_SETFL, O_NONBLOCK)) { | 92 if (!fcntl(2, F_SETFL, O_NONBLOCK)) { |
82 ignore_result(write(2, failure_message, sizeof(failure_message) - 1)); | 93 ignore_result(write(2, failure_message, sizeof(failure_message) - 1)); |
83 } | 94 } |
84 _exit(kExitForTimeout); | 95 _exit(kExitForTimeout); |
85 } | 96 } |
86 | 97 |
87 // Set a timeout with a handler that will automatically fail the | 98 // Set a timeout with a handler that will automatically fail the |
(...skipping 11 matching lines...) Expand all Loading... | |
99 // is expecting to handle SIGALRM. | 110 // is expecting to handle SIGALRM. |
100 SANDBOX_ASSERT((old_act.sa_flags & SA_SIGINFO) == 0); | 111 SANDBOX_ASSERT((old_act.sa_flags & SA_SIGINFO) == 0); |
101 SANDBOX_ASSERT(old_act.sa_handler == SIG_DFL); | 112 SANDBOX_ASSERT(old_act.sa_handler == SIG_DFL); |
102 sigset_t sigalrm_set; | 113 sigset_t sigalrm_set; |
103 SANDBOX_ASSERT(sigemptyset(&sigalrm_set) == 0); | 114 SANDBOX_ASSERT(sigemptyset(&sigalrm_set) == 0); |
104 SANDBOX_ASSERT(sigaddset(&sigalrm_set, SIGALRM) == 0); | 115 SANDBOX_ASSERT(sigaddset(&sigalrm_set, SIGALRM) == 0); |
105 SANDBOX_ASSERT(sigprocmask(SIG_UNBLOCK, &sigalrm_set, NULL) == 0); | 116 SANDBOX_ASSERT(sigprocmask(SIG_UNBLOCK, &sigalrm_set, NULL) == 0); |
106 SANDBOX_ASSERT(alarm(time_in_seconds) == 0); // There should be no previous | 117 SANDBOX_ASSERT(alarm(time_in_seconds) == 0); // There should be no previous |
107 // alarm. | 118 // alarm. |
108 } | 119 } |
120 #endif // !defined(OS_NACL_NONSFI) | |
109 | 121 |
110 // Runs a test in a sub-process. This is necessary for most of the code | 122 // Runs a test in a sub-process. This is necessary for most of the code |
111 // in the BPF sandbox, as it potentially makes global state changes and as | 123 // in the BPF sandbox, as it potentially makes global state changes and as |
112 // it also tends to raise fatal errors, if the code has been used in an | 124 // it also tends to raise fatal errors, if the code has been used in an |
113 // insecure manner. | 125 // insecure manner. |
114 void UnitTests::RunTestInProcess(SandboxTestRunner* test_runner, | 126 void UnitTests::RunTestInProcess(SandboxTestRunner* test_runner, |
115 DeathCheck death, | 127 DeathCheck death, |
116 const void* death_aux) { | 128 const void* death_aux) { |
117 CHECK(test_runner); | 129 CHECK(test_runner); |
118 // We need to fork(), so we can't be multi-threaded, as threads could hold | 130 // We need to fork(), so we can't be multi-threaded, as threads could hold |
(...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
156 // In child process | 168 // In child process |
157 // Redirect stderr to our pipe. This way, we can capture all error | 169 // Redirect stderr to our pipe. This way, we can capture all error |
158 // messages, if we decide we want to do so in our tests. | 170 // messages, if we decide we want to do so in our tests. |
159 SANDBOX_ASSERT(dup2(fds[1], 2) == 2); | 171 SANDBOX_ASSERT(dup2(fds[1], 2) == 2); |
160 SANDBOX_ASSERT(!close(fds[0])); | 172 SANDBOX_ASSERT(!close(fds[0])); |
161 SANDBOX_ASSERT(!close(fds[1])); | 173 SANDBOX_ASSERT(!close(fds[1])); |
162 | 174 |
163 // Don't set a timeout if running on Valgrind, since it's generally much | 175 // Don't set a timeout if running on Valgrind, since it's generally much |
164 // slower. | 176 // slower. |
165 if (!IsRunningOnValgrind()) { | 177 if (!IsRunningOnValgrind()) { |
178 #if !defined(OS_NACL_NONSFI) | |
166 SetProcessTimeout(GetSubProcessTimeoutTimeInSeconds()); | 179 SetProcessTimeout(GetSubProcessTimeoutTimeInSeconds()); |
180 #endif | |
167 } | 181 } |
168 | 182 |
169 // Disable core files. They are not very useful for our individual test | 183 // Disable core files. They are not very useful for our individual test |
170 // cases. | 184 // cases. |
171 struct rlimit no_core = {0}; | 185 struct rlimit no_core = {0}; |
172 setrlimit(RLIMIT_CORE, &no_core); | 186 setrlimit(RLIMIT_CORE, &no_core); |
173 | 187 |
174 test_runner->Run(); | 188 test_runner->Run(); |
175 if (test_runner->ShouldCheckForLeaks()) { | 189 if (test_runner->ShouldCheckForLeaks()) { |
176 #if defined(LEAK_SANITIZER) | 190 #if defined(LEAK_SANITIZER) |
(...skipping 98 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
275 #endif | 289 #endif |
276 EXPECT_FALSE(subprocess_exited_without_matching_message) << details; | 290 EXPECT_FALSE(subprocess_exited_without_matching_message) << details; |
277 } | 291 } |
278 | 292 |
279 void UnitTests::DeathSEGVMessage(int status, | 293 void UnitTests::DeathSEGVMessage(int status, |
280 const std::string& msg, | 294 const std::string& msg, |
281 const void* aux) { | 295 const void* aux) { |
282 std::string details(TestFailedMessage(msg)); | 296 std::string details(TestFailedMessage(msg)); |
283 const char* expected_msg = static_cast<const char*>(aux); | 297 const char* expected_msg = static_cast<const char*>(aux); |
284 | 298 |
285 #if defined(OS_ANDROID) | 299 #if defined(OS_ANDROID) || defined(OS_NACL_NONSFI) |
286 const bool subprocess_got_sigsegv = | 300 const bool subprocess_got_sigsegv = |
287 WIFSIGNALED(status) && (SIGSEGV == WTERMSIG(status)); | 301 WIFSIGNALED(status) && (SIGSEGV == WTERMSIG(status)); |
288 #else | 302 #else |
289 const bool subprocess_got_sigsegv = | 303 const bool subprocess_got_sigsegv = |
290 WIFEXITED(status) && (kExitAfterSIGSEGV == WEXITSTATUS(status)); | 304 WIFEXITED(status) && (kExitAfterSIGSEGV == WEXITSTATUS(status)); |
291 #endif | 305 #endif |
292 | 306 |
293 ASSERT_TRUE(subprocess_got_sigsegv) << "Exit status: " << status | 307 ASSERT_TRUE(subprocess_got_sigsegv) << "Exit status: " << status |
294 << " " << details; | 308 << " " << details; |
295 | 309 |
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
327 fflush(stderr); | 341 fflush(stderr); |
328 _exit(kExitWithAssertionFailure); | 342 _exit(kExitWithAssertionFailure); |
329 } | 343 } |
330 | 344 |
331 void UnitTests::IgnoreThisTest() { | 345 void UnitTests::IgnoreThisTest() { |
332 fflush(stderr); | 346 fflush(stderr); |
333 _exit(kIgnoreThisTest); | 347 _exit(kIgnoreThisTest); |
334 } | 348 } |
335 | 349 |
336 } // namespace | 350 } // namespace |
OLD | NEW |