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/syscall_wrappers.h" | 5 #include "sandbox/linux/services/syscall_wrappers.h" |
6 | 6 |
7 #include <sys/syscall.h> | 7 #include <sys/syscall.h> |
8 #include <sys/types.h> | 8 #include <sys/types.h> |
9 #include <sys/wait.h> | 9 #include <sys/wait.h> |
10 #include <unistd.h> | 10 #include <unistd.h> |
11 | 11 |
12 #include "base/logging.h" | 12 #include "base/logging.h" |
13 #include "base/posix/eintr_wrapper.h" | 13 #include "base/posix/eintr_wrapper.h" |
| 14 #include "base/third_party/valgrind/valgrind.h" |
14 #include "build/build_config.h" | 15 #include "build/build_config.h" |
15 #include "sandbox/linux/tests/test_utils.h" | 16 #include "sandbox/linux/tests/test_utils.h" |
| 17 #include "sandbox/linux/tests/unit_tests.h" |
16 #include "testing/gtest/include/gtest/gtest.h" | 18 #include "testing/gtest/include/gtest/gtest.h" |
17 | 19 |
18 namespace sandbox { | 20 namespace sandbox { |
19 | 21 |
20 namespace { | 22 namespace { |
21 | 23 |
22 TEST(SyscallWrappers, BasicSyscalls) { | 24 TEST(SyscallWrappers, BasicSyscalls) { |
23 EXPECT_EQ(getpid(), sys_getpid()); | 25 EXPECT_EQ(getpid(), sys_getpid()); |
24 } | 26 } |
25 | 27 |
(...skipping 25 matching lines...) Expand all Loading... |
51 _exit(1); | 53 _exit(1); |
52 } | 54 } |
53 | 55 |
54 ASSERT_NE(-1, pid); | 56 ASSERT_NE(-1, pid); |
55 int status = 0; | 57 int status = 0; |
56 ASSERT_EQ(pid, HANDLE_EINTR(waitpid(pid, &status, 0))); | 58 ASSERT_EQ(pid, HANDLE_EINTR(waitpid(pid, &status, 0))); |
57 ASSERT_TRUE(WIFEXITED(status)); | 59 ASSERT_TRUE(WIFEXITED(status)); |
58 EXPECT_EQ(kSuccessExit, WEXITSTATUS(status)); | 60 EXPECT_EQ(kSuccessExit, WEXITSTATUS(status)); |
59 } | 61 } |
60 | 62 |
| 63 TEST(SyscallWrappers, ForkWithFlagsUpdatesPidCache) { |
| 64 // The libc clone function, which allows ForkWithFlags to keep the pid cache |
| 65 // up to date, does not work on Valgrind. |
| 66 if (IsRunningOnValgrind()) { |
| 67 return; |
| 68 } |
| 69 |
| 70 // Warm up the libc pid cache, if there is one. |
| 71 ASSERT_EQ(sys_getpid(), getpid()); |
| 72 |
| 73 pid_t ctid = 0; |
| 74 pid_t pid = ForkWithFlags(CLONE_CHILD_SETTID | SIGCHLD, nullptr, &ctid); |
| 75 |
| 76 const int kSuccessExit = 0; |
| 77 if (0 == pid) { |
| 78 // In child. Check both the raw getpid syscall and the libc getpid wrapper |
| 79 // (which may rely on a pid cache). |
| 80 if (sys_getpid() == ctid && getpid() == ctid) |
| 81 _exit(kSuccessExit); |
| 82 _exit(1); |
| 83 } |
| 84 |
| 85 ASSERT_NE(-1, pid); |
| 86 int status = 0; |
| 87 ASSERT_EQ(pid, HANDLE_EINTR(waitpid(pid, &status, 0))); |
| 88 ASSERT_TRUE(WIFEXITED(status)); |
| 89 EXPECT_EQ(kSuccessExit, WEXITSTATUS(status)); |
| 90 } |
| 91 |
61 } // namespace | 92 } // namespace |
62 | 93 |
63 } // namespace sandbox | 94 } // namespace sandbox |
OLD | NEW |