Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(786)

Side by Side Diff: sandbox/linux/services/syscall_wrappers.cc

Issue 1099263003: Reland: Introduce sys_sigprocmask and sys_sigaction. (patchset #4 id:80001 of https://codereview.ch… (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Fix x86_64 debug build test failure and msan bots failure. Created 5 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 // Copyright 2014 The Chromium Authors. All rights reserved. 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 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/services/syscall_wrappers.h" 5 #include "sandbox/linux/services/syscall_wrappers.h"
6 6
7 #include <pthread.h> 7 #include <pthread.h>
8 #include <sched.h> 8 #include <sched.h>
9 #include <setjmp.h> 9 #include <setjmp.h>
10 #include <sys/resource.h> 10 #include <sys/resource.h>
11 #include <sys/syscall.h> 11 #include <sys/syscall.h>
12 #include <sys/time.h> 12 #include <sys/time.h>
13 #include <sys/types.h> 13 #include <sys/types.h>
14 #include <unistd.h> 14 #include <unistd.h>
15 #include <cstring>
15 16
16 #include "base/compiler_specific.h" 17 #include "base/compiler_specific.h"
17 #include "base/logging.h" 18 #include "base/logging.h"
18 #include "base/third_party/valgrind/valgrind.h" 19 #include "base/third_party/valgrind/valgrind.h"
19 #include "build/build_config.h" 20 #include "build/build_config.h"
20 #include "sandbox/linux/system_headers/capability.h" 21 #include "sandbox/linux/system_headers/capability.h"
22 #include "sandbox/linux/system_headers/linux_signal.h"
21 #include "sandbox/linux/system_headers/linux_syscalls.h" 23 #include "sandbox/linux/system_headers/linux_syscalls.h"
22 24
23 namespace sandbox { 25 namespace sandbox {
24 26
25 pid_t sys_getpid(void) { 27 pid_t sys_getpid(void) {
26 return syscall(__NR_getpid); 28 return syscall(__NR_getpid);
27 } 29 }
28 30
29 pid_t sys_gettid(void) { 31 pid_t sys_gettid(void) {
30 return syscall(__NR_gettid); 32 return syscall(__NR_gettid);
(...skipping 99 matching lines...) Expand 10 before | Expand all | Expand 10 after
130 } 132 }
131 133
132 int sys_chroot(const char* path) { 134 int sys_chroot(const char* path) {
133 return syscall(__NR_chroot, path); 135 return syscall(__NR_chroot, path);
134 } 136 }
135 137
136 int sys_unshare(int flags) { 138 int sys_unshare(int flags) {
137 return syscall(__NR_unshare, flags); 139 return syscall(__NR_unshare, flags);
138 } 140 }
139 141
142 int sys_sigprocmask(int how, const sigset_t* set, decltype(nullptr) oldset) {
143 // In some toolchain (in particular Android and PNaCl toolchain),
144 // sigset_t is 32 bits, but Linux ABI requires 64 bits.
145 uint64_t linux_value = 0;
146 std::memcpy(&linux_value, set, std::min(sizeof(sigset_t), sizeof(uint64_t)));
147 return syscall(__NR_rt_sigprocmask, how, &linux_value, nullptr,
148 sizeof(linux_value));
149 }
150
151 #if defined(MEMORY_SANITIZER)
hidehiko 2015/04/23 15:59:56 Note: I'll add (|| !defined(OS_NACL_NONSFI)) in a
mdempsky 2015/04/23 19:39:36 Is it an option to just add a #if defined(OS_NA
hidehiko 2015/04/24 17:38:38 I think we can use ifdef as you said here. Please
mdempsky 2015/04/24 18:03:02 Sure.
152 // If MEMORY_SANITIZER is enabled, it is necessary to call sigaction() here,
153 // rather than the direct syscall (sys_sigaction() defined by ourselves).
154 // It is because, if MEMORY_SANITIZER is enabled, sigaction is wrapped, and
155 // |act->sa_handler| is injected in order to unpoisonize the memory passed via
156 // callback's arguments. Please see msan_interceptors.cc for more details.
157 // So, if the direct syscall is used, as MEMORY_SANITIZER does not know about
158 // it, sigaction() invocation in other places would be broken (in more precise,
159 // returned |oldact| would have a broken |sa_handler| callback).
160 // Practically, it would break NaCl's signal handler installation.
161 // cf) native_client/src/trusted/service_runtime/linux/nacl_signal.c.
162 int sys_sigaction(int signum,
163 const struct sigaction* act,
164 struct sigaction* oldact) {
165 return sigaction(signum, act, oldact);
166 }
167 #else
168 // struct sigaction is different ABI from the Linux's.
169 struct KernelSigAction {
170 void (*kernel_handler)(int);
171 uint32_t sa_flags;
172 void (*sa_restorer)(void);
173 uint64_t sa_mask;
174 };
175
176 // On X86_64 arch, it is necessary to set sa_restorer always.
177 #if defined(ARCH_CPU_X86_64)
178 #if !defined(SA_RESTORER)
179 #define SA_RESTORER 0x04000000
180 #endif
181
182 // rt_sigreturn is a special system call that interacts with the user land
183 // stack. Thus, here prologue must not be created, which implies syscall()
184 // does not work properly, too. Note that rt_sigreturn will never return.
185 static __attribute__((naked)) void sys_rt_sigreturn() {
mdempsky 2015/04/23 19:39:36 Hm, the GCC manual says "This attribute is availab
hidehiko 2015/04/24 17:38:38 Good catch! GCC does not support naked, but clang
186 // Just invoke rt_sigreturn system call.
187 asm volatile ("syscall\n"
188 :: "a"(__NR_rt_sigreturn));
189 }
190 #endif
191
192 int sys_sigaction(int signum,
193 const struct sigaction* act,
194 struct sigaction* oldact) {
195 KernelSigAction kernel_act = {};
196 if (act) {
197 kernel_act.kernel_handler = act->sa_handler;
198 std::memcpy(&kernel_act.sa_mask, &act->sa_mask,
199 std::min(sizeof(kernel_act.sa_mask), sizeof(act->sa_mask)));
200 kernel_act.sa_flags = act->sa_flags;
201
202 #if defined(ARCH_CPU_X86_64)
203 if (!(kernel_act.sa_flags & SA_RESTORER)) {
204 kernel_act.sa_flags |= SA_RESTORER;
205 kernel_act.sa_restorer = sys_rt_sigreturn;
206 }
207 #endif
208 }
209
210 KernelSigAction kernel_oldact = {};
211 int result = syscall(__NR_rt_sigaction, signum, act ? &kernel_act : nullptr,
212 oldact ? &kernel_oldact : nullptr, sizeof(uint64_t));
213 if (result == 0 && oldact) {
214 oldact->sa_handler = kernel_oldact.kernel_handler;
215 sigemptyset(&oldact->sa_mask);
216 std::memcpy(&oldact->sa_mask, &kernel_oldact.sa_mask,
217 std::min(sizeof(kernel_act.sa_mask), sizeof(act->sa_mask)));
218 oldact->sa_flags = kernel_oldact.sa_flags;
219 }
220 return result;
221 }
222
223 #endif // defined(MEMORY_SANITIZER)
224
140 } // namespace sandbox 225 } // namespace sandbox
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698