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
3 // found in the LICENSE file.
5 #ifndef SANDBOX_LINUX_SECCOMP_BPF_SYSCALL_H__
6 #define SANDBOX_LINUX_SECCOMP_BPF_SYSCALL_H__
10 #include "base/macros.h"
11 #include "sandbox/sandbox_export.h"
15 // This purely static class can be used to perform system calls with some
17 class SANDBOX_EXPORT Syscall
{
19 // This performs system call |nr| with the arguments p0 to p5 from a constant
20 // userland address, which is for instance observable by seccomp-bpf filters.
21 // The constant userland address from which these system calls are made will
22 // be returned if |nr| is passed as -1.
23 // On error, this function will return a value between -1 and -4095 which
24 // should be interpreted as -errno.
25 static intptr_t Call(int nr
,
33 // System calls can take up to six parameters. Traditionally, glibc
34 // implements this property by using variadic argument lists. This works, but
35 // confuses modern tools such as valgrind, because we are nominally passing
36 // uninitialized data whenever we call through this function and pass less
37 // than the full six arguments.
38 // So, instead, we use C++'s template system to achieve a very similar
39 // effect. C++ automatically sets the unused parameters to zero for us, and
40 // it also does the correct type expansion (e.g. from 32bit to 64bit) where
42 // We have to use C-style cast operators as we want to be able to accept both
43 // integer and pointer types.
44 template <class T0
, class T1
, class T2
, class T3
, class T4
, class T5
>
45 static inline intptr_t
46 Call(int nr
, T0 p0
, T1 p1
, T2 p2
, T3 p3
, T4 p4
, T5 p5
) {
56 template <class T0
, class T1
, class T2
, class T3
, class T4
>
57 static inline intptr_t Call(int nr
, T0 p0
, T1 p1
, T2 p2
, T3 p3
, T4 p4
) {
58 return Call(nr
, p0
, p1
, p2
, p3
, p4
, 0);
61 template <class T0
, class T1
, class T2
, class T3
>
62 static inline intptr_t Call(int nr
, T0 p0
, T1 p1
, T2 p2
, T3 p3
) {
63 return Call(nr
, p0
, p1
, p2
, p3
, 0, 0);
66 template <class T0
, class T1
, class T2
>
67 static inline intptr_t Call(int nr
, T0 p0
, T1 p1
, T2 p2
) {
68 return Call(nr
, p0
, p1
, p2
, 0, 0, 0);
71 template <class T0
, class T1
>
72 static inline intptr_t Call(int nr
, T0 p0
, T1 p1
) {
73 return Call(nr
, p0
, p1
, 0, 0, 0, 0);
77 static inline intptr_t Call(int nr
, T0 p0
) {
78 return Call(nr
, p0
, 0, 0, 0, 0, 0);
81 static inline intptr_t Call(int nr
) { return Call(nr
, 0, 0, 0, 0, 0, 0); }
84 DISALLOW_IMPLICIT_CONSTRUCTORS(Syscall
);
87 } // namespace sandbox
89 #endif // SANDBOX_LINUX_SECCOMP_BPF_SYSCALL_H__