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