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 "sandbox/linux/seccomp-bpf/syscall.h" | 5 #include "sandbox/linux/seccomp-bpf/syscall.h" |
6 | 6 |
7 #include <asm/unistd.h> | 7 #include <asm/unistd.h> |
8 #include <fcntl.h> | 8 #include <fcntl.h> |
9 #include <sys/mman.h> | 9 #include <sys/mman.h> |
10 #include <sys/syscall.h> | 10 #include <sys/syscall.h> |
(...skipping 23 matching lines...) Expand all Loading... | |
34 #endif | 34 #endif |
35 | 35 |
36 TEST(Syscall, InvalidCallReturnsENOSYS) { | 36 TEST(Syscall, InvalidCallReturnsENOSYS) { |
37 EXPECT_EQ(-ENOSYS, Syscall::InvalidCall()); | 37 EXPECT_EQ(-ENOSYS, Syscall::InvalidCall()); |
38 } | 38 } |
39 | 39 |
40 TEST(Syscall, WellKnownEntryPoint) { | 40 TEST(Syscall, WellKnownEntryPoint) { |
41 // Test that Syscall::Call(-1) is handled specially. Don't do this on ARM, | 41 // Test that Syscall::Call(-1) is handled specially. Don't do this on ARM, |
42 // where syscall(-1) crashes with SIGILL. Not running the test is fine, as we | 42 // where syscall(-1) crashes with SIGILL. Not running the test is fine, as we |
43 // are still testing ARM code in the next set of tests. | 43 // are still testing ARM code in the next set of tests. |
44 #if !defined(__arm__) | 44 #if !defined(__arm__) && !defined(__aarch64__) |
45 EXPECT_NE(Syscall::Call(-1), syscall(-1)); | 45 EXPECT_NE(Syscall::Call(-1), syscall(-1)); |
46 #endif | 46 #endif |
47 | 47 |
48 // If possible, test that Syscall::Call(-1) returns the address right | 48 // If possible, test that Syscall::Call(-1) returns the address right |
49 // after | 49 // after |
50 // a kernel entry point. | 50 // a kernel entry point. |
51 #if defined(__i386__) | 51 #if defined(__i386__) |
52 EXPECT_EQ(0x80CDu, ((uint16_t*)Syscall::Call(-1))[-1]); // INT 0x80 | 52 EXPECT_EQ(0x80CDu, ((uint16_t*)Syscall::Call(-1))[-1]); // INT 0x80 |
53 #elif defined(__x86_64__) | 53 #elif defined(__x86_64__) |
54 EXPECT_EQ(0x050Fu, ((uint16_t*)Syscall::Call(-1))[-1]); // SYSCALL | 54 EXPECT_EQ(0x050Fu, ((uint16_t*)Syscall::Call(-1))[-1]); // SYSCALL |
55 #elif defined(__arm__) | 55 #elif defined(__arm__) |
56 #if defined(__thumb__) | 56 #if defined(__thumb__) |
57 EXPECT_EQ(0xDF00u, ((uint16_t*)Syscall::Call(-1))[-1]); // SWI 0 | 57 EXPECT_EQ(0xDF00u, ((uint16_t*)Syscall::Call(-1))[-1]); // SWI 0 |
58 #else | 58 #else |
59 EXPECT_EQ(0xEF000000u, ((uint32_t*)Syscall::Call(-1))[-1]); // SVC 0 | 59 EXPECT_EQ(0xEF000000u, ((uint32_t*)Syscall::Call(-1))[-1]); // SVC 0 |
60 #endif | 60 #endif |
61 #elif defined(__mips__) | 61 #elif defined(__mips__) |
62 // Opcode for MIPS sycall is in the lower 16-bits | 62 // Opcode for MIPS sycall is in the lower 16-bits |
63 EXPECT_EQ(0x0cu, (((uint32_t*)Syscall::Call(-1))[-1]) & 0x0000FFFF); | 63 EXPECT_EQ(0x0cu, (((uint32_t*)Syscall::Call(-1))[-1]) & 0x0000FFFF); |
64 #elif defined(__aarch64__) | |
65 EXPECT_EQ(0xD4000001u, ((uint32_t*)Syscall::Call(-1))[-1]); // SVC 0 | |
64 #else | 66 #else |
65 #warning Incomplete test case; need port for target platform | 67 #warning Incomplete test case; need port for target platform |
66 #endif | 68 #endif |
67 } | 69 } |
68 | 70 |
69 TEST(Syscall, TrivialSyscallNoArgs) { | 71 TEST(Syscall, TrivialSyscallNoArgs) { |
70 // Test that we can do basic system calls | 72 // Test that we can do basic system calls |
71 EXPECT_EQ(Syscall::Call(__NR_getpid), syscall(__NR_getpid)); | 73 EXPECT_EQ(Syscall::Call(__NR_getpid), syscall(__NR_getpid)); |
72 } | 74 } |
73 | 75 |
(...skipping 72 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
146 BPF_ASSERT((*BPF_AUX)[0] == static_cast<uint64_t>(syscall_args[0])); | 148 BPF_ASSERT((*BPF_AUX)[0] == static_cast<uint64_t>(syscall_args[0])); |
147 BPF_ASSERT((*BPF_AUX)[1] == static_cast<uint64_t>(syscall_args[1])); | 149 BPF_ASSERT((*BPF_AUX)[1] == static_cast<uint64_t>(syscall_args[1])); |
148 BPF_ASSERT((*BPF_AUX)[2] == static_cast<uint64_t>(syscall_args[2])); | 150 BPF_ASSERT((*BPF_AUX)[2] == static_cast<uint64_t>(syscall_args[2])); |
149 BPF_ASSERT((*BPF_AUX)[3] == static_cast<uint64_t>(syscall_args[3])); | 151 BPF_ASSERT((*BPF_AUX)[3] == static_cast<uint64_t>(syscall_args[3])); |
150 BPF_ASSERT((*BPF_AUX)[4] == static_cast<uint64_t>(syscall_args[4])); | 152 BPF_ASSERT((*BPF_AUX)[4] == static_cast<uint64_t>(syscall_args[4])); |
151 BPF_ASSERT((*BPF_AUX)[5] == static_cast<uint64_t>(syscall_args[5])); | 153 BPF_ASSERT((*BPF_AUX)[5] == static_cast<uint64_t>(syscall_args[5])); |
152 } | 154 } |
153 | 155 |
154 TEST(Syscall, ComplexSyscallSixArgs) { | 156 TEST(Syscall, ComplexSyscallSixArgs) { |
155 int fd; | 157 int fd; |
158 #if defined(__aarch64__) | |
159 ASSERT_LE(0, | |
160 fd = Syscall::Call(__NR_openat, AT_FDCWD, "/dev/null", O_RDWR, 0L)); | |
jln (very slow on Chromium)
2014/08/21 19:04:53
Could we use openat everywhere instead? It should
leecam
2014/08/22 10:44:18
Done.
| |
161 #else | |
156 ASSERT_LE(0, fd = Syscall::Call(__NR_open, "/dev/null", O_RDWR, 0L)); | 162 ASSERT_LE(0, fd = Syscall::Call(__NR_open, "/dev/null", O_RDWR, 0L)); |
157 | 163 #endif |
158 // Use mmap() to allocate some read-only memory | 164 // Use mmap() to allocate some read-only memory |
159 char* addr0; | 165 char* addr0; |
160 ASSERT_NE( | 166 ASSERT_NE( |
161 (char*)NULL, | 167 (char*)NULL, |
162 addr0 = reinterpret_cast<char*>(Syscall::Call(kMMapNr, | 168 addr0 = reinterpret_cast<char*>(Syscall::Call(kMMapNr, |
163 (void*)NULL, | 169 (void*)NULL, |
164 4096, | 170 4096, |
165 PROT_READ, | 171 PROT_READ, |
166 MAP_PRIVATE | MAP_ANONYMOUS, | 172 MAP_PRIVATE | MAP_ANONYMOUS, |
167 fd, | 173 fd, |
(...skipping 11 matching lines...) Expand all Loading... | |
179 fd, | 185 fd, |
180 0L))); | 186 0L))); |
181 ++*addr1; // This should not seg fault | 187 ++*addr1; // This should not seg fault |
182 | 188 |
183 // Clean up | 189 // Clean up |
184 EXPECT_EQ(0, Syscall::Call(__NR_munmap, addr1, 4096L)); | 190 EXPECT_EQ(0, Syscall::Call(__NR_munmap, addr1, 4096L)); |
185 EXPECT_EQ(0, IGNORE_EINTR(Syscall::Call(__NR_close, fd))); | 191 EXPECT_EQ(0, IGNORE_EINTR(Syscall::Call(__NR_close, fd))); |
186 | 192 |
187 // Check that the offset argument (i.e. the sixth argument) is processed | 193 // Check that the offset argument (i.e. the sixth argument) is processed |
188 // correctly. | 194 // correctly. |
195 #if defined(__aarch64__) | |
jln (very slow on Chromium)
2014/08/21 19:04:53
Same remark.
leecam
2014/08/22 10:44:18
Done.
| |
196 ASSERT_GE( | |
197 fd = Syscall::Call(__NR_openat, AT_FDCWD, "/proc/self/exe", O_RDONLY, 0L), | |
198 0); | |
199 #else | |
189 ASSERT_GE(fd = Syscall::Call(__NR_open, "/proc/self/exe", O_RDONLY, 0L), 0); | 200 ASSERT_GE(fd = Syscall::Call(__NR_open, "/proc/self/exe", O_RDONLY, 0L), 0); |
201 #endif | |
190 char* addr2, *addr3; | 202 char* addr2, *addr3; |
191 ASSERT_NE((char*)NULL, | 203 ASSERT_NE((char*)NULL, |
192 addr2 = reinterpret_cast<char*>(Syscall::Call( | 204 addr2 = reinterpret_cast<char*>(Syscall::Call( |
193 kMMapNr, (void*)NULL, 8192L, PROT_READ, MAP_PRIVATE, fd, 0L))); | 205 kMMapNr, (void*)NULL, 8192L, PROT_READ, MAP_PRIVATE, fd, 0L))); |
194 ASSERT_NE((char*)NULL, | 206 ASSERT_NE((char*)NULL, |
195 addr3 = reinterpret_cast<char*>(Syscall::Call(kMMapNr, | 207 addr3 = reinterpret_cast<char*>(Syscall::Call(kMMapNr, |
196 (void*)NULL, | 208 (void*)NULL, |
197 4096L, | 209 4096L, |
198 PROT_READ, | 210 PROT_READ, |
199 MAP_PRIVATE, | 211 MAP_PRIVATE, |
(...skipping 14 matching lines...) Expand all Loading... | |
214 | 226 |
215 // Clean up | 227 // Clean up |
216 EXPECT_EQ(0, Syscall::Call(__NR_munmap, addr2, 8192L)); | 228 EXPECT_EQ(0, Syscall::Call(__NR_munmap, addr2, 8192L)); |
217 EXPECT_EQ(0, Syscall::Call(__NR_munmap, addr3, 4096L)); | 229 EXPECT_EQ(0, Syscall::Call(__NR_munmap, addr3, 4096L)); |
218 EXPECT_EQ(0, IGNORE_EINTR(Syscall::Call(__NR_close, fd))); | 230 EXPECT_EQ(0, IGNORE_EINTR(Syscall::Call(__NR_close, fd))); |
219 } | 231 } |
220 | 232 |
221 } // namespace | 233 } // namespace |
222 | 234 |
223 } // namespace sandbox | 235 } // namespace sandbox |
OLD | NEW |