Upstream version 7.36.149.0
[platform/framework/web/crosswalk.git] / src / sandbox / linux / seccomp-bpf / syscall.h
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.
4
5 #ifndef SANDBOX_LINUX_SECCOMP_BPF_SYSCALL_H__
6 #define SANDBOX_LINUX_SECCOMP_BPF_SYSCALL_H__
7
8 #include <stdint.h>
9
10 #include "sandbox/linux/sandbox_export.h"
11
12 namespace sandbox {
13
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,
21                                        intptr_t p0,
22                                        intptr_t p1,
23                                        intptr_t p2,
24                                        intptr_t p3,
25                                        intptr_t p4,
26                                        intptr_t p5);
27
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
36 // necessary.
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
44
45 template <class T0 = intptr_t,
46           class T1 = intptr_t,
47           class T2 = intptr_t,
48           class T3 = intptr_t,
49           class T4 = intptr_t,
50           class T5 = intptr_t>
51 SANDBOX_EXPORT inline intptr_t SandboxSyscall(int nr,
52                                               T0 p0 = 0,
53                                               T1 p1 = 0,
54                                               T2 p2 = 0,
55                                               T3 p3 = 0,
56                                               T4 p4 = 0,
57                                               T5 p5 = 0)
58     __attribute__((always_inline));
59
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,
64                         (intptr_t)p0,
65                         (intptr_t)p1,
66                         (intptr_t)p2,
67                         (intptr_t)p3,
68                         (intptr_t)p4,
69                         (intptr_t)p5);
70 }
71
72 #else  // Pre-C++11
73
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
77
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,
86                         (intptr_t)p0,
87                         (intptr_t)p1,
88                         (intptr_t)p2,
89                         (intptr_t)p3,
90                         (intptr_t)p4,
91                         (intptr_t)p5);
92 }
93
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);
102 }
103
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);
112 }
113
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);
120 }
121
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);
128 }
129
130 template <class T0>
131 SANDBOX_EXPORT inline intptr_t SandboxSyscall(int nr, T0 p0)
132     __attribute__((always_inline));
133 template <class T0>
134 SANDBOX_EXPORT inline intptr_t SandboxSyscall(int nr, T0 p0) {
135   return SandboxSyscall(nr, p0, 0, 0, 0, 0, 0);
136 }
137
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);
142 }
143
144 #endif  // Pre-C++11
145
146 }  // namespace sandbox
147
148 #endif  // SANDBOX_LINUX_SECCOMP_BPF_SYSCALL_H__