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
3 // found in the LICENSE file.
5 #include "sandbox/linux/services/syscall_wrappers.h"
7 #include <sys/syscall.h>
13 #include "base/logging.h"
14 #include "base/posix/eintr_wrapper.h"
15 #include "base/third_party/valgrind/valgrind.h"
16 #include "build/build_config.h"
17 #include "sandbox/linux/system_headers/linux_signal.h"
18 #include "sandbox/linux/tests/test_utils.h"
19 #include "sandbox/linux/tests/unit_tests.h"
20 #include "testing/gtest/include/gtest/gtest.h"
26 TEST(SyscallWrappers
, BasicSyscalls
) {
27 EXPECT_EQ(getpid(), sys_getpid());
30 TEST(SyscallWrappers
, CloneBasic
) {
31 pid_t child
= sys_clone(SIGCHLD
);
32 TestUtils::HandlePostForkReturn(child
);
36 TEST(SyscallWrappers
, CloneParentSettid
) {
38 pid_t child
= sys_clone(CLONE_PARENT_SETTID
| SIGCHLD
, nullptr, &ptid
,
40 TestUtils::HandlePostForkReturn(child
);
42 EXPECT_EQ(child
, ptid
);
45 TEST(SyscallWrappers
, CloneChildSettid
) {
48 sys_clone(CLONE_CHILD_SETTID
| SIGCHLD
, nullptr, nullptr, &ctid
, nullptr);
50 const int kSuccessExit
= 0;
53 if (sys_getpid() == ctid
)
60 ASSERT_EQ(pid
, HANDLE_EINTR(waitpid(pid
, &status
, 0)));
61 ASSERT_TRUE(WIFEXITED(status
));
62 EXPECT_EQ(kSuccessExit
, WEXITSTATUS(status
));
65 TEST(SyscallWrappers
, GetRESUid
) {
66 uid_t ruid
, euid
, suid
;
67 uid_t sys_ruid
, sys_euid
, sys_suid
;
68 ASSERT_EQ(0, getresuid(&ruid
, &euid
, &suid
));
69 ASSERT_EQ(0, sys_getresuid(&sys_ruid
, &sys_euid
, &sys_suid
));
70 EXPECT_EQ(ruid
, sys_ruid
);
71 EXPECT_EQ(euid
, sys_euid
);
72 EXPECT_EQ(suid
, sys_suid
);
75 TEST(SyscallWrappers
, GetRESGid
) {
76 gid_t rgid
, egid
, sgid
;
77 gid_t sys_rgid
, sys_egid
, sys_sgid
;
78 ASSERT_EQ(0, getresgid(&rgid
, &egid
, &sgid
));
79 ASSERT_EQ(0, sys_getresgid(&sys_rgid
, &sys_egid
, &sys_sgid
));
80 EXPECT_EQ(rgid
, sys_rgid
);
81 EXPECT_EQ(egid
, sys_egid
);
82 EXPECT_EQ(sgid
, sys_sgid
);
85 TEST(SyscallWrappers
, LinuxSigSet
) {
87 ASSERT_EQ(0, sigemptyset(&sigset
));
88 ASSERT_EQ(0, sigaddset(&sigset
, LINUX_SIGSEGV
));
89 ASSERT_EQ(0, sigaddset(&sigset
, LINUX_SIGBUS
));
90 uint64_t linux_sigset
= 0;
91 std::memcpy(&linux_sigset
, &sigset
,
92 std::min(sizeof(sigset
), sizeof(linux_sigset
)));
93 EXPECT_EQ((1ULL << (LINUX_SIGSEGV
- 1)) | (1ULL << (LINUX_SIGBUS
- 1)),
99 } // namespace sandbox