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 |
| 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, we just drop the "timeout" feature from unittest |
| 88 // framework. |
| 89 #if !defined(OS_NACL_NONSFI) |
78 static void SigAlrmHandler(int) { | 90 static void SigAlrmHandler(int) { |
79 const char failure_message[] = "Timeout reached!\n"; | 91 const char failure_message[] = "Timeout reached!\n"; |
80 // Make sure that we never block here. | 92 // Make sure that we never block here. |
81 if (!fcntl(2, F_SETFL, O_NONBLOCK)) { | 93 if (!fcntl(2, F_SETFL, O_NONBLOCK)) { |
82 ignore_result(write(2, failure_message, sizeof(failure_message) - 1)); | 94 ignore_result(write(2, failure_message, sizeof(failure_message) - 1)); |
83 } | 95 } |
84 _exit(kExitForTimeout); | 96 _exit(kExitForTimeout); |
85 } | 97 } |
86 | 98 |
87 // Set a timeout with a handler that will automatically fail the | 99 // Set a timeout with a handler that will automatically fail the |
(...skipping 11 matching lines...) Expand all Loading... |
99 // is expecting to handle SIGALRM. | 111 // is expecting to handle SIGALRM. |
100 SANDBOX_ASSERT((old_act.sa_flags & SA_SIGINFO) == 0); | 112 SANDBOX_ASSERT((old_act.sa_flags & SA_SIGINFO) == 0); |
101 SANDBOX_ASSERT(old_act.sa_handler == SIG_DFL); | 113 SANDBOX_ASSERT(old_act.sa_handler == SIG_DFL); |
102 sigset_t sigalrm_set; | 114 sigset_t sigalrm_set; |
103 SANDBOX_ASSERT(sigemptyset(&sigalrm_set) == 0); | 115 SANDBOX_ASSERT(sigemptyset(&sigalrm_set) == 0); |
104 SANDBOX_ASSERT(sigaddset(&sigalrm_set, SIGALRM) == 0); | 116 SANDBOX_ASSERT(sigaddset(&sigalrm_set, SIGALRM) == 0); |
105 SANDBOX_ASSERT(sigprocmask(SIG_UNBLOCK, &sigalrm_set, NULL) == 0); | 117 SANDBOX_ASSERT(sigprocmask(SIG_UNBLOCK, &sigalrm_set, NULL) == 0); |
106 SANDBOX_ASSERT(alarm(time_in_seconds) == 0); // There should be no previous | 118 SANDBOX_ASSERT(alarm(time_in_seconds) == 0); // There should be no previous |
107 // alarm. | 119 // alarm. |
108 } | 120 } |
| 121 #endif // !defined(OS_NACL_NONSFI) |
109 | 122 |
110 // Runs a test in a sub-process. This is necessary for most of the code | 123 // 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 | 124 // 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 | 125 // it also tends to raise fatal errors, if the code has been used in an |
113 // insecure manner. | 126 // insecure manner. |
114 void UnitTests::RunTestInProcess(SandboxTestRunner* test_runner, | 127 void UnitTests::RunTestInProcess(SandboxTestRunner* test_runner, |
115 DeathCheck death, | 128 DeathCheck death, |
116 const void* death_aux) { | 129 const void* death_aux) { |
117 CHECK(test_runner); | 130 CHECK(test_runner); |
118 // We need to fork(), so we can't be multi-threaded, as threads could hold | 131 // We need to fork(), so we can't be multi-threaded, as threads could hold |
(...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
155 if (!pid) { | 168 if (!pid) { |
156 // In child process | 169 // In child process |
157 // Redirect stderr to our pipe. This way, we can capture all error | 170 // 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. | 171 // messages, if we decide we want to do so in our tests. |
159 SANDBOX_ASSERT(dup2(fds[1], 2) == 2); | 172 SANDBOX_ASSERT(dup2(fds[1], 2) == 2); |
160 SANDBOX_ASSERT(!close(fds[0])); | 173 SANDBOX_ASSERT(!close(fds[0])); |
161 SANDBOX_ASSERT(!close(fds[1])); | 174 SANDBOX_ASSERT(!close(fds[1])); |
162 | 175 |
163 // Don't set a timeout if running on Valgrind, since it's generally much | 176 // Don't set a timeout if running on Valgrind, since it's generally much |
164 // slower. | 177 // slower. |
| 178 #if !defined(OS_NACL_NONSFI) |
165 if (!IsRunningOnValgrind()) { | 179 if (!IsRunningOnValgrind()) { |
166 SetProcessTimeout(GetSubProcessTimeoutTimeInSeconds()); | 180 SetProcessTimeout(GetSubProcessTimeoutTimeInSeconds()); |
167 } | 181 } |
| 182 #endif |
168 | 183 |
169 // Disable core files. They are not very useful for our individual test | 184 // Disable core files. They are not very useful for our individual test |
170 // cases. | 185 // cases. |
171 struct rlimit no_core = {0}; | 186 struct rlimit no_core = {0}; |
172 setrlimit(RLIMIT_CORE, &no_core); | 187 setrlimit(RLIMIT_CORE, &no_core); |
173 | 188 |
174 test_runner->Run(); | 189 test_runner->Run(); |
175 if (test_runner->ShouldCheckForLeaks()) { | 190 if (test_runner->ShouldCheckForLeaks()) { |
176 #if defined(LEAK_SANITIZER) | 191 #if defined(LEAK_SANITIZER) |
177 __lsan_do_leak_check(); | 192 __lsan_do_leak_check(); |
(...skipping 97 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
275 #endif | 290 #endif |
276 EXPECT_FALSE(subprocess_exited_without_matching_message) << details; | 291 EXPECT_FALSE(subprocess_exited_without_matching_message) << details; |
277 } | 292 } |
278 | 293 |
279 void UnitTests::DeathSEGVMessage(int status, | 294 void UnitTests::DeathSEGVMessage(int status, |
280 const std::string& msg, | 295 const std::string& msg, |
281 const void* aux) { | 296 const void* aux) { |
282 std::string details(TestFailedMessage(msg)); | 297 std::string details(TestFailedMessage(msg)); |
283 const char* expected_msg = static_cast<const char*>(aux); | 298 const char* expected_msg = static_cast<const char*>(aux); |
284 | 299 |
285 #if defined(OS_ANDROID) | 300 #if defined(OS_ANDROID) || defined(OS_NACL_NONSFI) |
286 const bool subprocess_got_sigsegv = | 301 const bool subprocess_got_sigsegv = |
287 WIFSIGNALED(status) && (SIGSEGV == WTERMSIG(status)); | 302 WIFSIGNALED(status) && (SIGSEGV == WTERMSIG(status)); |
288 #else | 303 #else |
289 const bool subprocess_got_sigsegv = | 304 const bool subprocess_got_sigsegv = |
290 WIFEXITED(status) && (kExitAfterSIGSEGV == WEXITSTATUS(status)); | 305 WIFEXITED(status) && (kExitAfterSIGSEGV == WEXITSTATUS(status)); |
291 #endif | 306 #endif |
292 | 307 |
293 ASSERT_TRUE(subprocess_got_sigsegv) << "Exit status: " << status | 308 ASSERT_TRUE(subprocess_got_sigsegv) << "Exit status: " << status |
294 << " " << details; | 309 << " " << details; |
295 | 310 |
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
327 fflush(stderr); | 342 fflush(stderr); |
328 _exit(kExitWithAssertionFailure); | 343 _exit(kExitWithAssertionFailure); |
329 } | 344 } |
330 | 345 |
331 void UnitTests::IgnoreThisTest() { | 346 void UnitTests::IgnoreThisTest() { |
332 fflush(stderr); | 347 fflush(stderr); |
333 _exit(kIgnoreThisTest); | 348 _exit(kIgnoreThisTest); |
334 } | 349 } |
335 | 350 |
336 } // namespace | 351 } // namespace |
OLD | NEW |