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 "sandbox/linux/sandbox_export.h"
14 // We have to make sure that we have a single "magic" return address for
15 // our system calls, which we can check from within a BPF filter. This
16 // works by writing a little bit of asm() code that a) enters the kernel, and
17 // that also b) can be invoked in a way that computes this return address.
18 // Passing "nr" as "-1" computes the "magic" return address. Passing any
19 // other value invokes the appropriate system call.
20 SANDBOX_EXPORT intptr_t SandboxSyscall(int nr,
28 // System calls can take up to six parameters. Traditionally, glibc
29 // implements this property by using variadic argument lists. This works, but
30 // confuses modern tools such as valgrind, because we are nominally passing
31 // uninitialized data whenever we call through this function and pass less
32 // than the full six arguments.
33 // So, instead, we use C++'s template system to achieve a very similar
34 // effect. C++ automatically sets the unused parameters to zero for us, and
35 // it also does the correct type expansion (e.g. from 32bit to 64bit) where
37 // We have to use C-style cast operators as we want to be able to accept both
38 // integer and pointer types.
39 // We explicitly mark all functions as inline. This is not necessary in
40 // optimized builds, where the compiler automatically figures out that it
41 // can inline everything. But it makes stack traces of unoptimized builds
42 // easier to read as it hides implementation details.
43 #if __cplusplus >= 201103 // C++11
45 template <class T0 = intptr_t,
51 SANDBOX_EXPORT inline intptr_t SandboxSyscall(int nr,
58 __attribute__((always_inline));
60 template <class T0, class T1, class T2, class T3, class T4, class T5>
61 SANDBOX_EXPORT inline intptr_t
62 SandboxSyscall(int nr, T0 p0, T1 p1, T2 p2, T3 p3, T4 p4, T5 p5) {
63 return SandboxSyscall(nr,
74 // TODO(markus): C++11 has a much more concise and readable solution for
75 // expressing what we are doing here. Delete the fall-back code for older
76 // compilers as soon as we have fully switched to C++11
78 template <class T0, class T1, class T2, class T3, class T4, class T5>
79 SANDBOX_EXPORT inline intptr_t
80 SandboxSyscall(int nr, T0 p0, T1 p1, T2 p2, T3 p3, T4 p4, T5 p5)
81 __attribute__((always_inline));
82 template <class T0, class T1, class T2, class T3, class T4, class T5>
83 SANDBOX_EXPORT inline intptr_t
84 SandboxSyscall(int nr, T0 p0, T1 p1, T2 p2, T3 p3, T4 p4, T5 p5) {
85 return SandboxSyscall(nr,
94 template <class T0, class T1, class T2, class T3, class T4>
95 SANDBOX_EXPORT inline intptr_t
96 SandboxSyscall(int nr, T0 p0, T1 p1, T2 p2, T3 p3, T4 p4)
97 __attribute__((always_inline));
98 template <class T0, class T1, class T2, class T3, class T4>
99 SANDBOX_EXPORT inline intptr_t
100 SandboxSyscall(int nr, T0 p0, T1 p1, T2 p2, T3 p3, T4 p4) {
101 return SandboxSyscall(nr, p0, p1, p2, p3, p4, 0);
104 template <class T0, class T1, class T2, class T3>
105 SANDBOX_EXPORT inline intptr_t
106 SandboxSyscall(int nr, T0 p0, T1 p1, T2 p2, T3 p3)
107 __attribute__((always_inline));
108 template <class T0, class T1, class T2, class T3>
109 SANDBOX_EXPORT inline intptr_t
110 SandboxSyscall(int nr, T0 p0, T1 p1, T2 p2, T3 p3) {
111 return SandboxSyscall(nr, p0, p1, p2, p3, 0, 0);
114 template <class T0, class T1, class T2>
115 SANDBOX_EXPORT inline intptr_t SandboxSyscall(int nr, T0 p0, T1 p1, T2 p2)
116 __attribute__((always_inline));
117 template <class T0, class T1, class T2>
118 SANDBOX_EXPORT inline intptr_t SandboxSyscall(int nr, T0 p0, T1 p1, T2 p2) {
119 return SandboxSyscall(nr, p0, p1, p2, 0, 0, 0);
122 template <class T0, class T1>
123 SANDBOX_EXPORT inline intptr_t SandboxSyscall(int nr, T0 p0, T1 p1)
124 __attribute__((always_inline));
125 template <class T0, class T1>
126 SANDBOX_EXPORT inline intptr_t SandboxSyscall(int nr, T0 p0, T1 p1) {
127 return SandboxSyscall(nr, p0, p1, 0, 0, 0, 0);
131 SANDBOX_EXPORT inline intptr_t SandboxSyscall(int nr, T0 p0)
132 __attribute__((always_inline));
134 SANDBOX_EXPORT inline intptr_t SandboxSyscall(int nr, T0 p0) {
135 return SandboxSyscall(nr, p0, 0, 0, 0, 0, 0);
138 SANDBOX_EXPORT inline intptr_t SandboxSyscall(int nr)
139 __attribute__((always_inline));
140 SANDBOX_EXPORT inline intptr_t SandboxSyscall(int nr) {
141 return SandboxSyscall(nr, 0, 0, 0, 0, 0, 0);
146 } // namespace sandbox
148 #endif // SANDBOX_LINUX_SECCOMP_BPF_SYSCALL_H__