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> | |
12 #include <sys/time.h> | 10 #include <sys/time.h> |
13 #include <time.h> | 11 #include <time.h> |
14 #include <unistd.h> | 12 #include <unistd.h> |
15 | 13 |
16 #include "base/debug/leak_annotations.h" | 14 #include "base/debug/leak_annotations.h" |
17 #include "base/files/file_util.h" | 15 #include "base/files/file_util.h" |
18 #include "base/posix/eintr_wrapper.h" | 16 #include "base/posix/eintr_wrapper.h" |
19 #include "base/third_party/valgrind/valgrind.h" | 17 #include "base/third_party/valgrind/valgrind.h" |
20 #include "build/build_config.h" | 18 #include "build/build_config.h" |
21 #include "sandbox/linux/tests/unit_tests.h" | 19 #include "sandbox/linux/tests/unit_tests.h" |
22 | 20 |
23 // Specifically, PNaCl toolchain does not have this flag. | |
24 #if !defined(POLLRDHUP) | |
25 #define POLLRDHUP 0x2000 | |
26 #endif | |
27 | |
28 namespace { | 21 namespace { |
29 std::string TestFailedMessage(const std::string& msg) { | 22 std::string TestFailedMessage(const std::string& msg) { |
30 return msg.empty() ? std::string() : "Actual test failure: " + msg; | 23 return msg.empty() ? std::string() : "Actual test failure: " + msg; |
31 } | 24 } |
32 | 25 |
33 int GetSubProcessTimeoutTimeInSeconds() { | 26 int GetSubProcessTimeoutTimeInSeconds() { |
34 // 10s ought to be enough for anybody. | 27 // 10s ought to be enough for anybody. |
35 return 10; | 28 return 10; |
36 } | 29 } |
37 | 30 |
(...skipping 29 matching lines...) Expand all Loading... |
67 #endif | 60 #endif |
68 } | 61 } |
69 | 62 |
70 // TODO(jln): figure out why base/.../dynamic_annotations.h's | 63 // TODO(jln): figure out why base/.../dynamic_annotations.h's |
71 // RunningOnValgrind() cannot link. | 64 // RunningOnValgrind() cannot link. |
72 bool IsRunningOnValgrind() { return RUNNING_ON_VALGRIND; } | 65 bool IsRunningOnValgrind() { return RUNNING_ON_VALGRIND; } |
73 | 66 |
74 static const int kExpectedValue = 42; | 67 static const int kExpectedValue = 42; |
75 static const int kIgnoreThisTest = 43; | 68 static const int kIgnoreThisTest = 43; |
76 static const int kExitWithAssertionFailure = 1; | 69 static const int kExitWithAssertionFailure = 1; |
77 #if !defined(OS_NACL_NONSFI) | |
78 static const int kExitForTimeout = 2; | 70 static const int kExitForTimeout = 2; |
79 #endif | |
80 | 71 |
81 #if defined(SANDBOX_USES_BASE_TEST_SUITE) | 72 #if defined(SANDBOX_USES_BASE_TEST_SUITE) |
82 // This is due to StackDumpSignalHandler() performing _exit(1). | 73 // This is due to StackDumpSignalHandler() performing _exit(1). |
83 // TODO(jln): get rid of the collision with kExitWithAssertionFailure. | 74 // TODO(jln): get rid of the collision with kExitWithAssertionFailure. |
84 const int kExitAfterSIGSEGV = 1; | 75 const int kExitAfterSIGSEGV = 1; |
85 #endif | 76 #endif |
86 | 77 |
87 // PNaCl toolchain's signal ABIs are incompatible with Linux's. | |
88 // So, for simplicity, just drop the "timeout" feature from unittest framework | |
89 // with relying on the buildbot's timeout feature. | |
90 #if !defined(OS_NACL_NONSFI) | |
91 static void SigAlrmHandler(int) { | 78 static void SigAlrmHandler(int) { |
92 const char failure_message[] = "Timeout reached!\n"; | 79 const char failure_message[] = "Timeout reached!\n"; |
93 // Make sure that we never block here. | 80 // Make sure that we never block here. |
94 if (!fcntl(2, F_SETFL, O_NONBLOCK)) { | 81 if (!fcntl(2, F_SETFL, O_NONBLOCK)) { |
95 ignore_result(write(2, failure_message, sizeof(failure_message) - 1)); | 82 ignore_result(write(2, failure_message, sizeof(failure_message) - 1)); |
96 } | 83 } |
97 _exit(kExitForTimeout); | 84 _exit(kExitForTimeout); |
98 } | 85 } |
99 | 86 |
100 // Set a timeout with a handler that will automatically fail the | 87 // Set a timeout with a handler that will automatically fail the |
(...skipping 11 matching lines...) Expand all Loading... |
112 // is expecting to handle SIGALRM. | 99 // is expecting to handle SIGALRM. |
113 SANDBOX_ASSERT((old_act.sa_flags & SA_SIGINFO) == 0); | 100 SANDBOX_ASSERT((old_act.sa_flags & SA_SIGINFO) == 0); |
114 SANDBOX_ASSERT(old_act.sa_handler == SIG_DFL); | 101 SANDBOX_ASSERT(old_act.sa_handler == SIG_DFL); |
115 sigset_t sigalrm_set; | 102 sigset_t sigalrm_set; |
116 SANDBOX_ASSERT(sigemptyset(&sigalrm_set) == 0); | 103 SANDBOX_ASSERT(sigemptyset(&sigalrm_set) == 0); |
117 SANDBOX_ASSERT(sigaddset(&sigalrm_set, SIGALRM) == 0); | 104 SANDBOX_ASSERT(sigaddset(&sigalrm_set, SIGALRM) == 0); |
118 SANDBOX_ASSERT(sigprocmask(SIG_UNBLOCK, &sigalrm_set, NULL) == 0); | 105 SANDBOX_ASSERT(sigprocmask(SIG_UNBLOCK, &sigalrm_set, NULL) == 0); |
119 SANDBOX_ASSERT(alarm(time_in_seconds) == 0); // There should be no previous | 106 SANDBOX_ASSERT(alarm(time_in_seconds) == 0); // There should be no previous |
120 // alarm. | 107 // alarm. |
121 } | 108 } |
122 #endif // !defined(OS_NACL_NONSFI) | |
123 | 109 |
124 // Runs a test in a sub-process. This is necessary for most of the code | 110 // Runs a test in a sub-process. This is necessary for most of the code |
125 // in the BPF sandbox, as it potentially makes global state changes and as | 111 // in the BPF sandbox, as it potentially makes global state changes and as |
126 // it also tends to raise fatal errors, if the code has been used in an | 112 // it also tends to raise fatal errors, if the code has been used in an |
127 // insecure manner. | 113 // insecure manner. |
128 void UnitTests::RunTestInProcess(SandboxTestRunner* test_runner, | 114 void UnitTests::RunTestInProcess(SandboxTestRunner* test_runner, |
129 DeathCheck death, | 115 DeathCheck death, |
130 const void* death_aux) { | 116 const void* death_aux) { |
131 CHECK(test_runner); | 117 CHECK(test_runner); |
132 // We need to fork(), so we can't be multi-threaded, as threads could hold | 118 // 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... |
170 // In child process | 156 // In child process |
171 // Redirect stderr to our pipe. This way, we can capture all error | 157 // Redirect stderr to our pipe. This way, we can capture all error |
172 // messages, if we decide we want to do so in our tests. | 158 // messages, if we decide we want to do so in our tests. |
173 SANDBOX_ASSERT(dup2(fds[1], 2) == 2); | 159 SANDBOX_ASSERT(dup2(fds[1], 2) == 2); |
174 SANDBOX_ASSERT(!close(fds[0])); | 160 SANDBOX_ASSERT(!close(fds[0])); |
175 SANDBOX_ASSERT(!close(fds[1])); | 161 SANDBOX_ASSERT(!close(fds[1])); |
176 | 162 |
177 // Don't set a timeout if running on Valgrind, since it's generally much | 163 // Don't set a timeout if running on Valgrind, since it's generally much |
178 // slower. | 164 // slower. |
179 if (!IsRunningOnValgrind()) { | 165 if (!IsRunningOnValgrind()) { |
180 #if !defined(OS_NACL_NONSFI) | |
181 SetProcessTimeout(GetSubProcessTimeoutTimeInSeconds()); | 166 SetProcessTimeout(GetSubProcessTimeoutTimeInSeconds()); |
182 #endif | |
183 } | 167 } |
184 | 168 |
185 // Disable core files. They are not very useful for our individual test | 169 // Disable core files. They are not very useful for our individual test |
186 // cases. | 170 // cases. |
187 struct rlimit no_core = {0}; | 171 struct rlimit no_core = {0}; |
188 setrlimit(RLIMIT_CORE, &no_core); | 172 setrlimit(RLIMIT_CORE, &no_core); |
189 | 173 |
190 test_runner->Run(); | 174 test_runner->Run(); |
191 if (test_runner->ShouldCheckForLeaks()) { | 175 if (test_runner->ShouldCheckForLeaks()) { |
192 #if defined(LEAK_SANITIZER) | 176 #if defined(LEAK_SANITIZER) |
(...skipping 152 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
345 fflush(stderr); | 329 fflush(stderr); |
346 _exit(kExitWithAssertionFailure); | 330 _exit(kExitWithAssertionFailure); |
347 } | 331 } |
348 | 332 |
349 void UnitTests::IgnoreThisTest() { | 333 void UnitTests::IgnoreThisTest() { |
350 fflush(stderr); | 334 fflush(stderr); |
351 _exit(kIgnoreThisTest); | 335 _exit(kIgnoreThisTest); |
352 } | 336 } |
353 | 337 |
354 } // namespace | 338 } // namespace |
OLD | NEW |